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