summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2014-10-07 16:33:46 +0200
committerYorhel <git@yorhel.nl>2014-10-07 16:33:46 +0200
commit1e76ffaca01789340d40a118663b0d53a78d98f2 (patch)
tree2982bbc6a798937cf661f96adbf24d2df4fa79f8
parent00f407cef58a0b326e6ce9b969e777f23d1a48c0 (diff)
Add geoip_cc[46] settings to disable GeoIP or specify custom database
-rw-r--r--src/doc.h8
-rw-r--r--src/geoip.c56
-rw-r--r--src/main.c3
-rw-r--r--src/vars.c17
4 files changed, 57 insertions, 27 deletions
diff --git a/src/doc.h b/src/doc.h
index 37186a1..6a9c184 100644
--- a/src/doc.h
+++ b/src/doc.h
@@ -435,6 +435,14 @@ static const doc_set_t doc_sets[] = {
" your system for other things besides ncdc, you share large files (>100MB)"
" and people are not constantly downloading the same file from you."
},
+{ "geoip_cc4", 0, "<path>|disabled",
+ "Path to the GeoIP Country database file for IPv4, or 'disabled' to disable"
+ " GeoIP lookup for IPv4 addresses."
+},
+{ "geoip_cc6", 0, "<path>|disabled",
+ "Path to the GeoIP Country database file for IPv6, or 'disabled' to disable"
+ " GeoIP lookup for IPv6 addresses."
+},
{ "hash_rate", 0, "<speed>",
"Maximum file hashing speed. See the `download_rate' setting for allowed"
" formats for this setting."
diff --git a/src/geoip.c b/src/geoip.c
index 734e454..e5aaa71 100644
--- a/src/geoip.c
+++ b/src/geoip.c
@@ -35,33 +35,37 @@ static GeoIP *geoip6;
#endif
-void geoip_reinit() {
+void geoip_reinit(int v) {
#ifdef USE_GEOIP
- if(geoip4)
- GeoIP_delete(geoip4);
- if(geoip6)
- GeoIP_delete(geoip6);
- /* Get the file paths directly, so that we can offer more useful diagnostic
- * messages in case we fail to open it. Calling GeoIP_db_avail() ensures that
- * the GeoIPDBFileName variable has been initialized. */
- if(!GeoIPDBFileName)
- GeoIP_db_avail(GEOIP_COUNTRY_EDITION);
- const char *f4 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION];
- const char *f6 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION_V6];
-
- /* The '16' flag is GEOIP_SILENCE, but it's a fairly new option and not
- * defined in older versions. Just pass it along directly, ABI compatibility
- * should ensure this works with both old and new versions.
- * Also perform a g_file_test() first to ensure we're not opening
- * non-existing files. GeoIP versions that do not support GEOIP_SILENCE will
- * throw error messages on stdout, which screws up our ncurses UI, so
- * catching the most common error here is worth it. */
- geoip4 = g_file_test(f4, G_FILE_TEST_EXISTS) ? GeoIP_open(f4, 16 | GEOIP_MEMORY_CACHE) : NULL;
- geoip6 = g_file_test(f6, G_FILE_TEST_EXISTS) ? GeoIP_open(f6, 16 | GEOIP_MEMORY_CACHE) : NULL;
- if(!geoip4)
- ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv4 addresses.", f4);
- if(!geoip6)
- ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv6 addresses.", f6);
+ GeoIP **var = v == 4 ? &geoip4 : &geoip6;
+ if(*var) {
+ GeoIP_delete(*var);
+ *var = NULL;
+ }
+
+ const char *fn = var_get(0, v == 4 ? VAR_geoip_cc4 : VAR_geoip_cc6);
+ if(!fn) {
+ /* Get the file paths directly, so that we can offer more useful diagnostic
+ * messages in case we fail to open it. Calling GeoIP_db_avail() ensures that
+ * the GeoIPDBFileName variable has been initialized. */
+ if(!GeoIPDBFileName)
+ GeoIP_db_avail(GEOIP_COUNTRY_EDITION);
+ fn = GeoIPDBFileName[v == 4 ? GEOIP_COUNTRY_EDITION : GEOIP_COUNTRY_EDITION_V6];
+ }
+
+ if(strcmp(fn, "disabled") != 0) {
+ /* The '16' flag is GEOIP_SILENCE, but it's a fairly new option and not
+ * defined in older versions. Just pass it along directly, ABI compatibility
+ * should ensure this works with both old and new versions.
+ * Also perform a g_file_test() first to ensure we're not opening
+ * non-existing files. GeoIP versions that do not support GEOIP_SILENCE
+ * will throw error messages on stdout/stderr, which screws up our ncurses
+ * UI, so catching the most common error here is worth it. */
+ *var = g_file_test(fn, G_FILE_TEST_EXISTS) ? GeoIP_open(fn, 16 | GEOIP_MEMORY_CACHE) : NULL;
+ if(!*var)
+ ui_mf(NULL, 0, "Can't open '%s', no country codes will be displayed for IPv%d addresses.", fn, v);
+ }
+
geoip_available = geoip4 || geoip6;
#endif
}
diff --git a/src/main.c b/src/main.c
index eaf51c9..0ba6a21 100644
--- a/src/main.c
+++ b/src/main.c
@@ -453,7 +453,8 @@ int main(int argc, char **argv) {
dl_init_global();
ui_cmdhist_init("history");
ui_init(bracketed_paste);
- geoip_reinit();
+ geoip_reinit(4);
+ geoip_reinit(6);
// setup SIGWINCH
struct sigaction act;
diff --git a/src/vars.c b/src/vars.c
index 54004f3..d198574 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -603,6 +603,21 @@ static char *i_ffc() {
}
+// geoip
+
+static gboolean s_geoip_cc(guint64 hub, const char *key, const char *val, GError **err) {
+#ifdef USE_GEOIP
+ int v = strcmp(key, "geoip_cc4") == 0 ? 4 : 6;
+ db_vars_set(hub, key, val);
+ geoip_reinit(v);
+ return TRUE;
+#else
+ g_set_error(err, 1, 0, "This option can't be modified: %s.", "Ncdc has not been compiled with GeoIP support");
+ return FALSE;
+#endif
+}
+
+
// hubname
static char *p_hubname(const char *val, GError **err) {
@@ -947,6 +962,8 @@ struct var_t {
V(filelist_maxage, 1,0, f_interval, p_interval, su_old, NULL, NULL, "604800")\
V(fl_done, 0,0, NULL, NULL, NULL, NULL, NULL, "false")\
V(flush_file_cache, 1,0, f_ffc, p_ffc, su_ffc, g_ffc, s_ffc, i_ffc())\
+ V(geoip_cc4, 1,0, f_id, p_id, su_path, NULL, s_geoip_cc, NULL)\
+ V(geoip_cc6, 1,0, f_id, p_id, su_path, NULL, s_geoip_cc, NULL)\
V(hash_rate, 1,0, f_speed, p_speed, NULL, NULL, NULL, NULL)\
V(hubaddr, 0,0, NULL, NULL, NULL, NULL, NULL, NULL)\
V(hubkp, 0,0, NULL, NULL, NULL, NULL, NULL, NULL)\