4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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/function.hpp>
34 #include <boost/array.hpp>
35 #include <boost/operators.hpp>
36 #include "Utils/SafeBool.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.
56 \todo Add additional classes for CIDR addresses and networks and network math.
60 : public boost::array<boost::uint8_t,4>,
61 public ComparableSafeBool<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
69 typedef boost::function<void (INet4Address const &)> Callback;
70 ///< Callback for asynchronous from_string call
72 static INet4Address const None; ///< The empty (0) address
73 static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
74 static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
76 enum NoInit_t { noinit };
78 ///////////////////////////////////////////////////////////////////////////
79 ///\name Structors and default members
82 INet4Address(); ///< Construct an empty address
83 explicit INet4Address(NoInit_t); ///< Construct uninitialized (!) address
84 explicit INet4Address(address_type value);
85 ///< Construct an address constant
87 static INet4Address from_string(std::string const & s);
88 ///< Convert string to address
89 /**< This member will try to convert the given string into
90 an IP address. from_string() supports all standard IP
91 literal representations as well es hostnames.
92 \attention This call may block if \a s represents a
93 hostname which must be looked up via some network
94 protocol like DNS or NIS
95 \throws SyntaxException if the address cannot be
96 converted for some reason
97 \param[in] s Address literal or hostname */
99 static void from_string(std::string const & s, Callback const & cb);
100 ///< Convert string to address (async/non-blocking)
101 /**< This member works like
102 from_string(std::string const &). However unlike
103 from_string(std::string const &), this call will not
104 block. Instead it will call \a cb passing the
105 INet4Address instance as soon as the address has been
106 resolved (which may be immediate if the address
107 represents an IP literal). \par
108 On error, the address passed to \a cb will be empty.
109 \param[in] s Address literal or hostname
110 \param[in] cb Callback to pass the address to
113 template <class InputIterator>
114 static INet4Address from_data(InputIterator i);
115 ///< Construct address from 4 bytes of raw data
116 /**< from_data will build an address from 4 bytes of raw
117 data as accessed by the iterator. The data must be in
118 network byte order. */
119 static INet4Address from_inaddr(inaddr_type v);
120 ///< Construct address from integer in network byte order
121 /**< This call is used when interfacing with other legacy
122 code to convert a network byte order address in an
123 integer number into an INet4Address. */
126 ///////////////////////////////////////////////////////////////////////////
130 bool local() const; ///< \c true, if address is locally administered
131 /**< This call checks, if the address is within one of the
132 IANA private ranges. */
133 bool loopback() const; ///< \c true, if address is within the loopback network
134 /**< Checks, whether the address is in the IANA loopback
135 network 10.0.0.0/8 */
136 bool multicast() const; ///< \c true, if address is a multicast address
137 /**< Checks, whether the address is in the 224.0.0.0/4
138 network reserved for multicast addresses by the
140 bool broadcast() const; ///< \c true, if address is 255.255.255.255
141 bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
143 inaddr_type inaddr() const; ///< Return the raw network byte order address
144 /**< This member is used to interact with legacy code.
146 address_type address() const; ///< Return address represented as integer number
147 /**< This member returns the address as an integer number in
148 host byte order. This representation allows simple
149 network math operations. */
153 struct SyntaxException : public std::exception
154 { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } };
157 enum InAddr_t { IsInAddr };
158 INet4Address(inaddr_type addr, InAddr_t);
159 inaddr_type & iref();
160 inaddr_type iref() const;
163 /** \brief Output INet4Address instance as it's string representation
164 \related INet4Address
166 std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
168 /** \brief CHeck INet4Address against a fixed network prefix
170 This helper allows to easily and efficiently check an INet4Address against an arbitrary but
171 constant network prefix. The network prefix is represented by
174 <tt>senf::CheckINet4Network<</tt> <i>addr</i> <tt>,</tt> <i>prefix-len</i> <tt>></tt>
176 Where \a addr is the v4 Internet address as a 32-bit unsigned integer number in host byte
177 order and \a prefix_len is the length of the network prefix. The class exposes a single
178 static member <tt>match(</tt> <i>addr</i> <tt>)</tt> which matches the INet4Address \a addr
182 if (senf::CheckINet4Network<0x7F000000u,8u>::match(addr)) {
183 // 'addr' is within the 127.0.0.0/8 loopback network
188 \implementation This is implemented the way it is so the syntax is identical to the
189 CheckINet6Network syntax.
191 template <boost::uint32_t address, unsigned prefix_len>
192 class CheckINet4Network
195 static bool match(INet4Address const & addr);
198 /** \brief IpV4 network prefix
200 This class represents an IpV4 network prefix in CIDR notation.
203 : public boost::equality_comparable<INet4Network>,
204 public ComparableSafeBool<INet4Network>
207 ///////////////////////////////////////////////////////////////////////////
208 ///\name Structors and default members
211 INet4Network(); ///< Construct empty (0.0.0.0/0) network
212 INet4Network(INet4Address address, unsigned prefix_len);
213 ///< Construct network from given address and prefix length
214 explicit INet4Network(std::string s); ///< Construct network from CIDR notation
217 ///////////////////////////////////////////////////////////////////////////
219 INet4Address const & address() const; ///< Get the networks address
220 unsigned prefix_len() const; ///< Get the networks prefix length
222 bool boolean_test() const; ///< \c true, if INet4Network is non-empty
223 bool operator==(INet4Network const & other) const;
224 ///< Compare to networks for equality
226 bool match(INet4Address addr) const; ///< \c true, if the network includes \a addr
227 bool match(INet4Network net) const; ///< \c true, if the network includes \a net
228 /**< The is true, if \a net is sub-network (or the same as)
231 INet4Address host(boost::uint32_t number); ///< Return the host with the given number
232 /**< Returns the host with the given number within the
233 network. If the number is larger than the maximum
234 host number in the network, it is truncated. So \c
235 host(0) is the networks own address, \c host(1)
236 customarily is the default router and \c host(-1) is
237 the broadcast address. */
239 INet4Network subnet(boost::uint32_t net, unsigned prefix_len);
240 ///< Return the given subnet of \c this
241 /**< The returned INet4Network will be a subnet of \c this
242 with the given network number. The network number is
243 comprised by the bits above \a prefix_len:
245 INet4Network("192.168.0.0/16").subnet(111u,24u) == INet4Network("192.168.111.0/24")
246 INet4Network("192.168.111.0/24").subnet(1u,28u) == INet4Network("192.168.111.16/28")
248 \param[in] net network number
249 \param[in] prefix_len length of subnet prefix */
254 boost::uint32_t mask() const;
256 unsigned prefix_len_;
257 INet4Address address_;
260 /** \brief Output INet4Network instance as it's string representation
261 \related INet4Network
263 std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
267 ///////////////////////////////hh.e////////////////////////////////////////
268 #include "INet4Address.cci"
269 #include "INet4Address.ct"
270 #include "INet4Address.cti"
277 // comment-column: 40
278 // c-file-style: "senf"
279 // indent-tabs-mode: nil
280 // ispell-local-dictionary: "american"
281 // compile-command: "scons -u test"