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