]>
git.proxmox.com Git - 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
) & 0xFFFF0000) == (169U << 24 | 254U << 16);
49 if (family
== AF_INET6
)
50 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
55 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
59 if (family
== AF_INET
)
60 return a
->in
.s_addr
== b
->in
.s_addr
;
62 if (family
== AF_INET6
)
64 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
65 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
66 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
67 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
72 int in_addr_prefix_intersect(
74 const union in_addr_union
*a
,
76 const union in_addr_union
*b
,
77 unsigned bprefixlen
) {
84 /* Checks whether there are any addresses that are in both
87 m
= MIN(aprefixlen
, bprefixlen
);
89 if (family
== AF_INET
) {
92 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
93 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
98 if (family
== AF_INET6
) {
104 for (i
= 0; i
< 16; i
++) {
107 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
110 nm
= 0xFF << (8 - m
);
126 return -EAFNOSUPPORT
;
129 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
132 /* Increases the network part of an address by one. Returns
133 * positive it that succeeds, or 0 if this overflows. */
138 if (family
== AF_INET
) {
144 c
= be32toh(u
->in
.s_addr
);
145 n
= c
+ (1UL << (32 - prefixlen
));
148 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
150 u
->in
.s_addr
= htobe32(n
);
154 if (family
== AF_INET6
) {
155 struct in6_addr add
= {}, result
;
156 uint8_t overflow
= 0;
162 /* First calculate what we have to add */
163 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
165 for (i
= 16; i
> 0; i
--) {
168 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
169 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
179 return -EAFNOSUPPORT
;
182 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
189 if (family
== AF_INET
)
191 else if (family
== AF_INET6
)
192 l
= INET6_ADDRSTRLEN
;
194 return -EAFNOSUPPORT
;
201 if (!inet_ntop(family
, u
, x
, l
)) {
203 return errno
? -errno
: -EINVAL
;
210 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
215 if (!IN_SET(family
, AF_INET
, AF_INET6
))
216 return -EAFNOSUPPORT
;
219 if (inet_pton(family
, s
, ret
) <= 0)
220 return errno
? -errno
: -EINVAL
;
225 int in_addr_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
) {
232 r
= in_addr_from_string(AF_INET
, s
, ret
);
238 r
= in_addr_from_string(AF_INET6
, s
, ret
);
247 unsigned char in_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
250 return 32 - u32ctz(be32toh(addr
->s_addr
));
253 struct in_addr
* in_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
255 assert(prefixlen
<= 32);
257 /* Shifting beyond 32 is not defined, handle this specially. */
261 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
266 int in_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
267 uint8_t msb_octet
= *(uint8_t*) addr
;
269 /* addr may not be aligned, so make sure we only access it byte-wise */
275 /* class A, leading bits: 0 */
277 else if (msb_octet
< 192)
278 /* class B, leading bits 10 */
280 else if (msb_octet
< 224)
281 /* class C, leading bits 110 */
284 /* class D or E, no default prefixlen */
290 int in_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
291 unsigned char prefixlen
;
297 r
= in_addr_default_prefixlen(addr
, &prefixlen
);
301 in_addr_prefixlen_to_netmask(mask
, prefixlen
);
305 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
308 if (family
== AF_INET
) {
311 if (!in_addr_prefixlen_to_netmask(&mask
, prefixlen
))
314 addr
->in
.s_addr
&= mask
.s_addr
;
318 if (family
== AF_INET6
) {
321 for (i
= 0; i
< 16; i
++) {
324 if (prefixlen
>= 8) {
328 mask
= 0xFF << (8 - prefixlen
);
332 addr
->in6
.s6_addr
[i
] &= mask
;
338 return -EAFNOSUPPORT
;