Add hide findHostsByIP to enforce purge lock on hosts hash

This commit is contained in:
emanuele-f 2017-04-21 16:57:34 +02:00
parent b90545a0ac
commit a91781f66c
5 changed files with 132 additions and 85 deletions

View file

@ -144,7 +144,6 @@ class Host : public GenericHost {
inline float get_longitude() { return(longitude); }
bool isLocalInterfaceAddress();
char* get_name(char *buf, u_int buf_len, bool force_resolution_if_not_found);
inline char* get_string_key(char *buf, u_int buf_len) { return(ip.print(buf, buf_len)); };
char* get_hostkey(char *buf, u_int buf_len, bool force_vlan=false);
bool idle();
void incICMP(u_int8_t icmp_type, u_int8_t icmp_code, bool sent, Host *peer);

View file

@ -187,6 +187,9 @@ class NetworkInterface {
LocalTrafficStats *_localStats, nDPIStats *_ndpiStats,
PacketStats *_pktStats, TcpPacketStats *_tcpPacketStats);
Host* findHostsByIP(AddressTree *allowed_hosts,
char *host_ip, u_int16_t vlan_id);
public:
/**
* @brief A Constructor
@ -380,7 +383,6 @@ class NetworkInterface {
AutonomousSystem *getAS(IpAddress *ipa, bool createIfNotPresent);
Host* getHost(char *host_ip, u_int16_t vlan_id);
bool getHostInfo(lua_State* vm, AddressTree *allowed_hosts, char *host_ip, u_int16_t vlan_id);
bool loadHostAlertPrefs(lua_State* vm, AddressTree *allowed_hosts, char *host_ip, u_int16_t vlan_id);
bool correlateHostActivity(lua_State* vm, AddressTree *allowed_hosts, char *host_ip, u_int16_t vlan_id);
bool similarHostActivity(lua_State* vm, AddressTree *allowed_hosts, char *host_ip, u_int16_t vlan_id);
void findUserFlows(lua_State *vm, char *username);
@ -434,9 +436,6 @@ class NetworkInterface {
void loadScalingFactorPrefs();
void getnDPIFlowsCount(lua_State *vm);
Host* findHostsByIP(AddressTree *allowed_hosts,
char *host_ip, u_int16_t vlan_id);
inline HostHash* get_hosts_hash() { return(hosts_hash); }
inline MacHash* get_macs_hash() { return(macs_hash); }
inline AutonomousSystemHash* get_ases_hash() { return(ases_hash); }
@ -479,6 +478,15 @@ class NetworkInterface {
inline void getSFlowDeviceInfo(lua_State *vm, u_int32_t deviceIP) {
if(interfaceStats) interfaceStats->luaDeviceInfo(vm, deviceIP); else lua_newtable(vm);
};
int refreshHostAlertPrefs(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan);
int resetPeriodicHostStats(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan);
int updateHostTrafficPolicy(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan);
int setHostDumpTrafficPolicy(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan, bool dump_traffic_to_disk);
int getPeerBytes(AddressTree* allowed_networks, lua_State *vm, char *host_ip, u_int16_t host_vlan, u_int32_t peer_key);
int engageReleaseHostAlert(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan, bool engage,
char *engaged_alert_id, AlertType alert_type, AlertLevel alert_severity, const char *alert_json);
int luaEvalFlow(Flow *f, const LuaCallback cb);
inline void forceLuaInterpreterReload() { reloadLuaInterpreter = true; };
inline virtual bool isView() { return(false); };

View file

@ -160,7 +160,7 @@ void Host::initialize(u_int8_t _mac[6], u_int16_t _vlanId, bool init_all) {
asn = 0, asname = NULL;
as = NULL;
longitude = 0, latitude = 0;
k = get_string_key(key, sizeof(key));
k = ip.print(key, sizeof(key));
snprintf(redis_key, sizeof(redis_key), HOST_SERIALIZED_KEY, iface->get_id(), k, vlan_id);
dns = NULL, http = NULL, categoryStats = NULL, top_sites = NULL, old_sites = NULL,
user_activities = NULL, ifa_stats = NULL, icmp = NULL;
@ -833,7 +833,7 @@ void Host::serialize2redis() {
&& (!ip.isEmpty())) {
char *json = serialize();
char host_key[128], key[128];
char *k = get_string_key(host_key, sizeof(host_key));
char *k = ip.print(host_key, sizeof(host_key));
snprintf(key, sizeof(key), HOST_SERIALIZED_KEY, iface->get_id(), k, vlan_id);
ntop->getRedis()->set(key, json, ntop->getPrefs()->get_local_host_cache_duration());
@ -932,20 +932,20 @@ bool Host::addIfMatching(lua_State* vm, AddressTree *ptree, char *key) {
// if(symbolic_name) ntop->getTrace()->traceEvent(TRACE_WARNING, "%s/%s", symbolic_name, ip.print(keybuf, sizeof(keybuf)));
if(strcasestr((r = Utils::formatMac(mac ? mac->get_mac() : NULL, keybuf, sizeof(keybuf))), key)) {
lua_push_str_table_entry(vm, get_string_key(keybuf, sizeof(keybuf)), r);
lua_push_str_table_entry(vm, ip.print(keybuf, sizeof(keybuf)), r);
return(true);
} else if(strcasestr((r = ip.print(keybuf, sizeof(keybuf))), key)) {
if(vlan_id != 0) {
char valuebuf[96];
snprintf(valuebuf, sizeof(valuebuf), "%s@%u", r, vlan_id);
lua_push_str_table_entry(vm, get_string_key(keybuf, sizeof(keybuf)), valuebuf);
lua_push_str_table_entry(vm, ip.print(keybuf, sizeof(keybuf)), valuebuf);
} else
lua_push_str_table_entry(vm, get_string_key(keybuf, sizeof(keybuf)), r);
lua_push_str_table_entry(vm, ip.print(keybuf, sizeof(keybuf)), r);
return(true);
} else if(symbolic_name && strcasestr(symbolic_name, key)) {
lua_push_str_table_entry(vm, get_string_key(keybuf, sizeof(keybuf)), symbolic_name);
lua_push_str_table_entry(vm, ip.print(keybuf, sizeof(keybuf)), symbolic_name);
return(true);
}
@ -1484,7 +1484,7 @@ void Host::refreshHostAlertPrefs() {
if (trigger_host_alerts) {
/* This value does *not* contain vlan information, it is provided separately inside these functions */
key = get_string_key(ip_buf, sizeof(ip_buf));
key = ip.print(ip_buf, sizeof(ip_buf));
loadFlowRateAlertPrefs(key);
loadSynAlertPrefs(key);

View file

@ -1847,34 +1847,11 @@ static int ntop_getsflowdeviceinfo(lua_State* vm) {
/* ****************************************** */
static int ntop_interface_load_host_alert_prefs(lua_State* vm) {
NetworkInterface *ntop_interface = getCurrentInterface(vm);
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
get_host_vlan_info((char*)lua_tostring(vm, 1), &host_ip, &vlan_id, buf, sizeof(buf));
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
if((!ntop_interface) || !ntop_interface->loadHostAlertPrefs(vm, get_allowed_nets(vm), host_ip, vlan_id))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_host_reset_periodic_stats(lua_State* vm) {
NetworkInterface *ntop_interface = getCurrentInterface(vm);
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
@ -1884,12 +1861,10 @@ static int ntop_host_reset_periodic_stats(lua_State* vm) {
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL))
if(!ntop_interface)
return(CONST_LUA_ERROR);
h->resetPeriodicStats();
return(CONST_LUA_OK);
return ntop_interface->resetPeriodicHostStats(get_allowed_nets(vm), host_ip, vlan_id);
}
/* ****************************************** */
@ -2307,7 +2282,6 @@ static int ntop_update_host_traffic_policy(lua_State* vm) {
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
@ -2317,12 +2291,10 @@ static int ntop_update_host_traffic_policy(lua_State* vm) {
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL))
return(CONST_LUA_ERROR);
if(!ntop_interface)
return CONST_LUA_ERROR;
h->updateHostTrafficPolicy(host_ip);
return(CONST_LUA_OK);
return ntop_interface->updateHostTrafficPolicy(get_allowed_nets(vm), host_ip, vlan_id);
}
/* ****************************************** */
@ -2332,7 +2304,6 @@ static int ntop_refresh_host_alerts_configuration(lua_State* vm) {
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
@ -2342,12 +2313,10 @@ static int ntop_refresh_host_alerts_configuration(lua_State* vm) {
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL))
if(!ntop_interface)
return(CONST_LUA_ERROR);
h->refreshHostAlertPrefs();
return(CONST_LUA_OK);
return ntop_interface->refreshHostAlertPrefs(get_allowed_nets(vm), host_ip, vlan_id);
}
/* ****************************************** */
@ -2370,7 +2339,6 @@ static int ntop_set_host_dump_policy(lua_State* vm) {
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;
bool dump_traffic_to_disk;
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
@ -2384,12 +2352,10 @@ static int ntop_set_host_dump_policy(lua_State* vm) {
/* Optional VLAN id */
if(lua_type(vm, 3) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 3);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL))
if(!ntop_interface)
return(CONST_LUA_ERROR);
h->setDumpTrafficPolicy(dump_traffic_to_disk);
return(CONST_LUA_OK);
return ntop_interface->setHostDumpTrafficPolicy(get_allowed_nets(vm), host_ip, vlan_id, dump_traffic_to_disk);
}
/* ****************************************** */
@ -2414,12 +2380,10 @@ static int ntop_get_host_hit_rate(lua_State* vm) {
/* Optional VLAN id */
if(lua_type(vm, 3) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 3);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL))
if(!ntop_interface)
return(CONST_LUA_ERROR);
h->getPeerBytes(vm, peer_key);
return(CONST_LUA_OK);
return ntop_interface->getPeerBytes(get_allowed_nets(vm), vm, host_ip, vlan_id, peer_key);
#else
return(CONST_LUA_ERROR); // not supported
#endif
@ -5016,11 +4980,9 @@ static int ntop_interface_engage_release_host_alert(lua_State* vm, bool engage)
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;
int alert_severity;
int alert_type;
char *alert_json, *engaged_alert_id;
AlertsManager *am;
int ret;
ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);
@ -5040,17 +5002,11 @@ static int ntop_interface_engage_release_host_alert(lua_State* vm, bool engage)
if(ntop_lua_check(vm, __FUNCTION__, 5, LUA_TSTRING)) return(CONST_LUA_ERROR);
alert_json = (char*)lua_tostring(vm, 5);
if((!ntop_interface)
|| ((h = ntop_interface->findHostsByIP(get_allowed_nets(vm), host_ip, vlan_id)) == NULL)
|| ((am = ntop_interface->getAlertsManager()) == NULL))
if(!ntop_interface)
return(CONST_LUA_ERROR);
if(engage)
ret = am->engageHostAlert(h, engaged_alert_id,
(AlertType)alert_type, (AlertLevel)alert_severity, alert_json);
else
ret = am->releaseHostAlert(h, engaged_alert_id,
(AlertType)alert_type, (AlertLevel)alert_severity, alert_json);
ret = ntop_interface->engageReleaseHostAlert(get_allowed_nets(vm), host_ip, vlan_id, engage,
engaged_alert_id, (AlertType)alert_type, (AlertLevel)alert_severity, alert_json);
return ret >= 0 ? CONST_LUA_OK : CONST_LUA_ERROR;
}
@ -5752,7 +5708,6 @@ static const luaL_Reg ntop_interface_reg[] = {
{ "name2id", ntop_interface_name2id },
{ "loadDumpPrefs", ntop_load_dump_prefs },
{ "loadScalingFactorPrefs", ntop_load_scaling_factor_prefs },
{ "loadHostAlertPrefs", ntop_interface_load_host_alert_prefs },
/* Mac */
{ "getMacsInfo", ntop_get_interface_macs_info },

View file

@ -2512,20 +2512,6 @@ bool NetworkInterface::getHostInfo(lua_State* vm,
/* **************************************************** */
bool NetworkInterface::loadHostAlertPrefs(lua_State* vm,
AddressTree *allowed_hosts,
char *host_ip, u_int16_t vlan_id) {
Host *h = findHostsByIP(allowed_hosts, host_ip, vlan_id);
if(h) {
h->refreshHostAlertPrefs();
return(true);
}
return(false);
}
/* **************************************************** */
Host* NetworkInterface::findHostsByIP(AddressTree *allowed_hosts,
char *host_ip, u_int16_t vlan_id) {
if(host_ip != NULL) {
@ -5493,3 +5479,102 @@ bool NetworkInterface::getASInfo(lua_State* vm, u_int32_t asn) {
return ret;
}
/* **************************************** */
int NetworkInterface::refreshHostAlertPrefs(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan) {
Host *h;
int rv;
disablePurge(false);
if ((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL) {
h->refreshHostAlertPrefs();
rv = CONST_LUA_OK;
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};
int NetworkInterface::resetPeriodicHostStats(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan) {
Host *h;
int rv;
disablePurge(false);
if ((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL) {
h->resetPeriodicStats();
rv = CONST_LUA_OK;
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};
int NetworkInterface::updateHostTrafficPolicy(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan) {
Host *h;
int rv;
disablePurge(false);
if ((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL) {
h->updateHostTrafficPolicy(host_ip);
rv = CONST_LUA_OK;
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};
int NetworkInterface::setHostDumpTrafficPolicy(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan, bool dump_traffic_to_disk) {
Host *h;
int rv;
disablePurge(false);
if ((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL) {
h->setDumpTrafficPolicy(dump_traffic_to_disk);
rv = CONST_LUA_OK;
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};
int NetworkInterface::getPeerBytes(AddressTree* allowed_networks, lua_State *vm, char *host_ip, u_int16_t host_vlan, u_int32_t peer_key) {
Host *h;
int rv;
disablePurge(false);
if ((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL) {
h->getPeerBytes(vm, peer_key);
rv = CONST_LUA_OK;
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};
int NetworkInterface::engageReleaseHostAlert(AddressTree* allowed_networks, char *host_ip, u_int16_t host_vlan, bool engage,
char *engaged_alert_id, AlertType alert_type, AlertLevel alert_severity, const char *alert_json) {
Host *h;
AlertsManager *am;
int rv;
disablePurge(false);
if (((h = findHostsByIP(allowed_networks, host_ip, host_vlan)) != NULL)
&& ((am = getAlertsManager()) != NULL)) {
if(engage)
rv = am->engageHostAlert(h, engaged_alert_id,
alert_type, alert_severity, alert_json);
else
rv = am->releaseHostAlert(h, engaged_alert_id,
alert_type, alert_severity, alert_json);
} else
rv = CONST_LUA_ERROR;
enablePurge(false);
return rv;
};