public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
00d44e0ff6dd383e29855f58c9ebedb0c9d4fe9d blob 3099 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
/* 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 <errno.h>

#include "util.h"
#include "ip.h"
#include "siphash.h"
#include "inany.h"
#include "fwd.h"

const union inany_addr inany_loopback4 = INANY_INIT4(IN4ADDR_LOOPBACK_INIT);
const union inany_addr inany_any4 = INANY_INIT4(IN4ADDR_ANY_INIT);

/** 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
 */
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);
}

/** 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;
}

/** inany_prefix_pton - Parse an IPv[46] address with prefix length adjustment
 * @src:	IPv[46] address string
 * @dst:	output buffer, filled with parsed address
 * @prefix_len:	pointer to prefix length
 *
 * Return: AF_INET for IPv4, AF_INET6 for IPv6, -1 on error
 */
int inany_prefix_pton(char *src, union inany_addr *dst, int *prefix_len)
{
	bool mapped = false;
	struct in6_addr a6;
	struct in_addr a4;
	char *slash;
	char *end;
	int af;

	*prefix_len = 0;

	/* Check for presence of /prefix_len suffix */
	slash = strchr(src, '/');
	if (slash)
		*slash = '\0';

	/* Read address */
	if (inet_pton(AF_INET, src, &a4)) {
		inany_from_af(dst, AF_INET, &a4);
		af = AF_INET;
	} else if (inet_pton(AF_INET6, src, &a6)) {
		inany_from_af(dst, AF_INET6, &a6);
		af = AF_INET6;
		if (inany_v4(dst))
			mapped = true;
	} else {
		memset(dst, 0, sizeof(*dst));
		return -1;
	}

	if (!slash)
		return mapped ? AF_INET : af;

	/* Read prefix_len - /0 is not allowed */
	errno = 0;
	*prefix_len = strtoul(slash + 1, &end, 10);
	if (errno || *end || *prefix_len == 0)
		return -1;

	if (mapped) {
		/* IPv4-mapped: prefix already in IPv6 format, must be 96-128 */
		if (*prefix_len < 96 || *prefix_len > 128)
			return -1;
		return AF_INET;
	}

	if (af == AF_INET) {
		/* Native IPv4: convert to IPv6 format */
		if (*prefix_len > 32)
			return -1;
		*prefix_len += 96;
		return AF_INET;
	}

	/* Native IPv6: keep as-is */
	if (*prefix_len > 128)
		return -1;
	return AF_INET6;
}
debug log:

solving 00d44e0 ...
found 00d44e0 in https://archives.passt.top/passt-dev/20260130214447.2540791-2-jmaloy@redhat.com/
found 7680439 in https://passt.top/passt
preparing index
index prepared:
100644 7680439d4df6fd7ba5bd88def2d92357be7edb3e	inany.c

applying [1/1] https://archives.passt.top/passt-dev/20260130214447.2540791-2-jmaloy@redhat.com/
diff --git a/inany.c b/inany.c
index 7680439..00d44e0 100644

Checking patch inany.c...
Applied patch inany.c cleanly.

index at:
100644 00d44e0ff6dd383e29855f58c9ebedb0c9d4fe9d	inany.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).