gdhcp: Refactor alloc_dhcp_option to accept string or raw data.
authorGrant Erickson <marathon96@gmail.com>
Mon, 13 Feb 2012 17:56:07 +0000 (09:56 -0800)
committerSamuel Ortiz <sameo@linux.intel.com>
Mon, 13 Feb 2012 18:24:32 +0000 (19:24 +0100)
Support specifying NULL-terminated string or raw data for allocated
DHCP options.

gdhcp/client.c

index 99f9951..c11f019 100644 (file)
@@ -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);