Merge branch 'tomato-ND-USBmod' into tomato-RT
[tomato.git] / release / src / router / radvd / util.c
blob6c7ad029f369936f20f754db9340fcea01233ce9
1 /*
2 * $Id: util.c,v 1.12 2010/12/14 11:58:21 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 /* Check if a suffix exists in the dnssl list */
68 int
69 check_dnssl_presence(struct AdvDNSSL *dnssl, const char *suffix)
71 int i;
72 while (dnssl) {
73 for (i = 0; i < dnssl->AdvDNSSLNumber; i++) {
74 if (strcmp(dnssl->AdvDNSSLSuffixes[i], suffix) == 0)
75 break; /* suffix found in the list */
77 if (i != dnssl->AdvDNSSLNumber)
78 break;
80 dnssl = dnssl->next; /* no match */
82 return (dnssl != NULL);
85 /* Like read(), but retries in case of partial read */
86 ssize_t
87 readn(int fd, void *buf, size_t count)
89 size_t n = 0;
90 while (count > 0) {
91 int r = read(fd, buf, count);
92 if (r < 0) {
93 if (errno == EINTR)
94 continue;
95 return r;
97 if (r == 0)
98 return n;
99 buf = (char *)buf + r;
100 count -= r;
101 n += r;
103 return n;
106 /* Like write(), but retries in case of partial write */
107 ssize_t
108 writen(int fd, const void *buf, size_t count)
110 size_t n = 0;
111 while (count > 0) {
112 int r = write(fd, buf, count);
113 if (r < 0) {
114 if (errno == EINTR)
115 continue;
116 return r;
118 if (r == 0)
119 return n;
120 buf = (const char *)buf + r;
121 count -= r;
122 n += r;
124 return n;