iface_mgr.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/dhcp6.h>
  22. #include <dhcp/dhcp4.h>
  23. #include <dhcp/pkt4.h>
  24. #include <dhcp/pkt6.h>
  25. namespace isc {
  26. namespace dhcp {
  27. /// @brief handles network interfaces, transmission and reception
  28. ///
  29. /// IfaceMgr is an interface manager class that detects available network
  30. /// interfaces, configured addresses, link-local addresses, and provides
  31. /// API for using sockets.
  32. ///
  33. class IfaceMgr : public boost::noncopyable {
  34. public:
  35. /// type that defines list of addresses
  36. typedef std::vector<isc::asiolink::IOAddress> AddressCollection;
  37. /// maximum MAC address length (Infiniband uses 20 bytes)
  38. static const unsigned int MAX_MAC_LEN = 20;
  39. /// Holds information about socket.
  40. struct SocketInfo {
  41. uint16_t sockfd_; /// socket descriptor
  42. isc::asiolink::IOAddress addr_; /// bound address
  43. uint16_t port_; /// socket port
  44. uint16_t family_; /// IPv4 or IPv6
  45. /// @brief SocketInfo constructor.
  46. ///
  47. /// @param sockfd socket descriptor
  48. /// @param addr an address the socket is bound to
  49. /// @param port a port the socket is bound to
  50. SocketInfo(uint16_t sockfd, const isc::asiolink::IOAddress& addr,
  51. uint16_t port)
  52. :sockfd_(sockfd), addr_(addr), port_(port), family_(addr.getFamily()) { }
  53. };
  54. /// type that holds a list of socket informations
  55. typedef std::list<SocketInfo> SocketCollection;
  56. /// @brief represents a single network interface
  57. ///
  58. /// Iface structure represents network interface with all useful
  59. /// information, like name, interface index, MAC address and
  60. /// list of assigned addresses
  61. class Iface {
  62. public:
  63. /// @brief Iface constructor.
  64. ///
  65. /// Creates Iface object that represents network interface.
  66. ///
  67. /// @param name name of the interface
  68. /// @param ifindex interface index (unique integer identifier)
  69. Iface(const std::string& name, int ifindex);
  70. /// @brief Returns full interface name as "ifname/ifindex" string.
  71. ///
  72. /// @return string with interface name
  73. std::string getFullName() const;
  74. /// @brief Returns link-layer address a plain text.
  75. ///
  76. /// @return MAC address as a plain text (string)
  77. std::string getPlainMac() const;
  78. /// @brief Sets flag_*_ fields based on bitmask value returned by OS
  79. ///
  80. /// Note: Implementation of this method is OS-dependent as bits have
  81. /// different meaning on each OS.
  82. ///
  83. /// @param flags bitmask value returned by OS in interface detection
  84. void setFlags(uint32_t flags);
  85. /// @brief Returns interface index.
  86. ///
  87. /// @return interface index
  88. uint16_t getIndex() const { return ifindex_; }
  89. /// @brief Returns interface name.
  90. ///
  91. /// @return interface name
  92. std::string getName() const { return name_; };
  93. /// @brief Returns all interfaces available on an interface.
  94. ///
  95. /// Care should be taken to not use this collection after Iface object
  96. /// ceases to exist. That is easy in most cases as Iface objects are
  97. /// created by IfaceMgr that is a singleton an is expected to be
  98. /// available at all time. We may revisit this if we ever decide to
  99. /// implement dynamic interface detection, but such fancy feature would
  100. /// mostly be useful for clients with wifi/vpn/virtual interfaces.
  101. ///
  102. /// @return collection of addresses
  103. const AddressCollection& getAddresses() const { return addrs_; }
  104. /// @brief Adds an address to an interface.
  105. ///
  106. /// This only adds an address to collection, it does not physically
  107. /// configure address on actual network interface.
  108. ///
  109. /// @param addr address to be added
  110. void addAddress(const isc::asiolink::IOAddress& addr) {
  111. addrs_.push_back(addr);
  112. }
  113. /// @brief Deletes an address from an interface.
  114. ///
  115. /// This only deletes address from collection, it does not physically
  116. /// remove address configuration from actual network interface.
  117. ///
  118. /// @param addr address to be removed.
  119. ///
  120. /// @return true if removal was successful (address was in collection),
  121. /// false otherwise
  122. bool delAddress(const isc::asiolink::IOAddress& addr);
  123. /// @brief Adds socket descriptor to an interface.
  124. ///
  125. /// @param socket SocketInfo structure that describes socket.
  126. void addSocket(const SocketInfo& sock)
  127. { sockets_.push_back(sock); }
  128. /// @brief Closes socket.
  129. ///
  130. /// Closes socket and removes corresponding SocketInfo structure
  131. /// from an interface.
  132. ///
  133. /// @param socket descriptor to be closed/removed.
  134. /// @return true if there was such socket, false otherwise
  135. bool delSocket(uint16_t sockfd);
  136. /// socket used to sending data
  137. /// TODO: this should be protected
  138. SocketCollection sockets_;
  139. protected:
  140. /// network interface name
  141. std::string name_;
  142. /// interface index (a value that uniquely indentifies an interface)
  143. int ifindex_;
  144. /// list of assigned addresses
  145. AddressCollection addrs_;
  146. public:
  147. /// link-layer address
  148. uint8_t mac_[MAX_MAC_LEN];
  149. /// length of link-layer address (usually 6)
  150. int mac_len_;
  151. /// specifies if selected interface is loopback
  152. bool flag_loopback_;
  153. /// specifies if selected interface is up
  154. bool flag_up_;
  155. /// flag specifies if selected interface is running
  156. /// (e.g. cable plugged in, wifi associated)
  157. bool flag_running_;
  158. /// flag specifies if selected interface is multicast capable
  159. bool flag_multicast_;
  160. /// flag specifies if selected interface is broadcast capable
  161. bool flag_broadcast_;
  162. /// interface flags (this value is as is returned by OS,
  163. /// it may mean different things on different OSes)
  164. uint32_t flags_;
  165. /// hardware type
  166. uint16_t hardware_type_;
  167. };
  168. // TODO performance improvement: we may change this into
  169. // 2 maps (ifindex-indexed and name-indexed) and
  170. // also hide it (make it public make tests easier for now)
  171. /// type that holds a list of interfaces
  172. typedef std::list<Iface> IfaceCollection;
  173. /// IfaceMgr is a singleton class. This method returns reference
  174. /// to its sole instance.
  175. ///
  176. /// @return the only existing instance of interface manager
  177. static IfaceMgr& instance();
  178. /// @brief Returns interface with specified interface index
  179. ///
  180. /// @param ifindex index of searched interface
  181. ///
  182. /// @return interface with requested index (or NULL if no such
  183. /// interface is present)
  184. ///
  185. Iface* getIface(int ifindex);
  186. /// @brief Returns interface with specified interface name
  187. ///
  188. /// @param ifname name of searched interface
  189. ///
  190. /// @return interface with requested name (or NULL if no such
  191. /// interface is present)
  192. ///
  193. Iface*
  194. getIface(const std::string& ifname);
  195. /// @brief Return most suitable socket for transmitting specified IPv6 packet.
  196. ///
  197. /// This method takes Pkt6 (see overloaded implementation that takes
  198. /// Pkt4) and chooses appropriate socket to send it. This method
  199. /// may throw BadValue if specified packet does not have outbound
  200. /// interface specified, no such interface exists, or specified
  201. /// interface does not have any appropriate sockets open.
  202. ///
  203. /// @param pkt a packet to be transmitted
  204. ///
  205. /// @return a socket descriptor
  206. uint16_t getSocket(const isc::dhcp::Pkt6& pkt);
  207. /// @brief Return most suitable socket for transmitting specified IPv6 packet.
  208. ///
  209. /// This method takes Pkt4 (see overloaded implementation that takes
  210. /// Pkt6) and chooses appropriate socket to send it. This method
  211. /// may throw BadValue if specified packet does not have outbound
  212. /// interface specified, no such interface exists, or specified
  213. /// interface does not have any appropriate sockets open.
  214. ///
  215. /// @param pkt a packet to be transmitted
  216. ///
  217. /// @return a socket descriptor
  218. uint16_t getSocket(const isc::dhcp::Pkt4& pkt);
  219. /// debugging method that prints out all available interfaces
  220. ///
  221. /// @param out specifies stream to print list of interfaces to
  222. void
  223. printIfaces(std::ostream& out = std::cout);
  224. /// @brief Sends an IPv6 packet.
  225. ///
  226. /// Sends an IPv6 packet. All parameters for actual transmission are specified in
  227. /// Pkt6 structure itself. That includes destination address, src/dst port
  228. /// and interface over which data will be sent.
  229. ///
  230. /// @param pkt packet to be sent
  231. ///
  232. /// @return true if sending was successful
  233. bool send(boost::shared_ptr<Pkt6>& pkt);
  234. /// @brief Sends an IPv4 packet.
  235. ///
  236. /// Sends an IPv4 packet. All parameters for actual transmission are specified
  237. /// in Pkt4 structure itself. That includes destination address, src/dst
  238. /// port and interface over which data will be sent.
  239. ///
  240. /// @param pkt a packet to be sent
  241. ///
  242. /// @return true if sending was successful
  243. bool send(boost::shared_ptr<Pkt4>& pkt);
  244. /// @brief Tries to receive IPv6 packet over open IPv6 sockets.
  245. ///
  246. /// Attempts to receive a single IPv6 packet of any of the open IPv6 sockets.
  247. /// If reception is successful and all information about its sender
  248. /// are obtained, Pkt6 object is created and returned.
  249. ///
  250. /// TODO Start using select() and add timeout to be able
  251. /// to not wait infinitely, but rather do something useful
  252. /// (e.g. remove expired leases)
  253. ///
  254. /// @return Pkt6 object representing received packet (or NULL)
  255. boost::shared_ptr<Pkt6> receive6();
  256. /// @brief Tries to receive IPv4 packet over open IPv4 sockets.
  257. ///
  258. /// Attempts to receive a single IPv4 packet of any of the open IPv4 sockets.
  259. /// If reception is successful and all information about its sender
  260. /// are obtained, Pkt4 object is created and returned.
  261. ///
  262. /// TODO Start using select() and add timeout to be able
  263. /// to not wait infinitely, but rather do something useful
  264. /// (e.g. remove expired leases)
  265. ///
  266. /// @return Pkt4 object representing received packet (or NULL)
  267. boost::shared_ptr<Pkt4> receive4();
  268. /// Opens UDP/IP socket and binds it to address, interface and port.
  269. ///
  270. /// Specific type of socket (UDP/IPv4 or UDP/IPv6) depends on passed addr
  271. /// family.
  272. ///
  273. /// @param ifname name of the interface
  274. /// @param addr address to be bound.
  275. /// @param port UDP port.
  276. ///
  277. /// Method will throw if socket creation, socket binding or multicast
  278. /// join fails.
  279. ///
  280. /// @return socket descriptor, if socket creation, binding and multicast
  281. /// group join were all successful.
  282. int openSocket(const std::string& ifname,
  283. const isc::asiolink::IOAddress& addr, int port);
  284. /// Opens IPv6 sockets on detected interfaces.
  285. ///
  286. /// Will throw exception if socket creation fails.
  287. ///
  288. /// @param port specifies port number (usually DHCP6_SERVER_PORT)
  289. ///
  290. /// @return true if any sockets were open
  291. bool openSockets6(uint16_t port = DHCP6_SERVER_PORT);
  292. /// @brief Closes all open sockets.
  293. /// Is used in destructor, but also from Dhcpv4_srv and Dhcpv6_srv classes.
  294. void closeSockets();
  295. /// Opens IPv4 sockets on detected interfaces.
  296. /// Will throw exception if socket creation fails.
  297. ///
  298. /// @param port specifies port number (usually DHCP6_SERVER_PORT)
  299. ///
  300. /// @return true if any sockets were open
  301. bool openSockets4(uint16_t port = DHCP4_SERVER_PORT);
  302. /// @brief returns number of detected interfaces
  303. ///
  304. /// @return number of detected interfaces
  305. uint16_t countIfaces() { return ifaces_.size(); }
  306. // don't use private, we need derived classes in tests
  307. protected:
  308. /// @brief Protected constructor.
  309. ///
  310. /// Protected constructor. This is a singleton class. We don't want
  311. /// anyone to create instances of IfaceMgr. Use instance() method instead.
  312. IfaceMgr();
  313. virtual ~IfaceMgr();
  314. /// @brief Opens IPv4 socket.
  315. ///
  316. /// Please do not use this method directly. Use openSocket instead.
  317. ///
  318. /// This method may throw exception if socket creation fails.
  319. ///
  320. /// @param iface reference to interface structure.
  321. /// @param addr an address the created socket should be bound to
  322. /// @param port a port that created socket should be bound to
  323. ///
  324. /// @return socket descriptor
  325. int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
  326. /// @brief Opens IPv6 socket.
  327. ///
  328. /// Please do not use this method directly. Use openSocket instead.
  329. ///
  330. /// This method may throw exception if socket creation fails.
  331. ///
  332. /// @param iface reference to interface structure.
  333. /// @param addr an address the created socket should be bound to
  334. /// @param port a port that created socket should be bound to
  335. ///
  336. /// @return socket descriptor
  337. int openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
  338. /// @brief Adds an interface to list of known interfaces.
  339. ///
  340. /// @param iface reference to Iface object.
  341. void addInterface(const Iface& iface) {
  342. ifaces_.push_back(iface);
  343. }
  344. /// @brief Detects network interfaces.
  345. ///
  346. /// This method will eventually detect available interfaces. For now
  347. /// it offers stub implementation. First interface name and link-local
  348. /// IPv6 address is read from intefaces.txt file.
  349. void
  350. detectIfaces();
  351. /// @brief Stub implementation of network interface detection.
  352. ///
  353. /// This implementations reads a single line from interfaces.txt file
  354. /// and pretends to detect such interface. First interface name and
  355. /// link-local IPv6 address or IPv4 address is read from the
  356. /// intefaces.txt file.
  357. void
  358. stubDetectIfaces();
  359. // TODO: having 2 maps (ifindex->iface and ifname->iface would)
  360. // probably be better for performance reasons
  361. /// List of available interfaces
  362. IfaceCollection ifaces_;
  363. /// a pointer to a sole instance of this class (a singleton)
  364. static IfaceMgr * instance_;
  365. // TODO: Also keep this interface on Iface once interface detection
  366. // is implemented. We may need it e.g. to close all sockets on
  367. // specific interface
  368. //int recvsock_; // TODO: should be fd_set eventually, but we have only
  369. //int sendsock_; // 2 sockets for now. Will do for until next release
  370. // we can't use the same socket, as receiving socket
  371. // is bound to multicast address. And we all know what happens
  372. // to people who try to use multicast as source address.
  373. /// length of the control_buf_ array
  374. int control_buf_len_;
  375. /// control-buffer, used in transmission and reception
  376. boost::scoped_array<char> control_buf_;
  377. private:
  378. /// creates a single instance of this class (a singleton implementation)
  379. static void
  380. instanceCreate();
  381. /// @brief Joins IPv6 multicast group on a socket.
  382. ///
  383. /// Socket must be created and bound to an address. Note that this
  384. /// address is different than the multicast address. For example DHCPv6
  385. /// server should bind its socket to link-local address (fe80::1234...)
  386. /// and later join ff02::1:2 multicast group.
  387. ///
  388. /// @param sock socket fd (socket must be bound)
  389. /// @param ifname interface name (for link-scoped multicast groups)
  390. /// @param mcast multicast address to join (e.g. "ff02::1:2")
  391. ///
  392. /// @return true if multicast join was successful
  393. ///
  394. bool
  395. joinMulticast(int sock, const std::string& ifname,
  396. const std::string& mcast);
  397. };
  398. }; // namespace isc::dhcp
  399. }; // namespace isc
  400. #endif