intercept.c 24 KB

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