This patch adds a RFC 1533- and 2132-compliant DHCP client-id option
(61) to DHCPv4 packets.

---
 gdhcp/client.c |   50 +++++++++++++++++++++++++++++++++++++++++++-------
 gdhcp/gdhcp.h  |    2 ++
 src/dhcp.c     |    2 ++
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/gdhcp/client.c b/gdhcp/client.c
index 99f9951..7dcf40e 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -2445,19 +2445,53 @@ 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));
+}
+
+GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
+{
+       const unsigned maclen = 6;
+       const unsigned idlen = maclen + 1;
+       const uint8_t option_code = G_DHCP_CLIENT_ID;
+       uint8_t idbuf[idlen];
+       uint8_t *data_option;
+
+       idbuf[0] = ARPHRD_ETHER;
+
+       memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
+
+       data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
+       if (data_option == NULL)
+               return G_DHCP_CLIENT_ERROR_NOMEM;
+
+       g_hash_table_insert(dhcp_client->send_value_hash,
+               GINT_TO_POINTER((int) option_code), data_option);
+
+       return G_DHCP_CLIENT_ERROR_NONE;
+}
+
 /* Now only support send hostname */
 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
                unsigned char option_code, const char *option_value)
@@ -2465,8 +2499,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);
diff --git a/gdhcp/gdhcp.h b/gdhcp/gdhcp.h
index edebc9e..4453b26 100644
--- a/gdhcp/gdhcp.h
+++ b/gdhcp/gdhcp.h
@@ -74,6 +74,7 @@ typedef enum {
 #define G_DHCP_DOMAIN_NAME     0x0f
 #define G_DHCP_HOST_NAME       0x0c
 #define G_DHCP_NTP_SERVER      0x2a
+#define G_DHCP_CLIENT_ID       0x3d
 
 #define G_DHCPV6_CLIENTID      1
 #define G_DHCPV6_SERVERID      2
@@ -121,6 +122,7 @@ GDHCPClientError g_dhcp_client_set_request(GDHCPClient 
*client,
                                                unsigned int option_code);
 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client);
 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client);
+GDHCPClientError g_dhcp_client_set_id(GDHCPClient *client);
 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *client,
                                                unsigned char option_code,
                                                const char *option_value);
diff --git a/src/dhcp.c b/src/dhcp.c
index 86e4261..ce1d240 100644
--- a/src/dhcp.c
+++ b/src/dhcp.c
@@ -411,6 +411,8 @@ static int dhcp_request(struct connman_dhcp *dhcp)
        if (getenv("CONNMAN_DHCP_DEBUG"))
                g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
 
+       g_dhcp_client_set_id(dhcp_client);
+
        hostname = connman_utsname_get_hostname();
        if (hostname != NULL)
                g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
-- 
1.7.8.2

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to