blob: 2f809ac288a35bf574d87b46027390410d6cb278 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0ba6d642017-07-17 00:53:22 +00002/*
Junxiao Shi64d99f22017-01-21 23:06:36 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Chengyu Fan4381fb62015-01-14 11:37:04 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UDP_FACTORY_HPP
27#define NFD_DAEMON_FACE_UDP_FACTORY_HPP
Giulio Grassi624f6c62014-02-18 19:42:14 +010028
29#include "protocol-factory.hpp"
30#include "udp-channel.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010031
Giulio Grassi624f6c62014-02-18 19:42:14 +010032namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000033namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010034
Junxiao Shi64d99f22017-01-21 23:06:36 +000035/** \brief protocol factory for UDP over IPv4 and IPv6
36 *
37 * UDP unicast is available over both IPv4 and IPv6.
38 * UDP multicast is available over IPv4 only.
39 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010040class UdpFactory : public ProtocolFactory
41{
42public:
43 /**
44 * \brief Exception of UdpFactory
45 */
Alexander Afanasyev5959b012014-06-02 19:18:12 +030046 class Error : public ProtocolFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010047 {
Alexander Afanasyev5959b012014-06-02 19:18:12 +030048 public:
49 explicit
50 Error(const std::string& what)
51 : ProtocolFactory::Error(what)
52 {
53 }
Giulio Grassi624f6c62014-02-18 19:42:14 +010054 };
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060055
Junxiao Shib47247d2017-01-24 15:09:16 +000056 static const std::string&
57 getId();
58
Junxiao Shi0ba6d642017-07-17 00:53:22 +000059 explicit
60 UdpFactory(const CtorParams& params);
61
Junxiao Shi64d99f22017-01-21 23:06:36 +000062 /** \brief process face_system.udp config section
63 */
64 void
65 processConfig(OptionalConfigSection configSection,
66 FaceSystem::ConfigContext& context) override;
67
68 void
Eric Newberry78e32b02017-04-01 14:34:44 +000069 createFace(const FaceUri& remoteUri,
70 const ndn::optional<FaceUri>& localUri,
Junxiao Shi64d99f22017-01-21 23:06:36 +000071 ndn::nfd::FacePersistency persistency,
72 bool wantLocalFieldsEnabled,
73 const FaceCreatedCallback& onCreated,
74 const FaceCreationFailedCallback& onFailure) override;
Alexander Afanasyev5959b012014-06-02 19:18:12 +030075
Giulio Grassi624f6c62014-02-18 19:42:14 +010076 /**
77 * \brief Create UDP-based channel using udp::Endpoint
78 *
79 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
80 *
Davide Pesavento46afec42017-05-28 14:28:47 -040081 * If this method is called twice with the same endpoint, only one channel
82 * will be created. The second call will just return the existing channel.
Giulio Grassi624f6c62014-02-18 19:42:14 +010083 *
84 * If a multicast face is already active on the same local endpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040085 * the creation fails and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +010086 *
Davide Pesavento46afec42017-05-28 14:28:47 -040087 * \return always a valid pointer to a UdpChannel object, an exception
88 * is thrown if it cannot be created.
89 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010090 */
91 shared_ptr<UdpChannel>
92 createChannel(const udp::Endpoint& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040093 time::nanoseconds idleTimeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010094
95 /**
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080096 * \brief Create UDP-based channel using specified IP address and port number
Giulio Grassi624f6c62014-02-18 19:42:14 +010097 *
Davide Pesavento46afec42017-05-28 14:28:47 -040098 * This method is just a helper that converts the string representation of \p localIp
99 * and \p localPort to udp::Endpoint and calls the other createChannel overload.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400101 * If \p localIp is an IPv6 address of a specific device, it must be in the form
102 * <tt>[ip address]%[interface name]</tt>, e.g. <tt>"fe80::5e96:9dff:fe7d:9c8d%en1"</tt>.
103 * Otherwise, you can use <tt>"::"</tt>.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400105 * \return always a valid pointer to a UdpChannel object, an exception
106 * is thrown if it cannot be created.
107 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108 */
109 shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200110 createChannel(const std::string& localIp, const std::string& localPort,
Davide Pesavento46afec42017-05-28 14:28:47 -0400111 time::nanoseconds idleTimeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100112
Junxiao Shi64d99f22017-01-21 23:06:36 +0000113 std::vector<shared_ptr<const Channel>>
114 getChannels() const override;
115
Giulio Grassi624f6c62014-02-18 19:42:14 +0100116 /**
117 * \brief Create MulticastUdpFace using udp::Endpoint
118 *
119 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
120 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400121 * The face will join the specified multicast group.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100122 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400123 * If this method is called twice with the same pair of endpoints, only one
124 * face will be created. The second call will just return the existing face.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100125 *
126 * If an unicast face is already active on the same local NIC and port, the
Davide Pesavento46afec42017-05-28 14:28:47 -0400127 * creation fails and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128 *
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500129 * \param localEndpoint local endpoint
130 * \param multicastEndpoint multicast endpoint
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200131 * \param networkInterfaceName name of the network interface on which the face will be bound
132 * (Used only on multihomed linux machine with more than one MulticastUdpFace for
133 * the same multicast group. If specified, will requires CAP_NET_RAW capability)
134 * An empty string can be provided in other system or in linux machine with only one
135 * MulticastUdpFace per multicast group
136 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400137 * \return always a valid pointer to a MulticastUdpFace object, an exception
138 * is thrown if it cannot be created.
139 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700141 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142 createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200143 const udp::Endpoint& multicastEndpoint,
144 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600145
Junxiao Shicde37ad2015-12-24 01:02:05 -0700146 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100147 createMulticastFace(const std::string& localIp,
148 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200149 const std::string& multicastPort,
150 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600151
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800152PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600153 void
154 prohibitEndpoint(const udp::Endpoint& endpoint);
155
156 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200157 prohibitAllIpv4Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600158
159 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200160 prohibitAllIpv6Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600161
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200162private:
Junxiao Shi64d99f22017-01-21 23:06:36 +0000163 void
164 applyMulticastConfig(const FaceSystem::ConfigContext& context);
165
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200166private:
167 std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000168
169 struct MulticastConfig
170 {
171 bool isEnabled = false;
172 udp::Endpoint group = udp::getDefaultMulticastGroup();
Teng Liangfe4fce32017-03-29 04:49:38 +0000173 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS;
Junxiao Shic31080d2017-01-24 15:10:12 +0000174 NetworkInterfacePredicate netifPredicate;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000175 };
176
177 MulticastConfig m_mcastConfig;
178 std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600179
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200180PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600181 std::set<udp::Endpoint> m_prohibitedEndpoints;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100182};
183
Junxiao Shi64d99f22017-01-21 23:06:36 +0000184} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185} // namespace nfd
186
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700187#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP