-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIP2Location.pas
708 lines (603 loc) · 23.3 KB
/
IP2Location.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
////////////////////////////////////////////////
// Unit : IP2Location.pas //
// Version : 1.1 beta //
// Date : August 2014 //
// Translator: Benbaha Abdelkrim AKA DeadC0der //
// Email : DeadC0der7@gmail.com //
// License : Included :) //
////////////////////////////////////////////////
unit IP2Location;
interface
uses Windows,SysUtils,IP2Loc_DBInterface;
const
API_VERSION = 4.00;
MAX_IPV4_RANGE = 4294967295;
MAX_IPV6_RANGE = '340282366920938463463374607431768211455';
IPV4 = 0;
IPV6 = 1;
_COUNTRYSHORT = $00001;
_COUNTRYLONG = $00002;
_REGION = $00004;
_CITY = $00008;
_ISP = $00010;
_LATITUDE = $00020;
_LONGITUDE = $00040;
_DOMAIN = $00080;
_ZIPCODE = $00100;
_TIMEZONE = $00200;
_NETSPEED = $00400;
_IDDCODE = $00800;
_AREACODE = $01000;
_WEATHERSTATIONCODE = $02000;
_WEATHERSTATIONNAME = $04000;
_MCC = $08000;
_MNC = $10000;
_MOBILEBRAND = $20000;
_ELEVATION = $40000;
_USAGETYPE = $80000;
_ALL = _COUNTRYSHORT or
_COUNTRYLONG or
_REGION or
_CITY or
_ISP or
_LATITUDE or
_LONGITUDE or
_DOMAIN or
_ZIPCODE or
_TIMEZONE or
_NETSPEED or
_IDDCODE or
_AREACODE or
_WEATHERSTATIONCODE or
_WEATHERSTATIONNAME or
_MCC or
_MNC or
_MOBILEBRAND or
_ELEVATION or
_USAGETYPE;
DEFAULT = $0001;
NO_EMPTY_STRING = $0002;
NO_LEADING = $0004;
NO_TRAILING = $0008;
INVALID_IPV6_ADDRESS = 'INVALID IPV6 ADDRESS';
INVALID_IPV4_ADDRESS = 'INVALID IPV4 ADDRESS';
NOT_SUPPORTED = 'This parameter is unavailable for selected data file. Please upgrade the data file.';
type
PIP2Location = ^TIP2LOCATION;
_IP2Location = record
filehandle:integer;
databasetype:byte;
databasecolumn:byte;
databaseday:byte;
databasemonth:byte;
databaseyear:byte;
databasecount:DWORD;
databaseaddr:DWORD;
ipversion:DWORD;
ipv4databasecount:DWORD;
ipv4databaseaddr:DWORD;
ipv6databasecount:DWORD;
ipv6databaseaddr:DWORD;
end;
TIP2Location = _IP2Location;
PIP2LocationRecord = ^TIP2LocationRecord;
_IP2LocationRecord = record
country_short:PChar;
country_long:PChar;
region:PChar;
city:PChar;
isp:PChar;
latitude:Single;
longitude:Single;
domain:PChar;
zipcode:PChar;
timezone:PChar;
netspeed:PChar;
iddcode:PChar;
areacode:PChar;
weatherstationcode:PChar;
weatherstationname:PChar;
mcc:PChar;
mnc:PChar;
mobilebrand:PChar;
elevation:Single;
usagetype:PChar;
end;
TIP2LocationRecord = _IP2LocationRecord;
function IP2Location_open(db:PChar):TIP2Location;
function IP2Location_open_mem(loc:TIP2Location;mtype:TIP2Location_mem_type):integer;
function IP2Location_close(loc:TIP2Location):DWORD;
function IP2Location_get_country_short(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_long(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_region(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_city(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_isp(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_latitude(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_longitude(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_domain(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_zipcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_timezone(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_netspeed(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_iddcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_areacode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_weatherstationcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_weatherstationname(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_mcc(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_mnc(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_mobilebrand(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_elevation(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_usagetype(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
function IP2Location_get_country_all(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
implementation
uses WinSock,Classes;
const
COUNTRY_POSITION :array [0..24] 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);
REGION_POSITION :array [0..24] 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);
CITY_POSITION :array [0..24] 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);
ISP_POSITION :array [0..24] 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);
LATITUDE_POSITION :array [0..24] 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);
LONGITUDE_POSITION :array [0..24] 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);
DOMAIN_POSITION :array [0..24] 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);
ZIPCODE_POSITION :array [0..24] 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);
TIMEZONE_POSITION :array [0..24] 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);
NETSPEED_POSITION :array [0..24] 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);
IDDCODE_POSITION :array [0..24] 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);
AREACODE_POSITION :array [0..24] 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);
WEATHERSTATIONCODE_POSITION:array [0..24] 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);
WEATHERSTATIONNAME_POSITION:array [0..24] 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);
MCC_POSITION :array [0..24] 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);
MNC_POSITION :array [0..24] 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);
MOBILEBRAND_POSITION :array [0..24] 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);
ELEVATION_POSITION :array [0..24] 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);
USAGETYPE_POSITION :array [0..24] 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);
AF_INET6 = 23;
type
ipv_t = record
ipversion:Integer;
ipv4:cardinal;
ipv6:Tin6_addr_local;
end;
var
openMemFlag:Boolean = False;
function IP2Location_get_record(loc:TIP2Location;ipstring:PChar;mode:Cardinal):TIP2LocationRecord;forward;
function IP2Location_get_ipv6_record (loc:TIP2Location; ipstring:PChar;mode:Cardinal):TIP2LocationRecord;forward;
function read_record(loc:TIP2Location;rowaddr,mode:cardinal):TIP2LocationRecord;forward;
function IP2Location_initialize(var loc:TIP2Location):integer;forward;
function ipv6_compare(addr1,addr2:Tin6_addr_local):integer;forward;
procedure IP2Location_delete_shm;forward;
function IP2Location_ip_is_ipv4(ipaddr:PChar):integer;forward;
function IP2Location_ip_is_ipv6(ipaddr:PChar):integer;forward;
function IP2Location_ip2no(ipstring:PChar):Cardinal;forward;
function parse_addr(addr:PChar):ipv_t;forward;
function bad_record(const aMessage:PChar):TIP2LocationRecord;forward;
{$IFDEF UNICODE}
function InetPton(Family:integer;pszAddrString:pchar;var pAddrBuf ):integer;stdcall;external 'ws2_32.dll' name 'InetPtonW';
{$ELSE}
function InetPton(Family:integer;pszAddrString:pchar;var pAddrBuf ):integer;stdcall;external 'ws2_32.dll' name 'inet_pton';
{$ENDIF}
// Descrption:initializes the record with the appropriate value from the database.
function IP2Location_initialize(var loc:TIP2Location):integer;
begin
loc.databasetype := IP2Location_read8(loc.filehandle,1);
loc.databasecolumn := IP2Location_read8(loc.filehandle,2);
loc.databaseyear := IP2Location_read8(loc.filehandle,3);
loc.databasemonth := IP2Location_read8(loc.filehandle,4);
loc.databaseday := IP2Location_read8(loc.filehandle,5);
loc.databasecount := IP2Location_read32(loc.filehandle,6);
loc.databaseaddr := IP2Location_read32(loc.filehandle,10);
loc.ipversion := IP2Location_read32(loc.filehandle,14);
loc.ipv4databasecount := IP2Location_read32(loc.filehandle, 6);
loc.ipv4databaseaddr := IP2Location_read32(loc.filehandle, 10);
loc.ipv6databasecount := IP2Location_read32(loc.filehandle, 14);
loc.ipv6databaseaddr := IP2Location_read32(loc.filehandle, 18);
result:=0;
end;
// Description: Delete IP2Location shared memory if it is present.
procedure IP2Location_delete_shm;
begin
IP2Location_DB_del_shm();
end;
function ipv6_compare(addr1,addr2:Tin6_addr_local):integer;
var i:integer;
begin
result:=0;
i:=0;
while (i<16) and (result=0) do
begin
if addr1.addr8[i] > addr2.addr8[i] then result:=1
else
if addr1.addr8[i] < addr2.addr8[i] then result:=-1;
inc(i);
end;
end;
function IP2Location_ip_is_ipv4(ipaddr:PChar):integer;
var IA:IN_ADDR;
begin
result:=InetPton(AF_INET, ipaddr,IA);
end;
function IP2Location_ip_is_ipv6(ipaddr:PChar):integer;
var IA:Tin6_addr_local;
begin
result:=InetPton(AF_INET6, ipaddr,IA);
end;
function IP2Location_ip2no(ipstring:PChar):Cardinal;
var P_Out:PAnsiChar;
aResult:integer;
begin
Result:=0;
{$IFDEF UNICODE}
P_Out:=AllocMem(StrLen(ipstring)+1);
WideCharToMultiByte(CP_UTF8,0,ipstring,StrLen(ipstring),P_Out,StrLen(ipstring),nil,nil);
aResult:=inet_addr(P_Out);
{$ELSE}
aResult:=inet_addr(ipstring);
{$ENDIF}
if (ipstring <>nil) then
Result:=(LoByte(LoWord(aResult))shl 24) or (HiByte(LoWord(aResult))shl 16) or (LoByte(HiWord(aResult))shl 8) or HiByte(HiWord(aResult)) ;
end;
function parse_addr(addr:PChar):ipv_t;
begin
if IP2Location_ip_is_ipv6(addr)=1 then
if (strlen(addr)>7) and (addr[0]=':') and (addr[1] = ':') and
((addr[2]='F') or (addr[2]='f')) and
((addr[3]='F') or (addr[3]='f')) and
((addr[4]='F') or (addr[4]='f')) and
((addr[5]='F') or (addr[5]='f')) and
(addr[6]=':') and (IP2Location_ip_is_ipv4(addr+7)=1) then
begin
result.ipversion := 4;
result.ipv4 :=IP2Location_ip2no(addr + 7);
end
else
begin
result.ipversion := 6;
InetPton(AF_INET6,addr,result.ipv6);
end
else if IP2Location_ip_is_ipv4(addr)=1 then
begin
result.ipversion := 4;
result.ipv4 := IP2Location_ip2no(addr);
end
else
result.ipversion:=-1;
end;
function bad_record(const aMessage:PChar):TIP2LocationRecord;
begin
result.country_short:=aMessage;
result.country_long:=aMessage;
result.region:=aMessage;
result.city:=aMessage;
result.isp:=aMessage;
result.latitude:= 0;
result.longitude:= 0;
result.domain:=aMessage;
result.zipcode:=aMessage;
result.timezone:=aMessage;
result.netspeed:=aMessage;
result.iddcode:=aMessage;
result.areacode:=aMessage;
result.weatherstationcode:=aMessage;
result.weatherstationname:=aMessage;
result.mcc:=aMessage;
result.mnc:=aMessage;
result.mobilebrand:=aMessage;
result.elevation := 0;
result.usagetype:=aMessage;
end;
function IP2Location_get_record(loc:TIP2Location;ipstring:PChar;mode:Cardinal):TIP2LocationRecord;
var
handle:integer;
baseaddr,dbcolumn,low,high,mid,ipno,ipfrom,ipto:Cardinal;
parsed_ipv:ipv_t;
begin
handle := loc.filehandle;
baseaddr := loc.ipv4databaseaddr;
dbcolumn := loc.databasecolumn;
low := 0;
high := loc.ipv4databasecount;
mid := 0;
parsed_ipv := parse_addr(ipstring);
if (parsed_ipv.ipversion = 6) then
begin
result:=IP2Location_get_ipv6_record(loc, ipstring, mode);
exit;
end;
if parsed_ipv.ipversion <> 4 then
begin
result:=bad_record(INVALID_IPV4_ADDRESS);
exit;
end;
ipno := parsed_ipv.ipv4;
if (ipno = MAX_IPV4_RANGE) then
ipno := ipno - 1;
while (low <= high) do
begin
mid:=(low + high)div 2;
ipfrom:= IP2Location_read32(handle, baseaddr + mid * dbcolumn * 4);
ipto:= IP2Location_read32(handle, baseaddr + (mid + 1) * dbcolumn * 4);
if (ipno >= ipfrom) and (ipno < ipto) then
begin
result:=read_record(loc, baseaddr + (mid * dbcolumn * 4), mode);
exit;
end
else
begin
if ipno < ipfrom then
high := mid - 1
else
low := mid + 1;
end;
end;
end;
function IP2Location_get_ipv6_record (loc:TIP2Location; ipstring:PChar;mode:Cardinal):TIP2LocationRecord;
var
handle:integer;
baseaddr,dbcolumn,low,
high,mid:cardinal;
ipno,ipfrom,ipto:Tin6_addr_local;
parsed_ipv:ipv_t;
begin
handle := loc.filehandle;
baseaddr := loc.ipv6databaseaddr;
dbcolumn := loc.databasecolumn;
low := 0;
high := loc.ipv6databasecount;
mid := 0;
parsed_ipv:= parse_addr(ipstring);
if parsed_ipv.ipversion = 4 then
begin
result:=IP2Location_get_record(loc, ipstring, mode);
exit;
end;
if parsed_ipv.ipversion <> 6 then
begin
result:=bad_record(INVALID_IPV6_ADDRESS);
exit;
end;
ipno := parsed_ipv.ipv6;
while (low <= high) do
begin
mid :=(low + high) div 2;
ipfrom := IP2Location_readIPv6Address(handle, baseaddr + mid * (dbcolumn * 4 + 12));
ipto := IP2Location_readIPv6Address(handle, baseaddr + ( mid + 1 ) * (dbcolumn * 4 + 12));
if (ipv6_compare(ipno, ipfrom) >= 0) and (ipv6_compare(ipno, ipto) < 0) then
begin
result:=read_record(loc, baseaddr + mid * (dbcolumn * 4 + 12) + 12, mode);
exit;
end
else
begin
if ipv6_compare(ipno, ipfrom) < 0 then
high := mid - 1
else
low := mid + 1;
end;
end;
end;
function read_record(loc:TIP2Location;rowaddr,mode:cardinal):TIP2LocationRecord;
var
dbtype:Byte;
handle:integer;
begin
dbtype := loc.databasetype;
handle := loc.filehandle;
if ((mode and _COUNTRYSHORT>0) and (COUNTRY_POSITION[dbtype] <> 0)) then
result.country_short := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (COUNTRY_POSITION[dbtype]-1)))
else
result.country_short :=NOT_SUPPORTED;
if ((mode and _COUNTRYLONG>0) and (COUNTRY_POSITION[dbtype] <> 0)) then
result.country_long := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (COUNTRY_POSITION[dbtype]-1))+3)
else
result.country_long :=NOT_SUPPORTED;
if ((mode and _REGION>0) and (REGION_POSITION[dbtype] <> 0)) then
result.region := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (REGION_POSITION[dbtype]-1)))
else
result.region :=NOT_SUPPORTED;
if ((mode and _CITY>0) and (CITY_POSITION[dbtype] <> 0)) then
result.city := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (CITY_POSITION[dbtype]-1)))
else
result.city :=NOT_SUPPORTED;
if ((mode and _ISP>0) and (ISP_POSITION[dbtype] <> 0)) then
result.isp := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (ISP_POSITION[dbtype]-1)))
else
result.isp :=NOT_SUPPORTED;
if ((mode and _LATITUDE>0) and (LATITUDE_POSITION[dbtype] <> 0)) then
result.latitude := IP2Location_readFloat(handle, rowaddr + 4 * (LATITUDE_POSITION[dbtype]-1))
else
result.latitude := 0.0;
if ((mode and _LONGITUDE>0) and (LONGITUDE_POSITION[dbtype] <> 0)) then
result.longitude := IP2Location_readFloat(handle, rowaddr + 4 * (LONGITUDE_POSITION[dbtype]-1))
else
result.longitude := 0.0;
if ((mode and _DOMAIN>0) and (DOMAIN_POSITION[dbtype] <> 0)) then
result.domain := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (DOMAIN_POSITION[dbtype]-1)))
else
result.domain :=NOT_SUPPORTED;
if ((mode and _ZIPCODE>0) and (ZIPCODE_POSITION[dbtype] <> 0)) then
result.zipcode := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (ZIPCODE_POSITION[dbtype]-1)))
else
result.zipcode :=NOT_SUPPORTED;
if ((mode and _TIMEZONE>0) and (TIMEZONE_POSITION[dbtype] <> 0)) then
result.timezone := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (TIMEZONE_POSITION[dbtype]-1)))
else
result.timezone :=NOT_SUPPORTED;
if ((mode and _NETSPEED>0) and (NETSPEED_POSITION[dbtype] <> 0)) then
result.netspeed := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (NETSPEED_POSITION[dbtype]-1)))
else
result.netspeed :=NOT_SUPPORTED;
if ((mode and _IDDCODE>0) and (IDDCODE_POSITION[dbtype] <> 0)) then
result.iddcode := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (IDDCODE_POSITION[dbtype]-1)))
else
result.iddcode :=NOT_SUPPORTED;
if ((mode and _AREACODE>0) and (AREACODE_POSITION[dbtype] <> 0)) then
result.areacode := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (AREACODE_POSITION[dbtype]-1)))
else
result.areacode :=NOT_SUPPORTED;
if ((mode and _WEATHERSTATIONCODE>0) and (WEATHERSTATIONCODE_POSITION[dbtype] <> 0)) then
result.weatherstationcode := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (WEATHERSTATIONCODE_POSITION[dbtype]-1)))
else
result.weatherstationcode :=NOT_SUPPORTED;
if ((mode and _WEATHERSTATIONNAME>0) and (WEATHERSTATIONNAME_POSITION[dbtype] <> 0)) then
result.weatherstationname := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (WEATHERSTATIONNAME_POSITION[dbtype]-1)))
else
result.weatherstationname :=NOT_SUPPORTED;
if ((mode and _MCC>0) and (MCC_POSITION[dbtype] <> 0)) then
result.mcc := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (MCC_POSITION[dbtype]-1)))
else
result.mcc :=NOT_SUPPORTED;
if ((mode and _MNC>0) and (MNC_POSITION[dbtype] <> 0)) then
result.mnc := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (MNC_POSITION[dbtype]-1)))
else
result.mnc :=NOT_SUPPORTED;
if ((mode and _MOBILEBRAND>0) and (MOBILEBRAND_POSITION[dbtype] <> 0)) then
result.mobilebrand := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (MOBILEBRAND_POSITION[dbtype]-1)))
else
result.mobilebrand :=NOT_SUPPORTED;
if ((mode and _ELEVATION>0) and (ELEVATION_POSITION[dbtype] <> 0)) then
result.elevation := strtofloat(IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (ELEVATION_POSITION[dbtype]-1))))
else
result.elevation := 0.0;
if ((mode and _USAGETYPE>0) and (USAGETYPE_POSITION[dbtype] <> 0)) then
result.usagetype := IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (USAGETYPE_POSITION[dbtype]-1)))
else
result.usagetype :=NOT_SUPPORTED;
end;
// Description: Open the IP2Location database file
function IP2Location_open(db:PChar):TIP2Location;
var f:integer;
begin
f:=FileOpen(db,fmOpenRead);
if f <> INVALID_HANDLE_VALUE then
begin
result.filehandle:=f;
IP2Location_initialize(result);
end;
end;
//Description: This function to set the DB access type.
function IP2Location_open_mem(loc:TIP2Location;mtype:TIP2Location_mem_type):integer;
begin
result:=-1;
if (loc.filehandle <>0) and not(openMemFlag) then
begin
openMemFlag:=True;
case mtype of
IP2LOCATION_FILE_IO :result:=0;//Just return,by default its IP2LOCATION_FILE_IO
IP2LOCATION_CACHE_MEMORY :result:=IP2Location_DB_set_memory_cache(loc.filehandle);
IP2LOCATION_SHARED_MEMORY:result:=IP2Location_DB_set_shared_memory(loc.filehandle);
end;
end;
end;
// Description: Close the IP2Location database file
function IP2Location_close(loc:TIP2Location):DWORD;
begin
openMemFlag:=False;
if loc.filehandle<>0 then
IP2Location_DB_close(loc.filehandle);
result:=0;
end;
// Description: Get country code
function IP2Location_get_country_short(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _COUNTRYSHORT);
end;
// Description: Get country name
function IP2Location_get_country_long(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _COUNTRYLONG);
end;
// Description: Get the name of state/region
function IP2Location_get_country_region(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _REGION);
end;
// Description: Get city name
function IP2Location_get_country_city(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _CITY);
end;
// Description: Get ISP name
function IP2Location_get_country_isp(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _ISP);
end;
// Description: Get latitude
function IP2Location_get_country_latitude(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _LATITUDE);
end;
// Description: Get longitude
function IP2Location_get_country_longitude(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _LONGITUDE);
end;
// Description: Get domain name
function IP2Location_get_country_domain(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _DOMAIN);
end;
// Description: Get ZIP code
function IP2Location_get_country_zipcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _ZIPCODE);
end;
// Description: Get time zone
function IP2Location_get_country_timezone(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _TIMEZONE);
end;
// Description: Get net speed
function IP2Location_get_country_netspeed(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _NETSPEED);
end;
// Description: Get IDD code
function IP2Location_get_country_iddcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc, ip, _IDDCODE);
end;
// Description: Get area code
function IP2Location_get_country_areacode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_AREACODE);
end;
// Description: Get weather station code
function IP2Location_get_country_weatherstationcode(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_WEATHERSTATIONCODE);
end;
// Description: Get weather station name
function IP2Location_get_country_weatherstationname(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_WEATHERSTATIONNAME);
end;
// Description: Get mobile country code
function IP2Location_get_country_mcc(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_MCC);
end;
// Description: Get mobile national code
function IP2Location_get_country_mnc(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_MNC);
end;
// Description: Get mobile carrier brand
function IP2Location_get_country_mobilebrand(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_MOBILEBRAND);
end;
// Description: Get elevation
function IP2Location_get_country_elevation(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_ELEVATION);
end;
// Description: Get usage type
function IP2Location_get_country_usagetype(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_USAGETYPE);
end;
// Description: Get all records
function IP2Location_get_country_all(loc:TIP2Location;ip:PChar):TIP2LocationRecord;
begin
result:=IP2Location_get_record(loc,ip,_ALL);
end;
end.