Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame^] | 2 | /* |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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. |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 28 | #ifndef NDN_NET_FACE_URI_HPP |
| 29 | #define NDN_NET_FACE_URI_HPP |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame^] | 31 | #include "asio-fwd.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 32 | #include "ethernet.hpp" |
| 33 | #include "../util/time.hpp" |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 35 | #include <boost/asio/ip/tcp.hpp> |
| 36 | #include <boost/asio/ip/udp.hpp> |
| 37 | #include <boost/asio/local/stream_protocol.hpp> |
| 38 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 39 | namespace ndn { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 40 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 41 | /** \brief represents the underlying protocol and address used by a Face |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 42 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 43 | */ |
| 44 | class FaceUri |
| 45 | { |
| 46 | public: |
| 47 | class Error : public std::invalid_argument |
| 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
| 52 | : std::invalid_argument(what) |
| 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | FaceUri(); |
| 58 | |
| 59 | /** \brief construct by parsing |
| 60 | * |
| 61 | * \param uri scheme://host[:port]/path |
| 62 | * \throw FaceUri::Error if URI cannot be parsed |
| 63 | */ |
| 64 | explicit |
| 65 | FaceUri(const std::string& uri); |
| 66 | |
| 67 | // This overload is needed so that calls with string literal won't be |
| 68 | // resolved to boost::asio::local::stream_protocol::endpoint overload. |
| 69 | explicit |
| 70 | FaceUri(const char* uri); |
| 71 | |
| 72 | /// exception-safe parsing |
| 73 | bool |
| 74 | parse(const std::string& uri); |
| 75 | |
| 76 | public: // scheme-specific construction |
| 77 | /// construct udp4 or udp6 canonical FaceUri |
| 78 | explicit |
| 79 | FaceUri(const boost::asio::ip::udp::endpoint& endpoint); |
| 80 | |
| 81 | /// construct tcp4 or tcp6 canonical FaceUri |
| 82 | explicit |
| 83 | FaceUri(const boost::asio::ip::tcp::endpoint& endpoint); |
| 84 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 85 | /// construct tcp canonical FaceUri with custom scheme |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 86 | FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme); |
| 87 | |
| 88 | #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 89 | /// construct unix canonical FaceUri |
| 90 | explicit |
| 91 | FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint); |
| 92 | #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 93 | |
| 94 | /// create fd FaceUri from file descriptor |
| 95 | static FaceUri |
| 96 | fromFd(int fd); |
| 97 | |
| 98 | /// construct ether canonical FaceUri |
| 99 | explicit |
| 100 | FaceUri(const ethernet::Address& address); |
| 101 | |
| 102 | /// create dev FaceUri from network device name |
| 103 | static FaceUri |
| 104 | fromDev(const std::string& ifname); |
| 105 | |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 106 | /// create udp4 or udp6 NIC-associated FaceUri from endpoint and network device name |
| 107 | static FaceUri |
| 108 | fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, const std::string& ifname); |
| 109 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 110 | public: // getters |
| 111 | /// get scheme (protocol) |
| 112 | const std::string& |
| 113 | getScheme() const |
| 114 | { |
| 115 | return m_scheme; |
| 116 | } |
| 117 | |
| 118 | /// get host (domain) |
| 119 | const std::string& |
| 120 | getHost() const |
| 121 | { |
| 122 | return m_host; |
| 123 | } |
| 124 | |
| 125 | /// get port |
| 126 | const std::string& |
| 127 | getPort() const |
| 128 | { |
| 129 | return m_port; |
| 130 | } |
| 131 | |
| 132 | /// get path |
| 133 | const std::string& |
| 134 | getPath() const |
| 135 | { |
| 136 | return m_path; |
| 137 | } |
| 138 | |
| 139 | /// write as a string |
| 140 | std::string |
| 141 | toString() const; |
| 142 | |
| 143 | public: // EqualityComparable concept |
| 144 | bool |
| 145 | operator==(const FaceUri& rhs) const; |
| 146 | |
| 147 | bool |
| 148 | operator!=(const FaceUri& rhs) const; |
| 149 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 150 | public: // canonical FaceUri |
| 151 | /** \return whether a FaceUri of the scheme can be canonized |
| 152 | */ |
| 153 | static bool |
| 154 | canCanonize(const std::string& scheme); |
| 155 | |
| 156 | /** \brief determine whether this FaceUri is in canonical form |
| 157 | * \return true if this FaceUri is in canonical form, |
| 158 | * false if this FaceUri is not in canonical form or |
| 159 | * or it's undetermined whether this FaceUri is in canonical form |
| 160 | */ |
| 161 | bool |
| 162 | isCanonical() const; |
| 163 | |
| 164 | typedef function<void(const FaceUri&)> CanonizeSuccessCallback; |
| 165 | typedef function<void(const std::string& reason)> CanonizeFailureCallback; |
| 166 | |
| 167 | /** \brief asynchronously convert this FaceUri to canonical form |
| 168 | * \param onSuccess function to call after this FaceUri is converted to canonical form |
| 169 | * \note A new FaceUri in canonical form will be created; this FaceUri is unchanged. |
| 170 | * \param onFailure function to call if this FaceUri cannot be converted to canonical form |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 171 | * \param io reference to `boost::asio::io_service` instance |
| 172 | * \param timeout maximum allowable duration of the operations. |
| 173 | * It's intentional not to provide a default value: the caller should set |
| 174 | * a reasonable value in balance between network delay and user experience. |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 175 | */ |
| 176 | void |
| 177 | canonize(const CanonizeSuccessCallback& onSuccess, |
| 178 | const CanonizeFailureCallback& onFailure, |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 179 | boost::asio::io_service& io, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 180 | time::nanoseconds timeout) const; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 182 | private: |
| 183 | std::string m_scheme; |
| 184 | std::string m_host; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 185 | std::string m_port; |
| 186 | std::string m_path; |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 187 | /// whether to add [] around host when writing string |
| 188 | bool m_isV6; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 189 | |
| 190 | friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri); |
| 191 | }; |
| 192 | |
| 193 | std::ostream& |
| 194 | operator<<(std::ostream& os, const FaceUri& uri); |
| 195 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 196 | } // namespace ndn |
| 197 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 198 | #endif // NDN_NET_FACE_URI_HPP |