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

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

(1.4x libT) backport the bandwidth fixes back to the 1.4x branch, pass #2: most of the bandwidth code, plus autoconf and xcode makefile stuff

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