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