2009-05-19 18:38:26 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009 by Juliusz Chroboczek
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2009-05-20 15:56:58 +00:00
|
|
|
/* ansi */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2009-05-20 15:56:58 +00:00
|
|
|
|
|
|
|
/* posix */
|
|
|
|
#include <netinet/in.h> /* sockaddr_in */
|
|
|
|
#include <signal.h> /* sig_atomic_t */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2009-05-20 15:56:58 +00:00
|
|
|
#include <sys/socket.h> /* socket(), bind() */
|
|
|
|
#include <unistd.h> /* close() */
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-20 15:56:58 +00:00
|
|
|
/* third party */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include <event.h>
|
|
|
|
#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"
|
|
|
|
#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
|
|
|
|
#include "platform.h" /* tr_threadNew() */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "session.h"
|
2009-05-20 15:56:58 +00:00
|
|
|
#include "torrent.h" /* tr_torrentFindFromHash() */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "tr-dht.h"
|
2009-05-20 15:56:58 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "utils.h"
|
2009-05-20 03:55:09 +00:00
|
|
|
#include "version.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
static int dht_socket;
|
|
|
|
static struct event dht_event;
|
|
|
|
static tr_port dht_port;
|
|
|
|
static unsigned char myid[20];
|
|
|
|
static tr_session *session = NULL;
|
|
|
|
|
|
|
|
static void event_callback(int s, short type, void *ignore);
|
|
|
|
|
|
|
|
struct bootstrap_closure {
|
|
|
|
tr_session *session;
|
|
|
|
uint8_t *nodes;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
dht_bootstrap(void *closure)
|
|
|
|
{
|
|
|
|
struct bootstrap_closure *cl = closure;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if(session != cl->session)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(i = 0; i < cl->len; i += 6)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tr_port port;
|
|
|
|
struct tr_address addr;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
memcpy(&addr.addr.addr4, &cl->nodes[i], 4);
|
|
|
|
memcpy(&port, &cl->nodes[i + 4], 2);
|
|
|
|
port = ntohs(port);
|
|
|
|
/* There's no race here -- if we uninit between the test and the
|
|
|
|
AddNode, the AddNode will be ignored. */
|
2009-05-19 19:05:34 +00:00
|
|
|
status = tr_dhtStatus(cl->session, NULL);
|
2009-05-19 18:38:26 +00:00
|
|
|
if(status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED)
|
|
|
|
break;
|
|
|
|
tr_dhtAddNode(cl->session, &addr, port, 1);
|
2009-06-12 23:01:35 +00:00
|
|
|
tr_timevalSet( &tv, 2 + tr_cryptoWeakRandInt( 5 ), tr_cryptoWeakRandInt( 1000000 ) );
|
|
|
|
select( 0, NULL, NULL, NULL, &tv );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
tr_free( cl->nodes );
|
|
|
|
tr_free( closure );
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg( "DHT", "Finished bootstrapping" );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-08-07 14:47:46 +00:00
|
|
|
tr_dhtInit(tr_session *ss, tr_address * tr_addr)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
tr_benc benc;
|
|
|
|
int rc;
|
|
|
|
tr_bool have_id = FALSE;
|
|
|
|
char * dat_file;
|
|
|
|
uint8_t * nodes = NULL;
|
|
|
|
const uint8_t * raw;
|
|
|
|
size_t len;
|
2009-05-20 03:55:09 +00:00
|
|
|
char v[5];
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
if( session ) /* already initialized */
|
2009-05-19 18:38:26 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
dht_port = tr_sessionGetPeerPort(ss);
|
|
|
|
if(dht_port <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg( "DHT", "Initialising DHT" );
|
|
|
|
|
|
|
|
dht_socket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if(dht_socket < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
2009-08-07 14:47:46 +00:00
|
|
|
memcpy(&(sin.sin_addr), &(tr_addr->addr.addr4), sizeof (struct in_addr));
|
2009-05-19 18:38:26 +00:00
|
|
|
sin.sin_port = htons(dht_port);
|
|
|
|
rc = bind(dht_socket, (struct sockaddr*)&sin, sizeof(sin));
|
|
|
|
if(rc < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
if( getenv( "TR_DHT_VERBOSE" ) != NULL )
|
|
|
|
dht_debug = stderr;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
dat_file = tr_buildPath( ss->configDir, "dht.dat", NULL );
|
2009-06-02 01:48:48 +00:00
|
|
|
rc = tr_bencLoadFile( &benc, TR_FMT_BENC, dat_file );
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_free( dat_file );
|
|
|
|
if(rc == 0) {
|
2009-05-20 04:08:58 +00:00
|
|
|
if(( have_id = tr_bencDictFindRaw( &benc, "id", &raw, &len ) && len==20 ))
|
|
|
|
memcpy( myid, raw, len );
|
2009-08-07 14:38:09 +00:00
|
|
|
if( tr_bencDictFindRaw( &benc, "nodes", &raw, &len ) && !(len%6) ) {
|
2009-05-20 04:08:58 +00:00
|
|
|
nodes = tr_memdup( raw, len );
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ninf( "DHT", "Bootstrapping from %d old nodes", (int)(len/6) );
|
|
|
|
}
|
2009-05-20 04:08:58 +00:00
|
|
|
tr_bencFree( &benc );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
if( have_id )
|
|
|
|
tr_ninf( "DHT", "Reusing old id" );
|
|
|
|
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. */
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ninf( "DHT", "Generating new id" );
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_cryptoRandBuf( myid, 20 );
|
|
|
|
}
|
|
|
|
|
2009-05-20 03:58:15 +00:00
|
|
|
v[0] = 'T';
|
|
|
|
v[1] = 'R';
|
|
|
|
v[2] = (SVN_REVISION_NUM >> 8) & 0xFF;
|
|
|
|
v[3] = SVN_REVISION_NUM & 0xFF;
|
2009-05-20 03:55:09 +00:00
|
|
|
rc = dht_init( dht_socket, myid, (const unsigned char*)v );
|
2009-05-19 18:38:26 +00:00
|
|
|
if(rc < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
session = ss;
|
|
|
|
|
|
|
|
if(nodes) {
|
|
|
|
struct bootstrap_closure * cl = tr_new( struct bootstrap_closure, 1 );
|
2009-05-20 04:08:58 +00:00
|
|
|
cl->session = session;
|
|
|
|
cl->nodes = nodes;
|
|
|
|
cl->len = len;
|
|
|
|
tr_threadNew( dht_bootstrap, cl );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2009-05-20 04:08:58 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
event_set( &dht_event, dht_socket, EV_READ, event_callback, NULL );
|
2009-06-15 03:24:40 +00:00
|
|
|
tr_timerAdd( &dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg( "DHT", "DHT initialised" );
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
{
|
|
|
|
const int save = errno;
|
|
|
|
close(dht_socket);
|
|
|
|
dht_socket = -1;
|
|
|
|
session = NULL;
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg( "DHT", "DHT initialisation failed (errno = %d)", save );
|
2009-05-19 18:38:26 +00:00
|
|
|
errno = save;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_dhtUninit(tr_session *ss)
|
|
|
|
{
|
|
|
|
if(session != ss)
|
|
|
|
return;
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg( "DHT", "Uninitialising DHT" );
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
event_del(&dht_event);
|
|
|
|
|
|
|
|
/* Since we only save known good nodes, avoid erasing older data if we
|
|
|
|
don't know enough nodes. */
|
2009-08-07 14:38:09 +00:00
|
|
|
if(tr_dhtStatus(ss, NULL) < TR_DHT_FIREWALLED)
|
|
|
|
tr_ninf( "DHT", "Not saving nodes, DHT not ready" );
|
|
|
|
else {
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_benc benc;
|
|
|
|
struct sockaddr_in sins[300];
|
|
|
|
char compact[300 * 6];
|
|
|
|
char *dat_file;
|
2009-05-20 15:56:58 +00:00
|
|
|
int i;
|
|
|
|
int n = dht_get_nodes(sins, 300);
|
|
|
|
int j = 0;
|
2009-08-07 14:38:09 +00:00
|
|
|
|
|
|
|
tr_ninf( "DHT", "Saving %d nodes", n );
|
2009-05-20 15:56:58 +00:00
|
|
|
for( i=0; i<n; ++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-05-20 15:56:58 +00:00
|
|
|
tr_bencInitDict( &benc, 2 );
|
|
|
|
tr_bencDictAddRaw( &benc, "id", myid, 20 );
|
|
|
|
tr_bencDictAddRaw( &benc, "nodes", compact, j );
|
2009-05-19 18:38:26 +00:00
|
|
|
dat_file = tr_buildPath( ss->configDir, "dht.dat", NULL );
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToFile( &benc, TR_FMT_BENC, dat_file );
|
2009-06-03 23:42:13 +00:00
|
|
|
tr_bencFree( &benc );
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_free( dat_file );
|
|
|
|
}
|
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
dht_uninit( dht_socket, 0 );
|
|
|
|
EVUTIL_CLOSESOCKET( dht_socket );
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_ndbg("DHT", "Done uninitialising DHT");
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
2009-05-20 15:56:58 +00:00
|
|
|
tr_dhtEnabled( const tr_session * ss )
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-05-20 15:56:58 +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
|
|
|
|
{
|
|
|
|
sig_atomic_t status;
|
|
|
|
sig_atomic_t count;
|
|
|
|
};
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
static void
|
2009-05-20 15:56:58 +00:00
|
|
|
getstatus( void * closure )
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-05-20 04:08:58 +00:00
|
|
|
struct getstatus_closure * ret = closure;
|
2009-05-19 18:38:26 +00:00
|
|
|
int good, dubious, incoming;
|
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
dht_nodes( &good, &dubious, NULL, &incoming );
|
|
|
|
|
|
|
|
ret->count = good + dubious;
|
2009-05-19 19:05:34 +00:00
|
|
|
|
|
|
|
if( good < 4 || good + dubious <= 8 )
|
|
|
|
ret->status = TR_DHT_BROKEN;
|
|
|
|
else if( good < 40 )
|
|
|
|
ret->status = TR_DHT_POOR;
|
|
|
|
else if( incoming < 8 )
|
|
|
|
ret->status = TR_DHT_FIREWALLED;
|
2009-05-19 18:38:26 +00:00
|
|
|
else
|
2009-05-19 19:05:34 +00:00
|
|
|
ret->status = TR_DHT_GOOD;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-05-20 15:56:58 +00:00
|
|
|
tr_dhtStatus( tr_session * ss, int * nodes_return )
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-05-19 19:05:34 +00:00
|
|
|
struct getstatus_closure ret = { -1, - 1 };
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
if( !tr_dhtEnabled( ss ) )
|
2009-05-19 18:38:26 +00:00
|
|
|
return TR_DHT_STOPPED;
|
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
tr_runInEventThread( ss, getstatus, &ret );
|
|
|
|
while( ret.status < 0 )
|
|
|
|
tr_wait( 10 /*msec*/ );
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
if( nodes_return )
|
|
|
|
*nodes_return = ret.count;
|
|
|
|
|
|
|
|
return ret.status;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_port
|
2009-05-20 15:56:58 +00:00
|
|
|
tr_dhtPort( const tr_session *ss )
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
return tr_dhtEnabled( ss ) ? dht_port : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_dhtAddNode(tr_session *ss, tr_address *address, tr_port port,
|
|
|
|
tr_bool bootstrap)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
if( !tr_dhtEnabled( ss ) )
|
2009-05-19 18:38:26 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
if( address->type != TR_AF_INET )
|
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-05-19 18:38:26 +00:00
|
|
|
if(bootstrap) {
|
2009-05-19 19:05:34 +00:00
|
|
|
if(tr_dhtStatus(ss, NULL) >= TR_DHT_FIREWALLED)
|
2009-05-19 18:38:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
memcpy(&sin.sin_addr, &address->addr.addr4, 4);
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
dht_ping_node(dht_socket, &sin);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
const char *
|
|
|
|
tr_dhtPrintableStatus(int status)
|
|
|
|
{
|
|
|
|
switch(status) {
|
|
|
|
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
|
2009-05-20 04:08:58 +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
|
|
|
{
|
2009-05-20 04:08:58 +00:00
|
|
|
if( event == DHT_EVENT_VALUES )
|
|
|
|
{
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_torrent *tor;
|
2009-05-20 04:08:58 +00:00
|
|
|
tr_globalLock( session );
|
|
|
|
tor = tr_torrentFindFromHash( session, info_hash );
|
|
|
|
if( tor && tr_torrentAllowsDHT( tor ))
|
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
tr_pex * pex = tr_peerMgrCompactToPex(data, data_len, NULL, 0, &n);
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_peerMgrAddPex( tor, TR_PEER_FROM_DHT, pex+i );
|
|
|
|
tr_free(pex);
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_torinf(tor, "Learned %d peers from DHT", (int)n);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2009-05-20 04:08:58 +00:00
|
|
|
tr_globalUnlock( session );
|
|
|
|
}
|
|
|
|
else if( event == DHT_EVENT_SEARCH_DONE )
|
|
|
|
{
|
2009-05-19 19:05:34 +00:00
|
|
|
tr_torrent * tor = tr_torrentFindFromHash( session, info_hash );
|
2009-08-07 14:38:09 +00:00
|
|
|
if( tor ) {
|
|
|
|
tr_torinf(tor, "DHT announce done");
|
2009-05-19 18:38:26 +00:00
|
|
|
tor->dhtAnnounceInProgress = 0;
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_dhtAnnounce(tr_torrent *tor, tr_bool announce)
|
|
|
|
{
|
2009-08-07 14:38:09 +00:00
|
|
|
int rc, status, numnodes;
|
2009-06-01 18:24:30 +00:00
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
if( !tr_torrentAllowsDHT( tor ) )
|
2009-05-19 18:38:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
status = tr_dhtStatus( tor->session, &numnodes );
|
|
|
|
if(status < TR_DHT_POOR ) {
|
|
|
|
tr_tordbg(tor, "DHT not ready (%s, %d nodes)",
|
|
|
|
tr_dhtPrintableStatus(status), numnodes);
|
2009-05-19 18:38:26 +00:00
|
|
|
return 0;
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-06-01 18:24:30 +00:00
|
|
|
rc = dht_search( dht_socket, tor->info.hash,
|
|
|
|
announce ? tr_sessionGetPeerPort(session) : 0,
|
|
|
|
callback, NULL);
|
|
|
|
|
2009-08-07 14:38:09 +00:00
|
|
|
if( rc >= 1 ) {
|
|
|
|
tr_torinf(tor, "Starting DHT announce (%s, %d nodes)",
|
|
|
|
tr_dhtPrintableStatus(status), numnodes);
|
2009-06-01 18:24:30 +00:00
|
|
|
tor->dhtAnnounceInProgress = TRUE;
|
2009-08-07 14:38:09 +00:00
|
|
|
} else {
|
|
|
|
tr_torerr(tor, "DHT announce failed, errno = %d (%s, %d nodes)",
|
|
|
|
errno, tr_dhtPrintableStatus(status), numnodes);
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-19 19:39:56 +00:00
|
|
|
event_callback(int s, short type, void *ignore UNUSED )
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
|
|
|
time_t tosleep;
|
|
|
|
|
2009-05-19 20:42:01 +00:00
|
|
|
if( dht_periodic(s, type == EV_READ, &tosleep, callback, NULL) < 0 ) {
|
2009-05-19 18:38:26 +00:00
|
|
|
if(errno == EINTR) {
|
|
|
|
tosleep = 0;
|
|
|
|
} else {
|
2009-08-07 14:38:09 +00:00
|
|
|
tr_nerr("DHT", "dht_periodic failed (errno = %d)", errno);
|
2009-05-19 20:42:01 +00:00
|
|
|
if(errno == EINVAL || errno == EFAULT)
|
2009-05-19 18:38:26 +00:00
|
|
|
abort();
|
|
|
|
tosleep = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-19 20:42:01 +00:00
|
|
|
/* Being slightly late is fine,
|
|
|
|
and has the added benefit of adding some jitter. */
|
2009-06-15 03:24:40 +00:00
|
|
|
tr_timerAdd( &dht_event, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dht_hash(void *hash_return, int hash_size,
|
|
|
|
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];
|
|
|
|
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
|
|
|
|
dht_random_bytes( void * buf, size_t size )
|
|
|
|
{
|
|
|
|
tr_cryptoRandBuf( buf, size );
|
|
|
|
return size;
|
|
|
|
}
|