xref: /f-stack/app/micro_thread/mt_sys_hook.h (revision a9643ea8)
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  *  @info  ΢�߳�hookϵͳapi, �Բ��ö�����������, תͬ��Ϊ�첽��
23  *         HOOK ����, �ο�pth��libcoʵ��
24  */
25 
26 #ifndef _MT_SYS_HOOK___
27 #define _MT_SYS_HOOK___
28 
29 #include <poll.h>
30 #include <dlfcn.h>
31 
32 #include "ff_api.h"
33 
34 #ifdef  __cplusplus
35 extern "C" {
36 #endif
37 
38 /******************************************************************************/
39 /*         1. HOOK �ĺ������岿��                                             */
40 /******************************************************************************/
41 
42 typedef int (*func_socket)(int domain, int type, int protocol);
43 typedef int (*func_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
44 typedef int (*func_listen)(int sockfd, int backlog);
45 typedef int (*func_close)(int fd);
46 typedef int (*func_connect)(int socket, const struct sockaddr *address, socklen_t address_len);
47 typedef int (*func_accept)(int socket, struct sockaddr *address, socklen_t *addrlen);
48 typedef ssize_t (*func_read)(int fildes, void *buf, size_t nbyte);
49 typedef ssize_t (*func_write)(int fildes, const void *buf, size_t nbyte);
50 typedef ssize_t (*func_sendto)(int socket, const void *message, size_t length,
51                         int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
52 typedef ssize_t (*func_recvfrom)(int socket, void *buffer, size_t length,
53 	                    int flags, struct sockaddr *address, socklen_t *address_len);
54 typedef size_t (*func_send)(int socket, const void *buffer, size_t length, int flags);
55 typedef ssize_t (*func_recv)(int socket, void *buffer, size_t length, int flags);
56 typedef int (*func_select)(int nfds, fd_set *readfds, fd_set *writefds,
57                         fd_set *exceptfds, struct timeval *timeout);
58 typedef int (*func_poll)(struct pollfd fds[], nfds_t nfds, int timeout);
59 typedef int (*func_setsockopt)(int socket, int level, int option_name,
60 			            const void *option_value, socklen_t option_len);
61 typedef int (*func_ioctl)(int fd, unsigned long cmd, ...);
62 typedef int (*func_fcntl)(int fd, int cmd, ...);
63 
64 typedef unsigned int (*func_sleep)(unsigned int seconds);
65 
66 
67 /******************************************************************************/
68 /*         2.  ȫ�ֵ�hook�����ṹ                                             */
69 /******************************************************************************/
70 
71 /**
72  * @brief Hook��ԭʼ�������й�����, ֧�ֶ�̬��������
73  */
74 typedef struct mt_syscall_func_tab
75 {
76     func_socket             real_socket;
77 	func_bind               real_bind;
78 	func_listen             real_listen;
79     func_close              real_close;
80     func_connect            real_connect;
81     func_read               real_read;
82     func_write              real_write;
83     func_sendto             real_sendto;
84     func_recvfrom           real_recvfrom;
85     func_send               real_send;
86     func_recv               real_recv;
87     func_setsockopt         real_setsockopt;
88     func_fcntl              real_fcntl;
89     func_ioctl              real_ioctl;
90 
91     func_sleep              real_sleep;             // �ݲ�֧�֣���Ϊû����fd����, ��ֹ����
92     func_select             real_select;            // �ݲ�֧��, 1024��������
93     func_poll               real_poll;              // �ݲ�֧��, ȷ�������ʵʩ
94 
95     func_accept             real_accept;
96 }MtSyscallFuncTab;
97 
98 
99 /******************************************************************************/
100 /*         3.  ֱ�ӵ���ԭʼϵͳapi�Ľӿ�                                      */
101 /******************************************************************************/
102 extern MtSyscallFuncTab  g_mt_syscall_tab;            // ȫ�ַ��ű�
103 extern int               g_mt_hook_flag;              // ȫ�ֿ��Ʊ��
104 extern int               g_ff_hook_flag;              // ȫ�ֿ��Ʊ��
105 
106 #define mt_hook_syscall(name)                                                   \
107 do  {                                                                           \
108         if (!g_mt_syscall_tab.real_##name) {                                    \
109 			g_mt_syscall_tab.real_##name = (func_##name)dlsym(RTLD_NEXT, #name);\
110         }                                                                       \
111     } while (0)
112 
113 #define mt_real_func(name)      g_mt_syscall_tab.real_##name
114 
115 #define mt_set_hook_flag()      (g_mt_hook_flag = 1)
116 #define mt_unset_hook_flag()    (g_mt_hook_flag = 0)
117 
118 #define mt_hook_active()        (g_mt_hook_flag == 1)
119 
120 #define ff_set_hook_flag()      (g_ff_hook_flag = 1)
121 #define ff_unset_hook_flag()    (g_ff_hook_flag = 0)
122 #define ff_hook_active()        (g_ff_hook_flag == 1)
123 
124 #ifdef  __cplusplus
125 }
126 #endif
127 
128 #endif
129 
130 
131