Blame
Date:
Mon Jan 11 09:00:47 2021 UTC
Message:
a usleep makes it unnecessary slow, I guess it forces a context switch...
001
2014-11-14
pjp
/*
002
2018-10-19
pjp
* Copyright (c) 2010-2018 Peter J. Philipp
003
2014-11-14
pjp
* All rights reserved.
004
2014-11-14
pjp
*
005
2014-11-14
pjp
* Redistribution and use in source and binary forms, with or without
006
2014-11-14
pjp
* modification, are permitted provided that the following conditions
007
2014-11-14
pjp
* are met:
008
2014-11-14
pjp
* 1. Redistributions of source code must retain the above copyright
009
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer.
010
2014-11-14
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer in the
012
2014-11-14
pjp
* documentation and/or other materials provided with the distribution.
013
2014-11-14
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2014-11-14
pjp
* derived from this software without specific prior written permission
015
2014-11-14
pjp
*
016
2014-11-14
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2014-11-14
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2014-11-14
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2014-11-14
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2014-11-14
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2014-11-14
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2014-11-14
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2014-11-14
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2014-11-14
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2014-11-14
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2014-11-14
pjp
*
027
2014-11-14
pjp
*/
028
2017-10-26
pjp
029
2019-06-06
pjp
#include <sys/types.h>
030
2019-06-06
pjp
#include <sys/socket.h>
031
2017-10-26
pjp
032
2019-06-06
pjp
#include <netinet/in.h>
033
2019-06-06
pjp
#include <arpa/inet.h>
034
2019-06-06
pjp
#include <netdb.h>
035
2019-06-06
pjp
036
2019-06-06
pjp
#include <stdio.h>
037
2019-06-06
pjp
#include <stdlib.h>
038
2019-06-06
pjp
#include <string.h>
039
2019-06-06
pjp
#include <errno.h>
040
2019-06-06
pjp
#include <syslog.h>
041
2019-06-06
pjp
042
2019-06-06
pjp
#ifdef __linux__
043
2019-06-06
pjp
#include <grp.h>
044
2019-06-06
pjp
#define __USE_BSD 1
045
2019-06-06
pjp
#include <endian.h>
046
2019-06-06
pjp
#include <bsd/stdlib.h>
047
2019-06-06
pjp
#include <bsd/string.h>
048
2019-06-06
pjp
#include <bsd/sys/queue.h>
049
2019-06-06
pjp
#define __unused
050
2019-06-06
pjp
#include <bsd/sys/tree.h>
051
2019-06-06
pjp
#include <bsd/sys/endian.h>
052
2019-06-06
pjp
#else /* not linux */
053
2019-06-06
pjp
#include <sys/queue.h>
054
2019-06-06
pjp
#include <sys/tree.h>
055
2019-06-06
pjp
#endif /* __linux__ */
056
2019-06-06
pjp
057
2016-07-06
pjp
#include "ddd-dns.h"
058
2016-07-06
pjp
#include "ddd-db.h"
059
2016-07-06
pjp
060
2014-11-14
pjp
u_int8_t find_region(struct sockaddr_storage *, int);
061
2014-11-14
pjp
in_addr_t getmask(int);
062
2014-11-14
pjp
int getmask6(int, struct sockaddr_in6 *);
063
2014-11-14
pjp
void init_region(void);
064
2014-11-14
pjp
int insert_region(char *, char *, u_int8_t);
065
2014-11-14
pjp
066
2017-06-26
pjp
SLIST_HEAD(listhead, regionentry) regionhead;
067
2014-11-14
pjp
068
2017-06-26
pjp
static struct regionentry {
069
2014-11-14
pjp
char name[INET6_ADDRSTRLEN];
070
2014-11-14
pjp
int family;
071
2014-11-14
pjp
struct sockaddr_storage hostmask;
072
2014-11-14
pjp
struct sockaddr_storage netmask;
073
2014-11-14
pjp
u_int8_t region;
074
2014-11-14
pjp
u_int8_t prefixlen;
075
2017-06-26
pjp
SLIST_ENTRY(regionentry) region_entry;
076
2014-11-14
pjp
} *n2, *np;
077
2014-11-14
pjp
078
2014-11-14
pjp
079
2014-11-14
pjp
/*
080
2014-11-14
pjp
* INIT_REGION - initialize the region singly linked list
081
2014-11-14
pjp
*/
082
2014-11-14
pjp
083
2014-11-14
pjp
void
084
2014-11-14
pjp
init_region(void)
085
2014-11-14
pjp
{
086
2017-06-26
pjp
SLIST_INIT(&regionhead);
087
2014-11-14
pjp
return;
088
2014-11-14
pjp
}
089
2014-11-14
pjp
090
2014-11-14
pjp
/*
091
2014-11-14
pjp
* INSERT_REGION - insert particular address and prefix length and region
092
2014-11-14
pjp
* into the
093
2017-06-26
pjp
* singly linked list at "regionhead", if the address
094
2017-06-26
pjp
* contains
095
2014-11-14
pjp
* a colon then it is assumed to be an IPv6 address.
096
2014-11-14
pjp
* return -1 on error, 0 on successful insertion
097
2014-11-14
pjp
*/
098
2014-11-14
pjp
099
2014-11-14
pjp
int
100
2014-11-14
pjp
insert_region(char *address, char *prefixlen, u_int8_t region)
101
2014-11-14
pjp
{
102
2014-11-14
pjp
struct sockaddr_in *sin;
103
2014-11-14
pjp
struct sockaddr_in6 *sin6;
104
2014-11-14
pjp
int pnum;
105
2014-11-14
pjp
int ret;
106
2014-11-14
pjp
107
2014-11-14
pjp
pnum = atoi(prefixlen);
108
2017-06-26
pjp
n2 = malloc(sizeof(struct regionentry)); /* Insert after. */
109
2014-11-14
pjp
110
2014-11-14
pjp
if (strchr(address, ':') != NULL) {
111
2014-11-14
pjp
n2->family = AF_INET6;
112
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)&n2->hostmask;
113
2014-11-14
pjp
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
114
2014-11-14
pjp
return (-1);
115
2014-11-14
pjp
sin6->sin6_family = AF_INET6;
116
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)&n2->netmask;
117
2014-11-14
pjp
sin6->sin6_family = AF_INET6;
118
2014-11-14
pjp
if (getmask6(pnum, sin6) < 0)
119
2014-11-14
pjp
return(-1);
120
2014-11-14
pjp
n2->region = region;
121
2014-11-14
pjp
n2->prefixlen = pnum;
122
2014-11-14
pjp
} else {
123
2014-11-14
pjp
124
2014-11-14
pjp
n2->family = AF_INET;
125
2014-11-14
pjp
sin = (struct sockaddr_in *)&n2->hostmask;
126
2014-11-14
pjp
sin->sin_family = AF_INET;
127
2014-11-14
pjp
sin->sin_addr.s_addr = inet_addr(address);
128
2014-11-14
pjp
sin = (struct sockaddr_in *)&n2->netmask;
129
2014-11-14
pjp
sin->sin_family = AF_INET;
130
2014-11-14
pjp
sin->sin_addr.s_addr = getmask(pnum);
131
2014-11-14
pjp
n2->region = region;
132
2014-11-14
pjp
n2->prefixlen = pnum;
133
2014-11-14
pjp
134
2014-11-14
pjp
}
135
2014-11-14
pjp
136
2017-06-26
pjp
SLIST_INSERT_HEAD(&regionhead, n2, region_entry);
137
2014-11-14
pjp
138
2014-11-14
pjp
return (0);
139
2014-11-14
pjp
}
140
2014-11-14
pjp
141
2014-11-14
pjp
/*
142
2014-11-14
pjp
* FIND_REGION - walk the region list and find the correponding network with
143
2014-11-14
pjp
* the highest prefix length, so that a /24 has more precedence
144
2014-11-14
pjp
* than
145
2014-11-14
pjp
* a /8 for example. IPv6 and IPv4 addresses are kept seperate
146
2014-11-14
pjp
*/
147
2014-11-14
pjp
148
2014-11-14
pjp
u_int8_t
149
2014-11-14
pjp
find_region(struct sockaddr_storage *sst, int family)
150
2014-11-14
pjp
{
151
2014-11-14
pjp
struct sockaddr_in *sin, *sin0;
152
2014-11-14
pjp
struct sockaddr_in6 *sin6, *sin60, *sin61;
153
2014-11-14
pjp
u_int32_t hostmask, netmask;
154
2014-11-14
pjp
u_int32_t a;
155
2014-11-14
pjp
#ifdef __amd64
156
2014-11-14
pjp
u_int64_t *hm[2], *nm[2], *a6[2];
157
2014-11-14
pjp
#else
158
2014-11-14
pjp
u_int32_t *hm[4], *nm[4], *a6[4];
159
2014-11-14
pjp
#endif
160
2014-11-14
pjp
u_int8_t region = 0xff;
161
2014-11-14
pjp
u_int8_t prefixlen = 0;
162
2014-11-14
pjp
163
2017-06-26
pjp
SLIST_FOREACH(np, &regionhead, region_entry) {
164
2014-11-14
pjp
if (np->family == AF_INET) {
165
2014-11-14
pjp
if (family != AF_INET)
166
2014-11-14
pjp
continue;
167
2014-11-14
pjp
sin = (struct sockaddr_in *)sst;
168
2014-11-14
pjp
a = sin->sin_addr.s_addr;
169
2014-11-14
pjp
sin = (struct sockaddr_in *)&np->hostmask;
170
2014-11-14
pjp
sin0 = (struct sockaddr_in *)&np->netmask;
171
2014-11-14
pjp
hostmask = sin->sin_addr.s_addr;
172
2014-11-14
pjp
netmask = sin0->sin_addr.s_addr;
173
2014-11-14
pjp
if ((hostmask & netmask) == (a & netmask)) {
174
2014-11-14
pjp
if (np->prefixlen >= prefixlen) {
175
2014-11-14
pjp
region = np->region;
176
2014-11-14
pjp
prefixlen = np->prefixlen;
177
2014-11-14
pjp
}
178
2014-11-14
pjp
} /* if hostmask */
179
2014-11-14
pjp
} else if (np->family == AF_INET6) {
180
2014-11-14
pjp
if (family != AF_INET6)
181
2014-11-14
pjp
continue;
182
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)sst;
183
2014-11-14
pjp
sin60 = (struct sockaddr_in6 *)&np->hostmask;
184
2014-11-14
pjp
sin61 = (struct sockaddr_in6 *)&np->netmask;
185
2014-11-14
pjp
#ifdef __amd64
186
2014-11-14
pjp
/*
187
2014-11-14
pjp
* If this is on a 64 bit machine, we'll benefit
188
2014-11-14
pjp
* by using 64 bit registers, this should make it
189
2014-11-14
pjp
* a tad faster...
190
2014-11-14
pjp
*/
191
2014-11-14
pjp
hm[0] = (u_int64_t *)&sin60->sin6_addr.s6_addr;
192
2014-11-14
pjp
hm[1] = (hm[0] + 1);
193
2014-11-14
pjp
nm[0] = (u_int64_t *)&sin61->sin6_addr.s6_addr;
194
2014-11-14
pjp
nm[1] = (nm[0] + 1);
195
2014-11-14
pjp
a6[0] = (u_int64_t *)&sin6->sin6_addr.s6_addr;
196
2014-11-14
pjp
a6[1] = (a6[0] + 1);
197
2014-11-14
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
198
2014-11-14
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
199
2014-11-14
pjp
#else
200
2014-11-14
pjp
hm[0] = (u_int32_t *)&sin60->sin6_addr.s6_addr;
201
2014-11-14
pjp
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
202
2014-11-14
pjp
hm[3] = (hm[2] + 1);
203
2014-11-14
pjp
nm[0] = (u_int32_t *)&sin61->sin6_addr.s6_addr;
204
2014-11-14
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
205
2014-11-14
pjp
nm[3] = (nm[2] + 1);
206
2014-11-14
pjp
a6[0] = (u_int32_t *)&sin6->sin6_addr.s6_addr;
207
2014-11-14
pjp
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
208
2014-11-14
pjp
a6[3] = (a6[2] + 1);
209
2014-11-14
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
210
2014-11-14
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
211
2014-11-14
pjp
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
212
2014-11-14
pjp
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
213
2014-11-14
pjp
#endif
214
2014-11-14
pjp
215
2014-11-14
pjp
if (np->prefixlen >= prefixlen) {
216
2014-11-14
pjp
region = np->region;
217
2014-11-14
pjp
prefixlen = np->prefixlen;
218
2014-11-14
pjp
}
219
2014-11-14
pjp
} /* if ip6 address */
220
2014-11-14
pjp
221
2014-11-14
pjp
} /* if AF_INET6 */
222
2014-11-14
pjp
} /* SLIST */
223
2014-11-14
pjp
224
2014-11-14
pjp
return (region);
225
2014-11-14
pjp
}
226
2014-11-14
pjp
227
2014-11-14
pjp
/*
228
2014-11-14
pjp
* GETMASK - get the v4 netmask given by prefix length, return netmask in
229
2014-11-14
pjp
* network byte order, function can't fail unless prefix length
230
2014-11-14
pjp
* supplied is > 32
231
2014-11-14
pjp
*/
232
2014-11-14
pjp
233
2014-11-14
pjp
in_addr_t
234
2014-11-14
pjp
getmask(int prefixlen)
235
2014-11-14
pjp
{
236
2014-11-14
pjp
in_addr_t ret = 0xffffffff;
237
2014-11-14
pjp
238
2014-11-14
pjp
/* I know it's cheating */
239
2014-11-14
pjp
if (prefixlen > 31)
240
2014-11-14
pjp
return (htonl(ret));
241
2014-11-14
pjp
242
2014-11-14
pjp
ret >>= prefixlen; /* 0x00ffffff */
243
2014-11-14
pjp
ret = ~ret; /* 0xff000000 */
244
2014-11-14
pjp
245
2014-11-14
pjp
return (htonl(ret));
246
2014-11-14
pjp
}
247
2014-11-14
pjp
248
2014-11-14
pjp
/*
249
2014-11-14
pjp
* GETMASK6 - like getmask() but works on a supplied sockaddr_in6 instead of
250
2014-11-14
pjp
* returning results as return address. Function cannot fail
251
2014-11-14
pjp
* unless prefix length supplied is > 128. At which point a buffer
252
2014-11-14
pjp
* overflow is possible.
253
2014-11-14
pjp
*/
254
2014-11-14
pjp
255
2014-11-14
pjp
int
256
2014-11-14
pjp
getmask6(int prefixlen, struct sockaddr_in6 *sin6)
257
2014-11-14
pjp
{
258
2014-11-14
pjp
int i, j;
259
2014-11-14
pjp
u_int32_t *nm[4];
260
2014-11-14
pjp
261
2014-11-14
pjp
if (prefixlen > 128 || prefixlen < 0)
262
2014-11-14
pjp
return (-1);
263
2014-11-14
pjp
264
2014-11-14
pjp
memset(&sin6->sin6_addr.s6_addr, 0xff, sizeof(sin6->sin6_addr.s6_addr));
265
2014-11-14
pjp
nm[0] = (u_int32_t *)sin6->sin6_addr.s6_addr;
266
2014-11-14
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
267
2014-11-14
pjp
nm[3] = (nm[2] + 1);
268
2014-11-14
pjp
269
2014-11-14
pjp
for (i = 0, j = 0; j < prefixlen; j++) {
270
2014-11-14
pjp
if (*nm[i] == 1) {
271
2014-11-14
pjp
*nm[i] = 0;
272
2014-11-14
pjp
i++;
273
2014-11-14
pjp
} else
274
2014-11-14
pjp
*nm[i] >>= 1;
275
2014-11-14
pjp
}
276
2014-11-14
pjp
*nm[0] = htonl(~ *nm[0]);
277
2014-11-14
pjp
*nm[1] = htonl(~ *nm[1]);
278
2014-11-14
pjp
*nm[2] = htonl(~ *nm[2]);
279
2014-11-14
pjp
*nm[3] = htonl(~ *nm[3]);
280
2014-11-14
pjp
281
2014-11-14
pjp
return (0);
282
2014-11-14
pjp
}
repomaster@centroid.eu