void Geolocation::getInfo(IpAddress *addr, char **country_code, char **city, float *latitude, float *longitude) { #ifdef HAVE_GEOIP GeoIPRecord *geo = NULL; struct ipAddress *ip = addr->getIP(); switch(ip->ipVersion) { case 4: if(geo_ip_city_db != NULL) geo = GeoIP_record_by_ipnum(geo_ip_city_db, ntohl(ip->ipType.ipv4)); break; case 6: if(geo_ip_city_db_v6 != NULL) { struct in6_addr *ipv6 = (struct in6_addr*)&ip->ipType.ipv6; geo = GeoIP_record_by_ipnum_v6(geo_ip_city_db_v6, *ipv6); } break; } if(geo != NULL) { *country_code = geo->country_code ? strdup(geo->country_code) : NULL; *city = geo->city ? strdup(geo->city) : NULL; *latitude = geo->latitude, *longitude = geo->longitude; GeoIPRecord_delete(geo); } else *country_code = NULL, *city = NULL, *latitude = *longitude = 0; #endif }
int GeoIPLocIP(ftval *ip, enum ftype itype, float *latitude, float *longitude, char **country_code) { GeoIPRecord *gir; geoipv6_t gv6; const char *ccode; gir = NULL; ccode = NULL; switch (itype) { case FT_IPv4: if (gi == NULL) return -1; pthread_mutex_lock(&atom); if (city) { gir = GeoIP_record_by_ipnum(gi, ntohl(ip->uint32)); if (gir != NULL) { ccode = gir->country_code; } } else { ccode = GeoIP_country_code_by_ipnum(gi, ntohl(ip->uint32)); } pthread_mutex_unlock(&atom); break; case FT_IPv6: if (giv6 == NULL) return -1; memcpy(gv6.s6_addr, ip->ipv6, 16); pthread_mutex_lock(&atom6); if (cityv6) { gir = GeoIP_record_by_ipnum_v6(giv6, gv6); if (gir != NULL) ccode = gir->country_code; } else { ccode = GeoIP_country_code_by_ipnum_v6(giv6, gv6); } pthread_mutex_unlock(&atom6); break; default: LogPrintf(LV_ERROR, "GeoIP IP type error"); } if (gir != NULL) { *latitude = gir->latitude; *longitude = gir->longitude; if (country_code != NULL) *country_code = (char *)ccode; GeoIPRecord_delete(gir); return 0; } if (country_code != NULL && ccode != NULL) { *country_code = (char *)ccode; return 0; } return -1; }
/* * GeoIPRecord lookups are performed if the previous lookup was * from a different IP address than the current, or was for a search * outside the City database. */ static GeoIPRecord * city_lookup(GeoIP *db, dns_geoip_subtype_t subtype, unsigned int family, isc_uint32_t ipnum, const geoipv6_t *ipnum6) { GeoIPRecord *record = NULL; geoip_state_t *prev_state = NULL; REQUIRE(db != NULL); #ifndef HAVE_GEOIP_V6 /* no IPv6 support? give up now */ if (family == AF_INET6) return (NULL); #endif prev_state = get_state_for(family, ipnum, ipnum6); if (prev_state != NULL && is_city(prev_state->subtype)) record = prev_state->record; if (record == NULL) { if (family == AF_INET) record = GeoIP_record_by_ipnum(db, ipnum); #ifdef HAVE_GEOIP_V6 else record = GeoIP_record_by_ipnum_v6(db, *ipnum6); #endif if (record == NULL) return (NULL); set_state(family, ipnum, ipnum6, subtype, record, NULL, NULL, NULL, 0); } return (record); }
static GeoIPRecord * ngx_stream_geoip_get_city_record(ngx_stream_session_t *s) { ngx_stream_geoip_conf_t *gcf; gcf = ngx_stream_get_module_main_conf(s, ngx_stream_geoip_module); if (gcf->city) { #if (NGX_HAVE_GEOIP_V6) return gcf->city_v6 ? GeoIP_record_by_ipnum_v6(gcf->city, ngx_stream_geoip_addr_v6(s, gcf)) : GeoIP_record_by_ipnum(gcf->city, ngx_stream_geoip_addr(s, gcf)); #else return GeoIP_record_by_ipnum(gcf->city, ngx_stream_geoip_addr(s, gcf)); #endif } return NULL; }
static GeoIPRecord * ngx_http_geoip_get_city_record(ngx_http_request_t *r) { ngx_http_geoip_conf_t *gcf; gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); if (gcf->city) { #if (NGX_HAVE_GEOIP_V6) return gcf->city_v6 ? GeoIP_record_by_ipnum_v6(gcf->city, ngx_http_geoip_addr_v6(r, gcf)) : GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); #else return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf)); #endif } return NULL; }
void geoiplookup(GeoIP * gi, char *hostname, int i) { const char *country_code; const char *country_name; const char *asnum_name; int country_id; GeoIPRecord *gir; geoipv6_t ipnum; ipnum = _GeoIP_lookupaddress_v6(hostname); if (__GEOIP_V6_IS_NULL(ipnum)) { printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname); }else { #if 0 if (GEOIP_DOMAIN_EDITION_V6 == i) { domain_name = GeoIP_name_by_name_v6(gi, hostname); if (domain_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s\n", GeoIPDBDescription[i], domain_name); } } #endif if (GEOIP_LOCATIONA_EDITION_V6 == i || GEOIP_ASNUM_EDITION_V6 == i || GEOIP_USERTYPE_EDITION_V6 == i || GEOIP_REGISTRAR_EDITION_V6 == i || GEOIP_DOMAIN_EDITION_V6 == i || GEOIP_ORG_EDITION_V6 == i || GEOIP_ISP_EDITION_V6 == i || GEOIP_NETSPEED_EDITION_REV1_V6 == i) { asnum_name = GeoIP_name_by_ipnum_v6(gi, ipnum); if (asnum_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s\n", GeoIPDBDescription[i], asnum_name); // _say_range_by_ip(gi, ipnum); } }else if (GEOIP_CITY_EDITION_REV0_V6 == i) { gir = GeoIP_record_by_ipnum_v6(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA( gir->region), _mk_NA(gir->city), _mk_NA( gir->postal_code), gir->latitude, gir->longitude); } }else if (GEOIP_CITY_EDITION_REV1_V6 == i) { gir = GeoIP_record_by_ipnum_v6(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA( gir->region), _mk_NA(gir->city), _mk_NA( gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code); } }else if (GEOIP_COUNTRY_EDITION_V6 == i) { country_id = GeoIP_id_by_ipnum_v6(gi, ipnum); country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (country_id == 0) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name); } } } #if 0 else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) {