/src/libtorrent/fuzzers/src/dht_node.cpp
Line | Count | Source |
1 | | /* |
2 | | |
3 | | Copyright (c) 2019, 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/kademlia/dht_tracker.hpp" |
34 | | #include "libtorrent/performance_counters.hpp" |
35 | | #include "libtorrent/kademlia/dht_observer.hpp" |
36 | | #include "libtorrent/ip_filter.hpp" |
37 | | #include "libtorrent/aux_/session_impl.hpp" |
38 | | |
39 | | #include <memory> |
40 | | |
41 | | using namespace lt; |
42 | | |
43 | | aux::session_settings sett; |
44 | | dht::dht_state state; |
45 | | std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(sett)); |
46 | | |
47 | | counters cnt; |
48 | | |
49 | | struct obs : dht::dht_observer |
50 | | { |
51 | | void set_external_address(lt::aux::listen_socket_handle const&, lt::address const& /* addr */ |
52 | | , lt::address const&) override |
53 | 46 | {} |
54 | | int get_listen_port(aux::transport ssl, aux::listen_socket_handle const& s) override |
55 | 0 | { return 6881; } |
56 | | |
57 | 0 | void get_peers(lt::sha1_hash const&) override {} |
58 | | void outgoing_get_peers(sha1_hash const& |
59 | 0 | , sha1_hash const&, lt::udp::endpoint const&) override {} |
60 | 0 | void announce(sha1_hash const&, lt::address const&, int) override {} |
61 | | bool on_dht_request(string_view |
62 | | , dht::msg const&, entry&) override |
63 | 21 | { return false; } |
64 | | |
65 | | #ifndef TORRENT_DISABLE_LOGGING |
66 | | |
67 | | void log(dht_logger::module_t, char const*, ...) override {} |
68 | | |
69 | | bool should_log(module_t) const override { return true; } |
70 | | void log_packet(message_direction_t |
71 | | , span<char const> |
72 | | , lt::udp::endpoint const&) override {} |
73 | | #endif // TORRENT_DISABLE_LOGGING |
74 | | }; |
75 | | |
76 | | obs o; |
77 | | io_context ios; |
78 | | dht::dht_tracker dht_node(&o |
79 | | , ios |
80 | | , [](aux::listen_socket_handle const&, udp::endpoint const& |
81 | 140 | , span<char const>, error_code&, udp_send_flags_t) {} |
82 | | , sett |
83 | | , cnt |
84 | | , *dht_storage |
85 | | , std::move(state)); |
86 | | auto listen_socket = std::make_shared<aux::listen_socket_t>(); |
87 | | aux::listen_socket_handle s(listen_socket); |
88 | | |
89 | | error_code ignore; |
90 | | lt::address_v4 src = make_address_v4("2.2.2.2", ignore); |
91 | | udp::endpoint ep(src, 6881); |
92 | | std::once_flag once_flag; |
93 | | |
94 | | extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) |
95 | 25.8k | { |
96 | 25.8k | ep.address(src); |
97 | 25.8k | src = lt::address_v4(aux::plus_one(src.to_bytes())); |
98 | 25.8k | std::call_once(once_flag, []{ dht_node.new_socket(s); }); |
99 | 25.8k | dht_node.incoming_packet(s, ep, {reinterpret_cast<char const*>(data), int(size)}); |
100 | 25.8k | return 0; |
101 | 25.8k | } |
102 | | |