diff options
| author | erdgeist <> | 2007-01-20 11:13:30 +0000 |
|---|---|---|
| committer | erdgeist <> | 2007-01-20 11:13:30 +0000 |
| commit | 5f7c044b54d7711dd898fce9c959b27b35898437 (patch) | |
| tree | 1f4b273b54d915cb991a5aea8001c9a9361827b1 | |
| parent | 67689057d40de8ba21031a05b585cc7bf5366605 (diff) | |
Full scrape, kinda tested
| -rw-r--r-- | opentracker.c | 43 | ||||
| -rw-r--r-- | trackerlogic.c | 36 | ||||
| -rw-r--r-- | trackerlogic.h | 1 |
3 files changed, 74 insertions, 6 deletions
diff --git a/opentracker.c b/opentracker.c index df46f8d..2020b43 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -71,6 +71,29 @@ int header_complete(struct http_data* r) { | |||
| 71 | return 0; | 71 | return 0; |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | void sendmallocdata( int64 s, struct http_data *h, char * buffer, size_t size ) { | ||
| 75 | tai6464 t; | ||
| 76 | char *header; | ||
| 77 | size_t header_size; | ||
| 78 | |||
| 79 | if( !h ) { free( buffer); return; } | ||
| 80 | array_reset(&h->r); | ||
| 81 | |||
| 82 | header = malloc( SUCCESS_HTTP_HEADER_LENGTH ); | ||
| 83 | if( !header ) { free( buffer ); return; } | ||
| 84 | |||
| 85 | header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size ); | ||
| 86 | |||
| 87 | iob_reset( &h->batch ); | ||
| 88 | iob_addbuf_free( &h->batch, header, header_size ); | ||
| 89 | iob_addbuf_free( &h->batch, buffer, size ); | ||
| 90 | |||
| 91 | // writeable sockets just have a tcp timeout | ||
| 92 | taia_uint(&t,0); io_timeout( s, t ); | ||
| 93 | io_dontwantread( s ); | ||
| 94 | io_wantwrite( s ); | ||
| 95 | } | ||
| 96 | |||
| 74 | /* whoever sends data is not interested in its input-array */ | 97 | /* whoever sends data is not interested in its input-array */ |
| 75 | void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) { | 98 | void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) { |
| 76 | size_t written_size; | 99 | size_t written_size; |
| @@ -84,7 +107,6 @@ void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) { | |||
| 84 | #endif | 107 | #endif |
| 85 | free(h); io_close( s ); | 108 | free(h); io_close( s ); |
| 86 | } else { | 109 | } else { |
| 87 | /* here we would take a copy of the buffer and remember it */ | ||
| 88 | char * outbuf = malloc( size - written_size ); | 110 | char * outbuf = malloc( size - written_size ); |
| 89 | tai6464 t; | 111 | tai6464 t; |
| 90 | 112 | ||
| @@ -103,6 +125,8 @@ void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) { | |||
| 103 | 125 | ||
| 104 | // writeable sockets just have a tcp timeout | 126 | // writeable sockets just have a tcp timeout |
| 105 | taia_uint(&t,0); io_timeout( s, t ); | 127 | taia_uint(&t,0); io_timeout( s, t ); |
| 128 | io_dontwantread( s ); | ||
| 129 | io_wantwrite( s ); | ||
| 106 | } | 130 | } |
| 107 | } | 131 | } |
| 108 | 132 | ||
| @@ -221,13 +245,20 @@ e400_param: | |||
| 221 | } | 245 | } |
| 222 | } | 246 | } |
| 223 | 247 | ||
| 224 | /* Scanned whole query string, wo */ | 248 | /* Scanned whole query string, no hash means full scrape... you might want to limit that */ |
| 225 | if( !hash ) | 249 | if( !hash ) { |
| 226 | return httperror(s,h,"400 Invalid Request","This server only serves specific scrapes."); | 250 | char * reply; |
| 251 | |||
| 252 | reply_size = return_fullscrape_for_tracker( &reply ); | ||
| 253 | if( reply_size ) | ||
| 254 | return sendmallocdata( s, h, reply, reply_size ); | ||
| 227 | 255 | ||
| 228 | /* Enough for http header + whole scrape string */ | ||
| 229 | if( ( reply_size = return_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_scratch ) ) <= 0 ) | ||
| 230 | goto e500; | 256 | goto e500; |
| 257 | } else { | ||
| 258 | /* Enough for http header + whole scrape string */ | ||
| 259 | if( ( reply_size = return_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_scratch ) ) <= 0 ) | ||
| 260 | goto e500; | ||
| 261 | } | ||
| 231 | break; | 262 | break; |
| 232 | case 8: | 263 | case 8: |
| 233 | if( byte_diff(data,8,"announce")) | 264 | if( byte_diff(data,8,"announce")) |
diff --git a/trackerlogic.c b/trackerlogic.c index 6c988df..4f2ba68 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
| @@ -290,6 +290,42 @@ size_t return_peers_for_torrent( ot_torrent *torrent, unsigned int amount, char | |||
| 290 | return r - reply; | 290 | return r - reply; |
| 291 | } | 291 | } |
| 292 | 292 | ||
| 293 | /* Fetch full scrape info for all torrents */ | ||
| 294 | size_t return_fullscrape_for_tracker( char **reply ) { | ||
| 295 | int torrent_count = 0, i, j, k; | ||
| 296 | char* r; | ||
| 297 | time_t time_now = NOW; | ||
| 298 | |||
| 299 | for( i=0; i<256; ++i ) { | ||
| 300 | ot_vector *torrents_list = &all_torrents[i]; | ||
| 301 | torrent_count += torrents_list->size; | ||
| 302 | } | ||
| 303 | |||
| 304 | r = *reply = malloc( 128*torrent_count ); | ||
| 305 | if( !reply ) return 0; | ||
| 306 | |||
| 307 | memmove( r, "d5:filesd", 9 ); r += 9; | ||
| 308 | for( i=0; i<256; ++i ) { | ||
| 309 | ot_vector *torrents_list = &all_torrents[i]; | ||
| 310 | for( j=0; j<torrents_list->size; ++j ) { | ||
| 311 | ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; | ||
| 312 | ot_hash *hash =&( ((ot_torrent*)(torrents_list->data))[j] ).hash; | ||
| 313 | int peers = 0, seeds = 0; | ||
| 314 | clean_peerlist( time_now, peer_list ); | ||
| 315 | for( k=0; k<OT_POOLS_COUNT; ++k ) { | ||
| 316 | peers += peer_list->peers[k].size; | ||
| 317 | seeds += peer_list->seed_count[k]; | ||
| 318 | } | ||
| 319 | memmove( r, "20:", 3 ); r+=3; | ||
| 320 | memmove( r, hash, 20 ); r+=20; | ||
| 321 | r += sprintf( r, "d8:completei%de10:downloadedi%de10:incompletei%de", seeds, peer_list->downloaded, peers-seeds ); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | *r++='e'; *r++='e'; | ||
| 326 | return r - *reply; | ||
| 327 | } | ||
| 328 | |||
| 293 | /* Fetches scrape info for a specific torrent */ | 329 | /* Fetches scrape info for a specific torrent */ |
| 294 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) { | 330 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) { |
| 295 | char *r = reply; | 331 | char *r = reply; |
diff --git a/trackerlogic.h b/trackerlogic.h index 413c10b..b047d7d 100644 --- a/trackerlogic.h +++ b/trackerlogic.h | |||
| @@ -83,6 +83,7 @@ enum { STATS_MRTG, STATS_TOP5 }; | |||
| 83 | 83 | ||
| 84 | ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); | 84 | ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); |
| 85 | size_t return_peers_for_torrent( ot_torrent *torrent, unsigned int amount, char *reply ); | 85 | size_t return_peers_for_torrent( ot_torrent *torrent, unsigned int amount, char *reply ); |
| 86 | size_t return_fullscrape_for_tracker( char **reply ); | ||
| 86 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); | 87 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); |
| 87 | size_t return_stats_for_tracker( char *reply, int mode ); | 88 | size_t return_stats_for_tracker( char *reply, int mode ); |
| 88 | void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); | 89 | void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); |
