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 int ff_hook_socket(int domain, int type, int protocol) 38 { 39 if ((AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) { 40 return mt_real_func(socket)(domain, type, protocol); 41 } 42 return ff_socket(domain, type, protocol); 43 } 44 45 int ff_hook_close(int fd) 46 { 47 if (ff_fdisused(fd)) { 48 return ff_close(fd); 49 } else { 50 return mt_real_func(close)(fd); 51 } 52 } 53 54 int ff_hook_connect(int fd, const struct sockaddr *address, socklen_t addrlen_len) 55 { 56 if (ff_fdisused(fd)) { 57 return ff_connect(fd, (struct linux_sockaddr *)address, addrlen_len); 58 } else { 59 return mt_real_func(connect)(fd, address, addrlen_len); 60 } 61 } 62 63 ssize_t ff_hook_read(int fd, void *buf, size_t nbyte) 64 { 65 if (ff_fdisused(fd)) { 66 return ff_read(fd, buf, nbyte); 67 } else { 68 return mt_real_func(read)(fd, buf, nbyte); 69 } 70 } 71 72 ssize_t ff_hook_write(int fd, const void *buf, size_t nbyte) 73 { 74 if (ff_fdisused(fd)) { 75 return ff_write(fd, buf, nbyte); 76 } else { 77 return mt_real_func(write)(fd, buf, nbyte); 78 } 79 } 80 ssize_t ff_hook_sendto(int fd, const void *message, size_t length, int flags, 81 const struct sockaddr *dest_addr, socklen_t dest_len) 82 { 83 if (ff_fdisused(fd)) { 84 return ff_sendto(fd, message, length, flags, (struct linux_sockaddr *)dest_addr, dest_len); 85 } else { 86 return mt_real_func(sendto)(fd, message, length, flags, dest_addr, dest_len); 87 } 88 } 89 ssize_t ff_hook_recvfrom(int fd, void *buffer, size_t length, int flags, 90 struct sockaddr *address, socklen_t *address_len) 91 { 92 if (ff_fdisused(fd)) { 93 return ff_recvfrom(fd, buffer, length, flags, (struct linux_sockaddr *)address, address_len); 94 } else { 95 return mt_real_func(recvfrom)(fd, buffer, length, flags, address, address_len); 96 } 97 } 98 ssize_t ff_hook_recv(int fd, void *buffer, size_t length, int flags) 99 { 100 if (ff_fdisused(fd)) { 101 return ff_recv(fd, buffer, length, flags); 102 } else { 103 return mt_real_func(recv)(fd, buffer, length, flags); 104 } 105 } 106 ssize_t ff_hook_send(int fd, const void *buf, size_t nbyte, int flags) 107 { 108 if (ff_fdisused(fd)) { 109 return ff_send(fd, buf, nbyte, flags); 110 } else { 111 return mt_real_func(send)(fd, buf, nbyte, flags); 112 } 113 114 } 115 int ff_hook_setsockopt(int fd, int level, int option_name, const void *option_value, socklen_t option_len) 116 { 117 if (ff_fdisused(fd)) { 118 return ff_setsockopt(fd, level, option_name, option_value, option_len); 119 } else { 120 return mt_real_func(setsockopt)(fd, level, option_name, option_value, option_len); 121 } 122 } 123 124 int ff_hook_ioctl(int fd, int cmd, void *arg) 125 { 126 if (ff_fdisused(fd)) { 127 return ff_ioctl(fd, cmd, arg); 128 } else { 129 return mt_real_func(ioctl)(fd, cmd, arg); 130 } 131 } 132 133 int ff_hook_fcntl(int fd, int cmd, void *arg) 134 { 135 if (ff_fdisused(fd)) { 136 return ff_fcntl(fd, cmd, arg); 137 } else { 138 return mt_real_func(fcntl)(fd, cmd, arg); 139 } 140 } 141 142 int ff_hook_listen(int fd, int backlog) 143 { 144 if (ff_fdisused(fd)) { 145 return ff_listen(fd, backlog); 146 } else { 147 return mt_real_func(listen)(fd, backlog); 148 } 149 } 150 151 int ff_hook_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) 152 { 153 if (ff_fdisused(fd)) { 154 return ff_bind(fd, (struct linux_sockaddr *)addr, addrlen); 155 } else { 156 return mt_real_func(bind)(fd, addr, addrlen); 157 } 158 } 159 160 int ff_hook_accept(int fd, struct sockaddr *addr, socklen_t *addrlen) 161 { 162 if (ff_fdisused(fd)) { 163 return ff_accept(fd, (struct linux_sockaddr *)addr, addrlen); 164 } else { 165 return mt_real_func(accept)(fd, addr, addrlen); 166 } 167 } 168