intercept.c 24 KB

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