1
0
mirror of https://passt.top/passt synced 2024-09-28 18:15:49 +00:00

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>
This commit is contained in:
David Gibson 2024-09-20 14:12:44 +10:00 committed by Stefano Brivio
parent cbde4192ee
commit b55013b1a7
3 changed files with 22 additions and 8 deletions

9
conf.c
View File

@ -215,9 +215,6 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
if (ifname == buf + 1) { /* Interface without address */ if (ifname == buf + 1) { /* Interface without address */
addr = NULL; addr = NULL;
} else { } else {
struct in6_addr a6;
struct in_addr a4;
p = buf; p = buf;
/* Allow square brackets for IPv4 too for convenience */ /* 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++; p++;
} }
if (inet_pton(AF_INET, p, &a4)) if (!inany_pton(p, addr))
inany_from_af(addr, AF_INET, &a4);
else if (inet_pton(AF_INET6, p, &a6))
inany_from_af(addr, AF_INET6, &a6);
else
goto bad; goto bad;
} }
} else { } else {

20
inany.c
View File

@ -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); 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;
}

View File

@ -270,5 +270,6 @@ static inline void inany_siphash_feed(struct siphash_state *state,
#define INANY_ADDRSTRLEN MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) #define INANY_ADDRSTRLEN MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)
const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size); 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 */ #endif /* INANY_H */