source: trunk/libtransmission/net.c @ 920

Last change on this file since 920 was 920, checked in by joshe, 15 years ago

Merge nat-traversal branch to trunk.

  • Property svn:keywords set to Date Rev Author Id
File size: 11.7 KB
Line 
1/******************************************************************************
2 * $Id: net.c 920 2006-09-25 18:37:45Z joshe $
3 *
4 * Copyright (c) 2005-2006 Transmission authors and contributors
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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 *****************************************************************************/
24
25#include "transmission.h"
26
27/***********************************************************************
28 * DNS resolution
29 **********************************************************************/
30
31/***********************************************************************
32 * tr_netResolve
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 **********************************************************************/
38int tr_netResolve( const char * address, struct in_addr * addr )
39{
40    addr->s_addr = inet_addr( address );
41    return ( addr->s_addr == 0xFFFFFFFF );
42}
43
44/* TODO: Make this code reentrant */
45static tr_thread_t  resolveThread;
46static tr_lock_t    resolveLock;
47static volatile int resolveDie;
48static tr_resolve_t * resolveQueue;
49
50static void resolveRelease ( tr_resolve_t * );
51static void resolveFunc    ( void * );
52
53struct tr_resolve_s
54{
55    tr_tristate_t  status;
56    char           * address;
57    struct in_addr addr;
58
59    int            refcount;
60    tr_resolve_t   * next;
61};
62
63/***********************************************************************
64 * tr_netResolveThreadInit
65 ***********************************************************************
66 * Initializes the static variables used for resolution and launch the
67 * gethostbyname thread.
68 **********************************************************************/
69void tr_netResolveThreadInit()
70{
71    resolveDie   = 0;
72    resolveQueue = NULL;
73    tr_lockInit( &resolveLock );
74    tr_threadCreate( &resolveThread, resolveFunc, NULL );
75}
76
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 **********************************************************************/
84void tr_netResolveThreadClose()
85{
86    tr_lockLock( &resolveLock );
87    resolveDie = 1;
88    tr_lockUnlock( &resolveLock );
89    tr_wait( 200 );
90}
91
92/***********************************************************************
93 * tr_netResolveInit
94 ***********************************************************************
95 * Adds an address to the resolution queue.
96 **********************************************************************/
97tr_resolve_t * tr_netResolveInit( const char * address )
98{
99    tr_resolve_t * r;
100
101    r           = malloc( sizeof( tr_resolve_t ) );
102    r->status   = TR_WAIT;
103    r->address  = strdup( address );
104    r->refcount = 2;
105    r->next     = NULL;
106
107    tr_lockLock( &resolveLock );
108    if( !resolveQueue )
109    {
110        resolveQueue = r;
111    }
112    else
113    {
114        tr_resolve_t * iter;
115        for( iter = resolveQueue; iter->next; iter = iter->next );
116        iter->next = r;
117    }
118    tr_lockUnlock( &resolveLock );
119
120    return r;
121}
122
123/***********************************************************************
124 * tr_netResolvePulse
125 ***********************************************************************
126 * Checks the current status of a resolution.
127 **********************************************************************/
128tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
129{
130    tr_tristate_t ret;
131
132    tr_lockLock( &resolveLock );
133    ret = r->status;
134    if( ret == TR_OK )
135    {
136        *addr = r->addr;
137    }
138    tr_lockUnlock( &resolveLock );
139
140    return ret;
141}
142
143/***********************************************************************
144 * tr_netResolveClose
145 ***********************************************************************
146 *
147 **********************************************************************/
148void tr_netResolveClose( tr_resolve_t * r )
149{
150    resolveRelease( r );
151}
152
153/***********************************************************************
154 * resolveRelease
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
159 * take it out.
160 **********************************************************************/
161static void resolveRelease( tr_resolve_t * r )
162{
163    if( --r->refcount < 1 )
164    {
165        free( r->address );
166        free( r );
167    }
168}
169
170/***********************************************************************
171 * resolveFunc
172 ***********************************************************************
173 * Keeps waiting for addresses to resolve, and removes them from the
174 * queue once resolution is done.
175 **********************************************************************/
176static void resolveFunc( void * arg UNUSED )
177{
178    tr_resolve_t * r;
179    struct hostent * host;
180
181    tr_dbg( "Resolve thread started" );
182
183    tr_lockLock( &resolveLock );
184
185    while( !resolveDie )
186    {
187        if( !( r = resolveQueue ) )
188        {
189            /* TODO: Use a condition wait */
190            tr_lockUnlock( &resolveLock );
191            tr_wait( 50 );
192            tr_lockLock( &resolveLock );
193            continue;
194        }
195
196        /* Blocking resolution */
197        tr_lockUnlock( &resolveLock );
198        host = gethostbyname( r->address );
199        tr_lockLock( &resolveLock );
200
201        if( host )
202        {
203            memcpy( &r->addr, host->h_addr, host->h_length );
204            r->status = TR_OK;
205        }
206        else
207        {
208            r->status = TR_ERROR;
209        }
210       
211        resolveQueue = r->next;
212        resolveRelease( r );
213    }
214
215    /* Clean up  */
216    tr_lockUnlock( &resolveLock );
217    tr_lockClose( &resolveLock );
218    while( ( r = resolveQueue ) )
219    {
220        resolveQueue = r->next;
221        resolveRelease( r );
222    }
223
224    tr_dbg( "Resolve thread exited" );
225}
226
227
228/***********************************************************************
229 * TCP sockets
230 **********************************************************************/
231
232static int makeSocketNonBlocking( int s )
233{
234    int flags;
235
236#ifdef SYS_BEOS
237    flags = 1;
238    if( setsockopt( s, SOL_SOCKET, SO_NONBLOCK,
239                    &flags, sizeof( int ) ) < 0 )
240#else
241    if( ( flags = fcntl( s, F_GETFL, 0 ) ) < 0 ||
242        fcntl( s, F_SETFL, flags | O_NONBLOCK ) < 0 )
243#endif
244    {
245        tr_err( "Could not set socket to non-blocking mode (%s)",
246                strerror( errno ) );
247        tr_netClose( s );
248        return -1;
249    }
250
251    return s;
252}
253
254static int createSocket( int type )
255{
256    int s;
257
258    s = socket( AF_INET, type, 0 );
259    if( s < 0 )
260    {
261        tr_err( "Could not create socket (%s)", strerror( errno ) );
262        return -1;
263    }
264
265    return makeSocketNonBlocking( s );
266}
267
268int tr_netOpen( struct in_addr addr, in_port_t port, int type )
269{
270    int s;
271    struct sockaddr_in sock;
272
273    s = createSocket( type );
274    if( s < 0 )
275    {
276        return -1;
277    }
278
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;
283
284    if( connect( s, (struct sockaddr *) &sock,
285                 sizeof( struct sockaddr_in ) ) < 0 &&
286        errno != EINPROGRESS )
287    {
288        tr_err( "Could not connect socket (%s)", strerror( errno ) );
289        tr_netClose( s );
290        return -1;
291    }
292
293    return s;
294}
295
296#ifdef IP_ADD_MEMBERSHIP
297int tr_netMcastOpen( int port, struct in_addr addr )
298{
299    int fd;
300    struct ip_mreq req;
301
302    fd = tr_netBindUDP( port );
303    if( 0 > fd )
304    {
305        return -1;
306    }
307
308    memset( &req, 0, sizeof( req ) );
309    req.imr_multiaddr.s_addr = addr.s_addr;
310    req.imr_interface.s_addr = htonl( INADDR_ANY );
311    if( setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof ( req ) ) )
312    {
313        tr_err( "Could not join multicast group (%s)", strerror( errno ) );
314        tr_netClose( fd );
315        return -1;
316    }
317
318    return fd;
319}
320#else /* IP_ADD_MEMBERSHIP */
321int tr_netMcastOpen( int port UNUSED, struct in_addr addr UNUSED )
322{
323    return -1;
324}
325#endif /* IP_ADD_MEMBERSHIP */
326
327int tr_netBind( int port, int type )
328{
329    int s;
330    struct sockaddr_in sock;
331#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
332    int optval;
333#endif
334
335    s = createSocket( type );
336    if( s < 0 )
337    {
338        return -1;
339    }
340
341#ifdef SO_REUSEADDR
342    optval = 1;
343    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( optval ) );
344#endif
345
346#ifdef SO_REUSEPORT
347    if( SOCK_DGRAM == type )
348    {
349        optval = 1;
350        setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof( optval ) );
351    }
352#endif
353
354    memset( &sock, 0, sizeof( sock ) );
355    sock.sin_family      = AF_INET;
356    sock.sin_addr.s_addr = INADDR_ANY;
357    sock.sin_port        = htons( port );
358
359    if( bind( s, (struct sockaddr *) &sock,
360               sizeof( struct sockaddr_in ) ) )
361    {
362        tr_err( "Could not bind port %d", port );
363        tr_netClose( s );
364        return -1;
365    }
366
367    return s;
368}
369
370int tr_netAccept( int s, struct in_addr * addr, in_port_t * port )
371{
372    int t;
373    unsigned len;
374    struct sockaddr_in sock;
375
376    len = sizeof( sock );
377    t   = accept( s, (struct sockaddr *) &sock, &len );
378
379    if( t < 0 )
380    {
381        return -1;
382    }
383   
384    *addr = sock.sin_addr;
385    *port = sock.sin_port;
386
387    return makeSocketNonBlocking( t );
388}
389
390int tr_netSend( int s, uint8_t * buf, int size )
391{
392    int ret;
393
394    ret = send( s, buf, size, 0 );
395    if( ret < 0 )
396    {
397        if( errno == ENOTCONN || errno == EAGAIN || errno == EWOULDBLOCK )
398        {
399            ret = TR_NET_BLOCK;
400        }
401        else
402        {
403            ret = TR_NET_CLOSE;
404        }
405    }
406
407    return ret;
408}
409
410int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
411{
412    socklen_t len;
413    int       ret;
414
415    len = ( NULL == addr ? 0 : sizeof( *addr ) );
416    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
417    if( ret < 0 )
418    {
419        if( errno == EAGAIN || errno == EWOULDBLOCK )
420        {
421            ret = TR_NET_BLOCK;
422        }
423        else
424        {
425            ret = TR_NET_CLOSE;
426        }
427    }
428    if( !ret )
429    {
430        ret = TR_NET_CLOSE;
431    }
432
433    return ret;
434}
435
436void tr_netClose( int s )
437{
438#ifdef BEOS_NETSERVER
439    closesocket( s );
440#else
441    close( s );
442#endif
443}
444
445void tr_netNtop( const struct in_addr * addr, char * buf, int len )
446{
447    const uint8_t * cast;
448
449    cast = (const uint8_t *)addr;
450    snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
451              cast[0], cast[1], cast[2], cast[3] );
452}
Note: See TracBrowser for help on using the repository browser.