32 #include <boost/algorithm/string.hpp> 33 #include <boost/lexical_cast.hpp> 34 #include <boost/mpl/vector.hpp> 35 #include <boost/mpl/for_each.hpp> 43 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>));
53 BOOST_THROW_EXCEPTION(
Error(
"Malformed URI: " + uri));
71 static const std::regex protocolExp(
"(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
72 std::smatch protocolMatch;
73 if (!std::regex_match(uri, protocolMatch, protocolExp)) {
76 m_scheme = protocolMatch[1];
77 std::string authority = protocolMatch[3];
78 m_path = protocolMatch[4];
81 static const std::regex v6LinkLocalExp(
"^\\[([a-fA-F0-9:]+)%([^\\s/:]+)\\](?:\\:(\\d+))?$");
83 static const std::regex v6Exp(
"^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
85 static const std::regex etherExp(
"^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$");
87 static const std::regex v4MappedV6Exp(
"^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
89 static const std::regex v4HostExp(
"^([^:]+)(?:\\:(\\d+))?$");
91 if (authority.empty()) {
96 if (std::regex_match(authority, match, v6LinkLocalExp)) {
98 m_host = match[1].str() +
"%" + match[2].str();
103 m_isV6 = std::regex_match(authority, match, v6Exp);
105 std::regex_match(authority, match, etherExp) ||
106 std::regex_match(authority, match, v4MappedV6Exp) ||
107 std::regex_match(authority, match, v4HostExp)) {
121 m_isV6 = endpoint.address().is_v6();
122 m_scheme = m_isV6 ?
"udp6" :
"udp4";
123 m_host = endpoint.address().to_string();
129 m_isV6 = endpoint.address().is_v6();
130 m_scheme = m_isV6 ?
"tcp6" :
"tcp4";
131 m_host = endpoint.address().to_string();
135 FaceUri::FaceUri(
const boost::asio::ip::tcp::endpoint& endpoint,
const std::string& scheme)
137 m_isV6 = endpoint.address().is_v6();
139 m_host = endpoint.address().to_string();
143 #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS 144 FaceUri::FaceUri(
const boost::asio::local::stream_protocol::endpoint& endpoint)
146 , m_path(endpoint.path())
150 #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS 172 uri.m_scheme =
"dev";
181 uri.m_scheme = endpoint.address().is_v6() ?
"udp6+dev" :
"udp4+dev";
190 return m_isV6 == rhs.m_isV6 &&
191 m_scheme == rhs.m_scheme &&
192 m_host == rhs.m_host &&
193 m_port == rhs.m_port &&
194 m_path == rhs.m_path;
200 return !(*
this == rhs);
206 std::ostringstream os;
214 os << uri.m_scheme <<
"://";
216 os <<
"[" << uri.m_host <<
"]";
221 if (!uri.m_port.empty()) {
222 os <<
":" << uri.m_port;
237 virtual std::set<std::string>
247 time::nanoseconds timeout)
const = 0;
250 template<
typename Protocol>
254 std::set<std::string>
257 return {m_baseScheme, m_v4Scheme, m_v6Scheme};
263 BOOST_THROW_EXCEPTION(std::runtime_error(
"IP host canonization not supported"));
315 time::nanoseconds timeout)
const override 317 BOOST_THROW_EXCEPTION(std::runtime_error(
"IP host canonization not supported"));
359 uint16_t defaultUnicastPort = 6363,
360 uint16_t defaultMulticastPort = 56363)
361 : m_baseScheme(baseScheme)
362 , m_v4Scheme(baseScheme +
'4')
363 , m_v6Scheme(baseScheme +
'6')
364 , m_defaultUnicastPort(defaultUnicastPort)
365 , m_defaultMulticastPort(defaultMulticastPort)
420 unescapeHost(std::string host)
422 auto escapePos = host.find(
"%25");
423 if (escapePos != std::string::npos && escapePos < host.size() - 3) {
430 std::string m_baseScheme;
431 std::string m_v4Scheme;
432 std::string m_v6Scheme;
433 uint16_t m_defaultUnicastPort;
434 uint16_t m_defaultMulticastPort;
468 std::set<std::string>
477 if (!faceUri.
getPort().empty()) {
480 if (!faceUri.
getPath().empty()) {
492 time::nanoseconds timeout)
const override 496 return onFailure(
"invalid ethernet address '" + faceUri.
getHost() +
"'");
501 onSuccess(canonicalUri);
508 std::set<std::string>
524 time::nanoseconds timeout)
const override 526 if (faceUri.
getHost().empty()) {
527 onFailure(
"network interface name is missing");
530 if (!faceUri.
getPort().empty()) {
531 onFailure(
"port number is not allowed");
535 onFailure(
"path is not allowed");
541 onSuccess(canonicalUri);
548 std::set<std::string>
551 return {
"udp4+dev",
"udp6+dev"};
557 if (faceUri.
getPort().empty()) {
560 if (!faceUri.
getPath().empty()) {
570 time::nanoseconds timeout)
const override 576 onFailure(
"cannot canonize " + faceUri.
toString());
582 TcpCanonizeProvider*,
583 EtherCanonizeProvider*,
584 DevCanonizeProvider*,
593 : m_providerTable(providerTable)
597 template<
typename CP>
601 shared_ptr<CanonizeProvider> cp = make_shared<CP>();
602 auto schemes = cp->getSchemes();
603 BOOST_ASSERT(!schemes.empty());
605 for (
const auto& scheme : schemes) {
606 BOOST_ASSERT(m_providerTable.count(scheme) == 0);
607 m_providerTable[scheme] = cp;
615 static const CanonizeProvider*
619 if (providerTable.empty()) {
621 BOOST_ASSERT(!providerTable.empty());
624 auto it = providerTable.find(scheme);
625 return it == providerTable.end() ? nullptr : it->second.get();
649 time::nanoseconds timeout)
const 654 onFailure(
"scheme not supported");
660 onSuccess ? onSuccess : [] (
auto&&) {},
661 onFailure ? onFailure : [] (
auto&&) {},
IpHostCanonizeProvider(const std::string &baseScheme, uint16_t defaultUnicastPort=6363, uint16_t defaultMulticastPort=56363)
std::set< std::string > getSchemes() const override
virtual ~CanonizeProvider()=default
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation,...
Copyright (c) 2011-2015 Regents of the University of California.
function< void(const std::string &reason)> CanonizeFailureCallback
static FaceUri fromFd(int fd)
create fd FaceUri from file descriptor
bool isCanonical(const FaceUri &faceUri) const override
const std::string & getHost() const
get host (domain)
bool isCanonical(const FaceUri &faceUri) const override
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const override
bool operator!=(const FaceUri &rhs) const
function< void(const FaceUri &)> CanonizeSuccessCallback
std::ostream & operator<<(std::ostream &os, const Data &data)
static bool canCanonize(const std::string &scheme)
std::string toString() const
write as a string
virtual bool isCanonical(const FaceUri &faceUri) const =0
bool isCanonical(const FaceUri &faceUri) const override
static const CanonizeProvider * getCanonizeProvider(const std::string &scheme)
void canonize(const CanonizeSuccessCallback &onSuccess, const CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const
asynchronously convert this FaceUri to canonical form
const std::string & getPort() const
get port
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const override
std::string toString(char sep=':') const
Converts the address to a human-readable string.
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const override
bool isCanonical(const FaceUri &faceUri) const override
std::set< std::string > getSchemes() const override
static FaceUri fromUdpDev(const boost::asio::ip::udp::endpoint &endpoint, const std::string &ifname)
create udp4 or udp6 NIC-associated FaceUri from endpoint and network device name
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
a CanonizeProvider provides FaceUri canonization functionality for a group of schemes
bool parse(const std::string &uri)
exception-safe parsing
std::string unescape(const std::string &str)
Decode a percent-encoded string.
std::set< std::string > getSchemes() const override
represents the underlying protocol and address used by a Face
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const override
represents an Ethernet hardware address
const std::string & getScheme() const
get scheme (protocol)
virtual void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, time::nanoseconds timeout) const =0
CanonizeProviderTableInitializer(CanonizeProviderTable &providerTable)
bool operator==(const FaceUri &rhs) const
std::set< std::string > getSchemes() const override
std::string to_string(const V &v)
bool isCanonical() const
determine whether this FaceUri is in canonical form
std::map< std::string, shared_ptr< CanonizeProvider > > CanonizeProviderTable
const std::string & getPath() const
get path
boost::mpl::vector< UdpCanonizeProvider *, TcpCanonizeProvider *, EtherCanonizeProvider *, DevCanonizeProvider *, UdpDevCanonizeProvider * > CanonizeProviders
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
virtual std::set< std::string > getSchemes() const =0