xref: /f-stack/app/micro_thread/ff_hook.cpp (revision 144c6bcd)
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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <sched.h>
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <fcntl.h>
32 #include <stdarg.h>
33 #include "ff_api.h"
34 #include "mt_sys_hook.h"
35 #include "ff_hook.h"
36 
37 
38 
39 
40 /*
41 void ff_hook_new_fd(int fd)
42 {
43 	if (fd < 0 || fd >= ff_HOOK_MAX_FD) {
44 		return;
45 	}
46 	g_ff_hook_fd_tab[fd] = 1;
47 }
48 
49 bool ff_hook_find_fd(int fd) {
50 	if (fd < 0 || fd >= ff_HOOK_MAX_FD) {
51 		return false;
52 	}
53 
54 	if (g_ff_hook_fd_tab[fd] == 1) {
55 		return true;
56 	} else {
57 		return false;
58 	}
59 }
60 
61 void ff_hook_free_fd(int fd)
62 {
63 	if (fd < 0 || fd >= ff_HOOK_MAX_FD) {
64 		return;
65 	}
66 	g_ff_hook_fd_tab[fd] = 0;
67 }
68 */
69 
70 int ff_hook_socket(int domain, int type, int protocol)
71 {
72     if (!ff_hook_active() ||  (AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) {
73         return mt_real_func(socket)(domain, type, protocol);
74 	}
75 	int fd = ff_socket(domain, type, protocol);
76 	if (fd >= 0) {
77 		fd |= 1 << FF_FD_BITS;
78 	}
79 	return fd;
80 }
81 
82 int ff_hook_close(int fd)
83 {
84 	if (CHK_FD_BIT(fd)) {
85 		fd = CLR_FD_BIT(fd);
86 		return ff_close(fd);
87 	} else {
88         return mt_real_func(close)(fd);
89 	}
90 }
91 
92 int ff_hook_connect(int fd, const struct sockaddr *address, socklen_t addrlen_len)
93 {
94 	if (CHK_FD_BIT(fd)) {
95 		fd = CLR_FD_BIT(fd);
96 		return ff_connect(fd, (struct linux_sockaddr *)address, addrlen_len);
97 	} else {
98         return mt_real_func(connect)(fd, address, addrlen_len);
99 	}
100 }
101 
102 ssize_t ff_hook_read(int fd, void *buf, size_t nbyte)
103 {
104 	if (CHK_FD_BIT(fd)) {
105 		fd = CLR_FD_BIT(fd);
106 		return ff_read(fd, buf, nbyte);
107 	} else {
108         return mt_real_func(read)(fd, buf, nbyte);
109 	}
110 }
111 
112 ssize_t ff_hook_write(int fd, const void *buf, size_t nbyte)
113 {
114 	if (CHK_FD_BIT(fd)) {
115 		fd = CLR_FD_BIT(fd);
116 		return ff_write(fd, buf, nbyte);
117 	} else {
118         return mt_real_func(write)(fd, buf, nbyte);
119 	}
120 }
121 ssize_t ff_hook_sendto(int fd, const void *message, size_t length, int flags,
122                const struct sockaddr *dest_addr, socklen_t dest_len)
123 {
124 	if (CHK_FD_BIT(fd)) {
125 		fd = CLR_FD_BIT(fd);
126         return ff_sendto(fd, message, length, flags, (struct linux_sockaddr *)dest_addr, dest_len);
127 	} else {
128         return mt_real_func(sendto)(fd, message, length, flags, dest_addr, dest_len);
129 	}
130 }
131 ssize_t ff_hook_recvfrom(int fd, void *buffer, size_t length, int flags,
132                   struct sockaddr *address, socklen_t *address_len)
133 {
134 	if (CHK_FD_BIT(fd)) {
135 		fd = CLR_FD_BIT(fd);
136         return ff_recvfrom(fd, buffer, length, flags, (struct linux_sockaddr *)address, address_len);
137 	} else {
138         return mt_real_func(recvfrom)(fd, buffer, length, flags, address, address_len);
139 	}
140 }
141 ssize_t ff_hook_recv(int fd, void *buffer, size_t length, int flags)
142 {
143 	if (CHK_FD_BIT(fd)) {
144 		fd = CLR_FD_BIT(fd);
145 		return ff_recv(fd, buffer, length, flags);
146 	} else {
147         return mt_real_func(recv)(fd, buffer, length, flags);
148 	}
149 }
150 ssize_t ff_hook_send(int fd, const void *buf, size_t nbyte, int flags)
151 {
152 	if (CHK_FD_BIT(fd)) {
153 		fd = CLR_FD_BIT(fd);
154 		return ff_send(fd, buf, nbyte, flags);
155 	} else {
156         return mt_real_func(send)(fd, buf, nbyte, flags);
157 	}
158 
159 }
160 int ff_hook_setsockopt(int fd, int level, int option_name, const void *option_value, socklen_t option_len)
161 {
162 	if (CHK_FD_BIT(fd)) {
163 		fd = CLR_FD_BIT(fd);
164         return ff_setsockopt(fd, level, option_name, option_value, option_len);
165 	} else {
166         return mt_real_func(setsockopt)(fd, level, option_name, option_value, option_len);
167 	}
168 }
169 
170 int ff_hook_ioctl(int fd, int cmd, void *arg)
171 {
172 	if (CHK_FD_BIT(fd)) {
173 		fd = CLR_FD_BIT(fd);
174 		return ff_ioctl(fd, cmd, arg);
175 	} else {
176         return mt_real_func(ioctl)(fd, cmd, arg);
177 	}
178 }
179 
180 int ff_hook_fcntl(int fd, int cmd, void *arg)
181 {
182 	if (CHK_FD_BIT(fd)) {
183 		fd = CLR_FD_BIT(fd);
184 		return ff_fcntl(fd, cmd, arg);
185 	} else {
186         return mt_real_func(fcntl)(fd, cmd, arg);
187 	}
188 }
189 
190 int ff_hook_listen(int fd, int backlog)
191 {
192 	if (CHK_FD_BIT(fd)) {
193 		fd = CLR_FD_BIT(fd);
194 		return ff_listen(fd, backlog);
195 	} else {
196 		return mt_real_func(listen)(fd, backlog);
197 	}
198 }
199 
200 int ff_hook_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
201 {
202 	if (CHK_FD_BIT(fd)) {
203 		fd = CLR_FD_BIT(fd);
204 		return ff_bind(fd, (struct linux_sockaddr *)addr, addrlen);
205 	} else {
206 		return mt_real_func(bind)(fd, addr, addrlen);
207 	}
208 }
209 int ff_hook_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
210 {
211 	if (CHK_FD_BIT(fd)) {
212 		fd = CLR_FD_BIT(fd);
213 		int c = ff_accept(fd, (struct linux_sockaddr *)addr, addrlen);
214 		if (c < 0) {
215 			return c;
216 		}
217 		c |= 1 << FF_FD_BITS;
218 		return c;
219 	} else {
220 		return mt_real_func(accept)(fd, addr, addrlen);
221 	}
222 }
223