#include "dhcpd.h"
#include "dhctoken.h"
void
skip_to_semi(FILE *cfile)
{
int token;
int brace_count = 0;
do {
token = peek_token(NULL, cfile);
if (token == '}') {
if (brace_count) {
token = next_token(NULL, cfile);
if (!--brace_count)
return;
} else
return;
} else if (token == '{') {
brace_count++;
} else if (token == ';' && !brace_count) {
token = next_token(NULL, cfile);
return;
} else if (token == '\n') {
token = next_token(NULL, cfile);
return;
}
token = next_token(NULL, cfile);
} while (token != EOF);
}
int
parse_semi(FILE *cfile)
{
int token;
token = next_token(NULL, cfile);
if (token != ';') {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (0);
}
return (1);
}
char *
parse_string(FILE *cfile)
{
char *val, *s;
int token;
token = next_token(&val, cfile);
if (token != TOK_STRING) {
parse_warn("filename must be a string");
skip_to_semi(cfile);
return (NULL);
}
s = strdup(val);
if (!s)
error("no memory for string %s.", val);
if (!parse_semi(cfile)) {
free(s);
return (NULL);
}
return (s);
}
int
parse_ip_addr(FILE *cfile, struct iaddr *addr)
{
addr->len = 4;
return (parse_numeric_aggregate(cfile, addr->iabuf, addr->len, '.',
10));
}
void
parse_hardware_param(FILE *cfile, struct hardware *hardware)
{
int token;
token = next_token(NULL, cfile);
switch (token) {
case TOK_ETHERNET:
hardware->htype = HTYPE_ETHER;
hardware->hlen = 6;
break;
case TOK_TOKEN_RING:
hardware->htype = HTYPE_IEEE802;
hardware->hlen = 6;
break;
case TOK_FDDI:
hardware->htype = HTYPE_FDDI;
hardware->hlen = 6;
break;
default:
parse_warn("expecting a network hardware type");
skip_to_semi(cfile);
return;
}
if (parse_numeric_aggregate(cfile, hardware->haddr, hardware->hlen,
':', 16) == 0)
return;
token = next_token(NULL, cfile);
if (token != ';') {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
void
parse_lease_time(FILE *cfile, time_t *timep)
{
char *val;
int token;
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("Expecting numeric lease time");
skip_to_semi(cfile);
return;
}
convert_num((unsigned char *)timep, val, 10, 32);
*timep = ntohl(*timep);
parse_semi(cfile);
}
int
parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int max, int separator,
int base)
{
char *val;
int token, count;
if (buf == NULL || max == 0)
error("no space for numeric aggregate");
for (count = 0; count < max; count++, buf++) {
if (count && (peek_token(&val, cfile) == separator))
token = next_token(&val, cfile);
token = next_token(&val, cfile);
if (token == TOK_NUMBER || (base == 16 && token == TOK_NUMBER_OR_NAME))
convert_num(buf, val, base, 8);
else
break;
}
if (count < max) {
parse_warn("numeric aggregate too short.");
return (0);
}
return (1);
}
void
convert_num(unsigned char *buf, char *str, int base, int size)
{
int negative = 0, tval, max;
u_int32_t val = 0;
char *ptr = str;
if (*ptr == '-') {
negative = 1;
ptr++;
}
if (!base) {
if (ptr[0] == '0') {
if (ptr[1] == 'x') {
base = 16;
ptr += 2;
} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
base = 8;
ptr += 1;
} else
base = 10;
} else
base = 10;
}
do {
tval = *ptr++;
if (tval >= 'a')
tval = tval - 'a' + 10;
else if (tval >= 'A')
tval = tval - 'A' + 10;
else if (tval >= '0')
tval -= '0';
else {
warning("Bogus number: %s.", str);
break;
}
if (tval >= base) {
warning("Bogus number: %s: digit %d not in base %d",
str, tval, base);
break;
}
val = val * base + tval;
} while (*ptr);
if (negative)
max = (1 << (size - 1));
else
max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
if (val > max) {
switch (base) {
case 8:
warning("value %s%o exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
case 16:
warning("value %s%x exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
default:
warning("value %s%u exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
}
}
if (negative)
switch (size) {
case 8:
*buf = -(unsigned long)val;
break;
case 16:
putShort(buf, -(unsigned long)val);
break;
case 32:
putLong(buf, -(unsigned long)val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
else
switch (size) {
case 8:
*buf = (u_int8_t)val;
break;
case 16:
putUShort(buf, (u_int16_t)val);
break;
case 32:
putULong(buf, val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
}
time_t
parse_date(FILE *cfile)
{
static int months[11] = { 31, 59, 90, 120, 151, 181,
212, 243, 273, 304, 334 };
int guess, token;
struct tm tm;
char *val;
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric day of week expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_wday = atoi(val);
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric year expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_year = atoi(val);
if (tm.tm_year > 1900)
tm.tm_year -= 1900;
token = next_token(&val, cfile);
if (token != '/') {
parse_warn("expected slash separating year from month.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric month expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_mon = atoi(val) - 1;
token = next_token(&val, cfile);
if (token != '/') {
parse_warn("expected slash separating month from day.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric day of month expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_mday = atoi(val);
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric hour expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_hour = atoi(val);
token = next_token(&val, cfile);
if (token != ':') {
parse_warn("expected colon separating hour from minute.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric minute expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_min = atoi(val);
token = next_token(&val, cfile);
if (token != ':') {
parse_warn("expected colon separating minute from second.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("numeric second expected.");
if (token != ';')
skip_to_semi(cfile);
return (0);
}
tm.tm_sec = atoi(val);
tm.tm_isdst = 0;
tm.tm_yday = 0;
token = next_token(&val, cfile);
if (token != ';') {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (0);
}
guess = ((((((365 * (tm.tm_year - 70) +
(tm.tm_year - 69) / 4 +
(tm.tm_mon
? months[tm.tm_mon - 1] : 0) +
(tm.tm_mon > 1 &&
!((tm.tm_year - 72) & 3)) +
tm.tm_mday - 1) * 24) +
tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec;
return (guess);
}