1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/daemon/proxy.c

347 lines
9.4 KiB
C
Raw Normal View History

2007-04-18 16:39:10 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2007 Joshua Elsasser
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <event.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libtransmission/transmission.h>
#include <libtransmission/trcompat.h>
2007-04-18 16:39:10 +00:00
#include "errors.h"
#include "misc.h"
static void usage ( const char *, ... );
static enum confpathtype readargs ( int, char **, char ** );
static int makesock ( enum confpathtype, const char * );
2007-04-18 16:39:10 +00:00
static void inread ( struct bufferevent *, void * );
static void noop ( struct bufferevent *, void * );
static void inerr ( struct bufferevent *, short, void * );
static void wtf ( struct bufferevent *, void * );
static void outerr ( struct bufferevent *, short, void * );
static void sockread ( struct bufferevent *, void * );
static void sockwrite( struct bufferevent *, void * );
static void sockerr ( struct bufferevent *, short, void * );
static struct bufferevent * gl_in = NULL;
static struct bufferevent * gl_out = NULL;
static struct bufferevent * gl_sock = NULL;
int
main( int argc, char ** argv )
{
struct event_base * base;
enum confpathtype type;
char * sockpath;
2007-04-18 16:39:10 +00:00
int sockfd;
setmyname( argv[0] );
type = readargs( argc, argv, &sockpath );
2007-04-18 16:39:10 +00:00
base = event_init();
sockfd = makesock( type, sockpath );
2007-04-18 16:39:10 +00:00
if( 0 > sockfd )
{
return EXIT_FAILURE;
}
gl_in = bufferevent_new( STDIN_FILENO, inread, noop, inerr, NULL );
if( NULL == gl_in )
{
errnomsg( "failed to set up event buffer for stdin" );
return EXIT_FAILURE;
}
/* XXX bufferevent_base_set( base, gl_in ); */
bufferevent_enable( gl_in, EV_READ );
bufferevent_disable( gl_in, EV_WRITE );
gl_out = bufferevent_new( STDOUT_FILENO, wtf, noop, outerr, NULL );
if( NULL == gl_in )
{
errnomsg( "failed to set up event buffer for stdin" );
return EXIT_FAILURE;
}
/* XXX bufferevent_base_set( base, gl_out ); */
bufferevent_disable( gl_out, EV_READ );
bufferevent_enable( gl_out, EV_WRITE );
gl_sock = bufferevent_new( sockfd, sockread, sockwrite, sockerr, NULL );
if( NULL == gl_in )
{
errnomsg( "failed to set up event buffer for stdin" );
return EXIT_FAILURE;
}
/* XXX bufferevent_base_set( base, gl_sock ); */
bufferevent_enable( gl_sock, EV_READ );
bufferevent_enable( gl_sock, EV_WRITE );
event_dispatch();
/* XXX event_base_dispatch( base ); */
return EXIT_FAILURE;
}
void
usage( const char * msg, ... )
{
va_list ap;
if( NULL != msg )
{
printf( "%s: ", getmyname() );
va_start( ap, msg );
vprintf( msg, ap );
va_end( ap );
printf( "\n" );
}
printf(
"usage: %s [options] [files]...\n"
"\n"
"Transmission %s http://transmission.m0k.org/\n"
2007-04-18 16:39:10 +00:00
"A free, lightweight BitTorrent client with a simple, intuitive interface.\n"
"\n"
" -h --help Display this message and exit\n"
" -t --type daemon Use the daemon frontend, transmission-daemon\n"
" -t --type gtk Use the GTK+ frontend, transmission-gtk\n"
" -t --type mac Use the Mac OS X frontend\n",
getmyname(), LONG_VERSION_STRING );
2007-04-18 16:39:10 +00:00
exit( EXIT_SUCCESS );
}
enum confpathtype
readargs( int argc, char ** argv, char ** sockpath )
2007-04-18 16:39:10 +00:00
{
char optstr[] = "ht:";
struct option longopts[] =
{
{ "help", no_argument, NULL, 'h' },
{ "type", required_argument, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
enum confpathtype type;
int opt;
type = CONF_PATH_TYPE_DAEMON;
*sockpath = NULL;
2007-04-18 16:39:10 +00:00
while( 0 <= ( opt = getopt_long( argc, argv, optstr, longopts, NULL ) ) )
{
switch( opt )
{
case 't':
if( 0 == strcasecmp( "daemon", optarg ) )
{
type = CONF_PATH_TYPE_DAEMON;
*sockpath = NULL;
2007-04-18 16:39:10 +00:00
}
else if( 0 == strcasecmp( "gtk", optarg ) )
{
type = CONF_PATH_TYPE_GTK;
*sockpath = NULL;
2007-04-18 16:39:10 +00:00
}
else if( 0 == strcasecmp( "mac", optarg ) ||
0 == strcasecmp( "osx", optarg ) ||
0 == strcasecmp( "macos", optarg ) ||
0 == strcasecmp( "macosx", optarg ) )
{
type = CONF_PATH_TYPE_OSX;
*sockpath = NULL;
}
2007-04-18 16:39:10 +00:00
else
{
*sockpath = optarg;
2007-04-18 16:39:10 +00:00
}
break;
default:
usage( NULL );
break;
}
}
return type;
}
int
makesock( enum confpathtype type, const char * path )
2007-04-18 16:39:10 +00:00
{
struct sockaddr_un sa;
2007-04-18 16:39:10 +00:00
int fd;
memset( &sa, 0, sizeof sa );
sa.sun_family = AF_LOCAL;
if( NULL == path )
{
confpath( sa.sun_path, sizeof sa.sun_path, CONF_FILE_SOCKET, type );
}
else
{
strlcpy( sa.sun_path, path, sizeof sa.sun_path );
}
2007-04-18 16:39:10 +00:00
fd = socket( AF_UNIX, SOCK_STREAM, 0 );
if( 0 > fd )
{
errnomsg( "failed to create socket" );
return -1;
}
if( 0 > connect( fd, ( struct sockaddr * )&sa, SUN_LEN( &sa ) ) )
2007-04-18 16:39:10 +00:00
{
errnomsg( "failed to connect to socket file: %s", sa.sun_path );
2007-04-18 16:39:10 +00:00
close( fd );
return -1;
}
return fd;
}
void
inread( struct bufferevent * ev UNUSED, void * arg UNUSED )
{
bufferevent_write_buffer( gl_sock, EVBUFFER_INPUT( gl_in ) );
}
void
noop( struct bufferevent * ev UNUSED, void * arg UNUSED )
{
}
void
inerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
{
if( EVBUFFER_EOF & what )
{
bufferevent_free( gl_in );
gl_in = NULL;
sockwrite( NULL, NULL );
return;
}
if( EVBUFFER_TIMEOUT & what )
{
errmsg( "timed out reading from stdin" );
}
else if( EVBUFFER_READ & what )
{
errmsg( "read error on stdin" );
}
else if( EVBUFFER_ERROR & what )
{
errmsg( "error on stdin" );
}
else
{
errmsg( "unknown error on stdin: 0x%x", what );
}
exit( EXIT_FAILURE );
}
void
wtf( struct bufferevent * ev, void * arg UNUSED )
{
/* this shouldn't happen, but let's drain the buffer anyway */
evbuffer_drain( EVBUFFER_INPUT( ev ),
EVBUFFER_LENGTH( EVBUFFER_INPUT( ev ) ) );
}
void
outerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
{
if( EVBUFFER_TIMEOUT & what )
{
errmsg( "timed out writing to stdout" );
}
else if( EVBUFFER_WRITE & what )
{
errmsg( "write error on stdout" );
}
else if( EVBUFFER_ERROR & what )
{
errmsg( "error on client stdout" );
}
else
{
errmsg( "unknown error on stdout connection: 0x%x", what );
}
exit( EXIT_FAILURE );
}
void
sockread( struct bufferevent * ev UNUSED, void * arg UNUSED )
{
bufferevent_write_buffer( gl_out, EVBUFFER_INPUT( gl_sock ) );
}
void
sockwrite( struct bufferevent * ev UNUSED, void * arg UNUSED )
{
if( NULL == gl_in && 0 == EVBUFFER_LENGTH( EVBUFFER_OUTPUT( gl_sock ) ) )
{
exit( EXIT_SUCCESS );
}
}
void
sockerr( struct bufferevent * ev UNUSED, short what, void * arg UNUSED )
{
if( EVBUFFER_EOF & what )
{
errmsg( "server closed connection" );
}
else if( EVBUFFER_TIMEOUT & what )
{
errmsg( "server connection timed out" );
}
else if( EVBUFFER_READ & what )
{
errmsg( "read error on server connection" );
}
else if( EVBUFFER_WRITE & what )
{
errmsg( "write error on server connection" );
}
else if( EVBUFFER_ERROR & what )
{
errmsg( "error on server connection" );
}
else
{
errmsg( "unknown error on server connection: 0x%x", what );
}
exit( EXIT_FAILURE );
}