Ignore:
Timestamp:
Apr 2, 2007, 8:37:07 PM (15 years ago)
Author:
joshe
Message:

Make adding torrents work when using a proxy on another machine.
Add option to disable pex.
Miscellaneous cleanups and bug fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/daemon/daemon/proxy.c

    r1630 r1635  
    4040#include "transmission.h"
    4141
    42 #define TIMEOUT                 ( 60 )
    43 
    44 static void                 usage    ( const char *, ... );
    45 static enum confpathtype    readargs ( int, char ** );
    46 static int                  makesock ( enum confpathtype );
    47 static struct bufferevent * setupev  ( struct event_base *, int,
    48                                        void ( * )( struct bufferevent *, short,
    49                                                    void * ), void * );
    50 static void                 noop     ( struct bufferevent *, void * );
    51 static void                 relay    ( struct bufferevent *, void * );
    52 static void                 outerr   ( struct bufferevent *, short, void * );
    53 static void                 inerr    ( struct bufferevent *, short, void * );
    54 static void                 sockerr  ( struct bufferevent *, short, void * );
     42static void              usage    ( const char *, ... );
     43static enum confpathtype readargs ( int, char ** );
     44static int               makesock ( enum confpathtype );
     45static void              inread   ( struct bufferevent *, void * );
     46static void              noop     ( struct bufferevent *, void * );
     47static void              inerr    ( struct bufferevent *, short, void * );
     48static void              wtf      ( struct bufferevent *, void * );
     49static void              outerr   ( struct bufferevent *, short, void * );
     50static void              sockread ( struct bufferevent *, void * );
     51static void              sockwrite( struct bufferevent *, void * );
     52static void              sockerr  ( struct bufferevent *, short, void * );
     53
     54static struct bufferevent * gl_in   = NULL;
     55static struct bufferevent * gl_out  = NULL;
     56static struct bufferevent * gl_sock = NULL;
    5557
    5658int
     
    6062    enum confpathtype    type;
    6163    int                  sockfd;
    62     struct bufferevent * outev, * inev, * sockev;
    6364
    6465    setmyname( argv[0] );
     
    7273    }
    7374
    74     outev  = setupev( base, STDOUT_FILENO, outerr,  NULL );
    75     sockev = setupev( base, sockfd,        sockerr, outev );
    76     inev   = setupev( base, STDIN_FILENO,  inerr,   sockev );
    77 
    78     if( NULL == outev || NULL == inev || NULL == sockev )
    79     {
     75    gl_in = bufferevent_new( STDIN_FILENO, inread, noop, inerr, NULL );
     76    if( NULL == gl_in )
     77    {
     78        errnomsg( "failed to set up event buffer for stdin" );
    8079        return EXIT_FAILURE;
    8180    }
    82 
    83     bufferevent_disable( outev,  EV_READ );
    84     bufferevent_enable(  outev,  EV_WRITE );
    85     bufferevent_enable(  inev,   EV_READ );
    86     bufferevent_disable( inev,   EV_WRITE );
    87     bufferevent_enable(  sockev, EV_READ );
    88     bufferevent_enable(  sockev, EV_WRITE );
     81    bufferevent_base_set( base, gl_in );
     82    bufferevent_enable( gl_in, EV_READ );
     83    bufferevent_disable( gl_in, EV_WRITE );
     84
     85    gl_out  = bufferevent_new( STDOUT_FILENO, wtf, noop, outerr,  NULL );
     86    if( NULL == gl_in )
     87    {
     88        errnomsg( "failed to set up event buffer for stdin" );
     89        return EXIT_FAILURE;
     90    }
     91    bufferevent_base_set( base, gl_out );
     92    bufferevent_disable( gl_out, EV_READ );
     93    bufferevent_enable( gl_out, EV_WRITE );
     94
     95    gl_sock = bufferevent_new( sockfd, sockread, sockwrite, sockerr, NULL );
     96    if( NULL == gl_in )
     97    {
     98        errnomsg( "failed to set up event buffer for stdin" );
     99        return EXIT_FAILURE;
     100    }
     101    bufferevent_base_set( base, gl_sock );
     102    bufferevent_enable( gl_sock, EV_READ );
     103    bufferevent_enable( gl_sock, EV_WRITE );
    89104
    90105    event_base_dispatch( base );
     
    189204}
    190205
    191 struct bufferevent *
    192 setupev( struct event_base * base, int fd,
    193          void ( * efunc )( struct bufferevent *, short, void * ), void * arg )
    194 {
    195     struct bufferevent * ev;
    196 
    197     ev = bufferevent_new( fd, relay, noop, efunc, arg );
    198     if( NULL == ev )
    199     {
    200         mallocmsg( -1 );
    201         return NULL;
    202     }
    203 
    204     bufferevent_base_set( base, ev );
    205     bufferevent_settimeout( ev, TIMEOUT, TIMEOUT );
    206 
    207     return ev;
     206void
     207inread( struct bufferevent * ev UNUSED, void * arg UNUSED )
     208{
     209    bufferevent_write_buffer( gl_sock, EVBUFFER_INPUT( gl_in ) );
    208210}
    209211
     
    211213noop( struct bufferevent * ev UNUSED, void * arg UNUSED )
    212214{
    213     /* libevent prior to 1.2 couldn't handle a NULL write callback */
    214 }
    215 
    216 void
    217 relay( struct bufferevent * in, void * arg )
    218 {
    219     struct bufferevent * out = arg;
    220 
    221     if( NULL == arg )
    222     {
    223         /* this shouldn't happen, but let's drain the buffer anyway */
    224         evbuffer_drain( EVBUFFER_INPUT( in ),
    225                         EVBUFFER_LENGTH( EVBUFFER_INPUT( in ) ) );
     215}
     216
     217void
     218inerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
     219{
     220    if( EVBUFFER_EOF & what )
     221    {
     222        bufferevent_free( gl_in );
     223        gl_in = NULL;
     224        sockwrite( NULL, NULL );
     225        return;
     226    }
     227
     228    if( EVBUFFER_TIMEOUT & what )
     229    {
     230        errmsg( "timed out reading from stdin" );
     231    }
     232    else if( EVBUFFER_READ & what )
     233    {
     234        errmsg( "read error on stdin" );
     235    }
     236    else if( EVBUFFER_ERROR & what )
     237    {
     238        errmsg( "error on stdin" );
    226239    }
    227240    else
    228241    {
    229         bufferevent_write_buffer( out, EVBUFFER_INPUT( in ) );
    230     }
     242        errmsg( "unknown error on stdin: 0x%x", what );
     243    }
     244
     245    exit( EXIT_FAILURE );
     246}
     247
     248void
     249wtf( struct bufferevent * ev, void * arg UNUSED )
     250{
     251    /* this shouldn't happen, but let's drain the buffer anyway */
     252    evbuffer_drain( EVBUFFER_INPUT( ev ),
     253                    EVBUFFER_LENGTH( EVBUFFER_INPUT( ev ) ) );
    231254}
    232255
     
    255278
    256279void
    257 inerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
     280sockread( struct bufferevent * ev UNUSED, void * arg UNUSED )
     281{
     282    bufferevent_write_buffer( gl_out, EVBUFFER_INPUT( gl_sock ) );
     283}
     284
     285void
     286sockwrite( struct bufferevent * ev UNUSED, void * arg UNUSED )
     287{
     288    if( NULL == gl_in && 0 == EVBUFFER_LENGTH( EVBUFFER_OUTPUT( gl_sock ) ) )
     289    {
     290        exit( EXIT_SUCCESS );
     291    }
     292}
     293
     294void
     295sockerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
    258296{
    259297    if( EVBUFFER_EOF & what )
    260298    {
    261         exit( EXIT_SUCCESS );
     299        errmsg( "server closed connection" );
    262300    }
    263301    else if( EVBUFFER_TIMEOUT & what )
    264302    {
    265         errmsg( "timed out reading from stdin" );
     303        errmsg( "server connection timed out" );
    266304    }
    267305    else if( EVBUFFER_READ & what )
    268306    {
    269         errmsg( "read error on stdin" );
     307        errmsg( "read error on server connection" );
     308    }
     309    else if( EVBUFFER_WRITE & what )
     310    {
     311        errmsg( "write error on server connection" );
    270312    }
    271313    else if( EVBUFFER_ERROR & what )
    272314    {
    273         errmsg( "error on stdin" );
     315        errmsg( "error on server connection" );
    274316    }
    275317    else
    276318    {
    277         errmsg( "unknown error on stdin: 0x%x", what );
     319        errmsg( "unknown error on server connection: 0x%x", what );
    278320    }
    279321
    280322    exit( EXIT_FAILURE );
    281323}
    282 
    283 void
    284 sockerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
    285 {
    286     if( EVBUFFER_EOF & what )
    287     {
    288         errmsg( "server closed connection" );
    289     }
    290     else if( EVBUFFER_TIMEOUT & what )
    291     {
    292         errmsg( "server connection timed out" );
    293     }
    294     else if( EVBUFFER_READ & what )
    295     {
    296         errmsg( "read error on server connection" );
    297     }
    298     else if( EVBUFFER_WRITE & what )
    299     {
    300         errmsg( "write error on server connection" );
    301     }
    302     else if( EVBUFFER_ERROR & what )
    303     {
    304         errmsg( "error on server connection" );
    305     }
    306     else
    307     {
    308         errmsg( "unknown error on server connection: 0x%x", what );
    309     }
    310 
    311     exit( EXIT_FAILURE );
    312 }
Note: See TracChangeset for help on using the changeset viewer.