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