From 8f172f642d4aa3a30f5a356a4611f66227e74681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= 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 --- 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 + * + * 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 . + * + */ + +// Unit test for gdmaps + +#include "config.h" +#include +#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