radvd 1.6 update
[tomato.git] / release / src / router / radvd / util.c
blobde9c6a677af98b8d8ac7ecf0f0927e6e7a600794
1 /*
2 * $Id: util.c,v 1.10 2008/10/15 05:34:35 psavola Exp $
4 * Authors:
5 * Lars Fenneberg <lf@elemental.net>
7 * This software is Copyright 1996,1997 by the above mentioned author(s),
8 * All Rights Reserved.
10 * The license which is distributed with this software in the file COPYRIGHT
11 * applies to this software. If your distribution is missing this file, you
12 * may request it from <pekkas@netcore.fi>.
16 #include <config.h>
17 #include <includes.h>
18 #include <radvd.h>
20 void
21 mdelay(double msecs)
23 struct timeval tv;
25 tv.tv_sec = (time_t)(msecs / 1000.0);
26 tv.tv_usec = (suseconds_t)((msecs - tv.tv_sec * 1000.0) * 1000.0);
28 select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL, &tv);
31 double
32 rand_between(double lower, double upper)
34 return ((upper - lower) / (RAND_MAX + 1.0) * rand() + lower);
37 void
38 print_addr(struct in6_addr *addr, char *str)
40 const char *res;
42 /* XXX: overflows 'str' if it isn't big enough */
43 res = inet_ntop(AF_INET6, (void *)addr, str, INET6_ADDRSTRLEN);
45 if (res == NULL)
47 flog(LOG_ERR, "print_addr: inet_ntop: %s", strerror(errno));
48 strcpy(str, "[invalid address]");
52 /* Check if an in6_addr exists in the rdnss list */
53 int
54 check_rdnss_presence(struct AdvRDNSS *rdnss, struct in6_addr *addr)
56 while (rdnss) {
57 if ( !memcmp(&rdnss->AdvRDNSSAddr1, addr, sizeof(struct in6_addr))
58 || !memcmp(&rdnss->AdvRDNSSAddr2, addr, sizeof(struct in6_addr))
59 || !memcmp(&rdnss->AdvRDNSSAddr3, addr, sizeof(struct in6_addr)) )
60 break; /* rdnss address found in the list */
61 else
62 rdnss = rdnss->next; /* no match */
64 return (rdnss != NULL);
67 /* Like read(), but retries in case of partial read */
68 ssize_t
69 readn(int fd, void *buf, size_t count)
71 size_t n = 0;
72 while (count > 0) {
73 int r = read(fd, buf, count);
74 if (r < 0) {
75 if (errno == EINTR)
76 continue;
77 return r;
79 if (r == 0)
80 return n;
81 buf = (char *)buf + r;
82 count -= r;
83 n += r;
85 return n;
88 /* Like write(), but retries in case of partial write */
89 ssize_t
90 writen(int fd, const void *buf, size_t count)
92 size_t n = 0;
93 while (count > 0) {
94 int r = write(fd, buf, count);
95 if (r < 0) {
96 if (errno == EINTR)
97 continue;
98 return r;
100 if (r == 0)
101 return n;
102 buf = (const char *)buf + r;
103 count -= r;
104 n += r;
106 return n;