1
/******************************************************************************
2
* $Id: net.c 261 2006-05-29 21:27:31Z titer $
4
* Copyright (c) 2005-2006 Transmission authors and contributors
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
23
*****************************************************************************/
25
#include "transmission.h"
27
/***********************************************************************
29
**********************************************************************/
31
/***********************************************************************
33
***********************************************************************
34
* Synchronous "resolution": only works with character strings
35
* representing numbers expressed in the Internet standard `.' notation.
36
* Returns a non-zero value if an error occurs.
37
**********************************************************************/
38
int tr_netResolve( char * address, struct in_addr * addr )
40
addr->s_addr = inet_addr( address );
41
return ( addr->s_addr == 0xFFFFFFFF );
44
/* TODO: Make this code reentrant */
45
static tr_thread_t resolveThread;
46
static tr_lock_t resolveLock;
47
static volatile int resolveDie;
48
static tr_resolve_t * resolveQueue;
50
static void resolveRelease ( tr_resolve_t * );
51
static void resolveFunc ( void * );
63
/***********************************************************************
64
* tr_netResolveThreadInit
65
***********************************************************************
66
* Initializes the static variables used for resolution and launch the
67
* gethostbyname thread.
68
**********************************************************************/
69
void tr_netResolveThreadInit()
73
tr_lockInit( &resolveLock );
74
tr_threadCreate( &resolveThread, resolveFunc, NULL );
77
/***********************************************************************
78
* tr_netResolveThreadClose
79
***********************************************************************
80
* Notices the gethostbyname thread that is should terminate. Doesn't
81
* wait until it does, in case it is stuck in a resolution: we let it
82
* die and clean itself up.
83
**********************************************************************/
84
void tr_netResolveThreadClose()
86
tr_lockLock( &resolveLock );
88
tr_lockUnlock( &resolveLock );
92
/***********************************************************************
94
***********************************************************************
95
* Adds an address to the resolution queue.
96
**********************************************************************/
97
tr_resolve_t * tr_netResolveInit( char * address )
101
r = malloc( sizeof( tr_resolve_t ) );
102
r->status = TR_RESOLVE_WAIT;
103
r->address = strdup( address );
107
tr_lockLock( &resolveLock );
115
for( iter = resolveQueue; iter->next; iter = iter->next );
118
tr_lockUnlock( &resolveLock );
123
/***********************************************************************
125
***********************************************************************
126
* Checks the current status of a resolution.
127
**********************************************************************/
128
int tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
132
tr_lockLock( &resolveLock );
134
if( ret == TR_RESOLVE_OK )
138
tr_lockUnlock( &resolveLock );
143
/***********************************************************************
145
***********************************************************************
147
**********************************************************************/
148
void tr_netResolveClose( tr_resolve_t * r )
153
/***********************************************************************
155
***********************************************************************
156
* The allocated tr_resolve_t structures should be freed when
157
* tr_netResolveClose was called *and* it was removed from the queue.
158
* This can happen in any order, so we use a refcount to know we can
160
**********************************************************************/
161
static void resolveRelease( tr_resolve_t * r )
163
if( --r->refcount < 1 )
170
/***********************************************************************
172
***********************************************************************
173
* Keeps waiting for addresses to resolve, and removes them from the
174
* queue once resolution is done.
175
**********************************************************************/
176
static void resolveFunc( void * arg UNUSED )
179
struct hostent * host;
181
tr_dbg( "Resolve thread started" );
183
tr_lockLock( &resolveLock );
187
if( !( r = resolveQueue ) )
189
/* TODO: Use a condition wait */
190
tr_lockUnlock( &resolveLock );
192
tr_lockLock( &resolveLock );
196
/* Blocking resolution */
197
tr_lockUnlock( &resolveLock );
198
host = gethostbyname( r->address );
199
tr_lockLock( &resolveLock );
203
memcpy( &r->addr, host->h_addr, host->h_length );
204
r->status = TR_RESOLVE_OK;
208
r->status = TR_RESOLVE_ERROR;
211
resolveQueue = r->next;
216
tr_lockUnlock( &resolveLock );
217
tr_lockClose( &resolveLock );
218
while( ( r = resolveQueue ) )
220
resolveQueue = r->next;
224
tr_dbg( "Resolve thread exited" );
228
/***********************************************************************
230
**********************************************************************/
232
static int makeSocketNonBlocking( int s )
238
if( setsockopt( s, SOL_SOCKET, SO_NONBLOCK,
239
&flags, sizeof( int ) ) < 0 )
241
if( ( flags = fcntl( s, F_GETFL, 0 ) ) < 0 ||
242
fcntl( s, F_SETFL, flags | O_NONBLOCK ) < 0 )
245
tr_err( "Could not set socket to non-blocking mode (%s)",
254
static int createSocket()
258
s = socket( AF_INET, SOCK_STREAM, 0 );
261
tr_err( "Could not create socket (%s)", strerror( errno ) );
265
return makeSocketNonBlocking( s );
268
int tr_netOpen( struct in_addr addr, in_port_t port )
271
struct sockaddr_in sock;
279
memset( &sock, 0, sizeof( sock ) );
280
sock.sin_family = AF_INET;
281
sock.sin_addr.s_addr = addr.s_addr;
282
sock.sin_port = port;
284
if( connect( s, (struct sockaddr *) &sock,
285
sizeof( struct sockaddr_in ) ) < 0 &&
286
errno != EINPROGRESS )
288
tr_err( "Could not connect socket (%s)", strerror( errno ) );
296
int tr_netBind( int port )
299
struct sockaddr_in sock;
312
setsockopt( s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( optval ) );
315
memset( &sock, 0, sizeof( sock ) );
316
sock.sin_family = AF_INET;
317
sock.sin_addr.s_addr = INADDR_ANY;
318
sock.sin_port = htons( port );
320
if( bind( s, (struct sockaddr *) &sock,
321
sizeof( struct sockaddr_in ) ) )
323
tr_err( "Could not bind port %d", port );
328
tr_inf( "Binded port %d", port );
334
int tr_netAccept( int s, struct in_addr * addr, in_port_t * port )
338
struct sockaddr_in sock;
340
len = sizeof( sock );
341
t = accept( s, (struct sockaddr *) &sock, &len );
348
*addr = sock.sin_addr;
349
*port = sock.sin_port;
351
return makeSocketNonBlocking( t );
354
int tr_netSend( int s, uint8_t * buf, int size )
358
ret = send( s, buf, size, 0 );
361
if( errno == ENOTCONN || errno == EAGAIN || errno == EWOULDBLOCK )
374
int tr_netRecv( int s, uint8_t * buf, int size )
378
ret = recv( s, buf, size, 0 );
381
if( errno == EAGAIN || errno == EWOULDBLOCK )
398
void tr_netClose( int s )
400
#ifdef BEOS_NETSERVER