diff mbox series

[10/11] Parse GUIDs in DHCP user events

Message ID e1b6cc7e05c97869eeaeb0fa6db732bcc4eab566.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
Use new, common functions to preprocess and transform hardware addresses
to strip any non-local address information.

Signed-off-by: Daniel M. Weeks <weeksd2@rpi.edu>
---
 discover/device-handler.c |  2 ++
 discover/user-event.c     | 25 +++++++++++++++----------
 2 files changed, 17 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 4dfe537..da65125 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -27,6 +27,7 @@ 
 #include <netdb.h>
 #include <arpa/inet.h>
 
+#include <util/util.h>
 #include "device-handler.h"
 #include "discover-server.h"
 #include "devmapper.h"
@@ -1491,6 +1492,7 @@  static void process_url_cb(struct load_url_result *result, void *data)
 	}
 
 	mac = event_get_param(event, "mac");
+	mac = hwaddr_preprocess(mac, NULL);
 	char *url = talloc_asprintf(event, "file://%s", result->local);
 	event_set_param(event, "pxeconffile-local", url);
 
diff --git a/discover/user-event.c b/discover/user-event.c
index c1b1311..0263423 100644
--- a/discover/user-event.c
+++ b/discover/user-event.c
@@ -396,18 +396,20 @@  static int user_event_dhcp(struct user_event *uev, struct event *event)
 {
 	struct device_handler *handler = uev->handler;
 	struct discover_device *dev;
+	const char *mac;
 
-	uint8_t hwaddr[MAC_ADDR_SIZE];
+	uint8_t hwaddr[MAX_ADDR_SIZE];
+	size_t hwaddr_len;
 
-	sscanf(event_get_param(event, "mac"),
-	       "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
-	       hwaddr, hwaddr + 1, hwaddr + 2,
-	       hwaddr + 3, hwaddr + 4, hwaddr + 5);
+	mac = event_get_param(event, "mac");
+	mac = hwaddr_preprocess(mac, &hwaddr_len);
+	if (hwaddr_from_pretty_str(mac, hwaddr_len, hwaddr, MAX_ADDR_SIZE) < 0)
+		return -1;
 
-	system_info_set_interface_address(sizeof(hwaddr), hwaddr,
+	system_info_set_interface_address(hwaddr_len, hwaddr,
 					  event_get_param(event, "ip"));
 
-	dev = discover_device_create(handler, event_get_param(event, "mac"),
+	dev = discover_device_create(handler, mac,
 					event->device);
 
 	device_handler_dhcp(handler, dev, event);
@@ -420,10 +422,12 @@  static int user_event_add(struct user_event *uev, struct event *event)
 	struct device_handler *handler = uev->handler;
 	struct discover_context *ctx;
 	struct discover_device *dev;
+	const char *mac;
 
 	/* In case this is a network interface, try to refer to it by UUID */
-	dev = discover_device_create(handler, event_get_param(event, "mac"),
-					event->device);
+	mac = event_get_param(event, "mac");
+	mac = hwaddr_preprocess(mac, NULL);
+	dev = discover_device_create(handler, mac, event->device);
 	dev->device->id = talloc_strdup(dev, event->device);
 	ctx = device_handler_discover_context_create(handler, dev);
 
@@ -441,9 +445,10 @@  static int user_event_remove(struct user_event *uev, struct event *event)
 	struct device_handler *handler = uev->handler;
 	struct discover_device *dev;
 	const char *mac = event_get_param(event, "mac");
+	mac = hwaddr_preprocess(mac, NULL);
 
 	if (mac)
-		dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
+		dev = device_lookup_by_uuid(handler, mac);
 	else
 		dev = device_lookup_by_id(handler, event->device);