switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / TunTapSocketHandle.cc
1 // $Id$
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
30
31  */
32
33 #include "TunTapSocketHandle.hh"
34 //#include "TunTapSocketHandle.ih"
35
36 // Custom includes
37 #include <sys/ioctl.h>
38 #include <fcntl.h>
39 #include <net/if.h>
40 #include <linux/if_tun.h>
41 #include <errno.h>
42
43 //#include "TunTapSocketHandle.mpp"
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 prefix_ void senf::TapSocketProtocol::init_client()
48     const
49 {
50     init_client(std::string());
51 }
52
53 prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
54     const
55 {
56     int f;
57     if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
58         SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
59     struct ifreq ifr;
60     ::memset( &ifr, 0, sizeof(ifr));
61     ifr.ifr_flags = IFF_TAP;
62     if (NO_PI)
63         ifr.ifr_flags |= IFF_NO_PI;
64     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
65     if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 )
66         SENF_THROW_SYSTEM_EXCEPTION( "Could not create tap device: ") << ifr.ifr_name << ".";
67     ifaceIndex_ = if_nametoindex(ifr.ifr_name);
68     fd(f);
69 }
70
71 prefix_ unsigned senf::TapSocketProtocol::available()
72   const
73 {
74     if (!fh().readable())
75         return 0;
76     ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
77     if (l < 0)
78         //SENF_THROW_SYSTEM_EXCEPTION("");
79         return 1588;
80     return l;
81 }
82
83 /*
84 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
85
86 prefix_ unsigned senf::TapSocketProtocol::available()
87   const
88 {
89   if (! body().readable())
90       return 0;
91   int n;
92   if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
93       SENF_THROW_SYSTEM_EXCEPTION("");
94   return n;
95 }
96 */
97
98 prefix_ bool senf::TapSocketProtocol::eof()
99     const
100 {
101     return false;
102 }
103
104 prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
105     const
106 {
107     return ifaceIndex_;
108 }
109
110 prefix_ std::string senf::TapSocketProtocol::ifaceName()
111     const
112 {
113     char buf[IF_NAMESIZE];
114     if_indextoname(ifaceIndex_, buf);
115     return std::string(buf);
116 }
117
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 #undef prefix_
120 //#include "TunTapSocketHandle.mpp"
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // comment-column: 40
131 // End: