2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief INet4Address public header */
24 #ifndef HH_INet4Address_
25 #define HH_INet4Address_ 1
30 #include <boost/cstdint.hpp>
31 #include <boost/function.hpp>
32 #include <boost/array.hpp>
33 #include "Utils/SafeBool.hh"
35 //#include "INet4Address.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
40 /** \brief IpV4 Internet address
42 INet4Address represents a simple IP address. It is modelled as a fixed-size
43 container/sequence of 4 bytes.
45 \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
46 instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
47 doubles as string literal and as arbitrary data iterator. The iterator constructor can
48 therefore not be distinguished from initialization with a string literal. Therefore we
49 need to disambiguate using the named constructors.
51 \todo Add additional classes for CIDR addresses and networks and network math.
55 : public boost::array<boost::uint8_t,4>,
56 public ComparableSafeBool<INet4Address>
60 ///////////////////////////////////////////////////////////////////////////
63 typedef uint32_t address_type; ///< Address representation as number in host byte order
64 typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
65 typedef boost::function<void (INet4Address const &)> Callback;
66 ///< Callback for asynchronous from_string call
68 static INet4Address const None; ///< The empty (0) address
69 static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
70 static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
72 enum NoInit_t { noinit };
74 ///////////////////////////////////////////////////////////////////////////
75 ///\name Structors and default members
78 INet4Address(); ///< Construct an empty address
79 explicit INet4Address(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 SyntaxException if the address cannot be
92 converted for some reason
93 \param[in] s Address literal or hostname */
95 static void from_string(std::string const & s, Callback const & cb);
96 ///< Convert string to address (async/non-blocking)
97 /**< This member works like
98 from_string(std::string const &). However unlike
99 from_string(std::string const &), this call will not
100 block. Instead it will call \a cb passing the
101 INet4Address instance as soon as the address has been
102 resolved (which may be immediate if the address
103 represents an IP literal). \par
104 On error, the address passed to \a cb will be empty.
105 \param[in] s Address literal or hostname
106 \param[in] cb Callback to pass the address to
109 template <class InputIterator>
110 static INet4Address from_data(InputIterator i);
111 ///< Construct address from 4 bytes of raw data
112 /**< from_data will build an address from 4 bytes of raw
113 data as accessed by the iterator. The data must be in
114 network byte order. */
115 static INet4Address from_inaddr(inaddr_type v);
116 ///< Construct address from integer in network byte order
117 /**< This call is used when interfacing with other legacy
118 code to convert a network byte order address in an
119 integer number into an INet4Address. */
122 ///////////////////////////////////////////////////////////////////////////
126 bool local() const; ///< \c true, if address is locally administered
127 /**< This call checks, if the address is within one of the
128 IANA private ranges. */
129 bool loopback() const; ///< \c true, if address is within the loopback network
130 /**< Checks, whether the address is in the IANA loopback
131 network 10.0.0.0/8 */
132 bool multicast() const; ///< \c true, if address is a multicast address
133 /**< Checks, whether the address is in the 224.0.0.0/4
134 network reserved for multicast addresses by the
136 bool broadcast() const; ///< \c true, if address is 255.255.255.255
137 bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
139 inaddr_type inaddr() const; ///< Return the raw network byte order address
140 /**< This member is used to interact with legacy code.
142 address_type address() const; ///< Return address represented as integer number
143 /**< This member returns the address as an integer number in
144 host byte order. This representation allows simple
145 network math operations. */
149 struct SyntaxException : public std::exception
150 { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } };
153 enum InAddr_t { IsInAddr };
154 INet4Address(inaddr_type addr, InAddr_t);
155 inaddr_type & iref();
156 inaddr_type iref() const;
159 std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
163 ///////////////////////////////hh.e////////////////////////////////////////
164 #include "INet4Address.cci"
165 #include "INet4Address.ct"
166 //#include "INet4Address.cti"
173 // comment-column: 40
174 // c-file-style: "senf"
175 // indent-tabs-mode: nil
176 // ispell-local-dictionary: "american"
177 // compile-command: "scons -u test"