Kea 2.7.5
|
Represents a single network interface. More...
#include <iface_mgr.h>
Public Types | |
typedef util::Optional< asiolink::IOAddress > | Address |
Address type. | |
typedef std::list< Address > | AddressCollection |
Type that defines list of addresses. | |
using | ErrorBuffer = std::vector<std::string> |
Type definition for a list of error messages. | |
typedef std::list< SocketInfo > | SocketCollection |
Type that holds a list of socket information. | |
Public Member Functions | |
Iface (const std::string &name, unsigned int ifindex) | |
Iface constructor. | |
~Iface () | |
Destructor. | |
void | addAddress (const isc::asiolink::IOAddress &addr) |
Adds an address to an interface. | |
void | addError (std::string const &message) |
Add an error to the list of messages. | |
void | addSocket (const SocketInfo &sock) |
Adds socket descriptor to an interface. | |
void | addUnicast (const isc::asiolink::IOAddress &addr) |
Adds unicast the server should listen on. | |
void | clearErrors () |
Clears all errors. | |
void | clearUnicasts () |
Removes any unicast addresses. | |
void | closeSockets () |
Closes all open sockets on interface. | |
void | closeSockets (const uint16_t family) |
Closes all IPv4 or IPv6 sockets. | |
unsigned int | countActive4 () const |
Returns a number of activated IPv4 addresses on the interface. | |
bool | delAddress (const isc::asiolink::IOAddress &addr) |
Deletes an address from an interface. | |
bool | delSocket (uint16_t sockfd) |
Closes socket. | |
bool | getAddress4 (isc::asiolink::IOAddress &address) const |
Returns IPv4 address assigned to the interface. | |
const AddressCollection & | getAddresses () const |
Returns all addresses available on an interface. | |
ErrorBuffer const & | getErrors () const |
Get the consistent list of error messages. | |
std::string | getFullName () const |
Returns full interface name as "ifname/ifindex" string. | |
uint16_t | getHWType () const |
Returns hardware type of the interface. | |
unsigned int | getIndex () const |
Returns interface index. | |
const uint8_t * | getMac () const |
Returns pointer to MAC address. | |
size_t | getMacLen () const |
Returns MAC length. | |
std::string | getName () const |
Returns interface name. | |
std::string | getPlainMac () const |
Returns link-layer address a plain text. | |
uint8_t * | getReadBuffer () |
Returns the pointer to the buffer used for data reading. | |
size_t | getReadBufferSize () const |
Returns the current size of the socket read buffer. | |
const SocketCollection & | getSockets () const |
Returns collection of all sockets added to interface. | |
const AddressCollection & | getUnicasts () const |
Returns a container of addresses the server should listen on. | |
bool | hasAddress (const isc::asiolink::IOAddress &address) const |
Check if the interface has the specified address assigned. | |
void | resizeReadBuffer (const size_t new_size) |
Reallocates the socket read buffer. | |
void | setActive (const bool active) |
Activates or deactivates all addresses for the interface. | |
void | setActive (const isc::asiolink::IOAddress &address, const bool active) |
Activates or deactivates address for the interface. | |
void | setFlags (uint64_t flags) |
Sets flag_*_ fields based on bitmask value returned by OS. | |
void | setHWType (uint16_t type) |
Sets up hardware type of the interface. | |
void | setMac (const uint8_t *mac, size_t macLen) |
Sets MAC address of the interface. | |
Public Attributes | |
bool | flag_broadcast_ |
Flag specifies if selected interface is broadcast capable. | |
bool | flag_loopback_ |
Specifies if selected interface is loopback. | |
bool | flag_multicast_ |
Flag specifies if selected interface is multicast capable. | |
bool | flag_running_ |
Flag specifies if selected interface is running (e.g. | |
bool | flag_up_ |
Specifies if selected interface is up. | |
uint64_t | flags_ |
Interface flags (this value is as is returned by OS, it may mean different things on different OSes). | |
bool | inactive4_ |
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface. | |
bool | inactive6_ |
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface. | |
Static Public Attributes | |
static const unsigned int | MAX_MAC_LEN = 20 |
Maximum MAC address length (Infiniband uses 20 bytes) | |
Protected Attributes | |
AddressCollection | addrs_ |
List of assigned addresses. | |
uint16_t | hardware_type_ |
Hardware type. | |
unsigned int | ifindex_ |
Interface index (a value that uniquely identifies an interface). | |
uint8_t | mac_ [MAX_MAC_LEN] |
Link-layer address. | |
size_t | mac_len_ |
Length of link-layer address (usually 6). | |
std::string | name_ |
Network interface name. | |
SocketCollection | sockets_ |
Socket used to send data. | |
AddressCollection | unicasts_ |
List of unicast addresses the server should listen on. | |
Represents a single network interface.
Iface structure represents network interface with all useful information, like name, interface index, MAC address and list of assigned addresses
This class also holds the pointer to the socket read buffer. Functions reading from the socket may utilize this buffer to store the data being read from the socket. The advantage of using the pre-allocated buffer is that the buffer is allocated only once, rather than on every read. In addition, some OS specific code (e.g. BPF) may require use of fixed-size buffers. The size of such a buffer is returned by the OS kernel when the socket is opened. Hence, it is convenient to allocate the buffer when the socket is being opened and utilize it throughout the lifetime of the socket.
In order to avoid potentially expensive copies of the Iface
objects holding pre-allocated buffers and multiple containers, this class is noncopyable.
Definition at line 118 of file iface_mgr.h.
Address type.
Definition at line 125 of file iface_mgr.h.
typedef std::list<Address> isc::dhcp::Iface::AddressCollection |
Type that defines list of addresses.
Definition at line 128 of file iface_mgr.h.
using isc::dhcp::Iface::ErrorBuffer = std::vector<std::string> |
Type definition for a list of error messages.
Definition at line 142 of file iface_mgr.h.
typedef std::list<SocketInfo> isc::dhcp::Iface::SocketCollection |
Type that holds a list of socket information.
Definition at line 139 of file iface_mgr.h.
isc::dhcp::Iface::Iface | ( | const std::string & | name, |
unsigned int | ifindex ) |
|
inline |
Destructor.
Definition at line 154 of file iface_mgr.h.
void isc::dhcp::Iface::addAddress | ( | const isc::asiolink::IOAddress & | addr | ) |
Adds an address to an interface.
This only adds an address to collection, it does not physically configure address on actual network interface.
addr | address to be added |
Definition at line 250 of file iface_mgr.cc.
References addrs_, and hasAddress().
void isc::dhcp::Iface::addError | ( | std::string const & | message | ) |
Add an error to the list of messages.
message | the error message |
Definition at line 1979 of file iface_mgr.cc.
|
inline |
Adds socket descriptor to an interface.
sock | SocketInfo structure that describes socket. |
Definition at line 321 of file iface_mgr.h.
References sockets_.
void isc::dhcp::Iface::addUnicast | ( | const isc::asiolink::IOAddress & | addr | ) |
void isc::dhcp::Iface::clearErrors | ( | ) |
Clears all errors.
Definition at line 1984 of file iface_mgr.cc.
|
inline |
Removes any unicast addresses.
Removes any unicast addresses that the server was configured to listen on
Definition at line 353 of file iface_mgr.h.
References unicasts_.
void isc::dhcp::Iface::closeSockets | ( | ) |
Closes all open sockets on interface.
Definition at line 77 of file iface_mgr.cc.
References closeSockets().
Referenced by closeSockets().
void isc::dhcp::Iface::closeSockets | ( | const uint16_t | family | ) |
Closes all IPv4 or IPv6 sockets.
This function closes sockets of the specific 'type' and closes them. The 'type' of the socket indicates whether it is used to send IPv4 or IPv6 packets. The allowed values of the parameter are AF_INET and AF_INET6 for IPv4 and IPv6 packets respectively. It is important to realize that the actual types of sockets may be different than AF_INET for IPv4 packets. This is because, historically the IfaceMgr always used AF_INET sockets for IPv4 traffic. This is no longer the case when the Direct IPv4 traffic must be supported. In order to support direct traffic, the IfaceMgr operates on raw sockets, e.g. AF_PACKET family sockets on Linux.
family | type of the sockets to be closed (AF_INET or AF_INET6) |
BadValue | if family value is different than AF_INET or AF_INET6. |
Definition at line 85 of file iface_mgr.cc.
unsigned int isc::dhcp::Iface::countActive4 | ( | ) | const |
Returns a number of activated IPv4 addresses on the interface.
Definition at line 276 of file iface_mgr.cc.
References addrs_.
bool isc::dhcp::Iface::delAddress | ( | const isc::asiolink::IOAddress & | addr | ) |
Deletes an address from an interface.
This only deletes address from collection, it does not physically remove address configuration from actual network interface.
addr | address to be removed. |
Definition at line 158 of file iface_mgr.cc.
References addrs_.
bool isc::dhcp::Iface::delSocket | ( | uint16_t | sockfd | ) |
Closes socket.
Closes socket and removes corresponding SocketInfo structure from an interface.
sockfd | socket descriptor to be closed/removed. |
Definition at line 168 of file iface_mgr.cc.
References sockets_.
bool isc::dhcp::Iface::getAddress4 | ( | isc::asiolink::IOAddress & | address | ) | const |
Returns IPv4 address assigned to the interface.
This function looks for an IPv4 address assigned to the interface and returns it through the argument.
[out] | address | IPv4 address assigned to the interface. |
Definition at line 224 of file iface_mgr.cc.
References getAddresses().
|
inline |
Returns all addresses available on an interface.
The returned addresses are encapsulated in the util::Optional
class to be able to selectively flag some of the addresses as active (when optional value is specified) or inactive (when optional value is specified). If the address is marked as active, the IfaceMgr::openSockets4
method will open socket and bind to this address. Otherwise, it will not bind any socket to this address. This is useful when an interface has multiple IPv4 addresses assigned.
Care should be taken to not use this collection after Iface object ceases to exist. That is easy in most cases as Iface objects are created by IfaceMgr that is a singleton an is expected to be available at all time. We may revisit this if we ever decide to implement dynamic interface detection, but such fancy feature would mostly be useful for clients with wifi/vpn/virtual interfaces.
Definition at line 254 of file iface_mgr.h.
References addrs_.
Referenced by getAddress4(), and hasAddress().
Iface::ErrorBuffer const & isc::dhcp::Iface::getErrors | ( | ) | const |
Get the consistent list of error messages.
Definition at line 1989 of file iface_mgr.cc.
std::string isc::dhcp::Iface::getFullName | ( | ) | const |
Returns full interface name as "ifname/ifindex" string.
Definition at line 124 of file iface_mgr.cc.
References ifindex_, and name_.
Referenced by setMac().
|
inline |
Returns hardware type of the interface.
Definition at line 234 of file iface_mgr.h.
References hardware_type_.
|
inline |
Returns interface index.
Definition at line 219 of file iface_mgr.h.
References ifindex_.
|
inline |
Returns pointer to MAC address.
Note: Returned pointer is only valid as long as the interface object that returned it.
Definition at line 205 of file iface_mgr.h.
References mac_.
|
inline |
Returns MAC length.
Definition at line 199 of file iface_mgr.h.
References mac_len_.
|
inline |
Returns interface name.
Definition at line 224 of file iface_mgr.h.
References name_.
Referenced by setActive().
std::string isc::dhcp::Iface::getPlainMac | ( | ) | const |
Returns link-layer address a plain text.
Definition at line 131 of file iface_mgr.cc.
|
inline |
Returns the pointer to the buffer used for data reading.
The returned pointer is only valid during the lifetime of the object which returns it or until the buffer is resized. This function is meant to be used with socket API to gather data from the socket.
Definition at line 379 of file iface_mgr.h.
|
inline |
Returns the current size of the socket read buffer.
Definition at line 387 of file iface_mgr.h.
|
inline |
Returns collection of all sockets added to interface.
When new socket is created with IfaceMgr::openSocket it is added to sockets collection on particular interface. If socket is opened by other means (e.g. function that does not use IfaceMgr::openSocket) it will not be available in this collection. Note that functions like IfaceMgr::openSocketFromIface use IfaceMgr::openSocket internally. The returned reference is only valid during the lifetime of the IfaceMgr object that returned it.
Definition at line 347 of file iface_mgr.h.
References sockets_.
|
inline |
Returns a container of addresses the server should listen on.
Definition at line 366 of file iface_mgr.h.
References unicasts_.
bool isc::dhcp::Iface::hasAddress | ( | const isc::asiolink::IOAddress & | address | ) | const |
Check if the interface has the specified address assigned.
address | Address to be checked. |
Definition at line 240 of file iface_mgr.cc.
References getAddresses().
Referenced by addAddress().
|
inline |
Reallocates the socket read buffer.
new_size | New size of the buffer. |
Definition at line 394 of file iface_mgr.h.
void isc::dhcp::Iface::setActive | ( | const bool | active | ) |
Activates or deactivates all addresses for the interface.
This method marks all addresses on the interface active or inactive. If the address is marked inactive, the IfaceMgr::openSockets4
method will NOT open socket for this address.
active | A boolean flag which indicates that the addresses should be active (if true) or inactive (if false). |
Definition at line 269 of file iface_mgr.cc.
References addrs_.
void isc::dhcp::Iface::setActive | ( | const isc::asiolink::IOAddress & | address, |
const bool | active ) |
Activates or deactivates address for the interface.
This method marks a specified address on the interface active or inactive. If the address is marked inactive, the IfaceMgr::openSockets4
method will NOT open socket for this address.
address | An address which should be activated, deactivated. |
active | A boolean flag which indicates that the specified address should be active (if true) or inactive (if false). |
BadValue | if specified address doesn't exist for the interface. |
Definition at line 257 of file iface_mgr.cc.
References addrs_, getName(), and isc_throw.
void isc::dhcp::Iface::setFlags | ( | uint64_t | flags | ) |
Sets flag_*_ fields based on bitmask value returned by OS.
flags | bitmask value returned by OS in interface detection |
|
inline |
Sets up hardware type of the interface.
type | hardware type |
Definition at line 229 of file iface_mgr.h.
References hardware_type_.
void isc::dhcp::Iface::setMac | ( | const uint8_t * | mac, |
size_t | macLen ) |
Sets MAC address of the interface.
mac | pointer to MAC address buffer |
macLen | length of mac address |
Definition at line 145 of file iface_mgr.cc.
References getFullName(), isc_throw, mac_, mac_len_, and MAX_MAC_LEN.
|
protected |
List of assigned addresses.
Definition at line 422 of file iface_mgr.h.
Referenced by addAddress(), countActive4(), delAddress(), getAddresses(), setActive(), and setActive().
bool isc::dhcp::Iface::flag_broadcast_ |
Flag specifies if selected interface is broadcast capable.
Definition at line 454 of file iface_mgr.h.
bool isc::dhcp::Iface::flag_loopback_ |
Specifies if selected interface is loopback.
Definition at line 441 of file iface_mgr.h.
bool isc::dhcp::Iface::flag_multicast_ |
Flag specifies if selected interface is multicast capable.
Definition at line 451 of file iface_mgr.h.
bool isc::dhcp::Iface::flag_running_ |
Flag specifies if selected interface is running (e.g.
cable plugged in, wifi associated).
Definition at line 448 of file iface_mgr.h.
bool isc::dhcp::Iface::flag_up_ |
Specifies if selected interface is up.
Definition at line 444 of file iface_mgr.h.
uint64_t isc::dhcp::Iface::flags_ |
Interface flags (this value is as is returned by OS, it may mean different things on different OSes).
Solaris based os have unsigned long flags field (64 bits). It is usually 32 bits, though.
Definition at line 460 of file iface_mgr.h.
|
protected |
Hardware type.
Definition at line 434 of file iface_mgr.h.
Referenced by getHWType(), and setHWType().
|
protected |
Interface index (a value that uniquely identifies an interface).
Definition at line 419 of file iface_mgr.h.
Referenced by getFullName(), and getIndex().
bool isc::dhcp::Iface::inactive4_ |
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface.
Definition at line 464 of file iface_mgr.h.
bool isc::dhcp::Iface::inactive6_ |
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface.
Definition at line 468 of file iface_mgr.h.
|
protected |
Link-layer address.
Definition at line 428 of file iface_mgr.h.
Referenced by Iface(), getMac(), getPlainMac(), and setMac().
|
protected |
Length of link-layer address (usually 6).
Definition at line 431 of file iface_mgr.h.
Referenced by getMacLen(), getPlainMac(), and setMac().
|
static |
Maximum MAC address length (Infiniband uses 20 bytes)
Definition at line 122 of file iface_mgr.h.
Referenced by setMac().
|
protected |
Network interface name.
Definition at line 416 of file iface_mgr.h.
Referenced by addUnicast(), getFullName(), and getName().
|
protected |
Socket used to send data.
Definition at line 413 of file iface_mgr.h.
Referenced by addSocket(), closeSockets(), delSocket(), and getSockets().
|
protected |
List of unicast addresses the server should listen on.
Definition at line 425 of file iface_mgr.h.
Referenced by addUnicast(), clearUnicasts(), and getUnicasts().