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 | |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 30 | #include "core/privilege-helper.hpp" |
| 31 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 32 | #include <boost/functional/hash.hpp> |
| 33 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 34 | #ifdef __linux__ |
| 35 | #include <cerrno> // for errno |
| 36 | #include <cstring> // for std::strerror() |
| 37 | #include <sys/socket.h> // for setsockopt() |
| 38 | #endif // __linux__ |
| 39 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 40 | namespace nfd { |
| 41 | namespace face { |
| 42 | |
| 43 | NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(DatagramTransport, MulticastUdpTransport::protocol, |
| 44 | Multicast, "MulticastUdpTransport"); |
| 45 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 46 | MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& multicastGroup, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 47 | protocol::socket&& recvSocket, |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 48 | protocol::socket&& sendSocket, |
| 49 | ndn::nfd::LinkType linkType) |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 50 | : DatagramTransport(std::move(recvSocket)) |
| 51 | , m_multicastGroup(multicastGroup) |
| 52 | , m_sendSocket(std::move(sendSocket)) |
| 53 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 54 | this->setLocalUri(FaceUri(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 55 | this->setRemoteUri(FaceUri(multicastGroup)); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 56 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 57 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 58 | this->setLinkType(linkType); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 59 | this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 60 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 61 | protocol::socket::send_buffer_size sendBufferSizeOption; |
| 62 | boost::system::error_code error; |
| 63 | m_sendSocket.get_option(sendBufferSizeOption); |
| 64 | if (error) { |
| 65 | NFD_LOG_FACE_WARN("Failed to obtain send queue capacity from socket: " << error.message()); |
| 66 | this->setSendQueueCapacity(QUEUE_ERROR); |
| 67 | } |
| 68 | else { |
| 69 | this->setSendQueueCapacity(sendBufferSizeOption.value()); |
| 70 | } |
| 71 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 72 | NFD_LOG_FACE_INFO("Creating transport"); |
| 73 | } |
| 74 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 75 | ssize_t |
| 76 | MulticastUdpTransport::getSendQueueLength() |
| 77 | { |
| 78 | ssize_t queueLength = getTxQueueLength(m_sendSocket.native_handle()); |
| 79 | if (queueLength == QUEUE_ERROR) { |
| 80 | NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno)); |
| 81 | } |
| 82 | return queueLength; |
| 83 | } |
| 84 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 85 | void |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 86 | MulticastUdpTransport::doSend(Transport::Packet&& packet) |
| 87 | { |
| 88 | NFD_LOG_FACE_TRACE(__func__); |
| 89 | |
| 90 | m_sendSocket.async_send_to(boost::asio::buffer(packet.packet), m_multicastGroup, |
| 91 | bind(&MulticastUdpTransport::handleSend, this, |
| 92 | boost::asio::placeholders::error, |
| 93 | boost::asio::placeholders::bytes_transferred, |
| 94 | packet.packet)); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | MulticastUdpTransport::doClose() |
| 99 | { |
| 100 | if (m_sendSocket.is_open()) { |
| 101 | NFD_LOG_FACE_TRACE("Closing sending socket"); |
| 102 | |
| 103 | // Cancel all outstanding operations and close the socket. |
| 104 | // Use the non-throwing variants and ignore errors, if any. |
| 105 | boost::system::error_code error; |
| 106 | m_sendSocket.cancel(error); |
| 107 | m_sendSocket.close(error); |
| 108 | } |
| 109 | |
| 110 | DatagramTransport::doClose(); |
| 111 | } |
| 112 | |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 113 | static void |
| 114 | bindToDevice(int fd, const std::string& ifname) |
| 115 | { |
| 116 | // On Linux, if there is more than one MulticastUdpTransport for the same multicast |
| 117 | // group but they are on different network interfaces, each socket needs to be bound |
| 118 | // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will |
| 119 | // receive all packets sent to the other interfaces as well. |
| 120 | // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group |
| 121 | // option is sufficient to obtain the desired behavior. |
| 122 | |
| 123 | #ifdef __linux__ |
| 124 | PrivilegeHelper::runElevated([=] { |
| 125 | if (::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname.data(), ifname.size() + 1) < 0) { |
| 126 | BOOST_THROW_EXCEPTION(MulticastUdpTransport::Error("Cannot bind multicast rx socket to " + |
| 127 | ifname + ": " + std::strerror(errno))); |
| 128 | } |
| 129 | }); |
| 130 | #endif // __linux__ |
| 131 | } |
| 132 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 133 | void |
| 134 | MulticastUdpTransport::openRxSocket(protocol::socket& sock, |
| 135 | const protocol::endpoint& multicastGroup, |
| 136 | const boost::asio::ip::address& localAddress, |
| 137 | const shared_ptr<const ndn::net::NetworkInterface>& netif) |
| 138 | { |
| 139 | BOOST_ASSERT(!sock.is_open()); |
| 140 | |
| 141 | sock.open(multicastGroup.protocol()); |
| 142 | sock.set_option(protocol::socket::reuse_address(true)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 143 | |
| 144 | if (multicastGroup.address().is_v4()) { |
| 145 | BOOST_ASSERT(localAddress.is_v4()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 146 | sock.bind(multicastGroup); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 147 | sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(), |
| 148 | localAddress.to_v4())); |
| 149 | } |
| 150 | else { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 151 | BOOST_ASSERT(localAddress.is_v6()); |
| 152 | sock.set_option(boost::asio::ip::v6_only(true)); |
| 153 | #ifdef WITH_TESTS |
| 154 | // To simplify unit tests, we bind to the "any" IPv6 address if the supplied multicast |
| 155 | // address lacks a scope id. Calling bind() without a scope id would otherwise fail. |
| 156 | if (multicastGroup.address().to_v6().scope_id() == 0) |
| 157 | sock.bind(protocol::endpoint(boost::asio::ip::address_v6::any(), multicastGroup.port())); |
| 158 | else |
| 159 | #endif |
| 160 | sock.bind(multicastGroup); |
| 161 | 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] | 162 | } |
| 163 | |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 164 | if (netif) |
| 165 | bindToDevice(sock.native_handle(), netif->getName()); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
| 169 | MulticastUdpTransport::openTxSocket(protocol::socket& sock, |
| 170 | const protocol::endpoint& localEndpoint, |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 171 | const shared_ptr<const ndn::net::NetworkInterface>& netif, |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 172 | bool enableLoopback) |
| 173 | { |
| 174 | BOOST_ASSERT(!sock.is_open()); |
| 175 | |
| 176 | sock.open(localEndpoint.protocol()); |
| 177 | sock.set_option(protocol::socket::reuse_address(true)); |
| 178 | sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 179 | |
| 180 | if (localEndpoint.address().is_v4()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 181 | sock.bind(localEndpoint); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 182 | if (!localEndpoint.address().is_unspecified()) |
| 183 | sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
| 184 | } |
| 185 | else { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 186 | sock.set_option(boost::asio::ip::v6_only(true)); |
| 187 | sock.bind(localEndpoint); |
| 188 | if (netif) |
| 189 | sock.set_option(boost::asio::ip::multicast::outbound_interface(netif->getIndex())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Davide Pesavento | eee7b97 | 2015-11-18 21:34:46 +0100 | [diff] [blame] | 193 | template<> |
| 194 | Transport::EndpointId |
| 195 | DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep) |
| 196 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 197 | if (ep.address().is_v4()) { |
| 198 | return (static_cast<uint64_t>(ep.port()) << 32) | |
| 199 | static_cast<uint64_t>(ep.address().to_v4().to_ulong()); |
| 200 | } |
| 201 | else { |
| 202 | size_t seed = 0; |
| 203 | const auto& addrBytes = ep.address().to_v6().to_bytes(); |
| 204 | boost::hash_range(seed, addrBytes.begin(), addrBytes.end()); |
| 205 | boost::hash_combine(seed, ep.port()); |
| 206 | return seed; |
| 207 | } |
Davide Pesavento | eee7b97 | 2015-11-18 21:34:46 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 210 | } // namespace face |
| 211 | } // namespace nfd |