diff --git a/include/Host.h b/include/Host.h index 64069e894a..1f62dc31b7 100644 --- a/include/Host.h +++ b/include/Host.h @@ -85,6 +85,7 @@ class Host : public GenericHashEntry { char* printMask(char *str, u_int str_len) { return ip.printMask(str, str_len, isLocalHost()); }; void freeHostData(); virtual void deleteHostData(); + char* get_mac_based_tskey(Mac *mac, char *buf, size_t bufsize); public: Host(NetworkInterface *_iface, char *ipAddress, u_int16_t _vlanId); Host(NetworkInterface *_iface, Mac *_mac, u_int16_t _vlanId, IpAddress *_ip); diff --git a/include/LocalHost.h b/include/LocalHost.h index 1ad3c6d2f6..edd33e7e2c 100644 --- a/include/LocalHost.h +++ b/include/LocalHost.h @@ -39,7 +39,7 @@ class LocalHost : public Host { void freeLocalHostData(); virtual void deleteHostData(); - char * getMacBasedSerializationKey(char *redis_key, size_t size); + char * getMacBasedSerializationKey(Mac *mac, char *redis_key, size_t size); char * getIpBasedSerializationKey(char *redis_key, size_t size); bool deserialize(); bool deserializeFromRedisKey(char *key); diff --git a/src/Host.cpp b/src/Host.cpp index 4e45cca3b6..7ad756590b 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -1342,6 +1342,17 @@ void Host::deleteHostData() { /* *************************************** */ +char* Host::get_mac_based_tskey(Mac *mac, char *buf, size_t bufsize) { + char *k = mac->print(buf, bufsize); + + /* NOTE: it is important to differentiate between v4 and v6 for macs */ + strncat(buf, get_ip()->isIPv4() ? "_v4" : "_v6", bufsize); + + return(k); +} + +/* *************************************** */ + /* TODO merge with get_hostkey after migrating alerts and other stuff */ char* Host::get_tskey(char *buf, size_t bufsize) { char *k; @@ -1349,10 +1360,7 @@ char* Host::get_tskey(char *buf, size_t bufsize) { if(cur_mac && isBroadcastDomainHost() && ntop->getPrefs()->serialize_local_broadcast_hosts_as_macs()) { - k = cur_mac->print(buf, bufsize); - - /* NOTE: it is important to differentiate between v4 and v6 for macs */ - strncat(buf, get_ip()->isIPv4() ? "v4" : "v6", bufsize); + k = get_mac_based_tskey(cur_mac, buf, bufsize); } else k = get_hostkey(buf, bufsize); diff --git a/src/LocalHost.cpp b/src/LocalHost.cpp index fc202630b6..f5df19e92a 100644 --- a/src/LocalHost.cpp +++ b/src/LocalHost.cpp @@ -319,12 +319,12 @@ void LocalHost::deleteHostData() { /* *************************************** */ -char * LocalHost::getMacBasedSerializationKey(char *redis_key, size_t size) { +char * LocalHost::getMacBasedSerializationKey(Mac *mac, char *redis_key, size_t size) { char buf[CONST_MAX_LEN_REDIS_KEY]; - Mac *mac = getMac(); + snprintf(redis_key, size, HOST_BY_MAC_SERIALIZED_KEY, - iface->get_id(), mac ? mac->print(buf, sizeof(buf)) : get_tskey(buf, sizeof(buf))); + iface->get_id(), get_mac_based_tskey(mac, buf, sizeof(buf))); return redis_key; } @@ -342,9 +342,11 @@ char * LocalHost::getIpBasedSerializationKey(char *redis_key, size_t size) { /* *************************************** */ char* LocalHost::getSerializationKey(char *redis_key, size_t size) { - if(isBroadcastDomainHost() && getMac() && + Mac *mac = getMac(); + + if(isBroadcastDomainHost() && mac && ntop->getPrefs()->serialize_local_broadcast_hosts_as_macs()) - return getMacBasedSerializationKey(redis_key, size); + return getMacBasedSerializationKey(mac, redis_key, size); return(getIpBasedSerializationKey(redis_key, size)); } @@ -353,10 +355,11 @@ char* LocalHost::getSerializationKey(char *redis_key, size_t size) { bool LocalHost::deserialize() { char redis_key[CONST_MAX_LEN_REDIS_KEY], *k = NULL; + Mac *mac = getMac(); /* First try to deserialize with the mac based key */ - if(getMac() && ntop->getPrefs()->serialize_local_broadcast_hosts_as_macs()) { - k = getMacBasedSerializationKey(redis_key, sizeof(redis_key)); + if(mac && ntop->getPrefs()->serialize_local_broadcast_hosts_as_macs()) { + k = getMacBasedSerializationKey(mac, redis_key, sizeof(redis_key)); if(deserializeFromRedisKey(k)) { setBroadcastDomainHost();