1 /*
2 
3 Copyright (c) 2009-2018, Arvid Norberg
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #include <string>
34 
35 #include "libtorrent/error_code.hpp"
36 #include "libtorrent/socket.hpp"
37 #include "libtorrent/socket_io.hpp"
38 #include "libtorrent/address.hpp"
39 #include "libtorrent/io.hpp" // for write_uint16
40 #include "libtorrent/hasher.hpp" // for hasher
41 #include "libtorrent/aux_/escape_string.hpp" // for trim
42 
43 namespace libtorrent {
44 
print_address(address const & addr)45 	std::string print_address(address const& addr)
46 	{
47 		error_code ec;
48 		return addr.to_string(ec);
49 	}
50 
address_to_bytes(address const & a)51 	std::string address_to_bytes(address const& a)
52 	{
53 		std::string ret;
54 		std::back_insert_iterator<std::string> out(ret);
55 		detail::write_address(a, out);
56 		return ret;
57 	}
58 
endpoint_to_bytes(udp::endpoint const & ep)59 	std::string endpoint_to_bytes(udp::endpoint const& ep)
60 	{
61 		std::string ret;
62 		std::back_insert_iterator<std::string> out(ret);
63 		detail::write_endpoint(ep, out);
64 		return ret;
65 	}
66 
print_endpoint(address const & addr,int port)67 	std::string print_endpoint(address const& addr, int port)
68 	{
69 		error_code ec;
70 		char buf[200];
71 		if (addr.is_v6())
72 			std::snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string(ec).c_str(), port);
73 		else
74 			std::snprintf(buf, sizeof(buf), "%s:%d", addr.to_string(ec).c_str(), port);
75 		return buf;
76 	}
77 
print_endpoint(tcp::endpoint const & ep)78 	std::string print_endpoint(tcp::endpoint const& ep)
79 	{
80 		return print_endpoint(ep.address(), ep.port());
81 	}
82 
print_endpoint(udp::endpoint const & ep)83 	std::string print_endpoint(udp::endpoint const& ep)
84 	{
85 		return print_endpoint(ep.address(), ep.port());
86 	}
87 
parse_endpoint(string_view str,error_code & ec)88 	tcp::endpoint parse_endpoint(string_view str, error_code& ec)
89 	{
90 		tcp::endpoint ret;
91 
92 		str = trim(str);
93 
94 		string_view addr;
95 		string_view port;
96 
97 		if (str.empty())
98 		{
99 			ec = errors::invalid_port;
100 			return ret;
101 		}
102 
103 		// this is for IPv6 addresses
104 		if (str.front() == '[')
105 		{
106 			auto const close_bracket = str.find_first_of(']');
107 			if (close_bracket == string_view::npos)
108 			{
109 				ec = errors::expected_close_bracket_in_address;
110 				return ret;
111 			}
112 			addr = str.substr(1, close_bracket - 1);
113 			port = str.substr(close_bracket + 1);
114 			if (port.empty() || port.front() != ':')
115 			{
116 				ec = errors::invalid_port;
117 				return ret;
118 			}
119 			// shave off the ':'
120 			port = port.substr(1);
121 			ret.address(make_address_v6(addr.to_string(), ec));
122 			if (ec) return ret;
123 		}
124 		else
125 		{
126 			auto const port_pos = str.find_first_of(':');
127 			if (port_pos == string_view::npos)
128 			{
129 				ec = errors::invalid_port;
130 				return ret;
131 			}
132 			addr = str.substr(0, port_pos);
133 			port = str.substr(port_pos + 1);
134 			ret.address(make_address_v4(addr.to_string(), ec));
135 			if (ec) return ret;
136 		}
137 
138 		if (port.empty())
139 		{
140 			ec = errors::invalid_port;
141 			return ret;
142 		}
143 
144 		int const port_num = std::atoi(port.to_string().c_str());
145 		if (port_num <= 0 || port_num > std::numeric_limits<std::uint16_t>::max())
146 		{
147 			ec = errors::invalid_port;
148 			return ret;
149 		}
150 		ret.port(static_cast<std::uint16_t>(port_num));
151 		return ret;
152 	}
153 
hash_address(address const & ip)154 	sha1_hash hash_address(address const& ip)
155 	{
156 		if (ip.is_v6())
157 		{
158 			address_v6::bytes_type b = ip.to_v6().to_bytes();
159 			return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
160 		}
161 		else
162 		{
163 			address_v4::bytes_type b = ip.to_v4().to_bytes();
164 			return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
165 		}
166 	}
167 
168 }
169