4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief INet[46]Address and INet[46]AddressingPolicy public header
27 #ifndef HH_SENF_Socket_Protocols_INet_INetAddressing_
28 #define HH_SENF_Socket_Protocols_INet_INetAddressing_ 1
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"
42 //#include "INetAddressing.mpp"
43 ///////////////////////////////hh.p////////////////////////////////////////
47 /** \brief IPv4 socket address
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.
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.
57 class INet4SocketAddress
58 : public BSDSocketAddress
61 static short const addressFamily = AF_INET;
63 ///////////////////////////////////////////////////////////////////////////
64 ///\name Structors and default members
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
76 \throws UnknownHostnameException if the
77 address cannot be resolved. */
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 */
84 explicit INet4SocketAddress(unsigned port);
85 ///< Set port, address is set to 0.0.0.0
86 /**< \param[in] port port number */
88 INet4SocketAddress(const INet4SocketAddress& other);
89 INet4SocketAddress& operator=(const INet4SocketAddress& other);
92 ///////////////////////////////////////////////////////////////////////////
94 INet4Address address() const; ///< Return address
95 unsigned port() const; ///< Return port number
97 void address(INet4Address const & addr); ///< Set address
98 void port(unsigned p); ///< Set port number
100 using BSDSocketAddress::sockaddr_p;
101 using BSDSocketAddress::socklen_p;
104 struct ::sockaddr_in addr_;
107 /** \brief Write address and port to os
109 \related INet4SocketAddress
111 std::ostream & operator<<(std::ostream & os, INet4SocketAddress const & addr);
113 /** \brief IPv6 socket address
115 This class wraps the standard \c sockaddr_in6 structure. INet6SocketAddress provides access
116 to all members of the sockaddr_in6 structure. Additionally, INet6SocketAddress supports the
117 string representations
120 <tt>[</tt> <i>address</i> [ <tt>%</tt> <i>zone-id</i> ] <tt>]:</tt> <i>port</i> \n
121 <i>hostname</i> <tt>:</tt> <i>port</i>
123 Where \e address is an arbitrary numeric IPv6 address, \e zone-id is an optional network
124 interface name and \e port is the port number. So some example addresses are
127 <tt>[2001:db8:1::1]:80</tt> \n
128 <tt>www.6bone.net:80</tt> \n
129 <tt>[fe80::1\%eth0]:443</tt>
131 \implementation The sockaddr_in6 structure has an sin6_flowinfo member. However RFC3493 does
132 not give the use of this field and specifies, that the field should be ignored ... so
133 that's what we do. Furthermore, the GNU libc reference states, that this field is not
134 implemented in the library.
136 \idea Implement a INet6Address_ref class which has an interface identical to INet6Address
137 and is convertible to INet6Address (the latter has a conversion constructor taking the
138 former as arg). This class however references an external in6_addr instead of containing
139 one itself. This can be used in INet6SocketAddress to increase the performance of some
144 class INet6SocketAddress
145 : public BSDSocketAddress
148 static short const addressFamily = AF_INET6;
150 ///////////////////////////////////////////////////////////////////////////
151 ///\name Structors and default members
154 INet6SocketAddress(); ///< Create empty instance
155 explicit INet6SocketAddress(std::string const & addr,
156 INet6Address::Resolve_t resolve = INet6Address::ResolveINet6);
157 ///< Initialize/convert from string representation
158 /**< \throws AddressSyntaxException if the address syntax is
160 \throws UnknownHostnameException if the
161 address cannot be resolved.
162 \param[in] addr Address to parse
163 \param[in] resolve If this is
164 INet6Address::ResolveINet4, support IPv4
165 addresses. See INet6Address. */
166 INet6SocketAddress(INet6Address const & addr, unsigned port);
167 ///< Initialize from address and port
168 INet6SocketAddress(INet6Address const & addr, unsigned port, std::string const & iface);
169 ///< Initialize explicitly from given parameters
170 explicit INet6SocketAddress(unsigned port);
171 ///< Initialize from port and set to 'unspecified' addr
172 /**< The address is set to [::]
173 \param[in] port port number */
175 INet6SocketAddress(const INet6SocketAddress& other);
176 INet6SocketAddress& operator=(const INet6SocketAddress& other);
179 ///////////////////////////////////////////////////////////////////////////
181 INet6Address address() const; ///< Get printable address representation
182 void address(INet6Address const & addr); ///< Change address
184 unsigned port() const; ///< Get port number
185 void port(unsigned poirt); ///< Change port number
187 std::string iface() const; ///< Get interface name
188 void iface(std::string const & iface); ///< Change interface
190 using BSDSocketAddress::sockaddr_p;
191 using BSDSocketAddress::socklen_p;
196 void assignIface(std::string const & iface);
198 struct sockaddr_in6 sockaddr_;
201 /** \brief Output INet6SocketAddress instance as it's string representation
202 \related INet6SocketAddress
204 std::ostream & operator<<(std::ostream & os, INet6SocketAddress const & addr);
206 /// \addtogroup policy_impl_group
209 /** \brief Addressing policy supporting IPv4 addressing
214 This addressing policy implements addressing using Internet V4
217 The various members are directly imported from
218 BSDAddressingPolicyMixin which see for a detailed
221 struct INet4AddressingPolicy
222 : public BSDAddressingPolicy,
223 private BSDAddressingPolicyMixin<INet4SocketAddress>
225 typedef INet4SocketAddress Address;
227 using BSDAddressingPolicyMixin<INet4SocketAddress>::peer;
228 using BSDAddressingPolicyMixin<INet4SocketAddress>::local;
229 using BSDAddressingPolicyMixin<INet4SocketAddress>::connect;
230 using BSDAddressingPolicyMixin<INet4SocketAddress>::bind;
233 /** \brief Addressing policy supporting IPv6 addressing
238 This addressing policy implements addressing using Internet V6
241 The various members are directly imported from
242 BSDAddressingPolicyMixin which see for a detailed
245 struct INet6AddressingPolicy
246 : public BSDAddressingPolicy,
247 private BSDAddressingPolicyMixin<INet6SocketAddress>
249 typedef INet6SocketAddress Address;
251 using BSDAddressingPolicyMixin<INet6SocketAddress>::peer;
252 using BSDAddressingPolicyMixin<INet6SocketAddress>::local;
253 using BSDAddressingPolicyMixin<INet6SocketAddress>::connect;
254 using BSDAddressingPolicyMixin<INet6SocketAddress>::bind;
261 ///////////////////////////////hh.e////////////////////////////////////////
262 #include "INetAddressing.cci"
263 //#include "INetAddressing.ct"
264 //#include "INetAddressing.cti"
265 //#include "INetAddressing.mpp"
272 // c-file-style: "senf"
273 // indent-tabs-mode: nil
274 // ispell-local-dictionary: "american"
275 // compile-command: "scons -u test"
276 // comment-column: 40