static GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) { GeoIPRegion * region; region = (GeoIPRegion*)malloc(sizeof(GeoIPRegion)); if (region) { GeoIP_assign_region_by_inetaddr(gi, htonl(ipnum), region); } return region; }
int main() { GeoIP *gi; GeoIPRegion *gir, giRegion; FILE *f; char ipAddress[30]; char expectedCountry[3]; char expectedCountry3[4]; const char *time_zone; gi = GeoIP_open("../data/GeoIPRegion.dat", GEOIP_MEMORY_CACHE); if (gi == NULL) { fprintf(stderr, "Error opening database\n"); exit(1); } f = fopen("region_test.txt", "r"); if (f == NULL) { fprintf(stderr, "Error opening region_test.txt\n"); exit(1); } gir = GeoIP_region_by_addr(gi, "10.0.0.0"); if (gir != NULL) { printf("lookup of private IP address: country = %s, region = %s\n", gir->country_code, gir->region); } while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) != EOF) { printf("ip = %s\n", ipAddress); gir = GeoIP_region_by_name(gi, ipAddress); if (gir != NULL) { time_zone = GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region); printf("%s, %s, %s, %s\n", gir->country_code, (!gir->region[0]) ? "N/A" : gir->region, _mk_NA(GeoIP_region_name_by_code (gir->country_code, gir->region)), _mk_NA(time_zone)); } else { printf("NULL!\n"); } GeoIP_assign_region_by_inetaddr(gi, inetaddr(ipAddress), &giRegion); if (gir != NULL) { assert(giRegion.country_code[0]); assert(!strcmp(gir->country_code, giRegion.country_code)); if (gir->region[0]) { assert(giRegion.region[0]); assert(!strcmp(gir->region, giRegion.region)); } else { assert(!giRegion.region[0]); } } else { assert(!giRegion.country_code[0]); } if (gir != NULL) { GeoIPRegion_delete(gir); } } GeoIP_delete(gi); fclose(f); return 0; }