client.c 16 KB

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