TunTapSocketHandle.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
19 #include "TunTapSocketHandle.hh"
20 //#include "TunTapSocketHandle.ih"
21 
22 // Custom includes
23 #include <sys/ioctl.h>
24 #include <fcntl.h>
25 #include <net/if.h>
26 #include <linux/if_tun.h>
27 #include <errno.h>
28 
29 //#include "TunTapSocketHandle.mpp"
30 #define prefix_
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32 
34  const
35 {
36  init_client(std::string());
37 }
38 
39 prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
40  const
41 {
42  int f;
43  if ( (f = ::open("/dev/net/tun", O_RDWR | O_CLOEXEC)) < 0 )
44  SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
45  struct ifreq ifr;
46  ::memset( &ifr, 0, sizeof(ifr));
47  ifr.ifr_flags = IFF_TAP;
48  if (NO_PI)
49  ifr.ifr_flags |= IFF_NO_PI;
50  interface_name.copy( ifr.ifr_name, IFNAMSIZ);
51  if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 ) {
52  ::close(f);
53  SENF_THROW_SYSTEM_EXCEPTION( "Could not create tap device: ") << ifr.ifr_name << ".";
54  }
55  ifaceIndex_ = if_nametoindex(ifr.ifr_name);
56  if (ifaceIndex_ == 0)
57  SENF_THROW_SYSTEM_EXCEPTION( "if_nametoindex(): failed to access new tap device '" )
58  << ifr.ifr_name << "'.";
59  fd(f);
60 }
61 
63  const
64 {
65  if (!fh().readable())
66  return 0;
67  ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
68  if (l < 0)
69  //SENF_THROW_SYSTEM_EXCEPTION("");
70  return 1588;
71  return l;
72 }
73 
74 /*
75 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
76 
77 prefix_ unsigned senf::TapSocketProtocol::available()
78  const
79 {
80  if (! body().readable())
81  return 0;
82  int n;
83  if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
84  SENF_THROW_SYSTEM_EXCEPTION("");
85  return n;
86 }
87 */
88 
90  const
91 {
92  return false;
93 }
94 
96  const
97 {
98  return ifaceIndex_;
99 }
100 
102  const
103 {
104  char buf[IF_NAMESIZE];
105  if_indextoname(ifaceIndex_, buf);
106  return std::string(buf);
107 }
108 
109 //-/////////////////////////////////////////////////////////////////////////////////////////////////
110 #undef prefix_
111 //#include "TunTapSocketHandle.mpp"
112 
113 
114 // Local Variables:
115 // mode: c++
116 // fill-column: 100
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // comment-column: 40
122 // End:
bool eof() const
returns always false.
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
int fd() const
Get file descriptor.
unsigned int ifaceIndex() const
returns the index of the correspondent tap interface
void init_client() const
Open tap socket and create new tap interface.
FileHandle fh() const
Get a FileHandle for this instance.
unsigned available() const
Return (maximum) number of bytes available for reading without < blocking.
virtual void close()
Close socket.
PacketSocketProtocol and PacketSocketHandle public header.
std::string ifaceName() const
returns the actual name of the correspondent tap interface
#define prefix_