#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
+#include <boost/weak_ptr.hpp>
#include <senf/Utils/Exception.hh>
+#include <senf/Socket/Protocols/Raw/MACAddress.hh>
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
+#define doIoctl(ifr, request, errorMsg) \
+ if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 ) \
+ SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
+
+
prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
: sockfd_ (sockfd())
{
struct ifreq ifr;
::memset( &ifr, 0, sizeof(ifr));
interface_name.copy( ifr.ifr_name, IFNAMSIZ);
- doIoctl( ifr, SIOCGIFINDEX);
+ doIoctl(ifr, SIOCGIFINDEX, "Could not discover the index of interface \"" + interface_name + "\"");
ifindex_ = ifr.ifr_ifindex;
}
return std::string( ifr.ifr_name);
}
-prefix_ void senf::NetdeviceController::interfaceName(const std::string & newname)
+prefix_ void senf::NetdeviceController::interfaceName(std::string const & 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 ;
- }
+ doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
}
return;
}
{
struct ifreq ifr;
ifrName( ifr);
- doIoctl( ifr, SIOCGIFHWADDR);
- return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
+ doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
+ return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
}
-prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) {
+prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & 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 ;
- }
+ doIoctl(ifr, SIOCSIFHWADDR, "Could not change the interface MAC address. Is the interface really down?");
}
prefix_ int senf::NetdeviceController::mtu()
{
struct ifreq ifr;
ifrName( ifr);
- doIoctl( ifr, SIOCGIFMTU);
+ doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
return ifr.ifr_mtu;
}
struct ifreq ifr;
ifrName( ifr);
ifr.ifr_mtu = new_mtu;
- doIoctl( ifr, SIOCSIFMTU);
+ doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
}
prefix_ int senf::NetdeviceController::txqueuelen()
{
struct ifreq ifr;
ifrName( ifr);
- doIoctl( ifr, SIOCGIFTXQLEN);
+ doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
return ifr.ifr_qlen;
}
struct ifreq ifr;
ifrName( ifr);
ifr.ifr_qlen = new_txqueuelen;
- doIoctl( ifr, SIOCSIFTXQLEN);
+ doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
}
{
struct ifreq ifr;
ifrName( ifr);
- doIoctl( ifr, SIOCGIFFLAGS);
+ doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
return ifr.ifr_flags & IFF_PROMISC;
}
{
struct ifreq ifr;
ifrName( ifr);
- doIoctl( ifr, SIOCGIFFLAGS);
+ doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
if (mode)
ifr.ifr_flags |= IFF_PROMISC;
else
ifr.ifr_flags &= ~IFF_PROMISC;
- doIoctl( ifr, SIOCSIFFLAGS);
+ doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
}
prefix_ bool senf::NetdeviceController::isUp()
{
struct ifreq ifr;
ifrName(ifr);
- doIoctl(ifr, SIOCGIFFLAGS);
+ doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
return ifr.ifr_flags & IFF_UP;
}
{
struct ifreq ifr;
ifrName(ifr);
- doIoctl(ifr, SIOCGIFFLAGS);
+ doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
ifr.ifr_flags |= IFF_UP;
- doIoctl(ifr, SIOCSIFFLAGS);
+ doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
}
prefix_ void senf::NetdeviceController::down()
{
struct ifreq ifr;
ifrName(ifr);
- doIoctl(ifr, SIOCGIFFLAGS);
+ doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
ifr.ifr_flags &= ~IFF_UP;
- doIoctl(ifr, SIOCSIFFLAGS);
+ doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
}
prefix_ int senf::NetdeviceController::interfaceIndex()
return ifindex_;
}
-prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
+prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
const
{
::memset( &ifr, 0, sizeof(ifr));
<< " could not discover the name of the interface with index " << ifindex_ << ".";
}
-prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
- const
-{
- if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )
- SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
-}
+#undef doIoctl
///////////////////////////////////////////////////////////////////////////
// senf::NetdeviceController::SockFd
// Custom includes
#include <string>
#include <boost/shared_ptr.hpp>
-#include <boost/weak_ptr.hpp>
-#include <senf/Socket/Protocols/Raw/MACAddress.hh>
-
//#include "NetdeviceController.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
+ class MACAddress;
+
+
/** \brief Netdevice Controller
This controller provides an interface which can be used to configure network
int interfaceIndex() const; ///< return the interface index
MACAddress hardwareAddress() const;
///< return hardware address
- void hardwareAddress(const MACAddress &newAddress);
+ void hardwareAddress(MACAddress const & newAddress);
///< set hardware address
/**< Changes the hardware address of the interface.
Note, that setting the hardware address is a privileged
SystemException to be thrown. */
std::string interfaceName() const;
///< return interface name
- void interfaceName(const std::string &newName);
+ void interfaceName(std::string const & newName);
///< set interface name
/**< Changes the name of the interface.
Note, that setting the name is a privileged operation.
static SockFd::ptr sockfd();
private:
- void doIoctl(ifreq& ifr, int request) const;
- void ifrName(ifreq& ifr) const;
+ void ifrName(ifreq & ifr) const;
SockFd::ptr sockfd_;
int ifindex_;
};