1 #include "resolvproto.h"
8 ResolvProtoRequest::ResolvProtoRequest(int fd)
14 ResolvProtoRequest::ResolvProtoRequest(std::string name, uint8_t query_type):
15 query_type(query_type), name(name)
19 void ResolvProtoRequest::send(int sockfd) const
20 throw (std::runtime_error)
22 if (libtcp_send(sockfd, &query_type, sizeof(uint8_t)) != sizeof(uint8_t))
23 throw std::runtime_error("Error al enviar query_type por socket");
24 uint16_t size = name.size();
25 if (libtcp_send(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t))
26 throw std::runtime_error("Error al enviar size por socket");
27 if (libtcp_send(sockfd, name.c_str(), size) != size)
28 throw std::runtime_error("Error al enviar name por socket");
32 void ResolvProtoRequest::recv(int sockfd)
33 throw (std::runtime_error)
35 if (libtcp_receive_bin(sockfd, &query_type, sizeof(uint8_t)) != sizeof(uint8_t))
36 throw std::runtime_error("Error al recibir query_type por socket");
38 if (libtcp_receive_bin(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t))
39 throw std::runtime_error("Error al recibir size por socket");
40 char* buf = (char*) malloc(size);
41 if (libtcp_receive_bin(sockfd, buf, size) != size)
44 throw std::runtime_error("Error al recibir name por socket");
46 name.assign(buf, size);
50 /// Impresión de request
51 std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr)
53 return os << "ResolvProtoRequest(query_type=" << unsigned(rpr.query_type)
54 << ", name=" << rpr.name << ")";
58 ResolvProtoResponse::ResolvProtoResponse(): ret(R_NOTFOUND), ttl(0)
63 ResolvProtoResponse::ResolvProtoResponse(int fd)
69 ResolvProtoResponse::ResolvProtoResponse(ret_t ret, uint32_t ttl,
71 ret(ret), ttl(ttl), ips(ips)
75 void ResolvProtoResponse::send(int sockfd) const
76 throw (std::runtime_error)
78 if (libtcp_send(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t))
79 throw std::runtime_error("Error al enviar ret por socket");
80 if (libtcp_send(sockfd, &ttl, sizeof(uint32_t)) != sizeof(uint32_t))
81 throw std::runtime_error("Error al enviar ttl por socket");
82 uint8_t count = ips.size();
83 if (libtcp_send(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t))
84 throw std::runtime_error("Error al enviar count por socket");
85 for (ipvec_t::const_iterator i = ips.begin(); i != ips.end(); ++i)
88 if (libtcp_send(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t))
89 throw std::runtime_error("Error al enviar IPAddr por socket");
94 void ResolvProtoResponse::recv(int sockfd)
95 throw (std::runtime_error)
97 if (libtcp_receive_bin(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t))
98 throw std::runtime_error("Error al recibir ret por socket");
99 if (libtcp_receive_bin(sockfd, &ttl, sizeof(uint32_t)) != sizeof(uint32_t))
100 throw std::runtime_error("Error al recibir ttl por socket");
102 if (libtcp_receive_bin(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t))
103 throw std::runtime_error("Error al recibir count por socket");
106 for (uint8_t i = 0; i < count; ++i)
109 if (libtcp_receive_bin(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t))
110 throw std::runtime_error("Error al recibir IPAddr por socket");
115 /// Impresión de response
116 std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr)
118 os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret)
119 << ", ttl=" << rpr.ttl;
123 std::copy(rpr.ips.begin(), rpr.ips.end() - 1,
124 std::ostream_iterator< IPAddr >(os, ", "));
125 return os << rpr.ips.back() << ")";
128 // vim: set et sw=4 sts=4 :