summaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorGDR! <gdr@go2.pl>2014-12-11 23:27:26 +0100
committerGDR! <gdr@go2.pl>2014-12-11 23:27:26 +0100
commit99a66836911b804dce1455580cbf75dc99f23538 (patch)
tree2eee635f310b1dd9e39dbc45e74c60168a1fdce3 /client.c
parentc9f51df320380a537843145ebbf4ceff7ef1c561 (diff)
Pipe mode working, yay!
Diffstat (limited to 'client.c')
-rw-r--r--client.c82
1 files changed, 64 insertions, 18 deletions
diff --git a/client.c b/client.c
index 4cf6b7b..3395757 100644
--- a/client.c
+++ b/client.c
@@ -103,17 +103,20 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame)
103 /* Mark that we can accept() another connection */ 103 /* Mark that we can accept() another connection */
104 client_tunnel.sockfd = -1; 104 client_tunnel.sockfd = -1;
105 105
106 printf("New tunnel ID: %d\n", tun->connid); 106// printf("New tunnel ID: %d\n", tun->connid);
107 107
108 if(client_local_port_mode) 108 if(client_local_port_mode || client_pipe_mode)
109 { 109 {
110 update_select_nfds(tun->sockfd); 110 update_select_nfds(tun->sockfd);
111 FD_SET(tun->sockfd, &client_master_fdset); 111 FD_SET(tun->sockfd, &client_master_fdset);
112 fprintf(stderr, "Accepted a new connection on port %d\n", local_port); 112 if(client_local_port_mode)
113 {
114 fprintf(stderr, "Accepted a new connection on port %d\n", local_port);
115 }
113 } 116 }
114 else 117 else
115 { 118 {
116 fprintf(stderr, "This tunnel mode is not supported yet"); 119 fprintf(stderr, "This tunnel mode is not supported yet\n");
117 exit(1); 120 exit(1);
118 } 121 }
119} 122}
@@ -136,20 +139,33 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame)
136 while(offset < rcvd_frame->data_length) 139 while(offset < rcvd_frame->data_length)
137 { 140 {
138 int sent_bytes; 141 int sent_bytes;
142 int write_sockfd;
143
144 if(client_pipe_mode)
145 {
146 sent_bytes = write(
147 1, /* STDOUT */
148 rcvd_frame->data + offset,
149 rcvd_frame->data_length - offset
150 );
151 }
152 else
153 {
154 sent_bytes = send(
155 tun->sockfd,
156 rcvd_frame->data + offset,
157 rcvd_frame->data_length - offset,
158 MSG_NOSIGNAL
159 );
160 }
139 161
140 sent_bytes = send(
141 tun->sockfd,
142 rcvd_frame->data + offset,
143 rcvd_frame->data_length - offset,
144 MSG_NOSIGNAL
145 );
146 162
147 if(sent_bytes < 0) 163 if(sent_bytes < 0)
148 { 164 {
149 char data[PROTOCOL_BUFFER_OFFSET]; 165 char data[PROTOCOL_BUFFER_OFFSET];
150 protocol_frame frame_st, *frame; 166 protocol_frame frame_st, *frame;
151 167
152 fprintf(stderr, "Could not write to socket %d: %s\n", tun->sockfd, strerror(errno)); 168 fprintf(stderr, "Could not write to socket %d: %s\n", write_sockfd, strerror(errno));
153 169
154 frame = &frame_st; 170 frame = &frame_st;
155 memset(frame, 0, sizeof(protocol_frame)); 171 memset(frame, 0, sizeof(protocol_frame));
@@ -166,7 +182,7 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame)
166 offset += sent_bytes; 182 offset += sent_bytes;
167 } 183 }
168 184
169 printf("Got %d bytes from server - wrote to fd %d\n", rcvd_frame->data_length, tun->sockfd); 185// printf("Got %d bytes from server - wrote to fd %d\n", rcvd_frame->data_length, tun->sockfd);
170 186
171 return 0; 187 return 0;
172} 188}
@@ -213,7 +229,7 @@ int do_client_loop(char *tox_id_str)
213 exit(1); 229 exit(1);
214 } 230 }
215 231
216 if(!ping_mode) /* TODO handle pipe mode */ 232 if(!ping_mode && !client_pipe_mode)
217 { 233 {
218 local_bind(); 234 local_bind();
219 signal(SIGPIPE, SIG_IGN); 235 signal(SIGPIPE, SIG_IGN);
@@ -277,6 +293,10 @@ int do_client_loop(char *tox_id_str)
277 { 293 {
278 state = CLIENT_STATE_SEND_PING; 294 state = CLIENT_STATE_SEND_PING;
279 } 295 }
296 else if(client_pipe_mode)
297 {
298 state = CLIENT_STATE_SETUP_PIPE;
299 }
280 else 300 else
281 { 301 {
282 state = CLIENT_STATE_BIND_PORT; 302 state = CLIENT_STATE_BIND_PORT;
@@ -315,6 +335,14 @@ int do_client_loop(char *tox_id_str)
315 state = CLIENT_STATE_FORWARDING; 335 state = CLIENT_STATE_FORWARDING;
316 } 336 }
317 break; 337 break;
338 case CLIENT_STATE_SETUP_PIPE:
339 send_tunnel_request_packet(
340 remote_host,
341 remote_port,
342 friendnumber
343 );
344 state = CLIENT_STATE_FORWARDING;
345 break;
318 case CLIENT_STATE_REQUEST_TUNNEL: 346 case CLIENT_STATE_REQUEST_TUNNEL:
319 send_tunnel_request_packet( 347 send_tunnel_request_packet(
320 remote_host, 348 remote_host,
@@ -324,6 +352,12 @@ int do_client_loop(char *tox_id_str)
324 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; 352 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
325 break; 353 break;
326 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: 354 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
355 client_tunnel.sockfd = 0;
356 send_tunnel_request_packet(
357 remote_host,
358 remote_port,
359 friendnumber
360 );
327 break; 361 break;
328 case CLIENT_STATE_FORWARDING: 362 case CLIENT_STATE_FORWARDING:
329 { 363 {
@@ -336,7 +370,8 @@ int do_client_loop(char *tox_id_str)
336 fds = client_master_fdset; 370 fds = client_master_fdset;
337 371
338 /* Handle accepting new connections */ 372 /* Handle accepting new connections */
339 if(client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ 373 if(!client_pipe_mode &&
374 client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */
340 { 375 {
341 accept_fd = accept(bind_sockfd, NULL, NULL); 376 accept_fd = accept(bind_sockfd, NULL, NULL);
342 if(accept_fd != -1) 377 if(accept_fd != -1)
@@ -359,9 +394,20 @@ int do_client_loop(char *tox_id_str)
359 { 394 {
360 if(FD_ISSET(tun->sockfd, &fds)) 395 if(FD_ISSET(tun->sockfd, &fds))
361 { 396 {
362 int nbytes = recv(tun->sockfd, 397 int nbytes;
363 tox_packet_buf + PROTOCOL_BUFFER_OFFSET, 398 if(client_local_port_mode)
364 READ_BUFFER_SIZE, 0); 399 {
400 nbytes = recv(tun->sockfd,
401 tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
402 READ_BUFFER_SIZE, 0);
403 }
404 else
405 {
406 nbytes = read(tun->sockfd,
407 tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
408 READ_BUFFER_SIZE
409 );
410 }
365 411
366 /* Check if connection closed */ 412 /* Check if connection closed */
367 if(nbytes == 0) 413 if(nbytes == 0)
@@ -392,7 +438,7 @@ int do_client_loop(char *tox_id_str)
392 frame->data_length = nbytes; 438 frame->data_length = nbytes;
393 send_frame(frame, tox_packet_buf); 439 send_frame(frame, tox_packet_buf);
394 440
395 printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); 441// printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid);
396 } 442 }
397 } 443 }
398 } 444 }