Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 2 | /* |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "multicast-udp-transport.hpp" |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 27 | #include "socket-utils.hpp" |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 28 | #include "udp-protocol.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 29 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 30 | #include <boost/functional/hash.hpp> |
| 31 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 32 | #ifdef __linux__ |
| 33 | #include <cerrno> // for errno |
| 34 | #include <cstring> // for std::strerror() |
| 35 | #include <sys/socket.h> // for setsockopt() |
| 36 | #endif // __linux__ |
| 37 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 38 | namespace nfd { |
| 39 | namespace face { |
| 40 | |
| 41 | NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(DatagramTransport, MulticastUdpTransport::protocol, |
| 42 | Multicast, "MulticastUdpTransport"); |
| 43 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 44 | MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& multicastGroup, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 45 | protocol::socket&& recvSocket, |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 46 | protocol::socket&& sendSocket, |
| 47 | ndn::nfd::LinkType linkType) |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 48 | : DatagramTransport(std::move(recvSocket)) |
| 49 | , m_multicastGroup(multicastGroup) |
| 50 | , m_sendSocket(std::move(sendSocket)) |
| 51 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 52 | this->setLocalUri(FaceUri(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 53 | this->setRemoteUri(FaceUri(multicastGroup)); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 54 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 55 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 56 | this->setLinkType(linkType); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 57 | this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 58 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 59 | protocol::socket::send_buffer_size sendBufferSizeOption; |
| 60 | boost::system::error_code error; |
| 61 | m_sendSocket.get_option(sendBufferSizeOption); |
| 62 | if (error) { |
| 63 | NFD_LOG_FACE_WARN("Failed to obtain send queue capacity from socket: " << error.message()); |
| 64 | this->setSendQueueCapacity(QUEUE_ERROR); |
| 65 | } |
| 66 | else { |
| 67 | this->setSendQueueCapacity(sendBufferSizeOption.value()); |
| 68 | } |
| 69 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 70 | NFD_LOG_FACE_INFO("Creating transport"); |
| 71 | } |
| 72 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 73 | ssize_t |
| 74 | MulticastUdpTransport::getSendQueueLength() |
| 75 | { |
| 76 | ssize_t queueLength = getTxQueueLength(m_sendSocket.native_handle()); |
| 77 | if (queueLength == QUEUE_ERROR) { |
| 78 | NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno)); |
| 79 | } |
| 80 | return queueLength; |
| 81 | } |
| 82 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 83 | void |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 84 | MulticastUdpTransport::doSend(Transport::Packet&& packet) |
| 85 | { |
| 86 | NFD_LOG_FACE_TRACE(__func__); |
| 87 | |
| 88 | m_sendSocket.async_send_to(boost::asio::buffer(packet.packet), m_multicastGroup, |
| 89 | bind(&MulticastUdpTransport::handleSend, this, |
| 90 | boost::asio::placeholders::error, |
| 91 | boost::asio::placeholders::bytes_transferred, |
| 92 | packet.packet)); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | MulticastUdpTransport::doClose() |
| 97 | { |
| 98 | if (m_sendSocket.is_open()) { |
| 99 | NFD_LOG_FACE_TRACE("Closing sending socket"); |
| 100 | |
| 101 | // Cancel all outstanding operations and close the socket. |
| 102 | // Use the non-throwing variants and ignore errors, if any. |
| 103 | boost::system::error_code error; |
| 104 | m_sendSocket.cancel(error); |
| 105 | m_sendSocket.close(error); |
| 106 | } |
| 107 | |
| 108 | DatagramTransport::doClose(); |
| 109 | } |
| 110 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 111 | void |
| 112 | MulticastUdpTransport::openRxSocket(protocol::socket& sock, |
| 113 | const protocol::endpoint& multicastGroup, |
| 114 | const boost::asio::ip::address& localAddress, |
| 115 | const shared_ptr<const ndn::net::NetworkInterface>& netif) |
| 116 | { |
| 117 | BOOST_ASSERT(!sock.is_open()); |
| 118 | |
| 119 | sock.open(multicastGroup.protocol()); |
| 120 | sock.set_option(protocol::socket::reuse_address(true)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 121 | |
| 122 | if (multicastGroup.address().is_v4()) { |
| 123 | BOOST_ASSERT(localAddress.is_v4()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 124 | sock.bind(multicastGroup); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 125 | sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(), |
| 126 | localAddress.to_v4())); |
| 127 | } |
| 128 | else { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 129 | BOOST_ASSERT(localAddress.is_v6()); |
| 130 | sock.set_option(boost::asio::ip::v6_only(true)); |
| 131 | #ifdef WITH_TESTS |
| 132 | // To simplify unit tests, we bind to the "any" IPv6 address if the supplied multicast |
| 133 | // address lacks a scope id. Calling bind() without a scope id would otherwise fail. |
| 134 | if (multicastGroup.address().to_v6().scope_id() == 0) |
| 135 | sock.bind(protocol::endpoint(boost::asio::ip::address_v6::any(), multicastGroup.port())); |
| 136 | else |
| 137 | #endif |
| 138 | sock.bind(multicastGroup); |
| 139 | sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v6())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #ifdef __linux__ |
| 143 | if (netif) { |
| 144 | // On Linux, if there is more than one MulticastUdpTransport for the same multicast |
| 145 | // group but they are on different network interfaces, each socket needs to be bound |
| 146 | // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will |
| 147 | // receive all packets sent to the other interfaces as well. |
| 148 | // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group |
| 149 | // option is sufficient to obtain the desired behavior. |
| 150 | if (::setsockopt(sock.native_handle(), SOL_SOCKET, SO_BINDTODEVICE, |
| 151 | netif->getName().data(), netif->getName().size() + 1) < 0) { |
| 152 | BOOST_THROW_EXCEPTION(Error("Cannot bind multicast rx socket to " + netif->getName() + |
| 153 | ": " + std::strerror(errno))); |
| 154 | } |
| 155 | } |
| 156 | #endif // __linux__ |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | MulticastUdpTransport::openTxSocket(protocol::socket& sock, |
| 161 | const protocol::endpoint& localEndpoint, |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 162 | const shared_ptr<const ndn::net::NetworkInterface>& netif, |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 163 | bool enableLoopback) |
| 164 | { |
| 165 | BOOST_ASSERT(!sock.is_open()); |
| 166 | |
| 167 | sock.open(localEndpoint.protocol()); |
| 168 | sock.set_option(protocol::socket::reuse_address(true)); |
| 169 | sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 170 | |
| 171 | if (localEndpoint.address().is_v4()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 172 | sock.bind(localEndpoint); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 173 | if (!localEndpoint.address().is_unspecified()) |
| 174 | sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
| 175 | } |
| 176 | else { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 177 | sock.set_option(boost::asio::ip::v6_only(true)); |
| 178 | sock.bind(localEndpoint); |
| 179 | if (netif) |
| 180 | sock.set_option(boost::asio::ip::multicast::outbound_interface(netif->getIndex())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Davide Pesavento | eee7b97 | 2015-11-18 21:34:46 +0100 | [diff] [blame] | 184 | template<> |
| 185 | Transport::EndpointId |
| 186 | DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep) |
| 187 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 188 | if (ep.address().is_v4()) { |
| 189 | return (static_cast<uint64_t>(ep.port()) << 32) | |
| 190 | static_cast<uint64_t>(ep.address().to_v4().to_ulong()); |
| 191 | } |
| 192 | else { |
| 193 | size_t seed = 0; |
| 194 | const auto& addrBytes = ep.address().to_v6().to_bytes(); |
| 195 | boost::hash_range(seed, addrBytes.begin(), addrBytes.end()); |
| 196 | boost::hash_combine(seed, ep.port()); |
| 197 | return seed; |
| 198 | } |
Davide Pesavento | eee7b97 | 2015-11-18 21:34:46 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 201 | } // namespace face |
| 202 | } // namespace nfd |