1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 * @file getinfo_geoip.c
8 * @brief GEOIP-related controller GETINFO commands.
11 #include "core/or/or.h"
12 #include "core/mainloop/connection.h"
13 #include "feature/control/control.h"
14 #include "feature/control/getinfo_geoip.h"
15 #include "lib/geoip/geoip.h"
17 /** Helper used to implement GETINFO ip-to-country/... controller command. */
19 getinfo_helper_geoip(control_connection_t
*control_conn
,
20 const char *question
, char **answer
,
24 if (!strcmpstart(question
, "ip-to-country/")) {
28 question
+= strlen("ip-to-country/");
30 if (!strcmp(question
, "ipv4-available") ||
31 !strcmp(question
, "ipv6-available")) {
32 family
= !strcmp(question
, "ipv4-available") ? AF_INET
: AF_INET6
;
33 const int available
= geoip_is_loaded(family
);
34 tor_asprintf(answer
, "%d", !! available
);
38 family
= tor_addr_parse(&addr
, question
);
39 if (family
!= AF_INET
&& family
!= AF_INET6
) {
40 *errmsg
= "Invalid address family";
43 if (!geoip_is_loaded(family
)) {
44 *errmsg
= "GeoIP data not loaded";
47 c
= geoip_get_country_by_addr(&addr
);
48 *answer
= tor_strdup(geoip_get_country_name(c
));