10 * format of a binding entry:
17 uchar bfirst[IPaddrlen];
19 char *xbinddir = "#9/ndb/dhcp";
22 * convert a byte array to hex
32 tohex(char *hdr, uchar *p, int len)
38 s = malloc(hlen + 2*len + 1);
42 for(; len > 0; len--){
44 *sp++ = hex(*p & 0xf);
52 * convert a client id to a string. If it's already
53 * ascii, leave it be. Otherwise, convert it to hex.
61 for(i = 0; i < n; i++)
63 return tohex("id", p, n);
71 * increment an ip address
78 for(i = IPaddrlen-1; i >= 0; i--){
88 * find a binding for an id or hardware address
96 for(tries = 0; tries < 5; tries++){
97 fd = open(file, OLOCK|ORDWR);
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 */
109 if(strstr(err, "exist") || strstr(err, "No such")){
110 /* no file, create an exclusive access file */
111 fd = create(file, ORDWR, DMEXCL|0666);
121 setbinding(Binding *b, char *id, long t)
126 b->boundto = strdup(id);
131 parsebinding(Binding *b, char *buf)
138 id = strchr(buf, '\n');
141 p = strchr(id, '\n');
147 /* replace any past info */
148 setbinding(b, id, t);
152 writebinding(int fd, Binding *b)
157 if(fprint(fd, "%ld\n%s\n", b->lease, b->boundto) < 0)
162 b->q.type = d->qid.type;
163 b->q.path = d->qid.path;
164 b->q.vers = d->qid.vers;
170 * synchronize cached binding with file. the file always wins.
173 syncbinding(Binding *b, int returnfd)
180 binddir = unsharp(xbinddir);
182 snprint(buf, sizeof(buf), "%s/%I", binddir, b->ip);
185 /* assume someone else is using it */
186 b->lease = time(0) + OfferTimeout;
190 /* reread if changed */
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);
198 parsebinding(b, buf);
199 b->lasttouched = d->mtime;
200 b->q.path = d->qid.path;
201 b->q.vers = d->qid.vers;
214 samenet(uchar *ip, Info *iip)
218 maskip(iip->ipmask, ip, x);
219 return ipcmp(x, iip->ipnet) == 0;
223 * create a record for each binding
226 initbinding(uchar *first, int n)
229 iptobinding(first, 1);
235 * find a binding for a specific ip address
238 iptobinding(uchar *ip, int mk)
242 for(b = bcache; b; b = b->next){
243 if(ipcmp(b->ip, ip) == 0){
251 b = malloc(sizeof(*b));
252 memset(b, 0, sizeof(*b));
261 lognolease(Binding *b)
263 /* renew the old binding, and hope it eventually goes away */
267 /* complain if we haven't in the last 5 minutes */
268 if(now - b->lastcomplained < 5*60)
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;
276 * find a free binding for a hw addr or id on the same network as iip
279 idtobinding(char *id, Info *iip, int ping)
285 * first look for an old binding that matches. that way
286 * clients will tend to keep the same ip addresses.
288 for(b = bcache; b; b = b->next){
289 if(b->boundto && strcmp(b->boundto, id) == 0){
290 if(!samenet(b->ip, iip))
293 /* check with the other servers */
295 if(strcmp(b->boundto, id) == 0)
300 print("looking for old for %I\n", iip->ipnet);
303 * look for oldest binding that we think is unused
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);
311 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
312 if(oldest == nil || b->lasttouched < oldesttime){
313 /* sync and check again */
315 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip))
316 if(oldest == nil || b->lasttouched < oldesttime){
318 print("have oldest\n");
319 oldesttime = b->lasttouched;
326 /* make sure noone is still using it */
328 print("return oldest\n");
329 if(ping == 0 || icmpecho(oldest->ip) == 0)
332 lognolease(oldest); /* sets lastcomplained */
335 /* try all bindings */
336 for(b = bcache; b; b = b->next){
339 if(b->lease < now && b->expoffer < now && samenet(b->ip, iip)){
341 if(ping == 0 || icmpecho(b->ip) == 0)
348 /* nothing worked, give up */
356 mkoffer(Binding *b, char *id, long leasetime)
359 if(b->lease > now + minlease)
360 leasetime = b->lease - now;
362 leasetime = minlease;
366 b->offeredto = strdup(id);
367 b->offer = leasetime;
368 b->expoffer = now + OfferTimeout;
372 * find an offer for this id
375 idtooffer(char *id, Info *iip)
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 */
385 print("b->lease %d now %d boundto %s offered %s\n", b->lease, now, b->boundto, b->offeredto);
387 || (b->boundto && strcmp(b->boundto, b->offeredto) == 0))
395 * commit a lease, this could fail
398 commitbinding(Binding *b)
405 if(b->offeredto == 0)
407 fd = syncbinding(b, 1);
410 if(b->lease > now && b->boundto && strcmp(b->boundto, b->offeredto) != 0){
414 setbinding(b, b->offeredto, now + b->offer);
415 b->lasttouched = now;
417 if(writebinding(fd, b) < 0){
426 * commit a lease, this could fail
429 releasebinding(Binding *b, char *id)
436 fd = syncbinding(b, 1);
439 if(b->lease > now && b->boundto && strcmp(b->boundto, id) != 0){
446 if(writebinding(fd, b) < 0){