Skip to content

Commit a5c2273

Browse files
committed
Added support for district, ASN and AS
1 parent 9cb582c commit a5c2273

File tree

7 files changed

+130
-59
lines changed

7 files changed

+130
-59
lines changed

CHANGELOG.TXT

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
8.0.0 Support Index BIN
1+
8.2.0 Added support for district, ASN and AS.
22

3-
8.1.0 Added supports for address type and category fields. Added exception handling for incorrect BIN database.
3+
8.1.0 Added support for address type and category fields. Added exception handling for incorrect BIN database.
4+
5+
8.0.0 Support Index BIN.

IP2Loc_DBInterface.pas

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
////////////////////////////////////////////////
22
// Unit : IP2Loc_DBInterface.pas //
3-
// Version : 8.1.0 //
4-
// Date : June 2021 //
3+
// Version : 8.2.0 //
4+
// Date : April 2023 //
55
// Translator: IP2Location //
66
// Email : support@ip2location.com //
77
// License : MIT //
@@ -131,7 +131,7 @@ function IP2Location_DB_close(filehandle:integer):integer;
131131
if (DB_access_type = IP2LOCATION_FILE_IO) and (handle <> 0) then
132132
begin
133133
FileSeek(handle,position-1,0);
134-
FileRead(handle,result,1);
134+
FileRead(handle,result,1);
135135
end
136136
else
137137
result:=PByteArray(cache_shm_ptr)^[position-1];
@@ -143,7 +143,7 @@ function IP2Location_read32(handle:integer;position:cardinal):cardinal;
143143
if (DB_access_type = IP2LOCATION_FILE_IO) and (handle <> 0) then
144144
begin
145145
FileSeek(handle,position-1,0);
146-
FileRead(handle,byte1,1);
146+
FileRead(handle,byte1,1);
147147
FileRead(handle,byte2,1);
148148
FileRead(handle,byte3,1);
149149
FileRead(handle,byte4,1);
@@ -155,7 +155,7 @@ function IP2Location_read32(handle:integer;position:cardinal):cardinal;
155155
byte3:=PByteArray(cache_shm_ptr)^[position+1];
156156
byte4:=PByteArray(cache_shm_ptr)^[position+2];
157157
end;
158-
result:= (byte4 shl 24) or (byte3 shl 16) or (byte2 shl 8) or byte1;
158+
result:= (byte4 shl 24) or (byte3 shl 16) or (byte2 shl 8) or byte1;
159159
end;
160160

161161
function IP2Location_readStr(handle:integer;position:cardinal):PChar;

IP2Location.pas

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
////////////////////////////////////////////////
22
// Unit : IP2Location.pas //
3-
// Version : 8.1.0 //
4-
// Date : June 2021 //
3+
// Version : 8.2.0 //
4+
// Date : April 2023 //
55
// Translator: IP2Location //
66
// Email : support@ip2location.com //
77
// License : MIT //
@@ -14,33 +14,36 @@ interface
1414
uses Windows,SysUtils,IP2Loc_DBInterface;
1515

