blob: 766f3531d22b6e736a56f1510d5010741019d573 [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
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 **/
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080024
25#ifndef NFD_CORE_FACE_URI_H
26#define NFD_CORE_FACE_URI_H
27
28#include "common.hpp"
29
30namespace nfd {
31
Junxiao Shie38b1c42014-04-01 23:45:36 -070032#ifdef HAVE_PCAP
33namespace ethernet {
34class Address;
35} // namespace ethernet
36#endif // HAVE_PCAP
37
38/** \brief represents the underlying protocol and address used by a Face
39 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
40 */
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080041class FaceUri
42{
43public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070044 class Error : public std::invalid_argument
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080045 {
46 public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070047 explicit
48 Error(const std::string& what)
49 : std::invalid_argument(what)
50 {
51 }
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080052 };
53
54 FaceUri();
Junxiao Shi61e3cc52014-03-03 20:40:28 -070055
56 /** \brief construct by parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080057 *
Junxiao Shie38b1c42014-04-01 23:45:36 -070058 * \param uri scheme://host[:port]/path
59 * \throw FaceUri::Error if URI cannot be parsed
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080060 */
61 explicit
62 FaceUri(const std::string& uri);
63
Junxiao Shi61e3cc52014-03-03 20:40:28 -070064 // This overload is needed so that calls with string literal won't be
65 // resolved to boost::asio::local::stream_protocol::endpoint overload.
66 explicit
67 FaceUri(const char* uri);
68
Junxiao Shi61e3cc52014-03-03 20:40:28 -070069 /// exception-safe parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080070 bool
71 parse(const std::string& uri);
72
Junxiao Shie38b1c42014-04-01 23:45:36 -070073public: // scheme-specific construction
74 /// construct tcp4 or tcp6 canonical FaceUri
75 explicit
76 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
77
78 /// construct udp4 or udp6 canonical FaceUri
79 explicit
80 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
81
82#ifdef HAVE_UNIX_SOCKETS
83 /// construct unix canonical FaceUri
84 explicit
85 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
86#endif // HAVE_UNIX_SOCKETS
87
88 /// create fd FaceUri from file descriptor
89 static FaceUri
90 fromFd(int fd);
91
92#ifdef HAVE_PCAP
93 /// construct ether canonical FaceUri
94 explicit
95 FaceUri(const ethernet::Address& address);
96#endif // HAVE_PCAP
97
98 /// create dev FaceUri from network device name
99 static FaceUri
100 fromDev(const std::string& ifname);
101
102public: // getters
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700103 /// get scheme (protocol)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800104 const std::string&
105 getScheme() const;
106
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700107 /// get host (domain)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800108 const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700109 getHost() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800110
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700111 /// get port
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800112 const std::string&
113 getPort() const;
114
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700115 /// get path
116 const std::string&
117 getPath() const;
118
119 /// write as a string
120 std::string
121 toString() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800122
123private:
124 std::string m_scheme;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700125 std::string m_host;
126 /// whether to add [] around host when writing string
127 bool m_isV6;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800128 std::string m_port;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700129 std::string m_path;
Junxiao Shie38b1c42014-04-01 23:45:36 -0700130
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700131 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800132};
133
134inline
135FaceUri::FaceUri()
Junxiao Shie38b1c42014-04-01 23:45:36 -0700136 : m_isV6(false)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800137{
138}
139
140inline const std::string&
141FaceUri::getScheme() const
142{
143 return m_scheme;
144}
145
146inline const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700147FaceUri::getHost() const
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800148{
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700149 return m_host;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800150}
151
152inline const std::string&
153FaceUri::getPort() const
154{
155 return m_port;
156}
157
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700158inline const std::string&
159FaceUri::getPath() const
160{
161 return m_path;
162}
163
164inline std::string
165FaceUri::toString() const
166{
167 std::ostringstream os;
168 os << *this;
169 return os.str();
170}
171
172inline std::ostream&
173operator<<(std::ostream& os, const FaceUri& uri)
174{
175 os << uri.m_scheme << "://";
176 if (uri.m_isV6) {
177 os << "[" << uri.m_host << "]";
178 }
179 else {
180 os << uri.m_host;
181 }
182 if (!uri.m_port.empty()) {
183 os << ":" << uri.m_port;
184 }
185 os << uri.m_path;
186 return os;
187}
188
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800189} // namespace nfd
190
191#endif // NFD_CORE_FACE_URI_H