Repository navigation
Expand file tree
/
Copy pathtags.c
More file actions
924 lines (838 loc) · 24.4 KB
/
Copy pathtags.c
File metadata and controls
924 lines (838 loc) · 24.4 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/* tags.c - Obtain DHCP tags from the config file
* Author: Kees J. Bot
* 16 Dec 2000
*/
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <configfile.h>
#include <sys/ioctl.h>
#include <sys/asynchio.h>
#include <net/hton.h>
#include <net/gen/socket.h>
#include <netdb.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/ether.h>
#include <net/gen/if_ether.h>
#include <net/gen/eth_hdr.h>
#include <net/gen/ip_hdr.h>
#include <net/gen/udp.h>
#include <net/gen/udp_hdr.h>
#include <net/gen/dhcp.h>
#include "dhcpd.h"
#define doff(field) offsetof(dhcp_t, field)
void settag(dhcp_t *dp, int tag, void *data, size_t len)
{
if (!dhcp_settag(dp, tag, data, len)) {
/* Oops, it didn't fit? Is this really Minix??? */
fprintf(stderr,
"%s: DHCP packet too big, please trim the configuration\n",
program);
exit(1);
}
}
static int name2ip(ipaddr_t *pip, const char *name, ipaddr_t ifip)
{
/* Translate a name to an IP address, preferably from the hosts file,
* but also from the DNS if being a server. Prefer the address closest
* to the interface with IP address 'ifip' if there are choices..
*/
extern struct hostent *_gethostent(void); /* File reading versions. */
extern void _endhostent(void);
struct hostent *he;
size_t len= strlen(name);
u32_t d, distance= -1;
ipaddr_t ip;
int i;
char *hn;
/* Already an IP address? */
if (inet_aton(name, pip)) return 1;
/* In the hosts file? */
while ((he= _gethostent()) != nil) {
hn= he->h_name;
i= -1;
do {
if (strncasecmp(name, hn, len) == 0
&& (hn[len] == 0 || hn[len] == '.')
) {
memcpy(&ip, he->h_addr, sizeof(ip));
d= ntohl(ip) ^ ntohl(ifip);
if (d < distance) {
*pip= ip;
distance= d;
}
break;
}
} while ((hn= he->h_aliases[++i]) != nil);
}
_endhostent();
if (distance < -1) return 1;
/* Nothing? Try the real DNS if being a server. */
if (serving) {
if ((he= gethostbyname(name)) != nil && he->h_addrtype == AF_INET) {
/* Select the address closest to 'ifip'. */
for (i= 0; he->h_addr_list[i] != nil; i++) {
memcpy(&ip, he->h_addr_list[i], sizeof(ip));
d= ntohl(ip) ^ ntohl(ifip);
if (d < distance) {
*pip= ip;
distance= d;
}
}
return 1;
}
}
return 0;
}
static char *ip2name(ipaddr_t ip)
{
/* Translate an IP address to a name, etc, etc. */
extern struct hostent *_gethostent(void); /* File reading versions. */
extern void _endhostent(void);
struct hostent *he;
/* In the hosts file? */
while ((he= _gethostent()) != nil) {
if (memcmp(he->h_addr, &ip, sizeof(ip)) == 0) break;
}
_endhostent();
/* Nothing? Try the real DNS if being a server. */
if (he == nil && serving) {
he= gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
}
return he != nil ? he->h_name : nil;
}
static int cidr_aton(const char *cidr, ipaddr_t *addr, ipaddr_t *mask)
{
char *slash, *check;
ipaddr_t a;
int ok;
unsigned long len;
if ((slash= strchr(cidr, '/')) == nil) return 0;
*slash++= 0;
ok= inet_aton(cidr, &a);
len= strtoul(slash, &check, 10);
if (check == slash || *check != 0 || len > 32) ok= 0;
*--slash= '/';
if (!ok) return 0;
*addr= a;
*mask= htonl(len == 0 ? 0 : (0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
return 1;
}
char *cidr_ntoa(ipaddr_t addr, ipaddr_t mask)
{
ipaddr_t testmask= 0xFFFFFFFFUL;
int n;
static char result[sizeof("255.255.255.255/255.255.255.255")];
for (n= 32; n >= 0; n--) {
if (mask == htonl(testmask)) break;
testmask= (testmask << 1) & 0xFFFFFFFFUL;
}
sprintf(result, "%s/%-2d", inet_ntoa(addr), n);
if (n == -1) strcpy(strchr(result, '/')+1, inet_ntoa(mask));
return result;
}
static size_t ascii2octet(u8_t *b, size_t size, const char *a)
{
/* Convert a series of hex digit pairs to an octet (binary) array at
* 'b' with length 'size'. Return the number of octets in 'a' or
* -1 on error.
*/
size_t len;
int n, c;
len= 0;
n= 0;
while ((c= *a++) != 0) {
if (between('0', c, '9')) c= (c - '0') + 0x0;
else
if (between('a', c, 'f')) c= (c - 'a') + 0xa;
else
if (between('A', c, 'F')) c= (c - 'A') + 0xA;
else {
return -1;
}
if (n == 0) {
if (len < size) b[len] = c << 4;
} else {
if (len < size) b[len] |= c;
len++;
}
n ^= 1;
}
return n == 0 ? len : -1;
}
void ether2clid(u8_t *clid, ether_addr_t *eth)
{
/* Convert an Ethernet address to the default client ID form. */
clid[0]= DHCP_HTYPE_ETH;
memcpy(clid+1, eth, DHCP_HLEN_ETH);
}
static size_t ascii2clid(u8_t *clid, const char *a)
{
/* Convert an ethernet address, or a series of hex digits to a client ID.
* Return its length if ok, otherwise -1.
*/
size_t len;
ether_addr_t *eth;
if ((eth= ether_aton(a)) != nil) {
ether2clid(clid, eth);
len= 1+DHCP_HLEN_ETH;
} else {
len= ascii2octet(clid, CLID_MAX, a);
}
return len;
}
static config_t *dhcpconf; /* In-core DHCP configuration. */
/* DHCP tag types. */
typedef enum { TT_ASCII, TT_BOOLEAN, TT_IP, TT_NUMBER, TT_OCTET } tagtype_t;
/* DHCP/BOOTP tag definitions. */
typedef struct tagdef {
u8_t tag; /* Tag number. */
u8_t type; /* Type and flags. */
u8_t gran; /* Granularity. */
u8_t max; /* Maximum number of arguments. */
const char *name; /* Defined name. */
} tagdef_t;
#define TF_TYPE 0x07 /* To mask out the type. */
#define TF_STATIC 0x08 /* "Static", i.e. a struct field. */
#define TF_RO 0x10 /* Read-only, user can't set. */
/* List of static DHCP fields. The tag field is misused here as an offset
* into the DHCP structure.
*/
static tagdef_t statictag[] = {
{ doff(op), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "op" },
{ doff(htype), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "htype" },
{ doff(hlen), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hlen" },
{ doff(hops), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hops" },
{ doff(xid), TT_NUMBER|TF_STATIC|TF_RO, 4, 1, "xid" },
{ doff(secs), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "secs" },
{ doff(flags), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "flags" },
{ doff(ciaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "ciaddr" },
{ doff(yiaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "yiaddr" },
{ doff(siaddr), TT_IP|TF_STATIC, 1, 1, "siaddr" },
{ doff(giaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "giaddr" },
{ doff(chaddr), TT_OCTET|TF_STATIC|TF_RO, 1, 16, "chaddr" },
{ doff(sname), TT_ASCII|TF_STATIC, 1, 64, "sname" },
{ doff(file), TT_ASCII|TF_STATIC, 1, 128, "file" },
};
#define N_STATIC arraysize(statictag)
static tagdef_t alltagdef[N_STATIC + 254]; /* List of tag definitions. */
#define tagdef (alltagdef+N_STATIC-1) /* Just the optional ones. */
#define tagdefined(tp) ((tp)->name != nil)
static void inittagdef(void)
{
/* Initialize the tag definitions from the "tag" commands in the config
* file.
*/
int t;
t
10BC8
agdef_t *tp;
static tagdef_t predef[] = {
{ DHCP_TAG_NETMASK, TT_IP, 1, 1, "netmask" },
{ DHCP_TAG_GATEWAY, TT_IP, 1, 255, "gateway" },
{ DHCP_TAG_DNS, TT_IP, 1, 255, "DNSserver" },
};
static char *typenames[] = { "ascii", "boolean", "ip", "number", "octet" };
config_t *cfg;
static u8_t rotags[] = {
DHCP_TAG_REQIP, DHCP_TAG_OVERLOAD, DHCP_TAG_TYPE, DHCP_TAG_SERVERID,
DHCP_TAG_REQPAR, DHCP_TAG_MESSAGE, DHCP_TAG_MAXDHCP
};
for (t= 1; t <= 254; t++) {
tp= &tagdef[t];
tp->tag= t;
tp->type= TT_OCTET;
tp->name= nil;
}
/* Set the static and "all Minix needs" tags. */
memcpy(alltagdef, statictag, sizeof(statictag));
for (tp= predef; tp < arraylimit(predef); tp++) tagdef[tp->tag] = *tp;
/* Search for tag definitions in the config file. */
for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
config_t *cmd= cfg->list;
if (strcasecmp(cmd->word, "tag") == 0) {
if (config_length(cmd) == 6
&& (cmd->next->flags & CFG_DULONG)
&& config_isatom(cmd->next->next)
&& config_isatom(cmd->next->next->next)
&& (cmd->next->next->next->next->flags & CFG_DULONG)
&& (cmd->next->next->next->next->next->flags & CFG_DULONG)
) {
unsigned long tag, gran, max;
const char *name, *typename;
unsigned type;
tag= strtoul(cmd->next->word, nil, 10);
name= cmd->next->next->word;
typename= cmd->next->next->next->word;
gran= strtoul(cmd->next->next->next->next->word, nil, 10);
max= strtoul(cmd->next->next->next->next->next->word, nil, 10);
for (type= 0; type < arraysize(typenames); type++) {
if (strcasecmp(typename, typenames[type]) == 0) break;
}
if (!(1 <= tag && tag <= 254)
|| !(type < arraysize(typenames))
|| !((type == TT_NUMBER
&& (gran == 1 || gran == 2 || gran == 4))
|| (type != TT_NUMBER && 1 <= gran && gran <= 16))
|| !(max <= 255)
) {
fprintf(stderr,
"\"%s\", line %u: Tag definition is incorrect\n",
cmd->file, cmd->line);
exit(1);
}
tp= &tagdef[(int)tag];
tp->type= type;
tp->name= name;
tp->gran= gran;
tp->max= max;
} else {
fprintf(stderr,
"\"%s\", line %u: Usage: tag number name type granularity max\n",
cmd->file, cmd->line);
exit(1);
}
}
}
/* Many DHCP tags are not for the user to play with. */
for (t= 0; t < arraysize(rotags); t++) tagdef[rotags[t]].type |= TF_RO;
}
static tagdef_t *tagdefbyname(const char *name)
{
/* Find a tag definition by the name of the tag. Return null if not
* defined.
*/
tagdef_t *tp;
for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
if (tagdefined(tp) && strcasecmp(tp->name, name) == 0) return tp;
}
return nil;
}
void initdhcpconf(void)
{
/* Read/refresh configuration from the DHCP configuration file. */
dhcpconf= config_read(configfile, 0, dhcpconf);
if (config_renewed(dhcpconf)) inittagdef();
}
static void configtag(dhcp_t *dp, config_t *cmd, ipaddr_t ifip)
{
/* Add a tag to a DHCP packet from the config file. */
tagdef_t *tp;
u8_t data[260], *d;
size_t i;
int delete= 0;
if (strcasecmp(cmd->word, "no") == 0) {
if (config_length(cmd) != 2 || !config_isatom(cmd->next)) {
fprintf(stderr, "\"%s\", line %u: Usage: no tag-name\n",
cmd->file, cmd->line);
exit(1);
}
cmd= cmd->next;
delete= 1;
}
if ((tp= tagdefbyname(cmd->word)) == nil) {
fprintf(stderr, "\"%s\", line %u: Unknown tag '%s'\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
if (tp->type & TF_RO) {
fprintf(stderr, "\"%s\", line %u: Tag '%s' can't be configured\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
i= 0;
d= data;
if (!delete) {
config_t *arg= cmd->next;
do {
switch (tp->type & TF_TYPE) {
case TT_ASCII: {
if (arg == nil || !config_isatom(arg) || arg->next != nil) {
fprintf(stderr, "\"%s\", line %u: Usage: %s string\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
strncpy((char *) data, arg->word, sizeof(data));
d += i = strnlen((char *) data, sizeof(data));
break;}
case TT_BOOLEAN: {
if (arg == nil || !config_isatom(arg)
|| !(strcasecmp(arg->word, "false") == 0
|| strcasecmp(arg->word, "true") == 0)
) {
fprintf(stderr,
"\"%s\", line %u: Usage: %s false|true ...\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
if (d < arraylimit(data)) {
*d++ = (arg->word[0] != 'f' && arg->word[0] != 'F');
}
i++;
break;}
case TT_IP: {
ipaddr_t ip;
unsigned long len;
char *end;
if (arg == nil || !config_isatom(arg)) {
fprintf(stderr, "\"%s\", line %u: Usage: %s host ...\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
if (arg->word[0] == '/'
&& between(1, len= strtoul(arg->word+1, &end, 10), 31)
&& *end == 0
) {
ip= htonl((0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
} else
if (!name2ip(&ip, arg->word, ifip)) {
fprintf(stderr,
"\"%s\", line %u: Can't translate %s to an IP address\n",
arg->file, arg->line, arg->word);
exit(1);
}
if (d <= arraylimit(data) - sizeof(ip)) {
memcpy(d, &ip, sizeof(ip));
d += sizeof(ip);
}
i++;
break;}
case TT_NUMBER: {
unsigned long n;
int g;
if (arg == nil || !(arg->flags & CFG_CLONG)) {
fprintf(stderr, "\"%s\", line %u: Usage: %s number ...\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
n= strtoul(arg->word, nil, 0);
g= tp->gran;
do {
if (d <= arraylimit(data)) *d++ = (n >> (--g * 8)) & 0xFF;
} while (g != 0);
i++;
break;}
case TT_OCTET: {
if (arg == nil || !config_isatom(arg) || arg->next != nil) {
fprintf(stderr, "\"%s\", line %u: Usage: %s hexdigits\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
i= ascii2octet(data, sizeof(data), arg->word);
if (i == -1) {
fprintf(stderr,
"\"%s\", line %u: %s: Bad hexdigit string\n",
arg->file, arg->line, arg->word);
exit(1);
}
d= data + i;
break;}
}
} while ((arg= arg->next) != nil);
if (d > data + 255) {
fprintf(stderr, "\"%s\", line %u: Tag value is way too big\n",
cmd->file, cmd->line);
exit(1);
}
if ((tp->type & TF_TYPE) != TT_NUMBER && (i % tp->gran) != 0) {
fprintf(stderr,
"\"%s\", line %u: Expected a multiple of %d initializers\n",
cmd->file, cmd->line, tp->gran);
exit(1);
}
if (tp->max != 0 && i > tp->max) {
fprintf(stderr,
"\"%s\", line %u: Got %d initializers, can have only %d\n",
cmd->file, cmd->line, (int) i, tp->max);
exit(1);
}
}
if (tp->type & TF_STATIC) {
size_t len= tp->gran * tp->max;
if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
memset(B(dp) + tp->tag, 0, len);
memcpy(B(dp) + tp->tag, data, (d - data));
} else {
settag(dp, tp->tag, data, (d - data));
}
}
int makedhcp(dhcp_t *dp, u8_t *class, size_t calen, u8_t *client, size_t cilen,
ipaddr_t ip, ipaddr_t ifip, network_t *np)
{
/* Fill in a DHCP packet at 'dp' for the host identified by the
* (class, client, ip) combination. Makedhcp is normally called twice,
* once to find the IP address (so ip == 0) and once again to find all
* data that goes with that IP address (ip != 0). On the first call the
* return value of this function should be ignored and only 'yiaddr'
* checked and used as 'ip' on the next pass. True is returned iff there
* is information for the client on the network at interface address
* 'ifip', by checking if the 'ip' and 'ifip' are on the same network.
* If np is nonnull then we are working for one of our own interfaces, so
* options can be set and adjourning interfaces can be programmed.
*/
config_t *todo[16];
size_t ntodo= 0;
ipaddr_t hip, mask;
u8_t *pmask;
char *hostname;
u32_t distance= -1;
initdhcpconf();
/* Start creating a packet. */
dhcp_init(dp);
dp->op= DHCP_BOOTREPLY;
/* The initial TODO list is the whole DHCP config. */
todo[ntodo++]= dhcpconf;
while (ntodo > 0) {
config_t *cmd, *follow;
if (todo[ntodo-1] == nil) { ntodo--; continue; }
cmd= todo[ntodo-1]->list;
todo[ntodo-1]= todo[ntodo-1]->next;
follow= nil; /* Macro or list to follow next? */
if (strcasecmp(cmd->word, "client") == 0) {
u8_t cfgid[CLID_MAX];
size_t cfglen;
char *name;
int ifno;
u32_t d;
if (between(3, config_length(cmd), 5)
&& config_isatom(cmd->next)
&& (cfglen= ascii2clid(cfgid, cmd->next->word)) != -1
&& config_isatom(cmd->next->next)
&& (((ifno= ifname2if(cmd->next->next->word)) == -1
&& config_length(cmd) <= 4)
|| ((ifno= ifname2if(cmd->next->next->word)) != -1
&& config_length(cmd) >= 4
&& config_isatom(cmd->next->next->next)))
) {
if (cilen == cfglen && memcmp(client, cfgid, cilen) == 0
&& (ifno == -1 || np == nil || ifno == np->n)
) {
config_t *atname= cmd->next->next;
if (ifno != -1) atname= atname->next;
name= atname->word;
if (name2ip(&hip, name, ifip) && (ip == 0 || ip == hip)) {
d= ntohl(hip) ^ ntohl(ifip);
if (d < distance) {
dp->yiaddr= hip;
follow= atname->next;
distance= d;
}
}
}
} else {
fprintf(stderr,
"\"%s\", line %u: Usage: client ID [ip#] host [macro|{params}]\n",
cmd->file, cmd->line);
exit(1);
}
} else
if (strcasecmp(cmd->word, "class") == 0) {
config_t *clist;
int match;
match= 0;
for (clist= cmd->next; clist != nil
&& clist->next != nil
&& config_isatom(clist); clist= clist->next) {
if (calen > 0
&& strncmp(clist->word, (char *) class, calen) == 0
) {
match= 1;
}
}
if (clist == cmd->next || clist->next != nil) {
fprintf(stderr,
"\"%s\", line %u: Usage: class class-name ... macro|{params}\n",
cmd->file, cmd->line);
}
if (match) follow= clist;
} else
if (strcasecmp(cmd->word, "host") == 0) {
if (config_length(cmd) == 3
&& config_isatom(cmd->next)
) {
if (ip != 0) {
if (cidr_aton(cmd->next->word, &hip, &mask)) {
if (((hip ^ ip) & mask) == 0) {
if (!gettag(dp, DHCP_TAG_NETMASK, nil, nil)) {
settag(dp, DHCP_TAG_NETMASK,
&mask, sizeof(mask));
}
dp->yiaddr= ip;
follow= cmd->next->next;
}
} else
if (name2ip(&hip, cmd->next->word, ifip)) {
if (hip == ip) {
dp->yiaddr= ip;
follow= cmd->next->next;
}
}
}
} else {
fprintf(stderr,
"\"%s\", line %u: Usage: host host-spec macro|{params}\n",
cmd->file, cmd->line);
exit(1);
}
} else
if (strcasecmp(cmd->word, "interface") == 0) {
if (between(3, config_length(cmd), 4)
&& config_isatom(cmd->next)
&& config_isatom(cmd->next->next)
) {
network_t *ifnp;
if (np != nil) {
if ((ifnp= if2net(ifname2if(cmd->next->word))) == nil) {
fprintf(stderr,
"\"%s\", line %u: Can't find interface %s\n",
cmd->next->file, cmd->next->line, cmd->next->word);
exit(1);
}
if (!name2ip(&hip, cmd->next->next->word, 0)) {
fprintf(stderr,
"\"%s\", line %u: Can't find IP address of %s\n",
cmd->next->next->file, cmd->next->next->line,
cmd->next->next->word);
exit(1);
}
ifnp->ip= hip;
if (ifnp == np) {
dp->yiaddr= hip;
follow= cmd->next->next->next;
}
}
} else {
fprintf(stderr,
"\"%s\", line %u: Usage: interface ip# host%s\n",
cmd->file, cmd->line, ntodo==1 ? " [macro|{params}]" : "");
exit(1);
}
} else
if (strcasecmp(cmd->word, "macro") == 0) {
if (config_length(cmd) == 2 && config_isatom(cmd->next)) {
follow= cmd->next;
} else
if (ntodo > 1) {
fprintf(stderr, "\"%s\", line %u: Usage: macro macro-name\n",
cmd->file, cmd->line);
exit(1);
}
} else
if (strcasecmp(cmd->word, "tag") == 0) {
if (ntodo > 1) {
fprintf(stderr,
"\"%s\", line %u: A %s can't be defined here\n",
cmd->file, cmd->line, cmd->word);
exit(1);
}
} else
if (strcasecmp(cmd->word, "option") == 0) {
int ifno;
network_t *ifnp;
config_t *opt;
if ((opt= cmd->next) != nil
&& config_isatom(opt)
&& (ifno= ifname2if(opt->word)) != -1
) {
if ((ifnp= if2net(ifno)) == nil) {
fprintf(stderr,
"\"%s\", line %u: Interface %s is not enabled\n",
opt->file, opt->line, opt->word);
exit(1);
}
opt= opt->next;
} else {
ifnp= np;
}
if (between(1, config_length(opt), 2)
&& config_isatom(opt)
&& strcasecmp(opt->word, "server") == 0
&& (opt->next == nil
|| strcasecmp(opt->next->word, "inform") == 0)
) {
if (np != nil) {
ifnp->flags |= NF_SERVING;
if (opt->next != nil) ifnp->flags |= NF_INFORM;
}
} else
if (config_length(opt) == 2
&& config_isatom(opt)
&& strcasecmp(opt->word, "relay") == 0
&& config_isatom(opt->next)
) {
if (np != nil) {
if (!name2ip(&hip, opt->next->word, ifip)) {
fprintf(stderr,
"\"%s\", line %u: Can't find IP address of %s\n",
opt->next->file, opt->next->line,
opt->next->word);
exit(1);
}
ifnp->flags |= NF_RELAYING;
ifnp->server= hip;
}
} else
if (config_length(opt) == 1
&& config_isatom(opt)
&& strcasecmp(opt->word, "possessive") == 0
) {
if (np != nil) ifnp->flags |= NF_POSSESSIVE;
} else
if (config_length(opt) == 2
&& config_isatom(opt)
&& strcasecmp(opt->word, "hostname") == 0
&& config_isatom(opt->next)
) {
if (np != nil) np->hostname= opt->next->word;
} else {
fprintf(stderr, "\"%s\", line %u: Unknown option\n",
cmd->file, cmd->line);
exit(1);
}
} else {
/* Must be an actual data carrying tag. */
configtag(dp, cmd, ifip);
}
if (follow != nil) {
/* A client/class/host entry selects a macro or list that must
* be followed next.
*/
config_t *macro;
if (config_isatom(follow)) { /* Macro name */
config_t *cfg;
for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
macro= cfg->list;
if (strcasecmp(macro->word, "macro") == 0) {
if (config_length(macro) == 3
&& config_isatom(macro->next)
&& config_issub(macro->next->next)
) {
if (strcasecmp(macro->next->word, follow->word) == 0
) {
break;
}
} else {
fprintf(stderr,
"\"%s\", line %u: Usage: macro macro-name {params}\n",
macro->file, macro->line);
}
}
}
follow= cfg == nil ? nil : macro->next->next->list;
} else {
/* Simply a list of more tags and stuff. */
follow= follow->list;
}
if (ntodo == arraysize(todo)) {
fprintf(stderr, "\"%s\", line %u: Nesting is too deep\n",
follow->file, follow->line);
exit(1);
}
todo[ntodo++]= follow;
}
}
/* Check if the IP and netmask are OK for the interface. */
if (!gettag(dp, DHCP_TAG_NETMASK, &pmask, nil)) return 0;
memcpy(&mask, pmask, sizeof(mask));
if (((ip ^ ifip) & mask) != 0) return 0;
/* Fill in the hostname and/or domain. */
if ((hostname= ip2name(ip)) != nil) {
char *domain;
if ((domain= strchr(hostname, '.')) != nil) *domain++ = 0;
if (!gettag(dp, DHCP_TAG_HOSTNAME, nil, nil)) {
settag(dp, DHCP_TAG_HOSTNAME, hostname, strlen(hostname));
}
if (domain != nil && !gettag(dp, DHCP_TAG_DOMAIN, nil, nil)) {
settag(dp, DHCP_TAG_DOMAIN, domain, strlen(domain));
}
}
return 1;
}
static char *dhcpopname(int op)
{
static char *onames[] = { "??\?", "REQUEST", "REPLY" };
return onames[op < arraysize(onames) ? op : 0];
}
char *dhcptypename(int type)
{
static char *tnames[] = {
"??\?", "DISCOVER", "OFFER", "REQUEST", "DECLINE",
"ACK", "NAK", "RELEASE", "INFORM"
};
return tnames[type < arraysize(tnames) ? type : 0];
}
void printdhcp(dhcp_t *dp)
{
/* Print the contents of a DHCP packet, usually for debug purposes. */
tagdef_t *tp;
u8_t *data, *ovld;
size_t i, len;
for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
if (tp->type & TF_STATIC) {
data= B(dp) + tp->tag;
len= tp->gran * tp->max;
if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
if (tp->tag == doff(chaddr)) len= dp->hlen;
/* Don't show uninteresting stuff. */
if (tp->tag == doff(htype) && dp->htype == DHCP_HTYPE_ETH) continue;
if (tp->tag == doff(hlen) && dp->hlen == DHCP_HLEN_ETH) continue;
if ((tp->tag == doff(file) || tp->tag == doff(sname))
&& gettag(dp, DHCP_TAG_OVERLOAD, &ovld, nil)
&& (ovld[0] & (tp->tag == doff(file) ? 1 : 2))
) {
continue;
}
for (i= 0; i < len && data[i] == 0; i++) {}
if (i == len) continue;
} else {
if (!gettag(dp, tp->tag, &data, &len)) continue;
}
if (tagdefined(tp)) {
printf("\t%s =", tp->name);
} else {
printf("\tT%d =", tp->tag);
}
i= 0;
while (i < len) {
switch (tp->type & TF_TYPE) {
case TT_ASCII: {
printf(" \"%.*s\"", (int) len, data);
i= len;
break;}
case TT_BOOLEAN: {
printf(data[i++] == 0 ? " false" : " true");
break;}
case TT_IP: {
ipaddr_t ip;
memcpy(&ip, data+i, sizeof(ip));
printf(" %s", inet_ntoa(ip));
i += sizeof(ip);
break;}
case TT_NUMBER: {
u32_t n= 0;
int g= tp->gran;
do n= (n << 8) | data[i++]; while (--g != 0);
printf(" %lu", (unsigned long) n);
if ((tp->type & TF_STATIC) && tp->tag == doff(op)) {
printf(" (%s)", dhcpopname(n));
}
if (!(tp->type & TF_STATIC) && tp->tag == DHCP_TAG_TYPE) {
printf(" (%s)", dhcptypename(n));
}
break;}
case TT_OCTET: {
if (i == 0) fputc(' ', stdout);
printf("%02X", data[i++]);
break;}
}
}
fputc('\n', stdout);
}
}