static PyObject * GeoIP_region_by_addr_Py(PyObject *self, PyObject * args) { char * name; GeoIPRegion * retval; GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } retval = GeoIP_region_by_addr(GeoIP->gi, name); return GeoIP_region_populate_dict(retval); }
static const char * vmod_region_name_by_addr(GeoIP *gi, const char *ip) { GeoIPRegion *gir; const char *region = NULL; gir = GeoIP_region_by_addr(gi, ip); if (gir == NULL) return (NULL); region = GeoIP_region_name_by_code(gir->country_code, gir->region); GeoIPRegion_delete(gir); return (region); }
/** * Region */ VCL_STRING vmod_region(const struct vrt_ctx *ctx, struct vmod_priv *pp, const char *ip) { const char *region = NULL; if (pp->priv) { struct GeoIP_databases* db = (struct GeoIP_databases*)pp->priv; if (db && db->region) { GeoIPRegion *gir; if ((gir = GeoIP_region_by_addr(db->region, ip)) != NULL) { region = GeoIP_region_name_by_code(gir->country_code, gir->region); GeoIPRegion_delete(gir); } } } if (!region) region = unknownRegion; return WS_Copy(ctx->ws, region, -1); }
void testgeoipregion(int flags, const char *msg, int numlookups) { GeoIP *i = NULL; GeoIPRegion *i3 = NULL; int i4 = 0; int i2 = 0; double t = 0; i = GeoIP_open("/usr/local/share/GeoIP/GeoIPRegion.dat", flags); if (i == NULL) { printf("error: GeoIPRegion.dat does not exist\n"); return; } timerstart(); for (i2 = 0; i2 < numlookups; i2++) { i3 = GeoIP_region_by_addr(i, ipstring[i4]); GeoIPRegion_delete(i3); i4 = (i4 + 1) % numipstrings; } t = timerstop(); printf("%s\n", msg); printf("%d lookups made in %f seconds \n", numlookups, t); GeoIP_delete(i); }
char *geo_lookup(GeoIP *gi, char *ipaddr, int bird) { /* * Lookup the country_code by ip address, we can * extend this in the future with more granular data * such as region,city or even zipcode. */ static char area[MAX_BUF_LENGTH]; // set the charset to UTF8 GeoIP_set_charset(gi, GEOIP_CHARSET_UTF8); switch(bird){ case COUNTRY: { const char *country= GeoIP_country_code_by_addr(gi, ipaddr); if (country==NULL){ strncpy(area, unknown_geography, MAX_BUF_LENGTH); } else { strncpy(area, country, MAX_BUF_LENGTH); } } break; case REGION:{ GeoIPRegion *gir; gir=GeoIP_region_by_addr(gi,ipaddr); if(gir == NULL || strlen(gir->region)==0){ strncpy(area, unknown_geography, MAX_BUF_LENGTH); } else { strncpy(area, gir->region, MAX_BUF_LENGTH); } if(gir != NULL) { GeoIPRegion_delete(gir); } break; } case CITY:{ GeoIPRecord *grecord; char *city; int mustFreeCity = 0; grecord = GeoIP_record_by_addr(gi, ipaddr); if (grecord !=NULL){ if (grecord->city == NULL){ strncpy(area, unknown_geography, MAX_BUF_LENGTH); } else { int len = strlen(grecord->city); city = strdup(grecord->city); mustFreeCity = 1; strncpy(area,city, MAX_BUF_LENGTH); replace_space_with_underscore(area, len); } if (mustFreeCity) { free(city); } GeoIPRecord_delete(grecord); } else { strncpy(area, unknown_geography, MAX_BUF_LENGTH); } break; } case LAT_LON: { GeoIPRecord *grecord; grecord = GeoIP_record_by_addr(gi, ipaddr); if (grecord!=NULL){ snprintf(area, MAX_BUF_LENGTH, "%f,%f", grecord->latitude, grecord->longitude); GeoIPRecord_delete(grecord); } else { strncpy(area, unknown_geography, MAX_BUF_LENGTH); } break; } case EVERYTHING: { GeoIPRecord *grecord; char *country = unknown_geography, *region = unknown_geography, *city = unknown_geography; int mustFreeCity = 0; float lat = 0.0, lon = 0.0; grecord = GeoIP_record_by_addr(gi, ipaddr); if (grecord != NULL) { if (grecord->city != NULL) { city = strdup(grecord->city); mustFreeCity = 1; } replace_space_with_underscore(city, strlen(city)); if (grecord->region != NULL) { region = grecord->region; } if (grecord->country_code != NULL) { country = grecord->country_code; } lat = grecord->latitude; lon = grecord->longitude; } snprintf(area, MAX_BUF_LENGTH, "%s|%s|%s|%f,%f", country, region, city, lat, lon); if (grecord != NULL) { GeoIPRecord_delete(grecord); } if (mustFreeCity) { free(city); } break; } default: break; } return area; }
short int GeoIP_update_database_general (char * user_id,char * license_key,char *data_base_type, int verbose,char ** client_ipaddr, void (*f)( char *)) { struct hostent *hostlist; int sock; char * buf; struct sockaddr_in sa; int offset = 0, err; char * request_uri; char * compr; unsigned long comprLen; FILE *comp_fh, *cur_db_fh, *gi_fh; gzFile gz_fh; char * file_path_gz, * file_path_test; MD5_CONTEXT context; MD5_CONTEXT context2; unsigned char buffer[1024], digest[16] ,digest2[16]; char hex_digest[33] = "0000000000000000000000000000000\0"; char hex_digest2[33] = "0000000000000000000000000000000\0"; unsigned int i; char *f_str; GeoIP * gi; char * db_info; char *ipaddress; char *geoipfilename; char *tmpstr; int dbtype; int lookupresult = 1; char block[BLOCK_SIZE]; int block_size = BLOCK_SIZE; size_t len; size_t request_uri_len; size_t size; hostlist = GeoIP_get_host_or_proxy(); if (hostlist == NULL) return GEOIP_DNS_ERR; if (hostlist->h_addrtype != AF_INET) return GEOIP_NON_IPV4_ERR; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (verbose == 1) { GeoIP_printf(f,"Connecting to MaxMind GeoIP server\n"); GeoIP_printf(f, "via Host or Proxy Server: %s:%d\n", hostlist->h_name, GeoIPHTTPPort); } if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) return GEOIP_CONNECTION_ERR; request_uri = malloc(sizeof(char) * (strlen(license_key) + strlen(GeoIPHTTPRequestMD5)+1036)); if (request_uri == NULL) return GEOIP_OUT_OF_MEMORY_ERR; /* get the file name from a web page using the product id */ sprintf(request_uri,GeoIPHTTPRequestFilename,GeoIPProxyHTTP,GeoIPProxiedHost,data_base_type,GeoIPUpdateHost); if (verbose == 1) { GeoIP_printf(f, "sending request %s \n",request_uri); } send(sock, request_uri, strlen(request_uri),0); /* send the request */ free(request_uri); buf = malloc(sizeof(char) * (block_size+4)); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; offset = 0; for (;;){ int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0){ break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset + block_size + 4); } buf[offset] = 0; offset = 0; tmpstr = strstr(buf, "\r\n\r\n") + 4; if (tmpstr[0] == '.' || strchr(tmpstr, '/') != NULL) { free(buf); return GEOIP_INVALID_SERVER_RESPONSE; } geoipfilename = _GeoIP_full_path_to(tmpstr); free(buf); /* print the database product id and the database filename */ if (verbose == 1){ GeoIP_printf(f, "database product id %s database file name %s \n",data_base_type,geoipfilename); } _GeoIP_setup_dbfilename(); /* get MD5 of current GeoIP database file */ if ((cur_db_fh = fopen (geoipfilename, "rb")) == NULL) { GeoIP_printf(f, NoCurrentDB, geoipfilename); } else { md5_init(&context); while ((len = fread (buffer, 1, 1024, cur_db_fh)) > 0) md5_write (&context, buffer, len); md5_final (&context); memcpy(digest,context.buf,16); fclose (cur_db_fh); for (i = 0; i < 16; i++) sprintf (&hex_digest[2*i], "%02x", digest[i]); GeoIP_printf(f, MD5Info, hex_digest ); } if (verbose == 1) { GeoIP_printf(f,"MD5 sum of database %s is %s \n",geoipfilename,hex_digest); } if (client_ipaddr[0] == NULL) { /* We haven't gotten our IP address yet, so let's request it */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { free(geoipfilename); return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (verbose == 1) GeoIP_printf(f,"Connecting to MaxMind GeoIP Update server\n"); /* Download gzip file */ if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) { free(geoipfilename); return GEOIP_CONNECTION_ERR; } request_uri = malloc(sizeof(char) * (strlen(license_key) + strlen(GeoIPHTTPRequestMD5)+1036)); if (request_uri == NULL) { free(geoipfilename); return GEOIP_OUT_OF_MEMORY_ERR; } /* get client ip address from MaxMind web page */ sprintf(request_uri,GeoIPHTTPRequestClientIP,GeoIPProxyHTTP,GeoIPProxiedHost,GeoIPUpdateHost); send(sock, request_uri, strlen(request_uri),0); /* send the request */ if (verbose == 1) { GeoIP_printf(f, "sending request %s", request_uri); } free(request_uri); buf = malloc(sizeof(char) * (block_size+1)); if (buf == NULL) { free(geoipfilename); return GEOIP_OUT_OF_MEMORY_ERR; } offset = 0; for (;;){ int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0) { break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset+block_size+1); } buf[offset] = 0; offset = 0; ipaddress = strstr(buf, "\r\n\r\n") + 4; /* get the ip address */ ipaddress = malloc(strlen(strstr(buf, "\r\n\r\n") + 4)+5); strcpy(ipaddress,strstr(buf, "\r\n\r\n") + 4); client_ipaddr[0] = ipaddress; if (verbose == 1) { GeoIP_printf(f, "client ip address: %s\n",ipaddress); } free(buf); close(sock); } ipaddress = client_ipaddr[0]; /* make a md5 sum of ip address and license_key and store it in hex_digest2 */ request_uri_len = sizeof(char) * 2036; request_uri = malloc(request_uri_len); md5_init(&context2); md5_write (&context2, (byte *)license_key, 12);//add license key to the md5 sum md5_write (&context2, (byte *)ipaddress, strlen(ipaddress));//add ip address to the md5 sum md5_final (&context2); memcpy(digest2,context2.buf,16); for (i = 0; i < 16; i++) snprintf (&hex_digest2[2*i], 3, "%02x", digest2[i]);// change the digest to a hex digest if (verbose == 1) { GeoIP_printf(f, "md5sum of ip address and license key is %s \n",hex_digest2); } /* send the request using the user id,product id, * md5 sum of the prev database and * the md5 sum of the license_key and ip address */ if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) return GEOIP_CONNECTION_ERR; snprintf(request_uri, request_uri_len, GeoIPHTTPRequestMD5,GeoIPProxyHTTP,GeoIPProxiedHost,hex_digest,hex_digest2,user_id,data_base_type); send(sock, request_uri, strlen(request_uri),0); if (verbose == 1) { GeoIP_printf(f, "sending request %s\n",request_uri); } free(request_uri); offset = 0; buf = malloc(sizeof(char) * block_size); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; if (verbose == 1) GeoIP_printf(f,"Downloading gzipped GeoIP Database...\n"); for (;;) { int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0) { break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset+block_size); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; } compr = strstr(buf, "\r\n\r\n") + 4; comprLen = offset + buf - compr; if (strstr(compr, "License Key Invalid") != NULL) { if (verbose == 1) GeoIP_printf(f,"Failed\n"); free(buf); return GEOIP_LICENSE_KEY_INVALID_ERR; } else if (strstr(compr, "No new updates available") != NULL) { free(buf); GeoIP_printf(f, "%s is up to date, no updates required\n", geoipfilename); return GEOIP_NO_NEW_UPDATES; } else if (strstr(compr, "Invalid UserId") != NULL){ free(buf); return GEOIP_USER_ID_INVALID_ERR; } else if (strstr(compr, "Invalid product ID or subscription expired") != NULL){ free(buf); return GEOIP_PRODUCT_ID_INVALID_ERR; } if (verbose == 1) GeoIP_printf(f,"Done\n"); GeoIP_printf(f, "Updating %s\n", geoipfilename); /* save gzip file */ file_path_gz = malloc(sizeof(char) * (strlen(geoipfilename) + 4)); if (file_path_gz == NULL) return GEOIP_OUT_OF_MEMORY_ERR; strcpy(file_path_gz,geoipfilename); strcat(file_path_gz,".gz"); if (verbose == 1) { GeoIP_printf(f, "%s%s", SavingGzip, file_path_gz ); } comp_fh = fopen(file_path_gz, "wb"); if(comp_fh == NULL) { free(file_path_gz); free(buf); return GEOIP_GZIP_IO_ERR; } size = fwrite(compr, 1, comprLen, comp_fh); fclose(comp_fh); free(buf); if ( size != comprLen ) { return GEOIP_GZIP_IO_ERR; } if (verbose == 1) { GeoIP_printf(f, "download data to a gz file named %s \n",file_path_gz); GeoIP_printf(f,"Done\n"); GeoIP_printf(f,"Uncompressing gzip file ... "); } file_path_test = malloc(sizeof(char) * (strlen(GeoIPDBFileName[GEOIP_COUNTRY_EDITION]) + 6)); if (file_path_test == NULL) { free(file_path_gz); return GEOIP_OUT_OF_MEMORY_ERR; } strcpy(file_path_test,GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); strcat(file_path_test,".test"); gi_fh = fopen(file_path_test, "wb"); if(gi_fh == NULL) { free(file_path_test); free(file_path_gz); return GEOIP_TEST_IO_ERR; } /* uncompress gzip file */ offset = 0; gz_fh = gzopen(file_path_gz, "rb"); for (;;) { int amt; amt = gzread(gz_fh, block, block_size); if (amt == -1) { free(file_path_gz); free(file_path_test); gzclose(gz_fh); fclose(gi_fh); return GEOIP_GZIP_READ_ERR; } if (amt == 0) { break; } if ( amt != fwrite(block,1,amt,gi_fh) ){ return GEOIP_GZIP_IO_ERR; } } gzclose(gz_fh); unlink(file_path_gz); free(file_path_gz); fclose(gi_fh); if (verbose == 1) GeoIP_printf(f,"Done\n"); if (verbose == 1) { len = strlen(WritingFile) + strlen(geoipfilename) - 1; f_str = malloc(len); snprintf(f_str,len,WritingFile,geoipfilename); free(f_str); } /* sanity check */ gi = GeoIP_open(file_path_test, GEOIP_STANDARD); if (verbose == 1) GeoIP_printf(f,"Performing santity checks ... "); if (gi == NULL) { GeoIP_printf(f,"Error opening sanity check database\n"); return GEOIP_SANITY_OPEN_ERR; } /* get the database type */ dbtype = GeoIP_database_edition(gi); if (verbose == 1) { GeoIP_printf(f, "Database type is %d\n",dbtype); } /* this checks to make sure the files is complete, since info is at the end dependent on future databases having MaxMind in info (ISP and Organization databases currently don't have info string */ if ((dbtype != GEOIP_ISP_EDITION)&& (dbtype != GEOIP_ORG_EDITION)) { if (verbose == 1) GeoIP_printf(f,"database_info "); db_info = GeoIP_database_info(gi); if (db_info == NULL) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL null\n"); return GEOIP_SANITY_INFO_FAIL; } if (strstr(db_info, "MaxMind") == NULL) { free(db_info); GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL maxmind\n"); return GEOIP_SANITY_INFO_FAIL; } free(db_info); if (verbose == 1) GeoIP_printf(f,"PASS "); } /* this performs an IP lookup test of a US IP address */ if (verbose == 1) GeoIP_printf(f,"lookup "); if (dbtype == GEOIP_NETSPEED_EDITION) { int netspeed = GeoIP_id_by_name(gi,"24.24.24.24"); lookupresult = 0; if (netspeed == GEOIP_CABLEDSL_SPEED){ lookupresult = 1; } } if (dbtype == GEOIP_COUNTRY_EDITION) { /* if data base type is country then call the function * named GeoIP_country_code_by_addr */ lookupresult = 1; if (strcmp(GeoIP_country_code_by_addr(gi,"24.24.24.24"), "US") != 0) { lookupresult = 0; } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_COUNTRY_EDITION\n"); } } if (dbtype == GEOIP_REGION_EDITION_REV1) { /* if data base type is region then call the function * named GeoIP_region_by_addr */ GeoIPRegion *r = GeoIP_region_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_REGION_EDITION\n"); } } if (dbtype == GEOIP_CITY_EDITION_REV1) { /* if data base type is city then call the function * named GeoIP_record_by_addr */ GeoIPRecord *r = GeoIP_record_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_CITY_EDITION\n"); } } if ((dbtype == GEOIP_ISP_EDITION)|| (dbtype == GEOIP_ORG_EDITION)) { /* if data base type is isp or org then call the function * named GeoIP_org_by_addr */ GeoIPRecord *r = (GeoIPRecord*)GeoIP_org_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { if (dbtype == GEOIP_ISP_EDITION) { GeoIP_printf(f,"testing GEOIP_ISP_EDITION\n"); } if (dbtype == GEOIP_ORG_EDITION) { GeoIP_printf(f,"testing GEOIP_ORG_EDITION\n"); } } } if (lookupresult == 0) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL\n"); return GEOIP_SANITY_LOOKUP_FAIL; } GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"PASS\n"); /* install GeoIP.dat.test -> GeoIP.dat */ err = rename(file_path_test, geoipfilename); if (err != 0) { GeoIP_printf(f,"GeoIP Install error while renaming file\n"); return GEOIP_RENAME_ERR; } if (verbose == 1) GeoIP_printf(f,"Done\n"); free(geoipfilename); return 0; }
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; }