main.c 25 KB

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