blob: fa0a62ace0e3a849037e485a1782c325e5228056 [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_CORE_FACE_URI_H
8#define NFD_CORE_FACE_URI_H
9
10#include "common.hpp"
11
12namespace nfd {
13
Junxiao Shie38b1c42014-04-01 23:45:36 -070014#ifdef HAVE_PCAP
15namespace ethernet {
16class Address;
17} // namespace ethernet
18#endif // HAVE_PCAP
19
20/** \brief represents the underlying protocol and address used by a Face
21 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
22 */
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080023class FaceUri
24{
25public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070026 class Error : public std::invalid_argument
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080027 {
28 public:
Junxiao Shie38b1c42014-04-01 23:45:36 -070029 explicit
30 Error(const std::string& what)
31 : std::invalid_argument(what)
32 {
33 }
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080034 };
35
36 FaceUri();
Junxiao Shi61e3cc52014-03-03 20:40:28 -070037
38 /** \brief construct by parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080039 *
Junxiao Shie38b1c42014-04-01 23:45:36 -070040 * \param uri scheme://host[:port]/path
41 * \throw FaceUri::Error if URI cannot be parsed
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080042 */
43 explicit
44 FaceUri(const std::string& uri);
45
Junxiao Shi61e3cc52014-03-03 20:40:28 -070046 // This overload is needed so that calls with string literal won't be
47 // resolved to boost::asio::local::stream_protocol::endpoint overload.
48 explicit
49 FaceUri(const char* uri);
50
Junxiao Shi61e3cc52014-03-03 20:40:28 -070051 /// exception-safe parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080052 bool
53 parse(const std::string& uri);
54
Junxiao Shie38b1c42014-04-01 23:45:36 -070055public: // scheme-specific construction
56 /// construct tcp4 or tcp6 canonical FaceUri
57 explicit
58 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
59
60 /// construct udp4 or udp6 canonical FaceUri
61 explicit
62 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
63
64#ifdef HAVE_UNIX_SOCKETS
65 /// construct unix canonical FaceUri
66 explicit
67 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
68#endif // HAVE_UNIX_SOCKETS
69
70 /// create fd FaceUri from file descriptor
71 static FaceUri
72 fromFd(int fd);
73
74#ifdef HAVE_PCAP
75 /// construct ether canonical FaceUri
76 explicit
77 FaceUri(const ethernet::Address& address);
78#endif // HAVE_PCAP
79
80 /// create dev FaceUri from network device name
81 static FaceUri
82 fromDev(const std::string& ifname);
83
84public: // getters
Junxiao Shi61e3cc52014-03-03 20:40:28 -070085 /// get scheme (protocol)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080086 const std::string&
87 getScheme() const;
88
Junxiao Shi61e3cc52014-03-03 20:40:28 -070089 /// get host (domain)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080090 const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -070091 getHost() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080092
Junxiao Shi61e3cc52014-03-03 20:40:28 -070093 /// get port
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080094 const std::string&
95 getPort() const;
96
Junxiao Shi61e3cc52014-03-03 20:40:28 -070097 /// get path
98 const std::string&
99 getPath() const;
100
101 /// write as a string
102 std::string
103 toString() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800104
105private:
106 std::string m_scheme;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700107 std::string m_host;
108 /// whether to add [] around host when writing string
109 bool m_isV6;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800110 std::string m_port;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700111 std::string m_path;
Junxiao Shie38b1c42014-04-01 23:45:36 -0700112
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700113 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800114};
115
116inline
117FaceUri::FaceUri()
Junxiao Shie38b1c42014-04-01 23:45:36 -0700118 : m_isV6(false)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800119{
120}
121
122inline const std::string&
123FaceUri::getScheme() const
124{
125 return m_scheme;
126}
127
128inline const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700129FaceUri::getHost() const
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800130{
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700131 return m_host;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800132}
133
134inline const std::string&
135FaceUri::getPort() const
136{
137 return m_port;
138}
139
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700140inline const std::string&
141FaceUri::getPath() const
142{
143 return m_path;
144}
145
146inline std::string
147FaceUri::toString() const
148{
149 std::ostringstream os;
150 os << *this;
151 return os.str();
152}
153
154inline std::ostream&
155operator<<(std::ostream& os, const FaceUri& uri)
156{
157 os << uri.m_scheme << "://";
158 if (uri.m_isV6) {
159 os << "[" << uri.m_host << "]";
160 }
161 else {
162 os << uri.m_host;
163 }
164 if (!uri.m_port.empty()) {
165 os << ":" << uri.m_port;
166 }
167 os << uri.m_path;
168 return os;
169}
170
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800171} // namespace nfd
172
173#endif // NFD_CORE_FACE_URI_H