GetCountryFromIP

There are many website out there in the internet that displays users country based on the IP address.

I tried to find a service which is free of cost and gives us accurate results based on the IP but i didnt find any. So i thought of writing my own api.

A Brief About American Registry for Internet Numbers (ARIN):
{
The American Registry for Internet Numbers (ARIN) is the Regional Internet Registry (RIR) for Canada, many Caribbean and North Atlantic islands, and the United States. ARIN manages the distribution of Internet number resources, including IPv4 and IPv6 address space and AS numbers. ARIN opened its doors for business on December 22, 1997[1] after incorporating on April 18, 1997.[2] ARIN is a nonprofit corporation with headquarters in Chantilly, Virginia, USA.[3][4]

What does they do:

IPv4 address allocation and assignment
IPv6 address allocation and assignment
AS number assignment
Directory services including:
Registration transaction information (WHOIS)
Routing information (Internet Routing Registry)
DNS (Reverse)
}
Note: This was copied from Wikipedia

There are few other orgnizations(total of 5) accross the globe similar to ARNIC. They perform similar kind of job.

Raw data provided by Arnic related to IP address allocation
Aric provides few files that provides raw data about the IP allocation in different countries.

ftp://ftp.arin.net/pub/stats/ripencc/delegated-ripencc-latest
ftp://ftp.arin.net/pub/stats/lacnic/delegated-lacnic-latest
ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest
ftp://ftp.arin.net/pub/stats/apnic/delegated-apnic-latest
ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest

Root Solution File
IP-GeoLocation\IPCountry\GetCountryFromIP\GetCountryFromIP\GenerateCountryFromIP.sln

MY project Implementation
1.The core project for this is GeneratefilesLibrary.csproj. it contains 2 class files
--GenerateFiles.cs : In this class file i am trying to read the raw data present in all the 5 ftp sites mentioned above and i am writing required data to like Europe.txt,North-America.txt,Asia.txt,Africa.txt,latinAmerica.txt. These files contains startIPAddress and country Name.

Later i am having a file \\GetCountryFromIP\\IpData\\IPCountry.txt which will contain data from all the above mentioned files. here i am trying to remove some unnecessary data from all the above 5 files.

How do we analyze raw data:
apnic|AU|ipv4|192.70.174.0|256|19900326|assigned
apnic|JP|ipv4|192.70.179.0|256|19930331|assigned
apnic|AU|ipv4|192.80.179.0|256|xxx|assigned

Here AU refers Australia and JP refers japan.
If we see 1st one the IP address 192.70.174.0 is the address that was allocated to AU and 192.70.179.0 is the starting one to JP. So all the IP address that were in between 192.70.174.0--192.70.179.0(excluding) comes under AU. Like 192.70.175.6 it comes under AU and 192.70.179.1 it comes under JP. Again starting from 192.80.179.0 to some other IP falls under AU.

2. Initially i am having a console application(GenerateFiles.csproj) this will call GenerateFiles class and generates required files for processing.

Note this console app can be executed only once per day to get updated values from server(ARIN) , we can write a windows scheduler to execute this console app everyday and collect raw data

3.Now we are having required files for processing. GetCountryNames.cs file contains the logic to proccess these files and get exact 2 letter country code for a given IP address.

4. Now i am having a servcie(asmx service) which is going to call a method in getCountryNames.cs and output the country name(2 letter country code) to the end user.

Thanks,
pavan(http://pavanarya.wordpress.com)

Last edited Jun 7, 2013 at 5:37 PM by paryasomayajulu, version 2