iface_mgr_sun.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2011, 2013 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. #include <config.h>
  15. #if defined(OS_SUN)
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcp/pkt_filter_inet.h>
  18. #include <exceptions/exceptions.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <net/if_dl.h>
  22. #include <net/if.h>
  23. #include <ifaddrs.h>
  24. using namespace std;
  25. using namespace isc;
  26. using namespace isc::asiolink;
  27. using namespace isc::dhcp;
  28. namespace isc {
  29. namespace dhcp {
  30. /// This is a Solaris specific interface detection code. It works on Solaris 11
  31. /// only, as earlier versions did not support getifaddrs() API.
  32. void
  33. IfaceMgr::detectIfaces() {
  34. struct ifaddrs * iflist = 0, * ifptr = 0;
  35. // Gets list of ifaddrs struct
  36. if(getifaddrs(& iflist) != 0) {
  37. isc_throw(Unexpected, "Network interfaces detection failed.");
  38. }
  39. typedef std::map<string, Iface> ifaceLst;
  40. ifaceLst::iterator iface_iter;
  41. ifaceLst ifaces;
  42. // First lookup for getting interfaces ...
  43. for(ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  44. const char * ifname = ifptr->ifa_name;
  45. uint ifindex = 0;
  46. if (!(ifindex = if_nametoindex(ifname))) {
  47. // Interface name does not have corresponding index ...
  48. freeifaddrs(iflist);
  49. isc_throw(Unexpected, "Interface " << ifname << " has no index");
  50. }
  51. iface_iter = ifaces.find(ifname);
  52. if (iface_iter != ifaces.end()) {
  53. continue;
  54. }
  55. Iface iface(ifname, ifindex);
  56. iface.setFlags(ifptr->ifa_flags);
  57. ifaces.insert(pair<string, Iface>(ifname, iface));
  58. }
  59. // Second lookup to get MAC and IP addresses
  60. for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  61. iface_iter = ifaces.find(ifptr->ifa_name);
  62. if (iface_iter == ifaces.end()) {
  63. continue;
  64. }
  65. // Common byte pointer for following data
  66. const uint8_t * ptr = 0;
  67. if (ifptr->ifa_addr->sa_family == AF_LINK) {
  68. // HWAddr
  69. struct sockaddr_dl * ldata =
  70. reinterpret_cast<struct sockaddr_dl *>(ifptr->ifa_addr);
  71. ptr = reinterpret_cast<uint8_t *>(LLADDR(ldata));
  72. iface_iter->second.setHWType(ldata->sdl_type);
  73. iface_iter->second.setMac(ptr, ldata->sdl_alen);
  74. } else if (ifptr->ifa_addr->sa_family == AF_INET6) {
  75. // IPv6 Addr
  76. struct sockaddr_in6 * adata =
  77. reinterpret_cast<struct sockaddr_in6 *>(ifptr->ifa_addr);
  78. ptr = reinterpret_cast<uint8_t *>(& adata->sin6_addr);
  79. IOAddress a = IOAddress::fromBytes(AF_INET6, ptr);
  80. iface_iter->second.addAddress(a);
  81. } else {
  82. // IPv4 Addr
  83. struct sockaddr_in * adata =
  84. reinterpret_cast<struct sockaddr_in *>(ifptr->ifa_addr);
  85. ptr = reinterpret_cast<uint8_t *>(& adata->sin_addr);
  86. IOAddress a = IOAddress::fromBytes(AF_INET, ptr);
  87. iface_iter->second.addAddress(a);
  88. }
  89. }
  90. freeifaddrs(iflist);
  91. // Interfaces registering
  92. for (ifaceLst::const_iterator iface_iter = ifaces.begin();
  93. iface_iter != ifaces.end(); ++iface_iter) {
  94. ifaces_.push_back(iface_iter->second);
  95. }
  96. }
  97. /// @brief sets flag_*_ fields
  98. ///
  99. /// Like Linux version, os specific flags
  100. ///
  101. /// @params flags
  102. void Iface::setFlags(uint64_t flags) {
  103. flags_ = flags;
  104. flag_loopback_ = flags & IFF_LOOPBACK;
  105. flag_up_ = flags & IFF_UP;
  106. flag_running_ = flags & IFF_RUNNING;
  107. flag_multicast_ = flags & IFF_MULTICAST;
  108. flag_broadcast_ = flags & IFF_BROADCAST;
  109. }
  110. void IfaceMgr::os_send4(struct msghdr& /*m*/,
  111. boost::scoped_array<char>& /*control_buf*/,
  112. size_t /*control_buf_len*/,
  113. const Pkt4Ptr& /*pkt*/) {
  114. // @todo: Are there any specific actions required before sending IPv4 packet
  115. // on Solaris based systems? See iface_mgr_linux.cc
  116. // for working Linux implementation.
  117. }
  118. bool IfaceMgr::os_receive4(struct msghdr& /*m*/, Pkt4Ptr& /*pkt*/) {
  119. // @todo: Are there any specific actions required before receiving IPv4 packet
  120. // on BSDs? See iface_mgr_linux.cc for working Linux implementation.
  121. return (true); // pretend that we have everything set up for reception.
  122. }
  123. void
  124. IfaceMgr::setMatchingPacketFilter(const bool /* direct_response_desired */) {
  125. // @todo Currently we ignore the preference to use direct traffic
  126. // because it hasn't been implemented for Solaris.
  127. setPacketFilter(PktFilterPtr(new PktFilterInet()));
  128. }
  129. } // end of isc::dhcp namespace
  130. } // end of dhcp namespace
  131. #endif