Don't enable dnsmasq IPSET functionality on 2.4 kernel
[tomato.git] / release / src / router / radvd / util.c
blob8f3a68a8a72805dc8cdb613aad98d83222474daa
1 /*
3 * Authors:
4 * Lars Fenneberg <lf@elemental.net>
6 * This software is Copyright 1996,1997 by the above mentioned author(s),
7 * All Rights Reserved.
9 * The license which is distributed with this software in the file COPYRIGHT
10 * applies to this software. If your distribution is missing this file, you
11 * may request it from <pekkas@netcore.fi>.
15 #include "config.h"
16 #include "includes.h"
17 #include "radvd.h"
19 void
20 mdelay(double msecs)
22 struct timeval tv;
24 tv.tv_sec = (time_t)(msecs / 1000.0);
25 tv.tv_usec = (suseconds_t)((msecs - tv.tv_sec * 1000.0) * 1000.0);
27 select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL, &tv);
30 double
31 rand_between(double lower, double upper)
33 return ((upper - lower) / (RAND_MAX + 1.0) * rand() + lower);
36 void
37 print_addr(struct in6_addr *addr, char *str)
39 const char *res;
41 /* XXX: overflows 'str' if it isn't big enough */
42 res = inet_ntop(AF_INET6, (void *)addr, str, INET6_ADDRSTRLEN);
44 if (res == NULL)
46 flog(LOG_ERR, "print_addr: inet_ntop: %s", strerror(errno));
47 strcpy(str, "[invalid address]");
51 /* Check if an in6_addr exists in the rdnss list */
52 int
53 check_rdnss_presence(struct AdvRDNSS *rdnss, struct in6_addr *addr)
55 while (rdnss) {
56 if ( !memcmp(&rdnss->AdvRDNSSAddr1, addr, sizeof(struct in6_addr))
57 || !memcmp(&rdnss->AdvRDNSSAddr2, addr, sizeof(struct in6_addr))
58 || !memcmp(&rdnss->AdvRDNSSAddr3, addr, sizeof(struct in6_addr)) )
59 break; /* rdnss address found in the list */
60 else
61 rdnss = rdnss->next; /* no match */
63 return (rdnss != NULL);
66 /* Check if a suffix exists in the dnssl list */
67 int
68 check_dnssl_presence(struct AdvDNSSL *dnssl, const char *suffix)
70 int i;
71 while (dnssl) {
72 for (i = 0; i < dnssl->AdvDNSSLNumber; i++) {
73 if (strcmp(dnssl->AdvDNSSLSuffixes[i], suffix) == 0)
74 break; /* suffix found in the list */
76 if (i != dnssl->AdvDNSSLNumber)
77 break;
79 dnssl = dnssl->next; /* no match */
81 return (dnssl != NULL);
84 /* Like read(), but retries in case of partial read */
85 ssize_t
86 readn(int fd, void *buf, size_t count)
88 size_t n = 0;
89 while (count > 0) {
90 int r = read(fd, buf, count);
91 if (r < 0) {
92 if (errno == EINTR)
93 continue;
94 return r;
96 if (r == 0)
97 return n;
98 buf = (char *)buf + r;
99 count -= r;
100 n += r;
102 return n;
105 /* Like write(), but retries in case of partial write */
106 ssize_t
107 writen(int fd, const void *buf, size_t count)
109 size_t n = 0;
110 while (count > 0) {
111 int r = write(fd, buf, count);
112 if (r < 0) {
113 if (errno == EINTR)
114 continue;
115 return r;
117 if (r == 0)
118 return n;
119 buf = (const char *)buf + r;
120 count -= r;
121 n += r;
123 return n;