forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoIP.c
1643 lines (1455 loc) · 53.5 KB
/
GeoIP.c
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* GeoIP.c
*
* Copyright (C) 2006 MaxMind LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* mdw for gettimeofday() */
#include <sys/time.h>
#include "GeoIP.h"
static geoipv6_t IPV6_NULL;
#if !defined(_WIN32)
#include <netdb.h>
#include <sys/mman.h>
#endif /* !defined(_WIN32) */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h> /* for fstat */
#include <sys/stat.h> /* for fstat */
#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h> /* for gettimeofday */
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h> /* For uint32_t */
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#define COUNTRY_BEGIN 16776960
#define STATE_BEGIN_REV0 16700000
#define STATE_BEGIN_REV1 16000000
#define STRUCTURE_INFO_MAX_SIZE 20
#define DATABASE_INFO_MAX_SIZE 100
#define MAX_ORG_RECORD_LENGTH 300
#define US_OFFSET 1
#define CANADA_OFFSET 677
#define WORLD_OFFSET 1353
#define FIPS_RANGE 360
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
exit(1); \
} \
}
const char GeoIP_country_code[253][3] = { "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN",
"AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
"BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
"BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
"CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
"CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
"DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
"FK","FM","FO","FR","FX","GA","GB","GD","GE","GF",
"GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
"GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
"IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
"JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
"KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
"LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
"MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
"MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
"NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
"PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
"PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
"SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
"SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
"TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
"TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
"VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
"ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
"BL","MF"};
static const unsigned num_GeoIP_countries = (unsigned)(sizeof(GeoIP_country_code)/sizeof(GeoIP_country_code[0]));
const char GeoIP_country_code3[253][4] = { "--","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT",
"AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
"BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
"BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC","COD",
"CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
"CUB","CPV","CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
"DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
"FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD","GEO","GUF",
"GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM",
"GUM","GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN",
"IRL","ISR","IND","IO","IRQ","IRN","ISL","ITA","JAM","JOR",
"JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
"CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
"LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
"MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
"MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
"NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
"PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
"PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
"SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
"SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF","TGO","THA",
"TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
"TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN",
"VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","SRB","ZAF",
"ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
"BLM","MAF"};
const char * GeoIP_country_name[253] = {"N/A","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles",
"Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados",
"Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia",
"Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the",
"Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica",
"Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic",
"Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji",
"Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana",
"Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala",
"Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia",
"Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan",
"Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait",
"Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania",
"Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali",
"Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives",
"Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua",
"Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia",
"Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau",
"Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan",
"Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname",
"Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand",
"Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan",
"Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela",
"Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
"Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
"Saint Barthelemy","Saint Martin"};
/* Possible continent codes are AF, AS, EU, NA, OC, SA for Africa, Asia, Europe, North America, Oceania
and South America. */
const char GeoIP_country_continent[253][3] = {"--","AS","EU","EU","AS","AS","SA","SA","EU","AS","SA",
"AF","AN","SA","OC","EU","OC","SA","AS","EU","SA",
"AS","EU","AF","EU","AS","AF","AF","SA","AS","SA",
"SA","SA","AS","AF","AF","EU","SA","NA","AS","AF",
"AF","AF","EU","AF","OC","SA","AF","AS","SA","SA",
"SA","AF","AS","AS","EU","EU","AF","EU","SA","SA",
"AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
"SA","OC","EU","EU","EU","AF","EU","SA","AS","SA",
"AF","EU","SA","AF","AF","SA","AF","EU","SA","SA",
"OC","AF","SA","AS","AF","SA","EU","SA","EU","AS",
"EU","AS","AS","AS","AS","AS","EU","EU","SA","AS",
"AS","AF","AS","AS","OC","AF","SA","AS","AS","AS",
"SA","AS","AS","AS","SA","EU","AS","AF","AF","EU",
"EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
"AS","AS","AS","OC","SA","AF","SA","EU","AF","AS",
"AF","NA","AS","AF","AF","OC","AF","OC","AF","SA",
"EU","EU","AS","OC","OC","OC","AS","SA","SA","OC",
"OC","AS","AS","EU","SA","OC","SA","AS","EU","OC",
"SA","AS","AF","EU","AS","AF","AS","OC","AF","AF",
"EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
"SA","AF","SA","AS","AF","SA","AF","AF","AF","AS",
"AS","OC","AS","AF","OC","AS","AS","SA","OC","AS",
"AF","EU","AF","OC","NA","SA","AS","EU","SA","SA",
"SA","SA","AS","OC","OC","OC","AS","AF","EU","AF",
"AF","EU","AF","--","--","--","EU","EU","EU","EU",
"SA","SA"};
geoipv6_t _GeoIP_lookupaddress_v6 (const char *host);
#if defined(_WIN32)
/* http://www.mail-archive.com/[email protected]/msg02107.html */
static const char * _GeoIP_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}
static int _GeoIP_inet_pton(int af, const char *src, void *dst)
{
struct addrinfo hints, *res, *ressave;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af;
if (getaddrinfo(src, NULL, &hints, &res) != 0)
{
fprintf(stderr, "Couldn't resolve host %s\n", src);
return -1;
}
ressave = res;
while (res)
{
memcpy(dst, res->ai_addr, res->ai_addrlen);
res = res->ai_next;
}
freeaddrinfo(ressave);
return 0;
}
#else
static int _GeoIP_inet_pton(int af, const char *src, void *dst) {
return inet_pton(af, src, dst);
}
static const char * _GeoIP_inet_ntop(int af, const void *src, char *dst, socklen_t cnt) {
return inet_ntop(af, src, dst, cnt);
}
#endif /* defined(_WIN32) */
int __GEOIP_V6_IS_NULL(geoipv6_t v6) {
int i;
for (i=0;i<16;i++) {
if (v6.s6_addr[i])
return 0;
}
return 1;
}
const char * GeoIPDBDescription[NUM_DB_TYPES] = {NULL, "GeoIP Country Edition", "GeoIP City Edition, Rev 1", "GeoIP Region Edition, Rev 1", "GeoIP ISP Edition", "GeoIP Organization Edition", "GeoIP City Edition, Rev 0", "GeoIP Region Edition, Rev 0","GeoIP Proxy Edition","GeoIP ASNum Edition","GeoIP Netspeed Edition","GeoIP Domain Name Edition", "GeoIP Country V6 Edition"};
char * custom_directory = NULL;
void GeoIP_setup_custom_directory (char * dir) {
custom_directory = dir;
}
char *_GeoIP_full_path_to(const char *file_name) {
int len;
char *path = (char *)malloc(sizeof(char) * 1024);
if (custom_directory == NULL){
#if !defined(_WIN32)
memset(path, 0, sizeof(char) * 1024);
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", "./" , file_name);
#else
char buf[MAX_PATH], *p, *q = NULL;
memset(buf, 0, sizeof(buf));
len = GetModuleFileNameA(GetModuleHandle(NULL), buf, sizeof(buf) - 1);
for (p = buf + len; p > buf; p--)
if (*p == '\\')
{
if (!q)
q = p;
else
*p = '/';
}
*q = 0;
memset(path, 0, sizeof(char) * 1024);
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", buf, file_name);
#endif
} else {
len = strlen(custom_directory);
if (custom_directory[len-1] != '/') {
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s",custom_directory, file_name);
} else {
snprintf(path, sizeof(char) * 1024 - 1, "%s%s", custom_directory, file_name);
}
}
return path;
}
char ** GeoIPDBFileName = NULL;
void _GeoIP_setup_dbfilename() {
if (NULL == GeoIPDBFileName) {
GeoIPDBFileName = (char **)malloc(sizeof(char *) * NUM_DB_TYPES);
memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES);
GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _GeoIP_full_path_to("GeoIP.dat");
GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _GeoIP_full_path_to("GeoIPRegion.dat");
GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _GeoIP_full_path_to("GeoIPRegion.dat");
GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _GeoIP_full_path_to("GeoIPCity.dat");
GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _GeoIP_full_path_to("GeoIPCity.dat");
GeoIPDBFileName[GEOIP_ISP_EDITION] = _GeoIP_full_path_to("GeoIPISP.dat");
GeoIPDBFileName[GEOIP_ORG_EDITION] = _GeoIP_full_path_to("GeoIPOrg.dat");
GeoIPDBFileName[GEOIP_PROXY_EDITION] = _GeoIP_full_path_to("GeoIPProxy.dat");
GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _GeoIP_full_path_to("GeoIPASNum.dat");
GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _GeoIP_full_path_to("GeoIPNetSpeed.dat");
GeoIPDBFileName[GEOIP_DOMAIN_EDITION] = _GeoIP_full_path_to("GeoIPDomain.dat");
GeoIPDBFileName[GEOIP_COUNTRY_EDITION_V6] = _GeoIP_full_path_to("GeoIPv6.dat");
}
}
static
int _file_exists(const char *file_name) {
struct stat file_stat;
return( (stat(file_name, &file_stat) == 0) ? 1:0);
}
int GeoIP_db_avail(int type) {
const char * filePath;
if (type < 0 || type >= NUM_DB_TYPES) {
return 0;
}
_GeoIP_setup_dbfilename();
filePath = GeoIPDBFileName[type];
if (NULL == filePath) {
return 0;
}
return _file_exists(filePath);
}
static
void _setup_segments(GeoIP * gi) {
int i, j;
unsigned char delim[3];
unsigned char buf[SEGMENT_RECORD_LENGTH];
size_t silence;
gi->databaseSegments = NULL;
/* default to GeoIP Country Edition */
gi->databaseType = GEOIP_COUNTRY_EDITION;
gi->record_length = STANDARD_RECORD_LENGTH;
fseek(gi->GeoIPDatabase, -3l, SEEK_END);
for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) {
silence = fread(delim, 1, 3, gi->GeoIPDatabase);
if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) {
silence = fread(&gi->databaseType, 1, 1, gi->GeoIPDatabase);
if (gi->databaseType >= 106) {
/* backwards compatibility with databases from April 2003 and earlier */
gi->databaseType -= 105;
}
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
/* Region Edition, pre June 2003 */
gi->databaseSegments = (unsigned int *)malloc(sizeof(int));
gi->databaseSegments[0] = STATE_BEGIN_REV0;
} else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) {
/* Region Edition, post June 2003 */
gi->databaseSegments = (unsigned int *)malloc(sizeof(int));
gi->databaseSegments[0] = STATE_BEGIN_REV1;
} else if (gi->databaseType == GEOIP_CITY_EDITION_REV0 ||
gi->databaseType == GEOIP_CITY_EDITION_REV1 ||
gi->databaseType == GEOIP_ORG_EDITION ||
gi->databaseType == GEOIP_ISP_EDITION ||
gi->databaseType == GEOIP_ASNUM_EDITION) {
/* City/Org Editions have two segments, read offset of second segment */
gi->databaseSegments = (unsigned int *)malloc(sizeof(int));
gi->databaseSegments[0] = 0;
silence = fread(buf, SEGMENT_RECORD_LENGTH, 1, gi->GeoIPDatabase);
for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) {
gi->databaseSegments[0] += (buf[j] << (j * 8));
}
if (gi->databaseType == GEOIP_ORG_EDITION ||
gi->databaseType == GEOIP_ISP_EDITION)
gi->record_length = ORG_RECORD_LENGTH;
}
break;
} else {
fseek(gi->GeoIPDatabase, -4l, SEEK_CUR);
}
}
if (gi->databaseType == GEOIP_COUNTRY_EDITION ||
gi->databaseType == GEOIP_PROXY_EDITION ||
gi->databaseType == GEOIP_NETSPEED_EDITION ||
gi->databaseType == GEOIP_COUNTRY_EDITION_V6 ) {
gi->databaseSegments = (unsigned int *)malloc(sizeof(int));
gi->databaseSegments[0] = COUNTRY_BEGIN;
}
}
static
int _check_mtime(GeoIP *gi) {
struct stat buf;
#if !defined(_WIN32)
struct timeval t;
/* stat only has second granularity, so don't
call it more than once a second */
gettimeofday(&t, NULL);
if (t.tv_sec == gi->last_mtime_check){
return 0;
}
gi->last_mtime_check = t.tv_sec;
#else /* !defined(_WIN32) */
FILETIME ft;
ULONGLONG t;
/* stat only has second granularity, so don't
call it more than once a second */
GetSystemTimeAsFileTime(&ft);
t = FILETIME_TO_USEC(ft) / 1000 / 1000;
if (t == gi->last_mtime_check){
return 0;
}
gi->last_mtime_check = t;
#endif /* !defined(_WIN32) */
if (gi->flags & GEOIP_CHECK_CACHE) {
if (stat(gi->file_path, &buf) != -1) {
if (buf.st_mtime != gi->mtime) {
/* GeoIP Database file updated */
if (gi->flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE)) {
if ( gi->flags & GEOIP_MMAP_CACHE) {
#if !defined(_WIN32)
/* MMAP is only avail on UNIX */
munmap(gi->cache, gi->size);
gi->cache = NULL;
#endif
} else {
/* reload database into memory cache */
if ((gi->cache = (unsigned char*) realloc(gi->cache, buf.st_size)) == NULL) {
fprintf(stderr,"Out of memory when reloading %s\n",gi->file_path);
return -1;
}
}
}
/* refresh filehandle */
fclose(gi->GeoIPDatabase);
gi->GeoIPDatabase = fopen(gi->file_path,"rb");
if (gi->GeoIPDatabase == NULL) {
fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path);
return -1;
}
gi->mtime = buf.st_mtime;
gi->size = buf.st_size;
if ( gi->flags & GEOIP_MMAP_CACHE) {
#if defined(_WIN32)
fprintf(stderr, "GEOIP_MMAP_CACHE is not supported on WIN32\n");
gi->cache = 0;
return -1;
#else
gi->cache = (unsigned char *)mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0);
if ( gi->cache == MAP_FAILED ) {
fprintf(stderr,"Error remapping file %s when reloading\n",gi->file_path);
gi->cache = 0;
return -1;
}
#endif
} else if ( gi->flags & GEOIP_MEMORY_CACHE ) {
if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) {
fprintf(stderr,"Error reading file %s when reloading\n",gi->file_path);
return -1;
}
}
if (gi->databaseSegments != NULL) {
free(gi->databaseSegments);
gi->databaseSegments = NULL;
}
_setup_segments(gi);
if (gi->databaseSegments == NULL) {
fprintf(stderr, "Error reading file %s -- corrupt\n", gi->file_path);
return -1;
}
if (gi->flags & GEOIP_INDEX_CACHE) {
gi->index_cache = (unsigned char *) realloc(gi->index_cache, sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2)));
if (gi->index_cache != NULL) {
fseek(gi->GeoIPDatabase, 0, SEEK_SET);
if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) {
fprintf(stderr,"Error reading file %s where reloading\n",gi->file_path);
return -1;
}
}
}
}
}
}
return 0;
}
#define ADDR_STR_LEN (8 * 4 + 7 + 1)
unsigned int _GeoIP_seek_record_v6 (GeoIP *gi, geoipv6_t ipnum) {
int depth;
char paddr[ADDR_STR_LEN];
unsigned int x;
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL;
unsigned int offset = 0;
const unsigned char * p;
int j;
size_t silence;
_check_mtime(gi);
for (depth = 127; depth >= 0; depth--) {
if (gi->cache == NULL && gi->index_cache == NULL) {
/* read from disk */
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
silence = fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
} else if (gi->index_cache == NULL) {
/* simply point to record in memory */
buf = gi->cache + (long)gi->record_length * 2 *offset;
} else {
buf = gi->index_cache + (long)gi->record_length * 2 * offset;
}
if (GEOIP_CHKBIT_V6(depth, ipnum.s6_addr )) {
/* Take the right-hand branch */
if ( gi->record_length == 3 ) {
/* Most common case is completely unrolled and uses constants. */
x = (buf[3*1 + 0] << (0*8))
+ (buf[3*1 + 1] << (1*8))
+ (buf[3*1 + 2] << (2*8));
} else {
/* General case */
j = gi->record_length;
p = &buf[2*j];
x = 0;
do {
x <<= 8;
x += *(--p);
} while ( --j );
}
} else {
/* Take the left-hand branch */
if ( gi->record_length == 3 ) {
/* Most common case is completely unrolled and uses constants. */
x = (buf[3*0 + 0] << (0*8))
+ (buf[3*0 + 1] << (1*8))
+ (buf[3*0 + 2] << (2*8));
} else {
/* General case */
j = gi->record_length;
p = &buf[1*j];
x = 0;
do {
x <<= 8;
x += *(--p);
} while ( --j );
}
}
if (x >= gi->databaseSegments[0]) {
gi->netmask = 128 - depth;
return x;
}
offset = x;
}
/* shouldn't reach here */
_GeoIP_inet_ntop(AF_INET6, &ipnum.s6_addr[0], paddr, ADDR_STR_LEN);
fprintf(stderr,"Error Traversing Database for ipnum = %s - Perhaps database is corrupt?\n", paddr);
return 0;
}
geoipv6_t
_GeoIP_addr_to_num_v6(const char *addr)
{
geoipv6_t ipnum;
if ( 1 == _GeoIP_inet_pton(AF_INET6, addr, &ipnum.s6_addr[0] ) )
return ipnum;
return IPV6_NULL;
}
unsigned int _GeoIP_seek_record (GeoIP *gi, unsigned long ipnum) {
int depth;
unsigned int x;
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL;
unsigned int offset = 0;
const unsigned char * p;
int j;
size_t silence;
_check_mtime(gi);
for (depth = 31; depth >= 0; depth--) {
if (gi->cache == NULL && gi->index_cache == NULL) {
/* read from disk */
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
silence = fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
} else if (gi->index_cache == NULL) {
/* simply point to record in memory */
buf = gi->cache + (long)gi->record_length * 2 *offset;
} else {
buf = gi->index_cache + (long)gi->record_length * 2 * offset;
}
if (ipnum & (1 << depth)) {
/* Take the right-hand branch */
if ( gi->record_length == 3 ) {
/* Most common case is completely unrolled and uses constants. */
x = (buf[3*1 + 0] << (0*8))
+ (buf[3*1 + 1] << (1*8))
+ (buf[3*1 + 2] << (2*8));
} else {
/* General case */
j = gi->record_length;
p = &buf[2*j];
x = 0;
do {
x <<= 8;
x += *(--p);
} while ( --j );
}
} else {
/* Take the left-hand branch */
if ( gi->record_length == 3 ) {
/* Most common case is completely unrolled and uses constants. */
x = (buf[3*0 + 0] << (0*8))
+ (buf[3*0 + 1] << (1*8))
+ (buf[3*0 + 2] << (2*8));
} else {
/* General case */
j = gi->record_length;
p = &buf[1*j];
x = 0;
do {
x <<= 8;
x += *(--p);
} while ( --j );
}
}
if (x >= gi->databaseSegments[0]) {
gi->netmask = 32 - depth;
return x;
}
offset = x;
}
/* shouldn't reach here */
fprintf(stderr,"Error Traversing Database for ipnum = %lu - Perhaps database is corrupt?\n",ipnum);
return 0;
}
unsigned long
_GeoIP_addr_to_num(const char *addr)
{
unsigned int c, octet, t;
unsigned long ipnum;
int i = 3;
octet = ipnum = 0;
while ((c = *addr++)) {
if (c == '.') {
if (octet > 255)
return 0;
ipnum <<= 8;
ipnum += octet;
i--;
octet = 0;
} else {
t = octet;
octet <<= 3;
octet += t;
octet += t;
c -= '0';
if (c > 9)
return 0;
octet += c;
}
}
if ((octet > 255) || (i != 0))
return 0;
ipnum <<= 8;
return ipnum + octet;
}
GeoIP* GeoIP_open_type (int type, int flags) {
GeoIP * gi;
const char * filePath;
if (type < 0 || type >= NUM_DB_TYPES) {
printf("Invalid database type %d\n", type);
return NULL;
}
_GeoIP_setup_dbfilename();
filePath = GeoIPDBFileName[type];
if (filePath == NULL) {
printf("Invalid database type %d\n", type);
return NULL;
}
gi = GeoIP_open (filePath, flags);
return gi;
}
GeoIP* GeoIP_new (int flags) {
GeoIP * gi;
_GeoIP_setup_dbfilename();
gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags);
return gi;
}
GeoIP* GeoIP_open (const char * filename, int flags) {
struct stat buf;
GeoIP * gi;
size_t len;
gi = (GeoIP *)malloc(sizeof(GeoIP));
if (gi == NULL)
return NULL;
len = sizeof(char) * (strlen(filename)+1);
gi->file_path = (char *)malloc(len);
if (gi->file_path == NULL) {
free(gi);
return NULL;
}
strncpy(gi->file_path, filename, len);
gi->GeoIPDatabase = fopen(filename,"rb");
if (gi->GeoIPDatabase == NULL) {
fprintf(stderr,"Error Opening file %s\n",filename);
free(gi->file_path);
free(gi);
return NULL;
} else {
if (flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE) ) {
if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) {
fprintf(stderr,"Error stating file %s\n",filename);
free(gi->file_path);
free(gi);
return NULL;
}
gi->mtime = buf.st_mtime;
gi->size = buf.st_size;
/* MMAP added my Peter Shipley */
if ( flags & GEOIP_MMAP_CACHE ) {
#if !defined(_WIN32)
gi->cache = (unsigned char *)mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0);
if ( gi->cache == MAP_FAILED ) {
fprintf(stderr,"Error mmaping file %s\n",filename);
free(gi->file_path);
free(gi);
return NULL;
}
#endif
} else {
gi->cache = (unsigned char *) malloc(sizeof(unsigned char) * buf.st_size);
if (gi->cache != NULL) {
if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) {
fprintf(stderr,"Error reading file %s\n",filename);
free(gi->cache);
free(gi->file_path);
free(gi);
return NULL;
}
}
}
} else {
if (flags & GEOIP_CHECK_CACHE) {
if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) {
fprintf(stderr,"Error stating file %s\n",filename);
free(gi->file_path);
free(gi);
return NULL;
}
gi->mtime = buf.st_mtime;
}
gi->cache = NULL;
}
gi->flags = flags;
gi->charset = GEOIP_CHARSET_ISO_8859_1;
_setup_segments(gi);
if (flags & GEOIP_INDEX_CACHE) {
gi->index_cache = (unsigned char *) malloc(sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2)));
if (gi->index_cache != NULL) {
fseek(gi->GeoIPDatabase, 0, SEEK_SET);
if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) {
fprintf(stderr,"Error reading file %s\n",filename);
free(gi->databaseSegments);
free(gi->index_cache);
free(gi);
return NULL;
}
}
} else {
gi->index_cache = NULL;
}
return gi;
}
}
void GeoIP_delete (GeoIP *gi) {
if (gi == NULL )
return;
if (gi->GeoIPDatabase != NULL)
fclose(gi->GeoIPDatabase);
if (gi->cache != NULL) {
if ( gi->flags & GEOIP_MMAP_CACHE ) {
#if !defined(_WIN32)
munmap(gi->cache, gi->size);
#endif
} else {
free(gi->cache);
}
gi->cache = NULL;
}
if (gi->index_cache != NULL)
free(gi->index_cache);
if (gi->file_path != NULL)
free(gi->file_path);
if (gi->databaseSegments != NULL)
free(gi->databaseSegments);
free(gi);
}
const char *GeoIP_country_code_by_name (GeoIP* gi, const char *name) {
int country_id;
country_id = GeoIP_id_by_name(gi, name);
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
}
const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *name) {
int country_id;
country_id = GeoIP_id_by_name(gi, name);
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
}
const char *GeoIP_country_name_by_name (GeoIP* gi, const char *name) {
int country_id;
country_id = GeoIP_id_by_name(gi, name);
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
}
unsigned long _GeoIP_lookupaddress (const char *host) {
unsigned long addr = inet_addr(host);
struct hostent phe2;
struct hostent * phe = &phe2;
char *buf = NULL;
#ifdef HAVE_GETHOSTBYNAME_R
int buflength = 16384;
int herr = 0;
#endif
int result = 0;
#ifdef HAVE_GETHOSTBYNAME_R
buf = malloc(buflength);
#endif
if (addr == INADDR_NONE) {
#ifdef HAVE_GETHOSTBYNAME_R
while (1) {
/* we use gethostbyname_r here because it is thread-safe and gethostbyname is not */
#ifdef GETHOSTBYNAME_R_RETURNS_INT
result = gethostbyname_r(host,&phe2,buf,buflength,&phe,&herr);
#else
phe = gethostbyname_r(host,&phe2,buf,buflength,&herr);
#endif
if (herr != ERANGE)
break;
if (result == 0)
break;
/* double the buffer if the buffer is too small */
buflength = buflength * 2;
buf = realloc(buf,buflength);
}
#else
/* Some systems do not support gethostbyname_r, such as Mac OS X */
// mdw -- took this out to fix compiler warning
//phe = gethostbyname(host);
#endif
if (!phe || result != 0) {
free(buf);
return 0;
}
#if !defined(_WIN32)
addr = *((in_addr_t *) phe->h_addr_list[0]);
#else
addr = *(phe->h_addr_list[0]);
#endif
}
#ifdef HAVE_GETHOSTBYNAME_R
free(buf);
#endif
return ntohl(addr);
}
geoipv6_t
_GeoIP_lookupaddress_v6(const char *host)
{
geoipv6_t ipnum;
//int gaierr;
struct addrinfo hints, *aifirst;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
/* hints.ai_flags = AI_V4MAPPED; */
hints.ai_socktype = SOCK_STREAM;
// mdw -- took this out to fix compiler warning
// if ((gaierr = getaddrinfo(host, NULL, &hints, &aifirst)) != 0) {
// /* fprintf(stderr, "Err: %s (%d %s)\n", host, gaierr, gai_strerror(gaierr)); */
// return IPV6_NULL;
// }
memcpy(ipnum.s6_addr, ((struct sockaddr_in6 *) aifirst->ai_addr)->sin6_addr.s6_addr, sizeof(geoipv6_t));
freeaddrinfo(aifirst);
/* inet_pton(AF_INET6, host, ipnum.s6_addr); */
return ipnum;
}
int GeoIP_id_by_name (GeoIP* gi, const char *name) {
unsigned long ipnum;
int ret;
if (name == NULL) {
return 0;
}
if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) {
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
return 0;
}
if (!(ipnum = _GeoIP_lookupaddress(name)))
return 0;
ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN;
return ret;
}
int GeoIP_id_by_name_v6 (GeoIP* gi, const char *name) {
geoipv6_t ipnum;
int ret;
if (name == NULL) {
return 0;
}
if (gi->databaseType != GEOIP_COUNTRY_EDITION_V6 && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) {
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION_V6]);
return 0;
}
ipnum = _GeoIP_lookupaddress_v6(name);
if (__GEOIP_V6_IS_NULL(ipnum))
return 0;
ret = _GeoIP_seek_record_v6(gi, ipnum) - COUNTRY_BEGIN;
return ret;
}
const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr) {
int country_id;
country_id = GeoIP_id_by_addr(gi, addr);
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
}
const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr) {
int country_id;
country_id = GeoIP_id_by_addr(gi, addr);
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
}
const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) {
int country_id;
country_id = GeoIP_id_by_addr(gi, addr);
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
}
const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum) {
int country_id;
country_id = GeoIP_id_by_ipnum(gi, ipnum);
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
}
const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
int country_id;
country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
}
const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum) {
int country_id;
country_id = GeoIP_id_by_ipnum(gi, ipnum);
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
}
const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {