[PATCH 08/11] New function to convert pretty hwaddr to raw

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


This function extracts a method used in several places to convert a
pretty hardware address back to raw bytes. It also includes support for
an 8-byte hardware address.

Signed-off-by: Daniel M. Weeks <weeksd2 at rpi.edu>
---
 lib/util/util.c | 25 +++++++++++++++++++++++++
 lib/util/util.h |  2 ++
 2 files changed, 27 insertions(+)

diff --git a/lib/util/util.c b/lib/util/util.c
index c965cc4..7f70084 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -15,6 +15,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <assert.h>
 
@@ -82,3 +83,27 @@ const char *hwaddr_preprocess(const char *mac, size_t *length)
 		return NULL;
 	}
 }
+
+int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size)
+{
+	if (hwaddr_size < hwaddr_len)
+		return -1;
+
+	if (hwaddr_len == 6) {
+		sscanf(hwaddr_str,
+		       "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
+		       hwaddr, hwaddr + 1, hwaddr + 2,
+		       hwaddr + 3, hwaddr + 4, hwaddr + 5);
+	} else if (hwaddr_len == 8) {
+		sscanf(hwaddr_str,
+		       "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
+		       hwaddr, hwaddr + 1, hwaddr + 2,
+		       hwaddr + 3, hwaddr + 4, hwaddr + 5,
+		       hwaddr + 6, hwaddr + 7);
+	} else {
+		pb_debug("unsupported hwaddr format\n");
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/lib/util/util.h b/lib/util/util.h
index d9cc46b..bbb6144 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -56,5 +56,7 @@ int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hw
 
 const char *hwaddr_preprocess(const char *mac, size_t *length);
 
+int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size);
+
 #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