Socket: Introduce sub-directory structure for concrete protocols
[senf.git] / Socket / Protocols / PacketSocketHandle.hh
1 // $Id:PacketSocketHandle.hh 218 2007-03-20 14:39:32Z tho $
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 PacketProtocol and PacketSocketHandle public header
25
26     \todo Implement global promisc() helper based on ioctl() interface.
27  */
28
29 #ifndef HH_PacketSocketHandle_
30 #define HH_PacketSocketHandle_ 1
31
32 // Custom includes
33 #include "Socket/SocketPolicy.hh"
34 #include "Socket/SocketProtocol.hh"
35 #include "Socket/ProtocolClientSocketHandle.hh"
36 #include "Socket/FramingPolicy.hh"
37 #include "Socket/CommunicationPolicy.hh"
38 #include "Socket/ReadWritePolicy.hh"
39 #include "Socket/BufferingPolicy.hh"
40 #include "LLAddressing.hh"
41 #include "BSDSocketProtocol.hh"
42
43 //#include "PacketSocketHandle.mpp"
44 #include "PacketSocketHandle.ih"
45 ///////////////////////////////hh.p////////////////////////////////////////
46
47 namespace senf {
48
49     /// \addtogroup concrete_protocol_group
50     /// @{
51
52     typedef MakeSocketPolicy<
53         LLAddressingPolicy,
54         DatagramFramingPolicy,
55         UnconnectedCommunicationPolicy,
56         ReadablePolicy,
57         WriteablePolicy,
58         SocketBufferingPolicy
59         >::policy Packet_Policy;        ///< Policy of PacketProtocol
60
61     /** \brief Raw Packet-Socket access (Linux)
62
63         \par Socket Handle typedefs:
64         \ref PacketSocketHandle (ProtocolClientSocketHandle)
65
66         \par Policy Interface:
67         ClientSocketHandle::read(), ClientSocketHandle::readfrom(), ClientSocketHandle::writeto(),
68         ClientSocketHandle::bind(), ClientSocketHandle::local(), ClientSocketHandle::rcvbuf(),
69         ClientSocketHandle::sndbuf()
70
71         \par Address Type:
72         LLSocketAddress
73
74         The PacketProtocol provides access to the linux packet socket API. This API gives access to
75         the low level network packets. The packet socket allows read() and write() operations. The
76         PacketProtocol has no concept of a server socket.
77
78         This class is utilized as the protocol class of the ProtocolClientSocketHandle via the
79         Socket Handle typedefs above.
80      */
81     class PacketProtocol
82         : public ConcreteSocketProtocol<Packet_Policy>,
83           public BSDSocketProtocol,
84           public senf::pool_alloc_mixin<PacketProtocol>
85     {
86     public:
87         enum SocketType { RawSocket, DatagramSocket };
88                                         ///< Socket types
89
90         ///\name Constructors
91         ///@{
92         void init_client(SocketType type = RawSocket, int protocol = -1) const;
93                                         ///< Create packet socket
94                                         /**< The new socket will receive all packets of the given
95                                              IEEE 802.3 \a protocol. The socket will receive all
96                                              packets, if \a protocol is -1.
97
98                                              If \a type is \c RawSocket, the packet will include the
99                                              link-level header (the Ethernet header). Sent packets
100                                              must already include a well formed ll header.
101
102                                              If \a type is \c DatagramSocket, the link level header
103                                              will not be part of the packet data. The ll header will
104                                              be removed from received packets and a correct ll
105                                              header will be created on sent packets.
106
107                                              \param[in] type socket type
108                                              \param[in] protocol IEEE 802.3 protocol number */
109                                         /**< \note This member is implicitly called from the
110                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
111                                              constructor */
112         ///@}
113
114         ///\name Protocol Interface
115         ///@{
116
117         // See LLSocketAddress for a discussion/rationale for ForwardRange here
118         template <class ForwardRange>
119         void mcAdd(std::string interface, ForwardRange const & address) const;
120                                         ///< Enable reception of a multicast group
121                                         /**< mcAdd will join a new multicast group. The address
122                                              parameter is specified as an arbitrary forward range
123                                              (see <a
124                                              href="http://www.boost.org/libs/range/index.html">Boost.Range</a>)
125                                              of up to 8 bytes. This  allows to initialize the
126                                              address from an arbitrary sources without excessive
127                                              copying.
128
129                                              \param[in] interface interface with which to join
130                                              \param[in] address multicast address to join
131
132                                              \see \ref LLSocketAddress */
133         template <class ForwardRange>
134         void mcDrop(std::string interface, ForwardRange const & address) const;
135                                         ///< Disable reception of a multicast group
136                                         /**< \see \ref mcAdd() */
137         ///@}
138
139         ///\name Abstract Interface Implementation
140         ///@{
141
142         std::auto_ptr<SocketProtocol> clone() const;
143         unsigned available() const;
144         bool eof() const;
145
146         ///@}
147
148     private:
149         template<class ForwardRange>
150         void do_mc(std::string interface, ForwardRange const & address, bool add) const;
151         void do_mc_i(std::string interface, detail::LLAddressCopier const & copier, bool add) const;
152     };
153
154     typedef ProtocolClientSocketHandle<PacketProtocol> PacketSocketHandle;
155                                         ///< SocketHandle of the PacketProtocol
156                                         /**< \related PacketPrototol */
157
158     /// @}
159 }
160
161 ///////////////////////////////hh.e////////////////////////////////////////
162 //#include "PacketSocketHandle.cci"
163 #include "PacketSocketHandle.ct"
164 #include "PacketSocketHandle.cti"
165 //#include "PacketSocketHandle.mpp"
166 #endif
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // comment-column: 40
177 // End: