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 4/4] inany: Add inany_pton() helper
Date: Fri, 20 Sep 2024 14:12:44 +1000	[thread overview]
Message-ID: <20240920041244.593192-5-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240920041244.593192-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 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 */
-- 
@@ -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


  parent reply	other threads:[~2024-09-20  4:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-20  4:12 [PATCH 0/4] Use inany addresses through port forwarding configuration David Gibson
2024-09-20  4:12 ` [PATCH 1/4] udp: Don't attempt to get dual-stack sockets in nonsensical cases David Gibson
2024-09-20  4:12 ` [PATCH 2/4] util, pif: Replace sock_l4() with pif_sock_l4() David Gibson
2024-09-20  4:12 ` [PATCH 3/4] tcp, udp: Make {tcp,udp}_sock_init() take an inany address David Gibson
2024-09-20  4:12 ` David Gibson [this message]
2024-09-25 17:39 ` [PATCH 0/4] Use inany addresses through port forwarding configuration Stefano Brivio

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=20240920041244.593192-5-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).