rippled
Loading...
Searching...
No Matches
IPEndpoint.cpp
1#include <xrpl/beast/net/IPAddress.h>
2#include <xrpl/beast/net/IPEndpoint.h>
3
4#include <boost/algorithm/string/trim.hpp>
5#include <boost/asio/ip/address.hpp>
6#include <boost/asio/ip/address_v4.hpp>
7#include <boost/system/detail/error_code.hpp>
8
9#include <cctype>
10#include <ios>
11#include <istream>
12#include <optional>
13#include <sstream>
14#include <string>
15
16namespace beast {
17namespace IP {
18
19Endpoint::Endpoint() : m_port(0)
20{
21}
22
23Endpoint::Endpoint(Address const& addr, Port port) : m_addr(addr), m_port(port)
24{
25}
26
29{
30 if (s.size() <= 64)
31 {
32 std::stringstream is(boost::trim_copy(s));
33 Endpoint endpoint;
34 is >> endpoint;
35 if (!is.fail() && is.rdbuf()->in_avail() == 0)
36 return endpoint;
37 }
38 return {};
39}
40
43{
44 if (std::optional<Endpoint> const result = from_string_checked(s))
45 return *result;
46 return Endpoint{};
47}
48
51{
53 s.reserve((address().is_v6() ? INET6_ADDRSTRLEN - 1 : 15) + (port() == 0 ? 0 : 6 + (address().is_v6() ? 2 : 0)));
54
55 if (port() != 0 && address().is_v6())
56 s += '[';
57 s += address().to_string();
58 if (port())
59 {
60 if (address().is_v6())
61 s += ']';
62 s += ":" + std::to_string(port());
63 }
64
65 return s;
66}
67
68bool
69operator==(Endpoint const& lhs, Endpoint const& rhs)
70{
71 return lhs.address() == rhs.address() && lhs.port() == rhs.port();
72}
73
74bool
75operator<(Endpoint const& lhs, Endpoint const& rhs)
76{
77 if (lhs.address() < rhs.address())
78 return true;
79 if (lhs.address() > rhs.address())
80 return false;
81 return lhs.port() < rhs.port();
82}
83
84//------------------------------------------------------------------------------
85
88{
89 std::string addrStr;
90 // valid addresses only need INET6_ADDRSTRLEN-1 chars, but allow the extra
91 // char to check for invalid lengths
92 addrStr.reserve(INET6_ADDRSTRLEN);
93 char i{0};
94 char readTo{0};
95 is.get(i);
96 if (i == '[') // we are an IPv6 endpoint
97 readTo = ']';
98 else
99 addrStr += i;
100
101 while (is && is.rdbuf()->in_avail() > 0 && is.get(i))
102 {
103 // NOTE: There is a legacy data format
104 // that allowed space to be used as address / port separator
105 // so we continue to honor that here by assuming we are at the end
106 // of the address portion if we hit a space (or the separator
107 // we were expecting to see)
108 if (isspace(static_cast<unsigned char>(i)) || (readTo && i == readTo))
109 break;
110
111 if ((i == '.') || (i >= '0' && i <= ':') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F'))
112 {
113 addrStr += i;
114
115 // don't exceed a reasonable length...
116 if (addrStr.size() == INET6_ADDRSTRLEN || (readTo && readTo == ':' && addrStr.size() > 15))
117 {
118 is.setstate(std::ios_base::failbit);
119 return is;
120 }
121
122 if (!readTo && (i == '.' || i == ':'))
123 {
124 // if we see a dot first, must be IPv4
125 // otherwise must be non-bracketed IPv6
126 readTo = (i == '.') ? ':' : ' ';
127 }
128 }
129 else // invalid char
130 {
131 is.unget();
132 is.setstate(std::ios_base::failbit);
133 return is;
134 }
135 }
136
137 if (readTo == ']' && is.rdbuf()->in_avail() > 0)
138 {
139 is.get(i);
140 if (!(isspace(static_cast<unsigned char>(i)) || i == ':'))
141 {
142 is.unget();
143 is.setstate(std::ios_base::failbit);
144 return is;
145 }
146 }
147
148 boost::system::error_code ec;
149 auto addr = boost::asio::ip::make_address(addrStr, ec);
150 if (ec)
151 {
152 is.setstate(std::ios_base::failbit);
153 return is;
154 }
155
156 if (is.rdbuf()->in_avail() > 0)
157 {
158 Port port;
159 is >> port;
160 if (is.fail())
161 return is;
162 endpoint = Endpoint(addr, port);
163 }
164 else
165 endpoint = Endpoint(addr);
166
167 return is;
168}
169
170} // namespace IP
171} // namespace beast
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:55
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Endpoint()
Create an unspecified endpoint.
static Endpoint from_string(std::string const &s)
bool is_v6() const
Definition IPEndpoint.h:68
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:41
std::string to_string() const
Returns a string representing the endpoint.
T fail(T... args)
T get(T... args)
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
boost::asio::ip::address Address
Definition IPAddress.h:19
bool operator==(Endpoint const &lhs, Endpoint const &rhs)
T rdbuf(T... args)
T reserve(T... args)
T setstate(T... args)
T size(T... args)
T to_string(T... args)
T unget(T... args)