forked from samyk/pwnat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
443 lines (375 loc) · 11.1 KB
/
client.c
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Project: udptunnel
* File: client.c
*
* Copyright (C) 2009 Daniel Meekins
* Contact: dmeekins - gmail
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <sys/time.h>
#endif /*WIN32*/
#include "common.h"
#include "client.h"
#include "socket.h"
extern int debug_level;
/*
* Allocates and initializes a new client object.
* id - ID number for the client to have
* tcp_sock/udp_sock - sockets attributed to the client. this function copies
* the structure, so the calling function can free the sockets passed to
* here.
* connected - whether the TCP socket is connected or not.
* Returns a pointer to the new structure. Call client_free() when done with
* it.
*/
client_t *client_create(uint16_t id, socket_t *tcp_sock, socket_t *udp_sock,
int connected)
{
client_t *c = NULL;
c = calloc(1, sizeof(client_t));
if(!c)
goto error;
c->id = id;
c->tcp_sock = sock_copy(tcp_sock);
c->udp_sock = sock_copy(udp_sock);
c->udp2tcp_state = CLIENT_WAIT_HELLO;
c->tcp2udp_state = CLIENT_WAIT_DATA0;
c->connected = connected;
timerclear(&c->keepalive);
timerclear(&c->tcp2udp_timeout);
c->resend_count = 0;
return c;
error:
if(c)
{
if(c->tcp_sock)
sock_free(c->tcp_sock);
if(c->udp_sock)
sock_free(c->udp_sock);
free(c);
}
return NULL;
}
/*
* Performs a deep copy of the client structure.
*/
client_t *client_copy(client_t *dst, client_t *src, size_t len)
{
if(!dst || !src)
return NULL;
memcpy(dst, src, sizeof(*src));
dst->tcp_sock = sock_copy(src->tcp_sock);
if(!dst->tcp_sock)
return NULL;
dst->udp_sock = sock_copy(src->udp_sock);
if(!dst->udp_sock)
return NULL;
return dst;
}
/*
* Compares the ID of the two clients.
*/
int client_cmp(client_t *c1, client_t *c2, size_t len)
{
return c1->id - c2->id;
}
/*
* Connects the TCP socket of the client (wrapper for sock_connect()). Returns
* 0 on success or -1 on error.
*/
int client_connect_tcp(client_t *c, char *port)
{
if(!c->connected)
{
if(sock_connect(c->tcp_sock, 0, port) == 0)
{
c->connected = 1;
return 0;
}
}
return -1;
}
/*
* Closes the TCP socket for the client (wrapper for sock_close()).
*/
void client_disconnect_tcp(client_t *c)
{
if(c->connected)
{
sock_close(c->tcp_sock);
c->connected = 0;
}
}
/*
* Closes the UDP socket for the client (wrapper for sock_close()).
*/
void client_disconnect_udp(client_t *c)
{
sock_close(c->udp_sock);
}
/*
* Releases the memory used by the client.
*/
void client_free(client_t *c)
{
if(c)
{
sock_free(c->tcp_sock);
sock_free(c->udp_sock);
free(c);
}
}
/*
* Receives a message from the UDP tunnel for the client. Only used in
* udpclient program because each client has their own UDP socket. Returns 0
* for success or -1 on error. The data is written to memory pointed to by
* data, and the id, msg_type, and len are set from the message header.
*/
int client_recv_udp_msg(client_t *client, char *data, int data_len,
uint16_t *id, uint8_t *msg_type, uint16_t *len)
{
int ret;
socket_t from;
ret = msg_recv_msg(client->udp_sock, &from, data, data_len,
id, msg_type, len);
if(ret < 0)
return ret;
if(!sock_addr_equal(client->udp_sock, &from))
return -1;
return 0;
}
/*
* Copy data to the internal buffer for sending to tcp connection and send ACK
* back to tunnel. Returns 0 on success, 1 if this was "resending" data, -1
* on error, or -2 if need to disconnect.
*/
int client_got_udp_data(client_t *client, char *data, int data_len,
uint8_t msg_type)
{
int ret;
int is_resend = 0;
if(data_len > MSG_MAX_LEN)
return -1;
/* Check if got new data, which is when got the data type (DATA0 or DATA1)
that it was waiting for, and write that new data to the buffer. */
if((msg_type == MSG_TYPE_DATA0 &&
client->udp2tcp_state == CLIENT_WAIT_DATA0)
|| (msg_type == MSG_TYPE_DATA1 &&
client->udp2tcp_state == CLIENT_WAIT_DATA1))
{
memcpy(client->udp2tcp, data, data_len);
client->udp2tcp_len = data_len;
}
else
is_resend = 1; /* Otherwise, the other host resent the data */
msg_type = (msg_type == MSG_TYPE_DATA0) ? MSG_TYPE_ACK0 : MSG_TYPE_ACK1;
/* Send the ACK for the data */
ret = msg_send_msg(client->udp_sock, client->id, msg_type, NULL, 0);
if(ret < 0)
return ret;
if(is_resend)
return 1;
/* Set the state to wait for the next type of data */
client->udp2tcp_state = client->udp2tcp_state == CLIENT_WAIT_DATA0 ?
CLIENT_WAIT_DATA1 : CLIENT_WAIT_DATA0;
return 0;
}
/*
* Send data received from UDP tunnel to TCP connection. Need to call
* client_got_udp_data() first. Returns -1 on general error, -2 if need to
* disconnect, and 0 on success.
*/
int client_send_tcp_data(client_t *client)
{
int ret;
ret = sock_send(client->tcp_sock, client->udp2tcp, client->udp2tcp_len);
if(ret < 0)
return -1;
else if(ret == 0)
return -2;
else
return 0;
}
/*
* Reads data that is ready on the TCP socket and stores it in the internal
* buffer. The routine client_send_udp_data() send that data to the tunnel.
*/
int client_recv_tcp_data(client_t *client)
{
int ret;
/* Don't read the tcp data yet if waiting for an ack or the hello */
if(client->tcp2udp_state == CLIENT_WAIT_ACK0 ||
client->tcp2udp_state == CLIENT_WAIT_ACK1 ||
client->udp2tcp_state == CLIENT_WAIT_HELLO)
return 1;
ret = sock_recv(client->tcp_sock, NULL, client->tcp2udp,
sizeof(client->tcp2udp));
if(ret < 0)
return -1;
if(ret == 0)
return -2;
client->tcp2udp_len = ret;
return 0;
}
/*
* Sends the data in the tcp2udp buffer to the UDP tunnel. Returns 0 for
* success, -1 on error, and -2 if needs to disconnect.
*/
int client_send_udp_data(client_t *client)
{
uint8_t msg_type;
int ret;
if(client->resend_count >= CLIENT_MAX_RESEND)
return -2;
/* Set the message type it is sending. If the client is in the WAIT_ACK
state, then it will send the same type of data again (since this would
have been called b/c of a timeout. */
switch(client->tcp2udp_state)
{
case CLIENT_WAIT_DATA0:
case CLIENT_WAIT_ACK0:
msg_type = MSG_TYPE_DATA0;
break;
case CLIENT_WAIT_DATA1:
case CLIENT_WAIT_ACK1:
msg_type = MSG_TYPE_DATA1;
break;
default:
return -1;
}
ret = msg_send_msg(client->udp_sock, client->id, msg_type,
client->tcp2udp, client->tcp2udp_len);
if(ret < 0)
return ret;
/* Set the state to wait for an ACK and set the timeout to some time in
the future */
client->tcp2udp_state = (msg_type == MSG_TYPE_DATA0) ?
CLIENT_WAIT_ACK0 : CLIENT_WAIT_ACK1;
gettimeofday(&client->tcp2udp_timeout, NULL);
client->tcp2udp_timeout.tv_sec += (client->resend_count+1)*CLIENT_TIMEOUT;
return 0;
}
/*
* Notifies the client that it got an ACK to change the internal state to
* wait for data. Returns 0 if ok or -1 if something weird happened.
*/
int client_got_ack(client_t *client, uint8_t ack_type)
{
if(ack_type == MSG_TYPE_ACK0 && client->tcp2udp_state == CLIENT_WAIT_ACK0)
{
client->tcp2udp_state = CLIENT_WAIT_DATA1;
client->resend_count = 0;
return 0;
}
if(ack_type == MSG_TYPE_ACK1 && client->tcp2udp_state == CLIENT_WAIT_ACK1)
{
client->tcp2udp_state = CLIENT_WAIT_DATA0;
client->resend_count = 0;
return 0;
}
return -1;
}
/*
* Sends a HELLO type message to the udpserver (proxy) to tell it to make a
* TCP connection to the specified host:port.
*/
int client_send_hello(client_t *client, char *host, char *port,
uint16_t req_id)
{
return msg_send_hello(client->udp_sock, host, port, req_id);
}
/*
* Sends a Hello ACK to the UDP tunnel.
*/
int client_send_helloack(client_t *client, uint16_t req_id)
{
req_id = htons(req_id);
return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_HELLOACK,
(char *)&req_id, sizeof(req_id));
}
/*
* Notify the client that it got a Hello ACK.
*/
int client_got_helloack(client_t *client)
{
if(client->udp2tcp_state == CLIENT_WAIT_HELLO)
client->udp2tcp_state = CLIENT_WAIT_DATA0;
return 0;
}
/*
* Sends a goodbye message to the UDP server.
*/
int client_send_goodbye(client_t *client)
{
return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_GOODBYE,
NULL, 0);
}
/*
* Checks the timeout state of the client and resend the data if the timeout
* is up.
*/
int client_check_and_resend(client_t *client, struct timeval curr_tv)
{
if((client->tcp2udp_state == CLIENT_WAIT_ACK0 ||
client->tcp2udp_state == CLIENT_WAIT_ACK1)
&& timercmp(&curr_tv, &client->tcp2udp_timeout, >))
{
client->resend_count++;
if(debug_level >= DEBUG_LEVEL2)
printf("client(%d): resending data, count %d\n",
CLIENT_ID(client), client->resend_count);
return client_send_udp_data(client);
}
return 0;
}
/*
* Sends a keepalive message to the UDP server.
*/
int client_check_and_send_keepalive(client_t *client, struct timeval curr_tv)
{
if(client_timed_out(client, curr_tv))
{
curr_tv.tv_sec += KEEP_ALIVE_SECS;
memcpy(&client->keepalive, &curr_tv, sizeof(struct timeval));
return msg_send_msg(client->udp_sock, client->id, MSG_TYPE_KEEPALIVE,
NULL, 0);
}
return 0;
}
/*
* Sets the client's keepalive timeout to be the current time plus the timeout
* period.
*/
void client_reset_keepalive(client_t *client)
{
struct timeval curr;
gettimeofday(&curr, NULL);
curr.tv_sec += KEEP_ALIVE_TIMEOUT_SECS;
memcpy(&client->keepalive, &curr, sizeof(struct timeval));
}
/*
* Returns 1 if the client timed out (didn't get any data or keep alive
* messages in the period), or 0 if it hasn't yet.
*/
int client_timed_out(client_t *client, struct timeval curr_tv)
{
if(timercmp(&curr_tv, &client->keepalive, >))
return 1;
else
return 0;
}