source: trunk/libtransmission/net.c @ 2555

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

add portability wrapper for in_port_t...

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