-
Notifications
You must be signed in to change notification settings - Fork 95
/
ip2location.go
1194 lines (1042 loc) · 38.1 KB
/
ip2location.go
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This ip2location package provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone,
// ISP, domain name, connection type, IDD code, area code, weather station code, station name, MCC, MNC,
// mobile brand, elevation, usage type, address type, IAB category, district, autonomous system number (ASN) and
// autonomous system (AS) from IP address by using IP2Location database.
package ip2location
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"lukechampine.com/uint128"
"math"
"math/big"
"net"
"os"
"strconv"
"unsafe"
)
type DBReader interface {
io.ReadCloser
io.ReaderAt
}
type ip2locationmeta struct {
databasetype uint8
databasecolumn uint8
databaseday uint8
databasemonth uint8
databaseyear uint8
ipv4databasecount uint32
ipv4databaseaddr uint32
ipv6databasecount uint32
ipv6databaseaddr uint32
ipv4indexed bool
ipv6indexed bool
ipv4indexbaseaddr uint32
ipv6indexbaseaddr uint32
ipv4columnsize uint32
ipv6columnsize uint32
productcode uint8
producttype uint8
filesize uint32
}
// The IP2Locationrecord struct stores all of the available
// geolocation info found in the IP2Location database.
type IP2Locationrecord struct {
Country_short string
Country_long string
Region string
City string
Isp string
Latitude float32
Longitude float32
Domain string
Zipcode string
Timezone string
Netspeed string
Iddcode string
Areacode string
Weatherstationcode string
Weatherstationname string
Mcc string
Mnc string
Mobilebrand string
Elevation float32
Usagetype string
Addresstype string
Category string
District string
Asn string
As string
}
type DB struct {
f DBReader
meta ip2locationmeta
country_position_offset uint32
region_position_offset uint32
city_position_offset uint32
isp_position_offset uint32
domain_position_offset uint32
zipcode_position_offset uint32
latitude_position_offset uint32
longitude_position_offset uint32
timezone_position_offset uint32
netspeed_position_offset uint32
iddcode_position_offset uint32
areacode_position_offset uint32
weatherstationcode_position_offset uint32
weatherstationname_position_offset uint32
mcc_position_offset uint32
mnc_position_offset uint32
mobilebrand_position_offset uint32
elevation_position_offset uint32
usagetype_position_offset uint32
addresstype_position_offset uint32
category_position_offset uint32
district_position_offset uint32
asn_position_offset uint32
as_position_offset uint32
country_enabled bool
region_enabled bool
city_enabled bool
isp_enabled bool
domain_enabled bool
zipcode_enabled bool
latitude_enabled bool
longitude_enabled bool
timezone_enabled bool
netspeed_enabled bool
iddcode_enabled bool
areacode_enabled bool
weatherstationcode_enabled bool
weatherstationname_enabled bool
mcc_enabled bool
mnc_enabled bool
mobilebrand_enabled bool
elevation_enabled bool
usagetype_enabled bool
addresstype_enabled bool
category_enabled bool
district_enabled bool
asn_enabled bool
as_enabled bool
metaok bool
}
var defaultDB = &DB{}
var country_position = [27]uint8{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}
var region_position = [27]uint8{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}
var city_position = [27]uint8{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}
var isp_position = [27]uint8{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}
var latitude_position = [27]uint8{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}
var longitude_position = [27]uint8{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}
var domain_position = [27]uint8{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}
var zipcode_position = [27]uint8{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}
var timezone_position = [27]uint8{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}
var netspeed_position = [27]uint8{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}
var iddcode_position = [27]uint8{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}
var areacode_position = [27]uint8{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}
var weatherstationcode_position = [27]uint8{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}
var weatherstationname_position = [27]uint8{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}
var mcc_position = [27]uint8{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}
var mnc_position = [27]uint8{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}
var mobilebrand_position = [27]uint8{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}
var elevation_position = [27]uint8{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}
var usagetype_position = [27]uint8{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}
var addresstype_position = [27]uint8{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}
var category_position = [27]uint8{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}
var district_position = [27]uint8{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}
var asn_position = [27]uint8{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}
var as_position = [27]uint8{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}
const api_version string = "9.7.0"
var max_ipv4_range = uint128.From64(4294967295)
var max_ipv6_range = uint128.From64(0)
var from_v4mapped = uint128.From64(281470681743360)
var to_v4mapped = uint128.From64(281474976710655)
var from_6to4 = uint128.From64(0)
var to_6to4 = uint128.From64(0)
var from_teredo = uint128.From64(0)
var to_teredo = uint128.From64(0)
var last_32bits = uint128.From64(4294967295)
const countryshort uint32 = 0x0000001
const countrylong uint32 = 0x0000002
const region uint32 = 0x0000004
const city uint32 = 0x0000008
const isp uint32 = 0x0000010
const latitude uint32 = 0x0000020
const longitude uint32 = 0x0000040
const domain uint32 = 0x0000080
const zipcode uint32 = 0x0000100
const timezone uint32 = 0x0000200
const netspeed uint32 = 0x0000400
const iddcode uint32 = 0x0000800
const areacode uint32 = 0x0001000
const weatherstationcode uint32 = 0x0002000
const weatherstationname uint32 = 0x0004000
const mcc uint32 = 0x0008000
const mnc uint32 = 0x0010000
const mobilebrand uint32 = 0x0020000
const elevation uint32 = 0x0040000
const usagetype uint32 = 0x0080000
const addresstype uint32 = 0x0100000
const category uint32 = 0x0200000
const district uint32 = 0x0400000
const asn uint32 = 0x0800000
const as uint32 = 0x1000000
const all uint32 = countryshort | countrylong | region | city | isp | latitude | longitude | domain | zipcode | timezone | netspeed | iddcode | areacode | weatherstationcode | weatherstationname | mcc | mnc | mobilebrand | elevation | usagetype | addresstype | category | district | asn | as
const invalid_address string = "Invalid IP address."
const missing_file string = "Invalid database file."
const not_supported string = "This parameter is unavailable for selected data file. Please upgrade the data file."
const invalid_bin string = "Incorrect IP2Location BIN file format. Please make sure that you are using the latest IP2Location BIN file."
func reverseBytes(s []byte) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
// get IP type and calculate IP number; calculates index too if exists
func (d *DB) checkip(ip string) (iptype uint32, ipnum uint128.Uint128, ipindex uint32) {
iptype = 0
ipnum = uint128.From64(0)
ipnumtmp := uint128.From64(0)
ipindex = 0
ipaddress := net.ParseIP(ip)
if ipaddress != nil {
v4 := ipaddress.To4()
if v4 != nil {
iptype = 4
ipnum = uint128.From64(uint64(binary.BigEndian.Uint32(v4)))
} else {
v6 := ipaddress.To16()
if v6 != nil {
iptype = 6
reverseBytes(v6)
ipnum = uint128.FromBytes(v6)
if ipnum.Cmp(from_v4mapped) >= 0 && ipnum.Cmp(to_v4mapped) <= 0 {
// ipv4-mapped ipv6 should treat as ipv4 and read ipv4 data section
iptype = 4
ipnum = ipnum.Sub(from_v4mapped)
} else if ipnum.Cmp(from_6to4) >= 0 && ipnum.Cmp(to_6to4) <= 0 {
// 6to4 so need to remap to ipv4
iptype = 4
ipnum = ipnum.Rsh(80)
ipnum = ipnum.And(last_32bits)
} else if ipnum.Cmp(from_teredo) >= 0 && ipnum.Cmp(to_teredo) <= 0 {
// Teredo so need to remap to ipv4
iptype = 4
ipnum = uint128.Uint128{^ipnum.Lo, ^ipnum.Hi}
ipnum = ipnum.And(last_32bits)
}
}
}
}
if iptype == 4 {
if d.meta.ipv4indexed {
ipnumtmp = ipnum.Rsh(16)
ipnumtmp = ipnumtmp.Lsh(3)
ipindex = uint32(ipnumtmp.Add(uint128.From64(uint64(d.meta.ipv4indexbaseaddr))).Lo)
}
} else if iptype == 6 {
if d.meta.ipv6indexed {
ipnumtmp = ipnum.Rsh(112)
ipnumtmp = ipnumtmp.Lsh(3)
ipindex = uint32(ipnumtmp.Add(uint128.From64(uint64(d.meta.ipv6indexbaseaddr))).Lo)
}
}
return
}
// read byte
func (d *DB) readuint8(pos int64) (uint8, error) {
var retval uint8
data := make([]byte, 1)
_, err := d.f.ReadAt(data, pos-1)
if err != nil {
return 0, err
}
retval = data[0]
return retval, nil
}
// read row
func (d *DB) read_row(pos uint32, size uint32) ([]byte, error) {
pos2 := int64(pos)
data := make([]byte, size)
_, err := d.f.ReadAt(data, pos2-1)
if err != nil {
return nil, err
}
return data, nil
}
// read unsigned 32-bit integer from slices
func (d *DB) readuint32_row(row []byte, pos uint32) uint32 {
var retval uint32
data := row[pos : pos+4]
retval = binary.LittleEndian.Uint32(data)
return retval
}
// read unsigned 32-bit integer
func (d *DB) readuint32(pos uint32) (uint32, error) {
pos2 := int64(pos)
var retval uint32
data := make([]byte, 4)
_, err := d.f.ReadAt(data, pos2-1)
if err != nil {
return 0, err
}
buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, &retval)
if err != nil {
fmt.Printf("binary read failed: %v", err)
}
return retval, nil
}
// read unsigned 128-bit integer from slices
func (d *DB) readuint128_row(row []byte, pos uint32) uint128.Uint128 {
retval := uint128.From64(0)
data := row[pos : pos+16]
// little endian to big endian
// for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
// data[i], data[j] = data[j], data[i]
// }
retval = uint128.FromBytes(data)
return retval
}
// read unsigned 128-bit integer
func (d *DB) readuint128(pos uint32) (uint128.Uint128, error) {
pos2 := int64(pos)
retval := uint128.From64(0)
data := make([]byte, 16)
_, err := d.f.ReadAt(data, pos2-1)
if err != nil {
return uint128.From64(0), err
}
// little endian to big endian
// for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
// data[i], data[j] = data[j], data[i]
// }
retval = uint128.FromBytes(data)
return retval, nil
}
// read string
func (d *DB) readstr(pos uint32) (string, error) {
pos2 := int64(pos)
readlen := 256 // max size of string field + 1 byte for the length
var retval string
data := make([]byte, readlen)
_, err := d.f.ReadAt(data, pos2)
if err != nil && err.Error() != "EOF" { // bypass EOF error coz we are reading 256 which may hit EOF
return "", err
}
strlen := data[0]
retval = convertBytesToString(data[1:(strlen + 1)])
return retval, nil
}
// read float from slices
func (d *DB) readfloat_row(row []byte, pos uint32) float32 {
var retval float32
data := row[pos : pos+4]
bits := binary.LittleEndian.Uint32(data)
retval = math.Float32frombits(bits)
return retval
}
func fatal(db *DB, err error) (*DB, error) {
_ = db.f.Close()
return nil, err
}
// Open takes the path to the IP2Location BIN database file. It will read all the metadata required to
// be able to extract the embedded geolocation data, and return the underlining DB object.
func OpenDB(dbpath string) (*DB, error) {
f, err := os.Open(dbpath)
if err != nil {
return nil, err
}
return OpenDBWithReader(f)
}
// OpenDBWithReader takes a DBReader to the IP2Location BIN database file. It will read all the metadata required to
// be able to extract the embedded geolocation data, and return the underlining DB object.
func OpenDBWithReader(reader DBReader) (*DB, error) {
var db = &DB{}
_max_ipv6_range := big.NewInt(0)
_max_ipv6_range.SetString("340282366920938463463374607431768211455", 10)
max_ipv6_range = uint128.FromBig(_max_ipv6_range)
_from_6to4 := big.NewInt(0)
_from_6to4.SetString("42545680458834377588178886921629466624", 10)
from_6to4 = uint128.FromBig(_from_6to4)
_to_6to4 := big.NewInt(0)
_to_6to4.SetString("42550872755692912415807417417958686719", 10)
to_6to4 = uint128.FromBig(_to_6to4)
_from_teredo := big.NewInt(0)
_from_teredo.SetString("42540488161975842760550356425300246528", 10)
from_teredo = uint128.FromBig(_from_teredo)
_to_teredo := big.NewInt(0)
_to_teredo.SetString("42540488241204005274814694018844196863", 10)
to_teredo = uint128.FromBig(_to_teredo)
db.f = reader
var row []byte
var err error
readlen := uint32(64) // 64-byte header
row, err = db.read_row(1, readlen)
if err != nil {
return fatal(db, err)
}
db.meta.databasetype = row[0]
db.meta.databasecolumn = row[1]
db.meta.databaseyear = row[2]
db.meta.databasemonth = row[3]
db.meta.databaseday = row[4]
db.meta.ipv4databasecount = db.readuint32_row(row, 5)
db.meta.ipv4databaseaddr = db.readuint32_row(row, 9)
db.meta.ipv6databasecount = db.readuint32_row(row, 13)
db.meta.ipv6databaseaddr = db.readuint32_row(row, 17)
db.meta.ipv4indexbaseaddr = db.readuint32_row(row, 21)
db.meta.ipv6indexbaseaddr = db.readuint32_row(row, 25)
db.meta.productcode = row[29]
db.meta.producttype = row[30]
db.meta.filesize = db.readuint32_row(row, 31)
// check if is correct BIN (should be 1 for IP2Location BIN file), also checking for zipped file (PK being the first 2 chars)
if (db.meta.productcode != 1 && db.meta.databaseyear >= 21) || (db.meta.databasetype == 80 && db.meta.databasecolumn == 75) { // only BINs from Jan 2021 onwards have this byte set
return fatal(db, errors.New(invalid_bin))
}
if db.meta.ipv4indexbaseaddr > 0 {
db.meta.ipv4indexed = true
}
if db.meta.ipv6databasecount > 0 && db.meta.ipv6indexbaseaddr > 0 {
db.meta.ipv6indexed = true
}
db.meta.ipv4columnsize = uint32(db.meta.databasecolumn << 2) // 4 bytes each column
db.meta.ipv6columnsize = uint32(16 + ((db.meta.databasecolumn - 1) << 2)) // 4 bytes each column, except IPFrom column which is 16 bytes
dbt := db.meta.databasetype
if country_position[dbt] != 0 {
db.country_position_offset = uint32(country_position[dbt]-2) << 2
db.country_enabled = true
}
if region_position[dbt] != 0 {
db.region_position_offset = uint32(region_position[dbt]-2) << 2
db.region_enabled = true
}
if city_position[dbt] != 0 {
db.city_position_offset = uint32(city_position[dbt]-2) << 2
db.city_enabled = true
}
if isp_position[dbt] != 0 {
db.isp_position_offset = uint32(isp_position[dbt]-2) << 2
db.isp_enabled = true
}
if domain_position[dbt] != 0 {
db.domain_position_offset = uint32(domain_position[dbt]-2) << 2
db.domain_enabled = true
}
if zipcode_position[dbt] != 0 {
db.zipcode_position_offset = uint32(zipcode_position[dbt]-2) << 2
db.zipcode_enabled = true
}
if latitude_position[dbt] != 0 {
db.latitude_position_offset = uint32(latitude_position[dbt]-2) << 2
db.latitude_enabled = true
}
if longitude_position[dbt] != 0 {
db.longitude_position_offset = uint32(longitude_position[dbt]-2) << 2
db.longitude_enabled = true
}
if timezone_position[dbt] != 0 {
db.timezone_position_offset = uint32(timezone_position[dbt]-2) << 2
db.timezone_enabled = true
}
if netspeed_position[dbt] != 0 {
db.netspeed_position_offset = uint32(netspeed_position[dbt]-2) << 2
db.netspeed_enabled = true
}
if iddcode_position[dbt] != 0 {
db.iddcode_position_offset = uint32(iddcode_position[dbt]-2) << 2
db.iddcode_enabled = true
}
if areacode_position[dbt] != 0 {
db.areacode_position_offset = uint32(areacode_position[dbt]-2) << 2
db.areacode_enabled = true
}
if weatherstationcode_position[dbt] != 0 {
db.weatherstationcode_position_offset = uint32(weatherstationcode_position[dbt]-2) << 2
db.weatherstationcode_enabled = true
}
if weatherstationname_position[dbt] != 0 {
db.weatherstationname_position_offset = uint32(weatherstationname_position[dbt]-2) << 2
db.weatherstationname_enabled = true
}
if mcc_position[dbt] != 0 {
db.mcc_position_offset = uint32(mcc_position[dbt]-2) << 2
db.mcc_enabled = true
}
if mnc_position[dbt] != 0 {
db.mnc_position_offset = uint32(mnc_position[dbt]-2) << 2
db.mnc_enabled = true
}
if mobilebrand_position[dbt] != 0 {
db.mobilebrand_position_offset = uint32(mobilebrand_position[dbt]-2) << 2
db.mobilebrand_enabled = true
}
if elevation_position[dbt] != 0 {
db.elevation_position_offset = uint32(elevation_position[dbt]-2) << 2
db.elevation_enabled = true
}
if usagetype_position[dbt] != 0 {
db.usagetype_position_offset = uint32(usagetype_position[dbt]-2) << 2
db.usagetype_enabled = true
}
if addresstype_position[dbt] != 0 {
db.addresstype_position_offset = uint32(addresstype_position[dbt]-2) << 2
db.addresstype_enabled = true
}
if category_position[dbt] != 0 {
db.category_position_offset = uint32(category_position[dbt]-2) << 2
db.category_enabled = true
}
if district_position[dbt] != 0 {
db.district_position_offset = uint32(district_position[dbt]-2) << 2
db.district_enabled = true
}
if asn_position[dbt] != 0 {
db.asn_position_offset = uint32(asn_position[dbt]-2) << 2
db.asn_enabled = true
}
if as_position[dbt] != 0 {
db.as_position_offset = uint32(as_position[dbt]-2) << 2
db.as_enabled = true
}
db.metaok = true
return db, nil
}
// Open takes the path to the IP2Location BIN database file. It will read all the metadata required to
// be able to extract the embedded geolocation data.
//
// Deprecated: No longer being updated.
func Open(dbpath string) {
db, err := OpenDB(dbpath)
if err != nil {
return
}
defaultDB = db
}
// Close will close the file handle to the BIN file.
//
// Deprecated: No longer being updated.
func Close() {
defaultDB.Close()
}
// Api_version returns the version of the component.
func Api_version() string {
return api_version
}
// PackageVersion returns the database type.
func (d *DB) PackageVersion() string {
return strconv.Itoa(int(d.meta.databasetype))
}
// DatabaseVersion returns the database version.
func (d *DB) DatabaseVersion() string {
return "20" + strconv.Itoa(int(d.meta.databaseyear)) + "." + strconv.Itoa(int(d.meta.databasemonth)) + "." + strconv.Itoa(int(d.meta.databaseday))
}
// populate record with message
func loadmessage(mesg string) IP2Locationrecord {
var x IP2Locationrecord
x.Country_short = mesg
x.Country_long = mesg
x.Region = mesg
x.City = mesg
x.Isp = mesg
x.Domain = mesg
x.Zipcode = mesg
x.Timezone = mesg
x.Netspeed = mesg
x.Iddcode = mesg
x.Areacode = mesg
x.Weatherstationcode = mesg
x.Weatherstationname = mesg
x.Mcc = mesg
x.Mnc = mesg
x.Mobilebrand = mesg
x.Usagetype = mesg
x.Addresstype = mesg
x.Category = mesg
x.District = mesg
x.Asn = mesg
x.As = mesg
return x
}
func handleError(rec IP2Locationrecord, err error) IP2Locationrecord {
if err != nil {
fmt.Print(err)
}
return rec
}
// convertBytesToString provides a no-copy []byte to string conversion.
// This implementation is adopted by official strings.Builder.
// Reference: https://github.com/golang/go/issues/25484
func convertBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// Get_all will return all geolocation fields based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_all(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, all))
}
// Get_country_short will return the ISO-3166 country code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_country_short(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, countryshort))
}
// Get_country_long will return the country name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_country_long(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, countrylong))
}
// Get_region will return the region name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_region(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, region))
}
// Get_city will return the city name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_city(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, city))
}
// Get_isp will return the Internet Service Provider name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_isp(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, isp))
}
// Get_latitude will return the latitude based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_latitude(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, latitude))
}
// Get_longitude will return the longitude based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_longitude(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, longitude))
}
// Get_domain will return the domain name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_domain(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, domain))
}
// Get_zipcode will return the postal code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_zipcode(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, zipcode))
}
// Get_timezone will return the time zone based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_timezone(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, timezone))
}
// Get_netspeed will return the Internet connection speed based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_netspeed(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, netspeed))
}
// Get_iddcode will return the International Direct Dialing code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_iddcode(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, iddcode))
}
// Get_areacode will return the area code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_areacode(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, areacode))
}
// Get_weatherstationcode will return the weather station code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_weatherstationcode(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, weatherstationcode))
}
// Get_weatherstationname will return the weather station name based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_weatherstationname(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, weatherstationname))
}
// Get_mcc will return the mobile country code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_mcc(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, mcc))
}
// Get_mnc will return the mobile network code based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_mnc(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, mnc))
}
// Get_mobilebrand will return the mobile carrier brand based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_mobilebrand(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, mobilebrand))
}
// Get_elevation will return the elevation in meters based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_elevation(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, elevation))
}
// Get_usagetype will return the usage type based on the queried IP address.
//
// Deprecated: No longer being updated.
func Get_usagetype(ipaddress string) IP2Locationrecord {
return handleError(defaultDB.query(ipaddress, usagetype))
}
// Get_all will return all geolocation fields based on the queried IP address.
func (d *DB) Get_all(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, all)
}
// Get_country_short will return the ISO-3166 country code based on the queried IP address.
func (d *DB) Get_country_short(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, countryshort)
}
// Get_country_long will return the country name based on the queried IP address.
func (d *DB) Get_country_long(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, countrylong)
}
// Get_region will return the region name based on the queried IP address.
func (d *DB) Get_region(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, region)
}
// Get_city will return the city name based on the queried IP address.
func (d *DB) Get_city(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, city)
}
// Get_isp will return the Internet Service Provider name based on the queried IP address.
func (d *DB) Get_isp(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, isp)
}
// Get_latitude will return the latitude based on the queried IP address.
func (d *DB) Get_latitude(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, latitude)
}
// Get_longitude will return the longitude based on the queried IP address.
func (d *DB) Get_longitude(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, longitude)
}
// Get_domain will return the domain name based on the queried IP address.
func (d *DB) Get_domain(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, domain)
}
// Get_zipcode will return the postal code based on the queried IP address.
func (d *DB) Get_zipcode(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, zipcode)
}
// Get_timezone will return the time zone based on the queried IP address.
func (d *DB) Get_timezone(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, timezone)
}
// Get_netspeed will return the Internet connection speed based on the queried IP address.
func (d *DB) Get_netspeed(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, netspeed)
}
// Get_iddcode will return the International Direct Dialing code based on the queried IP address.
func (d *DB) Get_iddcode(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, iddcode)
}
// Get_areacode will return the area code based on the queried IP address.
func (d *DB) Get_areacode(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, areacode)
}
// Get_weatherstationcode will return the weather station code based on the queried IP address.
func (d *DB) Get_weatherstationcode(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, weatherstationcode)
}
// Get_weatherstationname will return the weather station name based on the queried IP address.
func (d *DB) Get_weatherstationname(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, weatherstationname)
}
// Get_mcc will return the mobile country code based on the queried IP address.
func (d *DB) Get_mcc(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, mcc)
}
// Get_mnc will return the mobile network code based on the queried IP address.
func (d *DB) Get_mnc(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, mnc)
}
// Get_mobilebrand will return the mobile carrier brand based on the queried IP address.
func (d *DB) Get_mobilebrand(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, mobilebrand)
}
// Get_elevation will return the elevation in meters based on the queried IP address.
func (d *DB) Get_elevation(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, elevation)
}
// Get_usagetype will return the usage type based on the queried IP address.
func (d *DB) Get_usagetype(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, usagetype)
}
// Get_addresstype will return the address type based on the queried IP address.
func (d *DB) Get_addresstype(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, addresstype)
}
// Get_category will return the category based on the queried IP address.
func (d *DB) Get_category(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, category)
}
// Get_district will return the district name based on the queried IP address.
func (d *DB) Get_district(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, district)
}
// Get_asn will return the autonomous system number (ASN) based on the queried IP address.
func (d *DB) Get_asn(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, asn)
}
// Get_as will return the autonomous system (AS) based on the queried IP address.
func (d *DB) Get_as(ipaddress string) (IP2Locationrecord, error) {
return d.query(ipaddress, as)
}
// main query
func (d *DB) query(ipaddress string, mode uint32) (IP2Locationrecord, error) {
x := loadmessage(not_supported) // default message
// read metadata
if !d.metaok {
x = loadmessage(missing_file)
return x, nil
}
// check IP type and return IP number & index (if exists)
iptype, ipno, ipindex := d.checkip(ipaddress)
if iptype == 0 {
x = loadmessage(invalid_address)
return x, nil
}
var err error
var colsize uint32
var baseaddr uint32
var low uint32
var high uint32
var mid uint32
var rowoffset uint32
var firstcol uint32 = 4 // 4 bytes for ip from
var row []byte
var fullrow []byte
var readlen uint32
ipfrom := uint128.From64(0)
ipto := uint128.From64(0)
maxip := uint128.From64(0)
if iptype == 4 {
baseaddr = d.meta.ipv4databaseaddr
high = d.meta.ipv4databasecount
maxip = max_ipv4_range
colsize = d.meta.ipv4columnsize
} else {
firstcol = 16 // 16 bytes for ip from
baseaddr = d.meta.ipv6databaseaddr
high = d.meta.ipv6databasecount
maxip = max_ipv6_range
colsize = d.meta.ipv6columnsize
}
// reading index
if ipindex > 0 {
row, err = d.read_row(ipindex, 8) // 4 bytes each for IP From and IP To
if err != nil {
return x, err
}
low = d.readuint32_row(row, 0)
high = d.readuint32_row(row, 4)
}
if ipno.Cmp(maxip) >= 0 {
ipno = ipno.Sub(uint128.From64(1))
}
for low <= high {
mid = ((low + high) >> 1)
rowoffset = baseaddr + (mid * colsize)
// reading IP From + whole row + next IP From
readlen = colsize + firstcol
fullrow, err = d.read_row(rowoffset, readlen)
if err != nil {
return x, err
}
if iptype == 4 {
ipfrom32 := d.readuint32_row(fullrow, 0)
ipfrom = uint128.From64(uint64(ipfrom32))
ipto32 := d.readuint32_row(fullrow, colsize)
ipto = uint128.From64(uint64(ipto32))
} else {
ipfrom = d.readuint128_row(fullrow, 0)
ipto = d.readuint128_row(fullrow, colsize)
}
if ipno.Cmp(ipfrom) >= 0 && ipno.Cmp(ipto) < 0 {
rowlen := colsize - firstcol
row = fullrow[firstcol:(firstcol + rowlen)] // extract the actual row data