From: jkaeber Date: Fri, 7 Mar 2008 14:19:56 +0000 (+0000) Subject: Implemented void hardwareAddress(const MACAddress &newAddress) X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=6f838e26e6cfee2f1b429e3ddd096e3a0d80d0a5;p=senf.git Implemented void hardwareAddress(const MACAddress &newAddress) git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@735 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Socket/NetdeviceController.cc b/Socket/NetdeviceController.cc index 7f40c07..23ea64a 100644 --- a/Socket/NetdeviceController.cc +++ b/Socket/NetdeviceController.cc @@ -68,6 +68,14 @@ prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress() return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data); } +prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) { + struct ifreq ifr; + ifrName( ifr); + ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ??? + std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data); + doIoctl( ifr, SIOCSIFHWADDR); +} + prefix_ int senf::NetdeviceController::mtu() const { diff --git a/Socket/NetdeviceController.hh b/Socket/NetdeviceController.hh index dbc5ce6..cabf6a4 100644 --- a/Socket/NetdeviceController.hh +++ b/Socket/NetdeviceController.hh @@ -50,12 +50,17 @@ namespace senf { NetdeviceController(int interface_index); virtual ~NetdeviceController(); - MACAddress hardwareAddress() const; - std::string interfaceName() const; - int interfaceIndex() const; ///< return the interface index of the interface - int mtu() const; - void mtu(int new_mtu) const; + int interfaceIndex() const; ///< return the interface index + MACAddress hardwareAddress() const; ///< return hardware address + void hardwareAddress(const MACAddress &newAddress); ///< set hardware address + + std::string interfaceName() const; ///< return interface name + void interfaceName(const std::string &newName) const; ///< set interface name + + int mtu() const; ///< return the Maximum Transmission Unit + void mtu(int new_mtu) const; //< set the Maximum Transmission Unit + private: void openSocket(); void doIoctl(ifreq& ifr, int request) const; diff --git a/Socket/NetdeviceController.test.cc b/Socket/NetdeviceController.test.cc index dafed94..9992830 100644 --- a/Socket/NetdeviceController.test.cc +++ b/Socket/NetdeviceController.test.cc @@ -27,6 +27,7 @@ // Custom includes #include "NetdeviceController.hh" +#include "Protocols/Raw/MACAddress.hh" #include "../Utils/auto_unit_test.hh" #include @@ -34,10 +35,30 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -BOOST_AUTO_UNIT_TEST(NetdeviceController) -{ -// senf::NetdeviceController ctrl ("eth0"); -// std::cout << ctrl.hardwareAddress() << "\n"; +BOOST_AUTO_UNIT_TEST(NetdeviceController) { + + senf::NetdeviceController ctrl ("wlan0"); + std::cout << "name: " << ctrl.interfaceName() << "\n"; + + senf::MACAddress oldAddr(ctrl.hardwareAddress()); + int oldMTU = ctrl.mtu(); + + std::cout << "hw addr: " << oldAddr << "\n"; + std::cout << "mtu: " << oldMTU << "\n"; + + if (getuid() != 0) { + BOOST_WARN_MESSAGE(false, "Cannot run some tests of senf::NetdeviceController as non-root user"); + return; + } + + ctrl.mtu(oldMTU - 16); + std::cout << "new mtu: " << ctrl.mtu() << "\n"; + ctrl.mtu(oldMTU); + + senf::MACAddress newAddr(senf::MACAddress::from_string("00:18:de:2e:ec:00")); + ctrl.hardwareAddress(newAddr); + std::cout << "new hw addr: " << ctrl.hardwareAddress() << "\n"; + ctrl.hardwareAddress(oldAddr); } ///////////////////////////////cc.e////////////////////////////////////////