source: trunk/libtransmission/net.c @ 7410

Last change on this file since 7410 was 7410, checked in by charles, 12 years ago

(trunk libT) add some assertions to tr_compareAddresses()

  • Property svn:keywords set to Date Rev Author Id
File size: 10.7 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7410 2008-12-16 06:46:27Z charles $
3 *
4 * Copyright (c) 2005-2008 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#include <assert.h>
30
31#include <sys/types.h>
32
33#ifdef WIN32
34 #include <winsock2.h> /* inet_addr */
35#else
36 #include <arpa/inet.h> /* inet_addr */
37 #include <netdb.h>
38 #include <fcntl.h>
39#endif
40
41#include <evutil.h>
42
43#include "transmission.h"
44#include "fdlimit.h"
45#include "natpmp.h"
46#include "net.h"
47#include "peer-io.h"
48#include "platform.h"
49#include "utils.h"
50
51const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; 
52const tr_address tr_inaddr_any = { TR_AF_INET, 
53    { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; 
54
55
56void
57tr_netInit( void )
58{
59    static int initialized = FALSE;
60
61    if( !initialized )
62    {
63#ifdef WIN32
64        WSADATA wsaData;
65        WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
66#endif
67        initialized = TRUE;
68    }
69}
70
71
72const char * 
73tr_ntop( const tr_address * src, char * dst, int size ) 
74{ 
75    if( src->type == TR_AF_INET ) 
76        return inet_ntop( AF_INET, &src->addr, dst, size ); 
77    else 
78        return inet_ntop( AF_INET6, &src->addr, dst, size ); 
79} 
80
81/*
82 * Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer.
83 * This function is suitable to be called from libTransmission's networking code,
84 * which is single-threaded.
85 */ 
86const char * 
87tr_ntop_non_ts( const tr_address * src ) 
88{ 
89    static char buf[INET6_ADDRSTRLEN]; 
90    return tr_ntop( src, buf, sizeof( buf ) ); 
91} 
92
93tr_address * 
94tr_pton( const char * src, tr_address * dst ) 
95{ 
96    int retval = inet_pton( AF_INET, src, &dst->addr ); 
97    if( retval < 0 ) 
98        return NULL; 
99    else if( retval == 0 ) 
100        retval = inet_pton( AF_INET6, src, &dst->addr ); 
101    else
102    { 
103        dst->type = TR_AF_INET; 
104        return dst; 
105    } 
106
107    if( retval < 1 ) 
108        return NULL; 
109    dst->type = TR_AF_INET6; 
110    return dst; 
111}
112
113void
114tr_normalizeV4Mapped( tr_address * const addr )
115{
116    if( addr->type == TR_AF_INET6 && IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ) )
117    {
118        addr->type = TR_AF_INET;
119        memcpy( &addr->addr.addr4.s_addr, addr->addr.addr6.s6_addr + 12, 4 );
120    }
121}
122
123/*
124 * Compare two tr_address structures.
125 * Returns:
126 * <0 if a < b
127 * >0 if a > b
128 * 0  if a == b
129 */ 
130int
131tr_compareAddresses( const tr_address * a, const tr_address * b)
132{
133    int addrlen;
134
135    assert( a );
136    assert( b );
137    assert( a->type == TR_AF_INET || a->type == TR_AF_INET6 );
138    assert( b->type == TR_AF_INET || b->type == TR_AF_INET6 );
139
140    /* IPv6 addresses are always "greater than" IPv4 */ 
141    if( a->type != b->type )
142        return a->type == TR_AF_INET ? 1 : -1;
143
144    if( a->type == TR_AF_INET ) 
145        addrlen = sizeof( struct in_addr ); 
146    else 
147        addrlen = sizeof( struct in6_addr ); 
148    return memcmp( &a->addr, &b->addr, addrlen );
149} 
150
151/***********************************************************************
152 * Socket list housekeeping
153 **********************************************************************/
154struct tr_socketList
155{
156    int             socket;
157    tr_address      addr;
158    tr_socketList * next;
159};
160
161tr_socketList *
162tr_socketListAppend( tr_socketList * const head,
163                     const tr_address * const addr )
164{
165    tr_socketList * tmp;
166    assert( head );
167    for( tmp = head; tmp->next; tmp = tmp->next );
168    tmp->next = tr_socketListNew( addr );
169    return tmp->next;
170}
171
172tr_socketList *
173tr_socketListNew( const tr_address * const addr )
174{
175    tr_socketList * tmp;
176    tmp = tr_new( tr_socketList, 1 );
177    tmp->socket = -1;
178    tmp->addr = *addr;
179    tmp->next = NULL;
180    return tmp;
181}
182
183void
184tr_socketListFree( tr_socketList * const head )
185{
186    assert( head );
187    if( head->next )
188        tr_socketListFree( head->next );
189    tr_free( head );
190}
191
192void
193tr_socketListRemove( tr_socketList * const head,
194                     tr_socketList * const el)
195{
196    tr_socketList * tmp;
197    assert( head );
198    assert( el );
199    for( tmp = head; tmp->next && tmp->next != el; tmp = tmp->next );
200    tmp->next = el->next;
201    el->next = NULL;
202    tr_socketListFree(el);
203}
204
205void
206tr_socketListTruncate( tr_socketList * const head,
207                       tr_socketList * const start )
208{
209    tr_socketList * tmp;
210    assert( head );
211    assert( start );
212    for( tmp = head; tmp->next && tmp->next != start; tmp = tmp->next );
213    tr_socketListFree( start );
214    tmp->next = NULL;
215}
216
217int
218tr_socketListGetSocket( const tr_socketList * const el )
219{
220    assert( el );
221    return el->socket;
222}
223
224const tr_address *
225tr_socketListGetAddress( const tr_socketList * const el )
226{
227    assert( el );
228    return &el->addr;
229}
230
231void
232tr_socketListForEach( tr_socketList * const head,
233                      void ( * cb ) ( int * const,
234                                      tr_address * const,
235                                      void * const),
236                      void * const userData )
237{
238    tr_socketList * tmp;
239    for( tmp = head; tmp; tmp = tmp->next )
240        cb( &tmp->socket, &tmp->addr, userData );
241}
242
243/***********************************************************************
244 * TCP sockets
245 **********************************************************************/
246
247int
248tr_netSetTOS( int s,
249              int tos )
250{
251#ifdef IP_TOS
252    return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
253#else
254    return 0;
255#endif
256}
257
258static int
259makeSocketNonBlocking( int fd )
260{
261    if( fd >= 0 )
262    {
263        if( evutil_make_socket_nonblocking( fd ) )
264        {
265            int tmperrno;
266            tr_err( _( "Couldn't create socket: %s" ),
267                   tr_strerror( sockerrno ) );
268            tmperrno = sockerrno;
269            tr_netClose( fd );
270            fd = -tmperrno;
271        }
272    }
273
274    return fd;
275}
276
277static int
278createSocket( int domain, int type )
279{
280    return makeSocketNonBlocking( tr_fdSocketCreate( domain, type ) );
281}
282
283static void
284setSndBuf( tr_session * session UNUSED, int fd UNUSED )
285{
286#if 0
287    if( fd >= 0 )
288    {
289        const int sndbuf = session->so_sndbuf;
290        const int rcvbuf = session->so_rcvbuf;
291        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
292        setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
293    }
294#endif
295}
296
297static socklen_t
298setup_sockaddr( const tr_address        * addr,
299                tr_port                   port,
300                struct sockaddr_storage * sockaddr)
301{
302    struct sockaddr_in  sock4;
303    struct sockaddr_in6 sock6;
304    if( addr->type == TR_AF_INET )
305    {
306        memset( &sock4, 0, sizeof( sock4 ) );
307        sock4.sin_family      = AF_INET;
308        sock4.sin_addr.s_addr = addr->addr.addr4.s_addr;
309        sock4.sin_port        = port;
310        memcpy( sockaddr, &sock4, sizeof( sock4 ) );
311        return sizeof( struct sockaddr_in );
312    }
313    else
314    {
315        memset( &sock6, 0, sizeof( sock6 ) );
316        sock6.sin6_family = AF_INET6;
317        sock6.sin6_port = port;
318        sock6.sin6_flowinfo = 0;
319        sock6.sin6_addr = addr->addr.addr6;
320        memcpy( sockaddr, &sock6, sizeof( sock6 ) );
321        return sizeof( struct sockaddr_in6 );
322    }
323}
324
325int
326tr_netOpenTCP( tr_session        * session,
327               const tr_address  * addr,
328               tr_port             port )
329{
330    int                     s;
331    struct sockaddr_storage sock;
332    const int               type = SOCK_STREAM;
333    socklen_t               addrlen;
334
335    if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ),
336                            type ) ) < 0 )
337        return s;
338
339    setSndBuf( session, s );
340
341    addrlen = setup_sockaddr( addr, port, &sock );
342
343    if( ( connect( s, (struct sockaddr *) &sock,
344                  addrlen ) < 0 )
345#ifdef WIN32
346      && ( sockerrno != WSAEWOULDBLOCK )
347#endif
348      && ( sockerrno != EINPROGRESS ) )
349    {
350        int tmperrno;
351        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
352               s, tr_ntop_non_ts( addr ), (int)port, sockerrno, tr_strerror( sockerrno ) );
353        tmperrno = sockerrno;
354        tr_netClose( s );
355        s = -tmperrno;
356    }
357
358    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
359               s, tr_peerIoAddrStr( addr, port ) );
360
361    return s;
362}
363
364int
365tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs )
366{
367    int                     s;
368    struct sockaddr_storage sock;
369    const int               type = SOCK_STREAM;
370    int                     addrlen;
371
372#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
373    int                optval;
374#endif
375
376    if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ),
377                            type ) ) < 0 )
378        return s;
379
380#ifdef SO_REUSEADDR
381    optval = 1;
382    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
383#endif
384
385    addrlen = setup_sockaddr( addr, htons( port ), &sock );
386
387    if( bind( s, (struct sockaddr *) &sock,
388             addrlen ) )
389    {
390        int tmperrno;
391        if( !suppressMsgs )
392            tr_err( _( "Couldn't bind port %d on %s: %s" ), port,
393                    tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) );
394        tmperrno = sockerrno;
395        tr_netClose( s );
396        return -tmperrno;
397    }
398    if( !suppressMsgs )
399        tr_dbg(  "Bound socket %d to port %d on %s",
400                 s, port, tr_ntop_non_ts( addr ) );
401    return s;
402}
403
404int
405tr_netAccept( tr_session  * session,
406              int           b,
407              tr_address  * addr,
408              tr_port     * port )
409{
410    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
411    setSndBuf( session, fd );
412    return fd;
413}
414
415void
416tr_netClose( int s )
417{
418    tr_fdSocketClose( s );
419}
Note: See TracBrowser for help on using the repository browser.