intercept.c 24 KB

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