source: trunk/libtransmission/net.c @ 2544

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

this looks bug but it's not: just janitorial cleanup, moving #includes from headers into source file

  • Property svn:keywords set to Date Rev Author Id
File size: 11.6 KB
Line 
1/******************************************************************************
2 * $Id: net.c 2544 2007-07-29 18:11:21Z 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
41
42/***********************************************************************
43 * DNS resolution
44 **********************************************************************/
45
46/***********************************************************************
47 * tr_netResolve
48 ***********************************************************************
49 * Synchronous "resolution": only works with character strings
50 * representing numbers expressed in the Internet standard `.' notation.
51 * Returns a non-zero value if an error occurs.
52 **********************************************************************/
53int tr_netResolve( const char * address, struct in_addr * addr )
54{
55    addr->s_addr = inet_addr( address );
56    return ( addr->s_addr == 0xFFFFFFFF );
57}
58
59static tr_thread_t  resolveThread;
60static tr_lock_t    resolveLock;
61static tr_cond_t    resolveCond;
62static volatile int resolveDie;
63static tr_resolve_t * resolveQueue;
64
65static void resolveRelease ( tr_resolve_t * );
66static void resolveFunc    ( void * );
67
68struct tr_resolve_s
69{
70    tr_tristate_t  status;
71    char           * address;
72    struct in_addr addr;
73
74    int            refcount;
75    tr_resolve_t   * next;
76};
77
78/***********************************************************************
79 * tr_netResolveThreadInit
80 ***********************************************************************
81 * Initializes the static variables used for resolution and launch the
82 * gethostbyname thread.
83 **********************************************************************/
84void tr_netResolveThreadInit()
85{
86    resolveDie   = 0;
87    resolveQueue = NULL;
88    tr_lockInit( &resolveLock );
89    tr_condInit( &resolveCond );
90    tr_threadCreate( &resolveThread, resolveFunc, NULL, "resolve" );
91}
92
93/***********************************************************************
94 * tr_netResolveThreadClose
95 ***********************************************************************
96 * Notices the gethostbyname thread that is should terminate. Doesn't
97 * wait until it does, in case it is stuck in a resolution: we let it
98 * die and clean itself up.
99 **********************************************************************/
100void tr_netResolveThreadClose()
101{
102    tr_lockLock( &resolveLock );
103    resolveDie = 1;
104    tr_lockUnlock( &resolveLock );
105    tr_condSignal( &resolveCond );
106    tr_wait( 200 );
107}
108
109/***********************************************************************
110 * tr_netResolveInit
111 ***********************************************************************
112 * Adds an address to the resolution queue.
113 **********************************************************************/
114tr_resolve_t * tr_netResolveInit( const char * address )
115{
116    tr_resolve_t * r;
117
118    r           = malloc( sizeof( tr_resolve_t ) );
119    r->status   = TR_NET_WAIT;
120    r->address  = strdup( address );
121    r->refcount = 2;
122    r->next     = NULL;
123
124    tr_lockLock( &resolveLock );
125    if( !resolveQueue )
126    {
127        resolveQueue = r;
128    }
129    else
130    {
131        tr_resolve_t * iter;
132        for( iter = resolveQueue; iter->next; iter = iter->next );
133        iter->next = r;
134    }
135    tr_lockUnlock( &resolveLock );
136    tr_condSignal( &resolveCond );
137
138    return r;
139}
140
141/***********************************************************************
142 * tr_netResolvePulse
143 ***********************************************************************
144 * Checks the current status of a resolution.
145 **********************************************************************/
146tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
147{
148    tr_tristate_t ret;
149
150    tr_lockLock( &resolveLock );
151    ret = r->status;
152    if( ret == TR_NET_OK )
153    {
154        *addr = r->addr;
155    }
156    tr_lockUnlock( &resolveLock );
157
158    return ret;
159}
160
161/***********************************************************************
162 * tr_netResolveClose
163 ***********************************************************************
164 *
165 **********************************************************************/
166void tr_netResolveClose( tr_resolve_t * r )
167{
168    resolveRelease( r );
169}
170
171/***********************************************************************
172 * resolveRelease
173 ***********************************************************************
174 * The allocated tr_resolve_t structures should be freed when
175 * tr_netResolveClose was called *and* it was removed from the queue.
176 * This can happen in any order, so we use a refcount to know we can
177 * take it out.
178 **********************************************************************/
179static void resolveRelease( tr_resolve_t * r )
180{
181    if( --r->refcount < 1 )
182    {
183        free( r->address );
184        free( r );
185    }
186}
187
188/***********************************************************************
189 * resolveFunc
190 ***********************************************************************
191 * Keeps waiting for addresses to resolve, and removes them from the
192 * queue once resolution is done.
193 **********************************************************************/
194static void resolveFunc( void * arg UNUSED )
195{
196    tr_resolve_t * r;
197    struct hostent * host;
198
199    tr_lockLock( &resolveLock );
200
201    while( !resolveDie )
202    {
203        if( !( r = resolveQueue ) )
204        {
205            tr_condWait( &resolveCond, &resolveLock );
206            continue;
207        }
208
209        /* Blocking resolution */
210        tr_lockUnlock( &resolveLock );
211        host = gethostbyname( r->address );
212        tr_lockLock( &resolveLock );
213
214        if( host )
215        {
216            memcpy( &r->addr, host->h_addr, host->h_length );
217            r->status = TR_NET_OK;
218        }
219        else
220        {
221            r->status = TR_NET_ERROR;
222        }
223       
224        resolveQueue = r->next;
225        resolveRelease( r );
226    }
227
228    /* Clean up  */
229    tr_lockUnlock( &resolveLock );
230    tr_lockClose( &resolveLock );
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, in_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, in_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.