blob: f7263888ccdc7265a18ac87a6b022ddab98c9c54 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoe1081cd2016-12-19 23:58:15 -05003 * Copyright (c) 2014-2016, 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.
Junxiao Shi77dcadd2014-10-05 14:40:54 -070010 *
11 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12 *
13 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later version.
16 *
17 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 *
21 * You should have received copies of the GNU General Public License and GNU Lesser
22 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23 * <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26 */
27
28#ifndef NDN_UTIL_FACE_URI_HPP
29#define NDN_UTIL_FACE_URI_HPP
30
Vince Lehman8991bb52014-10-15 16:16:01 -050031#include "../common.hpp"
Junxiao Shi4083c8d2014-10-12 16:43:16 -070032#include "time.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070033
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050034#include <boost/asio/ip/tcp.hpp>
35#include <boost/asio/ip/udp.hpp>
36#include <boost/asio/local/stream_protocol.hpp>
37
Junxiao Shi77dcadd2014-10-05 14:40:54 -070038namespace ndn {
39namespace util {
40
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050041namespace ethernet {
42class Address;
43} // namespace ethernet
44
Junxiao Shi77dcadd2014-10-05 14:40:54 -070045/** \brief represents the underlying protocol and address used by a Face
46 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
47 */
48class FaceUri
49{
50public:
51 class Error : public std::invalid_argument
52 {
53 public:
54 explicit
55 Error(const std::string& what)
56 : std::invalid_argument(what)
57 {
58 }
59 };
60
61 FaceUri();
62
63 /** \brief construct by parsing
64 *
65 * \param uri scheme://host[:port]/path
66 * \throw FaceUri::Error if URI cannot be parsed
67 */
68 explicit
69 FaceUri(const std::string& uri);
70
71 // This overload is needed so that calls with string literal won't be
72 // resolved to boost::asio::local::stream_protocol::endpoint overload.
73 explicit
74 FaceUri(const char* uri);
75
76 /// exception-safe parsing
77 bool
78 parse(const std::string& uri);
79
80public: // scheme-specific construction
81 /// construct udp4 or udp6 canonical FaceUri
82 explicit
83 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
84
85 /// construct tcp4 or tcp6 canonical FaceUri
86 explicit
87 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
88
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050089 /// construct tcp canonical FaceUri with custom scheme
Junxiao Shi77dcadd2014-10-05 14:40:54 -070090 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
91
92#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
93 /// construct unix canonical FaceUri
94 explicit
95 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
96#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
97
98 /// create fd FaceUri from file descriptor
99 static FaceUri
100 fromFd(int fd);
101
102 /// construct ether canonical FaceUri
103 explicit
104 FaceUri(const ethernet::Address& address);
105
106 /// create dev FaceUri from network device name
107 static FaceUri
108 fromDev(const std::string& ifname);
109
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700110 /// create udp4 or udp6 NIC-associated FaceUri from endpoint and network device name
111 static FaceUri
112 fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, const std::string& ifname);
113
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700114public: // getters
115 /// get scheme (protocol)
116 const std::string&
117 getScheme() const
118 {
119 return m_scheme;
120 }
121
122 /// get host (domain)
123 const std::string&
124 getHost() const
125 {
126 return m_host;
127 }
128
129 /// get port
130 const std::string&
131 getPort() const
132 {
133 return m_port;
134 }
135
136 /// get path
137 const std::string&
138 getPath() const
139 {
140 return m_path;
141 }
142
143 /// write as a string
144 std::string
145 toString() const;
146
147public: // EqualityComparable concept
148 bool
149 operator==(const FaceUri& rhs) const;
150
151 bool
152 operator!=(const FaceUri& rhs) const;
153
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700154public: // canonical FaceUri
155 /** \return whether a FaceUri of the scheme can be canonized
156 */
157 static bool
158 canCanonize(const std::string& scheme);
159
160 /** \brief determine whether this FaceUri is in canonical form
161 * \return true if this FaceUri is in canonical form,
162 * false if this FaceUri is not in canonical form or
163 * or it's undetermined whether this FaceUri is in canonical form
164 */
165 bool
166 isCanonical() const;
167
168 typedef function<void(const FaceUri&)> CanonizeSuccessCallback;
169 typedef function<void(const std::string& reason)> CanonizeFailureCallback;
170
171 /** \brief asynchronously convert this FaceUri to canonical form
172 * \param onSuccess function to call after this FaceUri is converted to canonical form
173 * \note A new FaceUri in canonical form will be created; this FaceUri is unchanged.
174 * \param onFailure function to call if this FaceUri cannot be converted to canonical form
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700175 * \param io reference to `boost::asio::io_service` instance
176 * \param timeout maximum allowable duration of the operations.
177 * It's intentional not to provide a default value: the caller should set
178 * a reasonable value in balance between network delay and user experience.
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700179 */
180 void
181 canonize(const CanonizeSuccessCallback& onSuccess,
182 const CanonizeFailureCallback& onFailure,
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500183 boost::asio::io_service& io,
184 const time::nanoseconds& timeout) const;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700185
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700186private:
187 std::string m_scheme;
188 std::string m_host;
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700189 std::string m_port;
190 std::string m_path;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500191 /// whether to add [] around host when writing string
192 bool m_isV6;
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700193
194 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
195};
196
197std::ostream&
198operator<<(std::ostream& os, const FaceUri& uri);
199
200} // namespace util
201} // namespace ndn
202
203#endif // NDN_UTIL_FACE_URI_HPP