}
/// 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");
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");
/// 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
}
/// Constructor
-ResolvProtoResponse::ResolvProtoResponse(ret_t ret):
- ret(ret)
+ResolvProtoResponse::ResolvProtoResponse(ret_t ret, uint32_t ttl):
+ ret(ret), ttl(ttl)
{}
/// Envía por socket
{
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");
{
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");
{
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() << ")";
#include <stdexcept>
#include <stdint.h>
-/// 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;
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);
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;
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);