From 77a15cf8943d5588e17815771420d189f3f8bca8 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 28 May 2006 20:33:36 +0000 Subject: [PATCH] IPAddr. --- practicas/pipi/src/Makefile | 6 +++- practicas/pipi/src/ipaddr.cpp | 55 ++++++++++++++++++++++++++++++ practicas/pipi/src/ipaddr.h | 39 +++++++++++++++++++++ practicas/pipi/src/test_ipaddr.cpp | 14 ++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 practicas/pipi/src/ipaddr.cpp create mode 100644 practicas/pipi/src/ipaddr.h create mode 100644 practicas/pipi/src/test_ipaddr.cpp diff --git a/practicas/pipi/src/Makefile b/practicas/pipi/src/Makefile index 3c5f4e0..d0290d4 100644 --- a/practicas/pipi/src/Makefile +++ b/practicas/pipi/src/Makefile @@ -27,7 +27,7 @@ CXXFLAGS = $(CFLAGS) -fno-inline CC=g++ # Programas -targets=test_send test_recv +targets=test_send test_recv test_ipaddr # Fuentes fuentes ?= $(wildcard *.cpp) @@ -44,6 +44,8 @@ test_send: test_send.o media.o test_recv: test_recv.o media.o +test_ipaddr: test_ipaddr.o ipaddr.o + depend: @makedepend $(fuentes) > /dev/null 2>&1 @@ -52,6 +54,7 @@ clean: # DO NOT DELETE +ipaddr.o: ipaddr.h media.o: media.h frame.h /usr/include/unistd.h /usr/include/features.h media.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h media.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h @@ -66,6 +69,7 @@ media.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h media.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h media.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h media.o: /usr/include/bits/msq.h +test_ipaddr.o: ipaddr.h test_recv.o: ethernetframe.h frame.h media.h /usr/include/unistd.h test_recv.o: /usr/include/features.h /usr/include/sys/cdefs.h test_recv.o: /usr/include/gnu/stubs.h /usr/include/bits/posix_opt.h diff --git a/practicas/pipi/src/ipaddr.cpp b/practicas/pipi/src/ipaddr.cpp new file mode 100644 index 0000000..5f33669 --- /dev/null +++ b/practicas/pipi/src/ipaddr.cpp @@ -0,0 +1,55 @@ + +#include "ipaddr.h" +#include + +/// Constructor +IPAddr::IPAddr(atom a1, atom a2, atom a3, atom a4) +{ + atoms[0] = a1; + atoms[1] = a2; + atoms[2] = a3; + atoms[3] = a4; +} + +/// Constructor +IPAddr::IPAddr(unsigned ip) +{ + atoms[0] = ip >> 24; + atoms[1] = ip >> 16; + atoms[2] = ip >> 8; + atoms[3] = ip; +} + +/// Constructor +IPAddr::IPAddr(std::string ip) throw (std::logic_error) +{ + std::istringstream iss(ip); + for (int i = 0; i < 4; ++i) + { + if (!std::getline(iss, ip, '.')) + throw std::logic_error("Dirección IP inválida"); + atoms[i] = std::atoi(ip.c_str()); + } +} + +/// Operador de casteo a unsigned +IPAddr::operator unsigned () const +{ + return (atoms[0] << 24) + (atoms[1] << 16) + (atoms[2] << 8) + atoms[3]; +} + +/// Operador de casteo a std::string +IPAddr::operator std::string () const +{ + std::ostringstream oss; + oss << unsigned(atoms[0]) << "." << unsigned(atoms[1]) << "." + << unsigned(atoms[2]) << "." << unsigned(atoms[3]); + return oss.str(); +} + +std::ostream& operator<< (std::ostream& os, const IPAddr& ip) +{ + return os << std::string(ip); +} + +// vim: set et sw=4 sts=4 : diff --git a/practicas/pipi/src/ipaddr.h b/practicas/pipi/src/ipaddr.h new file mode 100644 index 0000000..eed830d --- /dev/null +++ b/practicas/pipi/src/ipaddr.h @@ -0,0 +1,39 @@ +#ifndef _IPADDR_H_ +#define _IPADDR_H_ + +#include +#include +#include + +/// Dirección IP +struct IPAddr +{ + + /// Átomo de dirección IP + typedef unsigned char atom; + + /// Representación interna + atom atoms[4]; + + /// Constructor + IPAddr(atom a1, atom a2, atom a3, atom a4); + + /// Constructor + IPAddr(unsigned ip); + + /// Constructor + IPAddr(std::string ip) throw (std::logic_error); + + /// Operador de casteo a unsigned + operator unsigned () const; + + /// Operador de casteo a std::string + operator std::string () const; + +}; + +std::ostream& operator<< (std::ostream& os, const IPAddr& ip); + +#endif // _IPADDR_H_ + +// vim: set et sw=4 sts=4 : diff --git a/practicas/pipi/src/test_ipaddr.cpp b/practicas/pipi/src/test_ipaddr.cpp new file mode 100644 index 0000000..b3a84d9 --- /dev/null +++ b/practicas/pipi/src/test_ipaddr.cpp @@ -0,0 +1,14 @@ +#include "ipaddr.h" +#include + +int main() +{ + IPAddr ip1(0x0a0a0a05); + IPAddr ip2("10.10.10.1"); + IPAddr ip3(10, 10, 10, 2); + std::cout << "IP1 = " << ip1 << "\n"; + std::cout << "IP2 = " << ip2 << "\n"; + std::cout << "IP3 = " << ip3 << "\n"; +} + +// vim: set et sw=4 sts=4 : -- 2.43.0