Intercept.c 25 KB

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