blob: 9e20be4bffdda26588715307853a58a50fffd869 [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 Newberry944f38b2017-07-20 20:54:22 -040069 createFace(const CreateFaceParams& params,
Junxiao Shi64d99f22017-01-21 23:06:36 +000070 const FaceCreatedCallback& onCreated,
71 const FaceCreationFailedCallback& onFailure) override;
Alexander Afanasyev5959b012014-06-02 19:18:12 +030072
Giulio Grassi624f6c62014-02-18 19:42:14 +010073 /**
74 * \brief Create UDP-based channel using udp::Endpoint
75 *
76 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
77 *
Davide Pesavento46afec42017-05-28 14:28:47 -040078 * If this method is called twice with the same endpoint, only one channel
79 * will be created. The second call will just return the existing channel.
Giulio Grassi624f6c62014-02-18 19:42:14 +010080 *
81 * If a multicast face is already active on the same local endpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040082 * the creation fails and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +010083 *
Davide Pesavento46afec42017-05-28 14:28:47 -040084 * \return always a valid pointer to a UdpChannel object, an exception
85 * is thrown if it cannot be created.
86 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010087 */
88 shared_ptr<UdpChannel>
89 createChannel(const udp::Endpoint& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040090 time::nanoseconds idleTimeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010091
Junxiao Shi64d99f22017-01-21 23:06:36 +000092 std::vector<shared_ptr<const Channel>>
93 getChannels() const override;
94
Giulio Grassi624f6c62014-02-18 19:42:14 +010095 /**
Davide Pesaventobb734df2017-10-24 18:05:36 -040096 * \brief Create a multicast UDP face
Giulio Grassi624f6c62014-02-18 19:42:14 +010097 *
98 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
99 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400100 * The face will join the specified multicast group.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100101 *
Davide Pesavento46afec42017-05-28 14:28:47 -0400102 * If this method is called twice with the same pair of endpoints, only one
103 * face will be created. The second call will just return the existing face.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 *
Davide Pesaventobb734df2017-10-24 18:05:36 -0400105 * If a UDP channel, unicast face, or multicast face already exists on the
106 * same local endpoint, the creation fails and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100107 *
Davide Pesaventobb734df2017-10-24 18:05:36 -0400108 * \param localEndpoint the local endpoint
109 * \param multicastEndpoint the multicast endpoint
110 * \param netif the network interface to which the face will be bound
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200111 *
Davide Pesaventobb734df2017-10-24 18:05:36 -0400112 * \return always a valid shared pointer to the created face;
113 * an exception is thrown if the face cannot be created.
Davide Pesavento46afec42017-05-28 14:28:47 -0400114 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +0100115 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 shared_ptr<Face>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100117 createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200118 const udp::Endpoint& multicastEndpoint,
Davide Pesaventobb734df2017-10-24 18:05:36 -0400119 const ndn::net::NetworkInterface& netif);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600120
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200121private:
Davide Pesaventobb734df2017-10-24 18:05:36 -0400122 /** \brief Create UDP multicast face on \p netif if needed by \p m_mcastConfig
Junxiao Shibbace1d2017-08-06 20:03:37 +0000123 * \return new or existing face, or nullptr if no face should be created
124 */
125 shared_ptr<Face>
126 applyMcastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif);
127
Davide Pesaventobb734df2017-10-24 18:05:36 -0400128 /** \brief Create and destroy UDP multicast faces according to \p m_mcastConfig
Junxiao Shibbace1d2017-08-06 20:03:37 +0000129 */
Junxiao Shi64d99f22017-01-21 23:06:36 +0000130 void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000131 applyMcastConfig(const FaceSystem::ConfigContext& context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000132
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200133private:
134 std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000135
136 struct MulticastConfig
137 {
138 bool isEnabled = false;
139 udp::Endpoint group = udp::getDefaultMulticastGroup();
Teng Liangfe4fce32017-03-29 04:49:38 +0000140 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS;
Junxiao Shic31080d2017-01-24 15:10:12 +0000141 NetworkInterfacePredicate netifPredicate;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000142 };
143
144 MulticastConfig m_mcastConfig;
145 std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000146
147 signal::ScopedConnection m_netifAddConn;
148 struct NetifConns
149 {
150 signal::ScopedConnection addrAddConn;
151 };
152 std::map<int, NetifConns> m_netifConns; // ifindex => signal connections
Giulio Grassi624f6c62014-02-18 19:42:14 +0100153};
154
Junxiao Shi64d99f22017-01-21 23:06:36 +0000155} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100156} // namespace nfd
157
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700158#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP