Debian Codegraph @

src/net/parse.cpp

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "parse.h"
31
32 #include <type_traits>
33 #include "hex.h"
34 #include "net/socks.h"
35 #include "net/tor_address.h"
36 #include "net/i2p_address.h"
37 #include "string_tools.h"
38 #include "string_tools_lexical.h"
39
40 namespace net
41 {
42 namespace
43 {
44 bool percent_decoding(std::string& out)
45 {
46 auto pos = out.find('%');
47 while (pos != std::string::npos)
48 {
49 if (out.size() - pos < 3)
50 return false;
51 if (!epee::from_hex::to_buffer(epee::as_mut_byte_span(out[pos]), {out.data() + pos + 1, 2}))
52 return false;
53 out.erase(pos + 1, 2);
54 pos = out.find('%', pos + 1);
55 }
56
57 return true;
58 }
59 } // anonymous
60
61 scheme_and_authority::scheme_and_authority(boost::string_ref uri)
62 : scheme(), authority()
63 {
64 static_assert(std::is_same<std::string::size_type, boost::string_ref::size_type>());
65
66 // Stop at scheme end or path begin. URN not supported
67 const auto split = uri.find_first_of(":/");
68 if (split != boost::string_ref::npos && uri.substr(split).starts_with("://"))
69 {
70 scheme.assign(uri.data(), split);
71 uri = uri.substr(split + 3);
72 }
73
74 uri = uri.substr(0, uri.find('/'));
75 authority.assign(uri.data(), uri.size());
76 }
77
78 userinfo_and_hostport::userinfo_and_hostport(boost::string_ref authority)
79 : userinfo(), hostport()
80 {
81 static_assert(std::is_same<std::string::size_type, boost::string_ref::size_type>());
82
83 const auto split = authority.find('@');
84 if (split != boost::string_ref::npos)
85 {
86 userinfo.assign(authority.data(), split);
87 authority = authority.substr(split + 1);
88 }
89
90 hostport.assign(authority.data(), authority.size());
91 }
92
93 boost::optional<user_and_pass> user_and_pass::get(boost::string_ref userinfo)
94 {
95 static_assert(std::is_same<std::string::size_type, boost::string_ref::size_type>());
96 boost::optional<user_and_pass> out = user_and_pass{};
97
98 const auto split = userinfo.find(':');
99 if (split != boost::string_ref::npos)
100 {
101 out->user.assign(userinfo.data(), split);
102 userinfo = userinfo.substr(split + 1);
103 }
104 else
105 {
106 out->user.assign(userinfo.data(), userinfo.size());
107 userinfo = {};
108 }
109
110 out->pass.assign(userinfo.data(), userinfo.size());
111 if (percent_decoding(out->user) && percent_decoding(out->pass))
112 return out;
113 return boost::none;
114 }
115
116 boost::optional<uri_components> uri_components::get(const boost::string_ref uri)
117 {
118 scheme_and_authority result1{uri};
119 userinfo_and_hostport result2{result1.authority};
120 auto result3 = user_and_pass::get(result2.userinfo);
121 if (!result3)
122 return boost::none;
123
124 boost::optional<uri_components> out = uri_components{};
125 out->scheme = std::move(result1.scheme);
126 out->userinfo = std::move(*result3);
127 out->hostport = std::move(result2.hostport);
128 return out;
129 }
130
131 void get_network_address_host_and_port(const std::string& address, std::string& host, std::string& port)
132 {
133 // If IPv6 address format with port "[addr:addr:addr:...:addr]:port"
134 if (address.find(']') != std::string::npos)
135 {
136 host = address.substr(1, address.rfind(']') - 1);
137 if ((host.size() + 2) < address.size())
138 {
139 port = address.substr(address.rfind(':') + 1);
140 }
141 }
142 // Else if IPv6 address format without port e.g. "addr:addr:addr:...:addr"
143 else if (std::count(address.begin(), address.end(), ':') >= 2)
144 {
145 host = address;
146 }
147 // Else IPv4, Tor, I2P address or hostname
148 else
149 {
150 host = address.substr(0, address.rfind(':'));
151 if (host.size() < address.size())
152 {
153 port = address.substr(host.size() + 1);
154 }
155 }
156 }
157
158 expect<epee::net_utils::network_address>
159 get_network_address(const boost::string_ref address, const std::uint16_t default_port)
160 {
161 std::string host_str = "";
162 std::string port_str = "";
163
164 bool ipv6 = false;
165
166 get_network_address_host_and_port(std::string(address), host_str, port_str);
167
168 boost::string_ref host_str_ref(host_str);
169 boost::string_ref port_str_ref(port_str);
170
171 if (host_str.empty())
172 return make_error_code(net::error::invalid_host);
173 if (host_str_ref.ends_with(".onion"))
174 return tor_address::make(address, default_port);
175 if (host_str_ref.ends_with(".i2p"))
176 return i2p_address::make(address);
177
178 boost::system::error_code ec;
179 boost::asio::ip::address_v6 v6 = boost::asio::ip::make_address_v6(host_str, ec);
180 ipv6 = !ec;
181
182 std::uint16_t port = default_port;
183 if (port_str.size())
184 {
185 if (!epee::string_tools::get_xtype_from_string(port, port_str))
186 return make_error_code(net::error::invalid_port);
187 }
188
189 if (ipv6)
190 {
191 return {epee::net_utils::ipv6_network_address{v6, port}};
192 }
193 else
194 {
195 std::uint32_t ip = 0;
196 if (epee::string_tools::get_ip_int32_from_string(ip, host_str))
197 return {epee::net_utils::ipv4_network_address{ip, port}};
198 }
199 return make_error_code(net::error::unsupported_address);
200 }
201
202 expect<epee::net_utils::ipv4_network_subnet>
203 get_ipv4_subnet_address(const boost::string_ref address, bool allow_implicit_32)
204 {
205 uint32_t mask = 32;
206 const boost::string_ref::size_type slash = address.find_first_of('/');
207 if (slash != boost::string_ref::npos)
208 {
209 if (!epee::string_tools::get_xtype_from_string(mask, std::string{address.substr(slash + 1)}))
210 return make_error_code(net::error::invalid_mask);
211 if (mask > 32)
212 return make_error_code(net::error::invalid_mask);
213 }
214 else if (!allow_implicit_32)
215 return make_error_code(net::error::invalid_mask);
216
217 std::uint32_t ip = 0;
218 boost::string_ref S(address.data(), slash != boost::string_ref::npos ? slash : address.size());
219 if (!epee::string_tools::get_ip_int32_from_string(ip, std::string(S)))
220 return make_error_code(net::error::invalid_host);
221
222 return {epee::net_utils::ipv4_network_subnet{ip, (uint8_t)mask}};
223 }
224
225 expect<boost::asio::ip::tcp::endpoint> get_tcp_endpoint(const boost::string_ref address)
226 {
227 uint16_t port = 0;
228 expect<epee::net_utils::network_address> parsed = get_network_address(address, port);
229 if (!parsed)
230 {
231 return parsed.error();
232 }
233
234 boost::asio::ip::tcp::endpoint result;
235 switch (parsed->get_type_id())
236 {
237 case epee::net_utils::ipv4_network_address::get_type_id():
238 {
239 const auto &ipv4 = parsed->as<epee::net_utils::ipv4_network_address>();
240 result = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(SWAP32BE(ipv4.ip())), ipv4.port());
241 break;
242 }
243 case epee::net_utils::ipv6_network_address::get_type_id():
244 {
245 const auto &ipv6 = parsed->as<epee::net_utils::ipv6_network_address>();
246 result = boost::asio::ip::tcp::endpoint(ipv6.ip(), ipv6.port());
247 break;
248 }
249 default:
250 return make_error_code(net::error::unsupported_address);
251 }
252 if (result.port() == 0)
253 {
254 return make_error_code(net::error::invalid_port);
255 }
256
257 return result;
258 }
259
260 namespace socks
261 {
262 endpoint::endpoint()
263 : endpoint(boost::asio::ip::tcp::endpoint{})
264 {}
265
266 endpoint::endpoint(const boost::asio::ip::tcp::endpoint& address)
267 : address(address), userinfo(), ver(version::v4a)
268 {}
269
270 expect<endpoint> endpoint::get(const boost::string_ref uri)
271 {
272 auto components = uri_components::get(uri);
273 if (!components)
274 return {net::error::invalid_encoding};
275 auto tcp_endpoint = get_tcp_endpoint(components->hostport);
276 if (!tcp_endpoint)
277 return tcp_endpoint.error();
278
279 endpoint out{};
280 if (components->scheme.empty() || components->scheme == "socks" || components->scheme == "socks4a")
281 out.ver = version::v4a;
282 else if (components->scheme == "socks4")
283 out.ver = version::v4;
284 else if (components->scheme == "socks5")
285 out.ver = version::v5;
286 else
287 return {net::error::invalid_scheme};
288
289 // Only version 5 supports user/pass authentication
290 if (!components->userinfo.user.empty() || !components->userinfo.pass.empty())
291 {
292 if (out.ver != version::v5)
293 return {net::error::unexpected_userinfo};
294 }
295
296 out.address = std::move(*tcp_endpoint);
297 out.userinfo = std::move(components->userinfo);
298 return out;
299 }
300 }
301 }