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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <winsock2.h> /* inet_addr */
|
2009-01-10 02:22:13 +00:00
|
|
|
#include <WS2tcpip.h>
|
2008-01-10 19:52:56 +00:00
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <arpa/inet.h> /* inet_addr */
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
#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"
|
2007-12-01 05:11:30 +00:00
|
|
|
#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"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
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
|
|
|
|
|
2008-12-02 03:41:58 +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 } } } } };
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
static const char *
|
|
|
|
inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
|
|
|
|
{
|
|
|
|
if (af == AF_INET)
|
|
|
|
{
|
|
|
|
struct sockaddr_in in;
|
|
|
|
memset(&in, 0, sizeof(in));
|
|
|
|
in.sin_family = AF_INET;
|
|
|
|
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
|
|
|
|
getnameinfo((struct sockaddr *)&in, sizeof(struct
|
|
|
|
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
else if (af == AF_INET6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 in;
|
|
|
|
memset(&in, 0, sizeof(in));
|
|
|
|
in.sin6_family = AF_INET6;
|
|
|
|
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
|
|
|
|
getnameinfo((struct sockaddr *)&in, sizeof(struct
|
|
|
|
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
inet_pton(int af, const char *src, void *dst)
|
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res;
|
|
|
|
struct addrinfo *ressave;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = af;
|
|
|
|
|
|
|
|
if (getaddrinfo(src, NULL, &hints, &res) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ressave = res;
|
|
|
|
|
|
|
|
while (res)
|
|
|
|
{
|
|
|
|
memcpy(dst, res->ai_addr, res->ai_addrlen);
|
|
|
|
res = res->ai_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(ressave);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
void
|
|
|
|
tr_netInit( void )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
const char *
|
|
|
|
tr_ntop( const tr_address * src, char * dst, int size )
|
2008-12-16 21:06:47 +00:00
|
|
|
{
|
|
|
|
assert( tr_isAddress( src ) );
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
if( src->type == TR_AF_INET )
|
|
|
|
return inet_ntop( AF_INET, &src->addr, dst, size );
|
|
|
|
else
|
|
|
|
return inet_ntop( AF_INET6, &src->addr, dst, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer.
|
|
|
|
* This function is suitable to be called from libTransmission's networking code,
|
|
|
|
* which is single-threaded.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
tr_ntop_non_ts( const tr_address * src )
|
|
|
|
{
|
|
|
|
static char buf[INET6_ADDRSTRLEN];
|
|
|
|
return tr_ntop( src, buf, sizeof( buf ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_address *
|
|
|
|
tr_pton( const char * src, tr_address * dst )
|
|
|
|
{
|
|
|
|
int retval = inet_pton( AF_INET, src, &dst->addr );
|
|
|
|
if( retval < 0 )
|
|
|
|
return NULL;
|
|
|
|
else if( retval == 0 )
|
|
|
|
retval = inet_pton( AF_INET6, src, &dst->addr );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst->type = TR_AF_INET;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( retval < 1 )
|
|
|
|
return NULL;
|
|
|
|
dst->type = TR_AF_INET6;
|
|
|
|
return dst;
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 03:41:58 +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
|
|
|
|
tr_compareAddresses( const tr_address * a, const tr_address * b)
|
|
|
|
{
|
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
|
|
|
|
2008-12-16 21:06:47 +00:00
|
|
|
assert( tr_isAddress( a ) );
|
|
|
|
assert( tr_isAddress( b ) );
|
2008-12-02 03:41:58 +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] );
|
2008-12-02 03:41:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
tr_bool
|
|
|
|
tr_net_hasIPv6( tr_port port )
|
2008-12-18 05:55:22 +00:00
|
|
|
{
|
2009-02-07 00:34:10 +00:00
|
|
|
static tr_bool alreadyDone = FALSE;
|
|
|
|
static tr_bool result = FALSE;
|
|
|
|
int s;
|
2008-12-18 05:55:22 +00:00
|
|
|
if( alreadyDone )
|
2009-02-07 00:34:10 +00:00
|
|
|
return result;
|
|
|
|
s = tr_netBindTCP( &tr_in6addr_any, port, TRUE );
|
|
|
|
if( s >= 0 || -s != EAFNOSUPPORT ) /* we support ipv6 */
|
2008-12-18 05:55:22 +00:00
|
|
|
{
|
2009-02-07 00:34:10 +00:00
|
|
|
result = TRUE;
|
|
|
|
tr_netClose( s );
|
2008-12-18 05:55:22 +00:00
|
|
|
}
|
|
|
|
alreadyDone = TRUE;
|
2009-02-07 00:34:10 +00:00
|
|
|
return result;
|
2008-12-18 05:55:22 +00:00
|
|
|
}
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Socket list housekeeping
|
|
|
|
**********************************************************************/
|
|
|
|
struct tr_socketList
|
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
tr_address addr;
|
|
|
|
tr_socketList * next;
|
|
|
|
};
|
|
|
|
|
|
|
|
tr_socketList *
|
|
|
|
tr_socketListAppend( tr_socketList * const head,
|
|
|
|
const tr_address * const addr )
|
|
|
|
{
|
|
|
|
tr_socketList * tmp;
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
assert( head );
|
2008-12-16 21:06:47 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
for( tmp = head; tmp->next; tmp = tmp->next );
|
|
|
|
tmp->next = tr_socketListNew( addr );
|
|
|
|
return tmp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_socketList *
|
|
|
|
tr_socketListNew( const tr_address * const addr )
|
|
|
|
{
|
|
|
|
tr_socketList * tmp;
|
2008-12-16 21:06:47 +00:00
|
|
|
|
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
tmp = tr_new( tr_socketList, 1 );
|
|
|
|
tmp->socket = -1;
|
|
|
|
tmp->addr = *addr;
|
|
|
|
tmp->next = NULL;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_socketListFree( tr_socketList * const head )
|
|
|
|
{
|
|
|
|
assert( head );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( head->next )
|
|
|
|
tr_socketListFree( head->next );
|
|
|
|
tr_free( head );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_socketListRemove( tr_socketList * const head,
|
|
|
|
tr_socketList * const el)
|
|
|
|
{
|
|
|
|
tr_socketList * tmp;
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
assert( head );
|
|
|
|
assert( el );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
for( tmp = head; tmp->next && tmp->next != el; tmp = tmp->next );
|
|
|
|
tmp->next = el->next;
|
|
|
|
el->next = NULL;
|
|
|
|
tr_socketListFree(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_socketListTruncate( tr_socketList * const head,
|
|
|
|
tr_socketList * const start )
|
|
|
|
{
|
|
|
|
tr_socketList * tmp;
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
assert( head );
|
|
|
|
assert( start );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
for( tmp = head; tmp->next && tmp->next != start; tmp = tmp->next );
|
|
|
|
tr_socketListFree( start );
|
|
|
|
tmp->next = NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-16 21:06:47 +00:00
|
|
|
#if 0
|
2008-12-15 00:17:08 +00:00
|
|
|
int
|
|
|
|
tr_socketListGetSocket( const tr_socketList * const el )
|
|
|
|
{
|
|
|
|
assert( el );
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
return el->socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tr_address *
|
|
|
|
tr_socketListGetAddress( const tr_socketList * const el )
|
|
|
|
{
|
|
|
|
assert( el );
|
|
|
|
return &el->addr;
|
|
|
|
}
|
2008-12-16 21:06:47 +00:00
|
|
|
#endif
|
2008-12-15 00:17:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_socketListForEach( tr_socketList * const head,
|
|
|
|
void ( * cb ) ( int * const,
|
|
|
|
tr_address * const,
|
|
|
|
void * const),
|
|
|
|
void * const userData )
|
|
|
|
{
|
|
|
|
tr_socketList * tmp;
|
|
|
|
for( tmp = head; tmp; tmp = tmp->next )
|
|
|
|
cb( &tmp->socket, &tmp->addr, userData );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
static int
|
|
|
|
makeSocketNonBlocking( int fd )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-25 23:10:34 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
|
|
|
if( evutil_make_socket_nonblocking( fd ) )
|
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
int tmperrno;
|
2008-03-18 19:14:21 +00:00
|
|
|
tr_err( _( "Couldn't create socket: %s" ),
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strerror( sockerrno ) );
|
2008-12-15 00:17:08 +00:00
|
|
|
tmperrno = sockerrno;
|
2007-09-25 23:10:34 +00:00
|
|
|
tr_netClose( fd );
|
2008-12-15 00:17:08 +00:00
|
|
|
fd = -tmperrno;
|
2007-09-25 23:10:34 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
static int
|
2008-12-15 00:17:08 +00:00
|
|
|
createSocket( int domain, int type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
return makeSocketNonBlocking( tr_fdSocketCreate( domain, type ) );
|
2008-11-21 16:32:55 +00:00
|
|
|
}
|
2008-11-16 21:16:14 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
static void
|
2008-11-29 21:34:06 +00:00
|
|
|
setSndBuf( tr_session * session UNUSED, int fd UNUSED )
|
2008-11-21 16:32:55 +00:00
|
|
|
{
|
2008-11-28 07:42:08 +00:00
|
|
|
#if 0
|
2008-11-21 16:32:55 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
|
|
|
const int sndbuf = session->so_sndbuf;
|
2008-11-24 04:21:23 +00:00
|
|
|
const int rcvbuf = session->so_rcvbuf;
|
2008-11-21 16:32:55 +00:00
|
|
|
setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
|
2008-11-24 04:21:23 +00:00
|
|
|
setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
|
2008-11-16 21:16:14 +00:00
|
|
|
}
|
2008-11-28 07:42:08 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +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)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sock4;
|
|
|
|
struct sockaddr_in6 sock6;
|
2008-12-16 21:06:47 +00:00
|
|
|
|
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( addr->type == TR_AF_INET )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
memset( &sock6, 0, sizeof( sock6 ) );
|
|
|
|
sock6.sin6_family = AF_INET6;
|
|
|
|
sock6.sin6_port = port;
|
|
|
|
sock6.sin6_flowinfo = 0;
|
|
|
|
sock6.sin6_addr = addr->addr.addr6;
|
|
|
|
memcpy( sockaddr, &sock6, sizeof( sock6 ) );
|
|
|
|
return sizeof( struct sockaddr_in6 );
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2009-01-17 23:14:35 +00:00
|
|
|
static tr_bool
|
|
|
|
isMulticastAddress( const tr_address * addr )
|
|
|
|
{
|
|
|
|
if( addr->type == TR_AF_INET && IN_MULTICAST( htonl( addr->addr.addr4.s_addr ) ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if( addr->type == TR_AF_INET6 && ( addr->addr.addr6.s6_addr[0] == 0xff ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-03-27 01:31:17 +00:00
|
|
|
static TR_INLINE tr_bool
|
|
|
|
isIPv4MappedOrCompatAddress( const tr_address * addr )
|
|
|
|
{
|
|
|
|
if( addr->type == TR_AF_INET6 )
|
|
|
|
{
|
|
|
|
if( IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ) ||
|
|
|
|
IN6_IS_ADDR_V4COMPAT( &addr->addr.addr6 ) )
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-02-08 22:45:30 +00:00
|
|
|
static TR_INLINE tr_bool
|
|
|
|
isIPv6LinkLocalAddress( const tr_address * addr )
|
|
|
|
{
|
|
|
|
if( addr->type == TR_AF_INET6 &&
|
2009-03-27 01:31:17 +00:00
|
|
|
IN6_IS_ADDR_LINKLOCAL( &addr->addr.addr6 ) )
|
2009-02-08 22:45:30 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-17 23:14:35 +00:00
|
|
|
tr_bool
|
|
|
|
tr_isValidPeerAddress( const tr_address * addr, tr_port port )
|
|
|
|
{
|
2009-03-27 01:31:17 +00:00
|
|
|
if( isMulticastAddress( addr ) || isIPv6LinkLocalAddress( addr ) ||
|
|
|
|
isIPv4MappedOrCompatAddress( addr ) )
|
2009-01-17 23:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( port == 0 )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
int
|
|
|
|
tr_netOpenTCP( tr_session * session,
|
|
|
|
const tr_address * addr,
|
|
|
|
tr_port port )
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_storage sock;
|
|
|
|
const int type = SOCK_STREAM;
|
|
|
|
socklen_t addrlen;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-12-16 21:06:47 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
2009-03-27 01:31:17 +00:00
|
|
|
if( isMulticastAddress( addr ) || isIPv6LinkLocalAddress( addr ) )
|
2008-12-31 21:25:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2009-01-16 23:17:31 +00:00
|
|
|
if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ), type ) ) < 0 )
|
2008-12-15 00:17:08 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
setSndBuf( session, s );
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen = setup_sockaddr( addr, port, &sock );
|
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)" ),
|
|
|
|
s, tr_ntop_non_ts( addr ), (int)port, tmperrno,
|
|
|
|
tr_strerror( tmperrno ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netClose( 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
|
|
|
|
2008-05-30 15:43:10 +00:00
|
|
|
int
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
int s;
|
|
|
|
struct sockaddr_storage sock;
|
|
|
|
const int type = SOCK_STREAM;
|
|
|
|
int addrlen;
|
2009-02-07 00:34:10 +00:00
|
|
|
int retval;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) || defined( IPV6_V6ONLY )
|
|
|
|
int optval = 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-16 21:35:14 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ),
|
|
|
|
type ) ) < 0 )
|
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#ifdef SO_REUSEADDR
|
2007-08-02 19:43:29 +00:00
|
|
|
setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2008-12-01 20:21:06 +00:00
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
2009-02-07 01:14:41 +00:00
|
|
|
if( addr->type == TR_AF_INET6 &&
|
|
|
|
( retval = setsockopt( s, IPPROTO_IPV6, IPV6_V6ONLY, &optval,
|
|
|
|
sizeof( optval ) ) ) == -1 ) {
|
2009-02-07 00:34:10 +00:00
|
|
|
/* the kernel may not support this. if not, ignore it */
|
|
|
|
if( errno != ENOPROTOOPT )
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen = setup_sockaddr( addr, htons( port ), &sock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( bind( s, (struct sockaddr *) &sock,
|
2008-12-15 00:17:08 +00:00
|
|
|
addrlen ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
int tmperrno;
|
|
|
|
if( !suppressMsgs )
|
|
|
|
tr_err( _( "Couldn't bind port %d on %s: %s" ), port,
|
|
|
|
tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) );
|
|
|
|
tmperrno = sockerrno;
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netClose( s );
|
2008-12-15 00:17:08 +00:00
|
|
|
return -tmperrno;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-12-15 00:17:08 +00:00
|
|
|
if( !suppressMsgs )
|
|
|
|
tr_dbg( "Bound socket %d to port %d on %s",
|
|
|
|
s, port, tr_ntop_non_ts( addr ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
int
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_netAccept( tr_session * session,
|
|
|
|
int b,
|
|
|
|
tr_address * addr,
|
|
|
|
tr_port * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-16 21:06:47 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
|
2008-11-21 16:32:55 +00:00
|
|
|
setSndBuf( session, fd );
|
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +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
|
|
|
}
|