transmission/libtransmission/net.c

242 lines
6.1 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2005-2008 Transmission authors and contributors
2006-07-16 19:39:23 +00:00
*
* 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h> /* inet_addr */
#else
#include <arpa/inet.h> /* inet_addr */
#include <netdb.h>
#include <fcntl.h>
#endif
#include <evutil.h>
2006-07-16 19:39:23 +00:00
#include "transmission.h"
2007-07-09 20:10:42 +00:00
#include "fdlimit.h"
#include "natpmp.h"
2007-07-09 20:10:42 +00:00
#include "net.h"
2008-08-16 21:06:57 +00:00
#include "peer-io.h"
#include "platform.h"
#include "utils.h"
2007-07-09 20:10:42 +00:00
2006-07-16 19:39:23 +00:00
void
tr_netInit( void )
{
static int initialized = FALSE;
if( !initialized )
{
#ifdef WIN32
WSADATA wsaData;
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
#endif
initialized = TRUE;
}
}
2006-07-16 19:39:23 +00:00
/***********************************************************************
* DNS resolution
*
2006-07-16 19:39:23 +00:00
* Synchronous "resolution": only works with character strings
* representing numbers expressed in the Internet standard `.' notation.
* Returns a non-zero value if an error occurs.
**********************************************************************/
int
tr_netResolve( const char * address,
struct in_addr * addr )
2006-07-16 19:39:23 +00:00
{
addr->s_addr = inet_addr( address );
return addr->s_addr == 0xFFFFFFFF;
2006-07-16 19:39:23 +00:00
}
/***********************************************************************
2008-05-28 16:56:38 +00:00
* TCP sockets
2006-07-16 19:39:23 +00:00
**********************************************************************/
2008-04-12 00:29:49 +00:00
int
tr_netSetTOS( int s,
int tos )
2008-04-12 00:29:49 +00:00
{
#ifdef IP_TOS
return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
#else
return 0;
#endif
}
static int
makeSocketNonBlocking( int fd )
2006-07-16 19:39:23 +00:00
{
if( fd >= 0 )
{
#if defined( __BEOS__ )
int flags = 1;
if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
&flags, sizeof( int ) ) < 0 )
2006-07-16 19:39:23 +00:00
#else
if( evutil_make_socket_nonblocking( fd ) )
2006-07-16 19:39:23 +00:00
#endif
{
tr_err( _( "Couldn't create socket: %s" ),
tr_strerror( sockerrno ) );
tr_netClose( fd );
fd = -1;
}
2006-07-16 19:39:23 +00:00
}
return fd;
2006-07-16 19:39:23 +00:00
}
static int
createSocket( int type,
int priority )
2006-07-16 19:39:23 +00:00
{
int fd;
fd = tr_fdSocketCreate( type, priority );
if( fd >= 0 )
fd = makeSocketNonBlocking( fd );
2008-08-16 21:06:57 +00:00
#if 0
if( fd >= 0 )
{
const int buffsize = 1500 * 3; /* 3x MTU for most ethernet/wireless */
setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) );
}
2008-08-16 21:06:57 +00:00
#endif
return fd;
2006-07-16 19:39:23 +00:00
}
int
tr_netOpenTCP( const struct in_addr * addr,
tr_port_t port,
int priority )
2006-07-16 19:39:23 +00:00
{
int s;
2006-07-16 19:39:23 +00:00
struct sockaddr_in sock;
const int type = SOCK_STREAM;
2006-07-16 19:39:23 +00:00
2007-01-21 19:42:11 +00:00
if( ( s = createSocket( type, priority ) ) < 0 )
2006-07-16 19:39:23 +00:00
return -1;
memset( &sock, 0, sizeof( sock ) );
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = addr->s_addr;
2006-07-16 19:39:23 +00:00
sock.sin_port = port;
if( ( connect( s, (struct sockaddr *) &sock,
sizeof( struct sockaddr_in ) ) < 0 )
#ifdef WIN32
&& ( sockerrno != WSAEWOULDBLOCK )
#endif
&& ( sockerrno != EINPROGRESS ) )
2006-07-16 19:39:23 +00:00
{
tr_err( _(
"Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
s, inet_ntoa( *addr ), port,
sockerrno, tr_strerror( sockerrno ) );
2006-07-16 19:39:23 +00:00
tr_netClose( s );
2007-12-15 03:17:50 +00:00
s = -1;
2006-07-16 19:39:23 +00:00
}
2008-08-16 21:06:57 +00:00
tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
s, tr_peerIoAddrStr( addr, port ) );
2008-08-16 21:06:57 +00:00
2006-07-16 19:39:23 +00:00
return s;
}
int
tr_netBindTCP( int port )
2006-07-16 19:39:23 +00:00
{
int s;
2006-07-16 19:39:23 +00:00
struct sockaddr_in sock;
const int type = SOCK_STREAM;
2006-09-25 18:37:45 +00:00
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
int optval;
2006-07-16 19:39:23 +00:00
#endif
2007-01-21 19:42:11 +00:00
if( ( s = createSocket( type, 1 ) ) < 0 )
2006-07-16 19:39:23 +00:00
return -1;
#ifdef SO_REUSEADDR
optval = 1;
setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
2006-07-16 19:39:23 +00:00
#endif
memset( &sock, 0, sizeof( sock ) );
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = INADDR_ANY;
2006-07-16 19:39:23 +00:00
sock.sin_port = htons( port );
if( bind( s, (struct sockaddr *) &sock,
sizeof( struct sockaddr_in ) ) )
2006-07-16 19:39:23 +00:00
{
tr_err( _( "Couldn't bind port %d: %s" ), port,
tr_strerror( sockerrno ) );
2006-07-16 19:39:23 +00:00
tr_netClose( s );
return -1;
}
2008-04-21 15:22:54 +00:00
tr_dbg( "Bound socket %d to port %d", s, port );
2006-07-16 19:39:23 +00:00
return s;
}
int
tr_netAccept( int b,
struct in_addr * addr,
tr_port_t * port )
2006-07-16 19:39:23 +00:00
{
return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
2006-07-16 19:39:23 +00:00
}
void
tr_netClose( int s )
2006-07-16 19:39:23 +00:00
{
2007-01-21 19:42:11 +00:00
tr_fdSocketClose( s );
2006-07-16 19:39:23 +00:00
}
2006-08-18 08:46:19 +00:00
void
tr_netNtop( const struct in_addr * addr,
char * buf,
int len )
2006-08-18 08:46:19 +00:00
{
const uint8_t * cast;
cast = (const uint8_t *)addr;
tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
cast[0], cast[1], cast[2], cast[3] );
2006-08-18 08:46:19 +00:00
}