Skip to content
Snippets Groups Projects
Commit e753af16 authored by tho's avatar tho
Browse files

first version for tap device support.

parent 72b66698
No related branches found
No related tags found
No related merge requests found
// $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"
// 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:
// $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
// 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:
// $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_
// 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:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment