blob: cfbd077610158a5bf62f9cc004c87d04d1c4fca1 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Chengyu Fan4381fb62015-01-14 11:37:04 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UDP_FACTORY_HPP
27#define NFD_DAEMON_FACE_UDP_FACTORY_HPP
Giulio Grassi624f6c62014-02-18 19:42:14 +010028
29#include "protocol-factory.hpp"
30#include "udp-channel.hpp"
31#include "multicast-udp-face.hpp"
32
33
34namespace nfd {
35
36// @todo The multicast support for ipv6 must be implemented
37
38class UdpFactory : public ProtocolFactory
39{
40public:
41 /**
42 * \brief Exception of UdpFactory
43 */
Alexander Afanasyev5959b012014-06-02 19:18:12 +030044 class Error : public ProtocolFactory::Error
Giulio Grassi624f6c62014-02-18 19:42:14 +010045 {
Alexander Afanasyev5959b012014-06-02 19:18:12 +030046 public:
47 explicit
48 Error(const std::string& what)
49 : ProtocolFactory::Error(what)
50 {
51 }
Giulio Grassi624f6c62014-02-18 19:42:14 +010052 };
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060053
Alexander Afanasyev5959b012014-06-02 19:18:12 +030054 typedef std::map< udp::Endpoint, shared_ptr<MulticastUdpFace> > MulticastFaceMap;
55
Giulio Grassi624f6c62014-02-18 19:42:14 +010056 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 Afanasyev5959b012014-06-02 19:18:12 +030085 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010086
87 /**
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080088 * \brief Create UDP-based channel using specified IP address and port number
Giulio Grassi624f6c62014-02-18 19:42:14 +010089 *
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080090 * This method is just a helper that converts a string representation of localIp and port to
91 * udp::Endpoint and calls the other createChannel overload.
Giulio Grassi624f6c62014-02-18 19:42:14 +010092 *
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060093 * If localHost is a IPv6 address of a specific device, it must be in the form:
Giulio Grassi624f6c62014-02-18 19:42:14 +010094 * ip address%interface name
95 * Example: fe80::5e96:9dff:fe7d:9c8d%en1
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060096 * Otherwise, you can use ::
Giulio Grassi624f6c62014-02-18 19:42:14 +010097 *
Giulio Grassi624f6c62014-02-18 19:42:14 +010098 * \throws UdpChannel::Error if the bind on the socket fails
99 * \throws UdpFactory::Error
100 */
101 shared_ptr<UdpChannel>
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800102 createChannel(const std::string& localIp,
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300103 const std::string& localPort,
104 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105
106 /**
107 * \brief Create MulticastUdpFace using udp::Endpoint
108 *
109 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
110 *
111 * The face will join the multicast group
112 *
113 * If this method called twice with the same endpoint and group, only one face
114 * will be created. The second call will just retrieve the existing
115 * channel.
116 *
117 * If an unicast face is already active on the same local NIC and port, the
118 * creation fails and an exception is thrown
119 *
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200120 * \param networkInterfaceName name of the network interface on which the face will be bound
121 * (Used only on multihomed linux machine with more than one MulticastUdpFace for
122 * the same multicast group. If specified, will requires CAP_NET_RAW capability)
123 * An empty string can be provided in other system or in linux machine with only one
124 * MulticastUdpFace per multicast group
125 *
126 *
Giulio Grassi624f6c62014-02-18 19:42:14 +0100127 * \returns always a valid pointer to a MulticastUdpFace object, an exception
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600128 * is thrown if it cannot be created.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100129 *
130 * \throws UdpFactory::Error
131 *
132 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
133 * for details on ways to create udp::Endpoint
134 */
135 shared_ptr<MulticastUdpFace>
136 createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200137 const udp::Endpoint& multicastEndpoint,
138 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600139
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140 shared_ptr<MulticastUdpFace>
141 createMulticastFace(const std::string& localIp,
142 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200143 const std::string& multicastPort,
144 const std::string& networkInterfaceName = "");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600145
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146 // from Factory
147 virtual void
148 createFace(const FaceUri& uri,
149 const FaceCreatedCallback& onCreated,
150 const FaceConnectFailedCallback& onConnectFailed);
151
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600152 virtual std::list<shared_ptr<const Channel> >
153 getChannels() const;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100154
155 /**
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300156 * \brief Get map of configured multicast faces
Giulio Grassi624f6c62014-02-18 19:42:14 +0100157 */
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300158 const MulticastFaceMap&
159 getMulticastFaces() const;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100160
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800161PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600162
163 void
164 prohibitEndpoint(const udp::Endpoint& endpoint);
165
166 void
167 prohibitAllIpv4Endpoints(const uint16_t port);
168
169 void
170 prohibitAllIpv6Endpoints(const uint16_t port);
171
Giulio Grassi624f6c62014-02-18 19:42:14 +0100172 void
173 afterFaceFailed(udp::Endpoint& endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600174
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175 /**
176 * \brief Look up UdpChannel using specified local endpoint
177 *
178 * \returns shared pointer to the existing UdpChannel object
179 * or empty shared pointer when such channel does not exist
180 *
181 * \throws never
182 */
183 shared_ptr<UdpChannel>
184 findChannel(const udp::Endpoint& localEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600185
186
Giulio Grassi624f6c62014-02-18 19:42:14 +0100187 /**
188 * \brief Look up multicast UdpFace using specified local endpoint
189 *
190 * \returns shared pointer to the existing multicast MulticastUdpFace object
191 * or empty shared pointer when such face does not exist
192 *
193 * \throws never
194 */
195 shared_ptr<MulticastUdpFace>
196 findMulticastFace(const udp::Endpoint& localEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600197
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800198PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Giulio Grassi624f6c62014-02-18 19:42:14 +0100199 typedef std::map< udp::Endpoint, shared_ptr<UdpChannel> > ChannelMap;
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300200
Giulio Grassi624f6c62014-02-18 19:42:14 +0100201 ChannelMap m_channels;
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300202 MulticastFaceMap m_multicastFaces;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600203
Giulio Grassi624f6c62014-02-18 19:42:14 +0100204 std::string m_defaultPort;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600205 std::set<udp::Endpoint> m_prohibitedEndpoints;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100206};
207
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300208
209inline const UdpFactory::MulticastFaceMap&
210UdpFactory::getMulticastFaces() const
211{
212 return m_multicastFaces;
213}
214
215
Giulio Grassi624f6c62014-02-18 19:42:14 +0100216} // namespace nfd
217
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700218#endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP