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 | /* |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "common/privilege-helper.hpp" |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 31 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/asio/ip/multicast.hpp> |
| 33 | #include <boost/asio/ip/v6_only.hpp> |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 34 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 35 | #ifdef __linux__ |
| 36 | #include <cerrno> // for errno |
| 37 | #include <cstring> // for std::strerror() |
| 38 | #include <sys/socket.h> // for setsockopt() |
| 39 | #endif // __linux__ |
| 40 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 41 | namespace nfd::face { |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 42 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 43 | namespace ip = boost::asio::ip; |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 44 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 45 | NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<ip::udp, Multicast>), MulticastUdpTransport); |
| 46 | |
| 47 | MulticastUdpTransport::MulticastUdpTransport(const ip::udp::endpoint& multicastGroup, |
| 48 | ip::udp::socket&& recvSocket, |
| 49 | ip::udp::socket&& sendSocket, |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 50 | ndn::nfd::LinkType linkType) |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 51 | : DatagramTransport(std::move(recvSocket)) |
| 52 | , m_multicastGroup(multicastGroup) |
| 53 | , m_sendSocket(std::move(sendSocket)) |
| 54 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 55 | this->setLocalUri(FaceUri(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 56 | this->setRemoteUri(FaceUri(multicastGroup)); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 57 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 58 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 59 | this->setLinkType(linkType); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 60 | this->setMtu(udp::computeMtu(m_sendSocket.local_endpoint())); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 61 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 62 | boost::asio::socket_base::send_buffer_size sendBufferSizeOption; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 63 | boost::system::error_code error; |
| 64 | m_sendSocket.get_option(sendBufferSizeOption); |
| 65 | if (error) { |
| 66 | NFD_LOG_FACE_WARN("Failed to obtain send queue capacity from socket: " << error.message()); |
| 67 | this->setSendQueueCapacity(QUEUE_ERROR); |
| 68 | } |
| 69 | else { |
| 70 | this->setSendQueueCapacity(sendBufferSizeOption.value()); |
| 71 | } |
| 72 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 73 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 76 | ssize_t |
| 77 | MulticastUdpTransport::getSendQueueLength() |
| 78 | { |
| 79 | ssize_t queueLength = getTxQueueLength(m_sendSocket.native_handle()); |
| 80 | if (queueLength == QUEUE_ERROR) { |
| 81 | NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno)); |
| 82 | } |
| 83 | return queueLength; |
| 84 | } |
| 85 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 86 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 87 | MulticastUdpTransport::doSend(const Block& packet) |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 88 | { |
| 89 | NFD_LOG_FACE_TRACE(__func__); |
| 90 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 91 | m_sendSocket.async_send_to(boost::asio::buffer(packet), m_multicastGroup, |
| 92 | // 'packet' is copied into the lambda to retain the underlying Buffer |
| 93 | [this, packet] (auto&&... args) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 94 | this->handleSend(std::forward<decltype(args)>(args)...); |
| 95 | }); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | MulticastUdpTransport::doClose() |
| 100 | { |
| 101 | if (m_sendSocket.is_open()) { |
| 102 | NFD_LOG_FACE_TRACE("Closing sending socket"); |
| 103 | |
| 104 | // Cancel all outstanding operations and close the socket. |
| 105 | // Use the non-throwing variants and ignore errors, if any. |
| 106 | boost::system::error_code error; |
| 107 | m_sendSocket.cancel(error); |
| 108 | m_sendSocket.close(error); |
| 109 | } |
| 110 | |
| 111 | DatagramTransport::doClose(); |
| 112 | } |
| 113 | |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 114 | static void |
| 115 | bindToDevice(int fd, const std::string& ifname) |
| 116 | { |
| 117 | // On Linux, if there is more than one MulticastUdpTransport for the same multicast |
| 118 | // group but they are on different network interfaces, each socket needs to be bound |
| 119 | // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will |
| 120 | // receive all packets sent to the other interfaces as well. |
| 121 | // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group |
| 122 | // option is sufficient to obtain the desired behavior. |
Ju Pan | bc59dd0 | 2018-11-18 14:03:47 -0700 | [diff] [blame] | 123 | // We dont't set SO_BINDTODEVICE on Android because this operation requires root privilege |
| 124 | // which is not allowed on Android, it will cause "Operation not permitted" error. |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 125 | |
Ju Pan | bc59dd0 | 2018-11-18 14:03:47 -0700 | [diff] [blame] | 126 | #if defined(__linux__) && !defined(__ANDROID__) |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 127 | PrivilegeHelper::runElevated([=] { |
| 128 | if (::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname.data(), ifname.size() + 1) < 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 129 | NDN_THROW_ERRNO(MulticastUdpTransport::Error("Cannot bind multicast rx socket to " + ifname)); |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 130 | } |
| 131 | }); |
Ju Pan | bc59dd0 | 2018-11-18 14:03:47 -0700 | [diff] [blame] | 132 | #endif |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 135 | void |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 136 | MulticastUdpTransport::openRxSocket(ip::udp::socket& sock, |
| 137 | const ip::udp::endpoint& multicastGroup, |
| 138 | const ip::address& localAddress, |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 139 | const ndn::net::NetworkInterface* netif) |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 140 | { |
| 141 | BOOST_ASSERT(!sock.is_open()); |
| 142 | |
| 143 | sock.open(multicastGroup.protocol()); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 144 | sock.set_option(boost::asio::socket_base::reuse_address(true)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 145 | |
| 146 | if (multicastGroup.address().is_v4()) { |
| 147 | BOOST_ASSERT(localAddress.is_v4()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 148 | sock.bind(multicastGroup); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 149 | sock.set_option(ip::multicast::join_group(multicastGroup.address().to_v4(), localAddress.to_v4())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 150 | } |
| 151 | else { |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 152 | BOOST_ASSERT(multicastGroup.address().to_v6().scope_id() != 0); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 153 | sock.set_option(ip::v6_only(true)); |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 154 | sock.bind(multicastGroup); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 155 | sock.set_option(ip::multicast::join_group(multicastGroup.address().to_v6())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Davide Pesavento | d7b44c8 | 2018-02-15 17:39:31 -0500 | [diff] [blame] | 158 | if (netif) |
| 159 | bindToDevice(sock.native_handle(), netif->getName()); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 163 | MulticastUdpTransport::openTxSocket(ip::udp::socket& sock, |
| 164 | const ip::udp::endpoint& localEndpoint, |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 165 | const ndn::net::NetworkInterface* netif, |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 166 | bool enableLoopback) |
| 167 | { |
| 168 | BOOST_ASSERT(!sock.is_open()); |
| 169 | |
| 170 | sock.open(localEndpoint.protocol()); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 171 | sock.set_option(boost::asio::socket_base::reuse_address(true)); |
| 172 | sock.set_option(ip::multicast::enable_loopback(enableLoopback)); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 173 | |
| 174 | if (localEndpoint.address().is_v4()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 175 | sock.bind(localEndpoint); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 176 | if (!localEndpoint.address().is_unspecified()) |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 177 | sock.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 178 | } |
| 179 | else { |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 180 | sock.set_option(ip::v6_only(true)); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 181 | sock.bind(localEndpoint); |
| 182 | if (netif) |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 183 | sock.set_option(ip::multicast::outbound_interface(netif->getIndex())); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 187 | } // namespace nfd::face |