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