iface_mgr.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. #include <sstream>
  15. #include <fstream>
  16. #include <string.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <dhcp/dhcp6.h>
  20. #include <dhcp6/iface_mgr.h>
  21. #include <exceptions/exceptions.h>
  22. using namespace std;
  23. using namespace isc;
  24. using namespace isc::asiolink;
  25. using namespace isc::dhcp;
  26. namespace isc {
  27. /// IfaceMgr is a singleton implementation
  28. IfaceMgr* IfaceMgr::instance_ = 0;
  29. void
  30. IfaceMgr::instanceCreate() {
  31. if (instance_) {
  32. // no need to do anything. Instance is already created.
  33. // Who called it again anyway? Uh oh. Had to be us, as
  34. // this is private method.
  35. return;
  36. }
  37. instance_ = new IfaceMgr();
  38. }
  39. IfaceMgr&
  40. IfaceMgr::instance() {
  41. if (instance_ == 0) {
  42. instanceCreate();
  43. }
  44. return (*instance_);
  45. }
  46. IfaceMgr::Iface::Iface(const std::string& name, int ifindex)
  47. :name_(name), ifindex_(ifindex), mac_len_(0) {
  48. memset(mac_, 0, sizeof(mac_));
  49. }
  50. std::string
  51. IfaceMgr::Iface::getFullName() const {
  52. ostringstream tmp;
  53. tmp << name_ << "/" << ifindex_;
  54. return (tmp.str());
  55. }
  56. std::string
  57. IfaceMgr::Iface::getPlainMac() const {
  58. ostringstream tmp;
  59. tmp.fill('0');
  60. tmp << hex;
  61. for (int i = 0; i < mac_len_; i++) {
  62. tmp.width(2);
  63. tmp << mac_[i];
  64. if (i < mac_len_-1) {
  65. tmp << ":";
  66. }
  67. }
  68. return (tmp.str());
  69. }
  70. bool IfaceMgr::Iface::delAddress(const isc::asiolink::IOAddress& addr) {
  71. // Let's delete all addresses that match. It really shouldn't matter
  72. // if we delete first or all, as the OS should allow to add a single
  73. // address to an interface only once. If OS allows multiple instances
  74. // of the same address added, we are in deep problems anyway.
  75. size_t size = addrs_.size();
  76. addrs_.erase(remove(addrs_.begin(), addrs_.end(), addr), addrs_.end());
  77. if (addrs_.size() < size) {
  78. return (true);
  79. } else {
  80. return (false);
  81. }
  82. }
  83. bool IfaceMgr::Iface::delSocket(uint16_t sockfd) {
  84. list<SocketInfo>::iterator sock = sockets_.begin();
  85. while (sock!=sockets_.end()) {
  86. if (sock->sockfd_ == sockfd) {
  87. close(sockfd);
  88. sockets_.erase(sock);
  89. return (true); //socket found
  90. }
  91. ++sock;
  92. }
  93. return (false); // socket not found
  94. }
  95. IfaceMgr::IfaceMgr()
  96. :control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))),
  97. control_buf_(new char[control_buf_len_])
  98. {
  99. cout << "IfaceMgr initialization." << endl;
  100. try {
  101. // required for sending/receiving packets
  102. // let's keep it in front, just in case someone
  103. // wants to send anything during initialization
  104. // control_buf_ = boost::scoped_array<char>();
  105. detectIfaces();
  106. } catch (const std::exception& ex) {
  107. cout << "IfaceMgr creation failed:" << ex.what() << endl;
  108. // TODO Uncomment this (or call LOG_FATAL) once
  109. // interface detection is implemented. Otherwise
  110. // it is not possible to run tests in a portable
  111. // way (see detectIfaces() method).
  112. // throw ex;
  113. }
  114. }
  115. IfaceMgr::~IfaceMgr() {
  116. for (IfaceCollection::iterator iface = ifaces_.begin();
  117. iface != ifaces_.end(); ++iface) {
  118. for (SocketCollection::iterator sock = iface->sockets_.begin();
  119. sock != iface->sockets_.end(); ++sock) {
  120. cout << "Closing socket " << sock->sockfd_ << endl;
  121. close(sock->sockfd_);
  122. }
  123. iface->sockets_.clear();
  124. }
  125. // control_buf_ is deleted automatically (scoped_ptr)
  126. control_buf_len_ = 0;
  127. }
  128. void
  129. IfaceMgr::detectIfaces() {
  130. string ifaceName, linkLocal;
  131. // TODO do the actual detection. Currently interface detection is faked
  132. // by reading a text file.
  133. cout << "Interface detection is not implemented yet. "
  134. << "Reading interfaces.txt file instead." << endl;
  135. cout << "Please use format: interface-name link-local-address" << endl;
  136. try {
  137. ifstream interfaces("interfaces.txt");
  138. if (!interfaces.good()) {
  139. cout << "Failed to read interfaces.txt file." << endl;
  140. isc_throw(Unexpected, "Failed to read interfaces.txt");
  141. }
  142. interfaces >> ifaceName;
  143. interfaces >> linkLocal;
  144. cout << "Detected interface " << ifaceName << "/" << linkLocal << endl;
  145. Iface iface(ifaceName, if_nametoindex( ifaceName.c_str() ) );
  146. IOAddress addr(linkLocal);
  147. iface.addAddress(addr);
  148. addInterface(iface);
  149. interfaces.close();
  150. } catch (const std::exception& ex) {
  151. // TODO: deallocate whatever memory we used
  152. // not that important, since this function is going to be
  153. // thrown away as soon as we get proper interface detection
  154. // implemented
  155. // TODO Do LOG_FATAL here
  156. std::cerr << "Interface detection failed." << std::endl;
  157. throw ex;
  158. }
  159. }
  160. void
  161. IfaceMgr::openSockets(uint16_t port) {
  162. int sock1, sock2;
  163. for (IfaceCollection::iterator iface = ifaces_.begin();
  164. iface != ifaces_.end(); ++iface) {
  165. AddressCollection addrs = iface->getAddresses();
  166. for (AddressCollection::iterator addr = addrs.begin();
  167. addr != addrs.end();
  168. ++addr) {
  169. sock1 = openSocket(iface->getName(), *addr, port);
  170. if (sock1 < 0) {
  171. isc_throw(Unexpected, "Failed to open unicast socket on "
  172. << " interface " << iface->getFullName());
  173. }
  174. if ( !joinMcast(sock1, iface->getName(),
  175. string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
  176. close(sock1);
  177. isc_throw(Unexpected, "Failed to join " << ALL_DHCP_RELAY_AGENTS_AND_SERVERS
  178. << " multicast group.");
  179. }
  180. // this doesn't work too well on NetBSD
  181. sock2 = openSocket(iface->getName(),
  182. IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
  183. port);
  184. if (sock2 < 0) {
  185. isc_throw(Unexpected, "Failed to open multicast socket on "
  186. << " interface " << iface->getFullName());
  187. iface->delSocket(sock1); // delete previously opened socket
  188. }
  189. }
  190. }
  191. }
  192. void
  193. IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
  194. for (IfaceCollection::const_iterator iface = ifaces_.begin();
  195. iface != ifaces_.end(); ++iface) {
  196. out << "Detected interface " << iface->getFullName() << endl;
  197. out << " " << iface->getAddresses().size() << " addr(s):" << endl;
  198. const AddressCollection addrs = iface->getAddresses();
  199. for (AddressCollection::const_iterator addr = addrs.begin();
  200. addr != addrs.end(); ++addr) {
  201. out << " " << addr->toText() << endl;
  202. }
  203. out << " mac: " << iface->getPlainMac() << endl;
  204. }
  205. }
  206. IfaceMgr::Iface*
  207. IfaceMgr::getIface(int ifindex) {
  208. for (IfaceCollection::iterator iface = ifaces_.begin();
  209. iface != ifaces_.end(); ++iface) {
  210. if (iface->getIndex() == ifindex) {
  211. return (&(*iface));
  212. }
  213. }
  214. return (NULL); // not found
  215. }
  216. IfaceMgr::Iface*
  217. IfaceMgr::getIface(const std::string& ifname) {
  218. for (IfaceCollection::iterator iface = ifaces_.begin();
  219. iface != ifaces_.end(); ++iface) {
  220. if (iface->getName() == ifname) {
  221. return (&(*iface));
  222. }
  223. }
  224. return (NULL); // not found
  225. }
  226. uint16_t
  227. IfaceMgr::openSocket(const std::string& ifname,
  228. const IOAddress& addr,
  229. int port) {
  230. Iface* iface = getIface(ifname);
  231. if (!iface) {
  232. isc_throw(BadValue, "There is no " << ifname << " interface present.");
  233. }
  234. switch (addr.getFamily()) {
  235. case AF_INET:
  236. return openSocket4(*iface, addr, port);
  237. case AF_INET6:
  238. return openSocket6(*iface, addr, port);
  239. default:
  240. isc_throw(BadValue, "Failed to detect family of address: "
  241. << addr.toText());
  242. }
  243. }
  244. uint16_t
  245. IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
  246. cout << "Creating UDP4 socket on " << iface.getFullName()
  247. << " " << addr.toText() << "/port=" << port << endl;
  248. struct sockaddr_in addr4;
  249. memset(&addr4, 0, sizeof(sockaddr));
  250. addr4.sin_family = AF_INET;
  251. addr4.sin_port = htons(port);
  252. memcpy(&addr4.sin_addr, addr.getAddress().to_v4().to_bytes().data(),
  253. sizeof(addr4.sin_addr));
  254. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  255. if (sock < 0) {
  256. isc_throw(Unexpected, "Failed to create UDP6 socket.");
  257. }
  258. if (bind(sock, (struct sockaddr *)&addr4, sizeof(addr4)) < 0) {
  259. close(sock);
  260. isc_throw(Unexpected, "Failed to bind socket " << sock << " to " << addr.toText()
  261. << "/port=" << port);
  262. }
  263. // If there is no support for IP_PKTINFO, we are really out of luck.
  264. // It will be difficult to understand, where this packet came from.
  265. #if defined(IP_PKTINFO)
  266. int flag = 1;
  267. if (setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &flag, sizeof(flag)) != 0) {
  268. close(sock);
  269. isc_throw(Unexpected, "setsockopt: IP_PKTINFO: failed.");
  270. }
  271. #endif
  272. cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
  273. addr.toText() << "/port=" << port << endl;
  274. iface.addSocket(SocketInfo(sock, addr, port));
  275. return (sock);
  276. }
  277. uint16_t
  278. IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
  279. cout << "Creating UDP6 socket on " << iface.getFullName()
  280. << " " << addr.toText() << "/port=" << port << endl;
  281. struct sockaddr_in6 addr6;
  282. memset(&addr6, 0, sizeof(addr6));
  283. addr6.sin6_family = AF_INET6;
  284. addr6.sin6_port = htons(port);
  285. addr6.sin6_scope_id = if_nametoindex(iface.getName().c_str());
  286. memcpy(&addr6.sin6_addr,
  287. addr.getAddress().to_v6().to_bytes().data(),
  288. sizeof(addr6.sin6_addr));
  289. #ifdef HAVE_SA_LEN
  290. addr6->sin6_len = sizeof(addr6);
  291. #endif
  292. // TODO: use sockcreator once it becomes available
  293. // make a socket
  294. int sock = socket(AF_INET6, SOCK_DGRAM, 0);
  295. if (sock < 0) {
  296. isc_throw(Unexpected, "Failed to create UDP6 socket.");
  297. }
  298. // Set the REUSEADDR option so that we don't fail to start if
  299. // we're being restarted.
  300. int flag = 1;
  301. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  302. (char *)&flag, sizeof(flag)) < 0) {
  303. close(sock);
  304. isc_throw(Unexpected, "Can't set SO_REUSEADDR option on dhcpv6 socket.");
  305. }
  306. if (bind(sock, (struct sockaddr *)&addr6, sizeof(addr6)) < 0) {
  307. close(sock);
  308. isc_throw(Unexpected, "Failed to bind socket " << sock << " to " << addr.toText()
  309. << "/port=" << port);
  310. }
  311. #ifdef IPV6_RECVPKTINFO
  312. // RFC3542 - a new way
  313. if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
  314. &flag, sizeof(flag)) != 0) {
  315. close(sock);
  316. isc_throw(Unexpected, "setsockopt: IPV6_RECVPKTINFO failed.");
  317. }
  318. #else
  319. // RFC2292 - an old way
  320. if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO,
  321. &flag, sizeof(flag)) != 0) {
  322. close(sock);
  323. isc_throw(Unexpected, "setsockopt: IPV6_PKTINFO: failed.");
  324. }
  325. #endif
  326. // multicast stuff
  327. if (addr.getAddress().to_v6().is_multicast()) {
  328. // both mcast (ALL_DHCP_RELAY_AGENTS_AND_SERVERS and ALL_DHCP_SERVERS)
  329. // are link and site-scoped, so there is no sense to join those groups
  330. // with global addresses.
  331. if ( !joinMcast( sock, iface.getName(),
  332. string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
  333. close(sock);
  334. isc_throw(Unexpected, "Failed to join " << ALL_DHCP_RELAY_AGENTS_AND_SERVERS
  335. << " multicast group.");
  336. }
  337. }
  338. cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
  339. addr.toText() << "/port=" << port << endl;
  340. iface.addSocket(SocketInfo(sock, addr, port));
  341. return (sock);
  342. }
  343. bool
  344. IfaceMgr::joinMcast(int sock, const std::string& ifname,
  345. const std::string & mcast) {
  346. struct ipv6_mreq mreq;
  347. if (inet_pton(AF_INET6, mcast.c_str(),
  348. &mreq.ipv6mr_multiaddr) <= 0) {
  349. cout << "Failed to convert " << ifname
  350. << " to IPv6 multicast address." << endl;
  351. return (false);
  352. }
  353. mreq.ipv6mr_interface = if_nametoindex(ifname.c_str());
  354. if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  355. &mreq, sizeof(mreq)) < 0) {
  356. cout << "Failed to join " << mcast << " multicast group." << endl;
  357. return (false);
  358. }
  359. cout << "Joined multicast " << mcast << " group." << endl;
  360. return (true);
  361. }
  362. bool
  363. IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
  364. struct msghdr m;
  365. struct iovec v;
  366. int result;
  367. struct in6_pktinfo *pktinfo;
  368. struct cmsghdr *cmsg;
  369. Iface* iface = getIface(pkt->iface_);
  370. if (!iface) {
  371. isc_throw(BadValue, "Unable to send Pkt6. Invalid interface ("
  372. << pkt->iface_ << ") specified.");
  373. }
  374. memset(&control_buf_[0], 0, control_buf_len_);
  375. // Initialize our message header structure.
  376. memset(&m, 0, sizeof(m));
  377. // Set the target address we're sending to.
  378. sockaddr_in6 to;
  379. memset(&to, 0, sizeof(to));
  380. to.sin6_family = AF_INET6;
  381. to.sin6_port = htons(pkt->remote_port_);
  382. memcpy(&to.sin6_addr,
  383. pkt->remote_addr_.getAddress().to_v6().to_bytes().data(),
  384. 16);
  385. to.sin6_scope_id = pkt->ifindex_;
  386. m.msg_name = &to;
  387. m.msg_namelen = sizeof(to);
  388. // Set the data buffer we're sending. (Using this wacky
  389. // "scatter-gather" stuff... we only have a single chunk
  390. // of data to send, so we declare a single vector entry.)
  391. v.iov_base = (char *) &pkt->data_[0];
  392. v.iov_len = pkt->data_len_;
  393. m.msg_iov = &v;
  394. m.msg_iovlen = 1;
  395. // Setting the interface is a bit more involved.
  396. //
  397. // We have to create a "control message", and set that to
  398. // define the IPv6 packet information. We could set the
  399. // source address if we wanted, but we can safely let the
  400. // kernel decide what that should be.
  401. m.msg_control = &control_buf_[0];
  402. m.msg_controllen = control_buf_len_;
  403. cmsg = CMSG_FIRSTHDR(&m);
  404. cmsg->cmsg_level = IPPROTO_IPV6;
  405. cmsg->cmsg_type = IPV6_PKTINFO;
  406. cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
  407. pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
  408. memset(pktinfo, 0, sizeof(*pktinfo));
  409. pktinfo->ipi6_ifindex = pkt->ifindex_;
  410. m.msg_controllen = cmsg->cmsg_len;
  411. result = sendmsg(getSocket(*pkt), &m, 0);
  412. if (result < 0) {
  413. cout << "Send packet failed." << endl;
  414. }
  415. cout << "Sent " << pkt->data_len_ << " bytes over socket " << getSocket(*pkt)
  416. << " on " << iface->getFullName() << " interface: "
  417. << " dst=" << pkt->remote_addr_.toText()
  418. << ", src=" << pkt->local_addr_.toText()
  419. << endl;
  420. return (result);
  421. }
  422. bool
  423. IfaceMgr::send(boost::shared_ptr<Pkt4>& )
  424. {
  425. /// TODO: Implement this (ticket #1240)
  426. isc_throw(NotImplemented, "Pkt4 send not implemented yet.");
  427. }
  428. boost::shared_ptr<Pkt4>
  429. IfaceMgr::receive4() {
  430. isc_throw(NotImplemented, "Pkt4 reception not implemented yet.");
  431. // TODO: To be implemented (ticket #1239)
  432. return (boost::shared_ptr<Pkt4>()); // NULL
  433. }
  434. boost::shared_ptr<Pkt6>
  435. IfaceMgr::receive6() {
  436. struct msghdr m;
  437. struct iovec v;
  438. int result;
  439. struct cmsghdr* cmsg;
  440. struct in6_pktinfo* pktinfo;
  441. struct sockaddr_in6 from;
  442. struct in6_addr to_addr;
  443. boost::shared_ptr<Pkt6> pkt;
  444. char addr_str[INET6_ADDRSTRLEN];
  445. try {
  446. // RFC3315 states that server responses may be
  447. // fragmented if they are over MTU. There is no
  448. // text whether client's packets may be larger
  449. // than 1500. Nevertheless to be on the safe side
  450. // we use larger buffer. This buffer limit is checked
  451. // during reception (see iov_len below), so we are
  452. // safe
  453. pkt = boost::shared_ptr<Pkt6>(new Pkt6(65536));
  454. } catch (const std::exception& ex) {
  455. cout << "Failed to create new packet." << endl;
  456. return (boost::shared_ptr<Pkt6>()); // NULL
  457. }
  458. memset(&control_buf_[0], 0, control_buf_len_);
  459. memset(&from, 0, sizeof(from));
  460. memset(&to_addr, 0, sizeof(to_addr));
  461. // Initialize our message header structure.
  462. memset(&m, 0, sizeof(m));
  463. // Point so we can get the from address.
  464. m.msg_name = &from;
  465. m.msg_namelen = sizeof(from);
  466. // Set the data buffer we're receiving. (Using this wacky
  467. // "scatter-gather" stuff... but we that doesn't really make
  468. // sense for us, so we use a single vector entry.)
  469. v.iov_base = (void*)&pkt->data_[0];
  470. v.iov_len = pkt->data_len_;
  471. m.msg_iov = &v;
  472. m.msg_iovlen = 1;
  473. // Getting the interface is a bit more involved.
  474. //
  475. // We set up some space for a "control message". We have
  476. // previously asked the kernel to give us packet
  477. // information (when we initialized the interface), so we
  478. // should get the destination address from that.
  479. m.msg_control = &control_buf_[0];
  480. m.msg_controllen = control_buf_len_;
  481. /// TODO: Need to move to select() and pool over
  482. /// all available sockets. For now, we just take the
  483. /// first interface and use first socket from it.
  484. IfaceCollection::const_iterator iface = ifaces_.begin();
  485. if (iface == ifaces_.end()) {
  486. isc_throw(Unexpected, "No interfaces detected. Can't receive anything.");
  487. }
  488. SocketCollection::const_iterator s = iface->sockets_.begin();
  489. const SocketInfo* candidate = 0;
  490. while (s != iface->sockets_.end()) {
  491. if (s->addr_.getAddress().to_v6().is_multicast()) {
  492. candidate = &(*s);
  493. break;
  494. }
  495. if (!candidate) {
  496. candidate = &(*s); // it's not multicast, but it's better than none
  497. }
  498. ++s;
  499. }
  500. if (!candidate) {
  501. isc_throw(Unexpected, "Interface " << iface->getFullName()
  502. << " does not have any sockets open.");
  503. }
  504. cout << "Trying to receive over socket " << candidate->sockfd_ << " bound to "
  505. << candidate->addr_.toText() << "/port=" << candidate->port_ << " on "
  506. << iface->getFullName() << endl;
  507. result = recvmsg(candidate->sockfd_, &m, 0);
  508. if (result >= 0) {
  509. // If we did read successfully, then we need to loop
  510. // through the control messages we received and
  511. // find the one with our destination address.
  512. //
  513. // We also keep a flag to see if we found it. If we
  514. // didn't, then we consider this to be an error.
  515. int found_pktinfo = 0;
  516. cmsg = CMSG_FIRSTHDR(&m);
  517. while (cmsg != NULL) {
  518. if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
  519. (cmsg->cmsg_type == IPV6_PKTINFO)) {
  520. pktinfo = (struct in6_pktinfo*)CMSG_DATA(cmsg);
  521. to_addr = pktinfo->ipi6_addr;
  522. pkt->ifindex_ = pktinfo->ipi6_ifindex;
  523. found_pktinfo = 1;
  524. }
  525. cmsg = CMSG_NXTHDR(&m, cmsg);
  526. }
  527. if (!found_pktinfo) {
  528. cout << "Unable to find pktinfo" << endl;
  529. return (boost::shared_ptr<Pkt6>()); // NULL
  530. }
  531. } else {
  532. cout << "Failed to receive data." << endl;
  533. return (boost::shared_ptr<Pkt6>()); // NULL
  534. }
  535. // That's ugly.
  536. // TODO add IOAddress constructor that will take struct in6_addr*
  537. // TODO: there's from_bytes() method added in IOAddress. Use it!
  538. inet_ntop(AF_INET6, &to_addr, addr_str,INET6_ADDRSTRLEN);
  539. pkt->local_addr_ = IOAddress(string(addr_str));
  540. // TODO: there's from_bytes() method added in IOAddress. Use it!
  541. inet_ntop(AF_INET6, &from.sin6_addr, addr_str, INET6_ADDRSTRLEN);
  542. pkt->remote_addr_ = IOAddress(string(addr_str));
  543. pkt->remote_port_ = ntohs(from.sin6_port);
  544. Iface* received = getIface(pkt->ifindex_);
  545. if (received) {
  546. pkt->iface_ = received->getName();
  547. } else {
  548. cout << "Received packet over unknown interface (ifindex="
  549. << pkt->ifindex_ << ")." << endl;
  550. return (boost::shared_ptr<Pkt6>()); // NULL
  551. }
  552. pkt->data_len_ = result;
  553. // TODO Move this to LOG_DEBUG
  554. cout << "Received " << pkt->data_len_ << " bytes over "
  555. << pkt->iface_ << "/" << pkt->ifindex_ << " interface: "
  556. << " src=" << pkt->remote_addr_.toText()
  557. << ", dst=" << pkt->local_addr_.toText()
  558. << endl;
  559. return (pkt);
  560. }
  561. uint16_t
  562. IfaceMgr::getSocket(isc::dhcp::Pkt6 const& pkt) {
  563. Iface* iface = getIface(pkt.iface_);
  564. if (!iface) {
  565. isc_throw(BadValue, "Tried to find socket for non-existent interface "
  566. << pkt.iface_);
  567. }
  568. SocketCollection::const_iterator s;
  569. for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
  570. if (s->family_ != AF_INET6) {
  571. // don't use IPv4 sockets
  572. continue;
  573. }
  574. if (s->addr_.getAddress().to_v6().is_multicast()) {
  575. // don't use IPv6 sockets bound to multicast address
  576. continue;
  577. }
  578. /// TODO: Add more checks here later. If remote address is
  579. /// not link-local, we can't use link local bound socket
  580. /// to send data.
  581. return (s->sockfd_);
  582. }
  583. isc_throw(Unexpected, "Interface " << iface->getFullName()
  584. << " does not have any suitable IPv6 sockets open.");
  585. }
  586. uint16_t
  587. IfaceMgr::getSocket(isc::dhcp::Pkt4 const& pkt) {
  588. Iface* iface = getIface(pkt.getIface());
  589. if (!iface) {
  590. isc_throw(BadValue, "Tried to find socket for non-existent interface "
  591. << pkt.getIface());
  592. }
  593. SocketCollection::const_iterator s;
  594. for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
  595. if (s->family_ != AF_INET) {
  596. // don't use IPv4 sockets
  597. continue;
  598. }
  599. /// TODO: Add more checks here later. If remote address is
  600. /// not link-local, we can't use link local bound socket
  601. /// to send data.
  602. return (s->sockfd_);
  603. }
  604. isc_throw(Unexpected, "Interface " << iface->getFullName()
  605. << " does not have any suitable IPv4 sockets open.");
  606. }
  607. }