[PATCH 4/4] net: implement ethaddr_string_cmp()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



We keep ethernet addresses either in binary or in text form at a couple
of places, e.g. device tree, device parameters, struct net_device,
... etc. We have memcmp() and strcmp() respectively to compare each, add
one more function to compare a text string with binary. This will be
used in a follow up commit.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
 include/net.h |  2 ++
 net/lib.c     | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/include/net.h b/include/net.h
index d9b7953a3c33..dbc45b806a51 100644
--- a/include/net.h
+++ b/include/net.h
@@ -366,6 +366,8 @@ IPaddr_t getenv_ip(const char *name);
 int setenv_ip(const char *name, IPaddr_t ip);
 
 int string_to_ethaddr(const char *str, u8 enetaddr[6]);
+void ethaddr_to_string(const u8 enetaddr[6], char *str);
+int ethaddr_string_cmp(const u8 enetaddr_a[6], const char *str_b);
 
 #ifdef CONFIG_NET_RESOLV
 int resolv(const char *host, IPaddr_t *ip);
diff --git a/net/lib.c b/net/lib.c
index dc6e138f392c..59bd4c280caf 100644
--- a/net/lib.c
+++ b/net/lib.c
@@ -37,6 +37,23 @@ int string_to_ethaddr(const char *str, u8 enetaddr[ETH_ALEN])
 	return 0;
 }
 
+void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
+{
+	sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+		 enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3],
+		 enetaddr[4], enetaddr[5]);
+}
+
+int ethaddr_string_cmp(const u8 enetaddr_a[ETH_ALEN], const char *str_b)
+{
+	u8 enetaddr_b[ETH_ALEN];
+
+	if (string_to_ethaddr(str_b, enetaddr_b))
+		return -EINVAL;
+
+	return memcmp(enetaddr_a, enetaddr_b, ETH_ALEN);
+}
+
 int string_to_ip(const char *s, IPaddr_t *ip)
 {
 	IPaddr_t addr = 0;
-- 
2.39.5





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux