Intercept.c 22 KB

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