34 #include <boost/algorithm/string.hpp> 35 #include <boost/lexical_cast.hpp> 36 #include <boost/mpl/vector.hpp> 37 #include <boost/mpl/for_each.hpp> 38 #include <boost/regex.hpp> 44 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>));
54 BOOST_THROW_EXCEPTION(
Error(
"Malformed URI: " + uri));
72 static const boost::regex protocolExp(
"(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
73 boost::smatch protocolMatch;
74 if (!boost::regex_match(uri, protocolMatch, protocolExp)) {
77 m_scheme = protocolMatch[1];
78 const std::string& authority = protocolMatch[3];
79 m_path = protocolMatch[4];
82 static const boost::regex v6LinkLocalExp(
"^\\[([a-fA-F0-9:]+)%([^\\s/:]+)\\](?:\\:(\\d+))?$");
84 static const boost::regex v6Exp(
"^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
86 static const boost::regex etherExp(
"^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$");
88 static const boost::regex v4MappedV6Exp(
"^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
90 static const boost::regex v4HostExp(
"^([^:]+)(?:\\:(\\d+))?$");
92 if (authority.empty()) {
97 if (boost::regex_match(authority, match, v6LinkLocalExp)) {
99 m_host = match[1] +
"%" + match[2];
104 m_isV6 = boost::regex_match(authority, match, v6Exp);
106 boost::regex_match(authority, match, etherExp) ||
107 boost::regex_match(authority, match, v4MappedV6Exp) ||
108 boost::regex_match(authority, match, v4HostExp)) {
122 m_isV6 = endpoint.address().is_v6();
123 m_scheme = m_isV6 ?
"udp6" :
"udp4";
124 m_host = endpoint.address().to_string();
130 m_isV6 = endpoint.address().is_v6();
131 m_scheme = m_isV6 ?
"tcp6" :
"tcp4";
132 m_host = endpoint.address().to_string();
136 FaceUri::FaceUri(
const boost::asio::ip::tcp::endpoint& endpoint,
const std::string& scheme)
138 m_isV6 = endpoint.address().is_v6();
140 m_host = endpoint.address().to_string();
144 #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS 145 FaceUri::FaceUri(
const boost::asio::local::stream_protocol::endpoint& endpoint)
147 , m_path(endpoint.path())
151 #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS 173 uri.m_scheme =
"dev";
182 uri.m_scheme = endpoint.address().is_v6() ?
"udp6+dev" :
"udp4+dev";
191 return m_isV6 == rhs.m_isV6 &&
192 m_scheme == rhs.m_scheme &&
193 m_host == rhs.m_host &&
194 m_port == rhs.m_port &&
195 m_path == rhs.m_path;
201 return !(*
this == rhs);
207 std::ostringstream os;
215 os << uri.m_scheme <<
"://";
217 os <<
"[" << uri.m_host <<
"]";
222 if (!uri.m_port.empty()) {
223 os <<
":" << uri.m_port;
238 virtual std::set<std::string>
248 boost::asio::io_service& io, time::nanoseconds timeout)
const = 0;
251 template<
typename Protocol>
255 std::set<std::string>
258 return {m_baseScheme, m_v4Scheme, m_v6Scheme};
264 if (faceUri.
getPort().empty()) {
267 if (!faceUri.
getPath().empty()) {
271 boost::system::error_code ec;
277 bool hasCorrectScheme = (faceUri.
getScheme() == m_v4Scheme && addr.is_v4()) ||
278 (faceUri.
getScheme() == m_v6Scheme && addr.is_v6());
279 if (!hasCorrectScheme) {
283 auto checkAddressWithUri = [] (
const boost::asio::ip::address& addr,
284 const FaceUri& faceUri) ->
bool {
285 if (addr.is_v4() || !addr.to_v6().is_link_local()) {
286 return addr.to_string() == faceUri.
getHost();
289 std::vector<std::string> addrFields, faceUriFields;
290 std::string addrString = addr.to_string();
291 std::string faceUriString = faceUri.
getHost();
293 boost::algorithm::split(addrFields, addrString, boost::is_any_of(
"%"));
294 boost::algorithm::split(faceUriFields, faceUriString, boost::is_any_of(
"%"));
295 if (addrFields.size() != 2 || faceUriFields.size() != 2) {
299 if (faceUriFields[1].size() > 2 && faceUriFields[1].compare(0, 2,
"25") == 0) {
304 return addrFields[0] == faceUriFields[0] &&
305 addrFields[1] == faceUriFields[1];
308 return checkAddressWithUri(addr, faceUri) && checkAddress(addr).first;
315 boost::asio::io_service& io, time::nanoseconds timeout)
const override 323 auto uri = make_shared<FaceUri>(faceUri);
324 boost::system::error_code ec;
328 if ((faceUri.
getScheme() == m_v4Scheme && !ipAddress.is_v4()) ||
329 (faceUri.
getScheme() == m_v6Scheme && !ipAddress.is_v6())) {
330 return onFailure(
"IPv4/v6 mismatch");
333 onDnsSuccess(uri, onSuccess, onFailure, ipAddress);
340 else if (faceUri.
getScheme() == m_v6Scheme) {
344 BOOST_ASSERT(faceUri.
getScheme() == m_baseScheme);
351 io, addressSelector, timeout);
358 uint16_t defaultUnicastPort = 6363,
359 uint16_t defaultMulticastPort = 56363)
360 : m_baseScheme(baseScheme)
361 , m_v4Scheme(baseScheme +
'4')
362 , m_v6Scheme(baseScheme +
'6')
363 , m_defaultUnicastPort(defaultUnicastPort)
364 , m_defaultMulticastPort(defaultMulticastPort)
370 onDnsSuccess(
const shared_ptr<FaceUri>& faceUri,
377 std::tie(isOk, reason) = this->checkAddress(ipAddress);
379 return onFailure(reason);
383 if (faceUri->getPort().empty()) {
384 port = ipAddress.is_multicast() ? m_defaultMulticastPort : m_defaultUnicastPort;
388 port = boost::lexical_cast<uint16_t>(faceUri->getPort());
390 catch (
const boost::bad_lexical_cast&) {
391 return onFailure(
"invalid port number '" + faceUri->getPort() +
"'");
395 FaceUri canonicalUri(
typename Protocol::endpoint(ipAddress, port));
396 BOOST_ASSERT(canonicalUri.isCanonical());
397 onSuccess(canonicalUri);
401 onDnsFailure(
const shared_ptr<FaceUri>& faceUri,
403 const std::string& reason)
const 412 virtual std::pair<bool, std::string>
419 unescapeHost(std::string host)
421 auto escapePos = host.find(
"%25");
422 if (escapePos != std::string::npos && escapePos < host.size() - 3) {
429 std::string m_baseScheme;
430 std::string m_v4Scheme;
431 std::string m_v6Scheme;
432 uint16_t m_defaultUnicastPort;
433 uint16_t m_defaultMulticastPort;
454 std::pair<bool, std::string>
457 if (ipAddress.is_multicast()) {
458 return {
false,
"cannot use multicast address"};
467 std::set<std::string>
476 if (!faceUri.
getPort().empty()) {
479 if (!faceUri.
getPath().empty()) {
491 boost::asio::io_service& io, time::nanoseconds timeout)
const override 495 return onFailure(
"invalid ethernet address '" + faceUri.
getHost() +
"'");
500 onSuccess(canonicalUri);
507 std::set<std::string>
523 boost::asio::io_service& io, time::nanoseconds timeout)
const override 525 if (faceUri.
getHost().empty()) {
526 onFailure(
"network interface name is missing");
529 if (!faceUri.
getPort().empty()) {
530 onFailure(
"port number is not allowed");
534 onFailure(
"path is not allowed");
540 onSuccess(canonicalUri);
547 std::set<std::string>
550 return {
"udp4+dev",
"udp6+dev"};
556 if (faceUri.
getPort().empty()) {
559 if (!faceUri.
getPath().empty()) {
569 boost::asio::io_service& io, time::nanoseconds timeout)
const override 575 onFailure(
"cannot canonize " + faceUri.
toString());
581 TcpCanonizeProvider*,
582 EtherCanonizeProvider*,
583 DevCanonizeProvider*,
592 : m_providerTable(providerTable)
596 template<
typename CP>
600 shared_ptr<CanonizeProvider> cp = make_shared<CP>();
601 auto schemes = cp->getSchemes();
602 BOOST_ASSERT(!schemes.empty());
604 for (
const auto& scheme : schemes) {
605 BOOST_ASSERT(m_providerTable.count(scheme) == 0);
606 m_providerTable[scheme] = cp;
614 static const CanonizeProvider*
618 if (providerTable.empty()) {
620 BOOST_ASSERT(!providerTable.empty());
623 auto it = providerTable.find(scheme);
624 return it == providerTable.end() ? nullptr : it->second.get();
648 boost::asio::io_service& io, time::nanoseconds timeout)
const 653 onFailure(
"scheme not supported");
661 onSuccess ? onSuccess : successNop,
662 onFailure ? onFailure : failureNop,
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, with colons or hyphens as separators.
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
bool operator!=(const FaceUri &rhs) const
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const override
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)
const std::string & getPort() const
get port
std::string toString(char sep=':') const
Converts the address to a human-readable string.
function< bool(const IpAddress &address)> AddressSelector
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
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const override
std::pair< bool, std::string > checkAddress(const dns::IpAddress &ipAddress) const override
when overriden in a subclass, check the IP address is allowable
std::string unescape(const std::string &str)
Decode a percent-encoded string.
std::set< std::string > getSchemes() const override
void canonize(const CanonizeSuccessCallback &onSuccess, const CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const
asynchronously convert this FaceUri to canonical form
represents the underlying protocol and address used by a Face
boost::asio::ip::address addressFromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IP address
represents an Ethernet hardware address
const std::string & getScheme() const
get scheme (protocol)
boost::asio::ip::address IpAddress
CanonizeProviderTableInitializer(CanonizeProviderTable &providerTable)
bool operator==(const FaceUri &rhs) const
std::set< std::string > getSchemes() const override
std::string to_string(const V &v)
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const override
bool isCanonical() const
determine whether this FaceUri is in canonical form
std::map< std::string, shared_ptr< CanonizeProvider > > CanonizeProviderTable
virtual void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const =0
const std::string & getPath() const
get path
boost::mpl::vector< UdpCanonizeProvider *, TcpCanonizeProvider *, EtherCanonizeProvider *, DevCanonizeProvider *, UdpDevCanonizeProvider * > CanonizeProviders
void asyncResolve(const std::string &host, const SuccessCallback &onSuccess, const ErrorCallback &onError, boost::asio::io_service &ioService, const AddressSelector &addressSelector, time::nanoseconds timeout)
Asynchronously resolve host.
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
void canonize(const FaceUri &faceUri, const FaceUri::CanonizeSuccessCallback &onSuccess, const FaceUri::CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const override
virtual std::set< std::string > getSchemes() const =0