struct search_zone: std::unary_function< NameServer::Zone, bool >
{
bool local;
- NameServer::CacheRecord crecord;
+ ResolvProtoResponse resp;
const NameServer::Name& name;
search_zone(const NameServer::Name& n): local(false), name(n) {}
bool operator() (const NameServer::Zone& z)
if ((local_part.size() > 1) && r.type != NameServer::Record::NS)
continue;
found = true;
- crecord.ttl = z.ttl;
- crecord.records.push_back(r);
+ resp.ret = (r.type == NameServer::Record::NS)
+ ? ResolvProtoResponse::R_NS : ResolvProtoResponse::R_A;
+ resp.ttl = z.ttl;
+ resp.ips.push_back(r.ip);
}
}
return found;
}
};
-void 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;
break;
if (found)
{
- // TODO mandar resultados?
#ifdef DEBUG_RESOLV
- std::cerr << "resolv_next found (local/hijo): " << zs.crecord << "\n";
+ std::cerr << "resolv_direct found (local/hijo): " << zs.resp << "\n";
#endif
- return;
+ return zs.resp;
}
if (zs.local)
{
- // TODO mandar NOT FOUND
#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;
+ return ResolvProtoResponse(ResolvProtoResponse::R_NOTFOUND);
}
cache_t::const_iterator i = cache.find(n);
// TODO TTL!?!
if (i != cache.end())
{
- // TODO mandar resultados?
#ifdef DEBUG_RESOLV
- std::cerr << "resolv_next found (en cache): " << i->second << "\n";
+ std::cerr << "resolv_direct found (en cache): " << i->second << "\n";
#endif
- return;
+ const CacheRecord& cr = i->second;
+ return ResolvProtoResponse(ResolvProtoResponse::R_A, cr.ttl, cr.ips);
}
- // TODO enviar nameserver padre, si no hay padre, mandar NOT FOUND
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_next found (al padre): " << zones.front().parent
+ std::cerr << "resolv_direct -> evaluando padre " << i->parent
<< "\n";
- return;
#endif
+ if (i->parent != IPAddr(0))
+ {
+#ifdef DEBUG_RESOLV
+ std::cerr << "resolv_direct found (al padre): "
+ << i->parent << "\n";
+#endif
+ ResolvProtoResponse rpr(ResolvProtoResponse::R_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(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!
+ }
}
- // TODO enviar NOT FOUND
#ifdef DEBUG_RESOLV
- std::cerr << "resolv_next NOT FOUND (no hay padre)\n";
+ 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)
std::ostream& operator<< (std::ostream& os, const NameServer::CacheRecord& cr)
{
os << "CacheRecord(ttl=" << cr.ttl << ", records=";
- std::copy(cr.records.begin(), cr.records.end(),
- std::ostream_iterator< NameServer::Record >(os, ","));
+ std::copy(cr.ips.begin(), cr.ips.end(),
+ std::ostream_iterator< IPAddr >(os, ","));
return os << ")";
}