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 763AC5A0272 for ; Tue, 6 Feb 2024 02:17:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707182259; bh=syJorEEOx6wdBJM6mDOic7u6sIXwK3QQ/pe3AURio50=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IVioWhe0waKIx/2O29bnpGZQHwvrVYfqAu6tVmI6iPpT/SE1V31SMwj5V1sez7yXM EPSYT7vdQFKu4T8sRCGmqw/AiH7G4xbjH6nrU5q4nTIhKnvqskU02Vez3DT0f7gtG2 x1MK+j3fTK6RzIi4mp5/NjvTPcp2jJzMZrGdiTejV+FtSBizVRLH/+HdcZVo0xvqHy JMx1ur1VTnahWjOrFRed9DXT1qE5lK+1miOrGCt4sI5X5r4lP9tnlGLHC0BOHnr5Qk Y0H8j+IWQB8scX3BYs6HTGjUTnbp2o1jTfA601bm57M+qVMj27sIfLuMgmattNU8dH DmZseI/AYs66w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TTQN3714fz4wd0; Tue, 6 Feb 2024 12:17:39 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 03/22] inany: Add inany_ntop() helper Date: Tue, 6 Feb 2024 12:17:15 +1100 Message-ID: <20240206011734.884138-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206011734.884138-1-david@gibson.dropbear.id.au> References: <20240206011734.884138-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: OFXSQPXFUIDVEENNIHIXU5EZJJFR2WVU X-Message-ID-Hash: OFXSQPXFUIDVEENNIHIXU5EZJJFR2WVU 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 2058f145..bf731166 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 */ -- 2.43.0