switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / TunTapSocketHandle.hh
1 // $Id:PacketSocketHandle.hh 218 2007-03-20 14:39:32Z tho $
2 //
3 // Copyright (C) 2008
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 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief PacketSocketProtocol and PacketSocketHandle public header
30  */
31
32 #ifndef HH_SENF_Socket_Protocols_Raw_TunTapSocketHandle_
33 #define HH_SENF_Socket_Protocols_Raw_TunTapSocketHandle_ 1
34
35 // Custom includes
36 #include <senf/Socket/SocketPolicy.hh>
37 #include <senf/Socket/SocketProtocol.hh>
38 #include <senf/Socket/ProtocolClientSocketHandle.hh>
39 #include <senf/Socket/FramingPolicy.hh>
40 #include <senf/Socket/CommunicationPolicy.hh>
41 #include <senf/Socket/ReadWritePolicy.hh>
42 #include <senf/Socket/Protocols/BSDSocketProtocol.hh>
43
44 //#include "TunTapSocketHandle.mpp"
45 //#include "TunTapSocketHandle.ih"
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace senf {
49
50     /// \addtogroup concrete_protocol_group
51     //\{
52
53     typedef MakeSocketPolicy<
54         NoAddressingPolicy,
55         DatagramFramingPolicy,
56         ConnectedCommunicationPolicy,
57         ReadablePolicy,
58         WriteablePolicy
59         >::policy Tap_Policy;        ///< Policy for TapSocketProtocol
60
61     /** \brief SocketProcol for the tap pseudo-device.
62
63         \par Socket Handle typedefs:
64             \ref TapSocketHandle
65
66         \par Policy Interface:
67             ClientSocketHandle::read() ...
68
69         The TapSocketProtocol provides access to the Linux tap device.
70
71         The tap device is a virtual ethernet network device. The tap driver was designed as low
72         level kernel support for ethernet tunneling. Userland application can write Ethernet
73         frames to the socket and kernel will receive this frames from the tap interface.
74         In the same time every frame that kernel writes to the tap interface can be read by
75         userland application from the socket.
76
77         This class is utilized as the protocol class of the ProtocolClientSocketHandle via the
78         Socket Handle typedef above.
79
80         \see <a href="http://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/Documentation/networking/tuntap.txt">Kernel documentation for the TUN/TAP device driver.</a>
81      */
82     class TapSocketProtocol
83         : public ConcreteSocketProtocol<Tap_Policy,TapSocketProtocol>,
84           public BSDSocketProtocol
85     {
86     public:
87         ///\name Constructors
88         //\{
89         void init_client() const;       ///< Open tap socket and create new tap interface
90                                         /**< Opens the tun/tap socket and create a new tap interface
91                                              Use \ref ifaceName() to get the actual name of the newly
92                                              created interface.
93
94                                              The new interface is down and has to be set up separately.
95                                              After closing the socket, the tap interface and all
96                                              corresponding routes will be deleted automatically. */
97                                         /**< \note This member is implicitly called from the
98                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
99                                              constructor */
100         void init_client(std::string const & interface_name, bool NO_PI=true) const;
101                                         ///< Open tap socket and create new tap interface
102                                         /**< Opens the tun/tap socket and create a new tap interface
103                                              with the given name. Note that the created interface can
104                                              have a different name as specified. Use \ref ifaceName()
105                                              to get the actual name.
106
107                                              The new interface is down and has to be set up separately.
108                                              After closing the socket, the tap interface and all
109                                              corresponding routes will be deleted automatically.
110                                              \param[in] interface_name name of the new tap interface.
111                                              \param[in] NO_PI if set to \c false each packet has a
112                                                  additional 4 bytes header (flags, proto) */
113                                         /**< \note This member is implicitly called from the
114                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
115                                              constructor */
116
117         //\}
118
119         ///\name Abstract Interface Implementation
120         //\{
121
122         unsigned available() const;
123         bool eof() const;               ///< returns always false.
124         unsigned int ifaceIndex() const;///< returns the index of the correspondent tap interface
125         std::string ifaceName() const;  ///< returns the actual name of the correspondent tap interface
126
127     private:
128         mutable unsigned int ifaceIndex_;
129         //\}
130     };
131
132     typedef ProtocolClientSocketHandle<TapSocketProtocol> TapSocketHandle;
133                                         ///< SocketHandle of TapSocketProtocol
134                                         /**< \related TapSocketProtocol */
135
136     //\}
137 }
138
139 //-/////////////////////////////////////////////////////////////////////////////////////////////////
140 //#include "TunTapSocketHandle.cci"
141 //#include "TunTapSocketHandle.ct"
142 //#include "TunTapSocketHandle.cti"
143 //#include "TunTapSocketHandle.mpp"
144 #endif
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // comment-column: 40
155 // End: