diff options
author | 2004-02-06 11:33:22 +0000 | |
---|---|---|
committer | 2004-02-06 11:33:22 +0000 | |
commit | c714dadc707e5a69f69cfac591a0fdc1c39743ce (patch) | |
tree | 65cc177234f65522d3cf19840f062ec9d309bc2c | |
parent | typo from Tamas Tevesz, via nick@; (diff) | |
download | wireguard-openbsd-c714dadc707e5a69f69cfac591a0fdc1c39743ce.tar.xz wireguard-openbsd-c714dadc707e5a69f69cfac591a0fdc1c39743ce.zip |
more Knall & Rauch -> ANSI C, KNF and stuff that makes this a bit more
readable. no effect on the binary.
From: Emil Mikulic <emikulic@dmr.ath.cx>
-rw-r--r-- | sbin/dhclient/options.c | 667 | ||||
-rw-r--r-- | sbin/dhclient/packet.c | 345 | ||||
-rw-r--r-- | sbin/dhclient/parse.c | 593 | ||||
-rw-r--r-- | sbin/dhclient/print.c | 188 | ||||
-rw-r--r-- | sbin/dhclient/socket.c | 120 |
5 files changed, 942 insertions, 971 deletions
diff --git a/sbin/dhclient/options.c b/sbin/dhclient/options.c index 0227a41b9ac..66fd0fad7d0 100644 --- a/sbin/dhclient/options.c +++ b/sbin/dhclient/options.c @@ -1,4 +1,4 @@ -/* $OpenBSD: options.c,v 1.2 2004/02/04 12:16:56 henning Exp $ */ +/* $OpenBSD: options.c,v 1.3 2004/02/06 11:33:22 henning Exp $ */ /* DHCP options parsing and reassembly. */ @@ -40,56 +40,60 @@ * Enterprises, see ``http://www.vix.com''. */ +#include <ctype.h> + #define DHCP_OPTION_DATA #include "dhcpd.h" -#include <ctype.h> int bad_options = 0; int bad_options_max = 5; -/* Parse all available options out of the specified packet. */ - -void parse_options (packet) - struct packet *packet; +/* + * Parse all available options out of the specified packet. + */ +void +parse_options(struct packet *packet) { /* Initially, zero all option pointers. */ - memset (packet -> options, 0, sizeof (packet -> options)); + memset(packet->options, 0, sizeof(packet->options)); /* If we don't see the magic cookie, there's nothing to parse. */ - if (memcmp (packet -> raw -> options, DHCP_OPTIONS_COOKIE, 4)) { - packet -> options_valid = 0; + if (memcmp(packet->raw->options, DHCP_OPTIONS_COOKIE, 4)) { + packet->options_valid = 0; return; } - /* Go through the options field, up to the end of the packet - or the End field. */ - parse_option_buffer (packet, &packet -> raw -> options [4], - packet -> packet_length - DHCP_FIXED_NON_UDP - 4); - /* If we parsed a DHCP Option Overload option, parse more - options out of the buffer(s) containing them. */ - if (packet -> options_valid - && packet -> options [DHO_DHCP_OPTION_OVERLOAD].data) { - if (packet -> options [DHO_DHCP_OPTION_OVERLOAD].data [0] & 1) - parse_option_buffer (packet, - (unsigned char *) - packet -> raw -> file, - sizeof packet -> raw -> file); - if (packet -> options [DHO_DHCP_OPTION_OVERLOAD].data [0] & 2) - parse_option_buffer (packet, - (unsigned char *) - packet -> raw -> sname, - sizeof packet -> raw -> sname); + /* + * Go through the options field, up to the end of the packet or + * the End field. + */ + parse_option_buffer(packet, &packet->raw->options[4], + packet->packet_length - DHCP_FIXED_NON_UDP - 4); + /* + * If we parsed a DHCP Option Overload option, parse more + * options out of the buffer(s) containing them. + */ + if (packet->options_valid && + packet->options[DHO_DHCP_OPTION_OVERLOAD].data) { + if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1) + parse_option_buffer(packet, + (unsigned char *)packet->raw->file, + sizeof(packet->raw->file)); + if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2) + parse_option_buffer(packet, + (unsigned char *)packet->raw->sname, + sizeof(packet->raw->sname)); } } -/* Parse options out of the specified buffer, storing addresses of option - values in packet -> options and setting packet -> options_valid if no - errors are encountered. */ - -void parse_option_buffer (packet, buffer, length) - struct packet *packet; - unsigned char *buffer; - int length; +/* + * Parse options out of the specified buffer, storing addresses of + * option values in packet->options and setting packet->options_valid if + * no errors are encountered. + */ +void +parse_option_buffer(struct packet *packet, + unsigned char *buffer, int length) { unsigned char *s, *t; unsigned char *end = buffer + length; @@ -97,7 +101,7 @@ void parse_option_buffer (packet, buffer, length) int code; for (s = buffer; *s != DHO_END && s < end; ) { - code = s [0]; + code = s[0]; /* Pad options don't have a length - just skip them. */ if (code == DHO_PAD) { @@ -108,116 +112,118 @@ void parse_option_buffer (packet, buffer, length) len = 65536; goto bogus; } - - /* All other fields (except end, see above) have a - one-byte length. */ - len = s [1]; - - /* If the length is outrageous, silently skip the - * rest, and mark the packet bad. Unfortuntely - * some crappy dhcp servers always seem to give - * us garbage on the end of a packet. so rather than - * keep refusing, give up and try to take one after - * seeing a few without anything good. + + /* + * All other fields (except end, see above) have a + * one-byte length. + */ + len = s[1]; + + /* + * If the length is outrageous, silently skip the rest, + * and mark the packet bad. Unfortuntely some crappy + * dhcp servers always seem to give us garbage on the + * end of a packet. so rather than keep refusing, give + * up and try to take one after seeing a few without + * anything good. */ if (s + len + 2 > end) { bogus: bad_options++; - warn ("option %s (%d) %s.", - dhcp_options [code].name, len, - "larger than buffer"); + warn("option %s (%d) %s.", + dhcp_options[code].name, len, + "larger than buffer"); if (bad_options == bad_options_max) { - packet -> options_valid = 1; + packet->options_valid = 1; bad_options = 0; - warn ("Many bogus options seen in offers."); - warn ("Taking this offer in spite of bogus"); - warn ("options - hope for the best!"); + warn("Many bogus options seen in offers."); + warn("Taking this offer in spite of bogus"); + warn("options - hope for the best!"); } else { - warn ("rejecting bogus offer."); - packet -> options_valid = 0; + warn("rejecting bogus offer."); + packet->options_valid = 0; } return; } - /* If we haven't seen this option before, just make - space for it and copy it there. */ - if (!packet -> options [code].data) { - if (!(t = ((unsigned char *) - dmalloc (len + 1, "parse_option_buffer")))) - error ("Can't allocate storage for option %s.", - dhcp_options [code].name); - /* Copy and NUL-terminate the option (in case it's an - ASCII string. */ - memcpy (t, &s [2], len); - t [len] = 0; - packet -> options [code].len = len; - packet -> options [code].data = t; + /* + * If we haven't seen this option before, just make + * space for it and copy it there. + */ + if (!packet->options[code].data) { + if (!(t = ((unsigned char *)dmalloc(len + 1, + "parse_option_buffer")))) + error("Can't allocate storage for option %s.", + dhcp_options[code].name); + /* + * Copy and NUL-terminate the option (in case + * it's an ASCII string. + */ + memcpy(t, &s[2], len); + t[len] = 0; + packet->options[code].len = len; + packet->options[code].data = t; } else { - /* If it's a repeat, concatenate it to whatever - we last saw. This is really only required - for clients, but what the heck... */ - t = ((unsigned char *) - dmalloc (len + packet -> options [code].len + 1, - "parse_option_buffer")); + /* + * If it's a repeat, concatenate it to whatever + * we last saw. This is really only required + * for clients, but what the heck... + */ + t = ((unsigned char *)dmalloc(len + + packet->options[code].len + 1, + "parse_option_buffer")); if (!t) - error ("Can't expand storage for option %s.", - dhcp_options [code].name); - memcpy (t, packet -> options [code].data, - packet -> options [code].len); - memcpy (t + packet -> options [code].len, - &s [2], len); - packet -> options [code].len += len; - t [packet -> options [code].len] = 0; - dfree (packet -> options [code].data, - "parse_option_buffer"); - packet -> options [code].data = t; + error("Can't expand storage for option %s.", + dhcp_options[code].name); + memcpy(t, packet->options[code].data, + packet->options[code].len); + memcpy(t + packet->options [code].len, + &s[2], len); + packet->options[code].len += len; + t[packet->options[code].len] = 0; + dfree(packet->options[code].data, + "parse_option_buffer"); + packet->options[code].data = t; } s += len + 2; } - packet -> options_valid = 1; + packet->options_valid = 1; } -/* cons options into a big buffer, and then split them out into the - three separate buffers if needed. This allows us to cons up a set - of vendor options using the same routine. */ - -int cons_options (inpacket, outpacket, mms, - options, overload, terminate, bootpp, prl, prl_len) - struct packet *inpacket; - struct dhcp_packet *outpacket; - int mms; - struct tree_cache **options; - int overload; /* Overload flags that may be set. */ - int terminate; - int bootpp; - u_int8_t *prl; - int prl_len; +/* + * cons options into a big buffer, and then split them out into the + * three separate buffers if needed. This allows us to cons up a set of + * vendor options using the same routine. + */ +int +cons_options(struct packet *inpacket, struct dhcp_packet *outpacket, + int mms, struct tree_cache **options, + int overload, /* Overload flags that may be set. */ + int terminate, int bootpp, u_int8_t *prl, int prl_len) { - unsigned char priority_list [300]; + unsigned char priority_list[300]; int priority_len; - unsigned char buffer [4096]; /* Really big buffer... */ + unsigned char buffer[4096]; /* Really big buffer... */ int main_buffer_size; int mainbufix, bufix; int option_size; int length; - /* If the client has provided a maximum DHCP message size, - use that; otherwise, if it's BOOTP, only 64 bytes; otherwise - use up to the minimum IP MTU size (576 bytes). */ - /* XXX if a BOOTP client specifies a max message size, we will - honor it. */ + /* + * If the client has provided a maximum DHCP message size, use + * that; otherwise, if it's BOOTP, only 64 bytes; otherwise use + * up to the minimum IP MTU size (576 bytes). + * + * XXX if a BOOTP client specifies a max message size, we will + * honor it. + */ if (!mms && inpacket && - inpacket -> options [DHO_DHCP_MAX_MESSAGE_SIZE].data && - (inpacket -> options [DHO_DHCP_MAX_MESSAGE_SIZE].len >= - sizeof (u_int16_t))) - mms = getUShort (inpacket -> options - [DHO_DHCP_MAX_MESSAGE_SIZE].data); - - /* If the client has provided a maximum DHCP message size, - use that; otherwise, if it's BOOTP, only 64 bytes; otherwise - use up to the minimum IP MTU size (576 bytes). */ - /* XXX if a BOOTP client specifies a max message size, we will - honor it. */ + inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data && + (inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].len >= + sizeof (u_int16_t))) + mms = getUShort( + inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data); + if (mms) main_buffer_size = mms - DHCP_FIXED_LEN; else if (bootpp) @@ -225,130 +231,121 @@ int cons_options (inpacket, outpacket, mms, else main_buffer_size = 576 - DHCP_FIXED_LEN; - if (main_buffer_size > sizeof buffer) - main_buffer_size = sizeof buffer; + if (main_buffer_size > sizeof(buffer)) + main_buffer_size = sizeof(buffer); /* Preload the option priority list with mandatory options. */ priority_len = 0; - priority_list [priority_len++] = DHO_DHCP_MESSAGE_TYPE; - priority_list [priority_len++] = DHO_DHCP_SERVER_IDENTIFIER; - priority_list [priority_len++] = DHO_DHCP_LEASE_TIME; - priority_list [priority_len++] = DHO_DHCP_MESSAGE; - - /* If the client has provided a list of options that it wishes - returned, use it to prioritize. Otherwise, prioritize - based on the default priority list. */ - + priority_list[priority_len++] = DHO_DHCP_MESSAGE_TYPE; + priority_list[priority_len++] = DHO_DHCP_SERVER_IDENTIFIER; + priority_list[priority_len++] = DHO_DHCP_LEASE_TIME; + priority_list[priority_len++] = DHO_DHCP_MESSAGE; + + /* + * If the client has provided a list of options that it wishes + * returned, use it to prioritize. Otherwise, prioritize based + * on the default priority list. + */ if (inpacket && - inpacket -> options [DHO_DHCP_PARAMETER_REQUEST_LIST].data) { - int prlen = (inpacket -> - options [DHO_DHCP_PARAMETER_REQUEST_LIST].len); - if (prlen + priority_len > sizeof priority_list) - prlen = (sizeof priority_list) - priority_len; - - memcpy (&priority_list [priority_len], - (inpacket -> options - [DHO_DHCP_PARAMETER_REQUEST_LIST].data), prlen); + inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data) { + int prlen = + inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].len; + if (prlen + priority_len > sizeof(priority_list)) + prlen = sizeof(priority_list) - priority_len; + + memcpy(&priority_list[priority_len], + inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data, + prlen); priority_len += prlen; prl = priority_list; } else if (prl) { - if (prl_len + priority_len > sizeof priority_list) - prl_len = (sizeof priority_list) - priority_len; - - memcpy (&priority_list [priority_len], prl, prl_len); + if (prl_len + priority_len > sizeof(priority_list)) + prl_len = sizeof(priority_list) - priority_len; + + memcpy(&priority_list[priority_len], prl, prl_len); priority_len += prl_len; prl = priority_list; } else { - memcpy (&priority_list [priority_len], - dhcp_option_default_priority_list, - sizeof_dhcp_option_default_priority_list); + memcpy(&priority_list[priority_len], + dhcp_option_default_priority_list, + sizeof_dhcp_option_default_priority_list); priority_len += sizeof_dhcp_option_default_priority_list; } /* Copy the options into the big buffer... */ - option_size = store_options (buffer, - (main_buffer_size - 7 + - ((overload & 1) ? DHCP_FILE_LEN : 0) + - ((overload & 2) ? DHCP_SNAME_LEN : 0)), - options, priority_list, priority_len, - main_buffer_size, - (main_buffer_size + - ((overload & 1) ? DHCP_FILE_LEN : 0)), - terminate); + option_size = store_options( + buffer, + (main_buffer_size - 7 + ((overload & 1) ? DHCP_FILE_LEN : 0) + + ((overload & 2) ? DHCP_SNAME_LEN : 0)), + options, priority_list, priority_len, main_buffer_size, + (main_buffer_size + ((overload & 1) ? DHCP_FILE_LEN : 0)), + terminate); /* Put the cookie up front... */ - memcpy (outpacket -> options, DHCP_OPTIONS_COOKIE, 4); + memcpy(outpacket->options, DHCP_OPTIONS_COOKIE, 4); mainbufix = 4; - /* If we're going to have to overload, store the overload - option at the beginning. If we can, though, just store the - whole thing in the packet's option buffer and leave it at - that. */ + /* + * If we're going to have to overload, store the overload option + * at the beginning. If we can, though, just store the whole + * thing in the packet's option buffer and leave it at that. + */ if (option_size <= main_buffer_size - mainbufix) { - memcpy (&outpacket -> options [mainbufix], - buffer, option_size); + memcpy(&outpacket->options[mainbufix], + buffer, option_size); mainbufix += option_size; if (mainbufix < main_buffer_size) - outpacket -> options [mainbufix++] - = DHO_END; + outpacket->options[mainbufix++] = DHO_END; length = DHCP_FIXED_NON_UDP + mainbufix; } else { - outpacket -> options [mainbufix++] = - DHO_DHCP_OPTION_OVERLOAD; - outpacket -> options [mainbufix++] = 1; - if (option_size > main_buffer_size - mainbufix + DHCP_FILE_LEN) - outpacket -> options [mainbufix++] = 3; + outpacket->options[mainbufix++] = DHO_DHCP_OPTION_OVERLOAD; + outpacket->options[mainbufix++] = 1; + if (option_size > + main_buffer_size - mainbufix + DHCP_FILE_LEN) + outpacket->options[mainbufix++] = 3; else - outpacket -> options [mainbufix++] = 1; + outpacket->options[mainbufix++] = 1; - memcpy (&outpacket -> options [mainbufix], - buffer, main_buffer_size - mainbufix); + memcpy(&outpacket->options[mainbufix], + buffer, main_buffer_size - mainbufix); bufix = main_buffer_size - mainbufix; length = DHCP_FIXED_NON_UDP + mainbufix; if (overload & 1) { if (option_size - bufix <= DHCP_FILE_LEN) { - memcpy (outpacket -> file, - &buffer [bufix], option_size - bufix); + memcpy(outpacket->file, + &buffer[bufix], option_size - bufix); mainbufix = option_size - bufix; if (mainbufix < DHCP_FILE_LEN) - outpacket -> file [mainbufix++] - = DHO_END; + outpacket->file[mainbufix++] = DHO_END; while (mainbufix < DHCP_FILE_LEN) - outpacket -> file [mainbufix++] - = DHO_PAD; + outpacket->file[mainbufix++] = DHO_PAD; } else { - memcpy (outpacket -> file, - &buffer [bufix], DHCP_FILE_LEN); + memcpy(outpacket->file, + &buffer[bufix], DHCP_FILE_LEN); bufix += DHCP_FILE_LEN; } } if ((overload & 2) && option_size < bufix) { - memcpy (outpacket -> sname, - &buffer [bufix], option_size - bufix); + memcpy(outpacket->sname, + &buffer[bufix], option_size - bufix); mainbufix = option_size - bufix; if (mainbufix < DHCP_SNAME_LEN) - outpacket -> file [mainbufix++] - = DHO_END; + outpacket->file[mainbufix++] = DHO_END; while (mainbufix < DHCP_SNAME_LEN) - outpacket -> file [mainbufix++] - = DHO_PAD; + outpacket->file[mainbufix++] = DHO_PAD; } } - return length; + return (length); } -/* Store all the requested options into the requested buffer. */ - -int store_options (buffer, buflen, options, priority_list, priority_len, - first_cutoff, second_cutoff, terminate) - unsigned char *buffer; - int buflen; - struct tree_cache **options; - unsigned char *priority_list; - int priority_len; - int first_cutoff, second_cutoff; - int terminate; +/* + * Store all the requested options into the requested buffer. + */ +int +store_options(unsigned char *buffer, int buflen, struct tree_cache **options, + unsigned char *priority_list, int priority_len, int first_cutoff, + int second_cutoff, int terminate) { int bufix = 0; int option_stored [256]; @@ -357,110 +354,116 @@ int store_options (buffer, buflen, options, priority_list, priority_len, int tto; /* Zero out the stored-lengths array. */ - memset (option_stored, 0, sizeof option_stored); + memset(option_stored, 0, sizeof(option_stored)); - /* Copy out the options in the order that they appear in the - priority list... */ + /* + * Copy out the options in the order that they appear in the + * priority list... + */ for (i = 0; i < priority_len; i++) { /* Code for next option to try to store. */ - int code = priority_list [i]; + int code = priority_list[i]; int optstart; - /* Number of bytes left to store (some may already - have been stored by a previous pass). */ + /* + * Number of bytes left to store (some may already have + * been stored by a previous pass). + */ int length; /* If no data is available for this option, skip it. */ - if (!options [code]) { + if (!options[code]) { continue; } - /* The client could ask for things that are mandatory, - in which case we should avoid storing them twice... */ - if (option_stored [code]) + /* + * The client could ask for things that are mandatory, + * in which case we should avoid storing them twice... + */ + if (option_stored[code]) continue; - option_stored [code] = 1; + option_stored[code] = 1; /* Find the value of the option... */ - if (!tree_evaluate (options [code])) { + if (!tree_evaluate(options[code])) continue; - } /* We should now have a constant length for the option. */ - length = options [code] -> len; + length = options[code] -> len; /* Do we add a NUL? */ - if (terminate && dhcp_options [code].format [0] == 't') { + if (terminate && dhcp_options[code].format [0] == 't') { length++; tto = 1; - } else { + } else tto = 0; - } /* Try to store the option. */ - /* If the option's length is more than 255, we must store it - in multiple hunks. Store 255-byte hunks first. However, - in any case, if the option data will cross a buffer - boundary, split it across that boundary. */ - + /* + * If the option's length is more than 255, we must + * store it in multiple hunks. Store 255-byte hunks + * first. However, in any case, if the option data will + * cross a buffer boundary, split it across that + * boundary. + */ ix = 0; optstart = bufix; while (length) { unsigned char incr = length > 255 ? 255 : length; - /* If this hunk of the buffer will cross a - boundary, only go up to the boundary in this - pass. */ + /* + * If this hunk of the buffer will cross a + * boundary, only go up to the boundary in this + * pass. + */ if (bufix < first_cutoff && bufix + incr > first_cutoff) incr = first_cutoff - bufix; else if (bufix < second_cutoff && - bufix + incr > second_cutoff) + bufix + incr > second_cutoff) incr = second_cutoff - bufix; - /* If this option is going to overflow the buffer, - skip it. */ + /* + * If this option is going to overflow the + * buffer, skip it. + */ if (bufix + 2 + incr > buflen) { bufix = optstart; break; } /* Everything looks good - copy it in! */ - buffer [bufix] = code; - buffer [bufix + 1] = incr; + buffer[bufix] = code; + buffer[bufix + 1] = incr; if (tto && incr == length) { - memcpy (buffer + bufix + 2, - options [code] -> value + ix, - incr - 1); - buffer [bufix + 2 + incr - 1] = 0; - } else { - memcpy (buffer + bufix + 2, - options [code] -> value + ix, incr); - } + memcpy(buffer + bufix + 2, + options[code]->value + ix, incr - 1); + buffer[bufix + 2 + incr - 1] = 0; + } else + memcpy(buffer + bufix + 2, + options[code]->value + ix, incr); length -= incr; ix += incr; bufix += 2 + incr; } } - return bufix; + return (bufix); } -/* Format the specified option so that a human can easily read it. */ - -char *pretty_print_option (code, data, len, emit_commas, emit_quotes) - unsigned int code; - unsigned char *data; - int len; - int emit_commas; - int emit_quotes; +/* + * Format the specified option so that a human can easily read it. + */ +char * +pretty_print_option(unsigned int code, unsigned char *data, int len, + int emit_commas, int emit_quotes) { - static char optbuf [32768]; /* XXX */ + static char optbuf[32768]; /* XXX */ int hunksize = 0; int numhunk = -1; int numelem = 0; - char fmtbuf [32]; + char fmtbuf[32]; int i, j, k; char *op = optbuf; int opleft = sizeof(optbuf); @@ -471,49 +474,48 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) /* Code should be between 0 and 255. */ if (code > 255) - error ("pretty_print_option: bad code %d\n", code); + error("pretty_print_option: bad code %d\n", code); if (emit_commas) comma = ','; else comma = ' '; - + /* Figure out the size of the data. */ - for (i = 0; dhcp_options [code].format [i]; i++) { + for (i = 0; dhcp_options[code].format[i]; i++) { if (!numhunk) { - warn ("%s: Excess information in format string: %s", - dhcp_options [code].name, - &(dhcp_options [code].format [i])); + warn("%s: Excess information in format string: %s", + dhcp_options[code].name, + &(dhcp_options[code].format[i])); break; } numelem++; - fmtbuf [i] = dhcp_options [code].format [i]; - switch (dhcp_options [code].format [i]) { + fmtbuf[i] = dhcp_options[code].format[i]; + switch (dhcp_options[code].format[i]) { case 'A': --numelem; - fmtbuf [i] = 0; + fmtbuf[i] = 0; numhunk = 0; break; case 'X': - for (k = 0; k < len; k++) { - if (!isascii (data [k]) || - !isprint (data [k])) + for (k = 0; k < len; k++) + if (!isascii(data[k]) || + !isprint(data[k])) break; - } if (k == len) { - fmtbuf [i] = 't'; + fmtbuf[i] = 't'; numhunk = -2; } else { - fmtbuf [i] = 'x'; + fmtbuf[i] = 'x'; hunksize++; comma = ':'; numhunk = 0; } - fmtbuf [i + 1] = 0; + fmtbuf[i + 1] = 0; break; case 't': - fmtbuf [i] = 't'; - fmtbuf [i + 1] = 0; + fmtbuf[i] = 't'; + fmtbuf[i + 1] = 0; numhunk = -2; break; case 'I': @@ -533,34 +535,31 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) case 'e': break; default: - warn ("%s: garbage in format string: %s", - dhcp_options [code].name, - &(dhcp_options [code].format [i])); + warn("%s: garbage in format string: %s", + dhcp_options[code].name, + &(dhcp_options[code].format[i])); break; - } + } } /* Check for too few bytes... */ if (hunksize > len) { - warn ("%s: expecting at least %d bytes; got %d", - dhcp_options [code].name, - hunksize, len); - return "<error>"; + warn("%s: expecting at least %d bytes; got %d", + dhcp_options[code].name, hunksize, len); + return ("<error>"); } /* Check for too many bytes... */ if (numhunk == -1 && hunksize < len) - warn ("%s: %d extra bytes", - dhcp_options [code].name, - len - hunksize); + warn("%s: %d extra bytes", + dhcp_options[code].name, len - hunksize); /* If this is an array, compute its size. */ if (!numhunk) numhunk = len / hunksize; /* See if we got an exact number of hunks. */ if (numhunk > 0 && numhunk * hunksize < len) - warn ("%s: %d extra bytes at end of array", - dhcp_options [code].name, - len - numhunk * hunksize); + warn("%s: %d extra bytes at end of array", + dhcp_options[code].name, len - numhunk * hunksize); /* A one-hunk array prints the same as a single hunk. */ if (numhunk < 0) @@ -570,15 +569,15 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) for (i = 0; i < numhunk; i++) { for (j = 0; j < numelem; j++) { int opcount; - switch (fmtbuf [j]) { + switch (fmtbuf[j]) { case 't': if (emit_quotes) { *op++ = '"'; opleft--; } for (; dp < data + len; dp++) { - if (!isascii (*dp) || - !isprint (*dp)) { + if (!isascii(*dp) || + !isprint(*dp)) { if (dp + 1 != data + len || *dp != 0) { snprintf(op, opleft, @@ -587,10 +586,10 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) opleft -= 4; } } else if (*dp == '"' || - *dp == '\'' || - *dp == '$' || - *dp == '`' || - *dp == '\\') { + *dp == '\'' || + *dp == '$' || + *dp == '`' || + *dp == '\\') { *op++ = '\\'; *op++ = *dp; opleft -= 2; @@ -603,80 +602,79 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) *op++ = '"'; opleft--; } - + *op = 0; break; case 'I': - foo.s_addr = htonl(getULong (dp)); - opcount = strlcpy(op, inet_ntoa (foo), - opleft); - if (opcount >= opleft) + foo.s_addr = htonl(getULong(dp)); + opcount = strlcpy(op, inet_ntoa(foo), opleft); + if (opcount >= opleft) goto toobig; opleft -= opcount; dp += 4; break; case 'l': - opcount = snprintf(op, opleft,"%ld", - (long)getLong (dp)); - if (opcount >= opleft) + opcount = snprintf(op, opleft, "%ld", + (long)getLong(dp)); + if (opcount >= opleft) goto toobig; opleft -= opcount; dp += 4; break; case 'L': opcount = snprintf(op, opleft, "%ld", - (unsigned long)getULong (dp)); - if (opcount >= opleft) + (unsigned long)getULong(dp)); + if (opcount >= opleft) goto toobig; opleft -= opcount; dp += 4; break; case 's': opcount = snprintf(op, opleft, "%d", - getShort (dp)); - if (opcount >= opleft) + getShort(dp)); + if (opcount >= opleft) goto toobig; opleft -= opcount; dp += 2; break; case 'S': opcount = snprintf(op, opleft, "%d", - getUShort (dp)); - if (opcount >= opleft) + getUShort(dp)); + if (opcount >= opleft) goto toobig; opleft -= opcount; dp += 2; break; case 'b': - opcount = snprintf(op, opleft, "%d", - *(char *)dp++); - if (opcount >= opleft) + opcount = snprintf(op, opleft, "%d", + *(char *)dp++); + if (opcount >= opleft) goto toobig; opleft -= opcount; break; case 'B': opcount = snprintf(op, opleft, "%d", *dp++); - if (opcount >= opleft) + if (opcount >= opleft) goto toobig; opleft -= opcount; break; case 'x': opcount = snprintf(op, opleft, "%x", *dp++); - if (opcount >= opleft) + if (opcount >= opleft) goto toobig; opleft -= opcount; break; - case 'f': - opcount = strlcpy(op, - *dp++ ? "true" : "false", opleft); - if (opcount >= opleft) + case 'f': + opcount = strlcpy(op, + *dp++ ? "true" : "false", opleft); + if (opcount >= opleft) goto toobig; opleft -= opcount; break; default: - warn ("Unexpected format code %c", fmtbuf [j]); + warn("Unexpected format code %c", fmtbuf[j]); } - op += strlen (op); + op += strlen(op); opleft -= strlen(op); if (opleft < 1) goto toobig; @@ -691,52 +689,45 @@ char *pretty_print_option (code, data, len, emit_commas, emit_quotes) } if (opleft < 1) goto toobig; - + } - return optbuf; + return (optbuf); toobig: - warn ("dhcp option too large"); - return "<error>"; + warn("dhcp option too large"); + return ("<error>"); } -void do_packet (interface, packet, len, from_port, from, hfrom) - struct interface_info *interface; - struct dhcp_packet *packet; - int len; - unsigned int from_port; - struct iaddr from; - struct hardware *hfrom; +void +do_packet(struct interface_info *interface, struct dhcp_packet *packet, + int len, unsigned int from_port, struct iaddr from, struct hardware *hfrom) { struct packet tp; int i; - if (packet -> hlen > sizeof packet -> chaddr) { - note ("Discarding packet with invalid hlen."); + if (packet->hlen > sizeof(packet->chaddr)) { + note("Discarding packet with invalid hlen."); return; } - memset (&tp, 0, sizeof tp); + memset(&tp, 0, sizeof(tp)); tp.raw = packet; tp.packet_length = len; tp.client_port = from_port; tp.client_addr = from; tp.interface = interface; tp.haddr = hfrom; - - parse_options (&tp); + + parse_options(&tp); if (tp.options_valid && - tp.options [DHO_DHCP_MESSAGE_TYPE].data) - tp.packet_type = - tp.options [DHO_DHCP_MESSAGE_TYPE].data [0]; + tp.options[DHO_DHCP_MESSAGE_TYPE].data) + tp.packet_type = tp.options[DHO_DHCP_MESSAGE_TYPE].data[0]; if (tp.packet_type) - dhcp (&tp); + dhcp(&tp); else - bootp (&tp); + bootp(&tp); /* Free the data associated with the options. */ - for (i = 0; i < 256; i++) { - if (tp.options [i].len && tp.options [i].data) - dfree (tp.options [i].data, "do_packet"); - } + for (i = 0; i < 256; i++) + if (tp.options[i].len && tp.options[i].data) + dfree(tp.options[i].data, "do_packet"); } - diff --git a/sbin/dhclient/packet.c b/sbin/dhclient/packet.c index e02760b4339..0c8e706a100 100644 --- a/sbin/dhclient/packet.c +++ b/sbin/dhclient/packet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.2 2004/02/04 12:16:56 henning Exp $ */ +/* $OpenBSD: packet.c,v 1.3 2004/02/06 11:33:22 henning Exp $ */ /* Packet assembly code, originally contributed by Archie Cobbs. */ @@ -46,95 +46,93 @@ #include <netinet/ip.h> #include <netinet/udp.h> -/* Compute the easy part of the checksum on a range of bytes. */ - -u_int32_t checksum (buf, nbytes, sum) - unsigned char *buf; - unsigned nbytes; - u_int32_t sum; +/* + * Compute the easy part of the checksum on a range of bytes. + */ +u_int32_t +checksum(unsigned char *buf, unsigned nbytes, u_int32_t sum) { int i; #ifdef DEBUG_CHECKSUM - debug ("checksum (%x %d %x)", buf, nbytes, sum); + debug("checksum (%x %d %x)", buf, nbytes, sum); #endif /* Checksum all the pairs of bytes first... */ for (i = 0; i < (nbytes & ~1U); i += 2) { #ifdef DEBUG_CHECKSUM_VERBOSE - debug ("sum = %x", sum); + debug("sum = %x", sum); #endif - sum += (u_int16_t) ntohs(*((u_int16_t *)(buf + i))); + sum += (u_int16_t)ntohs(*((u_int16_t *)(buf + i))); /* Add carry. */ if (sum > 0xFFFF) sum -= 0xFFFF; - } + } - /* If there's a single byte left over, checksum it, too. Network - byte order is big-endian, so the remaining byte is the high byte. */ + /* + * If there's a single byte left over, checksum it, too. + * Network byte order is big-endian, so the remaining byte is + * the high byte. + */ if (i < nbytes) { #ifdef DEBUG_CHECKSUM_VERBOSE - debug ("sum = %x", sum); + debug("sum = %x", sum); #endif - sum += buf [i] << 8; + sum += buf[i] << 8; /* Add carry. */ if (sum > 0xFFFF) sum -= 0xFFFF; } - - return sum; -} -/* Finish computing the sum, and then put it into network byte order. */ + return (sum); +} -u_int32_t wrapsum (sum) - u_int32_t sum; +/* + * Finish computing the sum, and then put it into network byte order. + */ +u_int32_t +wrapsum(u_int32_t sum) { #ifdef DEBUG_CHECKSUM - debug ("wrapsum (%x)", sum); + debug("wrapsum (%x)", sum); #endif sum = ~sum & 0xFFFF; #ifdef DEBUG_CHECKSUM_VERBOSE - debug ("sum = %x", sum); + debug("sum = %x", sum); #endif - + #ifdef DEBUG_CHECKSUM - debug ("wrapsum returns %x", htons (sum)); + debug("wrapsum returns %x", htons(sum)); #endif - return htons(sum); + return (htons(sum)); } -/* Assemble an hardware header... */ -/* XXX currently only supports ethernet; doesn't check for other types. */ - -void assemble_hw_header (interface, buf, bufix, to) - struct interface_info *interface; - unsigned char *buf; - int *bufix; - struct hardware *to; +/* + * Assemble a hardware header... + * + * XXX currently only supports ethernet; doesn't check for other types. + */ +void +assemble_hw_header(struct interface_info *interface, unsigned char *buf, + int *bufix, struct hardware *to) { -#if defined (HAVE_TR_SUPPORT) - if (interface -> hw_address.htype == HTYPE_IEEE802) - assemble_tr_header (interface, buf, bufix, to); +#ifdef HAVE_TR_SUPPORT + if (interface->hw_address.htype == HTYPE_IEEE802) + assemble_tr_header(interface, buf, bufix, to); else #endif - assemble_ethernet_header (interface, buf, bufix, to); + assemble_ethernet_header(interface, buf, bufix, to); } -/* UDP header and IP header assembled together for convenience. */ - -void assemble_udp_ip_header (interface, buf, bufix, - from, to, port, data, len) - struct interface_info *interface; - unsigned char *buf; - int *bufix; - u_int32_t from; - u_int32_t to; - unsigned int port; - unsigned char *data; - int len; +/* + * UDP header and IP header assembled together for convenience. + */ +void +assemble_udp_ip_header(struct interface_info *interface, unsigned char *buf, + int *bufix, u_int32_t from, u_int32_t to, unsigned int port, + unsigned char *data, int len) { struct ip ip; struct udphdr udp; @@ -151,151 +149,140 @@ void assemble_udp_ip_header (interface, buf, bufix, ip.ip_sum = 0; ip.ip_src.s_addr = from; ip.ip_dst.s_addr = to; - + /* Checksum the IP header... */ - ip.ip_sum = wrapsum (checksum ((unsigned char *)&ip, sizeof ip, 0)); - + ip.ip_sum = wrapsum(checksum((unsigned char *)&ip, sizeof(ip), 0)); + /* Copy the ip header into the buffer... */ - memcpy (&buf [*bufix], &ip, sizeof ip); - *bufix += sizeof ip; + memcpy(&buf[*bufix], &ip, sizeof(ip)); + *bufix += sizeof(ip); /* Fill out the UDP header */ udp.uh_sport = local_port; /* XXX */ udp.uh_dport = port; /* XXX */ udp.uh_ulen = htons(sizeof(udp) + len); - memset (&udp.uh_sum, 0, sizeof udp.uh_sum); - - /* Compute UDP checksums, including the ``pseudo-header'', the UDP - header and the data. */ + memset(&udp.uh_sum, 0, sizeof(udp.uh_sum)); - udp.uh_sum = - wrapsum (checksum ((unsigned char *)&udp, sizeof udp, - checksum (data, len, - checksum ((unsigned char *) - &ip.ip_src, - 2 * sizeof ip.ip_src, - IPPROTO_UDP + - (u_int32_t) - ntohs (udp.uh_ulen))))); + /* + * Compute UDP checksums, including the ``pseudo-header'', the + * UDP header and the data. + */ + udp.uh_sum = wrapsum(checksum((unsigned char *)&udp, sizeof(udp), + checksum(data, len, checksum((unsigned char *)&ip.ip_src, + 2 * sizeof(ip.ip_src), + IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen))))); /* Copy the udp header into the buffer... */ - memcpy (&buf [*bufix], &udp, sizeof udp); - *bufix += sizeof udp; + memcpy(&buf[*bufix], &udp, sizeof(udp)); + *bufix += sizeof(udp); } -/* Decode a hardware header... */ - -ssize_t decode_hw_header (interface, buf, bufix, from) - struct interface_info *interface; - unsigned char *buf; - int bufix; - struct hardware *from; +/* + * Decode a hardware header... + */ +ssize_t +decode_hw_header(struct interface_info *interface, unsigned char *buf, + int bufix, struct hardware *from) { -#if defined (HAVE_TR_SUPPORT) - if (interface -> hw_address.htype == HTYPE_IEEE802) - return decode_tr_header (interface, buf, bufix, from); +#ifdef HAVE_TR_SUPPORT + if (interface->hw_address.htype == HTYPE_IEEE802) + return (decode_tr_header(interface, buf, bufix, from)); else #endif - return decode_ethernet_header (interface, buf, bufix, from); + return (decode_ethernet_header(interface, buf, bufix, from)); } -/* UDP header and IP header decoded together for convenience. */ - -ssize_t decode_udp_ip_header (interface, buf, bufix, from, data, buflen) - struct interface_info *interface; - unsigned char *buf; - int bufix; - struct sockaddr_in *from; - unsigned char *data; - int buflen; +/* + * UDP header and IP header decoded together for convenience. + */ +ssize_t +decode_udp_ip_header(struct interface_info *interface, unsigned char *buf, + int bufix, struct sockaddr_in *from, unsigned char *data, int buflen) { - struct ip *ip; - struct udphdr *udp; - u_int32_t ip_len = (buf [bufix] & 0xf) << 2; - u_int32_t sum, usum; - static int ip_packets_seen; - static int ip_packets_bad_checksum; - static int udp_packets_seen; - static int udp_packets_bad_checksum; - static int udp_packets_length_checked; - static int udp_packets_length_overflow; - int len = 0; - - ip = (struct ip *)(buf + bufix); - udp = (struct udphdr *)(buf + bufix + ip_len); - - /* Check the IP header checksum - it should be zero. */ - ++ip_packets_seen; - if (wrapsum (checksum (buf + bufix, ip_len, 0))) { - ++ip_packets_bad_checksum; - if (ip_packets_seen > 4 && - (ip_packets_seen / ip_packets_bad_checksum) < 2) { - note ("%d bad IP checksums seen in %d packets", - ip_packets_bad_checksum, ip_packets_seen); - ip_packets_seen = ip_packets_bad_checksum = 0; - } - return -1; - } - - /* Check the IP packet length. */ - if (ntohs (ip -> ip_len) != buflen) - debug ("ip length %d disagrees with bytes received %d.", - ntohs (ip -> ip_len), buflen); - - /* Copy out the IP source address... */ - memcpy (&from -> sin_addr, &ip -> ip_src, 4); - - /* Compute UDP checksums, including the ``pseudo-header'', the UDP - header and the data. If the UDP checksum field is zero, we're - not supposed to do a checksum. */ - - if (!data) { - data = buf + bufix + ip_len + sizeof *udp; - len = ntohs (udp -> uh_ulen) - sizeof *udp; - ++udp_packets_length_checked; - if (len + data > buf + bufix + buflen) { - ++udp_packets_length_overflow; - if (udp_packets_length_checked > 4 && - (udp_packets_length_checked / - udp_packets_length_overflow) < 2) { - note ("%d udp packets in %d too long - dropped", - udp_packets_length_overflow, - udp_packets_length_checked); - udp_packets_length_overflow = - udp_packets_length_checked = 0; - } - return -1; - } - if (len + data != buf + bufix + buflen) - debug ("accepting packet with data after udp payload."); - } - - usum = udp -> uh_sum; - udp -> uh_sum = 0; - - sum = wrapsum (checksum ((unsigned char *)udp, sizeof *udp, - checksum (data, len, - checksum ((unsigned char *) - &ip -> ip_src, - 2 * sizeof ip -> ip_src, - IPPROTO_UDP + - (u_int32_t) - ntohs (udp -> uh_ulen))))); - - udp_packets_seen++; - if (usum && usum != sum) { - udp_packets_bad_checksum++; - if (udp_packets_seen > 4 && - (udp_packets_seen / udp_packets_bad_checksum) < 2) { - note ("%d bad udp checksums in %d packets", - udp_packets_bad_checksum, udp_packets_seen); - udp_packets_seen = udp_packets_bad_checksum = 0; - } - return -1; - } - - /* Copy out the port... */ - memcpy (&from -> sin_port, &udp -> uh_sport, sizeof udp -> uh_sport); - - return ip_len + sizeof *udp; + struct ip *ip; + struct udphdr *udp; + u_int32_t ip_len = (buf[bufix] & 0xf) << 2; + u_int32_t sum, usum; + static int ip_packets_seen; + static int ip_packets_bad_checksum; + static int udp_packets_seen; + static int udp_packets_bad_checksum; + static int udp_packets_length_checked; + static int udp_packets_length_overflow; + int len = 0; + + ip = (struct ip *)(buf + bufix); + udp = (struct udphdr *)(buf + bufix + ip_len); + + /* Check the IP header checksum - it should be zero. */ + ip_packets_seen++; + if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) { + ip_packets_bad_checksum++; + if (ip_packets_seen > 4 && + (ip_packets_seen / ip_packets_bad_checksum) < 2) { + note("%d bad IP checksums seen in %d packets", + ip_packets_bad_checksum, ip_packets_seen); + ip_packets_seen = ip_packets_bad_checksum = 0; + } + return (-1); + } + + /* Check the IP packet length. */ + if (ntohs(ip->ip_len) != buflen) + debug("ip length %d disagrees with bytes received %d.", + ntohs(ip->ip_len), buflen); + + /* Copy out the IP source address... */ + memcpy(&from->sin_addr, &ip->ip_src, 4); + + /* + * Compute UDP checksums, including the ``pseudo-header'', the + * UDP header and the data. If the UDP checksum field is zero, + * we're not supposed to do a checksum. + */ + if (!data) { + data = buf + bufix + ip_len + sizeof(*udp); + len = ntohs(udp->uh_ulen) - sizeof(*udp); + udp_packets_length_checked++; + if (len + data > buf + bufix + buflen) { + udp_packets_length_overflow++; + if (udp_packets_length_checked > 4 && + (udp_packets_length_checked / + udp_packets_length_overflow) < 2) { + note("%d udp packets in %d too long - dropped", + udp_packets_length_overflow, + udp_packets_length_checked); + udp_packets_length_overflow = + udp_packets_length_checked = 0; + } + return (-1); + } + if (len + data != buf + bufix + buflen) + debug("accepting packet with data after udp payload."); + } + + usum = udp->uh_sum; + udp->uh_sum = 0; + + sum = wrapsum(checksum((unsigned char *)udp, sizeof(*udp), + checksum(data, len, checksum((unsigned char *)&ip->ip_src, + 2 * sizeof(ip->ip_src), + IPPROTO_UDP + (u_int32_t)ntohs(udp -> uh_ulen))))); + + udp_packets_seen++; + if (usum && usum != sum) { + udp_packets_bad_checksum++; + if (udp_packets_seen > 4 && + (udp_packets_seen / udp_packets_bad_checksum) < 2) { + note("%d bad udp checksums in %d packets", + udp_packets_bad_checksum, udp_packets_seen); + udp_packets_seen = udp_packets_bad_checksum = 0; + } + return (-1); + } + + /* Copy out the port... */ + memcpy(&from->sin_port, &udp->uh_sport, sizeof(udp->uh_sport)); + + return (ip_len + sizeof(*udp)); } diff --git a/sbin/dhclient/parse.c b/sbin/dhclient/parse.c index c80de2976ff..c4d11e636dd 100644 --- a/sbin/dhclient/parse.c +++ b/sbin/dhclient/parse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.c,v 1.2 2004/02/04 12:16:56 henning Exp $ */ +/* $OpenBSD: parse.c,v 1.3 2004/02/06 11:33:22 henning Exp $ */ /* Common parser code for dhcpd and dhclient. */ @@ -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,305 +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 == EOL) { - /* 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); } - s = (char *)malloc (strlen (val) + 1); + s = (char *)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); - if (!parse_semi (cfile)) - return (char *)0; - 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 = (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 (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 = (char *)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 *timep; +/* + * lease-time :== NUMBER SEMI + */ +void +parse_lease_time(FILE *cfile, TIME *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 = (unsigned char *)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 = (char *)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 = (unsigned char *)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; @@ -384,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 { @@ -414,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; @@ -431,189 +431,189 @@ 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 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 +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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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); - return (TIME)0; + 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... */ @@ -628,14 +628,15 @@ TIME parse_date (cfile) 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; + /* + * 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); } diff --git a/sbin/dhclient/print.c b/sbin/dhclient/print.c index 94ed14fe770..743a816a92b 100644 --- a/sbin/dhclient/print.c +++ b/sbin/dhclient/print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: print.c,v 1.2 2004/02/04 12:16:56 henning Exp $ */ +/* $OpenBSD: print.c,v 1.3 2004/02/06 11:33:22 henning Exp $ */ /* Turn data structures into printable text. */ @@ -42,141 +42,136 @@ #include "dhcpd.h" -char *print_hw_addr (htype, hlen, data) - int htype; - int hlen; - unsigned char *data; +char * +print_hw_addr(int htype, int hlen, unsigned char *data) { - static char habuf [49]; + static char habuf[49]; char *s; int i; - if (htype == 0 || hlen == 0) { + if (htype == 0 || hlen == 0) goto bad; - } else { + else { int slen = sizeof(habuf); s = habuf; for (i = 0; i < hlen; i++) { int j; - j = snprintf (s, slen, "%02x", data [i]); + j = snprintf(s, slen, "%02x", data[i]); if (j <= 0) goto bad; - s += strlen (s); - slen -= (strlen(s) + 1); + s += strlen(s); + slen -= strlen(s) + 1; *s++ = ':'; } *--s = 0; } - return habuf; - bad: - strlcpy (habuf, "<null>", sizeof habuf); - return habuf; + return (habuf); +bad: + strlcpy(habuf, "<null>", sizeof(habuf)); + return (habuf); } -void print_lease (lease) - struct lease *lease; +void +print_lease(struct lease *lease) { struct tm *t; - char tbuf [32]; - - debug (" Lease %s", - piaddr (lease -> ip_addr)); - - t = gmtime (&lease -> starts); - strftime (tbuf, sizeof tbuf, "%Y/%m/%d %H:%M:%S", t); - debug (" start %s", tbuf); - - t = gmtime (&lease -> ends); - strftime (tbuf, sizeof tbuf, "%Y/%m/%d %H:%M:%S", t); - debug (" end %s", tbuf); - - t = gmtime (&lease -> timestamp); - strftime (tbuf, sizeof tbuf, "%Y/%m/%d %H:%M:%S", t); - debug (" stamp %s", tbuf); - - debug (" hardware addr = %s", - print_hw_addr (lease -> hardware_addr.htype, - lease -> hardware_addr.hlen, - lease -> hardware_addr.haddr)); - debug (" host %s ", - lease -> host ? lease -> host -> name : "<none>"); -} - -void dump_packet (tp) - struct packet *tp; + char tbuf[32]; + + debug(" Lease %s", piaddr(lease->ip_addr)); + + t = gmtime(&lease->starts); + strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", t); + debug(" start %s", tbuf); + + t = gmtime(&lease->ends); + strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", t); + debug(" end %s", tbuf); + + t = gmtime(&lease->timestamp); + strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", t); + debug(" stamp %s", tbuf); + + debug(" hardware addr = %s", print_hw_addr( + lease->hardware_addr.htype, + lease->hardware_addr.hlen, + lease->hardware_addr.haddr)); + debug(" host %s ", + lease->host ? lease->host->name : "<none>"); +} + +void +dump_packet(struct packet *tp) { - struct dhcp_packet *tdp = tp -> raw; - - debug ("packet length %d", tp -> packet_length); - debug ("op = %d htype = %d hlen = %d hops = %d", - tdp -> op, tdp -> htype, tdp -> hlen, tdp -> hops); - debug ("xid = %x secs = %d flags = %x", - tdp -> xid, tdp -> secs, tdp -> flags); - debug ("ciaddr = %s", inet_ntoa (tdp -> ciaddr)); - debug ("yiaddr = %s", inet_ntoa (tdp -> yiaddr)); - debug ("siaddr = %s", inet_ntoa (tdp -> siaddr)); - debug ("giaddr = %s", inet_ntoa (tdp -> giaddr)); - debug ("chaddr = %02x:%02x:%02x:%02x:%02x:%02x", - ((unsigned char *)(tdp -> chaddr)) [0], - ((unsigned char *)(tdp -> chaddr)) [1], - ((unsigned char *)(tdp -> chaddr)) [2], - ((unsigned char *)(tdp -> chaddr)) [3], - ((unsigned char *)(tdp -> chaddr)) [4], - ((unsigned char *)(tdp -> chaddr)) [5]); - debug ("filename = %s", tdp -> file); - debug ("server_name = %s", tdp -> sname); - if (tp -> options_valid) { + struct dhcp_packet *tdp = tp->raw; + + debug("packet length %d", tp->packet_length); + debug("op = %d htype = %d hlen = %d hops = %d", + tdp->op, tdp->htype, tdp->hlen, tdp->hops); + debug("xid = %x secs = %d flags = %x", + tdp->xid, tdp->secs, tdp->flags); + debug("ciaddr = %s", inet_ntoa(tdp->ciaddr)); + debug("yiaddr = %s", inet_ntoa(tdp->yiaddr)); + debug("siaddr = %s", inet_ntoa(tdp->siaddr)); + debug("giaddr = %s", inet_ntoa(tdp->giaddr)); + debug("chaddr = %02x:%02x:%02x:%02x:%02x:%02x", + ((unsigned char *)tdp->chaddr)[0], + ((unsigned char *)tdp->chaddr)[1], + ((unsigned char *)tdp->chaddr)[2], + ((unsigned char *)tdp->chaddr)[3], + ((unsigned char *)tdp->chaddr)[4], + ((unsigned char *)tdp->chaddr)[5]); + debug("filename = %s", tdp->file); + debug("server_name = %s", tdp->sname); + if (tp->options_valid) { int i; - for (i = 0; i < 256; i++) { - if (tp -> options [i].data) - debug (" %s = %s", - dhcp_options [i].name, - pretty_print_option - (i, tp -> options [i].data, - tp -> options [i].len, 1, 1)); - } + for (i = 0; i < 256; i++) + if (tp->options[i].data) + debug(" %s = %s", + dhcp_options[i].name, + pretty_print_option(i, + tp->options[i].data, + tp->options[i].len, 1, 1)); } - debug ("%s", ""); + debug("%s", ""); } -void dump_raw (buf, len) - unsigned char *buf; - int len; +void +dump_raw(unsigned char *buf, int len) { int i, j; - char lbuf [80]; + char lbuf[80]; int llen = sizeof(lbuf); int lbix = 0; - lbuf [0] = 0; + lbuf[0] = 0; for (i = 0; i < len; i++) { if ((i & 15) == 0) { if (lbix) - note (lbuf); - j = snprintf (lbuf, llen, "%03x:", i); + note(lbuf); + j = snprintf(lbuf, llen, "%03x:", i); if (j >= llen) return; - lbix+=j; - llen-=j; - } else if ((i & 7) == 0) { - lbuf [lbix++] = ' '; + lbix += j; + llen -= j; + } else if ((i & 7) == 0) { + lbuf[lbix++] = ' '; len--; } - j = snprintf (&lbuf [lbix], llen, " %02x", buf [i]); + j = snprintf(&lbuf[lbix], llen, " %02x", buf[i]); if (j >= llen) return; lbix += j; llen -= j; } - note (lbuf); + note(lbuf); } -void hash_dump (table) - struct hash_table *table; +void +hash_dump(struct hash_table *table) { int i; struct hash_bucket *bp; @@ -184,15 +179,14 @@ void hash_dump (table) if (!table) return; - for (i = 0; i < table -> hash_count; i++) { - if (!table -> buckets [i]) + for (i = 0; i < table->hash_count; i++) { + if (!table->buckets[i]) continue; - note ("hash bucket %d:", i); - for (bp = table -> buckets [i]; bp; bp = bp -> next) { - if (bp -> len) - dump_raw (bp -> name, bp -> len); + note("hash bucket %d:", i); + for (bp = table->buckets[i]; bp; bp = bp->next) + if (bp->len) + dump_raw(bp->name, bp->len); else - note ((char *)bp -> name); - } + note((char *)bp->name); } } diff --git a/sbin/dhclient/socket.c b/sbin/dhclient/socket.c index 02db6f5c109..e7e965af677 100644 --- a/sbin/dhclient/socket.c +++ b/sbin/dhclient/socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: socket.c,v 1.2 2004/02/04 12:16:56 henning Exp $ */ +/* $OpenBSD: socket.c,v 1.3 2004/02/06 11:33:22 henning Exp $ */ /* BSD socket interface code... */ @@ -42,112 +42,110 @@ #include "dhcpd.h" -/* Generic interface registration routine... */ -int if_register_socket (info) - struct interface_info *info; +/* + * Generic interface registration routine... + */ +int +if_register_socket(struct interface_info *info) { struct sockaddr_in name; int sock; int flag; /* Set up the address we're going to bind to. */ - memset(&name, 0, sizeof name); + memset(&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_port = local_port; name.sin_addr.s_addr = INADDR_ANY; /* Make a socket... */ - if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) - error ("Can't create dhcp socket: %m"); + if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) + error("Can't create dhcp socket: %m"); /* Set the REUSEADDR option so that we don't fail to start if we're being restarted. */ flag = 1; - if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, - (char *)&flag, sizeof flag) < 0) - error ("Can't set SO_REUSEADDR option on dhcp socket: %m"); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + (char *)&flag, sizeof(flag)) < 0) + error("Can't set SO_REUSEADDR option on dhcp socket: %m"); flag = 1; - if (setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, - (char *)&flag, sizeof flag) < 0) - error ("Can't set SO_REUSEPORT option on dhcp socket: %m"); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, + (char *)&flag, sizeof(flag)) < 0) + error("Can't set SO_REUSEPORT option on dhcp socket: %m"); /* Set the BROADCAST option so that we can broadcast DHCP responses. */ - if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST, - (char *)&flag, sizeof flag) < 0) - error ("Can't set SO_BROADCAST option on dhcp socket: %m"); + if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, + (char *)&flag, sizeof(flag)) < 0) + error("Can't set SO_BROADCAST option on dhcp socket: %m"); /* Bind the socket to this interface's IP address. */ - if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0) - error ("Can't bind to dhcp address: %m"); + if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) + error("Can't bind to dhcp address: %m"); flag = IPSEC_LEVEL_BYPASS; - if (setsockopt (sock, IPPROTO_IP, IP_AUTH_LEVEL, - (char *)&flag, sizeof flag) == -1) + if (setsockopt(sock, IPPROTO_IP, IP_AUTH_LEVEL, + (char *)&flag, sizeof(flag)) == -1) if (errno != EOPNOTSUPP) - error ("Can't bypass auth IPsec on dhcp socket: %m"); - if (setsockopt (sock, IPPROTO_IP, IP_ESP_TRANS_LEVEL, - (char *)&flag, sizeof flag) == -1) + error("Can't bypass auth IPsec on dhcp socket: %m"); + if (setsockopt(sock, IPPROTO_IP, IP_ESP_TRANS_LEVEL, + (char *)&flag, sizeof(flag)) == -1) if (errno != EOPNOTSUPP) - error ("Can't bypass ESP transport on dhcp socket: %m"); - if (setsockopt (sock, IPPROTO_IP, IP_ESP_NETWORK_LEVEL, - (char *)&flag, sizeof flag) == -1) + error("Can't bypass ESP transport on dhcp socket: %m"); + if (setsockopt(sock, IPPROTO_IP, IP_ESP_NETWORK_LEVEL, + (char *)&flag, sizeof(flag)) == -1) if (errno != EOPNOTSUPP) - error ("Can't bypass ESP network on dhcp socket: %m"); + error("Can't bypass ESP network on dhcp socket: %m"); - return sock; + return (sock); } -void if_register_fallback (info) - struct interface_info *info; +void +if_register_fallback(struct interface_info *info) { - - info -> wfdesc = if_register_socket (info); + info->wfdesc = if_register_socket(info); if (!quiet_interface_discovery) - note ("Sending on Socket/%s%s%s", - info -> name, - (info -> shared_network ? "/" : ""), - (info -> shared_network ? - info -> shared_network -> name : "")); + note("Sending on Socket/%s%s%s", + info->name, + (info->shared_network ? "/" : ""), + (info->shared_network ? + info->shared_network -> name : "")); } -ssize_t send_fallback (interface, packet, raw, len, from, to, hto) - struct interface_info *interface; - struct packet *packet; - struct dhcp_packet *raw; - size_t len; - struct in_addr from; - struct sockaddr_in *to; - struct hardware *hto; +ssize_t +send_fallback(struct interface_info *interface, struct packet *packet, + struct dhcp_packet *raw, size_t len, struct in_addr from, + struct sockaddr_in *to, struct hardware *hto) { int result; - result = sendto (interface -> wfdesc, (char *)raw, len, 0, - (struct sockaddr *)to, sizeof *to); + result = sendto(interface->wfdesc, (char *)raw, len, 0, + (struct sockaddr *)to, sizeof(*to)); if (result == -1) { - warn ("send_fallback: %m"); + warn("send_fallback: %m"); if (errno == ENETUNREACH) - warn ("send_fallback: please consult README file %s", - "regarding broadcast address."); + warn("send_fallback: please consult README file %s", + "regarding broadcast address."); } - return result; + return (result); } -/* This just reads in a packet and silently discards it. */ - -void fallback_discard (protocol) - struct protocol *protocol; +/* + * This just reads in a packet and silently discards it. + */ +void +fallback_discard(struct protocol *protocol) { - char buf [1540]; + char buf[1540]; struct sockaddr_in from; - socklen_t flen = sizeof from; + socklen_t flen = sizeof(from); int status; - struct interface_info *interface = protocol -> local; + struct interface_info *interface = protocol->local; - status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0, - (struct sockaddr *)&from, &flen); + status = recvfrom(interface->wfdesc, buf, sizeof(buf), 0, + (struct sockaddr *)&from, &flen); if (status == 0) - warn ("fallback_discard: %m"); + warn("fallback_discard: %m"); } |