1 #ifdef OHOS_SOCKET_HOOK_ENABLE
2 #include <unistd.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <dlfcn.h>
7 #include <errno.h>
8 #include <ctype.h>
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include "musl_socket_preinit_common.h"
13 #include "musl_log.h"
14
15 static char *__socket_hook_shared_lib = "libfwmark_client.z.so";
16 static char *__socket_hook_function_prefix = "ohos_socket_hook";
17 void* shared_lib_func[LAST_FUNC];
18 long long __ohos_socket_hook_shared_library;
19 typedef bool (*init_func_type)(const struct SocketDispatchType*, bool*, const char*);
20 typedef void (*finalize_func_type)();
21 #define MAX_SYMBOL_SIZE 1000
22
init_socket_function(void * shared_library_handler,SocketSocketType * func)23 static bool init_socket_function(void* shared_library_handler, SocketSocketType* func)
24 {
25 char symbol[MAX_SYMBOL_SIZE];
26 (void)snprintf(symbol, sizeof(symbol), "%s_%s", __socket_hook_function_prefix, "socket");
27 *func = (SocketSocketType)(dlsym(shared_library_handler, symbol));
28 if (*func == NULL) {
29 return false;
30 }
31 return true;
32 }
33
clear_socket_function()34 static void clear_socket_function()
35 {
36 memset(shared_lib_func, 0, sizeof(shared_lib_func));
37 }
38
socket_finalize()39 static void socket_finalize()
40 {
41 ((finalize_func_type)shared_lib_func[FINALIZE_FUNC])();
42 __current_dispatch = 0;
43 __socket_hook_begin_flag = false;
44 // Don't dlclose because hidumper crash
45 }
46
finish_install_ohos_socket_hooks(const char * options)47 static bool finish_install_ohos_socket_hooks(const char* options)
48 {
49 init_func_type init_func = (init_func_type)(shared_lib_func[INITIALIZE_FUNC]);
50 if (!init_func(&__libc_socket_default_dispatch, NULL, options)) {
51 MUSL_LOGI("Netsys, init_func failed.");
52 clear_socket_function();
53 return false;
54 }
55
56 int ret_value = atexit(socket_finalize);
57 if (ret_value != 0) {
58 MUSL_LOGI("Netsys, set atexit failed.");
59 }
60 return true;
61 }
62
init_socket_hook_shared_library(void * shared_library_handle)63 static bool init_socket_hook_shared_library(void* shared_library_handle)
64 {
65 static const char* names[] = {
66 "initialize",
67 "finalize",
68 "get_hook_flag",
69 "set_hook_flag",
70 };
71
72 for (int i = 0; i < LAST_FUNC; i++) {
73 char symbol[MAX_SYMBOL_SIZE];
74 (void)snprintf(symbol, sizeof(symbol), "%s_%s", __socket_hook_function_prefix, names[i]);
75 shared_lib_func[i] = dlsym(shared_library_handle, symbol);
76 if (shared_lib_func[i] == NULL) {
77 clear_socket_function();
78 return false;
79 }
80 }
81
82 if (!init_socket_function(shared_library_handle, &(__musl_libc_socket_dispatch.socket))) {
83 MUSL_LOGI("Netsys, set socket function failed.");
84 clear_socket_function();
85 return false;
86 }
87
88 return true;
89 }
90
load_socket_hook_shared_library()91 static void* load_socket_hook_shared_library()
92 {
93 void* shared_library_handle = NULL;
94
95 shared_library_handle = dlopen(__socket_hook_shared_lib, RTLD_NOW | RTLD_LOCAL);
96
97 if (shared_library_handle == NULL) {
98 MUSL_LOGI("Netsys, Unable to open shared library %s: %s.\n", __socket_hook_shared_lib, dlerror());
99 return NULL;
100 }
101
102 if (!init_socket_hook_shared_library(shared_library_handle)) {
103 dlclose(shared_library_handle);
104 shared_library_handle = NULL;
105 }
106 return shared_library_handle;
107 }
108
install_ohos_socket_hook()109 static void install_ohos_socket_hook()
110 {
111 void* shared_library_handle = (void *)__ohos_socket_hook_shared_library;
112 if (shared_library_handle != NULL && shared_library_handle != (void*)-1) {
113 MUSL_LOGI("Netsys, ohos_socket_hook_shared_library has had.");
114 return;
115 }
116
117 __current_dispatch = 0;
118 shared_library_handle = load_socket_hook_shared_library();
119 if (shared_library_handle == NULL) {
120 MUSL_LOGI("Netsys, load_socket_hook_shared_library failed.");
121 return;
122 }
123 MUSL_LOGI("Netsys, load_socket_hook_shared_library success.");
124
125 if (finish_install_ohos_socket_hooks(NULL)) {
126 MUSL_LOGI("Netsys, finish_install_ohos_socket_hooks success.");
127 __ohos_socket_hook_shared_library = (long long)shared_library_handle;
128 __current_dispatch = (long long)(&__musl_libc_socket_dispatch);
129 } else {
130 MUSL_LOGI("Netsys, finish_install_ohos_socket_hooks failed.");
131 __ohos_socket_hook_shared_library = 0;
132 dlclose((void *)shared_library_handle);
133 }
134 }
135
init_ohos_socket_hook()136 static void init_ohos_socket_hook()
137 {
138 install_ohos_socket_hook();
139 }
140
__musl_socket_initialize()141 __attribute__((constructor())) static void __musl_socket_initialize()
142 {
143 bool begin_flag = __get_socket_hook_begin_flag();
144 MUSL_LOGI("Netsys, %d begin musl_socket_initialize, flag %d.\n", getpid(), begin_flag);
145 if (!begin_flag) {
146 __socket_hook_begin_flag = true;
147 init_ohos_socket_hook();
148 }
149 }
150 #endif