Intercept.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifdef USE_GNU_SOURCE
  28. #define _GNU_SOURCE
  29. #endif
  30. #include <unistd.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <dlfcn.h>
  34. #include <strings.h>
  35. #include <netinet/in.h>
  36. #include <sys/time.h>
  37. #include <pwd.h>
  38. #include <errno.h>
  39. #include <stdarg.h>
  40. #include <netdb.h>
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/poll.h>
  45. #include <sys/un.h>
  46. #include <arpa/inet.h>
  47. #include <sys/resource.h>
  48. #if defined(__linux__)
  49. #include <linux/errno.h>
  50. #include <sys/syscall.h>
  51. #include <linux/net.h> /* for NPROTO */
  52. #endif
  53. #if defined(__linux__)
  54. #define SOCK_MAX (SOCK_PACKET + 1)
  55. #endif
  56. #define SOCK_TYPE_MASK 0xf
  57. #include "Intercept.h"
  58. #include "RPC.h"
  59. #include "common.inc.c"
  60. /*------------------------------------------------------------------------------
  61. ------------------- Intercept<--->Service Comm mechanisms ----------------------
  62. ------------------------------------------------------------------------------*/
  63. static char *netpath = (char *)0;
  64. /* Check whether the socket is mapped to the service or not. We
  65. need to know if this is a regular AF_LOCAL socket or an end of a socketpair
  66. that the service uses. We don't want to keep state in the intercept, so
  67. we simply ask the service via an RPC */
  68. static int connected_to_service(int sockfd)
  69. {
  70. dwr(MSG_DEBUG,"connected_to_service():\n");
  71. socklen_t len;
  72. struct sockaddr_storage addr;
  73. len = sizeof addr;
  74. struct sockaddr_un * addr_un;
  75. getpeername(sockfd, (struct sockaddr*)&addr, &len);
  76. if (addr.ss_family == AF_LOCAL || addr.ss_family == AF_LOCAL) {
  77. addr_un = (struct sockaddr_un*)&addr;
  78. if(strcmp(addr_un->sun_path, netpath) == 0) {
  79. dwr(MSG_DEBUG,"connected_to_service(): Yes, %s\n", addr_un->sun_path);
  80. return 1;
  81. }
  82. }
  83. dwr(MSG_DEBUG,"connected_to_service(): Not connected to service\n");
  84. return 0;
  85. }
  86. /* get symbols and initialize mutexes */
  87. static int set_up_intercept()
  88. {
  89. if (!realconnect) {
  90. #if defined(__linux__)
  91. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  92. realsyscall = dlsym(RTLD_NEXT, "syscall");
  93. #endif
  94. realconnect = dlsym(RTLD_NEXT, "connect");
  95. realbind = dlsym(RTLD_NEXT, "bind");
  96. realaccept = dlsym(RTLD_NEXT, "accept");
  97. reallisten = dlsym(RTLD_NEXT, "listen");
  98. realsocket = dlsym(RTLD_NEXT, "socket");
  99. realbind = dlsym(RTLD_NEXT, "bind");
  100. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  101. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  102. realclose = dlsym(RTLD_NEXT, "close");
  103. realgetsockname = dlsym(RTLD_NEXT, "getsockname");
  104. }
  105. if (!netpath) {
  106. netpath = getenv("ZT_NC_NETWORK");
  107. if (!netpath)
  108. return 0;
  109. dwr(MSG_DEBUG,"Connecting to service at: %s\n", netpath);
  110. /* Hook/intercept Posix net API symbols */
  111. rpc_mutex_init();
  112. }
  113. return 1;
  114. }
  115. /*------------------------------------------------------------------------------
  116. --------------------------------- setsockopt() ---------------------------------
  117. ------------------------------------------------------------------------------*/
  118. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  119. int setsockopt(SETSOCKOPT_SIG)
  120. {
  121. if (!set_up_intercept())
  122. return realsetsockopt(socket, level, option_name, option_value, option_len);
  123. dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
  124. #if defined(__linux__)
  125. if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
  126. return 0;
  127. if(level == SOL_IP && (option_name == IP_TTL || option_name == IP_TOS))
  128. return 0;
  129. #endif
  130. if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
  131. return 0;
  132. if(realsetsockopt(socket, level, option_name, option_value, option_len) < 0)
  133. perror("setsockopt():\n");
  134. return 0;
  135. }
  136. /*------------------------------------------------------------------------------
  137. --------------------------------- getsockopt() ---------------------------------
  138. ------------------------------------------------------------------------------*/
  139. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  140. int getsockopt(GETSOCKOPT_SIG)
  141. {
  142. dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
  143. if (!set_up_intercept() || !connected_to_service(sockfd))
  144. return realgetsockopt(sockfd, level, optname, optval, optlen);
  145. if(optname == SO_TYPE) {
  146. int* val = (int*)optval;
  147. *val = 2;
  148. optval = (void*)val;
  149. }
  150. return 0;
  151. }
  152. /*------------------------------------------------------------------------------
  153. ----------------------------------- socket() -----------------------------------
  154. ------------------------------------------------------------------------------*/
  155. /* int socket_family, int socket_type, int protocol
  156. socket() intercept function */
  157. int socket(SOCKET_SIG)
  158. {
  159. if (!set_up_intercept())
  160. return realsocket(socket_family, socket_type, protocol);
  161. dwr(MSG_DEBUG,"socket():\n");
  162. /* Check that type makes sense */
  163. #if defined(__linux__)
  164. int flags = socket_type & ~SOCK_TYPE_MASK;
  165. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  166. errno = EINVAL;
  167. return -1;
  168. }
  169. #endif
  170. socket_type &= SOCK_TYPE_MASK;
  171. /* Check protocol is in range */
  172. #if defined(__linux__)
  173. if (socket_family < 0 || socket_family >= NPROTO){
  174. errno = EAFNOSUPPORT;
  175. return -1;
  176. }
  177. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  178. errno = EINVAL;
  179. return -1;
  180. }
  181. #endif
  182. /* TODO: detect ENFILE condition */
  183. if(socket_family == AF_LOCAL
  184. #if defined(__linux__)
  185. || socket_family == AF_NETLINK
  186. #endif
  187. || socket_family == AF_UNIX) {
  188. int err = realsocket(socket_family, socket_type, protocol);
  189. dwr(MSG_DEBUG,"realsocket() = %d\n", err);
  190. return err;
  191. }
  192. /* Assemble and send RPC */
  193. struct socket_st rpc_st;
  194. rpc_st.socket_family = socket_family;
  195. rpc_st.socket_type = socket_type;
  196. rpc_st.protocol = protocol;
  197. rpc_st.__tid = syscall(SYS_gettid);
  198. /* -1 is passed since we we're generating the new socket in this call */
  199. return rpc_send_command(netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
  200. }
  201. /*------------------------------------------------------------------------------
  202. ---------------------------------- connect() -----------------------------------
  203. ------------------------------------------------------------------------------*/
  204. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  205. connect() intercept function */
  206. int connect(CONNECT_SIG)
  207. {
  208. if (!set_up_intercept())
  209. return realconnect(__fd, __addr, __len);
  210. struct sockaddr_in *connaddr;
  211. connaddr = (struct sockaddr_in *)__addr;
  212. if(__addr->sa_family == AF_LOCAL || __addr->sa_family == AF_UNIX) {
  213. struct sockaddr_storage storage;
  214. memcpy(&storage, __addr, __len);
  215. struct sockaddr_un *s_un = (struct sockaddr_un*)&storage;
  216. dwr(MSG_DEBUG, "connect(): address = %s\n", s_un->sun_path);
  217. }
  218. int port = connaddr->sin_port;
  219. int ip = connaddr->sin_addr.s_addr;
  220. unsigned char d[4];
  221. d[0] = ip & 0xFF;
  222. d[1] = (ip >> 8) & 0xFF;
  223. d[2] = (ip >> 16) & 0xFF;
  224. d[3] = (ip >> 24) & 0xFF;
  225. dwr(MSG_DEBUG,"connect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
  226. dwr(MSG_DEBUG,"connect(%d):\n", __fd);
  227. /* Check that this is a valid fd */
  228. if(fcntl(__fd, F_GETFD) < 0) {
  229. errno = EBADF;
  230. return -1;
  231. }
  232. /* Check that it is a socket */
  233. int sock_type;
  234. socklen_t sock_type_len = sizeof(sock_type);
  235. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  236. errno = ENOTSOCK;
  237. return -1;
  238. }
  239. #if defined(__linux__)
  240. /* Check family */
  241. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  242. errno = EAFNOSUPPORT;
  243. return -1;
  244. }
  245. #endif
  246. /* make sure we don't touch any standard outputs */
  247. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO)
  248. return(realconnect(__fd, __addr, __len));
  249. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  250. #if defined(__linux__)
  251. || connaddr->sin_family == PF_NETLINK
  252. || connaddr->sin_family == AF_NETLINK
  253. #endif
  254. || connaddr->sin_family == AF_UNIX)) {
  255. return realconnect(__fd, __addr, __len);
  256. }
  257. /* Assemble and send RPC */
  258. struct connect_st rpc_st;
  259. #if defined(__linux__)
  260. rpc_st.__tid = syscall(SYS_gettid);
  261. #endif
  262. rpc_st.__fd = __fd;
  263. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr_storage));
  264. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  265. return rpc_send_command(netpath, RPC_CONNECT, __fd, &rpc_st, sizeof(struct connect_st));
  266. }
  267. /*------------------------------------------------------------------------------
  268. ------------------------------------ bind() ------------------------------------
  269. ------------------------------------------------------------------------------*/
  270. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  271. bind() intercept function */
  272. int bind(BIND_SIG)
  273. {
  274. if (!set_up_intercept())
  275. return realbind(sockfd, addr, addrlen);
  276. dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
  277. /* Check that this is a valid fd */
  278. if(fcntl(sockfd, F_GETFD) < 0) {
  279. errno = EBADF;
  280. return -1;
  281. }
  282. /* Check that it is a socket */
  283. int opt = -1;
  284. socklen_t opt_len;
  285. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  286. errno = ENOTSOCK;
  287. return -1;
  288. }
  289. /* make sure we don't touch any standard outputs */
  290. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  291. return(realbind(sockfd, addr, addrlen));
  292. /* If local, just use normal syscall */
  293. struct sockaddr_in *connaddr;
  294. connaddr = (struct sockaddr_in *)addr;
  295. if(connaddr->sin_family == AF_LOCAL
  296. #if defined(__linux__)
  297. || connaddr->sin_family == AF_NETLINK
  298. #endif
  299. || connaddr->sin_family == AF_UNIX) {
  300. int err = realbind(sockfd, addr, addrlen);
  301. dwr(MSG_DEBUG,"realbind, err = %d\n", err);
  302. return err;
  303. }
  304. int port = connaddr->sin_port;
  305. int ip = connaddr->sin_addr.s_addr;
  306. unsigned char d[4];
  307. d[0] = ip & 0xFF;
  308. d[1] = (ip >> 8) & 0xFF;
  309. d[2] = (ip >> 16) & 0xFF;
  310. d[3] = (ip >> 24) & 0xFF;
  311. dwr(MSG_DEBUG,"bind(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
  312. /* Assemble and send RPC */
  313. struct bind_st rpc_st;
  314. rpc_st.sockfd = sockfd;
  315. #if defined(__linux__)
  316. rpc_st.__tid = syscall(SYS_gettid);
  317. #endif
  318. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
  319. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  320. return rpc_send_command(netpath, RPC_BIND, sockfd, &rpc_st, sizeof(struct bind_st));
  321. }
  322. /*------------------------------------------------------------------------------
  323. ----------------------------------- accept4() ----------------------------------
  324. ------------------------------------------------------------------------------*/
  325. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  326. #if defined(__linux__)
  327. int accept4(ACCEPT4_SIG)
  328. {
  329. dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
  330. if ((flags & SOCK_CLOEXEC))
  331. fcntl(sockfd, F_SETFL, FD_CLOEXEC);
  332. if ((flags & SOCK_NONBLOCK))
  333. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  334. return accept(sockfd, addr, addrlen);
  335. }
  336. #endif
  337. /*------------------------------------------------------------------------------
  338. ----------------------------------- accept() -----------------------------------
  339. ------------------------------------------------------------------------------*/
  340. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  341. accept() intercept function */
  342. int accept(ACCEPT_SIG)
  343. {
  344. if (!set_up_intercept())
  345. return realaccept(sockfd, addr, addrlen);
  346. dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
  347. /* Check that this is a valid fd */
  348. if(fcntl(sockfd, F_GETFD) < 0) {
  349. return -1;
  350. errno = EBADF;
  351. dwr(MSG_DEBUG,"EBADF\n");
  352. return -1;
  353. }
  354. /* Check that it is a socket */
  355. int opt;
  356. socklen_t opt_len;
  357. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  358. errno = ENOTSOCK;
  359. dwr(MSG_DEBUG,"ENOTSOCK\n");
  360. return -1;
  361. }
  362. /* Check that this socket supports accept() */
  363. if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
  364. errno = EOPNOTSUPP;
  365. dwr(MSG_DEBUG,"EOPNOTSUPP\n");
  366. return -1;
  367. }
  368. /* Check that we haven't hit the soft-limit file descriptors allowed */
  369. struct rlimit rl;
  370. getrlimit(RLIMIT_NOFILE, &rl);
  371. if(sockfd >= rl.rlim_cur){
  372. errno = EMFILE;
  373. dwr(MSG_DEBUG,"EMFILE\n");
  374. return -1;
  375. }
  376. /* Check address length */
  377. if(addrlen < 0) {
  378. errno = EINVAL;
  379. dwr(MSG_DEBUG,"EINVAL\n");
  380. return -1;
  381. }
  382. /* redirect calls for standard I/O descriptors to kernel */
  383. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
  384. dwr(MSG_DEBUG,"realaccept():\n");
  385. return(realaccept(sockfd, addr, addrlen));
  386. }
  387. if(addr)
  388. addr->sa_family = AF_INET;
  389. int new_fd = get_new_fd(sockfd);
  390. if(new_fd > 0) {
  391. errno = ERR_OK;
  392. return new_fd;
  393. }
  394. errno = EAGAIN;
  395. return -EAGAIN;
  396. }
  397. /*------------------------------------------------------------------------------
  398. ------------------------------------- listen()----------------------------------
  399. ------------------------------------------------------------------------------*/
  400. /* int sockfd, int backlog */
  401. int listen(LISTEN_SIG)
  402. {
  403. if (!set_up_intercept())
  404. return(reallisten(sockfd, backlog));
  405. dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
  406. int sock_type;
  407. socklen_t sock_type_len = sizeof(sock_type);
  408. /* Check that this is a valid fd */
  409. if(fcntl(sockfd, F_GETFD) < 0) {
  410. errno = EBADF;
  411. return -1;
  412. }
  413. /* Check that it is a socket */
  414. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  415. errno = ENOTSOCK;
  416. return -1;
  417. }
  418. /* Check that this socket supports accept() */
  419. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  420. errno = EOPNOTSUPP;
  421. return -1;
  422. }
  423. /* make sure we don't touch any standard outputs */
  424. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  425. return(reallisten(sockfd, backlog));
  426. if(!connected_to_service(sockfd)) {
  427. reallisten(sockfd, backlog);
  428. }
  429. /* Assemble and send RPC */
  430. struct listen_st rpc_st;
  431. rpc_st.sockfd = sockfd;
  432. rpc_st.backlog = backlog;
  433. #if defined(__linux__)
  434. rpc_st.__tid = syscall(SYS_gettid);
  435. #endif
  436. return rpc_send_command(netpath, RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st));
  437. }
  438. /*------------------------------------------------------------------------------
  439. ------------------------------------- close() ----------------------------------
  440. ------------------------------------------------------------------------------*/
  441. /* int fd */
  442. int close(CLOSE_SIG)
  443. {
  444. dwr(MSG_DEBUG, "close(%d)\n", fd);
  445. set_up_intercept();
  446. return realclose(fd);
  447. }
  448. /*------------------------------------------------------------------------------
  449. -------------------------------- getsockname() ---------------------------------
  450. ------------------------------------------------------------------------------*/
  451. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen */
  452. int getsockname(GETSOCKNAME_SIG)
  453. {
  454. if (!set_up_intercept())
  455. return realgetsockname(sockfd, addr, addrlen);
  456. dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd);
  457. if(!connected_to_service(sockfd)) {
  458. dwr(MSG_DEBUG,"getsockname(): not used by service\n");
  459. return realgetsockname(sockfd, addr, addrlen);
  460. }
  461. /* This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in
  462. * and is an IPv4 address. */
  463. /* assemble and send command */
  464. struct getsockname_st rpc_st;
  465. rpc_st.sockfd = sockfd;
  466. memcpy(&rpc_st.addr, addr, *addrlen);
  467. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  468. int rpcfd = rpc_send_command(netpath, RPC_GETSOCKNAME, sockfd, &rpc_st, sizeof(struct getsockname_st));
  469. /* read address info from service */
  470. char addrbuf[sizeof(struct sockaddr_storage)];
  471. memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
  472. if(rpcfd > -1)
  473. if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
  474. close(rpcfd);
  475. struct sockaddr_storage sock_storage;
  476. memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
  477. *addrlen = sizeof(struct sockaddr_in);
  478. memcpy(addr, &sock_storage, (*addrlen > sizeof(sock_storage)) ? sizeof(sock_storage) : *addrlen);
  479. addr->sa_family = AF_INET;
  480. return 0;
  481. }
  482. /*------------------------------------------------------------------------------
  483. ------------------------------------ syscall() ---------------------------------
  484. ------------------------------------------------------------------------------*/
  485. #if defined(__linux__)
  486. long syscall(SYSCALL_SIG)
  487. {
  488. va_list ap;
  489. uintptr_t a,b,c,d,e,f;
  490. va_start(ap, number);
  491. a=va_arg(ap, uintptr_t);
  492. b=va_arg(ap, uintptr_t);
  493. c=va_arg(ap, uintptr_t);
  494. d=va_arg(ap, uintptr_t);
  495. e=va_arg(ap, uintptr_t);
  496. f=va_arg(ap, uintptr_t);
  497. va_end(ap);
  498. if (!set_up_intercept())
  499. return realsyscall(number,a,b,c,d,e,f);
  500. dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
  501. #if defined(__i386__)
  502. /* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
  503. args[0] = (unsigned long) fd;
  504. args[1] = (unsigned long) addr;
  505. args[2] = (unsigned long) addrlen;
  506. args[3] = (unsigned long) flags;
  507. */
  508. #else
  509. if(number == __NR_accept4) {
  510. int sockfd = a;
  511. struct sockaddr * addr = (struct sockaddr*)b;
  512. socklen_t * addrlen = (socklen_t*)c;
  513. int flags = d;
  514. int old_errno = errno;
  515. int err = accept4(sockfd, addr, addrlen, flags);
  516. errno = old_errno;
  517. err = err == -EBADF ? -EAGAIN : err;
  518. return err;
  519. }
  520. #endif
  521. return realsyscall(number,a,b,c,d,e,f);
  522. }
  523. #endif