2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
2009-11-09 05:45:16 +00:00
|
|
|
*
|
2006-07-16 19:39:23 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2008-12-15 00:17:08 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2008-01-10 19:52:56 +00:00
|
|
|
#ifdef WIN32
|
2010-06-22 00:12:52 +00:00
|
|
|
#define _WIN32_WINNT 0x0501
|
|
|
|
#include <ws2tcpip.h>
|
2011-04-02 16:43:17 +00:00
|
|
|
#else
|
|
|
|
#include <netinet/tcp.h> /* TCP_CONGESTION */
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/util.h>
|
2011-03-24 22:57:39 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2011-03-24 22:57:39 +00:00
|
|
|
#include "fdlimit.h" /* tr_fdSocketClose() */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2011-03-24 22:57:39 +00:00
|
|
|
#include "peer-io.h" /* tr_peerIoAddrStr() FIXME this should be moved to net.h */
|
|
|
|
#include "session.h" /* tr_sessionGetPublicAddress() */
|
|
|
|
#include "tr-utp.h" /* tr_utpSendTo() */
|
|
|
|
#include "utils.h" /* tr_time(), tr_dbg() */
|
2007-07-09 20:10:42 +00:00
|
|
|
|
2009-01-02 21:45:55 +00:00
|
|
|
#ifndef IN_MULTICAST
|
|
|
|
#define IN_MULTICAST( a ) ( ( ( a ) & 0xf0000000 ) == 0xe0000000 )
|
|
|
|
#endif
|
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } };
|
|
|
|
const tr_address tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } };
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
void
|
|
|
|
tr_netInit( void )
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
static int initialized = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
if( !initialized )
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
WSADATA wsaData;
|
2008-09-23 19:11:04 +00:00
|
|
|
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2011-03-22 15:19:54 +00:00
|
|
|
initialized = true;
|
2007-08-02 19:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
char *
|
|
|
|
tr_net_strerror( char * buf, size_t buflen, int err )
|
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
#ifdef WIN32
|
2010-07-01 04:00:41 +00:00
|
|
|
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL );
|
2010-06-30 21:24:36 +00:00
|
|
|
#else
|
|
|
|
tr_strlcpy( buf, tr_strerror( err ), buflen );
|
|
|
|
#endif
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
const char *
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_to_string_with_buf( const tr_address * addr, char * buf, size_t buflen )
|
2008-12-16 21:06:47 +00:00
|
|
|
{
|
2011-03-25 05:34:26 +00:00
|
|
|
assert( tr_address_is_valid( addr ) );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
if( addr->type == TR_AF_INET )
|
|
|
|
return evutil_inet_ntop( AF_INET, &addr->addr, buf, buflen );
|
2009-08-10 20:04:08 +00:00
|
|
|
else
|
2011-03-25 05:34:26 +00:00
|
|
|
return evutil_inet_ntop( AF_INET6, &addr->addr, buf, buflen );
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-03-25 05:34:26 +00:00
|
|
|
* Non-threadsafe version of tr_address_to_string_with_buf()
|
|
|
|
* and uses a static memory area for a buffer.
|
2009-08-10 20:04:08 +00:00
|
|
|
* This function is suitable to be called from libTransmission's networking code,
|
|
|
|
* which is single-threaded.
|
|
|
|
*/
|
|
|
|
const char *
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_to_string( const tr_address * addr )
|
2009-08-10 20:04:08 +00:00
|
|
|
{
|
|
|
|
static char buf[INET6_ADDRSTRLEN];
|
2011-03-25 05:34:26 +00:00
|
|
|
return tr_address_to_string_with_buf( addr, buf, sizeof( buf ) );
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
bool
|
|
|
|
tr_address_from_string( tr_address * dst, const char * src )
|
2009-08-10 20:04:08 +00:00
|
|
|
{
|
2011-03-25 05:34:26 +00:00
|
|
|
bool ok;
|
|
|
|
|
|
|
|
if(( ok = evutil_inet_pton( AF_INET, src, &dst->addr ) == 1 ))
|
2009-08-10 20:04:08 +00:00
|
|
|
dst->type = TR_AF_INET;
|
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
if( !ok ) /* try IPv6 */
|
|
|
|
if(( ok = evutil_inet_pton( AF_INET6, src, &dst->addr ) == 1 ))
|
|
|
|
dst->type = TR_AF_INET6;
|
|
|
|
|
|
|
|
return ok;
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
/*
|
|
|
|
* Compare two tr_address structures.
|
|
|
|
* Returns:
|
|
|
|
* <0 if a < b
|
|
|
|
* >0 if a > b
|
|
|
|
* 0 if a == b
|
|
|
|
*/
|
2008-12-16 06:46:27 +00:00
|
|
|
int
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_compare( const tr_address * a, const tr_address * b)
|
2008-12-16 06:46:27 +00:00
|
|
|
{
|
2009-04-12 14:52:17 +00:00
|
|
|
static const int sizes[2] = { sizeof(struct in_addr), sizeof(struct in6_addr) };
|
2008-12-16 06:46:27 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
/* IPv6 addresses are always "greater than" IPv4 */
|
2008-12-16 06:46:27 +00:00
|
|
|
if( a->type != b->type )
|
|
|
|
return a->type == TR_AF_INET ? 1 : -1;
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2009-04-12 14:52:17 +00:00
|
|
|
return memcmp( &a->addr, &b->addr, sizes[a->type] );
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
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
|
2008-12-16 21:06:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-04-22 01:49:16 +00:00
|
|
|
int
|
2010-10-14 17:03:04 +00:00
|
|
|
tr_netSetCongestionControl( int s UNUSED, const char *algorithm UNUSED )
|
2010-04-22 01:49:16 +00:00
|
|
|
{
|
|
|
|
#ifdef TCP_CONGESTION
|
|
|
|
return setsockopt( s, IPPROTO_TCP, TCP_CONGESTION,
|
|
|
|
algorithm, strlen(algorithm) + 1 );
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_from_sockaddr_storage( tr_address * setme_addr,
|
|
|
|
tr_port * setme_port,
|
|
|
|
const struct sockaddr_storage * from )
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
|
|
|
if( from->ss_family == AF_INET )
|
|
|
|
{
|
|
|
|
struct sockaddr_in * sin = (struct sockaddr_in *)from;
|
|
|
|
setme_addr->type = TR_AF_INET;
|
|
|
|
setme_addr->addr.addr4.s_addr = sin->sin_addr.s_addr;
|
|
|
|
*setme_port = sin->sin_port;
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( from->ss_family == AF_INET6 )
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) from;
|
|
|
|
setme_addr->type = TR_AF_INET6;
|
|
|
|
setme_addr->addr.addr6 = sin6->sin6_addr;
|
|
|
|
*setme_port = sin6->sin6_port;
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
static socklen_t
|
|
|
|
setup_sockaddr( const tr_address * addr,
|
|
|
|
tr_port port,
|
|
|
|
struct sockaddr_storage * sockaddr)
|
|
|
|
{
|
2011-03-25 05:34:26 +00:00
|
|
|
assert( tr_address_is_valid( addr ) );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( addr->type == TR_AF_INET )
|
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
struct sockaddr_in sock4;
|
2008-12-15 00:17:08 +00:00
|
|
|
memset( &sock4, 0, sizeof( sock4 ) );
|
|
|
|
sock4.sin_family = AF_INET;
|
|
|
|
sock4.sin_addr.s_addr = addr->addr.addr4.s_addr;
|
|
|
|
sock4.sin_port = port;
|
|
|
|
memcpy( sockaddr, &sock4, sizeof( sock4 ) );
|
|
|
|
return sizeof( struct sockaddr_in );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
struct sockaddr_in6 sock6;
|
2008-12-15 00:17:08 +00:00
|
|
|
memset( &sock6, 0, sizeof( sock6 ) );
|
2009-05-14 13:42:29 +00:00
|
|
|
sock6.sin6_family = AF_INET6;
|
|
|
|
sock6.sin6_port = port;
|
2008-12-15 00:17:08 +00:00
|
|
|
sock6.sin6_flowinfo = 0;
|
2009-05-14 13:42:29 +00:00
|
|
|
sock6.sin6_addr = addr->addr.addr6;
|
2008-12-15 00:17:08 +00:00
|
|
|
memcpy( sockaddr, &sock6, sizeof( sock6 ) );
|
|
|
|
return sizeof( struct sockaddr_in6 );
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
int
|
2009-12-15 17:39:19 +00:00
|
|
|
tr_netOpenPeerSocket( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool clientIsSeed )
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
|
2008-12-15 00:17:08 +00:00
|
|
|
int s;
|
|
|
|
struct sockaddr_storage sock;
|
|
|
|
socklen_t addrlen;
|
2009-04-15 21:05:58 +00:00
|
|
|
const tr_address * source_addr;
|
|
|
|
socklen_t sourcelen;
|
|
|
|
struct sockaddr_storage source_sock;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
assert( tr_address_is_valid( addr ) );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
if( !tr_address_is_valid_for_peers( addr, port ) )
|
2008-12-31 21:25:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
s = tr_fdSocketCreate( session, domains[addr->type], SOCK_STREAM );
|
2009-05-14 13:42:29 +00:00
|
|
|
if( s < 0 )
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-12-02 05:30:46 +00:00
|
|
|
/* seeds don't need much of a read buffer... */
|
2009-12-15 17:39:19 +00:00
|
|
|
if( clientIsSeed ) {
|
2009-12-02 20:03:27 +00:00
|
|
|
int n = 8192;
|
2009-12-02 05:30:46 +00:00
|
|
|
if( setsockopt( s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n) ) )
|
|
|
|
tr_inf( "Unable to set SO_RCVBUF on socket %d: %s", s, tr_strerror( sockerrno ) );
|
|
|
|
}
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
if( evutil_make_socket_nonblocking( s ) < 0 ) {
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netClose( session, s );
|
2009-05-14 13:42:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-21 16:32:55 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen = setup_sockaddr( addr, port, &sock );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-04-15 21:05:58 +00:00
|
|
|
/* set source address */
|
2010-12-30 19:15:47 +00:00
|
|
|
source_addr = tr_sessionGetPublicAddress( session, addr->type, NULL );
|
2009-04-15 21:05:58 +00:00
|
|
|
assert( source_addr );
|
|
|
|
sourcelen = setup_sockaddr( source_addr, 0, &source_sock );
|
|
|
|
if( bind( s, ( struct sockaddr * ) &source_sock, sourcelen ) )
|
|
|
|
{
|
|
|
|
tr_err( _( "Couldn't set source address %s on %d: %s" ),
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_to_string( source_addr ), s, tr_strerror( errno ) );
|
2009-04-15 21:05:58 +00:00
|
|
|
return -errno;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-02 23:33:40 +00:00
|
|
|
if( ( connect( s, (struct sockaddr *) &sock,
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen ) < 0 )
|
2007-08-02 23:33:40 +00:00
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( sockerrno != WSAEWOULDBLOCK )
|
2007-08-02 23:33:40 +00:00
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( sockerrno != EINPROGRESS ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
int tmperrno;
|
|
|
|
tmperrno = sockerrno;
|
2009-02-08 22:52:47 +00:00
|
|
|
if( ( tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH )
|
|
|
|
|| addr->type == TR_AF_INET )
|
2008-12-20 08:51:32 +00:00
|
|
|
tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
|
2011-03-25 05:34:26 +00:00
|
|
|
s, tr_address_to_string( addr ), (int)ntohs( port ), tmperrno,
|
2008-12-20 08:51:32 +00:00
|
|
|
tr_strerror( tmperrno ) );
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netClose( session, s );
|
2008-12-15 00:17:08 +00:00
|
|
|
s = -tmperrno;
|
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)",
|
2008-09-23 19:11:04 +00:00
|
|
|
s, tr_peerIoAddrStr( addr, port ) );
|
2008-08-16 21:06:57 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
2007-07-31 14:26:44 +00:00
|
|
|
|
2011-02-18 00:36:19 +00:00
|
|
|
struct UTPSocket *
|
|
|
|
tr_netOpenPeerUTPSocket( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool clientIsSeed UNUSED )
|
2011-02-18 00:36:19 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t sslen;
|
|
|
|
sslen = setup_sockaddr( addr, port, &ss );
|
|
|
|
|
|
|
|
return UTP_Create( tr_utpSendTo, (void*)session,
|
|
|
|
(struct sockaddr*)&ss, sslen );
|
|
|
|
}
|
|
|
|
|
2009-07-09 18:14:33 +00:00
|
|
|
static int
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_netBindTCPImpl( const tr_address * addr, tr_port port, bool suppressMsgs, int * errOut )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
|
2008-12-15 00:17:08 +00:00
|
|
|
struct sockaddr_storage sock;
|
2009-05-14 13:42:29 +00:00
|
|
|
int fd;
|
|
|
|
int addrlen;
|
|
|
|
int optval;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
assert( tr_address_is_valid( addr ) );
|
2008-12-16 21:35:14 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
fd = socket( domains[addr->type], SOCK_STREAM, 0 );
|
2009-07-09 18:14:33 +00:00
|
|
|
if( fd < 0 ) {
|
|
|
|
*errOut = sockerrno;
|
2009-05-14 13:42:29 +00:00
|
|
|
return -1;
|
2009-07-09 18:14:33 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
if( evutil_make_socket_nonblocking( fd ) < 0 ) {
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = sockerrno;
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( fd );
|
2009-05-14 13:42:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) );
|
|
|
|
setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
|
2008-12-01 20:21:06 +00:00
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
2009-05-14 13:42:29 +00:00
|
|
|
if( addr->type == TR_AF_INET6 )
|
|
|
|
if( setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof( optval ) ) == -1 )
|
2009-07-09 18:14:33 +00:00
|
|
|
if( sockerrno != ENOPROTOOPT ) { /* if the kernel doesn't support it, ignore it */
|
|
|
|
*errOut = sockerrno;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-07 00:34:10 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen = setup_sockaddr( addr, htons( port ), &sock );
|
2009-05-14 13:42:29 +00:00
|
|
|
if( bind( fd, (struct sockaddr *) &sock, addrlen ) ) {
|
2009-05-15 12:28:12 +00:00
|
|
|
const int err = sockerrno;
|
2008-12-15 00:17:08 +00:00
|
|
|
if( !suppressMsgs )
|
2010-02-06 14:43:28 +00:00
|
|
|
{
|
|
|
|
const char * fmt;
|
|
|
|
const char * hint;
|
|
|
|
|
|
|
|
if( err == EADDRINUSE )
|
|
|
|
hint = _( "Is another copy of Transmission already running?" );
|
|
|
|
else
|
|
|
|
hint = NULL;
|
|
|
|
|
|
|
|
if( hint == NULL )
|
|
|
|
fmt = _( "Couldn't bind port %d on %s: %s" );
|
|
|
|
else
|
|
|
|
fmt = _( "Couldn't bind port %d on %s: %s (%s)" );
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_err( fmt, port, tr_address_to_string( addr ), tr_strerror( err ), hint );
|
2010-02-06 14:43:28 +00:00
|
|
|
}
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( fd );
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = err;
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( !suppressMsgs )
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_dbg( "Bound socket %d to port %d on %s", fd, port, tr_address_to_string( addr ) );
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
|
|
if( listen( fd, 128 ) == -1 ) {
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = sockerrno;
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( fd );
|
2009-07-09 18:14:33 +00:00
|
|
|
return -1;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 18:14:33 +00:00
|
|
|
int
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_netBindTCP( const tr_address * addr, tr_port port, bool suppressMsgs )
|
2009-07-09 18:14:33 +00:00
|
|
|
{
|
|
|
|
int unused;
|
|
|
|
return tr_netBindTCPImpl( addr, port, suppressMsgs, &unused );
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2009-07-09 18:14:33 +00:00
|
|
|
tr_net_hasIPv6( tr_port port )
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool result = false;
|
|
|
|
static bool alreadyDone = false;
|
2009-07-09 18:14:33 +00:00
|
|
|
|
|
|
|
if( !alreadyDone )
|
|
|
|
{
|
|
|
|
int err;
|
2011-03-22 15:19:54 +00:00
|
|
|
int fd = tr_netBindTCPImpl( &tr_in6addr_any, port, true, &err );
|
2009-07-09 18:14:33 +00:00
|
|
|
if( fd >= 0 || err != EAFNOSUPPORT ) /* we support ipv6 */
|
2011-03-22 15:19:54 +00:00
|
|
|
result = true;
|
2009-07-09 18:14:33 +00:00
|
|
|
if( fd >= 0 )
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( fd );
|
2011-03-22 15:19:54 +00:00
|
|
|
alreadyDone = true;
|
2009-07-09 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
int
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netAccept( tr_session * session,
|
2008-12-02 03:41:58 +00:00
|
|
|
int b,
|
|
|
|
tr_address * addr,
|
|
|
|
tr_port * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
int fd = tr_fdSocketAccept( session, b, addr, port );
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
|
|
if( fd>=0 && evutil_make_socket_nonblocking(fd)<0 ) {
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netClose( session, fd );
|
2009-05-14 13:42:29 +00:00
|
|
|
fd = -1;
|
|
|
|
}
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 20:27:27 +00:00
|
|
|
void
|
|
|
|
tr_netCloseSocket( int fd )
|
|
|
|
{
|
2011-03-13 19:47:21 +00:00
|
|
|
evutil_closesocket( fd );
|
2009-10-27 20:27:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_netClose( tr_session * session, int s )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketClose( session, s );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-20 03:44:45 +00:00
|
|
|
get_source_address() and global_unicast_address() were written by
|
|
|
|
Juliusz Chroboczek, and are covered under the same license as dht.c.
|
|
|
|
Please feel free to copy them into your software if it can help
|
|
|
|
unbreaking the double-stack Internet. */
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2010-12-27 19:18:17 +00:00
|
|
|
/* Get the source address used for a given destination address. Since
|
2009-11-09 05:45:16 +00:00
|
|
|
there is no official interface to get this information, we create
|
|
|
|
a connected UDP socket (connected UDP... hmm...) and check its source
|
|
|
|
address. */
|
|
|
|
static int
|
|
|
|
get_source_address( const struct sockaddr * dst,
|
|
|
|
socklen_t dst_len,
|
|
|
|
struct sockaddr * src,
|
|
|
|
socklen_t * src_len )
|
|
|
|
{
|
|
|
|
int s, rc, save;
|
|
|
|
|
|
|
|
s = socket(dst->sa_family, SOCK_DGRAM, 0);
|
|
|
|
if(s < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Since it's a UDP socket, this doesn't actually send any packets. */
|
|
|
|
rc = connect(s, dst, dst_len);
|
|
|
|
if(rc < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = getsockname(s, src, src_len);
|
|
|
|
if(rc < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2011-03-13 19:47:21 +00:00
|
|
|
evutil_closesocket( s );
|
2009-11-09 05:45:16 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
save = errno;
|
2011-03-13 19:47:21 +00:00
|
|
|
evutil_closesocket( s );
|
2009-11-09 05:45:16 +00:00
|
|
|
errno = save;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We all hate NATs. */
|
|
|
|
static int
|
|
|
|
global_unicast_address(struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
if(sa->sa_family == AF_INET) {
|
|
|
|
const unsigned char *a =
|
|
|
|
(unsigned char*)&((struct sockaddr_in*)sa)->sin_addr;
|
|
|
|
if(a[0] == 0 || a[0] == 127 || a[0] >= 224 ||
|
|
|
|
a[0] == 10 || (a[0] == 172 && a[1] >= 16 && a[1] <= 31) ||
|
|
|
|
(a[0] == 192 && a[1] == 168))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
} else if(sa->sa_family == AF_INET6) {
|
|
|
|
const unsigned char *a =
|
|
|
|
(unsigned char*)&((struct sockaddr_in6*)sa)->sin6_addr;
|
|
|
|
/* 2000::/3 */
|
|
|
|
return (a[0] & 0xE0) == 0x20;
|
|
|
|
} else {
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-25 07:35:38 +00:00
|
|
|
static int
|
2009-11-09 05:45:16 +00:00
|
|
|
tr_globalAddress( int af, void *addr, int *addr_len )
|
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
2010-04-20 03:44:45 +00:00
|
|
|
socklen_t sslen = sizeof(ss);
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
socklen_t salen;
|
2009-11-09 05:45:16 +00:00
|
|
|
int rc;
|
|
|
|
|
2010-04-20 03:44:45 +00:00
|
|
|
switch(af) {
|
|
|
|
case AF_INET:
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
2011-03-13 08:15:40 +00:00
|
|
|
evutil_inet_pton(AF_INET, "91.121.74.28", &sin.sin_addr);
|
2010-04-20 03:44:45 +00:00
|
|
|
sin.sin_port = htons(6969);
|
|
|
|
sa = (struct sockaddr*)&sin;
|
|
|
|
salen = sizeof(sin);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
/* In order for address selection to work right, this should be
|
|
|
|
a native IPv6 address, not Teredo or 6to4. */
|
2011-03-13 08:15:40 +00:00
|
|
|
evutil_inet_pton(AF_INET6, "2001:1890:1112:1::20", &sin6.sin6_addr);
|
2010-04-20 03:44:45 +00:00
|
|
|
sin6.sin6_port = htons(6969);
|
|
|
|
sa = (struct sockaddr*)&sin6;
|
|
|
|
salen = sizeof(sin6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = get_source_address( sa, salen, (struct sockaddr*)&ss, &sslen );
|
2009-11-09 05:45:16 +00:00
|
|
|
|
|
|
|
if( rc < 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( !global_unicast_address( (struct sockaddr*)&ss) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch(af) {
|
|
|
|
case AF_INET:
|
|
|
|
if(*addr_len < 4)
|
|
|
|
return -1;
|
|
|
|
memcpy(addr, &((struct sockaddr_in*)&ss)->sin_addr, 4);
|
|
|
|
*addr_len = 4;
|
|
|
|
return 1;
|
|
|
|
case AF_INET6:
|
|
|
|
if(*addr_len < 16)
|
|
|
|
return -1;
|
|
|
|
memcpy(addr, &((struct sockaddr_in6*)&ss)->sin6_addr, 16);
|
|
|
|
*addr_len = 16;
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-11-10 17:03:23 +00:00
|
|
|
|
|
|
|
/* Return our global IPv6 address, with caching. */
|
|
|
|
|
|
|
|
const unsigned char *
|
|
|
|
tr_globalIPv6( void )
|
|
|
|
{
|
|
|
|
static unsigned char ipv6[16];
|
|
|
|
static time_t last_time = 0;
|
|
|
|
static int have_ipv6 = 0;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-11-10 17:03:23 +00:00
|
|
|
|
|
|
|
/* Re-check every half hour */
|
|
|
|
if( last_time < now - 1800 )
|
|
|
|
{
|
|
|
|
int addrlen = 16;
|
|
|
|
const int rc = tr_globalAddress( AF_INET6, ipv6, &addrlen );
|
|
|
|
have_ipv6 = ( rc >= 0 ) && ( addrlen == 16 );
|
|
|
|
last_time = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
return have_ipv6 ? ipv6 : NULL;
|
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
2009-12-15 17:39:19 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-12-15 19:39:01 +00:00
|
|
|
isIPv4MappedAddress( const tr_address * addr )
|
2009-12-15 17:39:19 +00:00
|
|
|
{
|
2009-12-15 19:39:01 +00:00
|
|
|
return ( addr->type == TR_AF_INET6 ) && IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 );
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-12-15 17:39:19 +00:00
|
|
|
isIPv6LinkLocalAddress( const tr_address * addr )
|
|
|
|
{
|
|
|
|
return ( ( addr->type == TR_AF_INET6 )
|
|
|
|
&& IN6_IS_ADDR_LINKLOCAL( &addr->addr.addr6 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* isMartianAddr was written by Juliusz Chroboczek,
|
|
|
|
and is covered under the same license as third-party/dht/dht.c. */
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-12-15 17:39:19 +00:00
|
|
|
isMartianAddr( const struct tr_address * a )
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
|
|
|
static const unsigned char zeroes[16] =
|
|
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
|
2011-03-25 05:34:26 +00:00
|
|
|
assert( tr_address_is_valid( a ) );
|
2009-12-15 17:39:19 +00:00
|
|
|
|
|
|
|
switch( a->type )
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
2009-12-15 17:39:19 +00:00
|
|
|
case TR_AF_INET: {
|
|
|
|
const unsigned char * address = (const unsigned char*)&a->addr.addr4;
|
2009-12-14 02:07:47 +00:00
|
|
|
return (address[0] == 0) ||
|
|
|
|
(address[0] == 127) ||
|
|
|
|
((address[0] & 0xE0) == 0xE0);
|
2009-12-15 17:39:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
2009-12-15 17:39:19 +00:00
|
|
|
case TR_AF_INET6: {
|
|
|
|
const unsigned char * address = (const unsigned char*)&a->addr.addr6;
|
2009-12-14 02:07:47 +00:00
|
|
|
return (address[0] == 0xFF) ||
|
2009-12-15 20:06:48 +00:00
|
|
|
(memcmp(address, zeroes, 15) == 0 &&
|
2010-01-31 00:23:31 +00:00
|
|
|
(address[15] == 0 || address[15] == 1)) ||
|
|
|
|
/* Addresses outside of 2000::/3 are currently reserved,
|
2010-12-27 19:18:17 +00:00
|
|
|
but might be allocated at some future time. Since
|
2010-01-31 00:23:31 +00:00
|
|
|
there are a lot of buggy peers pushing around such
|
|
|
|
addresses over PEX, we reject them until the end of
|
|
|
|
the 13th Baktun. */
|
2010-01-31 17:15:05 +00:00
|
|
|
(tr_time() < 1356130800 && (address[0] & 0xE0) != 0x20);
|
2009-12-15 17:39:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
|
|
|
default:
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2009-12-14 02:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 17:39:19 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2011-03-25 05:34:26 +00:00
|
|
|
tr_address_is_valid_for_peers( const tr_address * addr, tr_port port )
|
2009-12-15 17:39:19 +00:00
|
|
|
{
|
|
|
|
return ( port != 0 )
|
2011-03-25 05:34:26 +00:00
|
|
|
&& ( tr_address_is_valid( addr ) )
|
2009-12-15 17:39:19 +00:00
|
|
|
&& ( !isIPv6LinkLocalAddress( addr ) )
|
2009-12-15 19:39:01 +00:00
|
|
|
&& ( !isIPv4MappedAddress( addr ) )
|
2009-12-15 17:39:19 +00:00
|
|
|
&& ( !isMartianAddr( addr ) );
|
|
|
|
}
|