source: trunk/libtransmission/net.c @ 3105

Last change on this file since 3105 was 3105, checked in by livings124, 14 years ago

merge encryption branch to trunk (xcode project is still out of date)

  • Property svn:keywords set to Date Rev Author Id
File size: 7.2 KB
Line 
1/******************************************************************************
2 * $Id: net.c 3105 2007-09-20 16:32:01Z livings124 $
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#ifndef WIN32
33#include <netdb.h>
34#include <fcntl.h>
35#endif
36
37#include "transmission.h"
38#include "fdlimit.h"
39#include "net.h"
40#include "platform.h"
41#include "utils.h"
42
43
44void
45tr_netInit( void )
46{
47    static int initialized = FALSE;
48    if( !initialized )
49    {
50#ifdef WIN32
51        WSADATA wsaData;
52        WSAStartup(MAKEWORD(2,2), &wsaData);
53#endif
54        initialized = TRUE;
55    }
56}
57
58/***********************************************************************
59 * DNS resolution
60 *
61 * Synchronous "resolution": only works with character strings
62 * representing numbers expressed in the Internet standard `.' notation.
63 * Returns a non-zero value if an error occurs.
64 **********************************************************************/
65int tr_netResolve( const char * address, struct in_addr * addr )
66{
67    addr->s_addr = inet_addr( address );
68    return ( addr->s_addr == 0xFFFFFFFF );
69}
70
71
72/***********************************************************************
73 * TCP/UDP sockets
74 **********************************************************************/
75
76static int makeSocketNonBlocking( int s )
77{
78#ifdef WIN32
79    unsigned long flags = 1;
80    if( ioctlsocket( s, FIONBIO, &flags) == SOCKET_ERROR )
81#elif defined(__BEOS__)
82    int flags = 1;
83    if( setsockopt( s, SOL_SOCKET, SO_NONBLOCK,
84                    &flags, sizeof( int ) ) < 0 )
85#else
86    int flags = 1;
87    if( ( flags = fcntl( s, F_GETFL, 0 ) ) < 0 ||
88        fcntl( s, F_SETFL, flags | O_NONBLOCK ) < 0 )
89#endif
90    {
91        tr_err( "Couldn't set socket to non-blocking mode (%s)",
92                strerror( sockerrno ) );
93        tr_netClose( s );
94        return -1;
95    }
96
97    return s;
98}
99
100static int createSocket( int type, int priority )
101{
102    int s;
103    if( ( s = tr_fdSocketCreate( type, priority ) ) < 0 )
104    {
105        return -1;
106    }
107    return makeSocketNonBlocking( s );
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    sock.sin_addr.s_addr = INADDR_ANY;
215    sock.sin_port        = htons( port );
216
217    if( bind( s, (struct sockaddr *) &sock,
218               sizeof( struct sockaddr_in ) ) )
219    {
220        tr_err( "Couldn't bind port %d", port );
221        tr_netClose( s );
222        return -1;
223    }
224
225    return s;
226}
227
228int
229tr_netBindTCP( int port )
230{
231    return tr_netBind( port, SOCK_STREAM );
232}
233
234int
235tr_netBindUDP( int port )
236{
237    return tr_netBind( port, SOCK_DGRAM );
238}
239
240
241int tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
242{
243    int s;
244    if( ( s = tr_fdSocketAccept( b, addr, port ) ) < 0 )
245    {
246        return -1;
247    }
248    return makeSocketNonBlocking( s );
249}
250
251int
252tr_netSend( int s, const void * buf, int size )
253{
254    const int ret = send( s, buf, size, 0 );
255    if( ret >= 0 )
256        return ret;
257
258    if( sockerrno == ENOTCONN || sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
259        return TR_NET_BLOCK;
260
261    return TR_NET_CLOSE;
262}
263
264int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
265{
266    socklen_t len;
267    int       ret;
268
269    len = ( NULL == addr ? 0 : sizeof( *addr ) );
270    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
271    if( ret < 0 )
272    {
273        if( sockerrno == EAGAIN || sockerrno == EWOULDBLOCK )
274        {
275            ret = TR_NET_BLOCK;
276        }
277        else
278        {
279            ret = TR_NET_CLOSE;
280        }
281    }
282    if( !ret )
283    {
284        ret = TR_NET_CLOSE;
285    }
286
287    return ret;
288}
289
290void
291tr_netClose( int s )
292{
293    tr_fdSocketClose( s );
294}
295
296void
297tr_netNtop( const struct in_addr * addr, char * buf, int len )
298{
299    const uint8_t * cast;
300
301    cast = (const uint8_t *)addr;
302    snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
303              cast[0], cast[1], cast[2], cast[3] );
304}
Note: See TracBrowser for help on using the repository browser.