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 = 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));
242 BOOST_THROW_EXCEPTION(
Error(
"UdpFactory::createFace does not support FACE_PERSISTENCY_ON_DEMAND"));
246 boost::lexical_cast<uint16_t>(uri.
getPort()));
248 if (endpoint.address().is_multicast()) {
249 onConnectFailed(
"The provided address is multicast. Please use createMulticastFace method");
253 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
254 onConnectFailed(
"Requested endpoint is prohibited " 255 "(reserved by this NFD or disallowed by face management protocol)");
260 for (
const auto& i : m_channels) {
261 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
262 (i.first.address().is_v6() && endpoint.address().is_v6())) {
263 i.second->connect(endpoint, persistency, onCreated, onConnectFailed);
268 onConnectFailed(
"No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
271 std::vector<shared_ptr<const Channel>>
274 std::vector<shared_ptr<const Channel>> channels;
275 channels.reserve(m_channels.size());
277 for (
const auto& i : m_channels)
278 channels.push_back(i.second);
283 shared_ptr<UdpChannel>
284 UdpFactory::findChannel(
const udp::Endpoint& localEndpoint)
const 286 auto i = m_channels.find(localEndpoint);
287 if (i != m_channels.end())
294 UdpFactory::findMulticastFace(
const udp::Endpoint& localEndpoint)
const 296 auto i = m_multicastFaces.find(localEndpoint);
297 if (i != m_multicastFaces.end())
#define NFD_LOG_DEBUG(expression)
function< void(const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
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()
shared_ptr< UdpChannel > createChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout=time::seconds(600))
Create UDP-based channel using udp::Endpoint.
virtual std::vector< shared_ptr< const Channel > > getChannels() const 1
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
Copyright (c) 2011-2015 Regents of the University of California.
const std::string & getPort() const
get port
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onConnectFailed) 1
Try to create Face using the supplied FaceUri.
boost::asio::ip::udp::endpoint Endpoint
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
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...
const std::string & getHost() const
get host (domain)