For some of you out there who came across GeoIP you know useful it is to get the location information of an IP. Why? Assuming you are hosting a website and you want some statistics as to where your visitors come from then knowing what IP ranges are assigned to what country will give you an insight as to where your users come from.
Other uses might be to block users from accessing your website or server based on what country they are from.
So how does GeoIP do its magic? Well somewhere on the Internet there are databases which store information about what country has what ranges of IPs assigned to it. By making a request to such a service you can ask where the IP came from, and you might also be able to get other information such as what ISP that IP belongs to.
Databases containing this information appear to be maintained by various people/organizations, with some people, such as the company behind GeoIP offering their data for a fee. Free alternatives are available though. The second contains a CSV file containing LongIP ranges each indicating the country those IPs belong to.
The following are of use
http://www.countryipblocks.net/
http://ip-to-country.webhosting.info/node/view/6?XID=95e1f61de9cd95ac5718c2e4d1193889
Now what i wanted to do is take a list of ip addresses, convert each one of the to a LongIP and check in the CSV file to see if that IP corresponds to one of the countries inside.
A longIp is an IP where a mathematical function is applied to it to turn it into a unique number. Have numbers instead of IPs makes it easier to search through and compare. The following function is applied:
For IP AAA.CCC.ZZZ.XXX
LongIP = AAA * 256 * 256 * 256 + CCC * 256 *256 + ZZZ * 256 + XXX
The CSV data file as i found out does not contain all addresses. So i resorted to the for mentioned website http://www.countryipblocks.net/. I found that there were IP ranges for Africa that were not included in the original CSV file. I wrote another script which turned all the IPs in the list into LongIPs and create a file similar to the original CSV. I then did some manual work with the help of a spreadsheet editor (added the country info for the IPs). I then cat the 2 files into 1 file which i then used in my original script to do the checking.
All scripts were written in Perl. One thing to make notice of is that the speed fo the searches for a 3000 IP list takes around one hour due to the fact that i am searching through a CSV file. Had i used a database product to store the info this process would have been a lot quicker.
I am tired and just had lunch so if this doesn’t make sense let me know and i ll add anything i neglected mentioning!
http://ip-to-country.webhosting.info/node/view/6?XID=95e1f61de9cd95ac5718c2e4d1193889