1
1
/******************************************************************************
2
* $Id: net.c 6594 2008-08-20 02:20:56Z charles $
2
* $Id: net.c 6944 2008-10-22 13:57:30Z charles $
4
4
* Copyright (c) 2005-2008 Transmission authors and contributors
53
54
static int initialized = FALSE;
58
WSAStartup(MAKEWORD(2,2), &wsaData);
60
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
60
62
initialized = TRUE;
64
66
/***********************************************************************
67
69
* Synchronous "resolution": only works with character strings
68
70
* representing numbers expressed in the Internet standard `.' notation.
69
71
* Returns a non-zero value if an error occurs.
70
72
**********************************************************************/
71
int tr_netResolve( const char * address, struct in_addr * addr )
74
tr_netResolve( const char * address,
75
struct in_addr * addr )
73
77
addr->s_addr = inet_addr( address );
74
return ( addr->s_addr == 0xFFFFFFFF );
78
return addr->s_addr == 0xFFFFFFFF;
78
81
/***********************************************************************
80
83
**********************************************************************/
83
tr_netSetTOS( int s, int tos )
86
90
return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
101
#if defined( __BEOS__ )
99
103
if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
100
&flags, sizeof( int ) ) < 0 )
104
&flags, sizeof( int ) ) < 0 )
102
106
if( evutil_make_socket_nonblocking( fd ) )
105
109
tr_err( _( "Couldn't create socket: %s" ),
106
tr_strerror( sockerrno ) );
110
tr_strerror( sockerrno ) );
107
111
tr_netClose( fd );
116
createSocket( int type, int priority )
120
createSocket( int type )
120
fd = tr_fdSocketCreate( type, priority );
123
fd = makeSocketNonBlocking( fd );
127
const int buffsize = 1500*3; /* 3x MTU for most ethernet/wireless */
128
setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) );
122
return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
136
tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
126
tr_netOpenTCP( const struct in_addr * addr,
139
130
struct sockaddr_in sock;
140
const int type = SOCK_STREAM;
131
const int type = SOCK_STREAM;
142
if( ( s = createSocket( type, priority ) ) < 0 )
133
if( ( s = createSocket( type ) ) < 0 )
147
136
memset( &sock, 0, sizeof( sock ) );
148
137
sock.sin_family = AF_INET;
150
139
sock.sin_port = port;
152
141
if( ( connect( s, (struct sockaddr *) &sock,
153
sizeof( struct sockaddr_in ) ) < 0 )
142
sizeof( struct sockaddr_in ) ) < 0 )
155
&& ( sockerrno != WSAEWOULDBLOCK )
144
&& ( sockerrno != WSAEWOULDBLOCK )
157
&& ( sockerrno != EINPROGRESS ) )
146
&& ( sockerrno != EINPROGRESS ) )
159
tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
160
s, inet_ntoa(*addr), port,
161
sockerrno, tr_strerror(sockerrno) );
149
"Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
150
s, inet_ntoa( *addr ), port,
151
sockerrno, tr_strerror( sockerrno ) );
162
152
tr_netClose( s );
156
tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
157
s, tr_peerIoAddrStr( addr, port ) );
170
163
tr_netBindTCP( int port )
173
166
struct sockaddr_in sock;
174
const int type = SOCK_STREAM;
167
const int type = SOCK_STREAM;
175
169
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
179
if( ( s = createSocket( type, 1 ) ) < 0 )
173
if( ( s = createSocket( type ) ) < 0 )
182
176
#ifdef SO_REUSEADDR
190
184
sock.sin_port = htons( port );
192
186
if( bind( s, (struct sockaddr *) &sock,
193
sizeof( struct sockaddr_in ) ) )
187
sizeof( struct sockaddr_in ) ) )
195
tr_err( _( "Couldn't bind port %d: %s" ), port, tr_strerror(sockerrno) );
189
tr_err( _( "Couldn't bind port %d: %s" ), port,
190
tr_strerror( sockerrno ) );
196
191
tr_netClose( s );
200
195
tr_dbg( "Bound socket %d to port %d", s, port );
205
tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
201
struct in_addr * addr,
207
204
return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
217
tr_netNtop( const struct in_addr * addr, char * buf, int len )
214
tr_netNtop( const struct in_addr * addr,
219
218
const uint8_t * cast;
221
220
cast = (const uint8_t *)addr;
222
221
tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
223
cast[0], cast[1], cast[2], cast[3] );
222
cast[0], cast[1], cast[2], cast[3] );