1. Based on IP access to the location of the user's ideas:
Query the database by IP address, then extract the corresponding geographic information from the database
2. Download the Maxmind database download address
https://dev.maxmind.com/geoip/geoip2/geolite2/
It has the binary format and CSV format, which can be downloaded first:
geolite2-city.mmdb.gz
geolite2-country.mmdb.gz
geolite2-city-csv_20180306.zip
GeoLite2-Country-CSV _20180306.zip
3. Then is the GitHub address of the LIBMAXMINDDB that operates its database:
Https://github.com/maxmind/libmaxminddb/releases
LIBMAXMINDDB installation command:
$/configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig
LIBMAXMINDDB Description Document Address:
http://maxmind.github.io/libmaxminddb/
And give an example:
#include <errno.h> #include <maxminddb.h> #include <stdlib.h> #include <string.h> int main (int a
RGC, Char **argv) {char *filename = argv[1];
Char *ip_address = argv[2];
mmdb_s MMDB;
int status = Mmdb_open (filename, mmdb_mode_mmap, &mmdb); if (mmdb_success!= status) {fprintf (stderr, "\ n Can ' t open%s-%s\n", FileName, Mmdb_strerror (
status));
if (Mmdb_io_error = = status) {fprintf (stderr, "IO ERROR:%s\n", Strerror (errno));
} exit (1);
int Gai_error, Mmdb_error;
mmdb_lookup_result_s result = mmdb_lookup_string (&mmdb, IP_Address, &gai_error, &mmdb_error);
if (0!= gai_error) {fprintf (stderr, "\ n error from Getaddrinfo for%s-%s\n\n",
IP_Address, Gai_strerror (Gai_error));
Exit (2); } if (mmdb_success!= mmdb_error) {fprintf (stderr, "\ n Got an error From Libmaxminddb:%s\n\n ", Mmdb_strerror (Mmdb_error));
Exit (3);
} mmdb_entry_data_list_s *entry_data_list = NULL;
int exit_code = 0;
if (result.found_entry) {int status = Mmdb_get_entry_data_list (&result.entry,
&entry_data_list); if (mmdb_success!= status) {fprintf (stderr, "Got an error looking up" en
Try data-%s\n ", mmdb_strerror (status);
Exit_code = 4;
Goto end;
} if (NULL!= entry_data_list) {mmdb_dump_entry_data_list (stdout, entry_data_list, 2);
} else {fprintf (stderr, "\ n No entry for this IP address (%s) is found\n\n",
IP_Address);
Exit_code = 5;
} end:mmdb_free_entry_data_list (Entry_data_list);
Mmdb_close (&mmdb);
Exit (Exit_code); }
compile command: gcc-v test.c-o mmdb-lmaxminddb
The order is as follows:
./mmdb <dbpath> <IP>
/mmdb/home/share/geolite2-city.mmdb 112.225.35.70
The effect is as follows:
{"City": {"geoname_id": 1805753 <uint32> "names": {" De ":" Jinan "<utf8_string>" en ":" Jinan "<utf8_string>" Es ":" Jinan "<utf8_string>" fr ":" Jinan "<utf8_string>"
JA ":" 済 nan "<utf8_string>" pt-br ":" Jinan "<utf8_string>
"Ru": "цзинань" <utf8_string> "ZH-CN": "Jinan" <utf8_string> } "Continent": {"code": "as" <utf8_string> "geoname_id": 6
255147 <uint32> "names": {"de": "Asien" <utf8_string>
"EN": "Asia" <utf8_string> "es": "Asia" <utf8_string> "FR": "ASie "<utf8_string>" ja ":" アジア "<utf8_string>" PT-BR ":" Á Sia "<utf8_string> ru": "Азия" <utf8_string> "ZH-CN": " Asian "<utf8_string>}}" Country: {"geoname_id": 1814991 <uint32>
; "Iso_code": "CN" <utf8_string> "names": {"de": "The" ;utf8_string> "en": "the" <utf8_string> "es": "The" ;utf8_string> "fr": "Chine" <utf8_string> "ja": "China" <ut "PT-BR": "f8_string>" <utf8_string> "ru": "Китай"
;utf8_string> "ZH-CN": "China" <utf8_string>}} "location": { "AccuRacy_radius ": <uint16>" latitude ": 36.668300 <double>" Longitude ": 116.997200 <double> "Time_zone": "Asia/shanghai" <utf8_string>} "regist Ered_country ": {" geoname_id ": 1814991 <uint32>" Iso_code ":" CN "<ut
F8_string> "names": {"de": "the" <utf8_string> "en":
"The" <utf8_string> "es": "The", "" "<utf8_string>" fr ":
"Chine" <utf8_string> "ja": "China" <utf8_string> "PT-BR": "Zh-c" <utf8_string> "ru": "Китай" <utf8_string> " N ":" China "<utf8_string>}}" Subdivisions ": [{" Geoname_ ID ": 1796328 <uint32> "Iso_code": "SD" <utf8_string> "names": { "EN": "Shandong" <utf8_string> "fr": "Province de Shandong" <u Tf8_string> "ZH-CN": "Shandong Province" <utf8_string>}]}
get additional information individually:
For example, I just want to get countries, provinces and cities I wrote the following examples:
* * Gcc-v test.c-o mmdb-lmaxminddb/* #include <maxminddb.h> #include <stdio.h> #include <stdlib.h>
; #include <string.h> #include <errno.h> #include "cJSON.h" int main (int argc, char **argv) {char *filena
me = "Geolite2-city.mmdb";
Char *ip_address = argv[1];
mmdb_s MMDB;
Char Country[8] = {0};
Char province[64] = {0};
Char city[32] = {0};
if (ARGC < 2) {printf ("Usage <ip>\n");
Exit (1);
int status = Mmdb_open (filename, mmdb_mode_mmap, &mmdb);
if (mmdb_success!= status) {printf ("can ' t open%s-%s\n", filename, Mmdb_strerror (status));
if (Mmdb_io_error = = status) {printf ("IO ERROR:%s\n", Strerror (errno));
} exit (1);
int Gai_error, Mmdb_error;
mmdb_lookup_result_s result = mmdb_lookup_string (&mmdb, IP_Address, &gai_error, &mmdb_error); if (0!= gai_error) {printf ("Error from getaddrinfo foR%s-%s\n\n ", IP_Address, Gai_strerror (Gai_error));
Exit (2); } if (mmdb_success!= mmdb_error) {printf ("Got an error from LIBMAXMINDDB:%s\n\n", Mmdb_str
Error (Mmdb_error));
Exit (3);
} mmdb_entry_data_s Entry_data;
Get Country status = Mmdb_get_value (&result.entry, &entry_data, "Country", "Iso_code", NULL);
if (! Entry_data.has_data) {printf ("Entry_data has no data\n");
Goto end;
} if (Entry_data.type!= mmdb_data_type_utf8_string) {printf ("Entry_data ' type isn ' t UTF-8 ');"
Goto end;
} memcpy (Country, entry_data.utf8_string, entry_data.data_size);
printf ("country:%s\n", country); Get Province status = Mmdb_get_value (&result.entry, &entry_data, "subdivisions", "0", "names",
"EN", NULL);
if (! Entry_data.has_data) {printf ("Entry_data has no data\n");
Goto end; } if (Entry_data.type!= mmdb_data_type_utf8_string) {printf ("Entry_data ' type isn ' t UTF-8 ');"
Goto end;
} memcpy (province, Entry_data.utf8_string, entry_data.data_size);
printf ("province:%s\n", province);
Get City status = Mmdb_get_value (&result.entry, &entry_data, "City", "names", "en", NULL);
if (! Entry_data.has_data) {printf ("Entry_data has no data\n");
Goto end;
} if (Entry_data.type!= mmdb_data_type_utf8_string) {printf ("Entry_data ' type isn ' t UTF-8 ');"
Goto end;
memcpy (city, Entry_data.utf8_string, entry_data.data_size);
printf ("city:%s\n", city);
End:mmdb_close (&mmdb);
Exit (0); }
Command:./mmdb 112.225.35.70
Effect as shown: