blob: 17cda7125316fe008e32ca1d8f1d9049ea4c827f [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3ffe66d2014-11-06 15:37:59 -07003 * 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 * 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/>.
Junxiao Shi3ffe66d2014-11-06 15:37:59 -070024 */
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080025
26#ifndef NFD_CORE_FACE_URI_H
27#define NFD_CORE_FACE_URI_H
28
29#include "common.hpp"
30
31namespace nfd {
32
Junxiao Shie38b1c42014-04-01 23:45:36 -070033/** \brief represents the underlying protocol and address used by a Face
34 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
35 */
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080036class FaceUri
37{
38public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070039 class Error : public std::invalid_argument
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080040 {
41 public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070042 explicit
43 Error(const std::string& what)
44 : std::invalid_argument(what)
45 {
46 }
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080047 };
48
49 FaceUri();
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050
51 /** \brief construct by parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080052 *
Junxiao Shie38b1c42014-04-01 23:45:36 -070053 * \param uri scheme://host[:port]/path
54 * \throw FaceUri::Error if URI cannot be parsed
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080055 */
56 explicit
57 FaceUri(const std::string& uri);
58
Junxiao Shi61e3cc52014-03-03 20:40:28 -070059 // This overload is needed so that calls with string literal won't be
60 // resolved to boost::asio::local::stream_protocol::endpoint overload.
61 explicit
62 FaceUri(const char* uri);
63
Junxiao Shi61e3cc52014-03-03 20:40:28 -070064 /// exception-safe parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080065 bool
66 parse(const std::string& uri);
67
Junxiao Shie38b1c42014-04-01 23:45:36 -070068public: // scheme-specific construction
69 /// construct tcp4 or tcp6 canonical FaceUri
70 explicit
71 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
72
73 /// construct udp4 or udp6 canonical FaceUri
74 explicit
75 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
76
Wentao Shang53df1632014-04-21 12:01:32 -070077 /// construct tcp canonical FaceUri with customized scheme
78 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
79
Junxiao Shie38b1c42014-04-01 23:45:36 -070080#ifdef HAVE_UNIX_SOCKETS
81 /// construct unix canonical FaceUri
82 explicit
83 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
84#endif // HAVE_UNIX_SOCKETS
85
86 /// create fd FaceUri from file descriptor
87 static FaceUri
88 fromFd(int fd);
89
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070090#ifdef HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -070091 /// construct ether canonical FaceUri
92 explicit
93 FaceUri(const ethernet::Address& address);
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070094#endif // HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -070095
96 /// create dev FaceUri from network device name
97 static FaceUri
98 fromDev(const std::string& ifname);
99
100public: // getters
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700101 /// get scheme (protocol)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800102 const std::string&
103 getScheme() const;
104
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700105 /// get host (domain)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800106 const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700107 getHost() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800108
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700109 /// get port
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800110 const std::string&
111 getPort() const;
112
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700113 /// get path
114 const std::string&
115 getPath() const;
116
117 /// write as a string
118 std::string
119 toString() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800120
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300121public: // EqualityComparable concept
122 /// equality operator
123 bool
124 operator==(const FaceUri& rhs) const;
125
126 /// inequality operator
127 bool
128 operator!=(const FaceUri& rhs) const;
129
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800130private:
131 std::string m_scheme;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700132 std::string m_host;
133 /// whether to add [] around host when writing string
134 bool m_isV6;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800135 std::string m_port;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700136 std::string m_path;
Junxiao Shie38b1c42014-04-01 23:45:36 -0700137
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700138 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800139};
140
141inline
142FaceUri::FaceUri()
Junxiao Shie38b1c42014-04-01 23:45:36 -0700143 : m_isV6(false)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800144{
145}
146
147inline const std::string&
148FaceUri::getScheme() const
149{
150 return m_scheme;
151}
152
153inline const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700154FaceUri::getHost() const
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800155{
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700156 return m_host;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800157}
158
159inline const std::string&
160FaceUri::getPort() const
161{
162 return m_port;
163}
164
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700165inline const std::string&
166FaceUri::getPath() const
167{
168 return m_path;
169}
170
171inline std::string
172FaceUri::toString() const
173{
174 std::ostringstream os;
175 os << *this;
176 return os.str();
177}
178
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300179inline bool
180FaceUri::operator==(const FaceUri& rhs) const
181{
182 return (m_scheme == rhs.m_scheme &&
183 m_host == rhs.m_host &&
184 m_isV6 == rhs.m_isV6 &&
185 m_port == rhs.m_port &&
186 m_path == rhs.m_path);
187}
188
189inline bool
190FaceUri::operator!=(const FaceUri& rhs) const
191{
192 return !(*this == rhs);
193}
194
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700195inline std::ostream&
196operator<<(std::ostream& os, const FaceUri& uri)
197{
198 os << uri.m_scheme << "://";
199 if (uri.m_isV6) {
200 os << "[" << uri.m_host << "]";
201 }
202 else {
203 os << uri.m_host;
204 }
205 if (!uri.m_port.empty()) {
206 os << ":" << uri.m_port;
207 }
208 os << uri.m_path;
209 return os;
210}
211
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800212} // namespace nfd
213
214#endif // NFD_CORE_FACE_URI_H