intercept.c 25 KB

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