1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "netsys_sock_client.h"
17 
18 #include <cerrno>
19 #include <atomic>
20 #include <mutex>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <unistd.h>
24 
25 #include "fwmark_client.h"
26 #include "net_manager_constants.h"
27 #include "netnative_log_wrapper.h"
28 
29 namespace {
30 std::atomic_int g_netIdForApp(0);
31 std::atomic<const SocketDispatchType*> g_dispatch(nullptr);
32 std::atomic_bool g_hookFlag(false);
33 std::once_flag g_onceFlag;
GetDispatch()34 const SocketDispatchType* GetDispatch()
35 {
36     return g_dispatch.load(std::memory_order_relaxed);
37 }
38 } // namespace
39 
HookSocket(int (* fn)(int,int,int),int domain,int type,int protocol)40 int HookSocket(int (*fn)(int, int, int), int domain, int type, int protocol)
41 {
42     int fd = -1;
43     if (fn) {
44         fd = fn(domain, type, protocol);
45     }
46 
47     if (fd < 0) {
48         return fd;
49     }
50 
51     if (g_netIdForApp > 0 && (domain == AF_INET || domain == AF_INET6)) {
52         if (OHOS::nmd::FwmarkClient().BindSocket(fd, g_netIdForApp) != OHOS::NetManagerStandard::NETMANAGER_SUCCESS) {
53             NETNATIVE_LOGE("BindSocket [%{public}d] to netid [%{public}d] failed",
54                 fd, g_netIdForApp.load(std::memory_order_relaxed));
55             return -1;
56         }
57     }
58 
59     return fd;
60 }
61 
ohos_socket_hook_initialize(const SocketDispatchType * disptch,bool *,const char *)62 bool ohos_socket_hook_initialize(const SocketDispatchType* disptch, bool*, const char*)
63 {
64     std::call_once(g_onceFlag, [&]() {
65         g_dispatch.store(disptch);
66         g_hookFlag = true;
67     });
68     return true;
69 }
70 
ohos_socket_hook_finalize(void)71 void ohos_socket_hook_finalize(void)
72 {
73     g_hookFlag = false;
74 }
75 
ohos_socket_hook_socket(int domain,int type,int protocol)76 int ohos_socket_hook_socket(int domain, int type, int protocol)
77 {
78     return HookSocket(GetDispatch()->socket, domain, type, protocol);
79 }
80 
ohos_socket_hook_get_hook_flag(void)81 bool ohos_socket_hook_get_hook_flag(void)
82 {
83     return g_hookFlag;
84 }
85 
ohos_socket_hook_set_hook_flag(bool flag)86 bool ohos_socket_hook_set_hook_flag(bool flag)
87 {
88     g_hookFlag = flag;
89     return true;
90 }
91 
SetNetForApp(int netId)92 void SetNetForApp(int netId)
93 {
94     g_netIdForApp = netId;
95 }
96 
GetNetForApp()97 int GetNetForApp()
98 {
99     return g_netIdForApp;
100 }
101