source: trunk/libtransmission/net.c @ 2343

Last change on this file since 2343 was 2343, checked in by joshe, 15 years ago

Change a couple functions to take an in_addr pointer instead of an in_addr.
Forward declare struct in_addr and include the relevant headers in the .c files where it's used.

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