diff --git a/libpromises/lastseen.c b/libpromises/lastseen.c index 8863239e8a..6ad015e670 100644 --- a/libpromises/lastseen.c +++ b/libpromises/lastseen.c @@ -766,3 +766,43 @@ int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent, return 0; } + +static bool OnlyRewriteIfChanged(KeyHostSeen *entry, ARG_UNUSED size_t size, KeyHostSeen *new) +{ + if (entry->acknowledged) + { + return false; + } + + new->acknowledged = true; + new->lastseen = entry->lastseen; + new->Q = entry->Q; + + return true; +} + +bool LastSeenHostAcknowledge(const char *host_key, bool incoming) +{ + 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 (!OverwriteDB(db, key, &value, sizeof(value), (OverwriteCondition)OnlyRewriteIfChanged, &value)) + { + Log(LOG_LEVEL_ERR, "Unable to overwrite 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..786c2cb5ed 100644 --- a/libpromises/lastseen.h +++ b/libpromises/lastseen.h @@ -63,4 +63,12 @@ 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. + * @return true if host entry was successfully acknowledged, otherwise false. + */ +bool LastSeenHostAcknowledge(const char *host_key, bool incoming); + #endif