blob: c788a3f62e441ec9bd00790065765cfdb9cc47e6 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02003 * Copyright (c) 2014-2016, 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 {
33
Davide Pesavento292e5e12015-03-13 02:08:33 +010034/// @todo IPv6 multicast support not implemented
Giulio Grassi624f6c62014-02-18 19:42:14 +010035
36class UdpFactory : public ProtocolFactory
37{
38public:
39 /**
40 * \brief Exception of UdpFactory
41 */
Alexander Afanasyev5959b012014-06-02 19:18:12 +030042 class Error : public ProtocolFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010043 {
Alexander Afanasyev5959b012014-06-02 19:18:12 +030044 public:
45 explicit
46 Error(const std::string& what)
47 : ProtocolFactory::Error(what)
48 {
49 }
Giulio Grassi624f6c62014-02-18 19:42:14 +010050 };
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060051
Junxiao Shicde37ad2015-12-24 01:02:05 -070052 typedef std::map<udp::Endpoint, shared_ptr<Face>> MulticastFaceMap;
Alexander Afanasyev5959b012014-06-02 19:18:12 +030053
Giulio Grassi624f6c62014-02-18 19:42:14 +010054 /**
55 * \brief Create UDP-based channel using udp::Endpoint
56 *
57 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
58 *
59 * If this method called twice with the same endpoint, only one channel
60 * will be created. The second call will just retrieve the existing
61 * channel.
62 *
63 * If a multicast face is already active on the same local endpoint,
64 * the creation fails and an exception is thrown
65 *
66 * Once a face is created, if it doesn't send/receive anything for
67 * a period of time equal to timeout, it will be destroyed
68 * @todo this funcionality has to be implemented
69 *
70 * \returns always a valid pointer to a UdpChannel object, an exception
71 * is thrown if it cannot be created.
72 *
73 * \throws UdpFactory::Error
74 *
75 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
76 * for details on ways to create udp::Endpoint
77 */
78 shared_ptr<UdpChannel>
79 createChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyev5959b012014-06-02 19:18:12 +030080 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010081
82 /**
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080083 * \brief Create UDP-based channel using specified IP address and port number
Giulio Grassi624f6c62014-02-18 19:42:14 +010084 *
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080085 * This method is just a helper that converts a string representation of localIp and port to
86 * udp::Endpoint and calls the other createChannel overload.
Giulio Grassi624f6c62014-02-18 19:42:14 +010087 *
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060088 * If localHost is a IPv6 address of a specific device, it must be in the form:
Giulio Grassi624f6c62014-02-18 19:42:14 +010089 * ip address%interface name
90 * Example: fe80::5e96:9dff:fe7d:9c8d%en1
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060091 * Otherwise, you can use ::
Giulio Grassi624f6c62014-02-18 19:42:14 +010092 *
Giulio Grassi624f6c62014-02-18 19:42:14 +010093 * \throws UdpChannel::Error if the bind on the socket fails
94 * \throws UdpFactory::Error
95 */
96 shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020097 createChannel(const std::string& localIp, const std::string& localPort,
Alexander Afanasyev5959b012014-06-02 19:18:12 +030098 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010099
100 /**
101 * \brief Create MulticastUdpFace using udp::Endpoint
102 *
103 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
104 *
105 * The face will join the multicast group
106 *
107 * If this method called twice with the same endpoint and group, only one face
108 * will be created. The second call will just retrieve the existing
109 * channel.
110 *
111 * If an unicast face is already active on the same local NIC and port, the
112 * creation fails and an exception is thrown
113 *
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500114 * \param localEndpoint local endpoint
115 * \param multicastEndpoint multicast endpoint
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200116 * \param networkInterfaceName name of the network interface on which the face will be bound
117 * (Used only on multihomed linux machine with more than one MulticastUdpFace for
118 * the same multicast group. If specified, will requires CAP_NET_RAW capability)
119 * An empty string can be provided in other system or in linux machine with only one
120 * MulticastUdpFace per multicast group
121 *
122 *
Giulio Grassi624f6c62014-02-18 19:42:14 +0100123 * \returns always a valid pointer to a MulticastUdpFace object, an exception
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600124 * is thrown if it cannot be created.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100125 *
126 * \throws UdpFactory::Error
127 *
128 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
129 * for details on ways to create udp::Endpoint
130 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700131 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100132 createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200133 const udp::Endpoint& multicastEndpoint,
134 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600135
Junxiao Shicde37ad2015-12-24 01:02:05 -0700136 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100137 createMulticastFace(const std::string& localIp,
138 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200139 const std::string& multicastPort,
140 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600141
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142 /**
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300143 * \brief Get map of configured multicast faces
Giulio Grassi624f6c62014-02-18 19:42:14 +0100144 */
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300145 const MulticastFaceMap&
146 getMulticastFaces() const;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100147
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200148public: // from ProtocolFactory
149 virtual void
150 createFace(const FaceUri& uri,
151 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700152 bool wantLocalFieldsEnabled,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200153 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700154 const FaceCreationFailedCallback& onFailure) override;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200155
156 virtual std::vector<shared_ptr<const Channel>>
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200157 getChannels() const override;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200158
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800159PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600160 void
161 prohibitEndpoint(const udp::Endpoint& endpoint);
162
163 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200164 prohibitAllIpv4Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600165
166 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200167 prohibitAllIpv6Endpoints(uint16_t port);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600168
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200169private:
Giulio Grassi624f6c62014-02-18 19:42:14 +0100170 /**
171 * \brief Look up UdpChannel using specified local endpoint
172 *
173 * \returns shared pointer to the existing UdpChannel object
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200174 * or nullptr when such channel does not exist
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175 */
176 shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200177 findChannel(const udp::Endpoint& localEndpoint) const;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600178
Giulio Grassi624f6c62014-02-18 19:42:14 +0100179 /**
180 * \brief Look up multicast UdpFace using specified local endpoint
181 *
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200182 * \returns shared pointer to the existing multicast UdpFace object
Yukai Tu0a49d342015-09-13 12:54:22 +0800183 * or nullptr when such face does not exist
Giulio Grassi624f6c62014-02-18 19:42:14 +0100184 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700185 shared_ptr<Face>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200186 findMulticastFace(const udp::Endpoint& localEndpoint) const;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600187
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200188private:
189 std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300190 MulticastFaceMap m_multicastFaces;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600191
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200192PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600193 std::set<udp::Endpoint> m_prohibitedEndpoints;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100194};
195
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300196inline const UdpFactory::MulticastFaceMap&
197UdpFactory::getMulticastFaces() const
198{
199 return m_multicastFaces;
200}
201
Giulio Grassi624f6c62014-02-18 19:42:14 +0100202} // namespace nfd
203
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700204#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP