Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Fix GeoIP_database_info truncation issue.
Browse files Browse the repository at this point in the history
Closes #79.
  • Loading branch information
oschwald committed Jun 24, 2016
1 parent 6e18deb commit e39dc33
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 137 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ config.log
config.status
config.sub
configure
data/GeoIP.dat
depcomp
geoip.pc
install-sh
Expand Down
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.6.10 2016-XX-XX

* GeoIP_database_info now returns the full version string rather than
incorrectly truncating it. GitHub #79.
* This API is now distributed with a small test copy of GeoIP.dat rather than
a full copy.

1.6.9 2016-01-11

* Fix a regression introduced in version 1.6.8, which caused
Expand Down
9 changes: 0 additions & 9 deletions bootstrap
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#!/bin/sh

# dl the dat file if needed
DIR="$( cd "$( dirname "$0" )" && pwd )"

# download geolite database for the tests
mkdir -p $DIR/data
if [ ! -f $DIR/data/GeoIP.dat ]; then
curl http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz | gzip -d > $DIR/data/GeoIP.dat
fi

# make sure to use the installed libtool
rm -f ltmain.sh
autoreconf -fiv
Expand Down
Binary file added data/GeoIP.dat
Binary file not shown.
11 changes: 6 additions & 5 deletions libGeoIP/GeoIP.c
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ char *GeoIP_database_info(GeoIP * gi)
int i;
unsigned char buf[3];
char *retval;
int hasStructureInfo = 0;
int has_structure_info = 0;
off_t offset = gi->size - 3;
int fno;

