source: trunk/libtransmission/net.c @ 2334

Last change on this file since 2334 was 2334, checked in by charles, 15 years ago

second draft of a fix for the tracker communication bug reported by TMT

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