1 /* Copyright © 2012 Brandon L Black <blblack@gmail.com>
2  *
3  * This file is part of gdnsd.
4  *
5  * gdnsd-plugin-geoip is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * gdnsd-plugin-geoip is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with gdnsd.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef DCLISTS_H
21 #define DCLISTS_H
22 
23 #include "dcinfo.h"
24 
25 #include <gdnsd/compiler.h>
26 #include <gdnsd/vscf.h>
27 
28 #include <inttypes.h>
29 #include <stdbool.h>
30 
31 typedef struct _dclists dclists_t;
32 
33 typedef enum {
34    KILL_NO_LISTS,
35    KILL_ALL_LISTS,
36    KILL_NEW_LISTS
37 } dclists_destroy_depth_t;
38 
39 // At the nlist/ntree layer, a uint32_t node reference has the high bit set
40 //   if it's a dclist, and cleared if it's an internal tree-node reference.
41 // This implies that legal, real dclist indices used in the construction of
42 //   nlist_t and ntree_t cannot have the high bit set, ever, and thus our
43 //   list of real dclist indices is a 31-bit value.
44 // As a further constraint, the maximal 31-bit value is not allowed for a real
45 //   dclist index because it is used in two magic ways:
46 //   1) At the dclists/dcmap/gdgeoip layer it's used to signal automatic
47 //     distance-mapped mapping (DCLIST_AUTO below), which will be translated
48 //     to a real dclist index before passing to the nlist/tree layer.
49 //   2) NN_UNDEF is the 'undefined' case at the nlist/tree layer and is the
50 //     value UINT32_MAX, and we don't want a legit dclist index, when OR'd
51 //     with the high-bit at the ntree layer, to overlap with this definition
52 //     of NN_UNDEF.
53 // Therefore:
54 #define DCLIST_AUTO 0x7FFFFFFF
55 #define DCLIST_MAX  0x7FFFFFFE
56 
57 F_NONNULL F_WUNUSED
58 dclists_t* dclists_new(const dcinfo_t* info);
59 F_NONNULL F_WUNUSED
60 dclists_t* dclists_clone(const dclists_t* old);
61 F_NONNULL F_PURE
62 unsigned dclists_get_count(const dclists_t* lists);
63 F_NONNULL F_PURE
64 const uint8_t* dclists_get_list(const dclists_t* lists, const uint32_t idx);
65 F_NONNULL
66 void dclists_replace_list0(dclists_t* lists, uint8_t* newlist);
67 
68 // retval here: true -> "auto", false -> normal list
69 F_NONNULL
70 bool dclists_xlate_vscf(dclists_t* lists, vscf_data_t* vscf_list, const char* map_name, uint8_t* newlist, const bool allow_auto);
71 
72 F_NONNULL
73 uint32_t dclists_find_or_add_vscf(dclists_t* lists, vscf_data_t* vscf_list, const char* map_name, const bool allow_auto);
74 F_NONNULL
75 uint32_t dclists_city_auto_map(dclists_t* lists, const char* map_name, const double lat, const double lon);
76 F_NONNULL
77 void dclists_destroy(dclists_t* lists, dclists_destroy_depth_t depth);
78 
79 #endif // DCLISTS_H
80