summaryrefslogtreecommitdiff
path: root/gdhcp
diff options
context:
space:
mode:
authorGrant Erickson <marathon96@gmail.com>2012-02-13 09:56:07 -0800
committerSamuel Ortiz <sameo@linux.intel.com>2012-02-13 19:24:32 +0100
commitdf176bf9cdf59757b6ea10281ad19ae47f935234 (patch)
tree8a03ec372418af8ea4029bf76474e5392a3fb7b3 /gdhcp
parentd4b273c2c160957e8de3fb70ed0843f4b3df6d8b (diff)
downloadconnman-df176bf9cdf59757b6ea10281ad19ae47f935234.tar.gz
connman-df176bf9cdf59757b6ea10281ad19ae47f935234.tar.bz2
connman-df176bf9cdf59757b6ea10281ad19ae47f935234.zip
gdhcp: Refactor alloc_dhcp_option to accept string or raw data.
Support specifying NULL-terminated string or raw data for allocated DHCP options.
Diffstat (limited to 'gdhcp')
-rw-r--r--gdhcp/client.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/gdhcp/client.c b/gdhcp/client.c
index 99f99510..c11f019d 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -2445,19 +2445,31 @@ void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
g_hash_table_remove_all(dhcp_client->send_value_hash);
}
-static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
+static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
{
uint8_t *storage;
- int len = strnlen(str, 255);
- storage = malloc(len + extra + OPT_DATA);
+ storage = g_try_malloc(size + OPT_DATA);
+ if (storage == NULL)
+ return NULL;
+
storage[OPT_CODE] = code;
- storage[OPT_LEN] = len + extra;
- memcpy(storage + extra + OPT_DATA, str, len);
+ storage[OPT_LEN] = size;
+ memcpy(&storage[OPT_DATA], data, size);
return storage;
}
+static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data, unsigned size)
+{
+ return alloc_dhcp_option(code, data, MIN(size, 255));
+}
+
+static uint8_t *alloc_dhcp_string_option(int code, const char *str)
+{
+ return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
+}
+
/* Now only support send hostname */
GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
unsigned char option_code, const char *option_value)
@@ -2465,8 +2477,10 @@ GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
uint8_t *binary_option;
if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
- binary_option = alloc_dhcp_option(option_code,
- option_value, 0);
+ binary_option = alloc_dhcp_string_option(option_code,
+ option_value);
+ if (binary_option == NULL)
+ return G_DHCP_CLIENT_ERROR_NOMEM;
g_hash_table_insert(dhcp_client->send_value_hash,
GINT_TO_POINTER((int) option_code), binary_option);