1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ 2/* test-geoip-region.c 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 21#include "GeoIP.h" 22#include <sys/types.h> /* For uint32_t */ 23#ifdef HAVE_STDINT_H 24#include <stdint.h> /* For uint32_t */ 25#endif 26#if !defined(_WIN32) 27#include <netdb.h> /* For gethostbyname */ 28#include <netinet/in.h> /* For ntohl */ 29#else 30#include <windows.h> 31#include <winsock.h> 32#endif 33#include <assert.h> 34 35unsigned long inetaddr(const char *name) 36{ 37 struct hostent *host; 38 struct in_addr inaddr; 39 40 host = gethostbyname(name); 41 assert(host); 42 inaddr.s_addr = *((uint32_t *) host->h_addr_list[0]); 43 return inaddr.s_addr; 44} 45 46static const char *_mk_NA(const char *p) 47{ 48 return p ? p : "N/A"; 49} 50 51int main() 52{ 53 GeoIP *gi; 54 GeoIPRegion *gir, giRegion; 55 56 FILE *f; 57 char ipAddress[30]; 58 char expectedCountry[3]; 59 char expectedCountry3[4]; 60 const char *time_zone; 61 62 gi = GeoIP_open("../data/GeoIPRegion.dat", GEOIP_MEMORY_CACHE); 63 64 if (gi == NULL) { 65 fprintf(stderr, "Error opening database\n"); 66 exit(1); 67 } 68 69 f = fopen("region_test.txt", "r"); 70 71 if (f == NULL) { 72 fprintf(stderr, "Error opening region_test.txt\n"); 73 exit(1); 74 } 75 76 gir = GeoIP_region_by_addr(gi, "10.0.0.0"); 77 if (gir != NULL) { 78 printf("lookup of private IP address: country = %s, region = %s\n", 79 gir->country_code, gir->region); 80 } 81 82 while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) != 83 EOF) { 84 printf("ip = %s\n", ipAddress); 85 86 gir = GeoIP_region_by_name(gi, ipAddress); 87 if (gir != NULL) { 88 time_zone = 89 GeoIP_time_zone_by_country_and_region(gir->country_code, 90 gir->region); 91 printf("%s, %s, %s, %s\n", 92 gir->country_code, 93 (!gir->region[0]) ? "N/A" : gir->region, 94 _mk_NA(GeoIP_region_name_by_code 95 (gir->country_code, gir->region)), _mk_NA(time_zone)); 96 } else { 97 printf("NULL!\n"); 98 } 99 100 GeoIP_assign_region_by_inetaddr(gi, inetaddr(ipAddress), &giRegion); 101 if (gir != NULL) { 102 assert(giRegion.country_code[0]); 103 assert(!strcmp(gir->country_code, giRegion.country_code)); 104 if (gir->region[0]) { 105 assert(giRegion.region[0]); 106 assert(!strcmp(gir->region, giRegion.region)); 107 } else { 108 assert(!giRegion.region[0]); 109 } 110 } else { 111 assert(!giRegion.country_code[0]); 112 } 113 114 if (gir != NULL) { 115 GeoIPRegion_delete(gir); 116 } 117 } 118 119 GeoIP_delete(gi); 120 fclose(f); 121 return 0; 122} 123