public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH 0/1] Leftover parsing patch
@ 2026-08-19  5:26 David Gibson
  2026-08-19  5:26 ` [PATCH 1/1] parse: Convert parse_mac() to conventions of parse.c David Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: David Gibson @ 2026-08-19  5:26 UTC (permalink / raw)
  To: Stefano Brivio, passt-dev; +Cc: David Gibson

I'm not sure if this patch is particularly useful, however it was
already complete, left over from the earlier parsing rework.  So, I
thought I might as well post it in case it's interesting.

David Gibson (1):
  parse: Convert parse_mac() to conventions of parse.c

 Makefile |  1 +
 conf.c   | 43 ++++++++-----------------------------------
 parse.c  | 30 ++++++++++++++++++++++++++++++
 parse.h  |  1 +
 4 files changed, 40 insertions(+), 35 deletions(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 1/1] parse: Convert parse_mac() to conventions of parse.c
  2026-08-19  5:26 [PATCH 0/1] Leftover parsing patch David Gibson
@ 2026-08-19  5:26 ` David Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2026-08-19  5:26 UTC (permalink / raw)
  To: Stefano Brivio, passt-dev; +Cc: David Gibson

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-19  5:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-19  5:26 [PATCH 0/1] Leftover parsing patch David Gibson
2026-08-19  5:26 ` [PATCH 1/1] parse: Convert parse_mac() to conventions of parse.c David Gibson

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).