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