diff options
author | Andrew Cady <d@jerkface.net> | 2020-08-21 18:16:29 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2020-08-21 18:16:29 -0400 |
commit | 4dbabac9eccd43445808c583777ba853b1ad1155 (patch) | |
tree | e94b5b72c440e2ec90f75fa49ed251720012317b | |
parent | 2a7edb9cda824338c4f7dcb00b264f37e3a6928c (diff) |
split large function in two, changing nothing
-rw-r--r-- | client.c | 228 |
1 files changed, 115 insertions, 113 deletions
@@ -79,8 +79,7 @@ int local_bind() | |||
79 | setsockopt_status = setsockopt(bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); | 79 | setsockopt_status = setsockopt(bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); |
80 | if(setsockopt_status < 0) | 80 | if(setsockopt_status < 0) |
81 | { | 81 | { |
82 | log_printf(L_ERROR, "Could not set socket options: %s\n", | 82 | log_printf(L_ERROR, "Could not set socket options: %s\n", strerror(errno)); |
83 | strerror(errno)); | ||
84 | freeaddrinfo(res); | 83 | freeaddrinfo(res); |
85 | exit(1); | 84 | exit(1); |
86 | } | 85 | } |
@@ -228,7 +227,7 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame) | |||
228 | } | 227 | } |
229 | 228 | ||
230 | /* Delete tunnel and clear client-side fdset */ | 229 | /* Delete tunnel and clear client-side fdset */ |
231 | void client_close_tunnel(tunnel *tun) | 230 | void client_close_tunnel(tunnel *tun) |
232 | { | 231 | { |
233 | if(tun->sockfd) | 232 | if(tun->sockfd) |
234 | { | 233 | { |
@@ -275,14 +274,123 @@ void client_close_all_connections() | |||
275 | } | 274 | } |
276 | } | 275 | } |
277 | 276 | ||
277 | void client_connected_loop_iteration(uint32_t friendnumber) | ||
278 | { | ||
279 | static unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; | ||
280 | static fd_set fds; | ||
281 | static struct timeval tv; | ||
282 | |||
283 | int accept_fd = 0; | ||
284 | int select_rv = 0; | ||
285 | tunnel *tmp = NULL; | ||
286 | tunnel *tun = NULL; | ||
287 | |||
288 | tv.tv_sec = 0; | ||
289 | tv.tv_usec = 20000; | ||
290 | fds = client_master_fdset; | ||
291 | |||
292 | /* Handle accepting new connections */ | ||
293 | if(program_mode != Mode_Client_Ping && client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ | ||
294 | { | ||
295 | accept_fd = accept(bind_sockfd, NULL, NULL); | ||
296 | if(accept_fd != -1) | ||
297 | { | ||
298 | log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n"); | ||
299 | |||
300 | /* Open a new tunnel for this FD */ | ||
301 | client_tunnel.sockfd = accept_fd; | ||
302 | send_tunnel_request_packet(remote_host, remote_port, friendnumber); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | /* Handle reading from sockets */ | ||
307 | select_rv = select(select_nfds, &fds, NULL, NULL, &tv); | ||
308 | if(select_rv == -1 || select_rv == 0) | ||
309 | { | ||
310 | if(select_rv == -1) | ||
311 | { | ||
312 | log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", errno, strerror(errno)); | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | log_printf(L_DEBUG3, "Nothing to read..."); | ||
317 | } | ||
318 | } | ||
319 | else | ||
320 | { | ||
321 | HASH_ITER(hh, by_id, tun, tmp) | ||
322 | { | ||
323 | if(FD_ISSET(tun->sockfd, &fds)) | ||
324 | { | ||
325 | int nbytes; | ||
326 | if(program_mode == Mode_Client_Local_Port_Forward) | ||
327 | { | ||
328 | nbytes = recv(tun->sockfd, | ||
329 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | ||
330 | READ_BUFFER_SIZE, 0); | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | nbytes = read(tun->sockfd, | ||
335 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | ||
336 | READ_BUFFER_SIZE | ||
337 | ); | ||
338 | } | ||
339 | |||
340 | /* Check if connection closed */ | ||
341 | if(nbytes == 0) | ||
342 | { | ||
343 | uint8_t data[PROTOCOL_BUFFER_OFFSET]; | ||
344 | protocol_frame frame_st, *frame; | ||
345 | |||
346 | log_printf(L_INFO, "Connection closed\n"); | ||
347 | |||
348 | frame = &frame_st; | ||
349 | memset(frame, 0, sizeof(protocol_frame)); | ||
350 | frame->friendnumber = tun->friendnumber; | ||
351 | frame->packet_type = PACKET_TYPE_TCP_FIN; | ||
352 | frame->connid = tun->connid; | ||
353 | frame->data_length = 0; | ||
354 | send_frame(frame, data); | ||
355 | if(tun->sockfd) | ||
356 | { | ||
357 | FD_CLR(tun->sockfd, &client_master_fdset); | ||
358 | } | ||
359 | tunnel_delete(tun); | ||
360 | } | ||
361 | else | ||
362 | { | ||
363 | protocol_frame frame_st, *frame; | ||
364 | |||
365 | frame = &frame_st; | ||
366 | memset(frame, 0, sizeof(protocol_frame)); | ||
367 | frame->friendnumber = tun->friendnumber; | ||
368 | frame->packet_type = PACKET_TYPE_TCP; | ||
369 | frame->connid = tun->connid; | ||
370 | frame->data_length = nbytes; | ||
371 | send_frame(frame, tox_packet_buf); | ||
372 | |||
373 | // printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); | ||
374 | } | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | |||
379 | fds = client_master_fdset; | ||
380 | |||
381 | /* Check friend connection status changes */ | ||
382 | if(friend_connection_status == TOX_CONNECTION_NONE) | ||
383 | { | ||
384 | log_printf(L_ERROR, "Lost connection to server. Exiting."); | ||
385 | exit(1); | ||
386 | } | ||
387 | } | ||
388 | |||
278 | /* Main loop for the client */ | 389 | /* Main loop for the client */ |
279 | int do_client_loop(uint8_t *tox_id_str) | 390 | int do_client_loop(uint8_t *tox_id_str) |
280 | { | 391 | { |
281 | unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; | ||
282 | unsigned char tox_id[TOX_ADDRESS_SIZE]; | 392 | unsigned char tox_id[TOX_ADDRESS_SIZE]; |
283 | uint32_t friendnumber = 0; | 393 | uint32_t friendnumber = 0; |
284 | struct timeval tv; | ||
285 | fd_set fds; | ||
286 | static time_t invitation_sent_time = 0; | 394 | static time_t invitation_sent_time = 0; |
287 | uint32_t invitations_sent = 0; | 395 | uint32_t invitations_sent = 0; |
288 | TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error; | 396 | TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error; |
@@ -424,113 +532,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
424 | case CLIENT_STATE_AWAIT_PONG: | 532 | case CLIENT_STATE_AWAIT_PONG: |
425 | break; | 533 | break; |
426 | case CLIENT_STATE_CONNECTED: | 534 | case CLIENT_STATE_CONNECTED: |
427 | { | 535 | client_connected_loop_iteration(friendnumber); |
428 | int accept_fd = 0; | ||
429 | int select_rv = 0; | ||
430 | tunnel *tmp = NULL; | ||
431 | tunnel *tun = NULL; | ||
432 | |||
433 | tv.tv_sec = 0; | ||
434 | tv.tv_usec = 20000; | ||
435 | fds = client_master_fdset; | ||
436 | |||
437 | /* Handle accepting new connections */ | ||
438 | if(program_mode != Mode_Client_Ping && client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ | ||
439 | { | ||
440 | accept_fd = accept(bind_sockfd, NULL, NULL); | ||
441 | if(accept_fd != -1) | ||
442 | { | ||
443 | log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n"); | ||
444 | |||
445 | /* Open a new tunnel for this FD */ | ||
446 | client_tunnel.sockfd = accept_fd; | ||
447 | send_tunnel_request_packet(remote_host, remote_port, friendnumber); | ||
448 | } | ||
449 | } | ||
450 | |||
451 | /* Handle reading from sockets */ | ||
452 | select_rv = select(select_nfds, &fds, NULL, NULL, &tv); | ||
453 | if(select_rv == -1 || select_rv == 0) | ||
454 | { | ||
455 | if(select_rv == -1) | ||
456 | { | ||
457 | log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", | ||
458 | errno, strerror(errno)); | ||
459 | } | ||
460 | else | ||
461 | { | ||
462 | log_printf(L_DEBUG3, "Nothing to read..."); | ||
463 | } | ||
464 | } | ||
465 | else | ||
466 | { | ||
467 | HASH_ITER(hh, by_id, tun, tmp) | ||
468 | { | ||
469 | if(FD_ISSET(tun->sockfd, &fds)) | ||
470 | { | ||
471 | int nbytes; | ||
472 | if(program_mode == Mode_Client_Local_Port_Forward) | ||
473 | { | ||
474 | nbytes = recv(tun->sockfd, | ||
475 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | ||
476 | READ_BUFFER_SIZE, 0); | ||
477 | } | ||
478 | else | ||
479 | { | ||
480 | nbytes = read(tun->sockfd, | ||
481 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | ||
482 | READ_BUFFER_SIZE | ||
483 | ); | ||
484 | } | ||
485 | |||
486 | /* Check if connection closed */ | ||
487 | if(nbytes == 0) | ||
488 | { | ||
489 | uint8_t data[PROTOCOL_BUFFER_OFFSET]; | ||
490 | protocol_frame frame_st, *frame; | ||
491 | |||
492 | log_printf(L_INFO, "Connection closed\n"); | ||
493 | |||
494 | frame = &frame_st; | ||
495 | memset(frame, 0, sizeof(protocol_frame)); | ||
496 | frame->friendnumber = tun->friendnumber; | ||
497 | frame->packet_type = PACKET_TYPE_TCP_FIN; | ||
498 | frame->connid = tun->connid; | ||
499 | frame->data_length = 0; | ||
500 | send_frame(frame, data); | ||
501 | if(tun->sockfd) | ||
502 | { | ||
503 | FD_CLR(tun->sockfd, &client_master_fdset); | ||
504 | } | ||
505 | tunnel_delete(tun); | ||
506 | } | ||
507 | else | ||
508 | { | ||
509 | protocol_frame frame_st, *frame; | ||
510 | |||
511 | frame = &frame_st; | ||
512 | memset(frame, 0, sizeof(protocol_frame)); | ||
513 | frame->friendnumber = tun->friendnumber; | ||
514 | frame->packet_type = PACKET_TYPE_TCP; | ||
515 | frame->connid = tun->connid; | ||
516 | frame->data_length = nbytes; | ||
517 | send_frame(frame, tox_packet_buf); | ||
518 | |||
519 | // printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); | ||
520 | } | ||
521 | } | ||
522 | } | ||
523 | } | ||
524 | |||
525 | fds = client_master_fdset; | ||
526 | |||
527 | /* Check friend connection status changes */ | ||
528 | if(friend_connection_status == TOX_CONNECTION_NONE) | ||
529 | { | ||
530 | log_printf(L_ERROR, "Lost connection to server. Exiting."); | ||
531 | exit(1); | ||
532 | } | ||
533 | } | ||
534 | break; | 536 | break; |
535 | } | 537 | } |
536 | 538 | ||