switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / BSDAddressingPolicy.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 BSDAddressingPolicyMixin public header
30  */
31
32 /** \defgroup addr_group Addressing classes
33  */
34
35 #ifndef HH_SENF_Socket_Protocols_BSDAddressingPolicy_
36 #define HH_SENF_Socket_Protocols_BSDAddressingPolicy_ 1
37
38 // Custom includes
39 #include <senf/Socket/SocketHandle.hh>
40 #include <senf/Socket/FileHandle.hh>
41 #include <senf/Socket/CommunicationPolicy.hh>
42 #include "BSDSocketAddress.hh"
43
44 //#include "BSDAddressingPolicy.mpp"
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 namespace senf {
48
49     /// \addtogroup policy_impl_group
50     //\{
51
52     /** \brief Non-template implementation class of BSDAddressingPolicyMixin template
53
54         \internal
55      */
56     struct BSDAddressingPolicyMixinBase
57     {
58         static void do_local(FileHandle const & handle, struct sockaddr * addr, socklen_t * len);
59         static void do_peer(FileHandle const & handle, struct sockaddr * addr, socklen_t * len);
60         static void do_bind(FileHandle const & handle, struct sockaddr const * addr, socklen_t len);
61         static void do_connect(FileHandle const & handle, struct sockaddr const * addr, socklen_t len);
62     };
63
64     /** \brief Template for generic AddressingPolicy implementation based on the BSD socket API
65
66         This template provides an implementation template to implement generic addressing policy
67         classes which rely on the standard BSD socket API for their implementation
68         (connect/bind/getsockname/getpeername).
69
70         The \a Address template parameter specifies the address type of the addressing policy. This
71         type must have two members: \c sockaddr_p() and \c socklen(). The first must return a
72         <tt>struct sockaddr *</tt> to the address, the second must return the size of the address in
73         bytes. The pointer returned by \c sockaddr_p() must be non-const if called on a non-const
74         address. <em>The underlying socket address stored at that pointer might be
75         modified</em>.
76
77         This template class is inherited into addressing policy classes via private
78         inheritance. Then the members supported by the respective addressing policy are made
79         available via \c using declarations (See INet4AddressingPolicy for an Example).
80
81         \idea We could explicitly provide open_sockaddr_p() and close_sockaddr_p()
82         members. sockaddr_p could always return a const * whereas open_sockaddr_p should return a
83         non-const pointer. The close operation would then explicitly signal, that the new value
84         should be incorporated into the class. With our current implementation, the close member
85         would be a no-op, however this should free us from using the sockaddr values as a direct
86         storage representation of the address.
87      */
88     template <class Address>
89     struct BSDAddressingPolicyMixin
90         : private BSDAddressingPolicyMixinBase
91     {
92 #       ifndef DOXYGEN
93         template <class SPolicy>
94         static void peer(SocketHandle<SPolicy> const & handle, Address & addr,
95                          typename IfCommunicationPolicyIs<SPolicy,ConnectedCommunicationPolicy>::type * = 0);
96 #       else
97         template <class SPolicy>
98         static void peer(SocketHandle<SPolicy> const & handle, Address & addr);
99                                         ///< Return address of remote peer on connected sockets
100                                         /**< This member is only available if the socket handles
101                                              communication policy is ConnectedCommunicationPolicy.
102
103                                              \param[in] handle socket handle to get peer address of
104                                              \param[out] addr address of remote peer */
105 #       endif
106         static void local(FileHandle const & handle, Address & addr);
107                                         ///< Return local of socket
108                                         /**< \param[in] handle socket handle to check
109                                              \param[out] addr local socket address */
110
111 #       ifndef DOXYGEN
112         template <class SPolicy>
113         static void connect(SocketHandle<SPolicy> const & handle, Address const & addr,
114                             typename IfCommunicationPolicyIs<SPolicy,ConnectedCommunicationPolicy>::type * = 0);
115 #       else
116         template <class SPolicy>
117         static void connect(SocketHandle<SPolicy> const & handle, Address const & addr);
118                                         ///< Connect to remote host
119                                         /**< This member is only available if the socket handles
120                                              communication policy is ConnectedCommunicationPolicy.
121
122                                              \param[in] handle socket handle
123                                              \param[in] addr address of remote peer to connect
124                                                  to */
125 #       endif
126         static void bind(FileHandle const & handle, Address const & addr);
127                                         ///< Set local socket address
128                                         /**< \param[in] handle socket handle
129                                              \param[in] addr local socket address */
130     };
131
132     //\}
133
134     struct BSDAddressingPolicy
135         : public AddressingPolicyBase,
136           private BSDAddressingPolicyMixin<GenericBSDSocketAddress>
137     {
138         typedef GenericBSDSocketAddress Address;
139
140         using BSDAddressingPolicyMixin<GenericBSDSocketAddress>::peer;
141         using BSDAddressingPolicyMixin<GenericBSDSocketAddress>::local;
142         using BSDAddressingPolicyMixin<GenericBSDSocketAddress>::connect;
143         using BSDAddressingPolicyMixin<GenericBSDSocketAddress>::bind;
144     };
145
146 }
147
148 //-/////////////////////////////////////////////////////////////////////////////////////////////////
149 //#include "BSDAddressingPolicy.cci"
150 //#include "BSDAddressingPolicy.ct"
151 #include "BSDAddressingPolicy.cti"
152 //#include "BSDAddressingPolicy.mpp"
153 #endif
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // fill-column: 100
159 // c-file-style: "senf"
160 // indent-tabs-mode: nil
161 // ispell-local-dictionary: "american"
162 // compile-command: "scons -u ../test"
163 // comment-column: 40
164 // End: