Socket: Add additional port-only constructor for INet[46]SocketAddress
[senf.git] / 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_INetAddressing_
28 #define HH_INetAddressing_ 1
29
30 // Custom includes
31 #include <string>
32 #include <exception>
33 #include <netinet/in.h>
34 #include <boost/operators.hpp>
35 #include "../../../Socket/SocketPolicy.hh"
36 #include "../../../Socket/ClientSocketHandle.hh"
37 #include "../../../Socket/CommunicationPolicy.hh"
38 #include "../../../Socket/Protocols/GenericAddressingPolicy.hh"
39 #include "../../../Utils/safe_bool.hh"
40 #include "INet4Address.hh"
41 #include "INet6Address.hh"
42
43 //#include "INetAddressing.mpp"
44 ///////////////////////////////hh.p////////////////////////////////////////
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 boost::equality_comparable<INet4SocketAddress>, 
60           public senf::comparable_safe_bool<INet4SocketAddress>
61     {
62     public:
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         ///@}
89         ///////////////////////////////////////////////////////////////////////////
90
91         bool operator==(INet4SocketAddress const & other) const;
92                                         ///< Check INet4SocketAddress for equality
93
94         INet4Address address() const;   ///< Return address
95         unsigned port() const;          ///< Return port number
96
97         bool boolean_test() const;      ///< \c true, if address is not empty (i.e. 0.0.0.0:0)
98
99         void clear();                   ///< Clear address/port to 0.0.0.0:0
100
101         void address(INet4Address const & addr); ///< Set address
102         void port(unsigned p);          ///< Set port number
103
104         /// \name Generic Address Interface
105         /// @{
106
107         struct sockaddr * sockaddr_p();
108         struct sockaddr const * sockaddr_p() const;
109         unsigned sockaddr_len() const;
110
111         /// @}
112
113     private:
114         struct ::sockaddr_in addr_;
115     };
116
117     /** \brief Write address and port to os
118
119         \related INet4SocketAddress
120      */
121     std::ostream & operator<<(std::ostream & os, INet4SocketAddress const & addr);
122
123     /** \brief IPv6 socket address
124
125         This class wraps the standard \c sockaddr_in6 structure. INet6SocketAddress provides access
126         to all members of the sockaddr_in6 structure. Additionally, INet6SocketAddress supports the
127         string representations
128
129         \par ""
130             <tt>[</tt> <i>address</i> [ <tt>%</tt> <i>zone-id</i> ] <tt>]:</tt> <i>port</i> \n
131             <i>hostname</i> <tt>:</tt> <i>port</i>
132
133         Where \e address is an arbitrary numeric IPv6 address, \e zone-id is an optional network
134         interface name and \e port is the port number. So some example addresses are
135         
136         \par ""
137             <tt>[2001:db8:1::1]:80</tt> \n
138             <tt>www.6bone.net:80</tt> \n
139             <tt>[fe80::1\%eth0]:443</tt>
140
141         \implementation The sockaddr_in6 structure has an sin6_flowinfo member. However RFC3493 does
142             not give the use of this field and specifies, that the field should be ignored ... so
143             that's what we do. Furthermore, the GNU libc reference states, that this field is not
144             implemented in the library.
145
146         \idea Implement a INet6Address_ref class which has an interface identical to INet6Address
147             and is convertible to INet6Address (the latter has a conversion constructor taking the
148             former as arg). This class however references an external in6_addr instead of containing
149             one itself. This can be used in INet6SocketAddress to increase the performance of some
150             operations.
151
152         \ingroup addr_group
153      */
154     class INet6SocketAddress
155         : public boost::equality_comparable<INet6SocketAddress>, 
156           public senf::comparable_safe_bool<INet6SocketAddress>
157     {
158     public:
159         ///////////////////////////////////////////////////////////////////////////
160         ///\name Structors and default members
161         ///@{
162
163         INet6SocketAddress();           ///< Create empty instance
164         explicit INet6SocketAddress(std::string const & addr, 
165                                     INet6Address::Resolve_t resolve = INet6Address::ResolveINet6);
166                                         ///< Initialize/convert from string representation
167                                         /**< \throws AddressSyntaxException if the address syntax is
168                                                  invalid
169                                              \throws UnknownHostnameException if the
170                                                  address cannot be resolved.
171                                              \param[in] addr Address to parse
172                                              \param[in] resolve If this is
173                                                  INet6Address::ResolveINet4, support IPv4
174                                                  addresses. See INet6Address. */
175         INet6SocketAddress(INet6Address const & addr, unsigned port);
176                                         ///< Initialize from address and port
177         INet6SocketAddress(INet6Address const & addr, unsigned port, std::string const & iface);
178                                         ///< Initialize explicitly from given parameters
179         explicit INet6SocketAddress(unsigned port);
180                                         ///< Initialize from port and set to 'unspecified' addr
181                                         /**< The address is set to [::]
182                                              \param[in] port port number  */
183
184         ///@}
185         ///////////////////////////////////////////////////////////////////////////
186
187         bool operator==(INet6SocketAddress const & other) const; ///< Check addresses for equality
188
189         void clear();                   ///< Clear socket address
190
191         INet6Address address() const;    ///< Get printable address representation
192
193         void address(INet6Address const & addr); ///< Change address
194
195         unsigned port() const;          ///< Get port number
196         void port(unsigned poirt);      ///< Change port number
197
198         std::string iface() const;      ///< Get interface name
199         void iface(std::string const & iface); ///< Change interface
200
201         bool boolean_test() const;      ///< \c true, if address is not empty (i.e. [::]:0)
202
203         ///\name Generic SocketAddress interface
204         ///@{
205
206         struct sockaddr * sockaddr_p();
207         struct sockaddr const * sockaddr_p() const;
208         unsigned sockaddr_len() const;
209
210         ///@}
211
212     protected:
213
214     private:
215         void assignIface(std::string const & iface);
216
217         struct sockaddr_in6 sockaddr_;
218     };
219
220     /** \brief Output INet6SocketAddress instance as it's string representation
221         \related INet6SocketAddress
222      */
223     std::ostream & operator<<(std::ostream & os, INet6SocketAddress const & addr);
224
225     /// \addtogroup policy_impl_group
226     /// @{
227
228     /** \brief Addressing policy supporting IPv4 addressing
229
230         \par Address Type:
231             INet4SocketAddress
232
233         This addressing policy implements addressing using Internet V4
234         addresses.
235
236         The various members are directly imported from
237         GenericAddressingPolicy which see for a detailed
238         documentation.
239      */
240     struct INet4AddressingPolicy
241         : public AddressingPolicyBase,
242           private GenericAddressingPolicy<INet4SocketAddress>
243     {
244         typedef INet4SocketAddress Address;
245
246         using GenericAddressingPolicy<INet4SocketAddress>::peer;
247         using GenericAddressingPolicy<INet4SocketAddress>::local;
248         using GenericAddressingPolicy<INet4SocketAddress>::connect;
249         using GenericAddressingPolicy<INet4SocketAddress>::bind;
250     };
251
252     /** \brief Addressing policy supporting IPv6 addressing
253
254         \par Address Type:
255             INet6SocketAddress
256
257         This addressing policy implements addressing using Internet V6
258         addresses.
259
260         The various members are directly imported from
261         GenericAddressingPolicy which see for a detailed
262         documentation.
263      */
264     struct INet6AddressingPolicy
265         : public AddressingPolicyBase,
266           private GenericAddressingPolicy<INet6SocketAddress>
267     {
268         typedef INet6SocketAddress Address;
269
270         using GenericAddressingPolicy<INet6SocketAddress>::peer;
271         using GenericAddressingPolicy<INet6SocketAddress>::local;
272         using GenericAddressingPolicy<INet6SocketAddress>::connect;
273         using GenericAddressingPolicy<INet6SocketAddress>::bind;
274     };
275
276     /// @}
277
278 }
279
280 ///////////////////////////////hh.e////////////////////////////////////////
281 #include "INetAddressing.cci"
282 //#include "INetAddressing.ct"
283 //#include "INetAddressing.cti"
284 //#include "INetAddressing.mpp"
285 #endif
286
287 \f
288 // Local Variables:
289 // mode: c++
290 // fill-column: 100
291 // c-file-style: "senf"
292 // indent-tabs-mode: nil
293 // ispell-local-dictionary: "american"
294 // compile-command: "scons -u test"
295 // comment-column: 40
296 // End: