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
2020-07-16
pjp
/*
002
2020-07-16
pjp
* Copyright (c) 2014-2020 Peter J. Philipp
003
2020-07-16
pjp
* All rights reserved.
004
2020-07-16
pjp
*
005
2020-07-16
pjp
* Redistribution and use in source and binary forms, with or without
006
2020-07-16
pjp
* modification, are permitted provided that the following conditions
007
2020-07-16
pjp
* are met:
008
2020-07-16
pjp
* 1. Redistributions of source code must retain the above copyright
009
2020-07-16
pjp
* notice, this list of conditions and the following disclaimer.
010
2020-07-16
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2020-07-16
pjp
* notice, this list of conditions and the following disclaimer in the
012
2020-07-16
pjp
* documentation and/or other materials provided with the distribution.
013
2020-07-16
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2020-07-16
pjp
* derived from this software without specific prior written permission
015
2020-07-16
pjp
*
016
2020-07-16
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2020-07-16
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2020-07-16
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2020-07-16
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2020-07-16
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2020-07-16
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2020-07-16
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2020-07-16
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2020-07-16
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2020-07-16
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2020-07-16
pjp
*
027
2020-07-16
pjp
*/
028
2020-07-16
pjp
029
2020-07-16
pjp
/*
030
2020-07-16
pjp
* this file is based on filter.c
031
2020-07-16
pjp
*/
032
2020-07-16
pjp
033
2020-07-16
pjp
034
2020-07-16
pjp
#include <sys/types.h>
035
2020-07-16
pjp
#include <sys/socket.h>
036
2020-07-16
pjp
037
2020-07-16
pjp
#include <netinet/in.h>
038
2020-07-16
pjp
#include <arpa/inet.h>
039
2020-07-16
pjp
#include <netdb.h>
040
2020-07-16
pjp
041
2020-07-16
pjp
#include <stdio.h>
042
2020-07-16
pjp
#include <stdlib.h>
043
2020-07-16
pjp
#include <string.h>
044
2020-07-16
pjp
045
2020-07-16
pjp
#ifdef __linux__
046
2020-07-16
pjp
#include <grp.h>
047
2020-07-16
pjp
#define __USE_BSD 1
048
2020-07-16
pjp
#include <endian.h>
049
2020-07-16
pjp
#include <bsd/stdlib.h>
050
2020-07-16
pjp
#include <bsd/string.h>
051
2020-07-16
pjp
#include <bsd/sys/queue.h>
052
2020-07-16
pjp
#define __unused
053
2020-07-16
pjp
#include <bsd/sys/tree.h>
054
2020-07-16
pjp
#include <bsd/sys/endian.h>
055
2020-07-16
pjp
#else /* not linux */
056
2020-07-16
pjp
#include <sys/queue.h>
057
2020-07-16
pjp
#include <sys/tree.h>
058
2020-07-16
pjp
#endif /* __linux__ */
059
2020-07-16
pjp
060
2020-07-16
pjp
061
2020-07-16
pjp
#include "ddd-dns.h"
062
2020-07-16
pjp
#include "ddd-db.h"
063
2020-07-16
pjp
064
2020-07-16
pjp
int find_passlist(struct sockaddr_storage *, int);
065
2020-07-16
pjp
void init_passlist(void);
066
2020-07-16
pjp
int insert_passlist(char *, char *);
067
2020-07-16
pjp
068
2020-07-16
pjp
extern void dolog(int, char *, ...);
069
2020-07-16
pjp
extern in_addr_t getmask(int);
070
2020-07-16
pjp
extern int getmask6(int, struct sockaddr_in6 *);
071
2020-07-16
pjp
072
2020-07-16
pjp
extern int debug, verbose;
073
2020-07-16
pjp
074
2020-07-16
pjp
int passlist = 0; /* passlist is off by default */
075
2020-07-16
pjp
076
2020-07-16
pjp
SLIST_HEAD(listhead, passlistentry) passlisthead;
077
2020-07-16
pjp
078
2020-07-16
pjp
static struct passlistentry {
079
2020-07-16
pjp
char name[INET6_ADDRSTRLEN];
080
2020-07-16
pjp
int family;
081
2020-07-16
pjp
struct sockaddr_storage hostmask;
082
2020-07-16
pjp
struct sockaddr_storage netmask;
083
2020-07-16
pjp
u_int8_t prefixlen;
084
2020-07-16
pjp
SLIST_ENTRY(passlistentry) passlist_entry;
085
2020-07-16
pjp
} *wln2, *wlnp;
086
2020-07-16
pjp
087
2020-07-16
pjp
088
2020-07-16
pjp
/*
089
2020-07-16
pjp
* INIT_PASSLIST - initialize the passlist singly linked list
090
2020-07-16
pjp
*/
091
2020-07-16
pjp
092
2020-07-16
pjp
void
093
2020-07-16
pjp
init_passlist(void)
094
2020-07-16
pjp
{
095
2020-07-16
pjp
SLIST_INIT(&passlisthead);
096
2020-07-16
pjp
return;
097
2020-07-16
pjp
}
098
2020-07-16
pjp
099
2020-07-16
pjp
/*
100
2020-07-16
pjp
* INSERT_PASSLIST - insert an address and prefixlen into the passlist slist
101
2020-07-16
pjp
*/
102
2020-07-16
pjp
103
2020-07-16
pjp
int
104
2020-07-16
pjp
insert_passlist(char *address, char *prefixlen)
105
2020-07-16
pjp
{
106
2020-07-16
pjp
struct sockaddr_in *sin;
107
2020-07-16
pjp
struct sockaddr_in6 *sin6;
108
2020-07-16
pjp
int pnum;
109
2020-07-16
pjp
int ret;
110
2020-07-16
pjp
111
2020-07-16
pjp
pnum = atoi(prefixlen);
112
2020-07-16
pjp
wln2 = malloc(sizeof(struct passlistentry)); /* Insert after. */
113
2020-07-16
pjp
114
2020-07-16
pjp
if (strchr(address, ':') != NULL) {
115
2020-07-16
pjp
wln2->family = AF_INET6;
116
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)&wln2->hostmask;
117
2020-07-16
pjp
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
118
2020-07-16
pjp
return (-1);
119
2020-07-16
pjp
sin6->sin6_family = AF_INET6;
120
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)&wln2->netmask;
121
2020-07-16
pjp
sin6->sin6_family = AF_INET6;
122
2020-07-16
pjp
if (getmask6(pnum, sin6) < 0)
123
2020-07-16
pjp
return(-1);
124
2020-07-16
pjp
wln2->prefixlen = pnum;
125
2020-07-16
pjp
} else {
126
2020-07-16
pjp
127
2020-07-16
pjp
wln2->family = AF_INET;
128
2020-07-16
pjp
sin = (struct sockaddr_in *)&wln2->hostmask;
129
2020-07-16
pjp
sin->sin_family = AF_INET;
130
2020-07-16
pjp
sin->sin_addr.s_addr = inet_addr(address);
131
2020-07-16
pjp
sin = (struct sockaddr_in *)&wln2->netmask;
132
2020-07-16
pjp
sin->sin_family = AF_INET;
133
2020-07-16
pjp
sin->sin_addr.s_addr = getmask(pnum);
134
2020-07-16
pjp
wln2->prefixlen = pnum;
135
2020-07-16
pjp
136
2020-07-16
pjp
}
137
2020-07-16
pjp
138
2020-07-16
pjp
SLIST_INSERT_HEAD(&passlisthead, wln2, passlist_entry);
139
2020-07-16
pjp
140
2020-07-16
pjp
return (0);
141
2020-07-16
pjp
}
142
2020-07-16
pjp
143
2020-07-16
pjp
/*
144
2020-07-16
pjp
* FIND_PASSLIST - walk the passlist list and find the correponding network
145
2020-07-16
pjp
* if a network matches return 1, if no match is found return
146
2020-07-16
pjp
* 0.
147
2020-07-16
pjp
*/
148
2020-07-16
pjp
149
2020-07-16
pjp
int
150
2020-07-16
pjp
find_passlist(struct sockaddr_storage *sst, int family)
151
2020-07-16
pjp
{
152
2020-07-16
pjp
struct sockaddr_in *sin, *sin0;
153
2020-07-16
pjp
struct sockaddr_in6 *sin6, *sin60, *sin61;
154
2020-07-16
pjp
u_int32_t hostmask, netmask;
155
2020-07-16
pjp
u_int32_t a;
156
2020-07-16
pjp
#ifdef __amd64
157
2020-07-16
pjp
u_int64_t *hm[2], *nm[2], *a6[2];
158
2020-07-16
pjp
#else
159
2020-07-16
pjp
u_int32_t *hm[4], *nm[4], *a6[4];
160
2020-07-16
pjp
#endif
161
2020-07-16
pjp
162
2020-07-16
pjp
SLIST_FOREACH(wlnp, &passlisthead, passlist_entry) {
163
2020-07-16
pjp
if (wlnp->family == AF_INET) {
164
2020-07-16
pjp
if (family != AF_INET)
165
2020-07-16
pjp
continue;
166
2020-07-16
pjp
sin = (struct sockaddr_in *)sst;
167
2020-07-16
pjp
a = sin->sin_addr.s_addr;
168
2020-07-16
pjp
sin = (struct sockaddr_in *)&wlnp->hostmask;
169
2020-07-16
pjp
sin0 = (struct sockaddr_in *)&wlnp->netmask;
170
2020-07-16
pjp
hostmask = sin->sin_addr.s_addr;
171
2020-07-16
pjp
netmask = sin0->sin_addr.s_addr;
172
2020-07-16
pjp
if ((hostmask & netmask) == (a & netmask)) {
173
2020-07-16
pjp
return (1);
174
2020-07-16
pjp
} /* if hostmask */
175
2020-07-16
pjp
} else if (wlnp->family == AF_INET6) {
176
2020-07-16
pjp
if (family != AF_INET6)
177
2020-07-16
pjp
continue;
178
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)sst;
179
2020-07-16
pjp
sin60 = (struct sockaddr_in6 *)&wlnp->hostmask;
180
2020-07-16
pjp
sin61 = (struct sockaddr_in6 *)&wlnp->netmask;
181
2020-07-16
pjp
#ifdef __amd64
182
2020-07-16
pjp
/*
183
2020-07-16
pjp
* If this is on a 64 bit machine, we'll benefit
184
2020-07-16
pjp
* by using 64 bit registers, this should make it
185
2020-07-16
pjp
* a tad faster...
186
2020-07-16
pjp
*/
187
2020-07-16
pjp
hm[0] = (u_int64_t *)&sin60->sin6_addr.s6_addr;
188
2020-07-16
pjp
hm[1] = (hm[0] + 1);
189
2020-07-16
pjp
nm[0] = (u_int64_t *)&sin61->sin6_addr.s6_addr;
190
2020-07-16
pjp
nm[1] = (nm[0] + 1);
191
2020-07-16
pjp
a6[0] = (u_int64_t *)&sin6->sin6_addr.s6_addr;
192
2020-07-16
pjp
a6[1] = (a6[0] + 1);
193
2020-07-16
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
194
2020-07-16
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
195
2020-07-16
pjp
#else
196
2020-07-16
pjp
hm[0] = (u_int32_t *)&sin60->sin6_addr.s6_addr;
197
2020-07-16
pjp
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
198
2020-07-16
pjp
hm[3] = (hm[2] + 1);
199
2020-07-16
pjp
nm[0] = (u_int32_t *)&sin61->sin6_addr.s6_addr;
200
2020-07-16
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
201
2020-07-16
pjp
nm[3] = (nm[2] + 1);
202
2020-07-16
pjp
a6[0] = (u_int32_t *)&sin6->sin6_addr.s6_addr;
203
2020-07-16
pjp
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
204
2020-07-16
pjp
a6[3] = (a6[2] + 1);
205
2020-07-16
pjp
206
2020-07-16
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
207
2020-07-16
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
208
2020-07-16
pjp
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
209
2020-07-16
pjp
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
210
2020-07-16
pjp
#endif
211
2020-07-16
pjp
212
2020-07-16
pjp
return (1);
213
2020-07-16
pjp
} /* if ip6 address */
214
2020-07-16
pjp
215
2020-07-16
pjp
} /* if AF_INET6 */
216
2020-07-16
pjp
} /* SLIST */
217
2020-07-16
pjp
218
2020-07-16
pjp
return (0);
219
2020-07-16
pjp
}
repomaster@centroid.eu