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