1Index: GeoIP.c 2=================================================================== 3RCS file: /home/maxmind/geoip/c/libGeoIP/GeoIP.c,v 4retrieving revision 1.32 5retrieving revision 1.33 6diff -u -r1.32 -r1.33 7--- GeoIP.c 25 Aug 2002 22:42:48 -0000 1.32 8+++ GeoIP.c 27 Aug 2002 06:50:02 -0000 1.33 9@@ -23,16 +23,24 @@ 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13+#ifndef _WIN32 14 #include <netdb.h> 15+#endif /* _WIN32 */ 16 #include <assert.h> 17 #include <sys/types.h> /* for fstat */ 18 #include <sys/stat.h> /* for fstat */ 19 #include "zlib.h" 20 #include "time.h" 21 22+#ifndef _WIN32 23+#include <unistd.h> 24 #include <netinet/in.h> 25 #include <arpa/inet.h> 26 #include <sys/socket.h> 27+#else 28+#include <io.h> 29+#include <windows.h> 30+#endif /* _WIN32 */ 31 32 #define COUNTRY_BEGIN 16776960; 33 const int RECORD_LENGTH = 3; 34@@ -57,9 +65,66 @@ 35 const char *GeoIPUpdateHost = "updates.maxmind.com"; 36 const char *GeoIPHTTPRequest = "GET /app/update?license_key=%s HTTP/1.0\nHost: updates.maxmind.com\n\n"; 37 38+#ifdef _WIN32 39+char * _dat_in_module_path () { 40+ HMODULE GeoIPdll; 41+ struct _stat st; 42+ int i; 43+ char * buf; 44+ 45+ buf = (char *) malloc(MAX_PATH); 46+ 47+ GeoIPdll = GetModuleHandle("GeoIP.dll"); 48+ if (!GeoIPdll) 49+ { 50+ GeoIPdll = GetModuleHandle(NULL); 51+ if (!GeoIPdll) 52+ return NULL; 53+ } 54+ GetModuleFileName(GeoIPdll, buf, MAX_PATH); 55+ for (i = strlen(buf); (i >= 0) && (buf[i] != '\\'); i--); 56+ if (i) 57+ { 58+ buf[i] = '\0'; 59+ strcat(buf, "\\"); 60+ strcat(buf, GeoIPDBFileName); 61+ if (_stat(buf, &st) == 0) 62+ return buf; 63+ } 64+ 65+ free(buf); 66+ return NULL; 67+} 68+ 69+char * _dat_path_in_regkey () { 70+ DWORD lpdwDisposition, type, size = MAX_PATH; 71+ HKEY hkGeoIP; 72+ char * buf, * filename; 73+ 74+ buf = (char *) malloc(MAX_PATH); 75+ filename = (char *) malloc(MAX_PATH); 76+ 77+ if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MaxMind\\GeoIP", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkGeoIP, &lpdwDisposition) != ERROR_SUCCESS) 78+ return NULL; 79+ if (RegQueryValueEx(hkGeoIP, "DATADIR", 0, &type, buf, &size) != ERROR_SUCCESS) 80+ strcpy(buf, "%SystemRoot%\\SYSTEM32"); 81+ if (RegSetValueEx(hkGeoIP, "DATADIR", 0, REG_EXPAND_SZ, buf, strlen(buf)) != ERROR_SUCCESS) 82+ return NULL; 83+ ExpandEnvironmentStrings(buf, filename, MAX_PATH); 84+ 85+ free(buf); 86+ strcat(filename, "\\"); 87+ strcat(filename, GeoIPDBFileName); 88+ 89+ return filename; 90+} 91+#endif /* _WIN32 */ 92+ 93 GeoIP* GeoIP_new (int flags) { 94 char * filename; 95 GeoIP * gi; 96+ 97+#ifndef _WIN32 98 filename = malloc(sizeof(char) * (strlen(DATADIR)+strlen(GeoIPDBFileName)+2)); 99 if (filename == NULL) 100 return NULL; 101@@ -67,6 +132,17 @@ 102 strcat(filename, DATADIR); 103 strcat(filename, "/"); 104 strcat(filename, GeoIPDBFileName); 105+#else 106+ filename = _dat_in_module_path(); 107+ if (filename == NULL) 108+ filename = _dat_path_in_regkey(); 109+ if (filename == NULL) 110+ { 111+ fprintf(stderr,"Unable to query registry for database location\n"); 112+ return NULL; 113+ } 114+#endif /* _WIN32 */ 115+ 116 gi = GeoIP_open (filename, flags); 117 free(filename); 118 return gi; 119@@ -96,7 +172,7 @@ 120 } 121 gi->cache = (unsigned char *) malloc(sizeof(unsigned char) * buf.st_size); 122 if (gi->cache != NULL) { 123- if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != buf.st_size) { 124+ if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (unsigned) buf.st_size) { 125 fprintf(stderr,"Error reading file %s\n",filename); 126 free(gi->cache); 127 free(gi); 128Index: GeoIP.h 129=================================================================== 130RCS file: /home/maxmind/geoip/c/libGeoIP/GeoIP.h,v 131retrieving revision 1.19 132retrieving revision 1.20 133diff -u -r1.19 -r1.20 134--- GeoIP.h 20 Aug 2002 00:52:00 -0000 1.19 135+++ GeoIP.h 27 Aug 2002 06:50:02 -0000 1.20 136@@ -45,25 +45,31 @@ 137 extern const char * GeoIP_country_name[246]; 138 extern const char * GeoIPConfFile; 139 140-GeoIP* GeoIP_new(int flags); 141-GeoIP* GeoIP_open(char * filename, int flags); 142-void GeoIP_delete(GeoIP* gi); 143-const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr); 144-const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host); 145-const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr); 146-const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host); 147-const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr); 148-const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host); 149-short int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr); 150-short int GeoIP_country_id_by_name (GeoIP* gi, const char *host); 151+#ifdef _WIN32 152+#define GEOIP_API __declspec(dllexport) 153+#else 154+#define GEOIP_API 155+#endif /* _WIN32 */ 156 157-char *GeoIP_database_info (GeoIP* gi); 158-short int GeoIP_update_database (GeoIP* gi, char * license_key, int verbose); 159+GEOIP_API GeoIP* GeoIP_new(int flags); 160+GEOIP_API GeoIP* GeoIP_open(char * filename, int flags); 161+GEOIP_API void GeoIP_delete(GeoIP* gi); 162+GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr); 163+GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host); 164+GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr); 165+GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host); 166+GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr); 167+GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host); 168+GEOIP_API short int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr); 169+GEOIP_API short int GeoIP_country_id_by_name (GeoIP* gi, const char *host); 170 171-int _seek_country (GeoIP* gi, const int offset, unsigned long ipnum, int depth); 172-unsigned long _addr_to_num (const char *addr); 173-unsigned long _h_addr_to_num (unsigned char *addr); 174-short int _is_ipaddr (const char *name); 175+GEOIP_API char *GeoIP_database_info (GeoIP* gi); 176+GEOIP_API short int GeoIP_update_database (GeoIP* gi, char * license_key, int verbose); 177+ 178+GEOIP_API int _seek_country (GeoIP* gi, const int offset, unsigned long ipnum, int depth); 179+GEOIP_API unsigned long _addr_to_num (const char *addr); 180+GEOIP_API unsigned long _h_addr_to_num (unsigned char *addr); 181 182 #ifdef __cplusplus 183 } 184