-// $Id: BufferingPolicy.cc 533 2007-11-23 17:34:30Z g0dil $
+// $Id$
//
// Copyright (C) 2007
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/** \file
- \brief BufferingPolicy non-inline non-template implementation
+ \brief NetdeviceController non-inline non-template implementation
*/
#include "NetdeviceController.hh"
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
-#include <netinet/ether.h>
#include "../Utils/Exception.hh"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
-prefix_ senf::NetdeviceController::NetdeviceController(std::string const interface_name)
+prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
{
- sockfd_ = ::socket(PF_PACKET, SOCK_DGRAM, 0);
- if (sockfd_ < 0)
- throwErrno();
- interfacename_ = interface_name;
+ openSocket();
+ struct ifreq ifr;
+ ::memset( &ifr, 0, sizeof(ifr));
+ interface_name.copy( ifr.ifr_name, IFNAMSIZ);
+ doIoctl( ifr, SIOCGIFINDEX);
+ ifindex_ = ifr.ifr_ifindex;
+}
+
+prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
+{
+ openSocket();
+ ifindex_ = interface_index;
+}
+
+prefix_ std::string senf::NetdeviceController::interfaceName()
+{
+ struct ifreq ifr;
+ set_ifr_name( ifr);
+ return std::string( ifr.ifr_name);
}
prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
{
struct ifreq ifr;
- ::memset( &ifr, 0, sizeof(ifr));
- interfacename_.copy( ifr.ifr_name, IFNAMSIZ);
- if ( ::ioctl( sockfd_, SIOCGIFHWADDR , &ifr ) < 0 )
- throwErrno();
- return senf::MACAddress::from_string(
- ether_ntoa( (struct ether_addr*) ifr.ifr_hwaddr.sa_data ) );
+ doIoctl( ifr, SIOCGIFHWADDR);
+ return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
+}
+
+prefix_ int senf::NetdeviceController::mtu()
+{
+ struct ifreq ifr;
+ set_ifr_name(ifr);
+ doIoctl( ifr, SIOCGIFMTU);
+ return ifr.ifr_mtu;
+}
+
+prefix_ void senf::NetdeviceController::mtu(int new_mtu)
+{
+ struct ifreq ifr;
+ set_ifr_name( ifr);
+ ifr.ifr_mtu = new_mtu;
+ doIoctl( ifr, SIOCSIFMTU);
+}
+
+prefix_ int senf::NetdeviceController::interfaceIndex()
+{
+ return ifindex_;
}
prefix_ senf::NetdeviceController::~NetdeviceController()
{
- close( sockfd_ );
+ close( sockfd_);
}
+prefix_ void senf::NetdeviceController::openSocket()
+{
+ sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
+ if ( sockfd_ < 0)
+ throwErrno();
+}
+prefix_ void senf::NetdeviceController::set_ifr_name(ifreq& ifr)
+{
+ ::memset( &ifr, 0, sizeof(ifr));
+ ifr.ifr_ifindex = ifindex_;
+ if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
+ throwErrno();
+}
+
+
+prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
+{
+ if ( ::ioctl( sockfd_, request, &ifr ) < 0 )
+ throwErrno();
+}
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
-// $Id: AddressingPolicy.hh 296 2007-07-10 20:39:34Z g0dil $
+// $Id$
//
// Copyright (C) 2007
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Custom includes
#include <string>
+#include <net/if.h>
#include "Protocols/Raw/MACAddress.hh"
+
//#include "NetdeviceController.mpp"
///////////////////////////////hh.p////////////////////////////////////////
class NetdeviceController
{
public:
- NetdeviceController(std::string const interface_name);
-
+ NetdeviceController(std::string const & interface_name);
+ NetdeviceController(int interface_index);
virtual ~NetdeviceController();
MACAddress hardwareAddress();
+ std::string interfaceName();
+ int interfaceIndex();
+ int mtu();
+ void mtu(int new_mtu);
private:
+ void openSocket();
+ void doIoctl(ifreq& ifr, int request);
+ void set_ifr_name(ifreq& ifr);
int sockfd_;
- std::string interfacename_;
+ int ifindex_;
};
}
--- /dev/null
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+// Thorsten Horstmann <thorsten.horstmann@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 "NetdeviceController.test.hh"
+//#include "NetdeviceController.test.ih"
+
+// Custom includes
+#include "NetdeviceController.hh"
+
+#include "../Utils/auto_unit_test.hh"
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+BOOST_AUTO_UNIT_TEST(NetdeviceController)
+{
+// senf::NetdeviceController ctrl ("eth0");
+}
+
+///////////////////////////////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: