]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3b653205 LP |
2 | #pragma once |
3 | ||
3b653205 | 4 | #include <netinet/in.h> |
11c3a366 TA |
5 | #include <stddef.h> |
6 | #include <sys/socket.h> | |
3b653205 | 7 | |
6c39e026 | 8 | #include "hash-funcs.h" |
3b653205 LP |
9 | #include "macro.h" |
10 | #include "util.h" | |
11 | ||
12 | union in_addr_union { | |
13 | struct in_addr in; | |
14 | struct in6_addr in6; | |
c7dfa1df | 15 | uint8_t bytes[CONST_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
3b653205 LP |
16 | }; |
17 | ||
97e5d693 LP |
18 | struct in_addr_data { |
19 | int family; | |
20 | union in_addr_union address; | |
21 | }; | |
22 | ||
34380032 | 23 | bool in4_addr_is_null(const struct in_addr *a); |
7653dcc3 YW |
24 | static inline bool in4_addr_is_set(const struct in_addr *a) { |
25 | return !in4_addr_is_null(a); | |
26 | } | |
27 | bool in6_addr_is_null(const struct in6_addr *a); | |
28 | static inline bool in6_addr_is_set(const struct in6_addr *a) { | |
29 | return !in6_addr_is_null(a); | |
30 | } | |
af93291c | 31 | int in_addr_is_null(int family, const union in_addr_union *u); |
7653dcc3 YW |
32 | static inline bool in_addr_is_set(int family, const union in_addr_union *u) { |
33 | return in_addr_is_null(family, u) == 0; | |
34 | } | |
35 | static inline int in_addr_data_is_null(const struct in_addr_data *a) { | |
36 | assert(a); | |
37 | return in_addr_is_null(a->family, &a->address); | |
38 | } | |
39 | static inline bool in_addr_data_is_set(const struct in_addr_data *a) { | |
40 | return in_addr_data_is_null(a); | |
41 | } | |
fdedbe26 | 42 | |
85257f48 SS |
43 | int in_addr_is_multicast(int family, const union in_addr_union *u); |
44 | ||
fdedbe26 | 45 | bool in4_addr_is_link_local(const struct in_addr *a); |
1235befa | 46 | bool in6_addr_is_link_local(const struct in6_addr *a); |
af93291c | 47 | int in_addr_is_link_local(int family, const union in_addr_union *u); |
5643cfc0 | 48 | bool in6_addr_is_link_local_all_nodes(const struct in6_addr *a); |
fdedbe26 LP |
49 | |
50 | bool in4_addr_is_localhost(const struct in_addr *a); | |
d830ebbd | 51 | int in_addr_is_localhost(int family, const union in_addr_union *u); |
b69bfa43 | 52 | int in_addr_is_localhost_one(int family, const union in_addr_union *u); |
fdedbe26 | 53 | |
ccea2448 | 54 | bool in4_addr_is_local_multicast(const struct in_addr *a); |
072320ea | 55 | bool in4_addr_is_non_local(const struct in_addr *a); |
c3e96088 | 56 | bool in6_addr_is_ipv4_mapped_address(const struct in6_addr *a); |
072320ea | 57 | |
9a897e22 | 58 | bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b); |
fa550434 | 59 | bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b); |
623a4c97 | 60 | int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b); |
0dd25fb9 LP |
61 | int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned aprefixlen, const union in_addr_union *b, unsigned bprefixlen); |
62 | int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen); | |
863b99cd | 63 | int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, uint64_t nth); |
c5236acd | 64 | int in_addr_random_prefix(int family, union in_addr_union *u, unsigned prefixlen_fixed_part, unsigned prefixlen); |
1534c579 YW |
65 | int in_addr_prefix_range( |
66 | int family, | |
67 | const union in_addr_union *in, | |
68 | unsigned prefixlen, | |
69 | union in_addr_union *ret_start, | |
70 | union in_addr_union *ret_end); | |
0dd25fb9 | 71 | int in_addr_to_string(int family, const union in_addr_union *u, char **ret); |
ae7b68db YW |
72 | static inline int in6_addr_to_string(const struct in6_addr *u, char **ret) { |
73 | return in_addr_to_string(AF_INET6, (const union in_addr_union*) u, ret); | |
74 | } | |
d9143dac | 75 | int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret); |
ae7b68db YW |
76 | static inline int in6_addr_prefix_to_string(const struct in6_addr *u, unsigned prefixlen, char **ret) { |
77 | return in_addr_prefix_to_string(AF_INET6, (const union in_addr_union*) u, prefixlen, ret); | |
78 | } | |
a723fb85 | 79 | int in_addr_port_ifindex_name_to_string(int family, const union in_addr_union *u, uint16_t port, int ifindex, const char *server_name, char **ret); |
c364cefb YW |
80 | static inline int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) { |
81 | return in_addr_port_ifindex_name_to_string(family, u, 0, ifindex, NULL, ret); | |
82 | } | |
c4fe6583 YW |
83 | static inline int in_addr_port_to_string(int family, const union in_addr_union *u, uint16_t port, char **ret) { |
84 | return in_addr_port_ifindex_name_to_string(family, u, port, 0, NULL, ret); | |
85 | } | |
0dd25fb9 | 86 | int in_addr_from_string(int family, const char *s, union in_addr_union *ret); |
4e2d5273 | 87 | int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret); |
5c3fa98d | 88 | |
5a941f5f LP |
89 | unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr); |
90 | struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen); | |
91 | int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen); | |
92 | int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask); | |
333f7d89 YW |
93 | int in4_addr_mask(struct in_addr *addr, unsigned char prefixlen); |
94 | int in6_addr_mask(struct in6_addr *addr, unsigned char prefixlen); | |
5a8bcb67 | 95 | int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen); |
e174b43f YW |
96 | int in4_addr_prefix_covers(const struct in_addr *prefix, unsigned char prefixlen, const struct in_addr *address); |
97 | int in6_addr_prefix_covers(const struct in6_addr *prefix, unsigned char prefixlen, const struct in6_addr *address); | |
1274b6c6 | 98 | int in_addr_prefix_covers(int family, const union in_addr_union *prefix, unsigned char prefixlen, const union in_addr_union *address); |
f4912f3a | 99 | int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret); |
9e0fdc21 YW |
100 | int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen); |
101 | ||
102 | typedef enum InAddrPrefixLenMode { | |
103 | PREFIXLEN_FULL, /* Default to prefixlen of address size, 32 for IPv4 or 128 for IPv6, if not specified. */ | |
104 | PREFIXLEN_REFUSE, /* Fail with -ENOANO if prefixlen is not specified. */ | |
105 | PREFIXLEN_LEGACY, /* Default to legacy default prefixlen calculation from address if not specified. */ | |
106 | } InAddrPrefixLenMode; | |
107 | ||
108 | int in_addr_prefix_from_string_auto_internal(const char *p, InAddrPrefixLenMode mode, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen); | |
a4798d4e | 109 | static inline int in_addr_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) { |
9e0fdc21 | 110 | return in_addr_prefix_from_string_auto_internal(p, PREFIXLEN_FULL, ret_family, ret_prefix, ret_prefixlen); |
a4798d4e | 111 | } |
3b653205 | 112 | |
9d485985 | 113 | static inline size_t FAMILY_ADDRESS_SIZE(int family) { |
3742095b | 114 | assert(IN_SET(family, AF_INET, AF_INET6)); |
9d485985 | 115 | return family == AF_INET6 ? 16 : 4; |
3b653205 | 116 | } |
5a8bcb67 | 117 | |
f9930401 YW |
118 | #define FAMILY_ADDRESS_SIZE_SAFE(f) \ |
119 | ({ \ | |
120 | int _f = (f); \ | |
121 | _f == AF_INET ? sizeof(struct in_addr) : \ | |
122 | _f == AF_INET6 ? sizeof(struct in6_addr) : 0; \ | |
123 | }) | |
124 | ||
adea26ee YW |
125 | /* Workaround for clang, explicitly specify the maximum-size element here. |
126 | * See also oss-fuzz#11344. */ | |
127 | #define IN_ADDR_NULL ((union in_addr_union) { .in6 = {} }) | |
6c39e026 | 128 | |
badd4928 YW |
129 | void in6_addr_hash_func(const struct in6_addr *addr, struct siphash *state); |
130 | int in6_addr_compare_func(const struct in6_addr *a, const struct in6_addr *b); | |
131 | ||
6c39e026 | 132 | extern const struct hash_ops in_addr_data_hash_ops; |
2968913e | 133 | extern const struct hash_ops in6_addr_hash_ops; |
f1cb8933 | 134 | extern const struct hash_ops in6_addr_hash_ops_free; |
28032398 YW |
135 | |
136 | #define IPV4_ADDRESS_FMT_STR "%u.%u.%u.%u" | |
137 | #define IPV4_ADDRESS_FMT_VAL(address) \ | |
138 | be32toh((address).s_addr) >> 24, \ | |
139 | (be32toh((address).s_addr) >> 16) & 0xFFu, \ | |
140 | (be32toh((address).s_addr) >> 8) & 0xFFu, \ | |
141 | be32toh((address).s_addr) & 0xFFu |