added some documentation
[senf.git] / Socket / Protocols / Raw / TunTapSocketHandle.cc
index c2ab043..f2be645 100644 (file)
@@ -1,9 +1,9 @@
-// $Id: PacketSocketHandle.cc 358 2007-07-27 12:14:51Z g0dil $
+// $Id$
 //
-// Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Copyright (C) 2008
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Thorsten Horstmann <tho@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -21,8 +21,8 @@
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 /** \file
-    \brief 
-    
+    \brief
+
  */
 
 #include "TunTapSocketHandle.hh"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-prefix_ void senf::TapProtocol::init_client() 
+prefix_ void senf::TapSocketProtocol::init_client()
     const
 {
     init_client(std::string());
 }
 
-prefix_ void senf::TapProtocol::init_client(std::string const & interface_name, bool const NO_PI) 
+prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_name, bool NO_PI)
     const
 {
-    int fd;
-    if ( (fd = ::open("/dev/net/tun", O_RDWR)) < 0 )
-        throwErrno();
+    int f;
+    if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
+        SENF_THROW_SYSTEM_EXCEPTION("Could not open tap control device: /dev/net/tun.");
     struct ifreq ifr;
     ::memset( &ifr, 0, sizeof(ifr));
     ifr.ifr_flags = IFF_TAP;
     if (NO_PI)
         ifr.ifr_flags |= IFF_NO_PI;
     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
-    if (::ioctl(fd, TUNSETIFF, (void *) &ifr) < 0 )
-        throwErrno();
-    body().fd(fd);
+    if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 )
+        SENF_THROW_SYSTEM_EXCEPTION( "Could not create tap device: ") << ifr.ifr_name << ".";
+    ifaceIndex_ = if_nametoindex(ifr.ifr_name);
+    fd(f);
 }
 
-prefix_ std::auto_ptr<senf::SocketProtocol> senf::TapProtocol::clone()
+prefix_ unsigned senf::TapSocketProtocol::available()
+  const
+{
+    if (!fh().readable())
+        return 0;
+    ssize_t l = ::recv(fd(), 0, 0, MSG_PEEK | MSG_TRUNC);
+    if (l < 0)
+        //SENF_THROW_SYSTEM_EXCEPTION("");
+        return 1588;
+    return l;
+}
+
+/*
+#include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
+
+prefix_ unsigned senf::TapSocketProtocol::available()
+  const
+{
+  if (! body().readable())
+      return 0;
+  int n;
+  if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
+      SENF_THROW_SYSTEM_EXCEPTION("");
+  return n;
+}
+*/
+
+prefix_ bool senf::TapSocketProtocol::eof()
     const
 {
-    return std::auto_ptr<SocketProtocol>(new TapProtocol());
+    return false;
 }
 
-prefix_ unsigned senf::TapProtocol::available()
+prefix_ unsigned int senf::TapSocketProtocol::ifaceIndex()
     const
 {
-    if (! body().readable())
-        return 0;
-    ssize_t l = ::recv(body().fd(),0,0,MSG_PEEK | MSG_TRUNC);
-    if (l < 0)
-        throwErrno();
-    return l;
+    return ifaceIndex_;
 }
 
-prefix_ bool senf::TapProtocol::eof()
+prefix_ std::string senf::TapSocketProtocol::ifaceName()
     const
 {
-    return false;
+    char buf[IF_NAMESIZE];
+    if_indextoname(ifaceIndex_, buf);
+    return std::string(buf);
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "TunTapSocketHandle.mpp"
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100