4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
28 #include "TunTapSocketHandle.hh"
29 //#include "TunTapSocketHandle.ih"
32 #include <sys/ioctl.h>
35 #include <linux/if_tun.h>
38 //#include "TunTapSocketHandle.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
42 prefix_ std::string senf::TapSocketProtocol::init_client()
45 return init_client(std::string());
48 prefix_ std::string senf::TapSocketProtocol::init_client(std::string const & interface_name, bool const NO_PI)
52 if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
53 SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
55 ::memset( &ifr, 0, sizeof(ifr));
56 ifr.ifr_flags = IFF_TAP;
58 ifr.ifr_flags |= IFF_NO_PI;
59 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
60 if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 )
61 SENF_THROW_SYSTEM_EXCEPTION( "Could not create tap device: ") << ifr.ifr_name << ".";
62 ifaceIndex_ = if_nametoindex(ifr.ifr_name);
67 prefix_ unsigned senf::TapSocketProtocol::available()
72 ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
74 //SENF_THROW_SYSTEM_EXCEPTION("");
80 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
82 prefix_ unsigned senf::TapSocketProtocol::available()
85 if (! body().readable())
88 if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
89 SENF_THROW_SYSTEM_EXCEPTION("");
94 prefix_ bool senf::TapSocketProtocol::eof()
100 prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
106 prefix_ std::string senf::TapSocketProtocol::ifaceName()
109 char buf[IF_NAMESIZE];
110 if_indextoname(ifaceIndex_, buf);
111 return std::string(buf);
114 ///////////////////////////////cc.e////////////////////////////////////////
116 //#include "TunTapSocketHandle.mpp"
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // comment-column: 40