rippled
Loading...
Searching...
No Matches
Port.cpp
1#include <xrpl/basics/BasicConfig.h>
2#include <xrpl/basics/contract.h>
3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/beast/core/LexicalCast.h>
5#include <xrpl/beast/net/IPEndpoint.h>
6#include <xrpl/beast/rfc2616.h>
7#include <xrpl/server/Port.h>
8
9#include <boost/algorithm/string/predicate.hpp>
10#include <boost/algorithm/string/trim.hpp>
11#include <boost/asio/ip/address.hpp>
12#include <boost/asio/ip/impl/network_v4.ipp>
13#include <boost/asio/ip/impl/network_v6.ipp>
14#include <boost/system/system_error.hpp>
15
16#include <cstdint>
17#include <exception>
18#include <ostream>
19#include <sstream>
20#include <stdexcept>
21#include <string>
22#include <vector>
23
24namespace xrpl {
25
26bool
28{
29 return protocol.count("peer") > 0 || protocol.count("https") > 0 || protocol.count("wss") > 0 ||
30 protocol.count("wss2") > 0;
31}
32
35{
37 for (auto iter = protocol.cbegin(); iter != protocol.cend(); ++iter)
38 s += (iter != protocol.cbegin() ? "," : "") + *iter;
39 return s;
40}
41
43operator<<(std::ostream& os, Port const& p)
44{
45 os << "'" << p.name << "' (ip=" << p.ip << ":" << p.port << ", ";
46
47 if (p.admin_nets_v4.size() || p.admin_nets_v6.size())
48 {
49 os << "admin nets:";
50 for (auto const& net : p.admin_nets_v4)
51 {
52 os << net.to_string();
53 os << ", ";
54 }
55 for (auto const& net : p.admin_nets_v6)
56 {
57 os << net.to_string();
58 os << ", ";
59 }
60 }
61
63 {
64 os << "secure_gateway nets:";
65 for (auto const& net : p.secure_gateway_nets_v4)
66 {
67 os << net.to_string();
68 os << ", ";
69 }
70 for (auto const& net : p.secure_gateway_nets_v6)
71 {
72 os << net.to_string();
73 os << ", ";
74 }
75 }
76
77 os << p.protocols() << ")";
78 return os;
79}
80
81//------------------------------------------------------------------------------
82
83static void
85 Section const& section,
86 std::string const& field,
87 std::ostream& log,
90{
91 auto const optResult = section.get(field);
92 if (!optResult)
93 return;
94
95 std::stringstream ss(*optResult);
96 std::string ip;
97
98 while (std::getline(ss, ip, ','))
99 {
100 boost::algorithm::trim(ip);
101 bool v4;
102 boost::asio::ip::network_v4 v4Net;
103 boost::asio::ip::network_v6 v6Net;
104
105 try
106 {
107 // First, check to see if 0.0.0.0 or ipv6 equivalent was configured,
108 // which means all IP addresses.
109 auto const addr = beast::IP::Endpoint::from_string_checked(ip);
110 if (addr)
111 {
112 if (is_unspecified(*addr))
113 {
114 nets4.push_back(boost::asio::ip::make_network_v4("0.0.0.0/0"));
115 nets6.push_back(boost::asio::ip::make_network_v6("::/0"));
116 // No reason to allow more IPs--it would be redundant.
117 break;
118 }
119
120 // The configured address is a single IP (or else addr would
121 // be unset). We need this to be a subnet, so append
122 // the number of network bits to make a subnet of 1,
123 // depending on type.
124 v4 = addr->is_v4();
125 std::string addressString = addr->to_string();
126 if (v4)
127 {
128 addressString += "/32";
129 v4Net = boost::asio::ip::make_network_v4(addressString);
130 }
131 else
132 {
133 addressString += "/128";
134 v6Net = boost::asio::ip::make_network_v6(addressString);
135 }
136 }
137 else
138 {
139 // Since addr is empty, assume that the entry is
140 // for a subnet which includes trailing /0-32 or /0-128
141 // depending on ip type.
142 // First, see if it's an ipv4 subnet. If not, try ipv6.
143 // If that throws, then there's nothing we can do with
144 // the entry.
145 try
146 {
147 v4Net = boost::asio::ip::make_network_v4(ip);
148 v4 = true;
149 }
150 catch (boost::system::system_error const&)
151 {
152 v6Net = boost::asio::ip::make_network_v6(ip);
153 v4 = false;
154 }
155 }
156
157 // Confirm that the address entry is the same as the subnet's
158 // underlying network address.
159 // 10.1.2.3/24 makes no sense. The underlying network address
160 // is 10.1.2.0/24.
161 if (v4)
162 {
163 if (v4Net != v4Net.canonical())
164 {
165 log << "The configured subnet " << v4Net.to_string()
166 << " is not the same as the network address, which is " << v4Net.canonical().to_string();
167 Throw<std::exception>();
168 }
169 nets4.push_back(v4Net);
170 }
171 else
172 {
173 if (v6Net != v6Net.canonical())
174 {
175 log << "The configured subnet " << v6Net.to_string()
176 << " is not the same as the network address, which is " << v6Net.canonical().to_string();
177 Throw<std::exception>();
178 }
179 nets6.push_back(v6Net);
180 }
181 }
182 catch (boost::system::system_error const& e)
183 {
184 log << "Invalid value '" << ip << "' for key '" << field << "' in [" << section.name() << "]: " << e.what();
185 Throw<std::exception>();
186 }
187 }
188}
189
190void
191parse_Port(ParsedPort& port, Section const& section, std::ostream& log)
192{
193 port.name = section.name();
194 {
195 auto const optResult = section.get("ip");
196 if (optResult)
197 {
198 try
199 {
200 port.ip = boost::asio::ip::make_address(*optResult);
201 }
202 catch (std::exception const&)
203 {
204 log << "Invalid value '" << *optResult << "' for key 'ip' in [" << section.name() << "]";
205 Rethrow();
206 }
207 }
208 }
209
210 {
211 auto const optResult = section.get("port");
212 if (optResult)
213 {
214 try
215 {
216 port.port = beast::lexicalCastThrow<std::uint16_t>(*optResult);
217
218 // Port 0 is not supported for [server]
219 if ((*port.port == 0) && (port.name == "server"))
220 Throw<std::exception>();
221 }
222 catch (std::exception const&)
223 {
224 log << "Invalid value '" << *optResult << "' for key "
225 << "'port' in [" << section.name() << "]";
226 Rethrow();
227 }
228 }
229 }
230
231 {
232 auto const optResult = section.get("protocol");
233 if (optResult)
234 {
235 for (auto const& s : beast::rfc2616::split_commas(optResult->begin(), optResult->end()))
236 port.protocol.insert(s);
237 }
238 }
239
240 {
241 auto const lim = get(section, "limit", "unlimited");
242
243 if (!boost::iequals(lim, "unlimited"))
244 {
245 try
246 {
247 port.limit = safe_cast<int>(beast::lexicalCastThrow<std::uint16_t>(lim));
248 }
249 catch (std::exception const&)
250 {
251 log << "Invalid value '" << lim << "' for key "
252 << "'limit' in [" << section.name() << "]";
253 Rethrow();
254 }
255 }
256 }
257
258 {
259 auto const optResult = section.get("send_queue_limit");
260 if (optResult)
261 {
262 try
263 {
264 port.ws_queue_limit = beast::lexicalCastThrow<std::uint16_t>(*optResult);
265
266 // Queue must be greater than 0
267 if (port.ws_queue_limit == 0)
268 Throw<std::exception>();
269 }
270 catch (std::exception const&)
271 {
272 log << "Invalid value '" << *optResult << "' for key "
273 << "'send_queue_limit' in [" << section.name() << "]";
274 Rethrow();
275 }
276 }
277 else
278 {
279 // Default Websocket send queue size limit
280 port.ws_queue_limit = 100;
281 }
282 }
283
284 populate(section, "admin", log, port.admin_nets_v4, port.admin_nets_v6);
285 populate(section, "secure_gateway", log, port.secure_gateway_nets_v4, port.secure_gateway_nets_v6);
286
287 set(port.user, "user", section);
288 set(port.password, "password", section);
289 set(port.admin_user, "admin_user", section);
290 set(port.admin_password, "admin_password", section);
291 set(port.ssl_key, "ssl_key", section);
292 set(port.ssl_cert, "ssl_cert", section);
293 set(port.ssl_chain, "ssl_chain", section);
294 set(port.ssl_ciphers, "ssl_ciphers", section);
295
296 port.pmd_options.server_enable = section.value_or("permessage_deflate", true);
297 port.pmd_options.client_max_window_bits = section.value_or("client_max_window_bits", 15);
298 port.pmd_options.server_max_window_bits = section.value_or("server_max_window_bits", 15);
299 port.pmd_options.client_no_context_takeover = section.value_or("client_no_context_takeover", false);
300 port.pmd_options.server_no_context_takeover = section.value_or("server_no_context_takeover", false);
301 port.pmd_options.compLevel = section.value_or("compress_level", 8);
302 port.pmd_options.memLevel = section.value_or("memory_level", 4);
303}
304
305} // namespace xrpl
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Holds a collection of configuration values.
Definition BasicConfig.h:25
std::optional< T > get(std::string const &name) const
std::string const & name() const
Returns the name of this section.
Definition BasicConfig.h:41
T value_or(std::string const &name, T const &other) const
Returns a value if present, else another value.
T getline(T... args)
T insert(T... args)
Result split_commas(FwdIt first, FwdIt last)
Definition rfc2616.h:175
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void parse_Port(ParsedPort &port, Section const &section, std::ostream &log)
Definition Port.cpp:191
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
static void populate(Section const &section, std::string const &field, std::ostream &log, std::vector< boost::asio::ip::network_v4 > &nets4, std::vector< boost::asio::ip::network_v6 > &nets6)
Definition Port.cpp:84
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:613
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:29
T push_back(T... args)
T size(T... args)
std::set< std::string, boost::beast::iless > protocol
Definition Port.h:83
boost::beast::websocket::permessage_deflate pmd_options
Definition Port.h:92
std::optional< boost::asio::ip::address > ip
Definition Port.h:96
std::vector< boost::asio::ip::network_v6 > secure_gateway_nets_v6
Definition Port.h:101
std::string admin_password
Definition Port.h:87
std::vector< boost::asio::ip::network_v4 > secure_gateway_nets_v4
Definition Port.h:100
std::string ssl_chain
Definition Port.h:90
std::string ssl_key
Definition Port.h:88
std::string name
Definition Port.h:82
std::uint16_t ws_queue_limit
Definition Port.h:94
std::vector< boost::asio::ip::network_v4 > admin_nets_v4
Definition Port.h:98
std::string user
Definition Port.h:84
std::optional< std::uint16_t > port
Definition Port.h:97
std::string ssl_ciphers
Definition Port.h:91
std::string password
Definition Port.h:85
std::vector< boost::asio::ip::network_v6 > admin_nets_v6
Definition Port.h:99
std::string admin_user
Definition Port.h:86
std::string ssl_cert
Definition Port.h:89
Configuration information for a Server listening port.
Definition Port.h:31
std::vector< boost::asio::ip::network_v4 > admin_nets_v4
Definition Port.h:38
std::string protocols() const
Definition Port.cpp:34
std::vector< boost::asio::ip::network_v6 > secure_gateway_nets_v6
Definition Port.h:41
bool secure() const
Definition Port.cpp:27
boost::asio::ip::address ip
Definition Port.h:35
std::vector< boost::asio::ip::network_v4 > secure_gateway_nets_v4
Definition Port.h:40
std::string name
Definition Port.h:34
std::vector< boost::asio::ip::network_v6 > admin_nets_v6
Definition Port.h:39
std::uint16_t port
Definition Port.h:36