udhcpd: HOST_NAME support for static leases

Philippe Jounin philippe.jounin at orange.fr
Tue May 7 21:18:18 UTC 2019


Hello all,

First of all, thanks for the continuous support of busybox. I love this set of small footprints applications.

I made a quick hack for udhcpd to support hostnames for static leases.
If instead of the documented example :
	static_lease 00:60:08:11:CE:4E 192.168.0.54
	static_lease 00:60:08:11:CE:3E 192.168.0.44
you write :
	static_lease 00:60:08:11:CE:4E 192.168.0.54 host-54
	static_lease 00:60:08:11:CE:3E 192.168.0.44 host-44

udhcpd will use the fourth word to set the HOST_NAME (0x0c) option, allowing the server to set the remote hostname for each static lease.

Here is the patch, feel free to add it into the next release if you think that it may be useful to someone else.
BTW i created the patch file with the command:
	diff -ruN busybox-1.30.1/networking/udhcp/ busybox-1.30.2/networking/udhcp/ > ~/udhcpd.patch,
hope it is fine.

Best regards,
Philippe Jounin




---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20190507/2013a91e/attachment.html>
-------------- next part --------------
diff -ruN busybox-1.30.1/networking/udhcp/dhcpd.c busybox-1.30.2/networking/udhcp/dhcpd.c
--- busybox-1.30.1/networking/udhcp/dhcpd.c	2018-12-30 16:14:20.000000000 +0100
+++ busybox-1.30.2/networking/udhcp/dhcpd.c	2019-05-07 22:46:32.518156001 +0200
@@ -53,6 +53,7 @@
  * 4 byte IP address */
 static void add_static_lease(struct static_lease **st_lease_pp,
 		uint8_t *mac,
+		const char *hostname,
 		uint32_t nip)
 {
 	struct static_lease *st_lease;
@@ -66,6 +67,7 @@
 	*st_lease_pp = st_lease = xzalloc(sizeof(*st_lease));
 	memcpy(st_lease->mac, mac, 6);
 	st_lease->nip = nip;
+	strncpy (st_lease->hostname, hostname, sizeof st_lease->hostname - 1);
 	/*st_lease->next = NULL;*/
 }
 
@@ -104,9 +106,10 @@
 
 	cur = *st_lease_pp;
 	while (cur) {
-		bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
+		bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x host:%s nip:%x",
 			cur->mac[0], cur->mac[1], cur->mac[2],
 			cur->mac[3], cur->mac[4], cur->mac[5],
+			cur->hostname, 
 			cur->nip
 		);
 		cur = cur->next;
@@ -340,6 +343,8 @@
 	char *line;
 	char *mac_string;
 	char *ip_string;
+	char *hostname;
+	char empty_string[] = "";
 	struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
 	uint32_t nip;
 
@@ -354,7 +359,11 @@
 	if (!ip_string || !udhcp_str2nip(ip_string, &nip))
 		return 0;
 
-	add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
+	/* optionnal hostname */
+	hostname = strtok_r(NULL, " \t", &line);
+	if (hostname==NULL) hostname = empty_string;
+
+	add_static_lease(arg, (uint8_t*) &mac_bytes, hostname, nip);
 
 	log_static_leases(arg);
 
@@ -616,6 +625,26 @@
 	udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
 }
 
+/* Add host name from static database */
+static void udhcp_add_hostname_option(struct dhcp_packet *packet, uint32_t nip)
+{
+	struct static_lease *st_lease ;
+	for ( st_lease = server_config.static_leases ;  
+              st_lease!=NULL && st_lease->nip != nip ; 
+              st_lease = st_lease->next );
+	if (st_lease!=NULL)
+	{
+		uint8_t storage [OPT_DATA + sizeof st_lease->hostname]; 
+		storage[OPT_CODE] = DHCP_HOST_NAME;
+		storage[OPT_LEN] = strlen (st_lease->hostname);
+		memcpy (& storage[OPT_DATA], st_lease->hostname, storage[OPT_LEN]);
+		udhcp_add_binary_option (packet, storage);
+		bb_error_msg("set option 0x%x to %s", DHCP_HOST_NAME, st_lease->hostname);
+	}
+
+} /* udhcp_add_hostname_option */
+
+
 /* Fill options field, siaddr_nip, and sname and boot_file fields.
  * TODO: teach this code to use overload option.
  */
@@ -635,6 +664,7 @@
 		strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
 	if (server_config.boot_file)
 		strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
+
 }
 
 static uint32_t select_lease_time(struct dhcp_packet *packet)
@@ -719,6 +749,7 @@
 
 	lease_time_sec = select_lease_time(oldpacket);
 	udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
+	udhcp_add_hostname_option(&packet, packet.yiaddr);
 	add_server_options(&packet);
 
 	addr.s_addr = packet.yiaddr;
@@ -751,6 +782,7 @@
 
 	lease_time_sec = select_lease_time(oldpacket);
 	udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
+	udhcp_add_hostname_option(&packet, packet.yiaddr);
 
 	add_server_options(&packet);
 
diff -ruN busybox-1.30.1/networking/udhcp/dhcpd.h busybox-1.30.2/networking/udhcp/dhcpd.h
--- busybox-1.30.1/networking/udhcp/dhcpd.h	2018-12-05 15:44:34.000000000 +0100
+++ busybox-1.30.2/networking/udhcp/dhcpd.h	2019-05-06 20:58:54.797068482 +0200
@@ -19,6 +19,7 @@
 	struct static_lease *next;
 	uint32_t nip;
 	uint8_t mac[6];
+	char hostname[20];
 };
 
 struct server_config_t {


More information about the busybox mailing list