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
14 static char *__socket_hook_shared_lib = "libfwmark_client.z.so";
15 static char *__socket_hook_function_prefix = "ohos_socket_hook";
16 void* shared_lib_func[LAST_FUNC];
17 long long __ohos_socket_hook_shared_library;
18 typedef bool (*init_func_type)(const struct SocketDispatchType*, bool*, const char*);
19 typedef void (*finalize_func_type)();
20 #define MAX_SYMBOL_SIZE 1000
21
init_socket_function(void * shared_library_handler,SocketSocketType * func)22 static bool init_socket_function(void* shared_library_handler, SocketSocketType* func)
23 {
24 char symbol[MAX_SYMBOL_SIZE];
25 (void)snprintf(symbol, sizeof(symbol), "%s_%s", __socket_hook_function_prefix, "socket");
26 *func = (SocketSocketType)(dlsym(shared_library_handler, symbol));
27 if (*func == NULL) {
28 return false;
29 }
30 return true;
31 }
32
clear_socket_function()33 static void clear_socket_function()
34 {
35 memset(shared_lib_func, 0, sizeof(shared_lib_func));
36 }
37
socket_finalize()38 static void socket_finalize()
39 {
40 ((finalize_func_type)shared_lib_func[FINALIZE_FUNC])();
41 __current_dispatch = 0;
42 __socket_hook_begin_flag = false;
43 // Don't dlclose because hidumper crash
44 }
45
finish_install_ohos_socket_hooks(const char * options)46 static bool finish_install_ohos_socket_hooks(const char* options)
47 {
48 init_func_type init_func = (init_func_type)(shared_lib_func[INITIALIZE_FUNC]);
49 if (!init_func(&__libc_socket_default_dispatch, NULL, options)) {
50 clear_socket_function();
51 return false;
52 }
53
54 atexit(socket_finalize);
55 return true;
56 }
57
init_socket_hook_shared_library(void * shared_library_handle)58 static bool init_socket_hook_shared_library(void* shared_library_handle)
59 {
60 static const char* names[] = {
61 "initialize",
62 "finalize",
63 "get_hook_flag",
64 "set_hook_flag",
65 };
66
67 for (int i = 0; i < LAST_FUNC; i++) {
68 char symbol[MAX_SYMBOL_SIZE];
69 (void)snprintf(symbol, sizeof(symbol), "%s_%s", __socket_hook_function_prefix, names[i]);
70 shared_lib_func[i] = dlsym(shared_library_handle, symbol);
71 if (shared_lib_func[i] == NULL) {
72 clear_socket_function();
73 return false;
74 }
75 }
76
77 if (!init_socket_function(shared_library_handle, &(__musl_libc_socket_dispatch.socket))) {
78 clear_socket_function();
79 return false;
80 }
81
82 return true;
83 }
84
load_socket_hook_shared_library()85 static void* load_socket_hook_shared_library()
86 {
87 void* shared_library_handle = NULL;
88
89 shared_library_handle = dlopen(__socket_hook_shared_lib, RTLD_NOW | RTLD_LOCAL);
90
91 if (shared_library_handle == NULL) {
92 return NULL;
93 }
94
95 if (!init_socket_hook_shared_library(shared_library_handle)) {
96 dlclose(shared_library_handle);
97 shared_library_handle = NULL;
98 }
99 return shared_library_handle;
100 }
101
install_ohos_socket_hook()102 static void install_ohos_socket_hook()
103 {
104 void* shared_library_handle = (void *)__ohos_socket_hook_shared_library;
105 if (shared_library_handle != NULL && shared_library_handle != (void*)-1) {
106 return;
107 }
108
109 __current_dispatch = 0;
110 shared_library_handle = load_socket_hook_shared_library();
111 if (shared_library_handle == NULL) {
112 return;
113 }
114
115 if (finish_install_ohos_socket_hooks(NULL)) {
116 __ohos_socket_hook_shared_library = (long long)shared_library_handle;
117 __current_dispatch = (long long)(&__musl_libc_socket_dispatch);
118 } else {
119 __ohos_socket_hook_shared_library = 0;
120 dlclose((void *)shared_library_handle);
121 }
122 }
123
init_ohos_socket_hook()124 static void init_ohos_socket_hook()
125 {
126 install_ohos_socket_hook();
127 }
128
__musl_socket_initialize()129 __attribute__((constructor())) static void __musl_socket_initialize()
130 {
131 bool begin_flag = __get_socket_hook_begin_flag();
132 if (!begin_flag) {
133 __socket_hook_begin_flag = true;
134 init_ohos_socket_hook();
135 }
136 }
137 #endif