1 #include "resolvproto.h"
7 ResolvProtoRequest::ResolvProtoRequest(std::string name, rp_pkt_type_t query_type):
8 query_type(query_type), name(name)
11 /// Constructor a partir de un buffer
12 ResolvProtoRequest::ResolvProtoRequest(std::string buf)
14 memcpy(&query_type, buf.data(), sizeof(uint8_t));
16 memcpy(&size, buf.data() + sizeof(uint8_t), sizeof(uint16_t));
17 name.assign(buf.data() + sizeof(uint8_t) + sizeof(uint16_t), size);
20 /// Convierte a un buffer
21 ResolvProtoRequest::operator std::string () const
23 std::string buf((char*)&query_type, sizeof(uint8_t));
24 uint16_t size = name.size();
25 buf.append((char*)&size, sizeof(uint16_t));
30 size_t ResolvProtoRequest::packet_size() const
32 return sizeof(uint8_t) + sizeof(uint16_t) + name.size();
35 /// Impresión de request
36 std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr)
38 return os << "ResolvProtoRequest(query_type=" << unsigned(rpr.query_type)
39 << ", name=" << rpr.name << ")";
43 ResolvProtoResponse::ResolvProtoResponse(): ret(RP_RES_NOTFOUND), ttl(0)
48 ResolvProtoResponse::ResolvProtoResponse(std::string buf)
50 memcpy(&ret, buf.data(), sizeof(uint8_t));
51 memcpy(&ttl, buf.data() + sizeof(uint8_t), sizeof(uint32_t));
53 memcpy(&count, buf.data() + sizeof(uint8_t) + sizeof(uint32_t),
56 for (uint8_t i = 0; i < count; ++i)
59 memcpy(&ip, buf.data() + 2 * sizeof(uint8_t)
60 + sizeof(uint32_t) * (i + 1), sizeof(uint32_t));
66 ResolvProtoResponse::ResolvProtoResponse(rp_pkt_type_t ret, uint32_t ttl,
68 ret(ret), ttl(ttl), ips(ips)
71 /// Convierte a buffer
72 ResolvProtoResponse::operator std::string () const
74 std::string buf((char*)&ret, sizeof(uint8_t));
75 buf.append((char*)&ttl, sizeof(uint32_t));
76 uint8_t count = ips.size();
77 buf.append((char*)&count, sizeof(uint8_t));
78 for (ipvec_t::const_iterator i = ips.begin(); i != ips.end(); ++i)
81 buf.append((char*)&ip, sizeof(uint32_t));
86 size_t ResolvProtoResponse::packet_size() const
88 return 2 * sizeof(uint8_t) + (ips.size() + 1) * sizeof(uint32_t);
91 /// Impresión de response
92 std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr)
94 os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret)
95 << ", ttl=" << rpr.ttl;
99 std::copy(rpr.ips.begin(), rpr.ips.end() - 1,
100 std::ostream_iterator< IPAddr >(os, ", "));
101 return os << rpr.ips.back() << ")";
104 // vim: set et sw=4 sts=4 :