+ return r; // NOTFOUND
+}
+
+/// Consulta a otro name server sobre un nombre
+ResolvProtoResponse NameServer::query(const Name& n, const IPAddr& ip)
+{
+#ifdef DEBUG_TRACE
+ std::cout << "NameServer::query()\n";
+#endif
+ ResolvProtoRequest r(std::string(n), RP_REQ_DIRECT);
+#ifdef DEBUG_RESOLV
+ std::cout << "NameServer::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::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)
+ {
+ 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
+ }
+ }
+ }
+}
+
+void NameServer::send_loop()
+{
+#ifdef DEBUG_TRACE
+ std::cout << "NameServer::send_loop()\n";
+#endif
+ while (true)
+ {
+ 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));
+ }
+}
+
+void NameServer::req_loop()
+{
+#ifdef DEBUG_TRACE
+ std::cout << "NameServer::req_loop()\n";
+#endif
+ while (true)
+ {
+#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));
+ }