1 #ifndef _MUSL_SOCKET_PREINIT_COMMON_H 2 #define _MUSL_SOCKET_PREINIT_COMMON_H 3 4 #include <stdio.h> 5 #include "musl_socket_dispatch.h" 6 #include "common_def.h" 7 8 extern struct SocketDispatchType __musl_libc_socket_dispatch; 9 extern struct SocketDispatchType __libc_socket_default_dispatch; 10 11 enum SocketFuncEnum { 12 INITIALIZE_FUNC, 13 FINALIZE_FUNC, 14 GET_HOOK_FLAG_FUNC, 15 SET_HOOK_FLAG_FUNC, 16 LAST_FUNC, 17 }; 18 19 #ifdef OHOS_SOCKET_HOOK_ENABLE 20 extern long long __current_dispatch; 21 extern bool __socket_hook_begin_flag; 22 extern long long __ohos_socket_hook_shared_library; 23 extern void* shared_lib_func[LAST_FUNC]; 24 #endif 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 __attribute__((always_inline)) __get_socket_hook_begin_flag()31inline bool __get_socket_hook_begin_flag() 32 { 33 #ifdef OHOS_SOCKET_HOOK_ENABLE 34 return __socket_hook_begin_flag; 35 #else 36 return false; 37 #endif 38 } 39 40 __attribute__((always_inline)) __get_socket_hook_flag()41inline bool __get_socket_hook_flag() 42 { 43 #ifdef OHOS_SOCKET_HOOK_ENABLE 44 void* handle = (void *)__ohos_socket_hook_shared_library; 45 if (handle == NULL) { 46 return false; 47 } else if (handle == (void *)-1) { 48 return true; 49 } else { 50 SocketGetHookFlagType get_hook_func_ptr = (SocketGetHookFlagType)(shared_lib_func[GET_HOOK_FLAG_FUNC]); 51 bool flag = get_hook_func_ptr(); 52 return flag; 53 } 54 #else 55 return false; 56 #endif 57 } 58 59 __attribute__((always_inline)) get_socket_dispatch()60inline volatile const struct SocketDispatchType* get_socket_dispatch() 61 { 62 #ifdef OHOS_SOCKET_HOOK_ENABLE 63 volatile const struct SocketDispatchType* ret = (struct SocketDispatchType *)__current_dispatch; 64 if (ret != NULL) { 65 if (!__get_socket_hook_begin_flag()) { 66 ret = NULL; 67 } else if (!__get_socket_hook_flag()) { 68 ret = NULL; 69 } else { 70 return ret; 71 } 72 } 73 return ret; 74 #else 75 return NULL; 76 #endif 77 } 78 79 #ifdef __cplusplus 80 } 81 #endif 82 83 #endif