TunTapSocketHandle.cc

Go to the documentation of this file.
00001 // $Id: TunTapSocketHandle.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Thorsten Horstmann <tho@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00028 #include "TunTapSocketHandle.hh"
00029 //#include "TunTapSocketHandle.ih"
00030 
00031 // Custom includes
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 //#include "TunTapSocketHandle.mpp"
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         //SENF_THROW_SYSTEM_EXCEPTION("");
00074         return 1588;
00075     return l;
00076 }
00077 
00078 /*
00079 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
00080 
00081 prefix_ unsigned senf::TapSocketProtocol::available()
00082   const
00083 {
00084   if (! body().readable())
00085       return 0;
00086   int n;
00087   if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
00088       SENF_THROW_SYSTEM_EXCEPTION("");
00089   return n;
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 //#include "TunTapSocketHandle.mpp"
00116 
00117 
00118 // Local Variables:
00119 // mode: c++
00120 // fill-column: 100
00121 // c-file-style: "senf"
00122 // indent-tabs-mode: nil
00123 // ispell-local-dictionary: "american"
00124 // compile-command: "scons -u test"
00125 // comment-column: 40
00126 // End: