blob: e99961a791c0c2031cb2af0d33bbed4018c2e4bd [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "udp-factory.hpp"
8#include "core/global-io.hpp"
9#include "core/resolver.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060010#include "core/network-interface.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010011
12namespace nfd {
13
14using namespace boost::asio;
Junxiao Shi79494162014-04-02 18:25:11 -070015
Giulio Grassi624f6c62014-02-18 19:42:14 +010016NFD_LOG_INIT("UdpFactory");
17
18UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/)
19 : m_defaultPort(defaultPort)
20{
21}
22
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060023
24
25void
26UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
27{
28 using namespace boost::asio::ip;
29
30 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
31 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
32
33 const address& address = endpoint.address();
34
35 if (address.is_v4() && address == ALL_V4_ENDPOINT)
36 {
37 prohibitAllIpv4Endpoints(endpoint.port());
38 }
39 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
40 {
41 prohibitAllIpv6Endpoints(endpoint.port());
42 }
43
44 NFD_LOG_TRACE("prohibiting UDP " <<
45 endpoint.address().to_string() << ":" << endpoint.port());
46
47 m_prohibitedEndpoints.insert(endpoint);
48}
49
50void
51UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
52{
53 using namespace boost::asio::ip;
54
55 static const address_v4 INVALID_BROADCAST(address_v4::from_string("0.0.0.0"));
56
57 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
58
59 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
60 i != nicList.end();
61 ++i)
62 {
63 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
64 const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses;
65
66 for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin();
67 j != ipv4Addresses.end();
68 ++j)
69 {
70 prohibitEndpoint(udp::Endpoint(*j, port));
71 }
72
73 if (nic->isBroadcastCapable() && nic->broadcastAddress != INVALID_BROADCAST)
74 {
75 NFD_LOG_TRACE("prohibiting broadcast address: " << nic->broadcastAddress.to_string());
76 prohibitEndpoint(udp::Endpoint(nic->broadcastAddress, port));
77 }
78 }
79
80 prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
81}
82
83void
84UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
85{
86 using namespace boost::asio::ip;
87
88 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
89
90 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
91 i != nicList.end();
92 ++i)
93 {
94 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
95 const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses;
96
97 for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin();
98 j != ipv6Addresses.end();
99 ++j)
100 {
101 prohibitEndpoint(udp::Endpoint(*j, port));
102 }
103 }
104}
105
Giulio Grassi624f6c62014-02-18 19:42:14 +0100106shared_ptr<UdpChannel>
107UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700108 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100109{
110 NFD_LOG_DEBUG("Creating unicast " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700111
Giulio Grassi624f6c62014-02-18 19:42:14 +0100112 shared_ptr<UdpChannel> channel = findChannel(endpoint);
113 if (static_cast<bool>(channel))
114 return channel;
115
Junxiao Shi79494162014-04-02 18:25:11 -0700116
117 //checking if the endpoint is already in use for multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100118 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
119 if (static_cast<bool>(multicast))
120 throw Error("Cannot create the requested UDP unicast channel, local "
121 "endpoint is already allocated for a UDP multicast face");
Junxiao Shi79494162014-04-02 18:25:11 -0700122
Giulio Grassi624f6c62014-02-18 19:42:14 +0100123 if (endpoint.address().is_multicast()) {
124 throw Error("This method is only for unicast channel. The provided "
125 "endpoint is multicast. Use createMulticastFace to "
126 "create a multicast face");
127 }
128
129 channel = make_shared<UdpChannel>(boost::cref(endpoint),
130 timeout);
131 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600132 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100133
134 return channel;
135}
136
137shared_ptr<UdpChannel>
138UdpFactory::createChannel(const std::string& localHost,
139 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700140 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100141{
142 return createChannel(UdpResolver::syncResolve(localHost, localPort),
143 timeout);
144}
145
146shared_ptr<MulticastUdpFace>
147UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700148 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100149{
150 //checking if the local and musticast endpoint are already in use for a multicast face
151 shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint);
152 if (static_cast<bool>(multicastFace)) {
153 if (multicastFace->getMulticastGroup() == multicastEndpoint)
154 return multicastFace;
155 else
156 throw Error("Cannot create the requested UDP multicast face, local "
157 "endpoint is already allocated for a UDP multicast face "
158 "on a different multicast group");
159 }
Junxiao Shi79494162014-04-02 18:25:11 -0700160
Giulio Grassi624f6c62014-02-18 19:42:14 +0100161 //checking if the local endpoint is already in use for an unicast channel
162 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
163 if (static_cast<bool>(unicast)) {
164 throw Error("Cannot create the requested UDP multicast face, local "
165 "endpoint is already allocated for a UDP unicast channel");
166 }
167
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600168 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
169 throw Error("Cannot create the requested UDP multicast face, "
170 "remote endpoint is owned by this NFD instance");
171 }
172
Giulio Grassi624f6c62014-02-18 19:42:14 +0100173 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
174 throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address");
175 }
Junxiao Shi79494162014-04-02 18:25:11 -0700176
Giulio Grassi624f6c62014-02-18 19:42:14 +0100177 if (localEndpoint.port() != multicastEndpoint.port()) {
178 throw Error("Cannot create the requested UDP multicast face, "
179 "both endpoints should have the same port number. ");
180 }
181
182 if (!multicastEndpoint.address().is_multicast()) {
183 throw Error("Cannot create the requested UDP multicast face, "
184 "the multicast group given as input is not a multicast address");
185 }
186
187 shared_ptr<ip::udp::socket> clientSocket =
188 make_shared<ip::udp::socket>(boost::ref(getGlobalIoService()));
Junxiao Shi79494162014-04-02 18:25:11 -0700189
Giulio Grassi624f6c62014-02-18 19:42:14 +0100190 clientSocket->open(multicastEndpoint.protocol());
191
192 clientSocket->set_option(ip::udp::socket::reuse_address(true));
193
194 try {
195 clientSocket->bind(multicastEndpoint);
196
197 if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) {
198 clientSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
199 }
200 clientSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
201 localEndpoint.address().to_v4()));
202 }
203 catch (boost::system::system_error& e) {
204 std::stringstream msg;
205 msg << "Failed to properly configure the socket, check the address (" << e.what() << ")";
206 throw Error(msg.str());
207 }
208
209 clientSocket->set_option(ip::multicast::enable_loopback(false));
210
Junxiao Shi79494162014-04-02 18:25:11 -0700211 multicastFace = make_shared<MulticastUdpFace>(boost::cref(clientSocket), localEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100212 multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint);
213
214 m_multicastFaces[localEndpoint] = multicastFace;
215
216 return multicastFace;
217}
218
219shared_ptr<MulticastUdpFace>
220UdpFactory::createMulticastFace(const std::string& localIp,
221 const std::string& multicastIp,
222 const std::string& multicastPort)
223{
Junxiao Shi79494162014-04-02 18:25:11 -0700224
Giulio Grassi624f6c62014-02-18 19:42:14 +0100225 return createMulticastFace(UdpResolver::syncResolve(localIp,
226 multicastPort),
227 UdpResolver::syncResolve(multicastIp,
228 multicastPort));
229}
230
231void
232UdpFactory::createFace(const FaceUri& uri,
233 const FaceCreatedCallback& onCreated,
234 const FaceConnectFailedCallback& onConnectFailed)
235{
236 resolver::AddressSelector addressSelector = resolver::AnyAddress();
237 if (uri.getScheme() == "udp4")
238 addressSelector = resolver::Ipv4Address();
239 else if (uri.getScheme() == "udp6")
240 addressSelector = resolver::Ipv6Address();
Junxiao Shi79494162014-04-02 18:25:11 -0700241
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600242 if (!uri.getPath().empty())
243 {
244 onConnectFailed("Invalid URI");
245 }
246
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700247 UdpResolver::asyncResolve(uri.getHost(),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100248 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
249 bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
250 onCreated, onConnectFailed),
251 onConnectFailed,
252 addressSelector);
253
254}
255
256void
257UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
258 const FaceCreatedCallback& onCreated,
259 const FaceConnectFailedCallback& onConnectFailed)
260{
261 if (endpoint.address().is_multicast()) {
262 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
263 return;
264 }
Junxiao Shi79494162014-04-02 18:25:11 -0700265
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600266 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
267 {
268 onConnectFailed("Requested endpoint is prohibited "
269 "(reserved by this NFD or disallowed by face management protocol)");
270 return;
271 }
272
Giulio Grassi624f6c62014-02-18 19:42:14 +0100273 // very simple logic for now
Junxiao Shi79494162014-04-02 18:25:11 -0700274
Giulio Grassi624f6c62014-02-18 19:42:14 +0100275 for (ChannelMap::iterator channel = m_channels.begin();
276 channel != m_channels.end();
277 ++channel)
278 {
279 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
280 (channel->first.address().is_v6() && endpoint.address().is_v6()))
281 {
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600282 channel->second->connect(endpoint, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100283 return;
284 }
285 }
286 onConnectFailed("No channels available to connect to "
287 + boost::lexical_cast<std::string>(endpoint));
288}
289
290shared_ptr<UdpChannel>
291UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
292{
293 ChannelMap::iterator i = m_channels.find(localEndpoint);
294 if (i != m_channels.end())
295 return i->second;
296 else
297 return shared_ptr<UdpChannel>();
298}
299
300shared_ptr<MulticastUdpFace>
301UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
302{
303 MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
304 if (i != m_multicastFaces.end())
305 return i->second;
306 else
307 return shared_ptr<MulticastUdpFace>();
308}
309
310void
311UdpFactory::afterFaceFailed(udp::Endpoint& endpoint)
312{
313 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
314 m_multicastFaces.erase(endpoint);
315}
316
317
318} // namespace nfd