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 | #include "face-uri.hpp" |
| 26 | #include "core/logger.hpp" |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 27 | |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 28 | #ifdef HAVE_LIBPCAP |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 29 | #include "ethernet.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 30 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 32 | #include <boost/regex.hpp> |
| 33 | |
| 34 | NFD_LOG_INIT("FaceUri"); |
| 35 | |
| 36 | namespace nfd { |
| 37 | |
| 38 | FaceUri::FaceUri(const std::string& uri) |
| 39 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 40 | if (!parse(uri)) { |
| 41 | throw Error("Malformed URI: " + uri); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | FaceUri::FaceUri(const char* uri) |
| 46 | { |
| 47 | if (!parse(uri)) { |
| 48 | throw Error("Malformed URI: " + std::string(uri)); |
| 49 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool |
| 53 | FaceUri::parse(const std::string& uri) |
| 54 | { |
| 55 | m_scheme.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 56 | m_host.clear(); |
| 57 | m_isV6 = false; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 58 | m_port.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 59 | m_path.clear(); |
| 60 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 61 | static const boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 62 | boost::smatch protocolMatch; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 63 | if (!boost::regex_match(uri, protocolMatch, protocolExp)) { |
| 64 | return false; |
| 65 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 66 | m_scheme = protocolMatch[1]; |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 67 | const std::string& authority = protocolMatch[2]; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 68 | m_path = protocolMatch[3]; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 69 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 70 | // pattern for IPv6 address enclosed in [ ], with optional port number |
| 71 | static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$"); |
| 72 | // pattern for Ethernet address in standard hex-digits-and-colons notation |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 73 | static const boost::regex etherExp("^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$"); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 74 | // pattern for IPv4/hostname/fd/ifname, with optional port number |
| 75 | static const boost::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 76 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 77 | if (authority.empty()) { |
| 78 | // UNIX, internal |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 79 | } |
| 80 | else { |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 81 | boost::smatch match; |
| 82 | m_isV6 = boost::regex_match(authority, match, v6Exp); |
| 83 | if (m_isV6 || |
| 84 | boost::regex_match(authority, match, etherExp) || |
| 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 |