Implemented void hardwareAddress(const MACAddress &newAddress)
jkaeber [Fri, 7 Mar 2008 14:19:56 +0000 (14:19 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@735 270642c3-0616-0410-b53a-bc976706d245

Socket/NetdeviceController.cc
Socket/NetdeviceController.hh
Socket/NetdeviceController.test.cc

index 7f40c07..23ea64a 100644 (file)
@@ -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
 {
index dbc5ce6..cabf6a4 100644 (file)
@@ -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;
index dafed94..9992830 100644 (file)
@@ -27,6 +27,7 @@
 
 // Custom includes
 #include "NetdeviceController.hh"
+#include "Protocols/Raw/MACAddress.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");
-//    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////////////////////////////////////////