From 286548b754896b7ec466be79843ae2df4c4c40d3 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Mon, 25 Jul 2011 23:34:32 -0400 Subject: [PATCH] Encapsulate all numeric option additions into options.c. --- ndhc/dhcp.c | 30 +++++++++++++----------------- ndhc/options.c | 29 ++++++++++++++++++++++++++--- ndhc/options.h | 7 ++++--- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/ndhc/dhcp.c b/ndhc/dhcp.c index e08a374..2fef189 100644 --- a/ndhc/dhcp.c +++ b/ndhc/dhcp.c @@ -584,7 +584,7 @@ static struct dhcpmsg init_packet(char type, uint32_t xid) .options[0] = DCODE_END, .xid = xid, }; - add_u8_option(&packet, DCODE_MSGTYPE, type); + add_option_msgtype(&packet, type); memcpy(packet.chaddr, client_config.arp, 6); add_option_clientid(&packet); return packet; @@ -594,9 +594,8 @@ int send_discover(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid); if (cs->clientAddr) - add_u32_option(&packet, DCODE_REQIP, cs->clientAddr); - add_u16_option(&packet, DCODE_MAX_SIZE, - htons(sizeof(struct ip_udp_dhcp_packet))); + add_option_reqip(&packet, cs->clientAddr); + add_option_maxsize(&packet); add_option_request_list(&packet); add_option_vendor(&packet); add_option_hostname(&packet); @@ -608,10 +607,9 @@ int send_selecting(struct client_state_t *cs) { char clibuf[INET_ADDRSTRLEN]; struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); - add_u32_option(&packet, DCODE_REQIP, cs->clientAddr); - add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr); - add_u16_option(&packet, DCODE_MAX_SIZE, - htons(sizeof(struct ip_udp_dhcp_packet))); + add_option_reqip(&packet, cs->clientAddr); + add_option_serverid(&packet, cs->serverAddr); + add_option_maxsize(&packet); add_option_request_list(&packet); add_option_vendor(&packet); add_option_hostname(&packet); @@ -625,8 +623,7 @@ int send_renew(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); packet.ciaddr = cs->clientAddr; - add_u16_option(&packet, DCODE_MAX_SIZE, - htons(sizeof(struct ip_udp_dhcp_packet))); + add_option_maxsize(&packet); add_option_request_list(&packet); add_option_vendor(&packet); add_option_hostname(&packet); @@ -638,9 +635,8 @@ int send_rebind(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); packet.ciaddr = cs->clientAddr; - add_u32_option(&packet, DCODE_REQIP, cs->clientAddr); - add_u16_option(&packet, DCODE_MAX_SIZE, - htons(sizeof(struct ip_udp_dhcp_packet))); + add_option_reqip(&packet, cs->clientAddr); + add_option_maxsize(&packet); add_option_request_list(&packet); add_option_vendor(&packet); add_option_hostname(&packet); @@ -651,8 +647,8 @@ int send_rebind(struct client_state_t *cs) int send_decline(struct client_state_t *cs, uint32_t server) { struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid); - add_u32_option(&packet, DCODE_REQIP, cs->clientAddr); - add_u32_option(&packet, DCODE_SERVER_ID, server); + add_option_reqip(&packet, cs->clientAddr); + add_option_serverid(&packet, server); log_line("Sending a decline message..."); return send_dhcp_raw(&packet); } @@ -661,8 +657,8 @@ int send_release(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPRELEASE, libc_random_u32()); packet.ciaddr = cs->clientAddr; - add_u32_option(&packet, DCODE_REQIP, cs->clientAddr); - add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr); + add_option_reqip(&packet, cs->clientAddr); + add_option_serverid(&packet, cs->serverAddr); log_line("Sending a release message..."); return send_dhcp_cooked(cs, &packet); } diff --git a/ndhc/options.c b/ndhc/options.c index b0f8ff8..924eefc 100644 --- a/ndhc/options.c +++ b/ndhc/options.c @@ -258,7 +258,7 @@ static ssize_t add_option_check(struct dhcpmsg *packet, uint8_t code, return end; } -size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data) +static size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data) { ssize_t end = add_option_check(packet, code, 1); if (end < 0) @@ -271,7 +271,8 @@ size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data) } // Data should be in network byte order. -size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data) +static size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, + uint16_t data) { ssize_t end = add_option_check(packet, code, 2); if (end < 0) @@ -286,7 +287,8 @@ size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data) } // Data should be in network byte order. -size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, uint32_t data) +static size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, + uint32_t data) { ssize_t end = add_option_check(packet, code, 4); if (end < 0) @@ -314,3 +316,24 @@ size_t add_option_request_list(struct dhcpmsg *packet) return add_option_string(packet, DCODE_PARAM_REQ, (char *)reqdata, j); } +void add_option_msgtype(struct dhcpmsg *packet, uint8_t type) +{ + add_u8_option(packet, DCODE_MSGTYPE, type); +} + +void add_option_reqip(struct dhcpmsg *packet, uint32_t ip) +{ + add_u32_option(packet, DCODE_REQIP, ip); +} + +void add_option_maxsize(struct dhcpmsg *packet) +{ + add_u16_option(packet, DCODE_MAX_SIZE, + htons(sizeof(struct ip_udp_dhcp_packet))); +} + +void add_option_serverid(struct dhcpmsg *packet, uint32_t sid) +{ + add_u32_option(packet, DCODE_SERVER_ID, sid); +} + diff --git a/ndhc/options.h b/ndhc/options.h index 10ba577..448460e 100644 --- a/ndhc/options.h +++ b/ndhc/options.h @@ -74,9 +74,10 @@ uint8_t *get_option_data(struct dhcpmsg *packet, int code, ssize_t *optlen); ssize_t get_end_option_idx(struct dhcpmsg *packet); size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str, size_t slen); -size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data); -size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data); -size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, uint32_t data); size_t add_option_request_list(struct dhcpmsg *packet); +void add_option_msgtype(struct dhcpmsg *packet, uint8_t type); +void add_option_reqip(struct dhcpmsg *packet, uint32_t ip); +void add_option_maxsize(struct dhcpmsg *packet); +void add_option_serverid(struct dhcpmsg *packet, uint32_t sid); #endif