Fix SCons 1.2.0 build failure
[senf.git] / senf / Socket / Protocols / INet / INetAddressing.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief INet[46]Address and INet[46]AddressingPolicy public header
25  */
26
27 #ifndef HH_SENF_Socket_Protocols_INet_INetAddressing_
28 #define HH_SENF_Socket_Protocols_INet_INetAddressing_ 1
29
30 // Custom includes
31 #include <string>
32 #include <exception>
33 #include <netinet/in.h>
34 #include "../../SocketPolicy.hh"
35 #include "../../ClientSocketHandle.hh"
36 #include "../../CommunicationPolicy.hh"
37 #include "../BSDAddressingPolicy.hh"
38 #include "../BSDSocketAddress.hh"
39 #include "INet4Address.hh"
40 #include "INet6Address.hh"
41
42 //#include "INetAddressing.mpp"
43 ///////////////////////////////hh.p////////////////////////////////////////
44
45 namespace senf {
46
47     /** \brief IPv4 socket address
48
49         INet4Address wraps the standard sockaddr_in datatype. It provides simple accessor methods
50         to access the host and port. It does \e not integrate \c gethostbyname or DNS lookup.
51
52         \implementation This implementation is based on sockaddr_in, which is needed since it needs
53             to provide a non-const struct sockaddr * for legacy compatibility.
54
55         \ingroup addr_group
56      */
57     class INet4SocketAddress
58         : public BSDSocketAddress
59     {
60     public:
61         static short const addressFamily = AF_INET;
62
63         ///////////////////////////////////////////////////////////////////////////
64         ///\name Structors and default members
65         ///@{
66
67         INet4SocketAddress();
68         explicit INet4SocketAddress(std::string const & addr); ///< Set address and port
69                                         /**< This constructor expects a string of the form
70                                              'host:port'. The constructor will use this value to
71                                              initialize the host and port members. Since it uses the
72                                              INet4Address::from_string constructor, this call may
73                                              block while waiting for the resolver.
74                                              \throws AddressSyntaxException if the address syntax is
75                                                  invalid
76                                              \throws UnknownHostnameException if the
77                                                  address cannot be resolved. */
78
79         INet4SocketAddress(INet4Address const & addr, unsigned port);
80                                         ///< Set address and port explicitly
81                                         /**< \param[in] addr IP address
82                                              \param[in] port port number */
83
84         explicit INet4SocketAddress(unsigned port);
85                                         ///< Set port, address is set to 0.0.0.0
86                                         /**< \param[in] port port number */
87
88         INet4SocketAddress(const INet4SocketAddress& other);
89         INet4SocketAddress& operator=(const INet4SocketAddress& other);
90
91         ///@}
92         ///////////////////////////////////////////////////////////////////////////
93
94         INet4Address address() const;   ///< Return address
95         unsigned port() const;          ///< Return port number
96
97         void address(INet4Address const & addr); ///< Set address
98         void port(unsigned p);          ///< Set port number
99
100         using BSDSocketAddress::sockaddr_p;
101         using BSDSocketAddress::socklen_p;
102
103     private:
104         struct ::sockaddr_in addr_;
105     };
106
107     /** \brief Write address and port to os
108
109         \related INet4SocketAddress
110      */
111     std::ostream & operator<<(std::ostream & os, INet4SocketAddress const & addr);
112     std::istream & operator>>(std::istream & is, INet4SocketAddress & addr);
113
114     /** \brief IPv6 socket address
115
116         This class wraps the standard \c sockaddr_in6 structure. INet6SocketAddress provides access
117         to all members of the sockaddr_in6 structure. Additionally, INet6SocketAddress supports the
118         string representations
119
120         \par ""
121             <tt>[</tt> <i>address</i> [ <tt>%</tt> <i>zone-id</i> ] <tt>]:</tt> <i>port</i> \n
122             <i>hostname</i> <tt>:</tt> <i>port</i>
123
124         Where \e address is an arbitrary numeric IPv6 address, \e zone-id is an optional network
125         interface name and \e port is the port number. So some example addresses are
126
127         \par ""
128             <tt>[2001:db8:1::1]:80</tt> \n
129             <tt>www.go6.net:80</tt> \n
130             <tt>[fe80::1\%eth0]:443</tt>
131
132         \implementation The sockaddr_in6 structure has an sin6_flowinfo member. However RFC3493 does
133             not give the use of this field and specifies, that the field should be ignored ... so
134             that's what we do. Furthermore, the GNU libc reference states, that this field is not
135             implemented in the library.
136
137         \idea Implement a INet6Address_ref class which has an interface identical to INet6Address
138             and is convertible to INet6Address (the latter has a conversion constructor taking the
139             former as arg). This class however references an external in6_addr instead of containing
140             one itself. This can be used in INet6SocketAddress to increase the performance of some
141             operations.
142
143         \ingroup addr_group
144      */
145     class INet6SocketAddress
146         : public BSDSocketAddress
147     {
148     public:
149         static short const addressFamily = AF_INET6;
150
151         ///////////////////////////////////////////////////////////////////////////
152         ///\name Structors and default members
153         ///@{
154
155         INet6SocketAddress();           ///< Create empty instance
156         explicit INet6SocketAddress(std::string const & addr,
157                                     INet6Address::Resolve_t resolve = INet6Address::ResolveINet6);
158                                         ///< Initialize/convert from string representation
159                                         /**< \throws AddressSyntaxException if the address syntax is
160                                                  invalid
161                                              \throws UnknownHostnameException if the
162                                                  address cannot be resolved.
163                                              \param[in] addr Address to parse
164                                              \param[in] resolve If this is
165                                                  INet6Address::ResolveINet4, support IPv4
166                                                  addresses. See INet6Address. */
167         INet6SocketAddress(INet6Address const & addr, unsigned port);
168                                         ///< Initialize from address and port
169         INet6SocketAddress(INet6Address const & addr, unsigned port, std::string const & iface);
170                                         ///< Initialize explicitly from given parameters
171                                         /**< \throws AddressSyntaxException if the
172                                                 given iface cannot be resolved. */
173         explicit INet6SocketAddress(unsigned port);
174                                         ///< Initialize from port and set to 'unspecified' addr
175                                         /**< The address is set to [::]
176                                              \param[in] port port number  */
177
178         INet6SocketAddress(const INet6SocketAddress& other);
179         INet6SocketAddress& operator=(const INet6SocketAddress& other);
180
181         ///@}
182         ///////////////////////////////////////////////////////////////////////////
183
184         INet6Address address() const;    ///< Get printable address representation
185         void address(INet6Address const & addr); ///< Change address
186
187         unsigned port() const;          ///< Get port number
188         void port(unsigned port);       ///< Change port number
189
190         std::string iface() const;      ///< Get interface name
191         void iface(std::string const & iface); ///< Change interface
192                                                /**< \throws AddressSyntaxException if the
193                                                         given iface cannot be resolved. */
194
195         using BSDSocketAddress::sockaddr_p;
196         using BSDSocketAddress::socklen_p;
197
198     protected:
199
200     private:
201         void assignIface(std::string const & iface);
202
203         struct sockaddr_in6 sockaddr_;
204     };
205
206     /** \brief Output INet6SocketAddress instance as it's string representation
207         \related INet6SocketAddress
208      */
209     std::ostream & operator<<(std::ostream & os, INet6SocketAddress const & addr);
210     std::istream & operator>>(std::istream & is, INet6SocketAddress & addr);
211
212     /// \addtogroup policy_impl_group
213     /// @{
214
215     /** \brief Addressing policy supporting IPv4 addressing
216
217         \par Address Type:
218             INet4SocketAddress
219
220         This addressing policy implements addressing using Internet V4
221         addresses.
222
223         The various members are directly imported from
224         BSDAddressingPolicyMixin which see for a detailed
225         documentation.
226      */
227     struct INet4AddressingPolicy
228         : public BSDAddressingPolicy,
229           private BSDAddressingPolicyMixin<INet4SocketAddress>
230     {
231         typedef INet4SocketAddress Address;
232
233         using BSDAddressingPolicyMixin<INet4SocketAddress>::peer;
234         using BSDAddressingPolicyMixin<INet4SocketAddress>::local;
235         using BSDAddressingPolicyMixin<INet4SocketAddress>::connect;
236         using BSDAddressingPolicyMixin<INet4SocketAddress>::bind;
237     };
238
239     /** \brief Addressing policy supporting IPv6 addressing
240
241         \par Address Type:
242             INet6SocketAddress
243
244         This addressing policy implements addressing using Internet V6
245         addresses.
246
247         The various members are directly imported from
248         BSDAddressingPolicyMixin which see for a detailed
249         documentation.
250      */
251     struct INet6AddressingPolicy
252         : public BSDAddressingPolicy,
253           private BSDAddressingPolicyMixin<INet6SocketAddress>
254     {
255         typedef INet6SocketAddress Address;
256
257         using BSDAddressingPolicyMixin<INet6SocketAddress>::peer;
258         using BSDAddressingPolicyMixin<INet6SocketAddress>::local;
259         using BSDAddressingPolicyMixin<INet6SocketAddress>::connect;
260         using BSDAddressingPolicyMixin<INet6SocketAddress>::bind;
261     };
262
263     /// @}
264
265 }
266
267 ///////////////////////////////hh.e////////////////////////////////////////
268 #include "INetAddressing.cci"
269 //#include "INetAddressing.ct"
270 //#include "INetAddressing.cti"
271 //#include "INetAddressing.mpp"
272 #endif
273
274
275 // Local Variables:
276 // mode: c++
277 // fill-column: 100
278 // c-file-style: "senf"
279 // indent-tabs-mode: nil
280 // ispell-local-dictionary: "american"
281 // compile-command: "scons -u test"
282 // comment-column: 40
283 // End: