This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.c
124 lines (111 loc) · 2.61 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
*
* Authors:
* Lars Fenneberg <[email protected]>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <[email protected]>.
*
*/
#include "config.h"
#include "includes.h"
#include "radvd.h"
void
mdelay(double msecs)
{
struct timeval tv;
tv.tv_sec = (time_t)(msecs / 1000.0);
tv.tv_usec = (suseconds_t)((msecs - tv.tv_sec * 1000.0) * 1000.0);
select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL, &tv);
}
double
rand_between(double lower, double upper)
{
return ((upper - lower) / (RAND_MAX + 1.0) * rand() + lower);
}
void
print_addr(struct in6_addr *addr, char *str)
{
const char *res;
/* XXX: overflows 'str' if it isn't big enough */
res = inet_ntop(AF_INET6, (void *)addr, str, INET6_ADDRSTRLEN);
if (res == NULL)
{
flog(LOG_ERR, "print_addr: inet_ntop: %s", strerror(errno));
strcpy(str, "[invalid address]");
}
}
/* Check if an in6_addr exists in the rdnss list */
int
check_rdnss_presence(struct AdvRDNSS *rdnss, struct in6_addr *addr)
{
while (rdnss) {
if ( !memcmp(&rdnss->AdvRDNSSAddr1, addr, sizeof(struct in6_addr))
|| !memcmp(&rdnss->AdvRDNSSAddr2, addr, sizeof(struct in6_addr))
|| !memcmp(&rdnss->AdvRDNSSAddr3, addr, sizeof(struct in6_addr)) )
break; /* rdnss address found in the list */
else
rdnss = rdnss->next; /* no match */
}
return (rdnss != NULL);
}
/* Check if a suffix exists in the dnssl list */
int
check_dnssl_presence(struct AdvDNSSL *dnssl, const char *suffix)
{
int i;
while (dnssl) {
for (i = 0; i < dnssl->AdvDNSSLNumber; i++) {
if (strcmp(dnssl->AdvDNSSLSuffixes[i], suffix) == 0)
break; /* suffix found in the list */
}
if (i != dnssl->AdvDNSSLNumber)
break;
dnssl = dnssl->next; /* no match */
}
return (dnssl != NULL);
}
/* Like read(), but retries in case of partial read */
ssize_t
readn(int fd, void *buf, size_t count)
{
size_t n = 0;
while (count > 0) {
int r = read(fd, buf, count);
if (r < 0) {
if (errno == EINTR)
continue;
return r;
}
if (r == 0)
return n;
buf = (char *)buf + r;
count -= r;
n += r;
}
return n;
}
/* Like write(), but retries in case of partial write */
ssize_t
writen(int fd, const void *buf, size_t count)
{
size_t n = 0;
while (count > 0) {
int r = write(fd, buf, count);
if (r < 0) {
if (errno == EINTR)
continue;
return r;
}
if (r == 0)
return n;
buf = (const char *)buf + r;
count -= r;
n += r;
}
return n;
}