This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.c
246 lines (211 loc) · 6.2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (c) 2009-2010 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <assert.h>
#include <pthread.h>
#include "util.h"
int debug_level = 0;
size_t pagesize;
void
debugf(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
if(debug_level >= level) {
vfprintf(stderr, format, args);
fflush(stderr);
}
va_end(args);
}
int
prefetch(void *begin, size_t length)
{
size_t b = (size_t)begin;
size_t br = b / pagesize * pagesize;
size_t l = length + (b - br);
size_t lr = (l + (pagesize - 1)) / pagesize * pagesize;
assert(length > 0 && length <= LARGE_CHUNK);
return posix_madvise((void*)br, lr, POSIX_MADV_WILLNEED);
}
int
incore(void *begin, size_t length)
{
size_t b = (size_t)begin;
size_t br = b / pagesize * pagesize;
size_t l = length + (b - br);
size_t lr = (l + (pagesize - 1)) / pagesize * pagesize;
unsigned char vec[32];
int i, rc;
if(lr > 32 * pagesize)
return -1;
rc = mincore((void*)br, l, vec);
if(rc < 0)
return -1;
for(i = 0; i < lr / pagesize; i++)
if(!(vec[i] & 1))
return 0;
return 1;
}
/* Get the source address used for a given interface address. Since there
is no official interface to get this information, we create a connected
UDP socket (connected UDP... hmm...) and check its source address. */
int
get_source_address(const struct sockaddr *dst, socklen_t dst_len,
struct sockaddr *src, socklen_t *src_len)
{
int s, rc, save;
s = socket(dst->sa_family, SOCK_DGRAM, 0);
if(s < 0)
goto fail;
/* Since it's a UDP socket, this doesn't actually send any packets. */
rc = connect(s, dst, dst_len);
if(rc < 0)
goto fail;
rc = getsockname(s, src, src_len);
if(rc < 0)
goto fail;
close(s);
return rc;
fail:
save = errno;
close(s);
errno = save;
return -1;
}
/* Like above, but for a given DNS name. */
int
get_name_source_address(int af, const char *name,
struct sockaddr *src, socklen_t *src_len)
{
struct addrinfo hints, *info, *infop;
int rc;
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_DGRAM;
rc = getaddrinfo(name, NULL, &hints, &info);
if(rc != 0) {
errno = ENOENT;
return -1;
}
rc = -1;
errno = ENOENT;
infop = info;
while(infop) {
if(infop->ai_addr->sa_family == af) {
rc = get_source_address(infop->ai_addr, infop->ai_addrlen,
src, src_len);
if(rc >= 0)
break;
}
infop = infop->ai_next;
}
freeaddrinfo(info);
return rc;
}
/* We all hate NATs. */
int
global_unicast_address(struct sockaddr *sa)
{
if(sa->sa_family == AF_INET) {
const unsigned char *a =
(unsigned char*)&((struct sockaddr_in*)sa)->sin_addr;
if(a[0] == 0 || a[0] == 127 || a[0] >= 224 ||
a[0] == 10 || (a[0] == 172 && a[1] >= 16 && a[1] <= 31) ||
(a[0] == 192 && a[1] == 168))
return 0;
return 1;
} else if(sa->sa_family == AF_INET6) {
const unsigned char *a =
(unsigned char*)&((struct sockaddr_in6*)sa)->sin6_addr;
/* 2000::/3 */
return (a[0] & 0xE0) == 0x20;
} else {
errno = EAFNOSUPPORT;
return -1;
}
}
int
find_global_address(int af, void *addr, int *addr_len)
{
struct sockaddr_storage ss;
socklen_t ss_len = sizeof(ss);
int rc;
/* This should be a name with both IPv4 and IPv6 addresses. */
rc = get_name_source_address( af, "www.transmissionbt.com",
(struct sockaddr*)&ss, &ss_len );
/* In case Charles removes IPv6 from his website. */
if( rc < 0 )
rc = get_name_source_address( af, "www.ietf.org",
(struct sockaddr*)&ss, &ss_len );
if( rc < 0 )
return -1;
if( !global_unicast_address( (struct sockaddr*)&ss) )
return -1;
switch(af) {
case AF_INET:
if(*addr_len < 4)
return -1;
memcpy(addr, &((struct sockaddr_in*)&ss)->sin_addr, 4);
*addr_len = 4;
return 1;
case AF_INET6:
if(*addr_len < 16)
return -1;
memcpy(addr, &((struct sockaddr_in6*)&ss)->sin6_addr, 16);
*addr_len = 16;
return 1;
default:
return -1;
}
}
void
init_random()
{
srandom((getpid() + 19) * (((int)(size_t)pthread_self()) + 71)
* (random() + 42) * time(NULL));
}
int
random_bytes(void *buffer, size_t size)
{
static int devrandom = -1;
int rc, i;
if(devrandom < 0) {
devrandom = open("/dev/urandom", O_RDONLY);
if(devrandom < 0)
goto fail;
}
rc = read(devrandom, buffer, size);
return rc;
fail:
for(i = 0; i < size; i++)
((unsigned char*)buffer)[i] = (unsigned char)(random());
return 0;
}