Kea 3.1.7
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
129class Iface : public boost::noncopyable {
130public:
131
133 static const unsigned int MAX_MAC_LEN = 20;
134
137
139 typedef std::list<Address> AddressCollection;
140
150 typedef std::list<SocketInfo> SocketCollection;
151
153 using ErrorBuffer = std::vector<std::string>;
154
162 Iface(const std::string& name, unsigned int ifindex);
163
165 ~Iface() = default;
166
168 void closeSockets();
169
189 void closeSockets(const uint16_t family);
190
194 std::string getFullName() const;
195
199 std::string getPlainMac() const;
200
205 void setMac(const uint8_t* mac, size_t macLen);
206
210 size_t getMacLen() const {
211 return (mac_len_);
212 }
213
218 const uint8_t* getMac() const {
219 return (mac_);
220 }
221
229 void setFlags(uint64_t flags);
230
234 unsigned int getIndex() const {
235 return (ifindex_);
236 }
237
241 std::string getName() const {
242 return (name_);
243 }
244
248 void setHWType(uint16_t type ) {
249 hardware_type_ = type;
250 }
251
255 uint16_t getHWType() const {
256 return (hardware_type_);
257 }
258
278 return (addrs_);
279 }
280
290 bool getAddress4(isc::asiolink::IOAddress& address) const;
291
296 bool hasAddress(const isc::asiolink::IOAddress& address) const;
297
304 void addAddress(const isc::asiolink::IOAddress& addr);
305
317 void setActive(const isc::asiolink::IOAddress& address, const bool active);
318
327 void setActive(const bool active);
328
330 unsigned int countActive4() const;
331
341 bool delAddress(const isc::asiolink::IOAddress& addr);
342
346 void addSocket(const SocketInfo& sock) {
347 sockets_.push_back(sock);
348 }
349
357 bool delSocket(uint16_t sockfd);
358
373 return (sockets_);
374 }
375
381 unicasts_.clear();
382 }
383
388 void addUnicast(const isc::asiolink::IOAddress& addr);
389
394 return (unicasts_);
395 }
396
406 uint8_t* getReadBuffer() {
407 if (read_buffer_.empty()) {
408 return (0);
409 }
410 return (&read_buffer_[0]);
411 }
412
414 size_t getReadBufferSize() const {
415 return (read_buffer_.size());
416 }
417
421 void resizeReadBuffer(const size_t new_size) {
422 read_buffer_.resize(new_size);
423 }
424
428 void addError(std::string const& message);
429
431 void clearErrors();
432
436 ErrorBuffer const& getErrors() const;
437
438protected:
441
443 std::string name_;
444
446 unsigned int ifindex_;
447
450
453
456
458 size_t mac_len_;
459
462
463public:
466
469
472
476
479
482
487 uint64_t flags_;
488
492
496
497private:
498
502 std::vector<uint8_t> read_buffer_;
503
510 ErrorBuffer errors_;
511};
512
514typedef boost::shared_ptr<Iface> IfacePtr;
515
518public:
519
529 typedef boost::multi_index_container<
530 // Container comprises elements of IfacePtr type.
531 IfacePtr,
532 // Here we start enumerating various indexes.
533 boost::multi_index::indexed_by<
534 // Sequenced index allows accessing elements in the same way
535 // as elements in std::list. Sequenced is the index #0.
536 boost::multi_index::sequenced<>,
537 // Start definition of index #1.
538 boost::multi_index::hashed_unique<
539 // Use the interface index as the key.
540 boost::multi_index::const_mem_fun<
541 Iface, unsigned int, &Iface::getIndex
542 >
543 >,
544 // Start definition of index #2.
545 boost::multi_index::hashed_unique<
546 // Use the interface name as the key.
547 boost::multi_index::const_mem_fun<
548 Iface, std::string, &Iface::getName
549 >
550 >
551 >
553
557 IfaceContainer::const_iterator begin() const {
558 return (ifaces_container_.begin());
559 }
560
564 IfaceContainer::const_iterator end() const {
565 return (ifaces_container_.end());
566 }
567
571 bool empty() const {
572 return (ifaces_container_.empty());
573 }
574
578 size_t size() const {
579 return (ifaces_container_.size());
580 }
581
583 void clear() {
584 cache_.reset();
585 ifaces_container_.clear();
586 }
587
593 void push_back(const IfacePtr& iface) {
594 ifaces_container_.push_back(iface);
595 }
596
601 IfacePtr getIface(const unsigned int ifindex);
602
607 IfacePtr getIface(const std::string& ifname);
608
609private:
615 IfacePtr getIfaceInternal(const unsigned int ifindex, const bool need_lock);
616
624 IfacePtr getIfaceInternal(const std::string& ifname, const bool need_lock);
625
628 std::mutex mutex_;
629
639 IfacePtr cache_;
640
642 IfaceContainer ifaces_container_;
643};
644
649typedef boost::multi_index_container<
652 // Here we start enumerating the only index.
653 boost::multi_index::indexed_by<
654 // Start definition of index #0.
655 boost::multi_index::hashed_unique<
656 // Use the address in its network order integer form as the key.
657 boost::multi_index::const_mem_fun<
659 >
660 >
661 >
663
665class IfaceMgr;
666
668typedef boost::shared_ptr<IfaceMgr> IfaceMgrPtr;
669
674typedef
675std::function<void(const std::string& errmsg)> IfaceMgrErrorMsgCallback;
676
683class IfaceMgr : public boost::noncopyable {
684public:
688 typedef std::function<void (int fd)> SocketCallback;
689
697 typedef std::function<bool (bool)> DetectCallback;
698
703
706
710
715 : socket_(socket), callback_(0), unusable_(false) {
716 }
717 };
718
723 typedef boost::multi_index_container<
724 // Container comprises elements of SocketCallbackInfo type.
725 SocketCallbackInfo,
726 // Here we start enumerating various indexes.
727 boost::multi_index::indexed_by<
728 // Sequenced index allows accessing elements in the same way
729 // as elements in std::list. Sequenced is the index #0.
730 boost::multi_index::sequenced<>,
731 // Use the file descriptor as the key for index #1.
732 boost::multi_index::hashed_unique<
733 boost::multi_index::member<
734 SocketCallbackInfo, int, &SocketCallbackInfo::socket_
735 >
736 >
737 >
739
741 typedef SocketCallbackInfoContainer::iterator SocketCallbackInfoIterator;
742
750 static const uint32_t RCVBUFSIZE = 1500;
751
756 static IfaceMgr& instance();
757
768 static const IfaceMgrPtr& instancePtr();
769
773 virtual ~IfaceMgr();
774
777
787 void setTestMode(const bool test_mode) {
788 test_mode_ = test_mode;
789 }
790
794 bool isTestMode() const {
795 return (test_mode_);
796 }
797
802 bool getCheckThreadId() const {
803 return (check_thread_id_);
804 }
805
811 void setCheckThreadId(const bool check) {
812 check_thread_id_ = check;
813 }
814
820 void setAllowLoopBack(const bool allow_loopback) {
821 allow_loopback_ = allow_loopback;
822 }
823
832 bool isDirectResponseSupported() const;
833
840 virtual bool isSocketReceivedTimeSupported() const;
841
849 IfacePtr getIface(const unsigned int ifindex);
850
857 IfacePtr getIface(const std::string& ifname);
858
869 IfacePtr getIface(const PktPtr& pkt);
870
879 return (ifaces_);
880 }
881
888 void clearIfaces();
889
895 detect_callback_ = cb;
896 }
897
906 bool checkDetectIfaces(bool update_only);
907
915 void detectIfaces(bool update_only = false);
916
918 void clearUnicasts();
919
921 void clearBoundAddresses();
922
925
939 uint16_t getSocket(const isc::dhcp::Pkt6Ptr& pkt);
940
955
959 void printIfaces(std::ostream& out = std::cout);
960
972 bool send(const Pkt6Ptr& pkt);
973
985 bool send(const Pkt4Ptr& pkt);
986
998 Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
999
1011 Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1012
1031 int openSocket(const std::string& ifname,
1032 const isc::asiolink::IOAddress& addr,
1033 const uint16_t port,
1034 const bool receive_bcast = false,
1035 const bool send_bcast = false);
1036
1054 int openSocketFromIface(const std::string& ifname,
1055 const uint16_t port,
1056 const uint8_t family);
1057
1073 const uint16_t port);
1074
1090 const uint16_t port);
1091
1138 bool openSockets6(const uint16_t port = DHCP6_SERVER_PORT,
1139 IfaceMgrErrorMsgCallback error_handler = 0,
1140 const bool skip_opened = false);
1141
1211 bool openSockets4(const uint16_t port = DHCP4_SERVER_PORT,
1212 const bool use_bcast = true,
1213 IfaceMgrErrorMsgCallback error_handler = 0,
1214 const bool skip_opened = false);
1215
1222 void closeSockets();
1223
1227 uint16_t countIfaces() {
1228 return (ifaces_.size());
1229 }
1230
1249 void addExternalSocket(int socketfd, SocketCallback callback);
1250
1254 bool isExternalSocket(int fd);
1255
1260 bool isExternalSocketUnusable(int fd);
1261
1268 void deleteExternalSocket(int socketfd);
1269
1277
1293 void setPacketFilter(const PktFilterPtr& packet_filter);
1294
1315 void setPacketFilter(const PktFilter6Ptr& packet_filter);
1316
1333 void setMatchingPacketFilter(const bool direct_response_desired = false);
1334
1341 void addInterface(const IfacePtr& iface);
1342
1349 bool hasOpenSocket(const uint16_t family) const;
1350
1368 bool hasOpenSocket(const isc::asiolink::IOAddress& addr, bool unicast = false) const;
1369
1374 return (packet_queue_mgr4_);
1375 }
1376
1384 return (packet_queue_mgr4_->getPacketQueue());
1385 }
1386
1391 return (packet_queue_mgr6_);
1392 }
1393
1401 return (packet_queue_mgr6_->getPacketQueue());
1402 }
1403
1414 void startDHCPReceiver(const uint16_t family);
1415
1420 void stopDHCPReceiver();
1421
1425 return (dhcp_receiver_ != 0 && dhcp_receiver_->isRunning());
1426 }
1427
1441 bool configureDHCPPacketQueue(const uint16_t family,
1442 data::ConstElementPtr queue_control);
1443
1444 // don't use private, we need derived classes in tests
1445protected:
1446
1451 IfaceMgr();
1452
1466 int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr,
1467 const uint16_t port, const bool receive_bcast = false,
1468 const bool send_bcast = false);
1469
1490 Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1491
1512 Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1513
1529 uint16_t port, const bool join_multicast);
1530
1551 Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1552
1573 Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1574
1576 std::thread::id id_;
1577
1580
1583
1584 // TODO: Also keep this interface on Iface once interface detection
1585 // is implemented. We may need it e.g. to close all sockets on
1586 // specific interface
1587 //int recvsock_; // TODO: should be fd_set eventually, but we have only
1588 //int sendsock_; // 2 sockets for now. Will do for until next release
1589
1590 // We can't use the same socket, as receiving socket
1591 // is bound to multicast address. And we all know what happens
1592 // to people who try to use multicast as source address.
1593
1594private:
1610 getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
1611 const uint16_t port);
1612
1632 bool openMulticastSocket(Iface& iface,
1633 const isc::asiolink::IOAddress& addr,
1634 const uint16_t port,
1635 IfaceMgrErrorMsgCallback error_handler = 0);
1636
1646 void receiveDHCP4Packets();
1647
1658 void receiveDHCP4Packet(Iface& iface, const SocketInfo& socket_info);
1659
1669 void receiveDHCP6Packets();
1670
1680 void receiveDHCP6Packet(const SocketInfo& socket_info);
1681
1685 void deleteExternalSocketInternal(int socketfd);
1686
1693 void handleClosedExternalSocket(SocketCallbackInfoIterator it);
1694
1696 void handleClosedExternalSockets();
1697
1702 void handleIfaceSocketError(const IfacePtr& iface, const SocketInfo& s);
1703
1712 PktFilterPtr packet_filter_;
1713
1718 PktFilter6Ptr packet_filter6_;
1719
1721 SocketCallbackInfoContainer callbacks_;
1722
1724 std::mutex callbacks_mutex_;
1725
1727 bool test_mode_;
1728
1730 std::atomic<bool> check_thread_id_;
1731
1737 DetectCallback detect_callback_;
1738
1740 bool allow_loopback_;
1741
1743 PacketQueueMgr4Ptr packet_queue_mgr4_;
1744
1746 PacketQueueMgr6Ptr packet_queue_mgr6_;
1747
1749 isc::util::WatchedThreadPtr dhcp_receiver_;
1750
1752 std::mutex receiver_mutex_;
1753
1755 util::FDEventHandlerPtr fd_event_handler_;
1756
1758 util::FDEventHandlerPtr receiver_fd_event_handler_;
1759};
1760
1761} // namespace isc::dhcp
1762} // namespace isc
1763
1764#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:517
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 > > > > IfaceContainer
Multi index container for network interfaces.
Definition iface_mgr.h:552
IfacePtr getIface(const unsigned int ifindex)
Lookup by interface index.
Definition iface_mgr.cc:825
IfaceContainer::const_iterator end() const
End iterator.
Definition iface_mgr.h:564
void clear()
Clear the collection.
Definition iface_mgr.h:583
IfaceContainer::const_iterator begin() const
Begin iterator.
Definition iface_mgr.h:557
size_t size() const
Return the number of interfaces.
Definition iface_mgr.h:578
void push_back(const IfacePtr &iface)
Adds an interface to the collection.
Definition iface_mgr.h:593
bool empty() const
Empty predicate.
Definition iface_mgr.h:571
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:683
void clearIfaces()
Removes detected interfaces.
Definition iface_mgr.cc:914
const IfaceCollection & getIfaces()
Returns container with all interfaces.
Definition iface_mgr.h:878
bool isExternalSocket(int fd)
Checks if socket's file description is registered.
Definition iface_mgr.cc:387
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:738
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition iface_mgr.cc:363
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:1390
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:510
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
Definition iface_mgr.h:688
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.
Definition iface_mgr.cc:989
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:801
uint16_t countIfaces()
Returns number of detected interfaces.
Definition iface_mgr.h:1227
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
Definition iface_mgr.cc:892
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:640
BoundAddresses bound_address_
Unordered set of IPv4 bound addresses.
Definition iface_mgr.h:1582
void setPacketFilter(const PktFilterPtr &packet_filter)
Set packet filter object to handle sending and receiving DHCPv4 messages.
Definition iface_mgr.cc:418
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:894
IfaceCollection ifaces_
List of available interfaces.
Definition iface_mgr.h:1579
void startDHCPReceiver(const uint16_t family)
Starts DHCP packet receiver.
Definition iface_mgr.cc:755
PacketQueueMgr4Ptr getPacketQueueMgr4()
Fetches the DHCPv4 packet queue manager.
Definition iface_mgr.h:1373
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:811
void clearUnicasts()
Clears unicast addresses on all interfaces.
Definition iface_mgr.cc:983
virtual bool isSocketReceivedTimeSupported() const
Check if the socket received time is supported.
Definition iface_mgr.cc:327
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:49
void initializeFDEventHandler()
Initialize the FD event handler;.
Definition iface_mgr.cc:211
bool isDHCPReceiverRunning() const
Returns true if there is a receiver exists and its thread is currently running.
Definition iface_mgr.h:1424
bool hasOpenSocket(const uint16_t family) const
Checks if there is at least one socket of the specified family open.
Definition iface_mgr.cc:460
virtual ~IfaceMgr()
Destructor.
Definition iface_mgr.cc:317
void collectBoundAddresses()
Collect the addresses all sockets are bound to.
Definition iface_mgr.cc:968
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:794
bool getCheckThreadId() const
Get the flag which indicates if thread ID is checked when performing operations with external sockets...
Definition iface_mgr.h:802
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.
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:741
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
Definition iface_mgr.cc:322
void clearBoundAddresses()
Clears the addresses all sockets are bound to.
Definition iface_mgr.cc:919
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
Definition iface_mgr.cc:332
void addInterface(const IfacePtr &iface)
Adds an interface to list of known interfaces.
Definition iface_mgr.cc:788
IfaceMgr()
Protected constructor.
Definition iface_mgr.cc:180
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:289
PacketQueue6Ptr getPacketQueue6()
Fetches the DHCPv6 receiver packet queue.
Definition iface_mgr.h:1400
bool isExternalSocketUnusable(int fd)
Checks if socket's file description is registered.
Definition iface_mgr.cc:395
static const IfaceMgrPtr & instancePtr()
Returns pointer to the sole instance of the interface manager.
Definition iface_mgr.cc:54
static const uint32_t RCVBUFSIZE
Packet reception buffer size.
Definition iface_mgr.h:750
void deleteAllExternalSockets()
Deletes all external sockets.
Definition iface_mgr.cc:407
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
void stopDHCPReceiver()
Stops the DHCP packet receiver.
Definition iface_mgr.cc:301
std::thread::id id_
Main thread ID.
Definition iface_mgr.h:1576
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:787
PacketQueue4Ptr getPacketQueue4()
Fetches the DHCPv4 receiver packet queue.
Definition iface_mgr.h:1383
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:820
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:697
IfaceNotFound(const char *file, size_t line, const char *what)
Definition iface_mgr.h:91
Represents a single network interface.
Definition iface_mgr.h:129
size_t getReadBufferSize() const
Returns the current size of the socket read buffer.
Definition iface_mgr.h:414
std::string getPlainMac() const
Returns link-layer address a plain text.
Definition iface_mgr.cc:126
size_t getMacLen() const
Returns MAC length.
Definition iface_mgr.h:210
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:487
bool inactive4_
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface.
Definition iface_mgr.h:491
size_t mac_len_
Length of link-layer address (usually 6).
Definition iface_mgr.h:458
void clearErrors()
Clears all errors.
AddressCollection addrs_
List of assigned addresses.
Definition iface_mgr.h:449
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:153
std::string getFullName() const
Returns full interface name as "ifname/ifindex" string.
Definition iface_mgr.cc:119
unsigned int ifindex_
Interface index (a value that uniquely identifies an interface).
Definition iface_mgr.h:446
std::string name_
Network interface name.
Definition iface_mgr.h:443
const AddressCollection & getUnicasts() const
Returns a container of addresses the server should listen on.
Definition iface_mgr.h:393
uint16_t hardware_type_
Hardware type.
Definition iface_mgr.h:461
SocketCollection sockets_
Socket used to send data.
Definition iface_mgr.h:440
const AddressCollection & getAddresses() const
Returns all addresses available on an interface.
Definition iface_mgr.h:277
uint8_t * getReadBuffer()
Returns the pointer to the buffer used for data reading.
Definition iface_mgr.h:406
bool flag_multicast_
Flag specifies if selected interface is multicast capable.
Definition iface_mgr.h:478
void setActive(const isc::asiolink::IOAddress &address, const bool active)
Activates or deactivates address for the interface.
Definition iface_mgr.cc:260
void addError(std::string const &message)
Add an error to the list of messages.
std::string getName() const
Returns interface name.
Definition iface_mgr.h:241
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:380
Iface(const std::string &name, unsigned int ifindex)
Iface constructor.
Definition iface_mgr.cc:59
uint16_t getHWType() const
Returns hardware type of the interface.
Definition iface_mgr.h:255
bool delAddress(const isc::asiolink::IOAddress &addr)
Deletes an address from an interface.
Definition iface_mgr.cc:153
unsigned int getIndex() const
Returns interface index.
Definition iface_mgr.h:234
bool hasAddress(const isc::asiolink::IOAddress &address) const
Check if the interface has the specified address assigned.
Definition iface_mgr.cc:243
void setMac(const uint8_t *mac, size_t macLen)
Sets MAC address of the interface.
Definition iface_mgr.cc:140
bool flag_running_
Flag specifies if selected interface is running (e.g.
Definition iface_mgr.h:475
~Iface()=default
Destructor.
void resizeReadBuffer(const size_t new_size)
Reallocates the socket read buffer.
Definition iface_mgr.h:421
bool delSocket(uint16_t sockfd)
Closes socket.
Definition iface_mgr.cc:163
std::list< Address > AddressCollection
Type that defines list of addresses.
Definition iface_mgr.h:139
const SocketCollection & getSockets() const
Returns collection of all sockets added to interface.
Definition iface_mgr.h:372
bool inactive6_
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface.
Definition iface_mgr.h:495
std::list< SocketInfo > SocketCollection
Type that holds a list of socket information.
Definition iface_mgr.h:150
static const unsigned int MAX_MAC_LEN
Maximum MAC address length (Infiniband uses 20 bytes)
Definition iface_mgr.h:133
bool flag_loopback_
Specifies if selected interface is loopback.
Definition iface_mgr.h:468
void addUnicast(const isc::asiolink::IOAddress &addr)
Adds unicast the server should listen on.
Definition iface_mgr.cc:216
unsigned int countActive4() const
Returns a number of activated IPv4 addresses on the interface.
Definition iface_mgr.cc:279
uint8_t mac_[MAX_MAC_LEN]
Link-layer address.
Definition iface_mgr.h:455
util::Optional< asiolink::IOAddress > Address
Address type.
Definition iface_mgr.h:136
void addAddress(const isc::asiolink::IOAddress &addr)
Adds an address to an interface.
Definition iface_mgr.cc:253
void closeSockets()
Closes all open sockets on interface.
Definition iface_mgr.cc:72
void addSocket(const SocketInfo &sock)
Adds socket descriptor to an interface.
Definition iface_mgr.h:346
bool flag_up_
Specifies if selected interface is up.
Definition iface_mgr.h:471
bool flag_broadcast_
Flag specifies if selected interface is broadcast capable.
Definition iface_mgr.h:481
void setHWType(uint16_t type)
Sets up hardware type of the interface.
Definition iface_mgr.h:248
const uint8_t * getMac() const
Returns pointer to MAC address.
Definition iface_mgr.h:218
AddressCollection unicasts_
List of unicast addresses the server should listen on.
Definition iface_mgr.h:452
bool getAddress4(isc::asiolink::IOAddress &address) const
Returns IPv4 address assigned to the interface.
Definition iface_mgr.cc:227
PacketFilterChangeDenied(const char *file, size_t line, const char *what)
Definition iface_mgr.h:53
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:36
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
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:668
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:514
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:662
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:675
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.
bool unusable_
Indicates if the socket can no longer be used for normal operations.
Definition iface_mgr.h:709
SocketCallback callback_
A callback that will be called when data arrives over socket_.
Definition iface_mgr.h:705
SocketCallbackInfo(int socket)
Constructor.
Definition iface_mgr.h:714
int socket_
Socket descriptor of the external socket.
Definition iface_mgr.h:702
Holds information about socket.
Definition socket_info.h:19
Defines the class, WatchSocket.