/* GeoIP Organization, ISP and AS Number Edition private method */ static char *_get_name (GeoIP* gi, unsigned long ipnum) { int seek_org; char buf[MAX_ORG_RECORD_LENGTH]; char * org_buf, * buf_pointer; int record_pointer; size_t len; if (gi->databaseType != GEOIP_ORG_EDITION && gi->databaseType != GEOIP_ISP_EDITION && gi->databaseType != GEOIP_ASNUM_EDITION) { printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_ORG_EDITION]); return 0; } seek_org = _GeoIP_seek_record(gi, ipnum); if (seek_org == gi->databaseSegments[0]) return NULL; record_pointer = seek_org + (2 * gi->record_length - 1) * gi->databaseSegments[0]; if (gi->cache == NULL) { fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET); fread(buf, sizeof(char), MAX_ORG_RECORD_LENGTH, gi->GeoIPDatabase); len = sizeof(char) * (strlen(buf)+1); org_buf = (char*)malloc(len); strncpy(org_buf, buf, len); } else { buf_pointer = (char*)(gi->cache + (long)record_pointer); len = sizeof(char) * (strlen(buf_pointer)+1); org_buf = (char*)malloc(len); strncpy(org_buf, buf_pointer, len); } return org_buf; }
void PlayerCmd_GetGeoLocation(scr_entref_t arg){ gentity_t* gentity; int entityNum = 0; int rettype; int locIndex; const char* countryname; mvabuf; if(HIWORD(arg)){ Scr_ObjectError("Not an entity"); return; }else{ entityNum = LOWORD(arg); gentity = &g_entities[entityNum]; if(!gentity->client){ Scr_ObjectError(va("Entity: %i is not a player", entityNum)); return; } } if(Scr_GetNumParam() != 1){ Scr_Error("Usage: self getgeolocation( <integer> )\n"); } rettype = Scr_GetInt(0); locIndex = _GeoIP_seek_record(BigLong(*(unsigned long*)&svs.clients[entityNum].netchan.remoteAddress.ip)); switch(rettype){ case SCR_GEOIP_CODE: countryname = _GeoIP_country_code(locIndex); break; case SCR_GEOIP_CODE3: countryname = _GeoIP_country_code3(locIndex); break; case SCR_GEOIP_COUNTRYNAME: countryname = _GeoIP_country_name(locIndex); break; case SCR_GEOIP_CONTINENT: countryname = _GeoIP_continent_name(locIndex); break; default: Scr_AddInt(locIndex); return; } Scr_AddString(countryname); }
int GeoIP_record_id_by_addr (GeoIP* gi, const char *addr) { unsigned long ipnum; if (gi->databaseType != GEOIP_CITY_EDITION_REV0 && gi->databaseType != GEOIP_CITY_EDITION_REV1) { printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]); return 0; } if (addr == NULL) { return 0; } ipnum = _GeoIP_addr_to_num(addr); return _GeoIP_seek_record(gi, ipnum); }
static GeoIPRecord * _get_record(GeoIP* gi, unsigned long ipnum) { unsigned int seek_record; if (gi->databaseType != GEOIP_CITY_EDITION_REV0 && gi->databaseType != GEOIP_CITY_EDITION_REV1) { printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_CITY_EDITION_REV1]); return 0; } seek_record = _GeoIP_seek_record(gi, ipnum); return _extract_record(gi, seek_record, NULL); }
int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum) { int ret; if (ipnum == 0) { return 0; } if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) { printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); return 0; } ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; return ret; }
int GeoIP_id_by_name (GeoIP* gi, const char *name) { unsigned long ipnum; int ret; if (name == NULL) { return 0; } if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) { printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); return 0; } if (!(ipnum = _GeoIP_lookupaddress(name))) return 0; ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; return ret; }
void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *region) { unsigned int seek_region; /* This also writes in the terminating NULs (if you decide to * keep them) and clear any fields that are not set. */ memset(region, 0, sizeof(GeoIPRegion)); seek_region = _GeoIP_seek_record(gi, ntohl(inetaddr)); if (gi->databaseType == GEOIP_REGION_EDITION_REV0) { /* Region Edition, pre June 2003 */ seek_region -= STATE_BEGIN_REV0; if (seek_region >= 1000) { region->country_code[0] = 'U'; region->country_code[1] = 'S'; region->region[0] = (char) ((seek_region - 1000)/26 + 65); region->region[1] = (char) ((seek_region - 1000)%26 + 65); } else { memcpy(region->country_code, GeoIP_country_code[seek_region], 2); } } else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) { /* Region Edition, post June 2003 */ seek_region -= STATE_BEGIN_REV1; if (seek_region < US_OFFSET) { /* Unknown */ /* we don't need to do anything here b/c we memset region to 0 */ } else if (seek_region < CANADA_OFFSET) { /* USA State */ region->country_code[0] = 'U'; region->country_code[1] = 'S'; region->region[0] = (char) ((seek_region - US_OFFSET)/26 + 65); region->region[1] = (char) ((seek_region - US_OFFSET)%26 + 65); } else if (seek_region < WORLD_OFFSET) { /* Canada Province */ region->country_code[0] = 'C'; region->country_code[1] = 'A'; region->region[0] = (char) ((seek_region - CANADA_OFFSET)/26 + 65); region->region[1] = (char) ((seek_region - CANADA_OFFSET)%26 + 65); } else { /* Not US or Canada */ memcpy(region->country_code, GeoIP_country_code[(seek_region - WORLD_OFFSET) / FIPS_RANGE], 2); } } }