main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. #include "main.h"
  2. #include "client.h"
  3. #include "tox_bootstrap.h"
  4. static Tox_Options tox_options;
  5. Tox *tox;
  6. int client_socket = 0;
  7. /** CONFIGURATION OPTIONS **/
  8. /* Whether we're a client */
  9. int client_mode = 0;
  10. /* Just send a ping and exit */
  11. int ping_mode = 0;
  12. /* Open a local port and forward it */
  13. int client_local_port_mode = 0;
  14. /* Forward stdin/stdout to remote machine - SSH ProxyCommand mode */
  15. int client_pipe_mode = 0;
  16. /* Remote Tox ID in client mode */
  17. char *remote_tox_id = NULL;
  18. /* Directory with config and tox save */
  19. char config_path[500] = "/etc/tuntox/";
  20. /* Ports and hostname for port forwarding */
  21. int remote_port = 0;
  22. char *remote_host = NULL;
  23. int local_port = 0;
  24. fd_set master_server_fds;
  25. /* We keep two hash tables: one indexed by sockfd and another by "connection id" */
  26. tunnel *by_id = NULL;
  27. /* Highest used fd + 1 for select() */
  28. int select_nfds = 4;
  29. /* Generate an unique tunnel ID. To be used in a server. */
  30. uint16_t get_random_tunnel_id()
  31. {
  32. while(1)
  33. {
  34. int key;
  35. uint16_t tunnel_id;
  36. tunnel *tun;
  37. tunnel_id = (uint16_t)rand();
  38. key = tunnel_id;
  39. HASH_FIND_INT(by_id, &key, tun);
  40. if(!tun)
  41. {
  42. return tunnel_id;
  43. }
  44. fprintf(stderr, "[i] Found duplicated tunnel ID %d\n", key);
  45. }
  46. }
  47. void update_select_nfds(int fd)
  48. {
  49. /* TODO maybe replace with a scan every time to make select() more efficient in the long run? */
  50. if(fd + 1 > select_nfds)
  51. {
  52. select_nfds = fd + 1;
  53. }
  54. }
  55. /* Constructor. Returns NULL on failure. */
  56. tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber)
  57. {
  58. tunnel *t = NULL;
  59. t = calloc(1, sizeof(tunnel));
  60. if(!t)
  61. {
  62. return NULL;
  63. }
  64. t->sockfd = sockfd;
  65. t->connid = connid;
  66. t->friendnumber = friendnumber;
  67. fprintf(stderr, "Created a new tunnel object connid=%d sockfd=%d\n", connid, sockfd);
  68. update_select_nfds(t->sockfd);
  69. HASH_ADD_INT( by_id, connid, t );
  70. return t;
  71. }
  72. void tunnel_delete(tunnel *t)
  73. {
  74. fprintf(stderr, "Deleting tunnel #%d\n", t->connid);
  75. if(t->sockfd)
  76. {
  77. close(t->sockfd);
  78. }
  79. HASH_DEL( by_id, t );
  80. free(t);
  81. }
  82. /* bootstrap to dht with bootstrap_nodes */
  83. /* From uTox/tox.c */
  84. static void do_bootstrap(Tox *tox)
  85. {
  86. static unsigned int j = 0;
  87. if (j == 0)
  88. j = rand();
  89. int i = 0;
  90. while(i < 4) {
  91. struct bootstrap_node *d = &bootstrap_nodes[j % countof(bootstrap_nodes)];
  92. tox_bootstrap_from_address(tox, d->address, d->port, d->key);
  93. i++;
  94. j++;
  95. }
  96. }
  97. /* Set username to the machine's FQDN */
  98. void set_tox_username(Tox *tox)
  99. {
  100. unsigned char hostname[1024];
  101. struct addrinfo hints, *info, *p;
  102. int gai_result;
  103. gethostname(hostname, 1024);
  104. hostname[1023] = '\0';
  105. tox_set_name(tox, hostname, strlen(hostname));
  106. }
  107. /* Get sockaddr, IPv4 or IPv6 */
  108. void *get_in_addr(struct sockaddr *sa)
  109. {
  110. if (sa->sa_family == AF_INET)
  111. {
  112. return &(((struct sockaddr_in*)sa)->sin_addr);
  113. }
  114. return &(((struct sockaddr_in6*)sa)->sin6_addr);
  115. }
  116. int get_client_socket(char *hostname, int port)
  117. {
  118. int sockfd, numbytes;
  119. char buf[READ_BUFFER_SIZE];
  120. struct addrinfo hints, *servinfo, *p;
  121. int rv;
  122. char s[INET6_ADDRSTRLEN];
  123. char port_str[6];
  124. snprintf(port_str, 6, "%d", port);
  125. memset(&hints, 0, sizeof hints);
  126. hints.ai_family = AF_INET;
  127. hints.ai_socktype = SOCK_STREAM;
  128. if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0)
  129. {
  130. /* Add a special case for "localhost" when name resolution is broken */
  131. if(!strncmp("localhost", hostname, 256))
  132. {
  133. const char localhostname[] = "127.0.0.1";
  134. if ((rv = getaddrinfo(localhostname, port_str, &hints, &servinfo)) != 0) {
  135. fprintf(stderr, "getaddrinfo failed for 127.0.0.1: %s\n", gai_strerror(rv));
  136. return -1;
  137. }
  138. }
  139. else
  140. {
  141. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  142. return -1;
  143. }
  144. }
  145. // loop through all the results and connect to the first we can
  146. for(p = servinfo; p != NULL; p = p->ai_next)
  147. {
  148. if (p->ai_family != AF_INET && p->ai_family != AF_INET6)
  149. continue;
  150. if ((sockfd = socket(p->ai_family, p->ai_socktype,
  151. p->ai_protocol)) == -1) {
  152. perror("client: socket");
  153. continue;
  154. }
  155. if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  156. close(sockfd);
  157. perror("client: connect");
  158. continue;
  159. }
  160. break;
  161. }
  162. if (p == NULL) {
  163. fprintf(stderr, "failed to connect to %s:%d\n", hostname, port);
  164. return -1;
  165. }
  166. inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
  167. fprintf(stderr, "connecting to %s\n", s);
  168. freeaddrinfo(servinfo); // all done with this structure
  169. fprintf(stderr, "Connected to %s:%d\n", hostname, port);
  170. return sockfd;
  171. }
  172. /* Proto - our protocol handling */
  173. /*
  174. * send_frame: (almost) zero-copy. Overwrites first PROTOCOL_BUFFER_OFFSET bytes of data
  175. * so actual data should start at position PROTOCOL_BUFFER_OFFSET
  176. */
  177. int send_frame(protocol_frame *frame, uint8_t *data)
  178. {
  179. int rv = -1;
  180. int i;
  181. data[0] = PROTOCOL_MAGIC_HIGH;
  182. data[1] = PROTOCOL_MAGIC_LOW;
  183. data[2] = BYTE2(frame->packet_type);
  184. data[3] = BYTE1(frame->packet_type);
  185. data[4] = BYTE2(frame->connid);
  186. data[5] = BYTE1(frame->connid);
  187. data[6] = BYTE2(frame->data_length);
  188. data[7] = BYTE1(frame->data_length);
  189. for(i = 0; i < 17;)
  190. {
  191. int j;
  192. rv = tox_send_lossless_packet(
  193. tox,
  194. frame->friendnumber,
  195. data,
  196. frame->data_length + PROTOCOL_BUFFER_OFFSET
  197. );
  198. if(rv < 0)
  199. {
  200. /* If this branch is ran, most likely we've hit congestion control. */
  201. fprintf(stderr, "[%d] Failed to send packet to friend %d\n", i, frame->friendnumber);
  202. }
  203. else
  204. {
  205. break;
  206. }
  207. if(i == 0) i = 2;
  208. else i = i * 2;
  209. for(j = 0; j < i; j++)
  210. {
  211. tox_do(tox);
  212. usleep(j * 10000);
  213. }
  214. }
  215. if(i > 0 && rv >= 0)
  216. {
  217. fprintf(stderr, "Packet succeeded at try %d\n", i+1);
  218. }
  219. return rv;
  220. }
  221. int send_tunnel_ack_frame(tunnel *tun)
  222. {
  223. protocol_frame frame_st;
  224. protocol_frame *frame;
  225. char data[PROTOCOL_BUFFER_OFFSET];
  226. frame = &frame_st;
  227. memset(frame, 0, sizeof(protocol_frame));
  228. frame->packet_type = PACKET_TYPE_ACKTUNNEL;
  229. frame->connid = tun->connid;
  230. frame->data_length = 0;
  231. frame->friendnumber = tun->friendnumber;
  232. return send_frame(frame, data);
  233. }
  234. int handle_ping_frame(protocol_frame *rcvd_frame)
  235. {
  236. uint8_t data[TOX_MAX_CUSTOM_PACKET_SIZE];
  237. protocol_frame frame_s;
  238. protocol_frame *frame = &frame_s;
  239. frame->data = data + PROTOCOL_BUFFER_OFFSET;
  240. memcpy(frame->data, rcvd_frame->data, rcvd_frame->data_length);
  241. frame->friendnumber = rcvd_frame->friendnumber;
  242. frame->packet_type = PACKET_TYPE_PONG;
  243. frame->data_length = rcvd_frame->data_length;
  244. send_frame(frame, data);
  245. }
  246. int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
  247. {
  248. char *hostname = NULL;
  249. tunnel *tun;
  250. int port = -1;
  251. int sockfd = 0;
  252. uint16_t tunnel_id;
  253. if(client_mode)
  254. {
  255. fprintf(stderr, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber);
  256. return -1;
  257. }
  258. port = rcvd_frame->connid;
  259. hostname = calloc(1, rcvd_frame->data_length + 1);
  260. if(!hostname)
  261. {
  262. fprintf(stderr, "Could not allocate memory for tunnel request hostname\n");
  263. return -1;
  264. }
  265. strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length);
  266. hostname[rcvd_frame->data_length] = '\0';
  267. fprintf(stderr, "Got a request to forward data from %s:%d\n", hostname, port);
  268. tunnel_id = get_random_tunnel_id();
  269. fprintf(stderr, "Tunnel ID: %d\n", tunnel_id);
  270. /* TODO make connection */
  271. sockfd = get_client_socket(hostname, port);
  272. if(sockfd > 0)
  273. {
  274. tun = tunnel_create(sockfd, tunnel_id, rcvd_frame->friendnumber);
  275. if(tun)
  276. {
  277. FD_SET(sockfd, &master_server_fds);
  278. update_select_nfds(sockfd);
  279. fprintf(stderr, "Created tunnel, yay!\n");
  280. send_tunnel_ack_frame(tun);
  281. }
  282. else
  283. {
  284. fprintf(stderr, "Couldn't allocate memory for tunnel\n");
  285. }
  286. }
  287. else
  288. {
  289. fprintf(stderr, "Could not connect to %s:%d\n", hostname, port);
  290. /* TODO send reject */
  291. }
  292. }
  293. /* Handle a TCP frame received from client */
  294. int handle_client_tcp_frame(protocol_frame *rcvd_frame)
  295. {
  296. tunnel *tun=NULL;
  297. int offset = 0;
  298. int connid = rcvd_frame->connid;
  299. HASH_FIND_INT(by_id, &connid, tun);
  300. if(!tun)
  301. {
  302. fprintf(stderr, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid);
  303. return -1;
  304. }
  305. if(tun->friendnumber != rcvd_frame->friendnumber)
  306. {
  307. fprintf(stderr, "Friend #%d tried to send packet to a tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
  308. return -1;
  309. }
  310. while(offset < rcvd_frame->data_length)
  311. {
  312. int sent_bytes;
  313. sent_bytes = send(
  314. tun->sockfd,
  315. rcvd_frame->data + offset,
  316. rcvd_frame->data_length - offset,
  317. MSG_NOSIGNAL
  318. );
  319. if(sent_bytes < 0)
  320. {
  321. fprintf(stderr, "Could not write to socket %d: %s\n", tun->sockfd, strerror(errno));
  322. return -1;
  323. }
  324. offset += sent_bytes;
  325. }
  326. return 0;
  327. }
  328. /* Handle close-tunnel frame received from the client */
  329. int handle_client_tcp_fin_frame(protocol_frame *rcvd_frame)
  330. {
  331. tunnel *tun=NULL;
  332. int offset = 0;
  333. int connid = rcvd_frame->connid;
  334. HASH_FIND_INT(by_id, &connid, tun);
  335. if(!tun)
  336. {
  337. fprintf(stderr, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid);
  338. return -1;
  339. }
  340. if(tun->friendnumber != rcvd_frame->friendnumber)
  341. {
  342. fprintf(stderr, "Friend #%d tried to close tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
  343. return -1;
  344. }
  345. tunnel_delete(tun);
  346. }
  347. /* This is a dispatcher for our encapsulated protocol */
  348. int handle_frame(protocol_frame *frame)
  349. {
  350. switch(frame->packet_type)
  351. {
  352. case PACKET_TYPE_PING:
  353. return handle_ping_frame(frame);
  354. break;
  355. case PACKET_TYPE_PONG:
  356. return handle_pong_frame(frame);
  357. break;
  358. case PACKET_TYPE_TCP:
  359. if(client_mode)
  360. {
  361. return handle_server_tcp_frame(frame);
  362. }
  363. else
  364. {
  365. return handle_client_tcp_frame(frame);
  366. }
  367. break;
  368. case PACKET_TYPE_REQUESTTUNNEL:
  369. handle_request_tunnel_frame(frame);
  370. break;
  371. case PACKET_TYPE_ACKTUNNEL:
  372. handle_acktunnel_frame(frame);
  373. break;
  374. case PACKET_TYPE_TCP_FIN:
  375. if(client_mode)
  376. {
  377. return handle_server_tcp_fin_frame(frame);
  378. }
  379. else
  380. {
  381. return handle_client_tcp_fin_frame(frame);
  382. }
  383. break;
  384. default:
  385. fprintf(stderr, "Got unknown packet type 0x%x from friend %d\n",
  386. frame->packet_type,
  387. frame->friendnumber
  388. );
  389. }
  390. return 0;
  391. }
  392. /*
  393. * This is a callback which gets a packet from Tox core.
  394. * It checks for basic inconsistiencies and allocates the
  395. * protocol_frame structure.
  396. */
  397. int parse_lossless_packet(void *sender_uc, const uint8_t *data, uint32_t len)
  398. {
  399. protocol_frame *frame = NULL;
  400. if(len < PROTOCOL_BUFFER_OFFSET)
  401. {
  402. fprintf(stderr, "Received too short data frame - only %d bytes, at least %d expected\n", len, PROTOCOL_BUFFER_OFFSET);
  403. return -1;
  404. }
  405. if(data[0] != PROTOCOL_MAGIC_HIGH || data[1] != PROTOCOL_MAGIC_LOW)
  406. {
  407. fprintf(stderr, "Received data frame with invalid protocol magic number 0x%x%x\n", data[0], data[1]);
  408. return -1;
  409. }
  410. frame = calloc(1, sizeof(protocol_frame));
  411. if(!frame)
  412. {
  413. fprintf(stderr, "Could not allocate memory for protocol_frame_t\n");
  414. return -1;
  415. }
  416. /* TODO check if friendnumber is the same in sender and connid tunnel*/
  417. frame->magic = INT16_AT(data, 0);
  418. frame->packet_type = INT16_AT(data, 2);
  419. frame->connid = INT16_AT(data, 4);
  420. frame->data_length = INT16_AT(data, 6);
  421. frame->data = (uint8_t *)(data + PROTOCOL_BUFFER_OFFSET);
  422. frame->friendnumber = *((uint32_t*)sender_uc);
  423. fprintf(stderr, "Got protocol frame magic 0x%x type 0x%x from friend %d\n", frame->magic, frame->packet_type, frame->friendnumber);
  424. if(len < frame->data_length + PROTOCOL_BUFFER_OFFSET)
  425. {
  426. fprintf(stderr, "Received frame too small (attempted buffer overflow?): %d bytes, excepted at least %d bytes\n", len, frame->data_length + PROTOCOL_BUFFER_OFFSET);
  427. return -1;
  428. }
  429. if(frame->data_length > (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET))
  430. {
  431. fprintf(stderr, "Declared data length too big (attempted buffer overflow?): %d bytes, excepted at most %d bytes\n", frame->data_length, (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET));
  432. return -1;
  433. }
  434. handle_frame(frame);
  435. }
  436. int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_number)
  437. {
  438. int packet_length = 0;
  439. protocol_frame frame_i, *frame;
  440. char *data = NULL;
  441. fprintf(stderr, "Sending packet to friend #%d to forward %s:%d\n", friend_number, remote_host, remote_port);
  442. packet_length = PROTOCOL_BUFFER_OFFSET + strlen(remote_host);
  443. frame = &frame_i;
  444. data = calloc(1, packet_length);
  445. if(!data)
  446. {
  447. fprintf(stderr, "Could not allocate memory for tunnel request packet\n");
  448. exit(1);
  449. }
  450. strcpy(data+PROTOCOL_BUFFER_OFFSET, remote_host);
  451. frame->friendnumber = friend_number;
  452. frame->packet_type = PACKET_TYPE_REQUESTTUNNEL;
  453. frame->connid = remote_port;
  454. frame->data_length = strlen(remote_host);
  455. send_frame(frame, data);
  456. free(data);
  457. return 0;
  458. }
  459. /* End proto */
  460. /* Save tox identity to a file */
  461. static void write_save(Tox *tox)
  462. {
  463. void *data;
  464. uint32_t size;
  465. uint8_t path_tmp[512], path_real[512], *p;
  466. FILE *file;
  467. size = tox_size(tox);
  468. data = malloc(size);
  469. tox_save(tox, data);
  470. strncpy(path_real, config_path, sizeof(config_path));
  471. p = path_real + strlen(path_real);
  472. memcpy(p, "tox_save", sizeof("tox_save"));
  473. unsigned int path_len = (p - path_real) + sizeof("tox_save");
  474. memcpy(path_tmp, path_real, path_len);
  475. memcpy(path_tmp + (path_len - 1), ".tmp", sizeof(".tmp"));
  476. file = fopen((char*)path_tmp, "wb");
  477. if(file) {
  478. fwrite(data, size, 1, file);
  479. fflush(file);
  480. fclose(file);
  481. if (rename((char*)path_tmp, (char*)path_real) != 0) {
  482. fprintf(stderr, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real);
  483. remove((const char *)path_real);
  484. if (rename((char*)path_tmp, (char*)path_real) != 0) {
  485. fprintf(stderr, "Saving Failed\n");
  486. } else {
  487. fprintf(stderr, "Saved data\n");
  488. }
  489. } else {
  490. fprintf(stderr, "Saved data\n");
  491. }
  492. }
  493. else
  494. {
  495. fprintf(stderr, "Could not open save file\n");
  496. }
  497. free(data);
  498. }
  499. /* Load tox identity from a file */
  500. static int load_save(Tox *tox)
  501. {
  502. void *data;
  503. uint32_t size;
  504. uint8_t path_tmp[512], path_real[512], *p;
  505. FILE *file;
  506. strncpy(path_real, config_path, sizeof(config_path));
  507. p = path_real + strlen(path_real);
  508. memcpy(p, "tox_save", sizeof("tox_save"));
  509. unsigned int path_len = (p - path_real) + sizeof("tox_save");
  510. data = file_raw((char *)path_real, &size);
  511. if(data)
  512. {
  513. tox_load(tox, data, size);
  514. free(data);
  515. return 1;
  516. }
  517. else
  518. {
  519. fprintf(stderr, "Could not open save file\n");
  520. return 0;
  521. }
  522. }
  523. void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
  524. {
  525. unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
  526. int32_t friendnumber;
  527. int32_t *friendnumber_ptr = NULL;
  528. fprintf(stderr, "Got friend request\n");
  529. friendnumber = tox_add_friend_norequest(tox, public_key);
  530. memset(tox_printable_id, '\0', sizeof(tox_printable_id));
  531. id_to_string(tox_printable_id, public_key);
  532. fprintf(stderr, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber);
  533. /* TODO: this is not freed right now, we're leaking 4 bytes per contact (OMG!) */
  534. friendnumber_ptr = malloc(sizeof(int32_t));
  535. if(!friendnumber_ptr)
  536. {
  537. fprintf(stderr, "Could not allocate memory for friendnumber_ptr\n");
  538. return;
  539. }
  540. *friendnumber_ptr = friendnumber;
  541. tox_lossless_packet_registerhandler(tox, friendnumber, (PROTOCOL_MAGIC_V1)>>8, parse_lossless_packet, (void*)friendnumber_ptr);
  542. }
  543. void cleanup(int status, void *tmp)
  544. {
  545. fprintf(stderr, "kthxbye\n");
  546. fflush(stdout);
  547. tox_kill(tox);
  548. if(client_socket)
  549. {
  550. close(client_socket);
  551. }
  552. }
  553. int do_server_loop()
  554. {
  555. struct timeval tv;
  556. fd_set fds;
  557. unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
  558. tunnel *tun = NULL;
  559. tunnel *tmp = NULL;
  560. int connected = 0;
  561. tv.tv_sec = 0;
  562. tv.tv_usec = 20000;
  563. FD_ZERO(&master_server_fds);
  564. while(1)
  565. {
  566. int tmp_isconnected = 0;
  567. /* Let tox do its stuff */
  568. tox_do(tox);
  569. /* Check change in connection state */
  570. tmp_isconnected = tox_isconnected(tox);
  571. if(tmp_isconnected != connected)
  572. {
  573. connected = tmp_isconnected;
  574. if(connected)
  575. {
  576. fprintf(stderr, "Connected to Tox network\n");
  577. }
  578. else
  579. {
  580. fprintf(stderr, "Disconnected from Tox network\n");
  581. }
  582. }
  583. fds = master_server_fds;
  584. /* Poll for data from our client connection */
  585. select(select_nfds, &fds, NULL, NULL, &tv);
  586. HASH_ITER(hh, by_id, tun, tmp)
  587. {
  588. if(FD_ISSET(tun->sockfd, &fds))
  589. {
  590. int nbytes = recv(tun->sockfd,
  591. tox_packet_buf+PROTOCOL_BUFFER_OFFSET,
  592. READ_BUFFER_SIZE, 0);
  593. /* Check if connection closed */
  594. if(nbytes == 0)
  595. {
  596. char data[PROTOCOL_BUFFER_OFFSET];
  597. protocol_frame frame_st, *frame;
  598. fprintf(stderr, "conn closed!\n");
  599. frame = &frame_st;
  600. memset(frame, 0, sizeof(protocol_frame));
  601. frame->friendnumber = tun->friendnumber;
  602. frame->packet_type = PACKET_TYPE_TCP_FIN;
  603. frame->connid = tun->connid;
  604. frame->data_length = 0;
  605. send_frame(frame, data);
  606. tunnel_delete(tun);
  607. continue;
  608. }
  609. else
  610. {
  611. protocol_frame frame_st, *frame;
  612. frame = &frame_st;
  613. memset(frame, 0, sizeof(protocol_frame));
  614. frame->friendnumber = tun->friendnumber;
  615. frame->packet_type = PACKET_TYPE_TCP;
  616. frame->connid = tun->connid;
  617. frame->data_length = nbytes;
  618. send_frame(frame, tox_packet_buf);
  619. }
  620. }
  621. }
  622. }
  623. }
  624. void help()
  625. {
  626. fprintf(stderr, "tuntox - Forward ports over the Tox protocol\n");
  627. fprintf(stderr, "USAGE:\n\n");
  628. fprintf(stderr, "-i <toxid> - remote point Tox ID\n");
  629. fprintf(stderr, "-L <localport>:<remotehostname>:<remoteport> - forward <remotehostname>:<remoteport> to 127.0.0.1:<localport>\n");
  630. fprintf(stderr, "-P <remotehostname>:<remoteport> - forward <remotehostname>:<remoteport> to stdin/stdout (SSH ProxyCommand mode)\n");
  631. fprintf(stderr, "-p - ping the server from -i and exit\n");
  632. fprintf(stderr, "-C <dir> - save private key in <dir> instead of /etc/tuntox in server mode\n");
  633. }
  634. int main(int argc, char *argv[])
  635. {
  636. unsigned char tox_id[TOX_FRIEND_ADDRESS_SIZE];
  637. unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
  638. int oc;
  639. while ((oc = getopt(argc, argv, "L:pi:C:P:")) != -1)
  640. {
  641. switch(oc)
  642. {
  643. case 'L':
  644. /* Local port forwarding */
  645. client_mode = 1;
  646. client_local_port_mode = 1;
  647. if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0)
  648. {
  649. fprintf(stderr, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n");
  650. exit(1);
  651. }
  652. fprintf(stderr, "Forwarding remote port %d to local port %d\n", remote_port, local_port);
  653. break;
  654. case 'P':
  655. /* Pipe forwarding */
  656. client_mode = 1;
  657. client_pipe_mode = 1;
  658. if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0)
  659. {
  660. fprintf(stderr, "Invalid value for -P option - use something like -P 127.0.0.1:22\n");
  661. exit(1);
  662. }
  663. fprintf(stderr, "Forwarding remote port %d to stdin/out\n", remote_port);
  664. break;
  665. case 'p':
  666. /* Ping */
  667. client_mode = 1;
  668. ping_mode = 1;
  669. break;
  670. case 'i':
  671. /* Tox ID */
  672. remote_tox_id = optarg;
  673. break;
  674. case 'C':
  675. /* Config directory */
  676. strncpy(config_path, optarg, sizeof(config_path) - 1);
  677. if(optarg[strlen(optarg) - 1] != '/')
  678. {
  679. int optarg_len = strlen(optarg);
  680. config_path[optarg_len] = '/';
  681. config_path[optarg_len + 1] = '\0';
  682. }
  683. break;
  684. case '?':
  685. default:
  686. help();
  687. exit(1);
  688. }
  689. }
  690. on_exit(cleanup, NULL);
  691. /* Bootstrap tox */
  692. tox_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
  693. tox_options.udp_disabled = 0;
  694. tox_options.proxy_enabled = 0;
  695. tox = tox_new(&tox_options);
  696. set_tox_username(tox);
  697. do_bootstrap(tox);
  698. /* TODO use proper argparse */
  699. if(client_mode)
  700. {
  701. tox_get_address(tox, tox_id);
  702. id_to_string(tox_printable_id, tox_id);
  703. tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
  704. fprintf(stderr, "Generated Tox ID: %s\n", tox_printable_id);
  705. if(!remote_tox_id)
  706. {
  707. fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n");
  708. exit(1);
  709. }
  710. do_client_loop(remote_tox_id);
  711. }
  712. else
  713. {
  714. /* Connect to the forwarded service */
  715. // client_socket = get_client_socket();
  716. if(!load_save(tox))
  717. {
  718. /* Write generated ID if one is not already present */
  719. write_save(tox);
  720. }
  721. tox_get_address(tox, tox_id);
  722. memset(tox_printable_id, '\0', sizeof(tox_printable_id));
  723. id_to_string(tox_printable_id, tox_id);
  724. tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
  725. fprintf(stderr, "Using Tox ID: %s\n", tox_printable_id);
  726. tox_callback_friend_request(tox, accept_friend_request, NULL);
  727. do_server_loop();
  728. }
  729. return 0;
  730. }