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 "in-addr-util.h"
26 int in_addr_is_null(int family, const union in_addr_union *u) {
29 if (family == AF_INET)
30 return u->in.s_addr == 0;
32 if (family == AF_INET6)
34 u->in6.s6_addr32[0] == 0 &&
35 u->in6.s6_addr32[1] == 0 &&
36 u->in6.s6_addr32[2] == 0 &&
37 u->in6.s6_addr32[3] == 0;
42 int in_addr_is_link_local(int family, const union in_addr_union *u) {
45 if (family == AF_INET)
46 return (be32toh(u->in.s_addr) & 0xFFFF0000) == (169U << 24 | 254U << 16);
48 if (family == AF_INET6)
49 return IN6_IS_ADDR_LINKLOCAL(&u->in6);
54 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
58 if (family == AF_INET)
59 return a->in.s_addr == b->in.s_addr;
61 if (family == AF_INET6)
63 a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
64 a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
65 a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
66 a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
71 int in_addr_prefix_intersect(
73 const union in_addr_union *a,
75 const union in_addr_union *b,
76 unsigned bprefixlen) {
83 /* Checks whether there are any addresses that are in both
86 m = MIN(aprefixlen, bprefixlen);
88 if (family == AF_INET) {
91 x = be32toh(a->in.s_addr ^ b->in.s_addr);
92 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
97 if (family == AF_INET6) {
103 for (i = 0; i < 16; i++) {
106 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
109 nm = 0xFF << (8 - m);
125 return -EAFNOSUPPORT;
128 int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
131 /* Increases the network part of an address by one. Returns
132 * positive it that succeeds, or 0 if this overflows. */
137 if (family == AF_INET) {
143 c = be32toh(u->in.s_addr);
144 n = c + (1UL << (32 - prefixlen));
147 n &= 0xFFFFFFFFUL << (32 - prefixlen);
149 u->in.s_addr = htobe32(n);
153 if (family == AF_INET6) {
154 struct in6_addr add = {}, result;
155 uint8_t overflow = 0;
161 /* First calculate what we have to add */
162 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
164 for (i = 16; i > 0; i--) {
167 result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
168 overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
178 return -EAFNOSUPPORT;
181 int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
188 if (family == AF_INET)
190 else if (family == AF_INET6)
191 l = INET6_ADDRSTRLEN;
193 return -EAFNOSUPPORT;
200 if (!inet_ntop(family, u, x, l)) {
202 return errno ? -errno : -EINVAL;
209 int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
214 if (!IN_SET(family, AF_INET, AF_INET6))
215 return -EAFNOSUPPORT;
218 if (inet_pton(family, s, ret) <= 0)
219 return errno ? -errno : -EINVAL;
224 int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
231 r = in_addr_from_string(AF_INET, s, ret);
237 r = in_addr_from_string(AF_INET6, s, ret);
246 unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
249 return 32 - u32ctz(be32toh(addr->s_addr));
252 int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
253 uint8_t msb_octet = *(uint8_t*) addr;
255 /* addr may not be aligned, so make sure we only access it byte-wise */
261 /* class A, leading bits: 0 */
263 else if (msb_octet < 192)
264 /* class B, leading bits 10 */
266 else if (msb_octet < 224)
267 /* class C, leading bits 110 */
270 /* class D or E, no default prefixlen */
276 int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
277 unsigned char prefixlen;
283 r = in_addr_default_prefixlen(addr, &prefixlen);
287 assert(prefixlen > 0 && prefixlen < 32);
289 mask->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);