2008-06-07 21:26:41 +00:00
|
|
|
/*
|
2011-01-06 15:12:12 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-06-07 21:26:41 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2008-06-07 21:26:41 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-06-07 21:26:41 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
#include <string.h> /* strlen() */
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2011-01-06 01:00:21 +00:00
|
|
|
#include <event2/event.h>
|
2008-06-07 21:26:41 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2011-03-03 18:33:24 +00:00
|
|
|
#include "bandwidth.h"
|
2011-01-06 01:00:21 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "inout.h" /* tr_ioFindFileLocation() */
|
|
|
|
#include "list.h"
|
|
|
|
#include "peer-mgr.h"
|
2008-06-07 21:26:41 +00:00
|
|
|
#include "torrent.h"
|
2011-07-10 15:24:51 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2008-06-07 21:26:41 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "web.h"
|
|
|
|
#include "webseed.h"
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
struct tr_webseed_task
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
struct evbuffer * content;
|
|
|
|
struct tr_webseed * webseed;
|
|
|
|
tr_block_index_t block;
|
|
|
|
tr_piece_index_t piece_index;
|
|
|
|
uint32_t piece_offset;
|
|
|
|
uint32_t length;
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_block_index_t blocks_done;
|
|
|
|
uint32_t block_size;
|
|
|
|
struct tr_web_task * web_task;
|
|
|
|
long response_code;
|
2011-01-06 01:00:21 +00:00
|
|
|
};
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
struct tr_webseed
|
|
|
|
{
|
|
|
|
tr_peer parent;
|
2011-03-03 18:33:24 +00:00
|
|
|
tr_bandwidth bandwidth;
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_session * session;
|
|
|
|
tr_peer_callback * callback;
|
|
|
|
void * callback_data;
|
|
|
|
tr_list * tasks;
|
|
|
|
struct event * timer;
|
|
|
|
char * base_url;
|
|
|
|
size_t base_url_len;
|
|
|
|
int torrent_id;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool is_stopping;
|
2011-02-03 04:17:48 +00:00
|
|
|
int consecutive_failures;
|
2011-07-10 15:24:51 +00:00
|
|
|
int retry_tickcount;
|
|
|
|
int retry_challenge;
|
|
|
|
int idle_connections;
|
|
|
|
int active_transfers;
|
|
|
|
char ** file_urls;
|
|
|
|
};
|
|
|
|
|
2011-01-06 15:12:12 +00:00
|
|
|
enum
|
|
|
|
{
|
2011-02-03 04:17:48 +00:00
|
|
|
TR_IDLE_TIMER_MSEC = 2000,
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
FAILURE_RETRY_INTERVAL = 150,
|
|
|
|
|
|
|
|
MAX_CONSECUTIVE_FAILURES = 5,
|
|
|
|
|
|
|
|
MAX_WEBSEED_CONNECTIONS = 4
|
2011-01-06 15:12:12 +00:00
|
|
|
};
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
static void
|
|
|
|
webseed_free( struct tr_webseed * w )
|
|
|
|
{
|
2011-03-03 18:33:24 +00:00
|
|
|
tr_torrent * tor = tr_torrentFindFromId( w->session, w->torrent_id );
|
2011-07-10 15:24:51 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
|
|
|
tr_file_index_t i;
|
|
|
|
|
2011-09-12 20:41:28 +00:00
|
|
|
for( i=0; i<inf->fileCount; ++i )
|
|
|
|
tr_free( w->file_urls[i] );
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_free( w->file_urls );
|
2008-06-10 01:38:12 +00:00
|
|
|
|
2011-03-03 18:33:24 +00:00
|
|
|
/* webseed destruct */
|
2011-01-06 01:00:21 +00:00
|
|
|
event_free( w->timer );
|
2011-03-03 18:33:24 +00:00
|
|
|
tr_bandwidthDestruct( &w->bandwidth );
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_free( w->base_url );
|
2011-03-03 18:33:24 +00:00
|
|
|
|
|
|
|
/* parent class destruct */
|
|
|
|
tr_peerDestruct( tor, &w->parent );
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_free( w );
|
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2010-06-19 14:33:10 +00:00
|
|
|
publish( tr_webseed * w, tr_peer_event * e )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2010-06-19 14:33:10 +00:00
|
|
|
if( w->callback != NULL )
|
2011-01-06 01:00:21 +00:00
|
|
|
w->callback( &w->parent, e, w->callback_data );
|
2009-02-11 16:34:35 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
static void
|
2011-07-10 15:24:51 +00:00
|
|
|
fire_client_got_rejs( tr_torrent * tor, tr_webseed * w,
|
|
|
|
tr_block_index_t block, tr_block_index_t count )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_block_index_t i;
|
2011-02-17 05:14:53 +00:00
|
|
|
tr_peer_event e = TR_PEER_EVENT_INIT;
|
2011-02-03 04:17:48 +00:00
|
|
|
e.eventType = TR_PEER_CLIENT_GOT_REJ;
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_torrentGetBlockLocation( tor, block, &e.pieceIndex, &e.offset, &e.length );
|
2011-07-10 15:24:51 +00:00
|
|
|
for( i = 1; i <= count; i++ ) {
|
|
|
|
if( i == count )
|
|
|
|
e.length = tr_torBlockCountBytes( tor, block + count - 1 );
|
|
|
|
publish( w, &e );
|
|
|
|
e.offset += e.length;
|
|
|
|
}
|
2011-02-03 04:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-10 15:24:51 +00:00
|
|
|
fire_client_got_blocks( tr_torrent * tor, tr_webseed * w,
|
|
|
|
tr_block_index_t block, tr_block_index_t count )
|
2011-02-03 04:17:48 +00:00
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_block_index_t i;
|
2011-02-17 05:14:53 +00:00
|
|
|
tr_peer_event e = TR_PEER_EVENT_INIT;
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_CLIENT_GOT_BLOCK;
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_torrentGetBlockLocation( tor, block, &e.pieceIndex, &e.offset, &e.length );
|
2011-07-10 15:24:51 +00:00
|
|
|
for( i = 1; i <= count; i++ ) {
|
|
|
|
if( i == count )
|
|
|
|
e.length = tr_torBlockCountBytes( tor, block + count - 1 );
|
|
|
|
publish( w, &e );
|
|
|
|
e.offset += e.length;
|
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-01-06 15:12:12 +00:00
|
|
|
fire_client_got_data( tr_webseed * w, uint32_t length )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-02-17 05:14:53 +00:00
|
|
|
tr_peer_event e = TR_PEER_EVENT_INIT;
|
2008-06-07 21:26:41 +00:00
|
|
|
e.eventType = TR_PEER_CLIENT_GOT_DATA;
|
|
|
|
e.length = length;
|
2011-03-22 15:19:54 +00:00
|
|
|
e.wasPieceData = true;
|
2008-06-07 21:26:41 +00:00
|
|
|
publish( w, &e );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
struct write_block_data
|
|
|
|
{
|
|
|
|
struct tr_webseed * webseed;
|
|
|
|
struct evbuffer * content;
|
|
|
|
tr_piece_index_t piece_index;
|
|
|
|
tr_block_index_t block_index;
|
|
|
|
tr_block_index_t count;
|
|
|
|
uint32_t block_offset;
|
|
|
|
};
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
static void
|
2011-09-12 21:46:15 +00:00
|
|
|
write_block_func( void * vdata )
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
struct write_block_data * data = vdata;
|
|
|
|
struct tr_webseed * w = data->webseed;
|
|
|
|
struct evbuffer * buf = data->content;
|
2011-07-10 15:24:51 +00:00
|
|
|
struct tr_torrent * tor;
|
|
|
|
|
|
|
|
tor = tr_torrentFindFromId( w->session, w->torrent_id );
|
|
|
|
if( tor )
|
|
|
|
{
|
|
|
|
const uint32_t block_size = tor->blockSize;
|
|
|
|
uint32_t len = evbuffer_get_length( buf );
|
2011-09-12 21:46:15 +00:00
|
|
|
const uint32_t offset_end = data->block_offset + len;
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_cache * cache = w->session->cache;
|
2011-09-12 21:46:15 +00:00
|
|
|
const tr_piece_index_t piece = data->piece_index;
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
while( len > 0 )
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
const uint32_t bytes_this_pass = MIN( len, block_size );
|
|
|
|
tr_cacheWriteBlock( cache, tor, piece, offset_end - len, bytes_this_pass, buf );
|
|
|
|
len -= bytes_this_pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
fire_client_got_blocks( tor, w, data->block_index, data->count );
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
evbuffer_free( buf );
|
2011-09-12 21:46:15 +00:00
|
|
|
tr_free( data );
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct connection_succeeded_data
|
|
|
|
{
|
|
|
|
struct tr_webseed * webseed;
|
|
|
|
char * real_url;
|
|
|
|
tr_piece_index_t piece_index;
|
|
|
|
uint32_t piece_offset;
|
|
|
|
};
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
static void
|
2011-09-12 21:46:15 +00:00
|
|
|
connection_succeeded( void * vdata )
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
struct connection_succeeded_data * data = vdata;
|
|
|
|
struct tr_webseed * w = data->webseed;
|
2011-07-10 15:24:51 +00:00
|
|
|
|
|
|
|
if( ++w->active_transfers >= w->retry_challenge && w->retry_challenge )
|
|
|
|
/* the server seems to be accepting more connections now */
|
|
|
|
w->consecutive_failures = w->retry_tickcount = w->retry_challenge = 0;
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
if( data->real_url &&
|
2011-07-10 15:24:51 +00:00
|
|
|
(tor = tr_torrentFindFromId( w->session, w->torrent_id )))
|
|
|
|
{
|
|
|
|
uint64_t file_offset;
|
|
|
|
tr_file_index_t file_index;
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
tr_ioFindFileLocation( tor, data->piece_index, data->piece_offset,
|
2011-07-10 15:24:51 +00:00
|
|
|
&file_index, &file_offset );
|
|
|
|
tr_free( w->file_urls[file_index] );
|
2011-09-12 21:46:15 +00:00
|
|
|
w->file_urls[file_index] = data->real_url;
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
static void
|
|
|
|
on_content_changed( struct evbuffer * buf,
|
2011-01-06 01:00:21 +00:00
|
|
|
const struct evbuffer_cb_info * info,
|
2011-07-10 15:24:51 +00:00
|
|
|
void * vtask )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
uint32_t len;
|
|
|
|
const size_t n_added = info->n_added;
|
2011-07-10 15:24:51 +00:00
|
|
|
struct tr_webseed_task * task = vtask;
|
|
|
|
struct tr_webseed * w = task->webseed;
|
2009-11-22 03:57:36 +00:00
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
if( n_added <= 0 )
|
2011-07-10 15:24:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( !w->is_stopping )
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
tr_bandwidthUsed( &w->bandwidth, TR_DOWN, n_added, true, tr_time_msec( ) );
|
|
|
|
fire_client_got_data( w, n_added );
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2011-09-12 05:39:27 +00:00
|
|
|
len = evbuffer_get_length( buf );
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
if( !task->response_code )
|
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
tr_webGetTaskInfo( task->web_task, TR_WEB_GET_CODE, &task->response_code );
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
if( task->response_code == 206 )
|
|
|
|
{
|
|
|
|
const char * url;
|
|
|
|
struct connection_succeeded_data * data;
|
|
|
|
|
|
|
|
url = NULL;
|
|
|
|
tr_webGetTaskInfo( task->web_task, TR_WEB_GET_REAL_URL, &url );
|
|
|
|
|
|
|
|
data = tr_new( struct connection_succeeded_data, 1 );
|
|
|
|
data->webseed = w;
|
|
|
|
data->real_url = tr_strdup( url );
|
|
|
|
data->piece_index = task->piece_index;
|
|
|
|
data->piece_offset = task->piece_offset
|
|
|
|
+ (task->blocks_done * task->block_size)
|
|
|
|
+ (len - 1);
|
|
|
|
|
|
|
|
/* processing this uses a tr_torrent pointer,
|
|
|
|
so push the work to the libevent thread... */
|
|
|
|
tr_runInEventThread( w->session, connection_succeeded, data );
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
if( ( task->response_code == 206 ) && ( len >= task->block_size ) )
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2011-09-12 21:46:15 +00:00
|
|
|
/* once we've got at least one full block, save it */
|
|
|
|
|
|
|
|
struct write_block_data * data;
|
2011-07-10 15:24:51 +00:00
|
|
|
const uint32_t block_size = task->block_size;
|
|
|
|
const tr_block_index_t completed = len / block_size;
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
data = tr_new( struct write_block_data, 1 );
|
|
|
|
data->webseed = task->webseed;
|
|
|
|
data->piece_index = task->piece_index;
|
|
|
|
data->block_index = task->block + task->blocks_done;
|
|
|
|
data->count = completed;
|
|
|
|
data->block_offset = task->piece_offset + task->blocks_done * block_size;
|
|
|
|
data->content = evbuffer_new( );
|
2011-07-10 15:24:51 +00:00
|
|
|
|
|
|
|
/* we don't use locking on this evbuffer so we must copy out the data
|
|
|
|
that will be needed when writing the block in a different thread */
|
2011-09-12 21:46:15 +00:00
|
|
|
evbuffer_remove_buffer( task->content, data->content,
|
2011-07-10 15:24:51 +00:00
|
|
|
block_size * completed );
|
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
tr_runInEventThread( w->session, write_block_func, data );
|
2011-07-10 15:24:51 +00:00
|
|
|
task->blocks_done += completed;
|
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
static void task_request_next_chunk( struct tr_webseed_task * task );
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2011-01-23 16:35:23 +00:00
|
|
|
webseed_has_tasks( const tr_webseed * w )
|
|
|
|
{
|
|
|
|
return w->tasks != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
static void
|
2011-01-06 01:00:21 +00:00
|
|
|
on_idle( tr_webseed * w )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_torrent * tor = tr_torrentFindFromId( w->session, w->torrent_id );
|
2011-07-10 15:24:51 +00:00
|
|
|
int want, running_tasks = tr_list_size( w->tasks );
|
|
|
|
|
|
|
|
if( w->consecutive_failures >= MAX_CONSECUTIVE_FAILURES ) {
|
|
|
|
want = w->idle_connections;
|
|
|
|
|
|
|
|
if( w->retry_tickcount >= FAILURE_RETRY_INTERVAL ) {
|
|
|
|
/* some time has passed since our connection attempts failed. try again */
|
|
|
|
++want;
|
|
|
|
/* if this challenge is fulfilled we will reset consecutive_failures */
|
|
|
|
w->retry_challenge = running_tasks + want;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
want = MAX_WEBSEED_CONNECTIONS - running_tasks;
|
|
|
|
w->retry_challenge = running_tasks + w->idle_connections + 1;
|
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-01-23 16:35:23 +00:00
|
|
|
if( w->is_stopping && !webseed_has_tasks( w ) )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
webseed_free( w );
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
2011-02-03 04:17:48 +00:00
|
|
|
else if( !w->is_stopping && tor
|
|
|
|
&& tor->isRunning
|
|
|
|
&& !tr_torrentIsSeed( tor )
|
2011-07-10 15:24:51 +00:00
|
|
|
&& want )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
int i;
|
|
|
|
int got = 0;
|
|
|
|
tr_block_index_t * blocks = NULL;
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
blocks = tr_new( tr_block_index_t, want*2 );
|
|
|
|
tr_peerMgrGetNextRequests( tor, &w->parent, want, blocks, &got, true );
|
|
|
|
|
|
|
|
w->idle_connections -= MIN( w->idle_connections, got );
|
|
|
|
if( w->retry_tickcount >= FAILURE_RETRY_INTERVAL && got == want )
|
|
|
|
w->retry_tickcount = 0;
|
2008-06-09 22:53:45 +00:00
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
for( i=0; i<got; ++i )
|
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
const tr_block_index_t b = blocks[i*2];
|
|
|
|
const tr_block_index_t be = blocks[i*2+1];
|
2011-01-06 01:00:21 +00:00
|
|
|
struct tr_webseed_task * task = tr_new( struct tr_webseed_task, 1 );
|
|
|
|
task->webseed = w;
|
|
|
|
task->block = b;
|
|
|
|
task->piece_index = tr_torBlockPiece( tor, b );
|
|
|
|
task->piece_offset = ( tor->blockSize * b )
|
2011-01-06 15:12:12 +00:00
|
|
|
- ( tor->info.pieceSize * task->piece_index );
|
2011-07-10 15:24:51 +00:00
|
|
|
task->length = (be - b) * tor->blockSize + tr_torBlockCountBytes( tor, be );
|
|
|
|
task->blocks_done = 0;
|
|
|
|
task->response_code = 0;
|
|
|
|
task->block_size = tor->blockSize;
|
2011-01-06 01:00:21 +00:00
|
|
|
task->content = evbuffer_new( );
|
2011-07-10 15:24:51 +00:00
|
|
|
evbuffer_add_cb( task->content, on_content_changed, task );
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_list_append( &w->tasks, task );
|
|
|
|
task_request_next_chunk( task );
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
2011-01-06 01:00:21 +00:00
|
|
|
|
|
|
|
tr_free( blocks );
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
static void
|
2011-01-06 01:00:21 +00:00
|
|
|
web_response_func( tr_session * session,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool did_connect UNUSED,
|
|
|
|
bool did_timeout UNUSED,
|
2011-01-06 01:00:21 +00:00
|
|
|
long response_code,
|
|
|
|
const void * response UNUSED,
|
|
|
|
size_t response_byte_count UNUSED,
|
|
|
|
void * vtask )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
struct tr_webseed_task * t = vtask;
|
2011-02-03 04:17:48 +00:00
|
|
|
tr_webseed * w = t->webseed;
|
2011-09-12 20:40:09 +00:00
|
|
|
tr_torrent * tor = tr_torrentFindFromId( session, w->torrent_id );
|
2011-01-06 01:00:21 +00:00
|
|
|
const int success = ( response_code == 206 );
|
2008-11-08 22:24:07 +00:00
|
|
|
|
2011-02-03 04:17:48 +00:00
|
|
|
if( tor )
|
2008-11-08 22:24:07 +00:00
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
/* active_transfers was only increased if the connection was successful */
|
|
|
|
if( t->response_code == 206 )
|
|
|
|
--w->active_transfers;
|
|
|
|
|
2011-02-03 04:17:48 +00:00
|
|
|
if( !success )
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
const tr_block_index_t blocks_remain = (t->length + tor->blockSize - 1)
|
|
|
|
/ tor->blockSize - t->blocks_done;
|
|
|
|
|
|
|
|
if( blocks_remain )
|
|
|
|
fire_client_got_rejs( tor, w, t->block + t->blocks_done, blocks_remain );
|
|
|
|
|
|
|
|
if( t->blocks_done )
|
|
|
|
++w->idle_connections;
|
|
|
|
else if( ++w->consecutive_failures >= MAX_CONSECUTIVE_FAILURES && !w->retry_tickcount )
|
|
|
|
/* now wait a while until retrying to establish a connection */
|
|
|
|
++w->retry_tickcount;
|
2011-01-06 01:00:21 +00:00
|
|
|
|
|
|
|
tr_list_remove_data( &w->tasks, t );
|
|
|
|
evbuffer_free( t->content );
|
|
|
|
tr_free( t );
|
2011-02-03 04:17:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
const uint32_t bytes_done = t->blocks_done * tor->blockSize;
|
|
|
|
const uint32_t buf_len = evbuffer_get_length( t->content );
|
|
|
|
|
|
|
|
if( bytes_done + buf_len < t->length )
|
2011-02-03 04:17:48 +00:00
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
/* request finished successfully but there's still data missing. that
|
|
|
|
means we've reached the end of a file and need to request the next one */
|
|
|
|
t->response_code = 0;
|
2011-02-03 04:17:48 +00:00
|
|
|
task_request_next_chunk( t );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-10 15:24:51 +00:00
|
|
|
if( buf_len ) {
|
|
|
|
/* on_content_changed() will not write a block if it is smaller than
|
|
|
|
the torrent's block size, i.e. the torrent's very last block */
|
|
|
|
tr_cacheWriteBlock( session->cache, tor,
|
|
|
|
t->piece_index, t->piece_offset + bytes_done,
|
|
|
|
buf_len, t->content );
|
|
|
|
|
|
|
|
fire_client_got_blocks( tor, t->webseed,
|
|
|
|
t->block + t->blocks_done, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
++w->idle_connections;
|
2011-02-03 04:17:48 +00:00
|
|
|
|
|
|
|
tr_list_remove_data( &w->tasks, t );
|
|
|
|
evbuffer_free( t->content );
|
|
|
|
tr_free( t );
|
|
|
|
|
|
|
|
on_idle( w );
|
|
|
|
}
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
2008-11-08 22:24:07 +00:00
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 20:52:07 +00:00
|
|
|
static struct evbuffer *
|
2011-09-06 16:10:27 +00:00
|
|
|
make_url( tr_webseed * w, const tr_file * file )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-04-27 20:52:07 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-04-27 20:52:07 +00:00
|
|
|
evbuffer_add( buf, w->base_url, w->base_url_len );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
2011-09-06 16:10:27 +00:00
|
|
|
/* if url ends with a '/', add the torrent name */
|
|
|
|
if( w->base_url[w->base_url_len - 1] == '/' && file->name )
|
2011-04-27 20:52:07 +00:00
|
|
|
tr_http_escape( buf, file->name, strlen(file->name), false );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
2011-04-27 20:52:07 +00:00
|
|
|
return buf;
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
task_request_next_chunk( struct tr_webseed_task * t )
|
|
|
|
{
|
2011-09-12 20:40:09 +00:00
|
|
|
tr_webseed * w = t->webseed;
|
|
|
|
tr_torrent * tor = tr_torrentFindFromId( w->session, w->torrent_id );
|
2011-01-06 01:00:21 +00:00
|
|
|
if( tor != NULL )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2011-01-06 01:00:21 +00:00
|
|
|
char range[64];
|
2011-07-10 15:24:51 +00:00
|
|
|
char ** urls = t->webseed->file_urls;
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2011-07-10 15:24:51 +00:00
|
|
|
const uint32_t remain = t->length - t->blocks_done * tor->blockSize
|
|
|
|
- evbuffer_get_length( t->content );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
|
|
|
const uint64_t total_offset = inf->pieceSize * t->piece_index
|
2011-07-10 15:24:51 +00:00
|
|
|
+ t->piece_offset + t->length - remain;
|
2011-01-06 01:00:21 +00:00
|
|
|
const tr_piece_index_t step_piece = total_offset / inf->pieceSize;
|
2011-01-06 15:12:12 +00:00
|
|
|
const uint32_t step_piece_offset
|
|
|
|
= total_offset - ( inf->pieceSize * step_piece );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
|
|
|
tr_file_index_t file_index;
|
|
|
|
uint64_t file_offset;
|
|
|
|
const tr_file * file;
|
|
|
|
uint32_t this_pass;
|
|
|
|
|
|
|
|
tr_ioFindFileLocation( tor, step_piece, step_piece_offset,
|
|
|
|
&file_index, &file_offset );
|
|
|
|
file = &inf->files[file_index];
|
|
|
|
this_pass = MIN( remain, file->length - file_offset );
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
if( !urls[file_index] )
|
2011-09-06 16:10:27 +00:00
|
|
|
urls[file_index] = evbuffer_free_to_str( make_url( t->webseed, file ) );
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_snprintf( range, sizeof range, "%"PRIu64"-%"PRIu64,
|
|
|
|
file_offset, file_offset + this_pass - 1 );
|
2011-09-12 20:40:09 +00:00
|
|
|
t->web_task = tr_webRunWithBuffer( w->session, urls[file_index],
|
2011-07-10 15:24:51 +00:00
|
|
|
range, NULL, web_response_func, t, t->content );
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2011-01-23 16:35:23 +00:00
|
|
|
tr_webseedGetSpeed_Bps( const tr_webseed * w, uint64_t now, int * setme_Bps )
|
2008-06-10 02:36:52 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
const bool is_active = webseed_has_tasks( w );
|
2011-03-03 18:33:24 +00:00
|
|
|
*setme_Bps = is_active ? tr_bandwidthGetPieceSpeed_Bps( &w->bandwidth, now, TR_DOWN ) : 0;
|
2011-01-23 16:35:23 +00:00
|
|
|
return is_active;
|
2008-06-10 02:36:52 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2011-01-23 16:35:23 +00:00
|
|
|
tr_webseedIsActive( const tr_webseed * w )
|
2008-06-10 01:38:12 +00:00
|
|
|
{
|
2011-01-23 16:35:23 +00:00
|
|
|
int Bps = 0;
|
|
|
|
return tr_webseedGetSpeed_Bps( w, tr_time_msec(), &Bps ) && ( Bps > 0 );
|
2008-06-10 01:38:12 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-01-06 01:00:21 +00:00
|
|
|
static void
|
|
|
|
webseed_timer_func( evutil_socket_t foo UNUSED, short bar UNUSED, void * vw )
|
|
|
|
{
|
|
|
|
tr_webseed * w = vw;
|
2011-07-10 15:24:51 +00:00
|
|
|
if( w->retry_tickcount )
|
|
|
|
++w->retry_tickcount;
|
2011-01-06 01:00:21 +00:00
|
|
|
on_idle( w );
|
2011-01-06 15:12:12 +00:00
|
|
|
tr_timerAddMsec( w->timer, TR_IDLE_TIMER_MSEC );
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_webseed*
|
2011-03-03 18:33:24 +00:00
|
|
|
tr_webseedNew( struct tr_torrent * tor,
|
|
|
|
const char * url,
|
|
|
|
tr_peer_callback * callback,
|
|
|
|
void * callback_data )
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
|
|
|
tr_webseed * w = tr_new0( tr_webseed, 1 );
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_peer * peer = &w->parent;
|
2011-07-10 15:24:51 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
2011-03-03 18:33:24 +00:00
|
|
|
/* construct parent class */
|
|
|
|
tr_peerConstruct( peer );
|
2011-03-22 15:19:54 +00:00
|
|
|
peer->peerIsChoked = true;
|
2011-01-06 01:00:21 +00:00
|
|
|
peer->clientIsInterested = !tr_torrentIsSeed( tor );
|
|
|
|
peer->client = tr_strdup( "webseed" );
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldSetHasAll( &peer->have );
|
2011-03-03 18:33:24 +00:00
|
|
|
tr_peerUpdateProgress( tor, peer );
|
2011-01-06 01:00:21 +00:00
|
|
|
|
|
|
|
w->torrent_id = tr_torrentId( tor );
|
|
|
|
w->session = tor->session;
|
|
|
|
w->base_url_len = strlen( url );
|
|
|
|
w->base_url = tr_strndup( url, w->base_url_len );
|
2008-06-07 21:26:41 +00:00
|
|
|
w->callback = callback;
|
2010-06-19 14:33:10 +00:00
|
|
|
w->callback_data = callback_data;
|
2011-07-10 15:24:51 +00:00
|
|
|
w->file_urls = tr_new0( char *, inf->fileCount );
|
2011-03-03 18:33:24 +00:00
|
|
|
//tr_rcConstruct( &w->download_rate );
|
2011-03-31 14:53:22 +00:00
|
|
|
tr_bandwidthConstruct( &w->bandwidth, tor->session, &tor->bandwidth );
|
2011-01-06 01:00:21 +00:00
|
|
|
w->timer = evtimer_new( w->session->event_base, webseed_timer_func, w );
|
2011-01-06 15:12:12 +00:00
|
|
|
tr_timerAddMsec( w->timer, TR_IDLE_TIMER_MSEC );
|
2008-06-07 21:26:41 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_webseedFree( tr_webseed * w )
|
|
|
|
{
|
|
|
|
if( w )
|
|
|
|
{
|
2011-01-23 16:35:23 +00:00
|
|
|
if( webseed_has_tasks( w ) )
|
2011-03-22 15:19:54 +00:00
|
|
|
w->is_stopping = true;
|
2008-06-07 21:26:41 +00:00
|
|
|
else
|
2011-01-06 01:00:21 +00:00
|
|
|
webseed_free( w );
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
}
|