]> git.llucax.com Git - z.facultad/75.74/practicos.git/blob - practicas/pipi/src/resolvproto.cpp
Bugfix (typo).
[z.facultad/75.74/practicos.git] / practicas / pipi / src / resolvproto.cpp
1 #include "resolvproto.h"
2 #include <cstdlib>
3 #include <algorithm>
4 #include <iterator>
5
6 /// Constructor
7 ResolvProtoRequest::ResolvProtoRequest(std::string name, rp_pkt_type_t query_type):
8     query_type(query_type), name(name)
9 {}
10
11 /// Constructor a partir de un buffer
12 ResolvProtoRequest::ResolvProtoRequest(std::string buf)
13 {
14     memcpy(&query_type, buf.data(), sizeof(uint8_t));
15     uint16_t size;
16     memcpy(&size, buf.data() + sizeof(uint8_t), sizeof(uint16_t));
17     name.assign(buf.data() + sizeof(uint8_t) + sizeof(uint16_t), size);
18 }
19
20 /// Convierte a un buffer
21 ResolvProtoRequest::operator std::string () const
22 {
23     std::string buf((char*)&query_type, sizeof(uint8_t));
24     uint16_t size = name.size();
25     buf.append((char*)&size, sizeof(uint16_t));
26     buf.append(name);
27     return buf;
28 }
29
30 size_t ResolvProtoRequest::packet_size() const
31 {
32     return sizeof(uint8_t) + sizeof(uint16_t) + name.size();
33 }
34
35 /// Impresión de request
36 std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr)
37 {
38     return os << "ResolvProtoRequest(query_type=" << unsigned(rpr.query_type)
39         << ", name=" << rpr.name << ")";
40 }
41
42 /// Constructor
43 ResolvProtoResponse::ResolvProtoResponse(): ret(RP_RES_NOTFOUND), ttl(0)
44 {
45 }
46
47 /// Constructor
48 ResolvProtoResponse::ResolvProtoResponse(std::string buf)
49 {
50     memcpy(&ret, buf.data(), sizeof(uint8_t));
51     memcpy(&ttl, buf.data() + sizeof(uint8_t), sizeof(uint32_t));
52     uint8_t count;
53     memcpy(&count, buf.data() + sizeof(uint8_t) + sizeof(uint32_t),
54             sizeof(uint8_t));
55     ips.reserve(count);
56     for (uint8_t i = 0; i < count; ++i)
57     {
58         uint32_t ip;
59         memcpy(&ip, buf.data() + 2 * sizeof(uint8_t)
60                 + sizeof(uint32_t) * (i + 1), sizeof(uint32_t));
61         ips.push_back(ip);
62     }
63 }
64
65 /// Constructor
66 ResolvProtoResponse::ResolvProtoResponse(rp_pkt_type_t ret, uint32_t ttl,
67         const ipvec_t& ips):
68     ret(ret), ttl(ttl), ips(ips)
69 {}
70
71 /// Convierte a buffer
72 ResolvProtoResponse::operator std::string () const
73 {
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)
79     {
80         uint32_t ip = *i;
81         buf.append((char*)&ip, sizeof(uint32_t));
82     }
83     return buf;
84 }
85
86 size_t ResolvProtoResponse::packet_size() const
87 {
88     return 2 * sizeof(uint8_t) + (ips.size() + 1) * sizeof(uint32_t);
89 }
90
91 /// Impresión de response
92 std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr)
93 {
94     os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret)
95         << ", ttl=" << rpr.ttl;
96     if (rpr.ips.empty())
97         return os << ")";
98     os << ", ";
99     std::copy(rpr.ips.begin(), rpr.ips.end() - 1,
100             std::ostream_iterator< IPAddr >(os, ", "));
101     return os << rpr.ips.back() << ")";
102 }
103
104 // vim: set et sw=4 sts=4 :