aboutsummaryrefslogtreecommitdiff
path: root/src/feature/control
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-09-27 09:35:47 -0400
committerNick Mathewson <nickm@torproject.org>2018-09-27 09:36:52 -0400
commitf403af22076b0092807822ddc9890acdbd62f578 (patch)
treee8b21036bd9148d765609d9f998baf4350f373c6 /src/feature/control
parent79208ee852b84c94b32db1b13a2ba802cae360f7 (diff)
downloadtor-f403af22076b0092807822ddc9890acdbd62f578.tar.gz
tor-f403af22076b0092807822ddc9890acdbd62f578.zip
Split geoip from geoip-related stats.
This commit just moves the code to two separate files. The geoip code still has a few needless dependencies on core/* and features/*.
Diffstat (limited to 'src/feature/control')
-rw-r--r--src/feature/control/control.c3
-rw-r--r--src/feature/control/getinfo_geoip.c45
-rw-r--r--src/feature/control/getinfo_geoip.h14
3 files changed, 61 insertions, 1 deletions
diff --git a/src/feature/control/control.c b/src/feature/control/control.c
index b8ddb3c13e..d02ecbac80 100644
--- a/src/feature/control/control.c
+++ b/src/feature/control/control.c
@@ -60,6 +60,7 @@
#include "feature/client/entrynodes.h"
#include "feature/control/control.h"
#include "feature/control/fmt_serverstatus.h"
+#include "feature/control/getinfo_geoip.h"
#include "feature/dircache/dirserv.h"
#include "feature/dirclient/dirclient.h"
#include "feature/dirclient/dlstatus.h"
@@ -83,7 +84,7 @@
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
#include "feature/rend/rendservice.h"
-#include "feature/stats/geoip.h"
+#include "feature/stats/geoip_stats.h"
#include "feature/stats/predict_ports.h"
#include "lib/container/buffers.h"
#include "lib/crypt_ops/crypto_rand.h"
diff --git a/src/feature/control/getinfo_geoip.c b/src/feature/control/getinfo_geoip.c
new file mode 100644
index 0000000000..35eb3f6aed
--- /dev/null
+++ b/src/feature/control/getinfo_geoip.c
@@ -0,0 +1,45 @@
+
+#include "core/or/or.h"
+#include "core/mainloop/connection.h"
+#include "feature/control/control.h"
+#include "feature/control/getinfo_geoip.h"
+#include "feature/stats/geoip.h"
+
+/** Helper used to implement GETINFO ip-to-country/... controller command. */
+int
+getinfo_helper_geoip(control_connection_t *control_conn,
+ const char *question, char **answer,
+ const char **errmsg)
+{
+ (void)control_conn;
+ if (!strcmpstart(question, "ip-to-country/")) {
+ int c;
+ sa_family_t family;
+ tor_addr_t addr;
+ question += strlen("ip-to-country/");
+
+ if (!strcmp(question, "ipv4-available") ||
+ !strcmp(question, "ipv6-available")) {
+ family = !strcmp(question, "ipv4-available") ? AF_INET : AF_INET6;
+ const int available = geoip_is_loaded(family);
+ tor_asprintf(answer, "%d", !! available);
+ return 0;
+ }
+
+ family = tor_addr_parse(&addr, question);
+ if (family != AF_INET && family != AF_INET6) {
+ *errmsg = "Invalid address family";
+ return -1;
+ }
+ if (!geoip_is_loaded(family)) {
+ *errmsg = "GeoIP data not loaded";
+ return -1;
+ }
+ if (family == AF_INET)
+ c = geoip_get_country_by_ipv4(tor_addr_to_ipv4h(&addr));
+ else /* AF_INET6 */
+ c = geoip_get_country_by_ipv6(tor_addr_to_in6(&addr));
+ *answer = tor_strdup(geoip_get_country_name(c));
+ }
+ return 0;
+}
diff --git a/src/feature/control/getinfo_geoip.h b/src/feature/control/getinfo_geoip.h
new file mode 100644
index 0000000000..ff77cefecd
--- /dev/null
+++ b/src/feature/control/getinfo_geoip.h
@@ -0,0 +1,14 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_GETINFO_GEOIP_H
+#define TOR_GETINFO_GEOIP_H
+
+int getinfo_helper_geoip(control_connection_t *control_conn,
+ const char *question, char **answer,
+ const char **errmsg);
+
+#endif