intercept.c 23 KB

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