diff mbox series

[07/11] New function to preprocess hardware address strings

Message ID ac9860c8648452cb5f5fc1d433e9e2958430a971.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, 5:59 p.m. UTC
This function will take a long hardware address, and identify the
beginning of the local, usable hardware address and, optionally, its
length. Since Infiniband IP over IB hardware addresses begin with 12
bytes of addressing data that can change and is not representative of
the local hardware, it is removed.

Signed-off-by: Daniel M. Weeks <weeksd2@rpi.edu>
---
 lib/util/util.c | 22 ++++++++++++++++++++++
 lib/util/util.h |  2 ++
 2 files changed, 24 insertions(+)
diff mbox series

Patch

diff --git a/lib/util/util.c b/lib/util/util.c
index e672d19..c965cc4 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -18,6 +18,7 @@ 
 #include <string.h>
 #include <assert.h>
 
+#include <log/log.h>
 #include <util/util.h>
 
 static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
@@ -60,3 +61,24 @@  int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hw
 
 	return memcmp(hwaddr1, hwaddr2, hwaddr1_len);
 }
+
+/* takes a pretty hardware address and returns the point to begin processing
+ * and a length or NULL if the length is unrecognized */
+const char *hwaddr_preprocess(const char *mac, size_t *length)
+{
+	if (!mac)
+		return NULL;
+
+	if (strlen(mac) == 17) { /* a pretty MAC address */
+		if (length)
+			*length = 6;
+		return mac;
+	} else if (strlen(mac) == 59) { /* a pretty IB address */
+		if (length)
+			*length = 8;
+		return mac+36;
+	} else {
+		pb_debug("unsupported hwaddr format (length %ld)\n", strlen(mac));
+		return NULL;
+	}
+}
diff --git a/lib/util/util.h b/lib/util/util.h
index 416e1af..d9cc46b 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -54,5 +54,7 @@  void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen);
 
 int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hwaddr2_len);
 
+const char *hwaddr_preprocess(const char *mac, size_t *length);
+
 #endif /* UTIL_H */