? win32.diff ? Makefile.vc ? READMEwin32static.txt ? apps/Makefile.vc ? data/GeoIP.dat ? libGeoIP/GeoIP.lib ? libGeoIP/Makefile.vc ? test/Makefile.vc Index: libGeoIP/GeoIP.c =================================================================== RCS file: /cvsroot/geoip/c/libGeoIP/GeoIP.c,v retrieving revision 1.2 diff -u -r1.2 GeoIP.c --- libGeoIP/GeoIP.c 23 Nov 2002 21:48:29 -0000 1.2 +++ libGeoIP/GeoIP.c 21 Dec 2002 19:11:33 -0000 @@ -23,7 +23,13 @@ #include #include #include + +#ifndef WIN32 #include +#else +#include +#endif /* WIN32 */ + #include #include /* for fstat */ #include /* for fstat */ @@ -48,7 +54,14 @@ "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, the Former Yugoslav Republic of","Mali","Myanmar","Mongolia","Macao","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","Puerto Rico","Palestinian Territory, Occupied","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","East Timor","Turkey","Trinidad and Tobago","Tuvalu","Taiwan, Province of China","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","Yugoslavia","South Africa","Zambia","Zaire","Zimbabwe", "Anonymous Proxy","Satellite Provider","Other"}; +#ifndef WIN32 const char *GeoIPDBFileName = GEOIPDATADIR "/GeoIP.dat"; +#else +const char *GeoIPDBFileName = "GeoIP.dat"; +#ifndef GEOIPDATADIR +#define GEOIPDATADIR "%SystemRoot%\\SYSTEM32" +#endif +#endif int _check_mtime(GeoIP *gi) { struct stat buf; @@ -198,9 +211,114 @@ return ipnum; } + +#ifdef WIN32 +char * _dat_in_module_path () { + HMODULE GeoIPdll; + struct _stat st; + int i; + char * buf; + + buf = (char *) malloc(MAX_PATH); + + GeoIPdll = GetModuleHandle("GeoIP.dll"); + if (!GeoIPdll) + { + GeoIPdll = GetModuleHandle(NULL); + if (!GeoIPdll) + return NULL; + } + GetModuleFileName(GeoIPdll, buf, MAX_PATH); + for (i = strlen(buf); (i >= 0) && (buf[i] != '\\'); i--); + if (i) + { + buf[i] = '\0'; + strcat(buf, "\\"); + strcat(buf, GeoIPDBFileName); + if (_stat(buf, &st) == 0) + return buf; + } + + free(buf); + return NULL; +} + +char * _dat_path_in_regkey () { + DWORD lpdwDisposition, type, size = MAX_PATH; + HKEY hkGeoIP; + char * buf, * filename; + + buf = (char *) malloc(MAX_PATH); + filename = (char *) malloc(MAX_PATH); + + if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MaxMind\\GeoIP", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkGeoIP, &lpdwDisposition) != ERROR_SUCCESS) + return NULL; + if (RegQueryValueEx(hkGeoIP, "DATADIR", 0, &type, buf, &size) != ERROR_SUCCESS) + strcpy(buf, GEOIPDATADIR); + if (RegSetValueEx(hkGeoIP, "DATADIR", 0, REG_EXPAND_SZ, buf, strlen(buf)) != ERROR_SUCCESS) + return NULL; + ExpandEnvironmentStrings(buf, filename, MAX_PATH); + free(buf); + strcat(filename, "\\"); + strcat(filename, GeoIPDBFileName); + + return filename; +} + +int wsastartup(void) { + WORD wVersionRequested; + WSADATA wsaData; + int err; + + wVersionRequested = MAKEWORD( 2, 2 ); + err = WSAStartup( wVersionRequested, &wsaData ); + if ( err != 0 ) { + /* Tell the user that we could not find a usable */ + /* WinSock DLL. */ + fprintf(stderr, "Could not find a usable WinSock DLL\n"); + return 0; + } + +/* Confirm that the WinSock DLL supports 2.2.*/ +/* Note that if the DLL supports versions greater */ +/* than 2.2 in addition to 2.2, it will still return */ +/* 2.2 in wVersion since that is the version we */ +/* requested. */ + + if ( LOBYTE( wsaData.wVersion ) != 2 || + HIBYTE( wsaData.wVersion ) != 2 ) { + /* Tell the user that we could not find a usable */ + /* WinSock DLL. */ + WSACleanup( ); + fprintf(stderr, "Could not find a usable WinSock DLL\n"); + return 0; + } + return 1; +} + +#endif /* WIN32 */ + + GeoIP* GeoIP_new (int flags) { GeoIP * gi; + +#ifdef WIN32 + char * filename; + + filename = _dat_in_module_path(); + if (filename == NULL) + filename = _dat_path_in_regkey(); + if (filename == NULL) { + fprintf(stderr,"Unable to query registry for database location\n"); + return NULL; + } + gi = GeoIP_open(filename, flags); + free(filename); + +#else gi = GeoIP_open (GeoIPDBFileName, flags); +#endif /* WIN32 */ + return gi; } @@ -242,6 +360,11 @@ } gi->flags = flags; _setup_segments(gi); + +#ifdef WIN32 + if (wsastartup() == 0) return NULL; +#endif + return gi; } } @@ -255,6 +378,11 @@ free(gi->file_path); if (gi->databaseSegments != NULL) free(gi->databaseSegments); + +#ifdef WIN32 + WSACleanup( ); +#endif + free(gi); } @@ -290,6 +418,7 @@ return 0; } ipnum = _h_addr_to_num((unsigned char *) host->h_addr_list[0]); + if (ipnum == 0) return 0; } ret = _seek_country(gi, ipnum) - COUNTRY_BEGIN; return ret; @@ -322,6 +451,7 @@ return 0; } ipnum = _addr_to_num(addr); + if (ipnum == 0) return 0; ret = _seek_country(gi, ipnum) - COUNTRY_BEGIN; return ret; } @@ -399,6 +529,7 @@ return 0; } ipnum = _addr_to_num(addr); + if (ipnum == 0) return 0; return _get_region(gi, ipnum); } @@ -415,6 +546,7 @@ return 0; } ipnum = _h_addr_to_num((unsigned char *) host->h_addr_list[0]); + if (ipnum == 0) return 0; } return _get_region(gi, ipnum); } Index: libGeoIP/GeoIPCity.c =================================================================== RCS file: /cvsroot/geoip/c/libGeoIP/GeoIPCity.c,v retrieving revision 1.4 diff -u -r1.4 GeoIPCity.c --- libGeoIP/GeoIPCity.c 15 Dec 2002 19:15:18 -0000 1.4 +++ libGeoIP/GeoIPCity.c 21 Dec 2002 19:11:33 -0000 @@ -20,7 +20,11 @@ #include #include +#ifndef WIN32 #include +#else +#include +#endif /* WIN32 */ const int FULL_RECORD_LENGTH = 50; @@ -108,6 +112,7 @@ return 0; } ipnum = _addr_to_num(addr); + if(ipnum == 0) return 0; return _get_record(gi, ipnum); } @@ -124,6 +129,7 @@ return 0; } ipnum = _h_addr_to_num((unsigned char *) host->h_addr_list[0]); + if(ipnum == 0) return 0; } return _get_record(gi, ipnum); } Index: test/benchmark.c =================================================================== RCS file: /cvsroot/geoip/c/test/benchmark.c,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 benchmark.c --- test/benchmark.c 21 Nov 2002 06:12:38 -0000 1.1.1.1 +++ test/benchmark.c 21 Dec 2002 19:11:33 -0000 @@ -20,7 +20,7 @@ #include -const int numIps = 70; +#define numIps 70 const int numLoops = 1000; int main () {