Kea  1.9.9-git
iface_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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>
15 #include <dhcp/packet_queue_mgr4.h>
16 #include <dhcp/packet_queue_mgr6.h>
17 #include <dhcp/pkt_filter.h>
18 #include <dhcp/pkt_filter6.h>
19 #include <util/optional.h>
20 #include <util/watch_socket.h>
21 #include <util/watched_thread.h>
22 
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>
30 
31 #include <functional>
32 #include <list>
33 #include <vector>
34 #include <mutex>
35 
36 namespace isc {
37 
38 namespace dhcp {
39 
41 class IfaceDetectError : public Exception {
42 public:
43  IfaceDetectError(const char* file, size_t line, const char* what) :
44  isc::Exception(file, line, what) { };
45 };
46 
49 public:
50  PacketFilterChangeDenied(const char* file, size_t line, const char* what) :
51  isc::Exception(file, line, what) { };
52 };
53 
56 public:
57  SignalInterruptOnSelect(const char* file, size_t line, const char* what) :
58  isc::Exception(file, line, what) { };
59 };
60 
63 class SocketConfigError : public Exception {
64 public:
65  SocketConfigError(const char* file, size_t line, const char* what) :
66  isc::Exception(file, line, what) { };
67 };
68 
71 class SocketReadError : public Exception {
72 public:
73  SocketReadError(const char* file, size_t line, const char* what) :
74  isc::Exception(file, line, what) { };
75 };
76 
79 class SocketWriteError : public Exception {
80 public:
81  SocketWriteError(const char* file, size_t line, const char* what) :
82  isc::Exception(file, line, what) { };
83 };
84 
86 class IfaceNotFound : public Exception {
87 public:
88  IfaceNotFound(const char* file, size_t line, const char* what) :
89  isc::Exception(file, line, what) { };
90 };
91 
93 class SocketNotFound : public Exception {
94 public:
95  SocketNotFound(const char* file, size_t line, const char* what) :
96  isc::Exception(file, line, what) { };
97 };
98 
118 class Iface : public boost::noncopyable {
119 public:
120 
122  static const unsigned int MAX_MAC_LEN = 20;
123 
126 
128  typedef std::list<Address> AddressCollection;
129 
139  typedef std::list<SocketInfo> SocketCollection;
140 
148  Iface(const std::string& name, unsigned int ifindex);
149 
151  ~Iface() { }
152 
154  void closeSockets();
155 
175  void closeSockets(const uint16_t family);
176 
180  std::string getFullName() const;
181 
185  std::string getPlainMac() const;
186 
191  void setMac(const uint8_t* mac, size_t macLen);
192 
196  size_t getMacLen() const { return mac_len_; }
197 
202  const uint8_t* getMac() const { return mac_; }
203 
211  void setFlags(uint64_t flags);
212 
216  uint32_t getIndex() const { return ifindex_; }
217 
221  std::string getName() const { return name_; };
222 
226  void setHWType(uint16_t type ) { hardware_type_ = type; }
227 
231  uint16_t getHWType() const { return hardware_type_; }
232 
251  const AddressCollection& getAddresses() const { return addrs_; }
252 
262  bool getAddress4(isc::asiolink::IOAddress& address) const;
263 
268  bool hasAddress(const isc::asiolink::IOAddress& address) const;
269 
276  void addAddress(const isc::asiolink::IOAddress& addr);
277 
289  void setActive(const isc::asiolink::IOAddress& address, const bool active);
290 
299  void setActive(const bool active);
300 
302  unsigned int countActive4() const;
303 
313  bool delAddress(const isc::asiolink::IOAddress& addr);
314 
318  void addSocket(const SocketInfo& sock) {
319  sockets_.push_back(sock);
320  }
321 
329  bool delSocket(uint16_t sockfd);
330 
344  const SocketCollection& getSockets() const { return sockets_; }
345 
350  void clearUnicasts() {
351  unicasts_.clear();
352  }
353 
358  void addUnicast(const isc::asiolink::IOAddress& addr);
359 
363  const AddressCollection& getUnicasts() const {
364  return unicasts_;
365  }
366 
376  uint8_t* getReadBuffer() {
377  if (read_buffer_.empty()) {
378  return (0);
379  }
380  return (&read_buffer_[0]);
381  }
382 
384  size_t getReadBufferSize() const {
385  return (read_buffer_.size());
386  }
387 
391  void resizeReadBuffer(const size_t new_size) {
392  read_buffer_.resize(new_size);
393  }
394 
395 protected:
397  SocketCollection sockets_;
398 
400  std::string name_;
401 
403  int ifindex_;
404 
406  AddressCollection addrs_;
407 
409  AddressCollection unicasts_;
410 
412  uint8_t mac_[MAX_MAC_LEN];
413 
415  size_t mac_len_;
416 
418  uint16_t hardware_type_;
419 
420 public:
423 
426 
428  bool flag_up_;
429 
433 
436 
439 
444  uint64_t flags_;
445 
449 
453 
454 private:
455 
459  std::vector<uint8_t> read_buffer_;
460 };
461 
463 typedef boost::shared_ptr<Iface> IfacePtr;
464 
467 public:
468 
478  typedef boost::multi_index_container<
479  // Container comprises elements of IfacePtr type.
480  IfacePtr,
481  // Here we start enumerating various indexes.
482  boost::multi_index::indexed_by<
483  // Sequenced index allows accessing elements in the same way
484  // as elements in std::list. Sequenced is the index #0.
485  boost::multi_index::sequenced<>,
486  // Start definition of index #1.
487  boost::multi_index::hashed_unique<
488  // Use the interface index as the key.
489  boost::multi_index::const_mem_fun<
490  Iface, uint32_t, &Iface::getIndex
491  >
492  >,
493  // Start definition of index #2.
494  boost::multi_index::hashed_unique<
495  // Use the interface name as the key.
496  boost::multi_index::const_mem_fun<
497  Iface, std::string, &Iface::getName
498  >
499  >
500  >
502 
506  IfaceContainer::const_iterator begin() const {
507  return (ifaces_container_.begin());
508  }
509 
513  IfaceContainer::const_iterator end() const {
514  return (ifaces_container_.end());
515  }
516 
520  bool empty() const {
521  return (ifaces_container_.empty());
522  }
523 
527  size_t size() const {
528  return (ifaces_container_.size());
529  }
530 
532  void clear() {
533  cache_.reset();
534  ifaces_container_.clear();
535  }
536 
542  void push_back(const IfacePtr& iface) {
543  ifaces_container_.push_back(iface);
544  }
545 
550  IfacePtr getIface(uint32_t ifindex);
551 
556  IfacePtr getIface(const std::string& ifname);
557 
558 private:
564  IfacePtr getIfaceInternal(uint32_t ifindex, bool need_lock);
565 
573  IfacePtr getIfaceInternal(const std::string& ifname, bool need_lock);
574 
577  std::mutex mutex_;
578 
588  IfacePtr cache_;
589 
591  IfaceContainer ifaces_container_;
592 };
593 
598 typedef boost::multi_index_container<
601  // Here we start enumerating the only index.
602  boost::multi_index::indexed_by<
603  // Start definition of index #0.
604  boost::multi_index::hashed_unique<
605  // Use the address in its network order integer form as the key.
606  boost::multi_index::const_mem_fun<
607  asiolink::IOAddress, uint32_t, &asiolink::IOAddress::toUint32
608  >
609  >
610  >
612 
614 class IfaceMgr;
615 
617 typedef boost::shared_ptr<IfaceMgr> IfaceMgrPtr;
618 
623 typedef
624 std::function<void(const std::string& errmsg)> IfaceMgrErrorMsgCallback;
625 
632 class IfaceMgr : public boost::noncopyable {
633 public:
636  typedef std::function<void (int fd)> SocketCallback;
637 
641  int socket_;
642 
644  SocketCallback callback_;
645  };
646 
648  typedef std::list<SocketCallbackInfo> SocketCallbackInfoContainer;
649 
657  static const uint32_t RCVBUFSIZE = 1500;
658 
663  static IfaceMgr& instance();
664 
675  static const IfaceMgrPtr& instancePtr();
676 
680  virtual ~IfaceMgr();
681 
691  void setTestMode(const bool test_mode) {
692  test_mode_ = test_mode;
693  }
694 
698  bool isTestMode() const {
699  return (test_mode_);
700  }
701 
707  void setAllowLoopBack(const bool allow_loopback) {
708  allow_loopback_ = allow_loopback;
709  }
710 
719  bool isDirectResponseSupported() const;
720 
728  IfacePtr getIface(int ifindex);
729 
736  IfacePtr getIface(const std::string& ifname);
737 
748  IfacePtr getIface(const PktPtr& pkt);
749 
757  const IfaceCollection& getIfaces() { return (ifaces_); }
758 
765  void clearIfaces();
766 
772  void detectIfaces();
773 
775  void clearUnicasts();
776 
778  void clearBoundAddresses();
779 
781  void collectBoundAddresses();
782 
796  uint16_t getSocket(const isc::dhcp::Pkt6Ptr& pkt);
797 
812 
816  void printIfaces(std::ostream& out = std::cout);
817 
829  bool send(const Pkt6Ptr& pkt);
830 
842  bool send(const Pkt4Ptr& pkt);
843 
855  Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
856 
868  Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
869 
888  int openSocket(const std::string& ifname,
889  const isc::asiolink::IOAddress& addr,
890  const uint16_t port,
891  const bool receive_bcast = false,
892  const bool send_bcast = false);
893 
911  int openSocketFromIface(const std::string& ifname,
912  const uint16_t port,
913  const uint8_t family);
914 
930  const uint16_t port);
931 
947  const uint16_t port);
948 
949 
995  bool openSockets6(const uint16_t port = DHCP6_SERVER_PORT,
996  IfaceMgrErrorMsgCallback error_handler = 0);
997 
1066  bool openSockets4(const uint16_t port = DHCP4_SERVER_PORT,
1067  const bool use_bcast = true,
1068  IfaceMgrErrorMsgCallback error_handler = 0);
1069 
1076  void closeSockets();
1077 
1081  uint16_t countIfaces() { return ifaces_.size(); }
1082 
1090  void addExternalSocket(int socketfd, SocketCallback callback);
1091 
1095  void deleteExternalSocket(int socketfd);
1096 
1106  int purgeBadSockets();
1107 
1109  void deleteAllExternalSockets();
1110 
1126  void setPacketFilter(const PktFilterPtr& packet_filter);
1127 
1148  void setPacketFilter(const PktFilter6Ptr& packet_filter);
1149 
1166  void setMatchingPacketFilter(const bool direct_response_desired = false);
1167 
1174  void addInterface(const IfacePtr& iface);
1175 
1182  bool hasOpenSocket(const uint16_t family) const;
1183 
1195  bool hasOpenSocket(const isc::asiolink::IOAddress& addr) const;
1196 
1201  return (packet_queue_mgr4_);
1202  }
1203 
1211  return (packet_queue_mgr4_->getPacketQueue());
1212  }
1213 
1218  return (packet_queue_mgr6_);
1219  }
1220 
1228  return (packet_queue_mgr6_->getPacketQueue());
1229  }
1230 
1241  void startDHCPReceiver(const uint16_t family);
1242 
1247  void stopDHCPReceiver();
1248 
1251  bool isDHCPReceiverRunning() const {
1252  return (dhcp_receiver_ != 0 && dhcp_receiver_->isRunning());
1253  }
1254 
1268  bool configureDHCPPacketQueue(const uint16_t family,
1269  data::ConstElementPtr queue_control);
1270 
1278  static void addFDtoSet(int fd, int& maxfd, fd_set* sockets);
1279 
1280  // don't use private, we need derived classes in tests
1281 protected:
1282 
1287  IfaceMgr();
1288 
1302  int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr,
1303  const uint16_t port, const bool receive_bcast = false,
1304  const bool send_bcast = false);
1305 
1326  Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1327 
1348  Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1349 
1364  int openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr,
1365  uint16_t port, const bool join_multicast);
1366 
1387  Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1388 
1409  Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0);
1410 
1411 
1418  void stubDetectIfaces();
1419 
1422 
1425 
1426  // TODO: Also keep this interface on Iface once interface detection
1427  // is implemented. We may need it e.g. to close all sockets on
1428  // specific interface
1429  //int recvsock_; // TODO: should be fd_set eventually, but we have only
1430  //int sendsock_; // 2 sockets for now. Will do for until next release
1431 
1432  // We can't use the same socket, as receiving socket
1433  // is bound to multicast address. And we all know what happens
1434  // to people who try to use multicast as source address.
1435 
1436 private:
1452  getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
1453  const uint16_t port);
1454 
1455 
1473  bool openMulticastSocket(Iface& iface,
1474  const isc::asiolink::IOAddress& addr,
1475  const uint16_t port,
1476  IfaceMgrErrorMsgCallback error_handler = 0);
1477 
1487  void receiveDHCP4Packets();
1488 
1499  void receiveDHCP4Packet(Iface& iface, const SocketInfo& socket_info);
1500 
1510  void receiveDHCP6Packets();
1511 
1521  void receiveDHCP6Packet(const SocketInfo& socket_info);
1522 
1526  void deleteExternalSocketInternal(int socketfd);
1527 
1536  PktFilterPtr packet_filter_;
1537 
1542  PktFilter6Ptr packet_filter6_;
1543 
1545  SocketCallbackInfoContainer callbacks_;
1546 
1548  std::mutex callbacks_mutex_;
1549 
1551  bool test_mode_;
1552 
1554  bool allow_loopback_;
1555 
1557  PacketQueueMgr4Ptr packet_queue_mgr4_;
1558 
1560  PacketQueueMgr6Ptr packet_queue_mgr6_;
1561 
1563  isc::util::WatchedThreadPtr dhcp_receiver_;
1564 };
1565 
1566 }; // namespace isc::dhcp
1567 }; // namespace isc
1568 
1569 #endif // IFACE_MGR_H
SocketCallback callback_
A callback that will be called when data arrives over socket_.
Definition: iface_mgr.h:644
void clearIfaces()
Removes detected interfaces.
Definition: iface_mgr.cc:900
IfacePtr getIface(int ifindex)
Returns interface specified interface index.
Definition: iface_mgr.cc:875
int openSocketFromRemoteAddress(const isc::asiolink::IOAddress &remote_addr, const uint16_t port)
Opens UDP/IP socket to be used to connect to remote address.
Definition: iface_mgr.cc:1014
Pkt4Ptr receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets directly or data from external sockets.
Definition: iface_mgr.cc:1256
void collectBoundAddresses()
Collect the addresses all sockets are bound to.
Definition: iface_mgr.cc:910
void setAllowLoopBack(const bool allow_loopback)
Allows or disallows the loopback interface.
Definition: iface_mgr.h:707
IfaceMgr exception thrown thrown when socket opening or configuration failed.
Definition: iface_mgr.h:63
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)...
Definition: iface_mgr.h:444
void addSocket(const SocketInfo &sock)
Adds socket descriptor to an interface.
Definition: iface_mgr.h:318
IfaceMgr exception thrown when there is no suitable socket found.
Definition: iface_mgr.h:93
void setPacketFilter(const PktFilterPtr &packet_filter)
Set packet filter object to handle sending and receiving DHCPv4 messages.
Definition: iface_mgr.cc:388
bool isDHCPReceiverRunning() const
Returns true if there is a receiver exists and its thread is currently running.
Definition: iface_mgr.h:1251
static const unsigned int MAX_MAC_LEN
Maximum MAC address length (Infiniband uses 20 bytes)
Definition: iface_mgr.h:122
size_t mac_len_
Length of link-layer address (usually 6).
Definition: iface_mgr.h:415
PacketQueueMgr4Ptr getPacketQueueMgr4()
Fetches the DHCPv4 packet queue manager.
Definition: iface_mgr.h:1200
Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets indirectly or data from external sockets.
Definition: iface_mgr.cc:1524
void addExternalSocket(int socketfd, SocketCallback callback)
Adds external socket and a callback.
Definition: iface_mgr.cc:324
bool delSocket(uint16_t sockfd)
Closes socket.
Definition: iface_mgr.cc:169
IfaceMgr exception thrown thrown when interface detection fails.
Definition: iface_mgr.h:41
bool flag_up_
Specifies if selected interface is up.
Definition: iface_mgr.h:428
Iface(const std::string &name, unsigned int ifindex)
Iface constructor.
Definition: iface_mgr.cc:63
bool empty() const
Empty predicate.
Definition: iface_mgr.h:520
const AddressCollection & getUnicasts() const
Returns a container of addresses the server should listen on.
Definition: iface_mgr.h:363
void addUnicast(const isc::asiolink::IOAddress &addr)
Adds unicast the server should listen on.
Definition: iface_mgr.cc:213
void setHWType(uint16_t type)
Sets up hardware type of the interface.
Definition: iface_mgr.h:226
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
Definition: iface_mgr.h:463
std::string getPlainMac() const
Returns link-layer address a plain text.
Definition: iface_mgr.cc:131
void startDHCPReceiver(const uint16_t family)
Starts DHCP packet receiver.
Definition: iface_mgr.cc:738
int ifindex_
Interface index (a value that uniquely identifies an interface).
Definition: iface_mgr.h:403
bool flag_loopback_
Specifies if selected interface is loopback.
Definition: iface_mgr.h:425
uint32_t getIndex() const
Returns interface index.
Definition: iface_mgr.h:216
void setActive(const isc::asiolink::IOAddress &address, const bool active)
Activates or deactivates address for the interface.
Definition: iface_mgr.cc:255
std::list< SocketInfo > SocketCollection
Type that holds a list of socket information.
Definition: iface_mgr.h:139
unsigned int countActive4() const
Returns a number of activated IPv4 addresses on the interface.
Definition: iface_mgr.cc:276
AddressCollection unicasts_
List of unicast addresses the server should listen on.
Definition: iface_mgr.h:409
Collection of pointers to network interfaces.
Definition: iface_mgr.h:466
int purgeBadSockets()
Scans registered socket set and removes any that are invalid.
Definition: iface_mgr.cc:364
bool openSockets6(const uint16_t port=DHCP6_SERVER_PORT, IfaceMgrErrorMsgCallback error_handler=0)
Opens IPv6 sockets on detected interfaces.
Definition: iface_mgr.cc:650
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:624
PacketQueue6Ptr getPacketQueue6()
Fetches the DHCPv6 receiver packet queue.
Definition: iface_mgr.h:1227
bool hasAddress(const isc::asiolink::IOAddress &address) const
Check if the interface has the specified address assigned.
Definition: iface_mgr.cc:240
Handles network interfaces, transmission and reception.
Definition: iface_mgr.h:632
size_t getMacLen() const
Returns MAC length.
Definition: iface_mgr.h:196
Represents a single network interface.
Definition: iface_mgr.h:118
SocketWriteError(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:81
int openSocketFromAddress(const isc::asiolink::IOAddress &addr, const uint16_t port)
Opens UDP/IP socket and binds to address specified.
Definition: iface_mgr.cc:991
~Iface()
Destructor.
Definition: iface_mgr.h:151
boost::shared_ptr< PacketQueueMgr6 > PacketQueueMgr6Ptr
Defines a shared pointer to PacketQueueMgr6.
boost::shared_ptr< PktFilter > PktFilterPtr
Pointer to a PktFilter object.
Definition: pkt_filter.h:134
boost::shared_ptr< PacketQueue< Pkt4Ptr > > PacketQueue4Ptr
Defines pointer to the DHCPv4 queue interface used at the application level.
Definition: packet_queue.h:132
const IfaceCollection & getIfaces()
Returns container with all interfaces.
Definition: iface_mgr.h:757
const SocketCollection & getSockets() const
Returns collection of all sockets added to interface.
Definition: iface_mgr.h:344
uint16_t getHWType() const
Returns hardware type of the interface.
Definition: iface_mgr.h:231
Defines the class, WatchSocket.
bool send(const Pkt6Ptr &pkt)
Sends an IPv6 packet.
Definition: iface_mgr.cc:1101
void closeSockets()
Closes all open sockets on interface.
Definition: iface_mgr.cc:77
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
Keeps callback information for external sockets.
Definition: iface_mgr.h:639
boost::shared_ptr< WatchedThread > WatchedThreadPtr
Defines a pointer to a WatchedThread.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
Definition: iface_mgr.cc:1126
bool inactive6_
Indicates that IPv6 sockets should (true) or should not (false) be opened on this interface...
Definition: iface_mgr.h:452
bool flag_multicast_
Flag specifies if selected interface is multicast capable.
Definition: iface_mgr.h:435
IfaceMgr exception thrown when there is no suitable interface.
Definition: iface_mgr.h:86
void deleteExternalSocket(int socketfd)
Deletes external socket.
Definition: iface_mgr.cc:347
BoundAddresses bound_address_
Unordered set of IPv4 bound addresses.
Definition: iface_mgr.h:1424
static const IfaceMgrPtr & instancePtr()
Returns pointer to the sole instance of the interface manager.
Definition: iface_mgr.cc:58
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
void clearUnicasts()
Clears unicast addresses on all interfaces.
Definition: iface_mgr.cc:925
IfacePtr getIface(uint32_t ifindex)
Lookup by interface index.
Definition: iface_mgr.cc:808
void clearBoundAddresses()
Clears the addresses all sockets are bound to.
Definition: iface_mgr.cc:905
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.
Definition: iface_mgr.h:501
boost::shared_ptr< PacketQueue< Pkt6Ptr > > PacketQueue6Ptr
Defines pointer to the DHCPv6 queue interface used at the application level.
Definition: packet_queue.h:137
IfaceMgr exception thrown thrown when error occurred during reading data from socket.
Definition: iface_mgr.h:71
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:797
bool flag_broadcast_
Flag specifies if selected interface is broadcast capable.
Definition: iface_mgr.h:438
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.
Definition: iface_mgr.h:202
void deleteAllExternalSockets()
Deletes all external sockets.
Definition: iface_mgr.cc:382
bool getAddress4(isc::asiolink::IOAddress &address) const
Returns IPv4 address assigned to the interface.
Definition: iface_mgr.cc:224
void addInterface(const IfacePtr &iface)
Adds an interface to list of known interfaces.
Definition: iface_mgr.cc:771
std::string getName() const
Returns interface name.
Definition: iface_mgr.h:221
PacketQueueMgr6Ptr getPacketQueueMgr6()
Fetches the DHCPv6 packet queue manager.
Definition: iface_mgr.h:1217
std::function< void(int fd)> SocketCallback
Defines callback used when data is received over external sockets.
Definition: iface_mgr.h:636
static const uint32_t RCVBUFSIZE
Packet reception buffer size.
Definition: iface_mgr.h:657
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.
Definition: iface_mgr.h:406
bool inactive4_
Indicates that IPv4 sockets should (true) or should not (false) be opened on this interface...
Definition: iface_mgr.h:448
std::list< Address > AddressCollection
Type that defines list of addresses.
Definition: iface_mgr.h:128
IfaceNotFound(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:88
bool hasOpenSocket(const uint16_t family) const
Checks if there is at least one socket of the specified family open.
Definition: iface_mgr.cc:430
boost::shared_ptr< PktFilter6 > PktFilter6Ptr
Pointer to a PktFilter object.
Definition: pkt_filter6.h:136
std::string getFullName() const
Returns full interface name as "ifname/ifindex" string.
Definition: iface_mgr.cc:124
util::Optional< asiolink::IOAddress > Address
Address type.
Definition: iface_mgr.h:125
SignalInterruptOnSelect(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:57
Exception thrown when a call to select is interrupted by a signal.
Definition: iface_mgr.h:55
bool flag_running_
Flag specifies if selected interface is running (e.g.
Definition: iface_mgr.h:432
PacketFilterChangeDenied(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:50
SocketConfigError(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:65
void closeSockets()
Closes all open sockets.
Definition: iface_mgr.cc:286
IfaceDetectError(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:43
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition: iface_mgr.cc:53
void resizeReadBuffer(const size_t new_size)
Reallocates the socket read buffer.
Definition: iface_mgr.h:391
SocketCollection sockets_
Socket used to send data.
Definition: iface_mgr.h:397
bool delAddress(const isc::asiolink::IOAddress &addr)
Deletes an address from an interface.
Definition: iface_mgr.cc:158
SocketNotFound(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:95
uint8_t mac_[MAX_MAC_LEN]
Link-layer address.
Definition: iface_mgr.h:412
uint16_t getSocket(const isc::dhcp::Pkt6Ptr &pkt)
Return most suitable socket for transmitting specified IPv6 packet.
Definition: iface_mgr.cc:1855
void detectIfaces()
Detects network interfaces.
void addAddress(const isc::asiolink::IOAddress &addr)
Adds an address to an interface.
Definition: iface_mgr.cc:250
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:931
virtual ~IfaceMgr()
Destructor.
Definition: iface_mgr.cc:314
int openSocketFromIface(const std::string &ifname, const uint16_t port, const uint8_t family)
Opens UDP/IP socket and binds it to interface specified.
Definition: iface_mgr.cc:950
static void addFDtoSet(int fd, int &maxfd, fd_set *sockets)
Convenience method for adding an descriptor to a set.
Definition: iface_mgr.cc:1390
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.
Definition: iface_mgr.cc:1088
PacketQueue4Ptr getPacketQueue4()
Fetches the DHCPv4 receiver packet queue.
Definition: iface_mgr.h:1210
boost::shared_ptr< PacketQueueMgr4 > PacketQueueMgr4Ptr
Defines a shared pointer to PacketQueueMgr4.
IfaceMgr()
Protected constructor.
Definition: iface_mgr.cc:186
void clearUnicasts()
Removes any unicast addresses.
Definition: iface_mgr.h:350
Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets or data from external sockets.
Definition: iface_mgr.cc:1381
void setTestMode(const bool test_mode)
Sets or clears the test mode for IfaceMgr.
Definition: iface_mgr.h:691
IfaceContainer::const_iterator begin() const
Begin iterator.
Definition: iface_mgr.h:506
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
Definition: iface_mgr.cc:319
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.
Definition: iface_mgr.h:641
void printIfaces(std::ostream &out=std::cout)
Debugging method that prints out all available interfaces.
Definition: iface_mgr.cc:784
bool isTestMode() const
Checks if the IfaceMgr is in the test mode.
Definition: iface_mgr.h:698
uint8_t * getReadBuffer()
Returns the pointer to the buffer used for data reading.
Definition: iface_mgr.h:376
Pkt6Ptr receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv6 packets directly or data from external sockets.
Definition: iface_mgr.cc:1402
std::string name_
Network interface name.
Definition: iface_mgr.h:400
uint16_t countIfaces()
Returns number of detected interfaces.
Definition: iface_mgr.h:1081
uint16_t hardware_type_
Hardware type.
Definition: iface_mgr.h:418
A template representing an optional value.
Definition: optional.h:36
void stopDHCPReceiver()
Stops the DHCP packet receiver.
Definition: iface_mgr.cc:298
Pkt4Ptr receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec=0)
Receive IPv4 packets indirectly or data from external sockets.
Definition: iface_mgr.cc:1134
boost::shared_ptr< IfaceMgr > IfaceMgrPtr
Type definition for the pointer to the IfaceMgr.
Definition: iface_mgr.h:614
const AddressCollection & getAddresses() const
Returns all addresses available on an interface.
Definition: iface_mgr.h:251
IfaceContainer::const_iterator end() const
End iterator.
Definition: iface_mgr.h:513
void stubDetectIfaces()
Stub implementation of network interface detection.
Definition: iface_mgr.cc:479
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:611
bool openSockets4(const uint16_t port=DHCP4_SERVER_PORT, const bool use_bcast=true, IfaceMgrErrorMsgCallback error_handler=0)
Opens IPv4 sockets on detected interfaces.
Definition: iface_mgr.cc:518
size_t size() const
Return the number of interfaces.
Definition: iface_mgr.h:527
IfaceMgr exception thrown thrown when error occurred during sending data through socket.
Definition: iface_mgr.h:79
std::list< SocketCallbackInfo > SocketCallbackInfoContainer
Defines storage container for callbacks for external sockets.
Definition: iface_mgr.h:648
Holds information about socket.
Definition: socket_info.h:19
Exception thrown when it is not allowed to set new Packet Filter.
Definition: iface_mgr.h:48
IfaceCollection ifaces_
List of available interfaces.
Definition: iface_mgr.h:1421
SocketReadError(const char *file, size_t line, const char *what)
Definition: iface_mgr.h:73
bool configureDHCPPacketQueue(const uint16_t family, data::ConstElementPtr queue_control)
Configures DHCP packet queue.
Definition: iface_mgr.cc:1945
void clear()
Clear the collection.
Definition: iface_mgr.h:532
size_t getReadBufferSize() const
Returns the current size of the socket read buffer.
Definition: iface_mgr.h:384
void push_back(const IfacePtr &iface)
Adds an interface to the collection.
Definition: iface_mgr.h:542
void setMac(const uint8_t *mac, size_t macLen)
Sets MAC address of the interface.
Definition: iface_mgr.cc:145