From 553ed8bbf31fbb431fe8b03347862d899a4d072e Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 19 Aug 2026 15:26:09 +1000 Subject: parse: Convert parse_mac() to conventions of parse.c 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 Signed-off-by: Stefano Brivio --- 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 b315242..f6c87b1 100644 --- a/Makefile +++ b/Makefile @@ -222,4 +222,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 0282f68..ea0f4bc 100644 --- a/conf.c +++ b/conf.c @@ -1116,39 +1116,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 @@ -1411,7 +1378,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) @@ -1678,7 +1648,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 44e9029..7c509df 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 257b9c5..c163547 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 */ -- cgit v1.2.3