From d03f09f8107b9b51b9379a521b96ccd25d73100c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 6 Apr 2015 02:07:47 -0400
Subject: [PATCH] dht: proprely update receive time

---
 src/dht.cpp | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/dht.cpp b/src/dht.cpp
index c2d9b5f5..ac0070cf 100644
--- a/src/dht.cpp
+++ b/src/dht.cpp
@@ -513,6 +513,7 @@ Dht::newNode(const InfoHash& id, const sockaddr *sa, socklen_t salen, int confir
         if (not n->isExpired(now))
             continue;
         n = cache.getNode(id, sa, salen, confirm ? now : TIME_INVALID, confirm >= 2 ? now : TIME_INVALID);
+        n->received(now, confirm >= 2);
 
         /* Try adding the node to searches */
         trySearchInsert(n);
@@ -553,13 +554,16 @@ Dht::newNode(const InfoHash& id, const sockaddr *sa, socklen_t salen, int confir
             memcpy(&b->cached, sa, salen);
             b->cachedlen = salen;
         }
-
-        return cache.getNode(id, sa, salen, confirm ? now : TIME_INVALID, confirm >= 2 ? now : TIME_INVALID);
+        auto cn = cache.getNode(id, sa, salen, confirm ? now : TIME_INVALID, confirm >= 2 ? now : TIME_INVALID);
+        cn->received(now, confirm >= 2);
+        trySearchInsert(cn);
+        return cn;
     }
 
     /* Create a new node. */
     auto cn = cache.getNode(id, sa, salen, confirm ? now : TIME_INVALID, confirm >= 2 ? now : TIME_INVALID);
     b->nodes.emplace_front(cn);
+    cn->received(now, confirm >= 2);
     trySearchInsert(cn);
     return cn;
 }
@@ -604,7 +608,7 @@ bool
 Dht::Search::removeExpiredNode(time_point now)
 {
     auto e = nodes.end();
-    while (e != nodes.begin()) {
+    while (e != nodes.cbegin()) {
         e = std::prev(e);
         if (e->node->isExpired(now)) {
             nodes.erase(e);
-- 
GitLab