/* Set country data by geoid or record into the given `location` buffer * based on the IP version and currently used database edition. */ void geoip_get_country (const char *ip, char *location, GTypeIP type_ip) { unsigned char rec = GeoIP_database_edition (geo_location_data); switch (rec) { case GEOIP_COUNTRY_EDITION: if (TYPE_IPV4 == type_ip) geoip_set_country_by_geoid (ip, location, TYPE_IPV4); else geoip_set_country (NULL, NULL, location); break; case GEOIP_COUNTRY_EDITION_V6: if (TYPE_IPV6 == type_ip) geoip_set_country_by_geoid (ip, location, TYPE_IPV6); else geoip_set_country (NULL, NULL, location); break; case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: if (TYPE_IPV4 == type_ip) geoip_set_country_by_record (ip, location, TYPE_IPV4); else geoip_set_country (NULL, NULL, location); break; case GEOIP_CITY_EDITION_REV0_V6: case GEOIP_CITY_EDITION_REV1_V6: if (TYPE_IPV6 == type_ip) geoip_set_country_by_record (ip, location, TYPE_IPV6); else geoip_set_country (NULL, NULL, location); break; } }
void geoip_get_country (const char *ip, char *location) { GeoIPRecord *rec = NULL; const char *country = NULL, *code = NULL, *addr = ip; int geoid = 0; /* Custom GeoIP database */ if (conf.geoip_city_data != NULL && geo_location_data != NULL) { rec = GeoIP_record_by_name (geo_location_data, addr); if (rec) { code = rec->country_code; country = rec->country_name; } } /* Legacy GeoIP database */ else if (geo_location_data != NULL) { geoid = GeoIP_id_by_name (geo_location_data, addr); country = GeoIP_country_name_by_name (geo_location_data, addr); code = GeoIP_code_by_id (geoid); } geoip_set_country (country, code, location); if (rec != NULL) GeoIPRecord_delete (rec); }
/* Set country data by geoid into the given `location` buffer based on * the IP version. */ static void geoip_set_country_by_geoid (const char *ip, char *location, GTypeIP type_ip) { const char *country = NULL, *code = NULL, *addr = ip; int geoid = 0; if (geo_location_data == NULL) return; if (!(country = geoip_get_country_by_geoid (addr, type_ip))) goto out; /* return two letter country code */ if (!(geoid = geoip_get_geoid (addr, type_ip))) goto out; code = GeoIP_code_by_id (geoid); out: geoip_set_country (country, code, location); }
/* Set country data by record into the given `location` buffer based * on the IP version. */ static void geoip_set_country_by_record (const char *ip, char *location, GTypeIP type_ip) { GeoIPRecord *rec = NULL; const char *country = NULL, *code = NULL, *addr = ip; if (conf.geoip_database == NULL || geo_location_data == NULL) return; /* Custom GeoIP database */ if ((rec = get_geoip_record (addr, type_ip))) { country = rec->country_name; code = rec->country_code; } geoip_set_country (country, code, location); if (rec != NULL) { GeoIPRecord_delete (rec); } }