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 | #include "face-uri.hpp" |
| 27 | #include "core/logger.hpp" |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 28 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 29 | #include <boost/regex.hpp> |
| 30 | |
| 31 | NFD_LOG_INIT("FaceUri"); |
| 32 | |
| 33 | namespace nfd { |
| 34 | |
| 35 | FaceUri::FaceUri(const std::string& uri) |
| 36 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 37 | if (!parse(uri)) { |
| 38 | throw Error("Malformed URI: " + uri); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | FaceUri::FaceUri(const char* uri) |
| 43 | { |
| 44 | if (!parse(uri)) { |
| 45 | throw Error("Malformed URI: " + std::string(uri)); |
| 46 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool |
| 50 | FaceUri::parse(const std::string& uri) |
| 51 | { |
| 52 | m_scheme.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 53 | m_host.clear(); |
| 54 | m_isV6 = false; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 55 | m_port.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 56 | m_path.clear(); |
| 57 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 58 | static const boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 59 | boost::smatch protocolMatch; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 60 | if (!boost::regex_match(uri, protocolMatch, protocolExp)) { |
| 61 | return false; |
| 62 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 63 | m_scheme = protocolMatch[1]; |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 64 | const std::string& authority = protocolMatch[2]; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 65 | m_path = protocolMatch[3]; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 66 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 67 | // pattern for IPv6 address enclosed in [ ], with optional port number |
| 68 | static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$"); |
| 69 | // pattern for Ethernet address in standard hex-digits-and-colons notation |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 70 | static const boost::regex etherExp("^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$"); |
Junxiao Shi | 5cc1e34 | 2014-05-29 06:32:16 -0700 | [diff] [blame] | 71 | // pattern for IPv4-mapped IPv6 address, with optional port number |
| 72 | static const boost::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$"); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 73 | // pattern for IPv4/hostname/fd/ifname, with optional port number |
| 74 | static const boost::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 75 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 76 | if (authority.empty()) { |
| 77 | // UNIX, internal |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 78 | } |
| 79 | else { |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 80 | boost::smatch match; |
| 81 | m_isV6 = boost::regex_match(authority, match, v6Exp); |
| 82 | if (m_isV6 || |
| 83 | boost::regex_match(authority, match, etherExp) || |
Junxiao Shi | 5cc1e34 | 2014-05-29 06:32:16 -0700 | [diff] [blame] | 84 | boost::regex_match(authority, match, v4MappedV6Exp) || |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 85 | boost::regex_match(authority, match, v4HostExp)) { |
| 86 | m_host = match[1]; |
| 87 | m_port = match[2]; |
| 88 | } |
| 89 | else { |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 90 | return false; |
| 91 | } |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | NFD_LOG_DEBUG("URI [" << uri << "] parsed into: " << |
| 95 | m_scheme << ", " << m_host << ", " << m_port << ", " << m_path); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 99 | FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint) |
| 100 | { |
| 101 | m_isV6 = endpoint.address().is_v6(); |
| 102 | m_scheme = m_isV6 ? "tcp6" : "tcp4"; |
| 103 | m_host = endpoint.address().to_string(); |
| 104 | m_port = boost::lexical_cast<std::string>(endpoint.port()); |
| 105 | } |
| 106 | |
| 107 | FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint) |
| 108 | { |
| 109 | m_isV6 = endpoint.address().is_v6(); |
| 110 | m_scheme = m_isV6 ? "udp6" : "udp4"; |
| 111 | m_host = endpoint.address().to_string(); |
| 112 | m_port = boost::lexical_cast<std::string>(endpoint.port()); |
| 113 | } |
| 114 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 115 | FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme) |
| 116 | : m_scheme(scheme) |
| 117 | { |
| 118 | m_isV6 = endpoint.address().is_v6(); |
| 119 | m_host = endpoint.address().to_string(); |
| 120 | m_port = boost::lexical_cast<std::string>(endpoint.port()); |
| 121 | } |
| 122 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 123 | #ifdef HAVE_UNIX_SOCKETS |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 124 | FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint) |
| 125 | : m_isV6(false) |
| 126 | { |
| 127 | m_scheme = "unix"; |
| 128 | m_path = endpoint.path(); |
| 129 | } |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 130 | #endif // HAVE_UNIX_SOCKETS |
| 131 | |
| 132 | FaceUri |
| 133 | FaceUri::fromFd(int fd) |
| 134 | { |
| 135 | FaceUri uri; |
| 136 | uri.m_scheme = "fd"; |
| 137 | uri.m_host = boost::lexical_cast<std::string>(fd); |
| 138 | return uri; |
| 139 | } |
| 140 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 141 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 142 | FaceUri::FaceUri(const ethernet::Address& address) |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 143 | : m_isV6(true) |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 144 | { |
| 145 | m_scheme = "ether"; |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 146 | m_host = address.toString(); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 147 | } |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 148 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 149 | |
| 150 | FaceUri |
| 151 | FaceUri::fromDev(const std::string& ifname) |
| 152 | { |
| 153 | FaceUri uri; |
| 154 | uri.m_scheme = "dev"; |
| 155 | uri.m_host = ifname; |
| 156 | return uri; |
| 157 | } |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 158 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 159 | } // namespace nfd |