source: trunk/libtransmission/net.c @ 3552

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

fix OpenBSD build error.

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