+#ifdef DEBUG_RESOLV
+ std::cerr << "resolv_direct -> tratando de resolver: " << n << "\n";
+#endif
+ search_zone zs(n);
+ bool found;
+ for (zones_t::const_iterator i = zones.begin(); i!= zones.end(); ++i)
+ if ((found = zs(*i)))
+ break;
+ if (found)
+ {
+#ifdef DEBUG_RESOLV
+ std::cerr << "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";
+#endif
+ return ResolvProtoResponse(RP_RES_NOTFOUND);
+ }
+ cache_t::const_iterator i = cache.find(n);
+ // TODO TTL!?!
+ if (i != cache.end())
+ {
+#ifdef DEBUG_RESOLV
+ std::cerr << "resolv_direct found (en cache): " << i->second << "\n";
+#endif
+ const CacheRecord& cr = i->second;
+ 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 -> evaluando padre " << i->parent
+ << "\n";
+#endif
+ if (i->parent != IPAddr(0))
+ {
+#ifdef DEBUG_RESOLV
+ std::cerr << "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";
+#endif
+ // 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)
+{
+ ResolvProtoResponse rpr = resolv_direct(n);
+ switch (rpr.ret)
+ {
+ 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 RP_RES_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 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 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 RP_RES_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)
+{
+ ResolvProtoRequest r(std::string(n), RP_REQ_DIRECT);
+#ifdef DEBUG_RESOLV
+ std::cerr << "query -> pidiendo " << r << " a " << ip << "\n";
+#endif
+ // 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)
+ {
+ 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
+ }
+ }
+ }
+}
+
+void NameServer::send_loop()
+{
+ while (true)
+ {
+ 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);
+ }
+ }
+}
+
+std::ostream& operator<< (std::ostream& os, const NameServer::Record::type_t& t)
+{
+ if (t == NameServer::Record::NS)
+ return os << "NS";