forked from radvd-project/radvd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
173 lines (152 loc) · 3.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
*
* Authors:
* Lars Fenneberg <lf@elemental.net>
*
* 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 <reubenhwk@gmail.com>.
*
*/
#include "config.h"
#include "includes.h"
#include "radvd.h"
#ifdef UNIT_TEST
#include "test/util.c"
#endif
struct safe_buffer * new_safe_buffer(void)
{
struct safe_buffer * sb = malloc(sizeof(struct safe_buffer));
*sb = SAFE_BUFFER_INIT;
sb->should_free = 1;
return sb;
}
void safe_buffer_free(struct safe_buffer * sb)
{
if (sb->buffer) {
free(sb->buffer);
}
if (sb->should_free) {
free(sb);
}
}
size_t safe_buffer_pad(struct safe_buffer * sb, size_t count)
{
size_t rc = 0;
unsigned char zero = 0;
while (count--) {
rc += safe_buffer_append(sb, &zero, 1);
}
return rc;
}
size_t safe_buffer_append(struct safe_buffer * sb, void const * v, size_t count)
{
if (sb) {
unsigned const char * m = (unsigned const char *)v;
if (sb->allocated <= sb->used + count) {
sb->allocated = sb->used + count + MSG_SIZE_SEND;
sb->buffer = realloc(sb->buffer, sb->allocated);
}
memcpy(&sb->buffer[sb->used], m, count);
sb->used += count;
if (sb->used >= MSG_SIZE_SEND) {
flog(LOG_ERR, "Too many prefixes, routes, rdnss or dnssl to fit in buffer. Exiting.");
exit(1);
}
}
return count;
}
__attribute__ ((format(printf, 1, 2)))
char * strdupf(char const * format, ...)
{
va_list va;
va_start(va, format);
char * strp = 0;
int rc = vasprintf(&strp, format, va);
if (rc == -1 || !strp) {
flog(LOG_ERR, "vasprintf failed: %s", strerror(errno));
exit(-1);
}
va_end(va);
return strp;
}
double rand_between(double lower, double upper)
{
return ((upper - lower) / (RAND_MAX + 1.0) * rand() + lower);
}
/* This assumes that str is not null and str_size > 0 */
void addrtostr(struct in6_addr *addr, char *str, size_t str_size)
{
const char *res;
res = inet_ntop(AF_INET6, (void *)addr, str, str_size);
if (res == NULL) {
flog(LOG_ERR, "addrtostr: inet_ntop: %s", strerror(errno));
strncpy(str, "[invalid address]", str_size);
str[str_size - 1] = '\0';
}
}
/* 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)))
return 1; /* rdnss address found in the list */
rdnss = rdnss->next;
}
return 0;
}
/* Check if a suffix exists in the dnssl list */
int check_dnssl_presence(struct AdvDNSSL *dnssl, const char *suffix)
{
while (dnssl) {
for (int i = 0; i < dnssl->AdvDNSSLNumber; ++i) {
if (0 == strcmp(dnssl->AdvDNSSLSuffixes[i], suffix))
return 1; /* suffix found in the list */
}
dnssl = dnssl->next;
}
return 0;
}
/* 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;
}