Expand All @@ -2044,23 +2044,24 @@ char *GeoIP_database_info(GeoIP * gi)
if (pread(fno, buf, 3, offset) != 3) {
return NULL;
}
offset += 3;
if (buf[0] == 255 && buf[1] == 255 && buf[2] == 255) {
hasStructureInfo = 1;
has_structure_info = 1;
break;
}
offset -= 1;
offset -= 4;
if (offset < 0) {
return NULL;
}
}
if (hasStructureInfo == 1) {
if (has_structure_info) {
offset -= 6;
if (offset < 0) {
return NULL;
}
} else {
/* no structure info, must be pre Sep 2002 database, go back to end */
offset -= 3;
offset = gi->size - 3;
if (offset < 0) {
return NULL;
}
Expand Down
16 changes: 8 additions & 8 deletions test/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ int numipstrings = 4;
#define DATADIR "/usr/local/share/GeoIP/"
#endif

#define GEOIP_OPEN(basename,flg) \
do { \
i = GeoIP_open (DATADIR basename, \
(flg) | GEOIP_SILENCE); \
if (i == NULL) { \
#define GEOIP_OPEN(basename, flg) \
do { \
i = GeoIP_open(DATADIR basename, \
(flg) | GEOIP_SILENCE); \
if (i == NULL) { \
printf("error: %s%s does not exist\n", \
DATADIR, basename); \
return; \
/* or a 'longjmp(geo_jmp,1)'? */ \
} \
} while (0)
} \
} while (0)

#if !defined(_WIN32)
struct timeval timer_t1;
Expand Down Expand Up @@ -68,7 +68,7 @@ double timerstop()
__int64 delta; /* VC6 can't convert an unsigned int64 to to double */
GetSystemTimeAsFileTime(&timer_t2);
delta = FILETIME_TO_USEC(timer_t2) - FILETIME_TO_USEC(timer_t1);
return ((double) delta) / 1E6;
return ((double)delta) / 1E6;
}
#endif /* !defined(_WIN32) */

Expand Down
130 changes: 61 additions & 69 deletions test/country_test.txt
Original file line number Diff line number Diff line change
@@ -1,69 +1,61 @@
216.236.135.152 US USA
192.106.51.100 IT ITA
147.251.48.1 CZ CZE
203.174.65.12 JP JPN
212.208.74.140 FR FRA
200.219.192.106 BR BRA
134.102.101.18 DE DEU
193.75.148.28 BE BEL
194.244.83.2 IT ITA
203.15.106.23 AU AUS
196.31.1.1 ZA ZAF
151.28.39.114 IT ITA
151.38.70.94 IT ITA
193.56.4.124 FR FRA
195.142.146.198 TR TUR
211.232.0.0 KR KOR
211.240.0.0 KR KOR
193.194.4.0 MA MAR
139.20.112.104 DE DEU
139.20.112.3 DE DEU
145.236.125.211 HU HUN
149.225.169.61 DE DEU
151.17.191.46 IT ITA
151.24.176.194 IT ITA
151.25.8.136 IT ITA
151.26.146.192 IT ITA
151.26.153.66 IT ITA
151.26.167.71 IT ITA
151.26.35.204 IT ITA
151.26.64.157 IT ITA
151.27.138.182 IT ITA
151.28.39.114 IT ITA
151.29.150.217 IT ITA
151.29.237.39 IT ITA
151.29.73.189 IT ITA
151.30.134.242 IT ITA
151.30.135.85 IT ITA
151.30.168.224 IT ITA
151.35.80.202 IT ITA
151.35.80.240 IT ITA
151.36.191.229 IT ITA
151.38.70.94 IT ITA
151.38.92.126 IT ITA
151.42.100.132 IT ITA
151.42.169.71 IT ITA
193.56.4.124 FR FRA
195.142.146.198 TR TUR
195.142.49.205 TR TUR
202.247.74.18 JP JPN
202.247.74.71 JP JPN
202.247.74.81 JP JPN
202.247.74.88 JP JPN
203.242.239.188 KR KOR
203.174.65.12 JP JPN
212.208.74.140 FR FRA
200.219.192.106 BR BRA
202.53.254.193 ID IDN
12.168.0.0 US USA
12.169.0.0 US USA
12.200.0.0 US USA
203.121.0.8 MY MYS
203.20.231.1 AU AUS
203.87.98.29 AU AUS
203.181.121.150 JP JPN
202.166.127.246 SG SGP
62.188.202.242 GB GBR
12.12.197.23 US USA
12.12.199.3 US USA
12.12.200.79 US USA
12.87.118.0 US USA
63.147.126.103 FR FRA
64.17.254.216 US USA
65.23.121.221 US USA
65.116.3.80 US USA
66.92.181.240 US USA
67.43.156.0 A1 A1
67.43.156.64 A1 A1
67.43.156.128 A1 A1
67.43.156.192 A1 A1
69.218.251.16 US USA
70.46.123.145 US USA
71.138.104.240 US USA
72.214.6.72 GB GBR
74.0.137.194 US USA
75.77.211.208 US USA
78.26.70.208 IT ITA
81.2.69.160 GB GBR
82.99.17.96 NO NOR
83.206.36.224 FR FRA
85.88.2.224 DE DEU
89.92.212.0 FR FRA
89.92.212.32 FR FRA
89.92.212.80 FR FRA
89.92.212.94 FR FRA
89.92.212.95 FR FRA
89.92.212.96 FR FRA
89.92.212.144 FR FRA
89.92.212.224 FR FRA
89.92.212.248 FR FRA
89.92.212.252 FR FRA
89.92.213.0 FR FRA
89.92.213.16 FR FRA
89.92.213.32 FR FRA
89.92.213.44 FR FRA
89.92.213.48 FR FRA
89.92.213.64 FR FRA
89.92.213.80 FR FRA
89.92.213.88 FR FRA
89.92.213.96 FR FRA
89.92.213.104 FR FRA
89.92.213.108 FR FRA
89.92.213.110 FR FRA
89.92.213.112 FR FRA
89.92.213.166 FR FRA
89.92.213.167 FR FRA
89.92.213.168 FR FRA
89.92.213.176 FR FRA
89.92.213.192 FR FRA
89.92.213.200 FR FRA
89.92.213.224 FR FRA
89.160.20.112 SE SWE
142.217.214.0 CA CAN
143.217.214.0 VN VNM
204.128.241.208 CA CAN
208.110.217.107 US USA
216.83.171.192 US USA
216.160.83.56 US USA
222.230.136.0 JP JPN
222.230.137.0 JP JPN
223.0.0.0 FR FRA
90 changes: 61 additions & 29 deletions test/country_test2.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
212.118.5.94 JO
64.170.57.29 US
202.7.216.215 AU
212.33.164.149 SA
68.96.110.210 US
213.166.131.168 SA
64.158.191.179 US
24.247.251.23 US
203.199.228.66 IN
195.14.141.225 CY
200.52.94.98 MX
203.197.187.193 IN
203.128.9.170 PK
144.106.240.140 US
195.248.180.102 UA
213.1.0.118 GB
64.255.148.52 US
12.78.124.119 US
212.68.224.183 BE
62.148.73.85 PL
203.146.135.180 TH
209.204.179.145 US
64.123.0.164 US
202.56.198.16 IN
61.0.94.172 IN
62.42.171.190 ES
192.117.245.177 IL
213.123.75.243 GB
80.56.171.62 NL
12.87.118.0 US
63.147.126.103 FR
64.17.254.216 US
65.23.121.221 US
65.116.3.80 US
66.92.181.240 US
67.43.156.0 A1
67.43.156.64 A1
67.43.156.128 A1
67.43.156.192 A1
69.218.251.16 US
70.46.123.145 US
71.138.104.240 US
72.214.6.72 GB
74.0.137.194 US
75.77.211.208 US
78.26.70.208 IT
81.2.69.160 GB
82.99.17.96 NO
83.206.36.224 FR
85.88.2.224 DE
89.92.212.0 FR
89.92.212.32 FR
89.92.212.80 FR
89.92.212.94 FR
89.92.212.95 FR
89.92.212.96 FR
89.92.212.144 FR
89.92.212.224 FR
89.92.212.248 FR
89.92.212.252 FR
89.92.213.0 FR
89.92.213.16 FR
89.92.213.32 FR
89.92.213.44 FR
89.92.213.48 FR
89.92.213.64 FR
89.92.213.80 FR
89.92.213.88 FR
89.92.213.96 FR
89.92.213.104 FR
89.92.213.108 FR
89.92.213.110 FR
89.92.213.112 FR
89.92.213.166 FR
89.92.213.167 FR
89.92.213.168 FR
89.92.213.176 FR
89.92.213.192 FR
89.92.213.200 FR
89.92.213.224 FR
89.160.20.112 SE
142.217.214.0 CA
143.217.214.0 VN
204.128.241.208 CA
208.110.217.107 US
216.83.171.192 US
216.160.83.56 US
222.230.136.0 JP
222.230.137.0 JP
223.0.0.0 FR
1 change: 0 additions & 1 deletion test/country_test_name.txt

This file was deleted.

18 changes: 3 additions & 15 deletions test/test-geoip.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ int main()
}

db_info = GeoIP_database_info(gi);
if (!db_info || !strstr(db_info, " Copyright")) {
if (!db_info || strcmp(db_info,
"GEO-106 20160621 Build 1 Copyright (c) 2016 MaxMind Inc All Rights Reserved"))
{
fprintf(stderr, "Error reading database info (got %s).\n", db_info);
free(db_info);
failed = 1;
Expand Down Expand Up @@ -121,20 +123,6 @@ int main()
}
test_num++;
}
fclose(f);

f = fopen(SRCDIR "/test/country_test_name.txt", "r");
while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) {
returnedCountry = GeoIP_country_code_by_name(gi, ipAddress);
if (returnedCountry == NULL
|| strcmp(returnedCountry, expectedCountry) != 0) {
fprintf(stderr, "Test addr %d %s failed, got %s, expected %s\n",
test_num, ipAddress, returnedCountry, expectedCountry);
failed = 1;
}
test_num++;
}

fclose(f);
GeoIP_delete(gi);
}
Expand Down

0 comments on commit e39dc33

Please sign in to comment.