Kea 2.7.5
|
Handles network interfaces, transmission and reception. More...
#include <iface_mgr.h>
Classes | |
struct | SocketCallbackInfo |
Keeps callback information for external sockets. More... | |
Public Types | |
typedef std::function< bool(bool)> | DetectCallback |
Defines callback used when detecting interfaces. | |
typedef std::function< void(int fd)> | SocketCallback |
Defines callback used when data is received over external sockets. | |
typedef std::list< SocketCallbackInfo > | SocketCallbackInfoContainer |
Defines storage container for callbacks for external sockets. | |
Public Member Functions | |
virtual | ~IfaceMgr () |
Destructor. | |
void | addExternalSocket (int socketfd, SocketCallback callback) |
Adds external socket and a callback. | |
void | addInterface (const IfacePtr &iface) |
Adds an interface to list of known interfaces. | |
bool | checkDetectIfaces (bool update_only) |
Check if the specific system calls used to detect interfaces should be executed. | |
void | clearBoundAddresses () |
Clears the addresses all sockets are bound to. | |
void | clearIfaces () |
Removes detected interfaces. | |
void | clearUnicasts () |
Clears unicast addresses on all interfaces. | |
void | closeSockets () |
Closes all open sockets. | |
void | collectBoundAddresses () |
Collect the addresses all sockets are bound to. | |
bool | configureDHCPPacketQueue (const uint16_t family, data::ConstElementPtr queue_control) |
Configures DHCP packet queue. | |
uint16_t | countIfaces () |
Returns number of detected interfaces. | |
void | deleteAllExternalSockets () |
Deletes all external sockets. | |
void | deleteExternalSocket (int socketfd) |
Deletes external socket. | |
void | detectIfaces (bool update_only=false) |
Detects network interfaces. | |
IfacePtr | getIface (const PktPtr &pkt) |
Returns interface with specified packet. | |
IfacePtr | getIface (const std::string &ifname) |
Returns interface with specified interface name. | |
IfacePtr | getIface (const unsigned int ifindex) |
Returns interface specified interface index. | |
const IfaceCollection & | getIfaces () |
Returns container with all interfaces. | |
PacketQueue4Ptr | getPacketQueue4 () |
Fetches the DHCPv4 receiver packet queue. | |
PacketQueue6Ptr | getPacketQueue6 () |
Fetches the DHCPv6 receiver packet queue. | |
PacketQueueMgr4Ptr | getPacketQueueMgr4 () |
Fetches the DHCPv4 packet queue manager. | |
PacketQueueMgr6Ptr | getPacketQueueMgr6 () |
Fetches the DHCPv6 packet queue manager. | |
SocketInfo | getSocket (const isc::dhcp::Pkt4Ptr &pkt) |
Return most suitable socket for transmitting specified IPv4 packet. | |
uint16_t | getSocket (const isc::dhcp::Pkt6Ptr &pkt) |
Return most suitable socket for transmitting specified IPv6 packet. | |
bool | hasOpenSocket (const isc::asiolink::IOAddress &addr) const |
Checks if there is a socket open and bound to an address. | |
bool | hasOpenSocket (const uint16_t family) const |
Checks if there is at least one socket of the specified family open. | |
bool | isDHCPReceiverRunning () const |
Returns true if there is a receiver exists and its thread is currently running. | |
bool | isDirectResponseSupported () const |
Check if packet be sent directly to the client having no address. | |
bool | isExternalSocket (int fd) |
Checks if socket's file description is registered. | |
virtual bool | isSocketReceivedTimeSupported () const |
Check if the socket received time is supported. | |
bool | isTestMode () const |
Checks if the IfaceMgr is in the test mode. | |
int | openSocket (const std::string &ifname, const isc::asiolink::IOAddress &addr, const uint16_t port, const bool receive_bcast=false, const bool send_bcast=false) |
Opens UDP/IP socket and binds it to address, interface and port. | |
int | openSocketFromAddress (const isc::asiolink::IOAddress &addr, const uint16_t port) |
Opens UDP/IP socket and binds to address specified. | |
int | openSocketFromIface (const std::string &ifname, const uint16_t port, const uint8_t family) |
Opens UDP/IP socket and binds it to interface specified. | |
int | openSocketFromRemoteAddress (const isc::asiolink::IOAddress &remote_addr, const uint16_t port) |
Opens UDP/IP socket to be used to connect to remote address. | |
bool | openSockets4 (const uint16_t port=DHCP4_SERVER_PORT, const bool use_bcast=true, IfaceMgrErrorMsgCallback error_handler=0, const bool skip_opened=false) |
Opens IPv4 sockets on detected interfaces. | |
bool | openSockets6 (const uint16_t port=DHCP6_SERVER_PORT, IfaceMgrErrorMsgCallback error_handler=0, const bool skip_opened=false) |
Opens IPv6 sockets on detected interfaces. | |
void | printIfaces (std::ostream &out=std::cout) |
Debugging method that prints out all available interfaces. | |
int | purgeBadSockets () |
Scans registered socket set and removes any that are invalid. | |
Pkt4Ptr | receive4 (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv4 packets or data from external sockets. | |
Pkt6Ptr | receive6 (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv4 packets or data from external sockets. | |
bool | send (const Pkt4Ptr &pkt) |
Sends an IPv4 packet. | |
bool | send (const Pkt6Ptr &pkt) |
Sends an IPv6 packet. | |
void | setAllowLoopBack (const bool allow_loopback) |
Allows or disallows the loopback interface. | |
void | setDetectCallback (const DetectCallback &cb) |
Set a callback to perform operations before executing specific system calls. | |
void | setMatchingPacketFilter (const bool direct_response_desired=false) |
Set Packet Filter object to handle send/receive packets. | |
void | setPacketFilter (const PktFilter6Ptr &packet_filter) |
Set packet filter object to handle sending and receiving DHCPv6 messages. | |
void | setPacketFilter (const PktFilterPtr &packet_filter) |
Set packet filter object to handle sending and receiving DHCPv4 messages. | |
void | setTestMode (const bool test_mode) |
Sets or clears the test mode for IfaceMgr . | |
void | startDHCPReceiver (const uint16_t family) |
Starts DHCP packet receiver. | |
void | stopDHCPReceiver () |
Stops the DHCP packet receiver. | |
Static Public Member Functions | |
static void | addFDtoSet (int fd, int &maxfd, fd_set *sockets) |
Convenience method for adding an descriptor to a set. | |
static IfaceMgr & | instance () |
IfaceMgr is a singleton class. | |
static const IfaceMgrPtr & | instancePtr () |
Returns pointer to the sole instance of the interface manager. | |
Static Public Attributes | |
static const uint32_t | RCVBUFSIZE = 1500 |
Packet reception buffer size. | |
Protected Member Functions | |
IfaceMgr () | |
Protected constructor. | |
int | openSocket4 (Iface &iface, const isc::asiolink::IOAddress &addr, const uint16_t port, const bool receive_bcast=false, const bool send_bcast=false) |
Opens IPv4 socket. | |
int | openSocket6 (Iface &iface, const isc::asiolink::IOAddress &addr, uint16_t port, const bool join_multicast) |
Opens IPv6 socket. | |
Pkt4Ptr | receive4Direct (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv4 packets directly or data from external sockets. | |
Pkt4Ptr | receive4Indirect (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv4 packets indirectly or data from external sockets. | |
Pkt6Ptr | receive6Direct (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv6 packets directly or data from external sockets. | |
Pkt6Ptr | receive6Indirect (uint32_t timeout_sec, uint32_t timeout_usec=0) |
Receive IPv6 packets indirectly or data from external sockets. | |
Protected Attributes | |
BoundAddresses | bound_address_ |
Unordered set of IPv4 bound addresses. | |
IfaceCollection | ifaces_ |
List of available interfaces. | |
Handles network interfaces, transmission and reception.
IfaceMgr is an interface manager class that detects available network interfaces, configured addresses, link-local addresses, and provides API for using sockets.
Definition at line 656 of file iface_mgr.h.
typedef std::function<bool (bool)> isc::dhcp::IfaceMgr::DetectCallback |
Defines callback used when detecting interfaces.
update_only | Only add interfaces that do not exist and update existing interfaces. |
Definition at line 670 of file iface_mgr.h.
typedef std::function<void (int fd)> isc::dhcp::IfaceMgr::SocketCallback |
Defines callback used when data is received over external sockets.
fd | socket descriptor of the ready socket |
Definition at line 661 of file iface_mgr.h.
typedef std::list<SocketCallbackInfo> isc::dhcp::IfaceMgr::SocketCallbackInfoContainer |
Defines storage container for callbacks for external sockets.
Definition at line 682 of file iface_mgr.h.
|
virtual |
Destructor.
Closes open sockets.
Definition at line 314 of file iface_mgr.cc.
References closeSockets().
|
protected |
Protected constructor.
Protected constructor. This is a singleton class. We don't want anyone to create instances of IfaceMgr. Use instance() method instead.
Definition at line 185 of file iface_mgr.cc.
References checkDetectIfaces(), detectIfaces(), isc_throw, and isc::Exception::what().
Referenced by instancePtr().
void isc::dhcp::IfaceMgr::addExternalSocket | ( | int | socketfd, |
SocketCallback | callback ) |
Adds external socket and a callback.
Specifies external socket and a callback that will be called when data will be received over that socket.
socketfd | socket descriptor |
callback | callback function |
Definition at line 329 of file iface_mgr.cc.
References isc_throw, and isc::dhcp::IfaceMgr::SocketCallbackInfo::socket_.
|
static |
Convenience method for adding an descriptor to a set.
fd | descriptor to add | |
[out] | maxfd | maximum fd value in the set. If the new fd is larger than it's current value, it will be updated to new fd value |
sockets | pointer to the set of sockets |
BadValue | if sockets is null |
Definition at line 1389 of file iface_mgr.cc.
References isc_throw.
Referenced by receive4Direct(), receive4Indirect(), receive6Direct(), and receive6Indirect().
void isc::dhcp::IfaceMgr::addInterface | ( | const IfacePtr & | iface | ) |
Adds an interface to list of known interfaces.
iface | reference to Iface object. |
Unexpected | when name or index already exists. |
Definition at line 775 of file iface_mgr.cc.
References ifaces_, isc_throw, and isc::dhcp::IfaceCollection::push_back().
bool isc::dhcp::IfaceMgr::checkDetectIfaces | ( | bool | update_only | ) |
Check if the specific system calls used to detect interfaces should be executed.
update_only | Only add interfaces that do not exist and update existing interfaces. |
Definition at line 1994 of file iface_mgr.cc.
Referenced by IfaceMgr().
void isc::dhcp::IfaceMgr::clearBoundAddresses | ( | ) |
Clears the addresses all sockets are bound to.
Definition at line 906 of file iface_mgr.cc.
References bound_address_.
Referenced by closeSockets().
void isc::dhcp::IfaceMgr::clearIfaces | ( | ) |
Removes detected interfaces.
This method removes all detected interfaces. This method should be used by unit tests to supply a custom set of interfaces. For example: a unit test may create a pool of fake interfaces and use the custom PktFilter
class to mimic socket operation on these interfaces.
Definition at line 901 of file iface_mgr.cc.
References isc::dhcp::IfaceCollection::clear(), and ifaces_.
void isc::dhcp::IfaceMgr::clearUnicasts | ( | ) |
Clears unicast addresses on all interfaces.
Definition at line 926 of file iface_mgr.cc.
References ifaces_.
void isc::dhcp::IfaceMgr::closeSockets | ( | ) |
Closes all open sockets.
It calls stopDHCPReceiver
to stop the receiver thread and then it closes all open interface sockets.
Is used in destructor, but also from Dhcpv4Srv and Dhcpv6Srv classes.
Definition at line 286 of file iface_mgr.cc.
References clearBoundAddresses(), ifaces_, and stopDHCPReceiver().
Referenced by ~IfaceMgr().
void isc::dhcp::IfaceMgr::collectBoundAddresses | ( | ) |
Collect the addresses all sockets are bound to.
Definition at line 911 of file iface_mgr.cc.
References bound_address_, ifaces_, isc::asiolink::IOAddress::isV4(), and isc::asiolink::IOAddress::toUint32().
Referenced by openSockets4().
bool isc::dhcp::IfaceMgr::configureDHCPPacketQueue | ( | const uint16_t | family, |
data::ConstElementPtr | queue_control ) |
Configures DHCP packet queue.
If the given configuration enables packet queueing, then the appropriate queue is created. Otherwise, the existing queue is destroyed. If the receiver thread is running when this function is invoked, it will throw.
family | indicates which receiver to start, (AF_INET or AF_INET6) |
queue_control | configuration containing "dhcp-queue-control" content |
InvalidOperation | if the receiver thread is currently running. |
Definition at line 1943 of file iface_mgr.cc.
References isc::data::SimpleParser::getBoolean(), isc_throw, and isDHCPReceiverRunning().
|
inline |
Returns number of detected interfaces.
Definition at line 1146 of file iface_mgr.h.
References ifaces_, and isc::dhcp::IfaceCollection::size().
void isc::dhcp::IfaceMgr::deleteAllExternalSockets | ( | ) |
Deletes all external sockets.
Definition at line 399 of file iface_mgr.cc.
void isc::dhcp::IfaceMgr::deleteExternalSocket | ( | int | socketfd | ) |
Deletes external socket.
socketfd | socket descriptor |
Definition at line 352 of file iface_mgr.cc.
void isc::dhcp::IfaceMgr::detectIfaces | ( | bool | update_only = false | ) |
Detects network interfaces.
If the detect callback returns true, the specific system calls are executed, otherwise the detectIfaces will return immediately.
update_only | Only add interfaces that do not exist and update existing interfaces. |
Referenced by IfaceMgr().
Returns interface with specified packet.
Pkt::indexSet
returns true) it is searched for, if it is not set the name instead is searched for.pkt | packet with interface index and name |
Definition at line 892 of file iface_mgr.cc.
References getIface().
IfacePtr isc::dhcp::IfaceMgr::getIface | ( | const std::string & | ifname | ) |
Returns interface with specified interface name.
ifname | name of searched interface |
Definition at line 884 of file iface_mgr.cc.
References isc::dhcp::IfaceCollection::getIface(), and ifaces_.
IfacePtr isc::dhcp::IfaceMgr::getIface | ( | const unsigned int | ifindex | ) |
Returns interface specified interface index.
ifindex | index of searched interface |
Definition at line 879 of file iface_mgr.cc.
References isc::dhcp::IfaceCollection::getIface(), and ifaces_.
Referenced by getIface(), getSocket(), getSocket(), openSocket(), send(), and send().
|
inline |
Returns container with all interfaces.
This reference is only valid as long as IfaceMgr is valid. However, since IfaceMgr is a singleton and is expected to be destroyed after main() function completes, you should not worry much about this.
Definition at line 799 of file iface_mgr.h.
References ifaces_.
|
inline |
Fetches the DHCPv4 receiver packet queue.
Incoming packets are read by the receiver thread and added to this queue. receive4()
dequeues and returns them.
Definition at line 1285 of file iface_mgr.h.
Referenced by receive4Indirect(), startDHCPReceiver(), and stopDHCPReceiver().
|
inline |
Fetches the DHCPv6 receiver packet queue.
Incoming packets are read by the receiver thread and added to this queue. receive6()
dequeues and returns them.
Definition at line 1302 of file iface_mgr.h.
Referenced by receive6Indirect(), startDHCPReceiver(), and stopDHCPReceiver().
|
inline |
Fetches the DHCPv4 packet queue manager.
Definition at line 1275 of file iface_mgr.h.
|
inline |
Fetches the DHCPv6 packet queue manager.
Definition at line 1292 of file iface_mgr.h.
SocketInfo isc::dhcp::IfaceMgr::getSocket | ( | const isc::dhcp::Pkt4Ptr & | pkt | ) |
Return most suitable socket for transmitting specified IPv4 packet.
This method uses the local address assigned to the packet and tries to match it with addresses to which sockets are bound for the particular interface. If the match is not found, the method returns the first IPv4 socket found for the particular interface. In case, there are no IPv4 sockets assigned to the interface the exception is thrown.
pkt | A packet to be transmitted. It must hold a local address and a valid pointer to the interface. |
SocketNotFound | if no suitable socket found. |
Definition at line 1910 of file iface_mgr.cc.
References getIface(), and isc_throw.
uint16_t isc::dhcp::IfaceMgr::getSocket | ( | const isc::dhcp::Pkt6Ptr & | pkt | ) |
Return most suitable socket for transmitting specified IPv6 packet.
This method takes Pkt6Ptr (see overloaded implementation that takes Pkt4Ptr) and chooses appropriate socket to send it. This method may throw if specified packet does not have outbound interface specified, no such interface exists, or specified interface does not have any appropriate sockets open.
pkt | a packet to be transmitted |
SocketNotFound | If no suitable socket found. |
IfaceNotFound | If interface is not set for the packet. |
Definition at line 1854 of file iface_mgr.cc.
References getIface(), and isc_throw.
Referenced by send(), and send().
bool isc::dhcp::IfaceMgr::hasOpenSocket | ( | const isc::asiolink::IOAddress & | addr | ) | const |
Checks if there is a socket open and bound to an address.
This function checks if one of the sockets opened by the IfaceMgr is bound to the IP address specified as the method parameter. If the socket is bound to the port (and address is unspecified), the method will check if the address passed in the argument is configured on an interface. Note: On BSD and Solaris the socket is opened for "::" address instead of the link-local address or the "ff02::1:2" address. If there are multiple interfaces joining the multicast group, this function matches the "::" address bound by any interface, not necessary the one with the specified link-local address and returns true.
addr | Address of the socket being searched. |
Definition at line 463 of file iface_mgr.cc.
References bound_address_, ifaces_, isc::asiolink::IOAddress::isV4(), and isc::asiolink::IOAddress::toUint32().
bool isc::dhcp::IfaceMgr::hasOpenSocket | ( | const uint16_t | family | ) | const |
Checks if there is at least one socket of the specified family open.
family | A socket family. |
Definition at line 447 of file iface_mgr.cc.
References ifaces_.
Referenced by openSockets4(), openSockets6(), setPacketFilter(), and setPacketFilter().
|
static |
IfaceMgr is a singleton class.
This method returns reference to its sole instance.
Definition at line 54 of file iface_mgr.cc.
References instancePtr().
Referenced by isc::dhcp::Dhcpv4Srv::Dhcpv4Srv(), isc::dhcp::Dhcpv6Srv::Dhcpv6Srv(), isc::perfdhcp::TestControl::TestControl(), isc::dhcp::Dhcpv4Srv::~Dhcpv4Srv(), isc::dhcp::Dhcpv6Srv::~Dhcpv6Srv(), isc::perfdhcp::PerfSocket::~PerfSocket(), isc::http::HttpConnection::acceptorCallback(), isc::dhcp::Dhcpv4Srv::acceptServerId(), isc::dhcp::Dhcpv4Srv::adjustIfaceData(), isc::dhcp::Dhcpv4Srv::adjustRemoteAddr(), isc::dhcp::Dhcpv4Srv::appendServerID(), isc::ha::HAService::clientCloseHandler(), isc::ha::HAService::clientConnectHandler(), isc::dhcp::Dhcp4o6IpcBase::close(), isc::http::HttpConnection::close(), isc::config::UnixCommandMgrImpl::closeCommandSocket(), isc::dhcp::CfgIface::closeSockets(), isc::http::HttpConnection::closeWatchSocket(), isc::dhcp::configureDhcp4Server(), isc::dhcp::configureDhcp6Server(), dhcp4_srv_configured(), dhcp6_srv_configured(), isc::perfdhcp::PerfSocket::getIface(), isc::dhcp::Pkt::getMACFromIPv6(), isc::dhcp::Pkt6::getMACFromRemoteIdRelayOption(), isc::dhcp::Dhcp6to4Ipc::handler(), isc::perfdhcp::PerfSocket::initSocketData(), isc::dhcp::Subnet4ConfigParser::initSubnet(), isc::dhcp::Subnet6ConfigParser::initSubnet(), isc::dhcp::Dhcp4to6Ipc::open(), isc::dhcp::Dhcp6to4Ipc::open(), isc::config::UnixCommandMgrImpl::openCommandSocket(), isc::perfdhcp::PerfSocket::openSocket(), isc::dhcp::CfgIface::openSockets(), isc::dhcp::IfacesConfigParser::parse(), isc::dhcp::SharedNetwork4Parser::parse(), isc::dhcp::SharedNetwork6Parser::parse(), isc::dhcp::ControlledDhcpv4Srv::processConfig(), isc::dhcp::ControlledDhcpv6Srv::processConfig(), isc::dhcp::Dhcp4o6IpcBase::receive(), isc::perfdhcp::PerfSocket::receive4(), isc::perfdhcp::PerfSocket::receive6(), isc::dhcp::Dhcpv4Srv::receivePacket(), isc::dhcp::Dhcpv6Srv::receivePacket(), isc::dhcp::CfgSubnets4::selectSubnet(), isc::perfdhcp::PerfSocket::send(), isc::perfdhcp::PerfSocket::send(), isc::dhcp::Dhcpv4Srv::sendPacket(), isc::dhcp::Dhcpv6Srv::sendPacket(), isc::http::HttpConnection::shutdown(), isc::http::HttpConnection::shutdownCallback(), isc::http::HttpListenerImpl::start(), isc::dhcp::D2ClientMgr::startSender(), isc::http::HttpListenerImpl::stop(), isc::dhcp::D2ClientMgr::stopSender(), and isc::dhcp::CfgIface::use().
|
static |
Returns pointer to the sole instance of the interface manager.
This method returns the pointer to the instance of the interface manager which can be held in singleton objects that depend on it. This will eliminate issues with the static deinitialization fiasco between this object and dependent singleton objects.
The IfaceMgr::instance
method should be considered deprecated.
IfaceMgr
instance. Definition at line 59 of file iface_mgr.cc.
References IfaceMgr().
Referenced by instance().
|
inline |
Returns true if there is a receiver exists and its thread is currently running.
Definition at line 1326 of file iface_mgr.h.
Referenced by configureDHCPPacketQueue(), receive4(), receive6(), startDHCPReceiver(), and stopDHCPReceiver().
bool isc::dhcp::IfaceMgr::isDirectResponseSupported | ( | ) | const |
Check if packet be sent directly to the client having no address.
Checks if IfaceMgr can send DHCPv4 packet to the client who hasn't got address assigned. If this is not supported broadcast address should be used to send response to the client.
Definition at line 319 of file iface_mgr.cc.
Referenced by openSockets4().
bool isc::dhcp::IfaceMgr::isExternalSocket | ( | int | fd | ) |
Checks if socket's file description is registered.
Definition at line 369 of file iface_mgr.cc.
|
virtual |
Check if the socket received time is supported.
If true, then received packets will include a SOCKET_RECEIVED event in their event stack.
Definition at line 324 of file iface_mgr.cc.
|
inline |
Checks if the IfaceMgr
is in the test mode.
IfaceMgr
is in the test mode, false otherwise. Definition at line 732 of file iface_mgr.h.
int isc::dhcp::IfaceMgr::openSocket | ( | const std::string & | ifname, |
const isc::asiolink::IOAddress & | addr, | ||
const uint16_t | port, | ||
const bool | receive_bcast = false, | ||
const bool | send_bcast = false ) |
Opens UDP/IP socket and binds it to address, interface and port.
Specific type of socket (UDP/IPv4 or UDP/IPv6) depends on passed addr family.
ifname | name of the interface |
addr | address to be bound. |
port | UDP port. |
receive_bcast | configure IPv4 socket to receive broadcast messages or IPv6 socket to join multicast group. |
send_bcast | configure IPv4 socket to send broadcast messages. This parameter is ignored for IPv6 sockets. |
Method will throw if socket creation, socket binding or multicast join fails.
Definition at line 932 of file iface_mgr.cc.
References getIface(), isc_throw, isc::asiolink::IOAddress::isV4(), isc::asiolink::IOAddress::isV6(), openSocket4(), and openSocket6().
Referenced by openSocketFromAddress(), openSocketFromIface(), openSockets4(), and openSockets6().
|
protected |
Opens IPv4 socket.
Please do not use this method directly. Use openSocket instead.
This method may throw exception if socket creation fails.
iface | reference to interface structure. |
addr | an address the created socket should be bound to |
port | a port that created socket should be bound to |
receive_bcast | configure socket to receive broadcast messages |
send_bcast | configure socket to send broadcast messages. |
Definition at line 1089 of file iface_mgr.cc.
References isc::db::info.
Referenced by openSocket().
|
protected |
Opens IPv6 socket.
Please do not use this method directly. Use openSocket instead.
This method may throw exception if socket creation fails.
iface | reference to interface structure. |
addr | an address the created socket should be bound to |
port | a port that created socket should be bound to |
join_multicast | A boolean parameter which indicates whether socket should join All_DHCP_Relay_Agents_and_servers multicast group. |
Referenced by openSocket().
int isc::dhcp::IfaceMgr::openSocketFromAddress | ( | const isc::asiolink::IOAddress & | addr, |
const uint16_t | port ) |
Opens UDP/IP socket and binds to address specified.
This methods differs from openSocket in that it does not require the specification of the interface to which the socket will be bound.
addr | address to be bound |
port | UDP port |
isc::Unexpected | if failed to create and bind socket |
isc::BadValue | if specified address is not available on any interface |
Definition at line 992 of file iface_mgr.cc.
References ifaces_, isc_throw, and openSocket().
Referenced by openSocketFromRemoteAddress().
int isc::dhcp::IfaceMgr::openSocketFromIface | ( | const std::string & | ifname, |
const uint16_t | port, | ||
const uint8_t | family ) |
Opens UDP/IP socket and binds it to interface specified.
This method differs from openSocket in that it does not require the specification of a local address to which socket will be bound. Instead, the method searches through the addresses on the specified interface and selects one that matches the address family.
ifname | name of the interface |
port | UDP port |
family | address family (AF_INET or AF_INET6) |
isc::Unexpected | if failed to create and bind socket. |
isc::BadValue | if there is no address on specified interface that belongs to given family. |
Definition at line 951 of file iface_mgr.cc.
References ifaces_, isc_throw, and openSocket().
int isc::dhcp::IfaceMgr::openSocketFromRemoteAddress | ( | const isc::asiolink::IOAddress & | remote_addr, |
const uint16_t | port ) |
Opens UDP/IP socket to be used to connect to remote address.
This method identifies the local address to be used to connect to the remote address specified as argument. Once the local address is identified, openSocket is called to open a socket and bind it to the interface, address and port.
remote_addr | remote address to connect to |
port | UDP port |
isc::Unexpected | if failed to create and bind socket |
Definition at line 1015 of file iface_mgr.cc.
References isc_throw, openSocketFromAddress(), and isc::Exception::what().
bool isc::dhcp::IfaceMgr::openSockets4 | ( | const uint16_t | port = DHCP4_SERVER_PORT, |
const bool | use_bcast = true, | ||
IfaceMgrErrorMsgCallback | error_handler = 0, | ||
const bool | skip_opened = false ) |
Opens IPv4 sockets on detected interfaces.
This method opens sockets only on interfaces which have the inactive4_
field set to false (are active). If the interface is active but it is not running, it is down, or is a loopback interface when loopback is not allowed, an error is reported.
The type of the socket being open depends on the selected Packet Filter represented by a class derived from isc::dhcp::PktFilter
abstract class.
If sockets were successfully opened, it calls @ startDHCPReceiver to start the receiver thread (if packet queueing is enabled).
It is possible to specify whether sockets should be broadcast capable. In most of the cases, the sockets should support broadcast traffic, e.g. DHCPv4 server and relay need to listen to broadcast messages sent by clients. If the socket has to be open on the particular interface, this interface must have broadcast flag set. If this condition is not met, the socket will be created in the unicast-only mode. If there are multiple broadcast-capable interfaces present, they may be all open in a broadcast mode only if the OS supports SO_BINDTODEVICE (bind socket to a device) socket option. If this option is not supported, only the first broadcast-capable socket will be opened in the broadcast mode. The error will be reported for sockets being opened on other interfaces. If the socket is bound to a device (interface), the broadcast traffic sent to this interface will be received on this interface only. This allows the DHCPv4 server or relay to detect the interface on which the broadcast message has been received. This interface is later used to send a response.
On the systems with multiple interfaces, it is often desired that the failure to open a socket on a particular interface doesn't cause a fatal error and sockets should be opened on remaining interfaces. However, the warning about the failure for the particular socket should be communicated to the caller. The libdhcp++ is a common library with no logger associated with it. Most of the functions in this library communicate errors via exceptions. In case of openSockets4 function exception must not be thrown if the function is supposed to continue opening sockets, despite an error. Therefore, if such a behavior is desired, the error handler function can be passed as a parameter. This error handler is called (if present) with an error string. Typically, error handler will simply log an error using an application logger, but it can do more sophisticated error handling too.
If the error handler is not installed (is null), the exception is thrown for each failure (default behavior).
IfaceMgr
. Therefore a caller should call IfaceMgr::closeSockets()
before calling this function. If there are any sockets open, the function may either throw an exception or invoke an error handler on attempt to bind the new socket to the same address and port.port | specifies port number (usually DHCP4_SERVER_PORT) |
use_bcast | configure sockets to support broadcast messages. |
error_handler | a pointer to an error handler function which is called by the openSockets4 when it fails to open a socket. This parameter can be null to indicate that the callback should not be used. |
skip_opened | skip the addresses that already have the opened port |
SocketOpenFailure | if tried and failed to open socket and callback function hasn't been specified. |
Definition at line 497 of file iface_mgr.cc.
References collectBoundAddresses(), hasOpenSocket(), IFACEMGR_ERROR, ifaces_, isDirectResponseSupported(), openSocket(), startDHCPReceiver(), and isc::Exception::what().
bool isc::dhcp::IfaceMgr::openSockets6 | ( | const uint16_t | port = DHCP6_SERVER_PORT, |
IfaceMgrErrorMsgCallback | error_handler = 0, | ||
const bool | skip_opened = false ) |
Opens IPv6 sockets on detected interfaces.
This method opens sockets only on interfaces which have the inactive6_
field set to false (are active). If the interface is active but it is not running, it is down, or is a loopback interface when loopback is not allowed, an error is reported.
If sockets were successfully opened, it calls @ startDHCPReceiver to start the receiver thread (if packet queueing is enabled).
On the systems with multiple interfaces, it is often desired that the failure to open a socket on a particular interface doesn't cause a fatal error and sockets should be opened on remaining interfaces. However, the warning about the failure for the particular socket should be communicated to the caller. The libdhcp++ is a common library with no logger associated with it. Most of the functions in this library communicate errors via exceptions. In case of openSockets6 function exception must not be thrown if the function is supposed to continue opening sockets, despite an error. Therefore, if such a behavior is desired, the error handler function can be passed as a parameter. This error handler is called (if present) with an error string. Typically, error handler will simply log an error using an application logger, but it can do more sophisticated error handling too.
If the error handler is not installed (is null), the exception is thrown for each failure (default behavior).
IfaceMgr
. Therefore a caller should call IfaceMgr::closeSockets()
before calling this function. If there are any sockets open, the function may either throw an exception or invoke an error handler on attempt to bind the new socket to the same address and port.port | specifies port number (usually DHCP6_SERVER_PORT) |
error_handler | a pointer to an error handler function which is called by the openSockets6 when it fails to open a socket. This parameter can be null to indicate that the callback should not be used. |
skip_opened | skip the addresses that already have the opened port |
SocketOpenFailure | if tried and failed to open socket. |
Definition at line 627 of file iface_mgr.cc.
References hasOpenSocket(), IFACEMGR_ERROR, ifaces_, openSocket(), startDHCPReceiver(), and isc::Exception::what().
void isc::dhcp::IfaceMgr::printIfaces | ( | std::ostream & | out = std::cout | ) |
Debugging method that prints out all available interfaces.
out | specifies stream to print list of interfaces to |
Definition at line 788 of file iface_mgr.cc.
References ifaces_.
int isc::dhcp::IfaceMgr::purgeBadSockets | ( | ) |
Scans registered socket set and removes any that are invalid.
Walks the list of registered external sockets and tests each for validity. If any are found to be invalid they are removed. This is primarily a self-defense mechanism against hook libs or other users of external sockets that may leave a closed socket registered by mistake.
Definition at line 381 of file iface_mgr.cc.
Referenced by receive4Direct(), receive4Indirect(), receive6Direct(), and receive6Indirect().
Pkt4Ptr isc::dhcp::IfaceMgr::receive4 | ( | uint32_t | timeout_sec, |
uint32_t | timeout_usec = 0 ) |
Receive IPv4 packets or data from external sockets.
Wrapper around calls to either receive4Direct
or receive4Indirect
. The former is called when packet queueing is disabled, the latter when it is enabled.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
Definition at line 1125 of file iface_mgr.cc.
References isDHCPReceiverRunning(), receive4Direct(), and receive4Indirect().
|
protected |
Receive IPv4 packets directly or data from external sockets.
Attempts to receive a single DHCPv4 message over any of the open IPv4 sockets. If reception is successful and all information about its sender is obtained, an Pkt4 object is created and returned.
This method also checks if data arrived over registered external socket. This data may be of a different protocol family than AF_INET.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
isc::BadValue | if timeout_usec is greater than one million |
isc::dhcp::SocketReadError | if error occurred when receiving a packet. |
isc::dhcp::SignalInterruptOnSelect | when a call to select() is interrupted by a signal. |
Definition at line 1255 of file iface_mgr.cc.
References addFDtoSet(), ifaces_, isc_throw, and purgeBadSockets().
Referenced by receive4().
|
protected |
Receive IPv4 packets indirectly or data from external sockets.
Attempts to receive a single DHCPv4 message from the packet queue. The queue is populated by the receiver thread. If a packet is waiting in the queue, a Pkt4 returned.
This method also checks if data arrived over registered external socket. This data may be of a different protocol family than AF_INET.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
isc::BadValue | if timeout_usec is greater than one million |
isc::dhcp::SocketReadError | if error occurred when receiving a packet. |
isc::dhcp::SignalInterruptOnSelect | when a call to select() is interrupted by a signal. |
Definition at line 1133 of file iface_mgr.cc.
References addFDtoSet(), isc::util::WatchedThread::ERROR, getPacketQueue4(), isc_throw, purgeBadSockets(), and isc::util::WatchedThread::READY.
Referenced by receive4().
Pkt6Ptr isc::dhcp::IfaceMgr::receive6 | ( | uint32_t | timeout_sec, |
uint32_t | timeout_usec = 0 ) |
Receive IPv4 packets or data from external sockets.
Wrapper around calls to either receive4Direct
or receive4Indirect
. The former is called when packet queueing is disabled, the latter when it is enabled.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
Definition at line 1380 of file iface_mgr.cc.
References isDHCPReceiverRunning(), receive6Direct(), and receive6Indirect().
|
protected |
Receive IPv6 packets directly or data from external sockets.
Attempts to receive a single DHCPv6 message over any of the open IPv6 sockets. If reception is successful and all information about its sender is obtained, an Pkt6 object is created and returned.
This method also checks if data arrived over registered external socket. This data may be of a different protocol family than AF_INET.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
isc::BadValue | if timeout_usec is greater than one million |
isc::dhcp::SocketReadError | if error occurred when receiving a packet. |
isc::dhcp::SignalInterruptOnSelect | when a call to select() is interrupted by a signal. |
Definition at line 1401 of file iface_mgr.cc.
References addFDtoSet(), ifaces_, isc_throw, and purgeBadSockets().
Referenced by receive6().
|
protected |
Receive IPv6 packets indirectly or data from external sockets.
Attempts to receive a single DHCPv6 message from the packet queue. The queue is populated by the receiver thread. If a packet is waiting in the queue, a Pkt6 returned.
This method also checks if data arrived over registered external socket. This data may be of a different protocol family than AF_INET.
timeout_sec | specifies integral part of the timeout (in seconds) |
timeout_usec | specifies fractional part of the timeout (in microseconds) |
isc::BadValue | if timeout_usec is greater than one million |
isc::dhcp::SocketReadError | if error occurred when receiving a packet. |
isc::dhcp::SignalInterruptOnSelect | when a call to select() is interrupted by a signal. |
Definition at line 1523 of file iface_mgr.cc.
References addFDtoSet(), isc::util::WatchedThread::ERROR, getPacketQueue6(), isc_throw, purgeBadSockets(), and isc::util::WatchedThread::READY.
Referenced by receive6().
bool isc::dhcp::IfaceMgr::send | ( | const Pkt4Ptr & | pkt | ) |
Sends an IPv4 packet.
Sends an IPv4 packet. All parameters for actual transmission are specified in Pkt4 structure itself. That includes destination address, src/dst port and interface over which data will be sent.
pkt | a packet to be sent |
isc::BadValue | if invalid interface specified in the packet. |
isc::dhcp::SocketWriteError | if sendmsg() failed to send packet. |
Definition at line 1113 of file iface_mgr.cc.
References getIface(), getSocket(), and isc_throw.
bool isc::dhcp::IfaceMgr::send | ( | const Pkt6Ptr & | pkt | ) |
Sends an IPv6 packet.
Sends an IPv6 packet. All parameters for actual transmission are specified in Pkt6 structure itself. That includes destination address, src/dst port and interface over which data will be sent.
pkt | packet to be sent |
isc::BadValue | if invalid interface specified in the packet. |
isc::dhcp::SocketWriteError | if sendmsg() failed to send packet. |
Definition at line 1100 of file iface_mgr.cc.
References getIface(), getSocket(), and isc_throw.
|
inline |
Allows or disallows the loopback interface.
By default the loopback interface is not considered when opening sockets. This flag provides a way to relax this constraint.
Definition at line 741 of file iface_mgr.h.
|
inline |
Set a callback to perform operations before executing specific system calls.
cb | The callback used before executing specific system calls. |
Definition at line 813 of file iface_mgr.h.
void isc::dhcp::IfaceMgr::setMatchingPacketFilter | ( | const bool | direct_response_desired = false | ) |
Set Packet Filter object to handle send/receive packets.
This function sets Packet Filter object to be used by IfaceMgr, appropriate for the current OS. Setting the argument to 'true' indicates that function should set a packet filter class which supports direct responses to clients having no address assigned yet. Filters picked by this function will vary, depending on the OS being used. There is no guarantee that there is an implementation that supports this feature on a particular OS. If there isn't, the PktFilterInet object will be set. If the argument is set to 'false', PktFilterInet object instance will be set as the Packet Filter regardless of the OS type.
direct_response_desired | specifies whether the Packet Filter object being set should support direct traffic to the host not having address assigned. |
void isc::dhcp::IfaceMgr::setPacketFilter | ( | const PktFilter6Ptr & | packet_filter | ) |
Set packet filter object to handle sending and receiving DHCPv6 messages.
Packet filter objects provide means for the IfaceMgr
to open sockets for IPv6 packets reception and sending. This function sets the new instance of the packet filter which will be used by IfaceMgr
to send and receive DHCPv6 messages, until replaced by another packet filter.
It is required that DHCPv6 messages are send and received using methods of the same object that was used to open socket. Therefore, it is required that all IPv6 sockets are closed prior to calling this function. Call closeSockets(AF_INET6) to close all hanging IPv6 sockets opened by the current packet filter object.
packet_filter | a pointer to the new packet filter object to be used by IfaceMgr . |
isc::dhcp::InvalidPacketFilter | if specified object is null. |
isc::dhcp::PacketFilterChangeDenied | if there are open IPv6 sockets. |
Definition at line 429 of file iface_mgr.cc.
References hasOpenSocket(), and isc_throw.
void isc::dhcp::IfaceMgr::setPacketFilter | ( | const PktFilterPtr & | packet_filter | ) |
Set packet filter object to handle sending and receiving DHCPv4 messages.
Packet filter objects provide means for the IfaceMgr
to open sockets for IPv4 packets reception and sending. This function sets custom packet filter (represented by a class derived from PktFilter) to be used by IfaceMgr
. Note that there must be no IPv4 sockets open when this function is called. Call closeSockets(AF_INET) to close all hanging IPv4 sockets opened by the current packet filter object.
packet_filter | a pointer to the new packet filter object to be used by IfaceMgr . |
InvalidPacketFilter | if provided packet filter object is null. |
PacketFilterChangeDenied | if there are open IPv4 sockets. |
Definition at line 405 of file iface_mgr.cc.
References hasOpenSocket(), and isc_throw.
|
inline |
Sets or clears the test mode for IfaceMgr
.
Various unit test may set this flag to true, to indicate that the IfaceMgr
is in the test mode. There are places in the code that modify the behavior depending if the IfaceMgr
is in the test mode or not.
test_mode | A flag which indicates that the IfaceMgr is in the test mode (if true), or not (if false). |
Definition at line 725 of file iface_mgr.h.
void isc::dhcp::IfaceMgr::startDHCPReceiver | ( | const uint16_t | family | ) |
Starts DHCP packet receiver.
Starts the DHCP packet receiver thread for the given. protocol, AF_NET or AF_INET6, if the packet queue exists, otherwise it simply returns.
family | indicates which receiver to start, (AF_INET or AF_INET6) |
Unexpected | if the thread already exists. |
Definition at line 742 of file iface_mgr.cc.
References getPacketQueue4(), getPacketQueue6(), isc_throw, and isDHCPReceiverRunning().
Referenced by openSockets4(), and openSockets6().
void isc::dhcp::IfaceMgr::stopDHCPReceiver | ( | ) |
Stops the DHCP packet receiver.
If the thread exists, it is stopped, deleted, and the packet queue is flushed.
Definition at line 298 of file iface_mgr.cc.
References getPacketQueue4(), getPacketQueue6(), and isDHCPReceiverRunning().
Referenced by closeSockets().
|
protected |
Unordered set of IPv4 bound addresses.
Definition at line 1490 of file iface_mgr.h.
Referenced by clearBoundAddresses(), collectBoundAddresses(), and hasOpenSocket().
|
protected |
List of available interfaces.
Definition at line 1487 of file iface_mgr.h.
Referenced by addInterface(), clearIfaces(), clearUnicasts(), closeSockets(), collectBoundAddresses(), countIfaces(), getIface(), getIface(), getIfaces(), hasOpenSocket(), hasOpenSocket(), openSocketFromAddress(), openSocketFromIface(), openSockets4(), openSockets6(), printIfaces(), receive4Direct(), and receive6Direct().
|
static |
Packet reception buffer size.
RFC 8415 states that server responses may be fragmented if they are over MTU. There is no text whether client's packets may be larger than 1500. For now, we can assume that we don't support packets larger than 1500.
Definition at line 691 of file iface_mgr.h.
Referenced by isc::dhcp::PktFilterInet6::receive(), isc::dhcp::PktFilterInet::receive(), and isc::dhcp::PktFilterLPF::receive().