From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202608 header.b=aGbKvpJD; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 9D78E5A026D for ; Wed, 19 Aug 2026 07:26:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202608; t=1787117172; bh=eX69iUpHczBuLm/XOO+zQlsjqKYafgu39yTf3KxtMmo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aGbKvpJDcpOBO0qRMvczSA0ohb1DGiAEw0//WT2wBuLP+JsITRvYJzMua+RyiATmV D5aXD/GvbT1+Oqhuh4p1k7WytB/vuEMajJzQDgeYbl8C5BBaJHsqxZYF0qWZ/KyKHR TPtrlNWK/Edvqb0G8hqWQvCbaofS57MiALQkO40pjXLHxzmN6EA6eT5UsGomIVGTOK PVWQXw+HPDxTSU3X8FmpMxnkqo31VQpva9Or01/UZ4YS+JOnrYOc4NOV+W6rTPbZLX VdHzO+71FYricn4iyQcnlIKangnxETUY1kzzWXpCuAUF3NKeZmlwUZFaHn9pp3cRoT VkKXqDNA62siA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4hPw5w1twjz4w2d; Wed, 19 Aug 2026 15:26:12 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/1] parse: Convert parse_mac() to conventions of parse.c Date: Wed, 19 Aug 2026 15:26:09 +1000 Message-ID: <20260819052609.1177273-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260819052609.1177273-1-david@gibson.dropbear.id.au> References: <20260819052609.1177273-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: KVJM6DDYRQBEDFRX2EV4XXPXSVZ3JMJ7 X-Message-ID-Hash: KVJM6DDYRQBEDFRX2EV4XXPXSVZ3JMJ7 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 #include #include +#include #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