static PyObject * GeoIP_id_by_addr_Py(PyObject *self, PyObject *args) { char * name; int i; GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } i = GeoIP_id_by_addr(GeoIP->gi, name); return Py_BuildValue("i", i); }
/** Apply GeoIP country data to a client. * @param[in] cptr Client to apply GeoIP country data to. */ void geoip_apply(struct Client* cptr) { #ifdef USE_GEOIP int gcid = 0; #endif /* USE_GEOIP */ #ifdef USE_GEOIP_GL GeoIPLookup gl; #endif /* USE_GEOIP_GL */ if (!feature_bool(FEAT_GEOIP_ENABLE)) return; if (!(cptr)) return; #ifdef USE_GEOIP if (irc_in_addr_is_ipv4(&cli_ip(cptr))) { /* User is IPv4 so use gi4. */ if (gi4 != NULL) #ifdef USE_GEOIP_GL gcid = GeoIP_id_by_addr_gl(gi4, cli_sock_ip(cptr), &gl); #else gcid = GeoIP_id_by_addr(gi4, cli_sock_ip(cptr)); #endif /* USE_GEOIP_GL */ } else { /* User is IPv6 so use gi6. */ if (gi6 != NULL) #ifdef USE_GEOIP_GL gcid = GeoIP_id_by_addr_v6_gl(gi6, cli_sock_ip(cptr), &gl); #else gcid = GeoIP_id_by_addr_v6(gi6, cli_sock_ip(cptr)); #endif /* USE_GEOIP_GL */ } #endif /* USE_GEOIP */ #ifdef USE_GEOIP if (gcid == 0) { #endif /* USE_GEOIP */ ircd_strncpy((char *)&cli_countrycode(cptr), "--", 3); ircd_strncpy((char *)&cli_countryname(cptr), "Unknown", 8); ircd_strncpy((char *)&cli_continentcode(cptr), "--", 3); ircd_strncpy((char *)&cli_continentname(cptr), "Unknown", 8); #ifdef USE_GEOIP } else { ircd_strncpy((char *)&cli_countrycode(cptr), GeoIP_code_by_id(gcid), 3); ircd_strncpy((char *)&cli_countryname(cptr), GeoIP_name_by_id(gcid), 256); ircd_strncpy((char *)&cli_continentcode(cptr), GeoIP_continent_by_id(gcid), 3); ircd_strncpy((char *)&cli_continentname(cptr), geoip_continent_name_by_code(GeoIP_continent_by_id(gcid)), 256); } #endif /* USE_GEOIP */ SetGeoIP(cptr); }
/* Pass this function an IP address as a string, it will return a hash * containing all the information that the database knows about the IP * db.look_up('24.24.24.24') * => {:country_code=>"US", * :country_code3=>"USA", * :country_name=>"United States"} */ VALUE rb_geoip_country_look_up(VALUE self, VALUE addr) { GeoIP *gi; VALUE hash = Qnil; int country_id; Check_Type(addr, T_STRING); Data_Get_Struct(self, GeoIP, gi); country_id = GeoIP_id_by_addr(gi, StringValuePtr(addr)); if(country_id < 1) return Qnil; hash = rb_hash_new(); rb_hash_sset(hash, "country_code", rb_str_new2(GeoIP_country_code[country_id])); rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id])); rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id])); return hash; }
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; }
/** * Lookup the IP address in the GeoIP database * * @param[in] ib IronBee engine * @param[in] tx Transaction * @param[in] event Event * @param[in] data callback data (Module configuration) */ static ib_status_t geoip_lookup( ib_engine_t *ib, ib_tx_t *tx, ib_state_event_type_t event, void *data ) { assert(ib != NULL); assert(event == handle_context_tx_event); assert(data != NULL); const char *ip = tx->er_ipstr; const module_data_t *mod_data = (const module_data_t *)data; if (ip == NULL) { ib_log_alert_tx(tx, "Trying to lookup NULL IP in GEOIP"); return IB_EINVAL; } #ifdef GEOIP_HAVE_VERSION /** * Some configurations exist as single characters and must be converted to * a string. This is simply a place to assemble that string before * it is passed into ip_data_add_nulstr. * This is only needed if we have support confidence items. WAM */ char one_char_str[2] = { '\0', '\0' }; #endif /* GEOIP_HAVE_VERSION */ ib_status_t rc; /* Declare and initialize the GeoIP property list. * Regardless of if we find a record or not, we want to create the list * artifact so that later modules know we ran and did [not] find a * record. */ ib_field_t *geoip_lst = NULL; ib_field_t *tmp_field = NULL; /* Id of geo ip record to read. */ int geoip_id; ib_log_debug_tx(tx, "GeoIP Lookup '%s'", ip); /* Build a new list. */ rc = ib_var_source_initialize( mod_data->geoip_source, &geoip_lst, tx->var_store, IB_FTYPE_LIST ); /* NOTICE: Called before GeoIP_record_by_addr allocates a * GeoIPRecord. */ if (rc != IB_OK) { ib_log_alert_tx(tx, "Unable to add GEOIP var."); return IB_EINVAL; } if (mod_data->geoip_db == NULL) { ib_log_alert_tx(tx, "GeoIP database was never opened. Perhaps the " "configuration file needs a GeoIPDatabaseFile " "\"/usr/share/geoip/GeoLite.dat\" line?"); return IB_EINVAL; } geoip_id = GeoIP_id_by_addr(mod_data->geoip_db, ip); if (geoip_id > 0) { const char *tmp_str; ib_log_debug_tx(tx, "GeoIP record found."); /* Add integers. */ tmp_field = NULL; tmp_str = GeoIP_code_by_id(geoip_id); if (tmp_str) { ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_code"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(tmp_str)); ib_field_list_add(geoip_lst, tmp_field); } tmp_str = GeoIP_code3_by_id(geoip_id); if (tmp_str) { ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_code3"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(tmp_str)); ib_field_list_add(geoip_lst, tmp_field); } tmp_str = GeoIP_country_name_by_id(mod_data->geoip_db, geoip_id); if (tmp_str) { ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_name"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(tmp_str)); ib_field_list_add(geoip_lst, tmp_field); } tmp_str = GeoIP_continent_by_id(geoip_id); if (tmp_str) { ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("continent_code"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(tmp_str)); ib_field_list_add(geoip_lst, tmp_field); } } else { ib_log_debug_tx(tx, "No GeoIP record found."); ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_code"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in("O1")); ib_field_list_add(geoip_lst, tmp_field); ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_code3"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in("O01")); ib_field_list_add(geoip_lst, tmp_field); ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("country_name"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in("Other Country")); ib_field_list_add(geoip_lst, tmp_field); ib_field_create(&tmp_field, tx->mp, IB_FIELD_NAME("continent_code"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in("O1")); ib_field_list_add(geoip_lst, tmp_field); } return IB_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 GeoIP_country_id_by_addr (GeoIP* gi, const char *addr) { return GeoIP_id_by_addr(gi, addr); }
const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) { int country_id; country_id = GeoIP_id_by_addr(gi, addr); return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; return GeoIP_country_name[country_id]; }