diff mbox series

[08/11] New function to compare hardware addresses

Message ID d37a39c6eb182ffa678ee97f5f4534d0f26cea30.1587059210.git.weeksd2@rpi.edu
State New
Headers show
Series IB netboot 1/3: flexible hwaddrs | expand

Commit Message

Daniel M. Weeks April 16, 2020, 5:55 p.m. UTC
This simplifies comparing hardware address buffers when they may be of
different lengths.

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

Patch

diff --git a/lib/util/util.c b/lib/util/util.c
index a18926c..e672d19 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -47,3 +47,16 @@  void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen)
 
 	return;
 }
+
+int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hwaddr2_len)
+{
+	/* emulate memcmp even though it's unnecessary */
+
+	if (hwaddr1_len > hwaddr2_len)
+		return 1;
+
+	if (hwaddr1_len < hwaddr2_len)
+		return -1;
+
+	return memcmp(hwaddr1, hwaddr2, hwaddr1_len);
+}
diff --git a/lib/util/util.h b/lib/util/util.h
index 39966d0..416e1af 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -18,6 +18,7 @@ 
 #ifndef UTIL_H
 #define UTIL_H
 
+#include <stddef.h>
 #include <stdint.h>
 
 #ifndef container_of
@@ -51,5 +52,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);
+
 #endif /* UTIL_H */