source: trunk/libtransmission/net.c @ 2578

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

More Win32 portability changes

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