2
0

intercept.c 23 KB

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