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"
38 //#include "INet4Address.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
43 /** \brief IPv4 Internet address
45 INet4Address represents a simple IP address. It is modelled as a fixed-size
46 container/sequence of 4 bytes.
48 \see CheckINet4Network \n INet4Network
50 \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
51 instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
52 doubles as string literal and as arbitrary data iterator. The iterator constructor can
53 therefore not be distinguished from initialization with a string literal. Therefore we
54 need to disambiguate using the named constructors.
59 : public boost::array<boost::uint8_t,4>,
60 public comparable_safe_bool<INet4Address>
63 ///////////////////////////////////////////////////////////////////////////
66 typedef uint32_t address_type; ///< Address representation as number in host byte order
67 typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
69 static INet4Address const None; ///< The empty (0) address
70 static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
71 static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
73 ///////////////////////////////////////////////////////////////////////////
74 ///\name Structors and default members
77 INet4Address(); ///< Construct an empty address
78 explicit INet4Address(senf::NoInit_t); ///< Construct uninitialized (!) address
79 explicit INet4Address(address_type value);
80 ///< Construct an address constant
82 static INet4Address from_string(std::string const & s);
83 ///< Convert string to address
84 /**< This member will try to convert the given string into
85 an IP address. from_string() supports all standard IP
86 literal representations as well es hostnames.
87 \attention This call may block if \a s represents a
88 hostname which must be looked up via some network
89 protocol like DNS or NIS
90 \throws SyntaxException if the address cannot be
91 converted for some reason
92 \param[in] s Address literal or hostname */
94 template <class InputIterator>
95 static INet4Address from_data(InputIterator i);
96 ///< Construct address from 4 bytes of raw data
97 /**< from_data will build an address from 4 bytes of raw
98 data as accessed by the iterator. The data must be in
99 network byte order. */
100 static INet4Address from_inaddr(inaddr_type v);
101 ///< Construct address from integer in network byte order
102 /**< This call is used when interfacing with other legacy
103 code to convert a network byte order address in an
104 integer number into an INet4Address. */
107 ///////////////////////////////////////////////////////////////////////////
111 bool local() const; ///< \c true, if address is locally administered
112 /**< This call checks, if the address is within one of the
113 IANA private ranges. */
114 bool loopback() const; ///< \c true, if address is within the loopback network
115 /**< Checks, whether the address is in the IANA loopback
116 network 10.0.0.0/8 */
117 bool multicast() const; ///< \c true, if address is a multicast address
118 /**< Checks, whether the address is in the 224.0.0.0/4
119 network reserved for multicast addresses by the
121 bool broadcast() const; ///< \c true, if address is 255.255.255.255
122 bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
124 inaddr_type inaddr() const; ///< Return the raw network byte order address
125 /**< This member is used to interact with legacy code.
127 address_type address() const; ///< Return address represented as integer number
128 /**< This member returns the address as an integer number in
129 host byte order. This representation allows simple
130 network math operations. */
134 /** \brief Base-class for INet4Address exceptions */
135 struct AddressException : public std::exception {};
137 /** \brief Invalid INet4 address syntax */
138 struct SyntaxException : public AddressException
139 { virtual char const * what() const throw()
140 { return "invalid INet4 address syntax"; } };
142 /** \brief Resolver failure */
143 struct UnknownHostnameException : public AddressException
144 { virtual char const * what() const throw()
145 { return "failed to resolve INet4 hostname"; } };
148 enum InAddr_t { IsInAddr };
149 INet4Address(inaddr_type addr, InAddr_t);
150 inaddr_type & iref();
151 inaddr_type iref() const;
154 /** \brief Output INet4Address instance as it's string representation
155 \related INet4Address
157 std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
159 /** \brief CHeck INet4Address against a fixed network prefix
161 This helper allows to easily and efficiently check an INet4Address against an arbitrary but
162 constant network prefix. The network prefix is represented by
165 <tt>senf::CheckINet4Network<</tt> <i>addr</i> <tt>,</tt> <i>prefix-len</i> <tt>></tt>
167 Where \a addr is the v4 Internet address as a 32-bit unsigned integer number in host byte
168 order and \a prefix_len is the length of the network prefix. The class exposes a single
169 static member <tt>match(</tt> <i>addr</i> <tt>)</tt> which matches the INet4Address \a addr
173 if (senf::CheckINet4Network<0x7F000000u,8u>::match(addr)) {
174 // 'addr' is within the 127.0.0.0/8 loopback network
179 \implementation This is implemented the way it is so the syntax is identical to the
180 CheckINet6Network syntax.
182 template <boost::uint32_t address, unsigned prefix_len>
183 class CheckINet4Network
186 static bool match(INet4Address const & addr);
189 /** \brief IPv4 network prefix
191 This class represents an IPv4 network prefix in CIDR notation.
194 : public boost::equality_comparable<INet4Network>,
195 public comparable_safe_bool<INet4Network>
198 ///////////////////////////////////////////////////////////////////////////
199 ///\name Structors and default members
202 INet4Network(); ///< Construct empty (0.0.0.0/0) network
203 INet4Network(INet4Address address, unsigned prefix_len);
204 ///< Construct network from given address and prefix length
205 explicit INet4Network(std::string s); ///< Construct network from CIDR notation
208 ///////////////////////////////////////////////////////////////////////////
210 INet4Address const & address() const; ///< Get the networks address
211 unsigned prefix_len() const; ///< Get the networks prefix length
213 bool boolean_test() const; ///< \c true, if INet4Network is non-empty
214 bool operator==(INet4Network const & other) const;
215 ///< Compare to networks for equality
217 bool match(INet4Address addr) const; ///< \c true, if the network includes \a addr
218 bool match(INet4Network net) const; ///< \c true, if the network includes \a net
219 /**< The is true, if \a net is sub-network (or the same as)
222 INet4Address host(boost::uint32_t number); ///< Return the host with the given number
223 /**< Returns the host with the given number within the
224 network. If the number is larger than the maximum
225 host number in the network, it is truncated. So \c
226 host(0) is the networks own address, \c host(1)
227 customarily is the default router and \c host(-1) is
228 the broadcast address. */
230 INet4Network subnet(boost::uint32_t net, unsigned prefix_len);
231 ///< Return the given subnet of \c this
232 /**< The returned INet4Network will be a subnet of \c this
233 with the given network number. The network number is
234 comprised by the bits above \a prefix_len:
236 INet4Network("192.168.0.0/16").subnet(111u,24u) == INet4Network("192.168.111.0/24")
237 INet4Network("192.168.111.0/24").subnet(1u,28u) == INet4Network("192.168.111.16/28")
239 \param[in] net network number
240 \param[in] prefix_len length of subnet prefix */
245 boost::uint32_t mask() const;
247 unsigned prefix_len_;
248 INet4Address address_;
251 /** \brief Output INet4Network instance as it's string representation
252 \related INet4Network
254 std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
258 ///////////////////////////////hh.e////////////////////////////////////////
259 #include "INet4Address.cci"
260 #include "INet4Address.ct"
261 #include "INet4Address.cti"
268 // comment-column: 40
269 // c-file-style: "senf"
270 // indent-tabs-mode: nil
271 // ispell-local-dictionary: "american"
272 // compile-command: "scons -u test"