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