Get Started With IP2Location C Module

Dependencies

This library requires IP2Location BIN database to function. You may download the BIN database at


Installation

Unix/Linux

    autoreconf -i -v --force
    ./configure
    make
    make install
    cd data
    perl ip-country.pl

							
						

Debian

curl -LO https://github.com/chrislim2888/IP2Location-C-Library/releases/download/8.6.1/ip2location-8.6.1.deb
sudo dpkg -i ip2location-8.6.1.deb

							
						

Ubuntu

sudo add-apt-repository ppa:ip2location/ip2location
sudo apt update
sudo apt install ip2location

							
						

Windows

    Execute "vcvarsall.bat". (This file is part of Microsoft Visual C, not ip2location code) 
    nmake -f Makefile.win
    cd data
    perl ip-country.pl

							
						

MacOS

    autoreconf -i -v --force
    export CFLAGS=-I/usr/include/malloc 
    ./configure
    make
    make install
    cd data
    perl ip-country.pl

							
						

Testing

    cd test
    test-IP2Location

							
						

Sample Codes

Query geolocation information from BIN database

You can query the geolocation information from the IP2Location BIN database as below:

#include <IP2Location.h>
#include <string.h>
#include <stdio.h>

int main () {
	char ipAddress[30];

#ifdef WIN32
	IP2Location *IP2LocationObj = IP2Location_open("..\\data\\IP-COUNTRY.BIN");
#else
	IP2Location *IP2LocationObj = IP2Location_open("../data/IP-COUNTRY.BIN");
#endif
	IP2LocationRecord *record = NULL;
	
	printf("IP2Location API version: %s \n", IP2Location_api_version_string());

	if (IP2LocationObj == NULL)
	{
		printf("Please install the database in correct path.\n");
		return -1;
	}

	fprintf(stdout,"IP2Location BIN version: %s\n", IP2Location_bin_version(IP2LocationObj));
	
	IP2Location_open_mem(IP2LocationObj, IP2LOCATION_SHARED_MEMORY);
	
	IP2LocationRecord *record = IP2Location_get_all(IP2LocationObj, "161.11.12.13");
	
	printf("%s %s %s %s %s %f %f %s %s %s %s %s %s %s %s %s %s %s %f %s\n", record->country_short, record->country_long, record->region, record->city, record->isp, record->latitude, record->longitude, record->domain, record->zipcode, record->timezone, record->netspeed, record->iddcode, record->areacode, record->weatherstationcode, record->weatherstationname, record->mcc, record->mnc, record->mobilebrand, record->elevation, record->usagetype, record->address_type, record->category, record->district, record->asn, record->as);
	
	IP2Location_free_record(record);
	
	IP2Location_close(IP2LocationObj);
	
	/*Below call will delete the shared memory unless if any other process is attached it. 
	 *if any other process is attached to it, shared memory will be closed when last process
	 *attached to it closes the shared memory 
	 *If any process call e, next process which IP2Location_open_mem
	 *with shared memory option, will open the new shared memory.Deleted memory will not be available for
	 * any new process but will be accesible for the processes which are already using it. 
	 */
	IP2Location_delete_shm();
}

							
						

IP2Location Sample Database (BIN)

Sample Packages - BIN File IPv4 IPv6
IP2Location DB1 Download (1,001.91 kB) Download (1.04 MB)
IP2Location DB2 Download (24.58 MB) Download (25.22 MB)
IP2Location DB3 Download (21.21 MB) Download (21.32 MB)
IP2Location DB4 Download (43.89 MB) Download (44.04 MB)
IP2Location DB5 Download (31.89 MB) Download (32.05 MB)
IP2Location DB6 Download (54.08 MB) Download (54.26 MB)
IP2Location DB7 Download (48.70 MB) Download (48.88 MB)
IP2Location DB8 Download (59.17 MB) Download (59.37 MB)
IP2Location DB9 Download (36.78 MB) Download (36.95 MB)
IP2Location DB10 Download (64.62 MB) Download (64.89 MB)
IP2Location DB11 Download (37.51 MB) Download (37.69 MB)
IP2Location DB12 Download (65.32 MB) Download (65.55 MB)
IP2Location DB13 Download (35.03 MB) Download (35.21 MB)
IP2Location DB14 Download (66.55 MB) Download (66.80 MB)
IP2Location DB15 Download (40.72 MB) Download (40.96 MB)
IP2Location DB16 Download (70.89 MB) Download (71.18 MB)
IP2Location DB17 Download (42.47 MB) Download (42.69 MB)
IP2Location DB18 Download (78.33 MB) Download (78.61 MB)
IP2Location DB19 Download (61.85 MB) Download (62.09 MB)
IP2Location DB20 Download (80.79 MB) Download (81.12 MB)
IP2Location DB21 Download (44.39 MB) Download (44.56 MB)
IP2Location DB22 Download (84.78 MB) Download (85.22 MB)
IP2Location DB23 Download (62.33 MB) Download (62.51 MB)
IP2Location DB24 Download (85.90 MB) Download (86.40 MB)
IP2Location DB25 Download (87.92 MB) Download (88.33 MB)
IP2Location DB26 Download (94.51 MB) Download (95.09 MB)

Articles & Tutorials