d7b13a1ffae5aeed878fd3a10d0f8a47af4365e1
[senf.git] / Socket / Protocols / INet / INet4Address.hh
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
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.
10 //
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.
15 //
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.
20
21 /** \file
22     \brief INet4Address public header */
23
24 #ifndef HH_INet4Address_
25 #define HH_INet4Address_ 1
26
27 // Custom includes
28 #include <iostream>
29 #include <string>
30 #include <boost/cstdint.hpp>
31 #include <boost/function.hpp>
32 #include <boost/array.hpp>
33 #include "Utils/SafeBool.hh"
34
35 //#include "INet4Address.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39     
40     /** \brief IpV4 Internet address
41         
42         INet4Address represents a simple IP address. It is modelled as a fixed-size
43         container/sequence of 4 bytes.
44
45         \todo Add additional classes for CIDR addresses and networks and network math.
46       */
47     class INet4Address
48         : public boost::array<boost::uint8_t,4>, 
49           public ComparableSafeBool<INet4Address>
50
51     {
52     public:
53         ///////////////////////////////////////////////////////////////////////////
54         // Types
55         
56         typedef uint32_t address_type;  ///< Address representation as number in host byte order
57         typedef uint32_t inaddr_type;   ///< Legacy address representation in network byte order
58         typedef boost::function<void (INet4Address const &)> Callback;
59                                         ///< Callback for asynchronous from_string call
60
61         static INet4Address const None; ///< The empty (0) address
62         static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
63         static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
64
65         enum NoInit_t { noinit };
66
67         ///////////////////////////////////////////////////////////////////////////
68         ///\name Structors and default members
69         ///@{
70
71         INet4Address();                 ///< Construct an empty address
72         explicit INet4Address(NoInit_t); ///< Construct uninitialized (!) address
73         explicit INet4Address(address_type value);
74                                         ///< Construct an address constant
75
76         static INet4Address from_string(std::string const & s);
77                                         ///< Convert string to address
78                                         /**< This member will try to convert the given string into
79                                              an IP address. from_string() supports all standard IP
80                                              literal representations as well es hostnames.
81                                              \attention This call may block if \a s represents a
82                                                  hostname which must be looked up via some network
83                                                  protocol like DNS or NIS
84                                              \throws SyntaxException if the address cannot be
85                                                  converted for some reason
86                                              \param[in] s Address literal or hostname */
87         
88         static void from_string(std::string const & s, Callback const & cb);
89                                         ///< Convert string to address (async/non-blocking)
90                                         /**< This member works like
91                                              from_string(std::string const &). However unlike
92                                              from_string(std::string const &), this call will not
93                                              block. Instead it will call \a cb passing the
94                                              INet4Address instance as soon as the address has been
95                                              resolved (which may be immediate if the address
96                                              represents an IP literal). \par
97                                              On error, the address passed to \a cb will be empty.
98                                              \param[in] s Address literal or hostname
99                                              \param[in] cb Callback to pass the address to 
100                                              \fixme Implement */
101
102         template <class InputIterator> 
103         static INet4Address from_data(InputIterator i);
104                                         ///< Construct address from 4 bytes of raw data
105                                         /**< from_data will build an address from 4 bytes of raw
106                                              data as accessed by the iterator. The data must be in
107                                              network byte order. */
108         static INet4Address from_inaddr(inaddr_type v);
109                                         ///< Construct address from integer in network byte order
110                                         /**< This call is used when interfacing with other legacy
111                                              code to convert a network byte order address in an
112                                              integer number into an INet4Address. */
113
114         ///@}
115         ///////////////////////////////////////////////////////////////////////////
116         ///\name Accessors
117         ///@{
118
119         bool local() const;             ///< \c true, if address is locally administered
120                                         /**< This call checks, if the address is within one of the
121                                              IANA private ranges. */
122         bool loopback() const;          ///< \c true, if address is within the loopback network
123                                         /**< Checks, whether the address is in the IANA loopback
124                                              network 10.0.0.0/8 */
125         bool multicast() const;         ///< \c true, if address is a multicast address
126                                         /**< Checks, whether the address is in the 224.0.0.0/4
127                                              network reserved for multicast addresses by the
128                                              IANA. */
129         bool broadcast() const;         ///< \c true, if address is 255.255.255.255
130         bool boolean_test() const;      ///< \c true, if address is non-empty (!= 0.0.0.0)
131
132         inaddr_type inaddr() const;     ///< Return the raw network byte order address
133                                         /**< This member is used to interact with legacy code. 
134                                              \return */
135         address_type address() const;   ///< Return address represented as integer number
136                                         /**< This member returns the address as an integer number in
137                                              host byte order. This representation allows simple
138                                              network math operations. */
139
140         ////@}
141
142         struct SyntaxException : public std::exception
143         { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } };
144
145     private:
146         enum InAddr_t { IsInAddr };
147         INet4Address(inaddr_type addr, InAddr_t);
148         inaddr_type & iref();
149         inaddr_type iref() const;
150     };
151
152     std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
153
154 }
155
156 ///////////////////////////////hh.e////////////////////////////////////////
157 #include "INet4Address.cci"
158 #include "INet4Address.ct"
159 //#include "INet4Address.cti"
160 #endif
161
162 \f
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // comment-column: 40
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // End: