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-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
#include <sys/types.h>
030
2020-08-26
pjp
#include <sys/socket.h>
031
2020-08-26
pjp
032
2020-08-26
pjp
#include <netinet/in.h>
033
2020-08-26
pjp
#include <arpa/inet.h>
034
2020-08-26
pjp
#include <netdb.h>
035
2020-08-26
pjp
036
2020-08-26
pjp
#include <stdio.h>
037
2020-08-26
pjp
#include <stdlib.h>
038
2020-08-26
pjp
#include <string.h>
039
2020-08-26
pjp
#include <errno.h>
040
2020-08-26
pjp
#include <syslog.h>
041
2020-08-26
pjp
#include <unistd.h>
042
2020-08-26
pjp
043
2020-08-26
pjp
#ifdef __linux__
044
2020-08-26
pjp
#include <grp.h>
045
2020-08-26
pjp
#define __USE_BSD 1
046
2020-08-26
pjp
#include <endian.h>
047
2020-08-26
pjp
#include <bsd/stdlib.h>
048
2020-08-26
pjp
#include <bsd/string.h>
049
2020-08-26
pjp
#include <bsd/sys/queue.h>
050
2020-08-26
pjp
#define __unused
051
2020-08-26
pjp
#include <bsd/sys/tree.h>
052
2020-08-26
pjp
#include <bsd/sys/endian.h>
053
2020-08-26
pjp
#else /* not linux */
054
2020-08-26
pjp
#include <sys/queue.h>
055
2020-08-26
pjp
#include <sys/tree.h>
056
2020-08-26
pjp
#endif /* __linux__ */
057
2020-08-26
pjp
058
2020-08-26
pjp
059
2020-08-26
pjp
060
2020-08-26
pjp
#include "ddd-dns.h"
061
2020-08-26
pjp
#include "ddd-db.h"
062
2020-08-26
pjp
063
2020-08-26
pjp
void init_zone(void);
064
2020-08-26
pjp
int insert_zone(char *);
065
2020-08-26
pjp
int have_zone(char *zonename, int zonelen);
066
2020-08-26
pjp
void populate_zone(ddDB *db);
067
2020-08-26
pjp
int zonecmp(struct zoneentry *, struct zoneentry *);
068
2021-01-05
pjp
struct zoneentry * zone_findzone(struct rbtree *);
069
2020-08-26
pjp
070
2020-08-26
pjp
extern void dolog(int, char *, ...);
071
2020-08-26
pjp
extern char * dns_label(char *, int *);
072
2020-08-26
pjp
extern void ddd_shutdown(void);
073
2020-08-26
pjp
extern int dn_contains(char *name, int len, char *anchorname, int alen);
074
2021-01-05
pjp
extern uint32_t match_zoneglue(struct rbtree *);
075
2020-08-26
pjp
076
2020-08-26
pjp
extern int debug, verbose;
077
2021-01-05
pjp
extern uint32_t zonenumber;
078
2020-08-26
pjp
079
2020-08-26
pjp
struct zonetree zonehead = RB_INITIALIZER(&zonehead);
080
2020-08-26
pjp
RB_GENERATE(zonetree, zoneentry, zone_entry, zonecmp);
081
2020-08-26
pjp
082
2020-08-26
pjp
int
083
2020-08-26
pjp
insert_zone(char *zonename)
084
2020-08-26
pjp
{
085
2020-08-26
pjp
struct zoneentry *zep;
086
2020-08-26
pjp
int len;
087
2020-08-26
pjp
char *tmp;
088
2020-08-26
pjp
089
2020-08-26
pjp
if (strlen(zonename) > DNS_MAXNAME) {
090
2020-08-26
pjp
dolog(LOG_INFO, "zonename too long\n");
091
2020-08-26
pjp
return -1;
092
2020-08-26
pjp
}
093
2020-08-26
pjp
094
2020-08-26
pjp
zep = malloc(sizeof(struct zoneentry));
095
2020-08-26
pjp
if (zep == NULL) {
096
2020-08-26
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
097
2020-08-26
pjp
return -1;
098
2020-08-26
pjp
}
099
2020-08-26
pjp
100
2020-08-26
pjp
tmp = dns_label(zonename, &len);
101
2020-08-26
pjp
zep->namelen = len;
102
2020-08-26
pjp
memcpy(zep->name, tmp, len);
103
2020-08-26
pjp
free(tmp);
104
2020-08-26
pjp
105
2020-08-26
pjp
zep->humanname = strdup(zonename);
106
2020-08-26
pjp
if (zep->humanname == NULL) {
107
2020-08-26
pjp
dolog(LOG_INFO, "strdup failed\n");
108
2020-08-26
pjp
return -1;
109
2020-08-26
pjp
}
110
2021-01-05
pjp
zep->zonenumber = zonenumber;
111
2020-08-26
pjp
112
2020-08-26
pjp
TAILQ_INIT(&zep->walkhead);
113
2020-08-26
pjp
114
2020-08-26
pjp
RB_INSERT(zonetree, &zonehead, zep);
115
2020-08-26
pjp
return (0);
116
2020-08-26
pjp
}
117
2020-08-26
pjp
118
2020-08-26
pjp
void
119
2020-08-26
pjp
populate_zone(ddDB *db)
120
2020-08-26
pjp
{
121
2020-08-26
pjp
struct node *walk;
122
2020-08-26
pjp
struct zoneentry find, *res;
123
2020-08-26
pjp
struct rbtree *rbt = NULL;
124
2020-08-26
pjp
char *p;
125
2020-08-26
pjp
int plen;
126
2020-08-26
pjp
127
2021-01-05
pjp
RB_FOREACH(walk, domaintree, &db->head) {
128
2021-01-05
pjp
rbt = (struct rbtree *)walk->data;
129
2021-01-05
pjp
if (rbt == NULL) {
130
2021-01-05
pjp
continue;
131
2021-01-05
pjp
}
132
2020-08-26
pjp
133
2021-01-05
pjp
res = NULL;
134
2021-01-05
pjp
for (plen = rbt->zonelen, p = rbt->zone; plen > 0;
135
2021-01-05
pjp
p++, plen--) {
136
2021-01-05
pjp
memcpy(find.name, p, plen);
137
2021-01-05
pjp
find.namelen = plen;
138
2021-01-05
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
139
2021-01-05
pjp
break;
140
2020-08-26
pjp
}
141
2020-08-26
pjp
142
2021-01-05
pjp
plen -= *p;
143
2021-01-05
pjp
p += *p;
144
2021-01-05
pjp
}
145
2020-08-26
pjp
146
2021-01-05
pjp
if (res == NULL)
147
2021-01-05
pjp
continue;
148
2021-01-05
pjp
149
2021-01-05
pjp
TAILQ_FOREACH(wep, &res->walkhead, walk_entry) {
150
2021-01-05
pjp
if (wep->rbt == rbt)
151
2021-01-05
pjp
break;
152
2021-01-05
pjp
}
153
2021-01-05
pjp
154
2021-01-05
pjp
if (wep)
155
2021-01-05
pjp
continue;
156
2021-01-05
pjp
157
2021-01-05
pjp
if ((wep = malloc(sizeof(struct walkentry))) == NULL) {
158
2021-01-05
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
159
2021-01-05
pjp
ddd_shutdown();
160
2021-01-05
pjp
sleep(10);
161
2021-01-05
pjp
exit(1);
162
2021-01-05
pjp
}
163
2021-01-05
pjp
164
2021-01-05
pjp
wep->rbt = rbt;
165
2021-01-05
pjp
/* wep->zonenumber = res->zonenumber; */
166
2021-01-05
pjp
TAILQ_INSERT_TAIL(&res->walkhead, wep, walk_entry);
167
2021-01-05
pjp
168
2021-01-05
pjp
/* there is a parent zone that has another entry */
169
2021-01-05
pjp
if (match_zoneglue(rbt)) {
170
2021-01-05
pjp
res = NULL;
171
2021-01-05
pjp
172
2021-01-05
pjp
plen -= *p; /* advance to higher parent */
173
2021-01-05
pjp
p += *p;
174
2021-01-05
pjp
175
2021-01-05
pjp
for (p++, plen--; plen > 0; p++, plen--) {
176
2021-01-05
pjp
memcpy(find.name, p, plen);
177
2021-01-05
pjp
find.namelen = plen;
178
2021-01-05
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
179
2021-01-05
pjp
break;
180
2021-01-05
pjp
}
181
2021-01-05
pjp
182
2021-01-05
pjp
plen -= *p;
183
2021-01-05
pjp
p += *p;
184
2021-01-05
pjp
}
185
2021-01-05
pjp
186
2021-01-05
pjp
if (res == NULL)
187
2021-01-05
pjp
continue;
188
2021-01-05
pjp
189
2021-01-05
pjp
TAILQ_FOREACH(wep, &res->walkhead, walk_entry) {
190
2021-01-05
pjp
if (wep->rbt == rbt)
191
2021-01-05
pjp
break;
192
2021-01-05
pjp
}
193
2021-01-05
pjp
194
2021-01-05
pjp
if (wep)
195
2021-01-05
pjp
continue;
196
2021-01-05
pjp
197
2021-01-05
pjp
if ((wep = malloc(sizeof(struct walkentry))) == NULL) {
198
2021-01-05
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
199
2021-01-05
pjp
ddd_shutdown();
200
2021-01-05
pjp
sleep(10);
201
2021-01-05
pjp
exit(1);
202
2021-01-05
pjp
}
203
2021-01-05
pjp
204
2021-01-05
pjp
wep->rbt = rbt;
205
2021-01-05
pjp
TAILQ_INSERT_TAIL(&res->walkhead, wep, walk_entry);
206
2021-01-05
pjp
}
207
2020-08-26
pjp
}
208
2020-08-26
pjp
}
209
2020-08-26
pjp
210
2020-08-26
pjp
int
211
2020-08-26
pjp
have_zone(char *zonename, int zonelen)
212
2020-08-26
pjp
{
213
2020-08-26
pjp
struct zoneentry find, *res;
214
2020-08-26
pjp
215
2020-08-26
pjp
memcpy(find.name, zonename, zonelen);
216
2020-08-26
pjp
find.namelen = zonelen;
217
2020-08-26
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
218
2020-08-26
pjp
return 1;
219
2020-08-26
pjp
}
220
2020-08-26
pjp
221
2020-08-26
pjp
return 0;
222
2020-08-26
pjp
}
223
2020-08-26
pjp
224
2020-08-26
pjp
int
225
2020-08-26
pjp
zonecmp(struct zoneentry *e1, struct zoneentry *e2)
226
2020-08-26
pjp
{
227
2020-08-26
pjp
if (e1->namelen == e2->namelen)
228
2020-08-26
pjp
return (memcmp(e1->name, e2->name, e1->namelen));
229
2020-08-26
pjp
else if (e1->namelen < e2->namelen)
230
2020-08-26
pjp
return -1;
231
2020-08-26
pjp
else
232
2020-08-26
pjp
return 1;
233
2021-01-05
pjp
}
234
2021-01-05
pjp
235
2021-01-05
pjp
/*
236
2021-01-05
pjp
* ZONE_FINDZONE - find the closest zonenumber and return its zoneentry or NULL
237
2021-01-05
pjp
*
238
2021-01-05
pjp
*/
239
2021-01-05
pjp
240
2021-01-05
pjp
struct zoneentry *
241
2021-01-05
pjp
zone_findzone(struct rbtree *rbt)
242
2021-01-05
pjp
{
243
2021-01-05
pjp
struct zoneentry find, *res;
244
2021-01-05
pjp
char *p;
245
2021-01-05
pjp
int plen;
246
2021-01-05
pjp
247
2021-01-05
pjp
/* find the corresponding zone for the zone number */
248
2021-01-05
pjp
for (plen = rbt->zonelen, p = rbt->zone; plen > 0; p++, plen--) {
249
2021-01-05
pjp
memcpy(find.name, p, plen);
250
2021-01-05
pjp
find.namelen = plen;
251
2021-01-05
pjp
252
2021-01-05
pjp
if ((res = RB_FIND(zonetree, &zonehead, &find)) != NULL) {
253
2021-01-05
pjp
return (res);
254
2021-01-05
pjp
}
255
2021-01-05
pjp
plen -= *p;
256
2021-01-05
pjp
p += *p;
257
2021-01-05
pjp
}
258
2021-01-05
pjp
259
2021-01-05
pjp
return (NULL);
260
2020-08-26
pjp
}
repomaster@centroid.eu