source: branches/1.4x/libtransmission/net.c @ 7455

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

(1.4x libT) backport handshake, peer, bandwidth, peer-io to 1.4x.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.5 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7455 2008-12-22 00:51:14Z 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
50
51void
52tr_netInit( void )
53{
54    static int initialized = FALSE;
55
56    if( !initialized )
57    {
58#ifdef WIN32
59        WSADATA wsaData;
60        WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
61#endif
62        initialized = TRUE;
63    }
64}
65
66tr_bool
67tr_isAddress( const tr_address * a )
68{
69    return a != NULL; /* this is implemented better in 1.50 */
70}
71
72
73/***********************************************************************
74 * DNS resolution
75 *
76 * Synchronous "resolution": only works with character strings
77 * representing numbers expressed in the Internet standard `.' notation.
78 * Returns a non-zero value if an error occurs.
79 **********************************************************************/
80int
81tr_netResolve( const char *     address,
82               struct in_addr * addr )
83{
84    addr->s_addr = inet_addr( address );
85    return addr->s_addr == 0xFFFFFFFF;
86}
87
88/***********************************************************************
89 * TCP sockets
90 **********************************************************************/
91
92int
93tr_netSetTOS( int s,
94              int tos )
95{
96#ifdef IP_TOS
97    return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
98#else
99    return 0;
100#endif
101}
102
103static int
104makeSocketNonBlocking( int fd )
105{
106    if( fd >= 0 )
107    {
108#if defined( __BEOS__ )
109        int flags = 1;
110        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
111                       &flags, sizeof( int ) ) < 0 )
112#else
113        if( evutil_make_socket_nonblocking( fd ) )
114#endif
115        {
116            tr_err( _( "Couldn't create socket: %s" ),
117                   tr_strerror( sockerrno ) );
118            tr_netClose( fd );
119            fd = -1;
120        }
121    }
122
123    return fd;
124}
125
126static int
127createSocket( int type )
128{
129    return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
130}
131
132static void
133setSndBuf( tr_session * session UNUSED, int fd UNUSED )
134{
135#if 0
136    if( fd >= 0 )
137    {
138        const int sndbuf = session->so_sndbuf;
139        const int rcvbuf = session->so_rcvbuf;
140        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
141        setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof( rcvbuf ) );
142    }
143#endif
144}
145
146int
147tr_netOpenTCP( tr_session            * session,
148               const struct in_addr  * addr,
149               tr_port_t               port )
150{
151    int                s;
152    struct sockaddr_in sock;
153    const int          type = SOCK_STREAM;
154
155    if( ( s = createSocket( type ) ) < 0 )
156        return -1;
157
158    setSndBuf( session, s );
159
160    memset( &sock, 0, sizeof( sock ) );
161    sock.sin_family      = AF_INET;
162    sock.sin_addr.s_addr = addr->s_addr;
163    sock.sin_port        = port;
164
165    if( ( connect( s, (struct sockaddr *) &sock,
166                  sizeof( struct sockaddr_in ) ) < 0 )
167#ifdef WIN32
168      && ( sockerrno != WSAEWOULDBLOCK )
169#endif
170      && ( sockerrno != EINPROGRESS ) )
171    {
172        tr_err( _(
173                   "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
174               s, inet_ntoa( *addr ), port,
175               sockerrno, tr_strerror( sockerrno ) );
176        tr_netClose( s );
177        s = -1;
178    }
179
180    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
181               s, tr_peerIoAddrStr( addr, port ) );
182
183    return s;
184}
185
186int
187tr_netBindTCP( int port )
188{
189    int                s;
190    struct sockaddr_in sock;
191    const int          type = SOCK_STREAM;
192
193#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
194    int                optval;
195#endif
196
197    if( ( s = createSocket( type ) ) < 0 )
198        return -1;
199
200#ifdef SO_REUSEADDR
201    optval = 1;
202    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
203#endif
204
205    memset( &sock, 0, sizeof( sock ) );
206    sock.sin_family      = AF_INET;
207    sock.sin_addr.s_addr = INADDR_ANY;
208    sock.sin_port        = htons( port );
209
210    if( bind( s, (struct sockaddr *) &sock,
211             sizeof( struct sockaddr_in ) ) )
212    {
213        tr_err( _( "Couldn't bind port %d: %s" ), port,
214               tr_strerror( sockerrno ) );
215        tr_netClose( s );
216        return -1;
217    }
218
219    tr_dbg(  "Bound socket %d to port %d", s, port );
220    return s;
221}
222
223int
224tr_netAccept( tr_session      * session,
225              int               b,
226              struct in_addr  * addr,
227              tr_port_t       * port )
228{
229    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
230    setSndBuf( session, fd );
231    return fd;
232}
233
234void
235tr_netClose( int s )
236{
237    tr_fdSocketClose( s );
238}
239
240void
241tr_netNtop( const struct in_addr * addr,
242            char *                 buf,
243            int                    len )
244{
245    const uint8_t * cast;
246
247    cast = (const uint8_t *)addr;
248    tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
249                 cast[0], cast[1], cast[2], cast[3] );
250}
251
252int
253tr_compareAddresses( const struct in_addr * a, const struct in_addr * b )
254{
255    if( a->s_addr != b->s_addr )
256        return a->s_addr < b->s_addr ? -1 : 1;
257
258    return 0;
259}
Note: See TracBrowser for help on using the repository browser.