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

Daniel M. Weeks weeksd2 at rpi.edu
Fri Apr 17 03:59:36 AEST 2020


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 at rpi.edu>
---
 lib/util/util.c | 22 ++++++++++++++++++++++
 lib/util/util.h |  2 ++
 2 files changed, 24 insertions(+)

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 */
 
-- 
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