diff --git a/musl_template.gni b/musl_template.gni index d46f605e2972aee178c95fdc41f96a8a0694d988..ca7f25b009022facc7b683634372c1632dfd4ecd 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -720,7 +720,6 @@ template("musl_libs") { "./porting/linux/user/src/hook/musl_preinit.c", "./porting/linux/user/src/hook/musl_preinit_common.c", "./porting/linux/user/src/hook/musl_socket_preinit.c", - "./porting/linux/user/src/hook/musl_socket_preinit_common.c", "./porting/linux/user/src/hook/socket_common.c", ] diff --git a/porting/linux/user/src/hook/musl_socket_preinit.c b/porting/linux/user/src/hook/musl_socket_preinit.c index 74f55ee6415d7c348079630d7f0cd897fcb45e8c..eb6c58789bbf3bba52768cff7701e347996e60f0 100644 --- a/porting/linux/user/src/hook/musl_socket_preinit.c +++ b/porting/linux/user/src/hook/musl_socket_preinit.c @@ -9,13 +9,24 @@ #include #include #include -#include "musl_socket_preinit_common.h" +#include #include "musl_log.h" +#include "musl_socket.h" +#include "musl_socket_preinit.h" static char *__socket_hook_shared_lib = "libfwmark_client.z.so"; static char *__socket_hook_function_prefix = "ohos_socket_hook"; void* shared_lib_func[LAST_FUNC]; + +long long __current_dispatch; long long __ohos_socket_hook_shared_library; +bool __socket_hook_begin_flag; + +struct SocketDispatchType __musl_libc_socket_dispatch; +struct SocketDispatchType __libc_socket_default_dispatch = { + .socket = MuslSocket(socket), +}; + typedef bool (*init_func_type)(const struct SocketDispatchType*, bool*, const char*); typedef void (*finalize_func_type)(); #define MAX_SYMBOL_SIZE 1000 @@ -147,4 +158,54 @@ __attribute__((constructor())) static void __musl_socket_initialize() init_ohos_socket_hook(); } } + +__attribute__((always_inline)) +inline bool __get_socket_hook_begin_flag() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + return __socket_hook_begin_flag; +#else + return false; +#endif +} + +__attribute__((always_inline)) +inline bool __get_socket_hook_flag() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + void* handle = (void *)__ohos_socket_hook_shared_library; + if (handle == NULL) { + return false; + } else if (handle == (void *)-1) { + return true; + } else { + SocketGetHookFlagType get_hook_func_ptr = (SocketGetHookFlagType)(shared_lib_func[GET_HOOK_FLAG_FUNC]); + bool flag = get_hook_func_ptr(); + return flag; + } +#else + return false; +#endif +} + +__attribute__((always_inline)) +inline volatile const struct SocketDispatchType* get_socket_dispatch() +{ +#ifdef OHOS_SOCKET_HOOK_ENABLE + volatile const struct SocketDispatchType* ret = (struct SocketDispatchType *)__current_dispatch; + if (ret != NULL) { + if (!__get_socket_hook_begin_flag()) { + ret = NULL; + } else if (!__get_socket_hook_flag()) { + ret = NULL; + } else { + return ret; + } + } + return ret; +#else + return NULL; +#endif +} + #endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit.h b/porting/linux/user/src/hook/musl_socket_preinit.h new file mode 100644 index 0000000000000000000000000000000000000000..2a8178ecf397b5a16b775c2d0b6ea936cb4668de --- /dev/null +++ b/porting/linux/user/src/hook/musl_socket_preinit.h @@ -0,0 +1,28 @@ +#ifndef _MUSL_SOCKET_PREINIT_COMMON_H +#define _MUSL_SOCKET_PREINIT_COMMON_H + +#include +#include "musl_socket_dispatch.h" +#include "common_def.h" + +enum SocketFuncEnum { + INITIALIZE_FUNC, + FINALIZE_FUNC, + GET_HOOK_FLAG_FUNC, + SET_HOOK_FLAG_FUNC, + LAST_FUNC, +}; + +#ifdef __cplusplus +extern "C" { +#endif + +bool __get_socket_hook_begin_flag(); +bool __get_socket_hook_flag(); +volatile const struct SocketDispatchType* get_socket_dispatch(); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.c b/porting/linux/user/src/hook/musl_socket_preinit_common.c deleted file mode 100644 index a586cf8785dc7f98dd5086474e1f799061691a3b..0000000000000000000000000000000000000000 --- a/porting/linux/user/src/hook/musl_socket_preinit_common.c +++ /dev/null @@ -1,16 +0,0 @@ -#ifdef OHOS_SOCKET_HOOK_ENABLE -#include -#include "musl_socket.h" -#include "musl_socket_preinit_common.h" - -struct SocketDispatchType __musl_libc_socket_dispatch; - -long long __current_dispatch; - -struct SocketDispatchType __libc_socket_default_dispatch = { - .socket = MuslSocket(socket), -}; - -bool __socket_hook_begin_flag; - -#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.h b/porting/linux/user/src/hook/musl_socket_preinit_common.h deleted file mode 100644 index c13b632063ed693466e00f3b015aebb0ee69ca3a..0000000000000000000000000000000000000000 --- a/porting/linux/user/src/hook/musl_socket_preinit_common.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef _MUSL_SOCKET_PREINIT_COMMON_H -#define _MUSL_SOCKET_PREINIT_COMMON_H - -#include -#include "musl_socket_dispatch.h" -#include "common_def.h" - -extern struct SocketDispatchType __musl_libc_socket_dispatch; -extern struct SocketDispatchType __libc_socket_default_dispatch; - -enum SocketFuncEnum { - INITIALIZE_FUNC, - FINALIZE_FUNC, - GET_HOOK_FLAG_FUNC, - SET_HOOK_FLAG_FUNC, - LAST_FUNC, -}; - -#ifdef OHOS_SOCKET_HOOK_ENABLE -extern long long __current_dispatch; -extern bool __socket_hook_begin_flag; -extern long long __ohos_socket_hook_shared_library; -extern void* shared_lib_func[LAST_FUNC]; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -__attribute__((always_inline)) -inline bool __get_socket_hook_begin_flag() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - return __socket_hook_begin_flag; -#else - return false; -#endif -} - -__attribute__((always_inline)) -inline bool __get_socket_hook_flag() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - void* handle = (void *)__ohos_socket_hook_shared_library; - if (handle == NULL) { - return false; - } else if (handle == (void *)-1) { - return true; - } else { - SocketGetHookFlagType get_hook_func_ptr = (SocketGetHookFlagType)(shared_lib_func[GET_HOOK_FLAG_FUNC]); - bool flag = get_hook_func_ptr(); - return flag; - } -#else - return false; -#endif -} - -__attribute__((always_inline)) -inline volatile const struct SocketDispatchType* get_socket_dispatch() -{ -#ifdef OHOS_SOCKET_HOOK_ENABLE - volatile const struct SocketDispatchType* ret = (struct SocketDispatchType *)__current_dispatch; - if (ret != NULL) { - if (!__get_socket_hook_begin_flag()) { - ret = NULL; - } else if (!__get_socket_hook_flag()) { - ret = NULL; - } else { - return ret; - } - } - return ret; -#else - return NULL; -#endif -} - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/porting/linux/user/src/hook/socket_common.c b/porting/linux/user/src/hook/socket_common.c index 939b75c5a25ed713f09c5395739e1c7f97c2006a..f7bfd7a926948c28e11bbc73c539828b09e54b98 100644 --- a/porting/linux/user/src/hook/socket_common.c +++ b/porting/linux/user/src/hook/socket_common.c @@ -2,7 +2,7 @@ #include "musl_socket.h" #include #include "common_def.h" -#include "musl_socket_preinit_common.h" +#include "musl_socket_preinit.h" int socket(int domain, int type, int protocol) { @@ -15,4 +15,4 @@ int socket(int domain, int type, int protocol) return result; } -#endif \ No newline at end of file +#endif