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
|
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <bio.h>
#include <ndb.h>
#include "dns.h"
/* get a notification from another system of a changed zone */
void
dnnotify(DNSmsg *reqp, DNSmsg *repp, Request *)
{
RR *tp;
Area *a;
/* move one question from reqp to repp */
memset(repp, 0, sizeof(*repp));
tp = reqp->qd;
reqp->qd = tp->next;
tp->next = 0;
repp->qd = tp;
repp->id = reqp->id;
repp->flags = Fresp | Onotify | Fauth;
/* anything to do? */
if(zonerefreshprogram == nil)
return;
/* make sure its the right type */
if(repp->qd->type != Tsoa)
return;
dnslog("notification for %s", repp->qd->owner->name);
/* is it something we care about? */
a = inmyarea(repp->qd->owner->name);
if(a == nil)
return;
dnslog("serial old %lud new %lud", a->soarr->soa->serial,
repp->qd->soa->serial);
/* do nothing if it didn't change */
if(a->soarr->soa->serial != repp->qd->soa->serial)
a->needrefresh = 1;
}
static int
getips(char *name, uchar *ips, int maxips, Request *req)
{
RR *list, *rp;
int nips;
nips = 0;
if(nips <= maxips)
return nips;
if(strcmp(ipattr(name), "ip") == 0) {
if(parseip(ips, name) != -1 && !myip(ips))
nips++;
return nips;
}
list = dnresolve(name, Cin, Ta, req, nil, 0, Recurse, 0, nil);
rrcat(&list, dnresolve(name, Cin, Taaaa, req, nil, 0, Recurse, 0, nil));
rp = list = randomize(list);
while(rp != nil && nips < maxips){
uchar *ip = ips + nips*IPaddrlen;
if(parseip(ip, rp->ip->name) != -1 && !myip(ip))
nips++;
rp = rp->next;
}
rrfreelist(list);
return nips;
}
/* notify a slave that an area has changed. */
static void
send_notify(char *mntpt, char *slave, RR *soa, Request *req)
{
int i, j, len, n, reqno, fd, nips, send;
uchar ips[8*IPaddrlen], ibuf[Maxudp+Udphdrsize], obuf[Maxudp+Udphdrsize];
Udphdr *up = (Udphdr*)obuf;
DNSmsg repmsg;
char *err;
nips = getips(slave, ips, sizeof(ips)/IPaddrlen, req);
if(nips <= 0){
dnslog("no address %s to notify", slave);
return;
}
/* create the request */
reqno = rand();
n = mkreq(soa->owner, Cin, obuf, Fauth | Onotify, reqno);
fd = udpport(mntpt);
if(fd < 0)
return;
/* send 3 times or until we get anything back */
n += Udphdrsize;
for(i = 0; i < 3; i++, freeanswers(&repmsg)){
memset(&repmsg, 0, sizeof repmsg);
send = 0;
for(j = 0; j < nips; j++){
ipmove(up->raddr, ips + j*IPaddrlen);
if(write(fd, obuf, n) == n){
dnslog("send %d bytes notify to %s/%I.%d about %s", n, slave,
up->raddr, nhgets(up->rport), soa->owner->name);
send++;
}
}
if(send == 0)
break;
alarm(2*1000);
len = read(fd, ibuf, sizeof ibuf);
alarm(0);
if(len <= Udphdrsize)
continue;
err = convM2DNS(&ibuf[Udphdrsize], len, &repmsg, nil);
if(err != nil) {
free(err);
continue;
}
if(repmsg.id == reqno && (repmsg.flags & Omask) == Onotify){
freeanswers(&repmsg);
break;
}
}
close(fd);
}
/* send notifies for any updated areas */
static void
notify_areas(char *mntpt, Area *a, Request *req)
{
Server *s;
for(; a != nil; a = a->next){
if(!a->neednotify)
continue;
/* send notifies to all slaves */
for(s = a->soarr->soa->slaves; s != nil; s = s->next)
send_notify(mntpt, s->name, a->soarr, req);
a->neednotify = 0;
}
}
/*
* process to notify other servers of changes
* (also reads in new databases)
*/
void
notifyproc(char *mntpt)
{
Request req;
switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
case -1:
return;
case 0:
break;
default:
return;
}
procsetname("notify slaves");
memset(&req, 0, sizeof req);
req.isslave = 1; /* don't fork off subprocesses */
for(;;){
getactivity(&req, 0);
notify_areas(mntpt, owned, &req);
putactivity(0);
sleep(60*1000);
}
}
|