8dfe2f1630e51773ea9689ff74ce506e73ea553f
[senf.git] / senf / Socket / Protocols / Raw / TunTapSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.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
25
26  */
27
28 #include "TunTapSocketHandle.hh"
29 //#include "TunTapSocketHandle.ih"
30
31 // Custom includes
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <net/if.h>
35 #include <linux/if_tun.h>
36 #include <errno.h>
37
38 //#include "TunTapSocketHandle.mpp"
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 prefix_ void senf::TapSocketProtocol::init_client()
43     const
44 {
45     init_client(std::string());
46 }
47
48 prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
49     const
50 {
51     int f;
52     if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
53         SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
54     struct ifreq ifr;
55     ::memset( &ifr, 0, sizeof(ifr));
56     ifr.ifr_flags = IFF_TAP;
57     if (NO_PI)
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);
63     fd(f);
64 }
65
66 prefix_ unsigned senf::TapSocketProtocol::available()
67   const
68 {
69     if (!fh().readable())
70         return 0;
71     ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
72     if (l < 0)
73         //SENF_THROW_SYSTEM_EXCEPTION("");
74         return 1588;
75     return l;
76 }
77
78 /*
79 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
80
81 prefix_ unsigned senf::TapSocketProtocol::available()
82   const
83 {
84   if (! body().readable())
85       return 0;
86   int n;
87   if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
88       SENF_THROW_SYSTEM_EXCEPTION("");
89   return n;
90 }
91 */
92
93 prefix_ bool senf::TapSocketProtocol::eof()
94     const
95 {
96     return false;
97 }
98
99 prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
100     const
101 {
102     return ifaceIndex_;
103 }
104
105 prefix_ std::string senf::TapSocketProtocol::ifaceName()
106     const
107 {
108     char buf[IF_NAMESIZE];
109     if_indextoname(ifaceIndex_, buf);
110     return std::string(buf);
111 }
112
113 ///////////////////////////////cc.e////////////////////////////////////////
114 #undef prefix_
115 //#include "TunTapSocketHandle.mpp"
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // comment-column: 40
126 // End: