Programmish
IP Address to Country Lookup

A flat file database available from Software77 allows for quick lookup of the country code associated with an IP address. Software77 updates the file every day, and it currently contains 3 billion+ IP addresses. For such a large coverage, the file weighs in at under a megabyte compressed.

Each entry in the flat file consists of a block of addresses, specified as a ‘From’ and ‘To’ address, the registry which handles the block, the date the block was assigned, and the Country full name, ISO-2 and ISO-3 forms. The full file specification can be found here.

Searching the file requires reformatting the IP address into a single number, using the following formula:

IP Address 1.2.3.4 becomes
(1 * 256 * 256 * 256) + (2 * 256 * 256) + (3 * 256) + 4 = 16,909,060

The ‘from’ and ‘to’ are ordered through the file, so searching for an IP address involves constructing the IP number as shown above, and then scanning a file for a block that has a from address <= the IP number and a to address >= the IP number.

The database is available in ZIP or GZ format.

Leave a Reply