]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/in-addr-util.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / basic / in-addr-util.c
CommitLineData
3b653205
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
20#include <arpa/inet.h>
11c3a366
TA
21#include <endian.h>
22#include <errno.h>
23#include <stdint.h>
24#include <stdlib.h>
3b653205 25
b5efdb8a 26#include "alloc-util.h"
3b653205 27#include "in-addr-util.h"
11c3a366
TA
28#include "macro.h"
29#include "util.h"
3b653205 30
af93291c 31int in_addr_is_null(int family, const union in_addr_union *u) {
3b653205
LP
32 assert(u);
33
34 if (family == AF_INET)
35 return u->in.s_addr == 0;
36
37 if (family == AF_INET6)
38 return
39 u->in6.s6_addr32[0] == 0 &&
40 u->in6.s6_addr32[1] == 0 &&
41 u->in6.s6_addr32[2] == 0 &&
42 u->in6.s6_addr32[3] == 0;
43
44 return -EAFNOSUPPORT;
45}
46
af93291c
LP
47int in_addr_is_link_local(int family, const union in_addr_union *u) {
48 assert(u);
49
50 if (family == AF_INET)
d830ebbd 51 return (be32toh(u->in.s_addr) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
af93291c
LP
52
53 if (family == AF_INET6)
54 return IN6_IS_ADDR_LINKLOCAL(&u->in6);
55
56 return -EAFNOSUPPORT;
57}
3b653205 58
d830ebbd
LP
59int in_addr_is_localhost(int family, const union in_addr_union *u) {
60 assert(u);
61
62 if (family == AF_INET)
63 /* All of 127.x.x.x is localhost. */
64 return (be32toh(u->in.s_addr) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
65
db15affc 66 if (family == AF_INET6)
d830ebbd
LP
67 return IN6_IS_ADDR_LOOPBACK(&u->in6);
68
69 return -EAFNOSUPPORT;
70}
71
623a4c97 72int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
3b653205
LP
73 assert(a);
74 assert(b);
75
76 if (family == AF_INET)
77 return a->in.s_addr == b->in.s_addr;
78
79 if (family == AF_INET6)
80 return
81 a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
82 a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
83 a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
84 a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
85
86 return -EAFNOSUPPORT;
87}
88
89int in_addr_prefix_intersect(
0dd25fb9 90 int family,
3b653205
LP
91 const union in_addr_union *a,
92 unsigned aprefixlen,
93 const union in_addr_union *b,
94 unsigned bprefixlen) {
95
96 unsigned m;
97
98 assert(a);
99 assert(b);
100
101 /* Checks whether there are any addresses that are in both
102 * networks */
103
104 m = MIN(aprefixlen, bprefixlen);
105
106 if (family == AF_INET) {
107 uint32_t x, nm;
108
109 x = be32toh(a->in.s_addr ^ b->in.s_addr);
110 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
111
112 return (x & nm) == 0;
113 }
114
115 if (family == AF_INET6) {
116 unsigned i;
117
118 if (m > 128)
119 m = 128;
120
121 for (i = 0; i < 16; i++) {
122 uint8_t x, nm;
123
124 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
125
126 if (m < 8)
127 nm = 0xFF << (8 - m);
128 else
129 nm = 0xFF;
130
131 if ((x & nm) != 0)
132 return 0;
133
134 if (m > 8)
135 m -= 8;
136 else
137 m = 0;
138 }
139
140 return 1;
141 }
142
143 return -EAFNOSUPPORT;
144}
145
0dd25fb9 146int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
3b653205
LP
147 assert(u);
148
149 /* Increases the network part of an address by one. Returns
150 * positive it that succeeds, or 0 if this overflows. */
151
152 if (prefixlen <= 0)
153 return 0;
154
155 if (family == AF_INET) {
156 uint32_t c, n;
157
158 if (prefixlen > 32)
159 prefixlen = 32;
160
161 c = be32toh(u->in.s_addr);
162 n = c + (1UL << (32 - prefixlen));
163 if (n < c)
164 return 0;
165 n &= 0xFFFFFFFFUL << (32 - prefixlen);
166
167 u->in.s_addr = htobe32(n);
168 return 1;
169 }
170
171 if (family == AF_INET6) {
172 struct in6_addr add = {}, result;
173 uint8_t overflow = 0;
174 unsigned i;
175
176 if (prefixlen > 128)
177 prefixlen = 128;
178
179 /* First calculate what we have to add */
180 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
181
182 for (i = 16; i > 0; i--) {
183 unsigned j = i - 1;
184
185 result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
186 overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
187 }
188
189 if (overflow)
190 return 0;
191
192 u->in6 = result;
193 return 1;
194 }
195
196 return -EAFNOSUPPORT;
197}
198
0dd25fb9 199int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
3b653205
LP
200 char *x;
201 size_t l;
202
203 assert(u);
204 assert(ret);
205
206 if (family == AF_INET)
207 l = INET_ADDRSTRLEN;
208 else if (family == AF_INET6)
209 l = INET6_ADDRSTRLEN;
210 else
211 return -EAFNOSUPPORT;
212
213 x = new(char, l);
214 if (!x)
215 return -ENOMEM;
216
217 errno = 0;
218 if (!inet_ntop(family, u, x, l)) {
219 free(x);
f5e5c28f 220 return errno > 0 ? -errno : -EINVAL;
3b653205
LP
221 }
222
223 *ret = x;
224 return 0;
225}
226
0dd25fb9 227int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
3b653205
LP
228
229 assert(s);
230 assert(ret);
231
232 if (!IN_SET(family, AF_INET, AF_INET6))
233 return -EAFNOSUPPORT;
234
235 errno = 0;
236 if (inet_pton(family, s, ret) <= 0)
f5e5c28f 237 return errno > 0 ? -errno : -EINVAL;
3b653205
LP
238
239 return 0;
240}
74b2466e 241
0dd25fb9 242int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
74b2466e
LP
243 int r;
244
245 assert(s);
246 assert(family);
247 assert(ret);
248
249 r = in_addr_from_string(AF_INET, s, ret);
250 if (r >= 0) {
251 *family = AF_INET;
252 return 0;
253 }
254
255 r = in_addr_from_string(AF_INET6, s, ret);
256 if (r >= 0) {
257 *family = AF_INET6;
258 return 0;
259 }
260
261 return -EINVAL;
262}
44e7b949 263
76917807 264unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
44e7b949
LP
265 assert(addr);
266
267 return 32 - u32ctz(be32toh(addr->s_addr));
268}
df40eee8 269
76917807
LP
270struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
271 assert(addr);
272 assert(prefixlen <= 32);
273
274 /* Shifting beyond 32 is not defined, handle this specially. */
275 if (prefixlen == 0)
276 addr->s_addr = 0;
277 else
278 addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
279
280 return addr;
281}
282
df40eee8 283int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
1caa12d0
TG
284 uint8_t msb_octet = *(uint8_t*) addr;
285
286 /* addr may not be aligned, so make sure we only access it byte-wise */
df40eee8
TG
287
288 assert(addr);
df40eee8
TG
289 assert(prefixlen);
290
1caa12d0 291 if (msb_octet < 128)
df40eee8
TG
292 /* class A, leading bits: 0 */
293 *prefixlen = 8;
1caa12d0 294 else if (msb_octet < 192)
df40eee8
TG
295 /* class B, leading bits 10 */
296 *prefixlen = 16;
1caa12d0 297 else if (msb_octet < 224)
df40eee8
TG
298 /* class C, leading bits 110 */
299 *prefixlen = 24;
300 else
301 /* class D or E, no default prefixlen */
302 return -ERANGE;
303
304 return 0;
305}
306
307int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
308 unsigned char prefixlen;
309 int r;
310
311 assert(addr);
312 assert(mask);
313
314 r = in_addr_default_prefixlen(addr, &prefixlen);
315 if (r < 0)
316 return r;
317
76917807 318 in_addr_prefixlen_to_netmask(mask, prefixlen);
df40eee8
TG
319 return 0;
320}
5a8bcb67
LP
321
322int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
323 assert(addr);
324
325 if (family == AF_INET) {
326 struct in_addr mask;
327
328 if (!in_addr_prefixlen_to_netmask(&mask, prefixlen))
329 return -EINVAL;
330
331 addr->in.s_addr &= mask.s_addr;
332 return 0;
333 }
334
335 if (family == AF_INET6) {
336 unsigned i;
337
338 for (i = 0; i < 16; i++) {
339 uint8_t mask;
340
341 if (prefixlen >= 8) {
342 mask = 0xFF;
343 prefixlen -= 8;
344 } else {
345 mask = 0xFF << (8 - prefixlen);
346 prefixlen = 0;
347 }
348
349 addr->in6.s6_addr[i] &= mask;
350 }
351
352 return 0;
353 }
354
355 return -EAFNOSUPPORT;
356}