iface_mgr.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (C) 2011 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 <list>
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/scoped_array.hpp>
  19. #include <boost/noncopyable.hpp>
  20. #include "asiolink/io_address.h"
  21. #include "dhcp/pkt6.h"
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief handles network interfaces, transmission and reception
  25. ///
  26. /// IfaceMgr is an interface manager class that detects available network
  27. /// interfaces, configured addresses, link-local addresses, and provides
  28. /// API for using sockets.
  29. ///
  30. class IfaceMgr : public boost::noncopyable {
  31. public:
  32. /// type that defines list of addresses
  33. typedef std::list<isc::asiolink::IOAddress> Addr6Lst;
  34. /// maximum MAC address length (Infiniband uses 20 bytes)
  35. static const unsigned int MAX_MAC_LEN = 20;
  36. /// @brief represents a single network interface
  37. ///
  38. /// Iface structure represents network interface with all useful
  39. /// information, like name, interface index, MAC address and
  40. /// list of assigned addresses
  41. struct Iface {
  42. /// constructor
  43. Iface(const std::string& name, int ifindex);
  44. /// returns full interface name in format ifname/ifindex
  45. std::string getFullName() const;
  46. /// returns link-layer address a plain text
  47. std::string getPlainMac() const;
  48. /// network interface name
  49. std::string name_;
  50. /// interface index (a value that uniquely indentifies an interface)
  51. int ifindex_;
  52. /// list of assigned addresses
  53. Addr6Lst addrs_;
  54. /// link-layer address
  55. uint8_t mac_[MAX_MAC_LEN];
  56. /// length of link-layer address (usually 6)
  57. int mac_len_;
  58. /// socket used to sending data
  59. int sendsock_;
  60. /// socket used for receiving data
  61. int recvsock_;
  62. };
  63. // TODO performance improvement: we may change this into
  64. // 2 maps (ifindex-indexed and name-indexed) and
  65. // also hide it (make it public make tests easier for now)
  66. /// type that holds a list of interfaces
  67. typedef std::list<Iface> IfaceLst;
  68. /// IfaceMgr is a singleton class. This method returns reference
  69. /// to its sole instance.
  70. ///
  71. /// @return the only existing instance of interface manager
  72. static IfaceMgr& instance();
  73. /// @brief Returns interface with specified interface index
  74. ///
  75. /// @param ifindex index of searched interface
  76. ///
  77. /// @return interface with requested index (or NULL if no such
  78. /// interface is present)
  79. ///
  80. Iface*
  81. getIface(int ifindex);
  82. /// @brief Returns interface with specified interface name
  83. ///
  84. /// @param ifname name of searched interface
  85. ///
  86. /// @return interface with requested name (or NULL if no such
  87. /// interface is present)
  88. ///
  89. Iface*
  90. getIface(const std::string& ifname);
  91. /// debugging method that prints out all available interfaces
  92. ///
  93. /// @param out specifies stream to print list of interfaces to
  94. void
  95. printIfaces(std::ostream& out = std::cout);
  96. /// @brief Sends a packet.
  97. ///
  98. /// Sends a packet. All parameters for actual transmission are specified in
  99. /// Pkt6 structure itself. That includes destination address, src/dst port
  100. /// and interface over which data will be sent.
  101. ///
  102. /// @param pkt packet to be sent
  103. ///
  104. /// @return true if sending was successful
  105. bool
  106. send(boost::shared_ptr<Pkt6>& pkt);
  107. /// @brief Tries to receive packet over open sockets.
  108. ///
  109. /// Attempts to receive a single packet of any of the open sockets.
  110. /// If reception is successful and all information about its sender
  111. /// are obtained, Pkt6 object is created and returned.
  112. ///
  113. /// TODO Start using select() and add timeout to be able
  114. /// to not wait infinitely, but rather do something useful
  115. /// (e.g. remove expired leases)
  116. ///
  117. /// @return Pkt6 object representing received packet (or NULL)
  118. boost::shared_ptr<Pkt6> receive();
  119. // don't use private, we need derived classes in tests
  120. protected:
  121. /// @brief Protected constructor.
  122. ///
  123. /// Protected constructor. This is a singleton class. We don't want
  124. /// anyone to create instances of IfaceMgr. Use instance() method
  125. IfaceMgr();
  126. ~IfaceMgr();
  127. /// @brief Detects network interfaces.
  128. ///
  129. /// This method will eventually detect available interfaces. For now
  130. /// it offers stub implementation. First interface name and link-local
  131. /// IPv6 address is read from intefaces.txt file.
  132. void
  133. detectIfaces();
  134. ///
  135. /// Opens UDP/IPv6 socket and binds it to address, interface and port.
  136. ///
  137. /// @param ifname name of the interface
  138. /// @param addr address to be bound.
  139. /// @param port UDP port.
  140. ///
  141. /// @return socket descriptor, if socket creation, binding and multicast
  142. /// group join were all successful. -1 otherwise.
  143. int openSocket(const std::string& ifname,
  144. const isc::asiolink::IOAddress& addr,
  145. int port);
  146. // TODO: having 2 maps (ifindex->iface and ifname->iface would)
  147. // probably be better for performance reasons
  148. /// List of available interfaces
  149. IfaceLst ifaces_;
  150. /// a pointer to a sole instance of this class (a singleton)
  151. static IfaceMgr * instance_;
  152. // TODO: Also keep this interface on Iface once interface detection
  153. // is implemented. We may need it e.g. to close all sockets on
  154. // specific interface
  155. int recvsock_; // TODO: should be fd_set eventually, but we have only
  156. int sendsock_; // 2 sockets for now. Will do for until next release
  157. // we can't use the same socket, as receiving socket
  158. // is bound to multicast address. And we all know what happens
  159. // to people who try to use multicast as source address.
  160. /// length of the control_buf_ array
  161. int control_buf_len_;
  162. /// control-buffer, used in transmission and reception
  163. boost::scoped_array<char> control_buf_;
  164. private:
  165. /// Opens sockets on detected interfaces.
  166. bool
  167. openSockets();
  168. /// creates a single instance of this class (a singleton implementation)
  169. static void
  170. instanceCreate();
  171. /// @brief Joins IPv6 multicast group on a socket.
  172. ///
  173. /// Socket must be created and bound to an address. Note that this
  174. /// address is different than the multicast address. For example DHCPv6
  175. /// server should bind its socket to link-local address (fe80::1234...)
  176. /// and later join ff02::1:2 multicast group.
  177. ///
  178. /// @param sock socket fd (socket must be bound)
  179. /// @param ifname interface name (for link-scoped multicast groups)
  180. /// @param mcast multicast address to join (e.g. "ff02::1:2")
  181. ///
  182. /// @return true if multicast join was successful
  183. ///
  184. bool
  185. joinMcast(int sock, const std::string& ifname,
  186. const std::string& mcast);
  187. };
  188. }; // namespace isc::dhcp
  189. }; // namespace isc
  190. #endif