X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/037c7a6a269f053f09e3ebab3c28e4453b576d18..63516fdd4cb64b01971750be1d29d7244b1f9e5e:/practicas/pipi/src/resolvproto.cpp?ds=sidebyside diff --git a/practicas/pipi/src/resolvproto.cpp b/practicas/pipi/src/resolvproto.cpp index 92a5512..4c45f69 100644 --- a/practicas/pipi/src/resolvproto.cpp +++ b/practicas/pipi/src/resolvproto.cpp @@ -11,16 +11,16 @@ ResolvProtoRequest::ResolvProtoRequest(int fd) } /// Constructor -ResolvProtoRequest::ResolvProtoRequest(std::string name, uint8_t type): - type(type), name(name) +ResolvProtoRequest::ResolvProtoRequest(std::string name, uint8_t query_type): + query_type(query_type), name(name) {} /// Envía por socket void ResolvProtoRequest::send(int sockfd) const throw (std::runtime_error) { - if (libtcp_send(sockfd, &type, sizeof(uint8_t)) != sizeof(uint8_t)) - throw std::runtime_error("Error al enviar type por socket"); + if (libtcp_send(sockfd, &query_type, sizeof(uint8_t)) != sizeof(uint8_t)) + throw std::runtime_error("Error al enviar query_type por socket"); uint16_t size = name.size(); if (libtcp_send(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t)) throw std::runtime_error("Error al enviar size por socket"); @@ -32,8 +32,8 @@ void ResolvProtoRequest::send(int sockfd) const void ResolvProtoRequest::recv(int sockfd) throw (std::runtime_error) { - if (libtcp_receive_bin(sockfd, &type, sizeof(uint8_t)) != sizeof(uint8_t)) - throw std::runtime_error("Error al recibir type por socket"); + if (libtcp_receive_bin(sockfd, &query_type, sizeof(uint8_t)) != sizeof(uint8_t)) + throw std::runtime_error("Error al recibir query_type por socket"); uint16_t size; if (libtcp_receive_bin(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t)) throw std::runtime_error("Error al recibir size por socket"); @@ -50,8 +50,13 @@ void ResolvProtoRequest::recv(int sockfd) /// Impresión de request std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr) { - return os << "ResolvProtoRequest(type=" << unsigned(rpr.type) << ", name=" - << rpr.name << ")"; + return os << "ResolvProtoRequest(query_type=" << unsigned(rpr.query_type) + << ", name=" << rpr.name << ")"; +} + +/// Constructor +ResolvProtoResponse::ResolvProtoResponse(): ret(R_NOTFOUND), ttl(0) +{ } /// Constructor @@ -61,8 +66,9 @@ ResolvProtoResponse::ResolvProtoResponse(int fd) } /// Constructor -ResolvProtoResponse::ResolvProtoResponse(ret_t ret): - ret(ret) +ResolvProtoResponse::ResolvProtoResponse(ret_t ret, uint32_t ttl, + const ipvec_t& ips): + ret(ret), ttl(ttl), ips(ips) {} /// Envía por socket @@ -71,6 +77,8 @@ void ResolvProtoResponse::send(int sockfd) const { if (libtcp_send(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t)) throw std::runtime_error("Error al enviar ret por socket"); + if (libtcp_send(sockfd, &ttl, sizeof(uint32_t)) != sizeof(uint32_t)) + throw std::runtime_error("Error al enviar ttl por socket"); uint8_t count = ips.size(); if (libtcp_send(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t)) throw std::runtime_error("Error al enviar count por socket"); @@ -88,15 +96,17 @@ void ResolvProtoResponse::recv(int sockfd) { if (libtcp_receive_bin(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t)) throw std::runtime_error("Error al recibir ret por socket"); + if (libtcp_receive_bin(sockfd, &ttl, sizeof(uint32_t)) != sizeof(uint32_t)) + throw std::runtime_error("Error al recibir ttl por socket"); uint8_t count; - if (libtcp_send(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t)) + if (libtcp_receive_bin(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t)) throw std::runtime_error("Error al recibir count por socket"); ips.clear(); ips.reserve(count); for (uint8_t i = 0; i < count; ++i) { uint32_t ip; - if (libtcp_send(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t)) + if (libtcp_receive_bin(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t)) throw std::runtime_error("Error al recibir IPAddr por socket"); ips.push_back(ip); } @@ -105,10 +115,14 @@ void ResolvProtoResponse::recv(int sockfd) /// Impresión de response std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr) { - os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) << ", "; - std::copy(rpr.ips.begin(), rpr.ips.end(), + os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) + << ", ttl=" << rpr.ttl; + if (rpr.ips.empty()) + return os << ")"; + os << ", "; + std::copy(rpr.ips.begin(), rpr.ips.end() - 1, std::ostream_iterator< IPAddr >(os, ", ")); - return os << ")"; + return os << rpr.ips.back() << ")"; } // vim: set et sw=4 sts=4 :