iface_mgr.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef IFACE_MGR_H
  15. #define IFACE_MGR_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/dhcp4.h>
  18. #include <dhcp/dhcp6.h>
  19. #include <dhcp/pkt4.h>
  20. #include <dhcp/pkt6.h>
  21. #include <dhcp/pkt_filter.h>
  22. #include <dhcp/pkt_filter6.h>
  23. #include <util/optional_value.h>
  24. #include <boost/function.hpp>
  25. #include <boost/noncopyable.hpp>
  26. #include <boost/scoped_array.hpp>
  27. #include <boost/shared_ptr.hpp>
  28. #include <list>
  29. #include <vector>
  30. namespace isc {
  31. namespace dhcp {
  32. /// @brief IfaceMgr exception thrown thrown when interface detection fails.
  33. class IfaceDetectError : public Exception {
  34. public:
  35. IfaceDetectError(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) { };
  37. };
  38. /// @brief Exception thrown when it is not allowed to set new Packet Filter.
  39. class PacketFilterChangeDenied : public Exception {
  40. public:
  41. PacketFilterChangeDenied(const char* file, size_t line, const char* what) :
  42. isc::Exception(file, line, what) { };
  43. };
  44. /// @brief Exception thrown when a call to select is interrupted by a signal.
  45. class SignalInterruptOnSelect : public Exception {
  46. public:
  47. SignalInterruptOnSelect(const char* file, size_t line, const char* what) :
  48. isc::Exception(file, line, what) { };
  49. };
  50. /// @brief IfaceMgr exception thrown thrown when socket opening
  51. /// or configuration failed.
  52. class SocketConfigError : public Exception {
  53. public:
  54. SocketConfigError(const char* file, size_t line, const char* what) :
  55. isc::Exception(file, line, what) { };
  56. };
  57. /// @brief IfaceMgr exception thrown thrown when error occured during
  58. /// reading data from socket.
  59. class SocketReadError : public Exception {
  60. public:
  61. SocketReadError(const char* file, size_t line, const char* what) :
  62. isc::Exception(file, line, what) { };
  63. };
  64. /// @brief IfaceMgr exception thrown thrown when error occured during
  65. /// sedning data through socket.
  66. class SocketWriteError : public Exception {
  67. public:
  68. SocketWriteError(const char* file, size_t line, const char* what) :
  69. isc::Exception(file, line, what) { };
  70. };
  71. /// @brief IfaceMgr exception thrown when there is no suitable interface.
  72. class IfaceNotFound : public Exception {
  73. public:
  74. IfaceNotFound(const char* file, size_t line, const char* what) :
  75. isc::Exception(file, line, what) { };
  76. };
  77. /// @brief IfaceMgr exception thrown when there is no suitable socket found.
  78. class SocketNotFound : public Exception {
  79. public:
  80. SocketNotFound(const char* file, size_t line, const char* what) :
  81. isc::Exception(file, line, what) { };
  82. };
  83. /// Holds information about socket.
  84. struct SocketInfo {
  85. isc::asiolink::IOAddress addr_; /// bound address
  86. uint16_t port_; /// socket port
  87. uint16_t family_; /// IPv4 or IPv6
  88. /// @brief Socket descriptor (a.k.a. primary socket).
  89. int sockfd_;
  90. /// @brief Fallback socket descriptor.
  91. ///
  92. /// This socket descriptor holds the handle to the fallback socket.
  93. /// The fallback socket is created when there is a need for the regular
  94. /// datagram socket to be bound to an IP address and port, besides
  95. /// primary socket (sockfd_) which is actually used to receive and process
  96. /// the DHCP messages. The fallback socket (if exists) is always associated
  97. /// with the primary socket. In particular, the need for the fallback socket
  98. /// arises when raw socket is a primary one. When primary socket is open,
  99. /// it is bound to an interface not the address and port. The implications
  100. /// include the possibility that the other process (e.g. the other instance
  101. /// of DHCP server) will bind to the same address and port through which the
  102. /// raw socket receives the DHCP messages.Another implication is that the
  103. /// kernel, being unaware of the DHCP server operating through the raw
  104. /// socket, will respond with the ICMP "Destination port unreachable"
  105. /// messages when DHCP messages are only received through the raw socket.
  106. /// In order to workaround the issues mentioned here, the fallback socket
  107. /// should be opened so as/ the kernel is aware that the certain address
  108. /// and port is in use.
  109. ///
  110. /// The fallback description is supposed to be set to a negative value if
  111. /// the fallback socket is closed (not open).
  112. int fallbackfd_;
  113. /// @brief SocketInfo constructor.
  114. ///
  115. /// @param addr An address the socket is bound to.
  116. /// @param port A port the socket is bound to.
  117. /// @param sockfd Socket descriptor.
  118. /// @param fallbackfd A descriptor of the fallback socket.
  119. SocketInfo(const isc::asiolink::IOAddress& addr, const uint16_t port,
  120. const int sockfd, const int fallbackfd = -1)
  121. : addr_(addr), port_(port), family_(addr.getFamily()),
  122. sockfd_(sockfd), fallbackfd_(fallbackfd) { }
  123. };
  124. /// @brief Represents a single network interface
  125. ///
  126. /// Iface structure represents network interface with all useful
  127. /// information, like name, interface index, MAC address and
  128. /// list of assigned addresses
  129. ///
  130. /// This class also holds the pointer to the socket read buffer.
  131. /// Functions reading from the socket may utilize this buffer to store the
  132. /// data being read from the socket. The advantage of using the
  133. /// pre-allocated buffer is that the buffer is allocated only once, rather
  134. /// than on every read. In addition, some OS specific code (e.g. BPF)
  135. /// may require use of fixed-size buffers. The size of such a buffer is
  136. /// returned by the OS kernel when the socket is opened. Hence, it is
  137. /// convenient to allocate the buffer when the socket is being opened and
  138. /// utilze it throughout the lifetime of the socket.
  139. ///
  140. /// In order to avoid potentially expensive copies of the @c Iface objects
  141. /// holding pre-allocated buffers and multiple containers, this class is
  142. /// noncopyable.
  143. class Iface : public boost::noncopyable {
  144. public:
  145. /// Maximum MAC address length (Infiniband uses 20 bytes)
  146. static const unsigned int MAX_MAC_LEN = 20;
  147. /// @brief Address type.
  148. typedef util::OptionalValue<asiolink::IOAddress> Address;
  149. /// Type that defines list of addresses
  150. typedef std::list<Address> AddressCollection;
  151. /// @brief Type that holds a list of socket information.
  152. ///
  153. /// @warning The type of the container used here must guarantee
  154. /// that the iterators do not invalidate when erase() is called.
  155. /// This is because, the \ref closeSockets function removes
  156. /// elements selectively by calling erase on the element to be
  157. /// removed and further iterates through remaining elements.
  158. ///
  159. /// @todo: Add SocketCollectionConstIter type
  160. typedef std::list<SocketInfo> SocketCollection;
  161. /// @brief Iface constructor.
  162. ///
  163. /// Creates Iface object that represents network interface.
  164. ///
  165. /// @param name name of the interface
  166. /// @param ifindex interface index (unique integer identifier)
  167. Iface(const std::string& name, int ifindex);
  168. /// @brief Destructor.
  169. ~Iface() { }
  170. /// @brief Closes all open sockets on interface.
  171. void closeSockets();
  172. /// @brief Closes all IPv4 or IPv6 sockets.
  173. ///
  174. /// This function closes sockets of the specific 'type' and closes them.
  175. /// The 'type' of the socket indicates whether it is used to send IPv4
  176. /// or IPv6 packets. The allowed values of the parameter are AF_INET and
  177. /// AF_INET6 for IPv4 and IPv6 packets respectively. It is important
  178. /// to realize that the actual types of sockets may be different than
  179. /// AF_INET for IPv4 packets. This is because, historically the IfaceMgr
  180. /// always used AF_INET sockets for IPv4 traffic. This is no longer the
  181. /// case when the Direct IPv4 traffic must be supported. In order to support
  182. /// direct traffic, the IfaceMgr operates on raw sockets, e.g. AF_PACKET
  183. /// family sockets on Linux.
  184. ///
  185. /// @todo Replace the AF_INET and AF_INET6 values with an enum
  186. /// which will not be confused with the actual socket type.
  187. ///
  188. /// @param family type of the sockets to be closed (AF_INET or AF_INET6)
  189. ///
  190. /// @throw BadValue if family value is different than AF_INET or AF_INET6.
  191. void closeSockets(const uint16_t family);
  192. /// @brief Returns full interface name as "ifname/ifindex" string.
  193. ///
  194. /// @return string with interface name
  195. std::string getFullName() const;
  196. /// @brief Returns link-layer address a plain text.
  197. ///
  198. /// @return MAC address as a plain text (string)
  199. std::string getPlainMac() const;
  200. /// @brief Sets MAC address of the interface.
  201. ///
  202. /// @param mac pointer to MAC address buffer
  203. /// @param macLen length of mac address
  204. void setMac(const uint8_t* mac, size_t macLen);
  205. /// @brief Returns MAC length.
  206. ///
  207. /// @return length of MAC address
  208. size_t getMacLen() const { return mac_len_; }
  209. /// @brief Returns pointer to MAC address.
  210. ///
  211. /// Note: Returned pointer is only valid as long as the interface object
  212. /// that returned it.
  213. const uint8_t* getMac() const { return mac_; }
  214. /// @brief Sets flag_*_ fields based on bitmask value returned by OS
  215. ///
  216. /// @note Implementation of this method is OS-dependent as bits have
  217. /// different meaning on each OS.
  218. /// We need to make it 64 bits, because Solaris uses 64, not 32 bits.
  219. ///
  220. /// @param flags bitmask value returned by OS in interface detection
  221. void setFlags(uint64_t flags);
  222. /// @brief Returns interface index.
  223. ///
  224. /// @return interface index
  225. uint16_t getIndex() const { return ifindex_; }
  226. /// @brief Returns interface name.
  227. ///
  228. /// @return interface name
  229. std::string getName() const { return name_; };
  230. /// @brief Sets up hardware type of the interface.
  231. ///
  232. /// @param type hardware type
  233. void setHWType(uint16_t type ) { hardware_type_ = type; }
  234. /// @brief Returns hardware type of the interface.
  235. ///
  236. /// @return hardware type
  237. uint16_t getHWType() const { return hardware_type_; }
  238. /// @brief Returns all addresses available on an interface.
  239. ///
  240. /// The returned addresses are encapsulated in the @c util::OptionalValue
  241. /// class to be able to selectively flag some of the addresses as active
  242. /// (when optional value is specified) or inactive (when optional value
  243. /// is specified). If the address is marked as active, the
  244. /// @c IfaceMgr::openSockets4 method will open socket and bind to this
  245. /// address. Otherwise, it will not bind any socket to this address.
  246. /// This is useful when an interface has multiple IPv4 addresses assigned.
  247. ///
  248. /// Care should be taken to not use this collection after Iface object
  249. /// ceases to exist. That is easy in most cases as Iface objects are
  250. /// created by IfaceMgr that is a singleton an is expected to be
  251. /// available at all time. We may revisit this if we ever decide to
  252. /// implement dynamic interface detection, but such fancy feature would
  253. /// mostly be useful for clients with wifi/vpn/virtual interfaces.
  254. ///
  255. /// @return collection of addresses
  256. const AddressCollection& getAddresses() const { return addrs_; }
  257. /// @brief Returns IPv4 address assigned to the interface.
  258. ///
  259. /// This function looks for an IPv4 address assigned to the interface
  260. /// and returns it through the argument.
  261. ///
  262. /// @param [out] address IPv4 address assigned to the interface.
  263. ///
  264. /// @return Boolean value which informs whether IPv4 address has been found
  265. /// for the interface (if true), or not (false).
  266. bool getAddress4(isc::asiolink::IOAddress& address) const;
  267. /// @brief Check if the interface has the specified address assigned.
  268. ///
  269. /// @param address Address to be checked.
  270. /// @return true if address is assigned to the intefrace, false otherwise.
  271. bool hasAddress(const isc::asiolink::IOAddress& address) const;
  272. /// @brief Adds an address to an interface.
  273. ///
  274. /// This only adds an address to collection, it does not physically
  275. /// configure address on actual network interface.
  276. ///
  277. /// @param addr address to be added
  278. void addAddress(const isc::asiolink::IOAddress& addr);
  279. /// @brief Activates or deactivates address for the interface.
  280. ///
  281. /// This method marks a specified address on the interface active or
  282. /// inactive. If the address is marked inactive, the
  283. /// @c IfaceMgr::openSockets4 method will NOT open socket for this address.
  284. ///
  285. /// @param address An address which should be activated, deactivated.
  286. /// @param active A boolean flag which indicates that the specified address
  287. /// should be active (if true) or inactive (if false).
  288. ///
  289. /// @throw BadValue if specified address doesn't exist for the interface.
  290. void setActive(const isc::asiolink::IOAddress& address, const bool active);
  291. /// @brief Activates or deactivates all addresses for the interface.
  292. ///
  293. /// This method marks all addresses on the interface active or inactive.
  294. /// If the address is marked inactive, the @c IfaceMgr::openSockets4
  295. /// method will NOT open socket for this address.
  296. ///
  297. /// @param active A boolean flag which indicates that the addresses
  298. /// should be active (if true) or inactive (if false).
  299. void setActive(const bool active);
  300. /// @brief Returns a number of activated IPv4 addresses on the interface.
  301. unsigned int countActive4() const;
  302. /// @brief Deletes an address from an interface.
  303. ///
  304. /// This only deletes address from collection, it does not physically
  305. /// remove address configuration from actual network interface.
  306. ///
  307. /// @param addr address to be removed.
  308. ///
  309. /// @return true if removal was successful (address was in collection),
  310. /// false otherwise
  311. bool delAddress(const isc::asiolink::IOAddress& addr);
  312. /// @brief Adds socket descriptor to an interface.
  313. ///
  314. /// @param sock SocketInfo structure that describes socket.
  315. void addSocket(const SocketInfo& sock) {
  316. sockets_.push_back(sock);
  317. }
  318. /// @brief Closes socket.
  319. ///
  320. /// Closes socket and removes corresponding SocketInfo structure
  321. /// from an interface.
  322. ///
  323. /// @param sockfd socket descriptor to be closed/removed.
  324. /// @return true if there was such socket, false otherwise
  325. bool delSocket(uint16_t sockfd);
  326. /// @brief Returns collection of all sockets added to interface.
  327. ///
  328. /// When new socket is created with @ref IfaceMgr::openSocket
  329. /// it is added to sockets collection on particular interface.
  330. /// If socket is opened by other means (e.g. function that does
  331. /// not use @ref IfaceMgr::openSocket) it will not be available
  332. /// in this collection. Note that functions like
  333. /// @ref IfaceMgr::openSocketFromIface use
  334. /// @ref IfaceMgr::openSocket internally.
  335. /// The returned reference is only valid during the lifetime of
  336. /// the IfaceMgr object that returned it.
  337. ///
  338. /// @return collection of sockets added to interface
  339. const SocketCollection& getSockets() const { return sockets_; }
  340. /// @brief Removes any unicast addresses
  341. ///
  342. /// Removes any unicast addresses that the server was configured to
  343. /// listen on
  344. void clearUnicasts() {
  345. unicasts_.clear();
  346. }
  347. /// @brief Adds unicast the server should listen on
  348. ///
  349. /// @throw BadValue if specified address is already defined on interface
  350. /// @param addr unicast address to listen on
  351. void addUnicast(const isc::asiolink::IOAddress& addr);
  352. /// @brief Returns a container of addresses the server should listen on
  353. ///
  354. /// @return address collection (may be empty)
  355. const AddressCollection& getUnicasts() const {
  356. return unicasts_;
  357. }
  358. /// @brief Returns the pointer to the buffer used for data reading.
  359. ///
  360. /// The returned pointer is only valid during the lifetime of the
  361. /// object which returns it or until the buffer is resized.
  362. /// This function is meant to be used with socket API to gather
  363. /// data from the socket.
  364. ///
  365. /// @return Pointer to the first element of the read buffer or
  366. /// NULL if the buffer is empty.
  367. uint8_t* getReadBuffer() {
  368. if (read_buffer_.empty()) {
  369. return NULL;
  370. }
  371. return (&read_buffer_[0]);
  372. }
  373. /// @brief Returns the current size of the socket read buffer.
  374. size_t getReadBufferSize() const {
  375. return (read_buffer_.size());
  376. }
  377. /// @brief Reallocates the socket read buffer.
  378. ///
  379. /// @param new_size New size of the buffer.
  380. void resizeReadBuffer(const size_t new_size) {
  381. read_buffer_.resize(new_size);
  382. }
  383. protected:
  384. /// Socket used to send data.
  385. SocketCollection sockets_;
  386. /// Network interface name.
  387. std::string name_;
  388. /// Interface index (a value that uniquely indentifies an interface).
  389. int ifindex_;
  390. /// List of assigned addresses.
  391. AddressCollection addrs_;
  392. /// List of unicast addresses the server should listen on
  393. AddressCollection unicasts_;
  394. /// Link-layer address.
  395. uint8_t mac_[MAX_MAC_LEN];
  396. /// Length of link-layer address (usually 6).
  397. size_t mac_len_;
  398. /// Hardware type.
  399. uint16_t hardware_type_;
  400. public:
  401. /// @todo: Make those fields protected once we start supporting more
  402. /// than just Linux
  403. /// Specifies if selected interface is loopback.
  404. bool flag_loopback_;
  405. /// Specifies if selected interface is up.
  406. bool flag_up_;
  407. /// Flag specifies if selected interface is running
  408. /// (e.g. cable plugged in, wifi associated).
  409. bool flag_running_;
  410. /// Flag specifies if selected interface is multicast capable.
  411. bool flag_multicast_;
  412. /// Flag specifies if selected interface is broadcast capable.
  413. bool flag_broadcast_;
  414. /// Interface flags (this value is as is returned by OS,
  415. /// it may mean different things on different OSes).
  416. /// Solaris based os have unsigned long flags field (64 bits).
  417. /// It is usually 32 bits, though.
  418. uint64_t flags_;
  419. /// Indicates that IPv4 sockets should (true) or should not (false)
  420. /// be opened on this interface.
  421. bool inactive4_;
  422. /// Indicates that IPv6 sockets should (true) or should not (false)
  423. /// be opened on this interface.
  424. bool inactive6_;
  425. private:
  426. /// @brief The buffer holding the data read from the socket.
  427. ///
  428. /// See @c Iface manager description for details.
  429. std::vector<uint8_t> read_buffer_;
  430. };
  431. typedef boost::shared_ptr<Iface> IfacePtr;
  432. /// @brief This type describes the callback function invoked when error occurs
  433. /// in the IfaceMgr.
  434. ///
  435. /// @param errmsg An error message.
  436. typedef
  437. boost::function<void(const std::string& errmsg)> IfaceMgrErrorMsgCallback;
  438. /// @brief Handles network interfaces, transmission and reception.
  439. ///
  440. /// IfaceMgr is an interface manager class that detects available network
  441. /// interfaces, configured addresses, link-local addresses, and provides
  442. /// API for using sockets.
  443. ///
  444. class IfaceMgr : public boost::noncopyable {
  445. public:
  446. /// Defines callback used when data is received over external sockets.
  447. typedef boost::function<void ()> SocketCallback;
  448. /// Keeps callback information for external sockets.
  449. struct SocketCallbackInfo {
  450. /// Socket descriptor of the external socket.
  451. int socket_;
  452. /// A callback that will be called when data arrives over socket_.
  453. SocketCallback callback_;
  454. };
  455. /// Defines storage container for callbacks for external sockets
  456. typedef std::list<SocketCallbackInfo> SocketCallbackInfoContainer;
  457. /// @brief Packet reception buffer size
  458. ///
  459. /// RFC3315 states that server responses may be
  460. /// fragmented if they are over MTU. There is no
  461. /// text whether client's packets may be larger
  462. /// than 1500. For now, we can assume that
  463. /// we don't support packets larger than 1500.
  464. static const uint32_t RCVBUFSIZE = 1500;
  465. // TODO performance improvement: we may change this into
  466. // 2 maps (ifindex-indexed and name-indexed) and
  467. // also hide it (make it public make tests easier for now)
  468. /// Type that holds a list of pointers to interfaces.
  469. typedef std::list<IfacePtr> IfaceCollection;
  470. /// IfaceMgr is a singleton class. This method returns reference
  471. /// to its sole instance.
  472. ///
  473. /// @return the only existing instance of interface manager
  474. static IfaceMgr& instance();
  475. /// @brief Sets or clears the test mode for @c IfaceMgr.
  476. ///
  477. /// Various unit test may set this flag to true, to indicate that the
  478. /// @c IfaceMgr is in the test mode. There are places in the code that
  479. /// modify the behavior depending if the @c IfaceMgr is in the test
  480. /// mode or not.
  481. ///
  482. /// @param test_mode A flag which indicates that the @c IfaceMgr is in the
  483. /// test mode (if true), or not (if false).
  484. void setTestMode(const bool test_mode) {
  485. test_mode_ = test_mode;
  486. }
  487. /// @brief Checks if the @c IfaceMgr is in the test mode.
  488. ///
  489. /// @return true if the @c IfaceMgr is in the test mode, false otherwise.
  490. bool isTestMode() const {
  491. return (test_mode_);
  492. }
  493. /// @brief Check if packet be sent directly to the client having no address.
  494. ///
  495. /// Checks if IfaceMgr can send DHCPv4 packet to the client
  496. /// who hasn't got address assigned. If this is not supported
  497. /// broadcast address should be used to send response to
  498. /// the client.
  499. ///
  500. /// @return true if direct response is supported.
  501. bool isDirectResponseSupported() const;
  502. /// @brief Returns interface specified interface index
  503. ///
  504. /// @param ifindex index of searched interface
  505. ///
  506. /// @return interface with requested index (or NULL if no such
  507. /// interface is present)
  508. ///
  509. IfacePtr getIface(int ifindex);
  510. /// @brief Returns interface with specified interface name
  511. ///
  512. /// @param ifname name of searched interface
  513. ///
  514. /// @return interface with requested name (or NULL if no such
  515. /// interface is present)
  516. IfacePtr getIface(const std::string& ifname);
  517. /// @brief Returns container with all interfaces.
  518. ///
  519. /// This reference is only valid as long as IfaceMgr is valid. However,
  520. /// since IfaceMgr is a singleton and is expected to be destroyed after
  521. /// main() function completes, you should not worry much about this.
  522. ///
  523. /// @return container with all interfaces.
  524. const IfaceCollection& getIfaces() { return (ifaces_); }
  525. /// @brief Removes detected interfaces.
  526. ///
  527. /// This method removes all detected interfaces. This method should be
  528. /// used by unit tests to supply a custom set of interfaces. For example:
  529. /// a unit test may create a pool of fake interfaces and use the custom
  530. /// @c PktFilter class to mimic socket operation on these interfaces.
  531. void clearIfaces();
  532. /// @brief Detects network interfaces.
  533. ///
  534. /// This method will eventually detect available interfaces. For now
  535. /// it offers stub implementation. First interface name and link-local
  536. /// IPv6 address is read from interfaces.txt file.
  537. void detectIfaces();
  538. /// @brief Clears unicast addresses on all interfaces.
  539. void clearUnicasts();
  540. /// @brief Return most suitable socket for transmitting specified IPv6 packet.
  541. ///
  542. /// This method takes Pkt6 (see overloaded implementation that takes
  543. /// Pkt4) and chooses appropriate socket to send it. This method
  544. /// may throw if specified packet does not have outbound interface specified,
  545. /// no such interface exists, or specified interface does not have any
  546. /// appropriate sockets open.
  547. ///
  548. /// @param pkt a packet to be transmitted
  549. ///
  550. /// @return a socket descriptor
  551. /// @throw SocketNotFound If no suitable socket found.
  552. /// @throw IfaceNotFound If interface is not set for the packet.
  553. uint16_t getSocket(const isc::dhcp::Pkt6& pkt);
  554. /// @brief Return most suitable socket for transmitting specified IPv4 packet.
  555. ///
  556. /// This method uses the local address assigned to the packet and tries
  557. /// to match it with addresses to which sockets are bound for the particular
  558. /// interface. If the match is not found, the method returns the first IPv4
  559. /// socket found for the particular interface. In case, there are no IPv4
  560. /// sockets assigned to the interface the exception is thrown.
  561. ///
  562. /// @param pkt A packet to be transmitted. It must hold a local address and
  563. /// a valid pointer to the interface.
  564. ///
  565. /// @return A structure describing a socket.
  566. /// @throw SocketNotFound if no suitable socket found.
  567. SocketInfo getSocket(const isc::dhcp::Pkt4& pkt);
  568. /// Debugging method that prints out all available interfaces.
  569. ///
  570. /// @param out specifies stream to print list of interfaces to
  571. void printIfaces(std::ostream& out = std::cout);
  572. /// @brief Sends an IPv6 packet.
  573. ///
  574. /// Sends an IPv6 packet. All parameters for actual transmission are specified in
  575. /// Pkt6 structure itself. That includes destination address, src/dst port
  576. /// and interface over which data will be sent.
  577. ///
  578. /// @param pkt packet to be sent
  579. ///
  580. /// @throw isc::BadValue if invalid interface specified in the packet.
  581. /// @throw isc::dhcp::SocketWriteError if sendmsg() failed to send packet.
  582. /// @return true if sending was successful
  583. bool send(const Pkt6Ptr& pkt);
  584. /// @brief Sends an IPv4 packet.
  585. ///
  586. /// Sends an IPv4 packet. All parameters for actual transmission are specified
  587. /// in Pkt4 structure itself. That includes destination address, src/dst
  588. /// port and interface over which data will be sent.
  589. ///
  590. /// @param pkt a packet to be sent
  591. ///
  592. /// @throw isc::BadValue if invalid interface specified in the packet.
  593. /// @throw isc::dhcp::SocketWriteError if sendmsg() failed to send packet.
  594. /// @return true if sending was successful
  595. bool send(const Pkt4Ptr& pkt);
  596. /// @brief Tries to receive DHCPv6 message over open IPv6 sockets.
  597. ///
  598. /// Attempts to receive a single DHCPv6 message over any of the open IPv6
  599. /// sockets. If reception is successful and all information about its
  600. /// sender is obtained, Pkt6 object is created and returned.
  601. ///
  602. /// This method also checks if data arrived over registered external socket.
  603. /// This data may be of a different protocol family than AF_INET6.
  604. ///
  605. /// @param timeout_sec specifies integral part of the timeout (in seconds)
  606. /// @param timeout_usec specifies fractional part of the timeout
  607. /// (in microseconds)
  608. ///
  609. /// @throw isc::BadValue if timeout_usec is greater than one million
  610. /// @throw isc::dhcp::SocketReadError if error occured when receiving a
  611. /// packet.
  612. /// @throw isc::dhcp::SignalInterruptOnSelect when a call to select() is
  613. /// interrupted by a signal.
  614. ///
  615. /// @return Pkt6 object representing received packet (or NULL)
  616. Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
  617. /// @brief Tries to receive IPv4 packet over open IPv4 sockets.
  618. ///
  619. /// Attempts to receive a single DHCPv4 message over any of the open
  620. /// IPv4 sockets. If reception is successful and all information about
  621. /// its sender is obtained, Pkt4 object is created and returned.
  622. ///
  623. /// This method also checks if data arrived over registered external socket.
  624. /// This data may be of a different protocol family than AF_INET.
  625. ///
  626. /// @param timeout_sec specifies integral part of the timeout (in seconds)
  627. /// @param timeout_usec specifies fractional part of the timeout
  628. /// (in microseconds)
  629. ///
  630. /// @throw isc::BadValue if timeout_usec is greater than one million
  631. /// @throw isc::dhcp::SocketReadError if error occured when receiving a
  632. /// packet.
  633. /// @throw isc::dhcp::SignalInterruptOnSelect when a call to select() is
  634. /// interrupted by a signal.
  635. ///
  636. /// @return Pkt4 object representing received packet (or NULL)
  637. Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
  638. /// Opens UDP/IP socket and binds it to address, interface and port.
  639. ///
  640. /// Specific type of socket (UDP/IPv4 or UDP/IPv6) depends on passed addr
  641. /// family.
  642. ///
  643. /// @param ifname name of the interface
  644. /// @param addr address to be bound.
  645. /// @param port UDP port.
  646. /// @param receive_bcast configure IPv4 socket to receive broadcast
  647. /// messages or IPv6 socket to join multicast group.
  648. /// @param send_bcast configure IPv4 socket to send broadcast messages.
  649. /// This parameter is ignored for IPv6 sockets.
  650. ///
  651. /// Method will throw if socket creation, socket binding or multicast
  652. /// join fails.
  653. ///
  654. /// @return socket descriptor, if socket creation, binding and multicast
  655. /// group join were all successful.
  656. int openSocket(const std::string& ifname,
  657. const isc::asiolink::IOAddress& addr,
  658. const uint16_t port,
  659. const bool receive_bcast = false,
  660. const bool send_bcast = false);
  661. /// @brief Opens UDP/IP socket and binds it to interface specified.
  662. ///
  663. /// This method differs from \ref openSocket in that it does not require
  664. /// the specification of a local address to which socket will be bound.
  665. /// Instead, the method searches through the addresses on the specified
  666. /// interface and selects one that matches the address family.
  667. ///
  668. /// @note This method does not join the socket to the multicast group.
  669. ///
  670. /// @param ifname name of the interface
  671. /// @param port UDP port
  672. /// @param family address family (AF_INET or AF_INET6)
  673. /// @return socket descriptor, if socket creation and binding was
  674. /// successful.
  675. /// @throw isc::Unexpected if failed to create and bind socket.
  676. /// @throw isc::BadValue if there is no address on specified interface
  677. /// that belongs to given family.
  678. int openSocketFromIface(const std::string& ifname,
  679. const uint16_t port,
  680. const uint8_t family);
  681. /// @brief Opens UDP/IP socket and binds to address specified
  682. ///
  683. /// This methods differs from \ref openSocket in that it does not require
  684. /// the specification of the interface to which the socket will be bound.
  685. ///
  686. /// @note This method does not join the socket to the multicast group.
  687. ///
  688. /// @param addr address to be bound
  689. /// @param port UDP port
  690. /// @return socket descriptor, if socket creation and binding was
  691. /// successful.
  692. /// @throw isc::Unexpected if failed to create and bind socket
  693. /// @throw isc::BadValue if specified address is not available on
  694. /// any interface
  695. int openSocketFromAddress(const isc::asiolink::IOAddress& addr,
  696. const uint16_t port);
  697. /// @brief Opens UDP/IP socket to be used to connect to remote address
  698. ///
  699. /// This method identifies the local address to be used to connect to the
  700. /// remote address specified as argument. Once the local address is
  701. /// identified, \ref openSocket is called to open a socket and bind it to
  702. /// the interface, address and port.
  703. ///
  704. /// @note This method does not join the socket to a multicast group.
  705. ///
  706. /// @param remote_addr remote address to connect to
  707. /// @param port UDP port
  708. /// @return socket descriptor, if socket creation and binding was
  709. /// successful.
  710. /// @throw isc::Unexpected if failed to create and bind socket
  711. int openSocketFromRemoteAddress(const isc::asiolink::IOAddress& remote_addr,
  712. const uint16_t port);
  713. /// @brief Opens IPv6 sockets on detected interfaces.
  714. ///
  715. /// This method opens sockets only on interfaces which have the
  716. /// @c inactive6_ field set to false (are active). If the interface is active
  717. /// but it is not running, it is down, or is a loopback interface,
  718. /// an error is reported.
  719. ///
  720. /// On the systems with multiple interfaces, it is often desired that the
  721. /// failure to open a socket on a particular interface doesn't cause a
  722. /// fatal error and sockets should be opened on remaining interfaces.
  723. /// However, the warning about the failure for the particular socket should
  724. /// be communicated to the caller. The libdhcp++ is a common library with
  725. /// no logger associated with it. Most of the functions in this library
  726. /// communicate errors via exceptions. In case of openSockets6 function
  727. /// exception must not be thrown if the function is supposed to continue
  728. /// opening sockets, despite an error. Therefore, if such a behavior is
  729. /// desired, the error handler function can be passed as a parameter.
  730. /// This error handler is called (if present) with an error string.
  731. /// Typically, error handler will simply log an error using an application
  732. /// logger, but it can do more sophisticated error handling too.
  733. ///
  734. /// @todo It is possible that additional parameters will have to be added
  735. /// to the error handler, e.g. Iface if it was really supposed to do
  736. /// some more sophisticated error handling.
  737. ///
  738. /// If the error handler is not installed (is NULL), the exception is thrown
  739. /// for each failure (default behavior).
  740. ///
  741. /// @warning This function does not check if there has been any sockets
  742. /// already open by the @c IfaceMgr. Therefore a caller should call
  743. /// @c IfaceMgr::closeSockets(AF_INET6) before calling this function.
  744. /// If there are any sockets open, the function may either throw an
  745. /// exception or invoke an error handler on attempt to bind the new socket
  746. /// to the same address and port.
  747. ///
  748. /// @param port specifies port number (usually DHCP6_SERVER_PORT)
  749. /// @param error_handler A pointer to an error handler function which is
  750. /// called by the openSockets6 when it fails to open a socket. This
  751. /// parameter can be NULL to indicate that the callback should not be used.
  752. ///
  753. /// @throw SocketOpenFailure if tried and failed to open socket.
  754. /// @return true if any sockets were open
  755. bool openSockets6(const uint16_t port = DHCP6_SERVER_PORT,
  756. IfaceMgrErrorMsgCallback error_handler = NULL);
  757. /// @brief Opens IPv4 sockets on detected interfaces.
  758. ///
  759. /// This method opens sockets only on interfaces which have the
  760. /// @c inactive4_ field set to false (are active). If the interface is active
  761. /// but it is not running, it is down, or is a loopback interface,
  762. /// an error is reported.
  763. ///
  764. /// The type of the socket being open depends on the selected Packet Filter
  765. /// represented by a class derived from @c isc::dhcp::PktFilter abstract
  766. /// class.
  767. ///
  768. /// It is possible to specify whether sockets should be broadcast capable.
  769. /// In most of the cases, the sockets should support broadcast traffic, e.g.
  770. /// DHCPv4 server and relay need to listen to broadcast messages sent by
  771. /// clients. If the socket has to be open on the particular interface, this
  772. /// interface must have broadcast flag set. If this condition is not met,
  773. /// the socket will be created in the unicast-only mode. If there are
  774. /// multiple broadcast-capable interfaces present, they may be all open
  775. /// in a broadcast mode only if the OS supports SO_BINDTODEVICE (bind socket
  776. /// to a device) socket option. If this option is not supported, only the
  777. /// first broadcast-capable socket will be opened in the broadcast mode.
  778. /// The error will be reported for sockets being opened on other interfaces.
  779. /// If the socket is bound to a device (interface), the broadcast traffic
  780. /// sent to this interface will be received on this interface only.
  781. /// This allows the DHCPv4 server or relay to detect the interface on which
  782. /// the broadcast message has been received. This interface is later used
  783. /// to send a response.
  784. ///
  785. /// On the systems with multiple interfaces, it is often desired that the
  786. /// failure to open a socket on a particular interface doesn't cause a
  787. /// fatal error and sockets should be opened on remaining interfaces.
  788. /// However, the warning about the failure for the particular socket should
  789. /// be communicated to the caller. The libdhcp++ is a common library with
  790. /// no logger associated with it. Most of the functions in this library
  791. /// communicate errors via exceptions. In case of openSockets4 function
  792. /// exception must not be thrown if the function is supposed to continue
  793. /// opening sockets, despite an error. Therefore, if such a behavior is
  794. /// desired, the error handler function can be passed as a parameter.
  795. /// This error handler is called (if present) with an error string.
  796. /// Typically, error handler will simply log an error using an application
  797. /// logger, but it can do more sophisticated error handling too.
  798. ///
  799. /// @todo It is possible that additional parameters will have to be added
  800. /// to the error handler, e.g. Iface if it was really supposed to do
  801. /// some more sophisticated error handling.
  802. ///
  803. /// If the error handler is not installed (is NULL), the exception is thrown
  804. /// for each failure (default behavior).
  805. ///
  806. /// @warning This function does not check if there has been any sockets
  807. /// already open by the @c IfaceMgr. Therefore a caller should call
  808. /// @c IfaceMgr::closeSockets(AF_INET) before calling this function.
  809. /// If there are any sockets open, the function may either throw an
  810. /// exception or invoke an error handler on attempt to bind the new socket
  811. /// to the same address and port.
  812. ///
  813. /// @param port specifies port number (usually DHCP4_SERVER_PORT)
  814. /// @param use_bcast configure sockets to support broadcast messages.
  815. /// @param error_handler A pointer to an error handler function which is
  816. /// called by the openSockets4 when it fails to open a socket. This
  817. /// parameter can be NULL to indicate that the callback should not be used.
  818. ///
  819. /// @throw SocketOpenFailure if tried and failed to open socket and callback
  820. /// function hasn't been specified.
  821. /// @return true if any sockets were open
  822. bool openSockets4(const uint16_t port = DHCP4_SERVER_PORT,
  823. const bool use_bcast = true,
  824. IfaceMgrErrorMsgCallback error_handler = NULL);
  825. /// @brief Closes all open sockets.
  826. /// Is used in destructor, but also from Dhcpv4Srv and Dhcpv6Srv classes.
  827. void closeSockets();
  828. /// @brief Closes all IPv4 or IPv6 sockets.
  829. ///
  830. /// This function closes sockets of the specific 'type' and closes them.
  831. /// The 'type' of the socket indicates whether it is used to send IPv4
  832. /// or IPv6 packets. The allowed values of the parameter are AF_INET and
  833. /// AF_INET6 for IPv4 and IPv6 packets respectively. It is important
  834. /// to realize that the actual types of sockets may be different than
  835. /// AF_INET for IPv4 packets. This is because, historically the IfaceMgr
  836. /// always used AF_INET sockets for IPv4 traffic. This is no longer the
  837. /// case when the Direct IPv4 traffic must be supported. In order to support
  838. /// direct traffic, the IfaceMgr operates on raw sockets, e.g. AF_PACKET
  839. /// family sockets on Linux.
  840. ///
  841. /// @todo Replace the AF_INET and AF_INET6 values with an enum
  842. /// which will not be confused with the actual socket type.
  843. ///
  844. /// @param family type of the sockets to be closed (AF_INET or AF_INET6)
  845. ///
  846. /// @throw BadValue if family value is different than AF_INET or AF_INET6.
  847. void closeSockets(const uint16_t family);
  848. /// @brief Returns number of detected interfaces.
  849. ///
  850. /// @return number of detected interfaces
  851. uint16_t countIfaces() { return ifaces_.size(); }
  852. /// @brief Adds external socket and a callback
  853. ///
  854. /// Specifies external socket and a callback that will be called
  855. /// when data will be received over that socket.
  856. ///
  857. /// @param socketfd socket descriptor
  858. /// @param callback callback function
  859. void addExternalSocket(int socketfd, SocketCallback callback);
  860. /// @brief Deletes external socket
  861. void deleteExternalSocket(int socketfd);
  862. /// @brief Deletes all external sockets.
  863. void deleteAllExternalSockets();
  864. /// @brief Set packet filter object to handle sending and receiving DHCPv4
  865. /// messages.
  866. ///
  867. /// Packet filter objects provide means for the @c IfaceMgr to open sockets
  868. /// for IPv4 packets reception and sending. This function sets custom packet
  869. /// filter (represented by a class derived from PktFilter) to be used by
  870. /// @c IfaceMgr. Note that there must be no IPv4 sockets open when this
  871. /// function is called. Call closeSockets(AF_INET) to close all hanging IPv4
  872. /// sockets opened by the current packet filter object.
  873. ///
  874. /// @param packet_filter A pointer to the new packet filter object to be
  875. /// used by @c IfaceMgr.
  876. ///
  877. /// @throw InvalidPacketFilter if provided packet filter object is NULL.
  878. /// @throw PacketFilterChangeDenied if there are open IPv4 sockets.
  879. void setPacketFilter(const PktFilterPtr& packet_filter);
  880. /// @brief Set packet filter object to handle sending and receving DHCPv6
  881. /// messages.
  882. ///
  883. /// Packet filter objects provide means for the @c IfaceMgr to open sockets
  884. /// for IPv6 packets reception and sending. This function sets the new
  885. /// instance of the packet filter which will be used by @c IfaceMgr to send
  886. /// and receive DHCPv6 messages, until replaced by another packet filter.
  887. ///
  888. /// It is required that DHCPv6 messages are send and received using methods
  889. /// of the same object that was used to open socket. Therefore, it is
  890. /// required that all IPv6 sockets are closed prior to calling this
  891. /// function. Call closeSockets(AF_INET6) to close all hanging IPv6 sockets
  892. /// opened by the current packet filter object.
  893. ///
  894. /// @param packet_filter A pointer to the new packet filter object to be
  895. /// used by @c IfaceMgr.
  896. ///
  897. /// @throw isc::dhcp::InvalidPacketFilter if specified object is NULL.
  898. /// @throw isc::dhcp::PacketFilterChangeDenied if there are open IPv6
  899. /// sockets.
  900. void setPacketFilter(const PktFilter6Ptr& packet_filter);
  901. /// @brief Set Packet Filter object to handle send/receive packets.
  902. ///
  903. /// This function sets Packet Filter object to be used by IfaceMgr,
  904. /// appropriate for the current OS. Setting the argument to 'true'
  905. /// indicates that function should set a packet filter class
  906. /// which supports direct responses to clients having no address
  907. /// assigned yet. Filters picked by this function will vary, depending
  908. /// on the OS being used. There is no guarantee that there is an
  909. /// implementation that supports this feature on a particular OS.
  910. /// If there isn't, the PktFilterInet object will be set. If the
  911. /// argument is set to 'false', PktFilterInet object instance will
  912. /// be set as the Packet Filter regrdaless of the OS type.
  913. ///
  914. /// @param direct_response_desired specifies whether the Packet Filter
  915. /// object being set should support direct traffic to the host
  916. /// not having address assigned.
  917. void setMatchingPacketFilter(const bool direct_response_desired = false);
  918. /// @brief Adds an interface to list of known interfaces.
  919. ///
  920. /// @param iface reference to Iface object.
  921. /// @note This function must be public because it has to be callable
  922. /// from unit tests.
  923. void addInterface(const IfacePtr& iface) {
  924. ifaces_.push_back(iface);
  925. }
  926. /// @brief Checks if there is at least one socket of the specified family
  927. /// open.
  928. ///
  929. /// @param family A socket family.
  930. ///
  931. /// @return true if there is at least one socket open, false otherwise.
  932. bool hasOpenSocket(const uint16_t family) const;
  933. /// @brief Checks if there is a socket open and bound to an address.
  934. ///
  935. /// This function checks if one of the sockets opened by the IfaceMgr is
  936. /// bound to the IP address specified as the method parameter. If the
  937. /// socket is bound to the port (and address is unspecified), the
  938. /// method will check if the address passed in the argument is configured
  939. /// on an interface.
  940. ///
  941. /// @param addr Address of the socket being searched.
  942. ///
  943. /// @return true if there is a socket bound to the specified address.
  944. bool hasOpenSocket(const isc::asiolink::IOAddress& addr) const;
  945. // don't use private, we need derived classes in tests
  946. protected:
  947. /// @brief Protected constructor.
  948. ///
  949. /// Protected constructor. This is a singleton class. We don't want
  950. /// anyone to create instances of IfaceMgr. Use instance() method instead.
  951. IfaceMgr();
  952. virtual ~IfaceMgr();
  953. /// @brief Opens IPv4 socket.
  954. ///
  955. /// Please do not use this method directly. Use openSocket instead.
  956. ///
  957. /// This method may throw exception if socket creation fails.
  958. ///
  959. /// @param iface reference to interface structure.
  960. /// @param addr an address the created socket should be bound to
  961. /// @param port a port that created socket should be bound to
  962. /// @param receive_bcast configure socket to receive broadcast messages
  963. /// @param send_bcast configure socket to send broadcast messages.
  964. ///
  965. /// @return socket descriptor
  966. int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr,
  967. const uint16_t port, const bool receive_bcast = false,
  968. const bool send_bcast = false);
  969. /// @brief Opens IPv6 socket.
  970. ///
  971. /// Please do not use this method directly. Use openSocket instead.
  972. ///
  973. /// This method may throw exception if socket creation fails.
  974. ///
  975. /// @param iface reference to interface structure.
  976. /// @param addr an address the created socket should be bound to
  977. /// @param port a port that created socket should be bound to
  978. /// @param join_multicast A boolean parameter which indicates whether
  979. /// socket should join All_DHCP_Relay_Agents_and_servers multicast
  980. /// group.
  981. ///
  982. /// @return socket descriptor
  983. int openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr,
  984. uint16_t port, const bool join_multicast);
  985. /// @brief Stub implementation of network interface detection.
  986. ///
  987. /// This implementations reads a single line from interfaces.txt file
  988. /// and pretends to detect such interface. First interface name and
  989. /// link-local IPv6 address or IPv4 address is read from the
  990. /// interfaces.txt file.
  991. void
  992. stubDetectIfaces();
  993. // TODO: having 2 maps (ifindex->iface and ifname->iface would)
  994. // probably be better for performance reasons
  995. /// List of available interfaces
  996. IfaceCollection ifaces_;
  997. // TODO: Also keep this interface on Iface once interface detection
  998. // is implemented. We may need it e.g. to close all sockets on
  999. // specific interface
  1000. //int recvsock_; // TODO: should be fd_set eventually, but we have only
  1001. //int sendsock_; // 2 sockets for now. Will do for until next release
  1002. // We can't use the same socket, as receiving socket
  1003. // is bound to multicast address. And we all know what happens
  1004. // to people who try to use multicast as source address.
  1005. /// Length of the control_buf_ array
  1006. size_t control_buf_len_;
  1007. /// Control-buffer, used in transmission and reception.
  1008. boost::scoped_array<char> control_buf_;
  1009. private:
  1010. /// @brief Identifies local network address to be used to
  1011. /// connect to remote address.
  1012. ///
  1013. /// This method identifies local network address that can be used
  1014. /// to connect to remote address specified.
  1015. /// It first creates socket and makes attempt to connect
  1016. /// to remote location via this socket. If connection
  1017. /// is established successfully, the local address to which
  1018. /// socket is bound is returned.
  1019. ///
  1020. /// @param remote_addr remote address to connect to
  1021. /// @param port port to be used
  1022. /// @return local address to be used to connect to remote address
  1023. /// @throw isc::Unexpected if unable to identify local address
  1024. isc::asiolink::IOAddress
  1025. getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
  1026. const uint16_t port);
  1027. /// @brief Open an IPv6 socket with multicast support.
  1028. ///
  1029. /// This function opens a socket capable of receiving messages sent to
  1030. /// the All_DHCP_Relay_Agents_and_Servers (ff02::1:2) multicast address.
  1031. /// The socket is bound to the in6addr_any address and the IPV6_JOIN_GROUP
  1032. /// option is set to point to the ff02::1:2 multicast address.
  1033. ///
  1034. /// @note This function is intended to be called internally by the
  1035. /// @c IfaceMgr::openSockets6. It is not intended to be called from any
  1036. /// other function.
  1037. ///
  1038. /// @param iface Interface on which socket should be open.
  1039. /// @param addr Link-local address to bind the socket to.
  1040. /// @param port Port number to bind socket to.
  1041. /// @param error_handler Error handler function to be called when an
  1042. /// error occurs during opening a socket, or NULL if exception should
  1043. /// be thrown upon error.
  1044. bool openMulticastSocket(Iface& iface,
  1045. const isc::asiolink::IOAddress& addr,
  1046. const uint16_t port,
  1047. IfaceMgrErrorMsgCallback error_handler = NULL);
  1048. /// Holds instance of a class derived from PktFilter, used by the
  1049. /// IfaceMgr to open sockets and send/receive packets through these
  1050. /// sockets. It is possible to supply custom object using
  1051. /// setPacketFilter method. Various Packet Filters differ mainly by using
  1052. /// different types of sockets, e.g. SOCK_DGRAM, SOCK_RAW and different
  1053. /// families, e.g. AF_INET, AF_PACKET etc. Another possible type of
  1054. /// Packet Filter is the one used for unit testing, which doesn't
  1055. /// open sockets but rather mimics their behavior (mock object).
  1056. PktFilterPtr packet_filter_;
  1057. /// Holds instance of a class derived from PktFilter6, used by the
  1058. /// IfaceMgr to manage sockets used to send and receive DHCPv6
  1059. /// messages. It is possible to supply a custom object using
  1060. /// setPacketFilter method.
  1061. PktFilter6Ptr packet_filter6_;
  1062. /// @brief Contains list of callbacks for external sockets
  1063. SocketCallbackInfoContainer callbacks_;
  1064. /// @brief Indicates if the IfaceMgr is in the test mode.
  1065. bool test_mode_;
  1066. };
  1067. }; // namespace isc::dhcp
  1068. }; // namespace isc
  1069. #endif // IFACE_MGR_H