-
Notifications
You must be signed in to change notification settings - Fork 7
/
rbldnsd_dnhash_fixed.c
215 lines (170 loc) · 4.86 KB
/
rbldnsd_dnhash_fixed.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* RBLDNSD
* Copyright (C) 2019 Vsevolod Stakhov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
/*
* Hashed dataset for fixed sized keys (defined by FIXED_HASHLEN macro)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "khash.h"
#include "rbldnsd.h"
#ifndef FIXED_HASHLEN
#define FIXED_HASHLEN 32
#endif
static inline int
key_hash_func(u_char *key)
{
int k;
memcpy (&k, key, sizeof (k));
return k;
}
static inline int
key_eq_func(u_char *k1, u_char *k2)
{
return memcmp(k1, k2, FIXED_HASHLEN) == 0;
}
KHASH_INIT(dnhash, u_char *, const char *, 1, key_hash_func, key_eq_func);
/* There are two similar arrays -
* for plain entries and for wildcard entries.
*/
struct dsdata {
khash_t(dnhash) *h;
const char *def_rr; /* default A and TXT RRs */
};
#define STRINGIFY_ARG(macro_or_string) #macro_or_string
#define STRINGIFY(macro_or_string) STRINGIFY_ARG(macro_or_string)
definedstype(dnhash_fixed, 0, "set of (domain name, value) pairs, fixed hash version (" STRINGIFY(FIXED_HASHLEN) " bytes)");
static void ds_dnhash_fixed_reset(struct dsdata *dsd, int UNUSED unused_freeall) {
kh_destroy(dnhash, dsd->h);
}
static void ds_dnhash_fixed_start(struct dataset *ds) {
struct dsdata *dsd = ds->ds_dsd;
ds->ds_dsd->def_rr = def_rr;
dsd->h = kh_init(dnhash);
}
static int
ds_dnhash_fixed_addent(struct dsdata *d,
const unsigned char *ldn, const char *rr,
unsigned dnlen) {
khiter_t k;
int ret;
u_char key[FIXED_HASHLEN];
const char **e;
if (*ldn == FIXED_HASHLEN) {
memcpy (key, ldn + 1, FIXED_HASHLEN);
k = kh_put(dnhash, d->h, key, &ret);
if (ret < 0) {
return 0;
}
e = &kh_value(d->h, k);
*e = rr;
}
else {
return 0;
}
return 1;
}
static int
ds_dnhash_fixed_line(struct dataset *ds, char *s, struct dsctx *dsc) {
struct dsdata *dsd = ds->ds_dsd;
unsigned char dn[DNS_MAXDN];
const char *rr;
unsigned char *ldn;
unsigned dnlen, size;
if (*s == ':') { /* default entry */
if (!(size = parse_a_txt(s, &rr, def_rr, dsc)))
return 1;
if (!(dsd->def_rr = mp_dmemdup(ds->ds_mp, rr, size)))
return 0;
return 1;
}
/* check negation */
if (*s == '!') {
return 0;
}
/* disallow emptry DN to be listed (i.e. "all"?) */
if (!(s = parse_dn(s, dn, &dnlen)) || dnlen == 1) {
dswarn(dsc, "invalid domain name");
return 1;
}
dns_dntol(dn, dn); /* lowercase */
/* else parse rest */
SKIPSPACE(s);
if (!*s || ISCOMMENT(*s)) /* use default if none given */
rr = dsd->def_rr;
else if (!(size = parse_a_txt(s, &rr, dsd->def_rr, dsc)))
return 1;
else if (!(rr = mp_dmemdup(ds->ds_mp, rr, size)))
return 0;
ldn = (unsigned char*)mp_alloc(ds->ds_mp, dnlen, 0);
if (!ldn)
return 0;
memcpy(ldn, dn, dnlen);
if (!ds_dnhash_fixed_addent(dsd, ldn, rr, dnlen - 1)) {
dswarn(dsc, "invalid domain name: %s", ldn + 1);
return 0;
}
return 1;
}
static void ds_dnhash_fixed_finish(struct dataset *ds, struct dsctx *dsc) {
struct dsdata *dsd = ds->ds_dsd;
dsloaded(dsc, "e=%u", kh_size(dsd->h));
}
static int
ds_dnhash_fixed_query(const struct dataset *ds, const struct dnsqinfo *qi,
struct dnspacket *pkt) {
const struct dsdata *dsd = ds->ds_dsd;
const unsigned char *dn = qi->qi_dn;
unsigned qlab = qi->qi_dnlab;
const char *e;
char name[DNS_MAXDOMAIN+1];
khiter_t k;
u_char srch[FIXED_HASHLEN];
if (!qlab) return 0; /* do not match empty dn */
check_query_overwrites(qi);
if (*dn != FIXED_HASHLEN) {
return 0;
}
memcpy(srch, dn + 1, FIXED_HASHLEN);
k = kh_get(dnhash, dsd->h, srch);
if (k != kh_end(dsd->h)) {
e = kh_value(dsd->h, k);
if (qi->qi_tflag & NSQUERY_TXT) {
dns_dntop(dn + 1, name, sizeof(name));
}
addrr_a_txt(pkt, qi->qi_tflag, e, name, ds);
return NSQUERY_FOUND;
}
return 0;
}
#ifndef NO_MASTER_DUMP
static void
ds_dnhash_fixed_dump(const struct dataset *ds,
const unsigned char UNUSED *unused_odn,
FILE *f) {
const struct dsdata *dsd = ds->ds_dsd;
const char *e;
u_char *ldn;
char name[DNS_MAXDOMAIN+4];
kh_foreach(dsd->h, ldn, e, {
dns_dntop(ldn, name, sizeof(name));
dump_a_txt(name, e, name, ds, f);
});
}
#endif