diff mbox series

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

Message ID 9e7aba1f603262afc64e3d607c54e0220dc8e329.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 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@rpi.edu>
---
 lib/util/util.c | 25 +++++++++++++++++++++++++
 lib/util/util.h |  2 ++
 2 files changed, 27 insertions(+)
diff mbox series

Patch

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