1 #include "nameserver.h"
9 static void parsename(const std::string& s, NameServer::name_t& name)
11 std::istringstream iss(s);
13 while (std::getline(iss, tok, '.'))
19 static std::ostream& operator<< (std::ostream& os, const NameServer::name_t& name)
23 std::copy(name.begin(), name.end() - 1,
24 std::ostream_iterator< std::string >(os, "."));
25 return os << name.back();
29 static std::istream& parsezone(std::istream& is, NameServer::Zone& z)
30 throw (std::runtime_error)
32 std::string line, sname, ip;
33 // [dominio] [ttl] [parent ip]
34 while (std::getline(is, line) && (line == "")); // Salteo líneas en blanco
37 std::istringstream iss(line);
38 if (!(iss >> sname >> z.ttl >> ip))
39 throw std::runtime_error("Error al parsear");
40 parsename(sname, z.name);
42 std::cerr << "parsezone: IP = " << ip << "\n\n";
44 z.parent = IPAddr(ip);
45 // un record por linea, sin líneas vacías
47 while (std::getline(is, line) && (line != ""))
51 std::string key, type;
52 if (!(iss >> key >> type >> ip))
53 throw std::runtime_error("Error al parsear");
54 typedef NameServer::Record Rec;
56 std::cerr << "parsezone: IP = " << ip << "\n\n";
58 Rec r((type == "NS") ? Rec::NS : Rec::A, IPAddr(ip));
59 z.records.insert(NameServer::Zone::records_t::value_type(key, r));
62 std::cerr << "parsezone: " << z << "\n\n";
68 NameServer::Zone::Zone(std::string sname, size_t ttl, const IPAddr& parent):
69 ttl(ttl), parent(parent)
71 parsename(sname, name);
75 NameServer::Zone::Zone(std::istream& is)
76 throw (std::runtime_error)
79 if (!parsezone(is, *this))
80 throw std::runtime_error("Error de parser, no hay zona");
84 void NameServer::Zone::clear()
91 NameServer::NameServer(std::istream& is)
92 throw (std::runtime_error)
95 while (parsezone(is, z))
100 std::cerr << "NameServer: " << z << "\n\n";
105 std::ostream& operator<< (std::ostream& os, const NameServer::Record& r)
107 if (r.type == NameServer::Record::NS)
114 std::ostream& operator<< (std::ostream& os,
115 const NameServer::Zone::records_t::value_type& p)
117 return os << p.first << ": " << p.second;
120 std::ostream& operator<< (std::ostream& os, const NameServer::Zone& z)
122 os << "Zone " << z.name << " " << z.ttl << " " << z.parent << "\n";
123 std::copy(z.records.begin(), z.records.end(), std::ostream_iterator<
124 NameServer::Zone::records_t::value_type >(os, "\n"));
128 std::ostream& operator<< (std::ostream& os, const NameServer& ns)
130 os << "NameServer: zones[" << ns.zones.size() << "] (\n\n";
131 std::copy(ns.zones.begin(), ns.zones.end(),
132 std::ostream_iterator< NameServer::Zone >(os, "\n"));
136 // vim: set et sw=4 sts=4 :