• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23 
24 #include "fwmark_client.h"
25 #include "net_manager_constants.h"
26 #include "netnative_log_wrapper.h"
27 
28 namespace {
29 std::atomic_int g_netIdForApp(0);
30 std::atomic<const SocketDispatchType*> g_dispatch(nullptr);
31 std::atomic_bool g_hookFlag(false);
GetDispatch()32 const SocketDispatchType* GetDispatch()
33 {
34     return g_dispatch.load(std::memory_order_relaxed);
35 }
36 } // namespace
37 
HookSocket(int (* fn)(int,int,int),int domain,int type,int protocol)38 int HookSocket(int (*fn)(int, int, int), int domain, int type, int protocol)
39 {
40     int fd = -1;
41     if (fn) {
42         fd = fn(domain, type, protocol);
43     }
44 
45     if (fd < 0) {
46         NETNATIVE_LOGE("musl create socket failed, errno %{public}d", errno);
47         return fd;
48     }
49 
50     if (g_netIdForApp > 0 && (domain == AF_INET || domain == AF_INET6)) {
51         if (OHOS::nmd::FwmarkClient().BindSocket(fd, g_netIdForApp) != OHOS::NetManagerStandard::NETMANAGER_SUCCESS) {
52             NETNATIVE_LOGE("BindSocket [%{public}d] to netid [%{public}d] failed",
53                 fd, g_netIdForApp.load(std::memory_order_relaxed));
54             return -1;
55         }
56     }
57 
58     return fd;
59 }
60 
ohos_socket_hook_initialize(const SocketDispatchType * disptch,bool *,const char *)61 bool ohos_socket_hook_initialize(const SocketDispatchType* disptch, bool*, const char*)
62 {
63     g_dispatch.store(disptch);
64     g_hookFlag = true;
65     return true;
66 }
67 
ohos_socket_hook_finalize(void)68 void ohos_socket_hook_finalize(void)
69 {
70     g_hookFlag = false;
71 }
72 
ohos_socket_hook_socket(int domain,int type,int protocol)73 int ohos_socket_hook_socket(int domain, int type, int protocol)
74 {
75     g_hookFlag = false;
76     int ret = HookSocket(GetDispatch()->socket, domain, type, protocol);
77     g_hookFlag = true;
78     return ret;
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