Files
xahaud/src/ripple/basics/impl/StringUtilities.cpp
Edward Hennis 2c71802e38 Propagate validator lists (VLs or UNLs) over the peer network:
* Whenever a node downloads a new VL, send it to all peers that
  haven't already sent or received it. It also saves it to the
  database_dir as a Json text file named "cache." plus the public key of
  the list signer. Any files that exist for public keys provided in
  [validator_list_keys] will be loaded and processed if any download
  from [validator_list_sites] fails or no [validator_list_sites] are
  configured.
* Whenever a node receives a broadcast VL message, it treats it as if
  it had downloaded it on it's own, broadcasting to other peers as
  described above.
* Because nodes normally download the VL once every 5 minutes, a single
  node downloading a VL with an updated sequence number could
  potentially propagate across a large part of a well-connected network
  before any other nodes attempt to download, decreasing the amount of
  time that different parts of the network are using different VLs.
* Send all of our current valid VLs to new peers on connection.
  This is probably the "noisiest" part of this change, but will give
  poorly connected or poorly networked nodes the best chance of syncing
  quickly. Nodes which have no http(s) access configured or available
  can get a VL with no extra effort.
* Requests on the peer port to the /vl/<pubkey> endpoint will return
  that VL in the same JSON format as is used to download now, IF the
  node trusts and has a valid instance of that VL.
* Upgrade protocol version to 2.1. VLs will only be sent to 2.1 and
  higher nodes.
* Resolves #2953
2020-02-12 10:19:23 -08:00

120 lines
3.5 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/contract.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/ToString.h>
#include <ripple/beast/core/LexicalCast.h>
#include <boost/algorithm/string.hpp>
#include <ripple/beast/net/IPEndpoint.h>
#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;
}
} // ripple