Socket/Protocols: Factor out address exception classes
g0dil [Fri, 8 Feb 2008 23:27:35 +0000 (23:27 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@675 270642c3-0616-0410-b53a-bc976706d245

13 files changed:
Socket/Protocols/INet/INet4Address.cc
Socket/Protocols/INet/INet4Address.hh
Socket/Protocols/INet/INet6Address.cc
Socket/Protocols/INet/INet6Address.hh
Socket/Protocols/INet/INet6Address.test.cc
Socket/Protocols/INet/INetAddressing.cc
Socket/Protocols/INet/INetAddressing.hh
Socket/Protocols/INet/INetAddressing.test.cc
Socket/Protocols/Raw/LLAddressing.cc
Socket/Protocols/Raw/LLAddressing.hh
Socket/Protocols/Raw/MACAddress.cc
Socket/Protocols/Raw/MACAddress.hh
Socket/Protocols/Raw/MACAddress.test.cc

index 6d75b16..2086b6e 100644 (file)
@@ -54,7 +54,7 @@ prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s
         return senf::INet4Address::from_inaddr(ina.s_addr);
 
     if  (s.empty())
-        throw SyntaxException();
+        throw AddressSyntaxException();
 
     int herr (0);
 
@@ -129,11 +129,11 @@ prefix_ senf::INet4Network::INet4Network(std::string s)
 {
     std::string::size_type i (s.find('/'));
     if (i == std::string::npos)
-        throw INet4Address::SyntaxException();
+        throw AddressSyntaxException();
     try {
         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
     } catch (boost::bad_lexical_cast const &) {
-        throw INet4Address::SyntaxException();
+        throw AddressSyntaxException();
     }
     address_ = INet4Address(INet4Address::from_string(std::string(s, 0, i)).address() & mask());
 }
index 637a2dc..4784d89 100644 (file)
@@ -34,6 +34,7 @@
 #include <boost/operators.hpp>
 #include "../../../Utils/safe_bool.hh"
 #include "../../../Utils/Tags.hh"
+#include "../AddressExceptions.hh"
 
 //#include "INet4Address.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -87,7 +88,7 @@ namespace senf {
                                              \attention This call may block if \a s represents a
                                                  hostname which must be looked up via some network
                                                  protocol like DNS or NIS
-                                             \throws SyntaxException if the address cannot be
+                                             \throws AddressSyntaxException if the address cannot be
                                                  converted for some reason
                                              \param[in] s Address literal or hostname */
         
@@ -131,19 +132,6 @@ namespace senf {
 
         ////@}
 
-        /** \brief Base-class for INet4Address exceptions */
-        struct AddressException : public std::exception {};
-
-        /** \brief Invalid INet4 address syntax */
-        struct SyntaxException : public AddressException
-        { virtual char const * what() const throw() 
-                { return "invalid INet4 address syntax"; } };
-
-        /** \brief Resolver failure */
-        struct UnknownHostnameException : public AddressException
-        { virtual char const * what() const throw() 
-                { return "failed to resolve INet4 hostname"; } };
-        
     private:
         enum InAddr_t { IsInAddr };
         INet4Address(inaddr_type addr, InAddr_t);
index a167849..ad2e741 100644 (file)
@@ -49,7 +49,7 @@ prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s
         return senf::INet6Address::from_data(&ina.s6_addr[0]);
 
     if (s.empty())
-        throw SyntaxException();
+        throw AddressSyntaxException();
 
     int herr (0);
 
@@ -80,13 +80,7 @@ prefix_ senf::INet6Address senf::INet6Address::from_string(std::string const & s
             &reinterpret_cast<in6_addr*>(*(ent->h_addr_list))->s6_addr[0]);
 
     if (resolve == ResolveINet4)
-        try {
-            return from_inet4address(INet4Address::from_string(s));
-        } catch (INet4Address::SyntaxException const & ex) {
-            throw SyntaxException();
-        } catch (INet4Address::UnknownHostnameException const & ex) {
-            throw UnknownHostnameException();
-        }
+        return from_inet4address(INet4Address::from_string(s));
     else
         throw UnknownHostnameException();
 }
@@ -116,11 +110,11 @@ prefix_ senf::INet6Network::INet6Network(std::string s)
     using boost::lambda::_2;
     std::string::size_type i (s.find('/'));
     if (i == std::string::npos)
-        throw INet6Address::SyntaxException();
+        throw AddressSyntaxException();
     try {
         prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
     } catch (boost::bad_lexical_cast const &) {
-        throw INet6Address::SyntaxException();
+        throw AddressSyntaxException();
     }
     address_ = INet6Address::from_string(std::string(s, 0, i));
     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
index 113ceae..d23a674 100644 (file)
@@ -35,6 +35,7 @@
 #include "../../../Utils/safe_bool.hh"
 #include "../../../Utils/Tags.hh"
 #include "INet4Address.hh"
+#include "../AddressExceptions.hh"
 
 //#include "INet6Address.mpp"
 #include "INet6Address.ih"
@@ -148,7 +149,7 @@ namespace senf {
                                              \attention This call may block if \a s represents a
                                                  hostname which must be looked up via some network
                                                  protocol like DNS or NIS
-                                             \throws SyntaxException if the address cannot be
+                                             \throws AddressSyntaxException if the address cannot be
                                                  converted for some reason
                                              \param[in] s Address literal or hostname 
                                              \param[in] resolve If this is set to \c ResolveINet4,
@@ -236,18 +237,6 @@ namespace senf {
 
         ///@}
 
-        /** \brief Base-class for INet6Address exceptions */
-        struct AddressException : public std::exception {};
-
-        /** \brief Invalid INet4 address syntax */
-        struct SyntaxException : public AddressException
-        { virtual char const * what() const throw() 
-                { return "invalid INet6 address syntax"; } };
-
-        /** \brief Resolver failure */
-        struct UnknownHostnameException : public AddressException
-        { virtual char const * what() const throw() 
-                { return "failed to resolve INet6 hostname"; } };
     };
 
     /** \brief Output INet6Address instance as it's string representation
index 0df239b..6a6c1d7 100644 (file)
@@ -40,6 +40,8 @@ BOOST_AUTO_UNIT_TEST(inet6Address)
 {
     using senf::INet6Address;
     using senf::INet4Address;
+    using senf::AddressSyntaxException;
+    using senf::UnknownHostnameException;
 
     {
         INet6Address addr1 (INet6Address::from_string("0102:0304:0506:0708:090A:0B0C:0D0E:0F00"));
@@ -69,7 +71,7 @@ BOOST_AUTO_UNIT_TEST(inet6Address)
         addr1 = INet6Address::None;
         addr2 = INet6Address::from_string("::");
         BOOST_CHECK_EQUAL( addr1, addr2 );
-        BOOST_CHECK_THROW( INet6Address::from_string(""), INet6Address::SyntaxException );
+        BOOST_CHECK_THROW( INet6Address::from_string(""), AddressSyntaxException );
         BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(addr1), "::" );
         unsigned char data[] = { 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x21, 0 };
         INet6Address addr3 (INet6Address::from_data(data));
@@ -77,7 +79,7 @@ BOOST_AUTO_UNIT_TEST(inet6Address)
         BOOST_CHECK_EQUAL( INet6Address::from_inet4address(INet4Address(0x01020304)),
                            INet6Address::from_string("::ffff:1.2.3.4") );
 
-        BOOST_CHECK_THROW( INet6Address::from_string("1.2.3.4"), INet6Address::UnknownHostnameException );
+        BOOST_CHECK_THROW( INet6Address::from_string("1.2.3.4"), UnknownHostnameException );
         BOOST_CHECK_EQUAL( INet6Address::from_string("1.2.3.4", INet6Address::ResolveINet4),
                            INet6Address::from_string("::ffff:1.2.3.4") );
     }
index 2033048..dd5d6e1 100644 (file)
@@ -48,19 +48,14 @@ prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & addr)
     clear();
     unsigned i = addr.find(':');
     if (i == std::string::npos)
-        throw SyntaxException();
+        throw AddressSyntaxException();
     try {
         port(boost::lexical_cast< ::u_int16_t >(std::string(addr,i+1)));
     }
     catch (boost::bad_lexical_cast const &) {
-        throw SyntaxException();
-    }
-    try {
-        address(INet4Address::from_string(std::string(addr,0,i)));
-    }
-    catch (INet4Address::SyntaxException const &) {
-        throw SyntaxException();
+        throw AddressSyntaxException();
     }
+    address(INet4Address::from_string(std::string(addr,0,i)));
 }
 
 prefix_ senf::INet4SocketAddress::INet4SocketAddress(INet4Address const & addr, unsigned p)
@@ -96,21 +91,17 @@ prefix_ senf::INet6SocketAddress::INet6SocketAddress(std::string const & addr,
     
     boost::smatch match;
     if (! regex_match(addr, match, addressRx))
-        throw SyntaxException();
+        throw AddressSyntaxException();
 
     if (match[ZoneId].matched)
         assignIface(match[ZoneId]);
 
     sockaddr_.sin6_port = htons(boost::lexical_cast<boost::uint16_t>(match[Port]));
 
-    try {
-        INet6Address a (INet6Address::from_string(
-                            match[NumericAddr].matched ? match[NumericAddr] : match[Hostname],
-                            resolve));
-        std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
-    } catch (INet6Address::SyntaxException const &) {
-          throw SyntaxException();
-    }
+    INet6Address a (INet6Address::from_string(
+                        match[NumericAddr].matched ? match[NumericAddr] : match[Hostname],
+                        resolve));
+    std::copy(a.begin(), a.end(), &sockaddr_.sin6_addr.s6_addr[0]);
 }
 
 prefix_ bool senf::INet6SocketAddress::operator==(INet6SocketAddress const & other)
@@ -154,7 +145,7 @@ prefix_ void senf::INet6SocketAddress::assignIface(std::string const & iface)
     else {
         sockaddr_.sin6_scope_id = if_nametoindex(iface.c_str());
         if (sockaddr_.sin6_scope_id == 0)
-            throw SyntaxException();
+            throw AddressSyntaxException();
     }
 }
 
index 5bf7f45..9a30aff 100644 (file)
@@ -67,9 +67,9 @@ namespace senf {
                                              initialize the host and port members. Since it uses the
                                              INet4Address::from_string constructor, this call may
                                              block while waiting for the resolver.
-                                             \throws SyntaxException if the address syntax is
+                                             \throws AddressSyntaxException if the address syntax is
                                                  invalid
-                                             \throws INet4Address::UnknownHostnameException if the
+                                             \throws UnknownHostnameException if the
                                                  address cannot be resolved. */
 
         INet4SocketAddress(INet4Address const & addr, unsigned port); 
@@ -99,10 +99,6 @@ namespace senf {
 
         /// @}
 
-        struct SyntaxException : public std::exception
-        { virtual char const * what() const throw() 
-                { return "Invalid IPv4 socket address syntax"; } };
-
     private:
         struct ::sockaddr_in addr_;
     };
@@ -158,9 +154,9 @@ namespace senf {
         explicit INet6SocketAddress(std::string const & addr, 
                                     INet6Address::Resolve_t resolve = INet6Address::ResolveINet6);
                                         ///< Initialize/convert from string representation
-                                        /**< \throws SyntaxException if the address syntax is
+                                        /**< \throws AddressSyntaxException if the address syntax is
                                                  invalid
-                                             \throws INet6Address::UnknownHostnameException if the
+                                             \throws UnknownHostnameException if the
                                                  address cannot be resolved.
                                              \param[in] addr Address to parse
                                              \param[in] resolve If this is
@@ -198,10 +194,6 @@ namespace senf {
 
         ///@}
 
-        struct SyntaxException : public std::exception
-        { virtual char const * what() const throw() 
-                { return "Invalid IPv6 socket address syntax"; } };
-
     protected:
 
     private:
index 225aae9..29d5c55 100644 (file)
@@ -38,6 +38,7 @@ BOOST_AUTO_UNIT_TEST(inet4SocketAddress)
 {
     using senf::INet4SocketAddress;
     using senf::INet4Address;
+    using senf::AddressSyntaxException;
 
     {
         INet4SocketAddress addr;
@@ -56,10 +57,10 @@ BOOST_AUTO_UNIT_TEST(inet4SocketAddress)
     BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345"), 
                        INet4SocketAddress(INet4Address::Loopback,12345) );
 
-    BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1"), INet4SocketAddress::SyntaxException );
-    BOOST_CHECK_THROW( INet4SocketAddress("foo:bar"), INet4SocketAddress::SyntaxException );
-    BOOST_CHECK_THROW( INet4SocketAddress(":12345"), INet4SocketAddress::SyntaxException );
-    BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1:1234a"), INet4SocketAddress::SyntaxException );
+    BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1"), AddressSyntaxException );
+    BOOST_CHECK_THROW( INet4SocketAddress("foo:bar"), AddressSyntaxException );
+    BOOST_CHECK_THROW( INet4SocketAddress(":12345"), AddressSyntaxException );
+    BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1:1234a"), AddressSyntaxException );
 
     BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345").address(), INet4Address::Loopback );
     BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345").port(), 12345u );
@@ -83,6 +84,7 @@ BOOST_AUTO_UNIT_TEST(inet6SocketAddress)
 {
     using senf::INet6Address;
     using senf::INet6SocketAddress;
+    using senf::AddressSyntaxException;
 
     {
         INet6SocketAddress addr;
@@ -116,11 +118,11 @@ BOOST_AUTO_UNIT_TEST(inet6SocketAddress)
         BOOST_CHECK_EQUAL( addr.port(), 100u );
         addr.address(INet6Address::from_string("::2"));
         BOOST_CHECK_EQUAL( addr.address(), INet6Address::from_string("::2") );
-        BOOST_CHECK_THROW( senf::INet6SocketAddress(""), INet6SocketAddress::SyntaxException );
+        BOOST_CHECK_THROW( senf::INet6SocketAddress(""), AddressSyntaxException );
         BOOST_CHECK_THROW( senf::INet6SocketAddress("[::1]"), 
-                           INet6SocketAddress::SyntaxException );
+                           AddressSyntaxException );
         BOOST_CHECK_THROW( senf::INet6SocketAddress("[::1]1234"), 
-                           INet6SocketAddress::SyntaxException );
+                           AddressSyntaxException );
         addr = senf::INet6SocketAddress("[12::21%lo]:12345");
         BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(addr), "[12::21%lo]:12345" );
         BOOST_CHECK_EQUAL( addr.address(), INet6Address::from_string("12::21") );
index d1cc710..97316c9 100644 (file)
@@ -35,6 +35,7 @@
 #include <boost/algorithm/string/finder.hpp>
 
 #include "../../../Utils/Exception.hh"
+#include "../AddressExceptions.hh"
 
 //#include "LLAddressing.mpp"
 #define prefix_
@@ -47,7 +48,7 @@ prefix_ std::string senf::LLSocketAddress::interface()
         return std::string();
     char name[IFNAMSIZ];
     if (! ::if_indextoname(addr_.sll_ifindex, name))
-        throw InvalidLLSocketAddressException();
+        throw AddressSyntaxException();
     return std::string(name);
 }
 
@@ -58,7 +59,7 @@ prefix_ void senf::LLSocketAddress::interface(std::string const & iface)
     else {
         addr_.sll_ifindex = if_nametoindex(iface.c_str());
         if (addr_.sll_ifindex == 0)
-            throw InvalidLLSocketAddressException();
+            throw AddressSyntaxException();
     }
 }
 
index 23527a7..2a796b5 100644 (file)
@@ -126,12 +126,6 @@ namespace senf {
         struct ::sockaddr_ll addr_;
     };
 
-    /** \brief Signal invalid link local address syntax
-        \related LLSocketAddress
-     */
-    struct InvalidLLSocketAddressException : public std::exception
-    { char const * what() const throw() { return "invalid ll address"; } };
-
     /// @}
 
     /// \addtogroup policy_impl_group
index 4341bb3..4ab2e3f 100644 (file)
@@ -41,26 +41,26 @@ namespace {
     boost::uint8_t hexToNibble(char c)
     {
         if (c<'0')
-            throw senf::MACAddress::SyntaxException();
+            throw senf::AddressSyntaxException();
         else if (c<='9')
             return c-'0';
         else if (c<'A')
-            throw senf::MACAddress::SyntaxException();
+            throw senf::AddressSyntaxException();
         else if (c<='F')
             return c-'A'+10;
         else if (c<'a')
-            throw senf::MACAddress::SyntaxException();
+            throw senf::AddressSyntaxException();
         else if (c<='f')
             return c-'a'+10;
         else
-            throw senf::MACAddress::SyntaxException();
+            throw senf::AddressSyntaxException();
     }
     
     template <class Range>
     boost::uint8_t hexToByte(Range const & range)
     {
         if (boost::size(range) != 2)
-            throw senf::MACAddress::SyntaxException();
+            throw senf::AddressSyntaxException();
         typename boost::range_const_iterator<Range>::type i (boost::begin(range));
         return hexToNibble(i[0])*16+hexToNibble(i[1]);
     }
@@ -84,14 +84,14 @@ prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string c
     for (; i!=i_end && j!=j_end; ++i, ++j)
         *j = hexToByte(*i);
     if (i!=i_end || j!=j_end)
-        throw SyntaxException();
+        throw AddressSyntaxException();
     return mac;
 }
 
 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
 {
     if ( boost::uint16_t(v>>24)  != 0xfffe )
-        throw SyntaxException();
+        throw AddressSyntaxException();
     MACAddress mac (senf::noinit);
     mac[0] = boost::uint8_t( v>>56 );
     mac[1] = boost::uint8_t( v>>48 );
index f73e745..f3aad01 100644 (file)
@@ -34,6 +34,7 @@
 #include <boost/type_traits.hpp>
 #include "../../../Utils/safe_bool.hh"
 #include "../../../Utils/Tags.hh"
+#include "../AddressExceptions.hh"
 
 //#include "MACAddress.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -83,8 +84,8 @@ namespace senf {
                                              to a MAC address. This conversion is only possible, if
                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
                                              transmission order) must be 0xFFFE.
-                                             \throws SyntaxException if \a v is not a MAC compatible
-                                                 EUI-64. */
+                                             \throws AddressSyntaxException if \a v is not a MAC
+                                                 compatible EUI-64. */
 
         bool local() const;             ///< \c true, if address is locally administered
         bool multicast() const;             ///< \c true, if address is a group/multicast address
@@ -96,9 +97,6 @@ namespace senf {
         
         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
 
-        /** \brief Bad MAC address syntax or conversion */
-        struct SyntaxException : public std::exception
-        { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
     };
 
     /** \brief Write MAC address
index 067eab0..d4126e0 100644 (file)
@@ -74,14 +74,14 @@ BOOST_AUTO_UNIT_TEST(macAddress)
     BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(mac2), "a1:b2:c3:d4:e5:f6" );
 
     BOOST_CHECK_THROW( senf::MACAddress::from_string("1:2:3:4:5:6"), 
-                       senf::MACAddress::SyntaxException );
+                       senf::AddressSyntaxException );
     BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05"), 
-                       senf::MACAddress::SyntaxException );
+                       senf::AddressSyntaxException );
     BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05:z6"), 
-                       senf::MACAddress::SyntaxException );
+                       senf::AddressSyntaxException );
 
     BOOST_CHECK_EQUAL( mac, senf::MACAddress::from_eui64(0xa1b2c3fffed4e5f6llu) );
-    BOOST_CHECK_THROW( senf::MACAddress::from_eui64(0u), senf::MACAddress::SyntaxException );
+    BOOST_CHECK_THROW( senf::MACAddress::from_eui64(0u), senf::AddressSyntaxException );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////