public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob a677aa702b3ea4242484d823e26c873ad5c3c7dd 2869 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
 
/* SPDX-License-Identifier: AGPL-3.0-or-later
 * Copyright Red Hat
 * Author: David Gibson <david@gibson.dropbear.id.au>
 *
 * inany.h - Types and helpers for handling addresses which could be
 *           IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses)
 */

#include <assert.h>

/** union inany_addr - Represents either an IPv4 or IPv6 address
 * @a6:		Address as an IPv6 address, may be IPv4-mapped
 * @_v4._zero:	All zero-bits for an IPv4 address
 * @_v4._one:	All one-bits for an IPv4 address
 * @_v4.a4:	If @a6 is an IPv4 mapped address, this is the raw IPv4 address
 *
 * Fields starting with _ shouldn't be accessed except via helpers.
 */
union inany_addr {
	struct in6_addr a6;
	struct {
		uint8_t _zero[10];
		uint8_t _one[2];
		struct in_addr a4;
	} _v4mapped;
};

/** inany_v4 - Extract IPv4 address, if present, from IPv[46] address
 * @addr:	IPv4 or IPv6 address
 *
 * Return: IPv4 address if @addr is IPv4, NULL otherwise
 */
static inline struct in_addr *inany_v4(const union inany_addr *addr)
{
	if (!IN6_IS_ADDR_V4MAPPED(&addr->a6))
		return NULL;
	return (struct in_addr *)&addr->_v4mapped.a4;
}

/** inany_equals - Compare two IPv[46] addresses
 * @a, @b:	IPv[46] addresses
 *
 * Return: true if @a and @b are the same address
 */
static inline bool inany_equals(const union inany_addr *a,
				const union inany_addr *b)
{
	return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6);
}

/** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address
 * @aa:		Pointer to store IPv[46] address
 * @af:		Address family of @addr
 * @addr:	struct in_addr (IPv4) or struct in6_addr (IPv6)
 */
static inline void inany_from_af(union inany_addr *aa, int af, const void *addr)
{
	if (af == AF_INET6) {
		aa->a6 = *((struct in6_addr *)addr);
	} else if (af == AF_INET) {
		memset(&aa->_v4mapped._zero, 0, sizeof(aa->_v4mapped._zero));
		memset(&aa->_v4mapped._one, 0xff, sizeof(aa->_v4mapped._one));
		aa->_v4mapped.a4 = *((struct in_addr *)addr);
	} else {
		/* Not valid to call with other address families */
		assert(0);
	}
}

/** inany_from_sockaddr - Extract IPv[46] address and port number from sockaddr
 * @a:		Pointer to store IPv[46] address
 * @port:	Pointer to store port number, host order
 * @addr:	struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6)
 */
static inline void inany_from_sockaddr(union inany_addr *aa, in_port_t *port,
				       const void *sa_)
{
	const struct sockaddr *sa = (const struct sockaddr *)sa_;

	if (sa->sa_family == AF_INET6) {
		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;

		inany_from_af(aa, AF_INET6, &sa6->sin6_addr);
		*port = ntohs(sa6->sin6_port);
	} else if (sa->sa_family == AF_INET) {
		struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;

		inany_from_af(aa, AF_INET, &sa4->sin_addr);
		*port = ntohs(sa4->sin_port);
	} else {
		/* Not valid to call with other address families */
		assert(0);
	}
}

debug log:

solving a677aa7 ...
found a677aa7 in https://archives.passt.top/passt-dev/20221116044212.3876516-28-david@gibson.dropbear.id.au/
found 4e53da9 in https://archives.passt.top/passt-dev/20221116044212.3876516-20-david@gibson.dropbear.id.au/

applying [1/2] https://archives.passt.top/passt-dev/20221116044212.3876516-20-david@gibson.dropbear.id.au/
diff --git a/inany.h b/inany.h
new file mode 100644
index 0000000..4e53da9


applying [2/2] https://archives.passt.top/passt-dev/20221116044212.3876516-28-david@gibson.dropbear.id.au/
diff --git a/inany.h b/inany.h
index 4e53da9..a677aa7 100644

Checking patch inany.h...
Applied patch inany.h cleanly.
Checking patch inany.h...
Applied patch inany.h cleanly.

index at:
100644 a677aa702b3ea4242484d823e26c873ad5c3c7dd	inany.h

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).