Files
rippled/src/ripple/basics/impl/StringUtilities.cpp
Nik Bougalis d282b0bf85 Report server domain to other servers:
This commit introduces a new configuration option that server
operators can set. The value is communicated to other servers
and is also reported via the `server_info` API.

The value is meant to allow third-party applications or tools
to group servers together. For example, a tool that visualizes
the network's topology can group servers together.

Similar to the "Domain" field in validator manifests, an operator
can claim any domain. Prior to relying on the value returned, the
domain should be verified by retrieving the xrp-ledger.toml file
from the domain and looking for the server's public key in the
`nodes` array.
2020-10-14 11:17:44 -07:00

151 lines
4.6 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/ToString.h>
#include <ripple/basics/contract.h>
#include <ripple/beast/core/LexicalCast.h>
#include <ripple/beast/net/IPEndpoint.h>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <algorithm>
#include <cstdarg>
namespace ripple {
uint64_t
uintFromHex(std::string const& strSrc)
{
uint64_t uValue(0);
if (strSrc.size() > 16)
Throw<std::invalid_argument>("overlong 64-bit value");
for (auto c : strSrc)
{
int ret = charUnHex(c);
if (ret == -1)
Throw<std::invalid_argument>("invalid hex digit");
uValue = (uValue << 4) | ret;
}
return uValue;
}
bool
parseUrl(parsedURL& pUrl, std::string const& strUrl)
{
// scheme://username:password@hostname:port/rest
static boost::regex reUrl(
"(?i)\\`\\s*"
// required scheme
"([[:alpha:]][-+.[:alpha:][:digit:]]*?):"
// We choose to support only URIs whose `hier-part` has the form
// `"//" authority path-abempty`.
"//"
// optional userinfo
"(?:([^:@/]*?)(?::([^@/]*?))?@)?"
// optional host
"([[:digit:]:]*[[:digit:]]|\\[[^]]+\\]|[^:/?#]*?)"
// optional port
"(?::([[:digit:]]+))?"
// optional path
"(/.*)?"
"\\s*?\\'");
boost::smatch smMatch;
// Bail if there is no match.
try
{
if (!boost::regex_match(strUrl, smMatch, reUrl))
return false;
}
catch (...)
{
return false;
}
pUrl.scheme = smMatch[1];
boost::algorithm::to_lower(pUrl.scheme);
pUrl.username = smMatch[2];
pUrl.password = smMatch[3];
const std::string domain = smMatch[4];
// We need to use Endpoint to parse the domain to
// strip surrounding brackets from IPv6 addresses,
// e.g. [::1] => ::1.
const auto result = beast::IP::Endpoint::from_string_checked(domain);
pUrl.domain = result ? result->address().to_string() : domain;
const std::string port = smMatch[5];
if (!port.empty())
{
pUrl.port = beast::lexicalCast<std::uint16_t>(port);
}
pUrl.path = smMatch[6];
return true;
}
std::string
trim_whitespace(std::string str)
{
boost::trim(str);
return str;
}
boost::optional<std::uint64_t>
to_uint64(std::string const& s)
{
std::uint64_t result;
if (beast::lexicalCastChecked(result, s))
return result;
return boost::none;
}
bool
isProperlyFormedTomlDomain(std::string const& domain)
{
// The domain must be between 4 and 128 characters long
if (domain.size() < 4 || domain.size() > 128)
return false;
// This regular expression should do a decent job of weeding out
// obviously wrong domain names but it isn't perfect. It does not
// really support IDNs. If this turns out to be an issue, a more
// thorough regex can be used or this check can just be removed.
static boost::regex const re(
"^" // Beginning of line
"(" // Beginning of a segment
"(?!-)" // - must not begin with '-'
"[a-zA-Z0-9-]{1,63}" // - only alphanumeric and '-'
"(?<!-)" // - must not end with '-'
"\\." // segment separator
")+" // 1 or more segments
"[A-Za-z]{2,63}" // TLD
"$" // End of line
,
boost::regex_constants::optimize);
return boost::regex_match(domain, re);
}
} // namespace ripple