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 \todo Add additional classes for CIDR addresses and networks and network math.
49 : public boost::array<boost::uint8_t,4>,
50 public ComparableSafeBool<INet4Address>
54 ///////////////////////////////////////////////////////////////////////////
57 typedef uint32_t address_type; ///< Address representation as number in host byte order
58 typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
59 typedef boost::function<void (INet4Address const &)> Callback;
60 ///< Callback for asynchronous from_string call
62 static INet4Address const None; ///< The empty (0) address
63 static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
64 static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
66 enum NoInit_t { noinit };
68 ///////////////////////////////////////////////////////////////////////////
69 ///\name Structors and default members
72 INet4Address(); ///< Construct an empty address
73 explicit INet4Address(NoInit_t); ///< Construct uninitialized (!) address
74 explicit INet4Address(address_type value);
75 ///< Construct an address constant
77 static INet4Address from_string(std::string const & s);
78 ///< Convert string to address
79 /**< This member will try to convert the given string into
80 an IP address. from_string() supports all standard IP
81 literal representations as well es hostnames.
82 \attention This call may block if \a s represents a
83 hostname which must be looked up via some network
84 protocol like DNS or NIS
85 \throws SyntaxException if the address cannot be
86 converted for some reason
87 \param[in] s Address literal or hostname */
89 static void from_string(std::string const & s, Callback const & cb);
90 ///< Convert string to address (async/non-blocking)
91 /**< This member works like
92 from_string(std::string const &). However unlike
93 from_string(std::string const &), this call will not
94 block. Instead it will call \a cb passing the
95 INet4Address instance as soon as the address has been
96 resolved (which may be immediate if the address
97 represents an IP literal). \par
98 On error, the address passed to \a cb will be empty.
99 \param[in] s Address literal or hostname
100 \param[in] cb Callback to pass the address to
103 template <class InputIterator>
104 static INet4Address from_data(InputIterator i);
105 ///< Construct address from 4 bytes of raw data
106 /**< from_data will build an address from 4 bytes of raw
107 data as accessed by the iterator. The data must be in
108 network byte order. */
109 static INet4Address from_inaddr(inaddr_type v);
110 ///< Construct address from integer in network byte order
111 /**< This call is used when interfacing with other legacy
112 code to convert a network byte order address in an
113 integer number into an INet4Address. */
116 ///////////////////////////////////////////////////////////////////////////
120 bool local() const; ///< \c true, if address is locally administered
121 /**< This call checks, if the address is within one of the
122 IANA private ranges. */
123 bool loopback() const; ///< \c true, if address is within the loopback network
124 /**< Checks, whether the address is in the IANA loopback
125 network 10.0.0.0/8 */
126 bool multicast() const; ///< \c true, if address is a multicast address
127 /**< Checks, whether the address is in the 224.0.0.0/4
128 network reserved for multicast addresses by the
130 bool broadcast() const; ///< \c true, if address is 255.255.255.255
131 bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
133 inaddr_type inaddr() const; ///< Return the raw network byte order address
134 /**< This member is used to interact with legacy code.
136 address_type address() const; ///< Return address represented as integer number
137 /**< This member returns the address as an integer number in
138 host byte order. This representation allows simple
139 network math operations. */
143 struct SyntaxException : public std::exception
144 { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } };
147 enum InAddr_t { IsInAddr };
148 INet4Address(inaddr_type addr, InAddr_t);
149 inaddr_type & iref();
150 inaddr_type iref() const;
153 std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
157 ///////////////////////////////hh.e////////////////////////////////////////
158 #include "INet4Address.cci"
159 #include "INet4Address.ct"
160 //#include "INet4Address.cti"
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"