Exemple #1
0
/*
* Lookup known nodes that are nearest to the given id.
*/
int kad_lookup_value( const char query[], IP addr_array[], size_t *addr_num ) {
	char hexbuf[SHA1_HEX_LENGTH+1];
	UCHAR id[SHA1_BIN_LENGTH];
	int rc;

	/* Generate the id, e.g. id = sha1(query) */
	id_compute( id, query );

	log_debug( "KAD: Lookup '%s' as '%s'.", query, str_id( id, hexbuf ) );

	dht_lock();

	rc = results_collect( id, addr_array, *addr_num );

	if( rc < 0 ) {
		/* No results item found - no search in progress - start search */
		dht_lock();
		results_add( id, query );
		dht_search( id, 0, gconf->af, dht_callback_func, NULL );
		dht_unlock();
		*addr_num = 0;
		rc = -1;
	} else {
		*addr_num = rc;
		rc = 0;
	}

	dht_unlock();

	return rc;
}
Exemple #2
0
/*
* Lookup known nodes that are nearest to the given id.
*/
int kad_lookup_value( const char _query[], IP addr_array[], size_t *addr_num ) {
	char query[QUERY_MAX_SIZE];
	struct results_t *results;
	int is_new;
	int rc;

	if( query_sanitize( query, sizeof(query), _query ) != 0 ) {
		return -2;
	}

	log_debug( "KAD: Lookup string: %s", query );

	dht_lock();

	/* Find existing or create new item */
	results = results_add( query, &is_new );

	if( results && is_new ) {
		/* Search own announced values */
		kad_lookup_local_values( results );
	}

	if( results == NULL ) {
		/* Failed to create a new search */
		rc = -1;
	} else if( results->done ) {
		/*
		* The search exists already but has finished. Restart the search when
		* no results have been found or more than half of the searches lifetime
		* has expired.
		*/
		if( results_entries_count( results ) == 0 ||
			(time_now_sec() - results->start_time) > (MAX_SEARCH_LIFETIME / 2)
		) {
			/* Mark search as in progress */
			results_done( results, 0 );

			/* Start another search for this id */
			dht_search( results->id, 0, gconf->af, dht_callback_func, NULL );
		}
		rc = 2;
	} else if( is_new ) {
		/* Start a new DHT search */
		dht_search( results->id, 0, gconf->af, dht_callback_func, NULL );
		rc = 1;
	} else {
		/* Search is still running */
		rc = 0;
	}

	/* Collect addresses to be returned */
	*addr_num = results_collect( results, addr_array, *addr_num );

	dht_unlock();

	return rc;
}