![]() |
Kea 3.1.9
|
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 boost::multi_index_container< SocketInfo, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::sequenced<> > > | SocketCollection |
| Type that holds a list of socket information. | |
| typedef SocketCollection::nth_index< 1 >::type | SocketLruIndex |
| SocketInfo LRU index type. | |
| typedef SocketLruIndex::iterator | SocketLruIterator |
| SocketInfo LRU iterator type. | |
Public Member Functions | |
| Iface (const std::string &name, unsigned int ifindex) | |
| Iface constructor. | |
| ~Iface ()=default | |
| 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. | |
| SocketCollection & | getSocketsRef () |
| 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 136 of file iface_mgr.h.
Address type.
Definition at line 143 of file iface_mgr.h.
| typedef std::list<Address> isc::dhcp::Iface::AddressCollection |
Type that defines list of addresses.
Definition at line 146 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 176 of file iface_mgr.h.
| typedef boost::multi_index_container< SocketInfo, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::sequenced<> > > isc::dhcp::Iface::SocketCollection |
Type that holds a list of socket information.
Definition at line 167 of file iface_mgr.h.
| typedef SocketCollection::nth_index<1>::type isc::dhcp::Iface::SocketLruIndex |
SocketInfo LRU index type.
Definition at line 170 of file iface_mgr.h.
| typedef SocketLruIndex::iterator isc::dhcp::Iface::SocketLruIterator |
SocketInfo LRU iterator type.
Definition at line 173 of file iface_mgr.h.
| isc::dhcp::Iface::Iface | ( | const std::string & | name, |
| unsigned int | ifindex ) |
Iface constructor.
Creates Iface object that represents network interface.
| name | name of the interface |
| ifindex | interface index (unique integer identifier) |
| BadValue | when name is empty. |
Definition at line 59 of file iface_mgr.cc.
References flag_broadcast_, flag_loopback_, flag_multicast_, flag_running_, flag_up_, flags_, hardware_type_, ifindex_, inactive4_, inactive6_, mac_, mac_len_, and name_.
|
default |
Destructor.
| 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 251 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 2241 of file iface_mgr.cc.
|
inline |
Adds socket descriptor to an interface.
| sock | SocketInfo structure that describes socket. |
Definition at line 369 of file iface_mgr.h.
References sockets_.
Referenced by isc::dhcp::IfaceMgr::openSocket4().
| void isc::dhcp::Iface::addUnicast | ( | const isc::asiolink::IOAddress & | addr | ) |
| void isc::dhcp::Iface::clearErrors | ( | ) |
Clears all errors.
Definition at line 2246 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 410 of file iface_mgr.h.
References unicasts_.
| void isc::dhcp::Iface::closeSockets | ( | ) |
Closes all open sockets on interface.
Definition at line 72 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 80 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 277 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 151 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 161 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 225 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 300 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 2251 of file iface_mgr.cc.
| std::string isc::dhcp::Iface::getFullName | ( | ) | const |
Returns full interface name as "ifname/ifindex" string.
Definition at line 117 of file iface_mgr.cc.
References ifindex_, and name_.
Referenced by setMac().
|
inline |
Returns hardware type of the interface.
Definition at line 278 of file iface_mgr.h.
References hardware_type_.
Referenced by isc::dhcp::PktFilterBPF::send(), and isc::dhcp::PktFilterLPF::send().
|
inline |
Returns interface index.
Definition at line 257 of file iface_mgr.h.
References ifindex_.
Referenced by isc::dhcp::PktFilterLPF::openSocket(), isc::dhcp::PktFilterBPF::receive(), isc::dhcp::PktFilterInet::receive(), isc::dhcp::PktFilterLPF::receive(), and isc::dhcp::PktFilterLPF::send().
|
inline |
Returns pointer to MAC address.
Note: Returned pointer is only valid as long as the interface object that returned it.
Definition at line 241 of file iface_mgr.h.
References mac_.
Referenced by isc::dhcp::PktFilterBPF::send(), and isc::dhcp::PktFilterLPF::send().
|
inline |
Returns MAC length.
Definition at line 233 of file iface_mgr.h.
References mac_len_.
Referenced by isc::dhcp::PktFilterBPF::send(), and isc::dhcp::PktFilterLPF::send().
|
inline |
Returns interface name.
Definition at line 264 of file iface_mgr.h.
References name_.
Referenced by isc::dhcp::PktFilterBPF::openSocket(), isc::dhcp::PktFilterInet6::openSocket(), isc::dhcp::PktFilterInet::openSocket(), isc::dhcp::PktFilterLPF::openSocket(), isc::dhcp::PktFilterBPF::receive(), isc::dhcp::PktFilterInet::receive(), isc::dhcp::PktFilterLPF::receive(), and setActive().
| std::string isc::dhcp::Iface::getPlainMac | ( | ) | const |
Returns link-layer address a plain text.
Definition at line 124 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 436 of file iface_mgr.h.
Referenced by isc::dhcp::PktFilterBPF::receive().
|
inline |
Returns the current size of the socket read buffer.
Definition at line 444 of file iface_mgr.h.
Referenced by isc::dhcp::PktFilterBPF::receive().
|
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 395 of file iface_mgr.h.
References sockets_.
|
inline |
Returns collection of all sockets added to interface.
Definition at line 402 of file iface_mgr.h.
References sockets_.
|
inline |
Returns a container of addresses the server should listen on.
Definition at line 423 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 241 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 451 of file iface_mgr.h.
Referenced by isc::dhcp::PktFilterBPF::openSocket().
| 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 270 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 258 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 271 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 138 of file iface_mgr.cc.
References getFullName(), isc_throw, mac_, mac_len_, and MAX_MAC_LEN.
|
protected |
List of assigned addresses.
Definition at line 479 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 511 of file iface_mgr.h.
Referenced by Iface(), and isc::dhcp::PktFilterInet::openSocket().
| bool isc::dhcp::Iface::flag_loopback_ |
Specifies if selected interface is loopback.
Definition at line 498 of file iface_mgr.h.
Referenced by Iface(), isc::dhcp::PktFilterBPF::openSocket(), isc::dhcp::PktFilterBPF::receive(), and isc::dhcp::PktFilterBPF::send().
| bool isc::dhcp::Iface::flag_multicast_ |
Flag specifies if selected interface is multicast capable.
Definition at line 508 of file iface_mgr.h.
Referenced by Iface().
| bool isc::dhcp::Iface::flag_running_ |
Flag specifies if selected interface is running (e.g.
cable plugged in, wifi associated).
Definition at line 505 of file iface_mgr.h.
Referenced by Iface().
| bool isc::dhcp::Iface::flag_up_ |
Specifies if selected interface is up.
Definition at line 501 of file iface_mgr.h.
Referenced by Iface().
| 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 517 of file iface_mgr.h.
Referenced by Iface().
|
protected |
Hardware type.
Definition at line 491 of file iface_mgr.h.
Referenced by Iface(), getHWType(), and setHWType().
|
protected |
Interface index (a value that uniquely identifies an interface).
Definition at line 476 of file iface_mgr.h.
Referenced by Iface(), 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 521 of file iface_mgr.h.
Referenced by Iface().
| bool isc::dhcp::Iface::inactive6_ |
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface.
Definition at line 525 of file iface_mgr.h.
Referenced by Iface().
|
protected |
Link-layer address.
Definition at line 485 of file iface_mgr.h.
Referenced by Iface(), getMac(), getPlainMac(), and setMac().
|
protected |
Length of link-layer address (usually 6).
Definition at line 488 of file iface_mgr.h.
Referenced by Iface(), getMacLen(), getPlainMac(), and setMac().
|
static |
Maximum MAC address length (Infiniband uses 20 bytes).
Definition at line 140 of file iface_mgr.h.
Referenced by setMac().
|
protected |
Network interface name.
Definition at line 473 of file iface_mgr.h.
Referenced by Iface(), addUnicast(), getFullName(), and getName().
|
protected |
Socket used to send data.
Definition at line 470 of file iface_mgr.h.
Referenced by addSocket(), closeSockets(), delSocket(), getSockets(), and getSocketsRef().
|
protected |
List of unicast addresses the server should listen on.
Definition at line 482 of file iface_mgr.h.
Referenced by addUnicast(), clearUnicasts(), and getUnicasts().