From: tho Date: Fri, 14 Dec 2007 15:28:17 +0000 (+0000) Subject: first working netdeviceController X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8d2a1b34ab80d96c9592d55f56effad26ef4f930;hp=ea2f773d1bf79ff787f2c68766ecd154bd9f3631;p=senf.git first working netdeviceController git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@563 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Socket/NetdeviceController.cc b/Socket/NetdeviceController.cc index 6cc7d6b..c51302b 100644 --- a/Socket/NetdeviceController.cc +++ b/Socket/NetdeviceController.cc @@ -1,4 +1,4 @@ -// $Id: BufferingPolicy.cc 533 2007-11-23 17:34:30Z g0dil $ +// $Id$ // // Copyright (C) 2007 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) @@ -21,7 +21,7 @@ // 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" @@ -31,37 +31,88 @@ #include #include #include -#include #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_ diff --git a/Socket/NetdeviceController.hh b/Socket/NetdeviceController.hh index 50f878f..bf224ef 100644 --- a/Socket/NetdeviceController.hh +++ b/Socket/NetdeviceController.hh @@ -1,4 +1,4 @@ -// $Id: AddressingPolicy.hh 296 2007-07-10 20:39:34Z g0dil $ +// $Id$ // // Copyright (C) 2007 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) @@ -29,8 +29,10 @@ // Custom includes #include +#include #include "Protocols/Raw/MACAddress.hh" + //#include "NetdeviceController.mpp" ///////////////////////////////hh.p//////////////////////////////////////// @@ -43,15 +45,22 @@ namespace senf { 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_; }; } diff --git a/Socket/NetdeviceController.test.cc b/Socket/NetdeviceController.test.cc new file mode 100644 index 0000000..96a1125 --- /dev/null +++ b/Socket/NetdeviceController.test.cc @@ -0,0 +1,54 @@ +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Thorsten Horstmann +// +// 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 + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(NetdeviceController) +{ +// senf::NetdeviceController ctrl ("eth0"); +} + +///////////////////////////////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: