mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 19:25:51 +00:00
Add alternate form string parsing to IPEndpoint
This commit is contained in:
@@ -235,6 +235,14 @@ public:
|
||||
*/
|
||||
static IPEndpoint from_string (std::string const& s);
|
||||
|
||||
/** Create an IPEndpoint from a string.
|
||||
If a parsing error occurs, the endpoint will be empty.
|
||||
This recognizes an alternate form of the text. Instead of a colon
|
||||
separating the optional port specification, any amount of whitespace
|
||||
is allowed.
|
||||
*/
|
||||
static IPEndpoint from_string_altform (std::string const& s);
|
||||
|
||||
/** Copy assign an IPv4 address.
|
||||
The port is set to zero.
|
||||
*/
|
||||
|
||||
@@ -500,6 +500,61 @@ std::istream& operator>> (std::istream &is, IPEndpoint& ep)
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
IPEndpoint IPEndpoint::from_string_altform (std::string const& s)
|
||||
{
|
||||
// Accept the regular form if it parses
|
||||
{
|
||||
IPEndpoint ep (IPEndpoint::from_string (s));
|
||||
if (! ep.empty())
|
||||
return ep;
|
||||
}
|
||||
|
||||
// Now try the alt form
|
||||
std::stringstream is (s);
|
||||
|
||||
IPEndpoint::V4 v4;
|
||||
is >> v4;
|
||||
if (! is.fail())
|
||||
{
|
||||
IPEndpoint ep (v4);
|
||||
|
||||
if (is.rdbuf()->in_avail()>0)
|
||||
{
|
||||
if (! parse::expect (is, ' '))
|
||||
return IPEndpoint();
|
||||
|
||||
while (is.rdbuf()->in_avail()>0)
|
||||
{
|
||||
char c;
|
||||
is.get(c);
|
||||
if (c != ' ')
|
||||
{
|
||||
is.unget();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint16 port;
|
||||
is >> port;
|
||||
if (is.fail())
|
||||
return IPEndpoint();
|
||||
|
||||
return ep.withPort (port);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Just an address with no port
|
||||
return ep;
|
||||
}
|
||||
}
|
||||
|
||||
// Could be V6 here...
|
||||
|
||||
return IPEndpoint();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int compare (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs)
|
||||
{
|
||||
if (lhs.value < rhs.value)
|
||||
|
||||
Reference in New Issue
Block a user