Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | namespace nfd { |
| 13 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 14 | #ifdef HAVE_PCAP |
| 15 | namespace ethernet { |
| 16 | class 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 23 | class FaceUri |
| 24 | { |
| 25 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 26 | class Error : public std::invalid_argument |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 27 | { |
| 28 | public: |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 29 | explicit |
| 30 | Error(const std::string& what) |
| 31 | : std::invalid_argument(what) |
| 32 | { |
| 33 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | FaceUri(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 37 | |
| 38 | /** \brief construct by parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 39 | * |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 40 | * \param uri scheme://host[:port]/path |
| 41 | * \throw FaceUri::Error if URI cannot be parsed |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 42 | */ |
| 43 | explicit |
| 44 | FaceUri(const std::string& uri); |
| 45 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 46 | // 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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 51 | /// exception-safe parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 52 | bool |
| 53 | parse(const std::string& uri); |
| 54 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 55 | public: // 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 | |
| 84 | public: // getters |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 85 | /// get scheme (protocol) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 86 | const std::string& |
| 87 | getScheme() const; |
| 88 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 89 | /// get host (domain) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 90 | const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 91 | getHost() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 92 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 93 | /// get port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 94 | const std::string& |
| 95 | getPort() const; |
| 96 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 97 | /// get path |
| 98 | const std::string& |
| 99 | getPath() const; |
| 100 | |
| 101 | /// write as a string |
| 102 | std::string |
| 103 | toString() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | std::string m_scheme; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 107 | std::string m_host; |
| 108 | /// whether to add [] around host when writing string |
| 109 | bool m_isV6; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 110 | std::string m_port; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 111 | std::string m_path; |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 113 | friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | inline |
| 117 | FaceUri::FaceUri() |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 118 | : m_isV6(false) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 119 | { |
| 120 | } |
| 121 | |
| 122 | inline const std::string& |
| 123 | FaceUri::getScheme() const |
| 124 | { |
| 125 | return m_scheme; |
| 126 | } |
| 127 | |
| 128 | inline const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 129 | FaceUri::getHost() const |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 130 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 131 | return m_host; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | inline const std::string& |
| 135 | FaceUri::getPort() const |
| 136 | { |
| 137 | return m_port; |
| 138 | } |
| 139 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 140 | inline const std::string& |
| 141 | FaceUri::getPath() const |
| 142 | { |
| 143 | return m_path; |
| 144 | } |
| 145 | |
| 146 | inline std::string |
| 147 | FaceUri::toString() const |
| 148 | { |
| 149 | std::ostringstream os; |
| 150 | os << *this; |
| 151 | return os.str(); |
| 152 | } |
| 153 | |
| 154 | inline std::ostream& |
| 155 | operator<<(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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 171 | } // namespace nfd |
| 172 | |
| 173 | #endif // NFD_CORE_FACE_URI_H |