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
|
/* ISC license. */
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <skalibs/uint16.h>
#include <skalibs/uint32.h>
#include <skalibs/error.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/socket.h>
#include <s6-dns/s6dns-constants.h>
#include <s6-dns/s6dns-message.h>
#include "dnsfunneld.h"
static stralloc q = STRALLOC_ZERO ;
static size_t head = 0 ;
size_t dfanswer_pending ()
{
return q.len - head ;
}
static void dfanswer_push (char const *s, size_t len, uint32_t ip, uint16_t port)
{
if (len > 512)
{
if (verbosity)
strerr_warnw1x("answer too big, dropping - enable truncation to avoid this") ;
}
else
{
if (!stralloc_readyplus(&q, len + 8)) strerr_diefu1sys(111, "queue answer to client") ;
uint32_pack_big(q.s + q.len, ip) ; q.len += 4 ;
uint16_pack_big(q.s + q.len, port) ; q.len += 2 ;
uint16_pack_big(q.s + q.len, len) ; q.len += 2 ;
memcpy(q.s + q.len, s, len) ; q.len += len ;
}
}
int dfanswer_flush ()
{
while (dfanswer_pending())
{
uint16_t port, len ;
uint16_unpack_big(q.s + head + 4, &port) ;
uint16_unpack_big(q.s + head + 6, &len) ;
if (socket_send4(0, q.s + head + 8, len, q.s + head, port) < 0)
return error_isagain(errno) ? (errno = 0, 0) : -1 ;
head += len + 8 ;
if ((q.len - head) >> 2 <= q.len)
{
memmove(q.s, q.s + head, q.len - head) ;
q.len -= head ;
head = 0 ;
}
}
return 1 ;
}
static void switchaux (char *buf, uint16_t len)
{
uint16_t qtype ;
uint16_unpack_big(buf + len - 4, &qtype) ;
switch (qtype)
{
case S6DNS_T_A : qtype = S6DNS_T_AAAA ; break ;
case S6DNS_T_AAAA : qtype = S6DNS_T_A ; break ;
default : strerr_dief1x(101, "can't happen: invalid qtype in auxiliary query") ;
}
uint16_pack_big(buf + len - 4, qtype) ;
}
void dfanswer_fail (dfquery_t const *q, int isaux)
{
char buf[512] ;
s6dns_message_header_t hdr ;
uint16_t len ;
uint16_unpack_big(q->dt.sa.s, &len) ;
memcpy(buf, q->dt.sa.s + 2, len) ;
s6dns_message_header_unpack(buf, &hdr) ;
hdr.id = q->id ;
hdr.qr = 1 ;
hdr.aa = 0 ;
hdr.tc = 0 ;
hdr.rd = 1 ;
hdr.ra = 1 ;
hdr.z = 0 ;
hdr.rcode = 2 ; /* servfail */
s6dns_message_header_pack(buf, &hdr) ;
if (isaux) switchaux(buf, len) ;
dfanswer_push(buf, len, q->ip, q->port) ;
}
void dfanswer_nxdomain (dfquery_t const *q, int isaux)
{
char buf[512] ;
s6dns_message_header_t hdr ;
uint16_t len ;
uint16_unpack_big(q->dt.sa.s, &len) ;
memcpy(buf, q->dt.sa.s + 2, len) ;
s6dns_message_header_unpack(buf, &hdr) ;
hdr.id = q->id ;
hdr.qr = 1 ;
hdr.aa = 1 ;
hdr.tc = 0 ;
hdr.rd = 1 ;
hdr.ra = 1 ;
hdr.z = 0 ;
hdr.rcode = 3 ; /* nxdomain */
s6dns_message_header_pack(buf, &hdr) ;
if (isaux) switchaux(buf, len) ;
dfanswer_push(buf, len, q->ip, q->port) ;
}
void dfanswer_nodata (dfquery_t const *q, int isaux)
{
char buf[512] ;
s6dns_message_header_t hdr ;
uint16_t len ;
uint16_unpack_big(q->dt.sa.s, &len) ;
memcpy(buf, q->dt.sa.s + 2, len) ;
s6dns_message_header_unpack(buf, &hdr) ;
hdr.id = q->id ;
hdr.qr = 1 ;
hdr.aa = 1 ;
hdr.tc = 0 ;
hdr.rd = 1 ;
hdr.ra = 1 ;
hdr.z = 0 ;
hdr.rcode = 0 ; /* success */
s6dns_message_header_pack(buf, &hdr) ;
if (isaux) switchaux(buf, len) ;
dfanswer_push(buf, len, q->ip, q->port) ;
}
void dfanswer_pass (dfquery_t const *q, char *s, unsigned int len)
{
s6dns_message_header_t hdr ;
s6dns_message_header_unpack(s, &hdr) ;
hdr.id = q->id ;
s6dns_message_header_pack(s, &hdr) ;
dfanswer_push(s, len, q->ip, q->port) ;
}
|