first version for tap device support.
tho [Fri, 23 Nov 2007 16:31:02 +0000 (16:31 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@532 270642c3-0616-0410-b53a-bc976706d245

Socket/Protocols/Raw/TunTapSocketHandle.cc [new file with mode: 0644]
Socket/Protocols/Raw/TunTapSocketHandle.hh [new file with mode: 0644]
Socket/Protocols/Raw/TunTapSocketHandle.test.cc [new file with mode: 0644]

diff --git a/Socket/Protocols/Raw/TunTapSocketHandle.cc b/Socket/Protocols/Raw/TunTapSocketHandle.cc
new file mode 100644 (file)
index 0000000..cd46920
--- /dev/null
@@ -0,0 +1,101 @@
+// $Id: PacketSocketHandle.cc 358 2007-07-27 12:14:51Z g0dil $
+//
+// Copyright (C) 2006
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <stefan.bund@fokus.fraunhofer.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
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief 
+    
+ */
+
+#include "TunTapSocketHandle.hh"
+//#include "TunTapSocketHandle.ih"
+
+// Custom includes
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <linux/if_tun.h>
+#include <errno.h>
+
+//#include "TunTapSocketHandle.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+prefix_ void senf::TapProtocol::init_client() 
+    const
+{
+    init_client(std::string());
+}
+
+prefix_ void senf::TapProtocol::init_client(std::string const & interface_name, bool const NO_PI) 
+    const
+{
+    int fd;
+    if ( (fd = ::open("/dev/net/tun", O_RDWR)) < 0 )
+        throw SystemException(errno);
+    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 )
+        throw SystemException(errno);
+    body().fd(fd);
+}
+
+prefix_ std::auto_ptr<senf::SocketProtocol> senf::TapProtocol::clone()
+    const
+{
+    return std::auto_ptr<SocketProtocol>(new TapProtocol());
+}
+
+prefix_ unsigned senf::TapProtocol::available()
+    const
+{
+    if (! body().readable())
+        return 0;
+    ssize_t l = ::recv(body().fd(),0,0,MSG_PEEK | MSG_TRUNC);
+    if (l < 0)
+        throw SystemException(errno);
+    return l;
+}
+
+prefix_ bool senf::TapProtocol::eof()
+    const
+{
+    return false;
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "TunTapSocketHandle.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
diff --git a/Socket/Protocols/Raw/TunTapSocketHandle.hh b/Socket/Protocols/Raw/TunTapSocketHandle.hh
new file mode 100644 (file)
index 0000000..bbe263f
--- /dev/null
@@ -0,0 +1,134 @@
+// $Id:PacketSocketHandle.hh 218 2007-03-20 14:39:32Z tho $
+//
+// Copyright (C) 2006
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <stefan.bund@fokus.fraunhofer.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
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief PacketProtocol and PacketSocketHandle public header
+
+    \todo Implement global promisc() helper based on ioctl() interface.
+ */
+
+#ifndef HH_TunTapSocketHandle_
+#define HH_TunTapSocketHandle_ 1
+
+// Custom includes
+#include "../../../Socket/SocketPolicy.hh"
+#include "../../../Socket/SocketProtocol.hh"
+#include "../../../Socket/ProtocolClientSocketHandle.hh"
+#include "../../../Socket/FramingPolicy.hh"
+#include "../../../Socket/CommunicationPolicy.hh"
+#include "../../../Socket/ReadWritePolicy.hh"
+#include "../../../Socket/BufferingPolicy.hh"
+#include "../../../Socket/Protocols/BSDSocketProtocol.hh"
+#include "LLAddressing.hh"
+
+//#include "TunTapSocketHandle.mpp"
+//#include "TunTapSocketHandle.ih"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace senf {
+
+    /// \addtogroup concrete_protocol_group
+    /// @{
+
+    typedef MakeSocketPolicy<
+        NoAddressingPolicy,
+        DatagramFramingPolicy,
+        UnconnectedCommunicationPolicy,
+        ReadablePolicy,
+        WriteablePolicy,
+        SocketBufferingPolicy
+        >::policy Tap_Policy;        ///< Policy for TAP
+
+    /** \brief TAP
+
+        \todo document me
+
+        \par Socket Handle typedefs:
+
+
+        \par Policy Interface:
+
+
+        \par Address Type:
+
+
+        This class is utilized as the protocol class of the ProtocolClientSocketHandle via the
+        Socket Handle typedefs above.
+     */
+    class TapProtocol
+        : public ConcreteSocketProtocol<Tap_Policy>,
+          public BSDSocketProtocol,
+          public senf::pool_alloc_mixin<TapProtocol>
+    {
+    public:
+        ///\name Constructors
+        ///@{
+        void init_client() const;
+                                        ///< Create TAP socket
+                                        /**< \todo document me */
+                                        /**< \note This member is implicitly called from the
+                                             ProtocolClientSocketHandle::ProtocolClientSocketHandle()
+                                             constructor */
+        void init_client(std::string const & interface_name, bool const NO_PI=true) const;
+                                        ///< Create TAP socket
+                                        /**< \todo document me
+                                             \param[in] address remote address to connect to */
+                                        /**< \note This member is implicitly called from the
+                                             ProtocolClientSocketHandle::ProtocolClientSocketHandle()
+                                             constructor */
+        
+        ///@}
+
+        ///\name Abstract Interface Implementation
+        ///@{
+
+        std::auto_ptr<SocketProtocol> clone() const;
+        unsigned available() const;
+        bool eof() const;
+
+        ///@}
+    };
+
+    typedef ProtocolClientSocketHandle<TapProtocol> TapSocketHandle;
+                                        ///< SocketHandle of TapProtocol
+                                        /**< \related TapPrototol */
+
+    /// @}
+}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "TunTapSocketHandle.cci"
+//#include "TunTapSocketHandle.ct"
+//#include "TunTapSocketHandle.cti"
+//#include "TunTapSocketHandle.mpp"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
diff --git a/Socket/Protocols/Raw/TunTapSocketHandle.test.cc b/Socket/Protocols/Raw/TunTapSocketHandle.test.cc
new file mode 100644 (file)
index 0000000..ec5fa10
--- /dev/null
@@ -0,0 +1,69 @@
+// $Id: PacketSocketHandle.test.cc 483 2007-10-30 14:48:42Z g0dil $
+//
+// Copyright (C) 2006
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <stefan.bund@fokus.fraunhofer.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
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+// Unit tests
+
+//#include "TunTapSocketHandle.test.hh"
+//#include "TunTapSocketHandle.test.ih"
+
+#include "TunTapSocketHandle.hh"
+#include "PacketSocketHandle.hh"
+
+// Custom includes
+#include <iostream>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "../../../Utils/auto_unit_test.hh"
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+BOOST_AUTO_UNIT_TEST(tapSocketHandle)
+{
+    if (getuid() != 0) {
+        BOOST_WARN_MESSAGE(false, "Cannot test senf::TunTapSocketHandle as non-root user");
+        return;
+    }
+    
+    senf::TapSocketHandle handle ("tap_unittest");
+    int ret = system( "ifconfig tap_unittest up");
+    BOOST_CHECK_EQUAL( WEXITSTATUS(ret), 0);
+    
+    senf::PacketSocketHandle sock;
+    BOOST_CHECK_NO_THROW( sock.bind(senf::LLSocketAddress("tap_unittest")) );
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End: