comparison toys/pending/dhcpd.c @ 1005:03f72b57a092

DHCP client and server, from Ashwini Sharma.
author Rob Landley <rob@landley.net>
date Wed, 14 Aug 2013 19:09:33 -0500
parents
children d3f9e55e350a
comparison
equal deleted inserted replaced
1004:13ac68b51d3d 1005:03f72b57a092
1 /* dhcpd.c - DHCP server for dynamic network configuration.
2 *
3 * Copyright 2013 Madhur Verma <mad.flexi@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gamil.com>
5 *
6 * No Standard
7 USE_DHCPD(NEWTOY(dhcpd, ">1P#<0>65535=67fS", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
8
9 config DHCPD
10 bool "dhcpd"
11 default n
12 help
13 Usage: dhcpd [-fS] [-P N] [CONFFILE]
14
15 -f Run in foreground
16 -S Log to syslog too
17 -P N Use port N (default 67)
18
19 config DEBUG_DHCP
20 bool "debugging messeges ON/OFF"
21 default n
22 depends on DHCPD
23 */
24
25 #define FOR_dhcpd
26
27 #include "toys.h"
28 #include "toynet.h"
29 #include <linux/sockios.h>
30 #include <linux/if_ether.h>
31
32 // Todo: headers not in posix
33 #include <netinet/ip.h>
34 #include <netinet/udp.h>
35 #include <netpacket/packet.h>
36
37 #if CFG_DEBUG_DHCP==1
38 # define dbg(fmt, arg...) printf(fmt, ##arg)
39 #else
40 # define dbg(fmt, arg...)
41 #endif
42
43 #define flag_get(f,v,d) ((toys.optflags & (f)) ? (v) : (d))
44 #define flag_chk(f) ((toys.optflags & (f)) ? 1 : 0)
45
46 #define LOG_SILENT 0x0
47 #define LOG_CONSOLE 0x1
48 #define LOG_SYSTEM 0x2
49
50 #define DHCP_MAGIC 0x63825363
51
52 #define DHCPDISCOVER 1
53 #define DHCPOFFER 2
54 #define DHCPREQUEST 3
55 #define DHCPDECLINE 4
56 #define DHCPACK 5
57 #define DHCPNAK 6
58 #define DHCPRELEASE 7
59 #define DHCPINFORM 8
60
61 #define DHCP_NUM8 (1<<8)
62 #define DHCP_NUM16 (1<<9)
63 #define DHCP_NUM32 DHCP_NUM16 | DHCP_NUM8
64 #define DHCP_STRING (1<<10)
65 #define DHCP_STRLST (1<<11)
66 #define DHCP_IP (1<<12)
67 #define DHCP_IPLIST (1<<13)
68 #define DHCP_IPPLST (1<<14)
69 #define DHCP_STCRTS (1<<15)
70
71 // DHCP option codes (partial list). See RFC 2132 and
72 #define DHCP_OPT_PADDING 0x00
73 #define DHCP_OPT_HOST_NAME DHCP_STRING | 0x0c // either client informs server or server gives name to client
74 #define DHCP_OPT_REQUESTED_IP DHCP_IP | 0x32 // sent by client if specific IP is wanted
75 #define DHCP_OPT_LEASE_TIME DHCP_NUM32 | 0x33
76 #define DHCP_OPT_OPTION_OVERLOAD 0x34
77 #define DHCP_OPT_MESSAGE_TYPE DHCP_NUM8 | 0x35
78 #define DHCP_OPT_SERVER_ID DHCP_IP | 0x36 // by default server's IP
79 #define DHCP_OPT_PARAM_REQ DHCP_STRING | 0x37 // list of options client wants
80 #define DHCP_OPT_END 0xff
81
82 GLOBALS(
83 long port;
84 );
85
86 typedef struct __attribute__((packed)) dhcp_msg_s {
87 uint8_t op;
88 uint8_t htype;
89 uint8_t hlen;
90 uint8_t hops;
91 uint32_t xid;
92 uint16_t secs;
93 uint16_t flags;
94 uint32_t ciaddr;
95 uint32_t yiaddr;
96 uint32_t nsiaddr;
97 uint32_t ngiaddr;
98 uint8_t chaddr[16];
99 uint8_t sname[64];
100 uint8_t file[128];
101 uint32_t cookie;
102 uint8_t options[308];
103 } dhcp_msg_t;
104
105 typedef struct __attribute__((packed)) dhcp_raw_s {
106 struct iphdr iph;
107 struct udphdr udph;
108 dhcp_msg_t dhcp;
109 } dhcp_raw_t;
110
111 typedef struct static_lease_s {
112 struct static_lease_s *next;
113 uint32_t nip;
114 int mac[6];
115 } static_lease;
116
117 typedef struct {
118 uint32_t expires;
119 uint32_t lease_nip;
120 uint8_t lease_mac[6];
121 char hostname[20];
122 uint8_t pad[2];
123 } dyn_lease;
124
125 typedef struct option_val_s {
126 char *key;
127 uint16_t code;
128 void *val;
129 size_t len;
130 } option_val_t;
131
132 typedef struct __attribute__((__may_alias__)) server_config_s {
133 char *interface; // interface to use
134 int ifindex;
135 uint32_t server_nip;
136 uint32_t port;
137 uint8_t server_mac[6]; // our MAC address (used only for ARP probing)
138 void *options[256]; // list of DHCP options loaded from the config file
139 /* start,end are in host order: we need to compare start <= ip <= end*/
140 uint32_t start_ip; // start address of leases, in host order
141 uint32_t end_ip; // end of leases, in host order
142 uint32_t max_lease_sec; // maximum lease time (host order)
143 uint32_t min_lease_sec; // minimum lease time a client can request
144 uint32_t max_leases; // maximum number of leases (including reserved addresses)
145 uint32_t auto_time; // how long should dhcpd wait before writing a config file.
146 // if this is zero, it will only write one on SIGUSR1
147 uint32_t decline_time; // how long an address is reserved if a client returns a
148 // decline message
149 uint32_t conflict_time; // how long an arp conflict offender is leased for
150 uint32_t offer_time; // how long an offered address is reserved
151 uint32_t siaddr_nip; // "next server" bootp option
152 char *lease_file;
153 char *pidfile;
154 char *notify_file; // what to run whenever leases are written
155 char *sname; // bootp server name
156 char *boot_file; // bootp boot file option
157 struct static_lease *static_leases; // List of ip/mac pairs to assign static leases
158 } server_config_t;
159
160 typedef struct __attribute__((__may_alias__)) server_state_s {
161 uint8_t rqcode;
162 int listensock;
163 dhcp_msg_t rcvd_pkt;
164 uint8_t* rqopt;
165 dhcp_msg_t send_pkt;
166 static_lease *sleases;
167 struct arg_list *dleases;
168 } server_state_t;
169
170 struct config_keyword {
171 char *keyword;
172 int (*handler)(const char *str, void *var);
173 void *var;
174 char *def;
175 };
176
177 static option_val_t options_list[] = {
178 {"lease" , DHCP_NUM32 | 0x33, NULL, 0},
179 {"subnet" , DHCP_IP | 0x01, NULL, 0},
180 {"broadcast" , DHCP_IP | 0x1c, NULL, 0},
181 {"router" , DHCP_IP | 0x03, NULL, 0},
182 {"ipttl" , DHCP_NUM8 | 0x17, NULL, 0},
183 {"mtu" , DHCP_NUM16 | 0x1a, NULL, 0},
184 {"hostname" , DHCP_STRING | 0x0c, NULL, 0},
185 {"domain" , DHCP_STRING | 0x0f, NULL, 0},
186 {"search" , DHCP_STRLST | 0x77, NULL, 0},
187 {"nisdomain" , DHCP_STRING | 0x28, NULL, 0},
188 {"timezone" , DHCP_NUM32 | 0x02, NULL, 0},
189 {"tftp" , DHCP_STRING | 0x42, NULL, 0},
190 {"bootfile" , DHCP_STRING | 0x43, NULL, 0},
191 {"bootsize" , DHCP_NUM16 | 0x0d, NULL, 0},
192 {"rootpath" , DHCP_STRING | 0x11, NULL, 0},
193 {"wpad" , DHCP_STRING | 0xfc, NULL, 0},
194 {"serverid" , DHCP_IP | 0x36, NULL, 0},
195 {"message" , DHCP_STRING | 0x38, NULL, 0},
196 {"vlanid" , DHCP_NUM32 | 0x84, NULL, 0},
197 {"vlanpriority" , DHCP_NUM32 | 0x85, NULL, 0},
198 {"dns" , DHCP_IPLIST | 0x06, NULL, 0},
199 {"wins" , DHCP_IPLIST | 0x2c, NULL, 0},
200 {"nissrv" , DHCP_IPLIST | 0x29, NULL, 0},
201 {"ntpsrv" , DHCP_IPLIST | 0x2a, NULL, 0},
202 {"lprsrv" , DHCP_IPLIST | 0x09, NULL, 0},
203 {"swapsrv" , DHCP_IP | 0x10, NULL, 0},
204 {"routes" , DHCP_STCRTS | 0x21, NULL, 0},
205 {"staticroutes" , DHCP_STCRTS | 0x79, NULL, 0},
206 {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
207 };
208
209 struct fd_pair { int rd; int wr; };
210 static server_config_t gconfig;
211 static server_state_t gstate;
212 static uint8_t infomode;
213 static struct fd_pair sigfd;
214 static int constone = 1;
215
216 // calculate options size.
217 static int dhcp_opt_size(uint8_t *optionptr)
218 {
219 int i = 0;
220 for(;optionptr[i] != 0xff; i++) if(optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1;
221 return i;
222 }
223
224 // calculates checksum for dhcp messeges.
225 static uint16_t dhcp_checksum(void *addr, int count)
226 {
227 int32_t sum = 0;
228 uint16_t tmp = 0, *source = (uint16_t *)addr;
229
230 while (count > 1) {
231 sum += *source++;
232 count -= 2;
233 }
234 if (count > 0) {
235 *(uint8_t*)&tmp = *(uint8_t*)source;
236 sum += tmp;
237 }
238 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
239 return ~sum;
240 }
241
242 // gets information of INTERFACE and updates IFINDEX, MAC and IP
243 static int get_interface(const char *interface, int *ifindex, uint32_t *oip, uint8_t *mac)
244 {
245 struct ifreq req;
246 struct sockaddr_in *ip;
247 int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
248
249 req.ifr_addr.sa_family = AF_INET;
250 strncpy(req.ifr_name, interface, IFNAMSIZ);
251 req.ifr_name[IFNAMSIZ-1] = '\0';
252
253 xioctl(fd, SIOCGIFFLAGS, &req);
254
255 if (!(req.ifr_flags & IFF_UP)) return -1;
256 if (oip) {
257 xioctl(fd, SIOCGIFADDR, &req);
258 ip = (struct sockaddr_in*) &req.ifr_addr;
259 dbg("IP %s\n", inet_ntoa(ip->sin_addr));
260 *oip = ntohl(ip->sin_addr.s_addr);
261 }
262 if (ifindex) {
263 xioctl(fd, SIOCGIFINDEX, &req);
264 dbg("Adapter index %d\n", req.ifr_ifindex);
265 *ifindex = req.ifr_ifindex;
266 }
267 if (mac) {
268 xioctl(fd, SIOCGIFHWADDR, &req);
269 memcpy(mac, req.ifr_hwaddr.sa_data, 6);
270 dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
271 }
272 close(fd);
273 return 0;
274 }
275
276 /*
277 *logs messeges to syslog or console
278 *opening the log is still left with applet.
279 *FIXME: move to more relevent lib. probably libc.c
280 */
281 static void infomsg(uint8_t infomode, char *s, ...)
282 {
283 int used;
284 char *msg;
285 va_list p, t;
286
287 if (infomode == LOG_SILENT) return;
288 va_start(p, s);
289 va_copy(t, p);
290 used = vsnprintf(NULL, 0, s, t);
291 used++;
292 va_end(t);
293
294 msg = xmalloc(used);
295 vsnprintf(msg, used, s, p);
296 va_end(p);
297
298 if (infomode & LOG_SYSTEM) syslog(LOG_INFO, "%s", msg);
299 if (infomode & LOG_CONSOLE) printf("%s\n", msg);
300 free(msg);
301 }
302
303 /*
304 * Writes self PID in file PATH
305 * FIXME: libc implementation only writes in /var/run
306 * this is more generic as some implemenation may provide
307 * arguments to write in specific file. as dhcpd does.
308 */
309 static void write_pid(char *path)
310 {
311 int pidfile = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
312 if (pidfile > 0) {
313 char *pidbuf = utoa(getpid());
314 write(pidfile, pidbuf, strlen(pidbuf));
315 close(pidfile);
316 }
317 }
318
319 // Generic signal handler real handling is done in main funcrion.
320 static void signal_handler(int sig)
321 {
322 unsigned char ch = sig;
323 if (write(sigfd.wr, &ch, 1) != 1) dbg("can't send signal\n");
324 }
325
326 // signal setup for SIGUSR1 SIGTERM
327 static int setup_signal()
328 {
329 if (pipe((int *)&sigfd) < 0) {
330 dbg("signal pipe failed\n");
331 return -1;
332 }
333 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC);
334 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC);
335 int flags = fcntl(sigfd.wr, F_GETFL);
336 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK);
337 signal(SIGUSR1, signal_handler);
338 signal(SIGTERM, signal_handler);
339 return 0;
340 }
341
342 // String STR to UINT32 conversion strored in VAR
343 static int strtou32(const char *str, void *var)
344 {
345 char *endptr = NULL;
346 int base = 10;
347 errno=0;
348 *((uint32_t*)(var)) = 0;
349 if (str[0]=='0' && (str[1]=='x' || str[1]=='X')) {
350 base = 16;
351 str+=2;
352 }
353 long ret_val = strtol(str, &endptr, base);
354 if (errno) infomsg(infomode, "config : Invalid num %s",str);
355 else if (endptr && (*endptr!='\0'||endptr == str))
356 infomsg(infomode, "config : Not a valid num %s",str);
357 else *((uint32_t*)(var)) = (uint32_t)ret_val;
358 return 0;
359 }
360
361 // copy string STR in variable VAR
362 static int strinvar(const char *str, void *var)
363 {
364 char **dest = var;
365 if (*dest) free(*dest);
366 *dest = strdup(str);
367 return 0;
368 }
369
370 // IP String STR to binary data.
371 static int striptovar(const char *str, void *var)
372 {
373 in_addr_t addr;
374 *((uint32_t*)(var)) = 0;
375 if(!str) {
376 error_msg("config : NULL address string \n");
377 return -1;
378 }
379 if((addr = inet_addr(str)) == -1) {
380 error_msg("config : wrong address %s \n",str );
381 return -1;
382 }
383 *((uint32_t*)(var)) = (uint32_t)addr;
384 return 0;
385 }
386
387 // String to dhcp option conversion
388 static int strtoopt(const char *str, void *var)
389 {
390 char *option, *valstr, *grp, *tp;
391 uint32_t optcode = 0, inf = infomode, convtmp, mask, nip, router;
392 uint16_t flag = 0;
393 int count, size = ARRAY_LEN(options_list);
394
395 if (!*str) return 0;
396 if (!(option = strtok((char*)str, " \t="))) return -1;
397
398 infomode = LOG_SILENT;
399 strtou32(option, (uint32_t*)&optcode);
400 infomode = inf;
401
402 if (optcode > 0 && optcode < 256) { // raw option
403 for (count = 0; count < size; count++) {
404 if ((options_list[count].code & 0X00FF) == optcode) {
405 flag = (options_list[count].code & 0XFF00);
406 break;
407 }
408 }
409 } else { //string option
410 for (count = 0; count < size; count++) {
411 if (!strncmp(options_list[count].key, option, strlen(options_list[count].key))) {
412 flag = (options_list[count].code & 0XFF00);
413 optcode = (options_list[count].code & 0X00FF);
414 break;
415 }
416 }
417 }
418 if (count == size) {
419 infomsg(inf, "config : Obsolete OR Unknown Option : %s", option);
420 return -1;
421 }
422
423 if (!flag || !optcode) return -1;
424
425 if (!(valstr = strtok(NULL, " \t"))) {
426 dbg("config : option %s has no value defined.\n", option);
427 return -1;
428 }
429 dbg(" value : %-20s : ", valstr);
430 switch (flag) {
431 case DHCP_NUM32:
432 options_list[count].len = sizeof(uint32_t);
433 options_list[count].val = xmalloc(sizeof(uint32_t));
434 strtou32(valstr, &convtmp);
435 memcpy(options_list[count].val, &convtmp, sizeof(uint32_t));
436 break;
437 case DHCP_NUM16:
438 options_list[count].len = sizeof(uint16_t);
439 options_list[count].val = xmalloc(sizeof(uint16_t));
440 strtou32(valstr, &convtmp);
441 memcpy(options_list[count].val, &convtmp, sizeof(uint16_t));
442 break;
443 case DHCP_NUM8:
444 options_list[count].len = sizeof(uint8_t);
445 options_list[count].val = xmalloc(sizeof(uint8_t));
446 strtou32(valstr, &convtmp);
447 memcpy(options_list[count].val, &convtmp, sizeof(uint8_t));
448 break;
449 case DHCP_IP:
450 options_list[count].len = sizeof(uint32_t);
451 options_list[count].val = xmalloc(sizeof(uint32_t));
452 striptovar(valstr, options_list[count].val);
453 break;
454 case DHCP_STRING:
455 options_list[count].len = strlen(valstr);
456 options_list[count].val = strdup(valstr);
457 break;
458 case DHCP_IPLIST:
459 while(valstr){
460 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + sizeof(uint32_t));
461 striptovar(valstr, ((uint8_t*)options_list[count].val)+options_list[count].len);
462 options_list[count].len += sizeof(uint32_t);
463 valstr = strtok(NULL," \t");
464 }
465 break;
466 case DHCP_IPPLST:
467 break;
468 case DHCP_STCRTS:
469 /* Option binary format:
470 * mask [one byte, 0..32]
471 * ip [0..4 bytes depending on mask]
472 * router [4 bytes]
473 * may be repeated
474 * staticroutes 10.0.0.0/8 10.127.0.1, 10.11.12.0/24 10.11.12.1
475 */
476 grp = strtok(valstr, ",");;
477 while(grp){
478 while(*grp == ' ' || *grp == '\t') grp++;
479 tp = strchr(grp, '/');
480 if (!tp) error_exit("wrong formated static route option");
481 *tp = '\0';
482 mask = strtol(++tp, &tp, 10);
483 if (striptovar(grp, (uint8_t*)&nip)<0) error_exit("wrong formated static route option");
484 while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
485 if (striptovar(tp, (uint8_t*)&router)<0) error_exit("wrong formated static route option");
486 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + 1 + mask/8 + 4);
487 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &mask, 1);
488 options_list[count].len += 1;
489 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &nip, mask/8);
490 options_list[count].len += mask/8;
491 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &router, 4);
492 options_list[count].len += 4;
493 tp = NULL;
494 grp = strtok(NULL, ",");
495 }
496 break;
497 }
498 return 0;
499 }
500
501 // Reads Static leases from STR and updates inner structures.
502 static int get_staticlease(const char *str, void *var)
503 {
504 struct static_lease_s *sltmp;
505 char *tkmac, *tkip;
506 int count;
507
508 if (!*str) return 0;
509
510 if (!(tkmac = strtok((char*)str, " \t"))) {
511 infomsg(infomode, "config : static lease : mac not found");
512 return 0;
513 }
514 if (!(tkip = strtok(NULL, " \t"))) {
515 infomsg(infomode, "config : static lease : no ip bind to mac %s", tkmac);
516 return 0;
517 }
518 sltmp = xzalloc(sizeof(struct static_lease_s));
519 for (count = 0; count < 6; count++, tkmac++) {
520 errno = 0;
521 sltmp->mac[count] = strtol(tkmac, &tkmac, 16);
522 if (sltmp->mac[count]>255 || sltmp->mac[count]<0 || (*tkmac && *tkmac!=':') || errno) {
523 infomsg(infomode, "config : static lease : mac address wrong format");
524 free(sltmp);
525 return 0;
526 }
527 }
528 striptovar(tkip, &sltmp->nip);
529 sltmp->next = gstate.sleases;
530 gstate.sleases = sltmp;
531
532 return 0;
533 }
534
535 static struct config_keyword keywords[] = {
536 // keyword handler variable address default
537 {"start" , striptovar , (void*)&gconfig.start_ip , "192.168.0.20"},
538 {"end" , striptovar , (void*)&gconfig.end_ip , "192.168.0.254"},
539 {"interface" , strinvar , (void*)&gconfig.interface , "eth0"},
540 {"port" , strtou32 , (void*)&gconfig.port , "67"},
541 {"min_lease" , strtou32 , (void*)&gconfig.min_lease_sec, "60"},
542 {"max_leases" , strtou32 , (void*)&gconfig.max_leases , "235"},
543 {"auto_time" , strtou32 , (void*)&gconfig.auto_time , "7200"},
544 {"decline_time" , strtou32 , (void*)&gconfig.decline_time , "3600"},
545 {"conflict_time", strtou32 , (void*)&gconfig.conflict_time, "3600"},
546 {"offer_time" , strtou32 , (void*)&gconfig.offer_time , "60"},
547 {"lease_file" , strinvar , (void*)&gconfig.lease_file , "/var/lib/misc/dhcpd.leases"}, //LEASES_FILE
548 {"pidfile" , strinvar , (void*)&gconfig.pidfile , "/var/run/dhcpd.pid"}, //DPID_FILE
549 {"siaddr" , striptovar , (void*)&gconfig.siaddr_nip , "0.0.0.0"},
550 {"option" , strtoopt , (void*)&gconfig.options , ""},
551 {"opt" , strtoopt , (void*)&gconfig.options , ""},
552 {"notify_file" , strinvar , (void*)&gconfig.notify_file , ""},
553 {"sname" , strinvar , (void*)&gconfig.sname , ""},
554 {"boot_file" , strinvar , (void*)&gconfig.boot_file , ""},
555 {"static_lease" , get_staticlease , (void*)&gconfig.static_leases, ""},
556 };
557
558 // Parses the server config file and updates the global server config accordingly.
559 static int parse_server_config(char *config_file, struct config_keyword *confkey)
560 {
561 FILE *fs = NULL;
562 char *confline_temp = NULL,*confline = NULL, *tk = NULL, *tokens[2] = {NULL, NULL};
563 int len, linelen, tcount, count, size = ARRAY_LEN(keywords);
564
565 for (count = 0; count < size; count++)
566 if (confkey[count].handler) confkey[count].handler(confkey[count].def, confkey[count].var);
567
568 if (!(fs = fopen(config_file, "r"))) perror_msg("%s", config_file);
569 for (len = 0, linelen = 0; fs;) {
570 len = getline(&confline_temp, (size_t*) &linelen, fs);
571 confline = confline_temp;
572 if (len <= 0) break;
573 for (; *confline == ' '; confline++, len--);
574 if ((confline[0] == '#') || (confline[0] == '\n')) goto free_conf_continue;
575 tk = strchr(confline, '#');
576 if (tk) {
577 for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
578 *tk = '\0';
579 }
580 tk = strchr(confline, '\n');
581 if (tk) {
582 for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
583 *tk = '\0';
584 }
585 for (tcount=0, tk=strtok(confline, " \t"); tk && (tcount < 2);
586 tcount++, tk=strtok(NULL,(tcount==1)?"":" \t")) {
587 while ((*tk == '\t') || (*tk == ' ')) tk++;
588 tokens[tcount] = xstrdup(tk);
589 }
590 if (tcount<=1) goto free_tk0_continue;
591 for (count = 0; count < size; count++) {
592 if (!strcmp(confkey[count].keyword,tokens[0])) {
593 dbg("got config : %15s : ", confkey[count].keyword);
594 if (confkey[count].handler(tokens[1], confkey[count].var) == 0)
595 dbg("%s \n", tokens[1]);
596 break;
597 }
598 }
599 if (tokens[1]) { free(tokens[1]); tokens[1] = NULL; }
600 free_tk0_continue:
601 if (tokens[0]) { free(tokens[0]); tokens[0] = NULL; }
602 free_conf_continue:
603 free(confline_temp);
604 confline_temp = NULL;
605 }
606 if (fs) fclose(fs);
607 return 0;
608 }
609
610 // opens UDP socket for listen
611 static int open_listensock(void)
612 {
613 struct sockaddr_in addr;
614 struct ifreq ifr;
615
616 if (gstate.listensock > 0) close(gstate.listensock);
617
618 dbg("Opening listen socket on *:%d %s\n", gconfig.port, gconfig.interface);
619 gstate.listensock = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
620 setsockopt(gstate.listensock, SOL_SOCKET, SO_REUSEADDR, &constone, sizeof(constone));
621 if (setsockopt(gstate.listensock, SOL_SOCKET, SO_BROADCAST, &constone, sizeof(constone)) == -1) {
622 dbg("OPEN : brodcast ioctl failed.\n");
623 close(gstate.listensock);
624 return -1;
625 }
626 memset(&ifr, 0, sizeof(ifr));
627 strncpy(ifr.ifr_name, gconfig.interface, IFNAMSIZ);
628 ifr.ifr_name[IFNAMSIZ -1] = '\0';
629 setsockopt(gstate.listensock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
630
631 memset(&addr, 0, sizeof(addr));
632 addr.sin_family = AF_INET;
633 addr.sin_port = (flag_chk(FLAG_P))?htons(TT.port):htons(67); //SERVER_PORT
634 addr.sin_addr.s_addr = INADDR_ANY ;
635
636 if (bind(gstate.listensock, (struct sockaddr *) &addr, sizeof(addr))) {
637 close(gstate.listensock);
638 perror_exit("bind failed");
639 }
640 dbg("OPEN : success\n");
641 return 0;
642 }
643
644 // Sends data through raw socket.
645 static int send_packet(uint8_t broadcast)
646 {
647 struct sockaddr_ll dest_sll;
648 dhcp_raw_t packet;
649 unsigned padding;
650 int fd, result = -1;
651 uint8_t bmacaddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
652
653 memset(&packet, 0, sizeof(dhcp_raw_t));
654 memcpy(&packet.dhcp, &gstate.send_pkt, sizeof(dhcp_msg_t));
655
656 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
657 dbg("SEND : socket failed\n");
658 return -1;
659 }
660 memset(&dest_sll, 0, sizeof(dest_sll));
661 dest_sll.sll_family = AF_PACKET;
662 dest_sll.sll_protocol = htons(ETH_P_IP);
663 dest_sll.sll_ifindex = gconfig.ifindex;
664 dest_sll.sll_halen = 6;
665 memcpy(dest_sll.sll_addr, (broadcast)?bmacaddr:gstate.rcvd_pkt.chaddr , 6);
666
667 if (bind(fd, (struct sockaddr *) &dest_sll, sizeof(dest_sll)) < 0) {
668 dbg("SEND : bind failed\n");
669 close(fd);
670 return -1;
671 }
672 padding = 308 - 1 - dhcp_opt_size(gstate.send_pkt.options);
673 packet.iph.protocol = IPPROTO_UDP;
674 packet.iph.saddr = gconfig.server_nip;
675 packet.iph.daddr = (broadcast || (gstate.rcvd_pkt.ciaddr == 0))?INADDR_BROADCAST:gstate.rcvd_pkt.ciaddr;
676 packet.udph.source = htons(67);//SERVER_PORT
677 packet.udph.dest = htons(68); //CLIENT_PORT
678 packet.udph.len = htons(sizeof(dhcp_raw_t) - sizeof(struct iphdr) - padding);
679 packet.iph.tot_len = packet.udph.len;
680 packet.udph.check = dhcp_checksum(&packet, sizeof(dhcp_raw_t) - padding);
681 packet.iph.tot_len = htons(sizeof(dhcp_raw_t) - padding);
682 packet.iph.ihl = sizeof(packet.iph) >> 2;
683 packet.iph.version = IPVERSION;
684 packet.iph.ttl = IPDEFTTL;
685 packet.iph.check = dhcp_checksum(&packet.iph, sizeof(packet.iph));
686
687 result = sendto(fd, &packet, sizeof(dhcp_raw_t) - padding, 0,
688 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
689
690 dbg("sendto %d\n", result);
691 close(fd);
692 if (result < 0) dbg("PACKET send error\n");
693 return result;
694 }
695
696 // Reads from UDP socket
697 static int read_packet(void)
698 {
699 int ret;
700
701 memset(&gstate.rcvd_pkt, 0, sizeof(dhcp_msg_t));
702 ret = read(gstate.listensock, &gstate.rcvd_pkt, sizeof(dhcp_msg_t));
703 if (ret < 0) {
704 dbg("Packet read error, ignoring. \n");
705 return ret; // returns -1
706 }
707 if (gstate.rcvd_pkt.cookie != htonl(DHCP_MAGIC)) {
708 dbg("Packet with bad magic, ignoring. \n");
709 return -2;
710 }
711 if (gstate.rcvd_pkt.op != 1) { //BOOTPREQUEST
712 dbg("Not a BOOT REQUEST ignoring. \n");
713 return -2;
714 }
715 if (gstate.rcvd_pkt.hlen != 6) {
716 dbg("hlen != 6 ignoring. \n");
717 return -2;
718 }
719 dbg("Received a packet. Size : %d \n", ret);
720 return ret;
721 }
722
723 // Preapres a dhcp packet with defaults and configs
724 static uint8_t* prepare_send_pkt(void)
725 {
726 memset((void*)&gstate.send_pkt, 0, sizeof(gstate.send_pkt));
727 gstate.send_pkt.op = 2; //BOOTPREPLY
728 gstate.send_pkt.htype = 1;
729 gstate.send_pkt.hlen = 6;
730 gstate.send_pkt.xid = gstate.rcvd_pkt.xid;
731 gstate.send_pkt.cookie = htonl(DHCP_MAGIC);
732 gstate.send_pkt.nsiaddr = gconfig.server_nip;
733 memcpy(gstate.send_pkt.chaddr, gstate.rcvd_pkt.chaddr, 16);
734 gstate.send_pkt.options[0] = DHCP_OPT_END;
735 return gstate.send_pkt.options;
736 }
737
738 // Sets a option value in dhcp packet's option field
739 static uint8_t* set_optval(uint8_t *optptr, uint16_t opt, void *var, size_t len)
740 {
741 while (*optptr != DHCP_OPT_END) optptr++;
742 *optptr++ = (uint8_t)(opt & 0x00FF);
743 *optptr++ = (uint8_t) len;
744 memcpy(optptr, var, len);
745 optptr += len;
746 *optptr = DHCP_OPT_END;
747 return optptr;
748 }
749
750 // Gets a option value from dhcp packet's option field
751 static uint8_t* get_optval(uint8_t *optptr, uint16_t opt, void *var)
752 {
753 size_t len;
754 uint8_t overloaded = 0;
755
756 while (1) {
757 while (*optptr == DHCP_OPT_PADDING) optptr++;
758 if ((*optptr & 0x00FF) == DHCP_OPT_END) break;
759 if ((*optptr & 0x00FF) == DHCP_OPT_OPTION_OVERLOAD) {
760 overloaded = optptr[2];
761 optptr += optptr[1] + 2;
762 }
763 len = optptr[1];
764 if (*optptr == (opt & 0x00FF))
765 switch (opt & 0xFF00) {
766 case DHCP_NUM32: // FALLTHROUGH
767 case DHCP_IP:
768 memcpy(var, optptr+2, sizeof(uint32_t));
769 optptr += len + 2;
770 return optptr;
771 break;
772 case DHCP_NUM16:
773 memcpy(var, optptr+2, sizeof(uint16_t));
774 optptr += len + 2;
775 return optptr;
776 break;
777 case DHCP_NUM8:
778 memcpy(var, optptr+2, sizeof(uint8_t));
779 optptr += len + 2;
780 return optptr;
781 break;
782 case DHCP_STRING:
783 var = xstrndup((char*) optptr, len);
784 optptr += len + 2;
785 return optptr;
786 break;
787 }
788 optptr += len + 2;
789 }
790 if ((overloaded == 1) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.file, opt, var);
791 if ((overloaded == 2) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.sname, opt, var);
792 return optptr;
793 }
794
795 // Retrives Requested Parameter list from dhcp req packet.
796 static uint8_t get_reqparam(uint8_t **list)
797 {
798 uint8_t len, *optptr;
799 if(*list) free(*list);
800 for (optptr = gstate.rcvd_pkt.options;
801 *optptr && *optptr!=((DHCP_OPT_PARAM_REQ) & 0x00FF); optptr+=optptr[1]+2);
802 len = *++optptr;
803 *list = xzalloc(len+1);
804 memcpy(*list, ++optptr, len);
805 return len;
806 }
807
808 // Sets values of req param in dhcp offer packet.
809 static uint8_t* set_reqparam(uint8_t *optptr, uint8_t *list)
810 {
811 uint8_t reqcode;
812 int count, size = ARRAY_LEN(options_list);
813
814 while (*list) {
815 reqcode = *list++;
816 for (count = 0; count < size; count++) {
817 if ((options_list[count].code & 0X00FF)==reqcode) {
818 if (!(options_list[count].len) || !(options_list[count].val)) break;
819 for (; *optptr && *optptr!=DHCP_OPT_END; optptr+=optptr[1]+2);
820 *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
821 *optptr++ = (uint8_t) options_list[count].len;
822 memcpy(optptr, options_list[count].val, options_list[count].len);
823 optptr += options_list[count].len;
824 *optptr = DHCP_OPT_END;
825 break;
826 }
827 }
828 }
829 return optptr;
830 }
831
832 static void run_notify(char **argv)
833 {
834 struct stat sts;
835 volatile int error = 0;
836 pid_t pid;
837
838 if (stat(argv[0], &sts) == -1 && errno == ENOENT) {
839 infomsg(infomode, "notify file: %s : not exist.", argv[0]);
840 return;
841 }
842 fflush(NULL);
843
844 pid = vfork();
845 if (pid < 0) {
846 dbg("Fork failed.\n");
847 return;
848 }
849 if (!pid) {
850 execvp(argv[0], argv);
851 error = errno;
852 _exit(111);
853 }
854 if (error) {
855 waitpid(pid, NULL, 0);
856 errno = error;
857 }
858 dbg("script complete.\n");
859 }
860
861 static int write_leasefile(void)
862 {
863 int fd;
864 uint32_t curr, tmp_time;
865 int64_t timestamp;
866 struct arg_list *listdls = gstate.dleases;
867 dyn_lease *dls;
868
869 if ((fd = open(gconfig.lease_file, O_WRONLY | O_CREAT | O_TRUNC)) < 0) {
870 perror_msg("can't open %s ", gconfig.lease_file);
871 return fd;
872 }
873
874 curr = timestamp = time(NULL);
875 timestamp = SWAP_BE64(timestamp);
876 writeall(fd, &timestamp, sizeof(timestamp));
877
878 while (listdls) {
879 dls = (dyn_lease*)listdls->arg;
880 tmp_time = dls->expires;
881 dls->expires -= curr;
882 if ((int32_t) dls->expires < 0) goto skip;
883 dls->expires = htonl(dls->expires);
884 writeall(fd, dls, sizeof(dyn_lease));
885 skip:
886 dls->expires = tmp_time;
887 listdls = listdls->next;
888 }
889 close(fd);
890 if (gconfig.notify_file) {
891 char *argv[3];
892 argv[0] = gconfig.notify_file;
893 argv[1] = gconfig.lease_file;
894 argv[2] = NULL;
895 run_notify(argv);
896 }
897 return 0;
898 }
899
900 // Update max lease time from options.
901 static void set_maxlease(void)
902 {
903 int count, size = ARRAY_LEN(options_list);
904 for (count = 0; count < size; count++)
905 if (options_list[count].val && options_list[count].code == (DHCP_OPT_LEASE_TIME)) {
906 gconfig.max_lease_sec = *((uint32_t*)options_list[count].val);
907 break;
908 }
909 if (!gconfig.max_lease_sec) gconfig.max_lease_sec = (60*60*24*10);// DEFAULT_LEASE_TIME;
910 }
911
912 // Returns lease time for client.
913 static uint32_t get_lease(uint32_t req_exp)
914 {
915 uint32_t now = time(NULL);
916 req_exp = req_exp - now;
917 if ((req_exp <= 0) || (req_exp > gconfig.max_lease_sec))
918 return gconfig.max_lease_sec;
919
920 if (req_exp < gconfig.min_lease_sec)
921 return gconfig.min_lease_sec;
922
923 return req_exp;
924 }
925
926 // Verify ip NIP in current leases ( assigned or not)
927 static int verifyip_in_lease(uint32_t nip, uint8_t mac[6])
928 {
929 static_lease *sls;
930 struct arg_list *listdls;
931
932 for (listdls = gstate.dleases; listdls; listdls = listdls->next) {
933 if (((dyn_lease*) listdls->arg)->lease_nip == nip) {
934 if (((int32_t)(((dyn_lease*) listdls->arg)->expires) - time(NULL)) < 0)
935 return 0;
936 return -1;
937 }
938 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) return -1;
939 }
940 for (sls = gstate.sleases; sls; sls = sls->next)
941 if (sls->nip == nip) return -2;
942
943 if ((ntohl(nip) < gconfig.start_ip) || (ntohl(nip) > gconfig.end_ip))
944 return -3;
945
946 return 0;
947 }
948
949 // add ip assigned_nip to dynamic lease.
950 static int addip_to_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname, uint8_t update)
951 {
952 dyn_lease *dls;
953 struct arg_list *listdls = gstate.dleases;
954 uint32_t now = time(NULL);
955
956 while (listdls) {
957 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
958 if (update) *req_exp = get_lease(*req_exp + ((dyn_lease*) listdls->arg)->expires);
959 ((dyn_lease*) listdls->arg)->expires = *req_exp + now;
960 return 0;
961 }
962 listdls = listdls->next;
963 }
964
965 dls = xzalloc(sizeof(dyn_lease));
966 memcpy(dls->lease_mac, mac, 6);
967 dls->lease_nip = assigned_nip;
968 if (hostname) memcpy(dls->hostname, hostname, 20);
969
970 if (update) *req_exp = get_lease(*req_exp + now);
971 dls->expires = *req_exp + now;
972
973 listdls = xzalloc(sizeof(struct arg_list));
974 listdls->next = gstate.dleases;
975 listdls->arg = (char*)dls;
976 gstate.dleases = listdls;
977
978 return 0;
979 }
980
981 // delete ip assigned_nip from dynamic lease.
982 static int delip_from_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t del_time)
983 {
984 struct arg_list *listdls = gstate.dleases;
985
986 while (listdls) {
987 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
988 ((dyn_lease*) listdls->arg)->expires = del_time + time(NULL);
989 return 0;
990 }
991 listdls = listdls->next;
992 }
993 return -1;
994 }
995
996 // returns a IP from static, dynamic leases or free ip pool, 0 otherwise.
997 static uint32_t getip_from_pool(uint32_t req_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname)
998 {
999 uint32_t nip = 0;
1000 static_lease *sls = gstate.sleases;
1001 struct arg_list *listdls = gstate.dleases, *tmp = NULL;
1002
1003 if (req_nip && (!verifyip_in_lease(req_nip, mac))) nip = req_nip;
1004
1005 if (!nip) {
1006 while (listdls) {
1007 if (!memcmp(((dyn_lease*)listdls->arg)->lease_mac, mac, 6)) {
1008 nip = ((dyn_lease*)listdls->arg)->lease_nip;
1009 if (tmp) tmp->next = listdls->next;
1010 else gstate.dleases = listdls->next;
1011 free(listdls->arg);
1012 free(listdls);
1013 if (verifyip_in_lease(nip, mac) < 0) nip = 0;
1014 break;
1015 }
1016 tmp = listdls;
1017 listdls = listdls->next;
1018 }
1019 }
1020 if (!nip) {
1021 while (sls) {
1022 if (memcmp(sls->mac, mac, 6) == 0) {
1023 nip = sls->nip;
1024 break;
1025 }
1026 sls = sls->next;
1027 }
1028 }
1029 if (!nip) {
1030 for (nip = htonl(gconfig.start_ip); ntohl(nip) <= gconfig.end_ip; ) {
1031 if (!verifyip_in_lease(nip, mac)) break;
1032 nip = ntohl(nip);
1033 nip = htonl(++nip);
1034 }
1035 if (ntohl(nip) > gconfig.end_ip) {
1036 nip = 0;
1037 infomsg(infomode, "can't find free IP in IP Pool.");
1038 }
1039 }
1040 if (nip) addip_to_lease(nip, mac, req_exp, hostname, 1);
1041 return nip;
1042 }
1043
1044 static int read_leasefile(void)
1045 {
1046 uint32_t passed, ip;
1047 int32_t tmp_time;
1048 int64_t timestamp;
1049 dyn_lease *dls;
1050 int ret = -1, fd = open(gconfig.lease_file, O_RDONLY);
1051
1052 if (fd < 0) return fd;
1053 dls = xzalloc(sizeof(dyn_lease));
1054
1055 if (read(fd, &timestamp, sizeof(timestamp)) != sizeof(timestamp)) goto error_exit;
1056
1057 timestamp = SWAP_BE64(timestamp);
1058 passed = time(NULL) - timestamp;
1059 if ((uint64_t)passed > 12 * 60 * 60) goto error_exit;
1060
1061 while (read(fd, dls, sizeof(dyn_lease)) == sizeof(dyn_lease)) {
1062 ip = ntohl(dls->lease_nip);
1063 if (ip >= gconfig.start_ip && ip <= gconfig.end_ip) {
1064 tmp_time = ntohl(dls->expires) - passed;
1065 if (tmp_time < 0) continue;
1066 addip_to_lease(dls->lease_nip, dls->lease_mac, (uint32_t*)&tmp_time, dls->hostname, 0);
1067 }
1068 }
1069 ret = 0;
1070 error_exit:
1071 free(dls);
1072 close(fd);
1073 return ret;
1074 }
1075
1076 void dhcpd_main(void)
1077 {
1078 struct timeval tv;
1079 int retval;
1080 uint8_t *optptr, msgtype = 0;
1081 uint32_t waited = 0, serverid = 0, requested_nip = 0;
1082 uint32_t reqested_lease = 0, ip_pool_size = 0;
1083 char *hstname = NULL;
1084 fd_set rfds;
1085
1086 infomode = LOG_CONSOLE;
1087 if (!(flag_chk(FLAG_f))) {
1088 daemonize();
1089 infomode = LOG_SILENT;
1090 }
1091 if (flag_chk(FLAG_S)) {
1092 openlog("UDHCPD :", LOG_PID, LOG_DAEMON);
1093 infomode |= LOG_SYSTEM;
1094 }
1095 setlinebuf(stdout);
1096 parse_server_config((toys.optc==1)?toys.optargs[0]:"/etc/dhcpd.conf", keywords); //DHCPD_CONF_FILE
1097 infomsg(infomode, "toybox dhcpd started");
1098 gconfig.start_ip = ntohl(gconfig.start_ip);
1099 gconfig.end_ip = ntohl(gconfig.end_ip);
1100 ip_pool_size = gconfig.end_ip - gconfig.start_ip + 1;
1101 if (gconfig.max_leases > ip_pool_size) {
1102 error_msg("max_leases=%u is too big, setting to %u", (unsigned) gconfig.max_leases, ip_pool_size);
1103 gconfig.max_leases = ip_pool_size;
1104 }
1105 write_pid(gconfig.pidfile);
1106 set_maxlease();
1107 read_leasefile();
1108
1109 if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip,
1110 gconfig.server_mac)<0)
1111 perror_exit("Failed to get interface %s", gconfig.interface);
1112 gconfig.server_nip = htonl(gconfig.server_nip);
1113
1114 setup_signal();
1115 open_listensock();
1116 fcntl(gstate.listensock, F_SETFD, FD_CLOEXEC);
1117
1118 for (;;) {
1119 uint32_t timestmp = time(NULL);
1120 FD_ZERO(&rfds);
1121 FD_SET(gstate.listensock, &rfds);
1122 FD_SET(sigfd.rd, &rfds);
1123 tv.tv_sec = gconfig.auto_time - waited;
1124 tv.tv_usec = 0;
1125 retval = 0;
1126 serverid = 0;
1127 msgtype = 0;
1128
1129 int maxfd = (sigfd.rd > gstate.listensock)? sigfd.rd : gstate.listensock;
1130 dbg("select waiting ....\n");
1131 retval = select(maxfd + 1, &rfds, NULL, NULL, (gconfig.auto_time?&tv:NULL));
1132 if (retval < 0) {
1133 if (errno == EINTR) {
1134 waited += (unsigned) time(NULL) - timestmp;
1135 continue;
1136 }
1137 dbg("Error in select wait again...\n");
1138 continue;
1139 }
1140 if (!retval) { // Timed out
1141 dbg("select wait Timed Out...\n");
1142 waited = 0;
1143 write_leasefile();
1144 if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip, gconfig.server_mac)<0)
1145 perror_exit("Interface lost %s\n", gconfig.interface);
1146 gconfig.server_nip = htonl(gconfig.server_nip);
1147 continue;
1148 }
1149 if (FD_ISSET(sigfd.rd, &rfds)) { // Some Activity on RDFDs : is signal
1150 unsigned char sig;
1151 if (read(sigfd.rd, &sig, 1) != 1) {
1152 dbg("signal read failed.\n");
1153 continue;
1154 }
1155 switch (sig) {
1156 case SIGUSR1:
1157 infomsg(infomode, "Received SIGUSR1");
1158 write_leasefile();
1159 continue;
1160 case SIGTERM:
1161 infomsg(infomode, "Received SIGTERM");
1162 write_leasefile();
1163 unlink(gconfig.pidfile);
1164 exit(0);
1165 break;
1166 default: break;
1167 }
1168 }
1169 if (FD_ISSET(gstate.listensock, &rfds)) { // Some Activity on RDFDs : is socket
1170 dbg("select listen sock read\n");
1171 if (read_packet() < 0) {
1172 open_listensock();
1173 continue;
1174 }
1175 waited += time(NULL) - timestmp;
1176 get_optval((uint8_t*)&gstate.rcvd_pkt.options, DHCP_OPT_MESSAGE_TYPE, &gstate.rqcode);
1177 if (gstate.rqcode == 0 || gstate.rqcode < DHCPDISCOVER
1178 || gstate.rqcode > DHCPINFORM) {
1179 dbg("no or bad message type option, ignoring packet.\n");
1180 continue;
1181 }
1182 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
1183 if (serverid && (serverid != gconfig.server_nip)) {
1184 dbg("server ID doesn't match, ignoring packet.\n");
1185 continue;
1186 }
1187 switch (gstate.rqcode) {
1188 case DHCPDISCOVER:
1189 msgtype = DHCPOFFER;
1190 dbg("Message Type : DHCPDISCOVER\n");
1191 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1192 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
1193 reqested_lease = gconfig.offer_time;
1194 get_reqparam(&gstate.rqopt);
1195 optptr = prepare_send_pkt();
1196 gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
1197 if(!gstate.send_pkt.yiaddr){
1198 msgtype = DHCPNAK;
1199 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1200 send_packet(1);
1201 break;
1202 }
1203 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
1204 reqested_lease = htonl(get_lease(reqested_lease + time(NULL)));
1205 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1206 optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
1207 optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
1208 optptr = set_reqparam(optptr, gstate.rqopt);
1209 send_packet(1);
1210 break;
1211 case DHCPREQUEST:
1212 msgtype = DHCPACK;
1213 dbg("Message Type : DHCPREQUEST\n");
1214 optptr = prepare_send_pkt();
1215 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1216 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
1217 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
1218 gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
1219 if (!serverid) reqested_lease = gconfig.max_lease_sec;
1220 if (!gstate.send_pkt.yiaddr) {
1221 msgtype = DHCPNAK;
1222 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1223 send_packet(1);
1224 break;
1225 }
1226 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1227 optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
1228 reqested_lease = htonl(reqested_lease);
1229 optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
1230 send_packet(1);
1231 write_leasefile();
1232 break;
1233 case DHCPDECLINE:// FALL THROUGH
1234 case DHCPRELEASE:
1235 dbg("Message Type : DHCPDECLINE or DHCPRELEASE \n");
1236 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
1237 if (serverid != gconfig.server_nip) break;
1238 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1239 delip_from_lease(requested_nip, gstate.rcvd_pkt.chaddr, (gstate.rqcode==DHCPRELEASE)?0:gconfig.decline_time);
1240 break;
1241 default:
1242 dbg("Message Type : %u\n", gstate.rqcode);
1243 break;
1244 }
1245 }
1246 }
1247 }