From db7f17b1c91696142bf100908cf0697dc6b5ec21 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 5 Nov 2024 10:43:17 +0100 Subject: [PATCH] Added Mac serialization --- src/LocalHost.cpp | 5 ++++- src/Mac.cpp | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/LocalHost.cpp b/src/LocalHost.cpp index 117526e76d92..de096279e36a 100644 --- a/src/LocalHost.cpp +++ b/src/LocalHost.cpp @@ -224,6 +224,8 @@ void LocalHost::addInactiveData() { #endif ndpi_init_serializer(&host_json, ndpi_serialization_format_json); + + ndpi_serialize_string_string(&host_json, "type", "host"); ndpi_serialize_string_string(&host_json, "ip", ip.print(buf, sizeof(buf))); ndpi_serialize_string_uint64(&host_json, "first_seen", get_first_seen()); @@ -731,7 +733,7 @@ void LocalHost::lua_get_fingerprints(lua_State *vm) { #ifdef NTOPNG_PRO void LocalHost::dumpAssetInfo() { char buf[64], mac_buf[32], *json_str = NULL, - *ip = printMask(buf, sizeof(buf)), *mac_ptr; + *ip = printMask(buf, sizeof(buf)), *mac_ptr; ndpi_serializer device_json; u_int32_t json_str_len = 0; @@ -768,6 +770,7 @@ void LocalHost::dumpAssetInfo() { if ((json_str != NULL) && (json_str_len > 0)) { char key[64]; + snprintf(key, sizeof(key), ASSET_LIST_INSERTION_KEY, iface->get_id()); ntop->getRedis()->rpush(key, json_str, 1024); } diff --git a/src/Mac.cpp b/src/Mac.cpp index a75c97eed759..8588ec94a81b 100644 --- a/src/Mac.cpp +++ b/src/Mac.cpp @@ -92,11 +92,32 @@ Mac::~Mac() { if ((!broadcast_mac) && (!special_mac)) dumpToRedis(); if (!special_mac && ntop->getRedis() && ntop->getPrefs()->is_pro_edition()) { - char mac_addr[64], mac_disconnection_key[128]; + char mac_addr[64], mac_disconnection_key[128], *mac, *json_str; + ndpi_serializer device_json; + u_int32_t json_str_len = 0; + char redis_key[64]; + + mac = print(mac_addr, sizeof(mac_addr)); + + ndpi_init_serializer(&device_json, ndpi_serialization_format_json); + ndpi_serialize_string_string(&device_json, "type", "mac"); + ndpi_serialize_string_string(&device_json, "mac", mac); + ndpi_serialize_string_uint32(&device_json, "first_seen", first_seen); + ndpi_serialize_string_uint32(&device_json, "last_seen", last_seen); + + json_str = ndpi_serializer_get_buffer(&device_json, &json_str_len); + if ((json_str != NULL) && (json_str_len > 0)) { + snprintf(redis_key, sizeof(redis_key), OFFLINE_LOCAL_HOSTS_MACS_QUEUE_NAME, iface->get_id()); + ntop->getRedis()->lpush(redis_key, json_str, CONST_MAX_INACTIVE_HOSTS_MAC_QUEUE_LEN); + } + + ndpi_term_serializer(&device_json); + + /* Will be removed */ snprintf(mac_disconnection_key, sizeof(mac_disconnection_key), - (char *)MACS_DISCONNECTION, iface->get_id()); - ntop->getRedis()->lpush(mac_disconnection_key, - print(mac_addr, sizeof(mac_addr)), 0 /* No Trim */); + (char *)MACS_DISCONNECTION, iface->get_id()); + + ntop->getRedis()->lpush(mac_disconnection_key, print(mac_addr, sizeof(mac_addr)), 0 /* No Trim */); } if (model) free(model); @@ -544,7 +565,7 @@ bool Mac::is_hash_entry_state_idle_transition_ready() { ? "true" : "false", time(NULL), last_seen, ntop->getPrefs()->macAddressCacheDuration()); -*/ +*/ return ((getUses() == 0) && is_active_entry_now_idle( ntop->getPrefs()->macAddressCacheDuration())); }