source: trunk/libtransmission/net.c @ 7231

Last change on this file since 7231 was 7231, checked in by charles, 13 years ago

(libT) re-apply jhujhiti's IPv6 patch. This merges in my tr_port cleanup, so any new bugs are mine :/

  • Property svn:keywords set to Date Rev Author Id
File size: 7.9 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7231 2008-12-02 03:41:58Z 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
30#include <sys/types.h>
31
32#ifdef WIN32
33 #include <winsock2.h> /* inet_addr */
34#else
35 #include <arpa/inet.h> /* inet_addr */
36 #include <netdb.h>
37 #include <fcntl.h>
38#endif
39
40#include <evutil.h>
41
42#include "transmission.h"
43#include "fdlimit.h"
44#include "natpmp.h"
45#include "net.h"
46#include "peer-io.h"
47#include "platform.h"
48#include "utils.h"
49
50const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; 
51const tr_address tr_inaddr_any = { TR_AF_INET, 
52    { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; 
53
54
55void
56tr_netInit( void )
57{
58    static int initialized = FALSE;
59
60    if( !initialized )
61    {
62#ifdef WIN32
63        WSADATA wsaData;
64        WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
65#endif
66        initialized = TRUE;
67    }
68}
69
70
71const char * 
72tr_ntop( const tr_address * src, char * dst, int size ) 
73{ 
74    if( src->type == TR_AF_INET ) 
75        return inet_ntop( AF_INET, &src->addr, dst, size ); 
76    else 
77        return inet_ntop( AF_INET6, &src->addr, dst, size ); 
78} 
79
80/*
81 * Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer.
82 * This function is suitable to be called from libTransmission's networking code,
83 * which is single-threaded.
84 */ 
85const char * 
86tr_ntop_non_ts( const tr_address * src ) 
87{ 
88    static char buf[INET6_ADDRSTRLEN]; 
89    return tr_ntop( src, buf, sizeof( buf ) ); 
90} 
91
92tr_address * 
93tr_pton( const char * src, tr_address * dst ) 
94{ 
95    int retval = inet_pton( AF_INET, src, &dst->addr ); 
96    if( retval < 0 ) 
97        return NULL; 
98    else if( retval == 0 ) 
99        retval = inet_pton( AF_INET6, src, &dst->addr ); 
100    else
101    { 
102        dst->type = TR_AF_INET; 
103        return dst; 
104    } 
105
106    if( retval < 1 ) 
107        return NULL; 
108    dst->type = TR_AF_INET6; 
109    return dst; 
110} 
111
112/*
113 * Compare two tr_address structures.
114 * Returns:
115 * <0 if a < b
116 * >0 if a > b
117 * 0  if a == b
118 */ 
119int 
120tr_compareAddresses( const tr_address * a, const tr_address * b) 
121{ 
122    int retval; 
123    int addrlen; 
124
125    /* IPv6 addresses are always "greater than" IPv4 */ 
126    if( a->type == TR_AF_INET && b->type == TR_AF_INET6 ) 
127        return 1; 
128    if( a->type == TR_AF_INET6 && b->type == TR_AF_INET ) 
129        return -1; 
130
131    if( a->type == TR_AF_INET ) 
132        addrlen = sizeof( struct in_addr ); 
133    else 
134        addrlen = sizeof( struct in6_addr ); 
135    retval = memcmp( &a->addr, &b->addr, addrlen ); 
136    if( retval == 0 ) 
137        return 0; 
138     
139    return retval; 
140} 
141
142/***********************************************************************
143 * TCP sockets
144 **********************************************************************/
145
146int
147tr_netSetTOS( int s,
148              int tos )
149{
150#ifdef IP_TOS
151    return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
152#else
153    return 0;
154#endif
155}
156
157static int
158makeSocketNonBlocking( int fd )
159{
160    if( fd >= 0 )
161    {
162#if defined( __BEOS__ )
163        int flags = 1;
164        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
165                       &flags, sizeof( int ) ) < 0 )
166#else
167        if( evutil_make_socket_nonblocking( fd ) )
168#endif
169        {
170            tr_err( _( "Couldn't create socket: %s" ),
171                   tr_strerror( sockerrno ) );
172            tr_netClose( fd );
173            fd = -1;
174        }
175    }
176
177    return fd;
178}
179
180static int
181createSocket( int type )
182{
183    return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
184}
185
186static void
187setSndBuf( tr_session * session UNUSED, int fd UNUSED )
188{
189#if 0
190    if( fd >= 0 )
191    {
192        const int sndbuf = session->so_sndbuf;
193        const int rcvbuf = session->so_rcvbuf;
194        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
195        setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
196    }
197#endif
198}
199
200static void 
201setup_sockaddr( const tr_address        * addr, 
202                tr_port                   port, 
203                struct sockaddr_storage * sockaddr) 
204{ 
205    struct sockaddr_in  sock4; 
206    struct sockaddr_in6 sock6; 
207
208    if( addr->type == TR_AF_INET ) 
209    { 
210        memset( &sock4, 0, sizeof( sock4 ) ); 
211        sock4.sin_family      = AF_INET; 
212        sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; 
213        sock4.sin_port        = port; 
214        memcpy( sockaddr, &sock4, sizeof( sock4 ) ); 
215    } 
216    else 
217    { 
218        memset( &sock6, 0, sizeof( sock6 ) ); 
219        sock6.sin6_family = AF_INET6; 
220        sock6.sin6_port = port; 
221        memcpy( &sock6.sin6_addr, &addr->addr, sizeof( struct in6_addr ) ); 
222        memcpy( sockaddr, &sock6, sizeof( sock6 ) ); 
223    } 
224} 
225 
226int 
227tr_netOpenTCP( tr_session        * session, 
228               const tr_address  * addr, 
229               tr_port             port ) 
230{ 
231    int                     s; 
232    struct sockaddr_storage sock; 
233    const int               type = SOCK_STREAM; 
234
235    if( ( s = createSocket( type ) ) < 0 )
236        return -1;
237
238    setSndBuf( session, s );
239
240    setup_sockaddr( addr, port, &sock );
241
242    if( ( connect( s, (struct sockaddr *) &sock,
243                  sizeof( struct sockaddr ) ) < 0 )
244#ifdef WIN32
245      && ( sockerrno != WSAEWOULDBLOCK )
246#endif
247      && ( sockerrno != EINPROGRESS ) )
248    {
249        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
250               s, tr_ntop_non_ts( addr ), (int)port, sockerrno, tr_strerror( sockerrno ) );
251        tr_netClose( s );
252        s = -1;
253    }
254
255    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
256               s, tr_peerIoAddrStr( addr, port ) );
257
258    return s;
259}
260
261int
262tr_netBindTCP( const tr_address * addr, tr_port port )
263{
264    int                      s;
265    struct sockaddr_storage  sock;
266    const int                type = SOCK_STREAM;
267
268#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
269    int                optval;
270#endif
271
272    if( ( s = createSocket( type ) ) < 0 )
273        return -1;
274
275#ifdef SO_REUSEADDR
276    optval = 1;
277    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
278#endif
279
280    setup_sockaddr( addr, htons( port ), &sock );
281
282    if( bind( s, (struct sockaddr *) &sock,
283             sizeof( struct sockaddr ) ) )
284    {
285        tr_err( _( "Couldn't bind port %d on %s: %s" ), port,
286               tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) );
287        tr_netClose( s );
288        return -1;
289    }
290
291    tr_dbg(  "Bound socket %d to port %d on %s",
292             s, port, tr_ntop_non_ts( addr ) );
293    return s;
294}
295
296int
297tr_netAccept( tr_session  * session,
298              int           b,
299              tr_address  * addr,
300              tr_port     * port )
301{
302    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
303    setSndBuf( session, fd );
304    return fd;
305}
306
307void
308tr_netClose( int s )
309{
310    tr_fdSocketClose( s );
311}
Note: See TracBrowser for help on using the repository browser.