blob: f8ee1a9a2e99322e5e572097bc9a914d1a7826cb [file] [log] [blame]
Yukai Tu0a49d342015-09-13 12:54:22 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8215a3a2017-12-25 19:14:33 -05002/*
Yanbiao Li32dab972016-11-27 12:26:09 +08003 * Copyright (c) 2014-2017, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * 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"
Junxiao Shi13546112015-10-14 19:33:07 -070027#include "udp-protocol.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080028
Davide Pesavento8215a3a2017-12-25 19:14:33 -050029#ifdef __linux__
30#include <cerrno> // for errno
31#include <cstring> // for std::strerror()
32#include <sys/socket.h> // for setsockopt()
33#endif // __linux__
34
Yukai Tu0a49d342015-09-13 12:54:22 +080035namespace nfd {
36namespace face {
37
38NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(DatagramTransport, MulticastUdpTransport::protocol,
39 Multicast, "MulticastUdpTransport");
40
41MulticastUdpTransport::MulticastUdpTransport(const protocol::endpoint& localEndpoint,
42 const protocol::endpoint& multicastGroup,
43 protocol::socket&& recvSocket,
Teng Liangfe4fce32017-03-29 04:49:38 +000044 protocol::socket&& sendSocket,
45 ndn::nfd::LinkType linkType)
Yukai Tu0a49d342015-09-13 12:54:22 +080046 : DatagramTransport(std::move(recvSocket))
47 , m_multicastGroup(multicastGroup)
48 , m_sendSocket(std::move(sendSocket))
49{
50 this->setLocalUri(FaceUri(localEndpoint));
51 this->setRemoteUri(FaceUri(multicastGroup));
Junxiao Shi13546112015-10-14 19:33:07 -070052 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu0a49d342015-09-13 12:54:22 +080053 this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Teng Liangfe4fce32017-03-29 04:49:38 +000054 this->setLinkType(linkType);
Junxiao Shi13546112015-10-14 19:33:07 -070055 this->setMtu(udp::computeMtu(localEndpoint));
Yukai Tu0a49d342015-09-13 12:54:22 +080056
57 NFD_LOG_FACE_INFO("Creating transport");
58}
59
60void
Yukai Tu0a49d342015-09-13 12:54:22 +080061MulticastUdpTransport::doSend(Transport::Packet&& packet)
62{
63 NFD_LOG_FACE_TRACE(__func__);
64
65 m_sendSocket.async_send_to(boost::asio::buffer(packet.packet), m_multicastGroup,
66 bind(&MulticastUdpTransport::handleSend, this,
67 boost::asio::placeholders::error,
68 boost::asio::placeholders::bytes_transferred,
69 packet.packet));
70}
71
72void
73MulticastUdpTransport::doClose()
74{
75 if (m_sendSocket.is_open()) {
76 NFD_LOG_FACE_TRACE("Closing sending socket");
77
78 // Cancel all outstanding operations and close the socket.
79 // Use the non-throwing variants and ignore errors, if any.
80 boost::system::error_code error;
81 m_sendSocket.cancel(error);
82 m_sendSocket.close(error);
83 }
84
85 DatagramTransport::doClose();
86}
87
Davide Pesavento8215a3a2017-12-25 19:14:33 -050088void
89MulticastUdpTransport::openRxSocket(protocol::socket& sock,
90 const protocol::endpoint& multicastGroup,
91 const boost::asio::ip::address& localAddress,
92 const shared_ptr<const ndn::net::NetworkInterface>& netif)
93{
94 BOOST_ASSERT(!sock.is_open());
95
96 sock.open(multicastGroup.protocol());
97 sock.set_option(protocol::socket::reuse_address(true));
98 sock.bind(protocol::endpoint(multicastGroup.protocol(), multicastGroup.port()));
99
100 if (multicastGroup.address().is_v4()) {
101 BOOST_ASSERT(localAddress.is_v4());
102 sock.set_option(boost::asio::ip::multicast::join_group(multicastGroup.address().to_v4(),
103 localAddress.to_v4()));
104 }
105 else {
106 // IPv6 multicast is not supported
107 BOOST_ASSERT(false);
108 }
109
110#ifdef __linux__
111 if (netif) {
112 // On Linux, if there is more than one MulticastUdpTransport for the same multicast
113 // group but they are on different network interfaces, each socket needs to be bound
114 // to the corresponding interface using SO_BINDTODEVICE, otherwise the transport will
115 // receive all packets sent to the other interfaces as well.
116 // This is needed only on Linux. On macOS, the boost::asio::ip::multicast::join_group
117 // option is sufficient to obtain the desired behavior.
118 if (::setsockopt(sock.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
119 netif->getName().data(), netif->getName().size() + 1) < 0) {
120 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast rx socket to " + netif->getName() +
121 ": " + std::strerror(errno)));
122 }
123 }
124#endif // __linux__
125}
126
127void
128MulticastUdpTransport::openTxSocket(protocol::socket& sock,
129 const protocol::endpoint& localEndpoint,
130 bool enableLoopback)
131{
132 BOOST_ASSERT(!sock.is_open());
133
134 sock.open(localEndpoint.protocol());
135 sock.set_option(protocol::socket::reuse_address(true));
136 sock.set_option(boost::asio::ip::multicast::enable_loopback(enableLoopback));
137 sock.bind(localEndpoint);
138
139 if (localEndpoint.address().is_v4()) {
140 if (!localEndpoint.address().is_unspecified())
141 sock.set_option(boost::asio::ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
142 }
143 else {
144 // IPv6 multicast is not supported
145 BOOST_ASSERT(false);
146 }
147}
148
Davide Pesaventoeee7b972015-11-18 21:34:46 +0100149template<>
150Transport::EndpointId
151DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep)
152{
153 // IPv6 multicast is not supported
154 BOOST_ASSERT(ep.address().is_v4());
155
156 return (static_cast<uint64_t>(ep.port()) << 32) |
157 static_cast<uint64_t>(ep.address().to_v4().to_ulong());
158}
159
Yukai Tu0a49d342015-09-13 12:54:22 +0800160} // namespace face
161} // namespace nfd