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 "errors.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "transmission.h"
|
2007-05-27 16:31:16 +00:00
|
|
|
#include "trcompat.h"
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
static void usage ( const char *, ... );
|
2007-05-09 06:56:51 +00:00
|
|
|
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;
|
2007-05-09 06:56:51 +00:00
|
|
|
char * sockpath;
|
2007-04-18 16:39:10 +00:00
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
setmyname( argv[0] );
|
2007-05-09 06:56:51 +00:00
|
|
|
type = readargs( argc, argv, &sockpath );
|
2007-04-18 16:39:10 +00:00
|
|
|
base = event_init();
|
|
|
|
|
2007-05-09 06:56:51 +00:00
|
|
|
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"
|
2007-07-10 19:40:21 +00:00
|
|
|
"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"
|
2007-05-28 02:27:13 +00:00
|
|
|
" -t --type gtk Use the GTK+ frontend, transmission-gtk\n"
|
|
|
|
" -t --type mac Use the Mac OS X frontend\n",
|
2007-07-10 19:40:21 +00:00
|
|
|
getmyname(), LONG_VERSION_STRING );
|
2007-04-18 16:39:10 +00:00
|
|
|
exit( EXIT_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
enum confpathtype
|
2007-05-09 06:56:51 +00:00
|
|
|
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;
|
|
|
|
|
2007-05-09 06:56:51 +00:00
|
|
|
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 ) )
|
|
|
|
{
|
2007-05-09 06:56:51 +00:00
|
|
|
type = CONF_PATH_TYPE_DAEMON;
|
|
|
|
*sockpath = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
else if( 0 == strcasecmp( "gtk", optarg ) )
|
|
|
|
{
|
2007-05-09 06:56:51 +00:00
|
|
|
type = CONF_PATH_TYPE_GTK;
|
|
|
|
*sockpath = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2007-05-28 02:27:13 +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
|
|
|
|
{
|
2007-05-09 06:56:51 +00:00
|
|
|
*sockpath = optarg;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage( NULL );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-05-09 06:56:51 +00:00
|
|
|
makesock( enum confpathtype type, const char * path )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
bzero( &sun, sizeof sun );
|
|
|
|
sun.sun_family = AF_LOCAL;
|
2007-05-09 06:56:51 +00:00
|
|
|
if( NULL == path )
|
|
|
|
{
|
|
|
|
confpath( sun.sun_path, sizeof sun.sun_path, CONF_FILE_SOCKET, type );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strlcpy( sun.sun_path, path, sizeof sun.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 * )&sun, SUN_LEN( &sun ) ) )
|
|
|
|
{
|
|
|
|
errnomsg( "failed to connect to socket file: %s", sun.sun_path );
|
|
|
|
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 );
|
|
|
|
}
|