1 #include "nameserver.h"
13 NameServer::Name::Name(const std::string& s)
15 std::istringstream iss(s);
17 while (std::getline(iss, tok, '.'))
21 std::ostream& operator<< (std::ostream& os, const NameServer::Name& name)
25 std::copy(name.begin(), name.end() - 1,
26 std::ostream_iterator< std::string >(os, "."));
27 return os << name.back();
30 NameServer::Name::operator std::string ()
38 static std::istream& parsezone(std::istream& is, NameServer::Zone& z)
39 throw (std::runtime_error)
41 std::string line, sname, ip;
42 // [dominio] [ttl] [parent ip]
43 while (std::getline(is, line) && (line == "")); // Salteo líneas en blanco
46 std::istringstream iss(line);
47 if (!(iss >> sname >> z.ttl >> ip))
48 throw std::runtime_error("Error al parsear");
51 std::cerr << "parsezone: IP = " << ip << "\n\n";
53 z.parent = IPAddr(ip);
54 // un record por linea, sin líneas vacías
56 while (std::getline(is, line) && (line != ""))
60 std::string key, type;
61 if (!(iss >> key >> type >> ip))
62 throw std::runtime_error("Error al parsear");
63 typedef NameServer::Record Rec;
65 std::cerr << "parsezone: IP = " << ip << "\n\n";
67 Rec r((type == "NS") ? Rec::NS : Rec::A, IPAddr(ip));
68 z.records.insert(NameServer::Zone::records_t::value_type(key, r));
71 std::cerr << "parsezone: " << z << "\n\n";
77 NameServer::Zone::Zone(std::string sname, size_t ttl, const IPAddr& parent):
78 name(sname), ttl(ttl), parent(parent)
83 NameServer::Zone::Zone(std::istream& is)
84 throw (std::runtime_error)
87 if (!parsezone(is, *this))
88 throw std::runtime_error("Error de parser, no hay zona");
92 void NameServer::Zone::clear()
99 NameServer::NameServer(std::istream& is)
100 throw (std::runtime_error)
103 while (parsezone(is, z))
108 std::cerr << "NameServer: " << z << "\n\n";
113 /// Devuelve la parte izquierda de n, si la parte derecha coincide exactamente
114 /// con m, si no devuelve un vector vacío.
115 /// Elemplo: name_split("mi.domi.nio", "domi.nio") == ["mi"]
116 /// name_split("dos.mi.domi.nio", "domi.nio") == ["dos", "mi"]
117 /// name_split("domi.nio", "domi.nio") == []
118 /// name_split("papeli.nio", "domi.nio") == []
119 static NameServer::Name
120 name_split(NameServer::Name n, NameServer::Name m)
123 std::reverse(n.begin(), n.end());
124 std::reverse(m.begin(), m.end());
125 // Si m es más grande o igual que n, no hay nada que hacer
126 if (n.size() <= m.size())
128 // Si no coincide la parte derecha, no hay nada que hacer
129 NameServer::Name::size_type i;
130 for (i = 0; i < m.size(); ++i)
133 // Si era todo igual y sobran cosas, devolvemos lo que "sobra"
136 std::reverse(r.begin(), r.end());
138 std::cerr << "name_split(" << n << ", " << m << ") -> " << r << "\n";
143 /// Devuelve -1 si es un nombre "hijo" (la parte derecha de n está contenida
144 /// completamente en m, pero m y n no son iguales), 0 si m == n y 1 si es
145 /// "padre" (m no coincide con la parte derecha de n).
146 /// Elemplo: namecmp("mi.domi.nio", "domi.nio") == -1
147 /// namecmp("otra.cosa", "domi.nio") == 1
148 /// namecmp("papeli.nio", "domi.nio") == 1
149 /// namecmp("domi.nio", "domi.nio") == 0
152 NC_DIRECT_CHILD, ///> Hijo directo, es decir, está en la zona m
153 NC_CHILD, ///> Hijo indirecto, está en una zona que cuelga de m
154 NC_EQUAL, ///> Es el host de la zona m
155 NC_DIRECT_PARENT, ///> Padre directo, es decir, coincide en parte con m
156 NC_PARENT ///> Padre indirecto, es completamente distinto a m
159 static int name_cmp(const NameServer::Name& n, const NameServer::Name& m)
161 // Si m es más grande que n, seguro es padre
162 if (n.size() < m.size())
164 // Si no coincide la parte derecha, seguro es padre
165 NameServer::Name::size_type i;
166 for (i = m.size(); i > 0; --i)
167 if (n[i-1] != m[i-1])
169 // Si era todo igual y sobran cosas, es hijo
172 // Si no, son iguales.
177 struct search_zone: std::unary_function< NameServer::Zone, bool >
180 NameServer::CacheRecord crecord;
181 const NameServer::Name& name;
182 search_zone(const NameServer::Name& n): local(false), name(n) {}
183 bool operator() (const NameServer::Zone& z)
186 NameServer::Name local_part = name_split(name, z.name);
187 if (!local_part.empty()) // Está en esta zona
190 std::string n = local_part.back(); // Obtengo última parte
192 typedef NameServer::Zone::records_t::const_iterator itt;
193 std::pair<itt, itt> p = z.records.equal_range(n);
194 for (; p.first != p.second; ++p.first)
196 const NameServer::Record& r = (*p.first).second;
197 // Tiene que buscar solo A porque era un nombre
198 if ((local_part.size() == 1) && (r.type != NameServer::Record::A))
200 // Tiene que seguir para abajo, solo busca NS
201 if ((local_part.size() > 1) && r.type != NameServer::Record::NS)
205 crecord.records.push_back(r);
212 void NameServer::resolv_next(const Name& n)
215 std::cerr << "resolv_next -> tratando de resolver: " << n << "\n";
219 for (zones_t::const_iterator i = zones.begin(); i!= zones.end(); ++i)
220 if ((found = zs(*i)))
224 // TODO mandar resultados?
226 std::cerr << "resolv_next found (local/hijo): " << zs.crecord << "\n";
232 // TODO mandar NOT FOUND
234 std::cerr << "resolv_next NOT FOUND (es local pero no existe)\n";
238 cache_t::const_iterator i = cache.find(n);
240 if (i != cache.end())
242 // TODO mandar resultados?
244 std::cerr << "resolv_next found (en cache): " << i->second << "\n";
248 // TODO enviar nameserver padre, si no hay padre, mandar NOT FOUND
252 std::cerr << "resolv_next found (al padre): " << zones.front().parent
257 // TODO enviar NOT FOUND
259 std::cerr << "resolv_next NOT FOUND (no hay padre)\n";
263 std::ostream& operator<< (std::ostream& os, const NameServer::Record::type_t& t)
265 if (t == NameServer::Record::NS)
271 std::ostream& operator<< (std::ostream& os, const NameServer::Record& r)
273 return os << r.type << " " << r.ip;
276 std::ostream& operator<< (std::ostream& os,
277 const NameServer::Zone::records_t::value_type& p)
279 return os << p.first << ": " << p.second;
282 std::ostream& operator<< (std::ostream& os, const NameServer::Zone& z)
284 os << "Zone " << z.name << " " << z.ttl << " " << z.parent << "\n";
285 std::copy(z.records.begin(), z.records.end(), std::ostream_iterator<
286 NameServer::Zone::records_t::value_type >(os, "\n"));
290 std::ostream& operator<< (std::ostream& os, const NameServer::CacheRecord& cr)
292 os << "CacheRecord(ttl=" << cr.ttl << ", records=";
293 std::copy(cr.records.begin(), cr.records.end(),
294 std::ostream_iterator< NameServer::Record >(os, ","));
298 std::ostream& operator<< (std::ostream& os, const NameServer& ns)
300 os << "NameServer: zones[" << ns.zones.size() << "] (\n\n";
301 std::copy(ns.zones.begin(), ns.zones.end(),
302 std::ostream_iterator< NameServer::Zone >(os, "\n"));
306 // vim: set et sw=4 sts=4 :