Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_UDP_CHANNEL_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | #include "channel.hpp" |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 30 | #include "udp-protocol.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | acca18a | 2017-04-09 13:23:16 -0400 | [diff] [blame] | 32 | #include <array> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 36 | /** |
| 37 | * \brief Class implementing UDP-based channel to create faces |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 38 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 39 | class UdpChannel final : public Channel |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | { |
| 41 | public: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | /** |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 43 | * \brief Create a UDP channel on the given \p localEndpoint |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 44 | * |
| 45 | * To enable creation of faces upon incoming connections, |
| 46 | * one needs to explicitly call UdpChannel::listen method. |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 47 | * The created socket is bound to \p localEndpoint. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 48 | */ |
| 49 | UdpChannel(const udp::Endpoint& localEndpoint, |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 50 | time::nanoseconds idleTimeout, |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 51 | bool wantCongestionMarking, |
| 52 | size_t defaultMtu); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 53 | |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 54 | bool |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 55 | isListening() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 56 | { |
| 57 | return m_socket.is_open(); |
| 58 | } |
| 59 | |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 60 | size_t |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 61 | size() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 62 | { |
| 63 | return m_channelFaces.size(); |
| 64 | } |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 67 | * \brief Create a unicast UDP face toward \p remoteEndpoint |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 68 | */ |
| 69 | void |
| 70 | connect(const udp::Endpoint& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 71 | const FaceParams& params, |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 72 | const FaceCreatedCallback& onFaceCreated, |
| 73 | const FaceCreationFailedCallback& onConnectFailed); |
| 74 | |
| 75 | /** |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 76 | * \brief Start listening |
| 77 | * |
| 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 Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 82 | * |
| 83 | * \param onFaceCreated Callback to notify successful creation of a face |
| 84 | * \param onFaceCreationFailed Callback to notify errors |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 85 | */ |
| 86 | void |
| 87 | listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 88 | const FaceCreationFailedCallback& onFaceCreationFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 89 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 90 | private: |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 91 | void |
| 92 | waitForNewPeer(const FaceCreatedCallback& onFaceCreated, |
| 93 | const FaceCreationFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 94 | |
| 95 | /** |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 96 | * \brief The channel has received a packet from a remote |
| 97 | * endpoint not associated with any UDP face yet |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 98 | */ |
| 99 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 100 | handleNewPeer(const boost::system::error_code& error, |
| 101 | size_t nBytesReceived, |
| 102 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 103 | const FaceCreationFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 104 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 105 | std::pair<bool, shared_ptr<Face>> |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 106 | createFace(const udp::Endpoint& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 107 | const FaceParams& params); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 108 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 109 | private: |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 110 | const udp::Endpoint m_localEndpoint; |
| 111 | udp::Endpoint m_remoteEndpoint; ///< The latest peer that started communicating with us |
| 112 | boost::asio::ip::udp::socket m_socket; ///< Socket used to "accept" new peers |
Davide Pesavento | acca18a | 2017-04-09 13:23:16 -0400 | [diff] [blame] | 113 | std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 114 | std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces; |
| 115 | const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 116 | bool m_wantCongestionMarking; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 119 | } // namespace nfd::face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 120 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 121 | #endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP |