-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpdns.c
More file actions
201 lines (169 loc) · 4.39 KB
/
tcpdns.c
File metadata and controls
201 lines (169 loc) · 4.39 KB
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
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "pack.h"
#include "stralloc.h"
enum { streams = 256 };
static struct pollfd fd[streams + 16];
static size_t fdc = streams;
static char buffer[streams][65535 + 2];
static struct sockaddr_storage peer[streams];
static uint64_t born[streams];
static size_t head[streams];
static size_t tail[streams];
size_t attach(struct pollfd *fd, size_t fdlen, int type,
const char *address, const char *port);
void lookup(stralloc *r, size_t max, const void *ip, size_t iplen);
void prepare(int fg, const char *user);
static size_t respond(size_t i) {
stralloc r = {
.s = buffer[i] + 2,
.len = head[i] - 2,
.size = sizeof *buffer - 2,
.limit = -1
};
if (peer[i].ss_family == AF_INET)
lookup(&r, -1, &((struct sockaddr_in *) (peer + i))->sin_addr, 4);
else if (peer[i].ss_family == AF_INET6)
lookup(&r, -1, &((struct sockaddr_in6 *) (peer + i))->sin6_addr, 16);
else
return 0;
pack_uint16_big(buffer[i], r.len);
head[i] = r.len + 2;
return r.len;
}
static uint64_t utime(void) {
struct timespec ts;
uint64_t now = 0;
clock_gettime(CLOCK_MONOTONIC, &ts);
now += (uint64_t) ts.tv_sec * 1000000;
now += (uint64_t) ts.tv_nsec / 1000;
return now;
}
static void drop(size_t i) {
if (fd[i].fd >= 0)
close(fd[i].fd);
fd[i].events = 0;
fd[i].fd = -1;
born[i] = 0;
head[i] = 0;
tail[i] = 0;
}
static void new(size_t i) {
struct sockaddr_storage sa;
socklen_t salen = sizeof sa;
size_t j, k;
int client;
if ((client = accept(fd[i].fd, (void *) &sa, &salen)) < 0)
return;
if (fcntl(client, F_SETFL, O_NONBLOCK) < 0) {
close(client);
return;
}
for (j = 0, k = 1; born[j] && k < streams; k++)
if (born[k] < born[j])
j = k;
drop(j);
fd[j].events = POLLIN;
fd[j].fd = client;
born[j] = utime();
peer[j] = sa;
}
static int stream(size_t i) {
size_t size;
ssize_t count;
if (head[i] < 2) {
count = read(fd[i].fd, buffer[i] + head[i], 2 - head[i]);
if (count < 0 && (errno == EINTR || errno == EAGAIN))
return POLLIN;
if (count <= 0)
return 0;
if (head[i] += count, head[i] < 2)
return POLLIN;
}
if (!(size = unpack_uint16_big(buffer[i])))
return 0;
if (head[i] < size + 2) {
count = read(fd[i].fd, buffer[i] + head[i], size + 2 - head[i]);
if (count < 0 && (errno == EINTR || errno == EAGAIN))
return POLLIN;
if (count <= 0)
return 0;
if (head[i] += count, head[i] < size + 2)
return POLLIN;
if (respond(i) == 0)
return 0;
}
count = write(fd[i].fd, buffer[i] + tail[i], head[i] - tail[i]);
if (count < 0 && (errno == EINTR || errno == EAGAIN))
return POLLOUT;
if (count <= 0)
return 0;
if (tail[i] += count, tail[i] < head[i])
return POLLOUT;
born[i] = utime();
head[i] = 0;
tail[i] = 0;
return POLLIN;
}
static int usage(const char *progname) {
fprintf(stderr, "\
Usage: %s [OPTIONS] ADDRESS...\n\
Options:\n\
-d DIR change directory to DIR before opening data.cdb\n\
-f run in the foreground instead of daemonizing\n\
-u UID:GID run with the specified numeric uid and gid\n\
-u USERNAME run with the uid and gid of user USERNAME\n\
", progname);
return 64;
}
int main(int argc, char **argv) {
int fg = 0, option;
char *user = 0;
while ((option = getopt(argc, argv, ":d:fu:")) > 0)
switch (option) {
case 'd':
if (chdir(optarg) < 0)
err(1, "chdir");
break;
case 'f':
fg = 1;
break;
case 'u':
user = optarg;
break;
default:
return usage(argv[0]);
}
if (argc <= optind)
return usage(argv[0]);
for (int i = optind; i < argc; i++)
fdc = fdc + attach(fd + fdc, sizeof fd / sizeof *fd - fdc,
SOCK_STREAM, argv[i], "53");
prepare(fg, user);
for (size_t i = 0; i < streams; i++)
fd[i].fd = -1;
signal(SIGPIPE, SIG_IGN);
while (1) {
if (poll(fd, fdc, -1) < 0) {
if (errno == EINTR)
continue;
err(1, "poll");
}
for (size_t i = 0; i < streams; i++)
if (fd[i].revents && !(fd[i].events = stream(i)))
drop(i);
for (size_t i = streams; i < fdc; i++)
if (fd[i].revents)
new(i);
}
}