Blame
Date:
Sat Nov 8 20:28:31 2014 UTC
Message:
0.9.0 is the last version of Wildcarddnsd, the succeeding project is called Delphinusdnsd and will have its first release around November 15th, 2015. The new project page is at http://delphinusdns.centroid.eu.
001
2014-05-01
pjp
/*
002
2014-05-01
pjp
* Copyright (c) 2014 Peter J. Philipp
003
2014-05-01
pjp
* All rights reserved.
004
2014-05-01
pjp
*
005
2014-05-01
pjp
* Redistribution and use in source and binary forms, with or without
006
2014-05-01
pjp
* modification, are permitted provided that the following conditions
007
2014-05-01
pjp
* are met:
008
2014-05-01
pjp
* 1. Redistributions of source code must retain the above copyright
009
2014-05-01
pjp
* notice, this list of conditions and the following disclaimer.
010
2014-05-01
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2014-05-01
pjp
* notice, this list of conditions and the following disclaimer in the
012
2014-05-01
pjp
* documentation and/or other materials provided with the distribution.
013
2014-05-01
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2014-05-01
pjp
* derived from this software without specific prior written permission
015
2014-05-01
pjp
*
016
2014-05-01
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2014-05-01
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2014-05-01
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2014-05-01
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2014-05-01
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2014-05-01
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2014-05-01
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2014-05-01
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2014-05-01
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2014-05-01
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2014-05-01
pjp
*
027
2014-05-01
pjp
*/
028
2014-05-01
pjp
#include "include.h"
029
2014-05-01
pjp
#include "dns.h"
030
2014-05-01
pjp
#include "db.h"
031
2014-05-01
pjp
032
2014-05-18
pjp
int find_filter(struct sockaddr_storage *, int);
033
2014-05-18
pjp
void init_filter(void);
034
2014-05-18
pjp
int insert_filter(char *, char *);
035
2014-05-01
pjp
036
2014-05-18
pjp
extern void dolog(int, char *, ...);
037
2014-05-18
pjp
extern in_addr_t getmask(int);
038
2014-05-18
pjp
extern int getmask6(int, struct sockaddr_in6 *);
039
2014-05-01
pjp
040
2014-05-01
pjp
extern int debug, verbose;
041
2014-05-01
pjp
042
2014-05-01
pjp
SLIST_HEAD(listhead, filterentry) filterhead;
043
2014-05-01
pjp
044
2014-05-01
pjp
static struct filterentry {
045
2014-05-01
pjp
char name[INET6_ADDRSTRLEN];
046
2014-05-01
pjp
int family;
047
2014-05-01
pjp
struct sockaddr_storage hostmask;
048
2014-05-01
pjp
struct sockaddr_storage netmask;
049
2014-05-01
pjp
u_int8_t prefixlen;
050
2014-05-01
pjp
SLIST_ENTRY(filterentry) filter_entry;
051
2014-05-01
pjp
} *fn2, *fnp;
052
2014-05-01
pjp
053
2014-05-01
pjp
054
2014-05-18
pjp
static const char rcsid[] = "$Id: filter.c,v 1.3 2014/05/18 17:14:05 pjp Exp $";
055
2014-05-01
pjp
056
2014-05-01
pjp
/*
057
2014-05-01
pjp
* INIT_FILTER - initialize the filter singly linked list
058
2014-05-01
pjp
*/
059
2014-05-01
pjp
060
2014-05-01
pjp
void
061
2014-05-01
pjp
init_filter(void)
062
2014-05-01
pjp
{
063
2014-05-01
pjp
SLIST_INIT(&filterhead);
064
2014-05-01
pjp
return;
065
2014-05-01
pjp
}
066
2014-05-01
pjp
067
2014-05-01
pjp
/*
068
2014-05-01
pjp
* INSERT_FILTER - insert an address and prefixlen into the filter slist
069
2014-05-01
pjp
*/
070
2014-05-01
pjp
071
2014-05-01
pjp
int
072
2014-05-01
pjp
insert_filter(char *address, char *prefixlen)
073
2014-05-01
pjp
{
074
2014-05-01
pjp
struct sockaddr_in *sin;
075
2014-05-01
pjp
struct sockaddr_in6 *sin6;
076
2014-05-01
pjp
int pnum;
077
2014-05-01
pjp
int ret;
078
2014-05-01
pjp
079
2014-05-01
pjp
pnum = atoi(prefixlen);
080
2014-05-01
pjp
fn2 = malloc(sizeof(struct filterentry)); /* Insert after. */
081
2014-05-01
pjp
082
2014-05-01
pjp
if (strchr(address, ':') != NULL) {
083
2014-05-01
pjp
fn2->family = AF_INET6;
084
2014-05-01
pjp
sin6 = (struct sockaddr_in6 *)&fn2->hostmask;
085
2014-05-01
pjp
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
086
2014-05-01
pjp
return (-1);
087
2014-05-01
pjp
sin6->sin6_family = AF_INET6;
088
2014-05-01
pjp
sin6 = (struct sockaddr_in6 *)&fn2->netmask;
089
2014-05-01
pjp
sin6->sin6_family = AF_INET6;
090
2014-05-01
pjp
if (getmask6(pnum, sin6) < 0)
091
2014-05-01
pjp
return(-1);
092
2014-05-01
pjp
fn2->prefixlen = pnum;
093
2014-05-01
pjp
} else {
094
2014-05-01
pjp
095
2014-05-01
pjp
fn2->family = AF_INET;
096
2014-05-01
pjp
sin = (struct sockaddr_in *)&fn2->hostmask;
097
2014-05-01
pjp
sin->sin_family = AF_INET;
098
2014-05-01
pjp
sin->sin_addr.s_addr = inet_addr(address);
099
2014-05-01
pjp
sin = (struct sockaddr_in *)&fn2->netmask;
100
2014-05-01
pjp
sin->sin_family = AF_INET;
101
2014-05-01
pjp
sin->sin_addr.s_addr = getmask(pnum);
102
2014-05-01
pjp
fn2->prefixlen = pnum;
103
2014-05-01
pjp
104
2014-05-01
pjp
}
105
2014-05-01
pjp
106
2014-05-01
pjp
SLIST_INSERT_HEAD(&filterhead, fn2, filter_entry);
107
2014-05-01
pjp
108
2014-05-01
pjp
return (0);
109
2014-05-01
pjp
}
110
2014-05-01
pjp
111
2014-05-01
pjp
/*
112
2014-05-01
pjp
* FIND_FILTER - walk the filter list and find the correponding network
113
2014-05-01
pjp
* if a network matches return 1, if no match is found return
114
2014-05-01
pjp
* 0.
115
2014-05-01
pjp
*/
116
2014-05-01
pjp
117
2014-05-01
pjp
int
118
2014-05-01
pjp
find_filter(struct sockaddr_storage *sst, int family)
119
2014-05-01
pjp
{
120
2014-05-01
pjp
struct sockaddr_in *sin, *sin0;
121
2014-05-01
pjp
struct sockaddr_in6 *sin6, *sin60, *sin61;
122
2014-05-01
pjp
u_int32_t hostmask, netmask;
123
2014-05-01
pjp
u_int32_t a;
124
2014-05-01
pjp
#ifdef __amd64
125
2014-05-01
pjp
u_int64_t *hm[2], *nm[2], *a6[2];
126
2014-05-01
pjp
#else
127
2014-05-01
pjp
u_int32_t *hm[4], *nm[4], *a6[4];
128
2014-05-01
pjp
#endif
129
2014-05-01
pjp
130
2014-05-01
pjp
SLIST_FOREACH(fnp, &filterhead, filter_entry) {
131
2014-05-01
pjp
if (fnp->family == AF_INET) {
132
2014-05-01
pjp
if (family != AF_INET)
133
2014-05-01
pjp
continue;
134
2014-05-01
pjp
sin = (struct sockaddr_in *)sst;
135
2014-05-01
pjp
a = sin->sin_addr.s_addr;
136
2014-05-01
pjp
sin = (struct sockaddr_in *)&fnp->hostmask;
137
2014-05-01
pjp
sin0 = (struct sockaddr_in *)&fnp->netmask;
138
2014-05-01
pjp
hostmask = sin->sin_addr.s_addr;
139
2014-05-01
pjp
netmask = sin0->sin_addr.s_addr;
140
2014-05-01
pjp
if ((hostmask & netmask) == (a & netmask)) {
141
2014-05-01
pjp
return (1);
142
2014-05-01
pjp
} /* if hostmask */
143
2014-05-01
pjp
} else if (fnp->family == AF_INET6) {
144
2014-05-01
pjp
if (family != AF_INET6)
145
2014-05-01
pjp
continue;
146
2014-05-01
pjp
sin6 = (struct sockaddr_in6 *)sst;
147
2014-05-01
pjp
sin60 = (struct sockaddr_in6 *)&fnp->hostmask;
148
2014-05-01
pjp
sin61 = (struct sockaddr_in6 *)&fnp->netmask;
149
2014-05-01
pjp
#ifdef __amd64
150
2014-05-01
pjp
/*
151
2014-05-01
pjp
* If this is on a 64 bit machine, we'll benefit
152
2014-05-01
pjp
* by using 64 bit registers, this should make it
153
2014-05-01
pjp
* a tad faster...
154
2014-05-01
pjp
*/
155
2014-05-01
pjp
hm[0] = (u_int64_t *)&sin60->sin6_addr.s6_addr;
156
2014-05-01
pjp
hm[1] = (hm[0] + 1);
157
2014-05-01
pjp
nm[0] = (u_int64_t *)&sin61->sin6_addr.s6_addr;
158
2014-05-01
pjp
nm[1] = (nm[0] + 1);
159
2014-05-01
pjp
a6[0] = (u_int64_t *)&sin6->sin6_addr.s6_addr;
160
2014-05-01
pjp
a6[1] = (a6[0] + 1);
161
2014-05-01
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
162
2014-05-01
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
163
2014-05-01
pjp
#else
164
2014-05-01
pjp
hm[0] = (u_int32_t *)&sin60->sin6_addr.s6_addr;
165
2014-05-01
pjp
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
166
2014-05-01
pjp
hm[3] = (hm[2] + 1);
167
2014-05-01
pjp
nm[0] = (u_int32_t *)&sin61->sin6_addr.s6_addr;
168
2014-05-01
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
169
2014-05-01
pjp
nm[3] = (nm[2] + 1);
170
2014-05-01
pjp
a6[0] = (u_int32_t *)&sin6->sin6_addr.s6_addr;
171
2014-05-01
pjp
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
172
2014-05-01
pjp
a6[3] = (a6[2] + 1);
173
2014-05-01
pjp
174
2014-05-01
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
175
2014-05-01
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
176
2014-05-01
pjp
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
177
2014-05-01
pjp
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
178
2014-05-01
pjp
#endif
179
2014-05-01
pjp
180
2014-05-01
pjp
return (1);
181
2014-05-01
pjp
} /* if ip6 address */
182
2014-05-01
pjp
183
2014-05-01
pjp
} /* if AF_INET6 */
184
2014-05-01
pjp
} /* SLIST */
185
2014-05-01
pjp
186
2014-05-01
pjp
return (0);
187
2014-05-01
pjp
}
repomaster@centroid.eu