1 /*
2 
3 Copyright (c) 2017, 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 "libtorrent/version.hpp"
34 #include "libtorrent/kademlia/dht_tracker.hpp"
35 #include "libtorrent/performance_counters.hpp"
36 #include "libtorrent/kademlia/dht_observer.hpp"
37 #include "libtorrent/ip_filter.hpp"
38 #include "libtorrent/aux_/session_impl.hpp"
39 
40 #include <memory>
41 
42 using namespace lt;
43 
44 #if LIBTORRENT_VERSION_NUM >= 10200
45 dht::settings sett;
46 dht::dht_state state;
47 std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(sett));
48 #else
49 dht_settings sett;
50 entry state;
51 #endif
52 
53 counters cnt;
54 
55 struct obs : dht::dht_observer
56 {
57 #if LIBTORRENT_VERSION_NUM >= 10200
set_external_addressobs58 	void set_external_address(lt::aux::listen_socket_handle const&, lt::address const& /* addr */
59 		, lt::address const&) override
60 	{}
get_listen_portobs61 	int get_listen_port(aux::transport ssl, aux::listen_socket_handle const& s) override
62 	{ return 6881; }
63 #else
64 	void set_external_address(address const& addr
65 		, address const& source) override {}
66 #endif
67 
get_peersobs68 	void get_peers(lt::sha1_hash const&) override {}
outgoing_get_peersobs69 	void outgoing_get_peers(sha1_hash const&
70 		, sha1_hash const&, lt::udp::endpoint const&) override {}
announceobs71 	void announce(sha1_hash const&, lt::address const&, int) override {}
72 #if LIBTORRENT_VERSION_NUM >= 10200
on_dht_requestobs73 	bool on_dht_request(string_view
74 		, dht::msg const&, entry&) override
75 	{ return false; }
76 #else
on_dht_requestobs77 	bool on_dht_request(char const* query, int query_len
78 			, dht::msg const& request, entry& response) override { return false; }
external_addressobs79 	address external_address() override { return address(); }
80 #endif
81 
82 #ifndef TORRENT_DISABLE_LOGGING
83 
logobs84 	void log(dht_logger::module_t, char const*, ...) override {}
85 
86 #if LIBTORRENT_VERSION_NUM < 10200
87 
log_packetobs88 	void log_packet(message_direction_t dir, char const* pkt, int len
89 		, udp::endpoint node) override {}
90 
91 #else
92 
should_logobs93 	bool should_log(module_t) const override { return true; }
log_packetobs94 	void log_packet(message_direction_t
95 		, span<char const>
96 		, lt::udp::endpoint const&) override {}
97 #endif // LIBTORRENT_VERSION_NUM
98 #endif // TORRENT_DISABLE_LOGGING
99 };
100 
101 obs o;
102 #if LIBTORRENT_VERSION_NUM >= 10300
103 io_context ios;
104 #else
105 io_service ios;
106 #endif
107 #if LIBTORRENT_VERSION_NUM < 10200
108 rate_limited_udp_socket sock(ios);
109 #endif
110 dht::dht_tracker dht_node(&o
111 #if LIBTORRENT_VERSION_NUM >= 10200
112 	, ios
113 	, [](aux::listen_socket_handle const&, udp::endpoint const&
__anonb4f598750102(aux::listen_socket_handle const&, udp::endpoint const& , span<char const>, error_code&, udp_send_flags_t) 114 		, span<char const>, error_code&, udp_send_flags_t) {}
115 #else
116 	, sock
117 #endif
118 	, sett
119 	, cnt
120 #if LIBTORRENT_VERSION_NUM >= 10200
121 	, *dht_storage
122 #else
123 	, dht::dht_default_storage_constructor
124 #endif
125 	, std::move(state));
126 auto listen_socket = std::make_shared<aux::listen_socket_t>();
127 aux::listen_socket_handle s(listen_socket);
128 
129 error_code ignore;
130 lt::address_v4 src = make_address_v4("2.2.2.2", ignore);
131 udp::endpoint ep(src, 6881);
132 std::once_flag once_flag;
133 
LLVMFuzzerTestOneInput(uint8_t const * data,size_t size)134 extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
135 {
136 	ep.address(src);
137 	src = lt::address_v4(detail::plus_one(src.to_bytes()));
138 	std::call_once(once_flag, []{ dht_node.new_socket(s); });
139 	dht_node.incoming_packet(s, ep, {reinterpret_cast<char const*>(data), int(size)});
140 	return 0;
141 }
142 
143