2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2007-10-25 13:59:46 +00:00
|
|
|
#include <arpa/inet.h> /* inet_addr */
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifndef WIN32
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <netdb.h>
|
2007-07-12 17:51:45 +00:00
|
|
|
#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"
|
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
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-02 19:43:29 +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
|
2007-09-20 16:32:01 +00:00
|
|
|
*
|
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.
|
|
|
|
**********************************************************************/
|
2006-09-25 18:37:45 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2007-01-21 19:42:11 +00:00
|
|
|
* TCP/UDP sockets
|
2006-07-16 19:39:23 +00:00
|
|
|
**********************************************************************/
|
|
|
|
|
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 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
|
2007-09-25 23:10:34 +00:00
|
|
|
if( evutil_make_socket_nonblocking( fd ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|
2007-09-25 23:10:34 +00:00
|
|
|
{
|
|
|
|
tr_err( "Couldn't set socket to non-blocking mode (%s)",
|
|
|
|
strerror( sockerrno ) );
|
|
|
|
tr_netClose( fd );
|
|
|
|
fd = -1;
|
|
|
|
}
|
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
|
|
|
|
createSocket( int type, int priority )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-25 23:10:34 +00:00
|
|
|
return makeSocketNonBlocking( tr_fdSocketCreate( type, priority ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-31 14:26:44 +00:00
|
|
|
static int
|
|
|
|
tr_netOpen( const struct in_addr * addr, tr_port_t port,
|
|
|
|
int type, int priority )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_in sock;
|
|
|
|
|
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;
|
2007-07-14 16:29:21 +00:00
|
|
|
sock.sin_addr.s_addr = addr->s_addr;
|
2006-07-16 19:39:23 +00:00
|
|
|
sock.sin_port = port;
|
|
|
|
|
2007-08-02 23:33:40 +00:00
|
|
|
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
|
|
|
{
|
2007-08-02 21:58:34 +00:00
|
|
|
tr_err( "Couldn't connect socket (%s)", strerror( sockerrno ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netClose( s );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2007-07-31 14:26:44 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
|
|
|
|
{
|
|
|
|
return tr_netOpen( addr, port, SOCK_STREAM, priority );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_netOpenUDP( const struct in_addr * addr, tr_port_t port, int priority )
|
|
|
|
{
|
|
|
|
return tr_netOpen( addr, port, SOCK_DGRAM, priority );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#ifdef IP_ADD_MEMBERSHIP
|
2007-07-14 16:29:21 +00:00
|
|
|
int tr_netMcastOpen( int port, const struct in_addr * addr )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct ip_mreq req;
|
|
|
|
|
|
|
|
fd = tr_netBindUDP( port );
|
|
|
|
if( 0 > fd )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( &req, 0, sizeof( req ) );
|
2007-07-14 16:29:21 +00:00
|
|
|
req.imr_multiaddr.s_addr = addr->s_addr;
|
2006-09-25 18:37:45 +00:00
|
|
|
req.imr_interface.s_addr = htonl( INADDR_ANY );
|
2007-08-02 19:43:29 +00:00
|
|
|
if( setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&req, sizeof ( req ) ) )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-08-02 21:58:34 +00:00
|
|
|
tr_err( "Couldn't join multicast group (%s)", strerror( sockerrno ) );
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_netClose( fd );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#else /* IP_ADD_MEMBERSHIP */
|
2007-07-14 16:29:21 +00:00
|
|
|
int tr_netMcastOpen( int port UNUSED, const struct in_addr * addr UNUSED )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* IP_ADD_MEMBERSHIP */
|
|
|
|
|
2007-07-31 14:26:44 +00:00
|
|
|
static int
|
|
|
|
tr_netBind( int port, int type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_in sock;
|
2006-09-25 18:37:45 +00:00
|
|
|
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
|
2006-07-16 19:39:23 +00:00
|
|
|
int optval;
|
|
|
|
#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;
|
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
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#ifdef SO_REUSEPORT
|
|
|
|
if( SOCK_DGRAM == type )
|
|
|
|
{
|
|
|
|
optval = 1;
|
|
|
|
setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof( optval ) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
memset( &sock, 0, sizeof( sock ) );
|
|
|
|
sock.sin_family = AF_INET;
|
2007-12-01 05:01:02 +00:00
|
|
|
|
|
|
|
/* Leopard closes a SO_REUSEADDR + INADDR_ANY hole, so we can't
|
|
|
|
* use INADDR_ANY when binding for nat-pmp. For details, refer to
|
|
|
|
* http://www.uwsg.indiana.edu/hypermail/linux/kernel/9902.1/0828.html .
|
|
|
|
* This can probably be done cleaner, but since we're only using SOCK_DGRAM
|
|
|
|
* for nat-pmp, this quick fix should work. */
|
|
|
|
if ( SOCK_DGRAM == type )
|
2007-12-01 05:11:30 +00:00
|
|
|
sock.sin_addr.s_addr = inet_addr( PMP_MCAST_ADDR );
|
2007-12-01 05:01:02 +00:00
|
|
|
else
|
|
|
|
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 ) ) )
|
|
|
|
{
|
2007-10-09 02:49:24 +00:00
|
|
|
tr_err( "Couldn't bind port %d: %s", port, strerror(sockerrno) );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netClose( s );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-07-31 14:26:44 +00:00
|
|
|
int
|
|
|
|
tr_netBindTCP( int port )
|
|
|
|
{
|
|
|
|
return tr_netBind( port, SOCK_STREAM );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_netBindUDP( int port )
|
|
|
|
{
|
|
|
|
return tr_netBind( port, SOCK_DGRAM );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
int
|
|
|
|
tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-25 23:10:34 +00:00
|
|
|
return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-13 01:34:14 +00:00
|
|
|
int
|
|
|
|
tr_netSend( int s, const void * buf, int size )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-13 01:34:14 +00:00
|
|
|
const int ret = send( s, buf, size, 0 );
|
|
|
|
if( ret >= 0 )
|
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-31 16:55:47 +00:00
|
|
|
if( sockerrno == ENOTCONN || sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
|
2007-07-13 01:34:14 +00:00
|
|
|
return TR_NET_BLOCK;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-13 01:34:14 +00:00
|
|
|
return TR_NET_CLOSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
socklen_t len;
|
|
|
|
int ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
len = ( NULL == addr ? 0 : sizeof( *addr ) );
|
|
|
|
ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
|
2006-07-16 19:39:23 +00:00
|
|
|
if( ret < 0 )
|
|
|
|
{
|
2007-07-31 16:55:47 +00:00
|
|
|
if( sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
ret = TR_NET_BLOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = TR_NET_CLOSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !ret )
|
|
|
|
{
|
|
|
|
ret = TR_NET_CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2006-08-18 08:46:19 +00:00
|
|
|
|
2007-09-20 16:32:01 +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;
|
|
|
|
snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
|
|
|
|
cast[0], cast[1], cast[2], cast[3] );
|
|
|
|
}
|