Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_CORE_FACE_URI_H |
| 27 | #define NFD_CORE_FACE_URI_H |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 33 | /** \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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 36 | class FaceUri |
| 37 | { |
| 38 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 39 | class Error : public std::invalid_argument |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 40 | { |
| 41 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 42 | explicit |
| 43 | Error(const std::string& what) |
| 44 | : std::invalid_argument(what) |
| 45 | { |
| 46 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | FaceUri(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 50 | |
| 51 | /** \brief construct by parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 52 | * |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 53 | * \param uri scheme://host[:port]/path |
| 54 | * \throw FaceUri::Error if URI cannot be parsed |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 55 | */ |
| 56 | explicit |
| 57 | FaceUri(const std::string& uri); |
| 58 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 59 | // 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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | /// exception-safe parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 65 | bool |
| 66 | parse(const std::string& uri); |
| 67 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 68 | public: // 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 77 | /// construct tcp canonical FaceUri with customized scheme |
| 78 | FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme); |
| 79 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 80 | #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 Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 90 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 91 | /// construct ether canonical FaceUri |
| 92 | explicit |
| 93 | FaceUri(const ethernet::Address& address); |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 94 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 95 | |
| 96 | /// create dev FaceUri from network device name |
| 97 | static FaceUri |
| 98 | fromDev(const std::string& ifname); |
| 99 | |
| 100 | public: // getters |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 101 | /// get scheme (protocol) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 102 | const std::string& |
| 103 | getScheme() const; |
| 104 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 105 | /// get host (domain) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 106 | const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 107 | getHost() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 108 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 109 | /// get port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 110 | const std::string& |
| 111 | getPort() const; |
| 112 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 113 | /// get path |
| 114 | const std::string& |
| 115 | getPath() const; |
| 116 | |
| 117 | /// write as a string |
| 118 | std::string |
| 119 | toString() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 121 | public: // 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 130 | private: |
| 131 | std::string m_scheme; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 132 | std::string m_host; |
| 133 | /// whether to add [] around host when writing string |
| 134 | bool m_isV6; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 135 | std::string m_port; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 136 | std::string m_path; |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 138 | friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | inline |
| 142 | FaceUri::FaceUri() |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 143 | : m_isV6(false) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 144 | { |
| 145 | } |
| 146 | |
| 147 | inline const std::string& |
| 148 | FaceUri::getScheme() const |
| 149 | { |
| 150 | return m_scheme; |
| 151 | } |
| 152 | |
| 153 | inline const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 154 | FaceUri::getHost() const |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 155 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 156 | return m_host; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | inline const std::string& |
| 160 | FaceUri::getPort() const |
| 161 | { |
| 162 | return m_port; |
| 163 | } |
| 164 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 165 | inline const std::string& |
| 166 | FaceUri::getPath() const |
| 167 | { |
| 168 | return m_path; |
| 169 | } |
| 170 | |
| 171 | inline std::string |
| 172 | FaceUri::toString() const |
| 173 | { |
| 174 | std::ostringstream os; |
| 175 | os << *this; |
| 176 | return os.str(); |
| 177 | } |
| 178 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 179 | inline bool |
| 180 | FaceUri::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 | |
| 189 | inline bool |
| 190 | FaceUri::operator!=(const FaceUri& rhs) const |
| 191 | { |
| 192 | return !(*this == rhs); |
| 193 | } |
| 194 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 195 | inline std::ostream& |
| 196 | operator<<(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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 212 | } // namespace nfd |
| 213 | |
| 214 | #endif // NFD_CORE_FACE_URI_H |