From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 1D45A5A0275 for ; Mon, 29 Jan 2024 05:36:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1706502959; bh=DOngbb2twdR88X3i5eoK8zPEJm9fCT6+fX3vx9V1XJo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L7CXBaJvzZt8lNXzvtv9+r6KWZgAeZfzGeWFNhuazU8mIjmmZISWXJZ/+3M7iTQsr NN9SrMQW2eBI0pFJi1lZzAtVVkaq2chIR3QhrGzS59I5HOk8UDxx05LPV+cjGQ3Gpu lhDasj7tfNTPl2yLqqhEVxrWpwYWlBONjpfzHlhQi5LI8EgrVKoWx+61sfAMdPMYy/ ylsEbENcm4KFko/KMQ42+64jr42pAggqQ+NENMEqC/+m/5hj9EHwMn+mDHD7kEQXlz xL90sWO6TTaI6fUc48Ae/xQNAfh1Ijj98XSQIZgiqglKWHZCxWSgiKyU9aSs5JUj/o chMAWnkNB309w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TNb8b6Nx4z4x5m; Mon, 29 Jan 2024 15:35:59 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 11/16] inany: Add inany_ntop() helper Date: Mon, 29 Jan 2024 15:35:52 +1100 Message-ID: <20240129043557.823451-12-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240129043557.823451-1-david@gibson.dropbear.id.au> References: <20240129043557.823451-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: JSCO4JMNRKUKTBJMLFDSICPRPUU5MR6O X-Message-ID-Hash: JSCO4JMNRKUKTBJMLFDSICPRPUU5MR6O 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: Add this helper to format an inany into either IPv4 or IPv6 text format as appropriate. Signed-off-by: David Gibson --- Makefile | 6 +++--- inany.c | 35 +++++++++++++++++++++++++++++++++++ inany.h | 4 ++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 inany.c diff --git a/Makefile b/Makefile index af4fa87e..1c709229 100644 --- a/Makefile +++ b/Makefile @@ -45,9 +45,9 @@ 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 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 + igmp.c inany.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 00000000..edf0b055 --- /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 d758f7ba..d08d1f33 100644 --- a/inany.h +++ b/inany.h @@ -125,4 +125,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 */ -- 2.43.0