33 #include <sys/socket.h> 43 boost::asio::ip::address_v4::from_string(
"0.0.0.0"));
46 boost::asio::ip::address_v6::from_string(
"::"));
49 : m_defaultPort(defaultPort)
58 const address& address = endpoint.address();
62 prohibitAllIpv4Endpoints(endpoint.port());
66 prohibitAllIpv6Endpoints(endpoint.port());
71 m_prohibitedEndpoints.insert(endpoint);
75 UdpFactory::prohibitAllIpv4Endpoints(
const uint16_t port)
80 for (
const address_v4& addr : nic.ipv4Addresses) {
86 if (nic.isBroadcastCapable() && nic.broadcastAddress !=
ALL_V4_ENDPOINT)
88 NFD_LOG_TRACE(
"prohibiting broadcast address: " << nic.broadcastAddress.to_string());
93 prohibitEndpoint(
udp::Endpoint(address::from_string(
"255.255.255.255"), port));
97 UdpFactory::prohibitAllIpv6Endpoints(
const uint16_t port)
102 for (
const address_v6& addr : nic.ipv6Addresses) {
110 shared_ptr<UdpChannel>
112 const time::seconds& timeout)
116 shared_ptr<UdpChannel> channel = findChannel(endpoint);
117 if (static_cast<bool>(channel))
121 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
122 if (static_cast<bool>(multicast))
123 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP unicast channel, local " 124 "endpoint is already allocated for a UDP multicast face"));
126 if (endpoint.address().is_multicast()) {
127 BOOST_THROW_EXCEPTION(
Error(
"This method is only for unicast channel. The provided " 128 "endpoint is multicast. Use createMulticastFace to " 129 "create a multicast face"));
132 channel = make_shared<UdpChannel>(endpoint, timeout);
133 m_channels[endpoint] = channel;
134 prohibitEndpoint(endpoint);
139 shared_ptr<UdpChannel>
141 const std::string& localPort,
142 const time::seconds& timeout)
145 udp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort));
149 shared_ptr<MulticastUdpFace>
152 const std::string& networkInterfaceName)
155 shared_ptr<MulticastUdpFace> face = findMulticastFace(localEndpoint);
156 if (static_cast<bool>(face)) {
157 if (face->getMulticastGroup() == multicastEndpoint)
160 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, local " 161 "endpoint is already allocated for a UDP multicast face " 162 "on a different multicast group"));
166 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
167 if (static_cast<bool>(unicast)) {
168 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, local " 169 "endpoint is already allocated for a UDP unicast channel"));
172 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
173 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 174 "remote endpoint is owned by this NFD instance"));
177 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
178 BOOST_THROW_EXCEPTION(
Error(
"IPv6 multicast is not supported yet. Please provide an IPv4 " 182 if (localEndpoint.port() != multicastEndpoint.port()) {
183 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 184 "both endpoints should have the same port number. "));
187 if (!multicastEndpoint.address().is_multicast()) {
188 BOOST_THROW_EXCEPTION(
Error(
"Cannot create the requested UDP multicast face, " 189 "the multicast group given as input is not a multicast address"));
193 receiveSocket.open(multicastEndpoint.protocol());
194 receiveSocket.set_option(ip::udp::socket::reuse_address(
true));
195 receiveSocket.bind(multicastEndpoint);
198 sendSocket.open(multicastEndpoint.protocol());
199 sendSocket.set_option(ip::udp::socket::reuse_address(
true));
200 sendSocket.set_option(ip::multicast::enable_loopback(
false));
201 sendSocket.bind(
udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
203 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
205 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
206 localEndpoint.address().to_v4()));
207 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
208 localEndpoint.address().to_v4()));
219 if (!networkInterfaceName.empty()) {
220 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
221 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
222 BOOST_THROW_EXCEPTION(
Error(
"Cannot bind multicast face to " + networkInterfaceName +
223 ": " + std::strerror(errno)));
228 face = make_shared<MulticastUdpFace>(multicastEndpoint,
FaceUri(localEndpoint),
229 std::move(receiveSocket), std::move(sendSocket));
231 face->onFail.connectSingleShot([
this, localEndpoint] (
const std::string& reason) {
232 m_multicastFaces.erase(localEndpoint);
234 m_multicastFaces[localEndpoint] = face;
239 shared_ptr<MulticastUdpFace>
241 const std::string& multicastIp,
242 const std::string& multicastPort,
243 const std::string& networkInterfaceName)
245 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
246 boost::lexical_cast<uint16_t>(multicastPort));
247 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
248 boost::lexical_cast<uint16_t>(multicastPort));
259 BOOST_THROW_EXCEPTION(
Error(
"UdpFactory::createFace does not support creating on-demand face"));
264 boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.
getHost());
267 if (endpoint.address().is_multicast()) {
268 onConnectFailed(
"The provided address is multicast. Please use createMulticastFace method");
272 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
273 onConnectFailed(
"Requested endpoint is prohibited " 274 "(reserved by this NFD or disallowed by face management protocol)");
280 channel != m_channels.end();
283 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
284 (channel->first.address().is_v6() && endpoint.address().is_v6()))
286 channel->second->connect(endpoint, persistency, onCreated, onConnectFailed);
291 onConnectFailed(
"No channels available to connect to " +
292 boost::lexical_cast<std::string>(endpoint));
295 shared_ptr<UdpChannel>
299 if (i != m_channels.end())
302 return shared_ptr<UdpChannel>();
305 shared_ptr<MulticastUdpFace>
306 UdpFactory::findMulticastFace(
const udp::Endpoint& localEndpoint)
309 if (i != m_multicastFaces.end())
312 return shared_ptr<MulticastUdpFace>();
315 std::list<shared_ptr<const Channel>>
318 std::list<shared_ptr<const Channel>> channels;
319 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
321 channels.push_back(i->second);
function< void(const std::string &reason)> FaceConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
#define NFD_LOG_DEBUG(expression)
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onCreated, const FaceConnectFailedCallback &onConnectFailed) 1
Try to create Face using the supplied FaceUri.
represents the underlying protocol and address used by a Face
static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(boost::asio::ip::address_v6::from_string("::"))
contains information about a network interface
UdpFactory(const std::string &defaultPort="6363")
virtual std::list< shared_ptr< const Channel > > getChannels() const
shared_ptr< UdpChannel > createChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout=time::seconds(600))
Create UDP-based channel using udp::Endpoint.
const std::string & getPort() const
get port
std::vector< NetworkInterfaceInfo > listNetworkInterfaces()
List configured network interfaces on the system and their info.
Table::const_iterator iterator
bool isCanonical() const
determine whether this FaceUri is in canonical form
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< MulticastUdpFace > createMulticastFace(const udp::Endpoint &localEndpoint, const udp::Endpoint &multicastEndpoint, const std::string &networkInterfaceName="")
Create MulticastUdpFace using udp::Endpoint.
boost::asio::ip::udp::endpoint Endpoint
#define NFD_LOG_INIT(name)
const std::string & getHost() const
get host (domain)
#define NFD_LOG_TRACE(expression)
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...
boost::asio::io_service & getGlobalIoService()
static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(boost::asio::ip::address_v4::from_string("0.0.0.0"))