11 /// Parsea un archivo de nameservers
12 static std::istream& parseservers(std::istream& is, Resolver::ipvec_t& nss)
13 throw (std::runtime_error)
17 while (std::getline(is, ns))
19 if (ns == "") continue; // Salteo líneas en blanco
20 nss.push_back(IPAddr(ns));
22 std::cout << "parseservers: IP = " << IPAddr(ns) << "\n";
29 Resolver::Resolver(std::istream& is, IPIn& ipin, IPOut& ipout, DevQue& res_que)
30 throw (std::runtime_error):
31 ipin(ipin), ipout(ipout), res_que(res_que)
33 parseservers(is, nameservers);
36 /// Obtiene la(s) IP de un host a través de su nombre de dominio
37 Resolver::result_t Resolver::gethostbyname(const std::string& name,
40 result_t result = NO_RECOVERY;
41 for (ipvec_t::const_iterator ip = nameservers.begin();
42 ip != nameservers.end(); ++ip)
44 ResolvProtoResponse r = query(name, *ip);
51 result = HOST_NOT_FOUND;
55 std::cout << "Resolver::gethostbyname ERROR!!! respuesta "
56 "inesperada: " << r << "\n";
64 /// Consulta a otro name server sobre un nombre
65 ResolvProtoResponse Resolver::query(const std::string& name, const IPAddr& ip)
67 ResolvProtoRequest r(name, RP_REQ_RECURSIVE);
69 std::cout << "query -> pidiendo " << r << " a " << ip << "\n";
71 ipout.send(std::string(r), RESOLV_PROTO, ip);
72 Dev::mac_type mac = ip;
73 std::string buf = res_que.receive(mac);
74 ResolvProtoResponse resp(buf);
76 std::cout << "query -> recibido " << resp << " de " << ip << "\n";
81 void Resolver::recv_loop()
87 std::string s = ipin.recv(proto, src, dst);
89 std::cout << "NameServer::recv_loop() -> recibido len=" << s.size()
90 << " de " << src << " para " << dst << " (proto = "
91 << unsigned(proto) << ")\n";
93 if (proto == RESOLV_PROTO) // Si es para nosotros
96 memcpy(&type, s.data(), sizeof(uint8_t));
101 case RP_REQ_RECURSIVE:
103 std::cout << "---> Recibido un REQUEST!!! Algo esta roto! "
104 << ResolvProtoRequest(s) << "\n";
110 std::cout << "---> " << ResolvProtoResponse(s) << "\n";
112 res_que.transmit(s, src); // Encolo
118 std::cout << "---> Recibido paquete IP desconocido (proto="
119 << unsigned(proto) << "): " << s << "\n";
125 std::ostream& operator<< (std::ostream& os, const Resolver& res)
127 return os << "Resolver(nameservers=" << res.nameservers << ")";
130 std::ostream& operator<< (std::ostream& os, const Resolver::ipvec_t& v)
135 std::copy(v.begin(), v.end() - 1,
136 std::ostream_iterator< IPAddr >(os, ", "));
137 return os << v.back() << "]";
140 // vim: set et sw=4 sts=4 :