4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief INet[46]Address and INet[46]AddressingPolicy public header
32 #ifndef HH_SENF_Socket_Protocols_INet_INetAddressing_
33 #define HH_SENF_Socket_Protocols_INet_INetAddressing_ 1
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"
43 //#include "INetAddressing.mpp"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 /** \brief IPv4 socket address
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.
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.
58 class INet4SocketAddress
59 : public BSDSocketAddress
62 static short const addressFamily = AF_INET;
64 //-////////////////////////////////////////////////////////////////////////
65 ///\name Structors and default members
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
77 \throws UnknownHostnameException if the
78 address cannot be resolved. */
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 */
85 explicit INet4SocketAddress(unsigned port);
86 ///< Set port, address is set to 0.0.0.0
87 /**< \param[in] port port number */
89 INet4SocketAddress(const INet4SocketAddress& other);
90 INet4SocketAddress& operator=(const INet4SocketAddress& other);
93 //-////////////////////////////////////////////////////////////////////////
95 INet4Address address() const; ///< Return address
96 unsigned port() const; ///< Return port number
98 void address(INet4Address const & addr); ///< Set address
99 void port(unsigned p); ///< Set port number
101 using BSDSocketAddress::sockaddr_p;
102 using BSDSocketAddress::socklen_p;
105 struct ::sockaddr_in addr_;
108 /** \brief Write address and port to os
110 \related INet4SocketAddress
112 std::ostream & operator<<(std::ostream & os, INet4SocketAddress const & addr);
113 std::istream & operator>>(std::istream & is, INet4SocketAddress & addr);
115 /** \brief IPv6 socket address
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
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>
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
129 <tt>[2001:db8:1::1]:80</tt> \n
130 <tt>www.go6.net:80</tt> \n
131 <tt>[fe80::1\%eth0]:443</tt>
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.
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
146 class INet6SocketAddress
147 : public BSDSocketAddress
150 static short const addressFamily = AF_INET6;
152 //-////////////////////////////////////////////////////////////////////////
153 ///\name Structors and default members
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
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 */
179 INet6SocketAddress(const INet6SocketAddress& other);
180 INet6SocketAddress& operator=(const INet6SocketAddress& other);
183 //-////////////////////////////////////////////////////////////////////////
185 INet6Address address() const; ///< Get printable address representation
186 void address(INet6Address const & addr); ///< Change address
188 unsigned port() const; ///< Get port number
189 void port(unsigned port); ///< Change port number
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. */
196 using BSDSocketAddress::sockaddr_p;
197 using BSDSocketAddress::socklen_p;
202 void assignIface(std::string const & iface);
204 struct sockaddr_in6 sockaddr_;
207 /** \brief Output INet6SocketAddress instance as it's string representation
208 \related INet6SocketAddress
210 std::ostream & operator<<(std::ostream & os, INet6SocketAddress const & addr);
211 std::istream & operator>>(std::istream & is, INet6SocketAddress & addr);
213 /// \addtogroup policy_impl_group
216 /** \brief Addressing policy supporting IPv4 addressing
221 This addressing policy implements addressing using Internet V4
224 The various members are directly imported from
225 BSDAddressingPolicyMixin which see for a detailed
228 struct INet4AddressingPolicy
229 : public BSDAddressingPolicy,
230 private BSDAddressingPolicyMixin<INet4SocketAddress>
232 typedef INet4SocketAddress Address;
234 using BSDAddressingPolicyMixin<INet4SocketAddress>::peer;
235 using BSDAddressingPolicyMixin<INet4SocketAddress>::local;
236 using BSDAddressingPolicyMixin<INet4SocketAddress>::connect;
237 using BSDAddressingPolicyMixin<INet4SocketAddress>::bind;
240 /** \brief Addressing policy supporting IPv6 addressing
245 This addressing policy implements addressing using Internet V6
248 The various members are directly imported from
249 BSDAddressingPolicyMixin which see for a detailed
252 struct INet6AddressingPolicy
253 : public BSDAddressingPolicy,
254 private BSDAddressingPolicyMixin<INet6SocketAddress>
256 typedef INet6SocketAddress Address;
258 using BSDAddressingPolicyMixin<INet6SocketAddress>::peer;
259 using BSDAddressingPolicyMixin<INet6SocketAddress>::local;
260 using BSDAddressingPolicyMixin<INet6SocketAddress>::connect;
261 using BSDAddressingPolicyMixin<INet6SocketAddress>::bind;
268 //-/////////////////////////////////////////////////////////////////////////////////////////////////
269 #include "INetAddressing.cci"
270 //#include "INetAddressing.ct"
271 //#include "INetAddressing.cti"
272 //#include "INetAddressing.mpp"
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