From 46098de47811c686844e5cb8b1d8f1b59586e6b8 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 22 Jun 2006 22:04:10 +0000 Subject: [PATCH] =?utf8?q?Se=20hace=20m=C3=A1s=20gen=C3=A9rico=20el=20prot?= =?utf8?q?ocolo=20del=20resolver=20(para=20que=20pueda=20ser=20usado=20par?= =?utf8?q?a=20comunicar=20el=20resolver=20con=20un=20nameserver=20y=20name?= =?utf8?q?servers=20entre=20s=C3=AD)=20y=20se=20agrega=20una=20doc=20gr?= =?utf8?q?=C3=A1fica=20del=20paquete.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- practicas/pipi/src/resolvproto.cpp | 27 +++++++++------- practicas/pipi/src/resolvproto.h | 41 +++++++++++++++++------- practicas/pipi/src/test_resolvprotos.cpp | 2 +- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/practicas/pipi/src/resolvproto.cpp b/practicas/pipi/src/resolvproto.cpp index fe2f94a..e711004 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,8 @@ 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 @@ -61,8 +61,8 @@ ResolvProtoResponse::ResolvProtoResponse(int fd) } /// Constructor -ResolvProtoResponse::ResolvProtoResponse(ret_t ret): - ret(ret) +ResolvProtoResponse::ResolvProtoResponse(ret_t ret, uint32_t ttl): + ret(ret), ttl(ttl) {} /// Envía por socket @@ -71,6 +71,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,6 +90,8 @@ 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_receive_bin(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t)) throw std::runtime_error("Error al recibir count por socket"); @@ -107,7 +111,8 @@ std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr) { if (rpr.ips.empty()) return os; - os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) << ", "; + os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) + << ", ttl=" << rpr.ttl << ", "; std::copy(rpr.ips.begin(), rpr.ips.end() - 1, std::ostream_iterator< IPAddr >(os, ", ")); return os << rpr.ips.back() << ")"; diff --git a/practicas/pipi/src/resolvproto.h b/practicas/pipi/src/resolvproto.h index 826fa38..7caf4f8 100644 --- a/practicas/pipi/src/resolvproto.h +++ b/practicas/pipi/src/resolvproto.h @@ -7,15 +7,21 @@ #include #include -/// Petición del resolver a un nameserver +/// Petición de un nameserver +/// +------------+------------+------------+ +/// | QUERY_TYPE | SIZE | NAME | +/// +------------+------------+------------+ +/// /-- 1 byte --/- 2 bytes --/- variable -/ struct ResolvProtoRequest { - /// Tipo de petición, por ahora sólo QUERY - enum { TYPE_QUERY }; - - /// Tipo de petición, por ahora sólo QUERY - uint8_t type; + /// Tipo de petición + enum query_type_t + { + QT_DIRECT, ///> Búsqueda directa (inter nameservers) + QT_RECURSIVE ///> Búsqueda recursiva (para resolvers) + }; + uint8_t query_type; /// Nombre a buscar std::string name; @@ -24,7 +30,7 @@ struct ResolvProtoRequest ResolvProtoRequest(int fd); /// Constructor - ResolvProtoRequest(std::string name, uint8_t type = TYPE_QUERY); + ResolvProtoRequest(std::string name, uint8_t query_type = QT_RECURSIVE); /// Envía por socket void send(int sockfd) const throw (std::runtime_error); @@ -38,16 +44,27 @@ struct ResolvProtoRequest std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr); -/// Respuesta del resolver a un nameserver +/// Respuesta de un nameserver +/// +-----------+-----------+-----------+-----------+-----------+-----------+ +/// | RET | TTL | COUNT | IP 1 | ... | IP N | +/// +-----------+-----------+-----------+-----------+-----------+-----------+ +/// /-- 1 byte -/- 4 bytes -/-- 1 byte -/- 4 bytes -/- 4 bytes -/- 4 bytes -/ struct ResolvProtoResponse { /// Resultado de la respuesta - enum ret_t { RET_OK, RET_NOTFOUND, RET_TIMEOUT }; - - /// Resultado de la respuesta + enum ret_t + { + R_A, ///> OK, se devolvió un registro A + R_NS, ///> OK, se devolvió un registro NS (sólo en directo) + R_NOTFOUND, ///> No se encontró + R_TIMEOUT ///> Tardó demasiado la consulta + }; uint8_t ret; + /// TTL (sólo útil para búsquedas (inter nameserver) + uint32_t ttl; + /// IPs devueltas typedef std::vector< IPAddr > ipvec_t; ipvec_t ips; @@ -56,7 +73,7 @@ struct ResolvProtoResponse ResolvProtoResponse(int fd); /// Constructor - ResolvProtoResponse(ret_t ret); + ResolvProtoResponse(ret_t ret, uint32_t ttl); /// Envía por socket void send(int sockfd) const throw (std::runtime_error); diff --git a/practicas/pipi/src/test_resolvprotos.cpp b/practicas/pipi/src/test_resolvprotos.cpp index 7286ab3..54c8c10 100644 --- a/practicas/pipi/src/test_resolvprotos.cpp +++ b/practicas/pipi/src/test_resolvprotos.cpp @@ -11,7 +11,7 @@ int main() close(sfd); ResolvProtoRequest rpr(cfd); std::cout << "Recibimos: " << rpr << "\n"; - ResolvProtoResponse rps(ResolvProtoResponse::RET_OK); + ResolvProtoResponse rps(ResolvProtoResponse::R_A, 600); rps.ips.push_back(IPAddr("10.10.10.2")); rps.ips.push_back(IPAddr("100.20.45.21")); rps.ips.push_back(IPAddr("230.23.62.189")); -- 2.43.0