Remove unneeded libboost-thread-dev build dependency (on glibc)
[senf.git] / Socket / Protocols / INet / INet4Address.cc
index 5495e32..5d50309 100644 (file)
@@ -1,3 +1,5 @@
+// $Id$
+//
 // Copyright (C) 2007 
 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
@@ -28,7 +30,8 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <sys/socket.h>
-#ifdef _REENTRANT
+#include <boost/lexical_cast.hpp>
+#ifd defined(_REENTRANT) && !defined(__GLIBC__)
 #include <boost/thread/mutex.hpp>
 #endif
 
@@ -49,6 +52,10 @@ 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 SyntaxException();
+
     int herr (0);
 
     // If available, we use the reentrant GNU variant. This has the additional advantage, that we
@@ -73,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(); 
     if (ent->h_addrtype != AF_INET)
-        throw SyntaxException();    
+        throw UnknownHostnameException();
 
     // We are only interested in the first address ...
     return senf::INet4Address::from_inaddr(
@@ -113,9 +119,24 @@ prefix_ senf::INet4Address::address_type senf::INet4Address::address()
 }
 
 senf::INet4Address const senf::INet4Address::None;
-senf::INet4Address const senf::INet4Address::Loopback = senf::INet4Address(0x7F000001u);
-senf::INet4Address const senf::INet4Address::Broadcast = senf::INet4Address(0xFFFFFFFFu);
+senf::INet4Address const senf::INet4Address::Loopback (0x7F000001u);
+senf::INet4Address const senf::INet4Address::Broadcast (0xFFFFFFFFu);
 
+///////////////////////////////////////////////////////////////////////////
+// senf::INet4Network
+
+prefix_ senf::INet4Network::INet4Network(std::string s)
+{
+    std::string::size_type i (s.find('/'));
+    if (i == std::string::npos)
+        throw INet4Address::SyntaxException();
+    try {
+        prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
+    } catch (boost::bad_lexical_cast const &) {
+        throw INet4Address::SyntaxException();
+    }
+    address_ = INet4Address(INet4Address::from_string(std::string(s, 0, i)).address() & mask());
+}
 
 ///////////////////////////////////////////////////////////////////////////
 // namespace members