2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 Juliusz Chroboczek.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-12-15 00:51:39 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-12-15 00:51:39 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
#include <memory>
|
2022-10-02 18:18:23 +00:00
|
|
|
#include <string_view>
|
2022-11-11 16:09:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <dht/dht.h>
|
2022-09-29 14:12:33 +00:00
|
|
|
|
2022-04-25 01:49:52 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
2022-07-09 23:44:20 +00:00
|
|
|
#include "net.h" // tr_port
|
2022-04-25 01:49:52 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
struct tr_pex;
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
class TimerMaker;
|
|
|
|
} // namespace libtransmission
|
|
|
|
|
|
|
|
class tr_dht
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Wrapper around DHT library.
|
|
|
|
// This calls `jech/dht` in production, but makes it possible for tests to inject a mock.
|
|
|
|
struct API
|
|
|
|
{
|
|
|
|
virtual ~API() = default;
|
|
|
|
|
|
|
|
virtual int get_nodes(struct sockaddr_in* sin, int* num, struct sockaddr_in6* sin6, int* num6)
|
|
|
|
{
|
|
|
|
return ::dht_get_nodes(sin, num, sin6, num6);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int nodes(int af, int* good_return, int* dubious_return, int* cached_return, int* incoming_return)
|
|
|
|
{
|
|
|
|
return ::dht_nodes(af, good_return, dubious_return, cached_return, incoming_return);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int periodic(
|
|
|
|
void const* buf,
|
|
|
|
size_t buflen,
|
|
|
|
struct sockaddr const* from,
|
|
|
|
int fromlen,
|
|
|
|
time_t* tosleep,
|
|
|
|
dht_callback_t callback,
|
|
|
|
void* closure)
|
|
|
|
{
|
|
|
|
return ::dht_periodic(buf, buflen, from, fromlen, tosleep, callback, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int ping_node(struct sockaddr const* sa, int salen)
|
|
|
|
{
|
|
|
|
return ::dht_ping_node(sa, salen);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int search(unsigned char const* id, int port, int af, dht_callback_t callback, void* closure)
|
|
|
|
{
|
|
|
|
return ::dht_search(id, port, af, callback, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int init(int s, int s6, unsigned const char* id, unsigned const char* v)
|
|
|
|
{
|
|
|
|
return ::dht_init(s, s6, id, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int uninit()
|
|
|
|
{
|
|
|
|
return ::dht_uninit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Mediator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Mediator() = default;
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] virtual std::vector<tr_torrent_id_t> torrents_allowing_dht() const = 0;
|
|
|
|
[[nodiscard]] virtual tr_sha1_digest_t torrent_info_hash(tr_torrent_id_t) const = 0;
|
2022-11-11 16:09:24 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] virtual std::string_view config_dir() const = 0;
|
|
|
|
[[nodiscard]] virtual libtransmission::TimerMaker& timer_maker() = 0;
|
2022-11-11 16:09:24 +00:00
|
|
|
[[nodiscard]] virtual API& api()
|
|
|
|
{
|
|
|
|
return api_;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
virtual void add_pex(tr_sha1_digest_t const&, tr_pex const* pex, size_t n_pex) = 0;
|
2022-11-11 16:09:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
API api_;
|
|
|
|
};
|
2022-10-02 18:18:23 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
[[nodiscard]] static std::unique_ptr<tr_dht> create(
|
|
|
|
Mediator& mediator,
|
|
|
|
tr_port peer_port,
|
|
|
|
tr_socket_t udp4_socket,
|
|
|
|
tr_socket_t udp6_socket);
|
|
|
|
virtual ~tr_dht() = default;
|
2022-10-02 18:18:23 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
virtual void add_node(tr_address const& address, tr_port port) = 0;
|
|
|
|
virtual void handle_message(unsigned char const* msg, size_t msglen, struct sockaddr* from, socklen_t fromlen) = 0;
|
2022-11-11 16:09:24 +00:00
|
|
|
};
|