Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 24 | |
| 25 | #ifndef NFD_CORE_FACE_URI_H |
| 26 | #define NFD_CORE_FACE_URI_H |
| 27 | |
| 28 | #include "common.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 32 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 33 | namespace ethernet { |
| 34 | class Address; |
| 35 | } // namespace ethernet |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 36 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 41 | class FaceUri |
| 42 | { |
| 43 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 44 | class Error : public std::invalid_argument |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 47 | explicit |
| 48 | Error(const std::string& what) |
| 49 | : std::invalid_argument(what) |
| 50 | { |
| 51 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | FaceUri(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 55 | |
| 56 | /** \brief construct by parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 57 | * |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 58 | * \param uri scheme://host[:port]/path |
| 59 | * \throw FaceUri::Error if URI cannot be parsed |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 60 | */ |
| 61 | explicit |
| 62 | FaceUri(const std::string& uri); |
| 63 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | // 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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 69 | /// exception-safe parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 70 | bool |
| 71 | parse(const std::string& uri); |
| 72 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 73 | public: // 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 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 82 | /// construct tcp canonical FaceUri with customized scheme |
| 83 | FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme); |
| 84 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 85 | #ifdef HAVE_UNIX_SOCKETS |
| 86 | /// construct unix canonical FaceUri |
| 87 | explicit |
| 88 | FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint); |
| 89 | #endif // HAVE_UNIX_SOCKETS |
| 90 | |
| 91 | /// create fd FaceUri from file descriptor |
| 92 | static FaceUri |
| 93 | fromFd(int fd); |
| 94 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 95 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 96 | /// construct ether canonical FaceUri |
| 97 | explicit |
| 98 | FaceUri(const ethernet::Address& address); |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 99 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 100 | |
| 101 | /// create dev FaceUri from network device name |
| 102 | static FaceUri |
| 103 | fromDev(const std::string& ifname); |
| 104 | |
| 105 | public: // getters |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 106 | /// get scheme (protocol) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 107 | const std::string& |
| 108 | getScheme() const; |
| 109 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 110 | /// get host (domain) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 111 | const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 112 | getHost() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 113 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 114 | /// get port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 115 | const std::string& |
| 116 | getPort() const; |
| 117 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 118 | /// get path |
| 119 | const std::string& |
| 120 | getPath() const; |
| 121 | |
| 122 | /// write as a string |
| 123 | std::string |
| 124 | toString() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 126 | public: // EqualityComparable concept |
| 127 | /// equality operator |
| 128 | bool |
| 129 | operator==(const FaceUri& rhs) const; |
| 130 | |
| 131 | /// inequality operator |
| 132 | bool |
| 133 | operator!=(const FaceUri& rhs) const; |
| 134 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 135 | private: |
| 136 | std::string m_scheme; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 137 | std::string m_host; |
| 138 | /// whether to add [] around host when writing string |
| 139 | bool m_isV6; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 140 | std::string m_port; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 141 | std::string m_path; |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 142 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 143 | friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | inline |
| 147 | FaceUri::FaceUri() |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 148 | : m_isV6(false) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 149 | { |
| 150 | } |
| 151 | |
| 152 | inline const std::string& |
| 153 | FaceUri::getScheme() const |
| 154 | { |
| 155 | return m_scheme; |
| 156 | } |
| 157 | |
| 158 | inline const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 159 | FaceUri::getHost() const |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 160 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 161 | return m_host; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | inline const std::string& |
| 165 | FaceUri::getPort() const |
| 166 | { |
| 167 | return m_port; |
| 168 | } |
| 169 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 170 | inline const std::string& |
| 171 | FaceUri::getPath() const |
| 172 | { |
| 173 | return m_path; |
| 174 | } |
| 175 | |
| 176 | inline std::string |
| 177 | FaceUri::toString() const |
| 178 | { |
| 179 | std::ostringstream os; |
| 180 | os << *this; |
| 181 | return os.str(); |
| 182 | } |
| 183 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 184 | inline bool |
| 185 | FaceUri::operator==(const FaceUri& rhs) const |
| 186 | { |
| 187 | return (m_scheme == rhs.m_scheme && |
| 188 | m_host == rhs.m_host && |
| 189 | m_isV6 == rhs.m_isV6 && |
| 190 | m_port == rhs.m_port && |
| 191 | m_path == rhs.m_path); |
| 192 | } |
| 193 | |
| 194 | inline bool |
| 195 | FaceUri::operator!=(const FaceUri& rhs) const |
| 196 | { |
| 197 | return !(*this == rhs); |
| 198 | } |
| 199 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 200 | inline std::ostream& |
| 201 | operator<<(std::ostream& os, const FaceUri& uri) |
| 202 | { |
| 203 | os << uri.m_scheme << "://"; |
| 204 | if (uri.m_isV6) { |
| 205 | os << "[" << uri.m_host << "]"; |
| 206 | } |
| 207 | else { |
| 208 | os << uri.m_host; |
| 209 | } |
| 210 | if (!uri.m_port.empty()) { |
| 211 | os << ":" << uri.m_port; |
| 212 | } |
| 213 | os << uri.m_path; |
| 214 | return os; |
| 215 | } |
| 216 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 217 | } // namespace nfd |
| 218 | |
| 219 | #endif // NFD_CORE_FACE_URI_H |