Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add IPv6 support for asn, country, city and IPv4 support for asn data…

…bases.
  • Loading branch information...
commit eecabd12099f5361dce68b352c631b3d19eb6635 1 parent 76cf9dd
@borisz borisz authored
View
11 DatabaseInfo.cs
@@ -34,6 +34,17 @@ public class DatabaseInfo {
public static int PROXY_EDITION = 8;
public static int ASNUM_EDITION = 9;
public static int NETSPEED_EDITION = 10;
+ public static int DOMAIN_EDITION = 11;
+ public static int COUNTRY_EDITION_V6 = 12;
+ public static int ASNUM_EDITION_V6 = 21;
+ public static int ISP_EDITION_V6 = 22;
+ public static int ORG_EDITION_V6 = 23;
+ public static int DOMAIN_EDITION_V6 = 24;
+ public static int CITY_EDITION_REV1_V6 = 30;
+ public static int CITY_EDITION_REV0_V6 = 31;
+ public static int NETSPEED_EDITION_REV1 = 32;
+ public static int NETSPEED_EDITION_REV1_V6 = 33;
+
//private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
View
273 LookupService.cs
@@ -175,13 +175,28 @@ public class LookupService{
} else if (databaseType == DatabaseInfo.CITY_EDITION_REV0 ||
databaseType == DatabaseInfo.CITY_EDITION_REV1 ||
databaseType == DatabaseInfo.ORG_EDITION ||
+ databaseType == DatabaseInfo.ORG_EDITION_V6 ||
databaseType == DatabaseInfo.ISP_EDITION ||
- databaseType == DatabaseInfo.ASNUM_EDITION)
+ databaseType == DatabaseInfo.ISP_EDITION_V6 ||
+ databaseType == DatabaseInfo.ASNUM_EDITION ||
+ databaseType == DatabaseInfo.ASNUM_EDITION_V6 ||
+ databaseType == DatabaseInfo.NETSPEED_EDITION_REV1 ||
+ databaseType == DatabaseInfo.NETSPEED_EDITION_REV1_V6 ||
+ databaseType == DatabaseInfo.CITY_EDITION_REV0_V6 ||
+ databaseType == DatabaseInfo.CITY_EDITION_REV1_V6
+ )
{
databaseSegments = new int[1];
databaseSegments[0] = 0;
if (databaseType == DatabaseInfo.CITY_EDITION_REV0 ||
- databaseType == DatabaseInfo.CITY_EDITION_REV1) {
+ databaseType == DatabaseInfo.CITY_EDITION_REV1 ||
+ databaseType == DatabaseInfo.ASNUM_EDITION_V6 ||
+ databaseType == DatabaseInfo.NETSPEED_EDITION_REV1 ||
+ databaseType == DatabaseInfo.NETSPEED_EDITION_REV1_V6 ||
+ databaseType == DatabaseInfo.CITY_EDITION_REV0_V6 ||
+ databaseType == DatabaseInfo.CITY_EDITION_REV1_V6 ||
+ databaseType == DatabaseInfo.ASNUM_EDITION
+ ) {
recordLength = STANDARD_RECORD_LENGTH;
}
else {
@@ -200,8 +215,9 @@ public class LookupService{
//file.Seek(file.position-4,SeekOrigin.Begin);
}
}
- if ((databaseType == DatabaseInfo.COUNTRY_EDITION) |
- (databaseType == DatabaseInfo.PROXY_EDITION) |
+ if ((databaseType == DatabaseInfo.COUNTRY_EDITION) ||
+ (databaseType == DatabaseInfo.COUNTRY_EDITION_V6) ||
+ (databaseType == DatabaseInfo.PROXY_EDITION) ||
(databaseType == DatabaseInfo.NETSPEED_EDITION)) {
databaseSegments = new int[1];
databaseSegments[0] = COUNTRY_BEGIN;
@@ -224,7 +240,19 @@ public class LookupService{
public Country getCountry(IPAddress ipAddress) {
return getCountry(bytestoLong(ipAddress.GetAddressBytes()));
}
- public Country getCountry(String ipAddress){
+ public Country getCountryV6(String ipAddress){
+ IPAddress addr;
+ try {
+ addr = IPAddress.Parse(ipAddress);
+ }
+ //catch (UnknownHostException e) {
+ catch (Exception e) {
+ Console.Write(e.Message);
+ return UNKNOWN_COUNTRY;
+ }
+ return getCountryV6(addr);
+ }
+ public Country getCountry(String ipAddress){
IPAddress addr;
try {
addr = IPAddress.Parse(ipAddress);
@@ -237,6 +265,32 @@ public class LookupService{
// return getCountry(bytestoLong(addr.GetAddressBytes()));
return getCountry(bytestoLong(addr.GetAddressBytes()));
}
+ public Country getCountryV6(IPAddress ipAddress){
+ if (file == null) {
+ //throw new IllegalStateException("Database has been closed.");
+ throw new Exception("Database has been closed.");
+ }
+ if ((databaseType == DatabaseInfo.CITY_EDITION_REV1) |
+ (databaseType == DatabaseInfo.CITY_EDITION_REV0)) {
+ Location l = getLocation(ipAddress);
+ if (l == null) {
+ return UNKNOWN_COUNTRY;
+ }
+ else {
+ return new Country(l.countryCode, l.countryName);
+ }
+ }
+ else {
+ int ret = SeekCountryV6(ipAddress) - COUNTRY_BEGIN;
+ if (ret == 0) {
+ return UNKNOWN_COUNTRY;
+ }
+ else {
+ return new Country(countryCode[ret], countryName[ret]);
+ }
+ }
+ }
+
public Country getCountry(long ipAddress){
if (file == null) {
//throw new IllegalStateException("Database has been closed.");
@@ -400,6 +454,19 @@ public class LookupService{
public Location getLocation(IPAddress addr){
return getLocation(bytestoLong(addr.GetAddressBytes()));
}
+ public Location getLocationV6(String str){
+ IPAddress addr;
+ try {
+ addr = IPAddress.Parse(str);
+ }
+ catch (Exception e) {
+ Console.Write(e.Message);
+ return null;
+ }
+
+ return getLocationV6(addr);
+ }
+
public Location getLocation(String str){
IPAddress addr;
try {
@@ -414,6 +481,97 @@ public class LookupService{
}
[MethodImpl(MethodImplOptions.Synchronized)]
+ public Location getLocationV6(IPAddress addr){
+ int record_pointer;
+ byte[] record_buf = new byte[FULL_RECORD_LENGTH];
+ char[] record_buf2 = new char[FULL_RECORD_LENGTH];
+ int record_buf_offset = 0;
+ Location record = new Location();
+ int str_length = 0;
+ int j, Seek_country;
+ double latitude = 0, longitude = 0;
+
+ try {
+ Seek_country = SeekCountryV6(addr);
+ if (Seek_country == databaseSegments[0]) {
+ return null;
+ }
+ record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]);
+ if ((dboptions & GEOIP_MEMORY_CACHE) == 1){
+ Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH));
+ } else {
+ file.Seek(record_pointer,SeekOrigin.Begin);
+ file.Read(record_buf,0,FULL_RECORD_LENGTH);
+ }
+ for (int a0 = 0;a0 < FULL_RECORD_LENGTH;a0++){
+ record_buf2[a0] = Convert.ToChar(record_buf[a0]);
+ }
+ // get country
+ record.countryCode = countryCode[unsignedByteToInt(record_buf[0])];
+ record.countryName = countryName[unsignedByteToInt(record_buf[0])];
+ record_buf_offset++;
+
+ // get region
+ while (record_buf[record_buf_offset + str_length] != '\0')
+ str_length++;
+ if (str_length > 0) {
+ record.region = new String(record_buf2, record_buf_offset, str_length);
+ }
+ record_buf_offset += str_length + 1;
+ str_length = 0;
+
+ // get region_name
+ record.regionName = RegionName.getRegionName( record.countryCode, record.region );
+
+ // get city
+ while (record_buf[record_buf_offset + str_length] != '\0')
+ str_length++;
+ if (str_length > 0) {
+ record.city = new String(record_buf2, record_buf_offset, str_length);
+ }
+ record_buf_offset += (str_length + 1);
+ str_length = 0;
+
+ // get postal code
+ while (record_buf[record_buf_offset + str_length] != '\0')
+ str_length++;
+ if (str_length > 0) {
+ record.postalCode = new String(record_buf2, record_buf_offset, str_length);
+ }
+ record_buf_offset += (str_length + 1);
+
+ // get latitude
+ for (j = 0; j < 3; j++)
+ latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
+ record.latitude = (float) latitude/10000 - 180;
+ record_buf_offset += 3;
+
+ // get longitude
+ for (j = 0; j < 3; j++)
+ longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
+ record.longitude = (float) longitude/10000 - 180;
+
+ record.metro_code = record.dma_code = 0;
+ record.area_code = 0;
+ if (databaseType == DatabaseInfo.CITY_EDITION_REV1
+ ||databaseType == DatabaseInfo.CITY_EDITION_REV1_V6) {
+ // get metro_code
+ int metroarea_combo = 0;
+ if (record.countryCode == "US"){
+ record_buf_offset += 3;
+ for (j = 0; j < 3; j++)
+ metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
+ record.metro_code = record.dma_code = metroarea_combo/1000;
+ record.area_code = metroarea_combo % 1000;
+ }
+ }
+ }
+ catch (IOException) {
+ Console.Write("IO Exception while seting up segments");
+ }
+ return record;
+ }
+ [MethodImpl(MethodImplOptions.Synchronized)]
public Location getLocation(long ipnum){
int record_pointer;
byte[] record_buf = new byte[FULL_RECORD_LENGTH];
@@ -506,6 +664,20 @@ public class LookupService{
public String getOrg(IPAddress addr) {
return getOrg(bytestoLong(addr.GetAddressBytes()));
}
+
+ public String getOrgV6(String str){
+ IPAddress addr;
+ try {
+ addr = IPAddress.Parse(str);
+ }
+ //catch (UnknownHostException e) {
+ catch (Exception e){
+ Console.Write(e.Message);
+ return null;
+ }
+ return getOrgV6(addr);
+ }
+
public String getOrg(String str){
IPAddress addr;
try {
@@ -520,6 +692,42 @@ public class LookupService{
}
[MethodImpl(MethodImplOptions.Synchronized)]
+ public String getOrgV6( IPAddress addr){
+ int Seek_org;
+ int record_pointer;
+ int str_length = 0;
+ byte [] buf = new byte[MAX_ORG_RECORD_LENGTH];
+ char [] buf2 = new char[MAX_ORG_RECORD_LENGTH];
+ String org_buf;
+
+ try {
+ Seek_org = SeekCountryV6(addr);
+ if (Seek_org == databaseSegments[0]) {
+ return null;
+ }
+
+ record_pointer = Seek_org + (2 * recordLength - 1) * databaseSegments[0];
+ if ((dboptions & GEOIP_MEMORY_CACHE) == 1) {
+ Array.Copy(dbbuffer, record_pointer, buf, 0, Math.Min(dbbuffer.Length - record_pointer, MAX_ORG_RECORD_LENGTH));
+ } else {
+ file.Seek(record_pointer,SeekOrigin.Begin);
+ file.Read(buf,0,MAX_ORG_RECORD_LENGTH);
+ }
+ while (buf[str_length] != 0) {
+ buf2[str_length] = Convert.ToChar(buf[str_length]);
+ str_length++;
+ }
+ buf2[str_length] = '\0';
+ org_buf = new String(buf2,0,str_length);
+ return org_buf;
+ }
+ catch (IOException) {
+ Console.Write("IO Exception");
+ return null;
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
public String getOrg(long ipnum){
int Seek_org;
int record_pointer;
@@ -556,6 +764,61 @@ public class LookupService{
}
[MethodImpl(MethodImplOptions.Synchronized)]
+ private int SeekCountryV6(IPAddress ipAddress){
+ byte [] v6vec = ipAddress.GetAddressBytes();
+ byte [] buf = new byte[2 * MAX_RECORD_LENGTH];
+ int [] x = new int[2];
+ int offset = 0;
+ for (int depth = 127; depth >= 0; depth--) {
+ try {
+ if ((dboptions & GEOIP_MEMORY_CACHE) == 1) {
+ for (int i = 0;i < (2 * MAX_RECORD_LENGTH);i++) {
+ buf[i] = dbbuffer[i+(2 * recordLength * offset)];
+ }
+ } else {
+ file.Seek(2 * recordLength * offset,SeekOrigin.Begin);
+ file.Read(buf,0,2 * MAX_RECORD_LENGTH);
+ }
+ }
+ catch (IOException) {
+ Console.Write("IO Exception");
+ }
+ for (int i = 0; i<2; i++) {
+ x[i] = 0;
+ for (int j = 0; j<recordLength; j++) {
+ int y = buf[(i*recordLength)+j];
+ if (y < 0) {
+ y+= 256;
+ }
+ x[i] += (y << (j * 8));
+ }
+ }
+
+
+ int bnum = 127 - depth;
+ int idx = bnum >> 3;
+ int b_mask = 1 << ( bnum & 7 ^ 7 );
+ if ((v6vec[idx] & b_mask) > 0) {
+ if (x[1] >= databaseSegments[0]) {
+ return x[1];
+ }
+ offset = x[1];
+ }
+ else {
+ if (x[0] >= databaseSegments[0]) {
+ return x[0];
+ }
+ offset = x[0];
+ }
+ }
+
+ // shouldn't reach here
+ Console.Write("Error Seeking country while Seeking " + ipAddress);
+ return 0;
+
+ }
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
private int SeekCountry(long ipAddress){
byte [] buf = new byte[2 * MAX_RECORD_LENGTH];
int [] x = new int[2];
View
27 README
@@ -1,18 +1,25 @@
-GeoIP C# API 1.12
+GeoIP C# API 1.14
================
-As of version 1.12 this API is thread safe.
+As of version 1.14 this API is thread safe.
Instructions for Mono - see http://www.mono-project.com/
Should also work in Windows C# environment.
To compile the examples
+
For country example
mono /usr/local/bin/mcs.exe countryExample.cs Country.cs DatabaseInfo.cs Location.cs LookupService.cs Region.cs RegionName.cs
For city example
mono /usr/local/bin/mcs.exe cityExample.cs Country.cs DatabaseInfo.cs Location.cs LookupService.cs Region.cs RegionName.cs
+For countryV6 example
+mono /usr/local/bin/mcs.exe countryV6Example.cs Country.cs DatabaseInfo.cs Location.cs LookupService.cs Region.cs RegionName.cs
+
+For cityV6 example
+mono /usr/local/bin/mcs.exe cityV6Example.cs Country.cs DatabaseInfo.cs Location.cs LookupService.cs Region.cs RegionName.cs
+
for region example
mono /usr/local/bin/mcs.exe regionExample.cs Country.cs DatabaseInfo.cs Location.cs LookupService.cs Region.cs RegionName.cs
@@ -22,11 +29,23 @@ for country example
mono countryExample.exe
for city example
-mono cityExample.exe
+mono cityExample.exe 24.24.24.24
for org example
mono orgExample.exe
+for asn example
+mono asnExample.exe
+
+for countryV6 example
+mono countryV6Example.exe
+
+for city example
+mono cityV6Example.exe ::24.24.24.24
+
+for asnV6 example
+mono asnV6Example.exe
+
MEMORY CACHING
There are two options available:
@@ -36,5 +55,5 @@ GEOIP_STANDARD - read database from filesystem, uses least memory.
GEOIP_MEMORY_CACHE - load database into memory, faster performance
but uses more memory
-Copyright 2009 MaxMind Inc
+Copyright 2011 MaxMind Inc
Licensed under the LGPL
View
12 asnExample.cs
@@ -0,0 +1,12 @@
+//an example of how to lookup the isp or org of a ip address
+using System;
+using System.IO;
+class Appa0{
+ public static void Main(String[] args){
+ //open the database
+ LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPASNum.dat", LookupService.GEOIP_STANDARD);
+ //get org of the ip address
+ String orgorisp = ls.getOrg("24.24.24.24");
+ Console.Write("asn: " + orgorisp + "\n");
+ }
+}
View
12 asnV6Example.cs
@@ -0,0 +1,12 @@
+//an example of how to lookup the isp or org of a ip address
+using System;
+using System.IO;
+class Appa0{
+ public static void Main(String[] args){
+ //open the database
+ LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPASNumv6.dat", LookupService.GEOIP_STANDARD);
+ //get org of the ip address
+ String orgorisp = ls.getOrgV6("2001:4860:0:1001::68");
+ Console.Write("asn: " + orgorisp + "\n");
+ }
+}
View
34 cityV6Example.cs
@@ -0,0 +1,34 @@
+//an example of how to lookup the city of a ip address
+using System;
+using System.IO;
+class Appa0{
+ public static void Main(String[] args){
+ //open the database
+ try {
+ LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoLiteCityV6.dat", LookupService.GEOIP_STANDARD);
+ //get city location of the ip address
+ if (args.Length > 0) {
+ Location l = ls.getLocationV6(args[0]);
+ if (l != null) {
+ Console.Write("country code " + l.countryCode + "\n");
+ Console.Write("country name " + l.countryName + "\n");
+ Console.Write("region " + l.region + "\n");
+ Console.Write("city " + l.city + "\n");
+ Console.Write("postal code " + l.postalCode + "\n");
+ Console.Write("latitude " + l.latitude + "\n");
+ Console.Write("longitude " + l.longitude + "\n");
+ Console.Write("metro code " + l.metro_code + "\n");
+ Console.Write("area code " + l.area_code + "\n");
+ Console.Write("region name " + l.regionName + "\n");
+ } else {
+ Console.Write("IP Address Not Found\n");
+ }
+ } else {
+ Console.Write("Usage: cityExample IPAddress\n");
+ }
+ } catch (System.Exception e) {
+ Console.Write("Error" + e.Message + "\n");
+ }
+ }
+}
+
View
18 countryV6Example.cs
@@ -0,0 +1,18 @@
+// an example of how to lookup the country of a ip address
+
+// This only works with the GeoIP Country database
+// See regionExample.cs, cityExample.cs, and orgExample.cs
+// for the Region database, city database, and ISP/Organization databases
+
+using System;
+using System.IO;
+class Appa0{
+ public static void Main(String[] args){
+ //open the database
+ LookupService ls = new LookupService("/usr/local/share/GeoIP/GeoIPv6.dat", LookupService.GEOIP_MEMORY_CACHE);
+ //get country of the ip address
+ Country c = ls.getCountryV6("2001:4860:0:1001::68");
+ Console.Write(" code: " + c.getCode()+"\n");
+ Console.Write(" name: " + c.getName()+"\n");
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.