aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-09-20 14:12:44 +1000
committerStefano Brivio <sbrivio@redhat.com>2024-09-25 19:03:17 +0200
commitb55013b1a7e7dd7e4e90455703d272b9ffc28b64 (patch)
tree3c36d9ee1f4f96d6954067237e8f3330d8e33569
parentcbde4192eeef7a5640aea6dd84d5eac02841ef5c (diff)
downloadpasst-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar.gz
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar.bz2
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar.lz
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar.xz
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.tar.zst
passt-b55013b1a7e7dd7e4e90455703d272b9ffc28b64.zip
inany: Add inany_pton() helper
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>
-rw-r--r--conf.c9
-rw-r--r--inany.c20
-rw-r--r--inany.h1
3 files changed, 22 insertions, 8 deletions
diff --git a/conf.c b/conf.c
index 9f1cd83..6e62510 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 5e391dc..f5483bf 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 d2893ce..6a12c29 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 */