IP2C

IP2C is a small library that provides IP to country resolution using a binary file.
IP2C is shipped with a database from Webhosting.info, but supports the following :

The CSV file from those sources is converted into a highly efficient binary format:

  • Software77 database is converted from a ~5mb CSV to ~250k binary.
  • Webhosting.info database is converted from a ~5MB CSV to a ~550k binary.

License

IP2C is released under the terms GPL v2.

Supports

  • Command line
  • Java
  • PHP

Performance

All tests done on an Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GH
PHP:

  • No cache: 5000 searches/sec (Fast startup, slow query)
  • Memory cache: : 5700 searches/sec (Slow startup, fastest query)

Java:

  • No cache: 13900 searches/second. (Fast startup, slow query)
  • Memory mapped file: 265k searches/second. (Fast startup, fast query)
  • Memory cache: 281k searches/second. (Slow startup, fastest query)

Download

IP2C 2.0.0 (Database version : 2012.12.10)
See the changelog.

Source

Browse source

Command line (requires java in path)

Usage : To resolve an IP address:

java -jar ip2c.jar -r ip-address

Output format: if not found: UNKNOWN

if found: 2C 3C NAME

Example:

java -jar ip2c.jar -r 85.64.225.159

Outputs: IL ISR ISRAEL

To build binary file from CSV:

java -jar ip2c.jar -c csv_file [bin_file]

bin_file is optional, if not specified, file.csv will be converted to file.bin.

Java code

String ip = 85.64.225.159;
int caching1 = IP2Country.NO_CACHE;  // Straight on file, Fastest startup, slowest queries
int caching2 = IP2Country.MEMORY_MAPPED; // Memory mapped file, fast startup, fast quries.
int caching3 = IP2Country.MEMORY_CACHE; // load file into memory, slowerst startup, fastest queries
IP2Country ip2c = new IP2Country(caching1);
Country c = ip2c.getCountry(ip);
if (c == null)
{
        System.out.println("UNKNOWN");                          
}
else
{
        // will output IL ISR ISRAEL
        System.out.println(c.get2cStr() + " " + c.get3cStr() + " " + c.getName());      
}

PHP

Install the ip2c.php and the binary file into the same directroy.

<?php
require_once('ip2c.php');
$caching = false;
$ip2c = new ip2country($caching);
$res = $ip2c->get_country("85.64.225.159");
if ($res == false)
  echo "not found";
else
{
  $o2c = $res['id2'];
  $o3c = $res['id3'];
  $oname = $res['name'];
  echo "$o2c $o3c $oname"; // will output IL ISR ISRAEL
}
?>