1616
const
17-
API_VERSION = '8.1.0';
17+
API_VERSION = '8.2.0';
1818
MAX_IPV4_RANGE = 4294967295;
1919
MAX_IPV6_RANGE = '340282366920938463463374607431768211455';
2020
IPV4 = 0;
2121
IPV6 = 1;
22-
_COUNTRYSHORT = $000001;
23-
_COUNTRYLONG = $000002;
24-
_REGION = $000004;
25-
_CITY = $000008;
26-
_ISP = $000010;
27-
_LATITUDE = $000020;
28-
_LONGITUDE = $000040;
29-
_DOMAIN = $000080;
30-
_ZIPCODE = $000100;
31-
_TIMEZONE = $000200;
32-
_NETSPEED = $000400;
33-
_IDDCODE = $000800;
34-
_AREACODE = $001000;
35-
_WEATHERSTATIONCODE = $002000;
36-
_WEATHERSTATIONNAME = $004000;
37-
_MCC = $008000;
38-
_MNC = $010000;
39-
_MOBILEBRAND = $020000;
40-
_ELEVATION = $040000;
41-
_USAGETYPE = $080000;
42-
_ADDRESSTYPE = $100000;
43-
_CATEGORY = $200000;
22+
_COUNTRYSHORT = $0000001;
23+
_COUNTRYLONG = $0000002;
24+
_REGION = $0000004;
25+
_CITY = $0000008;
26+
_ISP = $0000010;
27+
_LATITUDE = $0000020;
28+
_LONGITUDE = $0000040;
29+
_DOMAIN = $0000080;
30+
_ZIPCODE = $0000100;
31+
_TIMEZONE = $0000200;
32+
_NETSPEED = $0000400;
33+
_IDDCODE = $0000800;
34+
_AREACODE = $0001000;
35+
_WEATHERSTATIONCODE = $0002000;
36+
_WEATHERSTATIONNAME = $0004000;
37+
_MCC = $0008000;
38+
_MNC = $0010000;
39+
_MOBILEBRAND = $0020000;
40+
_ELEVATION = $0040000;
41+
_USAGETYPE = $0080000;
42+
_ADDRESSTYPE = $0100000;
43+
_CATEGORY = $0200000;
44+
_DISTRICT = $0400000;
45+
_ASN = $0800000;
46+
_AS = $1000000;
4447
_ALL = _COUNTRYSHORT or
4548
_COUNTRYLONG or
4649
_REGION or
@@ -62,7 +65,10 @@ interface
6265
_ELEVATION or
6366
_USAGETYPE or
6467
_ADDRESSTYPE or
65-
_CATEGORY;
68+
_CATEGORY or
69+
_DISTRICT or
70+
_ASN or
71+
_AS;
6672
DEFAULT = $0001;
6773
NO_EMPTY_STRING = $0002;
6874
NO_LEADING = $0004;
@@ -121,6 +127,9 @@ _IP2LocationRecord = record
121127
usagetype:PChar;
122128
addresstype:PChar;
123129
category:PChar;
130+
district:PChar;
131+
asn:PChar;
132+
asname:PChar;
124133
end;
125134
TIP2LocationRecord = _IP2LocationRecord;
126135

