source: trunk/libtransmission/net.c @ 2311

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

clean up #includes a bit.

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