GeoIP
1.6.12
About: GeoIP is a C library that enables the user to find the country that any IP address or hostname originates from (contains an initial free GeoIP Country database). Fossies Dox: GeoIP-1.6.12.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation) 
|
Go to the documentation of this file. 30 #if _MSC_VER < 1900 // VS 2015 supports snprintf 31 #define snprintf _snprintf 33 #if _MSC_VER >= 1400 // VS 2005+ deprecates fileno, lseek and read 34 #define fileno _fileno 51 #include <sys/types.h> 53 #ifdef HAVE_GETTIMEOFDAY 61 #if defined(_WIN32) && !defined(__MINGW32__) 66 #elif defined(__GNUC__) 67 #define _UNUSED __attribute__((unused)) 73 #define INADDR_NONE -1 76 #define COUNTRY_BEGIN 16776960 77 #define LARGE_COUNTRY_BEGIN 16515072 78 #define STATE_BEGIN_REV0 16700000 79 #define STATE_BEGIN_REV1 16000000 80 #define STRUCTURE_INFO_MAX_SIZE 20 81 #define DATABASE_INFO_MAX_SIZE 100 82 #define MAX_ORG_RECORD_LENGTH 300 84 #define CANADA_OFFSET 677 85 #define WORLD_OFFSET 1353 86 #define FIPS_RANGE 360 88 #define DEBUG_MSGF(flags, fmt, ...) \ 90 if (((flags)&GEOIP_SILENCE) == 0) { \ 91 fprintf(stderr, fmt, ##__VA_ARGS__); \ 96 #define pread(fd, buf, count, offset) \ 97 (lseek(fd, offset, SEEK_SET) == offset ? read(fd, buf, count) : -1) 101 "--",
"AP",
"EU",
"AD",
"AE",
"AF",
"AG",
"AI",
"AL",
"AM",
"CW",
"AO",
102 "AQ",
"AR",
"AS",
"AT",
"AU",
"AW",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
103 "BG",
"BH",
"BI",
"BJ",
"BM",
"BN",
"BO",
"BR",
"BS",
"BT",
"BV",
"BW",
104 "BY",
"BZ",
"CA",
"CC",
"CD",
"CF",
"CG",
"CH",
"CI",
"CK",
"CL",
"CM",
105 "CN",
"CO",
"CR",
"CU",
"CV",
"CX",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DM",
106 "DO",
"DZ",
"EC",
"EE",
"EG",
"EH",
"ER",
"ES",
"ET",
"FI",
"FJ",
"FK",
107 "FM",
"FO",
"FR",
"SX",
"GA",
"GB",
"GD",
"GE",
"GF",
"GH",
"GI",
"GL",
108 "GM",
"GN",
"GP",
"GQ",
"GR",
"GS",
"GT",
"GU",
"GW",
"GY",
"HK",
"HM",
109 "HN",
"HR",
"HT",
"HU",
"ID",
"IE",
"IL",
"IN",
"IO",
"IQ",
"IR",
"IS",
110 "IT",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KP",
"KR",
111 "KW",
"KY",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
112 "LV",
"LY",
"MA",
"MC",
"MD",
"MG",
"MH",
"MK",
"ML",
"MM",
"MN",
"MO",
113 "MP",
"MQ",
"MR",
"MS",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
114 "NC",
"NE",
"NF",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NU",
"NZ",
"OM",
115 "PA",
"PE",
"PF",
"PG",
"PH",
"PK",
"PL",
"PM",
"PN",
"PR",
"PS",
"PT",
116 "PW",
"PY",
"QA",
"RE",
"RO",
"RU",
"RW",
"SA",
"SB",
"SC",
"SD",
"SE",
117 "SG",
"SH",
"SI",
"SJ",
"SK",
"SL",
"SM",
"SN",
"SO",
"SR",
"ST",
"SV",
118 "SY",
"SZ",
"TC",
"TD",
"TF",
"TG",
"TH",
"TJ",
"TK",
"TM",
"TN",
"TO",
119 "TL",
"TR",
"TT",
"TV",
"TW",
"TZ",
"UA",
"UG",
"UM",
"US",
"UY",
"UZ",
120 "VA",
"VC",
"VE",
"VG",
"VI",
"VN",
"VU",
"WF",
"WS",
"YE",
"YT",
"RS",
121 "ZA",
"ZM",
"ME",
"ZW",
"A1",
"A2",
"O1",
"AX",
"GG",
"IM",
"JE",
"BL",
122 "MF",
"BQ",
"SS",
"O1"};
128 "--",
"AP",
"EU",
"AND",
"ARE",
"AFG",
"ATG",
"AIA",
"ALB",
"ARM",
"CUW",
129 "AGO",
"ATA",
"ARG",
"ASM",
"AUT",
"AUS",
"ABW",
"AZE",
"BIH",
"BRB",
"BGD",
130 "BEL",
"BFA",
"BGR",
"BHR",
"BDI",
"BEN",
"BMU",
"BRN",
"BOL",
"BRA",
"BHS",
131 "BTN",
"BVT",
"BWA",
"BLR",
"BLZ",
"CAN",
"CCK",
"COD",
"CAF",
"COG",
"CHE",
132 "CIV",
"COK",
"CHL",
"CMR",
"CHN",
"COL",
"CRI",
"CUB",
"CPV",
"CXR",
"CYP",
133 "CZE",
"DEU",
"DJI",
"DNK",
"DMA",
"DOM",
"DZA",
"ECU",
"EST",
"EGY",
"ESH",
134 "ERI",
"ESP",
"ETH",
"FIN",
"FJI",
"FLK",
"FSM",
"FRO",
"FRA",
"SXM",
"GAB",
135 "GBR",
"GRD",
"GEO",
"GUF",
"GHA",
"GIB",
"GRL",
"GMB",
"GIN",
"GLP",
"GNQ",
136 "GRC",
"SGS",
"GTM",
"GUM",
"GNB",
"GUY",
"HKG",
"HMD",
"HND",
"HRV",
"HTI",
137 "HUN",
"IDN",
"IRL",
"ISR",
"IND",
"IOT",
"IRQ",
"IRN",
"ISL",
"ITA",
"JAM",
138 "JOR",
"JPN",
"KEN",
"KGZ",
"KHM",
"KIR",
"COM",
"KNA",
"PRK",
"KOR",
"KWT",
139 "CYM",
"KAZ",
"LAO",
"LBN",
"LCA",
"LIE",
"LKA",
"LBR",
"LSO",
"LTU",
"LUX",
140 "LVA",
"LBY",
"MAR",
"MCO",
"MDA",
"MDG",
"MHL",
"MKD",
"MLI",
"MMR",
"MNG",
141 "MAC",
"MNP",
"MTQ",
"MRT",
"MSR",
"MLT",
"MUS",
"MDV",
"MWI",
"MEX",
"MYS",
142 "MOZ",
"NAM",
"NCL",
"NER",
"NFK",
"NGA",
"NIC",
"NLD",
"NOR",
"NPL",
"NRU",
143 "NIU",
"NZL",
"OMN",
"PAN",
"PER",
"PYF",
"PNG",
"PHL",
"PAK",
"POL",
"SPM",
144 "PCN",
"PRI",
"PSE",
"PRT",
"PLW",
"PRY",
"QAT",
"REU",
"ROU",
"RUS",
"RWA",
145 "SAU",
"SLB",
"SYC",
"SDN",
"SWE",
"SGP",
"SHN",
"SVN",
"SJM",
"SVK",
"SLE",
146 "SMR",
"SEN",
"SOM",
"SUR",
"STP",
"SLV",
"SYR",
"SWZ",
"TCA",
"TCD",
"ATF",
147 "TGO",
"THA",
"TJK",
"TKL",
"TKM",
"TUN",
"TON",
"TLS",
"TUR",
"TTO",
"TUV",
148 "TWN",
"TZA",
"UKR",
"UGA",
"UMI",
"USA",
"URY",
"UZB",
"VAT",
"VCT",
"VEN",
149 "VGB",
"VIR",
"VNM",
"VUT",
"WLF",
"WSM",
"YEM",
"MYT",
"SRB",
"ZAF",
"ZMB",
150 "MNE",
"ZWE",
"A1",
"A2",
"O1",
"ALA",
"GGY",
"IMN",
"JEY",
"BLM",
"MAF",
155 "Asia/Pacific Region",
158 "United Arab Emirates",
160 "Antigua and Barbuda",
175 "Bosnia and Herzegovina",
195 "Cocos (Keeling) Islands",
196 "Congo, The Democratic Republic of the",
197 "Central African Republic",
216 "Dominican Republic",
227 "Falkland Islands (Malvinas)",
228 "Micronesia, Federated States of",
231 "Sint Maarten (Dutch part)",
245 "South Georgia and the South Sandwich Islands",
251 "Heard Island and McDonald Islands",
260 "British Indian Ocean Territory",
262 "Iran, Islamic Republic of",
273 "Saint Kitts and Nevis",
274 "Korea, Democratic People's Republic of",
275 "Korea, Republic of",
279 "Lao People's Democratic Republic",
292 "Moldova, Republic of",
300 "Northern Mariana Islands",
331 "Saint Pierre and Miquelon",
334 "Palestinian Territory",
341 "Russian Federation",
351 "Svalbard and Jan Mayen",
358 "Sao Tome and Principe",
360 "Syrian Arab Republic",
362 "Turks and Caicos Islands",
364 "French Southern Territories",
374 "Trinidad and Tobago",
377 "Tanzania, United Republic of",
380 "United States Minor Outlying Islands",
384 "Holy See (Vatican City State)",
385 "Saint Vincent and the Grenadines",
387 "Virgin Islands, British",
388 "Virgin Islands, U.S.",
401 "Satellite Provider",
409 "Bonaire, Saint Eustatius and Saba",
415 "Asia/Pacific Region",
418 "United Arab Emirates",
420 "Antigua and Barbuda",
433 "Bosnia and Herzegovina",
453 "Cocos (Keeling) Islands",
454 "Congo, The Democratic Republic of the",
455 "Central African Republic",
474 "Dominican Republic",
485 "Falkland Islands (Malvinas)",
486 "Micronesia, Federated States of",
489 "Sint Maarten (Dutch part)",
503 "South Georgia and the South Sandwich Islands",
509 "Heard Island and McDonald Islands",
518 "British Indian Ocean Territory",
520 "Iran, Islamic Republic of",
531 "Saint Kitts and Nevis",
532 "Korea, Democratic People's Republic of",
533 "Korea, Republic of",
537 "Lao People's Democratic Republic",
550 "Moldova, Republic of",
558 "Northern Mariana Islands",
589 "Saint Pierre and Miquelon",
592 "Palestinian Territory",
599 "Russian Federation",
609 "Svalbard and Jan Mayen",
616 "Sao Tome and Principe",
618 "Syrian Arab Republic",
620 "Turks and Caicos Islands",
622 "French Southern Territories",
632 "Trinidad and Tobago",
635 "Tanzania, United Republic of",
638 "United States Minor Outlying Islands",
642 "Holy See (Vatican City State)",
643 "Saint Vincent and the Grenadines",
645 "Virgin Islands, British",
646 "Virgin Islands, U.S.",
659 "Satellite Provider",
667 "Bonaire, Saint Eustatius and Saba",
676 "--",
"AS",
"EU",
"EU",
"AS",
"AS",
"NA",
"NA",
"EU",
"AS",
"NA",
"AF",
677 "AN",
"SA",
"OC",
"EU",
"OC",
"NA",
"AS",
"EU",
"NA",
"AS",
"EU",
"AF",
678 "EU",
"AS",
"AF",
"AF",
"NA",
"AS",
"SA",
"SA",
"NA",
"AS",
"AN",
"AF",
679 "EU",
"NA",
"NA",
"AS",
"AF",
"AF",
"AF",
"EU",
"AF",
"OC",
"SA",
"AF",
680 "AS",
"SA",
"NA",
"NA",
"AF",
"AS",
"AS",
"EU",
"EU",
"AF",
"EU",
"NA",
681 "NA",
"AF",
"SA",
"EU",
"AF",
"AF",
"AF",
"EU",
"AF",
"EU",
"OC",
"SA",
682 "OC",
"EU",
"EU",
"NA",
"AF",
"EU",
"NA",
"AS",
"SA",
"AF",
"EU",
"NA",
683 "AF",
"AF",
"NA",
"AF",
"EU",
"AN",
"NA",
"OC",
"AF",
"SA",
"AS",
"AN",
684 "NA",
"EU",
"NA",
"EU",
"AS",
"EU",
"AS",
"AS",
"AS",
"AS",
"AS",
"EU",
685 "EU",
"NA",
"AS",
"AS",
"AF",
"AS",
"AS",
"OC",
"AF",
"NA",
"AS",
"AS",
686 "AS",
"NA",
"AS",
"AS",
"AS",
"NA",
"EU",
"AS",
"AF",
"AF",
"EU",
"EU",
687 "EU",
"AF",
"AF",
"EU",
"EU",
"AF",
"OC",
"EU",
"AF",
"AS",
"AS",
"AS",
688 "OC",
"NA",
"AF",
"NA",
"EU",
"AF",
"AS",
"AF",
"NA",
"AS",
"AF",
"AF",
689 "OC",
"AF",
"OC",
"AF",
"NA",
"EU",
"EU",
"AS",
"OC",
"OC",
"OC",
"AS",
690 "NA",
"SA",
"OC",
"OC",
"AS",
"AS",
"EU",
"NA",
"OC",
"NA",
"AS",
"EU",
691 "OC",
"SA",
"AS",
"AF",
"EU",
"EU",
"AF",
"AS",
"OC",
"AF",
"AF",
"EU",
692 "AS",
"AF",
"EU",
"EU",
"EU",
"AF",
"EU",
"AF",
"AF",
"SA",
"AF",
"NA",
693 "AS",
"AF",
"NA",
"AF",
"AN",
"AF",
"AS",
"AS",
"OC",
"AS",
"AF",
"OC",
694 "AS",
"EU",
"NA",
"OC",
"AS",
"AF",
"EU",
"AF",
"OC",
"NA",
"SA",
"AS",
695 "EU",
"NA",
"SA",
"NA",
"NA",
"AS",
"OC",
"OC",
"OC",
"AS",
"AF",
"EU",
696 "AF",
"AF",
"EU",
"AF",
"--",
"--",
"--",
"EU",
"EU",
"EU",
"EU",
"NA",
697 "NA",
"NA",
"AF",
"--"};
705 return ptr == NULL ?
"Unknown" : ptr;
715 struct sockaddr_in in;
716 memset(&in, 0,
sizeof(in));
717 in.sin_family = AF_INET;
718 memcpy(&in.sin_addr, src,
sizeof(
struct in_addr));
719 getnameinfo((
struct sockaddr *)&in,
720 sizeof(
struct sockaddr_in),
727 }
else if (af == AF_INET6) {
728 struct sockaddr_in6 in;
729 memset(&in, 0,
sizeof(in));
730 in.sin6_family = AF_INET6;
731 memcpy(&in.sin6_addr, src,
sizeof(
struct in_addr6));
732 getnameinfo((
struct sockaddr *)&in,
733 sizeof(
struct sockaddr_in6),
745 struct addrinfo hints, *res, *ressave;
747 memset(&hints, 0,
sizeof(
struct addrinfo));
748 hints.ai_family = af;
750 if (getaddrinfo(src, NULL, &hints, &res) != 0) {
757 memcpy(dst, res->ai_addr, res->ai_addrlen);
761 freeaddrinfo(ressave);
766 return inet_pton(af, src, dst);
770 return inet_ntop(af, src, dst, cnt);
776 for (i = 0; i < 16; i++) {
786 if ((v6->s6_addr[0]) != 0x20) {
789 if ((v6->s6_addr[1]) != 0x01) {
792 if ((v6->s6_addr[2]) != 0x00) {
795 if ((v6->s6_addr[3]) != 0x00) {
799 for (i = 0; i < 12; i++) {
802 for (; i < 16; i++) {
803 v6->s6_addr[i] ^= 0xff;
809 "GeoIP Country Edition",
810 "GeoIP City Edition, Rev 1",
811 "GeoIP Region Edition, Rev 1",
813 "GeoIP Organization Edition",
814 "GeoIP City Edition, Rev 0",
815 "GeoIP Region Edition, Rev 0",
816 "GeoIP Proxy Edition",
817 "GeoIP ASNum Edition",
818 "GeoIP Netspeed Edition",
819 "GeoIP Domain Name Edition",
820 "GeoIP Country V6 Edition",
821 "GeoIP LocationID ASCII Edition",
822 "GeoIP Accuracy Radius Edition",
825 "GeoIP Large Country Edition",
826 "GeoIP Large Country V6 Edition",
829 "GeoIP ASNum V6 Edition",
830 "GeoIP ISP V6 Edition",
831 "GeoIP Organization V6 Edition",
832 "GeoIP Domain Name V6 Edition",
833 "GeoIP LocationID ASCII V6 Edition",
834 "GeoIP Registrar Edition",
835 "GeoIP Registrar V6 Edition",
836 "GeoIP UserType Edition",
837 "GeoIP UserType V6 Edition",
838 "GeoIP City Edition V6, Rev 1",
839 "GeoIP City Edition V6, Rev 0",
840 "GeoIP Netspeed Edition, Rev 1",
841 "GeoIP Netspeed Edition V6, Rev1",
842 "GeoIP Country Confidence Edition",
843 "GeoIP City Confidence Edition",
844 "GeoIP Region Confidence Edition",
845 "GeoIP Postal Confidence Edition",
846 "GeoIP Accuracy Radius Edition V6"};
854 char *path = malloc(
sizeof(
char) * 1024);
858 memset(path, 0,
sizeof(
char) * 1024);
860 path,
sizeof(
char) * 1024 - 1,
"%s/%s", GEOIPDATADIR, file_name);
862 char buf[MAX_PATH], *p, *q = NULL;
863 memset(buf, 0,
sizeof(buf));
864 len = GetModuleFileNameA(GetModuleHandle(NULL), buf,
sizeof(buf) - 1);
865 for (p = buf + len; p > buf; p--) {
875 memset(path, 0,
sizeof(
char) * 1024);
876 snprintf(path,
sizeof(
char) * 1024 - 1,
"%s/%s", buf, file_name);
882 sizeof(
char) * 1024 - 1,
888 sizeof(
char) * 1024 - 1,
976 struct stat file_stat;
977 return (stat(file_name, &file_stat) == 0) ? 1 : 0;
984 char *t = (
char *)iso;
995 while ((c = *iso++)) {
1012 return ((ipnum >= 167772160U && ipnum <= 184549375U) ||
1013 (ipnum >= 2851995648U && ipnum <= 2852061183U) ||
1014 (ipnum >= 2886729728U && ipnum <= 2887778303U) ||
1015 (ipnum >= 3232235520U && ipnum <= 3232301055U) ||
1016 (ipnum >= 2130706432U && ipnum <= 2147483647U))
1027 const char *filePath;
1033 if (NULL == filePath) {
1054 unsigned int segment;
1057 return buf->st_size;
1070 if (index_size > buf->st_size) {
1078 int i, j, segment_record_length;
1079 unsigned char delim[3];
1081 off_t offset = gi->
size - 3;
1091 if (
pread(fno, delim, 3, offset) != 3) {
1095 if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) {
1154 if (
pread(fno, buf, segment_record_length, offset) !=
1155 segment_record_length) {
1161 for (j = 0; j < segment_record_length; j++) {
1207 #if !defined(_WIN32) 1216 #if !defined(_WIN32) 1219 if (0 != gettimeofday(&t, NULL)) {
1221 gi->
flags,
"Error calling gettimeofday: %s\n", strerror(errno));
1234 GetSystemTimeAsFileTime(&ft);
1235 t = FILETIME_TO_USEC(ft) / 1000 / 1000;
1248 if (buf.st_mtime != gi->
mtime &&
1253 #if !defined(_WIN32) 1260 if ((gi->
cache = (
unsigned char *)realloc(
1261 gi->
cache, buf.st_size)) == NULL) {
1263 "Out of memory when reloading %s\n",
1274 "Error Opening file %s when reloading\n",
1278 gi->
mtime = buf.st_mtime;
1279 gi->
size = buf.st_size;
1284 "GEOIP_MMAP_CACHE is not supported on WIN32\n");
1288 gi->
cache = mmap(NULL,
1294 if (gi->
cache == MAP_FAILED) {
1297 "Error remapping file %s when reloading\n",
1310 "Error reading file %s when reloading\n",
1323 "Error reading file %s -- corrupt\n",
1337 gi->
index_cache,
sizeof(
unsigned char) * idx_size);
1345 "Error reading file %s where reloading\n",
1357 #define ADDR_STR_LEN (8 * 4 + 7 + 1) 1364 const unsigned char *buf = (gi->
cache == NULL) ? stack_buffer : NULL;
1365 unsigned int offset = 0;
1367 const unsigned char *p;
1377 for (depth = 127; depth >= 0; depth--) {
1378 unsigned int byte_offset = record_pair_length * offset;
1379 if (byte_offset > gi->
size - record_pair_length) {
1386 fno, stack_buffer, record_pair_length, (
long)byte_offset) !=
1387 record_pair_length) {
1392 buf = gi->
cache + (long)byte_offset;
1402 x = (buf[3 * 1 + 0] << (0 * 8)) + (buf[3 * 1 + 1] << (1 * 8)) +
1403 (buf[3 * 1 + 2] << (2 * 8));
1421 x = (buf[3 * 0 + 0] << (0 * 8)) + (buf[3 * 0 + 1] << (1 * 8)) +
1422 (buf[3 * 0 + 2] << (2 * 8));
1445 "Error Traversing Database for ipnum = %s - Perhaps database is " 1464 const unsigned char *buf = (gi->
cache == NULL) ? stack_buffer : NULL;
1465 unsigned int offset = 0;
1467 const unsigned char *p;
1474 for (depth = 31; depth >= 0; depth--) {
1475 unsigned int byte_offset = record_pair_length * offset;
1476 if (byte_offset > gi->
size - record_pair_length) {
1482 if (
pread(fno, stack_buffer, record_pair_length, byte_offset) !=
1483 record_pair_length) {
1488 buf = gi->
cache + (long)byte_offset;
1493 if (ipnum & (1 << depth)) {
1498 x = (buf[3 * 1 + 0] << (0 * 8)) + (buf[3 * 1 + 1] << (1 * 8)) +
1499 (buf[3 * 1 + 2] << (2 * 8));
1517 x = (buf[3 * 0 + 0] << (0 * 8)) + (buf[3 * 0 + 1] << (1 * 8)) +
1518 (buf[3 * 0 + 2] << (2 * 8));
1539 "Error Traversing Database for ipnum = %lu - Perhaps database " 1546 unsigned int c, octet, t;
1547 unsigned long ipnum;
1551 while ((c = *addr++)) {
1572 if ((octet > 255) || (i != 0)) {
1576 return ipnum + octet;
1581 const char *filePath;
1583 printf(
"Invalid database type %d\n", type);
1588 if (filePath == NULL) {
1589 printf(
"Invalid database type %d\n", type);
1597 if (database_type > 105) {
1598 database_type -= 105;
1631 len =
sizeof(char) * (strlen(filename) + 1);
1640 DEBUG_MSGF(flags,
"Error Opening file %s\n", filename);
1646 DEBUG_MSGF(flags,
"Error stating file %s\n", filename);
1651 gi->
size = buf.st_size;
1653 gi->
mtime = buf.st_mtime;
1657 #if !defined(_WIN32) 1658 gi->
cache = mmap(NULL,
1664 if (gi->
cache == MAP_FAILED) {
1665 DEBUG_MSGF(flags,
"Error mmaping file %s\n", filename);
1672 (
unsigned char *)malloc(
sizeof(
unsigned char) * buf.st_size);
1674 if (gi->
cache != NULL) {
1678 DEBUG_MSGF(flags,
"Error reading file %s\n", filename);
1687 DEBUG_MSGF(flags,
"Error stating file %s\n", filename);
1691 gi->
mtime = buf.st_mtime;
1716 (
unsigned char *)malloc(
sizeof(
unsigned char) * idx_size);
1742 if (gi->
cache != NULL) {
1744 #if !defined(_WIN32) 1804 unsigned long addr = inet_addr(host);
1805 struct hostent phe2;
1806 struct hostent *phe = &phe2;
1808 #ifdef HAVE_GETHOSTBYNAME_R 1809 int buflength = 16384;
1813 #ifdef HAVE_GETHOSTBYNAME_R 1814 buf = malloc(buflength);
1817 #ifdef HAVE_GETHOSTBYNAME_R 1821 #ifdef GETHOSTBYNAME_R_RETURNS_INT 1822 result = gethostbyname_r(host, &phe2, buf, buflength, &phe, &herr);
1824 phe = gethostbyname_r(host, &phe2, buf, buflength, &herr);
1826 if (herr != ERANGE) {
1833 buflength = buflength * 2;
1834 buf = realloc(buf, buflength);
1838 phe = gethostbyname(host);
1840 if (!phe || result != 0) {
1844 #if !defined(_WIN32) 1845 addr = *((in_addr_t *)phe->h_addr_list[0]);
1847 addr = ((IN_ADDR *)phe->h_addr_list[0])->S_un.S_addr;
1850 #ifdef HAVE_GETHOSTBYNAME_R 1859 struct addrinfo hints, *aifirst;
1861 memset(&hints, 0,
sizeof(hints));
1862 hints.ai_family = AF_INET6;
1864 hints.ai_socktype = SOCK_STREAM;
1866 if ((gaierr = getaddrinfo(host, NULL, &hints, &aifirst)) != 0) {
1871 memcpy(ipnum.s6_addr,
1872 ((
struct sockaddr_in6 *)aifirst->ai_addr)->sin6_addr.s6_addr,
1874 freeaddrinfo(aifirst);
1881 unsigned long ipnum;
1890 printf(
"Invalid database type %s, expected %s\n",
1910 printf(
"Invalid database type %s, expected %s\n",
1967 unsigned long ipnum,
1982 unsigned long ipnum,
1997 unsigned long ipnum,
2040 printf(
"Invalid database type %s, expected %s\n",
2051 unsigned long ipnum;
2060 printf(
"Invalid database type %s, expected %s\n",
2074 printf(
"Invalid database type %s, expected %s\n",
2092 printf(
"Invalid database type %s, expected %s\n",
2103 unsigned char buf[3];
2105 int has_structure_info = 0;
2106 off_t offset = gi->
size - 3;
2119 if (
pread(fno, buf, 3, offset) != 3) {
2123 if (buf[0] == 255 && buf[1] == 255 && buf[2] == 255) {
2124 has_structure_info = 1;
2132 if (has_structure_info) {
2139 offset = gi->
size - 3;
2146 if (
pread(fno, buf, 3, offset) != 3) {
2150 if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) {
2151 retval = malloc(
sizeof(
char) * (i + 1));
2152 if (retval == NULL) {
2155 if (
pread(fno, retval, i, offset) != i) {
2172 unsigned long inetaddr,
2175 unsigned int seek_region;
2186 if (seek_region >= 1000) {
2189 region->
region[0] = (char)((seek_region - 1000) / 26 + 65);
2190 region->
region[1] = (char)((seek_region - 1000) % 26 + 65);
2231 unsigned int seek_region;
2242 if (seek_region >= 1000) {
2245 region->
region[0] = (char)((seek_region - 1000) / 26 + 65);
2246 region->
region[1] = (char)((seek_region - 1000) % 26 + 65);
2308 unsigned long ipnum;
2314 printf(
"Invalid database type %s, expected %s\n",
2331 printf(
"Invalid database type %s, expected %s\n",
2342 unsigned long ipnum;
2348 printf(
"Invalid database type %s, expected %s\n",
2367 printf(
"Invalid database type %s, expected %s\n",
2384 printf(
"Invalid database type %s, expected %s\n",
2396 printf(
"Invalid database type %s, expected %s\n",
2408 unsigned int seek_org;
2410 char *org_buf, *buf_pointer;
2427 printf(
"Invalid database type %s, expected %s\n",
2441 if (gi->
cache == NULL) {
2445 record_pointer) == -1) {
2451 len =
sizeof(char) * (strlen(buf) + 1);
2452 org_buf = malloc(len);
2453 strncpy(org_buf, buf, len);
2456 buf_pointer = (
char *)(gi->
cache + (
long)record_pointer);
2460 len =
sizeof(char) * (strlen(buf_pointer) + 1);
2461 org_buf = malloc(len);
2462 strncpy(org_buf, buf_pointer, len);
2469 unsigned int seek_org;
2471 char *org_buf, *buf_pointer;
2484 printf(
"Invalid database type %s, expected %s\n",
2498 if (gi->
cache == NULL) {
2502 record_pointer) == -1) {
2509 len =
sizeof(char) * (strlen(buf) + 1);
2510 org_buf = malloc(len);
2511 strncpy(org_buf, buf, len);
2514 buf_pointer = (
char *)(gi->
cache + (
long)record_pointer);
2518 len =
sizeof(char) * (strlen(buf_pointer) + 1);
2519 org_buf = malloc(len);
2520 strncpy(org_buf, buf_pointer, len);
2530 int num_chars_written, i;
2532 ret_str = malloc(
sizeof(
char) * 16);
2535 for (i = 0; i < 4; i++) {
2536 octet[3 - i] = ipnum % 256;
2540 for (i = 0; i < 4; i++) {
2541 num_chars_written = sprintf(cur_str,
"%d", octet[i]);
2542 cur_str += num_chars_written;
2554 unsigned long ipnum;
2555 unsigned long left_seek;
2556 unsigned long right_seek;
2559 unsigned int target_value;
2567 ret = malloc(
sizeof(
char *) * 2);
2572 mask = 0xffffffff << (32 - orig_netmask);
2573 left_seek = ipnum & mask;
2574 right_seek = left_seek + (0xffffffff & ~mask);
2576 while (left_seek != 0 &&
2580 mask = 0xffffffff << (32 - t.
netmask);
2581 left_seek = (left_seek - 1) & mask;
2585 while (right_seek != 0xffffffff &&
2589 mask = 0xffffffff << (32 - t.
netmask);
2590 right_seek = (right_seek + 1) & mask;
2591 right_seek += 0xffffffff & ~mask;
2620 unsigned long ipnum;
2638 unsigned long ipnum;
2680 int old_charset = gi->
charset;
2754 if (tmpGeoIPDBFileName) {
2756 if (tmpGeoIPDBFileName[i]) {
2757 free(tmpGeoIPDBFileName[i]);
2761 free(tmpGeoIPDBFileName);
unsigned int * databaseSegments
const char * GeoIP_country_name[256]
void GeoIP_assign_region_by_inetaddr_gl(GeoIP *gi, unsigned long inetaddr, GeoIPRegion *region, GeoIPLookup *gl)
static const unsigned num_GeoIP_countries
#define MAX_ORG_RECORD_LENGTH
const char * GeoIP_code_by_id(int id)
const char * GeoIP_country_name_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char GeoIP_country_continent[256][3]
int GeoIP_country_id_by_name_v6_gl(GeoIP *gi, const char *host, GeoIPLookup *gl)
#define STRUCTURE_INFO_MAX_SIZE
char * GeoIP_name_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
void __GEOIP_PREPARE_TEREDO(geoipv6_t *v6)
const char * GeoIP_country_code_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
int GeoIP_id_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
GeoIPRegion * GeoIP_region_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
const char * GeoIP_continent_by_id(int id)
#define STANDARD_RECORD_LENGTH
int __GEOIP_V6_IS_NULL(geoipv6_t v6)
const char * GeoIP_country_code_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
GeoIPRegion * GeoIP_region_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
#define DATABASE_INFO_MAX_SIZE
static int _file_exists(const char *file_name)
const char * GeoIP_country_name_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
static char * _get_name_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
GeoIPRegion * GeoIP_region_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
#define DEBUG_MSGF(flags, fmt,...)
char ** GeoIP_range_by_ip_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
int GeoIP_teredo(GeoIP *gi)
const char * GeoIP_country_name_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
const char * GeoIP_country_code_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_lib_version(void)
static const char * _GeoIP_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
int GeoIP_country_id_by_name_gl(GeoIP *gi, const char *host, GeoIPLookup *gl)
int GeoIP_id_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
static const char * get_db_description(int dbtype)
#define LARGE_SEGMENT_RECORD_LENGTH
unsigned char GeoIP_database_edition(GeoIP *gi)
int GeoIP_charset(GeoIP *gi)
unsigned long _GeoIP_lookupaddress(const char *host)
GeoIPRegion * GeoIP_region_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
GeoIP * GeoIP_new(int flags)
int GeoIP_id_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_country_code3_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
geoipv6_t _GeoIP_lookupaddress_v6(const char *host)
unsigned char * index_cache
const char * GeoIP_country_code_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
const char GeoIP_country_code[256][3]
geoipv6_t _GeoIP_addr_to_num_v6(const char *addr)
const char * GeoIPDBDescription[(38+1)]
unsigned long GeoIP_addr_to_num(const char *addr)
int GeoIP_id_by_code(const char *country)
int GeoIP_is_private_v4(const char *addr)
char * GeoIP_name_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
int GeoIP_country_id_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_country_code3_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_country_code_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
char * GeoIP_name_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
const char * GeoIP_country_name_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
int GeoIP_id_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
const char * GeoIP_utf8_country_name[256]
char * GeoIP_name_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
static int _GeoIP_inet_pton(int af, const char *src, void *dst)
const char * GeoIP_country_code3_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
char * GeoIP_name_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_name_by_id(int id)
char * GeoIP_num_to_addr(unsigned long ipnum)
int GeoIP_db_avail(int type)
#define LARGE_COUNTRY_BEGIN
const char * GeoIP_code3_by_id(int id)
GeoIPRegion * GeoIP_region_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
void _GeoIP_setup_dbfilename(void)
char * _GeoIP_full_path_to(const char *file_name)
static ssize_t get_index_size(GeoIP *gi, struct stat *buf)
#define MAX_RECORD_LENGTH
GeoIP * GeoIP_open(const char *filename, int flags)
static void _check_mtime(GeoIP *gi)
void GeoIP_delete(GeoIP *gi)
static GeoIPRegion * _get_region_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
const char * GeoIP_country_name_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
char * _GeoIP_iso_8859_1__utf8(const char *iso)
GeoIP * GeoIP_open_type(int type, int flags)
unsigned int _GeoIP_seek_record_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
static char * _get_name_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
GeoIPRegion * GeoIP_region_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
#define GEOIP_CHKBIT_V6(bit, ptr)
static int _database_has_content(int database_type)
const char * GeoIP_country_code3_by_name_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
char * GeoIP_name_by_name_v6_gl(GeoIP *gi, const char *name, GeoIPLookup *gl)
#define pread(fd, buf, count, offset)
int GeoIP_is_private_ipnum_v4(unsigned long ipnum)
void GeoIP_assign_region_by_inetaddr_v6_gl(GeoIP *gi, geoipv6_t inetaddr, GeoIPRegion *region, GeoIPLookup *gl)
int GeoIP_id_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
static GeoIPRegion * _get_region_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
#define SEGMENT_RECORD_LENGTH
int GeoIP_set_charset(GeoIP *gi, int charset)
#define ORG_RECORD_LENGTH
const char * GeoIP_country_code3_by_ipnum_gl(GeoIP *gi, unsigned long ipnum, GeoIPLookup *gl)
const char * GeoIP_country_code_by_addr_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char GeoIP_country_code3[256][4]
static void _setup_segments(GeoIP *gi)
int GeoIP_id_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
void GeoIP_range_by_ip_delete(char **ptr)
char * GeoIP_custom_directory
int GeoIP_enable_teredo(GeoIP *gi, int true_false)
char * GeoIP_database_info(GeoIP *gi)
unsigned GeoIP_num_countries(void)
struct in6_addr geoipv6_t
void GeoIP_setup_custom_directory(char *dir)
const char * GeoIP_country_name_by_id(GeoIP *gi, int id)
static geoipv6_t IPV6_NULL
const char * GeoIP_country_code3_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
int GeoIP_country_id_by_addr_v6_gl(GeoIP *gi, const char *addr, GeoIPLookup *gl)
const char * GeoIP_country_name_by_ipnum_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
unsigned int _GeoIP_seek_record_v6_gl(GeoIP *gi, geoipv6_t ipnum, GeoIPLookup *gl)
void GeoIPRegion_delete(GeoIPRegion *gir)