intercept.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. /* Global Declarations */
  65. #ifdef USE_SOCKS_DNS
  66. static int (*realresinit)(void);
  67. #endif
  68. static int (*realconnect)(CONNECT_SIG);
  69. static int (*realselect)(SELECT_SIG);
  70. static int (*realpoll)(POLL_SIG);
  71. static int (*realbind)(BIND_SIG);
  72. static int (*realaccept)(ACCEPT_SIG);
  73. static int (*reallisten)(LISTEN_SIG);
  74. static int (*realsocket)(SOCKET_SIG);
  75. static int (*realsetsockopt)(SETSOCKOPT_SIG);
  76. static int (*realgetsockopt)(GETSOCKOPT_SIG);
  77. static int (*realaccept4)(ACCEPT4_SIG);
  78. /* Exported Function Prototypes */
  79. void my_init(void);
  80. int connect(CONNECT_SIG);
  81. int select(SELECT_SIG);
  82. int poll(POLL_SIG);
  83. int close(CLOSE_SIG);
  84. int bind(BIND_SIG);
  85. int accept(ACCEPT_SIG);
  86. int listen(LISTEN_SIG);
  87. int socket(SOCKET_SIG);
  88. int setsockopt(SETSOCKOPT_SIG);
  89. int getsockopt(GETSOCKOPT_SIG);
  90. int accept4(ACCEPT4_SIG);
  91. #ifdef USE_SOCKS_DNS
  92. int res_init(void);
  93. #endif
  94. int connect_to_service(void);
  95. int init_service_connection();
  96. void dwr(const char *fmt, ...);
  97. void load_symbols(void);
  98. void set_up_intercept();
  99. int checkpid();
  100. /* defined in unistd.h, but we don't include it because
  101. it conflicts with our overriden symbols for read/write */
  102. #define STDIN_FILENO 0
  103. #define STDOUT_FILENO 1
  104. #define STDERR_FILENO 2
  105. #define BUF_SZ 1024
  106. #define SERVICE_CONNECT_ATTEMPTS 30
  107. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  108. /* threading */
  109. pthread_mutex_t lock;
  110. pthread_mutex_t loglock;
  111. /*------------------------------------------------------------------------------
  112. ------------------- Intercept<--->Service Comm mechanisms-----------------------
  113. ------------------------------------------------------------------------------*/
  114. static int is_initialized = 0;
  115. static int fdret_sock; // used for fd-transfers
  116. static int newfd; // used for "this_end" socket
  117. static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
  118. static int thispid;
  119. /*
  120. *
  121. * Check for forking
  122. *
  123. */
  124. int checkpid() {
  125. if(thispid != getpid()) {
  126. printf("clone/fork detected. re-initializing this instance.\n");
  127. set_up_intercept();
  128. fdret_sock = init_service_connection();
  129. thispid = getpid();
  130. }
  131. return 0;
  132. }
  133. /*
  134. *
  135. * Reads a return value from the service and sets errno (if applicable)
  136. *
  137. */
  138. int get_retval()
  139. {
  140. if(fdret_sock >= 0) {
  141. int retval;
  142. int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
  143. char retbuf[BUF_SZ];
  144. memset(&retbuf, '\0', sz);
  145. int n_read = read(fdret_sock, &retbuf, sz);
  146. if(n_read > 0) {
  147. memcpy(&retval, &retbuf[1], sizeof(retval));
  148. memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
  149. return retval;
  150. }
  151. else {
  152. dwr("unable to read connect: return value\n");
  153. return -1;
  154. }
  155. }
  156. }
  157. #define SLEEP_TIME 0
  158. /*------------------------------------------------------------------------------
  159. ---------- Unix-domain socket lazy initializer (for fd-transfers)--------------
  160. ------------------------------------------------------------------------------*/
  161. /* Sets up the connection pipes and sockets to the service */
  162. int init_service_connection()
  163. {
  164. usleep(SLEEP_TIME);
  165. if(!is_initialized)
  166. {
  167. struct sockaddr_un addr;
  168. int tfd = -1;
  169. memset(&addr, 0, sizeof(addr));
  170. addr.sun_family = AF_UNIX;
  171. strncpy(addr.sun_path, af_sock_name, sizeof(addr.sun_path)-1);
  172. int attempts = 0;
  173. int conn_err = -1;
  174. if ( (tfd = realsocket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  175. perror("socket error");
  176. exit(-1);
  177. }
  178. while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS)
  179. {
  180. dwr("trying connection (%d): %s\n", tfd, af_sock_name);
  181. conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
  182. if(conn_err < 0) {
  183. dwr("re-attempting connection in %ds\n", 1+attempts);
  184. sleep(1);
  185. }
  186. else {
  187. dwr("AF_UNIX connection established: %d\n", tfd);
  188. is_initialized = 1;
  189. return tfd;
  190. }
  191. attempts++;
  192. }
  193. }
  194. return -1;
  195. }
  196. /*------------------------------------------------------------------------------
  197. ------------------------ ctors and dtors (and friends)-------------------------
  198. ------------------------------------------------------------------------------*/
  199. void my_dest(void) __attribute__ ((destructor));
  200. void my_dest(void) {
  201. dwr("closing connections to service...\n");
  202. close(fdret_sock);
  203. pthread_mutex_destroy(&lock);
  204. }
  205. void load_symbols(void)
  206. {
  207. #ifdef USE_OLD_DLSYM
  208. void *lib;
  209. #endif
  210. /* possibly add check to beginning of each method to avoid needing to cll the constructor */
  211. if(thispid == getpid()) {
  212. dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
  213. }
  214. dwr(" -- pid = %d\n", getpid());
  215. dwr(" -- uid = %d\n", getuid());
  216. thispid = getpid();
  217. #ifndef USE_OLD_DLSYM
  218. realconnect = dlsym(RTLD_NEXT, "connect");
  219. realbind = dlsym(RTLD_NEXT, "bind");
  220. realaccept = dlsym(RTLD_NEXT, "accept");
  221. reallisten = dlsym(RTLD_NEXT, "listen");
  222. realsocket = dlsym(RTLD_NEXT, "socket");
  223. realbind = dlsym(RTLD_NEXT, "bind");
  224. realpoll = dlsym(RTLD_NEXT, "poll");
  225. realselect = dlsym(RTLD_NEXT, "select");
  226. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  227. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  228. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  229. #ifdef USE_SOCKS_DNS
  230. realresinit = dlsym(RTLD_NEXT, "res_init");
  231. #endif
  232. #else
  233. lib = dlopen(LIBCONNECT, RTLD_LAZY);
  234. realconnect = dlsym(lib, "connect");
  235. realbind = dlsym(lib, "bind");
  236. realaccept = dlsym(lib, "accept");
  237. reallisten = dlsym(lib, "listen");
  238. realsocket = dlsym(lib, "socket");
  239. realpoll = dlsym(lib, "poll");
  240. realselect = dlsym(lib, "select");
  241. realsetsockopt = dlsym(lib, "setsockopt");
  242. realgetsockopt = dlsym(lib, "getsockopt");
  243. realaccept4 = dlsym(lib), "accept4");
  244. #ifdef USE_SOCKS_DNS
  245. realresinit = dlsym(lib, "res_init");
  246. #endif
  247. dlclose(lib);
  248. lib = dlopen(LIBC, RTLD_LAZY);
  249. dlclose(lib);
  250. #endif
  251. }
  252. /* Private Function Prototypes */
  253. void _init(void) __attribute__ ((constructor));
  254. void _init(void) {
  255. set_up_intercept();
  256. }
  257. /* get symbols and initialize mutexes */
  258. void set_up_intercept()
  259. {
  260. load_symbols();
  261. if(pthread_mutex_init(&lock, NULL) != 0) {
  262. printf("error while initializing service call mutex\n");
  263. }
  264. if(pthread_mutex_init(&loglock, NULL) != 0) {
  265. printf("error while initializing log mutex mutex\n");
  266. }
  267. }
  268. /*------------------------------------------------------------------------------
  269. ------------------------- ioctl(), fcntl(), setsockopt()------------------------
  270. ------------------------------------------------------------------------------*/
  271. char *cmd_to_str(int cmd)
  272. {
  273. switch(cmd)
  274. {
  275. case F_DUPFD:
  276. return "F_DUPFD";
  277. case F_GETFD:
  278. return "F_GETFD";
  279. case F_SETFD:
  280. return "F_SETFD";
  281. case F_GETFL:
  282. return "F_GETFL";
  283. case F_SETFL:
  284. return "F_SETFL";
  285. case F_GETLK:
  286. return "F_GETLK";
  287. case F_SETLK:
  288. return "F_SETLK";
  289. case F_SETLKW:
  290. return "F_SETLKW";
  291. default:
  292. return "?";
  293. }
  294. return "?";
  295. }
  296. void arg_to_str(int arg)
  297. {
  298. if(arg & O_RDONLY) dwr("O_RDONLY ");
  299. if(arg & O_WRONLY) dwr("O_WRONLY ");
  300. if(arg & O_RDWR) dwr("O_RDWR ");
  301. if(arg & O_CREAT) dwr("O_CREAT ");
  302. if(arg & O_EXCL) dwr("O_EXCL ");
  303. if(arg & O_NOCTTY) dwr("O_NOCTTY ");
  304. if(arg & O_TRUNC) dwr("O_TRUNC ");
  305. if(arg & O_APPEND) dwr("O_APPEND ");
  306. if(arg & O_ASYNC) dwr("O_ASYNC ");
  307. if(arg & O_DIRECT) dwr("O_DIRECT ");
  308. if(arg & O_NOATIME) dwr("O_NOATIME ");
  309. if(arg & O_NONBLOCK) dwr("O_NONBLOCK ");
  310. if(arg & O_DSYNC) dwr("O_DSYNC ");
  311. if(arg & O_SYNC) dwr("O_SYNC ");
  312. }
  313. char* level_to_str(int level)
  314. {
  315. switch(level)
  316. {
  317. case SOL_SOCKET:
  318. return "SOL_SOCKET";
  319. case IPPROTO_TCP:
  320. return "IPPROTO_TCP";
  321. default:
  322. return "?";
  323. }
  324. return "?";
  325. }
  326. char* option_name_to_str(int opt)
  327. {
  328. if(opt == SO_DEBUG) return "SO_DEBUG";
  329. if(opt == SO_BROADCAST) return "SO_BROADCAST";
  330. if(opt == SO_BINDTODEVICE) return "SO_BINDTODEVICE";
  331. if(opt == SO_REUSEADDR) return "SO_REUSEADDR";
  332. if(opt == SO_KEEPALIVE) return "SO_KEEPALIVE";
  333. if(opt == SO_LINGER) return "SO_LINGER";
  334. if(opt == SO_OOBINLINE) return "SO_OOBINLINE";
  335. if(opt == SO_SNDBUF) return "SO_SNDBUF";
  336. if(opt == SO_RCVBUF) return "SO_RCVBUF";
  337. if(opt == SO_DONTROUTE) return "SO_DONTROUTEO_ASYNC";
  338. if(opt == SO_RCVLOWAT) return "SO_RCVLOWAT";
  339. if(opt == SO_RCVTIMEO) return "SO_RCVTIMEO";
  340. if(opt == SO_SNDLOWAT) return "SO_SNDLOWAT";
  341. if(opt == SO_SNDTIMEO)return "SO_SNDTIMEO";
  342. return "?";
  343. }
  344. /*------------------------------------------------------------------------------
  345. --------------------------------- setsockopt() ---------------------------------
  346. ------------------------------------------------------------------------------*/
  347. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  348. int setsockopt(SETSOCKOPT_SIG)
  349. {
  350. #ifdef DUMMY
  351. dwr("setsockopt(%d)\n", socket);
  352. return realsetsockopt(socket, level, option_name, option_value, option_len);
  353. #else
  354. /* make sure we don't touch any standard outputs */
  355. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  356. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  357. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  358. if(err < 0){
  359. //perror("setsockopt():\n");
  360. }
  361. return 0;
  362. #endif
  363. }
  364. /*------------------------------------------------------------------------------
  365. --------------------------------- getsockopt() ---------------------------------
  366. ------------------------------------------------------------------------------*/
  367. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  368. int getsockopt(GETSOCKOPT_SIG)
  369. {
  370. #ifdef DUMMY
  371. dwr("getsockopt(%d)\n", sockfd);
  372. return realgetsockopt(sockfd, level, optname, optval, optlen);
  373. #else
  374. // make sure we don't touch any standard outputs
  375. int err = realgetsockopt(sockfd, level, optname, optval, optlen);
  376. // FIXME: this condition will need a little more intelligence later on
  377. // -- we will need to know if this fd is a local we are spoofing, or a true local
  378. if(optname == SO_TYPE)
  379. {
  380. int* val = (int*)optval;
  381. *val = 2;
  382. optval = (void*)val;
  383. }
  384. if(err < 0){
  385. //perror("setsockopt():\n");
  386. }
  387. return 0;
  388. #endif
  389. }
  390. /*------------------------------------------------------------------------------
  391. ---------------------------------- shutdown() ----------------------------------
  392. ------------------------------------------------------------------------------*/
  393. void shutdown_arg_to_str(int arg)
  394. {
  395. if(arg & O_RDONLY) dwr("O_RDONLY ");
  396. if(arg & O_WRONLY) dwr("O_WRONLY ");
  397. if(arg & O_RDWR) dwr("O_RDWR ");
  398. if(arg & O_CREAT) dwr("O_CREAT ");
  399. if(arg & O_EXCL) dwr("O_EXCL ");
  400. if(arg & O_NOCTTY) dwr("O_NOCTTY ");
  401. if(arg & O_TRUNC) dwr("O_TRUNC ");
  402. if(arg & O_APPEND) dwr("O_APPEND ");
  403. if(arg & O_ASYNC) dwr("O_ASYNC ");
  404. if(arg & O_DIRECT) dwr("O_DIRECT ");
  405. if(arg & O_NOATIME) dwr("O_NOATIME ");
  406. if(arg & O_NONBLOCK) dwr("O_NONBLOCK ");
  407. if(arg & O_DSYNC) dwr("O_DSYNC ");
  408. if(arg & O_SYNC) dwr("O_SYNC ");
  409. }
  410. /*------------------------------------------------------------------------------
  411. ----------------------------------- socket() -----------------------------------
  412. ------------------------------------------------------------------------------*/
  413. void sock_type_to_str(int arg)
  414. {
  415. if(arg == SOCK_STREAM) printf("SOCK_STREAM ");
  416. if(arg == SOCK_DGRAM) printf("SOCK_DGRAM ");
  417. if(arg == SOCK_SEQPACKET) printf("SOCK_SEQPACKET ");
  418. if(arg == SOCK_RAW) printf("SOCK_RAW ");
  419. if(arg == SOCK_RDM) printf("SOCK_RDM ");
  420. if(arg == SOCK_PACKET) printf("SOCK_PACKET ");
  421. if(arg & SOCK_NONBLOCK) printf("| SOCK_NONBLOCK ");
  422. if(arg & SOCK_CLOEXEC) printf("| SOCK_CLOEXEC ");
  423. }
  424. void sock_domain_to_str(int domain)
  425. {
  426. if(domain == AF_UNIX) printf("AF_UNIX ");
  427. if(domain == AF_LOCAL) printf("AF_LOCAL ");
  428. if(domain == AF_INET) printf("AF_INET ");
  429. if(domain == AF_INET6) printf("AF_INET6 ");
  430. if(domain == AF_IPX) printf("AF_IPX ");
  431. if(domain == AF_NETLINK) printf("AF_NETLINK ");
  432. if(domain == AF_X25) printf("AF_X25 ");
  433. if(domain == AF_AX25) printf("AF_AX25 ");
  434. if(domain == AF_ATMPVC) printf("AF_ATMPVC ");
  435. if(domain == AF_APPLETALK) printf("AF_APPLETALK ");
  436. if(domain == AF_PACKET) printf("AF_PACKET ");
  437. }
  438. /* int socket_family, int socket_type, int protocol
  439. socket() intercept function */
  440. int socket(SOCKET_SIG)
  441. {
  442. #ifdef DUMMY
  443. dwr("socket(fam=%d, type=%d, prot=%d)\n", socket_family, socket_type, protocol);
  444. return realsocket(socket_family, socket_type, protocol);
  445. #else
  446. char cmd[BUF_SZ];
  447. fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
  448. if(socket_family == AF_LOCAL
  449. || socket_family == AF_NETLINK
  450. || socket_family == AF_UNIX) {
  451. int err = realsocket(socket_family, socket_type, protocol);
  452. return err;
  453. }
  454. /* Assemble and route command */
  455. struct socket_st rpc_st;
  456. rpc_st.socket_family = socket_family;
  457. rpc_st.socket_type = socket_type;
  458. rpc_st.protocol = protocol;
  459. rpc_st.__tid = syscall(SYS_gettid);
  460. memset(cmd, '\0', BUF_SZ);
  461. cmd[0] = RPC_SOCKET;
  462. memcpy(&cmd[1], &rpc_st, sizeof(struct socket_st));
  463. pthread_mutex_lock(&lock);
  464. write(fdret_sock,cmd, BUF_SZ);
  465. /* get new fd */
  466. char gmybuf[16];
  467. ssize_t size = sock_fd_read(fdret_sock, gmybuf, sizeof(gmybuf), &newfd);
  468. if(size > 0)
  469. {
  470. dwr("socket(): RXed FD = %d\n", newfd);
  471. /* send our local-fd number back to service so
  472. it can complete its mapping table entry */
  473. memset(cmd, '\0', BUF_SZ);
  474. cmd[0] = RPC_FD_MAP_COMPLETION;
  475. memcpy(&cmd[1], &newfd, sizeof(newfd));
  476. write(fdret_sock, cmd, BUF_SZ);
  477. pthread_mutex_unlock(&lock);
  478. return newfd;
  479. }
  480. else {
  481. dwr("Error while receiving new FD.\n");
  482. pthread_mutex_unlock(&lock);
  483. return -1;
  484. }
  485. return realsocket(socket_family, socket_type, protocol);
  486. #endif
  487. }
  488. /*------------------------------------------------------------------------------
  489. ---------------------------------- connect() -----------------------------------
  490. ------------------------------------------------------------------------------*/
  491. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  492. connect() intercept function */
  493. int connect(CONNECT_SIG)
  494. {
  495. #ifdef DUMMY
  496. dwr("connect(%d)\n", __fd);
  497. return realconnect(__fd, __addr, __len);
  498. #else
  499. /* make sure we don't touch any standard outputs */
  500. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO)
  501. return(realconnect(__fd, __addr, __len));
  502. int sock_type = -1;
  503. socklen_t sock_type_len = sizeof(sock_type);
  504. struct sockaddr_in *connaddr;
  505. connaddr = (struct sockaddr_in *) __addr;
  506. getsockopt(__fd, SOL_SOCKET, SO_TYPE,
  507. (void *) &sock_type, &sock_type_len);
  508. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  509. || connaddr->sin_family == PF_NETLINK
  510. || connaddr->sin_family == AF_NETLINK
  511. || connaddr->sin_family == AF_UNIX)) {
  512. int err = realconnect(__fd, __addr, __len);
  513. return err;
  514. }
  515. char cmd[BUF_SZ];
  516. if (realconnect == NULL) {
  517. dwr("Unresolved symbol: connect()\n");
  518. return -1;
  519. }
  520. /* assemble and route command */
  521. memset(cmd, '\0', BUF_SZ);
  522. struct connect_st rpc_st;
  523. rpc_st.__tid = syscall(SYS_gettid);
  524. rpc_st.__fd = __fd;
  525. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr));
  526. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  527. cmd[0] = RPC_CONNECT;
  528. memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st));
  529. pthread_mutex_lock(&lock);
  530. write(fdret_sock,cmd, BUF_SZ);
  531. if(fdret_sock >= 0) {
  532. int retval;
  533. char mynewbuf[BUF_SZ];
  534. memset(&mynewbuf, '\0', sizeof(mynewbuf));
  535. int n_read = read(fdret_sock, &mynewbuf, sizeof(mynewbuf));
  536. if(n_read > 0) {
  537. memcpy(&retval, &mynewbuf[1], sizeof(int));
  538. pthread_mutex_unlock(&lock);
  539. return retval;
  540. }
  541. else {
  542. pthread_mutex_unlock(&lock);
  543. dwr("unable to read connect: return value\n");
  544. }
  545. }
  546. return -1;
  547. #endif
  548. }
  549. /*------------------------------------------------------------------------------
  550. ---------------------------------- select() ------------------------------------
  551. ------------------------------------------------------------------------------*/
  552. /* int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout */
  553. int select(SELECT_SIG)
  554. {
  555. #ifdef DUMMY
  556. dwr("select(n=%d, <readfds>, <writefds>, <exceptfds>, <timeout>)\n", n);
  557. return realselect(n, readfds, writefds, exceptfds, timeout);
  558. #else
  559. return realselect(n, readfds, writefds, exceptfds, timeout);
  560. #endif
  561. }
  562. /*------------------------------------------------------------------------------
  563. ----------------------------------- poll() -------------------------------------
  564. ------------------------------------------------------------------------------*/
  565. /* struct pollfd *__fds, nfds_t __nfds, int __timeout */
  566. int poll(POLL_SIG)
  567. {
  568. #ifdef DUMMY
  569. dwr("poll(<ufds>, nfds=%d, timeout=%d)\n", __fds, __timeout);
  570. return realpoll(__fds, __nfds, __timeout);
  571. #else
  572. return realpoll(__fds, __nfds, __timeout);
  573. #endif
  574. }
  575. /*------------------------------------------------------------------------------
  576. ------------------------------------ bind() ------------------------------------
  577. ------------------------------------------------------------------------------*/
  578. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  579. bind() intercept function */
  580. int bind(BIND_SIG)
  581. {
  582. #ifdef DUMMY
  583. dwr("bind(%d)\n", sockfd);
  584. return realbind(sockfd, addr, addrlen);
  585. #else
  586. /* make sure we don't touch any standard outputs */
  587. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  588. return(realbind(sockfd, addr, addrlen));
  589. int sock_type = -1;
  590. socklen_t sock_type_len = sizeof(sock_type);
  591. struct sockaddr_in *connaddr;
  592. connaddr = (struct sockaddr_in *) addr;
  593. getsockopt(sockfd, SOL_SOCKET, SO_TYPE,
  594. (void *) &sock_type, &sock_type_len);
  595. if (addr != NULL && (connaddr->sin_family == AF_LOCAL
  596. || connaddr->sin_family == PF_NETLINK
  597. || connaddr->sin_family == AF_NETLINK
  598. || connaddr->sin_family == AF_UNIX)) {
  599. return(realbind(sockfd, addr, addrlen));
  600. }
  601. char cmd[BUF_SZ];
  602. if(realbind == NULL) {
  603. dwr("Unresolved symbol: bind()\n");
  604. return -1;
  605. }
  606. /* Assemble and route command */
  607. struct bind_st rpc_st;
  608. rpc_st.sockfd = sockfd;
  609. rpc_st.__tid = syscall(SYS_gettid);
  610. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr));
  611. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  612. cmd[0]=RPC_BIND;
  613. memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st));
  614. pthread_mutex_lock(&lock);
  615. write(fdret_sock, cmd, BUF_SZ);
  616. pthread_mutex_unlock(&lock);
  617. return get_retval();
  618. #endif
  619. }
  620. /*------------------------------------------------------------------------------
  621. ----------------------------------- accept4() ----------------------------------
  622. ------------------------------------------------------------------------------*/
  623. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  624. int accept4(ACCEPT4_SIG)
  625. {
  626. #ifdef DUMMY
  627. dwr("accept4(%d)\n", sockfd);
  628. return accept(sockfd, addr, addrlen);
  629. #else
  630. return accept(sockfd, addr, addrlen);
  631. #endif
  632. }
  633. /*------------------------------------------------------------------------------
  634. ----------------------------------- accept() -----------------------------------
  635. ------------------------------------------------------------------------------*/
  636. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  637. accept() intercept function */
  638. int accept(ACCEPT_SIG)
  639. {
  640. #ifdef DUMMY
  641. return realaccept(sockfd, addr, addrlen);
  642. #else
  643. /* make sure we don't touch any standard outputs */
  644. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  645. return(realaccept(sockfd, addr, addrlen));
  646. int sock_type = -1;
  647. socklen_t sock_type_len = sizeof(sock_type);
  648. getsockopt(sockfd, SOL_SOCKET, SO_TYPE,
  649. (void *) &sock_type, &sock_type_len);
  650. addr->sa_family = AF_INET;
  651. /* TODO: also get address info */
  652. char cmd[BUF_SZ];
  653. if(realaccept == NULL) {
  654. dwr( "Unresolved symbol: accept()\n");
  655. return -1;
  656. }
  657. char gmybuf[16];
  658. int new_conn_socket;
  659. char c[1];
  660. int n = read(sockfd, c, sizeof(c));
  661. if(n > 0)
  662. {
  663. ssize_t size = sock_fd_read(fdret_sock, gmybuf, sizeof(gmybuf), &new_conn_socket);
  664. if(size > 0)
  665. {
  666. dwr("accept(): RXed FD = %d\n", new_conn_socket);
  667. /* Send our local-fd number back to service so it can complete its mapping table */
  668. memset(cmd, '\0', BUF_SZ);
  669. cmd[0] = RPC_FD_MAP_COMPLETION;
  670. memcpy(&cmd[1], &new_conn_socket, sizeof(new_conn_socket));
  671. pthread_mutex_lock(&lock);
  672. write(fdret_sock, cmd, BUF_SZ);
  673. pthread_mutex_unlock(&lock);
  674. return new_conn_socket;
  675. }
  676. else {
  677. dwr("Error while receiving new FD.\n");
  678. return -1;
  679. }
  680. }
  681. errno = EWOULDBLOCK;
  682. return -1;
  683. /* TODO/FIXME: Set errno */
  684. #endif
  685. }
  686. /*------------------------------------------------------------------------------
  687. ------------------------------------- listen()----------------------------------
  688. ------------------------------------------------------------------------------*/
  689. /* int sockfd, int backlog
  690. listen() intercept function */
  691. int listen(LISTEN_SIG)
  692. {
  693. #ifdef DUMMY
  694. dwr("listen(%d)\n", sockfd);
  695. return reallisten(sockfd, backlog);
  696. #else
  697. /* make sure we don't touch any standard outputs */
  698. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  699. return(reallisten(sockfd, backlog));
  700. char cmd[BUF_SZ];
  701. dwr("listen(%d)\n", sockfd);
  702. /* Assemble and route command */
  703. memset(cmd, '\0', BUF_SZ);
  704. struct listen_st rpc_st;
  705. rpc_st.sockfd = sockfd;
  706. rpc_st.backlog = backlog;
  707. rpc_st.__tid = syscall(SYS_gettid);
  708. cmd[0] = RPC_LISTEN;
  709. memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
  710. pthread_mutex_lock(&lock);
  711. write(fdret_sock,cmd, BUF_SZ);
  712. pthread_mutex_unlock(&lock);
  713. return 0;
  714. /* FIXME: get real return value (should be 0 / -1) */
  715. /* FIXME: Also set errno */
  716. #endif
  717. }