diff mbox series

[09/11] Rename parse_mac_addr and parse GUIDs

Message ID 87ebb71bf5e442cede33547e8d9aa7b2e0961dae.1587059235.git.weeksd2@rpi.edu
State New
Headers show
Series IB netboot 2/3: longer hwaddr/guid support | expand

Commit Message

Daniel M. Weeks April 16, 2020, 6 p.m. UTC
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@rpi.edu>
---
 discover/user-event.c | 43 ++++++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 17 deletions(-)
diff mbox series

Patch

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) {