diff options
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 87 |
1 files changed, 41 insertions, 46 deletions
@@ -35,7 +35,7 @@ int handle_pong_frame() | |||
35 | 35 | ||
36 | log_printf(L_INFO, "GOT PONG! Time = %.3fs\n", secs2-secs1); | 36 | log_printf(L_INFO, "GOT PONG! Time = %.3fs\n", secs2-secs1); |
37 | 37 | ||
38 | if(ping_mode) | 38 | if(program_mode == Mode_Client_Ping) |
39 | { | 39 | { |
40 | state = CLIENT_STATE_SHUTDOWN; | 40 | state = CLIENT_STATE_SHUTDOWN; |
41 | } | 41 | } |
@@ -116,12 +116,24 @@ int local_bind() | |||
116 | return 0; | 116 | return 0; |
117 | } | 117 | } |
118 | 118 | ||
119 | bool tunnel_client_mode() | ||
120 | { | ||
121 | switch (program_mode) { | ||
122 | case Mode_Client_Local_Port_Forward: | ||
123 | case Mode_Client_Pipe: | ||
124 | return true; | ||
125 | case Mode_Client_Ping: | ||
126 | default: | ||
127 | return false; | ||
128 | } | ||
129 | } | ||
130 | |||
119 | /* Bind the client.sockfd to a tunnel */ | 131 | /* Bind the client.sockfd to a tunnel */ |
120 | int handle_acktunnel_frame(protocol_frame *rcvd_frame) | 132 | int handle_acktunnel_frame(protocol_frame *rcvd_frame) |
121 | { | 133 | { |
122 | tunnel *tun; | 134 | tunnel *tun; |
123 | 135 | ||
124 | if(!client_mode) | 136 | if(!tunnel_client_mode()) |
125 | { | 137 | { |
126 | log_printf(L_WARNING, "Got ACK tunnel frame when not in client mode!?\n"); | 138 | log_printf(L_WARNING, "Got ACK tunnel frame when not in client mode!?\n"); |
127 | return -1; | 139 | return -1; |
@@ -136,23 +148,12 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame) | |||
136 | /* Mark that we can accept() another connection */ | 148 | /* Mark that we can accept() another connection */ |
137 | client_tunnel.sockfd = -1; | 149 | client_tunnel.sockfd = -1; |
138 | 150 | ||
139 | // printf("New tunnel ID: %d\n", tun->connid); | 151 | update_select_nfds(tun->sockfd); |
140 | 152 | FD_SET(tun->sockfd, &client_master_fdset); | |
141 | if(client_local_port_mode || client_pipe_mode) | 153 | if(program_mode == Mode_Client_Local_Port_Forward) |
142 | { | 154 | { |
143 | update_select_nfds(tun->sockfd); | 155 | log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port); |
144 | FD_SET(tun->sockfd, &client_master_fdset); | ||
145 | if(client_local_port_mode) | ||
146 | { | ||
147 | log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port); | ||
148 | } | ||
149 | } | 156 | } |
150 | else | ||
151 | { | ||
152 | log_printf(L_ERROR, "This tunnel mode is not supported yet\n"); | ||
153 | exit(1); | ||
154 | } | ||
155 | |||
156 | return 0; | 157 | return 0; |
157 | } | 158 | } |
158 | 159 | ||
@@ -175,25 +176,18 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame) | |||
175 | { | 176 | { |
176 | int sent_bytes; | 177 | int sent_bytes; |
177 | 178 | ||
178 | if(client_pipe_mode) | 179 | switch (program_mode) { |
179 | { | 180 | case Mode_Client_Pipe: |
180 | sent_bytes = write( | 181 | sent_bytes = write(1, rcvd_frame->data + offset, rcvd_frame->data_length - offset); |
181 | 1, /* STDOUT */ | 182 | break; |
182 | rcvd_frame->data + offset, | 183 | case Mode_Client_Local_Port_Forward: |
183 | rcvd_frame->data_length - offset | 184 | sent_bytes = send(tun->sockfd, rcvd_frame->data + offset, rcvd_frame->data_length - offset, MSG_NOSIGNAL); |
184 | ); | 185 | break; |
185 | } | 186 | default: |
186 | else | 187 | log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__); |
187 | { | 188 | return -1; |
188 | sent_bytes = send( | ||
189 | tun->sockfd, | ||
190 | rcvd_frame->data + offset, | ||
191 | rcvd_frame->data_length - offset, | ||
192 | MSG_NOSIGNAL | ||
193 | ); | ||
194 | } | 189 | } |
195 | 190 | ||
196 | |||
197 | if(sent_bytes < 0) | 191 | if(sent_bytes < 0) |
198 | { | 192 | { |
199 | uint8_t data[PROTOCOL_BUFFER_OFFSET]; | 193 | uint8_t data[PROTOCOL_BUFFER_OFFSET]; |
@@ -296,7 +290,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
296 | exit(1); | 290 | exit(1); |
297 | } | 291 | } |
298 | 292 | ||
299 | if(!ping_mode && !client_pipe_mode) | 293 | if(program_mode == Mode_Client_Local_Port_Forward) |
300 | { | 294 | { |
301 | local_bind(); | 295 | local_bind(); |
302 | signal(SIGPIPE, SIG_IGN); | 296 | signal(SIGPIPE, SIG_IGN); |
@@ -400,17 +394,19 @@ int do_client_loop(uint8_t *tox_id_str) | |||
400 | break; | 394 | break; |
401 | } | 395 | } |
402 | case CLIENT_STATE_REQUEST_ACCEPTED: | 396 | case CLIENT_STATE_REQUEST_ACCEPTED: |
403 | if(ping_mode) | 397 | switch (program_mode) { |
404 | { | 398 | case Mode_Client_Ping: |
405 | state = CLIENT_STATE_SEND_PING; | 399 | state = CLIENT_STATE_SEND_PING; |
406 | } | 400 | break; |
407 | else if(client_pipe_mode) | 401 | case Mode_Client_Pipe: |
408 | { | ||
409 | state = CLIENT_STATE_SETUP_PIPE; | 402 | state = CLIENT_STATE_SETUP_PIPE; |
410 | } | 403 | break; |
411 | else | 404 | case Mode_Client_Local_Port_Forward: |
412 | { | ||
413 | state = CLIENT_STATE_BIND_PORT; | 405 | state = CLIENT_STATE_BIND_PORT; |
406 | break; | ||
407 | default: | ||
408 | log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__); | ||
409 | exit(1); | ||
414 | } | 410 | } |
415 | break; | 411 | break; |
416 | case CLIENT_STATE_SEND_PING: | 412 | case CLIENT_STATE_SEND_PING: |
@@ -490,8 +486,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
490 | fds = client_master_fdset; | 486 | fds = client_master_fdset; |
491 | 487 | ||
492 | /* Handle accepting new connections */ | 488 | /* Handle accepting new connections */ |
493 | if(!client_pipe_mode && | 489 | if(program_mode != Mode_Client_Ping && client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ |
494 | client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ | ||
495 | { | 490 | { |
496 | accept_fd = accept(bind_sockfd, NULL, NULL); | 491 | accept_fd = accept(bind_sockfd, NULL, NULL); |
497 | if(accept_fd != -1) | 492 | if(accept_fd != -1) |
@@ -529,7 +524,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
529 | if(FD_ISSET(tun->sockfd, &fds)) | 524 | if(FD_ISSET(tun->sockfd, &fds)) |
530 | { | 525 | { |
531 | int nbytes; | 526 | int nbytes; |
532 | if(client_local_port_mode) | 527 | if(program_mode == Mode_Client_Local_Port_Forward) |
533 | { | 528 | { |
534 | nbytes = recv(tun->sockfd, | 529 | nbytes = recv(tun->sockfd, |
535 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | 530 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |