Array f_geoip_region_by_name(CStrRef hostname) { GeoIP * gi; Array return_value = Array::Create(); if (is_db_avail(GEOIP_REGION_EDITION_REV1, false)) { gi = s_geoip->gi[GEOIP_REGION_EDITION_REV1]; } else if (is_db_avail(GEOIP_REGION_EDITION_REV0)) { gi = s_geoip->gi[GEOIP_REGION_EDITION_REV0]; } else return return_value; GeoIPRegion * region = GeoIP_region_by_name(gi, hostname.data()); if (NULL == region) { raise_notice("Host %s not found", hostname.data()); return return_value; } return_value.set("country_code", region->country_code); return_value.set("region", region->region); GeoIPRegion_delete(region); return return_value; }
static PyObject * GeoIP_region_by_name_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_name(GeoIP->gi, name); return GeoIP_region_populate_dict(retval); }
static int geoip_header_parser(request_rec * r) { char *orgorisp; char *ipaddr; char *free_me = NULL; short int country_id; const char *continent_code; const char *country_code; const char *country_name; const char *region_name; geoip_server_config_rec *cfg; unsigned char databaseType; GeoIPRecord *gir; GeoIPRegion *giregion; int i; int netspeed; /* For splitting proxy headers */ char *ipaddr_ptr = NULL; char *comma_ptr; cfg = ap_get_module_config(r->server->module_config, &geoip_module); if (!cfg) return DECLINED; if (!cfg->scanProxyHeaders) { ipaddr = _get_client_ip(r); } else { ap_add_common_vars(r); if (apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP"); } else if (apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR"); } else if (apr_table_get(r->headers_in, "X-Forwarded-For")) { ipaddr_ptr = (char *)apr_table_get(r->headers_in, "X-Forwarded-For"); } else if (apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR"); } if (!ipaddr_ptr) { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: Error while getting ipaddr from proxy headers. Using REMOTE_ADDR."); ipaddr = _get_client_ip(r); } else { /* Found XFF like header */ ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: IPADDR_PTR: %s", ipaddr_ptr); if (cfg->proxyHeaderMode == GEOIP_PROXY_HEADER_MODE_FIRST_NON_PRIVATE_IP) { ipaddr = free_me = _get_ip_from_xff(ipaddr_ptr); if (!ipaddr) ipaddr = _get_client_ip(r); } else { ipaddr = free_me = (char *)calloc(8 * 4 + 7 + 1, sizeof(char)); /* proxyHeaderMode is * GEOIP_PROXY_HEADER_MODE_LAST_IP or GEOIP_PROXY_HEADER_MODE_FIRST_IP */ /* * Check to ensure that the HTTP_CLIENT_IP or * X-Forwarded-For header is not a comma separated * list of addresses, which would cause mod_geoip to * return no country code. If the header is a comma * separated list, return the first IP address in the * list, which is (hopefully!) the real client IP. */ if (cfg->proxyHeaderMode == GEOIP_PROXY_HEADER_MODE_LAST_IP) { comma_ptr = strrchr(ipaddr_ptr, ','); if (comma_ptr) { /* skip over whitespace */ ipaddr_ptr = comma_ptr + strspn(comma_ptr, ", \t"); } } strncpy(ipaddr, ipaddr_ptr, 8 * 4 + 7); comma_ptr = strchr(ipaddr, ','); if (comma_ptr != 0) *comma_ptr = '\0'; } } } /* this block should be removed! */ #if 1 if (!cfg->gips) { if (cfg->GeoIPFilenames != NULL) { cfg->gips = malloc(sizeof(GeoIP *) * cfg->numGeoIPFiles); for (i = 0; i < cfg->numGeoIPFiles; i++) { cfg->gips[i] = GeoIP_open(cfg->GeoIPFilenames[i], (cfg->GeoIPFlags2[i] == GEOIP_UNKNOWN) ? cfg->GeoIPFlags : cfg->GeoIPFlags2[i]); if (cfg->gips[i]) { if (cfg->GeoIPEnableUTF8) { GeoIP_set_charset(cfg->gips[i], GEOIP_CHARSET_UTF8); } } else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file %s", cfg->GeoIPFilenames[i]); return DECLINED; } } } else { cfg->gips = malloc(sizeof(GeoIP *)); cfg->gips[0] = GeoIP_new(GEOIP_STANDARD); if (!cfg->gips[0]) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file"); return DECLINED; } cfg->numGeoIPFiles = 1; } } #endif set_geoip_output(cfg, r, "GEOIP_ADDR", ipaddr); for (i = 0; i < cfg->numGeoIPFiles; i++) { /* * skip database handles that can not be opned for some * reason */ if (cfg->gips[i] == NULL) continue; databaseType = cfg->gips[i] ? GeoIP_database_edition(cfg->gips[i]) : -1; /* -1 is "magic value" * in case file not * found */ switch (databaseType) { case GEOIP_NETSPEED_EDITION_REV1: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_NETSPEED", orgorisp); break; case GEOIP_NETSPEED_EDITION: netspeed = GeoIP_id_by_addr(cfg->gips[i], ipaddr); if (netspeed == GEOIP_UNKNOWN_SPEED) { netspeedstring = "unknown"; } else if (netspeed == GEOIP_DIALUP_SPEED) { netspeedstring = "dialup"; } else if (netspeed == GEOIP_CABLEDSL_SPEED) { netspeedstring = "cabledsl"; } else if (netspeed == GEOIP_CORPORATE_SPEED) { netspeedstring = "corporate"; } setn_geoip_output(cfg, r, "GEOIP_NETSPEED", netspeedstring); break; case GEOIP_COUNTRY_EDITION_V6: /* Get the Country ID */ country_id = GeoIP_id_by_addr_v6(cfg->gips[i], ipaddr); if (country_id > 0) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE_V6", continent_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE_V6", country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME_V6", country_name); } break; case GEOIP_COUNTRY_EDITION: /* Get the Country ID */ country_id = GeoIP_country_id_by_addr(cfg->gips[i], ipaddr); if (country_id > 0) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", continent_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", country_name); } break; case GEOIP_REGION_EDITION_REV0: case GEOIP_REGION_EDITION_REV1: giregion = GeoIP_region_by_name(cfg->gips[i], ipaddr); if (giregion != NULL) { region_name = NULL; if (giregion->country_code[0]) { region_name = GeoIP_region_name_by_code (giregion->country_code, giregion->region); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", giregion->country_code); country_id = GeoIP_id_by_code(giregion->country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", GeoIP_country_name[country_id]); setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", GeoIP_country_continent[country_id]); } if (giregion->region[0]) { set_geoip_output(cfg, r, "GEOIP_REGION", giregion->region); } if (region_name != NULL) { set_geoip_output(cfg, r, "GEOIP_REGION_NAME", region_name); } GeoIPRegion_delete(giregion); } break; case GEOIP_CITY_EDITION_REV0_V6: case GEOIP_CITY_EDITION_REV1_V6: gir = GeoIP_record_by_addr_v6(cfg->gips[i], ipaddr); if (gir != NULL) { if (gir->country_code != NULL) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); set_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE_V6", gir->continent_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE_V6", gir->country_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME_V6", gir->country_name); set_geoip_output(cfg, r, "GEOIP_REGION_V6", gir->region); set_geoip_output(cfg, r, "GEOIP_REGION_NAME_V6", region_name); set_geoip_output(cfg, r, "GEOIP_CITY_V6", gir->city); set_geoip_output(cfg, r, "GEOIP_DMA_CODE_V6", metrocodestr); set_geoip_output(cfg, r, "GEOIP_METRO_CODE_V6", metrocodestr); set_geoip_output(cfg, r, "GEOIP_AREA_CODE_V6", areacodestr); sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); set_geoip_output(cfg, r, "GEOIP_LATITUDE_V6", latstr); set_geoip_output(cfg, r, "GEOIP_LONGITUDE_V6", lonstr); set_geoip_output(cfg, r, "GEOIP_POSTAL_CODE_V6", gir->postal_code); GeoIPRecord_delete(gir); } break; case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: gir = GeoIP_record_by_addr(cfg->gips[i], ipaddr); if (gir != NULL) { if (gir->country_code != NULL) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); set_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", gir->continent_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", gir->country_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", gir->country_name); set_geoip_output(cfg, r, "GEOIP_REGION", gir->region); set_geoip_output(cfg, r, "GEOIP_REGION_NAME", region_name); set_geoip_output(cfg, r, "GEOIP_CITY", gir->city); set_geoip_output(cfg, r, "GEOIP_DMA_CODE", metrocodestr); set_geoip_output(cfg, r, "GEOIP_METRO_CODE", metrocodestr); set_geoip_output(cfg, r, "GEOIP_AREA_CODE", areacodestr); sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); set_geoip_output(cfg, r, "GEOIP_LATITUDE", latstr); set_geoip_output(cfg, r, "GEOIP_LONGITUDE", lonstr); set_geoip_output(cfg, r, "GEOIP_POSTAL_CODE", gir->postal_code); GeoIPRecord_delete(gir); } break; case GEOIP_ORG_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_ORGANIZATION", orgorisp); break; case GEOIP_ISP_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_ISP", orgorisp); break; case GEOIP_DOMAIN_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_DOMAIN", orgorisp); break; } } if (free_me) free(free_me); return OK; }
static int geoip_header_parser(request_rec * r) { char *orgorisp; char *ipaddr; short int country_id; GeoIP *gip; const char *continent_code; const char *country_code; const char *country_name; const char *region_name; geoip_server_config_rec *cfg; unsigned char databaseType; GeoIPRecord *gir; GeoIPRegion *giregion; int i; int netspeed; /* For splitting proxy headers */ char *ipaddr_ptr = 0; char *comma_ptr; char *found_ip; apr_sockaddr_t *sa; char *hostname = 0; cfg = ap_get_module_config(r->server->module_config, &geoip_module); if (!cfg) return DECLINED; if (!cfg->scanProxyHeaders) { ipaddr = r->connection->remote_ip; } else { ap_add_common_vars(r); if (apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP")) { ipaddr_ptr = (char *) apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP"); } else if (apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR")) { ipaddr_ptr = (char *) apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR"); } else if (apr_table_get(r->headers_in, "X-Forwarded-For")) { ipaddr_ptr = (char *) apr_table_get(r->headers_in, "X-Forwarded-For"); } else if (apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR")) { ipaddr_ptr = (char *) apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR"); } if (!ipaddr_ptr) { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: Error while getting ipaddr from proxy headers. Using REMOTE_ADDR."); ipaddr = r->connection->remote_ip; } else { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: IPADDR_PTR: %s", ipaddr_ptr); if (cfg->use_left_public_x_forwarded_for_ip) { // find the first public IP address in a potentially comma-separated list, // fall back to remote_ip if we can't find one ipaddr = first_public_ip_in_list(ipaddr_ptr, r->connection->remote_ip); } else { // leaving some of the following inconsistent indenting intact for easier diff to original maxmind src /* * Check to ensure that the HTTP_CLIENT_IP or * X-Forwarded-For header is not a comma separated * list of addresses, which would cause mod_geoip to * return no country code. If the header is a comma * separated list, return the first IP address in the * list, which is (hopefully!) the real client IP. */ ipaddr = (char *) calloc(8*4+7+1, sizeof(char)); if (cfg->use_last_x_forwarded_for_ip ){ comma_ptr = strrchr(ipaddr_ptr, ','); if ( comma_ptr ) { /* skip over whitespace */ ipaddr_ptr = comma_ptr + strspn(comma_ptr, ", \t"); } } strncpy(ipaddr, ipaddr_ptr, 8*4+7); comma_ptr = strchr(ipaddr, ','); if (comma_ptr != 0) *comma_ptr = '\0'; } } } /* this block should be removed! */ #if 1 if (!cfg->gips) { if (cfg->GeoIPFilenames != NULL) { cfg->gips = malloc(sizeof(GeoIP *) * cfg->numGeoIPFiles); for (i = 0; i < cfg->numGeoIPFiles; i++) { cfg->gips[i] = GeoIP_open(cfg->GeoIPFilenames[i], (cfg->GeoIPFlags2[i] == GEOIP_UNKNOWN) ? cfg->GeoIPFlags : cfg->GeoIPFlags2[i]); if (cfg->gips[i]) { if (cfg->GeoIPEnableUTF8) { GeoIP_set_charset(cfg->gips[i], GEOIP_CHARSET_UTF8); } } else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file %s", cfg->GeoIPFilenames[i]); return DECLINED; } } } else { cfg->gips = malloc(sizeof(GeoIP *)); cfg->gips[0] = GeoIP_new(GEOIP_STANDARD); if (!cfg->gips[0]) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file"); return DECLINED; } cfg->numGeoIPFiles = 1; } } #endif if (cfg->GeoIPEnableHostnameLookups && apr_sockaddr_info_get(&sa, ipaddr, APR_INET, 0, 0, r->pool) == APR_SUCCESS && apr_getnameinfo(&hostname, sa, 0) == APR_SUCCESS) { ap_str_tolower(hostname); } if (!hostname) hostname = ipaddr; if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_ADDR", ipaddr); apr_table_setn(r->notes, "GEOIP_HOST", hostname); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_ADDR", ipaddr); apr_table_setn(r->subprocess_env, "GEOIP_HOST", hostname); } for (i = 0; i < cfg->numGeoIPFiles; i++) { /* * skip database handles that can not be opned for some * reason */ if (cfg->gips[i] == NULL) continue; databaseType = cfg->gips[i] ? GeoIP_database_edition(cfg->gips[i]) : -1; /* -1 is "magic value" * in case file not * found */ switch (databaseType) { case GEOIP_NETSPEED_EDITION_REV1: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); if (orgorisp != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_NETSPEED", orgorisp); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_NETSPEED", orgorisp); } } break; case GEOIP_NETSPEED_EDITION: netspeed = GeoIP_id_by_addr(cfg->gips[i], ipaddr); if (netspeed == GEOIP_UNKNOWN_SPEED) { netspeedstring = "unknown"; } else if (netspeed == GEOIP_DIALUP_SPEED) { netspeedstring = "dialup"; } else if (netspeed == GEOIP_CABLEDSL_SPEED) { netspeedstring = "cabledsl"; } else if (netspeed == GEOIP_CORPORATE_SPEED) { netspeedstring = "corporate"; } if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_NETSPEED", netspeedstring); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_NETSPEED", netspeedstring); } break; case GEOIP_COUNTRY_EDITION_V6: /* Get the Country ID */ country_id = GeoIP_country_id_by_addr_v6(cfg->gips[i], ipaddr); if ( country_id > 0 ) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_CONTINENT_CODE_V6", continent_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_CODE_V6", country_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_NAME_V6", country_name); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_CONTINENT_CODE_V6", continent_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_CODE_V6", country_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_NAME_V6", country_name); } } break; case GEOIP_COUNTRY_EDITION: /* Get the Country ID */ country_id = GeoIP_country_id_by_addr(cfg->gips[i], ipaddr); if ( country_id > 0 ) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_CONTINENT_CODE", continent_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_CODE", country_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_NAME", country_name); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_CONTINENT_CODE", continent_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_CODE", country_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_NAME", country_name); } } break; case GEOIP_REGION_EDITION_REV0: case GEOIP_REGION_EDITION_REV1: giregion = GeoIP_region_by_name(cfg->gips[i], ipaddr); if (giregion != NULL) { if ( giregion->country_code[0] ) { region_name = GeoIP_region_name_by_code(giregion->country_code, giregion->region); } if (cfg->GeoIPOutput & GEOIP_NOTES) { if ( giregion->country_code[0] ){ apr_table_set(r->notes, "GEOIP_COUNTRY_CODE", giregion->country_code); } if (giregion->region[0]) { apr_table_set(r->notes, "GEOIP_REGION", giregion->region); } if ( region_name != NULL ){ apr_table_set(r->notes, "GEOIP_REGION_NAME", region_name); } } if (cfg->GeoIPOutput & GEOIP_ENV) { if ( giregion->country_code[0] ){ apr_table_set(r->subprocess_env, "GEOIP_COUNTRY_CODE", giregion->country_code); } if (giregion->region[0]) { apr_table_set(r->subprocess_env, "GEOIP_REGION", giregion->region); } if ( region_name != NULL ){ apr_table_set(r->subprocess_env, "GEOIP_REGION_NAME", region_name); } } GeoIPRegion_delete(giregion); } break; case GEOIP_CITY_EDITION_REV0_V6: case GEOIP_CITY_EDITION_REV1_V6: gir = GeoIP_record_by_addr_v6(cfg->gips[i], ipaddr); if (gir != NULL) { if ( gir->country_code != NULL ) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_CONTINENT_CODE_V6", gir->continent_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_CODE_V6", gir->country_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_NAME_V6", gir->country_name); if (gir->region != NULL) { apr_table_set(r->notes, "GEOIP_REGION_V6", gir->region); if ( region_name != NULL ){ apr_table_set(r->notes, "GEOIP_REGION_NAME_V6", region_name); } } if (gir->city != NULL) { apr_table_set(r->notes, "GEOIP_CITY_V6", gir->city); } apr_table_setn(r->notes, "GEOIP_DMA_CODE_V6", metrocodestr); apr_table_setn(r->notes, "GEOIP_METRO_CODE_V6", metrocodestr); apr_table_setn(r->notes, "GEOIP_AREA_CODE_V6", areacodestr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_CONTINENT_CODE_V6", gir->continent_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_CODE_V6", gir->country_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_NAME_V6", gir->country_name); if (gir->region != NULL) { apr_table_set(r->subprocess_env, "GEOIP_REGION_V6", gir->region); if ( region_name != NULL ){ apr_table_set(r->subprocess_env, "GEOIP_REGION_NAME_V6", region_name); } } if (gir->city != NULL) { apr_table_set(r->subprocess_env, "GEOIP_CITY_V6", gir->city); } apr_table_setn(r->subprocess_env, "GEOIP_DMA_CODE_V6", metrocodestr); apr_table_setn(r->subprocess_env, "GEOIP_METRO_CODE_V6", metrocodestr); apr_table_setn(r->subprocess_env, "GEOIP_AREA_CODE_V6", areacodestr); } sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_LATITUDE_V6", latstr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_LATITUDE_V6", latstr); } if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_LONGITUDE_V6", lonstr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_LONGITUDE_V6", lonstr); } if (gir->postal_code != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_set(r->notes, "GEOIP_POSTAL_CODE_V6", gir->postal_code); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_set(r->subprocess_env, "GEOIP_POSTAL_CODE_V6", gir->postal_code); } } GeoIPRecord_delete(gir); } break; case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: gir = GeoIP_record_by_addr(cfg->gips[i], ipaddr); if (gir != NULL) { if ( gir->country_code != NULL ) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_CONTINENT_CODE", gir->continent_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_CODE", gir->country_code); apr_table_setn(r->notes, "GEOIP_COUNTRY_NAME", gir->country_name); if (gir->region != NULL) { apr_table_set(r->notes, "GEOIP_REGION", gir->region); if ( region_name != NULL ){ apr_table_set(r->notes, "GEOIP_REGION_NAME", region_name); } } if (gir->city != NULL) { apr_table_set(r->notes, "GEOIP_CITY", gir->city); } apr_table_setn(r->notes, "GEOIP_DMA_CODE", metrocodestr); apr_table_setn(r->notes, "GEOIP_METRO_CODE", metrocodestr); apr_table_setn(r->notes, "GEOIP_AREA_CODE", areacodestr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_CONTINENT_CODE", gir->continent_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_CODE", gir->country_code); apr_table_setn(r->subprocess_env, "GEOIP_COUNTRY_NAME", gir->country_name); if (gir->region != NULL) { apr_table_set(r->subprocess_env, "GEOIP_REGION", gir->region); if ( region_name != NULL ){ apr_table_set(r->subprocess_env, "GEOIP_REGION_NAME", region_name); } } if (gir->city != NULL) { apr_table_set(r->subprocess_env, "GEOIP_CITY", gir->city); } apr_table_setn(r->subprocess_env, "GEOIP_DMA_CODE", metrocodestr); apr_table_setn(r->subprocess_env, "GEOIP_METRO_CODE", metrocodestr); apr_table_setn(r->subprocess_env, "GEOIP_AREA_CODE", areacodestr); } sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_LATITUDE", latstr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_LATITUDE", latstr); } if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_LONGITUDE", lonstr); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_LONGITUDE", lonstr); } if (gir->postal_code != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_set(r->notes, "GEOIP_POSTAL_CODE", gir->postal_code); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_set(r->subprocess_env, "GEOIP_POSTAL_CODE", gir->postal_code); } } GeoIPRecord_delete(gir); } break; case GEOIP_ORG_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); if (orgorisp != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_ORGANIZATION", orgorisp); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_ORGANIZATION", orgorisp); } } break; case GEOIP_ISP_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); if (orgorisp != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_ISP", orgorisp); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_ISP", orgorisp); } } break; case GEOIP_DOMAIN_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); if (orgorisp != NULL) { if (cfg->GeoIPOutput & GEOIP_NOTES) { apr_table_setn(r->notes, "GEOIP_DOMAIN", orgorisp); } if (cfg->GeoIPOutput & GEOIP_ENV) { apr_table_setn(r->subprocess_env, "GEOIP_DOMAIN", orgorisp); } } break; } } return OK; }
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; }