8000 minix/minix/commands/dhcpd/tags.c at master · svagner/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
924 lines (838 loc) · 24.4 KB

tags.c

File metadata and controls

924 lines (838 loc) · 24.4 KB
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);
}
}
0