summaryrefslogtreecommitdiffstats
path: root/src/edns_ecs_index.c
blob: 48e2bbc543d97008841904ea52a49de2c7e0d4c3 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (c) 2008-2024 OARC, Inc.
 * Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
 * Copyright (c) 2003-2007, The Measurement Factory, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "edns_ecs_index.h"
#include "xmalloc.h"
#include "hashtbl.h"
#include "inX_addr.h"

#include <string.h>
#include <sys/socket.h> // For AF_ on BSDs

// edns_ecs

int edns_ecs_indexer(const dns_message* m)
{
    if (m->malformed)
        return -1;
    return m->edns.option.ecs;
}

static int next_iter;

int edns_ecs_iterator(const char** label)
{
    if (NULL == label) {
        next_iter = 0;
        return 2;
    }
    if (next_iter > 1)
        return -1;
    if (next_iter)
        *label = "yes";
    else
        *label = "no";
    return next_iter++;
}

// edns_ecs_family

static int family_largest = 0;

int edns_ecs_family_indexer(const dns_message* m)
{
    if (m->malformed)
        return -1;
    // family + 1 because ECS can have family 0 and idx 0 is none
    if (!m->edns.option.ecs)
        return 0;
    if (m->edns.ecs.family + 1 > family_largest)
        family_largest = m->edns.ecs.family + 1;
    return m->edns.ecs.family + 1;
}

static int family_next_iter;

int edns_ecs_family_iterator(const char** label)
{
    static char label_buf[20];
    if (NULL == label) {
        family_next_iter = 0;
        return family_largest + 1;
    }
    if (family_next_iter > family_largest)
        return -1;
    if (family_next_iter == 0)
        snprintf(label_buf, sizeof(label_buf), "none");
    else
        snprintf(label_buf, sizeof(label_buf), "%d", family_next_iter - 1);
    *label = label_buf;
    return family_next_iter++;
}

void edns_ecs_family_reset()
{
    family_largest = 0;
}

// edns_ecs_source_prefix

static int source_prefix_largest = 0;

int edns_ecs_source_prefix_indexer(const dns_message* m)
{
    if (m->malformed)
        return -1;
    // source_prefix + 1 because ECS can have source_prefix 0 and idx 0 is none
    if (!m->edns.option.ecs)
        return 0;
    if (m->edns.ecs.source_prefix + 1 > source_prefix_largest)
        source_prefix_largest = m->edns.ecs.source_prefix + 1;
    return m->edns.ecs.source_prefix + 1;
}

static int source_prefix_next_iter;

int edns_ecs_source_prefix_iterator(const char** label)
{
    static char label_buf[20];
    if (NULL == label) {
        source_prefix_next_iter = 0;
        return source_prefix_largest + 1;
    }
    if (source_prefix_next_iter > source_prefix_largest)
        return -1;
    if (source_prefix_next_iter == 0)
        snprintf(label_buf, sizeof(label_buf), "none");
    else
        snprintf(label_buf, sizeof(label_buf), "%d", source_prefix_next_iter - 1);
    *label = label_buf;
    return source_prefix_next_iter++;
}

void edns_ecs_source_prefix_reset()
{
    source_prefix_largest = 0;
}

// edns_ecs_scope_prefix

static int scope_prefix_largest = 0;

int edns_ecs_scope_prefix_indexer(const dns_message* m)
{
    if (m->malformed)
        return -1;
    // scope_prefix + 1 because ECS can have scope_prefix 0 and idx 0 is none
    if (!m->edns.option.ecs)
        return 0;
    if (m->edns.ecs.scope_prefix + 1 > scope_prefix_largest)
        scope_prefix_largest = m->edns.ecs.scope_prefix + 1;
    return m->edns.ecs.scope_prefix + 1;
}

static int scope_prefix_next_iter;

int edns_ecs_scope_prefix_iterator(const char** label)
{
    static char label_buf[20];
    if (NULL == label) {
        scope_prefix_next_iter = 0;
        return scope_prefix_largest + 1;
    }
    if (scope_prefix_next_iter > scope_prefix_largest)
        return -1;
    if (scope_prefix_next_iter == 0)
        snprintf(label_buf, sizeof(label_buf), "none");
    else
        snprintf(label_buf, sizeof(label_buf), "%d", scope_prefix_next_iter - 1);
    *label = label_buf;
    return scope_prefix_next_iter++;
}

void edns_ecs_scope_prefix_reset()
{
    scope_prefix_largest = 0;
}

// edns_ecs_address

#define MAX_ARRAY_SZ 65536

typedef struct
{
    const void* address;
    size_t      len;
} addresskey;

typedef struct
{
    addresskey key;
    void*      address;
    int        index;
} addressobj;

static unsigned int address_hashfunc(const void* key)
{
    return hashendian(((addresskey*)key)->address, ((addresskey*)key)->len, 0);
}

static int address_cmpfunc(const void* a, const void* b)
{
    if (((addresskey*)a)->len == ((addresskey*)b)->len) {
        return memcmp(((addresskey*)a)->address, ((addresskey*)b)->address, ((addresskey*)a)->len);
    }
    return ((addresskey*)a)->len < ((addresskey*)b)->len ? -1 : 1;
}

static void address_freefunc(void* obj)
{
    if (obj)
        afree(((addressobj*)obj)->address);
    afree(obj);
}

static hashtbl* addressHash      = NULL;
static int      address_next_idx = 0;

int edns_ecs_address_indexer(const dns_message* m)
{
    addressobj* obj;
    if (m->malformed || !m->edns.ecs.address)
        return -1;
    addresskey key = { m->edns.ecs.address, m->edns.ecs.len };
    if (NULL == addressHash) {
        addressHash = hash_create(MAX_ARRAY_SZ, address_hashfunc, address_cmpfunc, 1, 0, address_freefunc);
        if (NULL == addressHash)
            return -1;
    }
    if ((obj = hash_find(&key, addressHash)))
        return obj->index;
    obj = acalloc(1, sizeof(*obj));
    if (NULL == obj)
        return -1;
    obj->address = amalloc(m->edns.ecs.len);
    if (NULL == obj->address) {
        afree(obj);
        return -1;
    }
    obj->key.len     = m->edns.ecs.len;
    obj->key.address = obj->address;
    memcpy(obj->address, m->edns.ecs.address, obj->key.len);
    obj->index = address_next_idx;
    if (0 != hash_add(&obj->key, obj, addressHash)) {
        afree(obj->address);
        afree(obj);
        return -1;
    }
    address_next_idx++;
    return obj->index;
}

int edns_ecs_address_iterator(const char** label)
{
    static char label_buf[1024];
    addressobj* obj;
    if (0 == address_next_idx)
        return -1;
    if (NULL == label) {
        /* initialize and tell caller how big the array is */
        hash_iter_init(addressHash);
        return address_next_idx;
    }
    if ((obj = hash_iterate(addressHash)) == NULL)
        return -1;
    size_t len = obj->key.len;
    if (len > 128)
        len = 128;
    strtohex(label_buf, obj->key.address, len);
    label_buf[len * 2] = 0;
    *label             = label_buf;
    return obj->index;
}

void edns_ecs_address_reset()
{
    addressHash      = NULL;
    address_next_idx = 0;
}

// edns_ecs_subnet

static hashtbl* subnetHash      = NULL;
static int      subnet_next_idx = 0;

typedef struct
{
    inX_addr addr;
    int      index;
} subnetobj;

static unsigned int
subnet_hashfunc(const void* key)
{
    return inXaddr_hash((const inX_addr*)key);
}

static int
subnet_cmpfunc(const void* a, const void* b)
{
    return inXaddr_cmp((const inX_addr*)a, (const inX_addr*)b);
}

int edns_ecs_subnet_indexer(const dns_message* m)
{
    subnetobj* obj;
    inX_addr   addr = { 0 };

    if (m->malformed || !m->edns.ecs.address)
        return -1;
    switch (m->edns.ecs.family) { // IANA Address Family Numbers
    case 1:
        if (m->edns.ecs.len > sizeof(addr.in4))
            return -1;
        addr.family = AF_INET;
        memcpy(&addr.in4, m->edns.ecs.address, m->edns.ecs.len);
        break;
    case 2:
        if (m->edns.ecs.len > sizeof(addr.in6))
            return -1;
        addr.family = AF_INET6;
        memcpy(&addr.in6, m->edns.ecs.address, m->edns.ecs.len);
        break;
    default:
        return -1;
    }
    if (NULL == subnetHash) {
        subnetHash = hash_create(MAX_ARRAY_SZ, subnet_hashfunc, subnet_cmpfunc, 1, NULL, afree);
        if (NULL == subnetHash)
            return -1;
    }
    if ((obj = hash_find(&addr, subnetHash)))
        return obj->index;
    obj = acalloc(1, sizeof(*obj));
    if (NULL == obj)
        return -1;
    obj->addr  = addr;
    obj->index = subnet_next_idx;
    if (0 != hash_add(&obj->addr, obj, subnetHash)) {
        afree(obj);
        return -1;
    }
    subnet_next_idx++;
    return obj->index;
}

int edns_ecs_subnet_iterator(const char** label)
{
    subnetobj*  obj;
    static char label_buf[128];
    if (0 == subnet_next_idx)
        return -1;
    if (NULL == label) {
        hash_iter_init(subnetHash);
        return subnet_next_idx;
    }
    if ((obj = hash_iterate(subnetHash)) == NULL)
        return -1;
    inXaddr_ntop(&obj->addr, label_buf, 128);
    *label = label_buf;
    return obj->index;
}

void edns_ecs_subnet_reset()
{
    subnetHash      = NULL;
    subnet_next_idx = 0;
}