blob: c7d10fd9786c0b1808000f3a532eb185c5d82a76 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * 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/>.
Junxiao Shi5dd26c32014-07-20 23:15:14 -070024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP
27#define NFD_DAEMON_FACE_UDP_CHANNEL_HPP
Giulio Grassi624f6c62014-02-18 19:42:14 +010028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Junxiao Shi64d99f22017-01-21 23:06:36 +000030#include "udp-protocol.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010031
Davide Pesaventoacca18a2017-04-09 13:23:16 -040032#include <array>
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050033#include <map>
Davide Pesaventoacca18a2017-04-09 13:23:16 -040034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010036
Giulio Grassi624f6c62014-02-18 19:42:14 +010037/**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040038 * \brief Class implementing a UDP-based channel to create faces.
Giulio Grassi624f6c62014-02-18 19:42:14 +010039 */
Davide Pesavento3db98072021-03-09 23:03:27 -050040class UdpChannel final : public Channel
Giulio Grassi624f6c62014-02-18 19:42:14 +010041{
42public:
Giulio Grassi624f6c62014-02-18 19:42:14 +010043 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040044 * \brief Create a UDP channel on the given \p localEndpoint.
Giulio Grassi624f6c62014-02-18 19:42:14 +010045 *
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040046 * To enable the creation of faces upon incoming connections, one needs to
47 * explicitly call listen(). The created socket is bound to \p localEndpoint.
Giulio Grassi624f6c62014-02-18 19:42:14 +010048 */
49 UdpChannel(const udp::Endpoint& localEndpoint,
Eric Newberry0c841642018-01-17 15:01:00 -070050 time::nanoseconds idleTimeout,
Junxiao Shia6286a92021-02-23 06:43:52 -070051 bool wantCongestionMarking,
52 size_t defaultMtu);
Giulio Grassi624f6c62014-02-18 19:42:14 +010053
Davide Pesaventoc19408d2017-04-08 00:42:55 -040054 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050055 isListening() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040056 {
57 return m_socket.is_open();
58 }
59
Junxiao Shic5401492015-12-23 16:33:21 -070060 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050061 size() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040062 {
63 return m_channelFaces.size();
64 }
Junxiao Shic5401492015-12-23 16:33:21 -070065
66 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040067 * \brief Create a unicast UDP face toward \p remoteEndpoint.
Junxiao Shic5401492015-12-23 16:33:21 -070068 */
69 void
70 connect(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050071 const FaceParams& params,
Junxiao Shic5401492015-12-23 16:33:21 -070072 const FaceCreatedCallback& onFaceCreated,
73 const FaceCreationFailedCallback& onConnectFailed);
74
75 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040076 * \brief Start listening.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040077 *
78 * Enable listening on the local endpoint, waiting for incoming datagrams,
79 * and creating a face when a datagram is received from a new remote host.
80 *
81 * Faces created in this way will have on-demand persistency.
Davide Pesavento77911cc2017-04-08 22:12:30 -040082 *
83 * \param onFaceCreated Callback to notify successful creation of a face
84 * \param onFaceCreationFailed Callback to notify errors
Giulio Grassi624f6c62014-02-18 19:42:14 +010085 */
86 void
87 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040088 const FaceCreationFailedCallback& onFaceCreationFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010089
Giulio Grassi624f6c62014-02-18 19:42:14 +010090private:
Junxiao Shic5401492015-12-23 16:33:21 -070091 void
92 waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
93 const FaceCreationFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010094
Giulio Grassi624f6c62014-02-18 19:42:14 +010095 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +010096 handleNewPeer(const boost::system::error_code& error,
97 size_t nBytesReceived,
98 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010099 const FaceCreationFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100
Junxiao Shicde37ad2015-12-24 01:02:05 -0700101 std::pair<bool, shared_ptr<Face>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400102 createFace(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500103 const FaceParams& params);
Junxiao Shic5401492015-12-23 16:33:21 -0700104
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400106 const udp::Endpoint m_localEndpoint;
107 udp::Endpoint m_remoteEndpoint; ///< The latest peer that started communicating with us
108 boost::asio::ip::udp::socket m_socket; ///< Socket used to "accept" new peers
Davide Pesaventoacca18a2017-04-09 13:23:16 -0400109 std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400110 std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
111 const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400112 const bool m_wantCongestionMarking;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100113};
114
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400115} // namespace nfd::face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100116
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700117#endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP