Skip to contentSkip to conversation

dhcp: Add domain name option handler

PATCH In review Tim Kourt 2 participants 2 messages Dec 6, 2019 – Dec 9, 2019

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/1 dhcp: Add domain name option handler

Add parser and accessor for the domain name lease option.

---
 ell/dhcp-lease.c   | 24 +++++++++++++++++++++++-
 ell/dhcp-private.h |  1 +
 ell/dhcp.h         |  1 +
 3 files changed, 25 insertions(+), 1 deletion(-)
ell/dhcp-lease.c +23 -1
index 5f801e7..06ef76a 100644
@@ -45,6 +45,8 @@ void _dhcp_lease_free(struct l_dhcp_lease *lease)
 		return;
 
 	l_free(lease->dns);
+	l_free(lease->domain_name);
+
 	l_free(lease);
 }
 
@@ -99,6 +101,18 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter)
 				}
 			}
 			break;
+		case L_DHCP_OPTION_DOMAIN_NAME:
+			if (l < 1)
+				break;
+
+			lease->domain_name = l_new(char, l + 1);
+
+			memcpy(lease->domain_name, v, l);
+
+			if (lease->domain_name[l - 1] != '\0')
+				lease->domain_name[l] = '\0';
+
+			break;
 		default:
 			break;
 		}
@@ -135,7 +149,7 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter)
 
 	return lease;
 error:
-	l_free(lease);
+	_dhcp_lease_free(lease);
 	return NULL;
 }
 
@@ -212,6 +226,14 @@ LIB_EXPORT char **l_dhcp_lease_get_dns(const struct l_dhcp_lease *lease)
 	return dns_list;
 }
 
+LIB_EXPORT char *l_dhcp_lease_get_domain_name(const struct l_dhcp_lease *lease)
+{
+	if (unlikely(!lease))
+		return NULL;
+
+	return l_strdup(lease->domain_name);
+}
+
 LIB_EXPORT uint32_t l_dhcp_lease_get_t1(const struct l_dhcp_lease *lease)
 {
 	if (unlikely(!lease))
ell/dhcp-private.h +1 -0
index 6554fc6..a75bb8b 100644
@@ -120,6 +120,7 @@ struct l_dhcp_lease {
 	uint32_t t2;
 	uint32_t router;
 	uint32_t *dns;
+	char *domain_name;
 };
 
 struct l_dhcp_lease *_dhcp_lease_new(void);
ell/dhcp.h +1 -0
index c3a4988..b8a5b41 100644
@@ -95,6 +95,7 @@ char *l_dhcp_lease_get_netmask(const struct l_dhcp_lease *lease);
 char *l_dhcp_lease_get_broadcast(const struct l_dhcp_lease *lease);
 char *l_dhcp_lease_get_server_id(const struct l_dhcp_lease *lease);
 char **l_dhcp_lease_get_dns(const struct l_dhcp_lease *lease);
+char *l_dhcp_lease_get_domain_name(const struct l_dhcp_lease *lease);
 
 uint32_t l_dhcp_lease_get_t1(const struct l_dhcp_lease *lease);
 uint32_t l_dhcp_lease_get_t2(const struct l_dhcp_lease *lease);
Signature · 1 line
2.13.6
#1 lore raw

Hi Tim,

Quoted 7 lines · Tim Kourt

Add parser and accessor for the domain name lease option.

---
  ell/dhcp-lease.c   | 24 +++++++++++++++++++++++-
  ell/dhcp-private.h |  1 +
  ell/dhcp.h         |  1 +
  3 files changed, 25 insertions(+), 1 deletion(-)

<snip>

Quoted 4 lines
ell/dhcp-lease.c +0 -0
@@ -99,6 +101,18 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter)
  				}
  			}
  			break;

ell/dhcp-lease.c struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter) · line 113

Quoted 10 lines
ell/dhcp-lease.c +10 -0
+		case L_DHCP_OPTION_DOMAIN_NAME:
+			if (l < 1)
+				break;
+
+			lease->domain_name = l_new(char, l + 1);
+
+			memcpy(lease->domain_name, v, l);
+
+			if (lease->domain_name[l - 1] != '\0')
+				lease->domain_name[l] = '\0';

So strictly speaking this if is not necessary as you already use l_new which memsets returned memory to 0. However, you might want to be much more strict about accepting NULs at the end. One is okay, multiple is likely not ok. Embedded NULs are probably not okay either.

Also, it might be a good idea to more strictly check the domain name here. Fail if we get an empty string, a dot or localhost. I believe hostnames has a max length as well. And we should maybe limit these to ascii or at least valid utf8 for now.

ell/dhcp-lease.c struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter) · line 118

Quoted 5 lines
ell/dhcp-lease.c +2 -0
+
+			break;
  		default:
  			break;
  		}

Regards,
-Denis