1 2 /** 3 * Tencent is pleased to support the open source community by making MSEC available. 4 * 5 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. 6 * 7 * Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. You may 9 * obtain a copy of the License at 10 * 11 * https://opensource.org/licenses/GPL-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software distributed under the 14 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 15 * either express or implied. See the License for the specific language governing permissions 16 * and limitations under the License. 17 */ 18 19 20 /** 21 * @filename mt_sys_hook.h 22 */ 23 24 #ifndef _MT_SYS_HOOK___ 25 #define _MT_SYS_HOOK___ 26 27 #include <poll.h> 28 #include <dlfcn.h> 29 30 #include "ff_api.h" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 typedef int (*func_socket)(int domain, int type, int protocol); 37 typedef int (*func_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 38 typedef int (*func_listen)(int sockfd, int backlog); 39 typedef int (*func_close)(int fd); 40 typedef int (*func_connect)(int socket, const struct sockaddr *address, socklen_t address_len); 41 typedef int (*func_accept)(int socket, struct sockaddr *address, socklen_t *addrlen); 42 typedef ssize_t (*func_read)(int fildes, void *buf, size_t nbyte); 43 typedef ssize_t (*func_write)(int fildes, const void *buf, size_t nbyte); 44 typedef ssize_t (*func_sendto)(int socket, const void *message, size_t length, 45 int flags, const struct sockaddr *dest_addr, socklen_t dest_len); 46 typedef ssize_t (*func_recvfrom)(int socket, void *buffer, size_t length, 47 int flags, struct sockaddr *address, socklen_t *address_len); 48 typedef size_t (*func_send)(int socket, const void *buffer, size_t length, int flags); 49 typedef ssize_t (*func_recv)(int socket, void *buffer, size_t length, int flags); 50 typedef int (*func_select)(int nfds, fd_set *readfds, fd_set *writefds, 51 fd_set *exceptfds, struct timeval *timeout); 52 typedef int (*func_poll)(struct pollfd fds[], nfds_t nfds, int timeout); 53 typedef int (*func_setsockopt)(int socket, int level, int option_name, 54 const void *option_value, socklen_t option_len); 55 typedef int (*func_ioctl)(int fd, unsigned long cmd, ...); 56 typedef int (*func_fcntl)(int fd, int cmd, ...); 57 58 typedef unsigned int (*func_sleep)(unsigned int seconds); 59 60 typedef struct mt_syscall_func_tab 61 { 62 func_socket real_socket; 63 func_bind real_bind; 64 func_listen real_listen; 65 func_close real_close; 66 func_connect real_connect; 67 func_read real_read; 68 func_write real_write; 69 func_sendto real_sendto; 70 func_recvfrom real_recvfrom; 71 func_send real_send; 72 func_recv real_recv; 73 func_setsockopt real_setsockopt; 74 func_fcntl real_fcntl; 75 func_ioctl real_ioctl; 76 77 func_sleep real_sleep; 78 func_select real_select; 79 func_poll real_poll; 80 81 func_accept real_accept; 82 } MtSyscallFuncTab; 83 84 extern MtSyscallFuncTab g_mt_syscall_tab; 85 extern int g_mt_hook_flag; 86 extern int g_ff_hook_flag; 87 88 #define mt_hook_syscall(name) \ 89 do { \ 90 if (!g_mt_syscall_tab.real_##name) { \ 91 g_mt_syscall_tab.real_##name = (func_##name)dlsym(RTLD_NEXT, #name);\ 92 } \ 93 } while (0) 94 95 #define mt_real_func(name) g_mt_syscall_tab.real_##name 96 97 #define mt_set_hook_flag() (g_mt_hook_flag = 1) 98 #define mt_unset_hook_flag() (g_mt_hook_flag = 0) 99 100 #define mt_hook_active() (g_mt_hook_flag == 1) 101 102 #define ff_set_hook_flag() (g_ff_hook_flag = 1) 103 #define ff_unset_hook_flag() (g_ff_hook_flag = 0) 104 #define ff_hook_active() (g_ff_hook_flag == 1) 105 106 #ifdef __cplusplus 107 } 108 #endif 109 110 #endif 111 112 113