From 80e9540c2028de3880c1672512cbf5383d2a3808 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Fri, 11 Oct 2024 13:04:01 +0200 Subject: [PATCH] Added acknowledge function for lastseen entries Added function to acknowledge the observation of a lastseen entry in DB. The function both acknowledges the observation and returns the previous state if acknowledgment. Ticket: ENT-11838 Changelog: None Signed-off-by: Lars Erik Wik --- libpromises/lastseen.c | 41 +++++++++++++++++++++++++++++++++++++++++ libpromises/lastseen.h | 9 +++++++++ 2 files changed, 50 insertions(+) diff --git a/libpromises/lastseen.c b/libpromises/lastseen.c index 8863239e8a..35ec8217f3 100644 --- a/libpromises/lastseen.c +++ b/libpromises/lastseen.c @@ -766,3 +766,44 @@ int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent, return 0; } + +/** + * @brief Acknowledge that lastseen host entry is observed. + * @param host_key The host key of the lastseen entry. + * @param already_acked Whether the lastseen entry was already acknowledged. + * @return true if host entry was successfully acknowledged, otherwise false. + */ +bool LastSeenHostAcknowledge(const char *host_key, bool incoming, bool *already_acked) +{ + DBHandle *db = NULL; + if (!OpenDB(&db, dbid_lastseen)) + { + Log(LOG_LEVEL_ERR, "Unable to open lastseen DB"); + return false; + } + + // Update quality-of-connection entry + char key[CF_BUFSIZE]; + NDEBUG_UNUSED int ret = snprintf(key, CF_BUFSIZE, "q%c%s", incoming ? 'i' : 'o', host_key); + assert(ret > 0 && ret < CF_BUFSIZE); + + KeyHostSeen value; + if (!ReadDB(db, key, &value, sizeof(value))) + { + Log(LOG_LEVEL_ERR, "Unable to read key '%s' from lastseen DB", key); + CloseDB(db); + return false; + } + + *already_acked = value.acknowledged; + value.acknowledged = true; + + if (!WriteDB(db, key, &value, sizeof(value))) { + Log(LOG_LEVEL_ERR, "Unable to write key '%s' to lastseen DB", key); + CloseDB(db); + return false; + } + + CloseDB(db); + return true; +} diff --git a/libpromises/lastseen.h b/libpromises/lastseen.h index 6b1823187b..ba207195cb 100644 --- a/libpromises/lastseen.h +++ b/libpromises/lastseen.h @@ -63,4 +63,13 @@ bool IsLastSeenCoherent(void); int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent, char *equivalent, size_t equivalent_size); +/** + * @brief Acknowledge that lastseen host entry is observed. + * @param host_key The host key of the lastseen entry. + * @param incoming Whether it is an incoming or outgoing entry. + * @param already_acked Whether the lastseen entry was already acknowledged. + * @return true if host entry was successfully acknowledged, otherwise false. + */ +bool LastSeenHostAcknowledge(const char *host_key, bool incoming, bool *already_acked); + #endif