Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [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_FACTORY_HPP |
| 27 | #define NFD_DAEMON_FACE_UDP_FACTORY_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
| 29 | #include "protocol-factory.hpp" |
| 30 | #include "udp-channel.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 33 | namespace face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 34 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 35 | /** \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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | class UdpFactory : public ProtocolFactory |
| 41 | { |
| 42 | public: |
| 43 | /** |
| 44 | * \brief Exception of UdpFactory |
| 45 | */ |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 46 | class Error : public ProtocolFactory::Error |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 47 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 48 | public: |
| 49 | explicit |
| 50 | Error(const std::string& what) |
| 51 | : ProtocolFactory::Error(what) |
| 52 | { |
| 53 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 54 | }; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 55 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 56 | static const std::string& |
| 57 | getId(); |
| 58 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 59 | /** \brief process face_system.udp config section |
| 60 | */ |
| 61 | void |
| 62 | processConfig(OptionalConfigSection configSection, |
| 63 | FaceSystem::ConfigContext& context) override; |
| 64 | |
| 65 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 66 | createFace(const FaceUri& remoteUri, |
| 67 | const ndn::optional<FaceUri>& localUri, |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 68 | ndn::nfd::FacePersistency persistency, |
| 69 | bool wantLocalFieldsEnabled, |
| 70 | const FaceCreatedCallback& onCreated, |
| 71 | const FaceCreationFailedCallback& onFailure) override; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 72 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 73 | /** |
| 74 | * \brief Create UDP-based channel using udp::Endpoint |
| 75 | * |
| 76 | * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint. |
| 77 | * |
| 78 | * If this method called twice with the same endpoint, only one channel |
| 79 | * will be created. The second call will just retrieve the existing |
| 80 | * channel. |
| 81 | * |
| 82 | * If a multicast face is already active on the same local endpoint, |
| 83 | * the creation fails and an exception is thrown |
| 84 | * |
| 85 | * Once a face is created, if it doesn't send/receive anything for |
| 86 | * a period of time equal to timeout, it will be destroyed |
| 87 | * @todo this funcionality has to be implemented |
| 88 | * |
| 89 | * \returns always a valid pointer to a UdpChannel object, an exception |
| 90 | * is thrown if it cannot be created. |
| 91 | * |
| 92 | * \throws UdpFactory::Error |
| 93 | * |
| 94 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 95 | * for details on ways to create udp::Endpoint |
| 96 | */ |
| 97 | shared_ptr<UdpChannel> |
| 98 | createChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 99 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 100 | |
| 101 | /** |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 102 | * \brief Create UDP-based channel using specified IP address and port number |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 103 | * |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 104 | * This method is just a helper that converts a string representation of localIp and port to |
| 105 | * udp::Endpoint and calls the other createChannel overload. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | * |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 107 | * If localHost is a IPv6 address of a specific device, it must be in the form: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 108 | * ip address%interface name |
| 109 | * Example: fe80::5e96:9dff:fe7d:9c8d%en1 |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 110 | * Otherwise, you can use :: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 111 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 112 | * \throws UdpChannel::Error if the bind on the socket fails |
| 113 | * \throws UdpFactory::Error |
| 114 | */ |
| 115 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 116 | createChannel(const std::string& localIp, const std::string& localPort, |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 117 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 118 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 119 | std::vector<shared_ptr<const Channel>> |
| 120 | getChannels() const override; |
| 121 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 122 | /** |
| 123 | * \brief Create MulticastUdpFace using udp::Endpoint |
| 124 | * |
| 125 | * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint. |
| 126 | * |
| 127 | * The face will join the multicast group |
| 128 | * |
| 129 | * If this method called twice with the same endpoint and group, only one face |
| 130 | * will be created. The second call will just retrieve the existing |
| 131 | * channel. |
| 132 | * |
| 133 | * If an unicast face is already active on the same local NIC and port, the |
| 134 | * creation fails and an exception is thrown |
| 135 | * |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 136 | * \param localEndpoint local endpoint |
| 137 | * \param multicastEndpoint multicast endpoint |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 138 | * \param networkInterfaceName name of the network interface on which the face will be bound |
| 139 | * (Used only on multihomed linux machine with more than one MulticastUdpFace for |
| 140 | * the same multicast group. If specified, will requires CAP_NET_RAW capability) |
| 141 | * An empty string can be provided in other system or in linux machine with only one |
| 142 | * MulticastUdpFace per multicast group |
| 143 | * |
| 144 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 145 | * \returns always a valid pointer to a MulticastUdpFace object, an exception |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 146 | * is thrown if it cannot be created. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 147 | * |
| 148 | * \throws UdpFactory::Error |
| 149 | * |
| 150 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 151 | * for details on ways to create udp::Endpoint |
| 152 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 153 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 154 | createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 155 | const udp::Endpoint& multicastEndpoint, |
| 156 | const std::string& networkInterfaceName = ""); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 157 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 158 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 159 | createMulticastFace(const std::string& localIp, |
| 160 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 161 | const std::string& multicastPort, |
| 162 | const std::string& networkInterfaceName = ""); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 163 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 164 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 165 | void |
| 166 | prohibitEndpoint(const udp::Endpoint& endpoint); |
| 167 | |
| 168 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 169 | prohibitAllIpv4Endpoints(uint16_t port); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 170 | |
| 171 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 172 | prohibitAllIpv6Endpoints(uint16_t port); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 173 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 174 | private: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 175 | /** |
| 176 | * \brief Look up UdpChannel using specified local endpoint |
| 177 | * |
| 178 | * \returns shared pointer to the existing UdpChannel object |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 179 | * or nullptr when such channel does not exist |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 180 | */ |
| 181 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 182 | findChannel(const udp::Endpoint& localEndpoint) const; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 183 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 184 | /** |
| 185 | * \brief Look up multicast UdpFace using specified local endpoint |
| 186 | * |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 187 | * \returns shared pointer to the existing multicast UdpFace object |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 188 | * or nullptr when such face does not exist |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 189 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 190 | shared_ptr<Face> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 191 | findMulticastFace(const udp::Endpoint& localEndpoint) const; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 192 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 193 | void |
| 194 | applyMulticastConfig(const FaceSystem::ConfigContext& context); |
| 195 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 196 | private: |
| 197 | std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 198 | |
| 199 | struct MulticastConfig |
| 200 | { |
| 201 | bool isEnabled = false; |
| 202 | udp::Endpoint group = udp::getDefaultMulticastGroup(); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 203 | ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 204 | NetworkInterfacePredicate netifPredicate; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | MulticastConfig m_mcastConfig; |
| 208 | std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 209 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 210 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 211 | std::set<udp::Endpoint> m_prohibitedEndpoints; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 212 | }; |
| 213 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 214 | } // namespace face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 215 | } // namespace nfd |
| 216 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 217 | #endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP |