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(
54 (address().is_v6() ? INET6_ADDRSTRLEN - 1 : 15) +
55 (port() == 0 ? 0 : 6 + (address().is_v6() ? 2 : 0)));
56
57 if (port() != 0 && address().is_v6())
58 s += '[';
59 s += address().to_string();
60 if (port())
61 {
62 if (address().is_v6())
63 s += ']';
64 s += ":" + std::to_string(port());
65 }
66
67 return s;
68}
69
70bool
71operator==(Endpoint const& lhs, Endpoint const& rhs)
72{
73 return lhs.address() == rhs.address() && lhs.port() == rhs.port();
74}
75
76bool
77operator<(Endpoint const& lhs, Endpoint const& rhs)
78{
79 if (lhs.address() < rhs.address())
80 return true;
81 if (lhs.address() > rhs.address())
82 return false;
83 return lhs.port() < rhs.port();
84}
85
86//------------------------------------------------------------------------------
87
90{
91 std::string addrStr;
92 // valid addresses only need INET6_ADDRSTRLEN-1 chars, but allow the extra
93 // char to check for invalid lengths
94 addrStr.reserve(INET6_ADDRSTRLEN);
95 char i{0};
96 char readTo{0};
97 is.get(i);
98 if (i == '[') // we are an IPv6 endpoint
99 readTo = ']';
100 else
101 addrStr += i;
102
103 while (is && is.rdbuf()->in_avail() > 0 && is.get(i))
104 {
105 // NOTE: There is a legacy data format
106 // that allowed space to be used as address / port separator
107 // so we continue to honor that here by assuming we are at the end
108 // of the address portion if we hit a space (or the separator
109 // we were expecting to see)
110 if (isspace(static_cast<unsigned char>(i)) || (readTo && i == readTo))
111 break;
112
113 if ((i == '.') || (i >= '0' && i <= ':') || (i >= 'a' && i <= 'f') ||
114 (i >= 'A' && i <= 'F'))
115 {
116 addrStr += i;
117
118 // don't exceed a reasonable length...
119 if (addrStr.size() == INET6_ADDRSTRLEN ||
120 (readTo && readTo == ':' && addrStr.size() > 15))
121 {
122 is.setstate(std::ios_base::failbit);
123 return is;
124 }
125
126 if (!readTo && (i == '.' || i == ':'))
127 {
128 // if we see a dot first, must be IPv4
129 // otherwise must be non-bracketed IPv6
130 readTo = (i == '.') ? ':' : ' ';
131 }
132 }
133 else // invalid char
134 {
135 is.unget();
136 is.setstate(std::ios_base::failbit);
137 return is;
138 }
139 }
140
141 if (readTo == ']' && is.rdbuf()->in_avail() > 0)
142 {
143 is.get(i);
144 if (!(isspace(static_cast<unsigned char>(i)) || i == ':'))
145 {
146 is.unget();
147 is.setstate(std::ios_base::failbit);
148 return is;
149 }
150 }
151
152 boost::system::error_code ec;
153 auto addr = boost::asio::ip::make_address(addrStr, ec);
154 if (ec)
155 {
156 is.setstate(std::ios_base::failbit);
157 return is;
158 }
159
160 if (is.rdbuf()->in_avail() > 0)
161 {
162 Port port;
163 is >> port;
164 if (is.fail())
165 return is;
166 endpoint = Endpoint(addr, port);
167 }
168 else
169 endpoint = Endpoint(addr);
170
171 return is;
172}
173
174} // namespace IP
175} // namespace beast
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:56
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:69
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:42
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:20
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)