blob: e5f961169218ef300597d03bc03b0ab4e2b09ebd [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie3ef6ee2014-10-05 14:40:54 -07003 * 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
Junxiao Shi77dcadd2014-10-05 14:40:54 -070010 *
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
28#include "face-uri.hpp"
29
30#include <boost/concept_check.hpp>
31#include <boost/regex.hpp>
Junxiao Shie3ef6ee2014-10-05 14:40:54 -070032#include <boost/lexical_cast.hpp>
Junxiao Shi77dcadd2014-10-05 14:40:54 -070033
34namespace ndn {
35namespace util {
36
37BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>));
38
39FaceUri::FaceUri()
40 : m_isV6(false)
41{
42}
43
44FaceUri::FaceUri(const std::string& uri)
45{
46 if (!parse(uri)) {
47 throw Error("Malformed URI: " + uri);
48 }
49}
50
51FaceUri::FaceUri(const char* uri)
52{
53 if (!parse(uri)) {
54 throw Error("Malformed URI: " + std::string(uri));
55 }
56}
57
58bool
59FaceUri::parse(const std::string& uri)
60{
61 m_scheme.clear();
62 m_host.clear();
63 m_isV6 = false;
64 m_port.clear();
65 m_path.clear();
66
67 static const boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?");
68 boost::smatch protocolMatch;
69 if (!boost::regex_match(uri, protocolMatch, protocolExp)) {
70 return false;
71 }
72 m_scheme = protocolMatch[1];
73 const std::string& authority = protocolMatch[2];
74 m_path = protocolMatch[3];
75
76 // pattern for IPv6 address enclosed in [ ], with optional port number
77 static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
78 // pattern for Ethernet address in standard hex-digits-and-colons notation
79 static const boost::regex etherExp("^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$");
80 // pattern for IPv4-mapped IPv6 address, with optional port number
81 static const boost::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
82 // pattern for IPv4/hostname/fd/ifname, with optional port number
83 static const boost::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$");
84
85 if (authority.empty()) {
86 // UNIX, internal
87 }
88 else {
89 boost::smatch match;
90 m_isV6 = boost::regex_match(authority, match, v6Exp);
91 if (m_isV6 ||
92 boost::regex_match(authority, match, etherExp) ||
93 boost::regex_match(authority, match, v4MappedV6Exp) ||
94 boost::regex_match(authority, match, v4HostExp)) {
95 m_host = match[1];
96 m_port = match[2];
97 }
98 else {
99 return false;
100 }
101 }
102
103 return true;
104}
105
106FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint)
107{
108 m_isV6 = endpoint.address().is_v6();
109 m_scheme = m_isV6 ? "udp6" : "udp4";
110 m_host = endpoint.address().to_string();
111 m_port = boost::lexical_cast<std::string>(endpoint.port());
112}
113
114FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint)
115{
116 m_isV6 = endpoint.address().is_v6();
117 m_scheme = m_isV6 ? "tcp6" : "tcp4";
118 m_host = endpoint.address().to_string();
119 m_port = boost::lexical_cast<std::string>(endpoint.port());
120}
121
122FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme)
123 : m_scheme(scheme)
124{
125 m_isV6 = endpoint.address().is_v6();
126 m_host = endpoint.address().to_string();
127 m_port = boost::lexical_cast<std::string>(endpoint.port());
128}
129
130#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
131FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint)
132 : m_isV6(false)
133{
134 m_scheme = "unix";
135 m_path = endpoint.path();
136}
137#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
138
139FaceUri
140FaceUri::fromFd(int fd)
141{
142 FaceUri uri;
143 uri.m_scheme = "fd";
144 uri.m_host = boost::lexical_cast<std::string>(fd);
145 return uri;
146}
147
148FaceUri::FaceUri(const ethernet::Address& address)
149 : m_isV6(true)
150{
151 m_scheme = "ether";
152 m_host = address.toString();
153}
154
155FaceUri
156FaceUri::fromDev(const std::string& ifname)
157{
158 FaceUri uri;
159 uri.m_scheme = "dev";
160 uri.m_host = ifname;
161 return uri;
162}
163
164bool
165FaceUri::operator==(const FaceUri& rhs) const
166{
167 return (m_scheme == rhs.m_scheme &&
168 m_host == rhs.m_host &&
169 m_isV6 == rhs.m_isV6 &&
170 m_port == rhs.m_port &&
171 m_path == rhs.m_path);
172}
173
174bool
175FaceUri::operator!=(const FaceUri& rhs) const
176{
177 return !(*this == rhs);
178}
179
180std::string
181FaceUri::toString() const
182{
183 std::ostringstream os;
184 os << *this;
185 return os.str();
186}
187
188std::ostream&
189operator<<(std::ostream& os, const FaceUri& uri)
190{
191 os << uri.m_scheme << "://";
192 if (uri.m_isV6) {
193 os << "[" << uri.m_host << "]";
194 }
195 else {
196 os << uri.m_host;
197 }
198 if (!uri.m_port.empty()) {
199 os << ":" << uri.m_port;
200 }
201 os << uri.m_path;
202 return os;
203}
204
205} // namespace util
206} // namespace ndn