@@ -151,6 +160,9 @@ function IP2Location_get_country_elevation(loc:TIP2Location;ip:PChar):TIP2Locat
151160
function IP2Location_get_country_usagetype(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
152161
function IP2Location_get_country_addresstype(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
153162
function IP2Location_get_country_category(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
163+
function IP2Location_get_country_district(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
164+
function IP2Location_get_country_asn(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
165+
function IP2Location_get_country_asname(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
154166
function IP2Location_get_country_all(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
155167

156168
implementation
@@ -159,27 +171,30 @@ implementation
159171

160172
const
161173

162-
COUNTRY_POSITION :array [0..25] of byte =(0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
163-
REGION_POSITION :array [0..25] of byte =(0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
164-
CITY_POSITION :array [0..25] of byte =(0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
165-
LATITUDE_POSITION :array [0..25] of byte =(0,0,0,0,0,5,5,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5);
166-
LONGITUDE_POSITION :array [0..25] of byte =(0,0,0,0,0,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6);
167-
ZIPCODE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,0,7,0,7,7,7,0,7,7);
168-
TIMEZONE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,8,8,7,8,8,8,7,8,0,8,8,8,0,8,8);
169-
ISP_POSITION :array [0..25] of byte =(0,0,3,0,5,0,7,5,7,0,8,0,9,0,9,0,9,0,9,7,9,0,9,7,9,9);
170-
DOMAIN_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,6,8,0,9,0,10,0,10,0,10,0,10,8,10,0,10,8,10,10);
171-
NETSPEED_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,8,11,0,11,8,11,0,11,0,11,0,11,11);
172-
IDDCODE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,12,0,12,0,12,9,12,0,12,12);
173-
AREACODE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 ,13 ,0,13,0,13,10,13,0,13,13);
174-
WEATHERSTATIONCODE_POSITION:array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,14,0,14,0,14,0,14,14);
175-
WEATHERSTATIONNAME_POSITION:array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,15,0,15,0,15,0,15,15);
176-
MCC_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,16,0,16,9,16,16);
177-
MNC_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,17,0,17,10,17,17);
178-
MOBILEBRAND_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,18,0,18,11,18,18);
179-
ELEVATION_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,19,0,19,19);
180-
USAGETYPE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,20,20);
181-
ADDRESSTYPE_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21);
182-
CATEGORY_POSITION :array [0..25] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22);
174+
COUNTRY_POSITION :array [0..26] of byte =(0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
175+
REGION_POSITION :array [0..26] of byte =(0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
176+
CITY_POSITION :array [0..26] of byte =(0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
177+
LATITUDE_POSITION :array [0..26] of byte =(0,0,0,0,0,5,5,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5);
178+
LONGITUDE_POSITION :array [0..26] of byte =(0,0,0,0,0,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6);
179+
ZIPCODE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,0,7,0,7,7,7,0,7,7,7);
180+
TIMEZONE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,8,8,7,8,8,8,7,8,0,8,8,8,0,8,8,8);
181+
ISP_POSITION :array [0..26] of byte =(0,0,3,0,5,0,7,5,7,0,8,0,9,0,9,0,9,0,9,7,9,0,9,7,9,9,9);
182+
DOMAIN_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,6,8,0,9,0,10,0,10,0,10,0,10,8,10,0,10,8,10,10,10);
183+
NETSPEED_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,8,11,0,11,8,11,0,11,0,11,0,11,11,11);
184+
IDDCODE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,12,0,12,0,12,9,12,0,12,12,12);
185+
AREACODE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 ,13 ,0,13,0,13,10,13,0,13,13,13);
186+
WEATHERSTATIONCODE_POSITION:array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,14,0,14,0,14,0,14,14,14);
187+
WEATHERSTATIONNAME_POSITION:array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,15,0,15,0,15,0,15,15,15);
188+
MCC_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,16,0,16,9,16,16,16);
189+
MNC_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,17,0,17,10,17,17,17);
190+
MOBILEBRAND_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,18,0,18,11,18,18,18);
191+
ELEVATION_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,19,0,19,19,19);
192+
USAGETYPE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,20,20,20);
193+
ADDRESSTYPE_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,21);
194+
CATEGORY_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,22);
195+
DISTRICT_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23);
196+
ASN_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24);
197+
AS_POSITION :array [0..26] of byte =(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25);
183198

184199
AF_INET6 = 23;
185200

@@ -232,8 +247,8 @@ function IP2Location_initialize(var loc:TIP2Location):integer;
232247
loc.ipv6databaseaddr := IP2Location_read32(loc.filehandle, 18);
233248
loc.ipv4indexbaseaddr := IP2Location_read32(loc.filehandle, 22);
234249
loc.ipv6indexbaseaddr := IP2Location_read32(loc.filehandle, 26);
235-
loc.productcode := IP2Location_read32(loc.filehandle, 30);
236-
loc.licensecode := IP2Location_read32(loc.filehandle, 31);
250+
loc.productcode := IP2Location_read8(loc.filehandle, 30);
251+
loc.licensecode := IP2Location_read8(loc.filehandle, 31);
237252
loc.databasesize := IP2Location_read32(loc.filehandle, 32);
238253
result:=0;
239254
end;
@@ -375,6 +390,9 @@ function bad_record(const aMessage:PChar):TIP2LocationRecord;
375390
result.usagetype:=aMessage;
376391
result.addresstype:=aMessage;
377392
result.category:=aMessage;
393+
result.district:=aMessage;
394+
result.asn:=aMessage;
395+
result.asname:=aMessage;
378396
end;
379397

