blob: 7ec42b36b59f7ea5b1b9ced28be6784cb4c2de10 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Shi64d99f22017-01-21 23:06:36 +000056 /** \brief process face_system.udp config section
57 */
58 void
59 processConfig(OptionalConfigSection configSection,
60 FaceSystem::ConfigContext& context) override;
61
62 void
63 createFace(const FaceUri& uri,
64 ndn::nfd::FacePersistency persistency,
65 bool wantLocalFieldsEnabled,
66 const FaceCreatedCallback& onCreated,
67 const FaceCreationFailedCallback& onFailure) override;
Alexander Afanasyev5959b012014-06-02 19:18:12 +030068
Giulio Grassi624f6c62014-02-18 19:42:14 +010069 /**
70 * \brief Create UDP-based channel using udp::Endpoint
71 *
72 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
73 *
74 * If this method called twice with the same endpoint, only one channel
75 * will be created. The second call will just retrieve the existing
76 * channel.
77 *
78 * If a multicast face is already active on the same local endpoint,
79 * the creation fails and an exception is thrown
80 *
81 * Once a face is created, if it doesn't send/receive anything for
82 * a period of time equal to timeout, it will be destroyed
83 * @todo this funcionality has to be implemented
84 *
85 * \returns always a valid pointer to a UdpChannel object, an exception
86 * is thrown if it cannot be created.
87 *
88 * \throws UdpFactory::Error
89 *
90 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
91 * for details on ways to create udp::Endpoint
92 */
93 shared_ptr<UdpChannel>
94 createChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyev5959b012014-06-02 19:18:12 +030095 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010096
97 /**
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080098 * \brief Create UDP-based channel using specified IP address and port number
Giulio Grassi624f6c62014-02-18 19:42:14 +010099 *
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800100 * This method is just a helper that converts a string representation of localIp and port to
101 * udp::Endpoint and calls the other createChannel overload.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100102 *
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600103 * If localHost is a IPv6 address of a specific device, it must be in the form:
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 * ip address%interface name
105 * Example: fe80::5e96:9dff:fe7d:9c8d%en1
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600106 * Otherwise, you can use ::
Giulio Grassi624f6c62014-02-18 19:42:14 +0100107 *
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108 * \throws UdpChannel::Error if the bind on the socket fails
109 * \throws UdpFactory::Error
110 */
111 shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200112 createChannel(const std::string& localIp, const std::string& localPort,
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300113 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100114
Junxiao Shi64d99f22017-01-21 23:06:36 +0000115 std::vector<shared_ptr<const Channel>>
116 getChannels() const override;
117
Giulio Grassi624f6c62014-02-18 19:42:14 +0100118 /**
119 * \brief Create MulticastUdpFace using udp::Endpoint
120 *
121 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
122 *
123 * The face will join the multicast group
124 *
125 * If this method called twice with the same endpoint and group, only one face
126 * will be created. The second call will just retrieve the existing
127 * channel.
128 *
129 * If an unicast face is already active on the same local NIC and port, the
130 * creation fails and an exception is thrown
131 *
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500132 * \param localEndpoint local endpoint
133 * \param multicastEndpoint multicast endpoint
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200134 * \param networkInterfaceName name of the network interface on which the face will be bound
135 * (Used only on multihomed linux machine with more than one MulticastUdpFace for
136 * the same multicast group. If specified, will requires CAP_NET_RAW capability)
137 * An empty string can be provided in other system or in linux machine with only one
138 * MulticastUdpFace per multicast group
139 *
140 *
Giulio Grassi624f6c62014-02-18 19:42:14 +0100141 * \returns always a valid pointer to a MulticastUdpFace object, an exception
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600142 * is thrown if it cannot be created.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100143 *
144 * \throws UdpFactory::Error
145 *
146 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
147 * for details on ways to create udp::Endpoint
148 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700149 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100150 createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200151 const udp::Endpoint& multicastEndpoint,
152 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600153
Junxiao Shicde37ad2015-12-24 01:02:05 -0700154 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100155 createMulticastFace(const std::string& localIp,
156 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200157 const std::string& multicastPort,
158 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600159
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800160PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600161 void
162 prohibitEndpoint(const udp::Endpoint& endpoint);
163
164 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200165 prohibitAllIpv4Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600166
167 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200168 prohibitAllIpv6Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600169
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200170private:
Giulio Grassi624f6c62014-02-18 19:42:14 +0100171 /**
172 * \brief Look up UdpChannel using specified local endpoint
173 *
174 * \returns shared pointer to the existing UdpChannel object
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200175 * or nullptr when such channel does not exist
Giulio Grassi624f6c62014-02-18 19:42:14 +0100176 */
177 shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200178 findChannel(const udp::Endpoint& localEndpoint) const;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600179
Giulio Grassi624f6c62014-02-18 19:42:14 +0100180 /**
181 * \brief Look up multicast UdpFace using specified local endpoint
182 *
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200183 * \returns shared pointer to the existing multicast UdpFace object
Yukai Tu0a49d342015-09-13 12:54:22 +0800184 * or nullptr when such face does not exist
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700186 shared_ptr<Face>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200187 findMulticastFace(const udp::Endpoint& localEndpoint) const;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600188
Junxiao Shi64d99f22017-01-21 23:06:36 +0000189 void
190 applyMulticastConfig(const FaceSystem::ConfigContext& context);
191
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200192private:
193 std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000194
195 struct MulticastConfig
196 {
197 bool isEnabled = false;
198 udp::Endpoint group = udp::getDefaultMulticastGroup();
199 };
200
201 MulticastConfig m_mcastConfig;
202 std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600203
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200204PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600205 std::set<udp::Endpoint> m_prohibitedEndpoints;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100206};
207
Junxiao Shi64d99f22017-01-21 23:06:36 +0000208} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209} // namespace nfd
210
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700211#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP