client.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include <time.h>
  2. /* MacOS related */
  3. #ifdef __MACH__
  4. #include "mach.h"
  5. #endif
  6. #include "log.h"
  7. #include "main.h"
  8. #include "client.h"
  9. /* The state machine */
  10. int state = CLIENT_STATE_INITIAL;
  11. /* Used in ping mode */
  12. struct timespec ping_sent_time;
  13. /* Client mode tunnel */
  14. tunnel client_tunnel;
  15. /* Sock representing the local port - call accept() on it */
  16. int bind_sockfd;
  17. fd_set client_master_fdset;
  18. int handle_pong_frame()
  19. {
  20. struct timespec pong_rcvd_time;
  21. double secs1, secs2;
  22. clock_gettime(CLOCK_MONOTONIC, &pong_rcvd_time);
  23. secs1 = (1.0 * ping_sent_time.tv_sec) + (1e-9 * ping_sent_time.tv_nsec);
  24. secs2 = (1.0 * pong_rcvd_time.tv_sec) + (1e-9 * pong_rcvd_time.tv_nsec);
  25. log_printf(L_INFO, "GOT PONG! Time = %.3fs\n", secs2-secs1);
  26. if(ping_mode)
  27. {
  28. state = CLIENT_STATE_SEND_PING;
  29. }
  30. return 0;
  31. }
  32. int local_bind()
  33. {
  34. struct addrinfo hints, *res;
  35. char port[6];
  36. int yes = 1;
  37. int flags;
  38. int gai_status;
  39. int setsockopt_status;
  40. snprintf(port, 6, "%d", local_port);
  41. memset(&hints, 0, sizeof hints);
  42. hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
  43. hints.ai_socktype = SOCK_STREAM;
  44. hints.ai_flags = AI_PASSIVE; // fill in my IP for me
  45. gai_status = getaddrinfo(NULL, port, &hints, &res);
  46. if(gai_status != 0)
  47. {
  48. log_printf(L_ERROR, "getaddrinfo: %s\n", gai_strerror(gai_status));
  49. exit(1);
  50. }
  51. bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  52. if(bind_sockfd < 0)
  53. {
  54. log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno));
  55. freeaddrinfo(res);
  56. exit(1);
  57. }
  58. setsockopt_status = setsockopt(bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  59. if(setsockopt_status < 0)
  60. {
  61. log_printf(L_ERROR, "Could not set socket options: %s\n",
  62. strerror(errno));
  63. freeaddrinfo(res);
  64. exit(1);
  65. }
  66. /* Set O_NONBLOCK to make accept() non-blocking */
  67. if (-1 == (flags = fcntl(bind_sockfd, F_GETFL, 0)))
  68. {
  69. flags = 0;
  70. }
  71. if(fcntl(bind_sockfd, F_SETFL, flags | O_NONBLOCK) < 0)
  72. {
  73. log_printf(L_ERROR, "Could not make the socket non-blocking: %s\n", strerror(errno));
  74. freeaddrinfo(res);
  75. exit(1);
  76. }
  77. if(bind(bind_sockfd, res->ai_addr, res->ai_addrlen) < 0)
  78. {
  79. log_printf(L_ERROR, "Bind to port %d failed: %s\n", local_port, strerror(errno));
  80. freeaddrinfo(res);
  81. close(bind_sockfd);
  82. exit(1);
  83. }
  84. freeaddrinfo(res);
  85. if(listen(bind_sockfd, 1) < 0)
  86. {
  87. log_printf(L_ERROR, "Listening on port %d failed: %s\n", local_port, strerror(errno));
  88. close(bind_sockfd);
  89. exit(1);
  90. }
  91. log_printf(L_DEBUG, "Bound to local port %d\n", local_port);
  92. return 0;
  93. }
  94. /* Bind the client.sockfd to a tunnel */
  95. int handle_acktunnel_frame(protocol_frame *rcvd_frame)
  96. {
  97. tunnel *tun;
  98. if(!client_mode)
  99. {
  100. log_printf(L_WARNING, "Got ACK tunnel frame when not in client mode!?\n");
  101. return -1;
  102. }
  103. tun = tunnel_create(
  104. client_tunnel.sockfd,
  105. rcvd_frame->connid,
  106. rcvd_frame->friendnumber
  107. );
  108. /* Mark that we can accept() another connection */
  109. client_tunnel.sockfd = -1;
  110. // printf("New tunnel ID: %d\n", tun->connid);
  111. if(client_local_port_mode || client_pipe_mode)
  112. {
  113. update_select_nfds(tun->sockfd);
  114. FD_SET(tun->sockfd, &client_master_fdset);
  115. if(client_local_port_mode)
  116. {
  117. log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port);
  118. }
  119. }
  120. else
  121. {
  122. log_printf(L_ERROR, "This tunnel mode is not supported yet\n");
  123. exit(1);
  124. }
  125. return 0;
  126. }
  127. /* Handle a TCP frame received from server */
  128. int handle_server_tcp_frame(protocol_frame *rcvd_frame)
  129. {
  130. int offset = 0;
  131. tunnel *tun = NULL;
  132. int tun_id = rcvd_frame->connid;
  133. HASH_FIND_INT(by_id, &tun_id, tun);
  134. if(!tun)
  135. {
  136. log_printf(L_WARNING, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid);
  137. return -1;
  138. }
  139. while(offset < rcvd_frame->data_length)
  140. {
  141. int sent_bytes;
  142. if(client_pipe_mode)
  143. {
  144. sent_bytes = write(
  145. 1, /* STDOUT */
  146. rcvd_frame->data + offset,
  147. rcvd_frame->data_length - offset
  148. );
  149. }
  150. else
  151. {
  152. sent_bytes = send(
  153. tun->sockfd,
  154. rcvd_frame->data + offset,
  155. rcvd_frame->data_length - offset,
  156. MSG_NOSIGNAL
  157. );
  158. }
  159. if(sent_bytes < 0)
  160. {
  161. uint8_t data[PROTOCOL_BUFFER_OFFSET];
  162. protocol_frame frame_st, *frame;
  163. log_printf(L_INFO, "Could not write to socket: %s\n", strerror(errno));
  164. frame = &frame_st;
  165. memset(frame, 0, sizeof(protocol_frame));
  166. frame->friendnumber = tun->friendnumber;
  167. frame->packet_type = PACKET_TYPE_TCP_FIN;
  168. frame->connid = tun->connid;
  169. frame->data_length = 0;
  170. send_frame(frame, data);
  171. if(tun->sockfd)
  172. {
  173. FD_CLR(tun->sockfd, &client_master_fdset);
  174. }
  175. tunnel_delete(tun);
  176. return -1;
  177. }
  178. offset += sent_bytes;
  179. }
  180. // printf("Got %d bytes from server - wrote to fd %d\n", rcvd_frame->data_length, tun->sockfd);
  181. return 0;
  182. }
  183. /* Handle close-tunnel frame recived from the server */
  184. int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame)
  185. {
  186. tunnel *tun=NULL;
  187. int connid = rcvd_frame->connid;
  188. HASH_FIND_INT(by_id, &connid, tun);
  189. if(!tun)
  190. {
  191. log_printf(L_WARNING, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid);
  192. return -1;
  193. }
  194. if(tun->friendnumber != rcvd_frame->friendnumber)
  195. {
  196. log_printf(L_WARNING, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
  197. return -1;
  198. }
  199. if(tun->sockfd)
  200. {
  201. FD_CLR(tun->sockfd, &client_master_fdset);
  202. }
  203. tunnel_delete(tun);
  204. return 0;
  205. }
  206. /* Main loop for the client */
  207. int do_client_loop(uint8_t *tox_id_str)
  208. {
  209. unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
  210. unsigned char tox_id[TOX_ADDRESS_SIZE];
  211. uint32_t friendnumber = 0;
  212. TOX_CONNECTION last_friend_connection_status = TOX_CONNECTION_NONE;
  213. time_t last_friend_connection_status_received = 0;
  214. struct timeval tv;
  215. fd_set fds;
  216. static time_t invitation_sent_time = 0;
  217. uint32_t invitations_sent = 0;
  218. TOX_ERR_FRIEND_QUERY friend_query_error;
  219. TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error;
  220. client_tunnel.sockfd = 0;
  221. FD_ZERO(&client_master_fdset);
  222. tox_callback_friend_lossless_packet(tox, parse_lossless_packet);
  223. if(!string_to_id(tox_id, tox_id_str))
  224. {
  225. log_printf(L_ERROR, "Invalid Tox ID");
  226. exit(1);
  227. }
  228. if(!ping_mode && !client_pipe_mode)
  229. {
  230. local_bind();
  231. signal(SIGPIPE, SIG_IGN);
  232. }
  233. log_printf(L_INFO, "Connecting to Tox...\n");
  234. while(1)
  235. {
  236. /* Let tox do its stuff */
  237. tox_iterate(tox, NULL);
  238. switch(state)
  239. {
  240. /*
  241. * Send friend request
  242. */
  243. case CLIENT_STATE_INITIAL:
  244. if(connection_status != TOX_CONNECTION_NONE)
  245. {
  246. state = CLIENT_STATE_CONNECTED;
  247. }
  248. break;
  249. case CLIENT_STATE_CONNECTED:
  250. {
  251. uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!";
  252. uint16_t length = sizeof(data);
  253. TOX_ERR_FRIEND_ADD add_error;
  254. if(use_shared_secret)
  255. {
  256. data = (uint8_t *)shared_secret;
  257. data[TOX_MAX_FRIEND_REQUEST_LENGTH-1] = '\0';
  258. length = strlen((char *)data)+1;
  259. log_printf(L_DEBUG, "Sent shared secret of length %u\n", length);
  260. }
  261. if(invitations_sent == 0)
  262. {
  263. log_printf(L_INFO, "Connected. Sending friend request.\n");
  264. }
  265. else
  266. {
  267. log_printf(L_INFO, "Sending another friend request.\n");
  268. }
  269. friendnumber = tox_friend_add(
  270. tox,
  271. tox_id,
  272. data,
  273. length,
  274. &add_error
  275. );
  276. if(add_error != TOX_ERR_FRIEND_ADD_OK)
  277. {
  278. unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1];
  279. id_to_string(tox_printable_id, tox_id);
  280. log_printf(L_ERROR, "Error %u adding friend %s\n", add_error, tox_printable_id);
  281. exit(-1);
  282. }
  283. invitation_sent_time = time(NULL);
  284. invitations_sent++;
  285. state = CLIENT_STATE_SENTREQUEST;
  286. log_printf(L_INFO, "Waiting for friend to accept us...\n");
  287. }
  288. break;
  289. case CLIENT_STATE_SENTREQUEST:
  290. {
  291. TOX_CONNECTION friend_connection_status;
  292. friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
  293. if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
  294. {
  295. log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u", friend_query_error);
  296. }
  297. else
  298. {
  299. last_friend_connection_status_received = time(NULL);
  300. last_friend_connection_status = friend_connection_status;
  301. if(friend_connection_status != TOX_CONNECTION_NONE)
  302. {
  303. const char* status = readable_connection_status(friend_connection_status);
  304. log_printf(L_INFO, "Friend request accepted (%s)!\n", status);
  305. state = CLIENT_STATE_REQUEST_ACCEPTED;
  306. }
  307. else
  308. {
  309. if(1 && (time(NULL) - invitation_sent_time > 45))
  310. {
  311. TOX_ERR_FRIEND_DELETE error = 0;
  312. log_printf(L_INFO, "Sending another friend request...");
  313. tox_friend_delete(
  314. tox,
  315. friendnumber,
  316. &error);
  317. if(error != TOX_ERR_FRIEND_DELETE_OK)
  318. {
  319. log_printf(L_ERROR, "Error %u deleting friend before reconnection\n", error);
  320. exit(-1);
  321. }
  322. state = CLIENT_STATE_CONNECTED;
  323. }
  324. }
  325. }
  326. break;
  327. }
  328. case CLIENT_STATE_REQUEST_ACCEPTED:
  329. if(ping_mode)
  330. {
  331. state = CLIENT_STATE_SEND_PING;
  332. }
  333. else if(client_pipe_mode)
  334. {
  335. state = CLIENT_STATE_SETUP_PIPE;
  336. }
  337. else
  338. {
  339. state = CLIENT_STATE_BIND_PORT;
  340. }
  341. break;
  342. case CLIENT_STATE_SEND_PING:
  343. /* Send the ping packet */
  344. {
  345. uint8_t data[] = {
  346. 0xa2, 0x6a, 0x01, 0x08, 0x00, 0x00, 0x00, 0x05,
  347. 0x48, 0x65, 0x6c, 0x6c, 0x6f
  348. };
  349. clock_gettime(CLOCK_MONOTONIC, &ping_sent_time);
  350. tox_friend_send_lossless_packet(
  351. tox,
  352. friendnumber,
  353. data,
  354. sizeof(data),
  355. &custom_packet_error
  356. );
  357. }
  358. if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK)
  359. {
  360. state = CLIENT_STATE_PING_SENT;
  361. }
  362. else
  363. {
  364. log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error);
  365. }
  366. break;
  367. case CLIENT_STATE_PING_SENT:
  368. /* Just sit there and wait for pong */
  369. break;
  370. case CLIENT_STATE_BIND_PORT:
  371. if(bind_sockfd < 0)
  372. {
  373. log_printf(L_ERROR, "Shutting down - could not bind to listening port\n");
  374. state = CLIENT_STATE_SHUTDOWN;
  375. }
  376. else
  377. {
  378. state = CLIENT_STATE_FORWARDING;
  379. }
  380. break;
  381. case CLIENT_STATE_SETUP_PIPE:
  382. send_tunnel_request_packet(
  383. remote_host,
  384. remote_port,
  385. friendnumber
  386. );
  387. state = CLIENT_STATE_FORWARDING;
  388. break;
  389. case CLIENT_STATE_REQUEST_TUNNEL:
  390. send_tunnel_request_packet(
  391. remote_host,
  392. remote_port,
  393. friendnumber
  394. );
  395. state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
  396. break;
  397. case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
  398. client_tunnel.sockfd = 0;
  399. send_tunnel_request_packet(
  400. remote_host,
  401. remote_port,
  402. friendnumber
  403. );
  404. break;
  405. case CLIENT_STATE_FORWARDING:
  406. {
  407. int accept_fd = 0;
  408. int select_rv = 0;
  409. tunnel *tmp = NULL;
  410. tunnel *tun = NULL;
  411. tv.tv_sec = 0;
  412. tv.tv_usec = 20000;
  413. fds = client_master_fdset;
  414. /* Handle accepting new connections */
  415. if(!client_pipe_mode &&
  416. client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */
  417. {
  418. accept_fd = accept(bind_sockfd, NULL, NULL);
  419. if(accept_fd != -1)
  420. {
  421. log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n");
  422. /* Open a new tunnel for this FD */
  423. client_tunnel.sockfd = accept_fd;
  424. send_tunnel_request_packet(
  425. remote_host,
  426. remote_port,
  427. friendnumber
  428. );
  429. }
  430. }
  431. /* Handle reading from sockets */
  432. select_rv = select(select_nfds, &fds, NULL, NULL, &tv);
  433. if(select_rv == -1 || select_rv == 0)
  434. {
  435. if(select_rv == -1)
  436. {
  437. log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n",
  438. errno, strerror(errno));
  439. }
  440. else
  441. {
  442. log_printf(L_DEBUG2, "Nothing to read...");
  443. }
  444. }
  445. else
  446. {
  447. HASH_ITER(hh, by_id, tun, tmp)
  448. {
  449. if(FD_ISSET(tun->sockfd, &fds))
  450. {
  451. int nbytes;
  452. if(client_local_port_mode)
  453. {
  454. nbytes = recv(tun->sockfd,
  455. tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
  456. READ_BUFFER_SIZE, 0);
  457. }
  458. else
  459. {
  460. nbytes = read(tun->sockfd,
  461. tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
  462. READ_BUFFER_SIZE
  463. );
  464. }
  465. /* Check if connection closed */
  466. if(nbytes == 0)
  467. {
  468. uint8_t data[PROTOCOL_BUFFER_OFFSET];
  469. protocol_frame frame_st, *frame;
  470. log_printf(L_INFO, "Connection closed\n");
  471. frame = &frame_st;
  472. memset(frame, 0, sizeof(protocol_frame));
  473. frame->friendnumber = tun->friendnumber;
  474. frame->packet_type = PACKET_TYPE_TCP_FIN;
  475. frame->connid = tun->connid;
  476. frame->data_length = 0;
  477. send_frame(frame, data);
  478. if(tun->sockfd)
  479. {
  480. FD_CLR(tun->sockfd, &client_master_fdset);
  481. }
  482. tunnel_delete(tun);
  483. }
  484. else
  485. {
  486. protocol_frame frame_st, *frame;
  487. frame = &frame_st;
  488. memset(frame, 0, sizeof(protocol_frame));
  489. frame->friendnumber = tun->friendnumber;
  490. frame->packet_type = PACKET_TYPE_TCP;
  491. frame->connid = tun->connid;
  492. frame->data_length = nbytes;
  493. send_frame(frame, tox_packet_buf);
  494. // printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid);
  495. }
  496. }
  497. }
  498. }
  499. fds = client_master_fdset;
  500. /* Check friend connection status changes */
  501. /* TODO: learned about tox_friend_connection_status_cb after writing this... */
  502. /* TODO: also check friend status tox_callback_friend_status */
  503. if(time(NULL) - last_friend_connection_status_received > 15)
  504. {
  505. TOX_CONNECTION friend_connection_status;
  506. friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
  507. if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
  508. {
  509. log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error);
  510. }
  511. else
  512. {
  513. if(friend_connection_status != last_friend_connection_status)
  514. {
  515. const char* status = readable_connection_status(friend_connection_status);
  516. log_printf(L_INFO, "Friend connection status changed to: %s\n", status);
  517. }
  518. last_friend_connection_status_received = time(NULL);
  519. last_friend_connection_status = friend_connection_status;
  520. }
  521. }
  522. }
  523. break;
  524. case CLIENT_STATE_SHUTDOWN:
  525. exit(0);
  526. break;
  527. }
  528. usleep(tox_iteration_interval(tox) * 1000);
  529. }
  530. }