Skip to content

Instantly share code, notes, and snippets.

@electrum
Created May 4, 2010 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electrum/390023 to your computer and use it in GitHub Desktop.
Save electrum/390023 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <GeoIPCity.h>
int main(int argc, char *argv[])
{
GeoIP *geo;
char buf[32768];
unsigned long ip;
GeoIPRecord *rec;
geo = GeoIP_open_type(GEOIP_CITY_EDITION_REV1, GEOIP_MEMORY_CACHE);
if (geo == NULL)
{
fprintf(stderr, "Failed to open GeoIP city database\n");
return 100;
}
while (!feof(stdin))
{
if (fgets(buf, sizeof(buf), stdin) == NULL)
break;
if (sscanf(buf, "%lu", &ip) != 1)
continue;
if (ip >= 4294967296)
continue;
rec = GeoIP_record_by_ipnum(geo, ip);
printf("%lu", ip);
if (rec == NULL)
printf("\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL\n");
else
{
printf("\t%s\t%s\t%s\t%s",
rec->country_code,
rec->region ? rec->region : "NULL",
rec->city ? rec->city : "NULL",
rec->postal_code ? rec->postal_code : "NULL");
if (rec->dma_code)
printf("\t%d", rec->dma_code);
else
printf("\tNULL");
if (rec->area_code)
printf("\t%d", rec->area_code);
else
printf("\tNULL");
if (rec->latitude)
printf("\t%0.6f", rec->latitude);
else
printf("\tNULL");
if (rec->longitude)
printf("\t%0.6f", rec->longitude);
else
printf("\tNULL");
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment