summaryrefslogtreecommitdiff
path: root/gdhcp
diff options
context:
space:
mode:
authorJukka Rissanen <jukka.rissanen@linux.intel.com>2012-10-11 10:11:52 +0300
committerMarcel Holtmann <marcel@holtmann.org>2012-10-11 15:31:43 +0200
commit132bb54b219270304d3047eedb5d60b58d94db16 (patch)
tree636c6a5516b9a3e7c1a0ba9bb9cfe74280fdacc8 /gdhcp
parent635f1e814e4e5a0d4f8356475dff32515e78f692 (diff)
downloadconnman-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')
-rw-r--r--gdhcp/client.c13
-rw-r--r--gdhcp/common.c75
-rw-r--r--gdhcp/common.h6
-rw-r--r--gdhcp/server.c9
4 files changed, 72 insertions, 31 deletions
diff --git a/gdhcp/client.c b/gdhcp/client.c
index 7340f3a0..b023c6dd 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -363,11 +363,11 @@ static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
packet.secs = dhcp_attempt_secs(dhcp_client);
if (requested)
- dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
+ dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
/* Explicitly saying that we want RFC-compliant packets helps
* some buggy DHCP servers to NOT send bigger packets */
- dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, 576);
+ dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
add_request_options(dhcp_client, &packet);
@@ -389,9 +389,10 @@ static int send_select(GDHCPClient *dhcp_client)
packet.xid = dhcp_client->xid;
packet.secs = dhcp_attempt_secs(dhcp_client);
- dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
- dhcp_client->requested_ip);
- dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
+ dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
+ dhcp_client->requested_ip);
+ dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
+ dhcp_client->server_ip);
add_request_options(dhcp_client, &packet);
@@ -451,7 +452,7 @@ static int send_release(GDHCPClient *dhcp_client,
packet.xid = rand();
packet.ciaddr = htonl(ciaddr);
- dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
+ dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
server, SERVER_PORT);
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)
diff --git a/gdhcp/common.h b/gdhcp/common.h
index e2bfc6c8..740eb9cd 100644
--- a/gdhcp/common.h
+++ b/gdhcp/common.h
@@ -179,7 +179,11 @@ int dhcp_end_option(uint8_t *optionptr);
void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt);
void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
uint16_t *pkt_len, uint8_t *addopt);
-void dhcp_add_simple_option(struct dhcp_packet *packet,
+void dhcp_add_option_uint8(struct dhcp_packet *packet,
+ uint8_t code, uint8_t data);
+void dhcp_add_option_uint16(struct dhcp_packet *packet,
+ uint8_t code, uint16_t data);
+void dhcp_add_option_uint32(struct dhcp_packet *packet,
uint8_t code, uint32_t data);
GDHCPOptionType dhcp_get_code_type(uint8_t code);
GDHCPOptionType dhcpv6_get_code_type(uint16_t code);
diff --git a/gdhcp/server.c b/gdhcp/server.c
index 2de7db69..0c5f2950 100644
--- a/gdhcp/server.c
+++ b/gdhcp/server.c
@@ -449,7 +449,8 @@ static void init_packet(GDHCPServer *dhcp_server, struct dhcp_packet *packet,
packet->flags = client_packet->flags;
packet->gateway_nip = client_packet->gateway_nip;
packet->ciaddr = client_packet->ciaddr;
- dhcp_add_simple_option(packet, DHCP_SERVER_ID, dhcp_server->server_nip);
+ dhcp_add_option_uint32(packet, DHCP_SERVER_ID,
+ dhcp_server->server_nip);
}
static void add_option(gpointer key, gpointer value, gpointer user_data)
@@ -469,7 +470,7 @@ static void add_option(gpointer key, gpointer value, gpointer user_data)
if (inet_aton(option_value, &nip) == 0)
return;
- dhcp_add_simple_option(packet, (uint8_t) option_code,
+ dhcp_add_option_uint32(packet, (uint8_t) option_code,
ntohl(nip.s_addr));
break;
default:
@@ -564,7 +565,7 @@ static void send_offer(GDHCPServer *dhcp_server,
return;
}
- dhcp_add_simple_option(&packet, DHCP_LEASE_TIME,
+ dhcp_add_option_uint32(&packet, DHCP_LEASE_TIME,
dhcp_server->lease_seconds);
add_server_options(dhcp_server, &packet);
@@ -600,7 +601,7 @@ static void send_ACK(GDHCPServer *dhcp_server,
lease_time_sec = dhcp_server->lease_seconds;
- dhcp_add_simple_option(&packet, DHCP_LEASE_TIME, lease_time_sec);
+ dhcp_add_option_uint32(&packet, DHCP_LEASE_TIME, lease_time_sec);
add_server_options(dhcp_server, &packet);