diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2004-04-14 01:27:50 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2004-04-14 01:27:50 +0000 |
commit | 4155c9e9f0564da7e0791d4e2abf1e86ecd4167f (patch) | |
tree | c89481980be76d9440283bdc32ceb9667191872e /usr.sbin/dhcpd/parse.c | |
parent | f13d7f71d21867a72b76986161f6923c4c43c547 (diff) |
the ones from dhclient do, and they're already KNF'd and annsified...
Diffstat (limited to 'usr.sbin/dhcpd/parse.c')
-rw-r--r-- | usr.sbin/dhcpd/parse.c | 592 |
1 files changed, 296 insertions, 296 deletions
diff --git a/usr.sbin/dhcpd/parse.c b/usr.sbin/dhcpd/parse.c index 05bce05ba72..853c6ae8ff0 100644 --- a/usr.sbin/dhcpd/parse.c +++ b/usr.sbin/dhcpd/parse.c @@ -1,6 +1,6 @@ -/* parse.c +/* $OpenBSD: parse.c,v 1.3 2004/04/14 01:27:49 henning Exp $ */ - Common parser code for dhcpd and dhclient. */ +/* Common parser code for dhcpd and dhclient. */ /* * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. @@ -44,31 +44,31 @@ #include "dhctoken.h" /* Skip to the semicolon ending the current statement. If we encounter - braces, the matching closing brace terminates the statement. If we - encounter a right brace but haven't encountered a left brace, return - leaving the brace in the token buffer for the caller. If we see a - semicolon and haven't seen a left brace, return. This lets us skip - over: - - statement; - statement foo bar { } - statement foo bar { statement { } } - statement} - - ...et cetera. */ - -void skip_to_semi (cfile) - FILE *cfile; + * braces, the matching closing brace terminates the statement. If we + * encounter a right brace but haven't encountered a left brace, return + * leaving the brace in the token buffer for the caller. If we see a + * semicolon and haven't seen a left brace, return. This lets us skip + * over: + * + * statement; + * statement foo bar { } + * statement foo bar { statement { } } + * statement} + * + * ...et cetera. + */ +void +skip_to_semi(FILE *cfile) { int token; char *val; int brace_count = 0; do { - token = peek_token (&val, cfile); + token = peek_token(&val, cfile); if (token == RBRACE) { if (brace_count) { - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (!--brace_count) return; } else @@ -76,306 +76,307 @@ void skip_to_semi (cfile) } else if (token == LBRACE) { brace_count++; } else if (token == SEMI && !brace_count) { - token = next_token (&val, cfile); + token = next_token(&val, cfile); return; } else if (token == '\n') { - /* EOL only happens when parsing /etc/resolv.conf, - and we treat it like a semicolon because the - resolv.conf file is line-oriented. */ - token = next_token (&val, cfile); + /* + * EOL only happens when parsing + * /etc/resolv.conf, and we treat it like a + * semicolon because the resolv.conf file is + * line-oriented. + */ + token = next_token(&val, cfile); return; } - token = next_token (&val, cfile); + token = next_token(&val, cfile); } while (token != EOF); } -int parse_semi (cfile) - FILE *cfile; +int +parse_semi(FILE *cfile) { int token; char *val; - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != SEMI) { - parse_warn ("semicolon expected."); - skip_to_semi (cfile); - return 0; + parse_warn("semicolon expected."); + skip_to_semi(cfile); + return (0); } - return 1; + return (1); } -/* string-parameter :== STRING SEMI */ - -char *parse_string (cfile) - FILE *cfile; +/* + * string-parameter :== STRING SEMI + */ +char * +parse_string(FILE *cfile) { char *val; int token; char *s; - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != STRING) { - parse_warn ("filename must be a string"); - skip_to_semi (cfile); - return (char *)0; + parse_warn("filename must be a string"); + skip_to_semi(cfile); + return (NULL); } - if (!parse_semi (cfile)) - return (char *)0; - - s = (char *)malloc (strlen (val) + 1); + s = malloc(strlen(val) + 1); if (!s) - error ("no memory for string %s.", val); - strlcpy (s, val, strlen(val) + 1); + error("no memory for string %s.", val); + strlcpy(s, val, strlen(val) + 1); - return s; + if (!parse_semi(cfile)) + return (NULL); + return (s); } -/* hostname :== identifier | hostname DOT identifier */ - -char *parse_host_name (cfile) - FILE *cfile; +/* + * hostname :== identifier | hostname DOT identifier + */ +char * +parse_host_name(FILE *cfile) { char *val; int token; int len = 0; char *s; char *t; - pair c = (pair)0; - + pair c = NULL; + /* Read a dotted hostname... */ do { /* Read a token, which should be an identifier. */ - token = next_token (&val, cfile); - if (!is_identifier (token) && token != NUMBER) { - parse_warn ("expecting an identifier in hostname"); - skip_to_semi (cfile); - return (char *)0; + token = next_token(&val, cfile); + if (!is_identifier(token) && token != NUMBER) { + parse_warn("expecting an identifier in hostname"); + skip_to_semi(cfile); + return (NULL); } /* Store this identifier... */ - if (!(s = (char *)malloc (strlen (val) + 1))) - error ("can't allocate temp space for hostname."); - strlcpy (s, val, strlen(val) + 1); - c = cons ((caddr_t)s, c); - len += strlen (s) + 1; - /* Look for a dot; if it's there, keep going, otherwise - we're done. */ - token = peek_token (&val, cfile); + if (!(s = malloc(strlen(val) + 1))) + error("can't allocate temp space for hostname."); + strlcpy(s, val, strlen(val) + 1); + c = cons((caddr_t)s, c); + len += strlen(s) + 1; + /* + * Look for a dot; if it's there, keep going, otherwise + * we're done. + */ + token = peek_token(&val, cfile); if (token == DOT) - token = next_token (&val, cfile); + token = next_token(&val, cfile); } while (token == DOT); /* Assemble the hostname together into a string. */ - if (!(s = (char *)malloc (len))) - error ("can't allocate space for hostname."); + if (!(s = malloc(len))) + error("can't allocate space for hostname."); t = s + len; - *--t = 0; + *--t = '\0'; while (c) { - pair cdr = c -> cdr; - int l = strlen ((char *)(c -> car)); + pair cdr = c->cdr; + int l = strlen((char *)c->car); t -= l; - memcpy (t, (char *)(c -> car), l); + memcpy(t, (char *)c->car, l); /* Free up temp space. */ - free (c -> car); - free (c); + free(c->car); + free(c); c = cdr; if (t != s) *--t = '.'; } - return s; + return (s); } -int parse_ip_addr (cfile, addr) - FILE *cfile; - struct iaddr *addr; +int +parse_ip_addr(FILE *cfile, struct iaddr *addr) { - addr -> len = 4; - if (parse_numeric_aggregate (cfile, addr -> iabuf, - &addr -> len, DOT, 10, 8)) - return 1; - return 0; -} - -/* hardware-parameter :== HARDWARE ETHERNET csns SEMI - csns :== NUMBER | csns COLON NUMBER */ - -void parse_hardware_param (cfile, hardware) - FILE *cfile; - struct hardware *hardware; + addr->len = 4; + if (parse_numeric_aggregate(cfile, addr->iabuf, + &addr->len, DOT, 10, 8)) + return (1); + return (0); +} + +/* + * hardware-parameter :== HARDWARE ETHERNET csns SEMI + * csns :== NUMBER | csns COLON NUMBER + */ +void +parse_hardware_param(FILE *cfile, struct hardware *hardware) { char *val; int token; int hlen; unsigned char *t; - token = next_token (&val, cfile); + token = next_token(&val, cfile); switch (token) { - case ETHERNET: - hardware -> htype = HTYPE_ETHER; + case ETHERNET: + hardware->htype = HTYPE_ETHER; break; - case TOKEN_RING: - hardware -> htype = HTYPE_IEEE802; + case TOKEN_RING: + hardware->htype = HTYPE_IEEE802; break; - case FDDI: - hardware -> htype = HTYPE_FDDI; + case FDDI: + hardware->htype = HTYPE_FDDI; break; - default: - parse_warn ("expecting a network hardware type"); - skip_to_semi (cfile); + default: + parse_warn("expecting a network hardware type"); + skip_to_semi(cfile); return; } - /* Parse the hardware address information. Technically, - it would make a lot of sense to restrict the length of the - data we'll accept here to the length of a particular hardware - address type. Unfortunately, there are some broken clients - out there that put bogus data in the chaddr buffer, and we accept - that data in the lease file rather than simply failing on such - clients. Yuck. */ + /* + * Parse the hardware address information. Technically, it + * would make a lot of sense to restrict the length of the data + * we'll accept here to the length of a particular hardware + * address type. Unfortunately, there are some broken clients + * out there that put bogus data in the chaddr buffer, and we + * accept that data in the lease file rather than simply failing + * on such clients. Yuck. + */ hlen = 0; - t = parse_numeric_aggregate (cfile, (unsigned char *)0, &hlen, - COLON, 16, 8); + t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8); if (!t) return; - if (hlen > sizeof hardware -> haddr) { - free (t); - parse_warn ("hardware address too long"); + if (hlen > sizeof(hardware->haddr)) { + free(t); + parse_warn("hardware address too long"); } else { - hardware -> hlen = hlen; - memcpy ((unsigned char *)&hardware -> haddr [0], - t, hardware -> hlen); - if (hlen < sizeof hardware -> haddr) - memset (&hardware -> haddr [hlen], 0, - (sizeof hardware -> haddr) - hlen); - free (t); + hardware->hlen = hlen; + memcpy((unsigned char *)&hardware->haddr[0], + t, hardware->hlen); + if (hlen < sizeof(hardware->haddr)) + memset(&hardware->haddr[hlen], 0, + sizeof(hardware->haddr) - hlen); + free(t); } - - token = next_token (&val, cfile); + + token = next_token(&val, cfile); if (token != SEMI) { - parse_warn ("expecting semicolon."); - skip_to_semi (cfile); + parse_warn("expecting semicolon."); + skip_to_semi(cfile); } } -/* lease-time :== NUMBER SEMI */ - -void parse_lease_time (cfile, timep) - FILE *cfile; - time_t *timep; +/* + * lease-time :== NUMBER SEMI + */ +void +parse_lease_time(FILE *cfile, time_t *timep) { char *val; int token; - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("Expecting numeric lease time"); - skip_to_semi (cfile); + parse_warn("Expecting numeric lease time"); + skip_to_semi(cfile); return; } - convert_num ((unsigned char *)timep, val, 10, 32); + convert_num((unsigned char *)timep, val, 10, 32); /* Unswap the number - convert_num returns stuff in NBO. */ - *timep = ntohl (*timep); /* XXX */ + *timep = ntohl(*timep); /* XXX */ - parse_semi (cfile); + parse_semi(cfile); } -/* No BNF for numeric aggregates - that's defined by the caller. What - this function does is to parse a sequence of numbers separated by - the token specified in separator. If max is zero, any number of - numbers will be parsed; otherwise, exactly max numbers are - expected. Base and size tell us how to internalize the numbers - once they've been tokenized. */ - -unsigned char *parse_numeric_aggregate (cfile, buf, - max, separator, base, size) - FILE *cfile; - unsigned char *buf; - int *max; - int separator; - int base; - int size; +/* + * No BNF for numeric aggregates - that's defined by the caller. What + * this function does is to parse a sequence of numbers separated by the + * token specified in separator. If max is zero, any number of numbers + * will be parsed; otherwise, exactly max numbers are expected. Base + * and size tell us how to internalize the numbers once they've been + * tokenized. + */ +unsigned char * +parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, + int separator, int base, int size) { char *val; int token; unsigned char *bufp = buf, *s = NULL; char *t; int count = 0; - pair c = (pair)0; + pair c = NULL; if (!bufp && *max) { - bufp = (unsigned char *)malloc (*max * size / 8); + bufp = malloc(*max * size / 8); if (!bufp) - error ("can't allocate space for numeric aggregate"); + error("can't allocate space for numeric aggregate"); } else s = bufp; do { if (count) { - token = peek_token (&val, cfile); + token = peek_token(&val, cfile); if (token != separator) { if (!*max) break; if (token != RBRACE && token != LBRACE) - token = next_token (&val, cfile); - parse_warn ("too few numbers."); + token = next_token(&val, cfile); + parse_warn("too few numbers."); if (token != SEMI) - skip_to_semi (cfile); - return (unsigned char *)0; + skip_to_semi(cfile); + return (NULL); } - token = next_token (&val, cfile); + token = next_token(&val, cfile); } - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token == EOF) { - parse_warn ("unexpected end of file"); + parse_warn("unexpected end of file"); break; } /* Allow NUMBER_OR_NAME if base is 16. */ if (token != NUMBER && (base != 16 || token != NUMBER_OR_NAME)) { - parse_warn ("expecting numeric value."); - skip_to_semi (cfile); - return (unsigned char *)0; + parse_warn("expecting numeric value."); + skip_to_semi(cfile); + return (NULL); } - /* If we can, convert the number now; otherwise, build - a linked list of all the numbers. */ + /* + * If we can, convert the number now; otherwise, build a + * linked list of all the numbers. + */ if (s) { - convert_num (s, val, base, size); + convert_num(s, val, base, size); s += size / 8; } else { - t = (char *)malloc (strlen (val) + 1); + t = malloc(strlen(val) + 1); if (!t) - error ("no temp space for number."); - strlcpy (t, val, strlen(val)+1); - c = cons (t, c); + error("no temp space for number."); + strlcpy(t, val, strlen(val) + 1); + c = cons(t, c); } } while (++count != *max); /* If we had to cons up a list, convert it now. */ if (c) { - bufp = (unsigned char *)malloc (count * size / 8); + bufp = malloc(count * size / 8); if (!bufp) - error ("can't allocate space for numeric aggregate."); + error("can't allocate space for numeric aggregate."); s = bufp + count - size / 8; *max = count; } while (c) { - pair cdr = c -> cdr; - convert_num (s, (char *)(c -> car), base, size); + pair cdr = c->cdr; + convert_num(s, (char *)c->car, base, size); s -= size / 8; /* Free up temp space. */ - free (c -> car); - free (c); + free(c->car); + free(c); c = cdr; } - return bufp; + return (bufp); } -void convert_num (buf, str, base, size) - unsigned char *buf; - char *str; - int base; - int size; +void +convert_num(unsigned char *buf, char *str, int base, int size) { char *ptr = str; int negative = 0; @@ -385,24 +386,22 @@ void convert_num (buf, str, base, size) if (*ptr == '-') { negative = 1; - ++ptr; + ptr++; } /* If base wasn't specified, figure it out from the data. */ if (!base) { - if (ptr [0] == '0') { - if (ptr [1] == 'x') { + if (ptr[0] == '0') { + if (ptr[1] == 'x') { base = 16; ptr += 2; - } else if (isascii (ptr [1]) && isdigit (ptr [1])) { + } else if (isascii(ptr[1]) && isdigit(ptr[1])) { base = 8; ptr += 1; - } else { + } else base = 10; - } - } else { + } else base = 10; - } } do { @@ -415,12 +414,12 @@ void convert_num (buf, str, base, size) else if (tval >= '0') tval -= '0'; else { - warn ("Bogus number: %s.", str); + warn("Bogus number: %s.", str); break; } if (tval >= base) { - warn ("Bogus number: %s: digit %d not in base %d", - str, tval, base); + warn("Bogus number: %s: digit %d not in base %d", + str, tval, base); break; } val = val * base + tval; @@ -432,211 +431,212 @@ void convert_num (buf, str, base, size) max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); if (val > max) { switch (base) { - case 8: - warn ("value %s%o exceeds max (%d) for precision.", - negative ? "-" : "", val, max); + case 8: + warn("value %s%o exceeds max (%d) for precision.", + negative ? "-" : "", val, max); break; - case 16: - warn ("value %s%x exceeds max (%d) for precision.", - negative ? "-" : "", val, max); + case 16: + warn("value %s%x exceeds max (%d) for precision.", + negative ? "-" : "", val, max); break; - default: - warn ("value %s%u exceeds max (%d) for precision.", - negative ? "-" : "", val, max); + default: + warn("value %s%u exceeds max (%d) for precision.", + negative ? "-" : "", val, max); break; } } - if (negative) { + if (negative) switch (size) { - case 8: + case 8: *buf = -(unsigned long)val; break; - case 16: - putShort (buf, -(unsigned long)val); + case 16: + putShort(buf, -(unsigned long)val); break; - case 32: - putLong (buf, -(unsigned long)val); + case 32: + putLong(buf, -(unsigned long)val); break; - default: - warn ("Unexpected integer size: %d", size); + default: + warn("Unexpected integer size: %d", size); break; } - } else { + else switch (size) { - case 8: + case 8: *buf = (u_int8_t)val; break; - case 16: - putUShort (buf, (u_int16_t)val); + case 16: + putUShort(buf, (u_int16_t)val); break; - case 32: - putULong (buf, val); + case 32: + putULong(buf, val); break; - default: - warn ("Unexpected integer size: %d", size); + default: + warn("Unexpected integer size: %d", size); break; } - } } -/* date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER - NUMBER COLON NUMBER COLON NUMBER SEMI - - Dates are always in GMT; first number is day of week; next is - year/month/day; next is hours:minutes:seconds on a 24-hour - clock. */ - -time_t parse_date (cfile) - FILE *cfile; +/* + * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER + * NUMBER COLON NUMBER COLON NUMBER SEMI + * + * Dates are always in GMT; first number is day of week; next is + * year/month/day; next is hours:minutes:seconds on a 24-hour + * clock. + */ +time_t +parse_date(FILE *cfile) { struct tm tm; int guess; char *val; int token; - static int months [11] = { 31, 59, 90, 120, 151, 181, - 212, 243, 273, 304, 334 }; + static int months[11] = { 31, 59, 90, 120, 151, 181, + 212, 243, 273, 304, 334 }; /* Day of week... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric day of week expected."); + parse_warn("numeric day of week expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_wday = atoi (val); + tm.tm_wday = atoi(val); /* Year... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric year expected."); + parse_warn("numeric year expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_year = atoi (val); + tm.tm_year = atoi(val); if (tm.tm_year > 1900) tm.tm_year -= 1900; /* Slash separating year from month... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != SLASH) { - parse_warn ("expected slash separating year from month."); + parse_warn("expected slash separating year from month."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } /* Month... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric month expected."); + parse_warn("numeric month expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_mon = atoi (val) - 1; + tm.tm_mon = atoi(val) - 1; /* Slash separating month from day... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != SLASH) { - parse_warn ("expected slash separating month from day."); + parse_warn("expected slash separating month from day."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } /* Month... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric day of month expected."); + parse_warn("numeric day of month expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_mday = atoi (val); + tm.tm_mday = atoi(val); /* Hour... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric hour expected."); + parse_warn("numeric hour expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_hour = atoi (val); + tm.tm_hour = atoi(val); /* Colon separating hour from minute... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != COLON) { - parse_warn ("expected colon separating hour from minute."); + parse_warn("expected colon separating hour from minute."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } /* Minute... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric minute expected."); + parse_warn("numeric minute expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_min = atoi (val); + tm.tm_min = atoi(val); /* Colon separating minute from second... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != COLON) { - parse_warn ("expected colon separating hour from minute."); + parse_warn("expected colon separating hour from minute."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } /* Minute... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != NUMBER) { - parse_warn ("numeric minute expected."); + parse_warn("numeric minute expected."); if (token != SEMI) - skip_to_semi (cfile); + skip_to_semi(cfile); return (NULL); } - tm.tm_sec = atoi (val); + tm.tm_sec = atoi(val); tm.tm_isdst = 0; - /* XXX */ /* We assume that mktime does not use tm_yday. */ + /* XXX: We assume that mktime does not use tm_yday. */ tm.tm_yday = 0; /* Make sure the date ends in a semicolon... */ - token = next_token (&val, cfile); + token = next_token(&val, cfile); if (token != SEMI) { - parse_warn ("semicolon expected."); - skip_to_semi (cfile); - return 0; + parse_warn("semicolon expected."); + skip_to_semi(cfile); + return (NULL); } /* Guess the time value... */ guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */ - (tm.tm_year - 69) / 4 + /* Leap days since '70 */ - (tm.tm_mon /* Days in months this year */ - ? months [tm.tm_mon - 1] - : 0) + - (tm.tm_mon > 1 && /* Leap day this year */ - !((tm.tm_year - 72) & 3)) + - tm.tm_mday - 1) * 24) + /* Day of month */ + (tm.tm_year - 69) / 4 + /* Leap days since '70 */ + (tm.tm_mon /* Days in months this year */ + ? months[tm.tm_mon - 1] + : 0) + + (tm.tm_mon > 1 && /* Leap day this year */ + !((tm.tm_year - 72) & 3)) + + tm.tm_mday - 1) * 24) + /* Day of month */ tm.tm_hour) * 60) + - tm.tm_min) * 60) + tm.tm_sec; - - /* This guess could be wrong because of leap seconds or other - weirdness we don't know about that the system does. For - now, we're just going to accept the guess, but at some point - it might be nice to do a successive approximation here to - get an exact value. Even if the error is small, if the - server is restarted frequently (and thus the lease database - is reread), the error could accumulate into something - significant. */ - - return guess; + tm.tm_min) * 60) + tm.tm_sec; + + /* + * This guess could be wrong because of leap seconds or other + * weirdness we don't know about that the system does. For + * now, we're just going to accept the guess, but at some point + * it might be nice to do a successive approximation here to get + * an exact value. Even if the error is small, if the server + * is restarted frequently (and thus the lease database is + * reread), the error could accumulate into something + * significant. + */ + return (guess); } |