380398
function IP2Location_get_record(loc:TIP2Location;ipstring:PChar;mode:Cardinal):TIP2LocationRecord;
@@ -641,6 +659,24 @@ function read_record(loc:TIP2Location;rowaddr,mode:cardinal):TIP2LocationRecord;
641659
result.category := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (CATEGORY_POSITION[dbtype]-1)))
642660
else
643661
result.category := NOT_SUPPORTED;
662+
663+
664+
if ((mode and _DISTRICT>0) and (DISTRICT_POSITION[dbtype] <> 0)) then
665+
result.district := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (DISTRICT_POSITION[dbtype]-1)))
666+
else
667+
result.district := NOT_SUPPORTED;
668+
669+
670+
if ((mode and _ASN>0) and (ASN_POSITION[dbtype] <> 0)) then
671+
result.asn := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (ASN_POSITION[dbtype]-1)))
672+
else
673+
result.asn := NOT_SUPPORTED;
674+
675+
676+
if ((mode and _AS>0) and (AS_POSITION[dbtype] <> 0)) then
677+
result.asname := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (AS_POSITION[dbtype]-1)))
678+
else
679+
result.asname := NOT_SUPPORTED;
644680
end;
645681

646682
// Description: Open the IP2Location database file
@@ -834,6 +870,24 @@ function IP2Location_get_country_category(loc:TIP2Location;ip:PChar):TIP2Locatio
834870
result:=IP2Location_get_record(loc,ip,_CATEGORY);
835871
end;
836872

873+
// Description: Get district
874+
function IP2Location_get_country_district(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
875+
begin
876+
result:=IP2Location_get_record(loc,ip,_DISTRICT);
877+
end;
878+
879+
// Description: Get asn
880+
function IP2Location_get_country_asn(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
881+
begin
882+
result:=IP2Location_get_record(loc,ip,_ASN);
883+
end;
884+
885+
// Description: Get as
886+
function IP2Location_get_country_asname(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
887+
begin
888+
result:=IP2Location_get_record(loc,ip,_AS);
889+
end;
890+
837891
// Description: Get all records
838892
function IP2Location_get_country_all(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
839893
begin

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 IP2Location.com
3+
Copyright (c) 2023 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IP2Location Pascal Library
22

3-
This is a IP2Location Pascal Library that enables the user to find the country, region or state, city, latitude and longitude, US ZIP code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, IP address type and IAB advertising category from IP address using IP2Location database. The library reads the geo location information from **IP2Location BIN data** file.
3+
This is a IP2Location Pascal Library that enables the user to find the country, region or state, district, city, latitude and longitude, ZIP/Postal code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, address type, IAB category and ASN from IP address using IP2Location database. The library reads the geo location information from **IP2Location BIN data** file.
44

55
Supported IPv4 and IPv6 address.
66

@@ -21,5 +21,5 @@ For more details, please visit:
2121
* Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
2222

2323
# Support
24-
Email: support@ip2location.com.
24+
Email: support@ip2location.com
2525
URL: [https://www.ip2location.com](https://www.ip2location.com)

Unit1.dfm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ object Form1: TForm1
201201
item
202202
Caption = 'Category'
203203
Width = 100
204+
end
205+
item
206+
Caption = 'District'
207+
Width = 100
208+
end
209+
item
210+
Caption = 'ASN'
211+
Width = 100
212+
end
213+
item
214+
Caption = 'AS'
215+
Width = 100
204216
end>
205217
GridLines = True
206218
RowSelect = True

Unit1.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ procedure TForm1.edt2KeyPress(Sender: TObject; var Key: Char);
153153
SubItems.Add(usagetype);
154154
SubItems.Add(addresstype);
155155
SubItems.Add(category);
156+
SubItems.Add(district);
157+
SubItems.Add(asn);
158+
SubItems.Add(asname);
156159
end;
157160
except
158161

0 commit comments

Comments
 (0)