Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 { |
| 33 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 34 | /// @todo IPv6 multicast support not implemented |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | |
| 36 | class UdpFactory : public ProtocolFactory |
| 37 | { |
| 38 | public: |
| 39 | /** |
| 40 | * \brief Exception of UdpFactory |
| 41 | */ |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 42 | class Error : public ProtocolFactory::Error |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 44 | public: |
| 45 | explicit |
| 46 | Error(const std::string& what) |
| 47 | : ProtocolFactory::Error(what) |
| 48 | { |
| 49 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 50 | }; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 51 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 52 | typedef std::map<udp::Endpoint, shared_ptr<face::LpFaceWrapper>> MulticastFaceMap; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 53 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 54 | explicit |
| 55 | UdpFactory(const std::string& defaultPort = "6363"); |
| 56 | |
| 57 | /** |
| 58 | * \brief Create UDP-based channel using udp::Endpoint |
| 59 | * |
| 60 | * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint. |
| 61 | * |
| 62 | * If this method called twice with the same endpoint, only one channel |
| 63 | * will be created. The second call will just retrieve the existing |
| 64 | * channel. |
| 65 | * |
| 66 | * If a multicast face is already active on the same local endpoint, |
| 67 | * the creation fails and an exception is thrown |
| 68 | * |
| 69 | * Once a face is created, if it doesn't send/receive anything for |
| 70 | * a period of time equal to timeout, it will be destroyed |
| 71 | * @todo this funcionality has to be implemented |
| 72 | * |
| 73 | * \returns always a valid pointer to a UdpChannel object, an exception |
| 74 | * is thrown if it cannot be created. |
| 75 | * |
| 76 | * \throws UdpFactory::Error |
| 77 | * |
| 78 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 79 | * for details on ways to create udp::Endpoint |
| 80 | */ |
| 81 | shared_ptr<UdpChannel> |
| 82 | createChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 83 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 84 | |
| 85 | /** |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 86 | * \brief Create UDP-based channel using specified IP address and port number |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 87 | * |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 88 | * This method is just a helper that converts a string representation of localIp and port to |
| 89 | * udp::Endpoint and calls the other createChannel overload. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 90 | * |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 91 | * 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] | 92 | * ip address%interface name |
| 93 | * Example: fe80::5e96:9dff:fe7d:9c8d%en1 |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 94 | * Otherwise, you can use :: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 95 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 96 | * \throws UdpChannel::Error if the bind on the socket fails |
| 97 | * \throws UdpFactory::Error |
| 98 | */ |
| 99 | shared_ptr<UdpChannel> |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 100 | createChannel(const std::string& localIp, |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 101 | const std::string& localPort, |
| 102 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * \brief Create MulticastUdpFace using udp::Endpoint |
| 106 | * |
| 107 | * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint. |
| 108 | * |
| 109 | * The face will join the multicast group |
| 110 | * |
| 111 | * If this method called twice with the same endpoint and group, only one face |
| 112 | * will be created. The second call will just retrieve the existing |
| 113 | * channel. |
| 114 | * |
| 115 | * If an unicast face is already active on the same local NIC and port, the |
| 116 | * creation fails and an exception is thrown |
| 117 | * |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 118 | * \param networkInterfaceName name of the network interface on which the face will be bound |
| 119 | * (Used only on multihomed linux machine with more than one MulticastUdpFace for |
| 120 | * the same multicast group. If specified, will requires CAP_NET_RAW capability) |
| 121 | * An empty string can be provided in other system or in linux machine with only one |
| 122 | * MulticastUdpFace per multicast group |
| 123 | * |
| 124 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 125 | * \returns always a valid pointer to a MulticastUdpFace object, an exception |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 126 | * is thrown if it cannot be created. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 127 | * |
| 128 | * \throws UdpFactory::Error |
| 129 | * |
| 130 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 131 | * for details on ways to create udp::Endpoint |
| 132 | */ |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 133 | shared_ptr<face::LpFaceWrapper> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 134 | createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 135 | const udp::Endpoint& multicastEndpoint, |
| 136 | const std::string& networkInterfaceName = ""); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 137 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 138 | shared_ptr<face::LpFaceWrapper> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 139 | createMulticastFace(const std::string& localIp, |
| 140 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 141 | const std::string& multicastPort, |
| 142 | const std::string& networkInterfaceName = ""); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 143 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 144 | // from ProtocolFactory |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 145 | virtual void |
| 146 | createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 147 | ndn::nfd::FacePersistency persistency, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 148 | const FaceCreatedCallback& onCreated, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 149 | const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 150 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 151 | virtual std::list<shared_ptr<const Channel>> |
Alexander Afanasyev | 435d705 | 2015-09-17 15:10:25 -0700 | [diff] [blame] | 152 | getChannels() const DECL_OVERRIDE; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 153 | |
| 154 | /** |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 155 | * \brief Get map of configured multicast faces |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 156 | */ |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 157 | const MulticastFaceMap& |
| 158 | getMulticastFaces() const; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 159 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 160 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 161 | void |
| 162 | prohibitEndpoint(const udp::Endpoint& endpoint); |
| 163 | |
| 164 | void |
| 165 | prohibitAllIpv4Endpoints(const uint16_t port); |
| 166 | |
| 167 | void |
| 168 | prohibitAllIpv6Endpoints(const uint16_t port); |
| 169 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 170 | void |
| 171 | afterFaceFailed(udp::Endpoint& endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 172 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 173 | /** |
| 174 | * \brief Look up UdpChannel using specified local endpoint |
| 175 | * |
| 176 | * \returns shared pointer to the existing UdpChannel object |
| 177 | * or empty shared pointer when such channel does not exist |
| 178 | * |
| 179 | * \throws never |
| 180 | */ |
| 181 | shared_ptr<UdpChannel> |
| 182 | findChannel(const udp::Endpoint& localEndpoint); |
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 | * |
| 187 | * \returns shared pointer to the existing multicast MulticastUdpFace 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 | */ |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 190 | shared_ptr<face::LpFaceWrapper> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 191 | findMulticastFace(const udp::Endpoint& localEndpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 192 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 193 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 194 | typedef std::map<udp::Endpoint, shared_ptr<UdpChannel>> ChannelMap; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 195 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 196 | ChannelMap m_channels; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 197 | MulticastFaceMap m_multicastFaces; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 198 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 199 | std::string m_defaultPort; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 200 | std::set<udp::Endpoint> m_prohibitedEndpoints; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 203 | inline const UdpFactory::MulticastFaceMap& |
| 204 | UdpFactory::getMulticastFaces() const |
| 205 | { |
| 206 | return m_multicastFaces; |
| 207 | } |
| 208 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 209 | } // namespace nfd |
| 210 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 211 | #endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP |