source: trunk/libtransmission/net.c @ 7195

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

(libT) patch from jhujhiti to add ipv6 support.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.9 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7195 2008-11-30 00:47:18Z 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
54void
55tr_netInit( void )
56{
57    static int initialized = FALSE;
58
59    if( !initialized )
60    {
61#ifdef WIN32
62        WSADATA wsaData;
63        WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
64#endif
65        initialized = TRUE;
66    }
67}
68
69const char *
70tr_ntop( const tr_address * src,
71         char * dst,
72         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,
94         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
113/*
114 * Compare two tr_address structures.
115 * Returns:
116 * <0 if a < b
117 * >0 if a > b
118 * 0  if a == b
119 */
120int
121tr_compareAddresses( const tr_address * a,
122                     const tr_address * b)
123{
124    int retval;
125    int addrlen;
126    /* IPv6 addresses are always "greater than" IPv4 */
127    if( a->type == TR_AF_INET && b->type == TR_AF_INET6 )
128        return 1;
129    if( a->type == TR_AF_INET6 && b->type == TR_AF_INET )
130        return -1;
131   
132    if( a->type == TR_AF_INET )
133        addrlen = sizeof( struct in_addr );
134    else
135        addrlen = sizeof( struct in6_addr );
136   
137    retval = memcmp( &a->addr, &b->addr, addrlen );
138    if( retval == 0 )
139        return 0;
140   
141    return retval;
142}
143
144/***********************************************************************
145 * TCP sockets
146 **********************************************************************/
147
148int
149tr_netSetTOS( int s,
150              int tos )
151{
152#ifdef IP_TOS
153    return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
154#else
155    return 0;
156#endif
157}
158
159static int
160makeSocketNonBlocking( int fd )
161{
162    if( fd >= 0 )
163    {
164#if defined( __BEOS__ )
165        int flags = 1;
166        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
167                       &flags, sizeof( int ) ) < 0 )
168#else
169        if( evutil_make_socket_nonblocking( fd ) )
170#endif
171        {
172            tr_err( _( "Couldn't create socket: %s" ),
173                   tr_strerror( sockerrno ) );
174            tr_netClose( fd );
175            fd = -1;
176        }
177    }
178
179    return fd;
180}
181
182static int
183createSocket( int type )
184{
185    return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
186}
187
188static void
189setSndBuf( tr_session * session UNUSED, int fd UNUSED )
190{
191#if 0
192    if( fd >= 0 )
193    {
194        const int sndbuf = session->so_sndbuf;
195        const int rcvbuf = session->so_rcvbuf;
196        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
197        setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
198    }
199#endif
200}
201
202static void
203setup_sockaddr( const tr_address        * addr,
204                tr_port_t                 port,
205                struct sockaddr_storage * sockaddr)
206{
207    struct sockaddr_in  sock4;
208    struct sockaddr_in6 sock6;
209    if( addr->type == TR_AF_INET )
210    {
211        memset( &sock4, 0, sizeof( sock4 ) );
212        sock4.sin_family      = AF_INET;
213        sock4.sin_addr.s_addr = addr->addr.addr4.s_addr;
214        sock4.sin_port        = port;
215        memcpy( sockaddr, &sock4, sizeof( sock4 ) );
216    }
217    else
218    {
219        memset( &sock6, 0, sizeof( sock6 ) );
220        sock6.sin6_family = AF_INET6;
221        sock6.sin6_port = port;
222        memcpy( &sock6.sin6_addr, &addr->addr, sizeof( struct in6_addr ) );
223        memcpy( sockaddr, &sock6, sizeof( sock6 ) );
224    }
225}
226
227int
228tr_netOpenTCP( tr_session        * session,
229               const tr_address  * addr,
230               tr_port_t           port )
231{
232    int                     s;
233    struct sockaddr_storage sock;
234    const int               type = SOCK_STREAM;
235
236    if( ( s = createSocket( type ) ) < 0 )
237        return -1;
238
239    setSndBuf( session, s );
240
241    setup_sockaddr( addr, port, &sock );
242
243    if( ( connect( s, (struct sockaddr *) &sock,
244                  sizeof( struct sockaddr ) ) < 0 )
245#ifdef WIN32
246      && ( sockerrno != WSAEWOULDBLOCK )
247#endif
248      && ( sockerrno != EINPROGRESS ) )
249    {
250        tr_err( _(
251                   "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
252               s, tr_ntop_non_ts( addr ), port,
253               sockerrno, tr_strerror( sockerrno ) );
254        tr_netClose( s );
255        s = -1;
256    }
257
258    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
259               s, tr_peerIoAddrStr( addr, port ) );
260
261    return s;
262}
263
264int
265tr_netBindTCP( const tr_address * addr,
266               int port )
267{
268    int                     s;
269    struct sockaddr_storage sock;
270    const int               type = SOCK_STREAM;
271
272#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
273    int                optval;
274#endif
275
276    if( ( s = createSocket( type ) ) < 0 )
277        return -1;
278
279#ifdef SO_REUSEADDR
280    optval = 1;
281    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
282#endif
283   
284    setup_sockaddr( addr, htons( port ), &sock );
285
286    if( bind( s, (struct sockaddr *) &sock,
287             sizeof( struct sockaddr ) ) )
288    {
289        tr_err( _( "Couldn't bind port %d on %s: %s" ), port,
290               tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) );
291        tr_netClose( s );
292        return -1;
293    }
294
295    tr_dbg(  "Bound socket %d to port %d on %s", s, port,
296           tr_ntop_non_ts( addr ) );
297    return s;
298}
299
300int
301tr_netAccept( tr_session * session,
302              int          b,
303              tr_address * addr,
304              tr_port_t  * port )
305{
306    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
307    setSndBuf( session, fd );
308    return fd;
309}
310
311void
312tr_netClose( int s )
313{
314    tr_fdSocketClose( s );
315}
Note: See TracBrowser for help on using the repository browser.