X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FProtocols%2FINet%2FINet4Address.cc;h=e162200355540708bccfc488b16636088fa06fd5;hb=a1fdb7bb122f0b05be809a922d4b7ef5e125fa67;hp=ad5160084df8c95dcced91efe708ea2d764b0bcc;hpb=ced8f321adf904c31149162de5f6258c971c7466;p=senf.git diff --git a/Socket/Protocols/INet/INet4Address.cc b/Socket/Protocols/INet/INet4Address.cc index ad51600..e162200 100644 --- a/Socket/Protocols/INet/INet4Address.cc +++ b/Socket/Protocols/INet/INet4Address.cc @@ -1,8 +1,8 @@ // $Id$ // -// Copyright (C) 2007 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY // Stefan Bund // // This program is free software; you can redistribute it and/or modify @@ -30,7 +30,8 @@ #include #include #include -#ifdef _REENTRANT +#include +#if defined(_REENTRANT) && !defined(__GLIBC__) #include #endif @@ -51,10 +52,14 @@ prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s struct in_addr ina; if (::inet_pton(AF_INET,s.c_str(),&ina) > 0) return senf::INet4Address::from_inaddr(ina.s_addr); + + if (s.empty()) + throw AddressSyntaxException(); + int herr (0); // If available, we use the reentrant GNU variant. This has the additional advantage, that we - // can explicitly ask for IpV4 addresses + // can explicitly ask for IPv4 addresses # ifdef __GLIBC__ @@ -75,10 +80,9 @@ prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s # endif // __GLIBC__ if (!ent) - ///\fixme Need to give better exception here - throw SyntaxException(); + throw UnknownHostnameException(s); if (ent->h_addrtype != AF_INET) - throw SyntaxException(); + throw UnknownHostnameException(s); // We are only interested in the first address ... return senf::INet4Address::from_inaddr( @@ -89,7 +93,7 @@ prefix_ bool senf::INet4Address::local() const { address_type l (address()); - return + return (l & 0xFF000000u) == 0x0A000000u || (l & 0xFFF00000u) == 0xAC100000u || (l & 0xFFFF0000u) == 0xA9FE0000u || @@ -118,6 +122,21 @@ senf::INet4Address const senf::INet4Address::None; senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u); senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu); +/////////////////////////////////////////////////////////////////////////// +// senf::INet4Network + +prefix_ senf::INet4Network::INet4Network(std::string const & s) +{ + std::string::size_type i (s.find('/')); + if (i == std::string::npos) + throw AddressSyntaxException(); + try { + prefix_len_ = boost::lexical_cast(std::string(s,i+1)); + } catch (boost::bad_lexical_cast const &) { + throw AddressSyntaxException(); + } + address_ = INet4Address(INet4Address::from_string(std::string(s, 0, i)).address() & mask()); +} /////////////////////////////////////////////////////////////////////////// // namespace members @@ -128,16 +147,30 @@ prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & char buffer[16]; ina.s_addr = addr.inaddr(); ::inet_ntop(AF_INET,&ina,buffer,16); - buffer[15] = 0; + buffer[15] = 0; os << buffer; return os; } +prefix_ std::istream & senf::operator>>(std::istream & is, INet4Address & addr) +{ + std::string s; + if (!(is >> s)) + return is; + try { + addr = INet4Address::from_string(s); + } + catch (AddressException &) { + is.setstate(std::ios::failbit); + } + return is; +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "INet4Address.mpp" - + // Local Variables: // mode: c++ // fill-column: 100