23 #include <boost/multi_index/hashed_index.hpp>
24 #include <boost/multi_index/mem_fun.hpp>
25 #include <boost/multi_index/sequenced_index.hpp>
26 #include <boost/multi_index_container.hpp>
27 #include <boost/noncopyable.hpp>
28 #include <boost/scoped_array.hpp>
29 #include <boost/shared_ptr.hpp>
118 class Iface :
public boost::noncopyable {
148 Iface(
const std::string& name,
unsigned int ifindex);
191 void setMac(
const uint8_t* mac,
size_t macLen);
377 if (read_buffer_.empty()) {
380 return (&read_buffer_[0]);
385 return (read_buffer_.size());
392 read_buffer_.resize(new_size);
459 std::vector<uint8_t> read_buffer_;
478 typedef boost::multi_index_container<
482 boost::multi_index::indexed_by<
485 boost::multi_index::sequenced<>,
487 boost::multi_index::hashed_unique<
489 boost::multi_index::const_mem_fun<
494 boost::multi_index::hashed_unique<
496 boost::multi_index::const_mem_fun<
506 IfaceContainer::const_iterator
begin()
const {
507 return (ifaces_container_.begin());
513 IfaceContainer::const_iterator
end()
const {
514 return (ifaces_container_.end());
521 return (ifaces_container_.empty());
528 return (ifaces_container_.size());
534 ifaces_container_.clear();
543 ifaces_container_.push_back(iface);
550 IfacePtr
getIface(uint32_t ifindex);
556 IfacePtr
getIface(
const std::string& ifname);
564 IfacePtr getIfaceInternal(uint32_t ifindex,
bool need_lock);
573 IfacePtr getIfaceInternal(
const std::string& ifname,
bool need_lock);
598 typedef boost::multi_index_container<
602 boost::multi_index::indexed_by<
604 boost::multi_index::hashed_unique<
606 boost::multi_index::const_mem_fun<
692 test_mode_ = test_mode;
708 allow_loopback_ = allow_loopback;
736 IfacePtr
getIface(
const std::string& ifname);
891 const bool receive_bcast =
false,
892 const bool send_bcast =
false);
913 const uint8_t family);
930 const uint16_t port);
947 const uint16_t port);
995 bool openSockets6(
const uint16_t port = DHCP6_SERVER_PORT,
996 IfaceMgrErrorMsgCallback error_handler = 0);
1066 bool openSockets4(
const uint16_t port = DHCP4_SERVER_PORT,
1067 const bool use_bcast =
true,
1068 IfaceMgrErrorMsgCallback error_handler = 0);
1201 return (packet_queue_mgr4_);
1211 return (packet_queue_mgr4_->getPacketQueue());
1218 return (packet_queue_mgr6_);
1228 return (packet_queue_mgr6_->getPacketQueue());
1252 return (dhcp_receiver_ != 0 && dhcp_receiver_->isRunning());
1278 static void addFDtoSet(
int fd,
int& maxfd, fd_set* sockets);
1303 const uint16_t port,
const bool receive_bcast =
false,
1304 const bool send_bcast =
false);
1365 uint16_t port,
const bool join_multicast);
1453 const uint16_t port);
1473 bool openMulticastSocket(
Iface& iface,
1475 const uint16_t port,
1476 IfaceMgrErrorMsgCallback error_handler = 0);
1487 void receiveDHCP4Packets();
1510 void receiveDHCP6Packets();
1521 void receiveDHCP6Packet(
const SocketInfo& socket_info);
1526 void deleteExternalSocketInternal(
int socketfd);
1545 SocketCallbackInfoContainer callbacks_;
1548 std::mutex callbacks_mutex_;
1554 bool allow_loopback_;
1569 #endif // IFACE_MGR_H
SocketCallback callback_
A callback that will be called when data arrives over socket_.
void clearIfaces()
Removes detected interfaces.
IfacePtr getIface(int ifindex)
Returns interface specified interface index.
int openSocketFromRemoteAddress(const isc::asiolink::IOAddress &remote_addr, const uint16_t port)
Opens UDP/IP socket to be used to connect to remote address.
Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets directly or data from external sockets.
void collectBoundAddresses()
Collect the addresses all sockets are bound to.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
void setAllowLoopBack(const bool allow_loopback)
Allows or disallows the loopback interface.
IfaceMgr exception thrown thrown when socket opening or configuration failed.
void setFlags(uint64_t flags)
Sets flag_*_ fields based on bitmask value returned by OS.
uint64_t flags_
Interface flags (this value is as is returned by OS, it may mean different things on different OSes)...
void addSocket(const SocketInfo &sock)
Adds socket descriptor to an interface.
IfaceMgr exception thrown when there is no suitable socket found.
void setPacketFilter(const PktFilterPtr &packet_filter)
Set packet filter object to handle sending and receiving DHCPv4 messages.
bool isDHCPReceiverRunning() const
Returns true if there is a receiver exists and its thread is currently running.
static const unsigned int MAX_MAC_LEN
Maximum MAC address length (Infiniband uses 20 bytes)
size_t mac_len_
Length of link-layer address (usually 6).
PacketQueueMgr4Ptr getPacketQueueMgr4()
Fetches the DHCPv4 packet queue manager.
Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets indirectly or data from external sockets.
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
bool delSocket(uint16_t sockfd)
Closes socket.
IfaceMgr exception thrown thrown when interface detection fails.
bool flag_up_
Specifies if selected interface is up.
Iface(const std::string &name, unsigned int ifindex)
Iface constructor.
bool empty() const
Empty predicate.
const AddressCollection & getUnicasts() const
Returns a container of addresses the server should listen on.
void addUnicast(const isc::asiolink::IOAddress &addr)
Adds unicast the server should listen on.
void setHWType(uint16_t type)
Sets up hardware type of the interface.
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
std::string getPlainMac() const
Returns link-layer address a plain text.
void startDHCPReceiver(const uint16_t family)
Starts DHCP packet receiver.
int ifindex_
Interface index (a value that uniquely identifies an interface).
bool flag_loopback_
Specifies if selected interface is loopback.
uint32_t getIndex() const
Returns interface index.
void setActive(const isc::asiolink::IOAddress &address, const bool active)
Activates or deactivates address for the interface.
std::list< SocketInfo > SocketCollection
Type that holds a list of socket information.
unsigned int countActive4() const
Returns a number of activated IPv4 addresses on the interface.
AddressCollection unicasts_
List of unicast addresses the server should listen on.
Collection of pointers to network interfaces.
int purgeBadSockets()
Scans registered socket set and removes any that are invalid.
bool openSockets6(const uint16_t port=DHCP6_SERVER_PORT, IfaceMgrErrorMsgCallback error_handler=0)
Opens IPv6 sockets on detected interfaces.
std::function< void(const std::string &errmsg)> IfaceMgrErrorMsgCallback
This type describes the callback function invoked when error occurs in the IfaceMgr.
PacketQueue6Ptr getPacketQueue6()
Fetches the DHCPv6 receiver packet queue.
bool hasAddress(const isc::asiolink::IOAddress &address) const
Check if the interface has the specified address assigned.
Handles network interfaces, transmission and reception.
size_t getMacLen() const
Returns MAC length.
Represents a single network interface.
SocketWriteError(const char *file, size_t line, const char *what)
int openSocketFromAddress(const isc::asiolink::IOAddress &addr, const uint16_t port)
Opens UDP/IP socket and binds to address specified.
boost::shared_ptr< PacketQueueMgr6 > PacketQueueMgr6Ptr
Defines a shared pointer to PacketQueueMgr6.
boost::shared_ptr< PktFilter > PktFilterPtr
Pointer to a PktFilter object.
boost::shared_ptr< PacketQueue< Pkt4Ptr > > PacketQueue4Ptr
Defines pointer to the DHCPv4 queue interface used at the application level.
const IfaceCollection & getIfaces()
Returns container with all interfaces.
const SocketCollection & getSockets() const
Returns collection of all sockets added to interface.
uint16_t getHWType() const
Returns hardware type of the interface.
Defines the class, WatchSocket.
bool send(const Pkt6Ptr &pkt)
Sends an IPv6 packet.
void closeSockets()
Closes all open sockets on interface.
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
Keeps callback information for external sockets.
boost::shared_ptr< WatchedThread > WatchedThreadPtr
Defines a pointer to a WatchedThread.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
bool inactive6_
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface...
bool flag_multicast_
Flag specifies if selected interface is multicast capable.
IfaceMgr exception thrown when there is no suitable interface.
void deleteExternalSocket(int socketfd)
Deletes external socket.
BoundAddresses bound_address_
Unordered set of IPv4 bound addresses.
static const IfaceMgrPtr & instancePtr()
Returns pointer to the sole instance of the interface manager.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
boost::shared_ptr< const Element > ConstElementPtr
void clearUnicasts()
Clears unicast addresses on all interfaces.
IfacePtr getIface(uint32_t ifindex)
Lookup by interface index.
void clearBoundAddresses()
Clears the addresses all sockets are bound to.
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, uint32_t,&Iface::getIndex > >, boost::multi_index::hashed_unique< boost::multi_index::const_mem_fun< Iface, std::string,&Iface::getName > > > > IfaceContainer
Multi index container for network interfaces.
boost::shared_ptr< PacketQueue< Pkt6Ptr > > PacketQueue6Ptr
Defines pointer to the DHCPv6 queue interface used at the application level.
IfaceMgr exception thrown thrown when error occurred during reading data from socket.
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
bool flag_broadcast_
Flag specifies if selected interface is broadcast capable.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
const uint8_t * getMac() const
Returns pointer to MAC address.
void deleteAllExternalSockets()
Deletes all external sockets.
bool getAddress4(isc::asiolink::IOAddress &address) const
Returns IPv4 address assigned to the interface.
void addInterface(const IfacePtr &iface)
Adds an interface to list of known interfaces.
std::string getName() const
Returns interface name.
PacketQueueMgr6Ptr getPacketQueueMgr6()
Fetches the DHCPv6 packet queue manager.
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
static const uint32_t RCVBUFSIZE
Packet reception buffer size.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
AddressCollection addrs_
List of assigned addresses.
bool inactive4_
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface...
std::list< Address > AddressCollection
Type that defines list of addresses.
IfaceNotFound(const char *file, size_t line, const char *what)
bool hasOpenSocket(const uint16_t family) const
Checks if there is at least one socket of the specified family open.
boost::shared_ptr< PktFilter6 > PktFilter6Ptr
Pointer to a PktFilter object.
std::string getFullName() const
Returns full interface name as "ifname/ifindex" string.
util::Optional< asiolink::IOAddress > Address
Address type.
SignalInterruptOnSelect(const char *file, size_t line, const char *what)
Exception thrown when a call to select is interrupted by a signal.
bool flag_running_
Flag specifies if selected interface is running (e.g.
PacketFilterChangeDenied(const char *file, size_t line, const char *what)
SocketConfigError(const char *file, size_t line, const char *what)
void closeSockets()
Closes all open sockets.
IfaceDetectError(const char *file, size_t line, const char *what)
static IfaceMgr & instance()
IfaceMgr is a singleton class.
void resizeReadBuffer(const size_t new_size)
Reallocates the socket read buffer.
SocketCollection sockets_
Socket used to send data.
bool delAddress(const isc::asiolink::IOAddress &addr)
Deletes an address from an interface.
SocketNotFound(const char *file, size_t line, const char *what)
uint8_t mac_[MAX_MAC_LEN]
Link-layer address.
uint16_t getSocket(const isc::dhcp::Pkt6Ptr &pkt)
Return most suitable socket for transmitting specified IPv6 packet.
void detectIfaces()
Detects network interfaces.
void addAddress(const isc::asiolink::IOAddress &addr)
Adds an address to an interface.
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.
virtual ~IfaceMgr()
Destructor.
int openSocketFromIface(const std::string &ifname, const uint16_t port, const uint8_t family)
Opens UDP/IP socket and binds it to interface specified.
static void addFDtoSet(int fd, int &maxfd, fd_set *sockets)
Convenience method for adding an descriptor to a set.
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.
PacketQueue4Ptr getPacketQueue4()
Fetches the DHCPv4 receiver packet queue.
boost::shared_ptr< PacketQueueMgr4 > PacketQueueMgr4Ptr
Defines a shared pointer to PacketQueueMgr4.
IfaceMgr()
Protected constructor.
void clearUnicasts()
Removes any unicast addresses.
Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
void setTestMode(const bool test_mode)
Sets or clears the test mode for IfaceMgr.
The IOAddress class represents an IP addresses (version agnostic)
IfaceContainer::const_iterator begin() const
Begin iterator.
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
int openSocket6(Iface &iface, const isc::asiolink::IOAddress &addr, uint16_t port, const bool join_multicast)
Opens IPv6 socket.
int socket_
Socket descriptor of the external socket.
void printIfaces(std::ostream &out=std::cout)
Debugging method that prints out all available interfaces.
bool isTestMode() const
Checks if the IfaceMgr is in the test mode.
uint8_t * getReadBuffer()
Returns the pointer to the buffer used for data reading.
Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets directly or data from external sockets.
std::string name_
Network interface name.
uint16_t countIfaces()
Returns number of detected interfaces.
uint16_t hardware_type_
Hardware type.
A template representing an optional value.
void stopDHCPReceiver()
Stops the DHCP packet receiver.
Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets indirectly or data from external sockets.
boost::shared_ptr< IfaceMgr > IfaceMgrPtr
Type definition for the pointer to the IfaceMgr.
const AddressCollection & getAddresses() const
Returns all addresses available on an interface.
IfaceContainer::const_iterator end() const
End iterator.
void stubDetectIfaces()
Stub implementation of network interface detection.
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.
bool openSockets4(const uint16_t port=DHCP4_SERVER_PORT, const bool use_bcast=true, IfaceMgrErrorMsgCallback error_handler=0)
Opens IPv4 sockets on detected interfaces.
size_t size() const
Return the number of interfaces.
IfaceMgr exception thrown thrown when error occurred during sending data through socket.
std::list< SocketCallbackInfo > SocketCallbackInfoContainer
Defines storage container for callbacks for external sockets.
Holds information about socket.
Exception thrown when it is not allowed to set new Packet Filter.
IfaceCollection ifaces_
List of available interfaces.
SocketReadError(const char *file, size_t line, const char *what)
bool configureDHCPPacketQueue(const uint16_t family, data::ConstElementPtr queue_control)
Configures DHCP packet queue.
void clear()
Clear the collection.
size_t getReadBufferSize() const
Returns the current size of the socket read buffer.
void push_back(const IfacePtr &iface)
Adds an interface to the collection.
void setMac(const uint8_t *mac, size_t macLen)
Sets MAC address of the interface.