From 8ac7768b961d3ef90e4b4b7ac6fc6b2322f1aa7f Mon Sep 17 00:00:00 2001 From: denis <> Date: Fri, 16 Mar 2007 22:46:41 +0000 Subject: [PATCH] support for udp scrape --- opentracker.c | 15 +++++++++++---- trackerlogic.c | 28 +++++++++++++++++++++++++++- trackerlogic.h | 3 ++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/opentracker.c b/opentracker.c index 5e8ebc1..2fb6bf5 100644 --- a/opentracker.c +++ b/opentracker.c @@ -315,7 +315,7 @@ SCRAPE_WORKAROUND: } /* Enough for http header + whole scrape string */ - if( !( reply_size = return_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) ) HTTPERROR_500; + if( !( reply_size = return_tcp_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) ) HTTPERROR_500; ot_overall_tcp_successfulannounces++; break; @@ -590,7 +590,7 @@ static void handle_udp4( int64 serversocket ) { unsigned long *outpacket = (unsigned long*)static_outbuf; unsigned long numwant, left, event; uint16 port, remoteport; - size_t r; + size_t r, r_out; r = socket_recv4( serversocket, static_inbuf, 8192, remoteip, &remoteport); @@ -647,7 +647,7 @@ static void handle_udp4( int64 serversocket ) { if( !torrent ) return; /* XXX maybe send error */ - outpacket[0] = htonl( 1 ); + outpacket[0] = htonl( 1 ); /* announce action */ outpacket[1] = inpacket[12/4]; r = 8 + return_peers_for_torrent( torrent, numwant, static_outbuf + 8, 0 ); socket_send4( serversocket, static_outbuf, r, remoteip, remoteport ); @@ -656,7 +656,14 @@ static void handle_udp4( int64 serversocket ) { break; case 2: /* This is a scrape action */ - ot_overall_udp_connections--; // subtract again because we don't answer scrapes but it is also not an error + outpacket[0] = htonl( 2 ); /* scrape action */ + outpacket[1] = inpacket[12/4]; + + for( r_out = 0; ( r_out * 20 < r - 16) && ( r_out <= 74 ); r_out++ ) + return_udp_scrape_for_torrent( (ot_hash*)( static_inbuf + 16 + 20 * r_out ), static_outbuf + 8 + 12 * r_out ); + + socket_send4( serversocket, static_outbuf, 8 + 12 * r_out, remoteip, remoteport ); + ot_overall_udp_successfulannounces++; break; } } diff --git a/trackerlogic.c b/trackerlogic.c index bc9010e..ff6f45a 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -408,7 +408,33 @@ size_t return_memstat_for_tracker( char **reply ) { } /* Fetches scrape info for a specific torrent */ -size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) { +size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ) { + int exactmatch, i; + size_t peers = 0, seeds = 0; + ot_vector *torrents_list = &all_torrents[*hash[0]]; + ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); + + if( !exactmatch ) { + memset( reply, 0, 12); + } + else + { + unsigned long *r = (unsigned long*) reply; + clean_peerlist( NOW, torrent->peer_list ); + + for( i=0; ipeer_list->peers[i].size; + seeds += torrent->peer_list->seed_count[i]; + } + r[0] = seeds; + r[1] = torrent->peer_list->downloaded; + r[2] = peers-seeds; + } + return 12; +} + +/* Fetches scrape info for a specific torrent */ +size_t return_tcp_scrape_for_torrent( ot_hash *hash, char *reply ) { char *r = reply; int exactmatch, i; size_t peers = 0, seeds = 0; diff --git a/trackerlogic.h b/trackerlogic.h index 59d256e..fbc86ea 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -96,7 +96,8 @@ enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP }; ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply, int is_tcp ); size_t return_fullscrape_for_tracker( char **reply ); -size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); +size_t return_tcp_scrape_for_torrent( ot_hash *hash, char *reply ); +size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ); size_t return_sync_for_torrent( ot_hash *hash, char **reply ); size_t return_stats_for_tracker( char *reply, int mode ); size_t return_memstat_for_tracker( char **reply );