Kea 3.2.0-git
iface_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2011-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef IFACE_MGR_H
8#define IFACE_MGR_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/dhcp4.h>
12#include <dhcp/dhcp6.h>
13#include <dhcp/pkt4.h>
14#include <dhcp/pkt6.h>
17#include <dhcp/pkt_filter.h>
18#include <dhcp/pkt_filter6.h>
20#include <util/optional.h>
21#include <util/watch_socket.h>
22#include <util/watched_thread.h>
23
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>
32
33#include <atomic>
34#include <functional>
35#include <list>
36#include <mutex>
37#include <thread>
38#include <vector>
39
40namespace isc {
41namespace dhcp {
42
45public:
46 IfaceDetectError(const char* file, size_t line, const char* what) :
47 isc::Exception(file, line, what) { }
48};
49
52public:
53 PacketFilterChangeDenied(const char* file, size_t line, const char* what) :
54 isc::Exception(file, line, what) { }
55};
56
59public:
60 SignalInterruptOnSelect(const char* file, size_t line, const char* what) :
61 isc::Exception(file, line, what) { }
62};
63
67public:
68 SocketConfigError(const char* file, size_t line, const char* what) :
69 isc::Exception(file, line, what) { }
70};
71
74class SocketReadError : public Exception {
75public:
76 SocketReadError(const char* file, size_t line, const char* what) :
77 isc::Exception(file, line, what) { }
78};
79
83public:
84 SocketWriteError(const char* file, size_t line, const char* what) :
85 isc::Exception(file, line, what) { }
86};
87
89class IfaceNotFound : public Exception {
90public:
91 IfaceNotFound(const char* file, size_t line, const char* what) :
92 isc::Exception(file, line, what) { }
93};
94
96class SocketNotFound : public Exception {
97public:
98 SocketNotFound(const char* file, size_t line, const char* what) :
99 isc::Exception(file, line, what) { }
100};
101
104class SocketFDError : public Exception {
105public:
106 SocketFDError(const char* file, size_t line, const char* what) :
107 isc::Exception(file, line, what) { }
108};
109
112public:
113 ReceiveTerminate(const char* file, size_t line, const char* what) :
114 isc::Exception(file, line, what) { }
115};
116
136class Iface : public isc::data::CfgToElement, public boost::noncopyable {
137public:
138
140 static const unsigned int MAX_MAC_LEN = 20;
141
144
146 typedef std::list<Address> AddressCollection;
147
157 typedef boost::multi_index_container<
158 // Container comprises elements of SocketInfo type.
160 // Here we start enumerating various indexes.
161 boost::multi_index::indexed_by<
162 // Sequenced index #0 in insertion order.
163 boost::multi_index::sequenced<>,
164 // Sequenced index #1 in Least Recently Used order.
165 boost::multi_index::sequenced<>
166 >
168
170 typedef SocketCollection::nth_index<1>::type SocketLruIndex;
171
173 typedef SocketLruIndex::iterator SocketLruIterator;
174
176 using ErrorBuffer = std::vector<std::string>;
177
185 Iface(const std::string& name, unsigned int ifindex);
186
188 ~Iface() = default;
189
191 void closeSockets();
192
212 void closeSockets(const uint16_t family);
213
217 std::string getFullName() const;
218
222 std::string getPlainMac() const;
223
228 void setMac(const uint8_t* mac, size_t macLen);
229
233 size_t getMacLen() const {
234 return (mac_len_);
235 }
236
241 const uint8_t* getMac() const {
242 return (mac_);
243 }
244
252 void setFlags(uint64_t flags);
253
257 unsigned int getIndex() const {
258 return (ifindex_);
259 }
260
264 std::string getName() const {
265 return (name_);
266 }
267
271 void setHWType(uint16_t type ) {
272 hardware_type_ = type;
273 }
274
278 uint16_t getHWType() const {
279 return (hardware_type_);
280 }
281
301 return (addrs_);
302 }
303
313 bool getAddress4(isc::asiolink::IOAddress& address) const;
314
319 bool hasAddress(const isc::asiolink::IOAddress& address) const;
320
327 void addAddress(const isc::asiolink::IOAddress& addr);
328
340 void setActive(const isc::asiolink::IOAddress& address, const bool active);
341
350 void setActive(const bool active);
351
353 unsigned int countActive4() const;
354
364 bool delAddress(const isc::asiolink::IOAddress& addr);
365
369 void addSocket(const SocketInfo& sock) {
370 sockets_.push_back(sock);
371 }
372
380 bool delSocket(uint16_t sockfd);
381
396 return (sockets_);
397 }
398
403 return (sockets_);
404 }
405
411 unicasts_.clear();
412 }
413
418 void addUnicast(const isc::asiolink::IOAddress& addr);
419
424 return (unicasts_);
425 }
426
436 uint8_t* getReadBuffer() {
437 if (read_buffer_.empty()) {
438 return (0);
439 }
440 return (&read_buffer_[0]);
441 }
442
444 size_t getReadBufferSize() const {
445 return (read_buffer_.size());
446 }
447
451 void resizeReadBuffer(const size_t new_size) {
452 read_buffer_.resize(new_size);
453 }
454
458 void addError(std::string const& message);
459
461 void clearErrors();
462
466 ErrorBuffer const& getErrors() const;
467
477 virtual isc::data::ElementPtr toElement() const;
478
479protected:
482
484 std::string name_;
485
487 unsigned int ifindex_;
488
491
494
497
499 size_t mac_len_;
500
503
504public:
507
510
513
517
520
523
528 uint64_t flags_;
529
533
537
538private:
539
543 std::vector<uint8_t> read_buffer_;
544
551 ErrorBuffer errors_;
552};
553
555typedef boost::shared_ptr<Iface> IfacePtr;
556
559public:
560
570 typedef boost::multi_index_container<
571 // Container comprises elements of IfacePtr type.
572 IfacePtr,
573 // Here we start enumerating various indexes.
574 boost::multi_index::indexed_by<
575 // Sequenced index allows accessing elements in the same way
576 // as elements in std::list. Sequenced is the index #0.
577 boost::multi_index::sequenced<>,
578 // Start definition of index #1.
579 boost::multi_index::hashed_unique<
580 // Use the interface index as the key.
581 boost::multi_index::const_mem_fun<
582 Iface, unsigned int, &Iface::getIndex
583 >
584 >,
585 // Start definition of index #2.
586 boost::multi_index::hashed_unique<
587 // Use the interface name as the key.
588 boost::multi_index::const_mem_fun<
589 Iface, std::string, &Iface::getName
590 >
591 >,
592 // Start definition of index #3.
593 // Sequence in Least Recently Used order.
594 boost::multi_index::sequenced<>
595 >
597
601 IfaceContainer::const_iterator begin() const {
602 return (ifaces_container_.begin());
603 }
604
608 IfaceContainer::const_iterator end() const {
609 return (ifaces_container_.end());
610 }
611
613 typedef IfaceContainer::nth_index<3>::type LruIndex;
614
619 return (ifaces_container_.get<3>());
620 }
621
623 typedef LruIndex::iterator LruIterator;
624
628 bool empty() const {
629 return (ifaces_container_.empty());
630 }
631
635 size_t size() const {
636 return (ifaces_container_.size());
637 }
638
640 void clear() {
641 cache_.reset();
642 ifaces_container_.clear();
643 }
644
650 void push_back(const IfacePtr& iface) {
651 ifaces_container_.push_back(iface);
652 }
653
658 IfacePtr getIface(const unsigned int ifindex);
659
664 IfacePtr getIface(const std::string& ifname);
665
666private:
672 IfacePtr getIfaceInternal(const unsigned int ifindex, const bool need_lock);
673
681 IfacePtr getIfaceInternal(const std::string& ifname, const bool need_lock);
682
685 std::mutex mutex_;
686
696 IfacePtr cache_;
697
699 IfaceContainer ifaces_container_;
700};
701
706typedef boost::multi_index_container<
709 // Here we start enumerating the only index.
710 boost::multi_index::indexed_by<
711 // Start definition of index #0.
712 boost::multi_index::hashed_unique<
713 // Use the address in its network order integer form as the key.
714 boost::multi_index::const_mem_fun<
716 >
717 >
718 >
720
722class IfaceMgr;
723
725typedef boost::shared_ptr<IfaceMgr> IfaceMgrPtr;
726
731typedef
732std::function<void(const std::string& errmsg)> IfaceMgrErrorMsgCallback;
733
740class IfaceMgr : public boost::noncopyable {
741public:
745 typedef std::function<void (int fd)> SocketCallback;
746
754 typedef std::function<bool (bool)> DetectCallback;
755
760
763
767
772 : socket_(socket), callback_(0), unusable_(false) {
773 }
774 };
775
780 typedef boost::multi_index_container<
781 // Container comprises elements of SocketCallbackInfo type.
782 SocketCallbackInfo,
783 // Here we start enumerating various indexes.
784 boost::multi_index::indexed_by<
785 // Sequenced index allows accessing elements in the same way
786 // as elements in std::list. Sequenced is the index #0.
787 boost::multi_index::sequenced<>,
788 // Use the file descriptor as the key for index #1.
789 boost::multi_index::hashed_unique<
790 boost::multi_index::member<
791 SocketCallbackInfo, int, &SocketCallbackInfo::socket_
792 >
793 >
794 >
796
798 typedef SocketCallbackInfoContainer::iterator SocketCallbackInfoIterator;
799
807 static const uint32_t RCVBUFSIZE = 1500;
808
813 static IfaceMgr& instance();
814
825 static const IfaceMgrPtr& instancePtr();
826
830 virtual ~IfaceMgr();
831
834
844 void setTestMode(const bool test_mode) {
845 test_mode_ = test_mode;
846 }
847
851 bool isTestMode() const {
852 return (test_mode_);
853 }
854
859 bool getCheckThreadId() const {
860 return (check_thread_id_);
861 }
862
868 void setCheckThreadId(const bool check) {
869 check_thread_id_ = check;
870 }
871
875 bool isMainThread() const {
876 return (std::this_thread::get_id() == id_);
877 }
878
884 void setAllowLoopBack(const bool allow_loopback) {
885 allow_loopback_ = allow_loopback;
886 }
887
896 bool isDirectResponseSupported() const;
897
904 virtual bool isSocketReceivedTimeSupported() const;
905
913 IfacePtr getIface(const unsigned int ifindex);
914
921 IfacePtr getIface(const std::string& ifname);
922
933 IfacePtr getIface(const PktPtr& pkt);
934
943 return (ifaces_);
944 }
945
952 void clearIfaces();
953
959 detect_callback_ = cb;
960 }
961
970 bool checkDetectIfaces(bool update_only);
971
979 void detectIfaces(bool update_only = false);
980
982 void clearUnicasts();
983
985 void clearBoundAddresses();
986
989
1003 uint16_t getSocket(const isc::dhcp::Pkt6Ptr& pkt);
1004
1019
1023 void printIfaces(std::ostream& out = std::cout);
1024
1036 bool send(const Pkt6Ptr& pkt);
1037
1049 bool send(const Pkt4Ptr& pkt);
1050
1062 Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1063
1075 Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1076
1095 int openSocket(const std::string& ifname,
1096 const isc::asiolink::IOAddress& addr,
1097 const uint16_t port,
1098 const bool receive_bcast = false,
1099 const bool send_bcast = false);
1100
1118 int openSocketFromIface(const std::string& ifname,
1119 const uint16_t port,
1120 const uint8_t family);
1121
1137 const uint16_t port);
1138
1154 const uint16_t port);
1155
1202 bool openSockets6(const uint16_t port = DHCP6_SERVER_PORT,
1203 IfaceMgrErrorMsgCallback error_handler = 0,
1204 const bool skip_opened = false);
1205
1275 bool openSockets4(const uint16_t port = DHCP4_SERVER_PORT,
1276 const bool use_bcast = true,
1277 IfaceMgrErrorMsgCallback error_handler = 0,
1278 const bool skip_opened = false);
1279
1286 void closeSockets();
1287
1291 uint16_t countIfaces() {
1292 return (ifaces_.size());
1293 }
1294
1313 void addExternalSocket(int socketfd, SocketCallback callback);
1314
1318 bool isExternalSocket(int fd);
1319
1324 bool isExternalSocketUnusable(int fd);
1325
1332 void deleteExternalSocket(int socketfd);
1333
1341
1345 std::list<int> getAllExternalSockets();
1346
1362 void setPacketFilter(const PktFilterPtr& packet_filter);
1363
1384 void setPacketFilter(const PktFilter6Ptr& packet_filter);
1385
1402 void setMatchingPacketFilter(const bool direct_response_desired = false);
1403
1410 void addInterface(const IfacePtr& iface);
1411
1418 bool hasOpenSocket(const uint16_t family) const;
1419
1437 bool hasOpenSocket(const isc::asiolink::IOAddress& addr, bool unicast = false) const;
1438
1443 return (packet_queue_mgr4_);
1444 }
1445
1453 return (packet_queue_mgr4_->getPacketQueue());
1454 }
1455
1460 return (packet_queue_mgr6_);
1461 }
1462
1470 return (packet_queue_mgr6_->getPacketQueue());
1471 }
1472
1483 void startDHCPReceiver(const uint16_t family);
1484
1492 void stopDHCPReceiver(bool clear_queue = true);
1493
1497 return (dhcp_receiver_ != 0 && dhcp_receiver_->isRunning());
1498 }
1499
1513 bool configureDHCPPacketQueue(const uint16_t family,
1514 data::ConstElementPtr queue_control);
1515
1517 void setFamily(uint16_t family) {
1518 family_ = family == AF_INET ? AF_INET : AF_INET6;
1519 }
1520
1522 uint16_t getFamily() const {
1523 return (family_);
1524 }
1525
1530
1531 // don't use private, we need derived classes in tests
1532protected:
1533
1538 IfaceMgr();
1539
1553 int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr,
1554 const uint16_t port, const bool receive_bcast = false,
1555 const bool send_bcast = false);
1556
1577 Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1578
1599 Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1600
1616 uint16_t port, const bool join_multicast);
1617
1638 Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1639
1660 Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1661
1663 std::thread::id id_;
1664
1667
1670
1671 // TODO: Also keep this interface on Iface once interface detection
1672 // is implemented. We may need it e.g. to close all sockets on
1673 // specific interface
1674 //int recvsock_; // TODO: should be fd_set eventually, but we have only
1675 //int sendsock_; // 2 sockets for now. Will do for until next release
1676
1677 // We can't use the same socket, as receiving socket
1678 // is bound to multicast address. And we all know what happens
1679 // to people who try to use multicast as source address.
1680
1681private:
1697 getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
1698 const uint16_t port);
1699
1719 bool openMulticastSocket(Iface& iface,
1720 const isc::asiolink::IOAddress& addr,
1721 const uint16_t port,
1722 IfaceMgrErrorMsgCallback error_handler = 0);
1723
1733 void receiveDHCP4Packets();
1734
1738 void scanReceiveDHCP4Packets();
1739
1750 void receiveDHCP4Packet(Iface& iface, const SocketInfo& socket_info);
1751
1761 void receiveDHCP6Packets();
1762
1766 void scanReceiveDHCP6Packets();
1767
1777 void receiveDHCP6Packet(const SocketInfo& socket_info);
1778
1782 void deleteExternalSocketInternal(int socketfd);
1783
1790 void handleClosedExternalSocket(SocketCallbackInfoIterator it);
1791
1793 void handleClosedExternalSockets();
1794
1799 void handleIfaceSocketError(const IfacePtr& iface, const SocketInfo& s);
1800
1809 PktFilterPtr packet_filter_;
1810
1815 PktFilter6Ptr packet_filter6_;
1816
1818 SocketCallbackInfoContainer callbacks_;
1819
1821 std::mutex callbacks_mutex_;
1822
1824 bool test_mode_;
1825
1827 std::atomic<bool> check_thread_id_;
1828
1834 DetectCallback detect_callback_;
1835
1837 bool allow_loopback_;
1838
1840 PacketQueueMgr4Ptr packet_queue_mgr4_;
1841
1843 PacketQueueMgr6Ptr packet_queue_mgr6_;
1844
1846 isc::util::WatchedThreadPtr dhcp_receiver_;
1847
1849 std::mutex receiver_mutex_;
1850
1852 util::FDEventHandlerPtr fd_event_handler_;
1853
1855 util::FDEventHandlerPtr receiver_fd_event_handler_;
1856
1858 uint16_t family_;
1859};
1860
1867class ReceiverCriticalSection : public boost::noncopyable {
1868public:
1869
1875 : is_running_(IfaceMgr::instance().isDHCPReceiverRunning()) {
1876 if (is_running_) {
1878 }
1879 }
1880
1886 if (is_running_ && !IfaceMgr::instance().isDHCPReceiverRunning()) {
1887 auto family = IfaceMgr::instance().getFamily();
1889 }
1890 }
1891
1892private:
1894 bool is_running_;
1895};
1896
1897} // namespace isc::dhcp
1898} // namespace isc
1899
1900#endif // IFACE_MGR_H
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.
Collection of pointers to network interfaces.
Definition iface_mgr.h:558
IfacePtr getIface(const unsigned int ifindex)
Lookup by interface index.
Definition iface_mgr.cc:870
IfaceContainer::const_iterator end() const
End iterator.
Definition iface_mgr.h:608
void clear()
Clear the collection.
Definition iface_mgr.h:640
IfaceContainer::const_iterator begin() const
Begin iterator.
Definition iface_mgr.h:601
LruIndex & getLru()
LRU index.
Definition iface_mgr.h:618
IfaceContainer::nth_index< 3 >::type LruIndex
Type of LRU index.
Definition iface_mgr.h:613
size_t size() const
Return the number of interfaces.
Definition iface_mgr.h:635
void push_back(const IfacePtr &iface)
Adds an interface to the collection.
Definition iface_mgr.h:650
bool empty() const
Empty predicate.
Definition iface_mgr.h:628
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.
Definition iface_mgr.h:596
LruIndex::iterator LruIterator
Type of LRU iterator.
Definition iface_mgr.h:623
IfaceDetectError(const char *file, size_t line, const char *what)
Definition iface_mgr.h:46
Handles network interfaces, transmission and reception.
Definition iface_mgr.h:740
void clearIfaces()
Removes detected interfaces.
Definition iface_mgr.cc:959
const IfaceCollection & getIfaces()
Returns container with all interfaces.
Definition iface_mgr.h:942
bool isExternalSocket(int fd)
Checks if socket's file description is registered.
Definition iface_mgr.cc:422
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.
Definition iface_mgr.h:795
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition iface_mgr.cc:398
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.
Definition iface_mgr.h:1459
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.
Definition iface_mgr.cc:555
void stopDHCPReceiver(bool clear_queue=true)
Stops the DHCP packet receiver.
Definition iface_mgr.cc:325
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
Definition iface_mgr.h:745
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.
Definition iface_mgr.cc:846
uint16_t countIfaces()
Returns number of detected interfaces.
Definition iface_mgr.h:1291
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
Definition iface_mgr.cc:937
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.
Definition iface_mgr.cc:685
BoundAddresses bound_address_
Unordered set of IPv4 bound addresses.
Definition iface_mgr.h:1669
void setPacketFilter(const PktFilterPtr &packet_filter)
Set packet filter object to handle sending and receiving DHCPv4 messages.
Definition iface_mgr.cc:463
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.
Definition iface_mgr.h:958
IfaceCollection ifaces_
List of available interfaces.
Definition iface_mgr.h:1666
void startDHCPReceiver(const uint16_t family)
Starts DHCP packet receiver.
Definition iface_mgr.cc:800
bool isMainThread() const
Check if called from the main thread.
Definition iface_mgr.h:875
PacketQueueMgr4Ptr getPacketQueueMgr4()
Fetches the DHCPv4 packet queue manager.
Definition iface_mgr.h:1442
void setCheckThreadId(const bool check)
Set the flag which indicates if thread ID is checked when performing operations with external sockets...
Definition iface_mgr.h:868
void setFamily(uint16_t family)
Sets address family (AF_INET or AF_INET6).
Definition iface_mgr.h:1517
void clearUnicasts()
Clears unicast addresses on all interfaces.
virtual bool isSocketReceivedTimeSupported() const
Check if the socket received time is supported.
Definition iface_mgr.cc:362
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:52
void initializeFDEventHandler()
Initialize the FD event handler;.
Definition iface_mgr.cc:213
bool isDHCPReceiverRunning() const
Returns true if there is a receiver exists and its thread is currently running.
Definition iface_mgr.h:1496
bool hasOpenSocket(const uint16_t family) const
Checks if there is at least one socket of the specified family open.
Definition iface_mgr.cc:505
virtual ~IfaceMgr()
Destructor.
Definition iface_mgr.cc:343
void collectBoundAddresses()
Collect the addresses all sockets are bound to.
isc::data::ElementPtr ifacesToElement() const
Unparses detected interface list.
Definition iface_mgr.cc:348
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.
Definition iface_mgr.h:851
uint16_t getFamily() const
Returns address family.
Definition iface_mgr.h:1522
bool getCheckThreadId() const
Get the flag which indicates if thread ID is checked when performing operations with external sockets...
Definition iface_mgr.h:859
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.
Definition iface_mgr.cc:453
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.
Definition iface_mgr.h:798
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
Definition iface_mgr.cc:357
void clearBoundAddresses()
Clears the addresses all sockets are bound to.
Definition iface_mgr.cc:964
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
Definition iface_mgr.cc:367
void addInterface(const IfacePtr &iface)
Adds an interface to list of known interfaces.
Definition iface_mgr.cc:833
IfaceMgr()
Protected constructor.
Definition iface_mgr.cc:181
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.
Definition iface_mgr.cc:313
PacketQueue6Ptr getPacketQueue6()
Fetches the DHCPv6 receiver packet queue.
Definition iface_mgr.h:1469
bool isExternalSocketUnusable(int fd)
Checks if socket's file description is registered.
Definition iface_mgr.cc:430
static const IfaceMgrPtr & instancePtr()
Returns pointer to the sole instance of the interface manager.
Definition iface_mgr.cc:57
static const uint32_t RCVBUFSIZE
Packet reception buffer size.
Definition iface_mgr.h:807
void deleteAllExternalSockets()
Deletes all external sockets.
Definition iface_mgr.cc:442
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
std::thread::id id_
Main thread ID.
Definition iface_mgr.h:1663
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.
Definition iface_mgr.h:844
PacketQueue4Ptr getPacketQueue4()
Fetches the DHCPv4 receiver packet queue.
Definition iface_mgr.h:1452
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.
Definition iface_mgr.h:884
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.
Definition iface_mgr.h:754
IfaceNotFound(const char *file, size_t line, const char *what)
Definition iface_mgr.h:91
Represents a single network interface.
Definition iface_mgr.h:136
size_t getReadBufferSize() const
Returns the current size of the socket read buffer.
Definition iface_mgr.h:444
std::string getPlainMac() const
Returns link-layer address a plain text.
Definition iface_mgr.cc:127
size_t getMacLen() const
Returns MAC length.
Definition iface_mgr.h:233
uint64_t flags_
Interface flags (this value is as is returned by OS, it may mean different things on different OSes).
Definition iface_mgr.h:528
bool inactive4_
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface.
Definition iface_mgr.h:532
size_t mac_len_
Length of link-layer address (usually 6).
Definition iface_mgr.h:499
void clearErrors()
Clears all errors.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition iface_mgr.cc:292
AddressCollection addrs_
List of assigned addresses.
Definition iface_mgr.h:490
ErrorBuffer const & getErrors() const
Get the consistent list of error messages.
std::vector< std::string > ErrorBuffer
Type definition for a list of error messages.
Definition iface_mgr.h:176
std::string getFullName() const
Returns full interface name as "ifname/ifindex" string.
Definition iface_mgr.cc:120
unsigned int ifindex_
Interface index (a value that uniquely identifies an interface).
Definition iface_mgr.h:487
std::string name_
Network interface name.
Definition iface_mgr.h:484
const AddressCollection & getUnicasts() const
Returns a container of addresses the server should listen on.
Definition iface_mgr.h:423
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.
Definition iface_mgr.h:167
uint16_t hardware_type_
Hardware type.
Definition iface_mgr.h:502
SocketCollection sockets_
Socket used to send data.
Definition iface_mgr.h:481
const AddressCollection & getAddresses() const
Returns all addresses available on an interface.
Definition iface_mgr.h:300
uint8_t * getReadBuffer()
Returns the pointer to the buffer used for data reading.
Definition iface_mgr.h:436
bool flag_multicast_
Flag specifies if selected interface is multicast capable.
Definition iface_mgr.h:519
void setActive(const isc::asiolink::IOAddress &address, const bool active)
Activates or deactivates address for the interface.
Definition iface_mgr.cc:262
void addError(std::string const &message)
Add an error to the list of messages.
SocketLruIndex::iterator SocketLruIterator
SocketInfo LRU iterator type.
Definition iface_mgr.h:173
std::string getName() const
Returns interface name.
Definition iface_mgr.h:264
void setFlags(uint64_t flags)
Sets flag_*_ fields based on bitmask value returned by OS.
void clearUnicasts()
Removes any unicast addresses.
Definition iface_mgr.h:410
Iface(const std::string &name, unsigned int ifindex)
Iface constructor.
Definition iface_mgr.cc:62
uint16_t getHWType() const
Returns hardware type of the interface.
Definition iface_mgr.h:278
bool delAddress(const isc::asiolink::IOAddress &addr)
Deletes an address from an interface.
Definition iface_mgr.cc:154
unsigned int getIndex() const
Returns interface index.
Definition iface_mgr.h:257
bool hasAddress(const isc::asiolink::IOAddress &address) const
Check if the interface has the specified address assigned.
Definition iface_mgr.cc:245
SocketCollection::nth_index< 1 >::type SocketLruIndex
SocketInfo LRU index type.
Definition iface_mgr.h:170
void setMac(const uint8_t *mac, size_t macLen)
Sets MAC address of the interface.
Definition iface_mgr.cc:141
bool flag_running_
Flag specifies if selected interface is running (e.g.
Definition iface_mgr.h:516
~Iface()=default
Destructor.
void resizeReadBuffer(const size_t new_size)
Reallocates the socket read buffer.
Definition iface_mgr.h:451
SocketCollection & getSocketsRef()
Returns collection of all sockets added to interface.
Definition iface_mgr.h:402
bool delSocket(uint16_t sockfd)
Closes socket.
Definition iface_mgr.cc:164
std::list< Address > AddressCollection
Type that defines list of addresses.
Definition iface_mgr.h:146
const SocketCollection & getSockets() const
Returns collection of all sockets added to interface.
Definition iface_mgr.h:395
bool inactive6_
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface.
Definition iface_mgr.h:536
static const unsigned int MAX_MAC_LEN
Maximum MAC address length (Infiniband uses 20 bytes).
Definition iface_mgr.h:140
bool flag_loopback_
Specifies if selected interface is loopback.
Definition iface_mgr.h:509
void addUnicast(const isc::asiolink::IOAddress &addr)
Adds unicast the server should listen on.
Definition iface_mgr.cc:218
unsigned int countActive4() const
Returns a number of activated IPv4 addresses on the interface.
Definition iface_mgr.cc:281
uint8_t mac_[MAX_MAC_LEN]
Link-layer address.
Definition iface_mgr.h:496
util::Optional< asiolink::IOAddress > Address
Address type.
Definition iface_mgr.h:143
void addAddress(const isc::asiolink::IOAddress &addr)
Adds an address to an interface.
Definition iface_mgr.cc:255
void closeSockets()
Closes all open sockets on interface.
Definition iface_mgr.cc:75
void addSocket(const SocketInfo &sock)
Adds socket descriptor to an interface.
Definition iface_mgr.h:369
bool flag_up_
Specifies if selected interface is up.
Definition iface_mgr.h:512
bool flag_broadcast_
Flag specifies if selected interface is broadcast capable.
Definition iface_mgr.h:522
void setHWType(uint16_t type)
Sets up hardware type of the interface.
Definition iface_mgr.h:271
const uint8_t * getMac() const
Returns pointer to MAC address.
Definition iface_mgr.h:241
AddressCollection unicasts_
List of unicast addresses the server should listen on.
Definition iface_mgr.h:493
bool getAddress4(isc::asiolink::IOAddress &address) const
Returns IPv4 address assigned to the interface.
Definition iface_mgr.cc:229
PacketFilterChangeDenied(const char *file, size_t line, const char *what)
Definition iface_mgr.h:53
ReceiveTerminate(const char *file, size_t line, const char *what)
Definition iface_mgr.h:113
SignalInterruptOnSelect(const char *file, size_t line, const char *what)
Definition iface_mgr.h:60
SocketConfigError(const char *file, size_t line, const char *what)
Definition iface_mgr.h:68
SocketFDError(const char *file, size_t line, const char *what)
Definition iface_mgr.h:106
SocketNotFound(const char *file, size_t line, const char *what)
Definition iface_mgr.h:98
SocketReadError(const char *file, size_t line, const char *what)
Definition iface_mgr.h:76
SocketWriteError(const char *file, size_t line, const char *what)
Definition iface_mgr.h:84
A template representing an optional value.
Definition optional.h:37
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:999
boost::shared_ptr< IfaceMgr > IfaceMgrPtr
Type definition for the pointer to the IfaceMgr.
Definition iface_mgr.h:725
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.
Definition pkt4.h:556
boost::shared_ptr< PktFilter > PktFilterPtr
Pointer to a PktFilter object.
Definition pkt_filter.h:144
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
Definition iface_mgr.h:555
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.
Definition iface_mgr.h:719
std::function< void(const std::string &errmsg)> IfaceMgrErrorMsgCallback
This type describes the callback function invoked when error occurs in the IfaceMgr.
Definition iface_mgr.h:732
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.
Definition pkt6.h:31
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.
Definition iface_mgr.h:766
SocketCallback callback_
A callback that will be called when data arrives over socket_.
Definition iface_mgr.h:762
SocketCallbackInfo(int socket)
Constructor.
Definition iface_mgr.h:771
int socket_
Socket descriptor of the external socket.
Definition iface_mgr.h:759
Holds information about socket.
Definition socket_info.h:18
Defines the class, WatchSocket.