static void
callback (void *ignore UNUSED, int event,
- unsigned char *info_hash, void *data, size_t data_len)
+ const unsigned char *info_hash, const void *data, size_t data_len)
{
if (event == DHT_EVENT_VALUES || event == DHT_EVENT_VALUES6) {
tr_torrent *tor;
+3 May 2014: dht-0.22
+
+ * INCOMPATIBLE CHANGE: the callback now takes const arguments.
+ * Consult the local storage when performing a search, which should
+ make bootstrapping of tiny DHTs easier. Note that we're still not
+ performing local stores, since that would require knowing our IP
+ address.
+ * Don't attempt to flush the debug stream if debugging is disabled.
+ This appears to work around a bug in Transmission.
+
25 July 2011: dht-0.21
* Blacklisting support.
static void
callback(void *closure,
int event,
- unsigned char *info_hash,
- void *data, size_t data_len)
+ const unsigned char *info_hash,
+ const void *data, size_t data_len)
{
if(event == DHT_EVENT_SEARCH_DONE)
printf("Search done.\n");
struct storage *next;
};
+static struct storage * find_storage(const unsigned char *id);
static void flush_search_node(struct search_node *n, struct search *sr);
static int send_ping(const struct sockaddr *sa, int salen,
{
va_list args;
va_start(args, format);
- if(dht_debug) {
+ if(dht_debug)
vfprintf(dht_debug, format, args);
- fflush(dht_debug);
- }
va_end(args);
+ if(dht_debug)
+ fflush(dht_debug);
}
static void
dht_callback *callback, void *closure)
{
struct search *sr;
+ struct storage *st;
struct bucket *b = find_bucket(id, af);
if(b == NULL) {
return -1;
}
+ /* Try to answer this search locally. In a fully grown DHT this
+ is very unlikely, but people are running modified versions of
+ this code in private DHTs with very few nodes. What's wrong
+ with flooding? */
+ if(callback) {
+ st = find_storage(id);
+ if(st) {
+ unsigned short swapped;
+ unsigned char buf[18];
+ int i;
+
+ debugf("Found local data (%d peers).\n", st->numpeers);
+
+ for(i = 0; i < st->numpeers; i++) {
+ swapped = htons(st->peers[i].port);
+ if(st->peers[i].len == 4) {
+ memcpy(buf, st->peers[i].ip, 4);
+ memcpy(buf + 4, &swapped, 2);
+ (*callback)(closure, DHT_EVENT_VALUES, id,
+ (void*)buf, 6);
+ } else if(st->peers[i].len == 16) {
+ memcpy(buf, st->peers[i].ip, 16);
+ memcpy(buf + 16, &swapped, 2);
+ (*callback)(closure, DHT_EVENT_VALUES6, id,
+ (void*)buf, 18);
+ }
+ }
+ }
+ }
+
sr = searches;
while(sr) {
if(sr->af == af && id_cmp(sr->id, id) == 0)
typedef void
dht_callback(void *closure, int event,
- unsigned char *info_hash,
- void *data, size_t data_len);
+ const unsigned char *info_hash,
+ const void *data, size_t data_len);
#define DHT_EVENT_NONE 0
#define DHT_EVENT_VALUES 1