File client.c changed (mode: 100644) (index 3073e5d..40e7cd9) |
... |
... |
int state = CLIENT_STATE_INITIAL; |
15 |
15 |
/* Used in ping mode */ |
/* Used in ping mode */ |
16 |
16 |
struct timespec ping_sent_time; |
struct timespec ping_sent_time; |
17 |
17 |
|
|
18 |
|
/* Client mode tunnel */ |
|
19 |
|
tunnel client_tunnel; |
|
20 |
|
|
|
21 |
|
/* Sock representing the local port - call accept() on it */ |
|
22 |
|
int bind_sockfd; |
|
23 |
|
|
|
24 |
18 |
fd_set client_master_fdset; |
fd_set client_master_fdset; |
25 |
19 |
|
|
26 |
20 |
int handle_pong_frame() |
int handle_pong_frame() |
|
... |
... |
int handle_pong_frame() |
42 |
36 |
return 0; |
return 0; |
43 |
37 |
} |
} |
44 |
38 |
|
|
45 |
|
int local_bind() |
|
|
39 |
|
int local_bind_one(local_port_forward *port_forward) |
46 |
40 |
{ |
{ |
47 |
41 |
struct addrinfo hints, *res; |
struct addrinfo hints, *res; |
48 |
42 |
char port[6]; |
char port[6]; |
|
... |
... |
int local_bind() |
51 |
45 |
int gai_status; |
int gai_status; |
52 |
46 |
int setsockopt_status; |
int setsockopt_status; |
53 |
47 |
|
|
54 |
|
snprintf(port, 6, "%d", local_port); |
|
|
48 |
|
snprintf(port, 6, "%d", port_forward->local_port); |
55 |
49 |
|
|
56 |
50 |
memset(&hints, 0, sizeof hints); |
memset(&hints, 0, sizeof hints); |
57 |
51 |
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever |
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever |
|
... |
... |
int local_bind() |
65 |
59 |
exit(1); |
exit(1); |
66 |
60 |
} |
} |
67 |
61 |
|
|
68 |
|
bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
|
69 |
|
if(bind_sockfd < 0) |
|
|
62 |
|
port_forward->bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
|
63 |
|
if(port_forward->bind_sockfd < 0) |
70 |
64 |
{ |
{ |
71 |
65 |
log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno)); |
log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno)); |
72 |
66 |
freeaddrinfo(res); |
freeaddrinfo(res); |
73 |
67 |
exit(1); |
exit(1); |
74 |
68 |
} |
} |
75 |
69 |
|
|
76 |
|
setsockopt_status = setsockopt(bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); |
|
|
70 |
|
setsockopt_status = setsockopt(port_forward->bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); |
77 |
71 |
if(setsockopt_status < 0) |
if(setsockopt_status < 0) |
78 |
72 |
{ |
{ |
79 |
73 |
log_printf(L_ERROR, "Could not set socket options: %s\n", |
log_printf(L_ERROR, "Could not set socket options: %s\n", |
|
... |
... |
int local_bind() |
83 |
77 |
} |
} |
84 |
78 |
|
|
85 |
79 |
/* Set O_NONBLOCK to make accept() non-blocking */ |
/* Set O_NONBLOCK to make accept() non-blocking */ |
86 |
|
if (-1 == (flags = fcntl(bind_sockfd, F_GETFL, 0))) |
|
|
80 |
|
if (-1 == (flags = fcntl(port_forward->bind_sockfd, F_GETFL, 0))) |
87 |
81 |
{ |
{ |
88 |
82 |
flags = 0; |
flags = 0; |
89 |
83 |
} |
} |
90 |
|
if(fcntl(bind_sockfd, F_SETFL, flags | O_NONBLOCK) < 0) |
|
|
84 |
|
if(fcntl(port_forward->bind_sockfd, F_SETFL, flags | O_NONBLOCK) < 0) |
91 |
85 |
{ |
{ |
92 |
86 |
log_printf(L_ERROR, "Could not make the socket non-blocking: %s\n", strerror(errno)); |
log_printf(L_ERROR, "Could not make the socket non-blocking: %s\n", strerror(errno)); |
93 |
87 |
freeaddrinfo(res); |
freeaddrinfo(res); |
94 |
88 |
exit(1); |
exit(1); |
95 |
89 |
} |
} |
96 |
90 |
|
|
97 |
|
if(bind(bind_sockfd, res->ai_addr, res->ai_addrlen) < 0) |
|
|
91 |
|
if(bind(port_forward->bind_sockfd, res->ai_addr, res->ai_addrlen) < 0) |
98 |
92 |
{ |
{ |
99 |
|
log_printf(L_ERROR, "Bind to port %d failed: %s\n", local_port, strerror(errno)); |
|
|
93 |
|
log_printf(L_ERROR, "Bind to port %d failed: %s\n", port_forward->local_port, strerror(errno)); |
100 |
94 |
freeaddrinfo(res); |
freeaddrinfo(res); |
101 |
|
close(bind_sockfd); |
|
|
95 |
|
close(port_forward->bind_sockfd); |
102 |
96 |
exit(1); |
exit(1); |
103 |
97 |
} |
} |
104 |
98 |
|
|
105 |
99 |
freeaddrinfo(res); |
freeaddrinfo(res); |
106 |
100 |
|
|
107 |
|
if(listen(bind_sockfd, 1) < 0) |
|
|
101 |
|
if(listen(port_forward->bind_sockfd, 1) < 0) |
108 |
102 |
{ |
{ |
109 |
|
log_printf(L_ERROR, "Listening on port %d failed: %s\n", local_port, strerror(errno)); |
|
110 |
|
close(bind_sockfd); |
|
|
103 |
|
log_printf(L_ERROR, "Listening on port %d failed: %s\n", port_forward->local_port, strerror(errno)); |
|
104 |
|
close(port_forward->bind_sockfd); |
111 |
105 |
exit(1); |
exit(1); |
112 |
106 |
} |
} |
113 |
107 |
|
|
114 |
|
log_printf(L_DEBUG, "Bound to local port %d\n", local_port); |
|
|
108 |
|
log_printf(L_DEBUG, "Bound to local port %d\n", port_forward->local_port); |
115 |
109 |
|
|
116 |
110 |
return 0; |
return 0; |
117 |
111 |
} |
} |
118 |
112 |
|
|
|
113 |
|
int local_bind() { |
|
114 |
|
local_port_forward *port_forward; |
|
115 |
|
|
|
116 |
|
LL_FOREACH(local_port_forwards, port_forward) |
|
117 |
|
{ |
|
118 |
|
local_bind_one(port_forward); |
|
119 |
|
} |
|
120 |
|
} |
|
121 |
|
|
119 |
122 |
/* Bind the client.sockfd to a tunnel */ |
/* Bind the client.sockfd to a tunnel */ |
120 |
123 |
int handle_acktunnel_frame(protocol_frame *rcvd_frame) |
int handle_acktunnel_frame(protocol_frame *rcvd_frame) |
121 |
124 |
{ |
{ |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
288 |
291 |
uint32_t invitations_sent = 0; |
uint32_t invitations_sent = 0; |
289 |
292 |
TOX_ERR_FRIEND_QUERY friend_query_error; |
TOX_ERR_FRIEND_QUERY friend_query_error; |
290 |
293 |
TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error; |
TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error; |
|
294 |
|
local_port_forward *port_forward; |
291 |
295 |
|
|
292 |
296 |
client_tunnel.sockfd = 0; |
client_tunnel.sockfd = 0; |
293 |
297 |
FD_ZERO(&client_master_fdset); |
FD_ZERO(&client_master_fdset); |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
457 |
461 |
break; |
break; |
458 |
462 |
|
|
459 |
463 |
case CLIENT_STATE_BIND_PORT: |
case CLIENT_STATE_BIND_PORT: |
460 |
|
if(bind_sockfd < 0) |
|
461 |
|
{ |
|
462 |
|
log_printf(L_ERROR, "Shutting down - could not bind to listening port\n"); |
|
463 |
|
state = CLIENT_STATE_SHUTDOWN; |
|
464 |
|
} |
|
465 |
|
else |
|
|
464 |
|
LL_FOREACH(local_port_forwards, port_forward) |
466 |
465 |
{ |
{ |
467 |
|
state = CLIENT_STATE_FORWARDING; |
|
|
466 |
|
if(port_forward->bind_sockfd < 0) |
|
467 |
|
{ |
|
468 |
|
log_printf(L_ERROR, "Shutting down - could not bind to listening port %d\n", port_forward->local_port); |
|
469 |
|
state = CLIENT_STATE_SHUTDOWN; |
|
470 |
|
break; |
|
471 |
|
} |
|
472 |
|
else |
|
473 |
|
{ |
|
474 |
|
state = CLIENT_STATE_FORWARDING; |
|
475 |
|
} |
468 |
476 |
} |
} |
469 |
477 |
break; |
break; |
470 |
478 |
case CLIENT_STATE_SETUP_PIPE: |
case CLIENT_STATE_SETUP_PIPE: |
471 |
|
send_tunnel_request_packet( |
|
472 |
|
remote_host, |
|
473 |
|
remote_port, |
|
474 |
|
friendnumber |
|
475 |
|
); |
|
|
479 |
|
LL_FOREACH(local_port_forwards, port_forward) |
|
480 |
|
{ |
|
481 |
|
send_tunnel_request_packet( |
|
482 |
|
port_forward->remote_host, |
|
483 |
|
port_forward->remote_port, |
|
484 |
|
friendnumber |
|
485 |
|
); |
|
486 |
|
} |
476 |
487 |
state = CLIENT_STATE_FORWARDING; |
state = CLIENT_STATE_FORWARDING; |
477 |
488 |
break; |
break; |
478 |
489 |
case CLIENT_STATE_REQUEST_TUNNEL: |
case CLIENT_STATE_REQUEST_TUNNEL: |
479 |
|
send_tunnel_request_packet( |
|
480 |
|
remote_host, |
|
481 |
|
remote_port, |
|
482 |
|
friendnumber |
|
483 |
|
); |
|
|
490 |
|
LL_FOREACH(local_port_forwards, port_forward) |
|
491 |
|
{ |
|
492 |
|
send_tunnel_request_packet( |
|
493 |
|
port_forward->remote_host, |
|
494 |
|
port_forward->remote_port, |
|
495 |
|
friendnumber |
|
496 |
|
); |
|
497 |
|
} |
484 |
498 |
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; |
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; |
485 |
499 |
break; |
break; |
486 |
500 |
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: |
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: |
File main.c changed (mode: 100644) (index ed2020e..bd4215c) |
... |
... |
enum rules_policy_enum rules_policy = NONE; |
45 |
45 |
rule *rules = NULL; |
rule *rules = NULL; |
46 |
46 |
|
|
47 |
47 |
/* Ports and hostname for port forwarding */ |
/* Ports and hostname for port forwarding */ |
48 |
|
int remote_port = 0; |
|
49 |
|
char *remote_host = NULL; |
|
50 |
|
int local_port = 0; |
|
|
48 |
|
local_port_forward *local_port_forwards = NULL; |
51 |
49 |
|
|
52 |
50 |
/* Whether to daemonize/fork after startup */ |
/* Whether to daemonize/fork after startup */ |
53 |
51 |
int daemonize = 0; |
int daemonize = 0; |
|
... |
... |
int main(int argc, char *argv[]) |
1275 |
1273 |
switch(oc) |
switch(oc) |
1276 |
1274 |
{ |
{ |
1277 |
1275 |
case 'L': |
case 'L': |
|
1276 |
|
local_port_forward *port_forward = calloc(sizeof(local_port_forward), 1); |
|
1277 |
|
|
|
1278 |
|
if(!port_forward) { |
|
1279 |
|
log_printf(L_ERROR, "Could not allocate memory for port forward\n"); |
|
1280 |
|
exit(1); |
|
1281 |
|
} |
|
1282 |
|
|
1278 |
1283 |
/* Local port forwarding */ |
/* Local port forwarding */ |
1279 |
1284 |
client_mode = 1; |
client_mode = 1; |
1280 |
1285 |
client_local_port_mode = 1; |
client_local_port_mode = 1; |
1281 |
|
if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0) |
|
|
1286 |
|
|
|
1287 |
|
|
|
1288 |
|
if(parse_local_port_forward(optarg, &(port_forward->local_port), &(port_forward->remote_host), &(port_forward->remote_port)) < 0) |
1282 |
1289 |
{ |
{ |
1283 |
1290 |
log_printf(L_ERROR, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n"); |
log_printf(L_ERROR, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n"); |
1284 |
1291 |
exit(1); |
exit(1); |
1285 |
1292 |
} |
} |
|
1293 |
|
|
|
1294 |
|
LL_APPEND(local_port_forwards, port_forward); |
|
1295 |
|
|
1286 |
1296 |
if(min_log_level == L_UNSET) |
if(min_log_level == L_UNSET) |
1287 |
1297 |
{ |
{ |
1288 |
1298 |
min_log_level = L_INFO; |
min_log_level = L_INFO; |