From f48d95822c7810a45daf26854de7262ccc20a940 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Tue, 4 Jul 2006 15:00:54 +0000 Subject: [PATCH] =?utf8?q?Se=20verifica=20si=20expir=C3=B3=20el=20cache=20?= =?utf8?q?y=20se=20lo=20elimina=20si=20as=C3=AD=20fuere.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- practicas/pipi/src/Makefile | 2 +- practicas/pipi/src/nameserver.cpp | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/practicas/pipi/src/Makefile b/practicas/pipi/src/Makefile index c543be5..8ae2d04 100644 --- a/practicas/pipi/src/Makefile +++ b/practicas/pipi/src/Makefile @@ -24,7 +24,7 @@ ifdef debug #CFLAGS += -DDEBUG_QUE #CFLAGS += -DDEBUG_NAME #CFLAGS += -DDEBUG_ROUTE -CFLAGS += -DDEBUG_TRACE +#CFLAGS += -DDEBUG_TRACE CFLAGS += -DDEBUG_RESOLV #CFLAGS += -DDEBUG_LIBTCP endif diff --git a/practicas/pipi/src/nameserver.cpp b/practicas/pipi/src/nameserver.cpp index 886a2fc..959286a 100644 --- a/practicas/pipi/src/nameserver.cpp +++ b/practicas/pipi/src/nameserver.cpp @@ -250,16 +250,26 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& n) #endif return ResolvProtoResponse(RP_RES_NOTFOUND); } - cache_t::const_iterator i = cache.find(n); - // TODO TTL!?! + cache_t::iterator i = cache.find(n); if (i != cache.end()) { + const CacheRecord& cr = i->second; #ifdef DEBUG_RESOLV std::cerr << "NameServer::resolv_direct -> found (en cache): " - << i->second << "\n"; + << cr << "\n"; #endif - const CacheRecord& cr = i->second; - return ResolvProtoResponse(RP_RES_A, cr.ttl, cr.ips); + if (unsigned(time(NULL)) > cr.expire) // Expiró + { + cache.erase(i); +#ifdef DEBUG_RESOLV + std::cerr << " -> pero no sirve, expiró " + "hace unos " << (time(NULL) - cr.expire) << " segundos\n"; +#endif + } + else + { + return ResolvProtoResponse(RP_RES_A, cr.ttl, cr.ips); + } } if (zones.size()) { @@ -449,6 +459,9 @@ void NameServer::req_loop() #endif while (true) { +#ifdef DEBUG_RESOLV + std::cout << "----------------------------------------------------\n"; +#endif Dev::mac_type mac = 0; ResolvProtoRequest req(req_que.receive(mac)); #ifdef DEBUG_RESOLV @@ -494,7 +507,8 @@ std::ostream& operator<< (std::ostream& os, const NameServer::Zone& z) std::ostream& operator<< (std::ostream& os, const NameServer::CacheRecord& cr) { - os << "CacheRecord(ttl=" << cr.ttl << ", records="; + os << "CacheRecord(expire=" << cr.expire << " [now=" << time(NULL) + << "], ttl=" << cr.ttl << ", records="; std::copy(cr.ips.begin(), cr.ips.end(), std::ostream_iterator< IPAddr >(os, ",")); return os << ")"; -- 2.43.0