Blame
Date:
Thu Nov 19 09:25:28 2020 UTC
Message:
update README before 1.5.0 release
001
2014-11-14
pjp
/*
002
2018-10-19
pjp
* Copyright (c) 2014-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
2017-10-26
pjp
/*
030
2019-06-06
pjp
* $Id: filter.c,v 1.6 2019/06/06 14:56:08 pjp Exp $
031
2017-10-26
pjp
*/
032
2017-10-26
pjp
033
2019-06-06
pjp
#include <sys/types.h>
034
2019-06-06
pjp
#include <sys/socket.h>
035
2019-06-06
pjp
036
2019-06-06
pjp
#include <netinet/in.h>
037
2019-06-06
pjp
#include <arpa/inet.h>
038
2019-06-06
pjp
#include <netdb.h>
039
2019-06-06
pjp
040
2019-06-06
pjp
#include <stdio.h>
041
2019-06-06
pjp
#include <stdlib.h>
042
2019-06-06
pjp
#include <string.h>
043
2019-06-06
pjp
#include <errno.h>
044
2019-06-06
pjp
#include <syslog.h>
045
2019-06-06
pjp
046
2019-06-06
pjp
#ifdef __linux__
047
2019-06-06
pjp
#include <grp.h>
048
2019-06-06
pjp
#define __USE_BSD 1
049
2019-06-06
pjp
#include <endian.h>
050
2019-06-06
pjp
#include <bsd/stdlib.h>
051
2019-06-06
pjp
#include <bsd/string.h>
052
2019-06-06
pjp
#include <bsd/sys/queue.h>
053
2019-06-06
pjp
#define __unused
054
2019-06-06
pjp
#include <bsd/sys/tree.h>
055
2019-06-06
pjp
#include <bsd/sys/endian.h>
056
2019-06-06
pjp
#else /* not linux */
057
2019-06-06
pjp
#include <sys/queue.h>
058
2019-06-06
pjp
#include <sys/tree.h>
059
2019-06-06
pjp
#endif /* __linux__ */
060
2019-06-06
pjp
061
2019-06-06
pjp
062
2019-06-06
pjp
063
2016-07-06
pjp
#include "ddd-dns.h"
064
2016-07-06
pjp
#include "ddd-db.h"
065
2016-07-06
pjp
066
2014-11-14
pjp
int find_filter(struct sockaddr_storage *, int);
067
2014-11-14
pjp
void init_filter(void);
068
2014-11-14
pjp
int insert_filter(char *, char *);
069
2014-11-14
pjp
070
2014-11-14
pjp
extern void dolog(int, char *, ...);
071
2014-11-14
pjp
extern in_addr_t getmask(int);
072
2014-11-14
pjp
extern int getmask6(int, struct sockaddr_in6 *);
073
2014-11-14
pjp
074
2014-11-14
pjp
extern int debug, verbose;
075
2014-11-14
pjp
076
2014-11-14
pjp
SLIST_HEAD(listhead, filterentry) filterhead;
077
2014-11-14
pjp
078
2014-11-14
pjp
static struct filterentry {
079
2014-11-14
pjp
char name[INET6_ADDRSTRLEN];
080
2014-11-14
pjp
int family;
081
2014-11-14
pjp
struct sockaddr_storage hostmask;
082
2014-11-14
pjp
struct sockaddr_storage netmask;
083
2014-11-14
pjp
u_int8_t prefixlen;
084
2014-11-14
pjp
SLIST_ENTRY(filterentry) filter_entry;
085
2014-11-14
pjp
} *fn2, *fnp;
086
2014-11-14
pjp
087
2014-11-14
pjp
088
2014-11-14
pjp
/*
089
2014-11-14
pjp
* INIT_FILTER - initialize the filter singly linked list
090
2014-11-14
pjp
*/
091
2014-11-14
pjp
092
2014-11-14
pjp
void
093
2014-11-14
pjp
init_filter(void)
094
2014-11-14
pjp
{
095
2014-11-14
pjp
SLIST_INIT(&filterhead);
096
2014-11-14
pjp
return;
097
2014-11-14
pjp
}
098
2014-11-14
pjp
099
2014-11-14
pjp
/*
100
2014-11-14
pjp
* INSERT_FILTER - insert an address and prefixlen into the filter slist
101
2014-11-14
pjp
*/
102
2014-11-14
pjp
103
2014-11-14
pjp
int
104
2014-11-14
pjp
insert_filter(char *address, char *prefixlen)
105
2014-11-14
pjp
{
106
2014-11-14
pjp
struct sockaddr_in *sin;
107
2014-11-14
pjp
struct sockaddr_in6 *sin6;
108
2014-11-14
pjp
int pnum;
109
2014-11-14
pjp
int ret;
110
2014-11-14
pjp
111
2014-11-14
pjp
pnum = atoi(prefixlen);
112
2014-11-14
pjp
fn2 = malloc(sizeof(struct filterentry)); /* Insert after. */
113
2014-11-14
pjp
114
2014-11-14
pjp
if (strchr(address, ':') != NULL) {
115
2014-11-14
pjp
fn2->family = AF_INET6;
116
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)&fn2->hostmask;
117
2014-11-14
pjp
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
118
2014-11-14
pjp
return (-1);
119
2014-11-14
pjp
sin6->sin6_family = AF_INET6;
120
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)&fn2->netmask;
121
2014-11-14
pjp
sin6->sin6_family = AF_INET6;
122
2014-11-14
pjp
if (getmask6(pnum, sin6) < 0)
123
2014-11-14
pjp
return(-1);
124
2014-11-14
pjp
fn2->prefixlen = pnum;
125
2014-11-14
pjp
} else {
126
2014-11-14
pjp
127
2014-11-14
pjp
fn2->family = AF_INET;
128
2014-11-14
pjp
sin = (struct sockaddr_in *)&fn2->hostmask;
129
2014-11-14
pjp
sin->sin_family = AF_INET;
130
2014-11-14
pjp
sin->sin_addr.s_addr = inet_addr(address);
131
2014-11-14
pjp
sin = (struct sockaddr_in *)&fn2->netmask;
132
2014-11-14
pjp
sin->sin_family = AF_INET;
133
2014-11-14
pjp
sin->sin_addr.s_addr = getmask(pnum);
134
2014-11-14
pjp
fn2->prefixlen = pnum;
135
2014-11-14
pjp
136
2014-11-14
pjp
}
137
2014-11-14
pjp
138
2014-11-14
pjp
SLIST_INSERT_HEAD(&filterhead, fn2, filter_entry);
139
2014-11-14
pjp
140
2014-11-14
pjp
return (0);
141
2014-11-14
pjp
}
142
2014-11-14
pjp
143
2014-11-14
pjp
/*
144
2014-11-14
pjp
* FIND_FILTER - walk the filter list and find the correponding network
145
2014-11-14
pjp
* if a network matches return 1, if no match is found return
146
2014-11-14
pjp
* 0.
147
2014-11-14
pjp
*/
148
2014-11-14
pjp
149
2014-11-14
pjp
int
150
2014-11-14
pjp
find_filter(struct sockaddr_storage *sst, int family)
151
2014-11-14
pjp
{
152
2014-11-14
pjp
struct sockaddr_in *sin, *sin0;
153
2014-11-14
pjp
struct sockaddr_in6 *sin6, *sin60, *sin61;
154
2014-11-14
pjp
u_int32_t hostmask, netmask;
155
2014-11-14
pjp
u_int32_t a;
156
2014-11-14
pjp
#ifdef __amd64
157
2014-11-14
pjp
u_int64_t *hm[2], *nm[2], *a6[2];
158
2014-11-14
pjp
#else
159
2014-11-14
pjp
u_int32_t *hm[4], *nm[4], *a6[4];
160
2014-11-14
pjp
#endif
161
2014-11-14
pjp
162
2014-11-14
pjp
SLIST_FOREACH(fnp, &filterhead, filter_entry) {
163
2014-11-14
pjp
if (fnp->family == AF_INET) {
164
2014-11-14
pjp
if (family != AF_INET)
165
2014-11-14
pjp
continue;
166
2014-11-14
pjp
sin = (struct sockaddr_in *)sst;
167
2014-11-14
pjp
a = sin->sin_addr.s_addr;
168
2014-11-14
pjp
sin = (struct sockaddr_in *)&fnp->hostmask;
169
2014-11-14
pjp
sin0 = (struct sockaddr_in *)&fnp->netmask;
170
2014-11-14
pjp
hostmask = sin->sin_addr.s_addr;
171
2014-11-14
pjp
netmask = sin0->sin_addr.s_addr;
172
2014-11-14
pjp
if ((hostmask & netmask) == (a & netmask)) {
173
2014-11-14
pjp
return (1);
174
2014-11-14
pjp
} /* if hostmask */
175
2014-11-14
pjp
} else if (fnp->family == AF_INET6) {
176
2014-11-14
pjp
if (family != AF_INET6)
177
2014-11-14
pjp
continue;
178
2014-11-14
pjp
sin6 = (struct sockaddr_in6 *)sst;
179
2014-11-14
pjp
sin60 = (struct sockaddr_in6 *)&fnp->hostmask;
180
2014-11-14
pjp
sin61 = (struct sockaddr_in6 *)&fnp->netmask;
181
2014-11-14
pjp
#ifdef __amd64
182
2014-11-14
pjp
/*
183
2014-11-14
pjp
* If this is on a 64 bit machine, we'll benefit
184
2014-11-14
pjp
* by using 64 bit registers, this should make it
185
2014-11-14
pjp
* a tad faster...
186
2014-11-14
pjp
*/
187
2014-11-14
pjp
hm[0] = (u_int64_t *)&sin60->sin6_addr.s6_addr;
188
2014-11-14
pjp
hm[1] = (hm[0] + 1);
189
2014-11-14
pjp
nm[0] = (u_int64_t *)&sin61->sin6_addr.s6_addr;
190
2014-11-14
pjp
nm[1] = (nm[0] + 1);
191
2014-11-14
pjp
a6[0] = (u_int64_t *)&sin6->sin6_addr.s6_addr;
192
2014-11-14
pjp
a6[1] = (a6[0] + 1);
193
2014-11-14
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
194
2014-11-14
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
195
2014-11-14
pjp
#else
196
2014-11-14
pjp
hm[0] = (u_int32_t *)&sin60->sin6_addr.s6_addr;
197
2014-11-14
pjp
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
198
2014-11-14
pjp
hm[3] = (hm[2] + 1);
199
2014-11-14
pjp
nm[0] = (u_int32_t *)&sin61->sin6_addr.s6_addr;
200
2014-11-14
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
201
2014-11-14
pjp
nm[3] = (nm[2] + 1);
202
2014-11-14
pjp
a6[0] = (u_int32_t *)&sin6->sin6_addr.s6_addr;
203
2014-11-14
pjp
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
204
2014-11-14
pjp
a6[3] = (a6[2] + 1);
205
2014-11-14
pjp
206
2014-11-14
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
207
2014-11-14
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
208
2014-11-14
pjp
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
209
2014-11-14
pjp
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
210
2014-11-14
pjp
#endif
211
2014-11-14
pjp
212
2014-11-14
pjp
return (1);
213
2014-11-14
pjp
} /* if ip6 address */
214
2014-11-14
pjp
215
2014-11-14
pjp
} /* if AF_INET6 */
216
2014-11-14
pjp
} /* SLIST */
217
2014-11-14
pjp
218
2014-11-14
pjp
return (0);
219
2014-11-14
pjp
}
repomaster@centroid.eu