source: trunk/libtransmission/net.c @ 5193

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

first cut at adding i18n hooks into libtransmission.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.3 KB
Line 
1/******************************************************************************
2 * $Id: net.c 5193 2008-03-04 02:02:25Z 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/UDP sockets
80 **********************************************************************/
81
82static int
83makeSocketNonBlocking( int fd )
84{
85    if( fd >= 0 )
86    {
87#if defined(__BEOS__)
88        int flags = 1;
89        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
90                        &flags, sizeof( int ) ) < 0 )
91#else
92        if( evutil_make_socket_nonblocking( fd ) )
93#endif
94        {
95            tr_err( _( "Couldn't set socket to non-blocking mode: %s" ),
96                    tr_strerror( sockerrno ) );
97            tr_netClose( fd );
98            fd = -1;
99        }
100    }
101
102    return fd;
103}
104
105static int
106createSocket( int type, int priority )
107{
108    return makeSocketNonBlocking( tr_fdSocketCreate( type, priority ) );
109}
110
111static int
112tr_netOpen( const struct in_addr * addr, tr_port_t port,
113            int type, int priority )
114{
115    int s;
116    struct sockaddr_in sock;
117
118    if( ( s = createSocket( type, priority ) ) < 0 )
119    {
120        return -1;
121    }
122
123    memset( &sock, 0, sizeof( sock ) );
124    sock.sin_family      = AF_INET;
125    sock.sin_addr.s_addr = addr->s_addr;
126    sock.sin_port        = port;
127
128    if( ( connect( s, (struct sockaddr *) &sock,
129                   sizeof( struct sockaddr_in ) ) < 0 )
130#ifdef WIN32
131        && ( sockerrno != WSAEWOULDBLOCK )
132#endif
133        && ( sockerrno != EINPROGRESS ) )
134    {
135        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
136                s, inet_ntoa(*addr), port,
137                sockerrno, tr_strerror(sockerrno) );
138        tr_netClose( s );
139        s = -1;
140    }
141
142    return s;
143}
144 
145int
146tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
147{
148    return tr_netOpen( addr, port, SOCK_STREAM, priority );
149}
150
151static int
152tr_netBind( int port, int type )
153{
154    int s;
155    struct sockaddr_in sock;
156#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
157    int optval;
158#endif
159
160    if( ( s = createSocket( type, 1 ) ) < 0 )
161    {
162        tr_err( _( "Couldn't create socket of type %d" ), type );
163        return -1;
164    }
165
166#ifdef SO_REUSEADDR
167    optval = 1;
168    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
169#endif
170
171    memset( &sock, 0, sizeof( sock ) );
172    sock.sin_family      = AF_INET;
173    sock.sin_addr.s_addr = INADDR_ANY;
174    sock.sin_port        = htons( port );
175
176    if( bind( s, (struct sockaddr *) &sock,
177               sizeof( struct sockaddr_in ) ) )
178    {
179        tr_err( _( "Couldn't bind port %d: %s" ), port, tr_strerror(sockerrno) );
180        tr_netClose( s );
181        return -1;
182    }
183     
184    tr_inf( _( "Bound socket %d to port %d" ), s, port );
185    return s;
186}
187
188int
189tr_netBindTCP( int port )
190{
191    return tr_netBind( port, SOCK_STREAM );
192}
193
194int
195tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
196{
197    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
198}
199
200int
201tr_netSend( int s, const void * buf, int size )
202{
203    const int ret = send( s, buf, size, 0 );
204    if( ret >= 0 )
205        return ret;
206
207    if( sockerrno == ENOTCONN || sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
208        return TR_NET_BLOCK;
209
210    return TR_NET_CLOSE;
211}
212
213int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
214{
215    socklen_t len;
216    int       ret;
217
218    len = ( NULL == addr ? 0 : sizeof( *addr ) );
219    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
220    if( ret < 0 )
221    {
222        if( sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
223        {
224            ret = TR_NET_BLOCK;
225        }
226        else
227        {
228            ret = TR_NET_CLOSE;
229        }
230    }
231    if( !ret )
232    {
233        ret = TR_NET_CLOSE;
234    }
235
236    return ret;
237}
238
239void
240tr_netClose( int s )
241{
242    tr_fdSocketClose( s );
243}
244
245void
246tr_netNtop( const struct in_addr * addr, char * buf, int len )
247{
248    const uint8_t * cast;
249
250    cast = (const uint8_t *)addr;
251    snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
252              cast[0], cast[1], cast[2], cast[3] );
253}
Note: See TracBrowser for help on using the repository browser.