source: trunk/libtransmission/net.c @ 2552

Last change on this file since 2552 was 2552, checked in by charles, 14 years ago

better encapsulation of platform-specific constructs: tr_thread_t, tr_cond_t, tr_lock_t

  • Property svn:keywords set to Date Rev Author Id
File size: 11.6 KB
Line 
1/******************************************************************************
2 * $Id: net.c 2552 2007-07-30 15:27:52Z charles $
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 <errno.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34#include <netdb.h>
35#include <fcntl.h>
36
37#include "transmission.h"
38#include "fdlimit.h"
39#include "net.h"
40
41
42/***********************************************************************
43 * DNS resolution
44 **********************************************************************/
45
46/***********************************************************************
47 * tr_netResolve
48 ***********************************************************************
49 * Synchronous "resolution": only works with character strings
50 * representing numbers expressed in the Internet standard `.' notation.
51 * Returns a non-zero value if an error occurs.
52 **********************************************************************/
53int tr_netResolve( const char * address, struct in_addr * addr )
54{
55    addr->s_addr = inet_addr( address );
56    return ( addr->s_addr == 0xFFFFFFFF );
57}
58
59static tr_thread_t  * resolveThread;
60static tr_lock_t    * resolveLock;
61static tr_cond_t    * resolveCond;
62static volatile int   resolveDie;
63static tr_resolve_t * resolveQueue;
64
65static void resolveRelease ( tr_resolve_t * );
66static void resolveFunc    ( void * );
67
68struct tr_resolve_s
69{
70    tr_tristate_t  status;
71    char           * address;
72    struct in_addr addr;
73
74    int            refcount;
75    tr_resolve_t   * next;
76};
77
78/***********************************************************************
79 * tr_netResolveThreadInit
80 ***********************************************************************
81 * Initializes the static variables used for resolution and launch the
82 * gethostbyname thread.
83 **********************************************************************/
84void tr_netResolveThreadInit()
85{
86    resolveDie   = 0;
87    resolveQueue = NULL;
88    resolveLock = tr_lockNew( );
89    resolveCond = tr_condNew( );
90    resolveThread = tr_threadNew( resolveFunc, NULL, "resolve" );
91}
92
93/***********************************************************************
94 * tr_netResolveThreadClose
95 ***********************************************************************
96 * Notices the gethostbyname thread that is should terminate. Doesn't
97 * wait until it does, in case it is stuck in a resolution: we let it
98 * die and clean itself up.
99 **********************************************************************/
100void tr_netResolveThreadClose()
101{
102    tr_lockLock( resolveLock );
103    resolveDie = 1;
104    tr_lockUnlock( resolveLock );
105    tr_condSignal( resolveCond );
106    tr_wait( 200 );
107}
108
109/***********************************************************************
110 * tr_netResolveInit
111 ***********************************************************************
112 * Adds an address to the resolution queue.
113 **********************************************************************/
114tr_resolve_t * tr_netResolveInit( const char * address )
115{
116    tr_resolve_t * r = tr_new0( tr_resolve_t, 1 );
117    r->status   = TR_NET_WAIT;
118    r->address  = strdup( address );
119    r->refcount = 2;
120    r->next     = NULL;
121
122    tr_lockLock( resolveLock );
123    if( !resolveQueue )
124    {
125        resolveQueue = r;
126    }
127    else
128    {
129        tr_resolve_t * iter;
130        for( iter = resolveQueue; iter->next; iter = iter->next );
131        iter->next = r;
132    }
133    tr_lockUnlock( resolveLock );
134    tr_condSignal( resolveCond );
135
136    return r;
137}
138
139/***********************************************************************
140 * tr_netResolvePulse
141 ***********************************************************************
142 * Checks the current status of a resolution.
143 **********************************************************************/
144tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
145{
146    tr_tristate_t ret;
147
148    tr_lockLock( resolveLock );
149    ret = r->status;
150    if( ret == TR_NET_OK )
151    {
152        *addr = r->addr;
153    }
154    tr_lockUnlock( resolveLock );
155
156    return ret;
157}
158
159/***********************************************************************
160 * tr_netResolveClose
161 ***********************************************************************
162 *
163 **********************************************************************/
164void tr_netResolveClose( tr_resolve_t * r )
165{
166    resolveRelease( r );
167}
168
169/***********************************************************************
170 * resolveRelease
171 ***********************************************************************
172 * The allocated tr_resolve_t structures should be freed when
173 * tr_netResolveClose was called *and* it was removed from the queue.
174 * This can happen in any order, so we use a refcount to know we can
175 * take it out.
176 **********************************************************************/
177static void resolveRelease( tr_resolve_t * r )
178{
179    if( --r->refcount < 1 )
180    {
181        free( r->address );
182        free( r );
183    }
184}
185
186/***********************************************************************
187 * resolveFunc
188 ***********************************************************************
189 * Keeps waiting for addresses to resolve, and removes them from the
190 * queue once resolution is done.
191 **********************************************************************/
192static void resolveFunc( void * arg UNUSED )
193{
194    tr_resolve_t * r;
195    struct hostent * host;
196
197    tr_lockLock( resolveLock );
198
199    while( !resolveDie )
200    {
201        if( !( r = resolveQueue ) )
202        {
203            tr_condWait( resolveCond, resolveLock );
204            continue;
205        }
206
207        /* Blocking resolution */
208        tr_lockUnlock( resolveLock );
209        host = gethostbyname( r->address );
210        tr_lockLock( resolveLock );
211
212        if( host )
213        {
214            memcpy( &r->addr, host->h_addr, host->h_length );
215            r->status = TR_NET_OK;
216        }
217        else
218        {
219            r->status = TR_NET_ERROR;
220        }
221       
222        resolveQueue = r->next;
223        resolveRelease( r );
224    }
225
226    /* Clean up  */
227    tr_lockUnlock( resolveLock );
228    tr_lockFree( resolveLock );
229    resolveLock = NULL;
230    while( ( r = resolveQueue ) )
231    {
232        resolveQueue = r->next;
233        resolveRelease( r );
234    }
235}
236
237
238/***********************************************************************
239 * TCP/UDP sockets
240 **********************************************************************/
241
242static int makeSocketNonBlocking( int s )
243{
244    int flags;
245
246#ifdef SYS_BEOS
247    flags = 1;
248    if( setsockopt( s, SOL_SOCKET, SO_NONBLOCK,
249                    &flags, sizeof( int ) ) < 0 )
250#else
251    if( ( flags = fcntl( s, F_GETFL, 0 ) ) < 0 ||
252        fcntl( s, F_SETFL, flags | O_NONBLOCK ) < 0 )
253#endif
254    {
255        tr_err( "Could not set socket to non-blocking mode (%s)",
256                strerror( errno ) );
257        tr_netClose( s );
258        return -1;
259    }
260
261    return s;
262}
263
264static int createSocket( int type, int priority )
265{
266    int s;
267    if( ( s = tr_fdSocketCreate( type, priority ) ) < 0 )
268    {
269        return -1;
270    }
271    return makeSocketNonBlocking( s );
272}
273
274int tr_netOpen( const struct in_addr * addr, in_port_t port,
275                int type, int priority )
276{
277    int s;
278    struct sockaddr_in sock;
279
280    if( ( s = createSocket( type, priority ) ) < 0 )
281    {
282        return -1;
283    }
284
285    memset( &sock, 0, sizeof( sock ) );
286    sock.sin_family      = AF_INET;
287    sock.sin_addr.s_addr = addr->s_addr;
288    sock.sin_port        = port;
289
290    if( connect( s, (struct sockaddr *) &sock,
291                 sizeof( struct sockaddr_in ) ) < 0 &&
292        errno != EINPROGRESS )
293    {
294        tr_err( "Could not connect socket (%s)", strerror( errno ) );
295        tr_netClose( s );
296        return -1;
297    }
298
299    return s;
300}
301
302#ifdef IP_ADD_MEMBERSHIP
303int tr_netMcastOpen( int port, const struct in_addr * addr )
304{
305    int fd;
306    struct ip_mreq req;
307
308    fd = tr_netBindUDP( port );
309    if( 0 > fd )
310    {
311        return -1;
312    }
313
314    memset( &req, 0, sizeof( req ) );
315    req.imr_multiaddr.s_addr = addr->s_addr;
316    req.imr_interface.s_addr = htonl( INADDR_ANY );
317    if( setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof ( req ) ) )
318    {
319        tr_err( "Could not join multicast group (%s)", strerror( errno ) );
320        tr_netClose( fd );
321        return -1;
322    }
323
324    return fd;
325}
326#else /* IP_ADD_MEMBERSHIP */
327int tr_netMcastOpen( int port UNUSED, const struct in_addr * addr UNUSED )
328{
329    return -1;
330}
331#endif /* IP_ADD_MEMBERSHIP */
332
333int tr_netBind( int port, int type )
334{
335    int s;
336    struct sockaddr_in sock;
337#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
338    int optval;
339#endif
340
341    if( ( s = createSocket( type, 1 ) ) < 0 )
342    {
343        return -1;
344    }
345
346#ifdef SO_REUSEADDR
347    optval = 1;
348    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( optval ) );
349#endif
350
351#ifdef SO_REUSEPORT
352    if( SOCK_DGRAM == type )
353    {
354        optval = 1;
355        setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof( optval ) );
356    }
357#endif
358
359    memset( &sock, 0, sizeof( sock ) );
360    sock.sin_family      = AF_INET;
361    sock.sin_addr.s_addr = INADDR_ANY;
362    sock.sin_port        = htons( port );
363
364    if( bind( s, (struct sockaddr *) &sock,
365               sizeof( struct sockaddr_in ) ) )
366    {
367        tr_err( "Could not bind port %d", port );
368        tr_netClose( s );
369        return -1;
370    }
371
372    return s;
373}
374
375int tr_netAccept( int b, struct in_addr * addr, in_port_t * port )
376{
377    int s;
378    if( ( s = tr_fdSocketAccept( b, addr, port ) ) < 0 )
379    {
380        return -1;
381    }
382    return makeSocketNonBlocking( s );
383}
384
385int
386tr_netSend( int s, const void * buf, int size )
387{
388    const int ret = send( s, buf, size, 0 );
389    if( ret >= 0 )
390        return ret;
391
392    if( errno == ENOTCONN || errno == EAGAIN || errno == EWOULDBLOCK )
393        return TR_NET_BLOCK;
394
395    return TR_NET_CLOSE;
396}
397
398int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
399{
400    socklen_t len;
401    int       ret;
402
403    len = ( NULL == addr ? 0 : sizeof( *addr ) );
404    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
405    if( ret < 0 )
406    {
407        if( errno == EAGAIN || errno == EWOULDBLOCK )
408        {
409            ret = TR_NET_BLOCK;
410        }
411        else
412        {
413            ret = TR_NET_CLOSE;
414        }
415    }
416    if( !ret )
417    {
418        ret = TR_NET_CLOSE;
419    }
420
421    return ret;
422}
423
424void tr_netClose( int s )
425{
426    tr_fdSocketClose( s );
427}
428
429void tr_netNtop( const struct in_addr * addr, char * buf, int len )
430{
431    const uint8_t * cast;
432
433    cast = (const uint8_t *)addr;
434    snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
435              cast[0], cast[1], cast[2], cast[3] );
436}
Note: See TracBrowser for help on using the repository browser.