X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FProtocols%2FINet%2FINet4Address.hh;h=a77903d96142e7139dde3062d073b47b0850ec2b;hb=6f50bf49e282c0528f51faa0a245bbfa2b867399;hp=1ed57e8e7b65b5b75c361e1aae29beecaf771a71;hpb=08c52f7433157c8ac0a74c1cdf0f145453b77f04;p=senf.git diff --git a/Socket/Protocols/INet/INet4Address.hh b/Socket/Protocols/INet/INet4Address.hh index 1ed57e8..a77903d 100644 --- a/Socket/Protocols/INet/INet4Address.hh +++ b/Socket/Protocols/INet/INet4Address.hh @@ -1,8 +1,8 @@ // $Id$ // // Copyright (C) 2007 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// 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,21 +30,22 @@ #include #include #include -#include #include -#include "Utils/SafeBool.hh" +#include +#include "../../../Utils/safe_bool.hh" +#include "../../../Utils/Tags.hh" //#include "INet4Address.mpp" ///////////////////////////////hh.p//////////////////////////////////////// namespace senf { - /** \brief IpV4 Internet address + /** \brief IPv4 Internet address INet4Address represents a simple IP address. It is modelled as a fixed-size container/sequence of 4 bytes. - \see CheckINet4Network + \see CheckINet4Network \n INet4Network \implementation We awkwardly need to use static named constructors (from_ members) instead of ordinarily overloaded constructors for one simple reason: char * @@ -52,13 +53,11 @@ namespace senf { therefore not be distinguished from initialization with a string literal. Therefore we need to disambiguate using the named constructors. - \todo Add additional classes for CIDR addresses and networks and network math. \ingroup addr_group */ class INet4Address : public boost::array, - public ComparableSafeBool - + public comparable_safe_bool { public: /////////////////////////////////////////////////////////////////////////// @@ -66,21 +65,17 @@ namespace senf { typedef uint32_t address_type; ///< Address representation as number in host byte order typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order - typedef boost::function Callback; - ///< Callback for asynchronous from_string call static INet4Address const None; ///< The empty (0) address static INet4Address const Loopback; ///< The loopback (127.0.0.1) address static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address - enum NoInit_t { noinit }; - /////////////////////////////////////////////////////////////////////////// ///\name Structors and default members ///@{ INet4Address(); ///< Construct an empty address - explicit INet4Address(NoInit_t); ///< Construct uninitialized (!) address + explicit INet4Address(senf::NoInit_t); ///< Construct uninitialized (!) address explicit INet4Address(address_type value); ///< Construct an address constant @@ -96,20 +91,6 @@ namespace senf { converted for some reason \param[in] s Address literal or hostname */ - static void from_string(std::string const & s, Callback const & cb); - ///< Convert string to address (async/non-blocking) - /**< This member works like - from_string(std::string const &). However unlike - from_string(std::string const &), this call will not - block. Instead it will call \a cb passing the - INet4Address instance as soon as the address has been - resolved (which may be immediate if the address - represents an IP literal). \par - On error, the address passed to \a cb will be empty. - \param[in] s Address literal or hostname - \param[in] cb Callback to pass the address to - \fixme Implement */ - template static INet4Address from_data(InputIterator i); ///< Construct address from 4 bytes of raw data @@ -150,9 +131,19 @@ namespace senf { ////@} - struct SyntaxException : public std::exception - { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } }; + /** \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); @@ -195,6 +186,73 @@ namespace senf { static bool match(INet4Address const & addr); }; + /** \brief IPv4 network prefix + + This class represents an IPv4 network prefix in CIDR notation. + */ + class INet4Network + : public boost::equality_comparable, + public comparable_safe_bool + { + public: + /////////////////////////////////////////////////////////////////////////// + ///\name Structors and default members + ///@{ + + INet4Network(); ///< Construct empty (0.0.0.0/0) network + INet4Network(INet4Address address, unsigned prefix_len); + ///< Construct network from given address and prefix length + explicit INet4Network(std::string s); ///< Construct network from CIDR notation + + ///@} + /////////////////////////////////////////////////////////////////////////// + + INet4Address const & address() const; ///< Get the networks address + unsigned prefix_len() const; ///< Get the networks prefix length + + bool boolean_test() const; ///< \c true, if INet4Network is non-empty + bool operator==(INet4Network const & other) const; + ///< Compare to networks for equality + + bool match(INet4Address addr) const; ///< \c true, if the network includes \a addr + bool match(INet4Network net) const; ///< \c true, if the network includes \a net + /**< The is true, if \a net is sub-network (or the same as) + \c this. */ + + INet4Address host(boost::uint32_t number); ///< Return the host with the given number + /**< Returns the host with the given number within the + network. If the number is larger than the maximum + host number in the network, it is truncated. So \c + host(0) is the networks own address, \c host(1) + customarily is the default router and \c host(-1) is + the broadcast address. */ + + INet4Network subnet(boost::uint32_t net, unsigned prefix_len); + ///< Return the given subnet of \c this + /**< The returned INet4Network will be a subnet of \c this + with the given network number. The network number is + comprised by the bits above \a prefix_len: + \code + INet4Network("192.168.0.0/16").subnet(111u,24u) == INet4Network("192.168.111.0/24") + INet4Network("192.168.111.0/24").subnet(1u,28u) == INet4Network("192.168.111.16/28") + \endcode + \param[in] net network number + \param[in] prefix_len length of subnet prefix */ + + protected: + + private: + boost::uint32_t mask() const; + + unsigned prefix_len_; + INet4Address address_; + }; + + /** \brief Output INet4Network instance as it's string representation + \related INet4Network + */ + std::ostream & operator<<(std::ostream & os, INet4Network const & addr); + } ///////////////////////////////hh.e////////////////////////////////////////