KBSoft IP Locator

Introduction

It is often necessary to get information about a user or a site by their IP address. For example, such a need appears at the determination of a site's target audience. Having received the data about site users, it is possible to promote the site more effectively and develop different marketing actions. There are quite a lot of commercial services providing such information. The choice of such systems is often determined by the volume of information received, by its accuracy and importance, and also by the simplicity of adjusting and getting information. The purpose of this article is to show techniques of getting information about a user or a site by the IP address, and demonstrate an example of developing a demo application enabling you to get this information quickly and simply. You can find the demo application allowing you to receive information by IP address and the source code of this application in the zip file attached to the article. The demo application uses a simplified version of the class, delivered in the form of a DLL library. This class provides basic functions of information receipt by IP address. You can find the source code of this class here. You can use this code with research aims in your desktop or web applications for getting information about site users or with an aim to get information about some site (it is enough to know this site's IP address).

This article is based on the experience of KBSoft, and information received from the documentation of IP addresses analyzing systems studied in this article.

Review of Services for Getting Information by IP Address

A lot of companies provide services for getting information by IP address. Some of them, that provide free demo services, are described in this article. One of them is CDYNE. This company presents a web service named IP2Geo, which returns information in the form of XML by IP address. This web service is convenient, and no restrictions were noticed for a test receipt of information. But, there are disadvantages as well: The service does not provide a large quantity of information and the accuracy of location definition is not very high.

The company, IP2Location, provides similar services. The company's site provides more information than CDYNE service, but the demo page restricts the quantity of requests to not more than 20 per day and presents results in an inconvenient way for processing a form.

The company FraudLabs also uses the IP2Location database. Advantages and disadvantages coincide with those of the previous company with the exception of the fact that the demo page returns the result in XML form, which significantly simplifies the processing of this data. Moreover, with the help of demo pages it is possible to get much more information than that from the previous company.

The most convenient services, from the author's point of view, are those provided by the company GeoBytes. Their advantage is a big volume of useful information, there is a possibility to get a reply from the server in XML form, and also more acceptable restrictions of the trial-version (not more than 20 requests per hour). Besides, for getting information in different formats, there is a possibility to use different templates, which can present information in a desirable view. Unfortunately, disadvantages also exist here: Requests are sent via HTTP GET, but not via web service (it is under development now); this influences the security of the sent confidential data not in the best form (in case you bought a paid access). Moreover, in case of the use of paid access for getting information, it is complicated due to the need to use a session token.

Why It's Possible to Get Wrong Information

The most useful information that can be received by IP address is the user's location. It is only necessary to get information about the latitude and the longitude for that. The rest of the information needed to make different reports (for example, monetary units, time zone, and so forth) can be received by knowing these characteristics. But, it's not that simple to do as it seems at first sight. The IP address is not assigned with certain geographical location (for example, IP addresses of subnets are absolutely useless for determinating a geographical location). That is why it is impossible to get information about the location of the IP address possessor with an absolute accuracy. Furthermore, there are proxy-servers and NAT. These have an opaque structure; that is why it is impossible to receive the location of an IP address using proxy-server or NAT. What can be maximally received is the location of the proxy-server or NAT itself. Moreover, there are proxy-servers that generate random IP addresses or use the HTTP_X_FORWARDED_FOR header. IP address from mobile phones or satellite systems will distinguish themselves with a most inaccurate location. Nevertheless, in the majority of cases, the location of IP addresses possessors can be precisely determined.

Program Code Description

The designed KBS_IPLocatorClass class provides opportunities on the request of information from two sites: CDYNE and Geobytes. There is a possibility to request information about your own IP address and about any other IP address entered in the request field. Besides, the class presents the potential to parse the received XML data. Along with an XML template, Geobytes presents other templates providing the achievement of other aims as well (see here). The most useful template is valuepairsAll.txt, because this template presents the most complete information, but, unfortunately, problems can occur while parsing data received. Nevertheless, having developed a function on parsing and having added it into the class source code, you easily can add a possibility of processing any template and get information you are interested in with the help of the LookupCustomIP()class. Besides, the class's flexible architecture enables you to add a request and process information from any site presenting such information.

