intercept.c 23 KB

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