22 #include <sys/socket.h> 23 #include <linux/sockios.h> 24 #include <sys/ioctl.h> 26 #include <linux/if_vlan.h> 27 #include <linux/net_tstamp.h> 28 #include <boost/weak_ptr.hpp> 29 #include <boost/filesystem/fstream.hpp> 37 #define doIoctl(ifr, request, errorMsg) \ 38 if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 ) \ 39 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg) 45 reset(interface_name);
57 ::memset( &ifr, 0,
sizeof(ifr));
58 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
59 doIoctl(ifr, SIOCGIFINDEX,
"Could not discover the index of interface \"" + interface_name +
"\"");
69 return std::string( ifr.ifr_name);
72 return "(interface not known)";
78 if (
sizeof(newname) <= IFNAMSIZ) {
81 newname.copy(ifr.ifr_newname, IFNAMSIZ);
82 doIoctl(ifr, SIOCSIFNAME,
"Could not change the interface name. Is the interface really down?");
92 doIoctl( ifr, SIOCGIFHWADDR,
"Could not discover hardwareAddress");
99 ifr.ifr_hwaddr.sa_family = 1;
100 std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
101 doIoctl(ifr, SIOCSIFHWADDR,
"Could not change the interface MAC address. Is the interface really down?");
109 doIoctl( ifr, SIOCGIFMTU,
"Could not discover mtu");
117 ifr.ifr_mtu = new_mtu;
118 doIoctl( ifr, SIOCSIFMTU,
"Could not set mtu");
126 doIoctl( ifr, SIOCGIFTXQLEN,
"Could not discover txqueuelen");
134 ifr.ifr_qlen = new_txqueuelen;
135 doIoctl( ifr, SIOCSIFTXQLEN,
"Could not set txqueuelen");
144 doIoctl( ifr, SIOCGIFFLAGS,
"Could not discover promisc mode");
145 return ifr.ifr_flags & IFF_PROMISC;
152 doIoctl( ifr, SIOCGIFFLAGS,
"Could not set promisc mode");
154 ifr.ifr_flags |= IFF_PROMISC;
156 ifr.ifr_flags &= ~IFF_PROMISC;
157 doIoctl( ifr, SIOCSIFFLAGS,
"Could not set promisc mode");
165 doIoctl(ifr, SIOCGIFFLAGS,
"Could not discover interface status");
166 return ifr.ifr_flags & IFF_UP;
173 doIoctl(ifr, SIOCGIFFLAGS,
"Could not set interface status");
174 ifr.ifr_flags |= IFF_UP;
175 doIoctl(ifr, SIOCSIFFLAGS,
"Could not set interface status");
182 doIoctl(ifr, SIOCGIFFLAGS,
"Could not set interface status");
183 ifr.ifr_flags &= ~IFF_UP;
184 doIoctl(ifr, SIOCSIFFLAGS,
"Could not set interface status");
189 vlan_ioctl_args vlan_request;
190 memset(&vlan_request, 0,
sizeof(vlan_request));
191 vlan_request.cmd = ADD_VLAN_CMD;
192 vlan_request.u.VID = vlanId;
193 strncpy(vlan_request.device1,
interfaceName().c_str(),
sizeof(vlan_request.device1)-1);
194 doIoctl (vlan_request, SIOCSIFVLAN,
"failed to add VLanId " + senf::str(vlanId) +
" to " +
interfaceName());
199 vlan_ioctl_args vlan_request;
200 memset(&vlan_request, 0,
sizeof(vlan_request));
201 vlan_request.cmd = DEL_VLAN_CMD;
202 strncpy(vlan_request.device1, (
interfaceName() +
"." + senf::str(vlanId)).c_str(),
sizeof(vlan_request.device1)-1);
203 doIoctl (vlan_request, SIOCSIFVLAN,
"failed to delete VLanId " + senf::str(vlanId) +
" from " +
interfaceName());
208 struct hwtstamp_config hwconf = { 0, txType, rxFilter};
211 ifr.ifr_data = (
char*) &hwconf;
212 doIoctl( ifr, SIOCSHWTSTAMP,
"Failed to set hw timestamp config");
224 ::memset( &ifr, 0,
sizeof(ifr));
226 if ( ::ioctl(
sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
228 <<
" could not discover the name of the interface with index " <<
ifindex_ <<
".";
238 : fd (::socket(PF_INET, SOCK_DGRAM, 0))
251 static boost::weak_ptr<SockFd>
sockfd;
264 boost::filesystem::ifstream file;
265 file.exceptions( std::ifstream::failbit | std::ifstream::badbit);
266 file.open(path, std::ios::in);
275 boost::filesystem::directory_iterator end_itr;
276 for (boost::filesystem::directory_iterator itr (
"/sys/class/net/"); itr != end_itr; ++itr) {
279 return itr->path().filename().string();
280 }
catch (std::exception &) {}
MACAddress public header.
int mtu() const
return the Maximum Transmission Unit
static SockFd::ptr sockfd()
void timestamping(int txType, int rxFilter)
configures hw timestamping for RX and/or TX
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
void addVLAN(std::uint16_t vlanId)
add a VLAN interface
void up()
ifconfig up interface
bool isUp() const
return true if interface is up
void reset(std::string const &interface_name)
reset the ifindex_ from the given iface_name (i.e. after USB Ethernet unplug/plug ...
int txqueuelen() const
return the Tx Queue Length
void ifrName(ifreq &ifr) const
NetdeviceController public header.
boost::shared_ptr< SockFd > ptr
void down()
ifconfig down interface
bool promisc() const
return true if interface is in promiscuous mode
MACAddress hardwareAddress() const
return hardware address
void delVLAN(std::uint16_t vlanId)
delete a VLAN interface
int interfaceIndex() const
return the interface index
#define doIoctl(ifr, request, errorMsg)
NetdeviceController(std::string const &interface_name)
Construct a new controller for the given interface name.
static MACAddress from_data(InputIterator i)
Construct address from raw data.
static MACAddress readMACAddressFromFile(boost::filesystem::path const &path)
std::string interfaceName() const
return interface name
static std::string macToName(MACAddress const &mac)