Here is the LookupCustomIP() method code:

public bool LookupCustomIP(string ipAddress, ServerTypes serverType)
{
   try
   {
      //create the request URL
      string requestURL="";
      //add the host element of the URL
      switch (serverType)
      {
         case ServerTypes.Geobytes:
            requestURL = "http://www.geobytes.com/
                          IpLocator.htm?GetLocation&template="
               + strTemplateFileName + "&IpAddress=" + ipAddress;
            break;
         case ServerTypes.Cdyne:
            requestURL = "http://ws.cdyne.com/ip2geo/ip2geo.asmx/
                          ResolveIP?ipAddress="

               + ipAddress + "&licenseKey=0";
            break;
      }

      // create the web client and obtain the response data
      // as a byte array
      WebClient webclient = new WebClient();
      byte[] response =
         webclient.DownloadData(requestURL.ToString());
      // process the XML result to obtain a validation result
      return processXMLResult(response);

   }
   catch (Exception)
   {
      return false;
   }
}

Besides, you can get to know your real IP address by which you will be identified in the global network with the help of the demo application. The class also presents a possibility to check the validity of the entered IP. The source code, containing only basic functions necessary for getting information by IP, is attached to the article. In real projects developed by KBSoft, a class with wider opportunities that doesn't use free services and allows users to collect information about visitors of the developed sites and make different reports is used.

KBSoft IP Locator

Using the Source Code

You can find the demo application allowing you to receive information by IP address and the source code of this application in the archive attached to the article. The demo application uses a simplified version of the class, delivered in the form of a DLL library. This class provides basic functions of information receipt by IP address. You can use this code in your program products (sites) with research aims, to receive information by IP address or just use the demo application for getting information about singular IP addresses. To use the source code attached to the article, it is necessary that .NET Framework 2.0 or higher is installed in the system, and the KBS_IPLocatorClass.dll library be in the same folder as the executed file. If you have Visual Studio 2005, you can run the demo application by opening the KBS_IPLocatorDemo.sln solution and executing the code. Or, you simply can start the executed file KBS_IPLocatorDemo.exe. Because Geobytes puts a restriction on the quantity of requests per hour (not more than 20), if you exhausted this limit, you can get information via CDYNE, but in this case the information will be less detailed.

The demo application presents the data shown in Table 1.

Table 1: Main parameters of the response.

