4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Thorsten Horstmann <tho@berlios.de>
33 #include "TunTapSocketHandle.hh"
34 //#include "TunTapSocketHandle.ih"
37 #include <sys/ioctl.h>
40 #include <linux/if_tun.h>
43 //#include "TunTapSocketHandle.mpp"
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 prefix_ void senf::TapSocketProtocol::init_client()
50 init_client(std::string());
53 prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
57 if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
58 SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
60 ::memset( &ifr, 0, sizeof(ifr));
61 ifr.ifr_flags = IFF_TAP;
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);
71 prefix_ unsigned senf::TapSocketProtocol::available()
76 ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
78 //SENF_THROW_SYSTEM_EXCEPTION("");
84 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
86 prefix_ unsigned senf::TapSocketProtocol::available()
89 if (! body().readable())
92 if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
93 SENF_THROW_SYSTEM_EXCEPTION("");
98 prefix_ bool senf::TapSocketProtocol::eof()
104 prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
110 prefix_ std::string senf::TapSocketProtocol::ifaceName()
113 char buf[IF_NAMESIZE];
114 if_indextoname(ifaceIndex_, buf);
115 return std::string(buf);
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 //#include "TunTapSocketHandle.mpp"
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