mirror of
https://github.com/transmission/transmission.git
synced 2026-05-08 09:39:08 +01:00
refactor: fix uninit var warnings in tr-dht
This commit is contained in:
@@ -31,13 +31,6 @@
|
||||
#include "utils.h"
|
||||
#include "variant.h"
|
||||
|
||||
enum tr_tristate_t
|
||||
{
|
||||
TR_NET_OK,
|
||||
TR_NET_ERROR,
|
||||
TR_NET_WAIT
|
||||
};
|
||||
|
||||
enum tr_auto_switch_state_t
|
||||
{
|
||||
TR_AUTO_SWITCH_UNUSED,
|
||||
|
||||
+83
-89
@@ -112,16 +112,15 @@ static int bootstrap_af(tr_session* session)
|
||||
|
||||
static void bootstrap_from_name(char const* name, tr_port port, int af)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo* info;
|
||||
char pp[10];
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
addrinfo hints = {};
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_family = af;
|
||||
|
||||
/* No, just passing p + 1 to gai won't work. */
|
||||
char pp[10];
|
||||
tr_snprintf(pp, sizeof(pp), "%d", (int)port);
|
||||
|
||||
addrinfo* info = nullptr;
|
||||
int const rc = getaddrinfo(name, pp, &hints, &info);
|
||||
|
||||
if (rc != 0)
|
||||
@@ -173,8 +172,8 @@ static void dht_bootstrap(void* closure)
|
||||
{
|
||||
if (i < num && !bootstrap_done(cl->session, AF_INET))
|
||||
{
|
||||
tr_port port;
|
||||
struct tr_address addr;
|
||||
auto port = tr_port{};
|
||||
auto addr = tr_address{};
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.type = TR_AF_INET;
|
||||
@@ -186,8 +185,8 @@ static void dht_bootstrap(void* closure)
|
||||
|
||||
if (i < num6 && !bootstrap_done(cl->session, AF_INET6))
|
||||
{
|
||||
tr_port port;
|
||||
struct tr_address addr;
|
||||
auto port = tr_port{};
|
||||
auto addr = tr_address{};
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.type = TR_AF_INET6;
|
||||
@@ -308,15 +307,6 @@ static void dht_bootstrap(void* closure)
|
||||
|
||||
int tr_dhtInit(tr_session* ss)
|
||||
{
|
||||
tr_variant benc;
|
||||
bool have_id = false;
|
||||
uint8_t* nodes = nullptr;
|
||||
uint8_t* nodes6 = nullptr;
|
||||
uint8_t const* raw;
|
||||
size_t len = 0;
|
||||
size_t len6 = 0;
|
||||
struct bootstrap_closure* cl;
|
||||
|
||||
if (session_ != nullptr) /* already initialized */
|
||||
{
|
||||
return -1;
|
||||
@@ -330,11 +320,18 @@ int tr_dhtInit(tr_session* ss)
|
||||
}
|
||||
|
||||
char* const dat_file = tr_buildPath(ss->configDir, "dht.dat", nullptr);
|
||||
auto benc = tr_variant{};
|
||||
int rc = tr_variantFromFile(&benc, TR_VARIANT_FMT_BENC, dat_file, nullptr) ? 0 : -1;
|
||||
tr_free(dat_file);
|
||||
|
||||
bool have_id = false;
|
||||
uint8_t* nodes = nullptr;
|
||||
uint8_t* nodes6 = nullptr;
|
||||
size_t len = 0;
|
||||
size_t len6 = 0;
|
||||
if (rc == 0)
|
||||
{
|
||||
uint8_t const* raw = nullptr;
|
||||
have_id = tr_variantDictFindRaw(&benc, TR_KEY_id, &raw, &len);
|
||||
|
||||
if (have_id && len == 20)
|
||||
@@ -345,26 +342,24 @@ int tr_dhtInit(tr_session* ss)
|
||||
if (ss->udp_socket != TR_BAD_SOCKET && tr_variantDictFindRaw(&benc, TR_KEY_nodes, &raw, &len) && len % 6 == 0)
|
||||
{
|
||||
nodes = static_cast<uint8_t*>(tr_memdup(raw, len));
|
||||
if (nodes == nullptr)
|
||||
{
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (ss->udp6_socket != TR_BAD_SOCKET && tr_variantDictFindRaw(&benc, TR_KEY_nodes6, &raw, &len6) && len6 % 18 == 0)
|
||||
{
|
||||
nodes6 = static_cast<uint8_t*>(tr_memdup(raw, len6));
|
||||
if (nodes6 == nullptr)
|
||||
{
|
||||
len6 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
tr_variantFree(&benc);
|
||||
}
|
||||
|
||||
if (nodes == nullptr)
|
||||
{
|
||||
len = 0;
|
||||
}
|
||||
|
||||
if (nodes6 == nullptr)
|
||||
{
|
||||
len6 = 0;
|
||||
}
|
||||
|
||||
if (have_id)
|
||||
{
|
||||
tr_logAddNamedInfo("DHT", "Reusing old id");
|
||||
@@ -381,12 +376,17 @@ int tr_dhtInit(tr_session* ss)
|
||||
|
||||
if (rc < 0)
|
||||
{
|
||||
goto fail;
|
||||
tr_free(nodes6);
|
||||
tr_free(nodes);
|
||||
|
||||
tr_logAddNamedDbg("DHT", "DHT initialization failed (errno = %d)", errno);
|
||||
session_ = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
session_ = ss;
|
||||
|
||||
cl = tr_new(struct bootstrap_closure, 1);
|
||||
auto* const cl = tr_new(struct bootstrap_closure, 1);
|
||||
cl->session = session_;
|
||||
cl->nodes = nodes;
|
||||
cl->nodes6 = nodes6;
|
||||
@@ -400,14 +400,6 @@ int tr_dhtInit(tr_session* ss)
|
||||
tr_logAddNamedDbg("DHT", "DHT initialized");
|
||||
|
||||
return 1;
|
||||
|
||||
fail:
|
||||
tr_free(nodes6);
|
||||
tr_free(nodes);
|
||||
|
||||
tr_logAddNamedDbg("DHT", "DHT initialization failed (errno = %d)", errno);
|
||||
session_ = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void tr_dhtUninit(tr_session* ss)
|
||||
@@ -508,10 +500,10 @@ struct getstatus_closure
|
||||
static void getstatus(void* cl)
|
||||
{
|
||||
auto* closure = static_cast<struct getstatus_closure*>(cl);
|
||||
int good;
|
||||
int dubious;
|
||||
int incoming;
|
||||
|
||||
int good = 0;
|
||||
int dubious = 0;
|
||||
int incoming = 0;
|
||||
dht_nodes(closure->af, &good, &dubious, nullptr, &incoming);
|
||||
|
||||
closure->count = good + dubious;
|
||||
@@ -676,59 +668,29 @@ static void callback(void* /*ignore*/, int event, unsigned char const* info_hash
|
||||
}
|
||||
}
|
||||
|
||||
static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
|
||||
enum class AnnounceResult
|
||||
{
|
||||
int numnodes;
|
||||
int ret = 0;
|
||||
INVALID,
|
||||
OK,
|
||||
FAILED
|
||||
};
|
||||
|
||||
static AnnounceResult tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
|
||||
{
|
||||
if (!tr_torrentAllowsDHT(tor))
|
||||
{
|
||||
return -1;
|
||||
return AnnounceResult::INVALID;
|
||||
}
|
||||
|
||||
int numnodes = 0;
|
||||
int const status = tr_dhtStatus(tor->session, af, &numnodes);
|
||||
|
||||
if (status == TR_DHT_STOPPED)
|
||||
{
|
||||
/* Let the caller believe everything is all right. */
|
||||
return 1;
|
||||
// let the caller believe everything is all right.
|
||||
return AnnounceResult::OK;
|
||||
}
|
||||
|
||||
if (status >= TR_DHT_POOR)
|
||||
{
|
||||
int const rc = dht_search(tor->info.hash, announce ? tr_sessionGetPeerPort(session_) : 0, af, callback, nullptr);
|
||||
if (rc >= 0)
|
||||
{
|
||||
tr_logAddTorInfo(
|
||||
tor,
|
||||
"Starting %s DHT announce (%s, %d nodes)",
|
||||
af == AF_INET6 ? "IPv6" : "IPv4",
|
||||
tr_dhtPrintableStatus(status),
|
||||
numnodes);
|
||||
|
||||
if (af == AF_INET)
|
||||
{
|
||||
tor->dhtAnnounceInProgress = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
tor->dhtAnnounce6InProgress = true;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_logAddTorErr(
|
||||
tor,
|
||||
"%s DHT announce failed (%s, %d nodes): %s",
|
||||
af == AF_INET6 ? "IPv6" : "IPv4",
|
||||
tr_dhtPrintableStatus(status),
|
||||
numnodes,
|
||||
tr_strerror(errno));
|
||||
}
|
||||
}
|
||||
else
|
||||
if (status < TR_DHT_POOR)
|
||||
{
|
||||
tr_logAddTorDbg(
|
||||
tor,
|
||||
@@ -736,9 +698,39 @@ static int tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
|
||||
af == AF_INET6 ? "IPv6" : "IPv4",
|
||||
tr_dhtPrintableStatus(status),
|
||||
numnodes);
|
||||
return AnnounceResult::FAILED;
|
||||
}
|
||||
|
||||
return ret;
|
||||
int const rc = dht_search(tor->info.hash, announce ? tr_sessionGetPeerPort(session_) : 0, af, callback, nullptr);
|
||||
if (rc < 0)
|
||||
{
|
||||
tr_logAddTorErr(
|
||||
tor,
|
||||
"%s DHT announce failed (%s, %d nodes): %s",
|
||||
af == AF_INET6 ? "IPv6" : "IPv4",
|
||||
tr_dhtPrintableStatus(status),
|
||||
numnodes,
|
||||
tr_strerror(errno));
|
||||
return AnnounceResult::FAILED;
|
||||
}
|
||||
|
||||
tr_logAddTorInfo(
|
||||
tor,
|
||||
"Starting %s DHT announce (%s, %d nodes)",
|
||||
af == AF_INET6 ? "IPv6" : "IPv4",
|
||||
tr_dhtPrintableStatus(status),
|
||||
numnodes);
|
||||
|
||||
if (af == AF_INET)
|
||||
{
|
||||
tor->dhtAnnounceInProgress = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
tor->dhtAnnounce6InProgress = true;
|
||||
}
|
||||
|
||||
return AnnounceResult::OK;
|
||||
}
|
||||
|
||||
void tr_dhtUpkeep(tr_session* session)
|
||||
@@ -754,16 +746,18 @@ void tr_dhtUpkeep(tr_session* session)
|
||||
|
||||
if (tor->dhtAnnounceAt <= now)
|
||||
{
|
||||
int const rc = tr_dhtAnnounce(tor, AF_INET, true);
|
||||
auto const rc = tr_dhtAnnounce(tor, AF_INET, true);
|
||||
|
||||
tor->dhtAnnounceAt = now + ((rc == 0) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
||||
tor->dhtAnnounceAt = now +
|
||||
((rc == AnnounceResult::OK) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
||||
}
|
||||
|
||||
if (tor->dhtAnnounce6At <= now)
|
||||
{
|
||||
int const rc = tr_dhtAnnounce(tor, AF_INET6, true);
|
||||
auto const rc = tr_dhtAnnounce(tor, AF_INET6, true);
|
||||
|
||||
tor->dhtAnnounce6At = now + ((rc == 0) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
||||
tor->dhtAnnounce6At = now +
|
||||
((rc == AnnounceResult::OK) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -777,7 +771,7 @@ void tr_dhtCallback(unsigned char* buf, int buflen, struct sockaddr* from, sockl
|
||||
return;
|
||||
}
|
||||
|
||||
time_t tosleep;
|
||||
time_t tosleep = 0;
|
||||
int rc = dht_periodic(buf, buflen, from, fromlen, &tosleep, callback, nullptr);
|
||||
|
||||
if (rc < 0)
|
||||
|
||||
Reference in New Issue
Block a user