public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/1] parse: Convert parse_mac() to conventions of parse.c
Date: Wed, 19 Aug 2026 15:26:09 +1000	[thread overview]
Message-ID: <20260819052609.1177273-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260819052609.1177273-1-david@gibson.dropbear.id.au>

parse_mac() predates parse.c and doesn't obey the same conventions it now
uses.  Convert it to work similarly to the other functions in parse.c.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 Makefile |  1 +
 conf.c   | 43 ++++++++-----------------------------------
 parse.c  | 30 ++++++++++++++++++++++++++++++
 parse.h  |  1 +
 4 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/Makefile b/Makefile
index 5757aeff..c8839530 100644
--- a/Makefile
+++ b/Makefile
@@ -227,4 +227,5 @@ pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:ip.h
 pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:serialise.c
 pesto.cppcheck: CPPCHECK_FLAGS += --suppress=staticFunction:fwd_rule.c
 pesto.cppcheck: CPPCHECK_FLAGS += --suppress=staticFunction:parse.c
+pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:parse.c
 pesto.cppcheck: $(PESTO_SRCS) $(PESTO_HEADERS) seccomp_pesto.h
diff --git a/conf.c b/conf.c
index 2223604e..965006c4 100644
--- a/conf.c
+++ b/conf.c
@@ -1099,39 +1099,6 @@ static void conf_open_files(struct ctx *c)
 	}
 }
 
-/**
- * parse_mac() - Parse a MAC address from a string
- * @mac:	Binary MAC address, initialised on success
- * @str:	String to parse
- *
- * Parses @str as an Ethernet MAC address stored in @mac on success.  Exits on
- * failure.
- */
-static void parse_mac(unsigned char mac[ETH_ALEN], const char *str)
-{
-	size_t i;
-
-	if (strlen(str) != (ETH_ALEN * 3 - 1))
-		goto fail;
-
-	for (i = 0; i < ETH_ALEN; i++) {
-		const char *octet = str + 3 * i;
-		unsigned long b;
-		char *end;
-
-		errno = 0;
-		b = strtoul(octet, &end, 16);
-		if (b > UCHAR_MAX || errno || end != octet + 2 ||
-		    *end != ((i == ETH_ALEN - 1) ? '\0' : ':'))
-			goto fail;
-		mac[i] = b;
-	}
-	return;
-
-fail:
-	die("Invalid MAC address: %s", str);
-}
-
 /**
  * conf_sock_listen() - Start listening for connections on configuration socket
  * @c:		Execution context
@@ -1394,7 +1361,10 @@ void conf(struct ctx *c, int argc, char **argv)
 			if (c->mode != MODE_PASTA)
 				die("--ns-mac-addr is for pasta mode only");
 
-			parse_mac(c->guest_mac, optarg);
+			p = optarg;
+			if (!parse_mac(&p, c->guest_mac) ||
+			    !parse_eoi(p))
+				die("Invalid MAC address: %s", optarg);
 			break;
 		case 5:
 			if (c->mode != MODE_PASTA)
@@ -1661,7 +1631,10 @@ void conf(struct ctx *c, int argc, char **argv)
 			opt_n = c->ip4.prefix_len + 96;
 			break;
 		case 'M':
-			parse_mac(c->our_tap_mac, optarg);
+			p = optarg;
+			if (!parse_mac(&p, c->our_tap_mac) ||
+			    !parse_eoi(p))
+				die("Invalid MAC address: %s", optarg);
 			break;
 		case 'g':
 			if (inet_pton(AF_INET6, optarg, &c->ip6.guest_gw) &&
diff --git a/parse.c b/parse.c
index 44e90295..7c509df8 100644
--- a/parse.c
+++ b/parse.c
@@ -19,6 +19,7 @@
 #include <limits.h>
 #include <arpa/inet.h>
 #include <net/if.h>
+#include <netinet/if_ether.h>
 
 #include "common.h"
 #include "parse.h"
@@ -242,3 +243,32 @@ bool parse_ifspec(const char **cursor, char *ifname)
 	*cursor = p + len;
 	return true;
 }
+
+/**
+ * parse_mac() - Parse an (Ethernet) MAC address from a string
+ * @cursor:	Point to parse from, updated on success
+ * @mac:	On success, updated with binary MAC address
+ */
+bool parse_mac(const char **cursor, unsigned char *mac)
+{
+	unsigned char tmp[ETH_ALEN];
+	const char *p = *cursor;
+	size_t i;
+
+	for (i = 0; i < sizeof(tmp); i++) {
+		const char *octet = p;
+		unsigned long b;
+
+		if (!parse_unsigned(&p, 16, &b) || p != octet + 2 ||
+		    b > UCHAR_MAX)
+			return false;
+
+		if (i < ETH_ALEN - 1 && !parse_literal(&p, ":"))
+			return false;
+		tmp[i] = b;
+	}
+
+	memcpy(mac, tmp, sizeof(tmp));
+	*cursor = p;
+	return true;
+}
diff --git a/parse.h b/parse.h
index 257b9c58..c1635475 100644
--- a/parse.h
+++ b/parse.h
@@ -33,5 +33,6 @@ bool parse_inany_(const char **cursor, union inany_addr *addr,
 #define parse_inany(cursor, addr)	parse_inany_((cursor), (addr), NULL)
 
 bool parse_ifspec(const char **cursor, char *ifname);
+bool parse_mac(const char **cursor, unsigned char *mac);
 
 #endif /* _PARSE_H */
-- 
2.55.0


      reply	other threads:[~2026-08-19  5:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-19  5:26 [PATCH 0/1] Leftover parsing patch David Gibson
2026-08-19  5:26 ` David Gibson [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=20260819052609.1177273-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).