Intercept.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. /* Name used in err msgs */
  31. char *progname = "";
  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 <stdlib.h>
  43. #include <netinet/in.h>
  44. #include <net/if.h>
  45. #include <sys/syscall.h>
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <sys/un.h>
  49. #include <arpa/inet.h>
  50. #include <poll.h>
  51. #include <pthread.h>
  52. #include <unistd.h>
  53. /* For NPs */
  54. #include <sys/stat.h>
  55. #include <sys/ipc.h>
  56. #include <sys/shm.h>
  57. /* for mmap */
  58. #include <sys/mman.h>
  59. #ifdef USE_SOCKS_DNS
  60. #include <resolv.h>
  61. #endif
  62. #include "Intercept.h"
  63. #include "Common.h"
  64. #ifdef CHECKS
  65. //#include <sys/time.h>
  66. #include <sys/resource.h>
  67. #include <linux/net.h> /* for NPROTO */
  68. #define SOCK_MAX (SOCK_PACKET + 1)
  69. #define SOCK_TYPE_MASK 0xf
  70. #endif
  71. /* Global Declarations */
  72. #ifdef USE_SOCKS_DNS
  73. static int (*realresinit)(void);
  74. #endif
  75. static int (*realconnect)(CONNECT_SIG);
  76. static int (*realselect)(SELECT_SIG);
  77. static int (*realpoll)(POLL_SIG);
  78. static int (*realbind)(BIND_SIG);
  79. static int (*realaccept)(ACCEPT_SIG);
  80. static int (*reallisten)(LISTEN_SIG);
  81. static int (*realsocket)(SOCKET_SIG);
  82. static int (*realsetsockopt)(SETSOCKOPT_SIG);
  83. static int (*realgetsockopt)(GETSOCKOPT_SIG);
  84. static int (*realaccept4)(ACCEPT4_SIG);
  85. /* Exported Function Prototypes */
  86. void my_init(void);
  87. int connect(CONNECT_SIG);
  88. int select(SELECT_SIG);
  89. int poll(POLL_SIG);
  90. int close(CLOSE_SIG);
  91. int bind(BIND_SIG);
  92. int accept(ACCEPT_SIG);
  93. int listen(LISTEN_SIG);
  94. int socket(SOCKET_SIG);
  95. int setsockopt(SETSOCKOPT_SIG);
  96. int getsockopt(GETSOCKOPT_SIG);
  97. int accept4(ACCEPT4_SIG);
  98. #ifdef USE_SOCKS_DNS
  99. int res_init(void);
  100. #endif
  101. int connect_to_service(void);
  102. int init_service_connection();
  103. void dwr(const char *fmt, ...);
  104. void load_symbols(void);
  105. void set_up_intercept();
  106. int checkpid();
  107. #define BUF_SZ 32
  108. #define SERVICE_CONNECT_ATTEMPTS 30
  109. #define ERR_OK 0
  110. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  111. /* threading */
  112. pthread_mutex_t lock;
  113. pthread_mutex_t loglock;
  114. /*------------------------------------------------------------------------------
  115. ------------------- Intercept<--->Service Comm mechanisms-----------------------
  116. ------------------------------------------------------------------------------*/
  117. static int is_initialized = 0;
  118. static int fdret_sock; // used for fd-transfers
  119. static int newfd; // used for "this_end" socket
  120. static int thispid;
  121. static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
  122. /*
  123. * Check for forking
  124. */
  125. int checkpid() {
  126. if(thispid != getpid()) {
  127. printf("clone/fork detected. re-initializing this instance.\n");
  128. set_up_intercept();
  129. fdret_sock = init_service_connection();
  130. thispid = getpid();
  131. }
  132. return 0;
  133. }
  134. /*
  135. * Sends an RPC command to the service
  136. */
  137. void send_command(int rpc_fd, char *cmd)
  138. {
  139. int n_write = write(rpc_fd, cmd, BUF_SZ);
  140. if(n_write < 0){
  141. dwr("Error writing command to service (CMD = %d)\n", cmd[0]);
  142. errno = 0;
  143. }
  144. }
  145. /*
  146. * Reads a return value from the service and sets errno (if applicable)
  147. */
  148. int get_retval()
  149. {
  150. if(fdret_sock >= 0) {
  151. int retval;
  152. int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
  153. char retbuf[BUF_SZ];
  154. memset(&retbuf, '\0', sz);
  155. int n_read = read(fdret_sock, &retbuf, sz);
  156. if(n_read > 0) {
  157. memcpy(&retval, &retbuf[1], sizeof(retval));
  158. memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
  159. return retval;
  160. }
  161. }
  162. dwr("unable to read connect: return value\n");
  163. return -1;
  164. }
  165. /*------------------------------------------------------------------------------
  166. ---------- Unix-domain socket lazy initializer (for fd-transfers)--------------
  167. ------------------------------------------------------------------------------*/
  168. /* Sets up the connection pipes and sockets to the service */
  169. int init_service_connection()
  170. {
  171. if(!is_initialized)
  172. {
  173. struct sockaddr_un addr;
  174. int tfd = -1, attempts = 0, conn_err = -1;
  175. memset(&addr, 0, sizeof(addr));
  176. addr.sun_family = AF_UNIX;
  177. strncpy(addr.sun_path, af_sock_name, sizeof(addr.sun_path)-1);
  178. if ( (tfd = realsocket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  179. perror("socket error");
  180. exit(-1);
  181. }
  182. while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS)
  183. {
  184. //dwr("trying connection (%d): %s\n", tfd, af_sock_name);
  185. conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
  186. if(conn_err < 0) {
  187. dwr("re-attempting connection in %ds\n", 1+attempts);
  188. sleep(1);
  189. }
  190. else {
  191. dwr("AF_UNIX connection established: %d\n", tfd);
  192. is_initialized = 1;
  193. return tfd;
  194. }
  195. attempts++;
  196. }
  197. }
  198. return -1;
  199. }
  200. /*------------------------------------------------------------------------------
  201. ------------------------ ctors and dtors (and friends)-------------------------
  202. ------------------------------------------------------------------------------*/
  203. void my_dest(void) __attribute__ ((destructor));
  204. void my_dest(void) {
  205. dwr("closing connections to service...\n");
  206. close(fdret_sock);
  207. pthread_mutex_destroy(&lock);
  208. }
  209. void load_symbols(void)
  210. {
  211. #ifdef USE_OLD_DLSYM
  212. void *lib;
  213. #endif
  214. /* possibly add check to beginning of each method to avoid needing to cll the constructor */
  215. if(thispid == getpid()) {
  216. dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
  217. }
  218. //dwr(" -- pid = %d\n", getpid());
  219. //dwr(" -- uid = %d\n", getuid());
  220. thispid = getpid();
  221. #ifndef USE_OLD_DLSYM
  222. realconnect = dlsym(RTLD_NEXT, "connect");
  223. realbind = dlsym(RTLD_NEXT, "bind");
  224. realaccept = dlsym(RTLD_NEXT, "accept");
  225. reallisten = dlsym(RTLD_NEXT, "listen");
  226. realsocket = dlsym(RTLD_NEXT, "socket");
  227. realbind = dlsym(RTLD_NEXT, "bind");
  228. realpoll = dlsym(RTLD_NEXT, "poll");
  229. realselect = dlsym(RTLD_NEXT, "select");
  230. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  231. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  232. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  233. #ifdef USE_SOCKS_DNS
  234. realresinit = dlsym(RTLD_NEXT, "res_init");
  235. #endif
  236. #else
  237. lib = dlopen(LIBCONNECT, RTLD_LAZY);
  238. realconnect = dlsym(lib, "connect");
  239. realbind = dlsym(lib, "bind");
  240. realaccept = dlsym(lib, "accept");
  241. reallisten = dlsym(lib, "listen");
  242. realsocket = dlsym(lib, "socket");
  243. realpoll = dlsym(lib, "poll");
  244. realselect = dlsym(lib, "select");
  245. realsetsockopt = dlsym(lib, "setsockopt");
  246. realgetsockopt = dlsym(lib, "getsockopt");
  247. realaccept4 = dlsym(lib), "accept4");
  248. #ifdef USE_SOCKS_DNS
  249. realresinit = dlsym(lib, "res_init");
  250. #endif
  251. dlclose(lib);
  252. lib = dlopen(LIBC, RTLD_LAZY);
  253. dlclose(lib);
  254. #endif
  255. }
  256. /* Private Function Prototypes */
  257. void _init(void) __attribute__ ((constructor));
  258. void _init(void) {
  259. set_up_intercept();
  260. }
  261. /* get symbols and initialize mutexes */
  262. void set_up_intercept()
  263. {
  264. load_symbols();
  265. if(pthread_mutex_init(&lock, NULL) != 0) {
  266. printf("error while initializing service call mutex\n");
  267. }
  268. if(pthread_mutex_init(&loglock, NULL) != 0) {
  269. printf("error while initializing log mutex mutex\n");
  270. }
  271. }
  272. /*------------------------------------------------------------------------------
  273. --------------------------------- setsockopt() ---------------------------------
  274. ------------------------------------------------------------------------------*/
  275. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  276. int setsockopt(SETSOCKOPT_SIG)
  277. {
  278. /* make sure we don't touch any standard outputs */
  279. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  280. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  281. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  282. if(err < 0){
  283. //perror("setsockopt():\n");
  284. }
  285. return 0;
  286. }
  287. /*------------------------------------------------------------------------------
  288. --------------------------------- getsockopt() ---------------------------------
  289. ------------------------------------------------------------------------------*/
  290. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  291. int getsockopt(GETSOCKOPT_SIG)
  292. {
  293. // make sure we don't touch any standard outputs
  294. int err = realgetsockopt(sockfd, level, optname, optval, optlen);
  295. // FIXME: this condition will need a little more intelligence later on
  296. // -- we will need to know if this fd is a local we are spoofing, or a true local
  297. if(optname == SO_TYPE)
  298. {
  299. int* val = (int*)optval;
  300. *val = 2;
  301. optval = (void*)val;
  302. }
  303. if(err < 0){
  304. //perror("setsockopt():\n");
  305. }
  306. return 0;
  307. }
  308. /*------------------------------------------------------------------------------
  309. ----------------------------------- socket() -----------------------------------
  310. ------------------------------------------------------------------------------*/
  311. /* int socket_family, int socket_type, int protocol
  312. socket() intercept function */
  313. int socket(SOCKET_SIG)
  314. {
  315. int err;
  316. #ifdef CHECKS
  317. /* Check that type makes sense */
  318. int flags = socket_type & ~SOCK_TYPE_MASK;
  319. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  320. errno = EINVAL;
  321. return -1;
  322. }
  323. socket_type &= SOCK_TYPE_MASK;
  324. /* Check protocol is in range */
  325. if (socket_family < 0 || socket_family >= NPROTO){
  326. errno = EAFNOSUPPORT;
  327. return -1;
  328. }
  329. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  330. errno = EINVAL;
  331. return -1;
  332. }
  333. /* Check that we haven't hit the soft-limit file descriptors allowed */
  334. /* FIXME: Find number of open fds
  335. struct rlimit rl;
  336. getrlimit(RLIMIT_NOFILE, &rl);
  337. if(sockfd >= rl.rlim_cur){
  338. errno = EMFILE;
  339. return -1;
  340. }
  341. */
  342. /* FIXME: detect ENFILE condition */
  343. #endif
  344. char cmd[BUF_SZ];
  345. fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
  346. if(socket_family == AF_LOCAL
  347. || socket_family == AF_NETLINK
  348. || socket_family == AF_UNIX) {
  349. return realsocket(socket_family, socket_type, protocol);
  350. }
  351. /* Assemble and route command */
  352. struct socket_st rpc_st;
  353. rpc_st.socket_family = socket_family;
  354. rpc_st.socket_type = socket_type;
  355. rpc_st.protocol = protocol;
  356. rpc_st.__tid = syscall(SYS_gettid);
  357. memset(cmd, '\0', BUF_SZ);
  358. cmd[0] = RPC_SOCKET;
  359. memcpy(&cmd[1], &rpc_st, sizeof(struct socket_st));
  360. pthread_mutex_lock(&lock);
  361. send_command(fdret_sock, cmd);
  362. /* get new fd */
  363. char rbuf[16];
  364. ssize_t sz = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &newfd);
  365. if(sz > 0)
  366. {
  367. /* send our local-fd number back to service so
  368. it can complete its mapping table entry */
  369. memset(cmd, '\0', BUF_SZ);
  370. cmd[0] = RPC_FD_MAP_COMPLETION;
  371. memcpy(&cmd[1], &newfd, sizeof(newfd));
  372. if(newfd > -1) {
  373. send_command(fdret_sock, cmd);
  374. pthread_mutex_unlock(&lock);
  375. errno = ERR_OK; // OK
  376. return newfd;
  377. }
  378. else { // Try to read retval+errno since we RXed a bad fd
  379. dwr("Error, service sent bad fd.\n");
  380. err = get_retval();
  381. pthread_mutex_unlock(&lock);
  382. return err;
  383. }
  384. }
  385. else {
  386. dwr("Error while receiving new FD.\n");
  387. err = get_retval();
  388. pthread_mutex_unlock(&lock);
  389. return err;
  390. }
  391. }
  392. /*------------------------------------------------------------------------------
  393. ---------------------------------- connect() -----------------------------------
  394. ------------------------------------------------------------------------------*/
  395. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  396. connect() intercept function */
  397. int connect(CONNECT_SIG)
  398. {
  399. struct sockaddr_in *connaddr;
  400. connaddr = (struct sockaddr_in *) __addr;
  401. #ifdef CHECKS
  402. /* Check that this is a valid fd */
  403. if(fcntl(__fd, F_GETFD) < 0) {
  404. return -1;
  405. errno = EBADF;
  406. }
  407. /* Check that it is a socket */
  408. int sock_type;
  409. socklen_t sock_type_len = sizeof(sock_type);
  410. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  411. errno = ENOTSOCK;
  412. return -1;
  413. }
  414. /* Check family */
  415. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  416. errno = EAFNOSUPPORT;
  417. return -1;
  418. }
  419. /* FIXME: Check that address is in user space, return EFAULT ? */
  420. #endif
  421. /* make sure we don't touch any standard outputs */
  422. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO){
  423. if (realconnect == NULL) {
  424. dwr("Unresolved symbol: connect(). Library is exiting.\n");
  425. exit(-1);
  426. }
  427. return(realconnect(__fd, __addr, __len));
  428. }
  429. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  430. || connaddr->sin_family == PF_NETLINK
  431. || connaddr->sin_family == AF_NETLINK
  432. || connaddr->sin_family == AF_UNIX)) {
  433. int err = realconnect(__fd, __addr, __len);
  434. return err;
  435. }
  436. //int flags = fcntl(__fd, F_GETFD);
  437. //dwr("connect(): socket flags = %d\n", flags);
  438. //if(sock_type && O_NONBLOCK) {
  439. // dwr("connect(): O_NONBLOCK\n");
  440. //}
  441. /* assemble and route command */
  442. int err;
  443. char cmd[BUF_SZ];
  444. memset(cmd, '\0', BUF_SZ);
  445. struct connect_st rpc_st;
  446. rpc_st.__tid = syscall(SYS_gettid);
  447. rpc_st.__fd = __fd;
  448. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr));
  449. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  450. cmd[0] = RPC_CONNECT;
  451. memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st));
  452. pthread_mutex_lock(&lock);
  453. send_command(fdret_sock, cmd);
  454. if(sock_type && O_NONBLOCK) {
  455. //pthread_mutex_unlock(&lock);
  456. //return EINPROGRESS;
  457. }
  458. err = get_retval();
  459. pthread_mutex_unlock(&lock);
  460. return err;
  461. }
  462. /*------------------------------------------------------------------------------
  463. ---------------------------------- select() ------------------------------------
  464. ------------------------------------------------------------------------------*/
  465. /* int n, fd_set *readfds, fd_set *writefds,
  466. fd_set *exceptfds, struct timeval *timeout */
  467. int select(SELECT_SIG)
  468. {
  469. return realselect(n, readfds, writefds, exceptfds, timeout);
  470. }
  471. /*------------------------------------------------------------------------------
  472. ----------------------------------- poll() -------------------------------------
  473. ------------------------------------------------------------------------------*/
  474. /* struct pollfd *__fds, nfds_t __nfds, int __timeout */
  475. int poll(POLL_SIG)
  476. {
  477. return realpoll(__fds, __nfds, __timeout);
  478. }
  479. /*------------------------------------------------------------------------------
  480. ------------------------------------ bind() ------------------------------------
  481. ------------------------------------------------------------------------------*/
  482. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  483. bind() intercept function */
  484. int bind(BIND_SIG)
  485. {
  486. #ifdef CHECKS
  487. /* Check that this is a valid fd */
  488. if(fcntl(sockfd, F_GETFD) < 0) {
  489. return -1;
  490. errno = EBADF;
  491. }
  492. /* Check that it is a socket */
  493. int sock_type = -1;
  494. socklen_t sock_type_len = sizeof(sock_type);
  495. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  496. errno = ENOTSOCK;
  497. return -1;
  498. }
  499. #endif
  500. int err;
  501. /* make sure we don't touch any standard outputs */
  502. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  503. return(realbind(sockfd, addr, addrlen));
  504. /* If local, just use normal syscall */
  505. struct sockaddr_in *connaddr;
  506. connaddr = (struct sockaddr_in *) addr;
  507. if (addr != NULL && (connaddr->sin_family == AF_LOCAL
  508. || connaddr->sin_family == PF_NETLINK
  509. || connaddr->sin_family == AF_NETLINK
  510. || connaddr->sin_family == AF_UNIX))
  511. {
  512. if(realbind == NULL) {
  513. dwr("Unresolved symbol: bind(). Library is exiting.\n");
  514. exit(-1);
  515. }
  516. return(realbind(sockfd, addr, addrlen));
  517. }
  518. /* Assemble and route command */
  519. char cmd[BUF_SZ];
  520. struct bind_st rpc_st;
  521. rpc_st.sockfd = sockfd;
  522. rpc_st.__tid = syscall(SYS_gettid);
  523. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr));
  524. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  525. cmd[0]=RPC_BIND;
  526. memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st));
  527. pthread_mutex_lock(&lock);
  528. send_command(fdret_sock, cmd);
  529. err = get_retval();
  530. pthread_mutex_unlock(&lock);
  531. errno = ERR_OK;
  532. return err;
  533. }
  534. /*------------------------------------------------------------------------------
  535. ----------------------------------- accept4() ----------------------------------
  536. ------------------------------------------------------------------------------*/
  537. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  538. int accept4(ACCEPT4_SIG)
  539. {
  540. #ifdef CHECKS
  541. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  542. errno = EINVAL;
  543. return -1;
  544. }
  545. #endif
  546. return accept(sockfd, addr, addrlen);
  547. }
  548. /*------------------------------------------------------------------------------
  549. ----------------------------------- accept() -----------------------------------
  550. ------------------------------------------------------------------------------*/
  551. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  552. accept() intercept function */
  553. int accept(ACCEPT_SIG)
  554. {
  555. #ifdef CHECKS
  556. /* Check that this is a valid fd */
  557. if(fcntl(sockfd, F_GETFD) < 0) {
  558. return -1;
  559. errno = EBADF;
  560. }
  561. /* Check that it is a socket */
  562. int sock_type;
  563. socklen_t sock_type_len = sizeof(sock_type);
  564. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  565. errno = ENOTSOCK;
  566. return -1;
  567. }
  568. /* Check that this socket supports accept() */
  569. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  570. errno = EOPNOTSUPP;
  571. return -1;
  572. }
  573. /* Check that we haven't hit the soft-limit file descriptors allowed */
  574. struct rlimit rl;
  575. getrlimit(RLIMIT_NOFILE, &rl);
  576. if(sockfd >= rl.rlim_cur){
  577. errno = EMFILE;
  578. return -1;
  579. }
  580. /* Check address length */
  581. if(addrlen < 0) {
  582. errno = EINVAL;
  583. return -1;
  584. }
  585. #endif
  586. /* make sure we don't touch any standard outputs */
  587. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  588. return(realaccept(sockfd, addr, addrlen));
  589. addr->sa_family = AF_INET;
  590. /* TODO: also get address info */
  591. char cmd[BUF_SZ];
  592. if(realaccept == NULL) {
  593. dwr( "Unresolved symbol: accept()\n");
  594. return -1;
  595. }
  596. char rbuf[16], c[1];
  597. int new_conn_socket;
  598. int n = read(sockfd, c, sizeof(c)); // Read signal byte
  599. if(n > 0)
  600. {
  601. ssize_t size = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &new_conn_socket);
  602. if(size > 0) {
  603. /* Send our local-fd number back to service so it can complete its mapping table */
  604. memset(cmd, '\0', BUF_SZ);
  605. cmd[0] = RPC_FD_MAP_COMPLETION;
  606. memcpy(&cmd[1], &new_conn_socket, sizeof(new_conn_socket));
  607. pthread_mutex_lock(&lock);
  608. int n_write = write(fdret_sock, cmd, BUF_SZ);
  609. if(n_write < 0) {
  610. dwr("Error sending perceived FD to service.\n");
  611. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  612. return -1;
  613. }
  614. pthread_mutex_unlock(&lock);
  615. //errno = ERR_OK;
  616. return new_conn_socket; // OK
  617. }
  618. else {
  619. dwr("Error receiving new FD from service.\n");
  620. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  621. return -1;
  622. }
  623. }
  624. dwr("Error reading signal byte from service.\n");
  625. //errno = EWOULDBLOCK;
  626. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  627. return -1;
  628. }
  629. /*------------------------------------------------------------------------------
  630. ------------------------------------- listen()----------------------------------
  631. ------------------------------------------------------------------------------*/
  632. /* int sockfd, int backlog
  633. listen() intercept function */
  634. int listen(LISTEN_SIG)
  635. {
  636. #ifdef CHECKS
  637. /* Check that this is a valid fd */
  638. if(fcntl(sockfd, F_GETFD) < 0) {
  639. return -1;
  640. errno = EBADF;
  641. }
  642. /* Check that it is a socket */
  643. int sock_type;
  644. socklen_t sock_type_len = sizeof(sock_type);
  645. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  646. errno = ENOTSOCK;
  647. return -1;
  648. }
  649. /* Check that this socket supports accept() */
  650. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  651. errno = EOPNOTSUPP;
  652. return -1;
  653. }
  654. #endif
  655. int err;
  656. /* make sure we don't touch any standard outputs */
  657. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  658. return(reallisten(sockfd, backlog));
  659. char cmd[BUF_SZ];
  660. /* Assemble and route command */
  661. memset(cmd, '\0', BUF_SZ);
  662. struct listen_st rpc_st;
  663. rpc_st.sockfd = sockfd;
  664. rpc_st.backlog = backlog;
  665. rpc_st.__tid = syscall(SYS_gettid);
  666. cmd[0] = RPC_LISTEN;
  667. memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
  668. pthread_mutex_lock(&lock);
  669. send_command(fdret_sock, cmd);
  670. //err = get_retval();
  671. pthread_mutex_unlock(&lock);
  672. //errno = ERR_OK;
  673. return ERR_OK;
  674. }