Add TCPv6 SocketHandle implementation
[senf.git] / Socket / INetAddressing.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 "SocketPolicy.hh"
35 #include "ClientSocketHandle.hh"
36 #include "CommunicationPolicy.hh"
37 #include "GenericAddressingPolicy.hh"
38
39 //#include "INetAddressing.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43
44     /// \addtogroup addr_group
45     /// @{
46
47     /** \brief IPv4 socket address
48
49         INet4Address wrapps the standard sockaddr_in datatype. It provides simple accessor methods
50         to accss the host and port. It does \e not integrate \c gethostbyname or DNS lookup.
51
52         \todo Implement real INet4Address datatype and rename this one to INet4SockAddress ...
53         \todo Implement more complete interface
54         \todo  gethostbyname support ?
55      */
56     class INet4Address
57     {
58     public:
59         INet4Address();
60         INet4Address(char const * address); ///< Set address and port
61                                         /**< See INet4Address(std::string)
62                                              \throws InvalidINetAddressException
63                                              \fixme Why do I need this version? Shouldn't the
64                                              std::string version be enough ? */
65         INet4Address(std::string address); ///< Set address and port
66                                         /**< This constructor expects a string of the form
67                                              'xxx.xxx.xxx.xxx:pppp'. The constructor will use this
68                                              value to initialize the host and port members. This
69                                              constructor does \e only support numeric ip addresses
70                                              not hostnames 
71                                              \param[in] address Address and port 
72                                              \throws InvalidINetAddressException */
73         INet4Address(std::string host, unsigned port); ///< Set address and port explicitly
74                                         /**< \param[in] host ip address in dotted-quad notation
75                                              \param[in] port port number 
76                                              \throws InvalidINetAddressException */
77         
78
79         bool operator==(INet4Address const & other) const; ///< Check INet4Address for equality
80
81         std::string str() const;        ///< Return "address:port" string
82         std::string host() const;       ///< Return address in doted quad notation
83         unsigned port() const;          ///< Return portnumber
84
85         void clear();                   ///< Clear address/port to 0.0.0.0:0
86
87         /// \name Generic Address Interface
88         /// @{
89
90         struct sockaddr * sockaddr_p();
91         struct sockaddr const * sockaddr_p() const;
92         unsigned sockaddr_len() const;
93
94         /// @}
95
96     private:
97         void assignString(std::string addr);
98         
99         struct ::sockaddr_in addr_;
100     };
101
102     /** \brief Write address and port to os
103
104         \related INet4Address 
105      */
106     std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
107
108     /** \brief IPv6 network address
109
110         INet6Address represents a 128bit IPv6 network address. This class supports all standard
111         numeric string representations of IPv6 addresses. This class does not integrate with \c
112         gethostbyname() and so does not support host names.
113
114         The conversion constructors allow the use of string constants whereever an INet6Address is
115         expected. Especially, it is possible to assign a string to an address to change it's value.
116
117         \implementation The <tt>char const *</tt> constructor overload is needed to support
118             string-literals where an INet6Address is expected (the C++ standard does not allow
119             chaining conversion constructors like char const * -> std::string -ยป INet6Address)
120      */
121     class INet6Address
122     {
123     public:
124         ///////////////////////////////////////////////////////////////////////////
125         // Types
126
127         ///////////////////////////////////////////////////////////////////////////
128         ///\name Structors and default members
129         ///@{
130
131         INet6Address();
132         INet6Address(std::string const & addr);
133         INet6Address(char const * addr);
134         INet6Address(struct in6_addr const & addr);
135
136         ///@}
137         ///////////////////////////////////////////////////////////////////////////
138
139         void clear();                   ///< Clear address
140         std::string address() const;    ///< Return printable address representation
141
142         bool operator==(INet6Address const & other) const; ///< Compare addresses for equality
143         bool operator!=(INet6Address const & other) const; ///< Inverse of above
144
145         struct in6_addr & addr();       ///< Access internal address representation
146         struct in6_addr const & addr() const; 
147                                         ///< Access internal address representation in const context
148         struct in6_addr * addr_p();     ///< Get pointer to internal address repr
149         struct in6_addr const * addr_p() const;
150                                         ///< Get const pointer to internal address repr
151         unsigned addr_len() const;      ///< Size of an IPv6 address (16 bytes)
152
153     protected:
154
155     private:
156         struct in6_addr addr_;
157     };
158
159     
160     /** \brief Output INet6Address instance as it's string representation
161      */
162     std::ostream & operator<<(std::ostream & os, INet6Address const & addr);
163
164     /** \brief IPv6 socket address
165
166         This class wrapps the standard \c sockaddr_in6 structure. INet6SocketAddress provides access
167         to all members of the sockaddr_in6 structure. Additionally, INet6SocketAddress supports the
168         string representation
169
170         \par "" <tt>[</tt> <i>address</i> [ <tt>\@</tt> <i>interface</i> ] <tt>]:</tt> <i>port</i>
171
172         Where \e address is an arbitrary numeric IPv6 address, \e interface is an optional network
173         interface name and \e port is the port number. The interface specification is only valid if
174         \e address is link-local address. The URL representation of an IPv6 address is as above
175         without the optional interface spec.
176
177         INet6SocketAddress supports conversion constructors from it's string
178         representation. Therefore, wherever a INet6SocketAddress instance is expected, a string may
179         be used instead.
180
181         \implementation The sockaddr_in6 structure has an sin6_flowinfo member. However RFC3493 does
182             not give the use of this field and specifies, that the field should be ignored ... so
183             that's what we do. Furthermore, the GNU libc reference states, that this field is not
184             implemented in the library.
185
186         \implementation We need to return the address in host() by value since we need to return a
187             INet6Address. However, sockaddr_in6 does not have one ...
188
189         \implementation The <tt>char const *</tt> constructor overload is needed to support
190             string-literals where an INet6SocketAddress is expected (the C++ standard does not allow
191             chaining conversion constructors like <tt>char const *</tt> -> \c std::string -> \c
192             INet6SocketAddress) 
193
194         \idea Implement a INet6Address_ref class which has an interface identical to INet6Address
195         and is convertible to INet6Address (the latter has a conversion constructor taking the
196         former as arg). This class however references an external in6_addr instead of containing one
197         itself. This can be used in INet6SocketAddress to increase the performance of some
198         operations.
199      */
200     class INet6SocketAddress
201     {
202     public:
203         ///////////////////////////////////////////////////////////////////////////
204         // Types
205
206         ///////////////////////////////////////////////////////////////////////////
207         ///\name Structors and default members
208         ///@{
209
210         INet6SocketAddress();           ///< Create empty instance
211         INet6SocketAddress(std::string const & addr); 
212                                         ///< Initialize/convert from string represenation
213         INet6SocketAddress(char const * addr); ///< Same as above to support string literals
214         INet6SocketAddress(INet6Address const & addr, unsigned port);
215                                         ///< Initialize from address and port
216         INet6SocketAddress(INet6Address const & addr, unsigned port, std::string const & iface);
217                                         ///< Initialize explicitly from given parameters
218         INet6SocketAddress(std::string const & addr, std::string const & iface);
219                                         ///< Initialize from URL representation and explit interface
220
221         ///@}
222         ///////////////////////////////////////////////////////////////////////////
223
224         bool operator==(INet6SocketAddress const & other) const; ///< Check addresses for equality
225         bool operator!=(INet6SocketAddress const & other) const; ///< Inverse of above
226
227         void clear();                   ///< Clear socket address
228
229         std::string address() const;    ///< Get printable address representation
230
231         INet6Address host() const;      ///< Get address
232         void host(INet6Address const & addr); ///< Change address
233         
234         unsigned port() const;          ///< Get port number
235         void port(unsigned poirt);      ///< Change port number
236         
237         std::string iface() const;      ///< Get interface name
238         void iface(std::string const & iface); ///< Change interface
239         
240         ///\name Generic SocketAddress interface
241         ///@{
242
243         struct sockaddr * sockaddr_p();
244         struct sockaddr const * sockaddr_p() const;
245         unsigned sockaddr_len() const;
246
247         ///@}
248
249     protected:
250
251     private:
252         void assignAddr(std::string const & addr);
253         void assignIface(std::string const & iface);
254
255         struct sockaddr_in6 sockaddr_;
256     };
257
258     /** \brief Output INet6SocketAddress instance as it's string representation
259      */
260     std::ostream & operator<<(std::ostream & os, INet6SocketAddress const & addr);
261
262     /** \brief Signal invalid INet address syntax
263
264         \related INet4Address
265         \relatesalso INet6Address
266      */
267     struct InvalidINetAddressException : public std::exception
268     { char const * what() const throw() { return "invalid inet address"; } };
269
270     /// @}
271
272     /// \addtogroup policy_impl_group
273     /// @{
274
275     /** \brief Addressing policy supporting IPv4 addressing
276         
277         \par Address Type:
278             INet4Address
279         
280         This addressing policy implements addressing using Internet V4
281         addresses.
282
283         The various members are directly importet from
284         GenericAddressingPolicy which see for a detailed
285         documentation.
286      */
287     struct INet4AddressingPolicy 
288         : public AddressingPolicyBase,
289           private GenericAddressingPolicy<INet4Address>
290     {
291         typedef INet4Address Address;
292
293         using GenericAddressingPolicy<INet4Address>::peer;
294         using GenericAddressingPolicy<INet4Address>::local;
295         using GenericAddressingPolicy<INet4Address>::connect;
296         using GenericAddressingPolicy<INet4Address>::bind;
297     };
298
299     /** \brief Addressing policy supporting IPv6 addressing
300         
301         \par Address Type:
302             INet6SocketAddress
303         
304         This addressing policy implements addressing using Internet V6
305         addresses.
306
307         The various members are directly importet from
308         GenericAddressingPolicy which see for a detailed
309         documentation.
310      */
311     struct INet6AddressingPolicy
312         : public AddressingPolicyBase,
313           private GenericAddressingPolicy<INet6SocketAddress>
314     {
315         typedef INet6SocketAddress Address;
316
317         using GenericAddressingPolicy<INet6SocketAddress>::peer;
318         using GenericAddressingPolicy<INet6SocketAddress>::local;
319         using GenericAddressingPolicy<INet6SocketAddress>::connect;
320         using GenericAddressingPolicy<INet6SocketAddress>::bind;
321     };
322
323     /// @}
324
325 }
326
327 ///////////////////////////////hh.e////////////////////////////////////////
328 #include "INetAddressing.cci"
329 //#include "INetAddressing.ct"
330 //#include "INetAddressing.cti"
331 //#include "INetAddressing.mpp"
332 #endif
333
334 \f
335 // Local Variables:
336 // mode: c++
337 // c-file-style: "senf"
338 // fill-column: 100
339 // End: