From 063b81e66f9c020f3e6bd4a21074e9b8a1a47464 Mon Sep 17 00:00:00 2001
From: dw6 <dw6@wiback.org>
Date: Tue, 25 Nov 2008 15:41:26 +0000
Subject: [PATCH] completing implementation, adding missing
 interfaceName(std::string)

---
 Socket/NetdeviceController.cc | 24 +++++++++++++++++++++++-
 Socket/NetdeviceController.hh | 10 +++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/Socket/NetdeviceController.cc b/Socket/NetdeviceController.cc
index 37c255f6..7ce1f953 100644
--- a/Socket/NetdeviceController.cc
+++ b/Socket/NetdeviceController.cc
@@ -30,6 +30,7 @@
 // Custom includes
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <net/if.h>
 #include "../Utils/Exception.hh"
 
 #define prefix_
@@ -59,6 +60,22 @@ prefix_ std::string senf::NetdeviceController::interfaceName()
     return std::string( ifr.ifr_name);
 }
 
+prefix_ void senf::NetdeviceController::interfaceName(const std::string & newname)
+{
+    if (sizeof(newname) <= IFNAMSIZ) {
+        struct ifreq ifr;
+        ifrName(ifr);
+        strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
+        try {
+            doIoctl(ifr, SIOCSIFNAME);
+        } catch (senf::SystemException e) {
+            e << "Could not change the interface name. Is the interface really down?";
+            throw ;
+        }
+    }
+    return;
+}
+
 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
     const
 {
@@ -73,7 +90,12 @@ prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAdd
     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);
+    try {
+        doIoctl(ifr, SIOCSIFHWADDR);
+    } catch (senf::SystemException e) {
+        e << "Could not change the interface MAC address. Is the interface really down?";
+        throw ;
+    }
 }
 
 prefix_ int senf::NetdeviceController::mtu()
diff --git a/Socket/NetdeviceController.hh b/Socket/NetdeviceController.hh
index babe980f..5f809681 100644
--- a/Socket/NetdeviceController.hh
+++ b/Socket/NetdeviceController.hh
@@ -61,13 +61,17 @@ namespace senf {
                                         ///< return hardware address
         void hardwareAddress(const MACAddress &newAddress);
                                         ///< set hardware address
-                                        /**< Note, that setting the hardware address is a privileged operation. */
+                                        /**< Changes the hardware address of the interface.
+                                             Note, that setting the hardware address is a privileged operation. It is only allowed when the interface
+                                             is not up. If the interface is up, this call will cause an SystemException to be thrown.*/
         std::string interfaceName() const;
                                         ///< return interface name
         void interfaceName(const std::string &newName);
                                         ///< set interface name
-                                        /**< Changes the name of the interface. Note, that this is a
-                                             privileged operation. It is only allowed when the interface is not up. */
+                                        /**< Changes the name of the interface.
+                                             Note, that setting the name is a privileged operation. It is only allowed when the interface
+                                             is not up. If the interface is up, this call will cause an SystemException to be thrown.*/
+
         int mtu() const;                ///< return the Maximum Transmission Unit
         void mtu(int new_mtu);          ///< set the Maximum Transmission Unit
                                         /**< Set the MTU (Maximum Transfer Unit) of the device.
-- 
GitLab