blob: a57cb25eb6d0a7e5c77797fe61722e2aaac9a769 [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/*
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05003 * Copyright (c) 2014-2018, 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
Davide Pesaventod27841b2018-11-13 00:22:24 -050035/** \brief Protocol factory for UDP over IPv4 and IPv6
Junxiao Shi64d99f22017-01-21 23:06:36 +000036 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010037class UdpFactory : public ProtocolFactory
38{
39public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030040 class Error : public ProtocolFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010041 {
Alexander Afanasyev5959b012014-06-02 19:18:12 +030042 public:
Davide Pesaventod27841b2018-11-13 00:22:24 -050043 using ProtocolFactory::Error::Error;
Giulio Grassi624f6c62014-02-18 19:42:14 +010044 };
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060045
Junxiao Shib47247d2017-01-24 15:09:16 +000046 static const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050047 getId() noexcept;
Junxiao Shib47247d2017-01-24 15:09:16 +000048
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049 explicit
50 UdpFactory(const CtorParams& params);
51
Giulio Grassi624f6c62014-02-18 19:42:14 +010052 /**
53 * \brief Create UDP-based channel using udp::Endpoint
54 *
55 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
56 *
Davide Pesavento46afec42017-05-28 14:28:47 -040057 * If this method is called twice with the same endpoint, only one channel
58 * will be created. The second call will just return the existing channel.
Giulio Grassi624f6c62014-02-18 19:42:14 +010059 *
60 * If a multicast face is already active on the same local endpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040061 * the creation fails and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +010062 *
Davide Pesavento46afec42017-05-28 14:28:47 -040063 * \return always a valid pointer to a UdpChannel object, an exception
64 * is thrown if it cannot be created.
65 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010066 */
67 shared_ptr<UdpChannel>
68 createChannel(const udp::Endpoint& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -040069 time::nanoseconds idleTimeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010070
Giulio Grassi624f6c62014-02-18 19:42:14 +010071 /**
Davide Pesaventobb734df2017-10-24 18:05:36 -040072 * \brief Create a multicast UDP face
Giulio Grassi624f6c62014-02-18 19:42:14 +010073 *
74 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
75 *
Davide Pesavento46afec42017-05-28 14:28:47 -040076 * The face will join the specified multicast group.
Giulio Grassi624f6c62014-02-18 19:42:14 +010077 *
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050078 * If this method is called twice with the same set of arguments, only one
Davide Pesavento46afec42017-05-28 14:28:47 -040079 * face will be created. The second call will just return the existing face.
Giulio Grassi624f6c62014-02-18 19:42:14 +010080 *
Davide Pesaventobb734df2017-10-24 18:05:36 -040081 * If a UDP channel, unicast face, or multicast face already exists on the
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050082 * same combination of local address and multicast port, the creation fails
83 * and an exception is thrown.
Giulio Grassi624f6c62014-02-18 19:42:14 +010084 *
Davide Pesaventobb734df2017-10-24 18:05:36 -040085 * \param netif the network interface to which the face will be bound
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050086 * \param localAddress the local IP address to which the face will be bound
87 * \param multicastEndpoint the multicast endpoint (multicast group and port number)
Giulio Grassi6d7176d2014-04-16 16:08:48 +020088 *
Davide Pesaventobb734df2017-10-24 18:05:36 -040089 * \return always a valid shared pointer to the created face;
90 * an exception is thrown if the face cannot be created.
Davide Pesavento46afec42017-05-28 14:28:47 -040091 * \throw UdpFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010092 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070093 shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050094 createMulticastFace(const shared_ptr<const ndn::net::NetworkInterface>& netif,
95 const boost::asio::ip::address& localAddress,
96 const udp::Endpoint& multicastEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060097
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020098private:
Davide Pesaventod27841b2018-11-13 00:22:24 -050099 /** \brief process face_system.udp config section
100 */
101 void
102 doProcessConfig(OptionalConfigSection configSection,
103 FaceSystem::ConfigContext& context) override;
104
105 void
106 doCreateFace(const CreateFaceRequest& req,
107 const FaceCreatedCallback& onCreated,
108 const FaceCreationFailedCallback& onFailure) override;
109
110 std::vector<shared_ptr<const Channel>>
111 doGetChannels() const override;
112
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500113 /** \brief Create UDP multicast faces on \p netif if needed by \p m_mcastConfig
114 * \return list of faces (just created or already existing) on \p netif
Junxiao Shibbace1d2017-08-06 20:03:37 +0000115 */
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500116 std::vector<shared_ptr<Face>>
Junxiao Shibbace1d2017-08-06 20:03:37 +0000117 applyMcastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif);
118
Davide Pesaventobb734df2017-10-24 18:05:36 -0400119 /** \brief Create and destroy UDP multicast faces according to \p m_mcastConfig
Junxiao Shibbace1d2017-08-06 20:03:37 +0000120 */
Junxiao Shi64d99f22017-01-21 23:06:36 +0000121 void
Junxiao Shibbace1d2017-08-06 20:03:37 +0000122 applyMcastConfig(const FaceSystem::ConfigContext& context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000123
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200124private:
Davide Pesaventod5e89e82018-02-07 21:40:21 -0500125 bool m_wantCongestionMarking = false;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200126 std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000127
128 struct MulticastConfig
129 {
130 bool isEnabled = false;
131 udp::Endpoint group = udp::getDefaultMulticastGroup();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500132 udp::Endpoint groupV6 = udp::getDefaultMulticastGroupV6();
Teng Liangfe4fce32017-03-29 04:49:38 +0000133 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS;
Junxiao Shic31080d2017-01-24 15:10:12 +0000134 NetworkInterfacePredicate netifPredicate;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000135 };
Junxiao Shi64d99f22017-01-21 23:06:36 +0000136 MulticastConfig m_mcastConfig;
137 std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000138
139 signal::ScopedConnection m_netifAddConn;
140 struct NetifConns
141 {
142 signal::ScopedConnection addrAddConn;
143 };
144 std::map<int, NetifConns> m_netifConns; // ifindex => signal connections
Giulio Grassi624f6c62014-02-18 19:42:14 +0100145};
146
Junxiao Shi64d99f22017-01-21 23:06:36 +0000147} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100148} // namespace nfd
149
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700150#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP