tor  0.4.2.1-alpha-dev
getinfo_geoip.c
1 
2 #include "core/or/or.h"
5 #include "feature/control/getinfo_geoip.h"
6 #include "lib/geoip/geoip.h"
7 
9 int
10 getinfo_helper_geoip(control_connection_t *control_conn,
11  const char *question, char **answer,
12  const char **errmsg)
13 {
14  (void)control_conn;
15  if (!strcmpstart(question, "ip-to-country/")) {
16  int c;
17  sa_family_t family;
18  tor_addr_t addr;
19  question += strlen("ip-to-country/");
20 
21  if (!strcmp(question, "ipv4-available") ||
22  !strcmp(question, "ipv6-available")) {
23  family = !strcmp(question, "ipv4-available") ? AF_INET : AF_INET6;
24  const int available = geoip_is_loaded(family);
25  tor_asprintf(answer, "%d", !! available);
26  return 0;
27  }
28 
29  family = tor_addr_parse(&addr, question);
30  if (family != AF_INET && family != AF_INET6) {
31  *errmsg = "Invalid address family";
32  return -1;
33  }
34  if (!geoip_is_loaded(family)) {
35  *errmsg = "GeoIP data not loaded";
36  return -1;
37  }
38  if (family == AF_INET)
40  else /* AF_INET6 */
42  *answer = tor_strdup(geoip_get_country_name(c));
43  }
44  return 0;
45 }
uint16_t sa_family_t
Definition: inaddr_st.h:77
Header file for connection.c.
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:152
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:206
Header file for geoip.c.
int geoip_get_country_by_ipv6(const struct in6_addr *addr)
Definition: geoip.c:401
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
Master header file for Tor-specific functionality.
int tor_addr_parse(tor_addr_t *addr, const char *src)
Definition: address.c:1255
const char * geoip_get_country_name(country_t num)
Definition: geoip.c:441
static const struct in6_addr * tor_addr_to_in6(const tor_addr_t *a)
Definition: address.h:111
Header file for control.c.
int geoip_get_country_by_ipv4(uint32_t ipaddr)
Definition: geoip.c:385