/* Set city data by geoid or record into the given `location` buffer * based on the IP version and currently used database edition. * It uses the custom GeoIP database - i.e., GeoLiteCity.dat */ void geoip_get_city (const char *ip, char *location, GTypeIP type_ip) { unsigned char rec = GeoIP_database_edition (geo_location_data); if (conf.geoip_database == NULL || geo_location_data == NULL) return; switch (rec) { case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: if (TYPE_IPV4 == type_ip) geoip_set_city_by_record (ip, location, TYPE_IPV4); else geoip_set_city (NULL, NULL, location); break; case GEOIP_CITY_EDITION_REV0_V6: case GEOIP_CITY_EDITION_REV1_V6: if (TYPE_IPV6 == type_ip) geoip_set_city_by_record (ip, location, TYPE_IPV6); else geoip_set_city (NULL, NULL, location); break; } }
/* Set city data by record into the given `location` buffer based on * the IP version. */ static void geoip_set_city_by_record (const char *ip, char *location, GTypeIP type_ip) { GeoIPRecord *rec = NULL; const char *city = NULL, *region = NULL, *addr = ip; /* Custom GeoIP database */ if ((rec = get_geoip_record (addr, type_ip))) { city = rec->city; region = rec->region; } geoip_set_city (city, region, location); if (rec != NULL) { GeoIPRecord_delete (rec); } }
/* Custom GeoIP database - i.e., GeoLiteCity.dat */ void geoip_get_city (const char *ip, char *location) { GeoIPRecord *rec = NULL; const char *city = NULL, *region = NULL, *addr = ip; if (geo_location_data != NULL) rec = GeoIP_record_by_name (geo_location_data, addr); if (rec != NULL) { city = rec->city; region = rec->region; } geoip_set_city (city, region, location); if (rec != NULL) GeoIPRecord_delete (rec); }