GeoLite2++  v0.0.1-2711
C++ API for MaxMind's GeoLite2 Database
Looking for a senior C++ dev? I'm looking for work. Hire me!
GeoLite2PP::DB Class Referencefinal

The DB class is the primary class used by GeoLite2++. More...

#include <GeoLite2PP.hpp>

Public Member Functions

 ~DB (void)
 Destructor. More...
 
 DB (const std::string &database_filename="GeoLite2-City.mmdb")
 Constructor. More...
 
std::string get_lib_version_mmdb (void) const
 Get the MaxMind library version number. More...
 
std::string get_lib_version_geolite2pp (void) const
 Get the GeoLite2++ library version number. More...
 
MMDB_metadata_s get_metadata_raw (void)
 Get the database metadata. More...
 
std::string get_metadata (void)
 Get the database metadata as a JSON string. More...
 
MMDB_lookup_result_s lookup_raw (const std::string &ip_address)
 Look up an IP address. More...
 
std::string lookup (const std::string &ip_address)
 Look up an IP address and return a JSON string of everything found. More...
 
MStr get_all_fields (const std::string &ip_address, const std::string &language="en")
 Return a std::map of many of the key fields available when looking up an address. More...
 
std::string get_field (const std::string &ip_address, const std::string &language, const VCStr &v)
 Get a specific field, or an empty string if the field does not exist. More...
 
std::string get_field (MMDB_lookup_result_s *lookup, const std::string &language, const VCStr &v)
 Get a specific field, or an empty string if the field does not exist. More...
 
std::string to_json (MMDB_entry_data_list_s *node)
 Process the specified node list and return a JSON-format string. More...
 

Detailed Description

The DB class is the primary class used by GeoLite2++.

It is used to point to the MaxMind's GeoLite2's database and to retrieve information based on IP addresses.

Constructor & Destructor Documentation

◆ ~DB()

GeoLite2PP::DB::~DB ( void  )

Destructor.

◆ DB()

GeoLite2PP::DB::DB ( const std::string &  database_filename = "GeoLite2-City.mmdb")

Constructor.

Parameters
[in]database_filenameThe database filename is the .mmdb file typically downloaded from MaxMind. Normally, this file is called GeoLite2-City.mmdb and may be downloaded for free from http://dev.maxmind.com/geoip/geoip2/geolite2/#Downloads. The name used can be relative to the current working directory, or an absolute path and filename.
std::string filename = "/opt/my_project_files/GeoLite2-City.mmdb";
GeoLite2PP::DB db( filename );
std::cout << db.get_mmdb_lib_version() << std::endl;
std::cout << db.lookup_metadata() << std::endl;
Warning
An object of type GeoLite2PP::DB cannot be constructed without a valid database file!
Note
The database can be manually downloaded, or the script geolite2pp_get_database.sh can be used to easily download and extract the database into the current working directory. But either way, the MaxMind database must be available for GeoLite2++ to work.
Exceptions
std::system_errorif the database file cannot be opened.
See also
GeoLite2PP::MMDBStatus
GeoLite2PP::ErrorCategory

References GeoLite2PP::get_error_category().

Here is the call graph for this function:

Member Function Documentation

◆ get_all_fields()

GeoLite2PP::MStr GeoLite2PP::DB::get_all_fields ( const std::string &  ip_address,
const std::string &  language = "en" 
)

Return a std::map of many of the key fields available when looking up an address.

This includes fields such as subdivision name, city name, country name, continent name, longitude, latitude, accuracy radius, and relevant iso codes. Not all fields are available for all addresses and languages.

Note
The method is mis-named. It returns many fields, but definitely does not return all fields. To see all fields, see lookup().
GeoLite2PP::MStr m = db.get_all_fields( "65.44.217.6" );
for ( const auto iter : m )
{
std::cout << iter.first << " -> " << iter.second << std::endl;
}

Example output:

accuracy_radius -> 200
city -> Fresno
continent -> North America
country -> United States
country_iso_code -> US
latitude -> 36.605500
longitude -> -119.752200
postal_code -> 93725
query_ip_address -> 65.44.217.6
query_language -> en
registered_country -> United States
subdivision -> California
subdivision_iso_code -> CA
time_zone -> America/Los_Angeles

References lookup_raw().

Here is the call graph for this function:

◆ get_field() [1/2]

std::string GeoLite2PP::DB::get_field ( const std::string &  ip_address,
const std::string &  language,
const VCStr v 
)

Get a specific field, or an empty string if the field does not exist.

A lookup of the given IP address needs to be performed every time this is called, which makes this less efficient than the other get_field() method which takes a MMDB_lookup_result_s parameter.

