-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsudptcp.c
463 lines (382 loc) · 9.3 KB
/
dnsudptcp.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* The TOR TCP DNS Daemon
* (c) Collin R. Mulliner <collin(AT)mulliner.org>
* http://www.mulliner.org/collin/ttdnsd.php
*
* License: GPLv2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <netdb.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
#define MAX_PEERS 1
#define MAX_TIME 3
#define MAX_TRY 1
#define MAX_REQUESTS 499
// 199, 1009
#define NOBODY 65534
#define DEF_PORT 5553
#define HELP_STR ""\
"syntax: torify ttdnsd [bfcd]\n"\
"\t-d\t\t\tDEBUG don't fork/chroot and print debug\n"\
"\n"
struct peer_t
{
struct sockaddr_in tcp;
int tcp_fd;
time_t timeout;
int con; /**< connection state 0=dead, 1=connecting..., 3=connected */
unsigned char b[1502]; /**< receive buffer */
int bl; /**< bytes in receive buffer */
};
struct request_t
{
struct sockaddr_in a;
socklen_t al;
unsigned char b[1502]; /**< request buffer */
int bl; /**< bytes in request buffer */
int id; /**< dns request id */
int rid; /**< real dns request id */
int active; /**< 1=sent, 0=waiting for tcp to become connected */
time_t timeout; /**< timeout of request */
};
struct peer_t peers[MAX_PEERS]; /**< TCP peers */
struct request_t requests[MAX_REQUESTS]; /**< requests queue */
int udp_fd; /**< port 53 socket */
uint16_t port = DEF_PORT;
int debug = 0;
int request_find(int id)
{
int pos = id % MAX_REQUESTS;
for (;;) {
if (requests[pos].id == id) {
printf("found id=%d at pos=%d\n", id, pos);
return pos;
}
else {
pos++;
pos %= MAX_REQUESTS;
if (pos == (id % MAX_REQUESTS)) {
printf("can't find id=%d\n", id);
return -1;
}
}
}
}
int peer_connect(int peer)
{
struct peer_t *p = &peers[peer];
int r = 1;
int cs;
if ((p->tcp_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return 0;
if (setsockopt(p->tcp_fd, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(int))) printf("Setting SO_REUSEADDR failed\n");
if (fcntl(p->tcp_fd, F_SETFL, O_NONBLOCK)) printf("Setting O_NONBLOCK failed\n");
p->tcp.sin_family = AF_INET;
p->tcp.sin_port = htons(port);
p->tcp.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
printf("connecting to: %s\n", inet_ntoa(p->tcp.sin_addr));
cs = connect(p->tcp_fd, (struct sockaddr*)&p->tcp, sizeof(struct sockaddr_in));
if (cs != 0) perror("connect status:");
else printf("connect returned 0\n");
p->bl = 0;
p->con = 1;
return 1;
}
int peer_connected(int peer)
{
struct peer_t *p = &peers[peer];
int cs;
cs = connect(p->tcp_fd, (struct sockaddr*)&p->tcp, sizeof(struct sockaddr_in));
if (cs == 0) {
p->con = 3;
return 1;
}
else {
printf("connection failed\n");
close(p->tcp_fd);
p->tcp_fd = -1;
p->con = 0;
return 0;
}
}
int peer_keepalive(int peer)
{
return 1;
}
int peer_sendreq(int peer, int req)
{
struct peer_t *p = &peers[peer];
struct request_t *r = &requests[req];
int ret;
printf("%s\n", __FUNCTION__);
while ((ret = write(p->tcp_fd, r->b, (r->bl + 2))) < 0 && errno == EAGAIN);
if (ret == 0) {
close(p->tcp_fd);
p->tcp_fd = -1;
p->con = 0;
printf("peer %d got disconnected\n", peer);
return 2;
}
return 1;
}
int peer_readres(int peer)
{
struct peer_t *p = &peers[peer];
struct request_t *r;
int ret;
unsigned short int *ul;
int id;
int req;
unsigned short int *l = (unsigned short int*)p->b;
int len;
//printf("%s\n", __FUNCTION__);
while ((ret = read(p->tcp_fd, (p->b + p->bl), (1502 - p->bl))) < 0 && errno == EAGAIN);
if (ret == 0) {
close(p->tcp_fd);
p->tcp_fd = -1;
printf("peer %d got disconnected\n", peer);
p->con = 0;
return 3;
}
p->bl += ret;
processanswer:
if (p->bl < 2) {
return 2;
}
else {
len = ntohs(*l);
//printf("r l=%d r=%d\n", len, p->bl-2);
if ((len + 2) > p->bl)
return 2;
}
printf("received answer %d bytes\n", p->bl);
ul = (unsigned short int*)(p->b + 2);
id = ntohs(*ul);
if ((req = request_find(id)) == -1) {
memmove(p->b, (p->b + len + 2), (p->bl - len - 2));
p->bl -= len + 2;
return 0;
}
r = &requests[req];
// write back real id
*ul = htons(r->rid);
r->a.sin_family = AF_INET;
while (sendto(udp_fd, (p->b + 2), len, 0, (struct sockaddr*)&r->a, sizeof(struct sockaddr_in)) < 0 && errno == EAGAIN);
printf("forwarding answer (%d bytes)\n", len);
memmove(p->b, p->b + len +2, p->bl - len - 2);
p->bl -= len + 2;
// mark as handled/unused
r->id = 0;
if (p->bl > 0) goto processanswer;
return 1;
}
void peer_handleoutstanding(int peer)
{
int i;
printf("%s\n", __FUNCTION__);
for (i = 0; i < MAX_REQUESTS; i++) {
if (requests[i].id != 0 && requests[i].active == 0) {
requests[i].active = 1;
peer_sendreq(peer, i);
}
}
}
int peer_select()
{
return 0;
}
int request_add(struct request_t *r)
{
int pos = r->id % MAX_REQUESTS;
int dst_peer;
unsigned short int *ul;
time_t ct = time(NULL);
printf("adding new request (id=%d)\n", r->id);
for (;;) {
if (requests[pos].id == 0) {
// this one is unused, take it
break;
}
else {
if (requests[pos].id == r->id) {
if (memcmp((char*)&r->a, (char*)&requests[pos].a, sizeof(r->a)) == 0) {
printf("hash position %d already taken by request with same id; dropping it\n", pos);
return 0;
}
else {
do {
r->id = ((rand()>>16) % 0xffff);
} while (r->id < 1);
pos = r->id % MAX_REQUESTS;
printf("NATing id (id was %d now is %d)\n", r->rid, r->id);
continue;
}
}
else if ((requests[pos].timeout + MAX_TIME) > ct) {
// request timed out, take it
break;
}
else {
pos++;
pos %= MAX_REQUESTS;
if (pos == (r->id % MAX_REQUESTS)) {
printf("no more free request slots, wow this is a busy node. dropping request!\n");
return 0;
}
}
}
}
r->timeout = time(NULL);
// update id
ul = (unsigned short int*)(r->b + 2);
*ul = htons(r->id);
printf("using requests slot %d\n", pos);
memcpy((char*)&requests[pos], (char*)r, sizeof(struct request_t));
dst_peer = peer_select();
if (peers[dst_peer].con == 3) {
r->active = 1;
return peer_sendreq(dst_peer, pos);
}
else {
return peer_connect(dst_peer);
}
}
int server(void)
{
struct sockaddr_in udp;
struct pollfd pfd[MAX_PEERS+1];
int poll2peers[MAX_PEERS];
int fr;
int i;
int pfd_num;
struct request_t tmp;
for (i = 0; i < MAX_PEERS; i++) {
peers[i].tcp_fd = -1;
poll2peers[i] = -1;
peers[i].con = 0;
}
memset((char*)requests, 0, sizeof(requests));
if ((udp_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return(-1);
}
memset((char*)&udp, 0, sizeof(struct sockaddr_in));
udp.sin_family = AF_INET;
udp.sin_port = htons(port);
udp.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(udp_fd, (struct sockaddr*)&udp, sizeof(struct sockaddr_in)) < 0) {
close(udp_fd);
printf("can't bind to %s:%d\n", "127.0.0.1", port);
return(-1);
}
// drop privileges
if (!debug) {
setgid(NOBODY);
setuid(NOBODY);
}
for (;;) {
// populate poll array
for (pfd_num = 1, i = 0; i < MAX_PEERS; i++) {
if (peers[i].tcp_fd != -1) {
pfd[pfd_num].fd = peers[i].tcp_fd;
switch (peers[i].con) {
case 3:
pfd[pfd_num].events = POLLIN|POLLPRI;
break;
default:
pfd[pfd_num].events = POLLOUT|POLLERR;
break;
}
poll2peers[pfd_num-1] = i;
pfd_num++;
}
}
pfd[0].fd = udp_fd;
pfd[0].events = POLLIN|POLLPRI;
printf("watching %d file descriptors\n", pfd_num);
fr = poll(pfd, pfd_num, -1);
printf("%d file descriptors became ready\n", fr);
// handle tcp connections
for (i = 1; i < pfd_num; i++) {
if (pfd[i].fd != -1 && ((pfd[i].revents & POLLIN) == POLLIN ||
(pfd[i].revents & POLLPRI) == POLLPRI || (pfd[i].revents & POLLOUT)
== POLLOUT || (pfd[i].revents & POLLERR) == POLLERR)) {
switch (peers[poll2peers[i-1]].con) {
case 3:
peer_readres(poll2peers[i-1]);
break;
case 1:
case 2:
if (peer_connected(poll2peers[i-1])) {
peer_handleoutstanding(poll2peers[i-1]);
}
break;
default:
printf("peer %d in bad state\n", peers[poll2peers[i-1]].con);
break;
}
}
}
if ((pfd[0].revents & POLLIN) == POLLIN || (pfd[0].revents & POLLPRI) == POLLPRI) {
unsigned short int *ul;
memset((char*)&tmp, 0, sizeof(struct request_t));
tmp.al = sizeof(struct sockaddr_in);
while ((tmp.bl = recvfrom(udp_fd, tmp.b+2, 1500, 0, (struct sockaddr*)&tmp.a, &tmp.al)) < 0 && errno == EAGAIN);
// get request id
ul = (unsigned short int*) (tmp.b + 2);
tmp.rid = tmp.id = ntohs(*ul);
// set request length
ul = (unsigned short int*)tmp.b;
*ul = htons(tmp.bl);
printf("received request of %d bytes, id = %d\n", tmp.bl, tmp.id);
request_add(&tmp);
}
}
}
int main(int argc, char **argv)
{
int opt;
while ((opt = getopt(argc, argv, "hd")) != EOF) {
switch (opt) {
case 'd':
debug = 1;
break;
case 'h':
default:
printf(HELP_STR);
exit(0);
break;
}
}
if (optind < argc) {
opt = atoi(argv[optind]);
if (opt > 0 && opt < 0xffff)
port = opt;
}
srand(time(NULL));
if (!debug) {
if (fork()) exit(0);
int fd;
fd = open("/dev/null", O_RDWR);
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
setsid();
}
server();
exit(0);
}