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