CC=g++
# Programas
-targets=test_send test_recv
+targets=test_send test_recv test_ipaddr
# Fuentes
fuentes ?= $(wildcard *.cpp)
test_recv: test_recv.o media.o
+test_ipaddr: test_ipaddr.o ipaddr.o
+
depend:
@makedepend $(fuentes) > /dev/null 2>&1
# 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
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
--- /dev/null
+
+#include "ipaddr.h"
+#include <sstream>
+
+/// 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 :
--- /dev/null
+#ifndef _IPADDR_H_
+#define _IPADDR_H_
+
+#include <string>
+#include <cstdlib>
+#include <stdexcept>
+
+/// 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 :
--- /dev/null
+#include "ipaddr.h"
+#include <iostream>
+
+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 :