[PATCH 09/11] Rename parse_mac_addr and parse GUIDs

Daniel M. Weeks weeksd2 at rpi.edu
Fri Apr 17 04:00:24 AEST 2020


The parse_mac_addr function is renamed to better indicate it has been
simplified by way of two new common functions and now simply returns a
Windows-style DHCP client ID. It also returns a placeholder "client ID"
for 8-byte hardware addresses that will likely go unused in practice.

Signed-off-by: Daniel M. Weeks <weeksd2 at rpi.edu>
---
 discover/user-event.c | 43 ++++++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git a/discover/user-event.c b/discover/user-event.c
index 77d28c1..c1b1311 100644
--- a/discover/user-event.c
+++ b/discover/user-event.c
@@ -33,6 +33,7 @@
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
 
+#include <util/util.h>
 #include "device-handler.h"
 #include "resource.h"
 #include "event.h"
@@ -40,7 +41,7 @@
 #include "sysinfo.h"
 
 
-#define MAC_ADDR_SIZE	6
+#define MAX_ADDR_SIZE	8
 #define IP_ADDR_SIZE	4
 
 struct user_event {
@@ -218,18 +219,23 @@ static const char *parse_host_addr(struct event *event)
 	return NULL;
 }
 
-static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
+static char *generate_clientid(struct discover_context *ctx, uint8_t *hwaddr, size_t hwaddr_len)
 {
-	unsigned int mac_addr_arr[MAC_ADDR_SIZE];
-	char *mac_addr;
-
-	sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
-			mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
-			mac_addr_arr + 5);
-
-	mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
-			mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
-			mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
+	char *mac_addr = NULL;
+
+	if (hwaddr_len == 6) {
+		mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
+			hwaddr[0], hwaddr[1], hwaddr[2],
+			hwaddr[3], hwaddr[4], hwaddr[5]);
+	} else if (hwaddr_len == 8) {
+		/* this isn't actually a valid client ID for IB */
+		mac_addr = talloc_asprintf(ctx, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
+			hwaddr[0], hwaddr[1], hwaddr[2],
+			hwaddr[3], hwaddr[4], hwaddr[5],
+			hwaddr[6], hwaddr[7]);
+	} else {
+		pb_debug("unsupported hwaddr format\n");
+	}
 
 	return mac_addr;
 }
@@ -334,16 +340,19 @@ struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
 char **user_event_parse_conf_filenames(
 		struct discover_context *ctx, struct event *event)
 {
-	char *mac_addr, *ip_hex;
+	char *mac_addr = NULL, *ip_hex;
 	const char *mac, *ip;
 	char **filenames;
 	int index, len;
+	uint8_t hwaddr[MAX_ADDR_SIZE];
+	size_t hwaddr_len;
 
 	mac = event_get_param(event, "mac");
-	if (mac)
-		mac_addr = parse_mac_addr(ctx, mac);
-	else
-		mac_addr = NULL;
+	if (mac) {
+		mac = hwaddr_preprocess(mac, &hwaddr_len);
+		if (hwaddr_from_pretty_str(mac, hwaddr_len, hwaddr, sizeof(hwaddr)) == 0)
+			mac_addr = generate_clientid(ctx, hwaddr, hwaddr_len);
+	}
 
 	ip = event_get_param(event, "ip");
 	if (ip) {
-- 
Daniel M. Weeks


-- 
Daniel M. Weeks
Lead HPC Developer
Center for Computational Innovations
Rensselaer Polytechnic Institute
Troy, NY 12180
518-276-4458


More information about the Petitboot mailing list