X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FNetdeviceController.cc;h=345679b382bf6974cf6e3b32dc9ce109c3ea3dad;hb=f2f5d59e83863f3b513950173baee1b6da2aee3c;hp=b93998c044ff21daa3bee505d468bfe45d95be0c;hpb=ab05c3d687d911b4d204aa20bf900dc23d174545;p=senf.git diff --git a/Socket/NetdeviceController.cc b/Socket/NetdeviceController.cc index b93998c..345679b 100644 --- a/Socket/NetdeviceController.cc +++ b/Socket/NetdeviceController.cc @@ -30,6 +30,7 @@ // Custom includes #include #include +#include #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 { @@ -68,6 +85,19 @@ 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); + 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() const { @@ -78,7 +108,6 @@ prefix_ int senf::NetdeviceController::mtu() } prefix_ void senf::NetdeviceController::mtu(int new_mtu) - const { struct ifreq ifr; ifrName( ifr); @@ -86,6 +115,27 @@ prefix_ void senf::NetdeviceController::mtu(int new_mtu) doIoctl( ifr, SIOCSIFMTU); } +prefix_ bool senf::NetdeviceController::promisc() + const +{ + struct ifreq ifr; + ifrName( ifr); + doIoctl( ifr, SIOCGIFFLAGS); + return ifr.ifr_flags & IFF_PROMISC; +} + +prefix_ void senf::NetdeviceController::promisc(bool mode) +{ + struct ifreq ifr; + ifrName( ifr); + doIoctl( ifr, SIOCGIFFLAGS); + if (mode) + ifr.ifr_flags |= IFF_PROMISC; + else + ifr.ifr_flags &= ~IFF_PROMISC; + doIoctl( ifr, SIOCSIFFLAGS); +} + prefix_ int senf::NetdeviceController::interfaceIndex() const { @@ -101,7 +151,7 @@ prefix_ void senf::NetdeviceController::openSocket() { sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0); if ( sockfd_ < 0) - throw SystemException(); + SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController."); } prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr) @@ -110,22 +160,22 @@ prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr) ::memset( &ifr, 0, sizeof(ifr)); ifr.ifr_ifindex = ifindex_; if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 ) - throw SystemException(); + SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController") + << " could not discover the name of the interface with index " << ifindex_ << "."; } - prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request) const { if ( ::ioctl( sockfd_, request, &ifr ) < 0 ) - throw SystemException(); + SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed."); } ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "NetdeviceController.mpp" - + // Local Variables: // mode: c++ // fill-column: 100