+
+#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 :