2009-05-19 18:38:26 +00:00
|
|
|
/*
|
2011-02-08 04:17:33 +00:00
|
|
|
* Copyright (c) 2009-2010 by Juliusz Chroboczek
|
2011-03-04 23:26:10 +00:00
|
|
|
*
|
2011-02-08 04:17:33 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2011-03-04 23:26:10 +00:00
|
|
|
*
|
2011-02-08 04:17:33 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2011-03-04 23:26:10 +00:00
|
|
|
*
|
2011-02-08 04:17:33 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-20 15:56:58 +00:00
|
|
|
/* ansi */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* memcpy (), memset (), memchr (), strlen () */
|
|
|
|
#include <stdlib.h> /* atoi () */
|
2009-05-20 15:56:58 +00:00
|
|
|
|
|
|
|
/* posix */
|
|
|
|
#include <signal.h> /* sig_atomic_t */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <sys/time.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <unistd.h> /* close () */
|
2010-06-22 00:12:52 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <inttypes.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#define _WIN32_WINNT 0x0501 /* freeaddrinfo (),getaddrinfo (),getnameinfo () */
|
2010-06-22 00:12:52 +00:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <sys/socket.h> /* socket (), bind () */
|
2010-06-22 00:12:52 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h> /* sockaddr_in */
|
|
|
|
#endif
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-20 15:56:58 +00:00
|
|
|
/* third party */
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.h>
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <dht/dht.h>
|
|
|
|
|
2009-05-20 15:56:58 +00:00
|
|
|
/* libT */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "transmission.h"
|
2009-05-20 15:56:58 +00:00
|
|
|
#include "bencode.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "crypto.h"
|
2009-05-20 15:56:58 +00:00
|
|
|
#include "net.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "peer-mgr.h" /* tr_peerMgrCompactToPex () */
|
|
|
|
#include "platform.h" /* tr_threadNew () */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "session.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "torrent.h" /* tr_torrentFindFromHash () */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "tr-dht.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread () */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2011-01-09 21:48:51 +00:00
|
|
|
static struct event *dht_timer = NULL;
|
2009-05-19 18:38:26 +00:00
|
|
|
static unsigned char myid[20];
|
|
|
|
static tr_session *session = NULL;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static void timer_callback (int s, short type, void *ignore);
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
struct bootstrap_closure {
|
|
|
|
tr_session *session;
|
|
|
|
uint8_t *nodes;
|
2009-11-24 01:59:51 +00:00
|
|
|
uint8_t *nodes6;
|
|
|
|
size_t len, len6;
|
2009-05-19 18:38:26 +00:00
|
|
|
};
|
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_done (tr_session *session, int af)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (af == 0)
|
2009-11-24 01:59:51 +00:00
|
|
|
return
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_done (session, AF_INET) &&
|
|
|
|
bootstrap_done (session, AF_INET6);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
status = tr_dhtStatus (session, af, NULL);
|
2009-11-24 01:59:51 +00:00
|
|
|
return status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
nap (int roughly_sec)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2010-06-30 21:24:36 +00:00
|
|
|
const int roughly_msec = roughly_sec * 1000;
|
2012-12-05 17:29:46 +00:00
|
|
|
const int msec = roughly_msec/2 + tr_cryptoWeakRandInt (roughly_msec);
|
|
|
|
tr_wait_msec (msec);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_af (tr_session *session)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap_done (session, AF_INET6))
|
2009-11-24 01:59:51 +00:00
|
|
|
return AF_INET;
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (bootstrap_done (session, AF_INET))
|
2009-11-24 01:59:51 +00:00
|
|
|
return AF_INET6;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_from_name (const char *name, tr_port port, int af)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
|
|
|
struct addrinfo hints, *info, *infop;
|
|
|
|
char pp[10];
|
|
|
|
int rc;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&hints, 0, sizeof (hints));
|
2009-11-24 01:59:51 +00:00
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_family = af;
|
|
|
|
/* No, just passing p + 1 to gai won't work. */
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (pp, sizeof (pp), "%d", (int)port);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
rc = getaddrinfo (name, pp, &hints, &info);
|
|
|
|
if (rc != 0) {
|
|
|
|
tr_nerr ("DHT", "%s:%s: %s", name, pp, gai_strerror (rc));
|
2009-11-24 01:59:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
infop = info;
|
2012-12-05 17:29:46 +00:00
|
|
|
while (infop) {
|
|
|
|
dht_ping_node (infop->ai_addr, infop->ai_addrlen);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
nap (15);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap_done (session, af))
|
2009-11-24 01:59:51 +00:00
|
|
|
break;
|
|
|
|
infop = infop->ai_next;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
freeaddrinfo (info);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_bootstrap (void *closure)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
struct bootstrap_closure *cl = closure;
|
2009-11-24 01:59:51 +00:00
|
|
|
int i;
|
|
|
|
int num = cl->len / 6, num6 = cl->len6 / 18;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session != cl->session)
|
2009-05-19 18:38:26 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cl->len > 0)
|
|
|
|
tr_ninf ("DHT", "Bootstrapping from %d IPv4 nodes", num);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cl->len6 > 0)
|
|
|
|
tr_ninf ("DHT", "Bootstrapping from %d IPv6 nodes", num6);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i = 0; i < MAX (num, num6); i++) {
|
|
|
|
if (i < num && !bootstrap_done (cl->session, AF_INET)) {
|
2009-11-24 01:59:51 +00:00
|
|
|
tr_port port;
|
|
|
|
struct tr_address addr;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&addr, 0, sizeof (addr));
|
2009-11-24 01:59:51 +00:00
|
|
|
addr.type = TR_AF_INET;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (&addr.addr.addr4, &cl->nodes[i * 6], 4);
|
|
|
|
memcpy (&port, &cl->nodes[i * 6 + 4], 2);
|
|
|
|
port = ntohs (port);
|
|
|
|
tr_dhtAddNode (cl->session, &addr, port, 1);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i < num6 && !bootstrap_done (cl->session, AF_INET6)) {
|
2009-11-24 01:59:51 +00:00
|
|
|
tr_port port;
|
|
|
|
struct tr_address addr;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&addr, 0, sizeof (addr));
|
2009-11-24 01:59:51 +00:00
|
|
|
addr.type = TR_AF_INET6;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (&addr.addr.addr6, &cl->nodes6[i * 18], 16);
|
|
|
|
memcpy (&port, &cl->nodes6[i * 18 + 16], 2);
|
|
|
|
port = ntohs (port);
|
|
|
|
tr_dhtAddNode (cl->session, &addr, port, 1);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Our DHT code is able to take up to 9 nodes in a row without
|
2010-12-27 19:18:17 +00:00
|
|
|
dropping any. After that, it takes some time to split buckets.
|
2009-11-24 01:59:51 +00:00
|
|
|
So ping the first 8 nodes quickly, then slow down. */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i < 8)
|
|
|
|
nap (2);
|
2009-11-24 01:59:51 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
nap (15);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap_done (session, 0))
|
2009-05-19 18:38:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!bootstrap_done (cl->session, 0)) {
|
2009-11-24 01:59:51 +00:00
|
|
|
char *bootstrap_file;
|
2009-12-28 23:27:17 +00:00
|
|
|
FILE *f = NULL;
|
2009-11-24 01:59:51 +00:00
|
|
|
|
|
|
|
bootstrap_file =
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_buildPath (cl->session->configDir, "dht.bootstrap", NULL);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap_file)
|
|
|
|
f = fopen (bootstrap_file, "rb");
|
|
|
|
if (f != NULL) {
|
|
|
|
tr_ninf ("DHT", "Attempting manual bootstrap");
|
|
|
|
for (;;) {
|
2009-11-24 01:59:51 +00:00
|
|
|
char buf[201];
|
|
|
|
char *p;
|
|
|
|
int port = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
p = fgets (buf, 200, f);
|
|
|
|
if (p == NULL)
|
2009-11-24 01:59:51 +00:00
|
|
|
break;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
p = memchr (buf, ' ', strlen (buf));
|
|
|
|
if (p != NULL)
|
|
|
|
port = atoi (p + 1);
|
|
|
|
if (p == NULL || port <= 0 || port >= 0x10000) {
|
|
|
|
tr_nerr ("DHT", "Couldn't parse %s", buf);
|
2009-11-24 01:59:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_from_name (buf, port, bootstrap_af (session));
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap_done (cl->session, 0))
|
2009-11-24 01:59:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
fclose (f);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2009-12-14 17:46:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (bootstrap_file);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!bootstrap_done (cl->session, 0)) {
|
|
|
|
for (i = 0; i < 6; i++) {
|
2011-02-15 19:10:56 +00:00
|
|
|
/* We don't want to abuse our bootstrap nodes, so be very
|
|
|
|
slow. The initial wait is to give other nodes a chance
|
|
|
|
to contact us before we attempt to contact a bootstrap
|
|
|
|
node, for example because we've just been restarted. */
|
2012-12-05 17:29:46 +00:00
|
|
|
nap (40);
|
|
|
|
if (bootstrap_done (cl->session, 0))
|
2011-02-15 19:10:56 +00:00
|
|
|
break;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i == 0)
|
|
|
|
tr_ninf ("DHT",
|
2011-02-15 19:10:56 +00:00
|
|
|
"Attempting bootstrap from dht.transmissionbt.com");
|
2012-12-05 17:29:46 +00:00
|
|
|
bootstrap_from_name ("dht.transmissionbt.com", 6881,
|
|
|
|
bootstrap_af (session));
|
2011-02-15 19:10:56 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cl->nodes)
|
|
|
|
tr_free (cl->nodes);
|
|
|
|
if (cl->nodes6)
|
|
|
|
tr_free (cl->nodes6);
|
|
|
|
tr_free (closure);
|
|
|
|
tr_ndbg ("DHT", "Finished bootstrapping");
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtInit (tr_session *ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
tr_benc benc;
|
|
|
|
int rc;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool have_id = false;
|
2009-05-19 18:38:26 +00:00
|
|
|
char * dat_file;
|
2009-11-24 01:59:51 +00:00
|
|
|
uint8_t * nodes = NULL, * nodes6 = NULL;
|
2009-05-19 18:38:26 +00:00
|
|
|
const uint8_t * raw;
|
2009-11-24 01:59:51 +00:00
|
|
|
size_t len, len6;
|
|
|
|
struct bootstrap_closure * cl;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session) /* already initialized */
|
2011-03-04 23:26:10 +00:00
|
|
|
return -1;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ndbg ("DHT", "Initializing DHT");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (getenv ("TR_DHT_VERBOSE") != NULL)
|
2009-05-20 04:08:58 +00:00
|
|
|
dht_debug = stderr;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dat_file = tr_buildPath (ss->configDir, "dht.dat", NULL);
|
|
|
|
rc = tr_bencLoadFile (&benc, TR_FMT_BENC, dat_file);
|
|
|
|
tr_free (dat_file);
|
|
|
|
if (rc == 0) {
|
|
|
|
have_id = tr_bencDictFindRaw (&benc, "id", &raw, &len);
|
|
|
|
if (have_id && len==20)
|
|
|
|
memcpy (myid, raw, len);
|
|
|
|
if (ss->udp_socket >= 0 &&
|
|
|
|
tr_bencDictFindRaw (&benc, "nodes", &raw, &len) && ! (len%6)) {
|
|
|
|
nodes = tr_memdup (raw, len);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ss->udp6_socket > 0 &&
|
|
|
|
tr_bencDictFindRaw (&benc, "nodes6", &raw, &len6) && ! (len6%18)) {
|
|
|
|
nodes6 = tr_memdup (raw, len6);
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bencFree (&benc);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nodes == NULL)
|
2009-11-24 01:59:51 +00:00
|
|
|
len = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nodes6 == NULL)
|
2009-11-24 01:59:51 +00:00
|
|
|
len6 = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (have_id)
|
|
|
|
tr_ninf ("DHT", "Reusing old id");
|
2009-08-07 14:38:09 +00:00
|
|
|
else {
|
2009-05-20 04:08:58 +00:00
|
|
|
/* Note that DHT ids need to be distributed uniformly,
|
|
|
|
* so it should be something truly random. */
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ninf ("DHT", "Generating new id");
|
|
|
|
tr_cryptoRandBuf (myid, 20);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
rc = dht_init (ss->udp_socket, ss->udp6_socket, myid, NULL);
|
|
|
|
if (rc < 0)
|
2009-05-19 18:38:26 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
session = ss;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
cl = tr_new (struct bootstrap_closure, 1);
|
2009-11-24 01:59:51 +00:00
|
|
|
cl->session = session;
|
|
|
|
cl->nodes = nodes;
|
|
|
|
cl->nodes6 = nodes6;
|
|
|
|
cl->len = len;
|
|
|
|
cl->len6 = len6;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_threadNew (dht_bootstrap, cl);
|
2009-05-20 04:08:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_timer = evtimer_new (session->event_base, timer_callback, session);
|
|
|
|
tr_timerAdd (dht_timer, 0, tr_cryptoWeakRandInt (1000000));
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ndbg ("DHT", "DHT initialized");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
return 1;
|
|
|
|
|
2011-01-09 21:48:36 +00:00
|
|
|
fail:
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ndbg ("DHT", "DHT initialization failed (errno = %d)", errno);
|
2011-01-09 21:48:36 +00:00
|
|
|
session = NULL;
|
2009-05-19 18:38:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtUninit (tr_session *ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session != ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ndbg ("DHT", "Uninitializing DHT");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (dht_timer != NULL) {
|
|
|
|
event_free (dht_timer);
|
2011-07-12 12:26:24 +00:00
|
|
|
dht_timer = NULL;
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
/* Since we only save known good nodes, avoid erasing older data if we
|
|
|
|
don't know enough nodes. */
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((tr_dhtStatus (ss, AF_INET, NULL) < TR_DHT_FIREWALLED) &&
|
|
|
|
(tr_dhtStatus (ss, AF_INET6, NULL) < TR_DHT_FIREWALLED)) {
|
|
|
|
tr_ninf ("DHT", "Not saving nodes, DHT not ready");
|
2012-11-11 14:42:14 +00:00
|
|
|
} else {
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_benc benc;
|
|
|
|
struct sockaddr_in sins[300];
|
2009-11-24 01:59:51 +00:00
|
|
|
struct sockaddr_in6 sins6[300];
|
|
|
|
char compact[300 * 6], compact6[300 * 18];
|
2009-05-19 18:38:26 +00:00
|
|
|
char *dat_file;
|
2009-11-24 01:59:51 +00:00
|
|
|
int i, j, num = 300, num6 = 300;
|
2012-12-05 17:29:46 +00:00
|
|
|
int n = dht_get_nodes (sins, &num, sins6, &num6);
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ninf ("DHT", "Saving %d (%d + %d) nodes", n, num, num6);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
|
|
|
j = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<num; ++i) {
|
|
|
|
memcpy (compact + j, &sins[i].sin_addr, 4);
|
|
|
|
memcpy (compact + j + 4, &sins[i].sin_port, 2);
|
2009-05-19 18:38:26 +00:00
|
|
|
j += 6;
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
j = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<num6; ++i) {
|
|
|
|
memcpy (compact6 + j, &sins6[i].sin6_addr, 16);
|
|
|
|
memcpy (compact6 + j + 16, &sins6[i].sin6_port, 2);
|
2009-11-24 01:59:51 +00:00
|
|
|
j += 18;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bencInitDict (&benc, 3);
|
|
|
|
tr_bencDictAddRaw (&benc, "id", myid, 20);
|
|
|
|
if (num > 0)
|
|
|
|
tr_bencDictAddRaw (&benc, "nodes", compact, num * 6);
|
|
|
|
if (num6 > 0)
|
|
|
|
tr_bencDictAddRaw (&benc, "nodes6", compact6, num6 * 18);
|
|
|
|
dat_file = tr_buildPath (ss->configDir, "dht.dat", NULL);
|
|
|
|
tr_bencToFile (&benc, TR_FMT_BENC, dat_file);
|
|
|
|
tr_bencFree (&benc);
|
|
|
|
tr_free (dat_file);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_uninit ();
|
|
|
|
tr_ndbg ("DHT", "Done uninitializing DHT");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtEnabled (const tr_session * ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return ss && (ss == session);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
struct getstatus_closure
|
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
int af;
|
2009-05-19 19:05:34 +00:00
|
|
|
sig_atomic_t status;
|
|
|
|
sig_atomic_t count;
|
|
|
|
};
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
getstatus (void * cl)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
struct getstatus_closure * closure = cl;
|
2009-05-19 18:38:26 +00:00
|
|
|
int good, dubious, incoming;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_nodes (closure->af, &good, &dubious, NULL, &incoming);
|
2009-05-20 04:08:58 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->count = good + dubious;
|
2009-05-19 19:05:34 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (good < 4 || good + dubious <= 8)
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_BROKEN;
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (good < 40)
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_POOR;
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (incoming < 8)
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_FIREWALLED;
|
2009-05-19 18:38:26 +00:00
|
|
|
else
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_GOOD;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtStatus (tr_session * session, int af, int * nodes_return)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
struct getstatus_closure closure = { af, -1, -1 };
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_dhtEnabled (session) ||
|
|
|
|
(af == AF_INET && session->udp_socket < 0) ||
|
|
|
|
(af == AF_INET6 && session->udp6_socket < 0)) {
|
|
|
|
if (nodes_return)
|
2009-11-29 18:31:36 +00:00
|
|
|
*nodes_return = 0;
|
2009-11-24 01:59:51 +00:00
|
|
|
return TR_DHT_STOPPED;
|
2009-11-29 18:31:36 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_runInEventThread (session, getstatus, &closure);
|
|
|
|
while (closure.status < 0)
|
|
|
|
tr_wait_msec (50 /*msec*/);
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nodes_return)
|
2009-11-24 01:59:51 +00:00
|
|
|
*nodes_return = closure.count;
|
2009-05-19 19:05:34 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
return closure.status;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_port
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtPort (tr_session *ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_dhtEnabled (ss) ? ss->udp_port : 0;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtAddNode (tr_session * ss,
|
2009-10-29 16:10:03 +00:00
|
|
|
const tr_address * address,
|
|
|
|
tr_port port,
|
2012-12-05 17:29:46 +00:00
|
|
|
bool bootstrap)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
int af = address->type == TR_AF_INET ? AF_INET : AF_INET6;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_dhtEnabled (ss))
|
2009-05-19 18:38:26 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
/* Since we don't want to abuse our bootstrap nodes,
|
|
|
|
* we don't ping them if the DHT is in a good state. */
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (bootstrap) {
|
|
|
|
if (tr_dhtStatus (ss, af, NULL) >= TR_DHT_FIREWALLED)
|
2009-05-19 18:38:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (address->type == TR_AF_INET) {
|
2009-11-24 01:59:51 +00:00
|
|
|
struct sockaddr_in sin;
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&sin, 0, sizeof (sin));
|
2009-11-24 01:59:51 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (&sin.sin_addr, &address->addr.addr4, 4);
|
|
|
|
sin.sin_port = htons (port);
|
|
|
|
dht_ping_node ((struct sockaddr*)&sin, sizeof (sin));
|
2009-11-24 01:59:51 +00:00
|
|
|
return 1;
|
2012-12-05 17:29:46 +00:00
|
|
|
} else if (address->type == TR_AF_INET6) {
|
2009-11-24 01:59:51 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (&sin6, 0, sizeof (sin6));
|
2009-11-24 01:59:51 +00:00
|
|
|
sin6.sin6_family = AF_INET6;
|
2012-12-05 17:29:46 +00:00
|
|
|
memcpy (&sin6.sin6_addr, &address->addr.addr6, 16);
|
|
|
|
sin6.sin6_port = htons (port);
|
|
|
|
dht_ping_node ((struct sockaddr*)&sin6, sizeof (sin6));
|
2009-11-24 01:59:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
return 0;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtPrintableStatus (int status)
|
2009-08-07 14:38:09 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (status) {
|
2009-08-07 14:38:09 +00:00
|
|
|
case TR_DHT_STOPPED: return "stopped";
|
|
|
|
case TR_DHT_BROKEN: return "broken";
|
|
|
|
case TR_DHT_POOR: return "poor";
|
|
|
|
case TR_DHT_FIREWALLED: return "firewalled";
|
|
|
|
case TR_DHT_GOOD: return "good";
|
|
|
|
default: return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
callback (void *ignore UNUSED, int event,
|
|
|
|
unsigned char *info_hash, void *data, size_t data_len)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (event == DHT_EVENT_VALUES || event == DHT_EVENT_VALUES6) {
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_torrent *tor;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionLock (session);
|
|
|
|
tor = tr_torrentFindFromHash (session, info_hash);
|
|
|
|
if (tor && tr_torrentAllowsDHT (tor))
|
2009-05-20 04:08:58 +00:00
|
|
|
{
|
|
|
|
size_t i, n;
|
2009-11-24 01:59:51 +00:00
|
|
|
tr_pex * pex;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (event == DHT_EVENT_VALUES)
|
|
|
|
pex = tr_peerMgrCompactToPex (data, data_len, NULL, 0, &n);
|
2009-11-24 01:59:51 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
pex = tr_peerMgrCompact6ToPex (data, data_len, NULL, 0, &n);
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
tr_peerMgrAddPex (tor, TR_PEER_FROM_DHT, pex+i, -1);
|
|
|
|
tr_free (pex);
|
|
|
|
tr_tordbg (tor, "Learned %d %s peers from DHT",
|
|
|
|
(int)n,
|
2011-07-25 19:53:39 +00:00
|
|
|
event == DHT_EVENT_VALUES6 ? "IPv6" : "IPv4");
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionUnlock (session);
|
|
|
|
} else if (event == DHT_EVENT_SEARCH_DONE ||
|
2009-11-24 01:59:51 +00:00
|
|
|
event == DHT_EVENT_SEARCH_DONE6) {
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent * tor = tr_torrentFindFromHash (session, info_hash);
|
|
|
|
if (tor) {
|
|
|
|
if (event == DHT_EVENT_SEARCH_DONE) {
|
2012-12-13 05:22:30 +00:00
|
|
|
tr_torinf (tor, "%s", "IPv4 DHT announce done");
|
2009-11-24 01:59:51 +00:00
|
|
|
tor->dhtAnnounceInProgress = 0;
|
|
|
|
} else {
|
2012-12-13 05:22:30 +00:00
|
|
|
tr_torinf (tor, "%s", "IPv6 DHT announce done");
|
2009-11-24 01:59:51 +00:00
|
|
|
tor->dhtAnnounce6InProgress = 0;
|
|
|
|
}
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-17 12:34:43 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtAnnounce (tr_torrent *tor, int af, bool announce)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
int rc, status, numnodes, ret = 0;
|
2009-06-01 18:24:30 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_torrentAllowsDHT (tor))
|
2009-05-19 18:38:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
status = tr_dhtStatus (tor->session, af, &numnodes);
|
2009-11-29 18:31:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (status == TR_DHT_STOPPED) {
|
2009-11-29 18:31:36 +00:00
|
|
|
/* Let the caller believe everything is all right. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (status >= TR_DHT_POOR) {
|
|
|
|
rc = dht_search (tor->info.hash,
|
|
|
|
announce ? tr_sessionGetPeerPort (session) : 0,
|
2009-11-24 01:59:51 +00:00
|
|
|
af, callback, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc >= 1) {
|
|
|
|
tr_torinf (tor, "Starting %s DHT announce (%s, %d nodes)",
|
2011-07-25 19:53:39 +00:00
|
|
|
af == AF_INET6 ? "IPv6" : "IPv4",
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtPrintableStatus (status), numnodes);
|
|
|
|
if (af == AF_INET)
|
2011-03-22 15:19:54 +00:00
|
|
|
tor->dhtAnnounceInProgress = true;
|
2009-11-24 01:59:51 +00:00
|
|
|
else
|
2011-03-22 15:19:54 +00:00
|
|
|
tor->dhtAnnounce6InProgress = true;
|
2009-11-24 01:59:51 +00:00
|
|
|
ret = 1;
|
|
|
|
} else {
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torerr (tor, "%s DHT announce failed (%s, %d nodes): %s",
|
2011-07-25 19:53:39 +00:00
|
|
|
af == AF_INET6 ? "IPv6" : "IPv4",
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtPrintableStatus (status), numnodes,
|
|
|
|
tr_strerror (errno));
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2009-08-07 14:38:09 +00:00
|
|
|
} else {
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_tordbg (tor, "%s DHT not ready (%s, %d nodes)",
|
2011-07-25 19:53:39 +00:00
|
|
|
af == AF_INET6 ? "IPv6" : "IPv4",
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtPrintableStatus (status), numnodes);
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
return ret;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 12:34:43 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtUpkeep (tr_session * session)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
2012-12-05 17:29:46 +00:00
|
|
|
const time_t now = tr_time ();
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((tor = tr_torrentNext (session, tor)))
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tor->isRunning || !tr_torrentAllowsDHT (tor))
|
2011-03-17 12:34:43 +00:00
|
|
|
continue;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->dhtAnnounceAt <= now)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int rc = tr_dhtAnnounce (tor, AF_INET, 1);
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2011-03-22 16:50:09 +00:00
|
|
|
tor->dhtAnnounceAt = now + ((rc == 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
? 5 + tr_cryptoWeakRandInt (5)
|
|
|
|
: 25 * 60 + tr_cryptoWeakRandInt (3*60));
|
2011-03-17 12:34:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->dhtAnnounce6At <= now)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int rc = tr_dhtAnnounce (tor, AF_INET6, 1);
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2011-03-22 16:50:09 +00:00
|
|
|
tor->dhtAnnounce6At = now + ((rc == 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
? 5 + tr_cryptoWeakRandInt (5)
|
|
|
|
: 25 * 60 + tr_cryptoWeakRandInt (3*60));
|
2011-03-17 12:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 21:48:46 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtCallback (unsigned char *buf, int buflen,
|
2011-01-09 21:48:46 +00:00
|
|
|
struct sockaddr *from, socklen_t fromlen,
|
2012-12-05 17:29:46 +00:00
|
|
|
void *sv)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
time_t tosleep;
|
2011-01-09 21:48:43 +00:00
|
|
|
int rc;
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_isSession (sv));
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (sv != session)
|
2011-01-09 21:48:46 +00:00
|
|
|
return;
|
2011-01-09 21:48:43 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
rc = dht_periodic (buf, buflen, from, fromlen,
|
2011-01-09 21:48:46 +00:00
|
|
|
&tosleep, callback, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EINTR) {
|
2009-05-19 18:38:26 +00:00
|
|
|
tosleep = 0;
|
|
|
|
} else {
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_nerr ("DHT", "dht_periodic failed: %s", tr_strerror (errno));
|
|
|
|
if (errno == EINVAL || errno == EFAULT)
|
|
|
|
abort ();
|
2009-05-19 18:38:26 +00:00
|
|
|
tosleep = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 21:48:46 +00:00
|
|
|
/* Being slightly late is fine,
|
|
|
|
and has the added benefit of adding some jitter. */
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_timerAdd (dht_timer, tosleep, tr_cryptoWeakRandInt (1000000));
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2011-01-09 21:48:46 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
timer_callback (int s UNUSED, short type UNUSED, void *session)
|
2011-01-09 21:48:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_dhtCallback (NULL, 0, NULL, 0, session);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 21:30:46 +00:00
|
|
|
/* This function should return true when a node is blacklisted. We do
|
|
|
|
not support using a blacklist with the DHT in Transmission, since
|
|
|
|
massive (ab)use of this feature could harm the DHT. However, feel
|
|
|
|
free to add support to your private copy as long as you don't
|
|
|
|
redistribute it. */
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_blacklisted (const struct sockaddr *sa UNUSED, int salen UNUSED)
|
2011-07-25 21:30:46 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_hash (void *hash_return, int hash_size,
|
2009-05-19 18:38:26 +00:00
|
|
|
const void *v1, int len1,
|
|
|
|
const void *v2, int len2,
|
|
|
|
const void *v3, int len3)
|
|
|
|
{
|
2009-05-20 04:08:58 +00:00
|
|
|
unsigned char sha1[SHA_DIGEST_LENGTH];
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sha1 (sha1, v1, len1, v2, len2, v3, len3, NULL);
|
|
|
|
memset (hash_return, 0, hash_size);
|
|
|
|
memcpy (hash_return, sha1, MIN (hash_size, SHA_DIGEST_LENGTH));
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2009-05-20 03:55:09 +00:00
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
dht_random_bytes (void * buf, size_t size)
|
2009-05-20 03:55:09 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoRandBuf (buf, size);
|
2009-05-20 03:55:09 +00:00
|
|
|
return size;
|
|
|
|
}
|