24#include <boost/multi_index/hashed_index.hpp>
25#include <boost/multi_index/member.hpp>
26#include <boost/multi_index/mem_fun.hpp>
27#include <boost/multi_index/sequenced_index.hpp>
28#include <boost/multi_index_container.hpp>
29#include <boost/noncopyable.hpp>
30#include <boost/scoped_array.hpp>
31#include <boost/shared_ptr.hpp>
157 typedef boost::multi_index_container<
161 boost::multi_index::indexed_by<
163 boost::multi_index::sequenced<>,
165 boost::multi_index::sequenced<>
185 Iface(
const std::string& name,
unsigned int ifindex);
228 void setMac(
const uint8_t* mac,
size_t macLen);
437 if (read_buffer_.empty()) {
440 return (&read_buffer_[0]);
445 return (read_buffer_.size());
452 read_buffer_.resize(new_size);
458 void addError(std::string
const& message);
543 std::vector<uint8_t> read_buffer_;
570 typedef boost::multi_index_container<
574 boost::multi_index::indexed_by<
577 boost::multi_index::sequenced<>,
579 boost::multi_index::hashed_unique<
581 boost::multi_index::const_mem_fun<
586 boost::multi_index::hashed_unique<
588 boost::multi_index::const_mem_fun<
594 boost::multi_index::sequenced<>
601 IfaceContainer::const_iterator
begin()
const {
602 return (ifaces_container_.begin());
608 IfaceContainer::const_iterator
end()
const {
609 return (ifaces_container_.end());
613 typedef IfaceContainer::nth_index<3>::type
LruIndex;
619 return (ifaces_container_.get<3>());
629 return (ifaces_container_.empty());
636 return (ifaces_container_.size());
642 ifaces_container_.clear();
651 ifaces_container_.push_back(iface);
672 IfacePtr getIfaceInternal(
const unsigned int ifindex,
const bool need_lock);
681 IfacePtr getIfaceInternal(
const std::string& ifname,
const bool need_lock);
706typedef boost::multi_index_container<
710 boost::multi_index::indexed_by<
712 boost::multi_index::hashed_unique<
714 boost::multi_index::const_mem_fun<
780 typedef boost::multi_index_container<
784 boost::multi_index::indexed_by<
787 boost::multi_index::sequenced<>,
789 boost::multi_index::hashed_unique<
790 boost::multi_index::member<
845 test_mode_ = test_mode;
860 return (check_thread_id_);
869 check_thread_id_ = check;
876 return (std::this_thread::get_id() ==
id_);
885 allow_loopback_ = allow_loopback;
959 detect_callback_ =
cb;
1097 const uint16_t port,
1098 const bool receive_bcast =
false,
1099 const bool send_bcast =
false);
1119 const uint16_t port,
1120 const uint8_t family);
1137 const uint16_t port);
1154 const uint16_t port);
1202 bool openSockets6(
const uint16_t port = DHCP6_SERVER_PORT,
1204 const bool skip_opened =
false);
1275 bool openSockets4(
const uint16_t port = DHCP4_SERVER_PORT,
1276 const bool use_bcast =
true,
1278 const bool skip_opened =
false);
1443 return (packet_queue_mgr4_);
1453 return (packet_queue_mgr4_->getPacketQueue());
1460 return (packet_queue_mgr6_);
1470 return (packet_queue_mgr6_->getPacketQueue());
1497 return (dhcp_receiver_ != 0 && dhcp_receiver_->isRunning());
1518 family_ = family == AF_INET ? AF_INET : AF_INET6;
1554 const uint16_t port,
const bool receive_bcast =
false,
1555 const bool send_bcast =
false);
1616 uint16_t port,
const bool join_multicast);
1698 const uint16_t port);
1719 bool openMulticastSocket(
Iface& iface,
1721 const uint16_t port,
1733 void receiveDHCP4Packets();
1738 void scanReceiveDHCP4Packets();
1761 void receiveDHCP6Packets();
1766 void scanReceiveDHCP6Packets();
1777 void receiveDHCP6Packet(
const SocketInfo& socket_info);
1782 void deleteExternalSocketInternal(
int socketfd);
1793 void handleClosedExternalSockets();
1821 std::mutex callbacks_mutex_;
1827 std::atomic<bool> check_thread_id_;
1837 bool allow_loopback_;
1849 std::mutex receiver_mutex_;
1875 : is_running_(
IfaceMgr::instance().isDHCPReceiverRunning()) {
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The IOAddress class represents an IP addresses (version agnostic).
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
Collection of pointers to network interfaces.
IfacePtr getIface(const unsigned int ifindex)
Lookup by interface index.
IfaceContainer::const_iterator end() const
End iterator.
void clear()
Clear the collection.
IfaceContainer::const_iterator begin() const
Begin iterator.
LruIndex & getLru()
LRU index.
IfaceContainer::nth_index< 3 >::type LruIndex
Type of LRU index.
size_t size() const
Return the number of interfaces.
void push_back(const IfacePtr &iface)
Adds an interface to the collection.
bool empty() const
Empty predicate.
boost::multi_index_container< IfacePtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique< boost::multi_index::const_mem_fun< Iface, unsigned int, &Iface::getIndex > >, boost::multi_index::hashed_unique< boost::multi_index::const_mem_fun< Iface, std::string, &Iface::getName > >, boost::multi_index::sequenced<> > > IfaceContainer
Multi index container for network interfaces.
LruIndex::iterator LruIterator
Type of LRU iterator.
IfaceDetectError(const char *file, size_t line, const char *what)
Handles network interfaces, transmission and reception.
void clearIfaces()
Removes detected interfaces.
const IfaceCollection & getIfaces()
Returns container with all interfaces.
bool isExternalSocket(int fd)
Checks if socket's file description is registered.
boost::multi_index_container< SocketCallbackInfo, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique< boost::multi_index::member< SocketCallbackInfo, int, &SocketCallbackInfo::socket_ > > > > SocketCallbackInfoContainer
Defines storage container for callbacks for external sockets.
void deleteExternalSocket(int socketfd)
Deletes external socket.
Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets indirectly or data from external sockets.
PacketQueueMgr6Ptr getPacketQueueMgr6()
Fetches the DHCPv6 packet queue manager.
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.
void stopDHCPReceiver(bool clear_queue=true)
Stops the DHCP packet receiver.
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
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.
void printIfaces(std::ostream &out=std::cout)
Debugging method that prints out all available interfaces.
uint16_t countIfaces()
Returns number of detected interfaces.
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
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.
bool openSockets6(const uint16_t port=DHCP6_SERVER_PORT, IfaceMgrErrorMsgCallback error_handler=0, const bool skip_opened=false)
Opens IPv6 sockets on detected interfaces.
BoundAddresses bound_address_
Unordered set of IPv4 bound addresses.
void setPacketFilter(const PktFilterPtr &packet_filter)
Set packet filter object to handle sending and receiving DHCPv4 messages.
int openSocketFromIface(const std::string &ifname, const uint16_t port, const uint8_t family)
Opens UDP/IP socket and binds it to interface specified.
Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
void detectIfaces(bool update_only=false)
Detects network interfaces.
void setDetectCallback(const DetectCallback &cb)
Set a callback to perform operations before executing specific system calls.
IfaceCollection ifaces_
List of available interfaces.
void startDHCPReceiver(const uint16_t family)
Starts DHCP packet receiver.
bool isMainThread() const
Check if called from the main thread.
PacketQueueMgr4Ptr getPacketQueueMgr4()
Fetches the DHCPv4 packet queue manager.
void setCheckThreadId(const bool check)
Set the flag which indicates if thread ID is checked when performing operations with external sockets...
void setFamily(uint16_t family)
Sets address family (AF_INET or AF_INET6).
void clearUnicasts()
Clears unicast addresses on all interfaces.
virtual bool isSocketReceivedTimeSupported() const
Check if the socket received time is supported.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
void initializeFDEventHandler()
Initialize the FD event handler;.
bool isDHCPReceiverRunning() const
Returns true if there is a receiver exists and its thread is currently running.
bool hasOpenSocket(const uint16_t family) const
Checks if there is at least one socket of the specified family open.
virtual ~IfaceMgr()
Destructor.
void collectBoundAddresses()
Collect the addresses all sockets are bound to.
isc::data::ElementPtr ifacesToElement() const
Unparses detected interface list.
int openSocket6(Iface &iface, const isc::asiolink::IOAddress &addr, uint16_t port, const bool join_multicast)
Opens IPv6 socket.
bool isTestMode() const
Checks if the IfaceMgr is in the test mode.
uint16_t getFamily() const
Returns address family.
bool getCheckThreadId() const
Get the flag which indicates if thread ID is checked when performing operations with external sockets...
bool configureDHCPPacketQueue(const uint16_t family, data::ConstElementPtr queue_control)
Configures DHCP packet queue.
int openSocketFromRemoteAddress(const isc::asiolink::IOAddress &remote_addr, const uint16_t port)
Opens UDP/IP socket to be used to connect to remote address.
std::list< int > getAllExternalSockets()
Get the list of external sockets.
Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets directly or data from external sockets.
bool checkDetectIfaces(bool update_only)
Check if the specific system calls used to detect interfaces should be executed.
SocketCallbackInfoContainer::iterator SocketCallbackInfoIterator
SocketCallbackInfo iterator type.
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
void clearBoundAddresses()
Clears the addresses all sockets are bound to.
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.
IfaceMgr()
Protected constructor.
bool send(const Pkt6Ptr &pkt)
Sends an IPv6 packet.
Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets indirectly or data from external sockets.
void closeSockets()
Closes all open sockets.
PacketQueue6Ptr getPacketQueue6()
Fetches the DHCPv6 receiver packet queue.
bool isExternalSocketUnusable(int fd)
Checks if socket's file description is registered.
static const IfaceMgrPtr & instancePtr()
Returns pointer to the sole instance of the interface manager.
static const uint32_t RCVBUFSIZE
Packet reception buffer size.
void deleteAllExternalSockets()
Deletes all external sockets.
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
std::thread::id id_
Main thread ID.
Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets directly or data from external sockets.
void setTestMode(const bool test_mode)
Sets or clears the test mode for IfaceMgr.
PacketQueue4Ptr getPacketQueue4()
Fetches the DHCPv4 receiver packet queue.
uint16_t getSocket(const isc::dhcp::Pkt6Ptr &pkt)
Return most suitable socket for transmitting specified IPv6 packet.
void setAllowLoopBack(const bool allow_loopback)
Allows or disallows the loopback interface.
Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
std::function< bool(bool)> DetectCallback
Defines callback used when detecting interfaces.
IfaceNotFound(const char *file, size_t line, const char *what)
Represents a single network interface.
size_t getReadBufferSize() const
Returns the current size of the socket read buffer.
std::string getPlainMac() const
Returns link-layer address a plain text.
size_t getMacLen() const
Returns MAC length.
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.
size_t mac_len_
Length of link-layer address (usually 6).
void clearErrors()
Clears all errors.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
AddressCollection addrs_
List of assigned addresses.
ErrorBuffer const & getErrors() const
Get the consistent list of error messages.
std::vector< std::string > ErrorBuffer
Type definition for a list of error messages.
std::string getFullName() const
Returns full interface name as "ifname/ifindex" string.
unsigned int ifindex_
Interface index (a value that uniquely identifies an interface).
std::string name_
Network interface name.
const AddressCollection & getUnicasts() const
Returns a container of addresses the server should listen on.
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.
uint16_t hardware_type_
Hardware type.
SocketCollection sockets_
Socket used to send data.
const AddressCollection & getAddresses() const
Returns all addresses available on an interface.
uint8_t * getReadBuffer()
Returns the pointer to the buffer used for data reading.
bool flag_multicast_
Flag specifies if selected interface is multicast capable.
void setActive(const isc::asiolink::IOAddress &address, const bool active)
Activates or deactivates address for the interface.
void addError(std::string const &message)
Add an error to the list of messages.
SocketLruIndex::iterator SocketLruIterator
SocketInfo LRU iterator type.
std::string getName() const
Returns interface name.
void setFlags(uint64_t flags)
Sets flag_*_ fields based on bitmask value returned by OS.
void clearUnicasts()
Removes any unicast addresses.
Iface(const std::string &name, unsigned int ifindex)
Iface constructor.
uint16_t getHWType() const
Returns hardware type of the interface.
bool delAddress(const isc::asiolink::IOAddress &addr)
Deletes an address from an interface.
unsigned int getIndex() const
Returns interface index.
bool hasAddress(const isc::asiolink::IOAddress &address) const
Check if the interface has the specified address assigned.
SocketCollection::nth_index< 1 >::type SocketLruIndex
SocketInfo LRU index type.
void setMac(const uint8_t *mac, size_t macLen)
Sets MAC address of the interface.
bool flag_running_
Flag specifies if selected interface is running (e.g.
~Iface()=default
Destructor.
void resizeReadBuffer(const size_t new_size)
Reallocates the socket read buffer.
SocketCollection & getSocketsRef()
Returns collection of all sockets added to interface.
bool delSocket(uint16_t sockfd)
Closes socket.
std::list< Address > AddressCollection
Type that defines list of addresses.
const SocketCollection & getSockets() const
Returns collection of all sockets added to interface.
bool inactive6_
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface.
static const unsigned int MAX_MAC_LEN
Maximum MAC address length (Infiniband uses 20 bytes).
bool flag_loopback_
Specifies if selected interface is loopback.
void addUnicast(const isc::asiolink::IOAddress &addr)
Adds unicast the server should listen on.
unsigned int countActive4() const
Returns a number of activated IPv4 addresses on the interface.
uint8_t mac_[MAX_MAC_LEN]
Link-layer address.
util::Optional< asiolink::IOAddress > Address
Address type.
void addAddress(const isc::asiolink::IOAddress &addr)
Adds an address to an interface.
void closeSockets()
Closes all open sockets on interface.
void addSocket(const SocketInfo &sock)
Adds socket descriptor to an interface.
bool flag_up_
Specifies if selected interface is up.
bool flag_broadcast_
Flag specifies if selected interface is broadcast capable.
void setHWType(uint16_t type)
Sets up hardware type of the interface.
const uint8_t * getMac() const
Returns pointer to MAC address.
AddressCollection unicasts_
List of unicast addresses the server should listen on.
bool getAddress4(isc::asiolink::IOAddress &address) const
Returns IPv4 address assigned to the interface.
PacketFilterChangeDenied(const char *file, size_t line, const char *what)
ReceiveTerminate(const char *file, size_t line, const char *what)
ReceiverCriticalSection()
Constructor.
~ReceiverCriticalSection()
Destructor.
SignalInterruptOnSelect(const char *file, size_t line, const char *what)
SocketConfigError(const char *file, size_t line, const char *what)
SocketFDError(const char *file, size_t line, const char *what)
SocketNotFound(const char *file, size_t line, const char *what)
SocketReadError(const char *file, size_t line, const char *what)
SocketWriteError(const char *file, size_t line, const char *what)
A template representing an optional value.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
boost::shared_ptr< IfaceMgr > IfaceMgrPtr
Type definition for the pointer to the IfaceMgr.
boost::shared_ptr< PacketQueue< Pkt4Ptr > > PacketQueue4Ptr
Defines pointer to the DHCPv4 queue interface used at the application level.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
boost::shared_ptr< PktFilter > PktFilterPtr
Pointer to a PktFilter object.
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
boost::multi_index_container< asiolink::IOAddress, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::const_mem_fun< asiolink::IOAddress, uint32_t, &asiolink::IOAddress::toUint32 > > > > BoundAddresses
Type definition for the unordered set of IPv4 bound addresses.
std::function< void(const std::string &errmsg)> IfaceMgrErrorMsgCallback
This type describes the callback function invoked when error occurs in the IfaceMgr.
boost::shared_ptr< PacketQueue< Pkt6Ptr > > PacketQueue6Ptr
Defines pointer to the DHCPv6 queue interface used at the application level.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
boost::shared_ptr< PktFilter6 > PktFilter6Ptr
Pointer to a PktFilter object.
boost::shared_ptr< PacketQueueMgr4 > PacketQueueMgr4Ptr
Defines a shared pointer to PacketQueueMgr4.
boost::shared_ptr< PacketQueueMgr6 > PacketQueueMgr6Ptr
Defines a shared pointer to PacketQueueMgr6.
boost::shared_ptr< WatchedThread > WatchedThreadPtr
Defines a pointer to a WatchedThread.
boost::shared_ptr< FDEventHandler > FDEventHandlerPtr
Shared pointer to an FD event handler.
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
bool unusable_
Indicates if the socket can no longer be used for normal operations.
SocketCallback callback_
A callback that will be called when data arrives over socket_.
SocketCallbackInfo(int socket)
Constructor.
int socket_
Socket descriptor of the external socket.
Holds information about socket.
Defines the class, WatchSocket.