source: trunk/libtransmission/net.c @ 5957

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

remove more dead code

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