TunTapSocketHandle.cc
Go to the documentation of this file.
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "TunTapSocketHandle.hh"
00029
00030
00031
00032 #include <sys/ioctl.h>
00033 #include <fcntl.h>
00034 #include <net/if.h>
00035 #include <linux/if_tun.h>
00036 #include <errno.h>
00037
00038
00039 #define prefix_
00040
00041
00042 prefix_ void senf::TapSocketProtocol::init_client()
00043 const
00044 {
00045 init_client(std::string());
00046 }
00047
00048 prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
00049 const
00050 {
00051 int f;
00052 if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
00053 SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
00054 struct ifreq ifr;
00055 ::memset( &ifr, 0, sizeof(ifr));
00056 ifr.ifr_flags = IFF_TAP;
00057 if (NO_PI)
00058 ifr.ifr_flags |= IFF_NO_PI;
00059 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
00060 if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 )
00061 SENF_THROW_SYSTEM_EXCEPTION( "Could not create tap device: ") << ifr.ifr_name << ".";
00062 ifaceIndex_ = if_nametoindex(ifr.ifr_name);
00063 fd(f);
00064 }
00065
00066 prefix_ unsigned senf::TapSocketProtocol::available()
00067 const
00068 {
00069 if (!fh().readable())
00070 return 0;
00071 ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
00072 if (l < 0)
00073
00074 return 1588;
00075 return l;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 prefix_ bool senf::TapSocketProtocol::eof()
00094 const
00095 {
00096 return false;
00097 }
00098
00099 prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
00100 const
00101 {
00102 return ifaceIndex_;
00103 }
00104
00105 prefix_ std::string senf::TapSocketProtocol::ifaceName()
00106 const
00107 {
00108 char buf[IF_NAMESIZE];
00109 if_indextoname(ifaceIndex_, buf);
00110 return std::string(buf);
00111 }
00112
00113
00114 #undef prefix_
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126