Encapsulate all string option additions into options.c. Note that these

functions already existed, but were in dhcp.c -- this is just code motion
between compilation units.
This commit is contained in:
Nicholas J. Kain 2011-07-25 23:48:35 -04:00
parent 286548b754
commit ea5d472424
3 changed files with 43 additions and 41 deletions

View File

@ -146,7 +146,7 @@ static int send_dhcp_cooked(struct client_state_t *cs, struct dhcpmsg *payload)
ssize_t endloc = get_end_option_idx(payload); ssize_t endloc = get_end_option_idx(payload);
if (endloc < 0) { if (endloc < 0) {
log_error("send_dhcp_cooked: Attempt to send packet with no DCODE_END."); log_error("send_dhcp_cooked: No end marker. Not sending.");
goto out_fd; goto out_fd;
} }
size_t payload_len = size_t payload_len =
@ -407,7 +407,7 @@ static int send_dhcp_raw(struct dhcpmsg *payload)
// and drop packets that are longer than 562 bytes. // and drop packets that are longer than 562 bytes.
ssize_t endloc = get_end_option_idx(payload); ssize_t endloc = get_end_option_idx(payload);
if (endloc < 0) { if (endloc < 0) {
log_error("send_dhcp_raw: Attempt to send packet with no DCODE_END."); log_error("send_dhcp_raw: No end marker. Not sending.");
close(fd); close(fd);
return ret; return ret;
} }
@ -538,41 +538,6 @@ void handle_packet(struct client_state_t *cs)
packet_action(cs, &packet, msgtype); packet_action(cs, &packet, msgtype);
} }
static void add_option_vendor(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.vendor);
if (len)
add_option_string(packet, DCODE_VENDOR, client_config.vendor, len);
else
add_option_string(packet, DCODE_VENDOR, "ndhc", sizeof "ndhc" - 1);
}
static void add_option_clientid(struct dhcpmsg *packet)
{
char buf[sizeof client_config.clientid + 1];
size_t len = 6;
buf[0] = 1; // Ethernet MAC
if (!client_config.clientid_mac) {
size_t slen = strlen(client_config.clientid);
if (!slen) {
memcpy(buf+1, client_config.arp, len);
} else {
buf[0] = 0; // Not a hardware address
len = slen;
memcpy(buf+1, client_config.clientid, slen);
}
} else
memcpy(buf+1, client_config.clientid, len);
add_option_string(packet, DCODE_CLIENT_ID, buf, len+1);
}
static void add_option_hostname(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.hostname);
if (len)
add_option_string(packet, DCODE_HOSTNAME, client_config.hostname, len);
}
// Initialize a DHCP client packet that will be sent to a server // Initialize a DHCP client packet that will be sent to a server
static struct dhcpmsg init_packet(char type, uint32_t xid) static struct dhcpmsg init_packet(char type, uint32_t xid)
{ {

View File

@ -217,8 +217,8 @@ ssize_t get_end_option_idx(struct dhcpmsg *packet)
// add an option string to the options (an option string contains an option // add an option string to the options (an option string contains an option
// code, length, then data) // code, length, then data)
size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str, static size_t add_option_string(struct dhcpmsg *packet, uint8_t code,
size_t slen) char *str, size_t slen)
{ {
size_t len = sizeof_option(code, slen); size_t len = sizeof_option(code, slen);
if (slen > 255 || len != slen + 2) { if (slen > 255 || len != slen + 2) {
@ -337,3 +337,38 @@ void add_option_serverid(struct dhcpmsg *packet, uint32_t sid)
add_u32_option(packet, DCODE_SERVER_ID, sid); add_u32_option(packet, DCODE_SERVER_ID, sid);
} }
void add_option_vendor(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.vendor);
if (len)
add_option_string(packet, DCODE_VENDOR, client_config.vendor, len);
else
add_option_string(packet, DCODE_VENDOR, "ndhc", sizeof "ndhc" - 1);
}
void add_option_clientid(struct dhcpmsg *packet)
{
char buf[sizeof client_config.clientid + 1];
size_t len = 6;
buf[0] = 1; // Ethernet MAC
if (!client_config.clientid_mac) {
size_t slen = strlen(client_config.clientid);
if (!slen) {
memcpy(buf+1, client_config.arp, len);
} else {
buf[0] = 0; // Not a hardware address
len = slen;
memcpy(buf+1, client_config.clientid, slen);
}
} else
memcpy(buf+1, client_config.clientid, len);
add_option_string(packet, DCODE_CLIENT_ID, buf, len+1);
}
void add_option_hostname(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.hostname);
if (len)
add_option_string(packet, DCODE_HOSTNAME, client_config.hostname, len);
}

View File

@ -72,12 +72,14 @@ int option_valid_list(uint8_t code);
uint8_t *get_option_data(struct dhcpmsg *packet, int code, ssize_t *optlen); uint8_t *get_option_data(struct dhcpmsg *packet, int code, ssize_t *optlen);
ssize_t get_end_option_idx(struct dhcpmsg *packet); 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_option_request_list(struct dhcpmsg *packet); size_t add_option_request_list(struct dhcpmsg *packet);
void add_option_msgtype(struct dhcpmsg *packet, uint8_t type); void add_option_msgtype(struct dhcpmsg *packet, uint8_t type);
void add_option_reqip(struct dhcpmsg *packet, uint32_t ip); void add_option_reqip(struct dhcpmsg *packet, uint32_t ip);
void add_option_maxsize(struct dhcpmsg *packet); void add_option_maxsize(struct dhcpmsg *packet);
void add_option_serverid(struct dhcpmsg *packet, uint32_t sid); void add_option_serverid(struct dhcpmsg *packet, uint32_t sid);
void add_option_vendor(struct dhcpmsg *packet);
void add_option_clientid(struct dhcpmsg *packet);
void add_option_hostname(struct dhcpmsg *packet);
#endif #endif