source: trunk/libtransmission/net.c @ 6556

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

#220: lazy bitfields

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