client.c 14 KB

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