|
|
@ -276,32 +276,18 @@ static size_t return_peers_selection( struct ot_workstruct *ws, ot_peerlist *pee
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Compiles a list of random peers for a torrent
|
|
|
|
static size_t return_peers_for_torrent_udp( struct ot_workstruct * ws, ot_torrent *torrent, size_t amount, char *reply ) {
|
|
|
|
* Reply must have enough space to hold:
|
|
|
|
char *r = reply;
|
|
|
|
* 92 + 6 * amount bytes for TCP/IPv4
|
|
|
|
|
|
|
|
* 92 + 18 * amount bytes for TCP/IPv6
|
|
|
|
|
|
|
|
* 12 + 6 * amount bytes for UDP/IPv4
|
|
|
|
|
|
|
|
* 12 + 18 * amount bytes for UDP/IPv6
|
|
|
|
|
|
|
|
* Does not yet check not to return self
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t return_peers_for_torrent( struct ot_workstruct * ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto ) {
|
|
|
|
|
|
|
|
size_t peer_size = peer_size_from_peer6(&ws->peer);
|
|
|
|
size_t peer_size = peer_size_from_peer6(&ws->peer);
|
|
|
|
ot_peerlist *peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4;
|
|
|
|
ot_peerlist *peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4;
|
|
|
|
char *r = reply;
|
|
|
|
|
|
|
|
size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( amount > peer_list->peer_count )
|
|
|
|
if( amount > peer_list->peer_count )
|
|
|
|
amount = peer_list->peer_count;
|
|
|
|
amount = peer_list->peer_count;
|
|
|
|
|
|
|
|
|
|
|
|
if( proto == FLAG_TCP ) {
|
|
|
|
*(uint32_t*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM );
|
|
|
|
int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM;
|
|
|
|
*(uint32_t*)(r+4) = htonl( peer_list->peer_count - peer_list->seed_count );
|
|
|
|
r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie%s%zd:", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count, erval, erval/2, peer_size == OT_PEER_SIZE6 ? PEERS_BENCODED6 : PEERS_BENCODED4, compare_size * amount );
|
|
|
|
*(uint32_t*)(r+8) = htonl( peer_list->seed_count );
|
|
|
|
} else {
|
|
|
|
r += 12;
|
|
|
|
*(uint32_t*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM );
|
|
|
|
|
|
|
|
*(uint32_t*)(r+4) = htonl( peer_list->peer_count - peer_list->seed_count );
|
|
|
|
|
|
|
|
*(uint32_t*)(r+8) = htonl( peer_list->seed_count );
|
|
|
|
|
|
|
|
r += 12;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( amount ) {
|
|
|
|
if( amount ) {
|
|
|
|
if( amount == peer_list->peer_count )
|
|
|
|
if( amount == peer_list->peer_count )
|
|
|
@ -309,13 +295,87 @@ size_t return_peers_for_torrent( struct ot_workstruct * ws, ot_torrent *torrent,
|
|
|
|
else
|
|
|
|
else
|
|
|
|
r += return_peers_selection( ws, peer_list, peer_size, amount, r );
|
|
|
|
r += return_peers_selection( ws, peer_list, peer_size, amount, r );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r - reply;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static size_t return_peers_for_torrent_tcp( struct ot_workstruct * ws, ot_torrent *torrent, size_t amount, char *reply ) {
|
|
|
|
|
|
|
|
char *r = reply;
|
|
|
|
|
|
|
|
int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM;
|
|
|
|
|
|
|
|
size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
|
|
|
|
|
|
|
|
size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count;
|
|
|
|
|
|
|
|
size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - seed_count;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Simple case: amount of peers in both lists is less than requested, here we return all results */
|
|
|
|
|
|
|
|
size_t amount_v4 = torrent->peer_list4->peer_count;
|
|
|
|
|
|
|
|
size_t amount_v6 = torrent->peer_list6->peer_count;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Complex case: both lists have more than enough entries and we need to split between v4 and v6 clients */
|
|
|
|
|
|
|
|
if( amount_v4 + amount_v6 > amount ) {
|
|
|
|
|
|
|
|
size_t amount_left, percent_v6 = 0, percent_v4 = 0, left_v6, left_v4;
|
|
|
|
|
|
|
|
const size_t SCALE = 1024;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* If possible, fill at least a quarter of peer from each family */
|
|
|
|
|
|
|
|
if( amount / 4 <= amount_v4 )
|
|
|
|
|
|
|
|
amount_v4 = amount / 4;
|
|
|
|
|
|
|
|
if( amount / 4 <= amount_v6 )
|
|
|
|
|
|
|
|
amount_v6 = amount / 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Fill the rest according to which family's pool provides more peers */
|
|
|
|
|
|
|
|
amount_left = amount - (amount_v4 + amount_v6);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
left_v4 = torrent->peer_list4->peer_count - amount_v4;
|
|
|
|
|
|
|
|
left_v6 = torrent->peer_list6->peer_count - amount_v6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( left_v4 + left_v6 ) {
|
|
|
|
|
|
|
|
percent_v4 = (SCALE * left_v4) / (left_v4 + left_v6);
|
|
|
|
|
|
|
|
percent_v6 = (SCALE * left_v6) / (left_v4 + left_v6);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
amount_v4 += (amount_left * percent_v4) / SCALE;
|
|
|
|
|
|
|
|
amount_v6 += (amount_left * percent_v6) / SCALE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Integer division rounding can leave out a peer */
|
|
|
|
|
|
|
|
if( amount_v4 + amount_v6 < amount && amount_v6 < torrent->peer_list6->peer_count )
|
|
|
|
|
|
|
|
++amount_v6;
|
|
|
|
|
|
|
|
if( amount_v4 + amount_v6 < amount && amount_v4 < torrent->peer_list4->peer_count )
|
|
|
|
|
|
|
|
++amount_v4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie", seed_count, down_count, peer_count, erval, erval/2 );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( amount_v4 ) {
|
|
|
|
|
|
|
|
r += sprintf( r, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4);
|
|
|
|
|
|
|
|
if( amount_v4 == torrent->peer_list4->peer_count )
|
|
|
|
|
|
|
|
r += return_peers_all( torrent->peer_list4, OT_PEER_SIZE4, r );
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
r += return_peers_selection( ws, torrent->peer_list4, OT_PEER_SIZE4, amount_v4, r );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( proto == FLAG_TCP )
|
|
|
|
if( amount_v6 ) {
|
|
|
|
*r++ = 'e';
|
|
|
|
r += sprintf( r, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6);
|
|
|
|
|
|
|
|
if( amount_v6 == torrent->peer_list6->peer_count )
|
|
|
|
|
|
|
|
r += return_peers_all( torrent->peer_list6, OT_PEER_SIZE6, r );
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
r += return_peers_selection( ws, torrent->peer_list6, OT_PEER_SIZE6, amount_v6, r );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*r++ = 'e';
|
|
|
|
|
|
|
|
|
|
|
|
return r - reply;
|
|
|
|
return r - reply;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Compiles a list of random peers for a torrent
|
|
|
|
|
|
|
|
* Reply must have enough space to hold:
|
|
|
|
|
|
|
|
* 92 + 6 * amount bytes for TCP/IPv4
|
|
|
|
|
|
|
|
* 92 + 18 * amount bytes for TCP/IPv6
|
|
|
|
|
|
|
|
* 12 + 6 * amount bytes for UDP/IPv4
|
|
|
|
|
|
|
|
* 12 + 18 * amount bytes for UDP/IPv6
|
|
|
|
|
|
|
|
* Does not yet check not to return self
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t return_peers_for_torrent( struct ot_workstruct * ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto ) {
|
|
|
|
|
|
|
|
return proto == FLAG_TCP ? return_peers_for_torrent_tcp(ws, torrent, amount, reply) : return_peers_for_torrent_udp(ws, torrent, amount, reply);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Fetches scrape info for a specific torrent */
|
|
|
|
/* Fetches scrape info for a specific torrent */
|
|
|
|
size_t return_udp_scrape_for_torrent( ot_hash const hash, char *reply ) {
|
|
|
|
size_t return_udp_scrape_for_torrent( ot_hash const hash, char *reply ) {
|
|
|
|
int exactmatch, delta_torrentcount = 0;
|
|
|
|
int exactmatch, delta_torrentcount = 0;
|
|
|
|