source: trunk/libtransmission/net.c @ 6795

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

run libT, cli, daemon, gtk through the source-code formatter "uncrustify" as promised/threatened

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: net.c 6795 2008-09-23 19:11:04Z 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              int priority )
122{
123    int fd;
124
125    fd = tr_fdSocketCreate( type, priority );
126
127    if( fd >= 0 )
128        fd = makeSocketNonBlocking( fd );
129
130#if 0
131    if( fd >= 0 )
132    {
133        const int buffsize = 1500 * 3; /* 3x MTU for most ethernet/wireless */
134        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) );
135    }
136#endif
137
138    return fd;
139}
140
141int
142tr_netOpenTCP( const struct in_addr * addr,
143               tr_port_t              port,
144               int                    priority )
145{
146    int                s;
147    struct sockaddr_in sock;
148    const int          type = SOCK_STREAM;
149
150    if( ( s = createSocket( type, priority ) ) < 0 )
151        return -1;
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, 1 ) ) < 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( int              b,
218              struct in_addr * addr,
219              tr_port_t *      port )
220{
221    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
222}
223
224void
225tr_netClose( int s )
226{
227    tr_fdSocketClose( s );
228}
229
230void
231tr_netNtop( const struct in_addr * addr,
232            char *                 buf,
233            int                    len )
234{
235    const uint8_t * cast;
236
237    cast = (const uint8_t *)addr;
238    tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
239                 cast[0], cast[1], cast[2], cast[3] );
240}
241
Note: See TracBrowser for help on using the repository browser.