From 330b5db77d4c73b0fad0467e6268c08c4176312e Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 28 Feb 2024 22:25:02 +1100 Subject: inany: Add inany_ntop() helper Add this helper to format an inany into either IPv4 or IPv6 text format as appropriate. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- Makefile | 4 ++-- inany.c | 35 +++++++++++++++++++++++++++++++++++ inany.h | 4 ++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 inany.c diff --git a/Makefile b/Makefile index 7b3a7ef..24cc255 100644 --- a/Makefile +++ b/Makefile @@ -45,8 +45,8 @@ FLAGS += -DVERSION=\"$(VERSION)\" FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS) PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c icmp.c \ - igmp.c iov.c isolation.c lineread.c log.c mld.c ndp.c netlink.c \ - packet.c passt.c pasta.c pcap.c pif.c port_fwd.c tap.c tcp.c \ + igmp.c inany.c iov.c isolation.c lineread.c log.c mld.c ndp.c \ + netlink.c packet.c passt.c pasta.c pcap.c pif.c port_fwd.c tap.c tcp.c \ tcp_splice.c udp.c util.c QRAP_SRCS = qrap.c SRCS = $(PASST_SRCS) $(QRAP_SRCS) diff --git a/inany.c b/inany.c new file mode 100644 index 0000000..edf0b05 --- /dev/null +++ b/inany.c @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright Red Hat + * Author: David Gibson + * + * inany.c - Types and helpers for handling addresses which could be + * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) + */ + +#include +#include +#include +#include +#include + +#include "util.h" +#include "siphash.h" +#include "inany.h" + +/** inany_ntop - Convert an IPv[46] address to text format + * @src: IPv[46] address + * @dst: output buffer, minimum INANY_ADDRSTRLEN bytes + * @size: size of buffer at @dst + * + * Return: On success, a non-null pointer to @dst, NULL on failure + */ +/* cppcheck-suppress unusedFunction */ +const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size) +{ + const struct in_addr *v4 = inany_v4(src); + + if (v4) + return inet_ntop(AF_INET, v4, dst, size); + + return inet_ntop(AF_INET6, &src->a6, dst, size); +} diff --git a/inany.h b/inany.h index d110380..be8b8da 100644 --- a/inany.h +++ b/inany.h @@ -162,4 +162,8 @@ static inline void inany_siphash_feed(struct siphash_state *state, siphash_feed(state, (uint64_t)aa->u32[2] << 32 | aa->u32[3]); } +#define INANY_ADDRSTRLEN MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + +const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size); + #endif /* INANY_H */ -- cgit v1.2.3