From 811cc67e167bed62b0a140e79e36e95ebabc74b0 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 30 Mar 2011 07:26:42 -0400 Subject: [PATCH] Move add_requests() from dhcpmsg.c to options.c. Fixes a layering violation. Document an ugly-as-hell code bit in ifchange.c. Add some debugging messages for the netlink response code. Clean headers a bit more. --- ndhc/dhcpmsg.c | 16 ---------------- ndhc/ifchange.c | 2 +- ndhc/netlink.c | 1 + ndhc/options.c | 22 ++++++++++++++++++++++ ndhc/options.h | 8 ++------ 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/ndhc/dhcpmsg.c b/ndhc/dhcpmsg.c index 545e01b..c6ce6d7 100644 --- a/ndhc/dhcpmsg.c +++ b/ndhc/dhcpmsg.c @@ -99,22 +99,6 @@ static void init_packet(struct dhcpMessage *packet, char type) add_option_string(packet->options, (unsigned char *)&vendor_id); } -/* Add a paramater request list for stubborn DHCP servers. Pull the data - * from the struct in options.c. Don't do bounds checking here because it - * goes towards the head of the packet. */ -static void add_requests(struct dhcpMessage *packet) -{ - int end = end_option(packet->options); - int i, len = 0; - - packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; - for (i = 0; options[i].code; i++) - if (options[i].flags & OPTION_REQ) - packet->options[end + OPT_DATA + len++] = options[i].code; - packet->options[end + OPT_LEN] = len; - packet->options[end + OPT_DATA + len] = DHCP_END; -} - #define MAC_BCAST_ADDR (unsigned char *) "\xff\xff\xff\xff\xff\xff" /* Wrapper that broadcasts a raw dhcp packet on the bound interface. */ static int bcast_raw_packet(struct dhcpMessage *packet) diff --git a/ndhc/ifchange.c b/ndhc/ifchange.c index f6f90fd..3b0b80a 100644 --- a/ndhc/ifchange.c +++ b/ndhc/ifchange.c @@ -69,7 +69,7 @@ static int fill_options(char *dest, unsigned char *option, if (!option) return -1; - int len = option[OPT_LEN - 2]; + int len = option[-1]; // XXX: WTF ugly as all hell odest = dest; diff --git a/ndhc/netlink.c b/ndhc/netlink.c index 85be768..fa9346f 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -132,6 +132,7 @@ static void nl_handlemsg(struct nlmsghdr *msg, unsigned int len, if (cs->dhcpState == DS_BOUND) { /* arp_check_gw(cs); */ } else if (cs->dhcpState != DS_INIT_SELECTING) { + log_line("nl: taking down interface"); // Same as packet.c: line 258 ifchange(NULL, IFCHANGE_DECONFIG); cs->dhcpState = DS_INIT_SELECTING; diff --git a/ndhc/options.c b/ndhc/options.c index e6d83e8..addba3d 100644 --- a/ndhc/options.c +++ b/ndhc/options.c @@ -13,6 +13,12 @@ #include "log.h" #include "malloc.h" +enum { + OPT_CODE = 0, + OPT_LEN = 1, + OPT_DATA = 2 +}; + /* supported options are easily added here */ struct dhcp_option options[] = { /* name[10] flags code */ @@ -242,3 +248,19 @@ struct option_set *find_option(struct option_set *opt_list, char code) return opt_list; return NULL; } + +/* Add a paramater request list for stubborn DHCP servers. Pull the data + * from the struct in options.c. Don't do bounds checking here because it + * goes towards the head of the packet. */ +void add_requests(struct dhcpMessage *packet) +{ + int end = end_option(packet->options); + int i, len = 0; + + packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; + for (i = 0; options[i].code; i++) + if (options[i].flags & OPTION_REQ) + packet->options[end + OPT_DATA + len++] = options[i].code; + packet->options[end + OPT_LEN] = len; + packet->options[end + OPT_DATA + len] = DHCP_END; +} diff --git a/ndhc/options.h b/ndhc/options.h index 6af6dda..4e22dca 100644 --- a/ndhc/options.h +++ b/ndhc/options.h @@ -68,12 +68,6 @@ enum { #define OPTION_REQ 0x10 /* have the client request this option */ #define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */ -enum { - OPT_CODE = 0, - OPT_LEN = 1, - OPT_DATA = 2 -}; - struct dhcp_option { char name[10]; char flags; @@ -103,4 +97,6 @@ int add_option_string(unsigned char *optionptr, unsigned char *string); int add_simple_option(unsigned char *optionptr, unsigned char code, uint32_t data); struct option_set *find_option(struct option_set *opt_list, char code); +void add_requests(struct dhcpMessage *packet); + #endif