blob: ad0ee1fed344db61099fa768804167ad600866f1 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "face-uri.hpp"
23
24#include <boost/concept_check.hpp>
25#include <boost/regex.hpp>
26
27namespace ndn {
28namespace util {
29
30BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>));
31
32FaceUri::FaceUri()
33 : m_isV6(false)
34{
35}
36
37FaceUri::FaceUri(const std::string& uri)
38{
39 if (!parse(uri)) {
40 throw Error("Malformed URI: " + uri);
41 }
42}
43
44FaceUri::FaceUri(const char* uri)
45{
46 if (!parse(uri)) {
47 throw Error("Malformed URI: " + std::string(uri));
48 }
49}
50
51bool
52FaceUri::parse(const std::string& uri)
53{
54 m_scheme.clear();
55 m_host.clear();
56 m_isV6 = false;
57 m_port.clear();
58 m_path.clear();
59
60 static const boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?");
61 boost::smatch protocolMatch;
62 if (!boost::regex_match(uri, protocolMatch, protocolExp)) {
63 return false;
64 }
65 m_scheme = protocolMatch[1];
66 const std::string& authority = protocolMatch[2];
67 m_path = protocolMatch[3];
68
69 // pattern for IPv6 address enclosed in [ ], with optional port number
70 static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
71 // pattern for Ethernet address in standard hex-digits-and-colons notation
72 static const boost::regex etherExp("^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$");
73 // pattern for IPv4-mapped IPv6 address, with optional port number
74 static const boost::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
75 // pattern for IPv4/hostname/fd/ifname, with optional port number
76 static const boost::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$");
77
78 if (authority.empty()) {
79 // UNIX, internal
80 }
81 else {
82 boost::smatch match;
83 m_isV6 = boost::regex_match(authority, match, v6Exp);
84 if (m_isV6 ||
85 boost::regex_match(authority, match, etherExp) ||
86 boost::regex_match(authority, match, v4MappedV6Exp) ||
87 boost::regex_match(authority, match, v4HostExp)) {
88 m_host = match[1];
89 m_port = match[2];
90 }
91 else {
92 return false;
93 }
94 }
95
96 return true;
97}
98
99FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint)
100{
101 m_isV6 = endpoint.address().is_v6();
102 m_scheme = m_isV6 ? "udp6" : "udp4";
103 m_host = endpoint.address().to_string();
104 m_port = boost::lexical_cast<std::string>(endpoint.port());
105}
106
107FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint)
108{
109 m_isV6 = endpoint.address().is_v6();
110 m_scheme = m_isV6 ? "tcp6" : "tcp4";
111 m_host = endpoint.address().to_string();
112 m_port = boost::lexical_cast<std::string>(endpoint.port());
113}
114
115FaceUri::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
123#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
124FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint)
125 : m_isV6(false)
126{
127 m_scheme = "unix";
128 m_path = endpoint.path();
129}
130#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
131
132FaceUri
133FaceUri::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
141FaceUri::FaceUri(const ethernet::Address& address)
142 : m_isV6(true)
143{
144 m_scheme = "ether";
145 m_host = address.toString();
146}
147
148FaceUri
149FaceUri::fromDev(const std::string& ifname)
150{
151 FaceUri uri;
152 uri.m_scheme = "dev";
153 uri.m_host = ifname;
154 return uri;
155}
156
157bool
158FaceUri::operator==(const FaceUri& rhs) const
159{
160 return (m_scheme == rhs.m_scheme &&
161 m_host == rhs.m_host &&
162 m_isV6 == rhs.m_isV6 &&
163 m_port == rhs.m_port &&
164 m_path == rhs.m_path);
165}
166
167bool
168FaceUri::operator!=(const FaceUri& rhs) const
169{
170 return !(*this == rhs);
171}
172
173std::string
174FaceUri::toString() const
175{
176 std::ostringstream os;
177 os << *this;
178 return os.str();
179}
180
181std::ostream&
182operator<<(std::ostream& os, const FaceUri& uri)
183{
184 os << uri.m_scheme << "://";
185 if (uri.m_isV6) {
186 os << "[" << uri.m_host << "]";
187 }
188 else {
189 os << uri.m_host;
190 }
191 if (!uri.m_port.empty()) {
192 os << ":" << uri.m_port;
193 }
194 os << uri.m_path;
195 return os;
196}
197
198} // namespace util
199} // namespace ndn