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=202408 header.b=i+PzCLCl; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id AA1125A0262 for ; Fri, 20 Sep 2024 06:12:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1726805566; bh=d9vr8AWA1cq2Q1vntNENIIj63KzjDVxkyF0EUF/fnvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i+PzCLCl+X81TJ3R3gCtmtRTv3lK1d2iTxN66dY50pIUIl0MXBBffRtCptNQkJzsk iguQuMiYgW1uAZRl6GmJGEG9IgtGTgktaL3a1AKPgtBbuWwz3i5O1Y3lZLkbIjV5dJ FHKUaFzn4RQkGRgDTgo30UtUFTRRHHJFuGVBZ51IVo0ZjDY2EeDJDL5QOXDingCznr xqHia7E0qXc7BUUEGbICNc2+MNzaLxuYtuPzEPLXt9fMsKFh+Jy8Q+JcDLgSIwBXuG gN9D1uP5H2bC0UIbu/6Sb0V+vRiA0uMr+82xNsavxbWuUfUEAwVe9Pvc0aLAbe8Gpd dqxL0eO4CXCYQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4X8zWL4T6gz4xRj; Fri, 20 Sep 2024 14:12:46 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 4/4] inany: Add inany_pton() helper Date: Fri, 20 Sep 2024 14:12:44 +1000 Message-ID: <20240920041244.593192-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240920041244.593192-1-david@gibson.dropbear.id.au> References: <20240920041244.593192-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: NC657PNBNGVMVDAD4I3G744NLO5U7DJS X-Message-ID-Hash: NC657PNBNGVMVDAD4I3G744NLO5U7DJS 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: We already have an inany_ntop() function to format inany addresses into text. Add inany_pton() to parse them from text, and use it in conf_ports(). Signed-off-by: David Gibson --- conf.c | 9 +-------- inany.c | 20 ++++++++++++++++++++ inany.h | 1 + 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/conf.c b/conf.c index 9f1cd835..6e62510d 100644 --- a/conf.c +++ b/conf.c @@ -215,9 +215,6 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, if (ifname == buf + 1) { /* Interface without address */ addr = NULL; } else { - struct in6_addr a6; - struct in_addr a4; - p = buf; /* Allow square brackets for IPv4 too for convenience */ @@ -226,11 +223,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, p++; } - if (inet_pton(AF_INET, p, &a4)) - inany_from_af(addr, AF_INET, &a4); - else if (inet_pton(AF_INET6, p, &a6)) - inany_from_af(addr, AF_INET6, &a6); - else + if (!inany_pton(p, addr)) goto bad; } } else { diff --git a/inany.c b/inany.c index 5e391dc7..f5483bfc 100644 --- a/inany.c +++ b/inany.c @@ -36,3 +36,23 @@ const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size) return inet_ntop(AF_INET6, &src->a6, dst, size); } + +/** inany_pton - Parse an IPv[46] address from text format + * @src: IPv[46] address + * @dst: output buffer, filled with parsed address + * + * Return: On success, 1, if no parseable address is found, 0 + */ +int inany_pton(const char *src, union inany_addr *dst) +{ + if (inet_pton(AF_INET, src, &dst->v4mapped.a4)) { + memset(&dst->v4mapped.zero, 0, sizeof(dst->v4mapped.zero)); + memset(&dst->v4mapped.one, 0xff, sizeof(dst->v4mapped.one)); + return 1; + } + + if (inet_pton(AF_INET6, src, &dst->a6)) + return 1; + + return 0; +} diff --git a/inany.h b/inany.h index d2893cec..6a12c292 100644 --- a/inany.h +++ b/inany.h @@ -270,5 +270,6 @@ static inline void inany_siphash_feed(struct siphash_state *state, #define INANY_ADDRSTRLEN MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size); +int inany_pton(const char *src, union inany_addr *dst); #endif /* INANY_H */ -- 2.46.1