source: trunk/libtransmission/net.c @ 7232

Last change on this file since 7232 was 7232, checked in by livings124, 12 years ago

BeOS and Amiga support is, at least for now, a lost cause and hasn't worked for long over a year - removing from the source (if you for whatever unknown reason still need this, get it through SVN)

  • Property svn:keywords set to Date Rev Author Id
File size: 7.8 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7232 2008-12-02 03:57:01Z livings124 $
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( evutil_make_socket_nonblocking( fd ) )
163        {
164            tr_err( _( "Couldn't create socket: %s" ),
165                   tr_strerror( sockerrno ) );
166            tr_netClose( fd );
167            fd = -1;
168        }
169    }
170
171    return fd;
172}
173
174static int
175createSocket( int type )
176{
177    return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
178}
179
180static void
181setSndBuf( tr_session * session UNUSED, int fd UNUSED )
182{
183#if 0
184    if( fd >= 0 )
185    {
186        const int sndbuf = session->so_sndbuf;
187        const int rcvbuf = session->so_rcvbuf;
188        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
189        setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
190    }
191#endif
192}
193
194static void 
195setup_sockaddr( const tr_address        * addr, 
196                tr_port                   port, 
197                struct sockaddr_storage * sockaddr) 
198{ 
199    struct sockaddr_in  sock4; 
200    struct sockaddr_in6 sock6; 
201
202    if( addr->type == TR_AF_INET ) 
203    { 
204        memset( &sock4, 0, sizeof( sock4 ) ); 
205        sock4.sin_family      = AF_INET; 
206        sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; 
207        sock4.sin_port        = port; 
208        memcpy( sockaddr, &sock4, sizeof( sock4 ) ); 
209    } 
210    else 
211    { 
212        memset( &sock6, 0, sizeof( sock6 ) ); 
213        sock6.sin6_family = AF_INET6; 
214        sock6.sin6_port = port; 
215        memcpy( &sock6.sin6_addr, &addr->addr, sizeof( struct in6_addr ) ); 
216        memcpy( sockaddr, &sock6, sizeof( sock6 ) ); 
217    } 
218} 
219 
220int 
221tr_netOpenTCP( tr_session        * session, 
222               const tr_address  * addr, 
223               tr_port             port ) 
224{ 
225    int                     s; 
226    struct sockaddr_storage sock; 
227    const int               type = SOCK_STREAM; 
228
229    if( ( s = createSocket( type ) ) < 0 )
230        return -1;
231
232    setSndBuf( session, s );
233
234    setup_sockaddr( addr, port, &sock );
235
236    if( ( connect( s, (struct sockaddr *) &sock,
237                  sizeof( struct sockaddr ) ) < 0 )
238#ifdef WIN32
239      && ( sockerrno != WSAEWOULDBLOCK )
240#endif
241      && ( sockerrno != EINPROGRESS ) )
242    {
243        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
244               s, tr_ntop_non_ts( addr ), (int)port, sockerrno, tr_strerror( sockerrno ) );
245        tr_netClose( s );
246        s = -1;
247    }
248
249    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
250               s, tr_peerIoAddrStr( addr, port ) );
251
252    return s;
253}
254
255int
256tr_netBindTCP( const tr_address * addr, tr_port port )
257{
258    int                      s;
259    struct sockaddr_storage  sock;
260    const int                type = SOCK_STREAM;
261
262#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
263    int                optval;
264#endif
265
266    if( ( s = createSocket( type ) ) < 0 )
267        return -1;
268
269#ifdef SO_REUSEADDR
270    optval = 1;
271    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
272#endif
273
274    setup_sockaddr( addr, htons( port ), &sock );
275
276    if( bind( s, (struct sockaddr *) &sock,
277             sizeof( struct sockaddr ) ) )
278    {
279        tr_err( _( "Couldn't bind port %d on %s: %s" ), port,
280               tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) );
281        tr_netClose( s );
282        return -1;
283    }
284
285    tr_dbg(  "Bound socket %d to port %d on %s",
286             s, port, tr_ntop_non_ts( addr ) );
287    return s;
288}
289
290int
291tr_netAccept( tr_session  * session,
292              int           b,
293              tr_address  * addr,
294              tr_port     * port )
295{
296    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
297    setSndBuf( session, fd );
298    return fd;
299}
300
301void
302tr_netClose( int s )
303{
304    tr_fdSocketClose( s );
305}
Note: See TracBrowser for help on using the repository browser.