PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/geoip/GeoIP_impl.h

http://urtconnector.googlecode.com/
C Header | 252 lines | 170 code | 47 blank | 35 comment | 0 complexity | f0bd39c0281eec284548f80e23b5cd9f MD5 | raw file
Possible License(s): AGPL-3.0
  1. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
  2. /* GeoIP.h
  3. *
  4. * Copyright (C) 2006 MaxMind LLC
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef GEOIP_H
  21. #define GEOIP_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <sys/types.h>
  26. #if !defined(_WIN32)
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #else /* !defined(_WIN32) */
  31. #include <winsock2.h>
  32. #include <ws2tcpip.h>
  33. #include <windows.h>
  34. #define snprintf _snprintf
  35. #define FILETIME_TO_USEC(ft) (((unsigned __int64) ft.dwHighDateTime << 32 | ft.dwLowDateTime) / 10)
  36. #endif /* !defined(_WIN32) */
  37. #include<stdio.h>
  38. #include<stdlib.h>
  39. #include<string.h>
  40. #include <sys/types.h> /* for fstat */
  41. #include <sys/stat.h> /* for fstat */
  42. #define SEGMENT_RECORD_LENGTH 3
  43. #define STANDARD_RECORD_LENGTH 3
  44. #define ORG_RECORD_LENGTH 4
  45. #define MAX_RECORD_LENGTH 4
  46. #define NUM_DB_TYPES 20
  47. /* 128 bit address in network order */
  48. typedef struct in6_addr geoipv6_t;
  49. #define GEOIP_CHKBIT_V6(bit,ptr) (ptr[((127UL - bit) >> 3)] & (1UL << (~(127 - bit) & 7)))
  50. typedef struct GeoIPTag {
  51. FILE *GeoIPDatabase;
  52. char *file_path;
  53. unsigned char *cache;
  54. unsigned char *index_cache;
  55. unsigned int *databaseSegments;
  56. char databaseType;
  57. time_t mtime;
  58. int flags;
  59. off_t size;
  60. char record_length;
  61. int charset; /* 0 iso-8859-1 1 utf8 */
  62. int record_iter; /* used in GeoIP_next_record */
  63. int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */
  64. time_t last_mtime_check;
  65. } GeoIP;
  66. typedef enum {
  67. GEOIP_CHARSET_ISO_8859_1 = 0,
  68. GEOIP_CHARSET_UTF8 = 1
  69. } GeoIPCharset;
  70. typedef struct GeoIPRegionTag {
  71. char country_code[3];
  72. char region[3];
  73. } GeoIPRegion;
  74. typedef enum {
  75. GEOIP_STANDARD = 0,
  76. GEOIP_MEMORY_CACHE = 1,
  77. GEOIP_CHECK_CACHE = 2,
  78. GEOIP_INDEX_CACHE = 4,
  79. GEOIP_MMAP_CACHE = 8,
  80. } GeoIPOptions;
  81. typedef enum {
  82. GEOIP_COUNTRY_EDITION = 1,
  83. GEOIP_REGION_EDITION_REV0 = 7,
  84. GEOIP_CITY_EDITION_REV0 = 6,
  85. GEOIP_ORG_EDITION = 5,
  86. GEOIP_ISP_EDITION = 4,
  87. GEOIP_CITY_EDITION_REV1 = 2,
  88. GEOIP_REGION_EDITION_REV1 = 3,
  89. GEOIP_PROXY_EDITION = 8,
  90. GEOIP_ASNUM_EDITION = 9,
  91. GEOIP_NETSPEED_EDITION = 10,
  92. GEOIP_DOMAIN_EDITION = 11,
  93. GEOIP_COUNTRY_EDITION_V6 = 12,
  94. } GeoIPDBTypes;
  95. typedef enum {
  96. GEOIP_ANON_PROXY = 1,
  97. GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2,
  98. GEOIP_HTTP_CLIENT_IP_PROXY = 3,
  99. } GeoIPProxyTypes;
  100. typedef enum {
  101. GEOIP_UNKNOWN_SPEED = 0,
  102. GEOIP_DIALUP_SPEED = 1,
  103. GEOIP_CABLEDSL_SPEED = 2,
  104. GEOIP_CORPORATE_SPEED = 3,
  105. } GeoIPNetspeedValues;
  106. extern char **GeoIPDBFileName;
  107. extern const char * GeoIPDBDescription[NUM_DB_TYPES];
  108. extern const char *GeoIPCountryDBFileName;
  109. extern const char *GeoIPRegionDBFileName;
  110. extern const char *GeoIPCityDBFileName;
  111. extern const char *GeoIPOrgDBFileName;
  112. extern const char *GeoIPISPDBFileName;
  113. /* Warning: do not use those arrays as doing so may break your
  114. * program with newer GeoIP versions */
  115. extern const char GeoIP_country_code[253][3];
  116. extern const char GeoIP_country_code3[253][4];
  117. extern const char * GeoIP_country_name[253];
  118. extern const char GeoIP_country_continent[253][3];
  119. #ifdef DLL
  120. #define GEOIP_API __declspec(dllexport)
  121. #else
  122. #define GEOIP_API
  123. #endif /* DLL */
  124. GEOIP_API void GeoIP_setup_custom_directory(char *dir);
  125. GEOIP_API GeoIP* GeoIP_open_type (int type, int flags);
  126. GEOIP_API GeoIP* GeoIP_new(int flags);
  127. GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags);
  128. GEOIP_API int GeoIP_db_avail(int type);
  129. GEOIP_API void GeoIP_delete(GeoIP* gi);
  130. GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr);
  131. GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host);
  132. GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr);
  133. GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host);
  134. GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr);
  135. GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host);
  136. GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
  137. GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum);
  138. GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum);
  139. /* */
  140. GEOIP_API const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  141. GEOIP_API const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  142. GEOIP_API const char *GeoIP_country_code3_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  143. /* Deprecated - for backwards compatibility only */
  144. GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr);
  145. GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host);
  146. GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr);
  147. GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host);
  148. GEOIP_API char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum);
  149. GEOIP_API char *GeoIP_org_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  150. GEOIP_API char *GeoIP_org_by_addr_v6 (GeoIP* gi, const char *addr);
  151. GEOIP_API char *GeoIP_org_by_name_v6 (GeoIP* gi, const char *name);
  152. /* End deprecated */
  153. GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr);
  154. GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host);
  155. GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum);
  156. GEOIP_API int GeoIP_id_by_addr_v6 (GeoIP* gi, const char *addr);
  157. GEOIP_API int GeoIP_id_by_name_v6 (GeoIP* gi, const char *host);
  158. GEOIP_API int GeoIP_id_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  159. GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr);
  160. GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host);
  161. GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum);
  162. GEOIP_API GeoIPRegion * GeoIP_region_by_addr_v6 (GeoIP* gi, const char *addr);
  163. GEOIP_API GeoIPRegion * GeoIP_region_by_name_v6 (GeoIP* gi, const char *host);
  164. GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum_v6 (GeoIP *gi, geoipv6_t ipnum);
  165. /* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
  166. GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
  167. GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir);
  168. GEOIP_API void GeoIP_assign_region_by_inetaddr_v6(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *gir);
  169. /* Used to query GeoIP Organization, ISP and AS Number databases */
  170. GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum);
  171. GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr);
  172. GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
  173. GEOIP_API char *GeoIP_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum);
  174. GEOIP_API char *GeoIP_name_by_addr_v6 (GeoIP* gi, const char *addr);
  175. GEOIP_API char *GeoIP_name_by_name_v6 (GeoIP* gi, const char *name);
  176. /** return two letter country code */
  177. GEOIP_API const char* GeoIP_code_by_id(int id);
  178. /** return three letter country code */
  179. GEOIP_API const char* GeoIP_code3_by_id(int id);
  180. /** return full name of country */
  181. GEOIP_API const char* GeoIP_name_by_id(int id);
  182. /** return continent of country */
  183. GEOIP_API const char* GeoIP_continent_by_id(int id);
  184. /** return id by country code **/
  185. GEOIP_API int GeoIP_id_by_code(const char *country);
  186. /** return return number of known countries */
  187. GEOIP_API unsigned GeoIP_num_countries(void);
  188. GEOIP_API char *GeoIP_database_info (GeoIP* gi);
  189. GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
  190. GEOIP_API int GeoIP_charset (GeoIP* gi);
  191. GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset);
  192. GEOIP_API int GeoIP_last_netmask (GeoIP* gi);
  193. GEOIP_API char **GeoIP_range_by_ip (GeoIP* gi, const char *addr);
  194. GEOIP_API void GeoIP_range_by_ip_delete(char **ptr);
  195. /* Convert region code to region name */
  196. GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code);
  197. /* Get timezone from country and region code */
  198. GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code);
  199. #ifdef BSD
  200. #define memcpy(dest, src, n) bcopy(src, dest, n)
  201. #endif
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* GEOIP_H */