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
|
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <bio.h>
#include <ndb.h>
#include "dns.h"
static int udpannounce(char*);
static void reply(int, uchar*, DNSmsg*, Request*);
extern char *logfile;
static void
ding(void *x, char *msg)
{
USED(x);
if(strcmp(msg, "alarm") == 0)
noted(NCONT);
else
noted(NDFLT);
}
typedef struct Inprogress Inprogress;
struct Inprogress
{
int inuse;
OUdphdr uh;
DN *owner;
int type;
int id;
};
Inprogress inprog[Maxactive+2];
/*
* record client id and ignore retransmissions.
* we're still single thread at this point.
*/
static Inprogress*
clientrxmit(DNSmsg *req, uchar *buf)
{
Inprogress *p, *empty;
OUdphdr *uh;
uh = (OUdphdr *)buf;
empty = 0;
for(p = inprog; p < &inprog[Maxactive]; p++){
if(p->inuse == 0){
if(empty == 0)
empty = p;
continue;
}
if(req->id == p->id)
if(req->qd->owner == p->owner)
if(req->qd->type == p->type)
if(memcmp(uh, &p->uh, OUdphdrsize) == 0)
return 0;
}
if(empty == 0)
return 0; /* shouldn't happen - see slave() and definition of Maxactive */
empty->id = req->id;
empty->owner = req->qd->owner;
empty->type = req->qd->type;
memmove(&empty->uh, uh, OUdphdrsize);
empty->inuse = 1;
return empty;
}
/*
* a process to act as a dns server for outside reqeusts
*/
void
dnudpserver(char *mntpt)
{
int fd, len, op;
Request req;
DNSmsg reqmsg, repmsg;
uchar buf[OUdphdrsize + Maxudp + 1024];
char *err;
Inprogress *p;
char tname[32];
OUdphdr *uh;
/* fork sharing text, data, and bss with parent */
switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
case -1:
break;
case 0:
break;
default:
return;
}
fd = -1;
notify(ding);
restart:
if(fd >= 0)
close(fd);
while((fd = udpannounce(mntpt)) < 0)
sleep(5000);
if(setjmp(req.mret))
putactivity();
req.isslave = 0;
/* loop on requests */
for(;; putactivity()){
memset(&repmsg, 0, sizeof(repmsg));
memset(&reqmsg, 0, sizeof(reqmsg));
alarm(60*1000);
len = udpread(fd, (OUdphdr*)buf, buf+OUdphdrsize, sizeof(buf)-OUdphdrsize);
alarm(0);
if(len <= 0)
goto restart;
uh = (OUdphdr*)buf;
getactivity(&req);
req.aborttime = now + 30; /* don't spend more than 30 seconds */
err = convM2DNS(&buf[OUdphdrsize], len, &reqmsg);
if(err){
syslog(0, logfile, "server: input error: %s from %I", err, buf);
continue;
}
if(reqmsg.qdcount < 1){
syslog(0, logfile, "server: no questions from %I", buf);
goto freereq;
}
if(reqmsg.flags & Fresp){
syslog(0, logfile, "server: reply not request from %I", buf);
goto freereq;
}
op = reqmsg.flags & Omask;
if(op != Oquery && op != Onotify){
syslog(0, logfile, "server: op %d from %I", reqmsg.flags & Omask, buf);
goto freereq;
}
if(debug || (trace && subsume(trace, reqmsg.qd->owner->name))){
syslog(0, logfile, "%d: serve (%I/%d) %d %s %s",
req.id, buf, ((uh->rport[0])<<8)+uh->rport[1],
reqmsg.id,
reqmsg.qd->owner->name,
rrname(reqmsg.qd->type, tname, sizeof tname));
}
p = clientrxmit(&reqmsg, buf);
if(p == 0){
if(debug)
syslog(0, logfile, "%d: duplicate", req.id);
goto freereq;
}
/* loop through each question */
while(reqmsg.qd){
memset(&repmsg, 0, sizeof(repmsg));
switch(op){
case Oquery:
dnserver(&reqmsg, &repmsg, &req);
break;
case Onotify:
dnnotify(&reqmsg, &repmsg, &req);
break;
}
reply(fd, buf, &repmsg, &req);
rrfreelist(repmsg.qd);
rrfreelist(repmsg.an);
rrfreelist(repmsg.ns);
rrfreelist(repmsg.ar);
}
p->inuse = 0;
freereq:
rrfreelist(reqmsg.qd);
rrfreelist(reqmsg.an);
rrfreelist(reqmsg.ns);
rrfreelist(reqmsg.ar);
if(req.isslave){
putactivity();
_exits(0);
}
}
}
/*
* announce on udp port
*/
static int
udpannounce(char *mntpt)
{
int fd;
char buf[40];
USED(mntpt);
if((fd=announce("udp!*!nameserver", buf)) < 0)
warning("can't announce on dns udp port");
return fd;
}
static void
reply(int fd, uchar *buf, DNSmsg *rep, Request *reqp)
{
int len;
char tname[32];
RR *rp;
if(debug || (trace && subsume(trace, rep->qd->owner->name)))
syslog(0, logfile, "%d: reply (%I/%d) %d %s %s an %R ns %R ar %R",
reqp->id, buf, ((buf[4])<<8)+buf[5],
rep->id, rep->qd->owner->name,
rrname(rep->qd->type, tname, sizeof tname), rep->an, rep->ns, rep->ar);
len = convDNS2M(rep, &buf[OUdphdrsize], Maxudp);
if(len <= 0){
syslog(0, logfile, "error converting reply: %s %d", rep->qd->owner->name,
rep->qd->type);
for(rp = rep->an; rp; rp = rp->next)
syslog(0, logfile, "an %R", rp);
for(rp = rep->ns; rp; rp = rp->next)
syslog(0, logfile, "ns %R", rp);
for(rp = rep->ar; rp; rp = rp->next)
syslog(0, logfile, "ar %R", rp);
return;
}
if(udpwrite(fd, (OUdphdr*)buf, buf+OUdphdrsize, len) != len)
syslog(0, logfile, "error sending reply: %r");
}
|