blob: f1ac0f1551d2007b39b58f8fcd313888f088e915 [file] [log] [blame]
Paul Jakma57345092011-12-25 17:52:09 +01001/*
2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
6 *
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * This file incorporates work covered by the following copyright and
16 * permission notice:
17 *
18Copyright (c) 2007, 2008 by Juliusz Chroboczek
19Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
20
21Permission is hereby granted, free of charge, to any person obtaining a copy
22of this software and associated documentation files (the "Software"), to deal
23in the Software without restriction, including without limitation the rights
24to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25copies of the Software, and to permit persons to whom the Software is
26furnished to do so, subject to the following conditions:
27
28The above copyright notice and this permission notice shall be included in
29all copies or substantial portions of the Software.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37THE SOFTWARE.
38*/
39
40#include <stdlib.h>
41#include <stdarg.h>
42#include <string.h>
43#include <sys/time.h>
44#include <time.h>
45#include <stdio.h>
46#include <unistd.h>
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <arpa/inet.h>
52
53#include "babel_main.h"
54#include "babeld.h"
55#include "util.h"
56
57unsigned
58roughly(unsigned value)
59{
60 return value * 3 / 4 + random() % (value / 2);
61}
62
63/* d = s1 - s2 */
64void
65timeval_minus(struct timeval *d,
66 const struct timeval *s1, const struct timeval *s2)
67{
68 if(s1->tv_usec >= s2->tv_usec) {
69 d->tv_usec = s1->tv_usec - s2->tv_usec;
70 d->tv_sec = s1->tv_sec - s2->tv_sec;
71 } else {
72 d->tv_usec = s1->tv_usec + 1000000 - s2->tv_usec;
73 d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
74 }
75}
76
77unsigned
78timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
79{
80 if(s1->tv_sec < s2->tv_sec)
81 return 0;
82
83 /* Avoid overflow. */
84 if(s1->tv_sec - s2->tv_sec > 2000000)
85 return 2000000000;
86
87 if(s1->tv_sec > s2->tv_sec)
88 return
89 (unsigned)((unsigned)(s1->tv_sec - s2->tv_sec) * 1000 +
90 ((int)s1->tv_usec - s2->tv_usec) / 1000);
91
92 if(s1->tv_usec <= s2->tv_usec)
93 return 0;
94
95 return (unsigned)(s1->tv_usec - s2->tv_usec) / 1000u;
96}
97
98/* d = s + msecs */
99void
100timeval_add_msec(struct timeval *d, const struct timeval *s, const int msecs)
101{
102 int usecs;
103 d->tv_sec = s->tv_sec + msecs / 1000;
104 usecs = s->tv_usec + (msecs % 1000) * 1000;
105 if(usecs < 1000000) {
106 d->tv_usec = usecs;
107 } else {
108 d->tv_usec = usecs - 1000000;
109 d->tv_sec++;
110 }
111}
112
113void
114set_timeout(struct timeval *timeout, int msecs)
115{
116 timeval_add_msec(timeout, &babel_now, roughly(msecs));
117}
118
119/* returns <0 if "s1" < "s2", etc. */
120int
121timeval_compare(const struct timeval *s1, const struct timeval *s2)
122{
123 if(s1->tv_sec < s2->tv_sec)
124 return -1;
125 else if(s1->tv_sec > s2->tv_sec)
126 return 1;
127 else if(s1->tv_usec < s2->tv_usec)
128 return -1;
129 else if(s1->tv_usec > s2->tv_usec)
130 return 1;
131 else
132 return 0;
133}
134
135/* set d at min(d, s) */
136/* {0, 0} represents infinity */
137void
138timeval_min(struct timeval *d, const struct timeval *s)
139{
140 if(s->tv_sec == 0)
141 return;
142
143 if(d->tv_sec == 0 || timeval_compare(d, s) > 0) {
144 *d = *s;
145 }
146}
147
148/* set d to min(d, x) with x in [secs, secs+1] */
149void
150timeval_min_sec(struct timeval *d, time_t secs)
151{
152 if(d->tv_sec == 0 || d->tv_sec > secs) {
153 d->tv_sec = secs;
154 d->tv_usec = random() % 1000000;
155 }
156}
157
158/* parse a float value in second and return the corresponding mili-seconds.
159 For example:
160 parse_msec("12.342345") returns 12342 */
161int
162parse_msec(const char *string)
163{
164 unsigned int in, fl;
165 int i, j;
166
167 in = fl = 0;
168 i = 0;
169 while(string[i] == ' ' || string[i] == '\t')
170 i++;
171 while(string[i] >= '0' && string[i] <= '9') {
172 in = in * 10 + string[i] - '0';
173 i++;
174 }
175 if(string[i] == '.') {
176 i++;
177 j = 0;
178 while(string[i] >= '0' && string[i] <= '9') {
179 fl = fl * 10 + string[i] - '0';
180 i++;
181 j++;
182 }
183
184 while(j > 3) {
185 fl /= 10;
186 j--;
187 }
188 while(j < 3) {
189 fl *= 10;
190 j++;
191 }
192 }
193
194 while(string[i] == ' ' || string[i] == '\t')
195 i++;
196
197 if(string[i] == '\0')
198 return in * 1000 + fl;
199
200 return -1;
201}
202
203void
204do_debugf(const char *format, ...)
205{
206 va_list args;
207 va_start(args, format);
208 vfprintf(stderr, format, args);
209 fprintf(stderr, "\n");
210 fflush(stderr);
211 va_end(args);
212}
213
214int
215in_prefix(const unsigned char *restrict address,
216 const unsigned char *restrict prefix, unsigned char plen)
217{
218 unsigned char m;
219
220 if(plen > 128)
221 plen = 128;
222
223 if(memcmp(address, prefix, plen / 8) != 0)
224 return 0;
225
226 if(plen % 8 == 0)
227 return 1;
228
229 m = 0xFF << (8 - (plen % 8));
230
231 return ((address[plen / 8] & m) == (prefix[plen / 8] & m));
232}
233
234unsigned char *
235mask_prefix(unsigned char *restrict ret,
236 const unsigned char *restrict prefix, unsigned char plen)
237{
238 if(plen >= 128) {
239 memcpy(ret, prefix, 16);
240 return ret;
241 }
242
243 memset(ret, 0, 16);
244 memcpy(ret, prefix, plen / 8);
245 if(plen % 8 != 0)
246 ret[plen / 8] =
247 (prefix[plen / 8] & ((0xFF << (8 - (plen % 8))) & 0xFF));
248 return ret;
249}
250
251static const unsigned char v4prefix[16] =
252 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
253
254static const unsigned char llprefix[16] =
255 {0xFE, 0x80};
256
257const char *
258format_address(const unsigned char *address)
259{
260 static char buf[4][INET6_ADDRSTRLEN];
261 static int i = 0;
262 i = (i + 1) % 4;
263 if(v4mapped(address))
264 inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
265 else
266 inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
267 return buf[i];
268}
269
270const char *
271format_prefix(const unsigned char *prefix, unsigned char plen)
272{
273 static char buf[4][INET6_ADDRSTRLEN + 4];
274 static int i = 0;
275 int n;
276 i = (i + 1) % 4;
277 if(plen >= 96 && v4mapped(prefix)) {
278 inet_ntop(AF_INET, prefix + 12, buf[i], INET6_ADDRSTRLEN);
279 n = strlen(buf[i]);
280 snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen - 96);
281 } else {
282 inet_ntop(AF_INET6, prefix, buf[i], INET6_ADDRSTRLEN);
283 n = strlen(buf[i]);
284 snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen);
285 }
286 return buf[i];
287}
288
289const char *
290format_eui64(const unsigned char *eui)
291{
292 static char buf[4][28];
293 static int i = 0;
294 i = (i + 1) % 4;
295 snprintf(buf[i], 28, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
296 eui[0], eui[1], eui[2], eui[3],
297 eui[4], eui[5], eui[6], eui[7]);
298 return buf[i];
299}
300
301int
302parse_address(const char *address, unsigned char *addr_r, int *af_r)
303{
304 struct in_addr ina;
305 struct in6_addr ina6;
306 int rc;
307
308 rc = inet_pton(AF_INET, address, &ina);
309 if(rc > 0) {
310 memcpy(addr_r, v4prefix, 12);
311 memcpy(addr_r + 12, &ina, 4);
312 if(af_r) *af_r = AF_INET;
313 return 0;
314 }
315
316 rc = inet_pton(AF_INET6, address, &ina6);
317 if(rc > 0) {
318 memcpy(addr_r, &ina6, 16);
319 if(af_r) *af_r = AF_INET6;
320 return 0;
321 }
322
323 return -1;
324}
325
326int
327parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
328 int *af_r)
329{
330 char buf[INET6_ADDRSTRLEN];
331 char *slash, *end;
332 unsigned char prefix[16];
333 long plen;
334 int af;
335 struct in_addr ina;
336 struct in6_addr ina6;
337 int rc;
338
339 if(strcmp(net, "default") == 0) {
340 memset(prefix, 0, 16);
341 plen = 0;
342 } else {
343 slash = strchr(net, '/');
344 if(slash == NULL) {
345 rc = parse_address(net, prefix, &af);
346 if(rc < 0)
347 return rc;
348 plen = 128;
349 } else {
350 if(slash - net >= INET6_ADDRSTRLEN)
351 return -1;
352 memcpy(buf, net, slash - net);
353 buf[slash - net] = '\0';
354 rc = inet_pton(AF_INET, buf, &ina);
355 if(rc > 0) {
356 memcpy(prefix, v4prefix, 12);
357 memcpy(prefix + 12, &ina, 4);
358 plen = strtol(slash + 1, &end, 0);
359 if(*end != '\0' || plen < 0 || plen > 32)
360 return -1;
361 plen += 96;
362 af = AF_INET;
363 } else {
364 rc = inet_pton(AF_INET6, buf, &ina6);
365 if(rc > 0) {
366 memcpy(prefix, &ina6, 16);
367 plen = strtol(slash + 1, &end, 0);
368 if(*end != '\0' || plen < 0 || plen > 128)
369 return -1;
370 af = AF_INET6;
371 } else {
372 return -1;
373 }
374 }
375 }
376 }
377 mask_prefix(prefix_r, prefix, plen);
378 *plen_r = plen;
379 if(af_r) *af_r = af;
380 return 0;
381}
382
383int
384parse_eui64(const char *eui, unsigned char *eui_r)
385{
386 int n;
387 n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
388 &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
389 &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
390 if(n == 8)
391 return 0;
392
393 n = sscanf(eui, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
394 &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
395 &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
396 if(n == 8)
397 return 0;
398
399 n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
400 &eui_r[0], &eui_r[1], &eui_r[2],
401 &eui_r[5], &eui_r[6], &eui_r[7]);
402 if(n == 6) {
403 eui_r[3] = 0xFF;
404 eui_r[4] = 0xFE;
405 return 0;
406 }
407 return -1;
408}
409
410int
411wait_for_fd(int direction, int fd, int msecs)
412{
413 fd_set fds;
414 int rc;
415 struct timeval tv;
416
417 tv.tv_sec = msecs / 1000;
418 tv.tv_usec = (msecs % 1000) * 1000;
419
420 FD_ZERO(&fds);
421 FD_SET(fd, &fds);
422 if(direction)
423 rc = select(fd + 1, NULL, &fds, NULL, &tv);
424 else
425 rc = select(fd + 1, &fds, NULL, NULL, &tv);
426
427 return rc;
428}
429
430int
431martian_prefix(const unsigned char *prefix, int plen)
432{
433 return
434 (plen >= 8 && prefix[0] == 0xFF) ||
435 (plen >= 10 && prefix[0] == 0xFE && (prefix[1] & 0xC0) == 0x80) ||
436 (plen >= 128 && memcmp(prefix, zeroes, 15) == 0 &&
437 (prefix[15] == 0 || prefix[15] == 1)) ||
438 (plen >= 96 && v4mapped(prefix) &&
439 ((plen >= 104 && (prefix[12] == 127 || prefix[12] == 0)) ||
440 (plen >= 100 && (prefix[12] & 0xE0) == 0xE0)));
441}
442
443int
444linklocal(const unsigned char *address)
445{
446 return memcmp(address, llprefix, 8) == 0;
447}
448
449int
450v4mapped(const unsigned char *address)
451{
452 return memcmp(address, v4prefix, 12) == 0;
453}
454
455void
456v4tov6(unsigned char *dst, const unsigned char *src)
457{
458 memcpy(dst, v4prefix, 12);
459 memcpy(dst + 12, src, 4);
460}
461
462void
463inaddr_to_uchar(unsigned char *dest, const struct in_addr *src)
464{
465 memcpy(dest, v4prefix, 12);
466 memcpy(dest + 12, src, 4);
467 assert(v4mapped(dest));
468}
469
470void
471uchar_to_inaddr(struct in_addr *dest, const unsigned char *src)
472{
473 assert(v4mapped(src));
474 memcpy(dest, src + 12, 4);
475}
476
477void
478in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src)
479{
480 memcpy(dest, src, 16);
481}
482
483void
484uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src)
485{
486 memcpy(dest, src, 16);
487}
488
489int
490daemonise()
491{
492 int rc;
493
494 fflush(stdout);
495 fflush(stderr);
496
497 rc = fork();
498 if(rc < 0)
499 return -1;
500
501 if(rc > 0)
502 exit(0);
503
504 rc = setsid();
505 if(rc < 0)
506 return -1;
507
508 return 1;
509}