source: trunk/libtransmission/net.c @ 6944

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

remove dead code

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