]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/in-addr-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <arpa/inet.h>
24 #include "alloc-util.h"
25 #include "in-addr-util.h"
27 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
30 if (family
== AF_INET
)
31 return u
->in
.s_addr
== 0;
33 if (family
== AF_INET6
)
35 u
->in6
.s6_addr32
[0] == 0 &&
36 u
->in6
.s6_addr32
[1] == 0 &&
37 u
->in6
.s6_addr32
[2] == 0 &&
38 u
->in6
.s6_addr32
[3] == 0;
43 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
46 if (family
== AF_INET
)
47 return (be32toh(u
->in
.s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
49 if (family
== AF_INET6
)
50 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
55 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
58 if (family
== AF_INET
)
59 /* All of 127.x.x.x is localhost. */
60 return (be32toh(u
->in
.s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
62 if (family
== AF_INET6
)
63 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
68 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
72 if (family
== AF_INET
)
73 return a
->in
.s_addr
== b
->in
.s_addr
;
75 if (family
== AF_INET6
)
77 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
78 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
79 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
80 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
85 int in_addr_prefix_intersect(
87 const union in_addr_union
*a
,
89 const union in_addr_union
*b
,
90 unsigned bprefixlen
) {
97 /* Checks whether there are any addresses that are in both
100 m
= MIN(aprefixlen
, bprefixlen
);
102 if (family
== AF_INET
) {
105 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
106 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
108 return (x
& nm
) == 0;
111 if (family
== AF_INET6
) {
117 for (i
= 0; i
< 16; i
++) {
120 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
123 nm
= 0xFF << (8 - m
);
139 return -EAFNOSUPPORT
;
142 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
145 /* Increases the network part of an address by one. Returns
146 * positive it that succeeds, or 0 if this overflows. */
151 if (family
== AF_INET
) {
157 c
= be32toh(u
->in
.s_addr
);
158 n
= c
+ (1UL << (32 - prefixlen
));
161 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
163 u
->in
.s_addr
= htobe32(n
);
167 if (family
== AF_INET6
) {
168 struct in6_addr add
= {}, result
;
169 uint8_t overflow
= 0;
175 /* First calculate what we have to add */
176 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
178 for (i
= 16; i
> 0; i
--) {
181 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
182 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
192 return -EAFNOSUPPORT
;
195 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
202 if (family
== AF_INET
)
204 else if (family
== AF_INET6
)
205 l
= INET6_ADDRSTRLEN
;
207 return -EAFNOSUPPORT
;
214 if (!inet_ntop(family
, u
, x
, l
)) {
216 return errno
? -errno
: -EINVAL
;
223 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
228 if (!IN_SET(family
, AF_INET
, AF_INET6
))
229 return -EAFNOSUPPORT
;
232 if (inet_pton(family
, s
, ret
) <= 0)
233 return errno
? -errno
: -EINVAL
;
238 int in_addr_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
) {
245 r
= in_addr_from_string(AF_INET
, s
, ret
);
251 r
= in_addr_from_string(AF_INET6
, s
, ret
);
260 unsigned char in_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
263 return 32 - u32ctz(be32toh(addr
->s_addr
));
266 struct in_addr
* in_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
268 assert(prefixlen
<= 32);
270 /* Shifting beyond 32 is not defined, handle this specially. */
274 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
279 int in_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
280 uint8_t msb_octet
= *(uint8_t*) addr
;
282 /* addr may not be aligned, so make sure we only access it byte-wise */
288 /* class A, leading bits: 0 */
290 else if (msb_octet
< 192)
291 /* class B, leading bits 10 */
293 else if (msb_octet
< 224)
294 /* class C, leading bits 110 */
297 /* class D or E, no default prefixlen */
303 int in_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
304 unsigned char prefixlen
;
310 r
= in_addr_default_prefixlen(addr
, &prefixlen
);
314 in_addr_prefixlen_to_netmask(mask
, prefixlen
);
318 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
321 if (family
== AF_INET
) {
324 if (!in_addr_prefixlen_to_netmask(&mask
, prefixlen
))
327 addr
->in
.s_addr
&= mask
.s_addr
;
331 if (family
== AF_INET6
) {
334 for (i
= 0; i
< 16; i
++) {
337 if (prefixlen
>= 8) {
341 mask
= 0xFF << (8 - prefixlen
);
345 addr
->in6
.s6_addr
[i
] &= mask
;
351 return -EAFNOSUPPORT
;