diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2024-02-28 22:25:02 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2024-02-29 09:47:25 +0100 |
commit | 330b5db77d4c73b0fad0467e6268c08c4176312e (patch) | |
tree | 55d897e629d7cc5db374762b799ccb4fa5541976 /inany.c | |
parent | d31277e292281aa7e61d3785f56af1c956520786 (diff) | |
download | passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar.gz passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar.bz2 passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar.lz passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar.xz passt-330b5db77d4c73b0fad0467e6268c08c4176312e.tar.zst passt-330b5db77d4c73b0fad0467e6268c08c4176312e.zip |
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 <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'inany.c')
-rw-r--r-- | inany.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright Red Hat + * Author: David Gibson <david@gibson.dropbear.id.au> + * + * inany.c - Types and helpers for handling addresses which could be + * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) + */ + +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +#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); +} |