source: trunk/libtransmission/net.c @ 7133

Last change on this file since 7133 was 7133, checked in by charles, 14 years ago

(libT) define peer connections' sockets' so_sndbuf size in the tr_session struct.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: net.c 7133 2008-11-21 16:32:55Z 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, int fd )
127{
128    if( fd >= 0 )
129    {
130        const int sndbuf = session->so_sndbuf;
131        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof( sndbuf ) );
132    }
133}
134
135int
136tr_netOpenTCP( tr_session            * session,
137               const struct in_addr  * addr,
138               tr_port_t               port )
139{
140    int                s;
141    struct sockaddr_in sock;
142    const int          type = SOCK_STREAM;
143
144    if( ( s = createSocket( type ) ) < 0 )
145        return -1;
146
147    setSndBuf( session, s );
148
149    memset( &sock, 0, sizeof( sock ) );
150    sock.sin_family      = AF_INET;
151    sock.sin_addr.s_addr = addr->s_addr;
152    sock.sin_port        = port;
153
154    if( ( connect( s, (struct sockaddr *) &sock,
155                  sizeof( struct sockaddr_in ) ) < 0 )
156#ifdef WIN32
157      && ( sockerrno != WSAEWOULDBLOCK )
158#endif
159      && ( sockerrno != EINPROGRESS ) )
160    {
161        tr_err( _(
162                   "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
163               s, inet_ntoa( *addr ), port,
164               sockerrno, tr_strerror( sockerrno ) );
165        tr_netClose( s );
166        s = -1;
167    }
168
169    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
170               s, tr_peerIoAddrStr( addr, port ) );
171
172    return s;
173}
174
175int
176tr_netBindTCP( int port )
177{
178    int                s;
179    struct sockaddr_in sock;
180    const int          type = SOCK_STREAM;
181
182#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
183    int                optval;
184#endif
185
186    if( ( s = createSocket( type ) ) < 0 )
187        return -1;
188
189#ifdef SO_REUSEADDR
190    optval = 1;
191    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
192#endif
193
194    memset( &sock, 0, sizeof( sock ) );
195    sock.sin_family      = AF_INET;
196    sock.sin_addr.s_addr = INADDR_ANY;
197    sock.sin_port        = htons( port );
198
199    if( bind( s, (struct sockaddr *) &sock,
200             sizeof( struct sockaddr_in ) ) )
201    {
202        tr_err( _( "Couldn't bind port %d: %s" ), port,
203               tr_strerror( sockerrno ) );
204        tr_netClose( s );
205        return -1;
206    }
207
208    tr_dbg(  "Bound socket %d to port %d", s, port );
209    return s;
210}
211
212int
213tr_netAccept( tr_session      * session,
214              int               b,
215              struct in_addr  * addr,
216              tr_port_t       * port )
217{
218    int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
219    setSndBuf( session, fd );
220    return fd;
221}
222
223void
224tr_netClose( int s )
225{
226    tr_fdSocketClose( s );
227}
228
229void
230tr_netNtop( const struct in_addr * addr,
231            char *                 buf,
232            int                    len )
233{
234    const uint8_t * cast;
235
236    cast = (const uint8_t *)addr;
237    tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
238                 cast[0], cast[1], cast[2], cast[3] );
239}
240
Note: See TracBrowser for help on using the repository browser.