aboutsummaryrefslogtreecommitdiffstats
path: root/main/gdnsd/geoip-autodc.patch
blob: ae313ef38e3480781810c6818ea7667721e1c025 (plain) (blame)
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
From 8f172f642d4aa3a30f5a356a4611f66227e74681 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Tue, 21 Aug 2012 16:45:45 +0300
Subject: [PATCH] plugin_geoip: allow datacenters to be omitted from
 auto_dc_coords
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Those datacenters will be never returned automatically. They are
available only from specific territories (map overrides) or subnets
(net overrides).

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
---
 plugins/meta/libgdmaps/gdmaps.c                | 46 +++++++++++++++-----------
 plugins/meta/libgdmaps/t/Makefile.am           |  3 +-
 plugins/meta/libgdmaps/t/t14_missingcoords.c   | 42 +++++++++++++++++++++++
 plugins/meta/libgdmaps/t/t14_missingcoords.cfg | 29 ++++++++++++++++
 4 files changed, 100 insertions(+), 20 deletions(-)
 create mode 100644 plugins/meta/libgdmaps/t/t14_missingcoords.c
 create mode 100644 plugins/meta/libgdmaps/t/t14_missingcoords.cfg

diff --git a/plugins/meta/libgdmaps/gdmaps.c b/plugins/meta/libgdmaps/gdmaps.c
index be23d9b..b367e6f 100644
--- a/plugins/meta/libgdmaps/gdmaps.c
+++ b/plugins/meta/libgdmaps/gdmaps.c
@@ -148,6 +148,7 @@ static dcinfo_t* dcinfo_new(const vscf_data_t* dc_cfg, const vscf_data_t* dc_aut
     dcinfo_t* info = malloc(sizeof(dcinfo_t));
 
     const unsigned num_dcs = vscf_array_get_len(dc_cfg);
+    unsigned num_auto = 0;
     if(!num_dcs)
         log_fatal("plugin_geoip: map '%s': 'datacenters' must be an array of one or more strings", map_name);
     if(num_dcs > 254)
@@ -164,34 +165,26 @@ static dcinfo_t* dcinfo_new(const vscf_data_t* dc_cfg, const vscf_data_t* dc_aut
             log_fatal("plugin_geoip: map '%s': datacenter name 'auto' is illegal", map_name);
     }
 
-    if(dc_auto_limit_cfg) {
-        unsigned long auto_limit_ul;
-        if(!vscf_is_simple(dc_auto_limit_cfg) || !vscf_simple_get_as_ulong(dc_auto_limit_cfg, &auto_limit_ul))
-            log_fatal("plugin_geoip: map '%s': auto_dc_limit must be a single unsigned integer value", map_name);
-        if(auto_limit_ul > num_dcs || !auto_limit_ul)
-            auto_limit_ul = num_dcs;
-        info->auto_limit = auto_limit_ul;
-    }
-    else {
-        info->auto_limit = (num_dcs > 3) ? 3 : num_dcs;
-    }
-
     if(dc_auto_cfg) {
         if(!vscf_is_hash(dc_auto_cfg))
             log_fatal("plugin_geoip: map '%s': auto_dc_coords must be a key-value hash", map_name);
-        const unsigned num_auto = vscf_hash_get_len(dc_auto_cfg);
-        if(num_auto != num_dcs)
-            log_fatal("plugin_geoip: map '%s': auto_dc_coords hash must contain one entry for each datacenter in 'datacenters'", map_name);
-        info->coords = malloc(num_auto * 2 * sizeof(double));
+        num_auto = vscf_hash_get_len(dc_auto_cfg);
+        if (info->auto_limit > num_auto)
+            info->auto_limit = num_auto;
+        info->coords = malloc(num_dcs * 2 * sizeof(double));
+        for(unsigned i = 0; i < 2*num_dcs; i++)
+            info->coords[i] = NAN;
         for(unsigned i = 0; i < num_auto; i++) {
             const char* dcname = vscf_hash_get_key_byindex(dc_auto_cfg, i, NULL);
             unsigned dcidx;
-            for(dcidx = 0; dcidx < info->num_dcs; dcidx++) {
+            for(dcidx = 0; dcidx < num_dcs; dcidx++) {
                 if(!strcmp(dcname, info->names[dcidx]))
                     break;
             }
-            if(dcidx == info->num_dcs)
+            if(dcidx == num_dcs)
                 log_fatal("plugin_geoip: map '%s': auto_dc_coords key '%s' not matched from 'datacenters' list", map_name, dcname);
+            if(!isnan(info->coords[(dcidx*2)]))
+                log_fatal("plugin_geoip: map '%s': auto_dc_coords key '%s' defined twice", map_name, dcname);
             const vscf_data_t* coord_cfg = vscf_hash_get_data_byindex(dc_auto_cfg, i);
             const vscf_data_t* lat_cfg;
             const vscf_data_t* lon_cfg;
@@ -216,6 +209,18 @@ static dcinfo_t* dcinfo_new(const vscf_data_t* dc_cfg, const vscf_data_t* dc_aut
         info->coords = NULL;
     }
 
+    if(dc_auto_limit_cfg) {
+        unsigned long auto_limit_ul;
+        if(!vscf_is_simple(dc_auto_limit_cfg) || !vscf_simple_get_as_ulong(dc_auto_limit_cfg, &auto_limit_ul))
+            log_fatal("plugin_geoip: map '%s': auto_dc_limit must be a single unsigned integer value", map_name);
+        if(auto_limit_ul > num_auto || !auto_limit_ul)
+            auto_limit_ul = num_auto;
+        info->auto_limit = auto_limit_ul;
+    }
+    else {
+        info->auto_limit = (num_auto > 3) ? 3 : num_auto;
+    }
+
     return info;
 }
 
@@ -438,7 +443,10 @@ static unsigned dclists_city_auto_map(dclists_t* lists, const char* map_name, co
     double dists[store_len];
     for(unsigned i = 0; i < num_dcs; i++) {
         const unsigned c_offs = i * 2;
-        dists[i + 1] = haversine(lat_rad, lon_rad, coords[c_offs], coords[c_offs + 1]);
+        if (!isnan(coords[c_offs]))
+            dists[i + 1] = haversine(lat_rad, lon_rad, coords[c_offs], coords[c_offs + 1]);
+        else
+            dists[i + 1] = +INFINITY;
     }
 
     // Given the relatively small num_dcs of most configs,
diff --git a/plugins/meta/libgdmaps/t/Makefile.am b/plugins/meta/libgdmaps/t/Makefile.am
index f83592f..e9d26ce 100644
--- a/plugins/meta/libgdmaps/t/Makefile.am
+++ b/plugins/meta/libgdmaps/t/Makefile.am
@@ -41,7 +41,8 @@ TESTLIST = \
 	t10_def \
 	t11_def2 \
 	t12_defnone \
-	t13_castatdef
+	t13_castatdef \
+	t14_missingcoords
 
 check_PROGRAMS = $(TESTLIST:=.bin)
 
diff --git a/plugins/meta/libgdmaps/t/t14_missingcoords.c b/plugins/meta/libgdmaps/t/t14_missingcoords.c
new file mode 100644
index 0000000..9329ee3
--- /dev/null
+++ b/plugins/meta/libgdmaps/t/t14_missingcoords.c
@@ -0,0 +1,42 @@
+/* Copyright © 2012 Brandon L Black <blblack@gmail.com>
+ *
+ * This file is part of gdnsd-plugin-geoip.
+ *
+ * gdnsd-plugin-geoip 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * gdnsd-plugin-geoip 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 gdnsd.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+// Unit test for gdmaps
+
+#include "config.h"
+#include <gdnsd-log.h>
+#include "gdmaps_test.h"
+
+int main(int argc, char* argv[]) {
+    if(argc != 2)
+        log_fatal("root directory must be set on commandline");
+
+    gdmaps_t* gdmaps = gdmaps_test_init(argv[1]);
+    unsigned tnum = 0;
+    //datacenters => [ us, ie, sg, tr, br ]
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "137.138.144.168", "\2\1\5\3", 16); // Geneva
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "69.58.186.119", "\1\2\5\3", 16); // US East Coast
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "117.53.170.202", "\3\5\1\2", 20); // Australia
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "133.11.114.194", "\2\4", 8); // JP, horrible custom 'map' entry
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "10.0.0.44", "\4\2", 24); // Custom 'nets' entry
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "10.0.1.44", "", 24); // Custom 'nets' entry, empty
+    gdmaps_test_lookup_check(tnum++, gdmaps, "my_prod_map", "192.168.1.1", "\1\2\3\4\5", 16); // meta-default, no loc
+    gdmaps_destroy(gdmaps);
+}
+
diff --git a/plugins/meta/libgdmaps/t/t14_missingcoords.cfg b/plugins/meta/libgdmaps/t/t14_missingcoords.cfg
new file mode 100644
index 0000000..8c57cdd
--- /dev/null
+++ b/plugins/meta/libgdmaps/t/t14_missingcoords.cfg
@@ -0,0 +1,29 @@
+options => { debug => true }
+plugins => {
+ geoip => {
+  maps => {
+   # bringing it all together: city-auto w/ 5 dcs,
+   #  dual GeoIP inputs, custom maps, custom nets
+   #  testing with one datacenter not used in auto coords
+   my_prod_map => {
+    geoip_db => GeoLiteCityv6-20111210.dat,
+    geoip_db_v4_overlay => GeoLiteCity-20111210.dat,
+    datacenters => [ us, ie, sg, tr, br ]
+    auto_dc_limit => 5,
+    auto_dc_coords => {
+     us = [ 38.9, -77 ]
+     ie = [ 53.3, -6.3 ]
+     sg = [ 1.3, 103.9 ]
+     br = [ -22.9, -43.2 ]
+    }
+    map => {
+     AS => { JP => [ ie, tr ] }
+    }
+    nets => {
+     10.0.1.0/24 => [ ]
+     10.0.0.0/24 => [ tr, ie ]
+    }
+   }
+  }
+ }
+}
-- 
1.7.12