source: trunk/libtransmission/net.c @ 4029

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

use PMP_MCAST_ADDR, instead of a hardcoded address, in net.c

  • Property svn:keywords set to Date Rev Author Id
File size: 7.6 KB
Line 
1/******************************************************************************
2 * $Id: net.c 4029 2007-12-01 05:11:30Z charles $
3 *
4 * Copyright (c) 2005-2006 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#include <arpa/inet.h> /* inet_addr */
33
34#ifndef WIN32
35#include <netdb.h>
36#include <fcntl.h>
37#endif
38
39#include <evutil.h>
40
41#include "transmission.h"
42#include "fdlimit.h"
43#include "natpmp.h"
44#include "net.h"
45#include "platform.h"
46#include "utils.h"
47
48
49void
50tr_netInit( void )
51{
52    static int initialized = FALSE;
53    if( !initialized )
54    {
55#ifdef WIN32
56        WSADATA wsaData;
57        WSAStartup(MAKEWORD(2,2), &wsaData);
58#endif
59        initialized = TRUE;
60    }
61}
62
63/***********************************************************************
64 * DNS resolution
65 *
66 * Synchronous "resolution": only works with character strings
67 * representing numbers expressed in the Internet standard `.' notation.
68 * Returns a non-zero value if an error occurs.
69 **********************************************************************/
70int tr_netResolve( const char * address, struct in_addr * addr )
71{
72    addr->s_addr = inet_addr( address );
73    return ( addr->s_addr == 0xFFFFFFFF );
74}
75
76
77/***********************************************************************
78 * TCP/UDP sockets
79 **********************************************************************/
80
81static int
82makeSocketNonBlocking( int fd )
83{
84    if( fd >= 0 )
85    {
86#if defined(__BEOS__)
87        int flags = 1;
88        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
89                        &flags, sizeof( int ) ) < 0 )
90#else
91        if( evutil_make_socket_nonblocking( fd ) )
92#endif
93        {
94            tr_err( "Couldn't set socket to non-blocking mode (%s)",
95                    strerror( sockerrno ) );
96            tr_netClose( fd );
97            fd = -1;
98        }
99    }
100
101    return fd;
102}
103
104static int
105createSocket( int type, int priority )
106{
107    return makeSocketNonBlocking( tr_fdSocketCreate( type, priority ) );
108}
109
110static int
111tr_netOpen( const struct in_addr * addr, tr_port_t port,
112            int type, int priority )
113{
114    int s;
115    struct sockaddr_in sock;
116
117    if( ( s = createSocket( type, priority ) ) < 0 )
118    {
119        return -1;
120    }
121
122    memset( &sock, 0, sizeof( sock ) );
123    sock.sin_family      = AF_INET;
124    sock.sin_addr.s_addr = addr->s_addr;
125    sock.sin_port        = port;
126
127    if( ( connect( s, (struct sockaddr *) &sock,
128                   sizeof( struct sockaddr_in ) ) < 0 )
129#ifdef WIN32
130        && ( sockerrno != WSAEWOULDBLOCK )
131#endif
132        && ( sockerrno != EINPROGRESS ) )
133    {
134        tr_err( "Couldn't connect socket (%s)", strerror( sockerrno ) );
135        tr_netClose( s );
136        return -1;
137    }
138
139    return s;
140}
141 
142int
143tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
144{
145    return tr_netOpen( addr, port, SOCK_STREAM, priority );
146}
147
148int
149tr_netOpenUDP( const struct in_addr * addr, tr_port_t port, int priority )
150{
151    return tr_netOpen( addr, port, SOCK_DGRAM, priority );
152}
153
154#ifdef IP_ADD_MEMBERSHIP
155int tr_netMcastOpen( int port, const struct in_addr * addr )
156{
157    int fd;
158    struct ip_mreq req;
159
160    fd = tr_netBindUDP( port );
161    if( 0 > fd )
162    {
163        return -1;
164    }
165
166    memset( &req, 0, sizeof( req ) );
167    req.imr_multiaddr.s_addr = addr->s_addr;
168    req.imr_interface.s_addr = htonl( INADDR_ANY );
169    if( setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&req, sizeof ( req ) ) )
170    {
171        tr_err( "Couldn't join multicast group (%s)", strerror( sockerrno ) );
172        tr_netClose( fd );
173        return -1;
174    }
175
176    return fd;
177}
178#else /* IP_ADD_MEMBERSHIP */
179int tr_netMcastOpen( int port UNUSED, const struct in_addr * addr UNUSED )
180{
181    return -1;
182}
183#endif /* IP_ADD_MEMBERSHIP */
184
185static int
186tr_netBind( int port, int type )
187{
188    int s;
189    struct sockaddr_in sock;
190#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
191    int optval;
192#endif
193
194    if( ( s = createSocket( type, 1 ) ) < 0 )
195    {
196        return -1;
197    }
198
199#ifdef SO_REUSEADDR
200    optval = 1;
201    setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) );
202#endif
203
204#ifdef SO_REUSEPORT
205    if( SOCK_DGRAM == type )
206    {
207        optval = 1;
208        setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof( optval ) );
209    }
210#endif
211
212    memset( &sock, 0, sizeof( sock ) );
213    sock.sin_family      = AF_INET;
214
215    /* Leopard closes a SO_REUSEADDR + INADDR_ANY hole, so we can't
216     * use INADDR_ANY when binding for nat-pmp. For details, refer to
217     * http://www.uwsg.indiana.edu/hypermail/linux/kernel/9902.1/0828.html .
218     * This can probably be done cleaner, but since we're only using SOCK_DGRAM
219     * for nat-pmp, this quick fix should work. */
220    if ( SOCK_DGRAM == type )
221        sock.sin_addr.s_addr = inet_addr( PMP_MCAST_ADDR );
222    else
223        sock.sin_addr.s_addr = INADDR_ANY;
224
225    sock.sin_port        = htons( port );
226
227    if( bind( s, (struct sockaddr *) &sock,
228               sizeof( struct sockaddr_in ) ) )
229    {
230        tr_err( "Couldn't bind port %d: %s", port, strerror(sockerrno) );
231        tr_netClose( s );
232        return -1;
233    }
234
235    return s;
236}
237
238int
239tr_netBindTCP( int port )
240{
241    return tr_netBind( port, SOCK_STREAM );
242}
243
244int
245tr_netBindUDP( int port )
246{
247    return tr_netBind( port, SOCK_DGRAM );
248}
249
250
251int
252tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
253{
254    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
255}
256
257int
258tr_netSend( int s, const void * buf, int size )
259{
260    const int ret = send( s, buf, size, 0 );
261    if( ret >= 0 )
262        return ret;
263
264    if( sockerrno == ENOTCONN || sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
265        return TR_NET_BLOCK;
266
267    return TR_NET_CLOSE;
268}
269
270int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
271{
272    socklen_t len;
273    int       ret;
274
275    len = ( NULL == addr ? 0 : sizeof( *addr ) );
276    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
277    if( ret < 0 )
278    {
279        if( sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
280        {
281            ret = TR_NET_BLOCK;
282        }
283        else
284        {
285            ret = TR_NET_CLOSE;
286        }
287    }
288    if( !ret )
289    {
290        ret = TR_NET_CLOSE;
291    }
292
293    return ret;
294}
295
296void
297tr_netClose( int s )
298{
299    tr_fdSocketClose( s );
300}
301
302void
303tr_netNtop( const struct in_addr * addr, char * buf, int len )
304{
305    const uint8_t * cast;
306
307    cast = (const uint8_t *)addr;
308    snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
309              cast[0], cast[1], cast[2], cast[3] );
310}
Note: See TracBrowser for help on using the repository browser.