iface_mgr.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "io_address.h"
  18. #include "dhcp/pkt6.h"
  19. namespace isc {
  20. /**
  21. * IfaceMgr is an interface manager class that detects available network
  22. * interfaces, configured addresses, link-local addresses, and provides
  23. * API for using sockets.
  24. *
  25. */
  26. class IfaceMgr {
  27. public:
  28. typedef std::list<isc::asiolink::IOAddress> Addr6Lst;
  29. struct Iface { // TODO: could be a class as well
  30. std::string name_; // network interface name
  31. int ifindex_; // interface index (a value that uniquely indentifies
  32. // an interface
  33. Addr6Lst addrs_;
  34. char mac_[20]; // Infiniband used 20 bytes indentifiers
  35. int mac_len_;
  36. Iface(const std::string& name, int ifindex);
  37. std::string getFullName() const;
  38. std::string getPlainMac() const;
  39. int sendsock_; // socket used to sending data
  40. int recvsock_; // socket used for receiving data
  41. // next field is not needed, let's keep it in cointainers
  42. };
  43. // TODO performance improvement: we may change this into
  44. // 2 maps (ifindex-indexed and name-indexed) and
  45. // also hide it (make it public make tests easier for now)
  46. typedef std::list<Iface> IfaceLst;
  47. static IfaceMgr& instance();
  48. Iface * getIface(int ifindex);
  49. Iface * getIface(const std::string& ifname);
  50. void printIfaces(std::ostream& out = std::cout);
  51. bool send(Pkt6& pkt);
  52. Pkt6* receive();
  53. // don't use private, we need derived classes in tests
  54. protected:
  55. IfaceMgr(); // don't create IfaceMgr directly, use instance() method
  56. ~IfaceMgr();
  57. void detectIfaces();
  58. int openSocket(const std::string& ifname,
  59. const isc::asiolink::IOAddress& addr,
  60. int port);
  61. // TODO: having 2 maps (ifindex->iface and ifname->iface would)
  62. // probably be better for performance reasons
  63. IfaceLst ifaces_;
  64. static IfaceMgr * instance_;
  65. // TODO: Also keep this interface on Iface once interface detection
  66. // is implemented. We may need it e.g. to close all sockets on
  67. // specific interface
  68. int recvsock_; // TODO: should be fd_set eventually, but we have only
  69. int sendsock_; // 2 sockets for now. Will do for until next release
  70. // we can't use the same socket, as receiving socket
  71. // is bound to multicast address. And we all know what happens
  72. // to people who try to use multicast as source address.
  73. char * control_buf_;
  74. int control_buf_len_;
  75. private:
  76. bool openSockets();
  77. static void instanceCreate();
  78. bool joinMcast(int sock, const std::string& ifname,
  79. const std::string& mcast);
  80. };
  81. };
  82. #endif