source: trunk/libtransmission/net.c @ 1357

Last change on this file since 1357 was 1357, checked in by titer, 15 years ago

Use tr_cond* a bit more

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