mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] net: implement ethaddr_string_cmp()
Date: Tue, 26 Nov 2024 16:17:44 +0100	[thread overview]
Message-ID: <20241126151744.3621717-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20241126151744.3621717-1-a.fatoum@pengutronix.de>

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@pengutronix.de>
---
 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




      parent reply	other threads:[~2024-11-26 15:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-26 15:17 [PATCH 0/4] net: ethernet address helpers for board code Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 1/4] net: add ethaddr sequence handling Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 2/4] net: factor out eth_of_get_fixup_node Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 3/4] net: export list of registered ethernet addresses Ahmad Fatoum
2024-11-26 15:17 ` Ahmad Fatoum [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241126151744.3621717-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox