1 /*
2 
3 Copyright (c) 2016, 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 "utils.hpp"
34 #include "test.hpp"
35 #include "libtorrent/session.hpp"
36 #include "libtorrent/settings_pack.hpp"
37 #include "libtorrent/address.hpp"
38 #include "libtorrent/ip_filter.hpp"
39 #include "libtorrent/alert_types.hpp"
40 #include "libtorrent/session_stats.hpp"
41 #include "libtorrent/alert.hpp"
42 #include "libtorrent/io_service.hpp"
43 #include "setup_swarm.hpp"
44 
45 using namespace lt;
46 
utp_only(lt::session & ses)47 void utp_only(lt::session& ses)
48 {
49 	settings_pack p;
50 	utp_only(p);
51 	ses.apply_settings(p);
52 }
53 
enable_enc(lt::session & ses)54 void enable_enc(lt::session& ses)
55 {
56 	settings_pack p;
57 	enable_enc(p);
58 	ses.apply_settings(p);
59 }
60 
filter_ips(lt::session & ses)61 void filter_ips(lt::session& ses)
62 {
63 	ip_filter filter;
64 	filter.add_rule(address_v4::from_string("50.0.0.1")
65 		, address_v4::from_string("50.0.0.2"), ip_filter::blocked);
66 	ses.set_ip_filter(filter);
67 }
68 
set_cache_size(lt::session & ses,int val)69 void set_cache_size(lt::session& ses, int val)
70 {
71 	settings_pack pack;
72 	pack.set_int(settings_pack::cache_size, val);
73 	ses.apply_settings(pack);
74 }
75 
get_cache_size(lt::session & ses)76 int get_cache_size(lt::session& ses)
77 {
78 	std::vector<stats_metric> stats = session_stats_metrics();
79 	int const read_cache_idx = find_metric_idx("disk.read_cache_blocks");
80 	int const write_cache_idx = find_metric_idx("disk.write_cache_blocks");
81 	TEST_CHECK(read_cache_idx >= 0);
82 	TEST_CHECK(write_cache_idx >= 0);
83 	ses.set_alert_notify([](){});
84 	ses.post_session_stats();
85 	std::vector<alert*> alerts;
86 	ses.pop_alerts(&alerts);
87 	std::int64_t cache_size = -1;
88 	for (auto const a : alerts)
89 	{
90 		if (auto const* st = alert_cast<session_stats_alert>(a))
91 		{
92 			cache_size = st->counters()[read_cache_idx];
93 			cache_size += st->counters()[write_cache_idx];
94 			break;
95 		}
96 	}
97 	TEST_CHECK(cache_size < std::numeric_limits<int>::max());
98 	return int(cache_size);
99 }
100 
set_proxy(lt::session & ses,int proxy_type,int flags,bool proxy_peer_connections)101 void set_proxy(lt::session& ses, int proxy_type, int flags, bool proxy_peer_connections)
102 {
103 	// apply the proxy settings to session 0
104 	settings_pack p;
105 	p.set_int(settings_pack::proxy_type, proxy_type);
106 	if (proxy_type == settings_pack::socks4)
107 		p.set_int(settings_pack::proxy_port, 4444);
108 	else
109 		p.set_int(settings_pack::proxy_port, 5555);
110 	if (flags & ipv6)
111 		p.set_str(settings_pack::proxy_hostname, "2001::2");
112 	else
113 		p.set_str(settings_pack::proxy_hostname, "50.50.50.50");
114 	p.set_bool(settings_pack::proxy_hostnames, true);
115 	p.set_bool(settings_pack::proxy_peer_connections, proxy_peer_connections);
116 	p.set_bool(settings_pack::proxy_tracker_connections, true);
117 
118 	ses.apply_settings(p);
119 }
120 
print_alerts(lt::session & ses,std::function<void (lt::session &,lt::alert const *)> on_alert,int const idx)121 void print_alerts(lt::session& ses
122 	, std::function<void(lt::session&, lt::alert const*)> on_alert
123 	, int const idx)
124 {
125 	lt::time_point start_time = lt::clock_type::now();
126 
127 	static std::vector<lt::alert*> alerts;
128 
129 	ses.set_alert_notify([&ses,start_time,on_alert,idx] {
130 		ses.get_io_service().post([&ses,start_time,on_alert,idx] {
131 
132 		try {
133 			alerts.clear();
134 			ses.pop_alerts(&alerts);
135 
136 			for (lt::alert const* a : alerts)
137 			{
138 				std::printf("%-3d [%d] %s\n"
139 					, int(lt::duration_cast<lt::seconds>(a->timestamp() - start_time).count())
140 					, idx
141 					, a->message().c_str());
142 				// call the user handler
143 				on_alert(ses, a);
144 			}
145 		} catch (std::exception const& e) {
146 			std::printf("print alerts: ERROR failed with exception: %s"
147 				, e.what());
148 		} catch (...) {
149 			std::printf("print alerts: ERROR failed with (unknown) exception");
150 		}
151 	} ); } );
152 }
153 
make_io_service(sim::simulation & sim,int i)154 std::unique_ptr<sim::asio::io_service> make_io_service(sim::simulation& sim, int i)
155 {
156 	char ep[30];
157 	std::snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
158 	return std::unique_ptr<sim::asio::io_service>(new sim::asio::io_service(
159 		sim, lt::address_v4::from_string(ep)));
160 }
161