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>));
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;
231 class CanonizeProvider : noncopyable
235 ~CanonizeProvider() =
default;
237 virtual std::set<std::string>
238 getSchemes()
const = 0;
247 boost::asio::io_service& io, time::nanoseconds timeout)
const = 0;
250 template<
typename Protocol>
251 class IpHostCanonizeProvider :
public CanonizeProvider
254 std::set<std::string>
255 getSchemes()
const override 257 return {m_baseScheme, m_v4Scheme, m_v6Scheme};
263 if (faceUri.
getPort().empty()) {
266 if (!faceUri.
getPath().empty()) {
270 boost::system::error_code ec;
271 auto addr = boost::asio::ip::address::from_string(unescapeHost(faceUri.
getHost()), ec);
276 bool hasCorrectScheme = (faceUri.
getScheme() == m_v4Scheme && addr.is_v4()) ||
277 (faceUri.
getScheme() == m_v6Scheme && addr.is_v6());
278 if (!hasCorrectScheme) {
282 auto checkAddressWithUri = [] (
const boost::asio::ip::address& addr,
283 const FaceUri& faceUri) ->
bool {
284 if (addr.is_v4() || !addr.to_v6().is_link_local()) {
285 return addr.to_string() == faceUri.
getHost();
288 std::vector<std::string> addrFields, faceUriFields;
289 std::string addrString = addr.to_string();
290 std::string faceUriString = faceUri.
getHost();
292 boost::algorithm::split(addrFields, addrString, boost::is_any_of(
"%"));
293 boost::algorithm::split(faceUriFields, faceUriString, boost::is_any_of(
"%"));
294 if (addrFields.size() != 2 || faceUriFields.size() != 2) {
298 if (faceUriFields[1].size() > 2 && faceUriFields[1].compare(0, 2,
"25") == 0) {
303 return addrFields[0] == faceUriFields[0] &&
304 addrFields[1] == faceUriFields[1];
307 return checkAddressWithUri(addr, faceUri) && checkAddress(addr).first;
314 boost::asio::io_service& io, time::nanoseconds timeout)
const override 322 auto uri = make_shared<FaceUri>(faceUri);
323 boost::system::error_code ec;
324 auto ipAddress = boost::asio::ip::address::from_string(unescapeHost(faceUri.
getHost()), ec);
327 if ((faceUri.
getScheme() == m_v4Scheme && !ipAddress.is_v4()) ||
328 (faceUri.
getScheme() == m_v6Scheme && !ipAddress.is_v6())) {
329 return onFailure(
"IPv4/v6 mismatch");
332 onDnsSuccess(uri, onSuccess, onFailure, ipAddress);
339 else if (faceUri.
getScheme() == m_v6Scheme) {
343 BOOST_ASSERT(faceUri.
getScheme() == m_baseScheme);
348 bind(&IpHostCanonizeProvider<Protocol>::onDnsSuccess,
this, uri, onSuccess, onFailure, _1),
349 bind(&IpHostCanonizeProvider<Protocol>::onDnsFailure,
this, uri, onFailure, _1),
350 io, addressSelector, timeout);
356 IpHostCanonizeProvider(
const std::string& baseScheme,
357 uint16_t defaultUnicastPort = 6363,
358 uint16_t defaultMulticastPort = 56363)
359 : m_baseScheme(baseScheme)
360 , m_v4Scheme(baseScheme +
'4')
361 , m_v6Scheme(baseScheme +
'6')
362 , m_defaultUnicastPort(defaultUnicastPort)
363 , m_defaultMulticastPort(defaultMulticastPort)
369 onDnsSuccess(
const shared_ptr<FaceUri>& faceUri,
376 std::tie(isOk, reason) = this->checkAddress(ipAddress);
378 return onFailure(reason);
382 if (faceUri->getPort().empty()) {
383 port = ipAddress.is_multicast() ? m_defaultMulticastPort : m_defaultUnicastPort;
387 port = boost::lexical_cast<uint16_t>(faceUri->getPort());
389 catch (
const boost::bad_lexical_cast&) {
390 return onFailure(
"invalid port number '" + faceUri->getPort() +
"'");
394 FaceUri canonicalUri(
typename Protocol::endpoint(ipAddress, port));
395 BOOST_ASSERT(canonicalUri.isCanonical());
396 onSuccess(canonicalUri);
400 onDnsFailure(
const shared_ptr<FaceUri>& faceUri,
402 const std::string& reason)
const 411 virtual std::pair<bool, std::string>
418 unescapeHost(std::string host)
420 auto escapePos = host.find(
"%25");
421 if (escapePos != std::string::npos && escapePos < host.size() - 3) {
428 std::string m_baseScheme;
429 std::string m_v4Scheme;
430 std::string m_v6Scheme;
431 uint16_t m_defaultUnicastPort;
432 uint16_t m_defaultMulticastPort;
435 class UdpCanonizeProvider :
public IpHostCanonizeProvider<boost::asio::ip::udp>
438 UdpCanonizeProvider()
439 : IpHostCanonizeProvider(
"udp")
444 class TcpCanonizeProvider :
public IpHostCanonizeProvider<boost::asio::ip::tcp>
447 TcpCanonizeProvider()
448 : IpHostCanonizeProvider(
"tcp")
453 std::pair<bool, std::string>
456 if (ipAddress.is_multicast()) {
457 return {
false,
"cannot use multicast address"};
463 class EtherCanonizeProvider :
public CanonizeProvider
466 std::set<std::string>
467 getSchemes()
const override 475 if (!faceUri.
getPort().empty()) {
478 if (!faceUri.
getPath().empty()) {
490 boost::asio::io_service& io, time::nanoseconds timeout)
const override 494 return onFailure(
"invalid ethernet address '" + faceUri.
getHost() +
"'");
498 BOOST_ASSERT(canonicalUri.isCanonical());
499 onSuccess(canonicalUri);
503 class DevCanonizeProvider :
public CanonizeProvider
506 std::set<std::string>
507 getSchemes()
const override 522 boost::asio::io_service& io, time::nanoseconds timeout)
const override 524 if (faceUri.
getHost().empty()) {
525 onFailure(
"network interface name is missing");
528 if (!faceUri.
getPort().empty()) {
529 onFailure(
"port number is not allowed");
533 onFailure(
"path is not allowed");
539 onSuccess(canonicalUri);
543 class UdpDevCanonizeProvider :
public CanonizeProvider
546 std::set<std::string>
547 getSchemes()
const override 549 return {
"udp4+dev",
"udp6+dev"};
555 if (faceUri.
getPort().empty()) {
558 if (!faceUri.
getPath().empty()) {
568 boost::asio::io_service& io, time::nanoseconds timeout)
const override 574 onFailure(
"cannot canonize " + faceUri.
toString());
580 TcpCanonizeProvider*,
581 EtherCanonizeProvider*,
582 DevCanonizeProvider*,
583 UdpDevCanonizeProvider*>;
586 class CanonizeProviderTableInitializer
591 : m_providerTable(providerTable)
595 template<
typename CP>
599 shared_ptr<CanonizeProvider> cp = make_shared<CP>();
600 auto schemes = cp->getSchemes();
601 BOOST_ASSERT(!schemes.empty());
603 for (
const auto& scheme : schemes) {
604 BOOST_ASSERT(m_providerTable.count(scheme) == 0);
605 m_providerTable[scheme] = cp;
613 static const CanonizeProvider*
617 if (providerTable.empty()) {
618 boost::mpl::for_each<CanonizeProviders>(CanonizeProviderTableInitializer(providerTable));
619 BOOST_ASSERT(!providerTable.empty());
622 auto it = providerTable.find(scheme);
623 return it == providerTable.end() ?
nullptr : it->second.get();
641 return cp->isCanonical(*
this);
647 boost::asio::io_service& io, time::nanoseconds timeout)
const 652 onFailure(
"scheme not supported");
658 onSuccess ? onSuccess : [] (
auto&&) {},
659 onFailure ? onFailure : [] (
auto&&) {},
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.
function< void(const std::string &reason)> CanonizeFailureCallback
static FaceUri fromFd(int fd)
create fd FaceUri from file descriptor
function< void(const FaceUri &)> CanonizeSuccessCallback
static bool canCanonize(const std::string &scheme)
static const CanonizeProvider * getCanonizeProvider(const std::string &scheme)
function< bool(const IpAddress &address)> AddressSelector
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
bool operator!=(const FaceUri &rhs) const
void canonize(const CanonizeSuccessCallback &onSuccess, const CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const
asynchronously convert this FaceUri to canonical form
bool parse(const std::string &uri)
exception-safe parsing
std::string toString() const
write as a string
std::string unescape(const std::string &str)
Decode a percent-encoded string.
std::string toString(char sep= ':') const
Converts the address to a human-readable string.
represents the underlying protocol and address used by a Face
std::map< std::string, shared_ptr< CanonizeProvider >> CanonizeProviderTable
represents an Ethernet hardware address
bool isCanonical() const
determine whether this FaceUri is in canonical form
bool operator==(const FaceUri &rhs) const
const std::string & getScheme() const
get scheme (protocol)
boost::asio::ip::address IpAddress
std::string to_string(const V &v)
const std::string & getHost() const
get host (domain)
const std::string & getPort() const
get port
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
const std::string & getPath() const
get path
friend std::ostream & operator<<(std::ostream &os, const FaceUri &uri)