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 | */ |
| 43 | struct Error : public ProtocolFactory::Error |
| 44 | { |
| 45 | Error(const std::string& what) : ProtocolFactory::Error(what) {} |
| 46 | }; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 47 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 48 | explicit |
| 49 | UdpFactory(const std::string& defaultPort = "6363"); |
| 50 | |
| 51 | /** |
| 52 | * \brief Create UDP-based channel using udp::Endpoint |
| 53 | * |
| 54 | * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint. |
| 55 | * |
| 56 | * If this method called twice with the same endpoint, only one channel |
| 57 | * will be created. The second call will just retrieve the existing |
| 58 | * channel. |
| 59 | * |
| 60 | * If a multicast face is already active on the same local endpoint, |
| 61 | * the creation fails and an exception is thrown |
| 62 | * |
| 63 | * Once a face is created, if it doesn't send/receive anything for |
| 64 | * a period of time equal to timeout, it will be destroyed |
| 65 | * @todo this funcionality has to be implemented |
| 66 | * |
| 67 | * \returns always a valid pointer to a UdpChannel object, an exception |
| 68 | * is thrown if it cannot be created. |
| 69 | * |
| 70 | * \throws UdpFactory::Error |
| 71 | * |
| 72 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 73 | * for details on ways to create udp::Endpoint |
| 74 | */ |
| 75 | shared_ptr<UdpChannel> |
| 76 | createChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 77 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * \brief Create UDP-based channel using specified host and port number |
| 81 | * |
| 82 | * This method will attempt to resolve the provided host and port numbers |
| 83 | * and will throw UdpFactory::Error when channel cannot be created. |
| 84 | * |
| 85 | * Note that this call will **BLOCK** until resolution is done or failed. |
| 86 | * |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 87 | * 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] | 88 | * ip address%interface name |
| 89 | * Example: fe80::5e96:9dff:fe7d:9c8d%en1 |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 90 | * Otherwise, you can use :: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 91 | * |
| 92 | * Once a face is created, if it doesn't send/receive anything for |
| 93 | * a period of time equal to timeout, it will be destroyed |
| 94 | * @todo this funcionality has to be implemented |
| 95 | * |
| 96 | * \throws UdpChannel::Error if the bind on the socket fails |
| 97 | * \throws UdpFactory::Error |
| 98 | */ |
| 99 | shared_ptr<UdpChannel> |
| 100 | createChannel(const std::string& localHost, |
| 101 | const std::string& localPort, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 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 | * |
| 118 | * \returns always a valid pointer to a MulticastUdpFace object, an exception |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 119 | * is thrown if it cannot be created. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 120 | * |
| 121 | * \throws UdpFactory::Error |
| 122 | * |
| 123 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html |
| 124 | * for details on ways to create udp::Endpoint |
| 125 | */ |
| 126 | shared_ptr<MulticastUdpFace> |
| 127 | createMulticastFace(const udp::Endpoint& localEndpoint, |
| 128 | const udp::Endpoint& multicastEndpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 129 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 130 | shared_ptr<MulticastUdpFace> |
| 131 | createMulticastFace(const std::string& localIp, |
| 132 | const std::string& multicastIp, |
| 133 | const std::string& multicastPort); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 134 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 135 | // from Factory |
| 136 | virtual void |
| 137 | createFace(const FaceUri& uri, |
| 138 | const FaceCreatedCallback& onCreated, |
| 139 | const FaceConnectFailedCallback& onConnectFailed); |
| 140 | |
| 141 | protected: |
| 142 | typedef std::map< udp::Endpoint, shared_ptr<MulticastUdpFace> > MulticastFaceMap; |
| 143 | |
| 144 | /** |
| 145 | * \brief Keeps tracking of the MulticastUdpFace created |
| 146 | */ |
| 147 | MulticastFaceMap m_multicastFaces; |
| 148 | |
| 149 | private: |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 150 | |
| 151 | void |
| 152 | prohibitEndpoint(const udp::Endpoint& endpoint); |
| 153 | |
| 154 | void |
| 155 | prohibitAllIpv4Endpoints(const uint16_t port); |
| 156 | |
| 157 | void |
| 158 | prohibitAllIpv6Endpoints(const uint16_t port); |
| 159 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 160 | void |
| 161 | afterFaceFailed(udp::Endpoint& endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 162 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 163 | /** |
| 164 | * \brief Look up UdpChannel using specified local endpoint |
| 165 | * |
| 166 | * \returns shared pointer to the existing UdpChannel object |
| 167 | * or empty shared pointer when such channel does not exist |
| 168 | * |
| 169 | * \throws never |
| 170 | */ |
| 171 | shared_ptr<UdpChannel> |
| 172 | findChannel(const udp::Endpoint& localEndpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 173 | |
| 174 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 175 | /** |
| 176 | * \brief Look up multicast UdpFace using specified local endpoint |
| 177 | * |
| 178 | * \returns shared pointer to the existing multicast MulticastUdpFace object |
| 179 | * or empty shared pointer when such face does not exist |
| 180 | * |
| 181 | * \throws never |
| 182 | */ |
| 183 | shared_ptr<MulticastUdpFace> |
| 184 | findMulticastFace(const udp::Endpoint& localEndpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 185 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 186 | void |
| 187 | continueCreateFaceAfterResolve(const udp::Endpoint& endpoint, |
| 188 | const FaceCreatedCallback& onCreated, |
| 189 | const FaceConnectFailedCallback& onConnectFailed); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 190 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 191 | typedef std::map< udp::Endpoint, shared_ptr<UdpChannel> > ChannelMap; |
| 192 | ChannelMap m_channels; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 193 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 194 | std::string m_defaultPort; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 195 | |
| 196 | std::set<udp::Endpoint> m_prohibitedEndpoints; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace nfd |
| 200 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 201 | #endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP |