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