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 "
167 << v4Net.canonical().to_string();
168 Throw<std::exception>();
169 }
170 nets4.push_back(v4Net);
171 }
172 else
173 {
174 if (v6Net != v6Net.canonical())
175 {
176 log << "The configured subnet " << v6Net.to_string()
177 << " is not the same as the network address, which is "
178 << v6Net.canonical().to_string();
179 Throw<std::exception>();
180 }
181 nets6.push_back(v6Net);
182 }
183 }
184 catch (boost::system::system_error const& e)
185 {
186 log << "Invalid value '" << ip << "' for key '" << field << "' in [" << section.name()
187 << "]: " << e.what();
188 Throw<std::exception>();
189 }
190 }
191}
192
193void
194parse_Port(ParsedPort& port, Section const& section, std::ostream& log)
195{
196 port.name = section.name();
197 {
198 auto const optResult = section.get("ip");
199 if (optResult)
200 {
201 try
202 {
203 port.ip = boost::asio::ip::make_address(*optResult);
204 }
205 catch (std::exception const&)
206 {
207 log << "Invalid value '" << *optResult << "' for key 'ip' in [" << section.name()
208 << "]";
209 Rethrow();
210 }
211 }
212 }
213
214 {
215 auto const optResult = section.get("port");
216 if (optResult)
217 {
218 try
219 {
220 port.port = beast::lexicalCastThrow<std::uint16_t>(*optResult);
221
222 // Port 0 is not supported for [server]
223 if ((*port.port == 0) && (port.name == "server"))
224 Throw<std::exception>();
225 }
226 catch (std::exception const&)
227 {
228 log << "Invalid value '" << *optResult << "' for key "
229 << "'port' in [" << section.name() << "]";
230 Rethrow();
231 }
232 }
233 }
234
235 {
236 auto const optResult = section.get("protocol");
237 if (optResult)
238 {
239 for (auto const& s : beast::rfc2616::split_commas(optResult->begin(), optResult->end()))
240 port.protocol.insert(s);
241 }
242 }
243
244 {
245 auto const lim = get(section, "limit", "unlimited");
246
247 if (!boost::iequals(lim, "unlimited"))
248 {
249 try
250 {
251 port.limit = safe_cast<int>(beast::lexicalCastThrow<std::uint16_t>(lim));
252 }
253 catch (std::exception const&)
254 {
255 log << "Invalid value '" << lim << "' for key "
256 << "'limit' in [" << section.name() << "]";
257 Rethrow();
258 }
259 }
260 }
261
262 {
263 auto const optResult = section.get("send_queue_limit");
264 if (optResult)
265 {
266 try
267 {
268 port.ws_queue_limit = beast::lexicalCastThrow<std::uint16_t>(*optResult);
269
270 // Queue must be greater than 0
271 if (port.ws_queue_limit == 0)
272 Throw<std::exception>();
273 }
274 catch (std::exception const&)
275 {
276 log << "Invalid value '" << *optResult << "' for key "
277 << "'send_queue_limit' in [" << section.name() << "]";
278 Rethrow();
279 }
280 }
281 else
282 {
283 // Default Websocket send queue size limit
284 port.ws_queue_limit = 100;
285 }
286 }
287
288 populate(section, "admin", log, port.admin_nets_v4, port.admin_nets_v6);
289 populate(
290 section, "secure_gateway", log, port.secure_gateway_nets_v4, port.secure_gateway_nets_v6);
291
292 set(port.user, "user", section);
293 set(port.password, "password", section);
294 set(port.admin_user, "admin_user", section);
295 set(port.admin_password, "admin_password", section);
296 set(port.ssl_key, "ssl_key", section);
297 set(port.ssl_cert, "ssl_cert", section);
298 set(port.ssl_chain, "ssl_chain", section);
299 set(port.ssl_ciphers, "ssl_ciphers", section);
300
301 port.pmd_options.server_enable = section.value_or("permessage_deflate", true);
302 port.pmd_options.client_max_window_bits = section.value_or("client_max_window_bits", 15);
303 port.pmd_options.server_max_window_bits = section.value_or("server_max_window_bits", 15);
304 port.pmd_options.client_no_context_takeover =
305 section.value_or("client_no_context_takeover", false);
306 port.pmd_options.server_no_context_takeover =
307 section.value_or("server_no_context_takeover", false);
308 port.pmd_options.compLevel = section.value_or("compress_level", 8);
309 port.pmd_options.memLevel = section.value_or("memory_level", 4);
310}
311
312} // 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:24
std::optional< T > get(std::string const &name) const
std::string const & name() const
Returns the name of this section.
Definition BasicConfig.h:40
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:177
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void parse_Port(ParsedPort &port, Section const &section, std::ostream &log)
Definition Port.cpp:194
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:615
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:28
T push_back(T... args)
T size(T... args)
std::set< std::string, boost::beast::iless > protocol
Definition Port.h:82
boost::beast::websocket::permessage_deflate pmd_options
Definition Port.h:91
std::optional< boost::asio::ip::address > ip
Definition Port.h:95
std::vector< boost::asio::ip::network_v6 > secure_gateway_nets_v6
Definition Port.h:100
std::string admin_password
Definition Port.h:86
std::vector< boost::asio::ip::network_v4 > secure_gateway_nets_v4
Definition Port.h:99
std::string ssl_chain
Definition Port.h:89
std::string ssl_key
Definition Port.h:87
std::string name
Definition Port.h:81
std::uint16_t ws_queue_limit
Definition Port.h:93
std::vector< boost::asio::ip::network_v4 > admin_nets_v4
Definition Port.h:97
std::string user
Definition Port.h:83
std::optional< std::uint16_t > port
Definition Port.h:96
std::string ssl_ciphers
Definition Port.h:90
std::string password
Definition Port.h:84
std::vector< boost::asio::ip::network_v6 > admin_nets_v6
Definition Port.h:98
std::string admin_user
Definition Port.h:85
std::string ssl_cert
Definition Port.h:88
Configuration information for a Server listening port.
Definition Port.h:30
std::vector< boost::asio::ip::network_v4 > admin_nets_v4
Definition Port.h:37
std::string protocols() const
Definition Port.cpp:34
std::vector< boost::asio::ip::network_v6 > secure_gateway_nets_v6
Definition Port.h:40
bool secure() const
Definition Port.cpp:27
boost::asio::ip::address ip
Definition Port.h:34
std::vector< boost::asio::ip::network_v4 > secure_gateway_nets_v4
Definition Port.h:39
std::string name
Definition Port.h:33
std::vector< boost::asio::ip::network_v6 > admin_nets_v6
Definition Port.h:38
std::uint16_t port
Definition Port.h:35