├── ChangeLog ├── README.md ├── examples ├── asnExample.cs ├── asnV6Example.cs ├── cityExample.cs ├── cityV6Example.cs ├── countryExample.cs ├── countryV6Example.cs ├── domainExample.cs ├── infoExample.cs ├── ispExample.cs ├── netspeedCellExample.cs ├── netspeedExample.cs ├── orgExample.cs └── regionExample.cs └── src ├── Country.cs ├── DatabaseInfo.cs ├── Location.cs ├── LookupService.cs ├── Region.cs └── RegionName.cs /ChangeLog: -------------------------------------------------------------------------------- 1 | 1.19 ??? 2 | - Update RegionNames 20130903 ( Boris Zentner ) 3 | - Add NetSpeedCell example ( Boris Zentner ) 4 | - LookupService throws an exception if the database file is not found. ( Guenter Knauf ) 5 | - Cleanup the example files ( Guenter Knauf ) 6 | - Add new example infoExample.cs ( Guenter Knauf ) 7 | - Lock init_region_names to make the initialization phase thread-safe ( Jeremy Walther ) 8 | 1.18 Feb 20th, 2013 9 | - Add South Sudan ( Boris Zentner ) 10 | - Update FIPS codes ( Boris Zentner ) 11 | - Fix garbage at the end of the database_type string ( Boris Zentner ) 12 | 1.17 Apr 10th, 2012 13 | - Update FIPS codes ( Boris Zentner ) 14 | 1.16 Mar 26th, 2012 15 | - Update country and FIPS codes ( Boris Zentner ) 16 | 1.15 Mar 15th, 2012 17 | - Update FIPS codes ( Boris Zentner ) 18 | 1.14 Jul 25th, 2011 19 | - Add new database types for CountryV6, CityV6, ASNumV6 and ASNum ( Boris Zentner ) 20 | 1.13 Jan 21th, 2011 21 | - Add ASNum support. ( Boris Zentner ) 22 | - Fix getDate and getType ( Richard Régul ) 23 | 1.12 Aug 18th, 2009 24 | - Sync RegionName.cs with more recent FIPS codes ( 20090723 ). ( Boris Zentner ) 25 | - Guard most functions in lookupService by either lock or [MethodImpl(MethodImplOptions.Synchronized)] ( Kfir Harel ) 26 | 1.11 May 16th, 2009 27 | - Fix IndexOutOfRangeException error when a lookup end in the last valid 28 | netblock using GEOIP_MEMORY_CACHE mode ( Boris Zentner ) 29 | - Fixed spelling of Kazakhstan, was Kazakstan ( Boris Zentner ) 30 | - Sync RegionName.cs with more recent FIPS codes. ( Boris Zentner ) 31 | 1.10 Oct 23th, 2008 32 | - Remove record.dma_code -- use record.metro_code instead. ( Boris Zentner ) 33 | 1.9 Oct 22th, 2008 34 | - Add new static class RegionName. use RegionName.getRegionName("FR", "02") to get the region name. rec.regionName does work for city records ( Boris Zentner ) 35 | 1.8 May 6th, 2008 36 | - remove duplicate getID functions using swapbytes in flavor of bytestoLong (Boris Zentner) 37 | 38 | 1.7 March 18th, 2008 39 | - Changed license from GPL to LGPL 40 | - Added BL/Saint Barthelemy, MF/Saint Martin (ISO-3166-1 additions) 41 | - getCountry method works with GeoIP City as well as GeoIP Country (Frank Mather) 42 | 43 | 1.6 February 20th, 2007 44 | - Added AX/Aland Islands, GG/Guernsey, IM/Isle of Man, JE/Jersey (ISO-3166-1 changes) 45 | - Replaced CS/Serbia and Montenegro with RS/Serbia, removed ZR/Zaire, added ME/Montenegro 46 | - Replaced swapbytes(addr.Address) with bytestoLong(addr.GetAddressBytes()) 47 | since IPAddress.Address property was deprecated in .NET 2.0 48 | (Malcolm van Raalte / GeoSign) 49 | - TP/East Timor changed to TL/Timor-Leste, reflecting changes in ISO-3166 50 | 51 | 1.5 January 19th, 2006 52 | - Added support for GeoIP Netspeed lookups (Frank Mather) 53 | - Replaced Yugoslavia with Serbia and Montenegro 54 | 55 | 1.4 December 18th, 2004 56 | - Added GEOIP_MEMORY_CACHE option (Frank Mather) 57 | 58 | 1.3 November 10th, 2004 59 | - Fixed City Database lookup (Frank Mather) 60 | 61 | 1.2 November 2nd, 2004 62 | - Fixed problem with postalCode, city and region fields 63 | returning null char (Frank Mather) 64 | 65 | 1.1 October 6th, 2004 66 | Added swapbytes function, fixed lookup issues (Frank Mather) 67 | 68 | 1.0 July 23rd, 2004 69 | Initial Release 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GeoIP Legacy .NET API # 2 | 3 | ## Deprecation Notice ## 4 | 5 | This API is deprecated and no longer supported. Please use our [our GeoIP2 6 | .NET API](https://github.com/maxmind/GeoIP2-dotnet) instead. 7 | 8 | ## IP Geolocation Usage ## 9 | 10 | IP geolocation is inherently imprecise. Locations are often near the center of 11 | the population. Any location provided by a GeoIP database or web service 12 | should not be used to identify a particular address or household. 13 | 14 | ## Thread Safety ## 15 | 16 | As of version 1.14 this API is thread safe. 17 | 18 | ## Instructions for Mono ## 19 | 20 | To compile the examples: 21 | 22 | Country: 23 | gmcs examples/countryExample.cs src/*.cs 24 | 25 | City: 26 | gmcs examples/cityExample.cs src/*.cs 27 | 28 | Country v6: 29 | gmcs examples/countryV6Example.cs src/*.cs 30 | 31 | City v6: 32 | gmcs examples/cityV6Example.cs src/*.cs 33 | 34 | Region: 35 | gmcs examples/regionExample.cs src/*.cs 36 | 37 | Organization: 38 | gmcs examples/orgExample.cs src/*.cs 39 | 40 | Domain: 41 | gmcs examples/domainExample.cs src/*.cs 42 | 43 | Netspeed cell: 44 | gmcs examples/netspeedCellExample.cs src/*.cs 45 | 46 | To run the examples: 47 | 48 | Country: 49 | mono countryExample.exe 50 | 51 | City: 52 | mono cityExample.exe 24.24.24.24 53 | 54 | Organization: 55 | mono orgExample.exe 56 | 57 | Domain: 58 | mono domainExample.exe 59 | 60 | ASN: 61 | mono asnExample.exe 62 | 63 | Country v6: 64 | mono countryV6Example.exe 65 | 66 | City: 67 | mono cityV6Example.exe ::24.24.24.24 68 | 69 | ASN v6: 70 | mono asnV6Example.exe 71 | 72 | ## Memory Caching ## 73 | 74 | There are two options available: 75 | 76 | * `GEOIP_STANDARD` - Read database from filesystem. Uses least memory. 77 | * `GEOIP_MEMORY_CACHE` - load database into memory, faster performance 78 | but uses more memory 79 | 80 | Copyright 2013 MaxMind, Inc. 81 | 82 | Licensed under the LGPL 83 | -------------------------------------------------------------------------------- /examples/asnExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPASNum.dat"; 8 | //open the database 9 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 10 | //get org of the ip address 11 | String orgorisp = ls.getOrg("24.24.24.24"); 12 | Console.Write("asn: " + orgorisp + "\n"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /examples/asnV6Example.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPASNumv6.dat"; 8 | //open the database 9 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 10 | //get org of the ip address 11 | String orgorisp = ls.getOrgV6("2001:4860:0:1001::68"); 12 | Console.Write("asn: " + orgorisp + "\n"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /examples/cityExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the city of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPCity.dat"; 8 | //open the database 9 | try { 10 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 11 | //get city location of the ip address 12 | if (args.Length > 0) { 13 | Location l = ls.getLocation(args[0]); 14 | if (l != null) { 15 | Console.Write("country code " + l.countryCode + "\n"); 16 | Console.Write("country name " + l.countryName + "\n"); 17 | Console.Write("region " + l.region + "\n"); 18 | Console.Write("city " + l.city + "\n"); 19 | Console.Write("postal code " + l.postalCode + "\n"); 20 | Console.Write("latitude " + l.latitude + "\n"); 21 | Console.Write("longitude " + l.longitude + "\n"); 22 | Console.Write("metro code " + l.metro_code + "\n"); 23 | Console.Write("area code " + l.area_code + "\n"); 24 | Console.Write("region name " + l.regionName + "\n"); 25 | } else { 26 | Console.Write("IP Address Not Found\n"); 27 | } 28 | } else { 29 | Console.Write("Usage: cityExample IPAddress\n"); 30 | } 31 | } catch (System.Exception e) { 32 | Console.Write("Error" + e.Message + "\n"); 33 | } 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /examples/cityV6Example.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the city of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPCity.dat"; 8 | //open the database 9 | try { 10 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 11 | //get city location of the ip address 12 | if (args.Length > 0) { 13 | Location l = ls.getLocationV6(args[0]); 14 | if (l != null) { 15 | Console.Write("country code " + l.countryCode + "\n"); 16 | Console.Write("country name " + l.countryName + "\n"); 17 | Console.Write("region " + l.region + "\n"); 18 | Console.Write("city " + l.city + "\n"); 19 | Console.Write("postal code " + l.postalCode + "\n"); 20 | Console.Write("latitude " + l.latitude + "\n"); 21 | Console.Write("longitude " + l.longitude + "\n"); 22 | Console.Write("metro code " + l.metro_code + "\n"); 23 | Console.Write("area code " + l.area_code + "\n"); 24 | Console.Write("region name " + l.regionName + "\n"); 25 | } else { 26 | Console.Write("IP Address Not Found\n"); 27 | } 28 | } else { 29 | Console.Write("Usage: cityExample IPAddress\n"); 30 | } 31 | } catch (System.Exception e) { 32 | Console.Write("Error" + e.Message + "\n"); 33 | } 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /examples/countryExample.cs: -------------------------------------------------------------------------------- 1 | // an example of how to lookup the country of a ip address 2 | 3 | // This only works with the GeoIP Country database 4 | // See regionExample.cs, cityExample.cs, and orgExample.cs 5 | // for the Region database, city database, and ISP/Organization databases 6 | 7 | using System; 8 | using System.IO; 9 | class Appa0{ 10 | public static void Main(String[] args){ 11 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 12 | string GeoipDb = GeoipDbPath + "GeoIP.dat"; 13 | //open the database 14 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_MEMORY_CACHE); 15 | //get country of the ip address 16 | Country c = ls.getCountry("24.24.24.24"); 17 | Console.Write(" code: " + c.getCode()+"\n"); 18 | Console.Write(" name: " + c.getName()+"\n"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /examples/countryV6Example.cs: -------------------------------------------------------------------------------- 1 | // an example of how to lookup the country of a ip address 2 | 3 | // This only works with the GeoIP Country database 4 | // See regionExample.cs, cityExample.cs, and orgExample.cs 5 | // for the Region database, city database, and ISP/Organization databases 6 | 7 | using System; 8 | using System.IO; 9 | class Appa0{ 10 | public static void Main(String[] args){ 11 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 12 | string GeoipDb = GeoipDbPath + "GeoIP.dat"; 13 | //open the database 14 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_MEMORY_CACHE); 15 | //get country of the ip address 16 | Country c = ls.getCountryV6("2001:4860:0:1001::68"); 17 | Console.Write(" code: " + c.getCode()+"\n"); 18 | Console.Write(" name: " + c.getName()+"\n"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /examples/domainExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | //open the database 7 | LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPDomain.dat", LookupService.GEOIP_STANDARD); 8 | //get org of the ip address 9 | String domain = ls.getOrg("24.24.24.24"); 10 | Console.Write(" domain: " + domain + "\n"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/infoExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the database info 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPCity.dat"; 8 | if (args.Length > 0) { 9 | GeoipDb = args[0]; 10 | } 11 | //open the database 12 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 13 | //get the database info 14 | DatabaseInfo dbinfo = ls.getDatabaseInfo(); 15 | Console.Write(" dbinfo string: " + dbinfo.toString() + "\n"); 16 | Console.Write(" dbinfo type: " + dbinfo.getType() + "\n"); 17 | Console.Write(" dbinfo date: " + dbinfo.getDate() + "\n"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /examples/ispExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | //open the database 7 | LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPISP.dat", LookupService.GEOIP_STANDARD); 8 | //get org of the ip address 9 | String orgorisp = ls.getOrg("24.24.24.24"); 10 | Console.Write(" isp: " + orgorisp + "\n"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/netspeedCellExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the netspeedcell, asnum, isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | //open the database 7 | LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPNetSpeedCell.dat", LookupService.GEOIP_STANDARD); 8 | //get org of the ip address 9 | String orgorisp = ls.getOrg("24.24.24.24"); 10 | Console.Write(" NetspeedCell: " + orgorisp + "\n"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/netspeedExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | class Appa0{ 4 | public static void Main(String[] args){ 5 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 6 | string GeoipDb = GeoipDbPath + "GeoIPNetSpeed.dat"; 7 | //open the database 8 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 9 | int id = ls.getID(args[0]); 10 | int speed = id; 11 | if (speed == LookupService.GEOIP_UNKNOWN_SPEED){ 12 | Console.Write("Unknown \n"); 13 | } else if (speed == LookupService.GEOIP_DIALUP_SPEED){ 14 | Console.Write("Dialup \n"); 15 | } else if (speed == LookupService.GEOIP_CABLEDSL_SPEED){ 16 | Console.Write("Cable/DSL \n"); 17 | } else if (speed == LookupService.GEOIP_CORPORATE_SPEED){ 18 | Console.Write("Corporate \n"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /examples/orgExample.cs: -------------------------------------------------------------------------------- 1 | //an example of how to lookup the isp or org of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | string GeoipDb = GeoipDbPath + "GeoIPOrg.dat"; 8 | //open the database 9 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 10 | //get org of the ip address 11 | String orgorisp = ls.getOrg("24.24.24.24"); 12 | Console.Write(" org: " + orgorisp + "\n"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /examples/regionExample.cs: -------------------------------------------------------------------------------- 1 | // an example of how to lookup the region of a ip address 2 | using System; 3 | using System.IO; 4 | class Appa0{ 5 | public static void Main(String[] args){ 6 | const string GeoipDbPath = "/usr/local/share/GeoIP/"; 7 | const string GeoipDb = GeoipDbPath + "GeoIPRegion.dat"; 8 | //open the database 9 | LookupService ls = new LookupService(GeoipDb, LookupService.GEOIP_STANDARD); 10 | //get region of the ip address 11 | Region c = ls.getRegion("24.24.24.24"); 12 | Console.Write(" code: " + c.getcountryCode()+"\n"); 13 | Console.Write(" name: " + c.getcountryName()+"\n"); 14 | Console.Write(" region: " + c.getregion() + "\n"); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/Country.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Country.cs 3 | * 4 | * Copyright (C) 2008 MaxMind Inc. All Rights Reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | 22 | using System; 23 | using System.IO; 24 | public class Country { 25 | 26 | private String code; 27 | private String name; 28 | 29 | /** 30 | * Creates a new Country. 31 | * 32 | * @param code the country code. 33 | * @param name the country name. 34 | */ 35 | public Country(String code, String name) { 36 | this.code = code; 37 | this.name = name; 38 | } 39 | /** 40 | * Returns the ISO two-letter country code of this country. 41 | * 42 | * @return the country code. 43 | */ 44 | public String getCode() { 45 | return code; 46 | } 47 | 48 | /** 49 | * Returns the name of this country. 50 | * 51 | * @return the country name. 52 | */ 53 | public String getName() { 54 | return name; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/DatabaseInfo.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * DatabaseInfo.cs 3 | * 4 | * Copyright (C) 2008 MaxMind Inc. All Rights Reserved. 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | 22 | using System; 23 | using System.IO; 24 | 25 | public class DatabaseInfo { 26 | 27 | public static int COUNTRY_EDITION = 1; 28 | public static int REGION_EDITION_REV0 = 7; 29 | public static int REGION_EDITION_REV1 = 3; 30 | public static int CITY_EDITION_REV0 = 6; 31 | public static int CITY_EDITION_REV1 = 2; 32 | public static int ORG_EDITION = 5; 33 | public static int ISP_EDITION = 4; 34 | public static int PROXY_EDITION = 8; 35 | public static int ASNUM_EDITION = 9; 36 | public static int NETSPEED_EDITION = 10; 37 | public static int DOMAIN_EDITION = 11; 38 | public static int COUNTRY_EDITION_V6 = 12; 39 | public static int ASNUM_EDITION_V6 = 21; 40 | public static int ISP_EDITION_V6 = 22; 41 | public static int ORG_EDITION_V6 = 23; 42 | public static int DOMAIN_EDITION_V6 = 24; 43 | public static int CITY_EDITION_REV1_V6 = 30; 44 | public static int CITY_EDITION_REV0_V6 = 31; 45 | public static int NETSPEED_EDITION_REV1 = 32; 46 | public static int NETSPEED_EDITION_REV1_V6 = 33; 47 | 48 | 49 | //private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 50 | 51 | private String info; 52 | /** 53 | * Creates a new DatabaseInfo object given the database info String. 54 | * @param info 55 | */ 56 | 57 | public DatabaseInfo(String info) { 58 | this.info = info; 59 | } 60 | 61 | public int getType() { 62 | if ((info == null) | (info == "")) { 63 | return COUNTRY_EDITION; 64 | } 65 | else { 66 | // Get the type code from the database info string and then 67 | // subtract 105 from the value to preserve compatability with 68 | // databases from April 2003 and earlier. 69 | return Convert.ToInt32(info.Substring(4, 3)) - 105; 70 | } 71 | } 72 | 73 | /** 74 | * Returns true if the database is the premium version. 75 | * 76 | * @return true if the premium version of the database. 77 | */ 78 | public bool isPremium() { 79 | return info.IndexOf("FREE") < 0; 80 | } 81 | 82 | /** 83 | * Returns the date of the database. 84 | * 85 | * @return the date of the database. 86 | */ 87 | public DateTime getDate() { 88 | for (int i=0; i= 106) { 145 | // Backward compatibility with databases from April 2003 and earlier 146 | databaseType -= 105; 147 | } 148 | // Determine the database type. 149 | if (databaseType == DatabaseInfo.REGION_EDITION_REV0) { 150 | databaseSegments = new int[1]; 151 | databaseSegments[0] = STATE_BEGIN_REV0; 152 | recordLength = STANDARD_RECORD_LENGTH; 153 | } else if (databaseType == DatabaseInfo.REGION_EDITION_REV1) { 154 | databaseSegments = new int[1]; 155 | databaseSegments[0] = STATE_BEGIN_REV1; 156 | recordLength = STANDARD_RECORD_LENGTH; 157 | } else if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || 158 | databaseType == DatabaseInfo.CITY_EDITION_REV1 || 159 | databaseType == DatabaseInfo.ORG_EDITION || 160 | databaseType == DatabaseInfo.ORG_EDITION_V6 || 161 | databaseType == DatabaseInfo.ISP_EDITION || 162 | databaseType == DatabaseInfo.ISP_EDITION_V6 || 163 | databaseType == DatabaseInfo.ASNUM_EDITION || 164 | databaseType == DatabaseInfo.ASNUM_EDITION_V6 || 165 | databaseType == DatabaseInfo.NETSPEED_EDITION_REV1 || 166 | databaseType == DatabaseInfo.NETSPEED_EDITION_REV1_V6 || 167 | databaseType == DatabaseInfo.CITY_EDITION_REV0_V6 || 168 | databaseType == DatabaseInfo.CITY_EDITION_REV1_V6 169 | ) 170 | { 171 | databaseSegments = new int[1]; 172 | databaseSegments[0] = 0; 173 | if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || 174 | databaseType == DatabaseInfo.CITY_EDITION_REV1 || 175 | databaseType == DatabaseInfo.ASNUM_EDITION_V6 || 176 | databaseType == DatabaseInfo.NETSPEED_EDITION_REV1 || 177 | databaseType == DatabaseInfo.NETSPEED_EDITION_REV1_V6 || 178 | databaseType == DatabaseInfo.CITY_EDITION_REV0_V6 || 179 | databaseType == DatabaseInfo.CITY_EDITION_REV1_V6 || 180 | databaseType == DatabaseInfo.ASNUM_EDITION 181 | ) { 182 | recordLength = STANDARD_RECORD_LENGTH; 183 | } 184 | else { 185 | recordLength = ORG_RECORD_LENGTH; 186 | } 187 | file.Read(buf,0,SEGMENT_RECORD_LENGTH); 188 | for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { 189 | databaseSegments[0] += (unsignedByteToInt(buf[j]) << (j * 8)); 190 | } 191 | } 192 | break; 193 | } 194 | else { 195 | //file.Seek(file.getFilePointer() - 4); 196 | file.Seek(-4,SeekOrigin.Current); 197 | //file.Seek(file.position-4,SeekOrigin.Begin); 198 | } 199 | } 200 | if ((databaseType == DatabaseInfo.COUNTRY_EDITION) || 201 | (databaseType == DatabaseInfo.COUNTRY_EDITION_V6) || 202 | (databaseType == DatabaseInfo.PROXY_EDITION) || 203 | (databaseType == DatabaseInfo.NETSPEED_EDITION)) { 204 | databaseSegments = new int[1]; 205 | databaseSegments[0] = COUNTRY_BEGIN; 206 | recordLength = STANDARD_RECORD_LENGTH; 207 | } 208 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 209 | int l = (int) file.Length; 210 | dbbuffer = new byte[l]; 211 | file.Seek(0,SeekOrigin.Begin); 212 | file.Read(dbbuffer,0,l); 213 | } 214 | } 215 | } 216 | public void close(){ 217 | try { 218 | lock ( ioLock ) { file.Close(); } 219 | file = null; 220 | } 221 | catch (Exception) { } 222 | } 223 | public Country getCountry(IPAddress ipAddress) { 224 | return getCountry(bytestoLong(ipAddress.GetAddressBytes())); 225 | } 226 | public Country getCountryV6(String ipAddress){ 227 | IPAddress addr; 228 | try { 229 | addr = IPAddress.Parse(ipAddress); 230 | } 231 | //catch (UnknownHostException e) { 232 | catch (Exception e) { 233 | Console.Write(e.Message); 234 | return UNKNOWN_COUNTRY; 235 | } 236 | return getCountryV6(addr); 237 | } 238 | public Country getCountry(String ipAddress){ 239 | IPAddress addr; 240 | try { 241 | addr = IPAddress.Parse(ipAddress); 242 | } 243 | //catch (UnknownHostException e) { 244 | catch (Exception e) { 245 | Console.Write(e.Message); 246 | return UNKNOWN_COUNTRY; 247 | } 248 | // return getCountry(bytestoLong(addr.GetAddressBytes())); 249 | return getCountry(bytestoLong(addr.GetAddressBytes())); 250 | } 251 | public Country getCountryV6(IPAddress ipAddress){ 252 | if (file == null) { 253 | //throw new IllegalStateException("Database has been closed."); 254 | throw new Exception("Database has been closed."); 255 | } 256 | if ((databaseType == DatabaseInfo.CITY_EDITION_REV1) | 257 | (databaseType == DatabaseInfo.CITY_EDITION_REV0)) { 258 | Location l = getLocation(ipAddress); 259 | if (l == null) { 260 | return UNKNOWN_COUNTRY; 261 | } 262 | else { 263 | return new Country(l.countryCode, l.countryName); 264 | } 265 | } 266 | else { 267 | int ret = SeekCountryV6(ipAddress) - COUNTRY_BEGIN; 268 | if (ret == 0) { 269 | return UNKNOWN_COUNTRY; 270 | } 271 | else { 272 | return new Country(countryCode[ret], countryName[ret]); 273 | } 274 | } 275 | } 276 | 277 | public Country getCountry(long ipAddress){ 278 | if (file == null) { 279 | //throw new IllegalStateException("Database has been closed."); 280 | throw new Exception("Database has been closed."); 281 | } 282 | if ((databaseType == DatabaseInfo.CITY_EDITION_REV1) | 283 | (databaseType == DatabaseInfo.CITY_EDITION_REV0)) { 284 | Location l = getLocation(ipAddress); 285 | if (l == null) { 286 | return UNKNOWN_COUNTRY; 287 | } 288 | else { 289 | return new Country(l.countryCode, l.countryName); 290 | } 291 | } 292 | else { 293 | int ret = SeekCountry(ipAddress) - COUNTRY_BEGIN; 294 | if (ret == 0) { 295 | return UNKNOWN_COUNTRY; 296 | } 297 | else { 298 | return new Country(countryCode[ret], countryName[ret]); 299 | } 300 | } 301 | } 302 | 303 | public int getID(String ipAddress){ 304 | IPAddress addr; 305 | try { 306 | addr = IPAddress.Parse(ipAddress); 307 | } 308 | catch (Exception e) { 309 | Console.Write(e.Message); 310 | return 0; 311 | } 312 | return getID(bytestoLong(addr.GetAddressBytes())); 313 | } 314 | 315 | public int getID(IPAddress ipAddress) { 316 | 317 | return getID(bytestoLong(ipAddress.GetAddressBytes())); 318 | } 319 | 320 | public int getID(long ipAddress){ 321 | if (file == null) { 322 | throw new Exception("Database has been closed."); 323 | } 324 | int ret = SeekCountry(ipAddress) - databaseSegments[0]; 325 | return ret; 326 | } 327 | public DatabaseInfo getDatabaseInfo(){ 328 | if (databaseInfo != null) { 329 | return databaseInfo; 330 | } 331 | try { 332 | // Synchronize since we're accessing the database file. 333 | lock ( ioLock ) { 334 | bool hasStructureInfo = false; 335 | byte [] delim = new byte[3]; 336 | // Advance to part of file where database info is stored. 337 | file.Seek(-3,SeekOrigin.End); 338 | for (int i=0; i= 1000){ 401 | record.countryCode = "US"; 402 | record.countryName = "United States"; 403 | ch[0] = (char)(((seek_region - 1000)/26) + 65); 404 | ch[1] = (char)(((seek_region - 1000)%26) + 65); 405 | record.region = new String(ch); 406 | } else { 407 | record.countryCode = countryCode[seek_region]; 408 | record.countryName = countryName[seek_region]; 409 | record.region = ""; 410 | } 411 | } else if (databaseType == DatabaseInfo.REGION_EDITION_REV1) { 412 | seek_region = SeekCountry(ipnum) - STATE_BEGIN_REV1; 413 | char [] ch = new char[2]; 414 | if (seek_region < US_OFFSET) { 415 | record.countryCode = ""; 416 | record.countryName = ""; 417 | record.region = ""; 418 | } else if (seek_region < CANADA_OFFSET) { 419 | record.countryCode = "US"; 420 | record.countryName = "United States"; 421 | ch[0] = (char)(((seek_region - US_OFFSET)/26) + 65); 422 | ch[1] = (char)(((seek_region - US_OFFSET)%26) + 65); 423 | record.region = new String(ch); 424 | } else if (seek_region < WORLD_OFFSET) { 425 | record.countryCode = "CA"; 426 | record.countryName = "Canada"; 427 | ch[0] = (char)(((seek_region - CANADA_OFFSET)/26) + 65); 428 | ch[1] = (char)(((seek_region - CANADA_OFFSET)%26) + 65); 429 | record.region = new String(ch); 430 | } else { 431 | record.countryCode = countryCode[(seek_region - WORLD_OFFSET) / FIPS_RANGE]; 432 | record.countryName = countryName[(seek_region - WORLD_OFFSET) / FIPS_RANGE]; 433 | record.region = ""; 434 | } 435 | } 436 | return record; 437 | } 438 | public Location getLocation(IPAddress addr){ 439 | return getLocation(bytestoLong(addr.GetAddressBytes())); 440 | } 441 | public Location getLocationV6(String str){ 442 | IPAddress addr; 443 | try { 444 | addr = IPAddress.Parse(str); 445 | } 446 | catch (Exception e) { 447 | Console.Write(e.Message); 448 | return null; 449 | } 450 | 451 | return getLocationV6(addr); 452 | } 453 | 454 | public Location getLocation(String str){ 455 | IPAddress addr; 456 | try { 457 | addr = IPAddress.Parse(str); 458 | } 459 | catch (Exception e) { 460 | Console.Write(e.Message); 461 | return null; 462 | } 463 | 464 | return getLocation(bytestoLong(addr.GetAddressBytes())); 465 | } 466 | 467 | [MethodImpl(MethodImplOptions.Synchronized)] 468 | public Location getLocationV6(IPAddress addr){ 469 | int record_pointer; 470 | byte[] record_buf = new byte[FULL_RECORD_LENGTH]; 471 | char[] record_buf2 = new char[FULL_RECORD_LENGTH]; 472 | int record_buf_offset = 0; 473 | Location record = new Location(); 474 | int str_length = 0; 475 | int j, Seek_country; 476 | double latitude = 0, longitude = 0; 477 | 478 | try { 479 | Seek_country = SeekCountryV6(addr); 480 | if (Seek_country == databaseSegments[0]) { 481 | return null; 482 | } 483 | record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]); 484 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1){ 485 | Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH)); 486 | } else { 487 | lock (ioLock ){ 488 | file.Seek(record_pointer,SeekOrigin.Begin); 489 | file.Read(record_buf,0,FULL_RECORD_LENGTH); 490 | } 491 | } 492 | for (int a0 = 0;a0 < FULL_RECORD_LENGTH;a0++){ 493 | record_buf2[a0] = Convert.ToChar(record_buf[a0]); 494 | } 495 | // get country 496 | record.countryCode = countryCode[unsignedByteToInt(record_buf[0])]; 497 | record.countryName = countryName[unsignedByteToInt(record_buf[0])]; 498 | record_buf_offset++; 499 | 500 | // get region 501 | while (record_buf[record_buf_offset + str_length] != '\0') 502 | str_length++; 503 | if (str_length > 0) { 504 | record.region = new String(record_buf2, record_buf_offset, str_length); 505 | } 506 | record_buf_offset += str_length + 1; 507 | str_length = 0; 508 | 509 | // get region_name 510 | record.regionName = RegionName.getRegionName( record.countryCode, record.region ); 511 | 512 | // get city 513 | while (record_buf[record_buf_offset + str_length] != '\0') 514 | str_length++; 515 | if (str_length > 0) { 516 | record.city = new String(record_buf2, record_buf_offset, str_length); 517 | } 518 | record_buf_offset += (str_length + 1); 519 | str_length = 0; 520 | 521 | // get postal code 522 | while (record_buf[record_buf_offset + str_length] != '\0') 523 | str_length++; 524 | if (str_length > 0) { 525 | record.postalCode = new String(record_buf2, record_buf_offset, str_length); 526 | } 527 | record_buf_offset += (str_length + 1); 528 | 529 | // get latitude 530 | for (j = 0; j < 3; j++) 531 | latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 532 | record.latitude = (float) latitude/10000 - 180; 533 | record_buf_offset += 3; 534 | 535 | // get longitude 536 | for (j = 0; j < 3; j++) 537 | longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 538 | record.longitude = (float) longitude/10000 - 180; 539 | 540 | record.metro_code = record.dma_code = 0; 541 | record.area_code = 0; 542 | if (databaseType == DatabaseInfo.CITY_EDITION_REV1 543 | ||databaseType == DatabaseInfo.CITY_EDITION_REV1_V6) { 544 | // get metro_code 545 | int metroarea_combo = 0; 546 | if (record.countryCode == "US"){ 547 | record_buf_offset += 3; 548 | for (j = 0; j < 3; j++) 549 | metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 550 | record.metro_code = record.dma_code = metroarea_combo/1000; 551 | record.area_code = metroarea_combo % 1000; 552 | } 553 | } 554 | } 555 | catch (IOException) { 556 | Console.Write("IO Exception while seting up segments"); 557 | } 558 | return record; 559 | } 560 | [MethodImpl(MethodImplOptions.Synchronized)] 561 | public Location getLocation(long ipnum){ 562 | int record_pointer; 563 | byte[] record_buf = new byte[FULL_RECORD_LENGTH]; 564 | char[] record_buf2 = new char[FULL_RECORD_LENGTH]; 565 | int record_buf_offset = 0; 566 | Location record = new Location(); 567 | int str_length = 0; 568 | int j, Seek_country; 569 | double latitude = 0, longitude = 0; 570 | 571 | try { 572 | Seek_country = SeekCountry(ipnum); 573 | if (Seek_country == databaseSegments[0]) { 574 | return null; 575 | } 576 | record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]); 577 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1){ 578 | Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH)); 579 | } else { 580 | lock ( ioLock ){ 581 | file.Seek(record_pointer,SeekOrigin.Begin); 582 | file.Read(record_buf,0,FULL_RECORD_LENGTH); 583 | } 584 | } 585 | for (int a0 = 0;a0 < FULL_RECORD_LENGTH;a0++){ 586 | record_buf2[a0] = Convert.ToChar(record_buf[a0]); 587 | } 588 | // get country 589 | record.countryCode = countryCode[unsignedByteToInt(record_buf[0])]; 590 | record.countryName = countryName[unsignedByteToInt(record_buf[0])]; 591 | record_buf_offset++; 592 | 593 | // get region 594 | while (record_buf[record_buf_offset + str_length] != '\0') 595 | str_length++; 596 | if (str_length > 0) { 597 | record.region = new String(record_buf2, record_buf_offset, str_length); 598 | } 599 | record_buf_offset += str_length + 1; 600 | str_length = 0; 601 | 602 | // get region_name 603 | record.regionName = RegionName.getRegionName( record.countryCode, record.region ); 604 | 605 | // get city 606 | while (record_buf[record_buf_offset + str_length] != '\0') 607 | str_length++; 608 | if (str_length > 0) { 609 | record.city = new String(record_buf2, record_buf_offset, str_length); 610 | } 611 | record_buf_offset += (str_length + 1); 612 | str_length = 0; 613 | 614 | // get postal code 615 | while (record_buf[record_buf_offset + str_length] != '\0') 616 | str_length++; 617 | if (str_length > 0) { 618 | record.postalCode = new String(record_buf2, record_buf_offset, str_length); 619 | } 620 | record_buf_offset += (str_length + 1); 621 | 622 | // get latitude 623 | for (j = 0; j < 3; j++) 624 | latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 625 | record.latitude = (float) latitude/10000 - 180; 626 | record_buf_offset += 3; 627 | 628 | // get longitude 629 | for (j = 0; j < 3; j++) 630 | longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 631 | record.longitude = (float) longitude/10000 - 180; 632 | 633 | record.metro_code = record.dma_code = 0; 634 | record.area_code = 0; 635 | if (databaseType == DatabaseInfo.CITY_EDITION_REV1) { 636 | // get metro_code 637 | int metroarea_combo = 0; 638 | if (record.countryCode == "US"){ 639 | record_buf_offset += 3; 640 | for (j = 0; j < 3; j++) 641 | metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); 642 | record.metro_code = record.dma_code = metroarea_combo/1000; 643 | record.area_code = metroarea_combo % 1000; 644 | } 645 | } 646 | } 647 | catch (IOException) { 648 | Console.Write("IO Exception while seting up segments"); 649 | } 650 | return record; 651 | } 652 | public String getOrg(IPAddress addr) { 653 | return getOrg(bytestoLong(addr.GetAddressBytes())); 654 | } 655 | 656 | public String getOrgV6(String str){ 657 | IPAddress addr; 658 | try { 659 | addr = IPAddress.Parse(str); 660 | } 661 | //catch (UnknownHostException e) { 662 | catch (Exception e){ 663 | Console.Write(e.Message); 664 | return null; 665 | } 666 | return getOrgV6(addr); 667 | } 668 | 669 | public String getOrg(String str){ 670 | IPAddress addr; 671 | try { 672 | addr = IPAddress.Parse(str); 673 | } 674 | //catch (UnknownHostException e) { 675 | catch (Exception e){ 676 | Console.Write(e.Message); 677 | return null; 678 | } 679 | return getOrg(bytestoLong(addr.GetAddressBytes())); 680 | } 681 | 682 | [MethodImpl(MethodImplOptions.Synchronized)] 683 | public String getOrgV6( IPAddress addr){ 684 | int Seek_org; 685 | int record_pointer; 686 | int str_length = 0; 687 | byte [] buf = new byte[MAX_ORG_RECORD_LENGTH]; 688 | char [] buf2 = new char[MAX_ORG_RECORD_LENGTH]; 689 | String org_buf; 690 | 691 | try { 692 | Seek_org = SeekCountryV6(addr); 693 | if (Seek_org == databaseSegments[0]) { 694 | return null; 695 | } 696 | 697 | record_pointer = Seek_org + (2 * recordLength - 1) * databaseSegments[0]; 698 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 699 | Array.Copy(dbbuffer, record_pointer, buf, 0, Math.Min(dbbuffer.Length - record_pointer, MAX_ORG_RECORD_LENGTH)); 700 | } else { 701 | lock ( ioLock ) { 702 | file.Seek(record_pointer,SeekOrigin.Begin); 703 | file.Read(buf,0,MAX_ORG_RECORD_LENGTH); 704 | } 705 | } 706 | while (buf[str_length] != 0) { 707 | buf2[str_length] = Convert.ToChar(buf[str_length]); 708 | str_length++; 709 | } 710 | buf2[str_length] = '\0'; 711 | org_buf = new String(buf2,0,str_length); 712 | return org_buf; 713 | } 714 | catch (IOException) { 715 | Console.Write("IO Exception"); 716 | return null; 717 | } 718 | } 719 | 720 | [MethodImpl(MethodImplOptions.Synchronized)] 721 | public String getOrg(long ipnum){ 722 | int Seek_org; 723 | int record_pointer; 724 | int str_length = 0; 725 | byte [] buf = new byte[MAX_ORG_RECORD_LENGTH]; 726 | char [] buf2 = new char[MAX_ORG_RECORD_LENGTH]; 727 | String org_buf; 728 | 729 | try { 730 | Seek_org = SeekCountry(ipnum); 731 | if (Seek_org == databaseSegments[0]) { 732 | return null; 733 | } 734 | 735 | record_pointer = Seek_org + (2 * recordLength - 1) * databaseSegments[0]; 736 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 737 | Array.Copy(dbbuffer, record_pointer, buf, 0, Math.Min(dbbuffer.Length - record_pointer, MAX_ORG_RECORD_LENGTH)); 738 | } else { 739 | lock ( ioLock ) { 740 | file.Seek(record_pointer,SeekOrigin.Begin); 741 | file.Read(buf,0,MAX_ORG_RECORD_LENGTH); 742 | } 743 | } 744 | while (buf[str_length] != 0) { 745 | buf2[str_length] = Convert.ToChar(buf[str_length]); 746 | str_length++; 747 | } 748 | buf2[str_length] = '\0'; 749 | org_buf = new String(buf2,0,str_length); 750 | return org_buf; 751 | } 752 | catch (IOException) { 753 | Console.Write("IO Exception"); 754 | return null; 755 | } 756 | } 757 | 758 | [MethodImpl(MethodImplOptions.Synchronized)] 759 | private int SeekCountryV6(IPAddress ipAddress){ 760 | byte [] v6vec = ipAddress.GetAddressBytes(); 761 | byte [] buf = new byte[2 * MAX_RECORD_LENGTH]; 762 | int [] x = new int[2]; 763 | int offset = 0; 764 | for (int depth = 127; depth >= 0; depth--) { 765 | try { 766 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 767 | for (int i = 0;i < (2 * MAX_RECORD_LENGTH);i++) { 768 | buf[i] = dbbuffer[i+(2 * recordLength * offset)]; 769 | } 770 | } else { 771 | lock ( ioLock ) { 772 | file.Seek(2 * recordLength * offset,SeekOrigin.Begin); 773 | file.Read(buf,0,2 * MAX_RECORD_LENGTH); 774 | } 775 | } 776 | } 777 | catch (IOException) { 778 | Console.Write("IO Exception"); 779 | } 780 | for (int i = 0; i<2; i++) { 781 | x[i] = 0; 782 | for (int j = 0; j> 3; 794 | int b_mask = 1 << ( bnum & 7 ^ 7 ); 795 | if ((v6vec[idx] & b_mask) > 0) { 796 | if (x[1] >= databaseSegments[0]) { 797 | return x[1]; 798 | } 799 | offset = x[1]; 800 | } 801 | else { 802 | if (x[0] >= databaseSegments[0]) { 803 | return x[0]; 804 | } 805 | offset = x[0]; 806 | } 807 | } 808 | 809 | // shouldn't reach here 810 | Console.Write("Error Seeking country while Seeking " + ipAddress); 811 | return 0; 812 | 813 | } 814 | 815 | [MethodImpl(MethodImplOptions.Synchronized)] 816 | private int SeekCountry(long ipAddress){ 817 | byte [] buf = new byte[2 * MAX_RECORD_LENGTH]; 818 | int [] x = new int[2]; 819 | int offset = 0; 820 | for (int depth = 31; depth >= 0; depth--) { 821 | try { 822 | if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 823 | for (int i = 0;i < (2 * recordLength);i++) { 824 | buf[i] = dbbuffer[i+(2 * recordLength * offset)]; 825 | } 826 | } else { 827 | lock ( ioLock ){ 828 | file.Seek(2 * recordLength * offset,SeekOrigin.Begin); 829 | file.Read(buf,0,2 * MAX_RECORD_LENGTH); 830 | } 831 | } 832 | } 833 | catch (IOException) { 834 | Console.Write("IO Exception"); 835 | } 836 | for (int i = 0; i<2; i++) { 837 | x[i] = 0; 838 | for (int j = 0; j 0) { 848 | if (x[1] >= databaseSegments[0]) { 849 | return x[1]; 850 | } 851 | offset = x[1]; 852 | } 853 | else { 854 | if (x[0] >= databaseSegments[0]) { 855 | return x[0]; 856 | } 857 | offset = x[0]; 858 | } 859 | } 860 | 861 | // shouldn't reach here 862 | Console.Write("Error Seeking country while Seeking " + ipAddress); 863 | return 0; 864 | 865 | } 866 | private static long swapbytes(long ipAddress){ 867 | return (((ipAddress>>0) & 255) << 24) | (((ipAddress>>8) & 255) << 16) 868 | | (((ipAddress>>16) & 255) << 8) | (((ipAddress>>24) & 255) << 0); 869 | } 870 | private static long bytestoLong(byte [] address){ 871 | long ipnum = 0; 872 | for (int i = 0; i < 4; ++i) { 873 | long y = address[i]; 874 | if (y < 0) { 875 | y+= 256; 876 | } 877 | ipnum += y << ((3-i)*8); 878 | } 879 | return ipnum; 880 | } 881 | private static int unsignedByteToInt(byte b) { 882 | return (int) b & 0xFF; 883 | } 884 | 885 | } 886 | -------------------------------------------------------------------------------- /src/Region.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | public class Region{ 4 | public String countryCode; 5 | public String countryName; 6 | public String region; 7 | public Region(){ 8 | } 9 | public Region(String countryCode,String countryName,String region){ 10 | this.countryCode = countryCode; 11 | this.countryName = countryName; 12 | this.region = region; 13 | } 14 | public String getcountryCode() { 15 | return countryCode; 16 | } 17 | public String getcountryName() { 18 | return countryName; 19 | } 20 | public String getregion() { 21 | return region; 22 | } 23 | } 24 | 25 | --------------------------------------------------------------------------------