Debian Codegraph @

monero/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 "net/tor_address.h"
33 #include "net/i2p_address.h"
34 #include "string_tools.h"
35 #include "string_tools_lexical.h"
36
37 namespace net
38 {
39 void get_network_address_host_and_port(const std::string& address, std::string& host, std::string& port)
40 {
41 // If IPv6 address format with port "[addr:addr:addr:...:addr]:port"
42 if (address.find(']') != std::string::npos)
43 {
44 host = address.substr(1, address.rfind(']') - 1);
45 if ((host.size() + 2) < address.size())
46 {
47 port = address.substr(address.rfind(':') + 1);
48 }
49 }
50 // Else if IPv6 address format without port e.g. "addr:addr:addr:...:addr"
51 else if (std::count(address.begin(), address.end(), ':') >= 2)
52 {
53 host = address;
54 }
55 // Else IPv4, Tor, I2P address or hostname
56 else
57 {
58 host = address.substr(0, address.rfind(':'));
59 if (host.size() < address.size())
60 {
61 port = address.substr(host.size() + 1);
62 }
63 }
64 }
65
66 expect<epee::net_utils::network_address>
67 get_network_address(const boost::string_ref address, const std::uint16_t default_port)
68 {
69 std::string host_str = "";
70 std::string port_str = "";
71
72 bool ipv6 = false;
73
74 get_network_address_host_and_port(std::string(address), host_str, port_str);
75
76 boost::string_ref host_str_ref(host_str);
77 boost::string_ref port_str_ref(port_str);
78
79 if (host_str.empty())
80 return make_error_code(net::error::invalid_host);
81 if (host_str_ref.ends_with(".onion"))
82 return tor_address::make(address, default_port);
83 if (host_str_ref.ends_with(".i2p"))
84 return i2p_address::make(address);
85
86 boost::system::error_code ec;
87 boost::asio::ip::address_v6 v6 = boost::asio::ip::make_address_v6(host_str, ec);
88 ipv6 = !ec;
89
90 std::uint16_t port = default_port;
91 if (port_str.size())
92 {
93 if (!epee::string_tools::get_xtype_from_string(port, port_str))
94 return make_error_code(net::error::invalid_port);
95 }
96
97 if (ipv6)
98 {
99 return {epee::net_utils::ipv6_network_address{v6, port}};
100 }
101 else
102 {
103 std::uint32_t ip = 0;
104 if (epee::string_tools::get_ip_int32_from_string(ip, host_str))
105 return {epee::net_utils::ipv4_network_address{ip, port}};
106 }
107
108 return make_error_code(net::error::unsupported_address);
109 }
110
111 expect<epee::net_utils::ipv4_network_subnet>
112 get_ipv4_subnet_address(const boost::string_ref address, bool allow_implicit_32)
113 {
114 uint32_t mask = 32;
115 const boost::string_ref::size_type slash = address.find_first_of('/');
116 if (slash != boost::string_ref::npos)
117 {
118 if (!epee::string_tools::get_xtype_from_string(mask, std::string{address.substr(slash + 1)}))
119 return make_error_code(net::error::invalid_mask);
120 if (mask > 32)
121 return make_error_code(net::error::invalid_mask);
122 }
123 else if (!allow_implicit_32)
124 return make_error_code(net::error::invalid_mask);
125
126 std::uint32_t ip = 0;
127 boost::string_ref S(address.data(), slash != boost::string_ref::npos ? slash : address.size());
128 if (!epee::string_tools::get_ip_int32_from_string(ip, std::string(S)))
129 return make_error_code(net::error::invalid_host);
130
131 return {epee::net_utils::ipv4_network_subnet{ip, (uint8_t)mask}};
132 }
133
134 expect<boost::asio::ip::tcp::endpoint> get_tcp_endpoint(const boost::string_ref address)
135 {
136 uint16_t port = 0;
137 expect<epee::net_utils::network_address> parsed = get_network_address(address, port);
138 if (!parsed)
139 {
140 return parsed.error();
141 }
142
143 boost::asio::ip::tcp::endpoint result;
144 switch (parsed->get_type_id())
145 {
146 case epee::net_utils::ipv4_network_address::get_type_id():
147 {
148 const auto &ipv4 = parsed->as<epee::net_utils::ipv4_network_address>();
149 result = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(SWAP32BE(ipv4.ip())), ipv4.port());
150 break;
151 }
152 case epee::net_utils::ipv6_network_address::get_type_id():
153 {
154 const auto &ipv6 = parsed->as<epee::net_utils::ipv6_network_address>();
155 result = boost::asio::ip::tcp::endpoint(ipv6.ip(), ipv6.port());
156 break;
157 }
158 default:
159 return make_error_code(net::error::unsupported_address);
160 }
161 if (result.port() == 0)
162 {
163 return make_error_code(net::error::invalid_port);
164 }
165
166 return result;
167 }
168 }