35 #include <sys/socket.h> 47 if (endpoint.address().is_v4() &&
48 endpoint.address() == ip::address_v4::any()) {
49 prohibitAllIpv4Endpoints(endpoint.port());
51 else if (endpoint.address().is_v6() &&
52 endpoint.address() == ip::address_v6::any()) {
53 prohibitAllIpv6Endpoints(endpoint.port());
57 m_prohibitedEndpoints.insert(endpoint);
61 UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
64 for (
const auto& addr : nic.ipv4Addresses) {
65 if (addr != ip::address_v4::any()) {
70 if (nic.isBroadcastCapable() &&
71 nic.broadcastAddress != ip::address_v4::any()) {
76 prohibitEndpoint(
udp::Endpoint(ip::address_v4::broadcast(), port));
80 UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
83 for (
const auto& addr : nic.ipv6Addresses) {
84 if (addr != ip::address_v6::any()) {
91 shared_ptr<UdpChannel>
93 const time::seconds& timeout)
97 auto channel = findChannel(endpoint);
101 if (endpoint.address().is_multicast()) {
102 BOOST_THROW_EXCEPTION(
Error(
"createChannel is only for unicast channels. The provided endpoint " 103 "is multicast. Use createMulticastFace to create a multicast face"));
107 auto face = findMulticastFace(endpoint);
109 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP unicast channel, local " 110 "endpoint is already allocated for a UDP multicast face"));
113 channel = std::make_shared<UdpChannel>(endpoint, timeout);
114 m_channels[endpoint] = channel;
115 prohibitEndpoint(endpoint);
120 shared_ptr<UdpChannel>
122 const time::seconds& timeout)
125 boost::lexical_cast<uint16_t>(localPort));
132 const std::string& networkInterfaceName)
135 auto face = findMulticastFace(localEndpoint);
137 if (
face->getRemoteUri() ==
FaceUri(multicastEndpoint))
140 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, local " 141 "endpoint is already allocated for a UDP multicast face " 142 "on a different multicast group"));
146 auto unicastCh = findChannel(localEndpoint);
148 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, local " 149 "endpoint is already allocated for a UDP unicast channel"));
152 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
153 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 154 "remote endpoint is owned by this NFD instance"));
157 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
158 BOOST_THROW_EXCEPTION(
Error(
"IPv6 multicast is not supported yet. Please provide an IPv4 " 162 if (localEndpoint.port() != multicastEndpoint.port()) {
163 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 164 "both endpoints should have the same port number. "));
167 if (!multicastEndpoint.address().is_multicast()) {
168 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 169 "the multicast group given as input is not a multicast address"));
173 receiveSocket.open(multicastEndpoint.protocol());
174 receiveSocket.set_option(ip::udp::socket::reuse_address(
true));
175 receiveSocket.bind(multicastEndpoint);
178 sendSocket.open(multicastEndpoint.protocol());
179 sendSocket.set_option(ip::udp::socket::reuse_address(
true));
180 sendSocket.set_option(ip::multicast::enable_loopback(
false));
181 sendSocket.bind(
udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
182 if (localEndpoint.address() != ip::address_v4::any())
183 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
185 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
186 localEndpoint.address().to_v4()));
187 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
188 localEndpoint.address().to_v4()));
199 if (!networkInterfaceName.empty()) {
200 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
201 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
202 BOOST_THROW_EXCEPTION(
Error(
"Cannot bind multicast face to " + networkInterfaceName +
203 ": " + std::strerror(errno)));
208 auto linkService = make_unique<face::GenericLinkService>();
209 auto transport = make_unique<face::MulticastUdpTransport>(localEndpoint, multicastEndpoint,
210 std::move(receiveSocket),
211 std::move(sendSocket));
212 face = make_shared<Face>(std::move(linkService), std::move(transport));
214 m_multicastFaces[localEndpoint] = face;
222 const std::string& multicastIp,
223 const std::string& multicastPort,
224 const std::string& networkInterfaceName)
226 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
227 boost::lexical_cast<uint16_t>(multicastPort));
228 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
229 boost::lexical_cast<uint16_t>(multicastPort));
236 bool wantLocalFieldsEnabled,
243 NFD_LOG_TRACE(
"createFace does not support FACE_PERSISTENCY_ON_DEMAND");
244 onFailure(406,
"Outgoing unicast UDP faces do not support on-demand persistency");
249 boost::lexical_cast<uint16_t>(uri.
getPort()));
251 if (endpoint.address().is_multicast()) {
252 NFD_LOG_TRACE(
"createFace does not support multicast faces");
253 onFailure(406,
"Cannot create multicast UDP faces");
257 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
259 "(reserved by this NFD or disallowed by face management protocol)");
260 onFailure(406,
"Requested endpoint is prohibited");
264 if (wantLocalFieldsEnabled) {
266 NFD_LOG_TRACE(
"createFace cannot create non-local face with local fields enabled");
267 onFailure(406,
"Local fields can only be enabled on faces with local scope");
272 for (
const auto& i : m_channels) {
273 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
274 (i.first.address().is_v6() && endpoint.address().is_v6())) {
275 i.second->connect(endpoint, persistency, onCreated, onFailure);
280 NFD_LOG_TRACE(
"No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
281 onFailure(504,
"No channels available to connect");
284 std::vector<shared_ptr<const Channel>>
287 std::vector<shared_ptr<const Channel>> channels;
288 channels.reserve(m_channels.size());
290 for (
const auto& i : m_channels)
291 channels.push_back(i.second);
296 shared_ptr<UdpChannel>
297 UdpFactory::findChannel(
const udp::Endpoint& localEndpoint)
const 299 auto i = m_channels.find(localEndpoint);
300 if (i != m_channels.end())
307 UdpFactory::findMulticastFace(
const udp::Endpoint& localEndpoint)
const 309 auto i = m_multicastFaces.find(localEndpoint);
310 if (i != m_multicastFaces.end())
virtual std::vector< shared_ptr< const Channel > > getChannels() const override
represents the underlying protocol and address used by a Face
shared_ptr< Face > createMulticastFace(const udp::Endpoint &localEndpoint, const udp::Endpoint &multicastEndpoint, const std::string &networkInterfaceName="")
Create MulticastUdpFace using udp::Endpoint.
bool isCanonical() const
determine whether this FaceUri is in canonical form
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
contains information about a network interface
detail::SimulatorIo & getGlobalIoService()
#define NFD_LOG_DEBUG(expression)
shared_ptr< UdpChannel > createChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout=time::seconds(600))
Create UDP-based channel using udp::Endpoint.
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
#define NFD_LOG_TRACE(expression)
Copyright (c) 2011-2015 Regents of the University of California.
const std::string & getPort() const
get port
boost::asio::ip::udp::endpoint Endpoint
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, bool wantLocalFieldsEnabled, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create Face using the supplied FaceUri.
const std::string & getHost() const
get host (domain)
#define NFD_LOG_INIT(name)