2008-06-07 21:26:41 +00:00
/*
2010-01-04 21:00:47 +00:00
* This file Copyright ( C ) 2008 - 2010 Mnemosyne LLC
2008-06-07 21:26:41 +00:00
*
* This file is licensed by the GPL version 2. Works owned by the
* 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 $
*/
# include <string.h> /* strlen */
# include <event.h>
# include "transmission.h"
# include "inout.h"
2008-06-10 01:38:12 +00:00
# include "ratecontrol.h"
2008-06-07 21:26:41 +00:00
# include "torrent.h"
# include "utils.h"
# include "web.h"
# include "webseed.h"
struct tr_webseed
{
2008-11-28 22:11:41 +00:00
tr_bool busy ;
tr_bool dead ;
2008-06-07 21:26:41 +00:00
2008-11-08 22:24:07 +00:00
uint8_t hash [ SHA_DIGEST_LENGTH ] ;
2008-06-07 21:26:41 +00:00
2008-11-08 22:24:07 +00:00
char * url ;
tr_delivery_func * callback ;
2008-09-23 19:11:04 +00:00
void * callback_userdata ;
2008-06-07 21:26:41 +00:00
2008-10-11 04:07:50 +00:00
tr_piece_index_t pieceIndex ;
uint32_t pieceOffset ;
uint32_t byteCount ;
2008-06-07 21:26:41 +00:00
2009-01-02 20:42:35 +00:00
tr_ratecontrol rateDown ;
2008-11-08 22:24:07 +00:00
tr_session * session ;
2008-06-10 01:38:12 +00:00
2008-11-08 22:24:07 +00:00
struct evbuffer * content ;
2008-06-07 21:26:41 +00:00
} ;
/***
* * * *
* * */
2009-10-29 16:10:03 +00:00
static const tr_peer_event blankEvent = { 0 , 0 , 0 , 0 , 0.0f , 0 , 0 , 0 , 0 } ;
2008-06-07 21:26:41 +00:00
static void
2008-09-23 19:11:04 +00:00
publish ( tr_webseed * w ,
tr_peer_event * e )
2008-06-07 21:26:41 +00:00
{
if ( w - > callback )
w - > callback ( NULL , e , w - > callback_userdata ) ;
}
2009-02-11 16:34:35 +00:00
static void
fireNeedReq ( tr_webseed * w )
{
2009-11-08 23:20:00 +00:00
#if 0
2009-02-11 16:34:35 +00:00
tr_peer_event e = blankEvent ;
e . eventType = TR_PEER_NEED_REQ ;
publish ( w , & e ) ;
2009-11-08 23:20:00 +00:00
# endif
2009-02-11 16:34:35 +00:00
}
2008-06-07 21:26:41 +00:00
static void
2008-12-05 22:56:19 +00:00
fireClientGotBlock ( tr_webseed * w , uint32_t pieceIndex , uint32_t offset , uint32_t length )
2008-06-07 21:26:41 +00:00
{
tr_peer_event e = blankEvent ;
e . eventType = TR_PEER_CLIENT_GOT_BLOCK ;
e . pieceIndex = pieceIndex ;
e . offset = offset ;
e . length = length ;
publish ( w , & e ) ;
}
static void
2008-12-05 22:56:19 +00:00
fireClientGotData ( tr_webseed * w , uint32_t length )
2008-06-07 21:26:41 +00:00
{
tr_peer_event e = blankEvent ;
e . eventType = TR_PEER_CLIENT_GOT_DATA ;
e . length = length ;
2008-11-17 04:00:57 +00:00
e . wasPieceData = TRUE ;
2008-06-07 21:26:41 +00:00
publish ( w , & e ) ;
}
/***
* * * *
* * */
static char *
2008-09-23 19:11:04 +00:00
makeURL ( tr_webseed * w ,
const tr_file * file )
2008-06-07 21:26:41 +00:00
{
2008-09-23 19:11:04 +00:00
char * ret ;
2009-06-14 01:00:36 +00:00
struct evbuffer * out = evbuffer_new ( ) ;
2008-09-23 19:11:04 +00:00
const char * url = w - > url ;
const size_t url_len = strlen ( url ) ;
2008-06-07 21:26:41 +00:00
evbuffer_add ( out , url , url_len ) ;
/* if url ends with a '/', add the torrent name */
2009-11-10 17:03:23 +00:00
if ( url [ url_len - 1 ] = = ' / ' & & file - > name )
2009-11-29 08:05:47 +00:00
tr_http_escape ( out , file - > name , strlen ( file - > name ) , FALSE ) ;
2009-11-22 03:57:36 +00:00
2008-06-07 21:26:41 +00:00
ret = tr_strndup ( EVBUFFER_DATA ( out ) , EVBUFFER_LENGTH ( out ) ) ;
2009-06-14 01:00:36 +00:00
evbuffer_free ( out ) ;
2008-06-07 21:26:41 +00:00
return ret ;
}
static void requestNextChunk ( tr_webseed * w ) ;
static void
2008-12-14 11:21:11 +00:00
webResponseFunc ( tr_session * session ,
2008-11-08 22:24:07 +00:00
long response_code ,
const void * response ,
size_t response_byte_count ,
void * vw )
2008-06-07 21:26:41 +00:00
{
tr_webseed * w = vw ;
2008-11-08 22:24:07 +00:00
tr_torrent * tor = tr_torrentFindFromHash ( session , w - > hash ) ;
2008-09-23 19:11:04 +00:00
const int success = ( response_code = = 206 ) ;
2008-06-07 21:26:41 +00:00
2008-09-23 19:11:04 +00:00
/*fprintf( stderr, "server responded with code %ld and %lu bytes\n",
response_code , ( unsigned long ) response_byte_count ) ; */
2008-06-07 21:26:41 +00:00
if ( ! success )
{
/* FIXME */
}
2008-11-08 22:24:07 +00:00
else if ( tor ! = NULL )
2008-06-07 21:26:41 +00:00
{
evbuffer_add ( w - > content , response , response_byte_count ) ;
2008-10-11 04:07:50 +00:00
if ( ! w - > dead )
2008-06-09 22:53:45 +00:00
{
2008-10-11 04:07:50 +00:00
fireClientGotData ( w , response_byte_count ) ;
2009-01-02 20:42:35 +00:00
tr_rcTransferred ( & w - > rateDown , response_byte_count ) ;
2008-06-09 22:53:45 +00:00
}
2008-10-11 04:07:50 +00:00
if ( EVBUFFER_LENGTH ( w - > content ) < w - > byteCount )
2008-06-07 21:26:41 +00:00
requestNextChunk ( w ) ;
2008-10-11 04:07:50 +00:00
else {
2008-11-08 22:24:07 +00:00
tr_ioWrite ( tor , w - > pieceIndex , w - > pieceOffset , w - > byteCount , EVBUFFER_DATA ( w - > content ) ) ;
2008-06-07 21:26:41 +00:00
evbuffer_drain ( w - > content , EVBUFFER_LENGTH ( w - > content ) ) ;
2008-10-11 04:07:50 +00:00
w - > busy = 0 ;
if ( w - > dead )
tr_webseedFree ( w ) ;
2009-02-11 16:34:35 +00:00
else {
2008-10-11 04:07:50 +00:00
fireClientGotBlock ( w , w - > pieceIndex , w - > pieceOffset , w - > byteCount ) ;
2009-02-11 16:34:35 +00:00
fireNeedReq ( w ) ;
}
2008-06-07 21:26:41 +00:00
}
}
}
static void
requestNextChunk ( tr_webseed * w )
{
2008-11-08 22:24:07 +00:00
tr_torrent * tor = tr_torrentFindFromHash ( w - > session , w - > hash ) ;
if ( tor ! = NULL )
{
const tr_info * inf = tr_torrentInfo ( tor ) ;
const uint32_t have = EVBUFFER_LENGTH ( w - > content ) ;
const uint32_t left = w - > byteCount - have ;
const uint32_t pieceOffset = w - > pieceOffset + have ;
tr_file_index_t fileIndex ;
uint64_t fileOffset ;
uint32_t thisPass ;
char * url ;
char * range ;
tr_ioFindFileLocation ( tor , w - > pieceIndex , pieceOffset ,
& fileIndex , & fileOffset ) ;
thisPass = MIN ( left , inf - > files [ fileIndex ] . length - fileOffset ) ;
url = makeURL ( w , & inf - > files [ fileIndex ] ) ;
2008-10-27 18:09:15 +00:00
/*fprintf( stderr, "url is [%s]\n", url );*/
2008-11-08 22:24:07 +00:00
range = tr_strdup_printf ( " % " PRIu64 " -% " PRIu64 , fileOffset , fileOffset + thisPass - 1 ) ;
2008-10-27 18:09:15 +00:00
/*fprintf( stderr, "range is [%s] ... we want %lu total, we have %lu, so %lu are left, and we're asking for %lu this time\n", range, (unsigned long)w->byteCount, (unsigned long)have, (unsigned long)left, (unsigned long)thisPass );*/
2008-11-08 22:24:07 +00:00
tr_webRun ( w - > session , url , range , webResponseFunc , w ) ;
tr_free ( range ) ;
tr_free ( url ) ;
}
2008-06-07 21:26:41 +00:00
}
2008-08-22 16:30:07 +00:00
tr_addreq_t
2008-10-11 04:07:50 +00:00
tr_webseedAddRequest ( tr_webseed * w ,
uint32_t pieceIndex ,
uint32_t pieceOffset ,
uint32_t byteCount )
2008-06-07 21:26:41 +00:00
{
int ret ;
2008-10-11 04:07:50 +00:00
if ( w - > busy | | w - > dead )
2008-06-07 21:26:41 +00:00
{
ret = TR_ADDREQ_FULL ;
}
else
{
2008-10-11 04:07:50 +00:00
w - > busy = 1 ;
w - > pieceIndex = pieceIndex ;
w - > pieceOffset = pieceOffset ;
w - > byteCount = byteCount ;
evbuffer_drain ( w - > content , EVBUFFER_LENGTH ( w - > content ) ) ;
requestNextChunk ( w ) ;
2008-06-07 21:26:41 +00:00
ret = TR_ADDREQ_OK ;
}
return ret ;
}
2008-06-10 02:36:52 +00:00
int
tr_webseedIsActive ( const tr_webseed * w )
{
2008-10-11 04:07:50 +00:00
return w - > busy ! = 0 ;
2008-06-10 02:36:52 +00:00
}
2008-06-10 01:38:12 +00:00
int
2009-01-05 04:27:54 +00:00
tr_webseedGetSpeed ( const tr_webseed * w , uint64_t now , float * setme_KiBs )
2008-06-10 01:38:12 +00:00
{
2008-06-10 02:36:52 +00:00
const int isActive = tr_webseedIsActive ( w ) ;
2008-09-23 19:11:04 +00:00
2009-01-05 04:27:54 +00:00
* setme_KiBs = isActive ? tr_rcRate ( & w - > rateDown , now ) : 0.0f ;
2008-06-10 02:36:52 +00:00
return isActive ;
2008-06-10 01:38:12 +00:00
}
2008-06-07 21:26:41 +00:00
/***
* * * *
* * */
tr_webseed *
tr_webseedNew ( struct tr_torrent * torrent ,
2008-09-23 19:11:04 +00:00
const char * url ,
2008-06-07 21:26:41 +00:00
tr_delivery_func callback ,
2008-09-23 19:11:04 +00:00
void * callback_userdata )
2008-06-07 21:26:41 +00:00
{
tr_webseed * w = tr_new0 ( tr_webseed , 1 ) ;
2008-09-23 19:11:04 +00:00
2008-11-08 22:24:07 +00:00
memcpy ( w - > hash , torrent - > info . hash , SHA_DIGEST_LENGTH ) ;
w - > session = torrent - > session ;
2008-06-07 21:26:41 +00:00
w - > content = evbuffer_new ( ) ;
w - > url = tr_strdup ( url ) ;
w - > callback = callback ;
w - > callback_userdata = callback_userdata ;
2009-01-02 20:42:35 +00:00
tr_rcConstruct ( & w - > rateDown ) ;
2008-06-09 22:53:45 +00:00
/*fprintf( stderr, "w->callback_userdata is %p\n", w->callback_userdata );*/
2008-06-07 21:26:41 +00:00
return w ;
}
void
tr_webseedFree ( tr_webseed * w )
{
if ( w )
{
2008-10-11 04:07:50 +00:00
if ( w - > busy )
2008-06-07 21:26:41 +00:00
{
w - > dead = 1 ;
}
else
{
evbuffer_free ( w - > content ) ;
2009-01-02 20:42:35 +00:00
tr_rcDestruct ( & w - > rateDown ) ;
2008-06-07 21:26:41 +00:00
tr_free ( w - > url ) ;
tr_free ( w ) ;
}
}
}