diff options
author | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2012-10-11 10:11:52 +0300 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2012-10-11 15:31:43 +0200 |
commit | 132bb54b219270304d3047eedb5d60b58d94db16 (patch) | |
tree | 636c6a5516b9a3e7c1a0ba9bb9cfe74280fdacc8 /gdhcp/common.c | |
parent | 635f1e814e4e5a0d4f8356475dff32515e78f692 (diff) | |
download | connman-132bb54b219270304d3047eedb5d60b58d94db16.tar.gz connman-132bb54b219270304d3047eedb5d60b58d94db16.tar.bz2 connman-132bb54b219270304d3047eedb5d60b58d94db16.zip |
gdhcp: Use data size specific option setting function
Instead of using dhcp_add_simple_option() the gdhcp now uses
three functions for setting uint8, uint16 and uint32 values.
Diffstat (limited to 'gdhcp/common.c')
-rw-r--r-- | gdhcp/common.c | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/gdhcp/common.c b/gdhcp/common.c index 55fddcbf..ed27e431 100644 --- a/gdhcp/common.c +++ b/gdhcp/common.c @@ -266,34 +266,69 @@ void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len, *pkt_len += len; } -void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, - uint32_t data) +static GDHCPOptionType check_option(uint8_t code, uint8_t data_len) { - uint8_t option[6], len; GDHCPOptionType type = dhcp_get_code_type(code); + uint8_t len; if (type == OPTION_UNKNOWN) + return type; + + len = dhcp_option_lengths[type & OPTION_TYPE_MASK]; + if (len != data_len) { + printf("Invalid option len %d (expecting %d) for code 0x%x\n", + data_len, len, code); + return OPTION_UNKNOWN; + } + + return type; +} + +void dhcp_add_option_uint32(struct dhcp_packet *packet, uint8_t code, + uint32_t data) +{ + uint8_t option[6]; + + if (check_option(code, sizeof(data)) == OPTION_UNKNOWN) return; option[OPT_CODE] = code; + option[OPT_LEN] = sizeof(data); + put_be32(data, option + OPT_DATA); - len = dhcp_option_lengths[type & OPTION_TYPE_MASK]; - option[OPT_LEN] = len; - - switch (len) { - case 1: - option[OPT_DATA] = data; - break; - case 2: - put_be16(data, option + OPT_DATA); - break; - case 4: - put_be32(data, option + OPT_DATA); - break; - default: - printf("Invalid option len %d for code 0x%x\n", len, code); + dhcp_add_binary_option(packet, option); + + return; +} + +void dhcp_add_option_uint16(struct dhcp_packet *packet, uint8_t code, + uint16_t data) +{ + uint8_t option[6]; + + if (check_option(code, sizeof(data)) == OPTION_UNKNOWN) return; - } + + option[OPT_CODE] = code; + option[OPT_LEN] = sizeof(data); + put_be16(data, option + OPT_DATA); + + dhcp_add_binary_option(packet, option); + + return; +} + +void dhcp_add_option_uint8(struct dhcp_packet *packet, uint8_t code, + uint8_t data) +{ + uint8_t option[6]; + + if (check_option(code, sizeof(data)) == OPTION_UNKNOWN) + return; + + option[OPT_CODE] = code; + option[OPT_LEN] = sizeof(data); + option[OPT_DATA] = data; dhcp_add_binary_option(packet, option); @@ -318,7 +353,7 @@ void dhcp_init_header(struct dhcp_packet *packet, char type) packet->cookie = htonl(DHCP_MAGIC); packet->options[0] = DHCP_END; - dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type); + dhcp_add_option_uint8(packet, DHCP_MESSAGE_TYPE, type); } void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type) |