From: Leandro Lucarella Date: Fri, 23 Jun 2006 03:11:46 +0000 (+0000) Subject: Primer intento de resolución de nombres recursivo. Falta el query remoto pero X-Git-Tag: svn_import~29 X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/commitdiff_plain/c5c7f9d1eedf0faf5ab7581c56ef1ca700d58fb5?ds=inline Primer intento de resolución de nombres recursivo. Falta el query remoto pero parece andar bien con un query dummy. --- diff --git a/practicas/pipi/src/nameserver.cpp b/practicas/pipi/src/nameserver.cpp index 1e6e19c..822f6f6 100644 --- a/practicas/pipi/src/nameserver.cpp +++ b/practicas/pipi/src/nameserver.cpp @@ -211,10 +211,11 @@ struct search_zone: std::unary_function< NameServer::Zone, bool > } }; -ResolvProtoResponse NameServer::resolv_next(const Name& n) +/// Resuelve un nombre de forma directa (no recursiva) +ResolvProtoResponse NameServer::resolv_direct(const Name& n) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_next -> tratando de resolver: " << n << "\n"; + std::cerr << "resolv_direct -> tratando de resolver: " << n << "\n"; #endif search_zone zs(n); bool found; @@ -224,14 +225,14 @@ ResolvProtoResponse NameServer::resolv_next(const Name& n) if (found) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_next found (local/hijo): " << zs.resp << "\n"; + std::cerr << "resolv_direct found (local/hijo): " << zs.resp << "\n"; #endif return zs.resp; } if (zs.local) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_next NOT FOUND (es local pero no existe)\n"; + std::cerr << "resolv_direct NOT FOUND (es local pero no existe)\n"; #endif return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); } @@ -240,7 +241,7 @@ ResolvProtoResponse NameServer::resolv_next(const Name& n) if (i != cache.end()) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_next found (en cache): " << i->second << "\n"; + std::cerr << "resolv_direct found (en cache): " << i->second << "\n"; #endif const CacheRecord& cr = i->second; return ResolvProtoResponse(ResolvProtoResponse::R_A, cr.ttl, cr.ips); @@ -248,7 +249,7 @@ ResolvProtoResponse NameServer::resolv_next(const Name& n) if (zones.size()) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_next found (al padre): " << zones.front().parent + std::cerr << "resolv_direct found (al padre): " << zones.front().parent << "\n"; ResolvProtoResponse rpr(ResolvProtoResponse::R_NS, zones.front().ttl); rpr.ips.push_back(zones.front().parent); @@ -256,11 +257,101 @@ ResolvProtoResponse NameServer::resolv_next(const Name& n) #endif } #ifdef DEBUG_RESOLV - std::cerr << "resolv_next NOT FOUND (no hay padre)\n"; + std::cerr << "resolv_direct NOT FOUND (no hay padre)\n"; #endif return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); } +/// Resuelve un nombre de forma recursiva +ResolvProtoResponse NameServer::resolv_recursive(const Name& n) +{ + ResolvProtoResponse rpr = resolv_direct(n); + switch (rpr.ret) + { + case ResolvProtoResponse::R_NS: +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive -> redirect a " << rpr << "\n"; +#endif + return resolv_recursive_r(n, rpr); // Sigo "bajando" + case ResolvProtoResponse::R_A: +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive -> gotcha! " << rpr << "\n"; +#endif + // TODO agregar a cache + break; + } + return rpr; // Devuelvo el A o NOTFOUND +} + +/// Resuelve un nombre de forma recursiva entrando a otros ns +ResolvProtoResponse NameServer::resolv_recursive_r(const Name& n, + ResolvProtoResponse rpr) +{ + ResolvProtoResponse r; + for (ResolvProtoResponse::ipvec_t::const_iterator ip = rpr.ips.begin(); + ip != rpr.ips.end(); ++ip) + { + r = query(n, *ip); + switch (r.ret) + { + case ResolvProtoResponse::R_NS: +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive_r -> redirect a " << r << "\n"; +#endif + return resolv_recursive_r(n, r); // Sigo "bajando" + case ResolvProtoResponse::R_NOTFOUND: +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive_r -> NOT FOUND en " << *ip + << ", sigo probando\n"; +#endif + break; // Sigo probando del mismo nivel + case ResolvProtoResponse::R_A: +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive_r -> gotcha! " << r << "\n"; +#endif + // TODO agregar a cache + return r; // Gotcha! + } + } +#ifdef DEBUG_RESOLV + std::cerr << "resolv_recursive_r -> NOT FOUND, no hay más por hacer\n"; +#endif + return r; // NOTFOUND +} + +/// Consulta a otro name server sobre un nombre +ResolvProtoResponse NameServer::query(const Name& n, const IPAddr& ip) +{ + // TODO IMPLEMENTAR!!! +#ifdef DEBUG_RESOLV + std::cerr << "query -> Haciendo query dummy\n"; +#endif + if (ip == IPAddr("10.10.10.1")) + { + ResolvProtoResponse r(ResolvProtoResponse::R_NS, 500); + r.ips.push_back(IPAddr("10.10.10.2")); + r.ips.push_back(IPAddr("10.10.10.3")); + return r; + } + else if (ip == IPAddr("10.10.10.2")) + { + return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); + } + else if (ip == IPAddr("10.10.10.3")) + { + ResolvProtoResponse r(ResolvProtoResponse::R_NS, 666); + r.ips.push_back(IPAddr("10.10.10.4")); + return r; + } + else + { + ResolvProtoResponse r(ResolvProtoResponse::R_A, 1500); + r.ips.push_back(IPAddr("10.10.10.5")); + r.ips.push_back(IPAddr("10.10.10.6")); + return r; + } +} + std::ostream& operator<< (std::ostream& os, const NameServer::Record::type_t& t) { if (t == NameServer::Record::NS) diff --git a/practicas/pipi/src/nameserver.h b/practicas/pipi/src/nameserver.h index 24b8e86..dbaecf7 100644 --- a/practicas/pipi/src/nameserver.h +++ b/practicas/pipi/src/nameserver.h @@ -80,8 +80,20 @@ struct NameServer /// Constructor NameServer(std::istream& is) throw (std::runtime_error); - /// Resuelve un nombre de forma no recursiva - ResolvProtoResponse resolv_next(const Name& n); + /// Resuelve un nombre de forma directa (no recursiva) + ResolvProtoResponse resolv_direct(const Name& n); + + /// Resuelve un nombre de forma recursiva + ResolvProtoResponse resolv_recursive(const Name& n); + + /// Consulta a otro name server sobre un nombre + ResolvProtoResponse query(const Name&n, const IPAddr& ip); + + private: + /// Resuelve un nombre de forma recursiva entrando a otros ns + ResolvProtoResponse resolv_recursive_r(const Name& n, + ResolvProtoResponse rpr); + }; /// Impresión (para debug) diff --git a/practicas/pipi/src/resolvproto.cpp b/practicas/pipi/src/resolvproto.cpp index d8c94ed..4c45f69 100644 --- a/practicas/pipi/src/resolvproto.cpp +++ b/practicas/pipi/src/resolvproto.cpp @@ -115,10 +115,11 @@ void ResolvProtoResponse::recv(int sockfd) /// Impresión de response std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr) { - if (rpr.ips.empty()) - return os; os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) - << ", ttl=" << rpr.ttl << ", "; + << ", ttl=" << rpr.ttl; + if (rpr.ips.empty()) + return os << ")"; + os << ", "; std::copy(rpr.ips.begin(), rpr.ips.end() - 1, std::ostream_iterator< IPAddr >(os, ", ")); return os << rpr.ips.back() << ")"; diff --git a/practicas/pipi/src/test_nameserver_resolvnext.cpp b/practicas/pipi/src/test_nameserver_resolvnext.cpp index 11fc133..2c62274 100644 --- a/practicas/pipi/src/test_nameserver_resolvnext.cpp +++ b/practicas/pipi/src/test_nameserver_resolvnext.cpp @@ -11,13 +11,14 @@ int main(int argc, char* argv[]) std::ifstream ifs("test_nameserver_zones.txt"); NameServer ns(ifs); std::cout << ns << "\n"; - std::cout << ns.resolv_next(N("tito.mi.super.nombre")) << "\n"; - std::cout << ns.resolv_next(N("juan.mi.super.nombre")) << "\n"; - std::cout << ns.resolv_next(N("pepe.otro.mi.super.nombre")) << "\n"; - std::cout << ns.resolv_next(N("pepe.mas.mi.super.nombre")) << "\n"; - std::cout << ns.resolv_next(N("super.nombre")) << "\n"; - std::cout << ns.resolv_next(N("nada.que.ver")) << "\n"; - std::cout << ns.resolv_next(N("tito.mas.super.nombres")) << "\n"; + std::cout << ns.resolv_direct(N("tito.mi.super.nombre")) << "\n"; + std::cout << ns.resolv_direct(N("juan.mi.super.nombre")) << "\n"; + std::cout << ns.resolv_direct(N("pepe.otro.mi.super.nombre")) << "\n"; + std::cout << ns.resolv_direct(N("pepe.mas.mi.super.nombre")) << "\n"; + std::cout << ns.resolv_direct(N("super.nombre")) << "\n"; + std::cout << ns.resolv_direct(N("nada.que.ver")) << "\n"; + std::cout << ns.resolv_direct(N("tito.mas.super.nombres")) << "\n"; + std::cout << ns.resolv_recursive(N("nada.que.ver")) << "\n"; return 0; }