From: Leandro Lucarella Date: Thu, 1 Jun 2006 18:11:47 +0000 (+0000) Subject: Mejora interfaz de IPOut. X-Git-Tag: svn_import~63 X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/commitdiff_plain/127afaea3cf716aa9261e5c2cf129a10acbd1688?ds=inline;hp=f3d349d8dd455a3c7ee639d2ad3152f4bfa49f13 Mejora interfaz de IPOut. --- diff --git a/practicas/pipi/src/ipout.cpp b/practicas/pipi/src/ipout.cpp index 48cbc30..fb60ffd 100644 --- a/practicas/pipi/src/ipout.cpp +++ b/practicas/pipi/src/ipout.cpp @@ -1,6 +1,7 @@ #include "ipout.h" #include "ipheader.h" +#include /// Constructor IPOut::IPOut(const IPAddr& ip, Dev& dev, std::ostream& log): @@ -18,8 +19,8 @@ void IPOut::drop(const std::string& msg, const IPHeader& iph) } /// Envía un paquete IP -bool IPOut::send(const std::string& data, IPAddr& src, IPAddr& dst, - uint8_t proto, bool df, uint8_t ttl, uint16_t id) +bool IPOut::send(const std::string& data, uint8_t proto, IPAddr dst, IPAddr src, + bool df, uint8_t ttl, uint16_t id) throw (std::runtime_error) { // TODO fragmentar @@ -28,6 +29,10 @@ bool IPOut::send(const std::string& data, IPAddr& src, IPAddr& dst, drop("Tamaño de paquete más grande que MTU", data); return false; } + if (!src) + src = ip; + if (!id) + id = get_id(); IPHeader iph(4, IPHeader::header_len() + data.size(), id, df, 0, 0, ttl, proto, src, dst); std::string buf((char*) &iph, sizeof(IPHeader)); @@ -40,4 +45,10 @@ bool IPOut::send(const std::string& data, IPAddr& src, IPAddr& dst, return true; } +/// Obtiene un identificador para el paquete +uint16_t IPOut::get_id() const +{ + return time(NULL); +} + // vim: set et sw=4 sts=4 : diff --git a/practicas/pipi/src/ipout.h b/practicas/pipi/src/ipout.h index 57c139b..61c2111 100644 --- a/practicas/pipi/src/ipout.h +++ b/practicas/pipi/src/ipout.h @@ -29,10 +29,13 @@ struct IPOut void drop(const std::string& msg, const IPHeader& iph); /// Envía un paquete IP - bool send(const std::string& data, IPAddr& src, IPAddr& dst, uint8_t proto, - bool df = 0, uint8_t ttl = 64, uint16_t id = 0) + bool send(const std::string& data, uint8_t proto, IPAddr dst, + IPAddr src = 0, bool df = 0, uint8_t ttl = 64, uint16_t id = 0) throw (std::runtime_error); + /// Obtiene un identificador para el paquete + uint16_t get_id() const; + // Nada de andar copiando... private: IPOut(const IPOut&); diff --git a/practicas/pipi/src/test_ipout.cpp b/practicas/pipi/src/test_ipout.cpp index bc68022..dff222a 100644 --- a/practicas/pipi/src/test_ipout.cpp +++ b/practicas/pipi/src/test_ipout.cpp @@ -15,10 +15,9 @@ int main() int que_id = msgget(DEV_DEFAULT_KEY, IPC_CREAT | 0666); assert(que_id != -1); IPAddr addr("10.10.10.2"); - IPAddr dst("10.10.10.1"); Dev dev(addr); IPOut ipout(addr, dev); - if (ipout.send("hola mundo", addr, dst, 0)) + if (ipout.send("hola mundo", 0, IPAddr("10.10.10.1"))) std::cout << "Enviado 'hola mundo' a 10.10.10.1\n"; else std::cout << "NO SE PUDO ENVIAR 'hola mundo' a 10.10.10.1\n";