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_null(unsigned family, 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;
43 int in_addr_equal(unsigned family, union in_addr_union *a, union in_addr_union *b) {
47 if (family == AF_INET)
48 return a->in.s_addr == b->in.s_addr;
50 if (family == AF_INET6)
52 a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
53 a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
54 a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
55 a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
60 int in_addr_prefix_intersect(
62 const union in_addr_union *a,
64 const union in_addr_union *b,
65 unsigned bprefixlen) {
72 /* Checks whether there are any addresses that are in both
75 m = MIN(aprefixlen, bprefixlen);
77 if (family == AF_INET) {
80 x = be32toh(a->in.s_addr ^ b->in.s_addr);
81 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
86 if (family == AF_INET6) {
92 for (i = 0; i < 16; i++) {
95 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
114 return -EAFNOSUPPORT;
117 int in_addr_prefix_next(unsigned family, union in_addr_union *u, unsigned prefixlen) {
120 /* Increases the network part of an address by one. Returns
121 * positive it that succeeds, or 0 if this overflows. */
126 if (family == AF_INET) {
132 c = be32toh(u->in.s_addr);
133 n = c + (1UL << (32 - prefixlen));
136 n &= 0xFFFFFFFFUL << (32 - prefixlen);
138 u->in.s_addr = htobe32(n);
142 if (family == AF_INET6) {
143 struct in6_addr add = {}, result;
144 uint8_t overflow = 0;
150 /* First calculate what we have to add */
151 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
153 for (i = 16; i > 0; i--) {
156 result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
157 overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
167 return -EAFNOSUPPORT;
170 int in_addr_to_string(unsigned family, const union in_addr_union *u, char **ret) {
177 if (family == AF_INET)
179 else if (family == AF_INET6)
180 l = INET6_ADDRSTRLEN;
182 return -EAFNOSUPPORT;
189 if (!inet_ntop(family, u, x, l)) {
191 return errno ? -errno : -EINVAL;
198 int in_addr_from_string(unsigned family, const char *s, union in_addr_union *ret) {
203 if (!IN_SET(family, AF_INET, AF_INET6))
204 return -EAFNOSUPPORT;
207 if (inet_pton(family, s, ret) <= 0)
208 return errno ? -errno : -EINVAL;