std::string city = db.get_field( "65.44.217.6", "en", GeoLite2PP::VCStr { "city" , "names" } );
std::string country = db.get_field( "65.44.217.6", "en", GeoLite2PP::VCStr { "country" , "names" } );
std::string latitude = db.get_field( "65.44.217.6", "en", GeoLite2PP::VCStr { "location", "latitude" } );

References lookup(), and lookup_raw().

Referenced by get_field().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_field() [2/2]

std::string GeoLite2PP::DB::get_field ( MMDB_lookup_result_s *  lookup,
const std::string &  language,
const VCStr v 
)

Get a specific field, or an empty string if the field does not exist.

This is a more efficient call than the other get_field() method since the address doesn't need to be looked up in the database at every call.

MMDB_lookup_result_s result = db.lookup_raw( "65.44.217.6" );
std::string city = db.get_field( &result, "en", GeoLite2PP::VCStr { "city" , "names" } );
std::string country = db.get_field( &result, "en", GeoLite2PP::VCStr { "country" , "names" } );
std::string latitude = db.get_field( &result, "en", GeoLite2PP::VCStr { "location", "latitude" } );

References get_field().

Here is the call graph for this function:

◆ get_lib_version_geolite2pp()

std::string GeoLite2PP::DB::get_lib_version_geolite2pp ( void  ) const

Get the GeoLite2++ library version number.

std::cout << db.get_lib_version_geolite2pp() << std::endl;

Example output:

0.0.1-1992

◆ get_lib_version_mmdb()

std::string GeoLite2PP::DB::get_lib_version_mmdb ( void  ) const

Get the MaxMind library version number.

std::cout << db.get_lib_version_mmdb() << std::endl;

Example output:

1.2.0

◆ get_metadata()

std::string GeoLite2PP::DB::get_metadata ( void  )

Get the database metadata as a JSON string.

std::cout << db.get_metadata() << std::endl;

Example output:

{
"binary_format_major_version" : 2,
"binary_format_minor_version" : 0,
"build_epoch" : 1475588619,
"database_type" : "GeoLite2-City",
"description" : { "en" : "GeoLite2 City database" },
"ip_version" : 6,
"languages" : [ "de", "en", "es", "fr", "ja", "pt-BR", "ru", "zh-CN" ],
"node_count" : 4065439,
"record_size" : 28
}
See also
get_metadata_raw()
Exceptions
std::system_errorif the lookup resulted in a MMDB error

References GeoLite2PP::get_error_category(), and to_json().

Here is the call graph for this function:

◆ get_metadata_raw()

MMDB_metadata_s GeoLite2PP::DB::get_metadata_raw ( void  )

Get the database metadata.

This returns a raw MMDB_metadata_s structure. This call is intended mostly for internal purposes, or to call directly into the original C MMDB API.

See also
get_metadata()

◆ lookup()

std::string GeoLite2PP::DB::lookup ( const std::string &  ip_address)

Look up an IP address and return a JSON string of everything found.

std::string json = db.lookup( "65.44.217.6" );
std::cout << json << std::endl;

Example output:

{ "city" : { "names" : { "en" : "Fresno" } },
"continent" : { "code" : "NA", "names" : { "en" : "North America" } },
"country" : { "iso_code" : "US", "names" : { "en" : "United States" } },
"location" : { "accuracy_radius" : 200,
"latitude" : 36.6055,
"longitude" : -119.752,
"time_zone" : "America/Los_Angeles" },
"postal" : { "code" : "93725" },
"subdivisions" : [ { "iso_code" : "CA", "names" : { "en" : "California" } } ]
}
Note
For clarity, some of the JSON entries have been removed in this example output.
Exceptions
std::system_errorif the lookup resulted in a MMDB error

References GeoLite2PP::get_error_category(), lookup_raw(), and to_json().

Referenced by get_field().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup_raw()

MMDB_lookup_result_s GeoLite2PP::DB::lookup_raw ( const std::string &  ip_address)

Look up an IP address.

This returns a raw MMDB_lookup_result_s structure. This call is intended mostly for internal purposes, or to call directly into the original C MMDB API.

See also
lookup()
Exceptions
std::invalid_argumentif the address is invalid
std::system_errorif the lookup resulted in a MMDB error
std::length_errorif an entry was not found in the database

References GeoLite2PP::get_error_category().

Referenced by get_all_fields(), get_field(), and lookup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_json()

std::string GeoLite2PP::DB::to_json ( MMDB_entry_data_list_s *  node)

Process the specified node list and return a JSON-format string.

Warning
This will de-allocate the node list prior to returning; do not call MMDB_free_entry_data_list() on the node.

This call is intended mostly for internal purposes.

See also
lookup()
get_metadata()

Referenced by get_metadata(), and lookup().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: