X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/eca6ac3844ea76056d395c67543ec340709d0f30..78d6e1ce97611abe9f0d083033197773f93c33f7:/practicas/pipi/src/nameserver.cpp?ds=sidebyside diff --git a/practicas/pipi/src/nameserver.cpp b/practicas/pipi/src/nameserver.cpp index cf7dac3..b007975 100644 --- a/practicas/pipi/src/nameserver.cpp +++ b/practicas/pipi/src/nameserver.cpp @@ -27,7 +27,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; @@ -96,8 +96,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)) @@ -201,8 +204,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); } @@ -234,7 +237,7 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& n) #ifdef DEBUG_RESOLV std::cerr << "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!?! @@ -244,7 +247,7 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& 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); + return ResolvProtoResponse(RP_RES_A, cr.ttl, cr.ips); } if (zones.size()) { @@ -261,7 +264,7 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& n) std::cerr << "resolv_direct found (al padre): " << i->parent << "\n"; #endif - ResolvProtoResponse rpr(ResolvProtoResponse::R_NS, i->ttl); + ResolvProtoResponse rpr(RP_RES_NS, i->ttl); rpr.ips.push_back(i->parent); return rpr; } @@ -271,7 +274,7 @@ ResolvProtoResponse NameServer::resolv_direct(const Name& n) std::cerr << "resolv_direct NOT FOUND (no hay padre)\n"; #endif // No hay padre, no puedo hacer nada más - return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); + return ResolvProtoResponse(RP_RES_NOTFOUND); } /// Resuelve un nombre de forma recursiva @@ -280,12 +283,12 @@ ResolvProtoResponse NameServer::resolv_recursive(const Name& n) 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"; #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"; #endif @@ -306,18 +309,18 @@ 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"; #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"; #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"; #endif @@ -334,33 +337,92 @@ 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!!! + ResolvProtoRequest r(std::string(n), RP_REQ_DIRECT); #ifdef DEBUG_RESOLV - std::cerr << "query -> Haciendo query dummy\n"; + std::cerr << "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")) - { - return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND); - } - else if (ip == IPAddr("10.10.10.3")) + // 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::cerr << "query -> recibido " << resp << " de " << ip << "\n"; +#endif + return resp; +} + +void NameServer::recv_loop() +{ + while (true) { - ResolvProtoResponse r(ResolvProtoResponse::R_NS, 666); - r.ips.push_back(IPAddr("10.10.10.4")); - return r; + 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.c_str(), 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 +} + +void NameServer::send_loop() +{ + 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; + if (!req_que.empty()) + { + Dev::mac_type mac = 0; + ResolvProtoRequest req(req_que.receive(mac)); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::send_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::send_loop() -> respondo " << res << "\n"; +#endif + ipout.send(res, RESOLV_PROTO, IPAddr(mac)); + } + else if (!snd_que.empty()) // Hay un request para enviar + { + Dev::mac_type mac = 0; + std::string buf = snd_que.receive(mac); +#ifdef DEBUG_RESOLV + std::cout << "NameServer::send_loop() -> envío request " + << ResolvProtoRequest(buf) << "\n"; +#endif + ipout.send(buf, RESOLV_PROTO, IPAddr(mac)); + } + else // No hay nada, esperamos un rato + { + usleep(10000); + } } }