///////////////////////////////cc.p////////////////////////////////////////
prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
+ : sockfd_ (sockfd())
{
- openSocket();
struct ifreq ifr;
::memset( &ifr, 0, sizeof(ifr));
interface_name.copy( ifr.ifr_name, IFNAMSIZ);
}
prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
+ : sockfd_ (sockfd())
{
- openSocket();
ifindex_ = interface_index;
}
prefix_ senf::NetdeviceController::~NetdeviceController()
{
- close( sockfd_);
-}
-
-prefix_ void senf::NetdeviceController::openSocket()
-{
- sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
- if ( sockfd_ < 0)
- SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
+ close( sockfd_->fd);
}
prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
{
::memset( &ifr, 0, sizeof(ifr));
ifr.ifr_ifindex = ifindex_;
- if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
+ if ( ::ioctl( sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
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 )
+ if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )
SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
}
+///////////////////////////////////////////////////////////////////////////
+// senf::NetdeviceController::SockFd
+
+prefix_ senf::NetdeviceController::SockFd::SockFd()
+ : fd (::socket(PF_INET, SOCK_DGRAM, 0))
+{
+ if ( fd < 0)
+ SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
+}
+
+prefix_ senf::NetdeviceController::SockFd::~SockFd()
+{
+ ::close(fd);
+}
+
+prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
+{
+ static boost::weak_ptr<SockFd> sockfd;
+
+ if (sockfd.expired()) {
+ SockFd::ptr newsockfd (new SockFd());
+ sockfd = newsockfd;
+ return newsockfd;
+ }
+ return sockfd.lock();
+
+}
+
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "NetdeviceController.mpp"
// Custom includes
#include <string>
+#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
#include "Protocols/Raw/MACAddress.hh"
class NetdeviceController
{
public:
- NetdeviceController(std::string const & interface_name);
+ explicit NetdeviceController(std::string const & interface_name);
///< Construct a new controller for the given interface name.
- NetdeviceController(int interface_index);
+ explicit NetdeviceController(int interface_index);
///< Construct a new controller for the given interface index.
virtual ~NetdeviceController();
void up(); ///< ifconfig up interface
void down(); ///< ifconfig down interface
+ struct SockFd {
+ typedef boost::shared_ptr<SockFd> ptr;
+ int fd;
+ SockFd();
+ ~SockFd();
+ };
+
+ static SockFd::ptr sockfd();
+
private:
- void openSocket();
void doIoctl(ifreq& ifr, int request) const;
void ifrName(ifreq& ifr) const;
- int sockfd_;
+ SockFd::ptr sockfd_;
int ifindex_;
};