]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/in-addr-util.c
2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
27 #include "alloc-util.h"
28 #include "in-addr-util.h"
30 #include "parse-util.h"
33 bool in4_addr_is_null(const struct in_addr
*a
) {
34 return a
->s_addr
== 0;
37 bool in6_addr_is_null(const struct in6_addr
*a
) {
39 a
->s6_addr32
[0] == 0 &&
40 a
->s6_addr32
[1] == 0 &&
41 a
->s6_addr32
[2] == 0 &&
45 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
48 if (family
== AF_INET
)
49 return in4_addr_is_null(&u
->in
);
51 if (family
== AF_INET6
)
52 return in6_addr_is_null(&u
->in6
);
57 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
60 if (family
== AF_INET
)
61 return (be32toh(u
->in
.s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
63 if (family
== AF_INET6
)
64 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
69 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
72 if (family
== AF_INET
)
73 /* All of 127.x.x.x is localhost. */
74 return (be32toh(u
->in
.s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
76 if (family
== AF_INET6
)
77 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
82 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
86 if (family
== AF_INET
)
87 return a
->in
.s_addr
== b
->in
.s_addr
;
89 if (family
== AF_INET6
)
91 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
92 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
93 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
94 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
99 int in_addr_prefix_intersect(
101 const union in_addr_union
*a
,
103 const union in_addr_union
*b
,
104 unsigned bprefixlen
) {
111 /* Checks whether there are any addresses that are in both
114 m
= MIN(aprefixlen
, bprefixlen
);
116 if (family
== AF_INET
) {
119 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
120 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
122 return (x
& nm
) == 0;
125 if (family
== AF_INET6
) {
131 for (i
= 0; i
< 16; i
++) {
134 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
137 nm
= 0xFF << (8 - m
);
153 return -EAFNOSUPPORT
;
156 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
159 /* Increases the network part of an address by one. Returns
160 * positive it that succeeds, or 0 if this overflows. */
165 if (family
== AF_INET
) {
171 c
= be32toh(u
->in
.s_addr
);
172 n
= c
+ (1UL << (32 - prefixlen
));
175 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
177 u
->in
.s_addr
= htobe32(n
);
181 if (family
== AF_INET6
) {
182 struct in6_addr add
= {}, result
;
183 uint8_t overflow
= 0;
189 /* First calculate what we have to add */
190 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
192 for (i
= 16; i
> 0; i
--) {
195 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
196 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
206 return -EAFNOSUPPORT
;
209 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
216 if (family
== AF_INET
)
218 else if (family
== AF_INET6
)
219 l
= INET6_ADDRSTRLEN
;
221 return -EAFNOSUPPORT
;
228 if (!inet_ntop(family
, u
, x
, l
)) {
230 return errno
> 0 ? -errno
: -EINVAL
;
237 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
245 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
246 * handle IPv6 link-local addresses. */
248 if (family
!= AF_INET6
)
253 r
= in_addr_is_link_local(family
, u
);
259 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
265 if (!inet_ntop(family
, u
, x
, l
)) {
267 return errno
> 0 ? -errno
: -EINVAL
;
270 sprintf(strchr(x
, 0), "%%%i", ifindex
);
276 return in_addr_to_string(family
, u
, ret
);
279 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
284 if (!IN_SET(family
, AF_INET
, AF_INET6
))
285 return -EAFNOSUPPORT
;
288 if (inet_pton(family
, s
, ret
) <= 0)
289 return errno
> 0 ? -errno
: -EINVAL
;
294 int in_addr_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
) {
301 r
= in_addr_from_string(AF_INET
, s
, ret
);
307 r
= in_addr_from_string(AF_INET6
, s
, ret
);
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 in_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
360 return 32 - u32ctz(be32toh(addr
->s_addr
));
363 struct in_addr
* in_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 in_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 in_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
401 unsigned char prefixlen
;
407 r
= in_addr_default_prefixlen(addr
, &prefixlen
);
411 in_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 (!in_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
;