Parameter Description
IP Presents the IP address for which the request was sent.
Country ID An internal key used to relate a specific row in the DB table to related rows in the Regions, Cities, and Dmas tables. This is the primary key and is unique. The number "113," for example, is the key for India.
Country A string representation of the country name; for example, "India," "Singapore,"or "Canada."
FIPS FIPS104, or more precisely FIPS 10-4, is a publication by the Federal Information Processing Standards body that identifies geopolitical entities around the world with a code. For example, "AS07" is the state of Victoria in Australia. This field represents logical groupings of these four-letter codes, so in this example the letters "AS" would be stored in the "FIPS104" field. More information about this publication can be found at the National Imagery and Mapping Agency at this page and this page.
ISO 2 Represents the "A 2" column in the ISO 3166 document that lists each country with associated alpha and numeric codes. More information on ISO 3166 standard can be found at the International Organization for Standardization website at this page. An example is "US," which represents the United States of America.
ISO 3 Represents the "A 3" column in the ISO 3166 document that lists each country with associated alpha and numeric codes. More information on ISO 3166 standard can be found at the International Organization for Standardization website at this page. An example is "GTM", which represents Guatemala.
ISO N Represents the "Number" column in the ISO 3166 document that lists each country with associated alpha and numeric codes. More information on the ISO 3166 standard can be found at the International Organization for Standardization website at this page. An example is "036", which represents Australia.
Internet Represents the "ccTLD" code designated by IANA (Internet Assigned Numbers Authority) that is employed in the DNS to identify hosts in various countries around the world. For example, the code "uk" is assigned to the United Kingdom. The "ccTLD" codes are based on the "A 2" column of the ISO 3166 standard document. More information on the Internet Assigned Numbers Authority can be found at this page, more information on the International Organization for Standardization can be found at this page.
Comment Represents the comments.
Region ID An internal key used to relate a specific row in the DB table to related rows in the "Cities" table.
Region The character representation of the sub-country geographical region. For example, the value of this column might be the state "Florida" in the U.S.A. or "New South Wales" in Australia.
Code A two-character code used to represent the general abbreviation for a sub-country geographical region. Each region has a unique code within a given country, and existing official codes were used where possible. "Official" can mean various things, but the ISO 3166-2 codes have been favored at this page. The codes are mnemonic. They consist of the first letter in the subdivision name, followed by a letter that occurs later in that name, unless it was impossible to assign unique codes by that rule.
ADM Related to the "FIPS104" field in the "Countries" table. Whereas the "FIPS104" code is used to identify a geopolitical region at country resolution, the "ADM1Code" is used to identify a geopolitical region at a sub-country region. For more information on the "ADM1Code" field visit the National Imagery and Mapping Agency Internet site at this page.
City ID An internal key used to relate specific rows in DB table to related rows in the "Subnets" and "NearbyCities" table. This is the primary key and is unique. The number "1225," for example, is the key for Melbourne, Australia.
City A character representation of the actual name of the city; for example: "New York," "London,"or "Paris." This field should not be used as the key for queries because the names of cities are not globally unique.
Latitude Stores the approximate latitude of the city specified by the rest of the row.
Longitude Stores the approximate longitude of the city specified by the rest of the row.
Time Zone Indicates which time zone the city is in. For example, the "TimeZone" field value for Sydney, Australia is "+10:00" being UTC (or GMT) plus 10 hours. The time is represented in UTC (Coordinated Universal Time). UTC was formerly known as GMT (Greenwich Mean Time). See ISO8601:2000(E) for further information on time zone representation. The ISO is available in this PDF file.
DMA The DMA code as assigned by Nielsen Media: http://www.nielsenmedia.com. Designated Market Areas (DMAs) are used by Nielsen Media Research to identify TV stations whose broadcast signals reach a specific area and attract the most viewers. A DMA consists of all counties whose largest viewing share is given to stations of that same market area. Non-overlapping DMAs cover the entire continental United States, Hawaii, and parts of Alaska. There are currently 210 Designated Market Areas throughout the U.S.
DMA ID An internal key used to relate specific rows in the DB table.
Market Represents the market area.
Certainty Lists the certainty for the information contained in the DB in relation to the subnet being searched on and the city that it is related to. A lower certainty rating may indicate that the data gathered was inconclusive or that the particular subnet serves a greater geographical area.
Location Code This field is a four-character alpha code used to represent a city within a given country and region. The code consists of the first letter of each word, followed by the next two characters in the last word. If the city has only one word, the first four characters would be used. When generating these codes, if there was a duplicate code generated, the next letter in the city name would be used. If a duplicate code is still found, the last character of the code is replaced by a letter from the alphabet in descending order.
IP Address The IP address that used to perform the resolution. It is useful to use this field to pass in a selected IP address for use with testing.
Area Code Represents the area code.
State Province Represents the information about state province.

Conclusion

In the article, various techniques of getting information by IP Address were demonstrated. The source code of the created class in the form of a DLL library presenting main functions of getting information by IP address, and also a demo application using this class were shown.

References

  • This is a demo page of the company CDYNE for getting information by IP Address.
  • This is a demo page of the company IP2Location for getting information by IP Address.
  • This page contains the set of demo pages of the company FraudLabs for getting information by IP Address.
  • This is a demo page of the company Geobytes for getting information by IP Address.
  • This page contains the latest version of this article and the C# implementation of the class described in the article.


About the Author

Anton Zlobin

Anton Zlobin is a .NET developer at KB_Soft Group, an offshore software development company located in Russia, Novosibirsk. Here he has worked on various .NET projects. He has a Master Degree in Computer Technology from Novosibirsk State Technical University.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds