Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <ip.h>
4 #include <bio.h>
5 #include <ndb.h>
6 #include <ctype.h>
7 #include "dat.h"
9 /*
10 * format of a binding entry:
11 * char ipaddr[32];
12 * char id[32];
13 * char hwa[32];
14 * char otime[10];
15 */
16 Binding *bcache;
17 uchar bfirst[IPaddrlen];
18 char *binddir = nil;
19 char *xbinddir = "#9/ndb/dhcp";
21 /*
22 * convert a byte array to hex
23 */
24 static char
25 hex(int x)
26 {
27 if(x < 10)
28 return x + '0';
29 return x - 10 + 'a';
30 }
31 extern char*
32 tohex(char *hdr, uchar *p, int len)
33 {
34 char *s, *sp;
35 int hlen;
37 hlen = strlen(hdr);
38 s = malloc(hlen + 2*len + 1);
39 sp = s;
40 strcpy(sp, hdr);
41 sp += hlen;
42 for(; len > 0; len--){
43 *sp++ = hex(*p>>4);
44 *sp++ = hex(*p & 0xf);
45 p++;
46 }
47 *sp = 0;
48 return s;
49 }
51 /*
52 * convert a client id to a string. If it's already
53 * ascii, leave it be. Otherwise, convert it to hex.
54 */
55 extern char*
56 toid(uchar *p, int n)
57 {
58 int i;
59 char *s;
61 for(i = 0; i < n; i++)
62 if(!isprint(p[i]))
63 return tohex("id", p, n);
64 s = malloc(n + 1);
65 memmove(s, p, n);
66 s[n] = 0;
67 return s;
68 }
70 /*
71 * increment an ip address
72 */
73 static void
74 incip(uchar *ip)
75 {
76 int i, x;
78 for(i = IPaddrlen-1; i >= 0; i--){
79 x = ip[i];
80 x++;
81 ip[i] = x;
82 if((x & 0x100) == 0)
83 break;
84 }
85 }
87 /*
88 * find a binding for an id or hardware address
89 */
90 static int
91 lockopen(char *file)
92 {
93 char err[ERRMAX];
94 int fd, tries;
96 for(tries = 0; tries < 5; tries++){
97 fd = open(file, OLOCK|ORDWR);
98 if(fd >= 0)
99 return fd;
100 print("open %s: %r\n", file);
101 errstr(err, sizeof err);
102 if(strstr(err, "lock")){
103 /* wait for other process to let go of lock */
104 sleep(250);
106 /* try again */
107 continue;
109 if(strstr(err, "exist") || strstr(err, "No such")){
110 /* no file, create an exclusive access file */
111 fd = create(file, ORDWR, DMEXCL|0666);
112 chmod(file, 0666);
113 if(fd >= 0)
114 return fd;
117 return -1;
120 void
121 setbinding(Binding *b, char *id, long t)
123 if(b->boundto)
124 free(b->boundto);
126 b->boundto = strdup(id);
127 b->lease = t;
130 static void
131 parsebinding(Binding *b, char *buf)
133 long t;
134 char *id, *p;
136 /* parse */
137 t = atoi(buf);
138 id = strchr(buf, '\n');
139 if(id){
140 *id++ = 0;
141 p = strchr(id, '\n');
142 if(p)
143 *p = 0;
144 } else
145 id = "";
147 /* replace any past info */
148 setbinding(b, id, t);
151 static int
152 writebinding(int fd, Binding *b)
154 Dir *d;
156 seek(fd, 0, 0);
157 if(fprint(fd, "%ld\n%s\n", b->lease, b->boundto) < 0)
158 return -1;
159 d = dirfstat(fd);
160 if(d == nil)
161 return -1;
162 b->q.type = d->qid.type;
163 b->q.path = d->qid.path;
164 b->q.vers = d->qid.vers;
165 free(d);
166 return 0;
169 /*
170 * synchronize cached binding with file. the file always wins.
171 */
172 int
173 syncbinding(Binding *b, int returnfd)
175 char buf[512];
176 int i, fd;
177 Dir *d;
179 if(binddir == nil)
180 binddir = unsharp(xbinddir);
182 snprint(buf, sizeof(buf), "%s/%I", binddir, b->ip);
183 fd = lockopen(buf);
184 if(fd < 0){
185 /* assume someone else is using it */
186 b->lease = time(0) + OfferTimeout;
187 return -1;
190 /* reread if changed */
191 d = dirfstat(fd);
192 if(d != nil) /* BUG? */
193 if(d->qid.type != b->q.type || d->qid.path != b->q.path || d->qid.vers != b->q.vers){
194 i = read(fd, buf, sizeof(buf)-1);
195 if(i < 0)
196 i = 0;
197 buf[i] = 0;
198 parsebinding(b, buf);
199 b->lasttouched = d->mtime;
200 b->q.path = d->qid.path;
201 b->q.vers = d->qid.vers;
204 free(d);
206 if(returnfd)
207 return fd;
209 close(fd);
210 return 0;
213 extern int
214 samenet(uchar *ip, Info *iip)
216 uchar x[IPaddrlen];
218 maskip(iip->ipmask, ip, x);
219 return ipcmp(x, iip->ipnet) == 0;
222 /*
223 * create a record for each binding
224 */
225 extern void
226 initbinding(uchar *first, int n)
228 while(n-- > 0){
229 iptobinding(first, 1);
230 incip(first);
234 /*
235 * find a binding for a specific ip address
236 */
237 extern Binding*
238 iptobinding(uchar *ip, int mk)
240 Binding *b;
242 for(b = bcache; b; b = b->next){
243 if(ipcmp(b->ip, ip) == 0){
244 syncbinding(b, 0);
245 return b;
249 if(mk == 0)
250 return 0;
251 b = malloc(sizeof(*b));
252 memset(b, 0, sizeof(*b));
253 ipmove(b->ip, ip);
254 b->next = bcache;
255 bcache = b;
256 syncbinding(b, 0);
257 return b;
260 static void
261 lognolease(Binding *b)
263 /* renew the old binding, and hope it eventually goes away */
264 b->offer = 5*60;
265 commitbinding(b);
267 /* complain if we haven't in the last 5 minutes */
268 if(now - b->lastcomplained < 5*60)
269 return;
270 syslog(0, blog, "dhcp: lease for %I to %s ended at %ld but still in use\n",
271 b->ip, b->boundto != nil ? b->boundto : "?", b->lease);
272 b->lastcomplained = now;
275 /*
276 * find a free binding for a hw addr or id on the same network as iip
277 */
278 extern Binding*
279 idtobinding(char *id, Info *iip, int ping)
281 Binding *b, *oldest;
282 int oldesttime;
284 /*
285 * first look for an old binding that matches. that way
286 * clients will tend to keep the same ip addresses.
287 */
288 for(b = bcache; b; b = b->next){
289 if(b->boundto && strcmp(b->boundto, id) == 0){
290 if(!samenet(b->ip, iip))
291 continue;
293 /* check with the other servers */
294 syncbinding(b, 0);
295 if(strcmp(b->boundto, id) == 0)
296 return b;
300 print("looking for old for %I\n", iip->ipnet);
302 /*
303 * look for oldest binding that we think is unused
304 */
305 for(;;){
306 oldest = nil;
307 oldesttime = 0;
308 for(b = bcache; b; b = b->next){
309 print("tried %d now %d lease %d exp %d %I\n", b->tried, now, b->lease, b->expoffer, b->ip);
310 if(b->tried != now)
311 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
312 if(oldest == nil || b->lasttouched < oldesttime){
313 /* sync and check again */
314 syncbinding(b, 0);
315 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
316 if(oldest == nil || b->lasttouched < oldesttime){
317 oldest = b;
318 print("have oldest\n");
319 oldesttime = b->lasttouched;
323 if(oldest == nil)
324 break;
326 /* make sure noone is still using it */
327 oldest->tried = now;
328 print("return oldest\n");
329 if(ping == 0 || icmpecho(oldest->ip) == 0)
330 return oldest;
332 lognolease(oldest); /* sets lastcomplained */
335 /* try all bindings */
336 for(b = bcache; b; b = b->next){
337 syncbinding(b, 0);
338 if(b->tried != now)
339 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip)){
340 b->tried = now;
341 if(ping == 0 || icmpecho(b->ip) == 0)
342 return b;
344 lognolease(b);
348 /* nothing worked, give up */
349 return 0;
352 /*
353 * create an offer
354 */
355 extern void
356 mkoffer(Binding *b, char *id, long leasetime)
358 if(leasetime <= 0){
359 if(b->lease > now + minlease)
360 leasetime = b->lease - now;
361 else
362 leasetime = minlease;
364 if(b->offeredto)
365 free(b->offeredto);
366 b->offeredto = strdup(id);
367 b->offer = leasetime;
368 b->expoffer = now + OfferTimeout;
371 /*
372 * find an offer for this id
373 */
374 extern Binding*
375 idtooffer(char *id, Info *iip)
377 Binding *b;
379 /* look for an offer to this id */
380 for(b = bcache; b; b = b->next){
381 print("%I %I ? offeredto %s id %s\n", b->ip, iip->ipnet, b->offeredto, id);
382 if(b->offeredto && strcmp(b->offeredto, id) == 0 && samenet(b->ip, iip)){
383 /* make sure some other system hasn't stolen it */
384 syncbinding(b, 0);
385 print("b->lease %d now %d boundto %s offered %s\n", b->lease, now, b->boundto, b->offeredto);
386 if(b->lease < now
387 || (b->boundto && strcmp(b->boundto, b->offeredto) == 0))
388 return b;
391 return 0;
394 /*
395 * commit a lease, this could fail
396 */
397 extern int
398 commitbinding(Binding *b)
400 int fd;
401 long now;
403 now = time(0);
405 if(b->offeredto == 0)
406 return -1;
407 fd = syncbinding(b, 1);
408 if(fd < 0)
409 return -1;
410 if(b->lease > now && b->boundto && strcmp(b->boundto, b->offeredto) != 0){
411 close(fd);
412 return -1;
414 setbinding(b, b->offeredto, now + b->offer);
415 b->lasttouched = now;
417 if(writebinding(fd, b) < 0){
418 close(fd);
419 return -1;
421 close(fd);
422 return 0;
425 /*
426 * commit a lease, this could fail
427 */
428 extern int
429 releasebinding(Binding *b, char *id)
431 int fd;
432 long now;
434 now = time(0);
436 fd = syncbinding(b, 1);
437 if(fd < 0)
438 return -1;
439 if(b->lease > now && b->boundto && strcmp(b->boundto, id) != 0){
440 close(fd);
441 return -1;
443 b->lease = 0;
444 b->expoffer = 0;
446 if(writebinding(fd, b) < 0){
447 close(fd);
448 return -1;
450 close(fd);
451 return 0;