4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief INet4Address public header */
26 #ifndef HH_INet4Address_
27 #define HH_INet4Address_ 1
32 #include <boost/cstdint.hpp>
33 #include <boost/array.hpp>
34 #include <boost/operators.hpp>
35 #include "../../../Utils/safe_bool.hh"
36 #include "../../../Utils/Tags.hh"
37 #include "../AddressExceptions.hh"
39 //#include "INet4Address.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
44 /** \brief IPv4 Internet address
46 INet4Address represents a simple IP address. It is modelled as a fixed-size
47 container/sequence of 4 bytes.
49 \see CheckINet4Network \n INet4Network
51 \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
52 instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
53 doubles as string literal and as arbitrary data iterator. The iterator constructor can
54 therefore not be distinguished from initialization with a string literal. Therefore we
55 need to disambiguate using the named constructors.
60 : public boost::array<boost::uint8_t,4>,
61 public comparable_safe_bool<INet4Address>
64 ///////////////////////////////////////////////////////////////////////////
67 typedef uint32_t address_type; ///< Address representation as number in host byte order
68 typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
70 static INet4Address const None; ///< The empty (0) address
71 static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
72 static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
74 ///////////////////////////////////////////////////////////////////////////
75 ///\name Structors and default members
78 INet4Address(); ///< Construct an empty address
79 explicit INet4Address(senf::NoInit_t); ///< Construct uninitialized (!) address
80 explicit INet4Address(address_type value);
81 ///< Construct an address constant
83 static INet4Address from_string(std::string const & s);
84 ///< Convert string to address
85 /**< This member will try to convert the given string into
86 an IP address. from_string() supports all standard IP
87 literal representations as well es hostnames.
88 \attention This call may block if \a s represents a
89 hostname which must be looked up via some network
90 protocol like DNS or NIS
91 \throws AddressSyntaxException if the address cannot be
92 converted for some reason
93 \param[in] s Address literal or hostname */
95 template <class InputIterator>
96 static INet4Address from_data(InputIterator i);
97 ///< Construct address from 4 bytes of raw data
98 /**< from_data will build an address from 4 bytes of raw
99 data as accessed by the iterator. The data must be in
100 network byte order. */
101 static INet4Address from_inaddr(inaddr_type v);
102 ///< Construct address from integer in network byte order
103 /**< This call is used when interfacing with other legacy
104 code to convert a network byte order address in an
105 integer number into an INet4Address. */
108 ///////////////////////////////////////////////////////////////////////////
112 bool local() const; ///< \c true, if address is locally administered
113 /**< This call checks, if the address is within one of the
114 IANA private ranges. */
115 bool loopback() const; ///< \c true, if address is within the loopback network
116 /**< Checks, whether the address is in the IANA loopback
117 network 10.0.0.0/8 */
118 bool multicast() const; ///< \c true, if address is a multicast address
119 /**< Checks, whether the address is in the 224.0.0.0/4
120 network reserved for multicast addresses by the
122 bool broadcast() const; ///< \c true, if address is 255.255.255.255
123 bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
125 inaddr_type inaddr() const; ///< Return the raw network byte order address
126 /**< This member is used to interact with legacy code.
128 address_type address() const; ///< Return address represented as integer number
129 /**< This member returns the address as an integer number in
130 host byte order. This representation allows simple
131 network math operations. */
136 enum InAddr_t { IsInAddr };
137 INet4Address(inaddr_type addr, InAddr_t);
138 inaddr_type & iref();
139 inaddr_type iref() const;
142 /** \brief Output INet4Address instance as it's string representation
143 \related INet4Address
145 std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
147 /** \brief CHeck INet4Address against a fixed network prefix
149 This helper allows to easily and efficiently check an INet4Address against an arbitrary but
150 constant network prefix. The network prefix is represented by
153 <tt>senf::CheckINet4Network<</tt> <i>addr</i> <tt>,</tt> <i>prefix-len</i> <tt>></tt>
155 Where \a addr is the v4 Internet address as a 32-bit unsigned integer number in host byte
156 order and \a prefix_len is the length of the network prefix. The class exposes a single
157 static member <tt>match(</tt> <i>addr</i> <tt>)</tt> which matches the INet4Address \a addr
161 if (senf::CheckINet4Network<0x7F000000u,8u>::match(addr)) {
162 // 'addr' is within the 127.0.0.0/8 loopback network
167 \implementation This is implemented the way it is so the syntax is identical to the
168 CheckINet6Network syntax.
170 template <boost::uint32_t address, unsigned prefix_len>
171 class CheckINet4Network
174 static bool match(INet4Address const & addr);
177 /** \brief IPv4 network prefix
179 This class represents an IPv4 network prefix in CIDR notation.
182 : public boost::equality_comparable<INet4Network>,
183 public comparable_safe_bool<INet4Network>
186 ///////////////////////////////////////////////////////////////////////////
187 ///\name Structors and default members
190 INet4Network(); ///< Construct empty (0.0.0.0/0) network
191 INet4Network(INet4Address address, unsigned prefix_len);
192 ///< Construct network from given address and prefix length
193 explicit INet4Network(std::string s); ///< Construct network from CIDR notation
196 ///////////////////////////////////////////////////////////////////////////
198 INet4Address const & address() const; ///< Get the networks address
199 unsigned prefix_len() const; ///< Get the networks prefix length
201 bool boolean_test() const; ///< \c true, if INet4Network is non-empty
202 bool operator==(INet4Network const & other) const;
203 ///< Compare to networks for equality
205 bool match(INet4Address addr) const; ///< \c true, if the network includes \a addr
206 bool match(INet4Network net) const; ///< \c true, if the network includes \a net
207 /**< The is true, if \a net is sub-network (or the same as)
210 INet4Address host(boost::uint32_t number); ///< Return the host with the given number
211 /**< Returns the host with the given number within the
212 network. If the number is larger than the maximum
213 host number in the network, it is truncated. So \c
214 host(0) is the networks own address, \c host(1)
215 customarily is the default router and \c host(-1) is
216 the broadcast address. */
218 INet4Network subnet(boost::uint32_t net, unsigned prefix_len);
219 ///< Return the given subnet of \c this
220 /**< The returned INet4Network will be a subnet of \c this
221 with the given network number. The network number is
222 comprised by the bits above \a prefix_len:
224 INet4Network("192.168.0.0/16").subnet(111u,24u) == INet4Network("192.168.111.0/24")
225 INet4Network("192.168.111.0/24").subnet(1u,28u) == INet4Network("192.168.111.16/28")
227 \param[in] net network number
228 \param[in] prefix_len length of subnet prefix */
233 boost::uint32_t mask() const;
235 unsigned prefix_len_;
236 INet4Address address_;
239 /** \brief Output INet4Network instance as it's string representation
240 \related INet4Network
242 std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
246 ///////////////////////////////hh.e////////////////////////////////////////
247 #include "INet4Address.cci"
248 #include "INet4Address.ct"
249 #include "INet4Address.cti"
256 // comment-column: 40
257 // c-file-style: "senf"
258 // indent-tabs-mode: nil
259 // ispell-local-dictionary: "american"
260 // compile-command: "scons -u test"