5

I have looked and looked and I can't seen to find a C++ version of GeoIP. I know there is a version of it in C, but I can't seem to get it working with Micosoft Visual Studio 2012 C++

I have tried:

So,

  1. Does a GeoIPC++ version exist?
  2. Is there another lib that does IP to country like GeoIP in C++?
  3. Is there a tutorial on how to get GeoIP working with C++?

Sorry for all the questions but i have looked and looked and I can't seem to find a solution.

Community
  • 1
  • 1
Jork449
  • 97
  • 2
  • 6

2 Answers2

4

Late to the party, but I wrote a C++ API for the MaxMind GeoIP db recently. I didn't test it under Windows, I only ran it under Linux, but it definitely isn't Linux-specific.

I called it GeoLite2++. You can find it here: https://www.ccoderun.ca/GeoLite2++/api/

Source tarball and .deb files for Ubuntu are here: https://www.ccoderun.ca/GeoLite2PP/download/?C=M;O=D

Example source code:

#include <GeoLite2PP.hpp>
...
GeoLite2PP::DB db( "/opt/stuff/GeoLite2-City.mmdb" );
std::string json = db.lookup( "216.58.216.163" );
std::cout << json << std::endl;

Example output:

{
    "city" : 
    {
        "names" : 
        {
            "de" : "Mountain View",
            "en" : "Mountain View",
            ...

It includes more, such as getting individual fields versus grabbing entire records as JSON strings.

An example showing how to grab a single field:

GeoLite2PP::DB db("GeoLite2-City.mmdb");
std::string city = db.get_field( "65.44.217.6", "en",
    GeoLite2PP::VCStr { "city", "names" } );

The central class is described here: https://www.ccoderun.ca/GeoLite2++/api/classGeoLite2PP_1_1DB.html

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • Hi Stéphane, late to the party and a newb in the C/C++ area. Is there an example in the C++ package of how to compile from main in a single(ish) command without full professional level interdependent scripts? I'm seeing undefined reference errors. If this is more appropriate as a question please advise and I'll post with a full code example. – James Scott May 05 '17 at 23:56
  • https://www.ccoderun.ca/GeoLite2++/api/usage.html is the documentation I wrote. If you're having problems linking, I'm guessing you forgot to add the library? Search for the missing dependency problem without the actual function name to see what people have to say about it. – Stéphane May 07 '17 at 14:55
2

GeoIP is an online database which is updating its data everyday (or monthly!).

Also, You can have a big offline database to map IP-to-Location beside your application (and you need keep it up to date periodically).

GeoIP is not bound to a specific programming language, you can connect to this database using a web-service mechanism. Simply connect to the online service by a TCP/HTTP request and retrieve data.

The HTTP API requires you to pass a set of parameters as an HTTP GET or POST. Results are returned in a simple text format documented below.

We offer several different services, each providing a different amount of information about the IP address.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • Thank you for your answer and yes i would like to connect to their offline database if possible in c++ or a fixed c version that can work in c++ :). – Jork449 Mar 26 '13 at 08:07
  • Yes, you can. Read [this](http://dev.maxmind.com/geoip/csv) and [this](http://dev.maxmind.com/geoip/downloadable). – masoud Mar 26 '13 at 08:12