Question

I want to map a bunch of IP addresses to their latitude and longitude by MaxMind database. My code is in C, I do not know how to use this database.

Was it helpful?

Solution

I assume you want something along these lines:

#include <stdio.h>
#include <GeoIP.h>
#include <GeoIPCity.h>

int main()
{
    GeoIP *gi;
    GeoIPRecord *gir;

    gi = GeoIP_open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_INDEX_CACHE);

    if (gi == NULL) {
        /* handle error */
    }

    gir = GeoIP_record_by_name(gi, "1.1.1.1");
    if (gir == NULL) {
        /* handle error */
    }

    printf("latitude: %f   longitude: %f", gir->latitude, gir->longitude);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top