Blame
Date:
Thu Oct 15 07:11:48 2020 UTC
Message:
up the debug a bit further while we're chasing a bug.
001
2020-08-26
pjp
/*
002
2020-08-26
pjp
* Copyright (c) 2020 Peter J. Philipp
003
2020-08-26
pjp
* All rights reserved.
004
2020-08-26
pjp
*
005
2020-08-26
pjp
* Redistribution and use in source and binary forms, with or without
006
2020-08-26
pjp
* modification, are permitted provided that the following conditions
007
2020-08-26
pjp
* are met:
008
2020-08-26
pjp
* 1. Redistributions of source code must retain the above copyright
009
2020-08-26
pjp
* notice, this list of conditions and the following disclaimer.
010
2020-08-26
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2020-08-26
pjp
* notice, this list of conditions and the following disclaimer in the
012
2020-08-26
pjp
* documentation and/or other materials provided with the distribution.
013
2020-08-26
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2020-08-26
pjp
* derived from this software without specific prior written permission
015
2020-08-26
pjp
*
016
2020-08-26
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2020-08-26
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2020-08-26
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2020-08-26
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2020-08-26
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2020-08-26
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2020-08-26
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2020-08-26
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2020-08-26
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2020-08-26
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2020-08-26
pjp
*
027
2020-08-26
pjp
*/
028
2020-08-26
pjp
029
2020-08-26
pjp
/*
030
2020-10-07
pjp
* $Id: zone.c,v 1.2 2020/10/07 06:42:44 pjp Exp $
031
2020-08-26
pjp
*/
032
2020-08-26
pjp
033
2020-08-26
pjp
#include <sys/types.h>
034
2020-08-26
pjp
#include <sys/socket.h>
035
2020-08-26
pjp
036
2020-08-26
pjp
#include <netinet/in.h>
037
2020-08-26
pjp
#include <arpa/inet.h>
038
2020-08-26
pjp
#include <netdb.h>
039
2020-08-26
pjp
040
2020-08-26
pjp
#include <stdio.h>
041
2020-08-26
pjp
#include <stdlib.h>
042
2020-08-26
pjp
#include <string.h>
043
2020-08-26
pjp
#include <errno.h>
044
2020-08-26
pjp
#include <syslog.h>
045
2020-08-26
pjp
#include <unistd.h>
046
2020-08-26
pjp
047
2020-08-26
pjp
#ifdef __linux__
048
2020-08-26
pjp
#include <grp.h>
049
2020-08-26
pjp
#define __USE_BSD 1
050
2020-08-26
pjp
#include <endian.h>
051
2020-08-26
pjp
#include <bsd/stdlib.h>
052
2020-08-26
pjp
#include <bsd/string.h>
053
2020-08-26
pjp
#include <bsd/sys/queue.h>
054
2020-08-26
pjp
#define __unused
055
2020-08-26
pjp
#include <bsd/sys/tree.h>
056
2020-08-26
pjp
#include <bsd/sys/endian.h>
057
2020-08-26
pjp
#else /* not linux */
058
2020-08-26
pjp
#include <sys/queue.h>
059
2020-08-26
pjp
#include <sys/tree.h>
060
2020-08-26
pjp
#endif /* __linux__ */
061
2020-08-26
pjp
062
2020-08-26
pjp
063
2020-08-26
pjp
064
2020-08-26
pjp
#include "ddd-dns.h"
065
2020-08-26
pjp
#include "ddd-db.h"
066
2020-08-26
pjp
067
2020-08-26
pjp
void init_zone(void);
068
2020-08-26
pjp
int insert_zone(char *);
069
2020-08-26
pjp
int have_zone(char *zonename, int zonelen);
070
2020-08-26
pjp
void populate_zone(ddDB *db);
071
2020-08-26
pjp
int zonecmp(struct zoneentry *, struct zoneentry *);
072
2020-08-26
pjp
073
2020-08-26
pjp
extern void dolog(int, char *, ...);
074
2020-08-26
pjp
extern in_addr_t getmask(int);
075
2020-08-26
pjp
extern int getmask6(int, struct sockaddr_in6 *);
076
2020-08-26
pjp
extern char * dns_label(char *, int *);
077
2020-08-26
pjp
extern void ddd_shutdown(void);
078
2020-08-26
pjp
extern int dn_contains(char *name, int len, char *anchorname, int alen);
079
2020-08-26
pjp
080
2020-08-26
pjp
extern int debug, verbose;
081
2020-08-26
pjp
082
2020-08-26
pjp
struct zonetree zonehead = RB_INITIALIZER(&zonehead);
083
2020-08-26
pjp
RB_GENERATE(zonetree, zoneentry, zone_entry, zonecmp);
084
2020-08-26
pjp
085
2020-08-26
pjp
int
086
2020-08-26
pjp
insert_zone(char *zonename)
087
2020-08-26
pjp
{
088
2020-08-26
pjp
struct zoneentry *zep;
089
2020-08-26
pjp
int len;
090
2020-08-26
pjp
char *tmp;
091
2020-08-26
pjp
092
2020-08-26
pjp
if (strlen(zonename) > DNS_MAXNAME) {
093
2020-08-26
pjp
dolog(LOG_INFO, "zonename too long\n");
094
2020-08-26
pjp
return -1;
095
2020-08-26
pjp
}
096
2020-08-26
pjp
097
2020-08-26
pjp
zep = malloc(sizeof(struct zoneentry));
098
2020-08-26
pjp
if (zep == NULL) {
099
2020-08-26
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
100
2020-08-26
pjp
return -1;
101
2020-08-26
pjp
}
102
2020-08-26
pjp
103
2020-08-26
pjp
tmp = dns_label(zonename, &len);
104
2020-08-26
pjp
zep->namelen = len;
105
2020-08-26
pjp
memcpy(zep->name, tmp, len);
106
2020-08-26
pjp
free(tmp);
107
2020-08-26
pjp
108
2020-08-26
pjp
zep->humanname = strdup(zonename);
109
2020-08-26
pjp
if (zep->humanname == NULL) {
110
2020-08-26
pjp
dolog(LOG_INFO, "strdup failed\n");
111
2020-08-26
pjp
return -1;
112
2020-08-26
pjp
}
113
2020-08-26
pjp
114
2020-08-26
pjp
TAILQ_INIT(&zep->walkhead);
115
2020-08-26
pjp
116
2020-08-26
pjp
RB_INSERT(zonetree, &zonehead, zep);
117
2020-08-26
pjp
return (0);
118
2020-08-26
pjp
}
119
2020-08-26
pjp
120
2020-08-26
pjp
void
121
2020-08-26
pjp
populate_zone(ddDB *db)
122
2020-08-26
pjp
{
123
2020-08-26
pjp
struct node *walk;
124
2020-08-26
pjp
struct zoneentry find, *res;
125
2020-08-26
pjp
struct rbtree *rbt = NULL;
126
2020-08-26
pjp
char *p;
127
2020-08-26
pjp
int plen;
128
2020-08-26
pjp
129
2020-08-26
pjp
RB_FOREACH(walk, domaintree, &db->head) {
130
2020-08-26
pjp
rbt = (struct rbtree *)walk->data;
131
2020-08-26
pjp
if (rbt == NULL) {
132
2020-08-26
pjp
continue;
133
2020-08-26
pjp
}
134
2020-08-26
pjp
135
2020-10-07
pjp
res = NULL;
136
2020-08-26
pjp
for (plen = rbt->zonelen, p = rbt->zone; plen > 0;
137
2020-08-26
pjp
p++, plen--) {
138
2020-08-26
pjp
memcpy(find.name, p, plen);
139
2020-08-26
pjp
find.namelen = plen;
140
2020-08-26
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
141
2020-08-26
pjp
break;
142
2020-08-26
pjp
}
143
2020-08-26
pjp
144
2020-08-26
pjp
plen -= *p;
145
2020-08-26
pjp
p += *p;
146
2020-08-26
pjp
}
147
2020-10-07
pjp
148
2020-10-07
pjp
if (res == NULL)
149
2020-10-07
pjp
continue;
150
2020-08-26
pjp
151
2020-08-26
pjp
TAILQ_FOREACH(wep, &res->walkhead, walk_entry) {
152
2020-08-26
pjp
if (wep->rbt == rbt)
153
2020-08-26
pjp
break;
154
2020-08-26
pjp
}
155
2020-08-26
pjp
156
2020-08-26
pjp
if (wep)
157
2020-08-26
pjp
continue;
158
2020-08-26
pjp
159
2020-08-26
pjp
if ((wep = malloc(sizeof(struct walkentry))) == NULL) {
160
2020-08-26
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
161
2020-08-26
pjp
ddd_shutdown();
162
2020-08-26
pjp
sleep(10);
163
2020-08-26
pjp
exit(1);
164
2020-08-26
pjp
}
165
2020-08-26
pjp
166
2020-08-26
pjp
wep->rbt = rbt;
167
2020-08-26
pjp
TAILQ_INSERT_TAIL(&res->walkhead, wep, walk_entry);
168
2020-08-26
pjp
}
169
2020-08-26
pjp
}
170
2020-08-26
pjp
171
2020-08-26
pjp
int
172
2020-08-26
pjp
have_zone(char *zonename, int zonelen)
173
2020-08-26
pjp
{
174
2020-08-26
pjp
struct zoneentry find, *res;
175
2020-08-26
pjp
176
2020-08-26
pjp
memcpy(find.name, zonename, zonelen);
177
2020-08-26
pjp
find.namelen = zonelen;
178
2020-08-26
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
179
2020-08-26
pjp
return 1;
180
2020-08-26
pjp
}
181
2020-08-26
pjp
182
2020-08-26
pjp
return 0;
183
2020-08-26
pjp
}
184
2020-08-26
pjp
185
2020-08-26
pjp
int
186
2020-08-26
pjp
zonecmp(struct zoneentry *e1, struct zoneentry *e2)
187
2020-08-26
pjp
{
188
2020-08-26
pjp
if (e1->namelen == e2->namelen)
189
2020-08-26
pjp
return (memcmp(e1->name, e2->name, e1->namelen));
190
2020-08-26
pjp
else if (e1->namelen < e2->namelen)
191
2020-08-26
pjp
return -1;
192
2020-08-26
pjp
else
193
2020-08-26
pjp
return 1;
194
2020-08-26
pjp
}
repomaster@centroid.eu