iface_mgr.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Copyright (C) 2011-2012 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 <boost/noncopyable.hpp>
  22. #include <boost/scoped_array.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <list>
  25. namespace isc {
  26. namespace dhcp {
  27. /// @brief IfaceMgr exception thrown thrown when interface detection fails.
  28. class IfaceDetectError : public Exception {
  29. public:
  30. IfaceDetectError(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) { };
  32. };
  33. /// @brief IfaceMgr exception thrown thrown when socket opening
  34. /// or configuration failed.
  35. class SocketConfigError : public Exception {
  36. public:
  37. SocketConfigError(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) { };
  39. };
  40. /// @brief IfaceMgr exception thrown thrown when error occured during
  41. /// reading data from socket.
  42. class SocketReadError : public Exception {
  43. public:
  44. SocketReadError(const char* file, size_t line, const char* what) :
  45. isc::Exception(file, line, what) { };
  46. };
  47. /// @brief IfaceMgr exception thrown thrown when error occured during
  48. /// sedning data through socket.
  49. class SocketWriteError : public Exception {
  50. public:
  51. SocketWriteError(const char* file, size_t line, const char* what) :
  52. isc::Exception(file, line, what) { };
  53. };
  54. /// @brief handles network interfaces, transmission and reception
  55. ///
  56. /// IfaceMgr is an interface manager class that detects available network
  57. /// interfaces, configured addresses, link-local addresses, and provides
  58. /// API for using sockets.
  59. ///
  60. class IfaceMgr : public boost::noncopyable {
  61. public:
  62. /// type that defines list of addresses
  63. typedef std::vector<isc::asiolink::IOAddress> AddressCollection;
  64. /// defines callback used when commands are received over control session
  65. typedef void (*SessionCallback) (void);
  66. /// maximum MAC address length (Infiniband uses 20 bytes)
  67. static const unsigned int MAX_MAC_LEN = 20;
  68. /// @brief Packet reception buffer size
  69. ///
  70. /// RFC3315 states that server responses may be
  71. /// fragmented if they are over MTU. There is no
  72. /// text whether client's packets may be larger
  73. /// than 1500. For now, we can assume that
  74. /// we don't support packets larger than 1500.
  75. static const uint32_t RCVBUFSIZE = 1500;
  76. /// Holds information about socket.
  77. struct SocketInfo {
  78. uint16_t sockfd_; /// socket descriptor
  79. isc::asiolink::IOAddress addr_; /// bound address
  80. uint16_t port_; /// socket port
  81. uint16_t family_; /// IPv4 or IPv6
  82. /// @brief SocketInfo constructor.
  83. ///
  84. /// @param sockfd socket descriptor
  85. /// @param addr an address the socket is bound to
  86. /// @param port a port the socket is bound to
  87. SocketInfo(uint16_t sockfd, const isc::asiolink::IOAddress& addr,
  88. uint16_t port)
  89. :sockfd_(sockfd), addr_(addr), port_(port), family_(addr.getFamily()) { }
  90. };
  91. /// type that holds a list of socket informations
  92. /// @todo: Add SocketCollectionConstIter type
  93. typedef std::list<SocketInfo> SocketCollection;
  94. /// @brief represents a single network interface
  95. ///
  96. /// Iface structure represents network interface with all useful
  97. /// information, like name, interface index, MAC address and
  98. /// list of assigned addresses
  99. class Iface {
  100. public:
  101. /// @brief Iface constructor.
  102. ///
  103. /// Creates Iface object that represents network interface.
  104. ///
  105. /// @param name name of the interface
  106. /// @param ifindex interface index (unique integer identifier)
  107. Iface(const std::string& name, int ifindex);
  108. /// @brief Closes all open sockets on interface.
  109. void closeSockets();
  110. /// @brief Returns full interface name as "ifname/ifindex" string.
  111. ///
  112. /// @return string with interface name
  113. std::string getFullName() const;
  114. /// @brief Returns link-layer address a plain text.
  115. ///
  116. /// @return MAC address as a plain text (string)
  117. std::string getPlainMac() const;
  118. /// @brief Sets MAC address of the interface.
  119. ///
  120. /// @param mac pointer to MAC address buffer
  121. /// @param macLen length of mac address
  122. void setMac(const uint8_t* mac, size_t macLen);
  123. /// @brief Returns MAC length.
  124. ///
  125. /// @return length of MAC address
  126. size_t getMacLen() const { return mac_len_; }
  127. /// @brief Returns pointer to MAC address.
  128. ///
  129. /// Note: Returned pointer is only valid as long as the interface object
  130. /// that returned it.
  131. const uint8_t* getMac() const { return mac_; }
  132. /// @brief Sets flag_*_ fields based on bitmask value returned by OS
  133. ///
  134. /// Note: Implementation of this method is OS-dependent as bits have
  135. /// different meaning on each OS.
  136. ///
  137. /// @param flags bitmask value returned by OS in interface detection
  138. void setFlags(uint32_t flags);
  139. /// @brief Returns interface index.
  140. ///
  141. /// @return interface index
  142. uint16_t getIndex() const { return ifindex_; }
  143. /// @brief Returns interface name.
  144. ///
  145. /// @return interface name
  146. std::string getName() const { return name_; };
  147. /// @brief Sets up hardware type of the interface.
  148. ///
  149. /// @param type hardware type
  150. void setHWType(uint16_t type ) { hardware_type_ = type; }
  151. /// @brief Returns hardware type of the interface.
  152. ///
  153. /// @return hardware type
  154. uint16_t getHWType() const { return hardware_type_; }
  155. /// @brief Returns all interfaces available on an interface.
  156. ///
  157. /// Care should be taken to not use this collection after Iface object
  158. /// ceases to exist. That is easy in most cases as Iface objects are
  159. /// created by IfaceMgr that is a singleton an is expected to be
  160. /// available at all time. We may revisit this if we ever decide to
  161. /// implement dynamic interface detection, but such fancy feature would
  162. /// mostly be useful for clients with wifi/vpn/virtual interfaces.
  163. ///
  164. /// @return collection of addresses
  165. const AddressCollection& getAddresses() const { return addrs_; }
  166. /// @brief Adds an address to an interface.
  167. ///
  168. /// This only adds an address to collection, it does not physically
  169. /// configure address on actual network interface.
  170. ///
  171. /// @param addr address to be added
  172. void addAddress(const isc::asiolink::IOAddress& addr) {
  173. addrs_.push_back(addr);
  174. }
  175. /// @brief Deletes an address from an interface.
  176. ///
  177. /// This only deletes address from collection, it does not physically
  178. /// remove address configuration from actual network interface.
  179. ///
  180. /// @param addr address to be removed.
  181. ///
  182. /// @return true if removal was successful (address was in collection),
  183. /// false otherwise
  184. bool delAddress(const isc::asiolink::IOAddress& addr);
  185. /// @brief Adds socket descriptor to an interface.
  186. ///
  187. /// @param sock SocketInfo structure that describes socket.
  188. void addSocket(const SocketInfo& sock)
  189. { sockets_.push_back(sock); }
  190. /// @brief Closes socket.
  191. ///
  192. /// Closes socket and removes corresponding SocketInfo structure
  193. /// from an interface.
  194. ///
  195. /// @param sockfd socket descriptor to be closed/removed.
  196. /// @return true if there was such socket, false otherwise
  197. bool delSocket(uint16_t sockfd);
  198. /// @brief Returns collection of all sockets added to interface.
  199. ///
  200. /// When new socket is created with @ref IfaceMgr::openSocket
  201. /// it is added to sockets collection on particular interface.
  202. /// If socket is opened by other means (e.g. function that does
  203. /// not use @ref IfaceMgr::openSocket) it will not be available
  204. /// in this collection. Note that functions like
  205. /// @ref IfaceMgr::openSocketFromIface use
  206. /// @ref IfaceMgr::openSocket internally.
  207. /// The returned reference is only valid during the lifetime of
  208. /// the IfaceMgr object that returned it.
  209. ///
  210. /// @return collection of sockets added to interface
  211. const SocketCollection& getSockets() const { return sockets_; }
  212. protected:
  213. /// socket used to sending data
  214. SocketCollection sockets_;
  215. /// network interface name
  216. std::string name_;
  217. /// interface index (a value that uniquely identifies an interface)
  218. int ifindex_;
  219. /// list of assigned addresses
  220. AddressCollection addrs_;
  221. /// link-layer address
  222. uint8_t mac_[MAX_MAC_LEN];
  223. /// length of link-layer address (usually 6)
  224. size_t mac_len_;
  225. /// hardware type
  226. uint16_t hardware_type_;
  227. public:
  228. /// @todo: Make those fields protected once we start supporting more
  229. /// than just Linux
  230. /// specifies if selected interface is loopback
  231. bool flag_loopback_;
  232. /// specifies if selected interface is up
  233. bool flag_up_;
  234. /// flag specifies if selected interface is running
  235. /// (e.g. cable plugged in, wifi associated)
  236. bool flag_running_;
  237. /// flag specifies if selected interface is multicast capable
  238. bool flag_multicast_;
  239. /// flag specifies if selected interface is broadcast capable
  240. bool flag_broadcast_;
  241. /// interface flags (this value is as is returned by OS,
  242. /// it may mean different things on different OSes)
  243. uint32_t flags_;
  244. };
  245. // TODO performance improvement: we may change this into
  246. // 2 maps (ifindex-indexed and name-indexed) and
  247. // also hide it (make it public make tests easier for now)
  248. /// type that holds a list of interfaces
  249. typedef std::list<Iface> IfaceCollection;
  250. /// IfaceMgr is a singleton class. This method returns reference
  251. /// to its sole instance.
  252. ///
  253. /// @return the only existing instance of interface manager
  254. static IfaceMgr& instance();
  255. /// @brief Returns interface with specified interface index
  256. ///
  257. /// @param ifindex index of searched interface
  258. ///
  259. /// @return interface with requested index (or NULL if no such
  260. /// interface is present)
  261. ///
  262. Iface* getIface(int ifindex);
  263. /// @brief Returns interface with specified interface name
  264. ///
  265. /// @param ifname name of searched interface
  266. ///
  267. /// @return interface with requested name (or NULL if no such
  268. /// interface is present)
  269. ///
  270. Iface*
  271. getIface(const std::string& ifname);
  272. /// @brief Returns container with all interfaces.
  273. ///
  274. /// This reference is only valid as long as IfaceMgr is valid. However,
  275. /// since IfaceMgr is a singleton and is expected to be destroyed after
  276. /// main() function completes, you should not worry much about this.
  277. ///
  278. /// @return container with all interfaces.
  279. const IfaceCollection& getIfaces() { return ifaces_; }
  280. /// @brief Return most suitable socket for transmitting specified IPv6 packet.
  281. ///
  282. /// This method takes Pkt6 (see overloaded implementation that takes
  283. /// Pkt4) and chooses appropriate socket to send it. This method
  284. /// may throw BadValue if specified packet does not have outbound
  285. /// interface specified, no such interface exists, or specified
  286. /// interface does not have any appropriate sockets open.
  287. ///
  288. /// @param pkt a packet to be transmitted
  289. ///
  290. /// @return a socket descriptor
  291. uint16_t getSocket(const isc::dhcp::Pkt6& pkt);
  292. /// @brief Return most suitable socket for transmitting specified IPv6 packet.
  293. ///
  294. /// This method takes Pkt4 (see overloaded implementation that takes
  295. /// Pkt6) and chooses appropriate socket to send it. This method
  296. /// may throw BadValue if specified packet does not have outbound
  297. /// interface specified, no such interface exists, or specified
  298. /// interface does not have any appropriate sockets open.
  299. ///
  300. /// @param pkt a packet to be transmitted
  301. ///
  302. /// @return a socket descriptor
  303. uint16_t getSocket(const isc::dhcp::Pkt4& pkt);
  304. /// debugging method that prints out all available interfaces
  305. ///
  306. /// @param out specifies stream to print list of interfaces to
  307. void
  308. printIfaces(std::ostream& out = std::cout);
  309. /// @brief Sends an IPv6 packet.
  310. ///
  311. /// Sends an IPv6 packet. All parameters for actual transmission are specified in
  312. /// Pkt6 structure itself. That includes destination address, src/dst port
  313. /// and interface over which data will be sent.
  314. ///
  315. /// @param pkt packet to be sent
  316. ///
  317. /// @throw isc::BadValue if invalid interface specified in the packet.
  318. /// @throw isc::dhcp::SocketWriteError if sendmsg() failed to send packet.
  319. /// @return true if sending was successful
  320. bool send(const Pkt6Ptr& pkt);
  321. /// @brief Sends an IPv4 packet.
  322. ///
  323. /// Sends an IPv4 packet. All parameters for actual transmission are specified
  324. /// in Pkt4 structure itself. That includes destination address, src/dst
  325. /// port and interface over which data will be sent.
  326. ///
  327. /// @param pkt a packet to be sent
  328. ///
  329. /// @throw isc::BadValue if invalid interface specified in the packet.
  330. /// @throw isc::dhcp::SocketWriteError if sendmsg() failed to send packet.
  331. /// @return true if sending was successful
  332. bool send(const Pkt4Ptr& pkt);
  333. /// @brief Tries to receive IPv6 packet over open IPv6 sockets.
  334. ///
  335. /// Attempts to receive a single IPv6 packet of any of the open IPv6 sockets.
  336. /// If reception is successful and all information about its sender
  337. /// are obtained, Pkt6 object is created and returned.
  338. ///
  339. /// TODO Start using select() and add timeout to be able
  340. /// to not wait infinitely, but rather do something useful
  341. /// (e.g. remove expired leases)
  342. ///
  343. /// @param timeout_sec specifies integral part of the timeout (in seconds)
  344. /// @param timeout_usec specifies fractional part of the timeout
  345. /// (in microseconds)
  346. ///
  347. /// @throw isc::BadValue if timeout_usec is greater than one million
  348. /// @throw isc::dhcp::SocketReadError if error occured when receiving a packet.
  349. /// @return Pkt6 object representing received packet (or NULL)
  350. Pkt6Ptr receive6(uint32_t timeout_sec, uint32_t timeout_usec = 0);
  351. /// @brief Tries to receive IPv4 packet over open IPv4 sockets.
  352. ///
  353. /// Attempts to receive a single IPv4 packet of any of the open IPv4 sockets.
  354. /// If reception is successful and all information about its sender
  355. /// are obtained, Pkt4 object is created and returned.
  356. ///
  357. /// @param timeout_sec specifies integral part of the timeout (in seconds)
  358. /// @param timeout_usec specifies fractional part of the timeout
  359. /// (in microseconds)
  360. ///
  361. /// @throw isc::BadValue if timeout_usec is greater than one million
  362. /// @throw isc::dhcp::SocketReadError if error occured when receiving a packet.
  363. /// @return Pkt4 object representing received packet (or NULL)
  364. Pkt4Ptr receive4(uint32_t timeout_sec, uint32_t timeout_usec = 0);
  365. /// Opens UDP/IP socket and binds it to address, interface and port.
  366. ///
  367. /// Specific type of socket (UDP/IPv4 or UDP/IPv6) depends on passed addr
  368. /// family.
  369. ///
  370. /// @param ifname name of the interface
  371. /// @param addr address to be bound.
  372. /// @param port UDP port.
  373. ///
  374. /// Method will throw if socket creation, socket binding or multicast
  375. /// join fails.
  376. ///
  377. /// @return socket descriptor, if socket creation, binding and multicast
  378. /// group join were all successful.
  379. int openSocket(const std::string& ifname,
  380. const isc::asiolink::IOAddress& addr,
  381. const uint16_t port);
  382. /// @brief Opens UDP/IP socket and binds it to interface specified.
  383. ///
  384. /// This method differs from \ref openSocket in that it does not require
  385. /// the specification of a local address to which socket will be bound.
  386. /// Instead, the method searches through the addresses on the specified
  387. /// interface and selects one that matches the address family.
  388. ///
  389. /// @param ifname name of the interface
  390. /// @param port UDP port
  391. /// @param family address family (AF_INET or AF_INET6)
  392. /// @return socket descriptor, if socket creation, binding and multicast
  393. /// group join were all successful.
  394. /// @throw isc::Unexpected if failed to create and bind socket.
  395. /// @throw isc::BadValue if there is no address on specified interface
  396. /// that belongs to given family.
  397. int openSocketFromIface(const std::string& ifname,
  398. const uint16_t port,
  399. const uint8_t family);
  400. /// @brief Opens UDP/IP socket and binds to address specified
  401. ///
  402. /// This methods differs from \ref openSocket in that it does not require
  403. /// the specification of the interface to which the socket will be bound.
  404. ///
  405. /// @param addr address to be bound
  406. /// @param port UDP port
  407. /// @return socket descriptor, if socket creation, binding and multicast
  408. /// group join were all successful.
  409. /// @throw isc::Unexpected if failed to create and bind socket
  410. /// @throw isc::BadValue if specified address is not available on
  411. /// any interface
  412. int openSocketFromAddress(const isc::asiolink::IOAddress& addr,
  413. const uint16_t port);
  414. /// @brief Opens UDP/IP socket to be used to connect to remote address
  415. ///
  416. /// This method identifies the local address to be used to connect to the
  417. /// remote address specified as argument. Once the local address is
  418. /// identified, \ref openSocket is called to open a socket and bind it to
  419. /// the interface, address and port.
  420. ///
  421. /// @param remote_addr remote address to connect to
  422. /// @param port UDP port
  423. /// @return socket descriptor, if socket creation, binding and multicast
  424. /// group join were all successful.
  425. /// @throw isc::Unexpected if failed to create and bind socket
  426. int openSocketFromRemoteAddress(const isc::asiolink::IOAddress& remote_addr,
  427. const uint16_t port);
  428. /// Opens IPv6 sockets on detected interfaces.
  429. ///
  430. /// Will throw exception if socket creation fails.
  431. ///
  432. /// @param port specifies port number (usually DHCP6_SERVER_PORT)
  433. ///
  434. /// @throw SocketOpenFailure if tried and failed to open socket.
  435. /// @return true if any sockets were open
  436. bool openSockets6(const uint16_t port = DHCP6_SERVER_PORT);
  437. /// @brief Closes all open sockets.
  438. /// Is used in destructor, but also from Dhcpv4_srv and Dhcpv6_srv classes.
  439. void closeSockets();
  440. /// Opens IPv4 sockets on detected interfaces.
  441. /// Will throw exception if socket creation fails.
  442. ///
  443. /// @param port specifies port number (usually DHCP4_SERVER_PORT)
  444. ///
  445. /// @throw SocketOpenFailure if tried and failed to open socket.
  446. /// @return true if any sockets were open
  447. bool openSockets4(const uint16_t port = DHCP4_SERVER_PORT);
  448. /// @brief returns number of detected interfaces
  449. ///
  450. /// @return number of detected interfaces
  451. uint16_t countIfaces() { return ifaces_.size(); }
  452. /// @brief Sets session socket and a callback
  453. ///
  454. /// Specifies session socket and a callback that will be called
  455. /// when data will be received over that socket.
  456. ///
  457. /// @param socketfd socket descriptor
  458. /// @param callback callback function
  459. void set_session_socket(int socketfd, SessionCallback callback) {
  460. session_socket_ = socketfd;
  461. session_callback_ = callback;
  462. }
  463. /// A value of socket descriptor representing "not specified" state.
  464. static const int INVALID_SOCKET = -1;
  465. // don't use private, we need derived classes in tests
  466. protected:
  467. /// @brief Protected constructor.
  468. ///
  469. /// Protected constructor. This is a singleton class. We don't want
  470. /// anyone to create instances of IfaceMgr. Use instance() method instead.
  471. IfaceMgr();
  472. virtual ~IfaceMgr();
  473. /// @brief Opens IPv4 socket.
  474. ///
  475. /// Please do not use this method directly. Use openSocket instead.
  476. ///
  477. /// This method may throw exception if socket creation fails.
  478. ///
  479. /// @param iface reference to interface structure.
  480. /// @param addr an address the created socket should be bound to
  481. /// @param port a port that created socket should be bound to
  482. ///
  483. /// @return socket descriptor
  484. int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr, uint16_t port);
  485. /// @brief Opens IPv6 socket.
  486. ///
  487. /// Please do not use this method directly. Use openSocket instead.
  488. ///
  489. /// This method may throw exception if socket creation fails.
  490. ///
  491. /// @param iface reference to interface structure.
  492. /// @param addr an address the created socket should be bound to
  493. /// @param port a port that created socket should be bound to
  494. ///
  495. /// @return socket descriptor
  496. int openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr, uint16_t port);
  497. /// @brief Adds an interface to list of known interfaces.
  498. ///
  499. /// @param iface reference to Iface object.
  500. void addInterface(const Iface& iface) {
  501. ifaces_.push_back(iface);
  502. }
  503. /// @brief Detects network interfaces.
  504. ///
  505. /// This method will eventually detect available interfaces. For now
  506. /// it offers stub implementation. First interface name and link-local
  507. /// IPv6 address is read from interfaces.txt file.
  508. void
  509. detectIfaces();
  510. /// @brief Stub implementation of network interface detection.
  511. ///
  512. /// This implementations reads a single line from interfaces.txt file
  513. /// and pretends to detect such interface. First interface name and
  514. /// link-local IPv6 address or IPv4 address is read from the
  515. /// interfaces.txt file.
  516. void
  517. stubDetectIfaces();
  518. // TODO: having 2 maps (ifindex->iface and ifname->iface would)
  519. // probably be better for performance reasons
  520. /// List of available interfaces
  521. IfaceCollection ifaces_;
  522. // TODO: Also keep this interface on Iface once interface detection
  523. // is implemented. We may need it e.g. to close all sockets on
  524. // specific interface
  525. //int recvsock_; // TODO: should be fd_set eventually, but we have only
  526. //int sendsock_; // 2 sockets for now. Will do for until next release
  527. // we can't use the same socket, as receiving socket
  528. // is bound to multicast address. And we all know what happens
  529. // to people who try to use multicast as source address.
  530. /// length of the control_buf_ array
  531. size_t control_buf_len_;
  532. /// control-buffer, used in transmission and reception
  533. boost::scoped_array<char> control_buf_;
  534. /// @brief A wrapper for OS-specific operations before sending IPv4 packet
  535. ///
  536. /// @param m message header (will be later used for sendmsg() call)
  537. /// @param control_buf buffer to be used during transmission
  538. /// @param control_buf_len buffer length
  539. /// @param pkt packet to be sent
  540. void os_send4(struct msghdr& m, boost::scoped_array<char>& control_buf,
  541. size_t control_buf_len, const Pkt4Ptr& pkt);
  542. /// @brief OS-specific operations during IPv4 packet reception
  543. ///
  544. /// @param m message header (was used during recvmsg() call)
  545. /// @param pkt packet received (some fields will be set here)
  546. ///
  547. /// @return true if successful, false otherwise
  548. bool os_receive4(struct msghdr& m, Pkt4Ptr& pkt);
  549. /// socket descriptor of the session socket
  550. int session_socket_;
  551. /// a callback that will be called when data arrives over session_socket_
  552. SessionCallback session_callback_;
  553. private:
  554. /// @brief Joins IPv6 multicast group on a socket.
  555. ///
  556. /// Socket must be created and bound to an address. Note that this
  557. /// address is different than the multicast address. For example DHCPv6
  558. /// server should bind its socket to link-local address (fe80::1234...)
  559. /// and later join ff02::1:2 multicast group.
  560. ///
  561. /// @param sock socket fd (socket must be bound)
  562. /// @param ifname interface name (for link-scoped multicast groups)
  563. /// @param mcast multicast address to join (e.g. "ff02::1:2")
  564. ///
  565. /// @return true if multicast join was successful
  566. ///
  567. bool
  568. joinMulticast(int sock, const std::string& ifname,
  569. const std::string& mcast);
  570. /// @brief Identifies local network address to be used to
  571. /// connect to remote address.
  572. ///
  573. /// This method identifies local network address that can be used
  574. /// to connect to remote address specified.
  575. /// It first creates socket and makes attempt to connect
  576. /// to remote location via this socket. If connection
  577. /// is established successfully, the local address to which
  578. /// socket is bound is returned.
  579. ///
  580. /// @param remote_addr remote address to connect to
  581. /// @param port port to be used
  582. /// @return local address to be used to connect to remote address
  583. /// @throw isc::Unexpected if unable to identify local address
  584. isc::asiolink::IOAddress
  585. getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
  586. const uint16_t port);
  587. };
  588. }; // namespace isc::dhcp
  589. }; // namespace isc
  590. #endif // IFACE_MGR_H