X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/c5c7f9d1eedf0faf5ab7581c56ef1ca700d58fb5..HEAD:/practicas/pipi/src/nameserver.cpp?ds=inline diff --git a/practicas/pipi/src/nameserver.cpp b/practicas/pipi/src/nameserver.cpp index 822f6f6..4fd894a 100644 --- a/practicas/pipi/src/nameserver.cpp +++ b/practicas/pipi/src/nameserver.cpp @@ -8,8 +8,6 @@ #include #endif -#define DEBUG_RESOLV - NameServer::Name::Name(const std::string& s) { std::istringstream iss(s); @@ -27,7 +25,7 @@ std::ostream& operator<< (std::ostream& os, const NameServer::Name& name) return os << name.back(); } -NameServer::Name::operator std::string () +NameServer::Name::operator std::string () const { std::stringstream ss; ss << *this; @@ -48,7 +46,7 @@ static std::istream& parsezone(std::istream& is, NameServer::Zone& z) throw std::runtime_error("Error al parsear"); z.name = sname; #ifdef DEBUG_PARSER - std::cerr << "parsezone: IP = " << ip << "\n\n"; + std::cout << "NameServer::parsezone: IP = " << ip << "\n\n"; #endif z.parent = IPAddr(ip); // un record por linea, sin líneas vacías @@ -62,13 +60,13 @@ static std::istream& parsezone(std::istream& is, NameServer::Zone& z) throw std::runtime_error("Error al parsear"); typedef NameServer::Record Rec; #ifdef DEBUG_PARSER - std::cerr << "parsezone: IP = " << ip << "\n\n"; + std::cout << "NameServer::parsezone: IP = " << ip << "\n\n"; #endif Rec r((type == "NS") ? Rec::NS : Rec::A, IPAddr(ip)); z.records.insert(NameServer::Zone::records_t::value_type(key, r)); } #ifdef DEBUG_PARSER - std::cerr << "parsezone: " << z << "\n\n"; + std::cout << "NameServer::parsezone: " << z << "\n\n"; #endif return is; } @@ -96,8 +94,11 @@ void NameServer::Zone::clear() } /// Constructor -NameServer::NameServer(std::istream& is) - throw (std::runtime_error) +NameServer::NameServer(std::istream& is, IPIn& ipin, IPOut& ipout, + DevQue& req_que, DevQue& res_que, DevQue& snd_que) + throw (std::runtime_error): + ipin(ipin), ipout(ipout), req_que(req_que), res_que(res_que), + snd_que(snd_que) { Zone z; while (parsezone(is, z)) @@ -105,9 +106,15 @@ NameServer::NameServer(std::istream& is) zones.push_back(z); z.clear(); #ifdef DEBUG_PARSER - std::cerr << "NameServer: " << z << "\n\n"; + std::cout << "NameServer::NameServer: " << z << "\n\n"; #endif } + +#ifdef DEBUG_RESOLV + std::cout << "NameServer::NameServer: req_que_id = " << req_que.que_id + << ", res_que_id = " << res_que.que_id + << ", snd_que_id = " << snd_que.que_id << "\n"; +#endif } /// Devuelve la parte izquierda de n, si la parte derecha coincide exactamente @@ -135,7 +142,8 @@ name_split(NameServer::Name n, NameServer::Name m) r.push_back(n[i++]); std::reverse(r.begin(), r.end()); #ifdef DEBUG_NAME - std::cerr << "name_split(" << n << ", " << m << ") -> " << r << "\n"; + std::cout << "NameServer::name_split(" << n << ", " << m << ") -> " + << r << "\n"; #endif return r; } @@ -201,8 +209,8 @@ struct search_zone: std::unary_function< NameServer::Zone, bool > if ((local_part.size() > 1) && r.type != NameServer::Record::NS) continue; found = true; - resp.ret = (r.type == NameServer::Record::NS) - ? ResolvProtoResponse::R_NS : ResolvProtoResponse::R_A; + resp.ret = (r.type == NameServer::Record::NS) ? RP_RES_NS + : RP_RES_A; resp.ttl = z.ttl; resp.ips.push_back(r.ip); } @@ -214,8 +222,12 @@ struct search_zone: std::unary_function< NameServer::Zone, bool > /// Resuelve un nombre de forma directa (no recursiva) ResolvProtoResponse NameServer::resolv_direct(const Name& n) { +#ifdef DEBUG_TRACE + std::cout << "NameServer::resolv_direct()\n"; +#endif #ifdef DEBUG_RESOLV - std::cerr << "resolv_direct -> tratando de resolver: " << n << "\n"; + std::cout << "NameServer::resolv_direct -> tratando de resolver: " + << n << "\n"; #endif search_zone zs(n); bool found; @@ -225,59 +237,88 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& n) if (found) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_direct found (local/hijo): " << zs.resp << "\n"; + std::cout << "NameServer::resolv_direct -> found (local/hijo): " + << zs.resp << "\n"; #endif return zs.resp; } if (zs.local) { #ifdef DEBUG_RESOLV - std::cerr << "resolv_direct NOT FOUND (es local pero no existe)\n"; + std::cout << "NameServer::resolv_direct -> NOT FOUND (es local pero " + "no existe)\n"; #endif - return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); + 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 << "resolv_direct found (en cache): " << i->second << "\n"; + std::cout << "NameServer::resolv_direct -> found (en cache): " + << cr << "\n"; #endif - const CacheRecord& cr = i->second; - return ResolvProtoResponse(ResolvProtoResponse::R_A, cr.ttl, cr.ips); + if (unsigned(time(NULL)) > cr.expire) // Expiró + { + cache.erase(i); +#ifdef DEBUG_RESOLV + std::cout << " -> 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()) { + // Busco una zona con padre para ver si puedo "trepar" + for (zones_t::const_iterator i = zones.begin(); i != zones.end(); ++i) + { #ifdef DEBUG_RESOLV - 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); - return rpr; + std::cout << "NameServer::resolv_direct -> evaluando padre " + << i->parent << "\n"; #endif + if (i->parent != IPAddr(0)) + { +#ifdef DEBUG_RESOLV + std::cout << "NameServer::resolv_direct -> found (al padre): " + << i->parent << "\n"; +#endif + ResolvProtoResponse rpr(RP_RES_NS, i->ttl); + rpr.ips.push_back(i->parent); + return rpr; + } + } } #ifdef DEBUG_RESOLV - std::cerr << "resolv_direct NOT FOUND (no hay padre)\n"; + std::cout << "NameServer::resolv_direct -> NOT FOUND (no hay padre)\n"; #endif - return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); + // No hay padre, no puedo hacer nada más + return ResolvProtoResponse(RP_RES_NOTFOUND); } /// Resuelve un nombre de forma recursiva ResolvProtoResponse NameServer::resolv_recursive(const Name& n) { +#ifdef DEBUG_TRACE + std::cout << "NameServer::resolv_recursive()\n"; +#endif ResolvProtoResponse rpr = resolv_direct(n); switch (rpr.ret) { - case ResolvProtoResponse::R_NS: + case RP_RES_NS: #ifdef DEBUG_RESOLV - std::cerr << "resolv_recursive -> redirect a " << rpr << "\n"; + std::cout << "NameServer::resolv_recursive -> redirect a " + << rpr << "\n"; #endif return resolv_recursive_r(n, rpr); // Sigo "bajando" - case ResolvProtoResponse::R_A: + case RP_RES_A: #ifdef DEBUG_RESOLV - std::cerr << "resolv_recursive -> gotcha! " << rpr << "\n"; + std::cout << "NameServer::resolv_recursive -> gotcha! " + << rpr << "\n"; #endif - // TODO agregar a cache break; } return rpr; // Devuelvo el A o NOTFOUND @@ -287,6 +328,9 @@ ResolvProtoResponse NameServer::resolv_recursive(const Name& n) ResolvProtoResponse NameServer::resolv_recursive_r(const Name& n, ResolvProtoResponse rpr) { +#ifdef DEBUG_TRACE + std::cout << "NameServer::resolv_recursive_r()\n"; +#endif ResolvProtoResponse r; for (ResolvProtoResponse::ipvec_t::const_iterator ip = rpr.ips.begin(); ip != rpr.ips.end(); ++ip) @@ -294,27 +338,29 @@ ResolvProtoResponse NameServer::resolv_recursive_r(const Name& n, r = query(n, *ip); switch (r.ret) { - case ResolvProtoResponse::R_NS: + case RP_RES_NS: #ifdef DEBUG_RESOLV - std::cerr << "resolv_recursive_r -> redirect a " << r << "\n"; + std::cout << "NameServer::resolv_recursive_r -> redirect a " + << r << "\n"; #endif return resolv_recursive_r(n, r); // Sigo "bajando" - case ResolvProtoResponse::R_NOTFOUND: + case RP_RES_NOTFOUND: #ifdef DEBUG_RESOLV - std::cerr << "resolv_recursive_r -> NOT FOUND en " << *ip - << ", sigo probando\n"; + std::cout << "NameServer::resolv_recursive_r -> NOT FOUND en " + << *ip << ", sigo probando\n"; #endif break; // Sigo probando del mismo nivel - case ResolvProtoResponse::R_A: + case RP_RES_A: #ifdef DEBUG_RESOLV - std::cerr << "resolv_recursive_r -> gotcha! " << r << "\n"; + std::cout << "NameServer::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"; + std::cout << "NameServer::resolv_recursive_r -> NOT FOUND, no hay más " + "por hacer\n"; #endif return r; // NOTFOUND } @@ -322,33 +368,113 @@ ResolvProtoResponse NameServer::resolv_recursive_r(const Name& n, /// Consulta a otro name server sobre un nombre ResolvProtoResponse NameServer::query(const Name& n, const IPAddr& ip) { - // TODO IMPLEMENTAR!!! +#ifdef DEBUG_TRACE + std::cout << "NameServer::query()\n"; +#endif + ResolvProtoRequest r(std::string(n), RP_REQ_DIRECT); #ifdef DEBUG_RESOLV - std::cerr << "query -> Haciendo query dummy\n"; + std::cout << "NameServer::query -> pidiendo " << r << " a " << ip << "\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")) + // Envía a través de la cola de envío + snd_que.transmit(std::string(r), ip); + Dev::mac_type mac = ip; + std::string buf = res_que.receive(mac); + ResolvProtoResponse resp(buf); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::query -> recibido " << resp << " de " + << ip << "\n"; +#endif + // Agrego a cache + if (resp.ret == RP_RES_A) + cache[n] = CacheRecord(time(NULL) + resp.ttl, resp.ttl, resp.ips); + return resp; +} + +void NameServer::recv_loop() +{ +#ifdef DEBUG_TRACE + std::cout << "NameServer::recv_loop()\n"; +#endif + while (true) { - return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); + IPAddr src, dst; + uint8_t proto; + std::string s = ipin.recv(proto, src, dst); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::recv_loop() -> recibido len=" << s.size() + << " de " << src << " para " << dst << " (proto = " + << unsigned(proto) << ")\n"; +#endif + if (proto == RESOLV_PROTO) // Si es para nosotros + { + rp_pkt_type_t type; + memcpy(&type, s.data(), sizeof(uint8_t)); + switch (type) + { + // Request + case RP_REQ_DIRECT: + case RP_REQ_RECURSIVE: +#ifdef DEBUG_RESOLV + std::cout << "---> " << ResolvProtoRequest(s) << "\n"; +#endif + req_que.transmit(s, src); // Encolo + break; + // Response + default: +#ifdef DEBUG_RESOLV + std::cout << "---> " << ResolvProtoResponse(s) << "\n"; +#endif + res_que.transmit(s, src); // Encolo + } + } } - else if (ip == IPAddr("10.10.10.3")) +} + +void NameServer::send_loop() +{ +#ifdef DEBUG_TRACE + std::cout << "NameServer::send_loop()\n"; +#endif + while (true) { - ResolvProtoResponse r(ResolvProtoResponse::R_NS, 666); - r.ips.push_back(IPAddr("10.10.10.4")); - return r; + Dev::mac_type mac = 0; + std::string buf = snd_que.receive(mac); +#ifdef DEBUG_RESOLV + uint8_t type; + memcpy(&type, buf.data(), sizeof(uint8_t)); + std::cout << "NameServer::send_loop() -> envío "; + if (type == RP_REQ_DIRECT || type == RP_REQ_RECURSIVE) + std::cout << "request " << ResolvProtoRequest(buf) << "\n"; + else + std::cout << "response " << ResolvProtoResponse(buf) << "\n"; +#endif + ipout.send(buf, RESOLV_PROTO, IPAddr(mac)); } - else +} + +void NameServer::req_loop() +{ +#ifdef DEBUG_TRACE + std::cout << "NameServer::req_loop()\n"; +#endif + while (true) { - 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; +#ifdef DEBUG_RESOLV + std::cout << "----------------------------------------------------\n"; +#endif + Dev::mac_type mac = 0; + ResolvProtoRequest req(req_que.receive(mac)); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::req_loop() -> recibido " << req << "\n"; +#endif + ResolvProtoResponse res + = (req.query_type == RP_REQ_DIRECT) + ? resolv_direct(req.name) + : resolv_recursive(req.name); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::req_loop() -> respondo " << res << "\n"; +#endif + snd_que.transmit(std::string(res), IPAddr(mac)); } } @@ -381,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 << ")";