X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FProtocols%2FINet%2FINet4Address.hh;h=b638448a6e87228debfc28dc20c413066d873dfc;hb=81ffa1c459b96dd44472bcef37e1e373934ee138;hp=f40b6dc840244e26fbd88645d038af49fc41917c;hpb=ced8f321adf904c31149162de5f6258c971c7466;p=senf.git diff --git a/Socket/Protocols/INet/INet4Address.hh b/Socket/Protocols/INet/INet4Address.hh index f40b6dc..b638448 100644 --- a/Socket/Protocols/INet/INet4Address.hh +++ b/Socket/Protocols/INet/INet4Address.hh @@ -32,6 +32,7 @@ #include #include #include +#include #include "Utils/SafeBool.hh" //#include "INet4Address.mpp" @@ -44,6 +45,8 @@ namespace senf { INet4Address represents a simple IP address. It is modelled as a fixed-size container/sequence of 4 bytes. + \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 * doubles as string literal and as arbitrary data iterator. The iterator constructor can @@ -56,7 +59,6 @@ namespace senf { class INet4Address : public boost::array, public ComparableSafeBool - { public: /////////////////////////////////////////////////////////////////////////// @@ -158,14 +160,114 @@ namespace senf { inaddr_type iref() const; }; + /** \brief Output INet4Address instance as it's string representation + \related INet4Address + */ std::ostream & operator<<(std::ostream & os, INet4Address const & addr); + /** \brief CHeck INet4Address against a fixed network prefix + + This helper allows to easily and efficiently check an INet4Address against an arbitrary but + constant network prefix. The network prefix is represented by + + \par "" + senf::CheckINet4Network< addr , prefix-len > + + Where \a addr is the v4 Internet address as a 32-bit unsigned integer number in host byte + order and \a prefix_len is the length of the network prefix. The class exposes a single + static member match( addr ) which matches the INet4Address \a addr + against the prefix: + + \code + if (senf::CheckINet4Network<0x7F000000u,8u>::match(addr)) { + // 'addr' is within the 127.0.0.0/8 loopback network + ... + } + \endcode + + \implementation This is implemented the way it is so the syntax is identical to the + CheckINet6Network syntax. + */ + template + class CheckINet4Network + { + public: + 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 ComparableSafeBool + { + 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//////////////////////////////////////// #include "INet4Address.cci" #include "INet4Address.ct" -//#include "INet4Address.cti" +#include "INet4Address.cti" #endif