void geoip_get_country (const char *ip, char *location) { GeoIPRecord *rec = NULL; const char *country = NULL, *code = NULL, *addr = ip; int geoid = 0; /* Custom GeoIP database */ if (conf.geoip_city_data != NULL && geo_location_data != NULL) { rec = GeoIP_record_by_name (geo_location_data, addr); if (rec) { code = rec->country_code; country = rec->country_name; } } /* Legacy GeoIP database */ else if (geo_location_data != NULL) { geoid = GeoIP_id_by_name (geo_location_data, addr); country = GeoIP_country_name_by_name (geo_location_data, addr); code = GeoIP_code_by_id (geoid); } geoip_set_country (country, code, location); if (rec != NULL) GeoIPRecord_delete (rec); }
void compose_json_dst_host_country(json_t *obj, struct chained_cache *cc) { char empty_string[] = ""; if (cc->primitives.dst_ip_country.id > 0) json_object_set_new_nocheck(obj, "country_ip_dst", json_string(GeoIP_code_by_id(cc->primitives.dst_ip_country.id))); else json_object_set_new_nocheck(obj, "country_ip_dst", json_string(empty_string)); }
/** 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); }
CslGeoIP::CslGeoIP() : m_type(GEOIP_COUNTRY) { for (wxInt32 i = 0;; i++) { const char *code = GeoIP_code_by_id(i); const char *name = GeoIP_name_by_id(i); if (!code || !name) break; m_countryCodes.push_back(C2U(code)); m_countryNames.push_back(C2U(name)); } }
const gchar * sim_geoip_lookup (SimInet *inet) { uint8_t *inet_addr; const gchar *ret; g_return_val_if_fail (SIM_IS_INET (inet), 0); inet_addr = sim_inet_get_in_addr (inet); if (sim_inet_is_ipv4 (inet)) { if (inet_addr[0] || inet_addr[1] || inet_addr[2] || inet_addr[3]) { unsigned long r_addr; r_addr = inet_addr[0] << 24 | inet_addr[1] << 16 | inet_addr[2] << 8 | inet_addr[3]; ret = GeoIP_code_by_id (GeoIP_id_by_ipnum (geoip_db, r_addr)); } else { ret = "--"; } } else { geoipv6_t r_addr; memcpy (r_addr.__in6_u.__u6_addr8, inet_addr, sizeof (geoipv6_t)); ret = GeoIP_code_by_id (GeoIP_id_by_ipnum_v6 (geoipV6_db, r_addr)); } g_free (inet_addr); return ret; }
/* Set country data by geoid into the given `location` buffer based on * the IP version. */ static void geoip_set_country_by_geoid (const char *ip, char *location, GTypeIP type_ip) { const char *country = NULL, *code = NULL, *addr = ip; int geoid = 0; if (geo_location_data == NULL) return; if (!(country = geoip_get_country_by_geoid (addr, type_ip))) goto out; /* return two letter country code */ if (!(geoid = geoip_get_geoid (addr, type_ip))) goto out; code = GeoIP_code_by_id (geoid); out: geoip_set_country (country, code, location); }
/** * 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; }
char *output(Results results) { json_t *jsonObject = json_object(); json_t *hitsObject = json_object(); json_t *visitsObject = json_object(); json_t *continentsArray = json_array(); json_t *countriesArray = json_array(); json_t *hoursArray = json_array(); json_t *httpStatusArray = json_array(); json_t *methodsArray = json_array(); json_t *protocolsArray = json_array(); for (int loop=0; loop<CONTINENTS; loop++) { if (results.continents[loop]) { json_array_append_new(continentsArray, json_pack("{s:s, s:s, s:i}", "data", continentsId[loop], "name", continentsNames[loop], "hits", results.continents[loop])); } } for (int loop=0; loop<COUNTRIES; loop++) { if (results.countries[loop]) { json_array_append_new(countriesArray, json_pack("{s:s, s:s, s:i}", "data", GeoIP_code_by_id(loop), "name", GeoIP_name_by_id(loop), "hits", results.countries[loop])); } } for (int loop=0; loop<24; loop++) { if (results.hours[loop]) { json_array_append_new(hoursArray, json_pack("{s:i, s:i}", "data", loop, "hits", results.hours[loop])); } } for (int loop=0; loop<STATUS_CODE_MAX; loop++) { if (results.status[loop]) { json_array_append_new(httpStatusArray, json_pack("{s:i, s:i}", "data", loop, "hits", results.status[loop])); } } for (int loop=0; loop<METHODS; loop++) { if (results.methods[loop]) { json_array_append_new(methodsArray, json_pack("{s:s, s:i}", "data", methods[loop], "hits", results.methods[loop])); } } for (int loop=0; loop<PROTOCOLS; loop++) { if (results.protocols[loop]) { json_array_append_new(protocolsArray, json_pack("{s:s, s:i}", "data", protocols[loop], "hits", results.protocols[loop])); } } json_object_set_new(hitsObject, "ipv4", json_integer(results.hitsIPv4)); json_object_set_new(hitsObject, "ipv6", json_integer(results.hitsIPv6)); json_object_set_new(hitsObject, "total", json_integer(results.hits)); json_object_set_new(visitsObject, "ipv4", json_integer(results.visitsIPv4)); json_object_set_new(visitsObject, "ipv6", json_integer(results.visitsIPv6)); json_object_set_new(visitsObject, "total", json_integer(results.visits)); json_object_set_new(jsonObject, "date", json_string(results.timeStamp)); json_object_set_new(jsonObject, "generator", json_string(VERSION)); json_object_set_new(jsonObject, "file_name", json_string(results.fileName)); json_object_set_new(jsonObject, "file_size", json_integer(results.fileSize)); json_object_set_new(jsonObject, "processed_lines", json_integer(results.processedLines)); json_object_set_new(jsonObject, "invalid_lines", json_integer(results.invalidLines)); json_object_set_new(jsonObject, "bandwidth", json_integer(results.bandwidth)); json_object_set_new(jsonObject, "runtime", json_real(results.runtime)); json_object_set_new(jsonObject, "hits", hitsObject); json_object_set_new(jsonObject, "visits", visitsObject); json_object_set_new(jsonObject, "continents", continentsArray); json_object_set_new(jsonObject, "countries", countriesArray); json_object_set_new(jsonObject, "hours", hoursArray); json_object_set_new(jsonObject, "methods", methodsArray); json_object_set_new(jsonObject, "protocols", protocolsArray); json_object_set_new(jsonObject, "status", httpStatusArray); return json_dumps(jsonObject, JSON_INDENT(3) | JSON_PRESERVE_ORDER); }