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; }
void GeoInfo::release() { if (m_pRegion) GeoIPRegion_delete(m_pRegion); if (m_pCity) GeoIPRecord_delete(m_pCity); if (m_pOrg) free(m_pOrg); if (m_pIsp) free(m_pIsp); }
static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { PyObject * retval; const char * region_name = NULL; retval = PyDict_New(); GeoIP_SetItemString(retval,"country_code",gir->country_code); GeoIP_SetItemString(retval,"region",gir->region); if ( gir->country_code[0] ) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } GeoIP_SetItemString(retval,"region_name",region_name); GeoIPRegion_delete(gir); return 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); }
static void clean_state(geoip_state_t *state) { if (state == NULL) return; if (state->record != NULL) { GeoIPRecord_delete(state->record); state->record = NULL; } if (state->region != NULL) { GeoIPRegion_delete(state->region); state->region = NULL; } if (state->name != NULL) { free (state->name); state->name = NULL; } state->ipnum = 0; state->text = NULL; state->id = 0; }
/** * 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); }
void geoiplookup(GeoIP * gi, char *hostname, int i) { const char *country_code; const char *country_name; const char *domain_name; const char *asnum_name; int netspeed; int country_id; GeoIPRegion *region; GeoIPRecord *gir; const char *org; uint32_t ipnum; ipnum = _GeoIP_lookupaddress(hostname); if (ipnum == 0) { printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname); }else { if (GEOIP_DOMAIN_EDITION == i) { domain_name = GeoIP_name_by_ipnum(gi, ipnum); if (domain_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s\n", GeoIPDBDescription[i], domain_name); _say_range_by_ip(gi, ipnum); free((void *)domain_name); } }else if (GEOIP_LOCATIONA_EDITION == i || GEOIP_ACCURACYRADIUS_EDITION == i || GEOIP_ASNUM_EDITION == i || GEOIP_USERTYPE_EDITION == i || GEOIP_REGISTRAR_EDITION == i || GEOIP_NETSPEED_EDITION_REV1 == i || GEOIP_COUNTRYCONF_EDITION == i || GEOIP_CITYCONF_EDITION == i || GEOIP_REGIONCONF_EDITION == i || GEOIP_POSTALCONF_EDITION == i) { asnum_name = GeoIP_name_by_ipnum(gi, ipnum); if (asnum_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s\n", GeoIPDBDescription[i], asnum_name); _say_range_by_ip(gi, ipnum); free((void *)asnum_name); } }else if (GEOIP_COUNTRY_EDITION == i) { country_id = GeoIP_id_by_ipnum(gi, ipnum); if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) { printf("%s: Invalid database\n", GeoIPDBDescription[i]); return; } country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (country_id == 0) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name); _say_range_by_ip(gi, ipnum); } }else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) { region = GeoIP_region_by_ipnum(gi, ipnum); if (NULL == region || region->country_code[0] == '\0') { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region); _say_range_by_ip(gi, ipnum); } if (region) { GeoIPRegion_delete(region); } }else if (GEOIP_CITY_EDITION_REV0 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA( gir->region), _mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)), _mk_NA(gir->city), _mk_NA( gir->postal_code), gir->latitude, gir->longitude); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } }else if (GEOIP_CITY_EDITION_REV1 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s, %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA( gir->region), _mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)), _mk_NA(gir->city), _mk_NA( gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } }else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) { org = GeoIP_org_by_ipnum(gi, ipnum); if (org == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); }else { printf("%s: %s\n", GeoIPDBDescription[i], org); _say_range_by_ip(gi, ipnum); free((void *)org); } }else if (GEOIP_NETSPEED_EDITION == i) { netspeed = GeoIP_id_by_ipnum(gi, ipnum); if (netspeed == GEOIP_UNKNOWN_SPEED) { printf("%s: Unknown\n", GeoIPDBDescription[i]); }else if (netspeed == GEOIP_DIALUP_SPEED) { printf("%s: Dialup\n", GeoIPDBDescription[i]); }else if (netspeed == GEOIP_CABLEDSL_SPEED) { printf("%s: Cable/DSL\n", GeoIPDBDescription[i]); }else if (netspeed == GEOIP_CORPORATE_SPEED) { printf("%s: Corporate\n", GeoIPDBDescription[i]); } _say_range_by_ip(gi, ipnum); } } }
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; }
void geoiplookup(GeoIP * gi, char *hostname, int i) { const char *country_code; const char *country_name; const char *domain_name; const char *asnum_name; int netspeed; int country_id; GeoIPRegion *region; GeoIPRecord *gir; const char *org; uint32_t ipnum; ipnum = _GeoIP_lookupaddress(hostname); if (ipnum == 0) { printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname); } else { if (GEOIP_DOMAIN_EDITION == i) { domain_name = GeoIP_name_by_ipnum(gi, ipnum); if (domain_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s\n", GeoIPDBDescription[i], domain_name); _say_range_by_ip(gi, ipnum); } } else if (GEOIP_LOCATIONA_EDITION == i || GEOIP_ACCURACYRADIUS_EDITION == i || GEOIP_ASNUM_EDITION == i || GEOIP_USERTYPE_EDITION == i || GEOIP_REGISTRAR_EDITION == i || GEOIP_NETSPEED_EDITION_REV1 == i ) { asnum_name = GeoIP_name_by_ipnum(gi, ipnum); if (asnum_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s\n", GeoIPDBDescription[i], asnum_name); _say_range_by_ip(gi, ipnum); } } else if (GEOIP_COUNTRY_EDITION == i) { country_id = GeoIP_id_by_ipnum(gi, ipnum); country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (country_id == 0) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name); _say_range_by_ip(gi, ipnum); } } else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) { region = GeoIP_region_by_ipnum(gi, ipnum); if (NULL == region || region->country_code[0] == '\0' ) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region); _say_range_by_ip(gi, ipnum); GeoIPRegion_delete(region); } } else if (GEOIP_CITY_EDITION_REV0 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } } else if (GEOIP_CITY_EDITION_REV1 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } } else if (GEOIP_CITYCONFIDENCE_EDITION == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { char country_str[5], region_str[5], city_str[5], postal_str[5]; _mk_conf_str(gir->country_conf, country_str, 5); _mk_conf_str(gir->region_conf, region_str, 5); _mk_conf_str(gir->city_conf, city_str, 5); _mk_conf_str(gir->postal_conf, postal_str, 5); printf("%s: %s, %s, %s, %s, %f, %f, %d, %d, %s, %s, %s, %s\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code, country_str, region_str, city_str, postal_str ); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } } else if (GEOIP_CITYCONFIDENCEDIST_EDITION == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { char country_str[5], region_str[5], city_str[5], postal_str[5], accuracy_radius_str[5]; _mk_conf_str(gir->country_conf, country_str, 5); _mk_conf_str(gir->region_conf, region_str, 5); _mk_conf_str(gir->city_conf, city_str, 5); _mk_conf_str(gir->postal_conf, postal_str, 5); if (gir->accuracy_radius != 1023){ sprintf(accuracy_radius_str, "%d", gir->accuracy_radius ); } else { strcpy(accuracy_radius_str,"N/A");} printf("%s: %s, %s, %s, %s, %f, %f, %d, %d, %s, %s, %s, %s, %s\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code, country_str, region_str, city_str, postal_str, accuracy_radius_str ); _say_range_by_ip(gi, ipnum); GeoIPRecord_delete(gir); } } else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) { org = GeoIP_org_by_ipnum(gi, ipnum); if (org == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s\n", GeoIPDBDescription[i], org); _say_range_by_ip(gi, ipnum); } } else if (GEOIP_NETSPEED_EDITION == i) { netspeed = GeoIP_id_by_ipnum(gi, ipnum); if (netspeed == GEOIP_UNKNOWN_SPEED) { printf("%s: Unknown\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_DIALUP_SPEED) { printf("%s: Dialup\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_CABLEDSL_SPEED) { printf("%s: Cable/DSL\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_CORPORATE_SPEED) { printf("%s: Corporate\n", GeoIPDBDescription[i]); } _say_range_by_ip(gi, ipnum); } else { /* * Silent ignore IPv6 databases. Otherwise we get annoying * messages whenever we have a mixed environment IPv4 and * IPv6 */ /* * printf("Can not handle database type -- try geoiplookup6\n"); */ ; } } }
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; }
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; }
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; }
void geoiplookup(GeoIP * gi, char *hostname, int i) { const char *country_code; const char *country_name; const char *domain_name; int netspeed; int country_id; GeoIPRegion *region; GeoIPRecord *gir; const char *org; uint32_t ipnum; ipnum = _GeoIP_lookupaddress(hostname); if (ipnum == 0) { printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname); } else { if (GEOIP_DOMAIN_EDITION == i) { domain_name = GeoIP_name_by_ipnum(gi, ipnum); if (domain_name == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s\n", GeoIPDBDescription[i], domain_name); } } else if (GEOIP_COUNTRY_EDITION == i) { country_id = GeoIP_id_by_ipnum(gi, ipnum); country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (country_id == 0) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name); } } else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) { region = GeoIP_region_by_ipnum(gi, ipnum); if (NULL == region) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region); GeoIPRegion_delete(region); } } else if (GEOIP_CITY_EDITION_REV0 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude); } } else if (GEOIP_CITY_EDITION_REV1 == i) { gir = GeoIP_record_by_ipnum(gi, ipnum); if (NULL == gir) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude, gir->metro_code, gir->area_code); } } else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) { org = GeoIP_org_by_ipnum(gi, ipnum); if (org == NULL) { printf("%s: IP Address not found\n", GeoIPDBDescription[i]); } else { printf("%s: %s\n", GeoIPDBDescription[i], org); } } else if (GEOIP_NETSPEED_EDITION == i) { netspeed = GeoIP_id_by_ipnum(gi, ipnum); if (netspeed == GEOIP_UNKNOWN_SPEED) { printf("%s: Unknown\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_DIALUP_SPEED) { printf("%s: Dialup\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_CABLEDSL_SPEED) { printf("%s: Cable/DSL\n", GeoIPDBDescription[i]); } else if (netspeed == GEOIP_CORPORATE_SPEED) { printf("%s: Corporate\n", GeoIPDBDescription[i]); } } else { /* * Silent ignore IPv6 databases. Otherwise we get annoying * messages whenever we have a mixed environment IPv4 and * IPv6 */ /* * printf("Can not handle database type -- try geoiplookup6\n"); */ ; } } }