void IPNDAgent::send(const DiscoveryAnnouncement &a, const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr) { // serialize announcement stringstream ss; ss << a; const std::string data = ss.str(); ibrcommon::socketset fds = _send_socket.get(iface); // send out discovery announcements on all bound sockets // (hopefully only one per interface) for (ibrcommon::socketset::const_iterator iter = fds.begin(); iter != fds.end(); ++iter) { ibrcommon::udpsocket &sock = dynamic_cast<ibrcommon::udpsocket&>(**iter); try { // prevent broadcasting in the wrong address family if (addr.family() != sock.get_family()) continue; sock.sendto(data.c_str(), data.length(), 0, addr); } catch (const ibrcommon::socket_exception &e) { IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 5) << "can not send message to " << addr.toString() << " via " << sock.get_address().toString() << "/" << iface.toString() << "; socket exception: " << e.what() << IBRCOMMON_LOGGER_ENDL; } catch (const ibrcommon::vaddress::address_exception &ex) { IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL; } } }
void IPNDAgent::join(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr) throw () { IBRCOMMON_LOGGER_DEBUG_TAG(TAG, 10) << "Join on " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << IBRCOMMON_LOGGER_ENDL; // only join IPv6 and IPv4 addresses if ((addr.family() != AF_INET) && (addr.family() != AF_INET6)) return; // do not join on loopback interfaces if (addr.isLocal()) return; // create a multicast socket and bind to given addr ibrcommon::multicastsocket *msock = new ibrcommon::multicastsocket(addr); // if we are in UP state if (_state) { try { // bring up msock->up(); // listen to multicast addresses for (std::set<ibrcommon::vaddress>::const_iterator it_addr = _destinations.begin(); it_addr != _destinations.end(); ++it_addr) { if (msock->get_family() != it_addr->family()) continue; try { msock->join(*it_addr, iface); } catch (const ibrcommon::socket_raw_error &e) { if (e.error() == EADDRINUSE) { // silent error } else if (e.error() == 92) { // silent error - protocol not available } else { IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << "Join to " << (*it_addr).toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL; } } catch (const ibrcommon::socket_exception &e) { IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "Join to " << (*it_addr).toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL; } } } catch (const ibrcommon::socket_exception &ex) { IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "Join failed on " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << "; " << ex.what() << IBRCOMMON_LOGGER_ENDL; delete msock; return; } } // add multicast socket to _socket _socket.add(msock, iface); }
void IPNDAgent::leave(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr) throw () { IBRCOMMON_LOGGER_DEBUG_TAG(TAG, 10) << "Leave " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << IBRCOMMON_LOGGER_ENDL; // get all sockets bound to the given interface ibrcommon::socketset ifsocks = _socket.get(iface); for (ibrcommon::socketset::iterator it = ifsocks.begin(); it != ifsocks.end(); ++it) { ibrcommon::multicastsocket *msock = dynamic_cast<ibrcommon::multicastsocket*>(*it); if (msock == NULL) continue; if (msock->get_address() == addr) { // remove the socket _socket.remove(msock); // shutdown the socket try { msock->down(); } catch (const ibrcommon::socket_exception &ex) { IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "leave failed: " << ex.what() << IBRCOMMON_LOGGER_ENDL; } // delete the socket delete msock; return; } } }
void IPNDAgent::join(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr) throw () { // only join IPv6 and IPv4 addresses if ((addr.family() != AF_INET) && (addr.family() != AF_INET6)) return; // do not join on loopback interfaces if (addr.isLocal()) return; // create a multicast socket and bind to given addr ibrcommon::multicastsocket *msock = new ibrcommon::multicastsocket(addr); try { // bring up msock->up(); // add multicast socket to _socket _socket.add(msock, iface); } catch (const ibrcommon::socket_exception &ex) { IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "Can not send on " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << "; " << ex.what() << IBRCOMMON_LOGGER_ENDL; delete msock; } }