1 /* tags.c - Obtain DHCP tags from the config file
16 #include <configfile.h>
17 #include <sys/ioctl.h>
18 #include <sys/asynchio.h>
20 #include <net/gen/socket.h>
21 #include <net/gen/netdb.h>
22 #include <net/gen/in.h>
23 #include <net/gen/inet.h>
24 #include <net/gen/ether.h>
25 #include <net/gen/if_ether.h>
26 #include <net/gen/eth_hdr.h>
27 #include <net/gen/ip_hdr.h>
28 #include <net/gen/udp.h>
29 #include <net/gen/udp_hdr.h>
30 #include <net/gen/dhcp.h>
33 #define doff(field) offsetof(dhcp_t, field)
35 void settag(dhcp_t
*dp
, int tag
, void *data
, size_t len
)
37 if (!dhcp_settag(dp
, tag
, data
, len
)) {
38 /* Oops, it didn't fit? Is this really Minix??? */
40 "%s: DHCP packet too big, please trim the configuration\n",
46 static int name2ip(ipaddr_t
*pip
, const char *name
, ipaddr_t ifip
)
48 /* Translate a name to an IP address, preferably from the hosts file,
49 * but also from the DNS if being a server. Prefer the address closest
50 * to the interface with IP address 'ifip' if there are choices..
52 extern struct hostent
*_gethostent(void); /* File reading versions. */
53 extern void _endhostent(void);
55 size_t len
= strlen(name
);
56 u32_t d
, distance
= -1;
61 /* Already an IP address? */
62 if (inet_aton(name
, pip
)) return 1;
64 /* In the hosts file? */
65 while ((he
= _gethostent()) != nil
) {
69 if (strncasecmp(name
, hn
, len
) == 0
70 && (hn
[len
] == 0 || hn
[len
] == '.')
72 memcpy(&ip
, he
->h_addr
, sizeof(ip
));
73 d
= ntohl(ip
) ^ ntohl(ifip
);
80 } while ((hn
= he
->h_aliases
[++i
]) != nil
);
83 if (distance
< -1) return 1;
85 /* Nothing? Try the real DNS if being a server. */
87 if ((he
= gethostbyname(name
)) != nil
&& he
->h_addrtype
== AF_INET
) {
88 /* Select the address closest to 'ifip'. */
89 for (i
= 0; he
->h_addr_list
[i
] != nil
; i
++) {
90 memcpy(&ip
, he
->h_addr_list
[i
], sizeof(ip
));
91 d
= ntohl(ip
) ^ ntohl(ifip
);
103 static char *ip2name(ipaddr_t ip
)
105 /* Translate an IP address to a name, etc, etc. */
106 extern struct hostent
*_gethostent(void); /* File reading versions. */
107 extern void _endhostent(void);
110 /* In the hosts file? */
111 while ((he
= _gethostent()) != nil
) {
112 if (memcmp(he
->h_addr
, &ip
, sizeof(ip
)) == 0) break;
116 /* Nothing? Try the real DNS if being a server. */
117 if (he
== nil
&& serving
) {
118 he
= gethostbyaddr((char *) &ip
, sizeof(ip
), AF_INET
);
120 return he
!= nil
? he
->h_name
: nil
;
123 static int cidr_aton(const char *cidr
, ipaddr_t
*addr
, ipaddr_t
*mask
)
130 if ((slash
= strchr(cidr
, '/')) == nil
) return 0;
133 ok
= inet_aton(cidr
, &a
);
135 len
= strtoul(slash
, &check
, 10);
136 if (check
== slash
|| *check
!= 0 || len
> 32) ok
= 0;
141 *mask
= htonl(len
== 0 ? 0 : (0xFFFFFFFFUL
<< (32-len
)) & 0xFFFFFFFFUL
);
145 char *cidr_ntoa(ipaddr_t addr
, ipaddr_t mask
)
147 ipaddr_t testmask
= 0xFFFFFFFFUL
;
149 static char result
[sizeof("255.255.255.255/255.255.255.255")];
151 for (n
= 32; n
>= 0; n
--) {
152 if (mask
== htonl(testmask
)) break;
153 testmask
= (testmask
<< 1) & 0xFFFFFFFFUL
;
156 sprintf(result
, "%s/%-2d", inet_ntoa(addr
), n
);
157 if (n
== -1) strcpy(strchr(result
, '/')+1, inet_ntoa(mask
));
161 static size_t ascii2octet(u8_t
*b
, size_t size
, const char *a
)
163 /* Convert a series of hex digit pairs to an octet (binary) array at
164 * 'b' with length 'size'. Return the number of octets in 'a' or
172 while ((c
= *a
++) != 0) {
173 if (between('0', c
, '9')) c
= (c
- '0') + 0x0;
175 if (between('a', c
, 'f')) c
= (c
- 'a') + 0xa;
177 if (between('A', c
, 'F')) c
= (c
- 'A') + 0xA;
183 if (len
< size
) b
[len
] = c
<< 4;
185 if (len
< size
) b
[len
] |= c
;
190 return n
== 0 ? len
: -1;
193 void ether2clid(u8_t
*clid
, ether_addr_t
*eth
)
195 /* Convert an Ethernet address to the default client ID form. */
196 clid
[0]= DHCP_HTYPE_ETH
;
197 memcpy(clid
+1, eth
, DHCP_HLEN_ETH
);
200 static size_t ascii2clid(u8_t
*clid
, const char *a
)
202 /* Convert an ethernet address, or a series of hex digits to a client ID.
203 * Return its length if ok, otherwise -1.
208 if ((eth
= ether_aton(a
)) != nil
) {
209 ether2clid(clid
, eth
);
210 len
= 1+DHCP_HLEN_ETH
;
212 len
= ascii2octet(clid
, CLID_MAX
, a
);
217 static config_t
*dhcpconf
; /* In-core DHCP configuration. */
219 /* DHCP tag types. */
220 typedef enum { TT_ASCII
, TT_BOOLEAN
, TT_IP
, TT_NUMBER
, TT_OCTET
} tagtype_t
;
222 /* DHCP/BOOTP tag definitions. */
223 typedef struct tagdef
{
224 u8_t tag
; /* Tag number. */
225 u8_t type
; /* Type and flags. */
226 u8_t gran
; /* Granularity. */
227 u8_t max
; /* Maximum number of arguments. */
228 const char *name
; /* Defined name. */
231 #define TF_TYPE 0x07 /* To mask out the type. */
232 #define TF_STATIC 0x08 /* "Static", i.e. a struct field. */
233 #define TF_RO 0x10 /* Read-only, user can't set. */
235 /* List of static DHCP fields. The tag field is misused here as an offset
236 * into the DHCP structure.
238 static tagdef_t statictag
[] = {
239 { doff(op
), TT_NUMBER
|TF_STATIC
|TF_RO
, 1, 1, "op" },
240 { doff(htype
), TT_NUMBER
|TF_STATIC
|TF_RO
, 1, 1, "htype" },
241 { doff(hlen
), TT_NUMBER
|TF_STATIC
|TF_RO
, 1, 1, "hlen" },
242 { doff(hops
), TT_NUMBER
|TF_STATIC
|TF_RO
, 1, 1, "hops" },
243 { doff(xid
), TT_NUMBER
|TF_STATIC
|TF_RO
, 4, 1, "xid" },
244 { doff(secs
), TT_NUMBER
|TF_STATIC
|TF_RO
, 2, 1, "secs" },
245 { doff(flags
), TT_NUMBER
|TF_STATIC
|TF_RO
, 2, 1, "flags" },
246 { doff(ciaddr
), TT_IP
|TF_STATIC
|TF_RO
, 1, 1, "ciaddr" },
247 { doff(yiaddr
), TT_IP
|TF_STATIC
|TF_RO
, 1, 1, "yiaddr" },
248 { doff(siaddr
), TT_IP
|TF_STATIC
, 1, 1, "siaddr" },
249 { doff(giaddr
), TT_IP
|TF_STATIC
|TF_RO
, 1, 1, "giaddr" },
250 { doff(chaddr
), TT_OCTET
|TF_STATIC
|TF_RO
, 1, 16, "chaddr" },
251 { doff(sname
), TT_ASCII
|TF_STATIC
, 1, 64, "sname" },
252 { doff(file
), TT_ASCII
|TF_STATIC
, 1, 128, "file" },
254 #define N_STATIC arraysize(statictag)
256 static tagdef_t alltagdef
[N_STATIC
+ 254]; /* List of tag definitions. */
257 #define tagdef (alltagdef+N_STATIC-1) /* Just the optional ones. */
259 #define tagdefined(tp) ((tp)->name != nil)
261 static void inittagdef(void)
263 /* Initialize the tag definitions from the "tag" commands in the config
268 static tagdef_t predef
[] = {
269 { DHCP_TAG_NETMASK
, TT_IP
, 1, 1, "netmask" },
270 { DHCP_TAG_GATEWAY
, TT_IP
, 1, 255, "gateway" },
271 { DHCP_TAG_DNS
, TT_IP
, 1, 255, "DNSserver" },
273 static char *typenames
[] = { "ascii", "boolean", "ip", "number", "octet" };
275 static u8_t rotags
[] = {
276 DHCP_TAG_REQIP
, DHCP_TAG_OVERLOAD
, DHCP_TAG_TYPE
, DHCP_TAG_SERVERID
,
277 DHCP_TAG_REQPAR
, DHCP_TAG_MESSAGE
, DHCP_TAG_MAXDHCP
280 for (t
= 1; t
<= 254; t
++) {
287 /* Set the static and "all Minix needs" tags. */
288 memcpy(alltagdef
, statictag
, sizeof(statictag
));
289 for (tp
= predef
; tp
< arraylimit(predef
); tp
++) tagdef
[tp
->tag
] = *tp
;
291 /* Search for tag definitions in the config file. */
292 for (cfg
= dhcpconf
; cfg
!= nil
; cfg
= cfg
->next
) {
293 config_t
*cmd
= cfg
->list
;
295 if (strcasecmp(cmd
->word
, "tag") == 0) {
296 if (config_length(cmd
) == 6
297 && (cmd
->next
->flags
& CFG_DULONG
)
298 && config_isatom(cmd
->next
->next
)
299 && config_isatom(cmd
->next
->next
->next
)
300 && (cmd
->next
->next
->next
->next
->flags
& CFG_DULONG
)
301 && (cmd
->next
->next
->next
->next
->next
->flags
& CFG_DULONG
)
303 unsigned long tag
, gran
, max
;
304 const char *name
, *typename
;
307 tag
= strtoul(cmd
->next
->word
, nil
, 10);
308 name
= cmd
->next
->next
->word
;
309 typename
= cmd
->next
->next
->next
->word
;
310 gran
= strtoul(cmd
->next
->next
->next
->next
->word
, nil
, 10);
311 max
= strtoul(cmd
->next
->next
->next
->next
->next
->word
, nil
, 10);
313 for (type
= 0; type
< arraysize(typenames
); type
++) {
314 if (strcasecmp(typename
, typenames
[type
]) == 0) break;
317 if (!(1 <= tag
&& tag
<= 254)
318 || !(type
< arraysize(typenames
))
319 || !((type
== TT_NUMBER
320 && (gran
== 1 || gran
== 2 || gran
== 4))
321 || (type
!= TT_NUMBER
&& 1 <= gran
&& gran
<= 16))
325 "\"%s\", line %u: Tag definition is incorrect\n",
326 cmd
->file
, cmd
->line
);
330 tp
= &tagdef
[(int)tag
];
337 "\"%s\", line %u: Usage: tag number name type granularity max\n",
338 cmd
->file
, cmd
->line
);
344 /* Many DHCP tags are not for the user to play with. */
345 for (t
= 0; t
< arraysize(rotags
); t
++) tagdef
[rotags
[t
]].type
|= TF_RO
;
348 static tagdef_t
*tagdefbyname(const char *name
)
350 /* Find a tag definition by the name of the tag. Return null if not
355 for (tp
= alltagdef
; tp
< arraylimit(alltagdef
); tp
++) {
356 if (tagdefined(tp
) && strcasecmp(tp
->name
, name
) == 0) return tp
;
361 void initdhcpconf(void)
363 /* Read/refresh configuration from the DHCP configuration file. */
364 dhcpconf
= config_read(configfile
, 0, dhcpconf
);
365 if (config_renewed(dhcpconf
)) inittagdef();
368 static void configtag(dhcp_t
*dp
, config_t
*cmd
, ipaddr_t ifip
)
370 /* Add a tag to a DHCP packet from the config file. */
376 if (strcasecmp(cmd
->word
, "no") == 0) {
377 if (config_length(cmd
) != 2 || !config_isatom(cmd
->next
)) {
378 fprintf(stderr
, "\"%s\", line %u: Usage: no tag-name\n",
379 cmd
->file
, cmd
->line
);
386 if ((tp
= tagdefbyname(cmd
->word
)) == nil
) {
387 fprintf(stderr
, "\"%s\", line %u: Unknown tag '%s'\n",
388 cmd
->file
, cmd
->line
, cmd
->word
);
392 if (tp
->type
& TF_RO
) {
393 fprintf(stderr
, "\"%s\", line %u: Tag '%s' can't be configured\n",
394 cmd
->file
, cmd
->line
, cmd
->word
);
401 config_t
*arg
= cmd
->next
;
403 switch (tp
->type
& TF_TYPE
) {
405 if (arg
== nil
|| !config_isatom(arg
) || arg
->next
!= nil
) {
406 fprintf(stderr
, "\"%s\", line %u: Usage: %s string\n",
407 cmd
->file
, cmd
->line
, cmd
->word
);
410 strncpy((char *) data
, arg
->word
, sizeof(data
));
411 d
+= i
= strnlen((char *) data
, sizeof(data
));
414 if (arg
== nil
|| !config_isatom(arg
)
415 || !(strcasecmp(arg
->word
, "false") == 0
416 || strcasecmp(arg
->word
, "true") == 0)
419 "\"%s\", line %u: Usage: %s false|true ...\n",
420 cmd
->file
, cmd
->line
, cmd
->word
);
423 if (d
< arraylimit(data
)) {
424 *d
++ = (arg
->word
[0] != 'f' && arg
->word
[0] != 'F');
433 if (arg
== nil
|| !config_isatom(arg
)) {
434 fprintf(stderr
, "\"%s\", line %u: Usage: %s host ...\n",
435 cmd
->file
, cmd
->line
, cmd
->word
);
438 if (arg
->word
[0] == '/'
439 && between(1, len
= strtoul(arg
->word
+1, &end
, 10), 31)
442 ip
= htonl((0xFFFFFFFFUL
<< (32-len
)) & 0xFFFFFFFFUL
);
444 if (!name2ip(&ip
, arg
->word
, ifip
)) {
446 "\"%s\", line %u: Can't translate %s to an IP address\n",
447 arg
->file
, arg
->line
, arg
->word
);
450 if (d
<= arraylimit(data
) - sizeof(ip
)) {
451 memcpy(d
, &ip
, sizeof(ip
));
460 if (arg
== nil
|| !(arg
->flags
& CFG_CLONG
)) {
461 fprintf(stderr
, "\"%s\", line %u: Usage: %s number ...\n",
462 cmd
->file
, cmd
->line
, cmd
->word
);
465 n
= strtoul(arg
->word
, nil
, 0);
468 if (d
<= arraylimit(data
)) *d
++ = (n
>> (--g
* 8)) & 0xFF;
473 if (arg
== nil
|| !config_isatom(arg
) || arg
->next
!= nil
) {
474 fprintf(stderr
, "\"%s\", line %u: Usage: %s hexdigits\n",
475 cmd
->file
, cmd
->line
, cmd
->word
);
478 i
= ascii2octet(data
, sizeof(data
), arg
->word
);
481 "\"%s\", line %u: %s: Bad hexdigit string\n",
482 arg
->file
, arg
->line
, arg
->word
);
488 } while ((arg
= arg
->next
) != nil
);
490 if (d
> data
+ 255) {
491 fprintf(stderr
, "\"%s\", line %u: Tag value is way too big\n",
492 cmd
->file
, cmd
->line
);
495 if ((tp
->type
& TF_TYPE
) != TT_NUMBER
&& (i
% tp
->gran
) != 0) {
497 "\"%s\", line %u: Expected a multiple of %d initializers\n",
498 cmd
->file
, cmd
->line
, tp
->gran
);
501 if (tp
->max
!= 0 && i
> tp
->max
) {
503 "\"%s\", line %u: Got %d initializers, can have only %d\n",
504 cmd
->file
, cmd
->line
, (int) i
, tp
->max
);
508 if (tp
->type
& TF_STATIC
) {
509 size_t len
= tp
->gran
* tp
->max
;
510 if ((tp
->type
& TF_TYPE
) == TT_IP
) len
*= sizeof(ipaddr_t
);
511 memset(B(dp
) + tp
->tag
, 0, len
);
512 memcpy(B(dp
) + tp
->tag
, data
, (d
- data
));
514 settag(dp
, tp
->tag
, data
, (d
- data
));
518 int makedhcp(dhcp_t
*dp
, u8_t
*class, size_t calen
, u8_t
*client
, size_t cilen
,
519 ipaddr_t ip
, ipaddr_t ifip
, network_t
*np
)
521 /* Fill in a DHCP packet at 'dp' for the host identified by the
522 * (class, client, ip) combination. Makedhcp is normally called twice,
523 * once to find the IP address (so ip == 0) and once again to find all
524 * data that goes with that IP address (ip != 0). On the first call the
525 * return value of this function should be ignored and only 'yiaddr'
526 * checked and used as 'ip' on the next pass. True is returned iff there
527 * is information for the client on the network at interface address
528 * 'ifip', by checking if the 'ip' and 'ifip' are on the same network.
529 * If np is nonnull then we are working for one of our own interfaces, so
530 * options can be set and adjourning interfaces can be programmed.
541 /* Start creating a packet. */
543 dp
->op
= DHCP_BOOTREPLY
;
545 /* The initial TODO list is the whole DHCP config. */
546 todo
[ntodo
++]= dhcpconf
;
549 config_t
*cmd
, *follow
;
551 if (todo
[ntodo
-1] == nil
) { ntodo
--; continue; }
552 cmd
= todo
[ntodo
-1]->list
;
553 todo
[ntodo
-1]= todo
[ntodo
-1]->next
;
555 follow
= nil
; /* Macro or list to follow next? */
557 if (strcasecmp(cmd
->word
, "client") == 0) {
558 u8_t cfgid
[CLID_MAX
];
564 if (between(3, config_length(cmd
), 5)
565 && config_isatom(cmd
->next
)
566 && (cfglen
= ascii2clid(cfgid
, cmd
->next
->word
)) != -1
567 && config_isatom(cmd
->next
->next
)
568 && (((ifno
= ifname2if(cmd
->next
->next
->word
)) == -1
569 && config_length(cmd
) <= 4)
570 || ((ifno
= ifname2if(cmd
->next
->next
->word
)) != -1
571 && config_length(cmd
) >= 4
572 && config_isatom(cmd
->next
->next
->next
)))
574 if (cilen
== cfglen
&& memcmp(client
, cfgid
, cilen
) == 0
575 && (ifno
== -1 || np
== nil
|| ifno
== np
->n
)
577 config_t
*atname
= cmd
->next
->next
;
578 if (ifno
!= -1) atname
= atname
->next
;
581 if (name2ip(&hip
, name
, ifip
) && (ip
== 0 || ip
== hip
)) {
582 d
= ntohl(hip
) ^ ntohl(ifip
);
585 follow
= atname
->next
;
592 "\"%s\", line %u: Usage: client ID [ip#] host [macro|{params}]\n",
593 cmd
->file
, cmd
->line
);
597 if (strcasecmp(cmd
->word
, "class") == 0) {
602 for (clist
= cmd
->next
; clist
!= nil
603 && clist
->next
!= nil
604 && config_isatom(clist
); clist
= clist
->next
) {
606 && strncmp(clist
->word
, (char *) class, calen
) == 0
611 if (clist
== cmd
->next
|| clist
->next
!= nil
) {
613 "\"%s\", line %u: Usage: class class-name ... macro|{params}\n",
614 cmd
->file
, cmd
->line
);
616 if (match
) follow
= clist
;
618 if (strcasecmp(cmd
->word
, "host") == 0) {
619 if (config_length(cmd
) == 3
620 && config_isatom(cmd
->next
)
623 if (cidr_aton(cmd
->next
->word
, &hip
, &mask
)) {
624 if (((hip
^ ip
) & mask
) == 0) {
625 if (!gettag(dp
, DHCP_TAG_NETMASK
, nil
, nil
)) {
626 settag(dp
, DHCP_TAG_NETMASK
,
627 &mask
, sizeof(mask
));
630 follow
= cmd
->next
->next
;
633 if (name2ip(&hip
, cmd
->next
->word
, ifip
)) {
636 follow
= cmd
->next
->next
;
642 "\"%s\", line %u: Usage: host host-spec macro|{params}\n",
643 cmd
->file
, cmd
->line
);
647 if (strcasecmp(cmd
->word
, "interface") == 0) {
648 if (between(3, config_length(cmd
), 4)
649 && config_isatom(cmd
->next
)
650 && config_isatom(cmd
->next
->next
)
655 if ((ifnp
= if2net(ifname2if(cmd
->next
->word
))) == nil
) {
657 "\"%s\", line %u: Can't find interface %s\n",
658 cmd
->next
->file
, cmd
->next
->line
, cmd
->next
->word
);
661 if (!name2ip(&hip
, cmd
->next
->next
->word
, 0)) {
663 "\"%s\", line %u: Can't find IP address of %s\n",
664 cmd
->next
->next
->file
, cmd
->next
->next
->line
,
665 cmd
->next
->next
->word
);
671 follow
= cmd
->next
->next
->next
;
676 "\"%s\", line %u: Usage: interface ip# host%s\n",
677 cmd
->file
, cmd
->line
, ntodo
==1 ? " [macro|{params}]" : "");
681 if (strcasecmp(cmd
->word
, "macro") == 0) {
682 if (config_length(cmd
) == 2 && config_isatom(cmd
->next
)) {
686 fprintf(stderr
, "\"%s\", line %u: Usage: macro macro-name\n",
687 cmd
->file
, cmd
->line
);
691 if (strcasecmp(cmd
->word
, "tag") == 0) {
694 "\"%s\", line %u: A %s can't be defined here\n",
695 cmd
->file
, cmd
->line
, cmd
->word
);
699 if (strcasecmp(cmd
->word
, "option") == 0) {
704 if ((opt
= cmd
->next
) != nil
705 && config_isatom(opt
)
706 && (ifno
= ifname2if(opt
->word
)) != -1
708 if ((ifnp
= if2net(ifno
)) == nil
) {
710 "\"%s\", line %u: Interface %s is not enabled\n",
711 opt
->file
, opt
->line
, opt
->word
);
719 if (between(1, config_length(opt
), 2)
720 && config_isatom(opt
)
721 && strcasecmp(opt
->word
, "server") == 0
723 || strcasecmp(opt
->next
->word
, "inform") == 0)
726 ifnp
->flags
|= NF_SERVING
;
727 if (opt
->next
!= nil
) ifnp
->flags
|= NF_INFORM
;
730 if (config_length(opt
) == 2
731 && config_isatom(opt
)
732 && strcasecmp(opt
->word
, "relay") == 0
733 && config_isatom(opt
->next
)
736 if (!name2ip(&hip
, opt
->next
->word
, ifip
)) {
738 "\"%s\", line %u: Can't find IP address of %s\n",
739 opt
->next
->file
, opt
->next
->line
,
743 ifnp
->flags
|= NF_RELAYING
;
747 if (config_length(opt
) == 1
748 && config_isatom(opt
)
749 && strcasecmp(opt
->word
, "possessive") == 0
751 if (np
!= nil
) ifnp
->flags
|= NF_POSSESSIVE
;
753 if (config_length(opt
) == 2
754 && config_isatom(opt
)
755 && strcasecmp(opt
->word
, "hostname") == 0
756 && config_isatom(opt
->next
)
758 if (np
!= nil
) np
->hostname
= opt
->next
->word
;
760 fprintf(stderr
, "\"%s\", line %u: Unknown option\n",
761 cmd
->file
, cmd
->line
);
765 /* Must be an actual data carrying tag. */
766 configtag(dp
, cmd
, ifip
);
770 /* A client/class/host entry selects a macro or list that must
775 if (config_isatom(follow
)) { /* Macro name */
778 for (cfg
= dhcpconf
; cfg
!= nil
; cfg
= cfg
->next
) {
781 if (strcasecmp(macro
->word
, "macro") == 0) {
782 if (config_length(macro
) == 3
783 && config_isatom(macro
->next
)
784 && config_issub(macro
->next
->next
)
786 if (strcasecmp(macro
->next
->word
, follow
->word
) == 0
792 "\"%s\", line %u: Usage: macro macro-name {params}\n",
793 macro
->file
, macro
->line
);
797 follow
= cfg
== nil
? nil
: macro
->next
->next
->list
;
799 /* Simply a list of more tags and stuff. */
800 follow
= follow
->list
;
803 if (ntodo
== arraysize(todo
)) {
804 fprintf(stderr
, "\"%s\", line %u: Nesting is too deep\n",
805 follow
->file
, follow
->line
);
808 todo
[ntodo
++]= follow
;
812 /* Check if the IP and netmask are OK for the interface. */
813 if (!gettag(dp
, DHCP_TAG_NETMASK
, &pmask
, nil
)) return 0;
814 memcpy(&mask
, pmask
, sizeof(mask
));
815 if (((ip
^ ifip
) & mask
) != 0) return 0;
817 /* Fill in the hostname and/or domain. */
818 if ((hostname
= ip2name(ip
)) != nil
) {
821 if ((domain
= strchr(hostname
, '.')) != nil
) *domain
++ = 0;
823 if (!gettag(dp
, DHCP_TAG_HOSTNAME
, nil
, nil
)) {
824 settag(dp
, DHCP_TAG_HOSTNAME
, hostname
, strlen(hostname
));
827 if (domain
!= nil
&& !gettag(dp
, DHCP_TAG_DOMAIN
, nil
, nil
)) {
828 settag(dp
, DHCP_TAG_DOMAIN
, domain
, strlen(domain
));
835 static char *dhcpopname(int op
)
837 static char *onames
[] = { "??\?", "REQUEST", "REPLY" };
838 return onames
[op
< arraysize(onames
) ? op
: 0];
841 char *dhcptypename(int type
)
843 static char *tnames
[] = {
844 "??\?", "DISCOVER", "OFFER", "REQUEST", "DECLINE",
845 "ACK", "NAK", "RELEASE", "INFORM"
847 return tnames
[type
< arraysize(tnames
) ? type
: 0];
850 void printdhcp(dhcp_t
*dp
)
852 /* Print the contents of a DHCP packet, usually for debug purposes. */
857 for (tp
= alltagdef
; tp
< arraylimit(alltagdef
); tp
++) {
858 if (tp
->type
& TF_STATIC
) {
859 data
= B(dp
) + tp
->tag
;
860 len
= tp
->gran
* tp
->max
;
861 if ((tp
->type
& TF_TYPE
) == TT_IP
) len
*= sizeof(ipaddr_t
);
862 if (tp
->tag
== doff(chaddr
)) len
= dp
->hlen
;
864 /* Don't show uninteresting stuff. */
865 if (tp
->tag
== doff(htype
) && dp
->htype
== DHCP_HTYPE_ETH
) continue;
867 if (tp
->tag
== doff(hlen
) && dp
->hlen
== DHCP_HLEN_ETH
) continue;
869 if ((tp
->tag
== doff(file
) || tp
->tag
== doff(sname
))
870 && gettag(dp
, DHCP_TAG_OVERLOAD
, &ovld
, nil
)
871 && (ovld
[0] & (tp
->tag
== doff(file
) ? 1 : 2))
875 for (i
= 0; i
< len
&& data
[i
] == 0; i
++) {}
876 if (i
== len
) continue;
878 if (!gettag(dp
, tp
->tag
, &data
, &len
)) continue;
881 if (tagdefined(tp
)) {
882 printf("\t%s =", tp
->name
);
884 printf("\tT%d =", tp
->tag
);
889 switch (tp
->type
& TF_TYPE
) {
891 printf(" \"%.*s\"", (int) len
, data
);
895 printf(data
[i
++] == 0 ? " false" : " true");
899 memcpy(&ip
, data
+i
, sizeof(ip
));
900 printf(" %s", inet_ntoa(ip
));
907 do n
= (n
<< 8) | data
[i
++]; while (--g
!= 0);
908 printf(" %lu", (unsigned long) n
);
909 if ((tp
->type
& TF_STATIC
) && tp
->tag
== doff(op
)) {
910 printf(" (%s)", dhcpopname(n
));
912 if (!(tp
->type
& TF_STATIC
) && tp
->tag
== DHCP_TAG_TYPE
) {
913 printf(" (%s)", dhcptypename(n
));
917 if (i
== 0) fputc(' ', stdout
);
918 printf("%02X", data
[i
++]);