-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnstcp.c
393 lines (347 loc) · 8.21 KB
/
dnstcp.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
/* (c) 2006, Quest Software Inc. All rights reserved. */
/* David Leonard, 2006 */
#include "common.h"
#if HAVE_SIGNAL_H
# include <signal.h>
#endif
#include "err.h"
#include "dns.h"
#include "dnstcp.h"
#if HAVE_SYS_UN_H
# include <sys/un.h>
#endif
/*
* DNS over TCP
* These functions provide a very simple way of connecting to
* a domain name server using TCP.
* UDP is not supported.
*/
static int tcp_connect(const char *host, const char *service);
extern int verbose;
#if HAVE_GETADDRINFO
/* Connects to a TCP service. Returns socket descriptor or -1 on failure. */
static int
tcp_connect(const char *host, const char *service)
{
struct addrinfo hints, *res, *res0;
int error, s;
const char *cause;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(host, service, &hints, &res0);
if (error) {
warnx("%s: %s", host, gai_strerror(error));
return -1;
}
s = -1;
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (s < 0) {
cause = "socket";
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
cause = "connect";
close(s);
s = -1;
continue;
}
break; /* success */
}
if (s < 0)
warn("%s: %s", host, cause);
freeaddrinfo(res0);
return s;
}
#else /* ! HAVE_GETADDRINFO */
# if HAVE_NETINET_IN_H
# include <netinet/in.h>
# endif
static int
tcp_connect(const char *host, const char *service)
{
struct servent *servent;
struct hostent *hostent;
struct sockaddr_in sin;
int s;
servent = getservbyname(service, "tcp");
if (!servent) {
warnx("unknown service tcp/%s", service);
return -1;
}
hostent = gethostbyname(host);
if (!hostent) {
warnx("unknown host %s", host);
return -1;
}
assert(hostent->h_addrtype == AF_INET);
assert(hostent->h_length == sizeof sin.sin_addr);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
warn("socket");
return -1;
}
memset(&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
sin.sin_port = servent->s_port; /* htons()?? */
memcpy(&sin.sin_addr, hostent->h_addr, sizeof sin.sin_addr);
if (verbose > 2)
fprintf(stderr, "connecting to port %u\n", ntohs(sin.sin_port));
if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
warn("%s: connect", host);
close(s);
return -1;
}
return s;
}
#endif /* ! HAVE_GETADDRINFO */
static int
unix_connect(const char *local)
{
#ifdef AF_UNIX
int s;
struct sockaddr_un unaddr;
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
warn("socket");
return -1;
}
memset(&unaddr, 0, sizeof unaddr);
unaddr.sun_family = AF_UNIX;
snprintf(unaddr.sun_path, sizeof unaddr.sun_path, "%s", local);
if (verbose > 2)
fprintf(stderr, "connecting to unix socket %s\n", unaddr.sun_path);
if (connect(s, (struct sockaddr *)&unaddr, sizeof unaddr) < 0) {
warn("connect");
close(s);
return -1;
}
return s;
#else
warnx("AF_UNIX not implemented");
return -1;
#endif
}
/*
* Connects to a local domain address, and sends the intended hostname
* as a string preceded by a 16-bit length
*/
static int
debug_connect_unix(const char *local, const char *host)
{
int s;
if ((s = unix_connect(local)) < 0)
return -1;
/* Write the target hostname preceded by a 16-bit length */
dnstcp_send(s, host, strlen(host));
return s;
}
/*
* Forks a wrapper program, setting up a TCP-like socket for communication
*/
static int
debug_connect_exec(const char *wrapper, const char *host)
{
int sp[2];
#if HAVE_SOCKETPAIR && defined(AF_UNIX)
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) {
warn("socketpair");
return -1;
}
#else
warnx("unable to create bidirectional IPC");
return -1;
#endif
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
warn("signal SIGCHLD");
switch (fork()) {
case -1:
warn("fork");
close(sp[0]);
close(sp[1]);
return -1;
default:
/* parent */
close(sp[1]);
return sp[0];
case 0:
/* child */
if (verbose)
fprintf(stderr, "starting wrapper pid %d: %s %s\n",
getpid(), wrapper, host);
close(sp[0]);
if (sp[1] != 0) {
if (close(0) < 0)
warn("close 0");
if (dup2(sp[1], 0) < 0)
warn("dup2 0");
}
if (sp[1] != 1) {
if (close(1) < 0)
warn("close 1");
if (dup2(sp[1], 1) < 0)
warn("dup2 1");
}
if (sp[1] != 0 && sp[1] != 1)
close(sp[1]);
execlp(wrapper, wrapper, host, NULL);
warn("%s", wrapper);
_exit(1);
/* NOTREACHED */
}
}
static struct {
const char *label;
int (*connect)(const char *where, const char *host);
} debug_intercepts[] = {
{ "unix:", debug_connect_unix },
{ "exec:", debug_connect_exec }
};
#ifndef lengthof
# define lengthof(a) (sizeof a / sizeof a[0])
#endif
/*
* Connects to a DNS server using TCP.
* Returns a socket decsriptor or -1 on error.
*/
int
dnstcp_connect(const char *host)
{
char *intercept;
intercept = getenv("DNSTCP_CONNECT_INTERCEPT");
if (intercept && *intercept) {
int i;
for (i = 0; i < lengthof(debug_intercepts); i++)
if (memcmp(intercept, debug_intercepts[i].label,
strlen(debug_intercepts[i].label)) == 0)
return (*debug_intercepts[i].connect)(
intercept + strlen(debug_intercepts[i].label),
host);
errx(1, "dnstcp_connect: bad prefix in DNSTCP_CONNECT_INTERCEPT: %s",
intercept);
}
return tcp_connect(host, "domain");
}
/*
* Sends data on a TCP/DNS connection preceded by uint16 length;
* Returns len or -1 on error.
*/
int
dnstcp_send(int s, const void *buf, size_t len)
{
unsigned char b[2];
assert(len <= 0xffff);
b[0] = (len >> 8) & 0xff;
b[1] = len & 0xff;
#if PACK_DNSTCP_SEND
{
/* There is a bug in wireshark's TCP/DNS decoder. This is
* temporary to get around it. */
/* See http://bugs.wireshark.org/bugzilla/show_bug.cgi?id=2272 */
char *tbuf = malloc(len + 2);
memcpy(tbuf, b, 2);
memcpy(tbuf + 2, buf, len);
if (write(s, tbuf, len + 2) != len + 2) {
warn("write");
free(tbuf);
return -1;
}
free(tbuf);
}
#else
if (verbose > 3)
fprintf(stderr, "dnstcp_send: writing length %02x %02x\n", b[0], b[1]);
if (write(s, b, sizeof b) != 2) {
warn("write");
return -1;
}
if (verbose > 3)
fprintf(stderr, "dnstcp_send: writing %d bytes to fd %d\n",
(int)len, s);
if (len > 0 && write(s, buf, len) != len) {
warn("write");
return -1;
}
#endif
return len;
}
void
dnstcp_close(int *s)
{
if (close(*s) == -1)
warn("close");
*s = -1;
}
/*
* Receives data from a TCP/DNS connection;
* Reads a uint16 length and then reads the subsequent data
* into the buffer provided.
* Retries/aborts on error or too-small buffer
* Returns length of data read (excluding length header), or
* -1 on error.
*/
int
dnstcp_recv(int s, void *buf, size_t bufsz)
{
unsigned char b[2];
int len, msglen;
int pos;
if (verbose > 3)
fprintf(stderr, "dnstcp_recv s %d buf %p bufsz %d\n",
s, buf, (int)bufsz);
for (pos = 0; pos < sizeof b; pos += len) {
len = read(s, b + pos, sizeof b - pos);
if (verbose > 3)
fprintf(stderr, " header read -> %d\n", len);
if (len < 0) {
warn("read");
return -1;
}
if (len == 0) {
if (pos) warn("close after short read");
return 0;
}
if (verbose > 3)
fprintf(stderr, "[read %d of %d header]\n", pos+len,
(int)sizeof b);
}
if (verbose > 3)
fprintf(stderr, " header %02x %02x\n", b[0], b[1]);
msglen = (b[0] << 8) | b[1];
if (msglen > bufsz) {
warn("buffer too small");
return -1;
}
for (pos = 0; pos < msglen; pos += len) {
if ((len = read(s, (char *)buf + pos, msglen - pos)) < 0) {
warn("read");
return -1;
}
if (len == 0) {
warn("close after short read");
return 0;
}
if (verbose > 3)
fprintf(stderr, "[read %d of %d]\n", pos+len, msglen);
}
return msglen;
}
int
dnstcp_sendmsg(int s, const struct dns_msg *msg)
{
void *base;
size_t len;
dns_msg_getbuf(msg, &base, &len);
return dnstcp_send(s, base, len);
}
int
dnstcp_recvmsg(int s, void *buf, size_t bufsz, struct dns_msg *msg)
{
int len;
len = dnstcp_recv(s, buf, bufsz);
if (len >= 0)
dns_msg_setbuf(msg, buf, len);
return len;
}