1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "in-addr-util.h"
13 #include "parse-util.h"
16 bool in4_addr_is_null(const struct in_addr
*a
) {
19 return a
->s_addr
== 0;
22 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
25 if (family
== AF_INET
)
26 return in4_addr_is_null(&u
->in
);
28 if (family
== AF_INET6
)
29 return IN6_IS_ADDR_UNSPECIFIED(&u
->in6
);
34 bool in4_addr_is_link_local(const struct in_addr
*a
) {
37 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
40 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
43 if (family
== AF_INET
)
44 return in4_addr_is_link_local(&u
->in
);
46 if (family
== AF_INET6
)
47 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
52 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
55 if (family
== AF_INET
)
56 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
58 if (family
== AF_INET6
)
59 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
64 bool in4_addr_is_localhost(const struct in_addr
*a
) {
67 /* All of 127.x.x.x is localhost. */
68 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
71 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
74 if (family
== AF_INET
)
75 return in4_addr_is_localhost(&u
->in
);
77 if (family
== AF_INET6
)
78 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
83 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
87 if (family
== AF_INET
)
88 return a
->in
.s_addr
== b
->in
.s_addr
;
90 if (family
== AF_INET6
)
92 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
93 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
94 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
95 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
100 int in_addr_prefix_intersect(
102 const union in_addr_union
*a
,
104 const union in_addr_union
*b
,
105 unsigned bprefixlen
) {
112 /* Checks whether there are any addresses that are in both
115 m
= MIN(aprefixlen
, bprefixlen
);
117 if (family
== AF_INET
) {
120 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
121 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
123 return (x
& nm
) == 0;
126 if (family
== AF_INET6
) {
132 for (i
= 0; i
< 16; i
++) {
135 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
138 nm
= 0xFF << (8 - m
);
154 return -EAFNOSUPPORT
;
157 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
160 /* Increases the network part of an address by one. Returns
161 * positive it that succeeds, or 0 if this overflows. */
166 if (family
== AF_INET
) {
172 c
= be32toh(u
->in
.s_addr
);
173 n
= c
+ (1UL << (32 - prefixlen
));
176 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
178 u
->in
.s_addr
= htobe32(n
);
182 if (family
== AF_INET6
) {
183 struct in6_addr add
= {}, result
;
184 uint8_t overflow
= 0;
190 /* First calculate what we have to add */
191 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
193 for (i
= 16; i
> 0; i
--) {
196 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
197 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
207 return -EAFNOSUPPORT
;
210 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
217 if (family
== AF_INET
)
219 else if (family
== AF_INET6
)
220 l
= INET6_ADDRSTRLEN
;
222 return -EAFNOSUPPORT
;
229 if (!inet_ntop(family
, u
, x
, l
)) {
231 return errno
> 0 ? -errno
: -EINVAL
;
238 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
246 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
247 * handle IPv6 link-local addresses. */
249 if (family
!= AF_INET6
)
254 r
= in_addr_is_link_local(family
, u
);
260 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
266 if (!inet_ntop(family
, u
, x
, l
)) {
268 return errno
> 0 ? -errno
: -EINVAL
;
271 sprintf(strchr(x
, 0), "%%%i", ifindex
);
277 return in_addr_to_string(family
, u
, ret
);
280 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
281 union in_addr_union buffer
;
284 if (!IN_SET(family
, AF_INET
, AF_INET6
))
285 return -EAFNOSUPPORT
;
288 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
289 return errno
> 0 ? -errno
: -EINVAL
;
294 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
299 r
= in_addr_from_string(AF_INET
, s
, ret
);
302 *ret_family
= AF_INET
;
306 r
= in_addr_from_string(AF_INET6
, s
, ret
);
309 *ret_family
= AF_INET6
;
316 int in_addr_ifindex_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
, int *ifindex
) {
324 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
325 * if one is found. */
327 suffix
= strchr(s
, '%');
331 /* If we shall return the interface index, try to parse it */
332 r
= parse_ifindex(suffix
+ 1, &ifi
);
336 u
= if_nametoindex(suffix
+ 1);
344 s
= strndupa(s
, suffix
- s
);
347 r
= in_addr_from_string_auto(s
, family
, ret
);
357 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
360 return 32 - u32ctz(be32toh(addr
->s_addr
));
363 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
365 assert(prefixlen
<= 32);
367 /* Shifting beyond 32 is not defined, handle this specially. */
371 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
376 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
377 uint8_t msb_octet
= *(uint8_t*) addr
;
379 /* addr may not be aligned, so make sure we only access it byte-wise */
385 /* class A, leading bits: 0 */
387 else if (msb_octet
< 192)
388 /* class B, leading bits 10 */
390 else if (msb_octet
< 224)
391 /* class C, leading bits 110 */
394 /* class D or E, no default prefixlen */
400 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
401 unsigned char prefixlen
;
407 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
411 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
415 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
418 if (family
== AF_INET
) {
421 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
424 addr
->in
.s_addr
&= mask
.s_addr
;
428 if (family
== AF_INET6
) {
431 for (i
= 0; i
< 16; i
++) {
434 if (prefixlen
>= 8) {
438 mask
= 0xFF << (8 - prefixlen
);
442 addr
->in6
.s6_addr
[i
] &= mask
;
448 return -EAFNOSUPPORT
;
451 int in_addr_prefix_covers(int family
,
452 const union in_addr_union
*prefix
,
453 unsigned char prefixlen
,
454 const union in_addr_union
*address
) {
456 union in_addr_union masked_prefix
, masked_address
;
462 masked_prefix
= *prefix
;
463 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
467 masked_address
= *address
;
468 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
472 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
475 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
479 if (!IN_SET(family
, AF_INET
, AF_INET6
))
480 return -EAFNOSUPPORT
;
482 r
= safe_atou8(p
, &u
);
486 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
493 int in_addr_prefix_from_string(
496 union in_addr_union
*ret_prefix
,
497 unsigned char *ret_prefixlen
) {
499 union in_addr_union buffer
;
506 if (!IN_SET(family
, AF_INET
, AF_INET6
))
507 return -EAFNOSUPPORT
;
511 l
= strndupa(p
, e
- p
);
515 r
= in_addr_from_string(family
, l
, &buffer
);
520 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
524 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
527 *ret_prefix
= buffer
;
534 int in_addr_prefix_from_string_auto(
537 union in_addr_union
*ret_prefix
,
538 unsigned char *ret_prefixlen
) {
540 union in_addr_union buffer
;
549 l
= strndupa(p
, e
- p
);
553 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
558 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
562 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
565 *ret_family
= family
;
567 *ret_prefix
= buffer
;
575 void in_addr_data_hash_func(const void *p
, struct siphash
*state
) {
576 const struct in_addr_data
*a
= p
;
578 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
579 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
582 int in_addr_data_compare_func(const void *a
, const void *b
) {
583 const struct in_addr_data
*x
= a
, *y
= b
;
586 r
= CMP(x
->family
, y
->family
);
590 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
593 const struct hash_ops in_addr_data_hash_ops
= {
594 .hash
= in_addr_data_hash_func
,
595 .compare
= in_addr_data_compare_func
,