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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <bio.h>
#include <ndb.h>
#include <ctype.h>
#include "dat.h"
/*
* format of a binding entry:
* char ipaddr[32];
* char id[32];
* char hwa[32];
* char otime[10];
*/
Binding *bcache;
uchar bfirst[IPaddrlen];
char *binddir = nil;
char *xbinddir = "#9/ndb/dhcp";
/*
* convert a byte array to hex
*/
static char
hex(int x)
{
if(x < 10)
return x + '0';
return x - 10 + 'a';
}
extern char*
tohex(char *hdr, uchar *p, int len)
{
char *s, *sp;
int hlen;
hlen = strlen(hdr);
s = malloc(hlen + 2*len + 1);
sp = s;
strcpy(sp, hdr);
sp += hlen;
for(; len > 0; len--){
*sp++ = hex(*p>>4);
*sp++ = hex(*p & 0xf);
p++;
}
*sp = 0;
return s;
}
/*
* convert a client id to a string. If it's already
* ascii, leave it be. Otherwise, convert it to hex.
*/
extern char*
toid(uchar *p, int n)
{
int i;
char *s;
for(i = 0; i < n; i++)
if(!isprint(p[i]))
return tohex("id", p, n);
s = malloc(n + 1);
memmove(s, p, n);
s[n] = 0;
return s;
}
/*
* increment an ip address
*/
static void
incip(uchar *ip)
{
int i, x;
for(i = IPaddrlen-1; i >= 0; i--){
x = ip[i];
x++;
ip[i] = x;
if((x & 0x100) == 0)
break;
}
}
/*
* find a binding for an id or hardware address
*/
static int
lockopen(char *file)
{
char err[ERRMAX];
int fd, tries;
for(tries = 0; tries < 5; tries++){
fd = open(file, OLOCK|ORDWR);
if(fd >= 0)
return fd;
errstr(err, sizeof err);
if(strstr(err, "lock")){
/* wait for other process to let go of lock */
sleep(250);
/* try again */
continue;
}
if(strstr(err, "exist") || strstr(err, "No such")){
/* no file, create an exclusive access file */
fd = create(file, ORDWR, DMEXCL|0666);
chmod(file, 0666);
if(fd >= 0)
return fd;
}
}
return -1;
}
void
setbinding(Binding *b, char *id, long t)
{
if(b->boundto)
free(b->boundto);
b->boundto = strdup(id);
b->lease = t;
}
static void
parsebinding(Binding *b, char *buf)
{
long t;
char *id, *p;
/* parse */
t = atoi(buf);
id = strchr(buf, '\n');
if(id){
*id++ = 0;
p = strchr(id, '\n');
if(p)
*p = 0;
} else
id = "";
/* replace any past info */
setbinding(b, id, t);
}
static int
writebinding(int fd, Binding *b)
{
Dir *d;
seek(fd, 0, 0);
if(fprint(fd, "%ld\n%s\n", b->lease, b->boundto) < 0)
return -1;
d = dirfstat(fd);
if(d == nil)
return -1;
b->q.type = d->qid.type;
b->q.path = d->qid.path;
b->q.vers = d->qid.vers;
free(d);
return 0;
}
/*
* synchronize cached binding with file. the file always wins.
*/
int
syncbinding(Binding *b, int returnfd)
{
char buf[512];
int i, fd;
Dir *d;
if(binddir == nil)
binddir = unsharp(xbinddir);
snprint(buf, sizeof(buf), "%s/%I", binddir, b->ip);
fd = lockopen(buf);
if(fd < 0){
/* assume someone else is using it */
b->lease = time(0) + OfferTimeout;
return -1;
}
/* reread if changed */
d = dirfstat(fd);
if(d != nil) /* BUG? */
if(d->qid.type != b->q.type || d->qid.path != b->q.path || d->qid.vers != b->q.vers){
i = read(fd, buf, sizeof(buf)-1);
if(i < 0)
i = 0;
buf[i] = 0;
parsebinding(b, buf);
b->lasttouched = d->mtime;
b->q.path = d->qid.path;
b->q.vers = d->qid.vers;
}
free(d);
if(returnfd)
return fd;
close(fd);
return 0;
}
extern int
samenet(uchar *ip, Info *iip)
{
uchar x[IPaddrlen];
maskip(iip->ipmask, ip, x);
return ipcmp(x, iip->ipnet) == 0;
}
/*
* create a record for each binding
*/
extern void
initbinding(uchar *first, int n)
{
while(n-- > 0){
iptobinding(first, 1);
incip(first);
}
}
/*
* find a binding for a specific ip address
*/
extern Binding*
iptobinding(uchar *ip, int mk)
{
Binding *b;
for(b = bcache; b; b = b->next){
if(ipcmp(b->ip, ip) == 0){
syncbinding(b, 0);
return b;
}
}
if(mk == 0)
return 0;
b = malloc(sizeof(*b));
memset(b, 0, sizeof(*b));
ipmove(b->ip, ip);
b->next = bcache;
bcache = b;
syncbinding(b, 0);
return b;
}
static void
lognolease(Binding *b)
{
/* renew the old binding, and hope it eventually goes away */
b->offer = 5*60;
commitbinding(b);
/* complain if we haven't in the last 5 minutes */
if(now - b->lastcomplained < 5*60)
return;
syslog(0, blog, "dhcp: lease for %I to %s ended at %ld but still in use\n",
b->ip, b->boundto != nil ? b->boundto : "?", b->lease);
b->lastcomplained = now;
}
/*
* find a free binding for a hw addr or id on the same network as iip
*/
extern Binding*
idtobinding(char *id, Info *iip, int ping)
{
Binding *b, *oldest;
int oldesttime;
/*
* first look for an old binding that matches. that way
* clients will tend to keep the same ip addresses.
*/
for(b = bcache; b; b = b->next){
if(b->boundto && strcmp(b->boundto, id) == 0){
if(!samenet(b->ip, iip))
continue;
/* check with the other servers */
syncbinding(b, 0);
if(strcmp(b->boundto, id) == 0)
return b;
}
}
/*
* look for oldest binding that we think is unused
*/
for(;;){
oldest = nil;
oldesttime = 0;
for(b = bcache; b; b = b->next){
if(b->tried != now)
if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
if(oldest == nil || b->lasttouched < oldesttime){
/* sync and check again */
syncbinding(b, 0);
if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
if(oldest == nil || b->lasttouched < oldesttime){
oldest = b;
oldesttime = b->lasttouched;
}
}
}
if(oldest == nil)
break;
/* make sure noone is still using it */
oldest->tried = now;
if(ping == 0 || icmpecho(oldest->ip) == 0)
return oldest;
lognolease(oldest); /* sets lastcomplained */
}
/* try all bindings */
for(b = bcache; b; b = b->next){
syncbinding(b, 0);
if(b->tried != now)
if(b->lease < now && b->expoffer < now && samenet(b->ip, iip)){
b->tried = now;
if(ping == 0 || icmpecho(b->ip) == 0)
return b;
lognolease(b);
}
}
/* nothing worked, give up */
return 0;
}
/*
* create an offer
*/
extern void
mkoffer(Binding *b, char *id, long leasetime)
{
if(leasetime <= 0){
if(b->lease > now + minlease)
leasetime = b->lease - now;
else
leasetime = minlease;
}
if(b->offeredto)
free(b->offeredto);
b->offeredto = strdup(id);
b->offer = leasetime;
b->expoffer = now + OfferTimeout;
}
/*
* find an offer for this id
*/
extern Binding*
idtooffer(char *id, Info *iip)
{
Binding *b;
/* look for an offer to this id */
for(b = bcache; b; b = b->next){
if(b->offeredto && strcmp(b->offeredto, id) == 0 && samenet(b->ip, iip)){
/* make sure some other system hasn't stolen it */
syncbinding(b, 0);
if(b->lease < now
|| (b->boundto && strcmp(b->boundto, b->offeredto) == 0))
return b;
}
}
return 0;
}
/*
* commit a lease, this could fail
*/
extern int
commitbinding(Binding *b)
{
int fd;
long now;
now = time(0);
if(b->offeredto == 0)
return -1;
fd = syncbinding(b, 1);
if(fd < 0)
return -1;
if(b->lease > now && b->boundto && strcmp(b->boundto, b->offeredto) != 0){
close(fd);
return -1;
}
setbinding(b, b->offeredto, now + b->offer);
b->lasttouched = now;
if(writebinding(fd, b) < 0){
close(fd);
return -1;
}
close(fd);
return 0;
}
/*
* commit a lease, this could fail
*/
extern int
releasebinding(Binding *b, char *id)
{
int fd;
long now;
now = time(0);
fd = syncbinding(b, 1);
if(fd < 0)
return -1;
if(b->lease > now && b->boundto && strcmp(b->boundto, id) != 0){
close(fd);
return -1;
}
b->lease = 0;
b->expoffer = 0;
if(writebinding(fd, b) < 0){
close(fd);
return -1;
}
close(fd);
return 0;
}
|