~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-updates

« back to all changes in this revision

Viewing changes to libtransmission/net.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: net.c 6594 2008-08-20 02:20:56Z charles $
 
2
 * $Id: net.c 6944 2008-10-22 13:57:30Z charles $
3
3
 *
4
4
 * Copyright (c) 2005-2008 Transmission authors and contributors
5
5
 *
30
30
#include <sys/types.h>
31
31
 
32
32
#ifdef WIN32
33
 
#include <winsock2.h> /* inet_addr */
 
33
 #include <winsock2.h> /* inet_addr */
34
34
#else
35
 
#include <arpa/inet.h> /* inet_addr */
36
 
#include <netdb.h>
37
 
#include <fcntl.h>
 
35
 #include <arpa/inet.h> /* inet_addr */
 
36
 #include <netdb.h>
 
37
 #include <fcntl.h>
38
38
#endif
39
39
 
40
40
#include <evutil.h>
43
43
#include "fdlimit.h"
44
44
#include "natpmp.h"
45
45
#include "net.h"
 
46
#include "peer-io.h"
46
47
#include "platform.h"
47
48
#include "utils.h"
48
49
 
51
52
tr_netInit( void )
52
53
{
53
54
    static int initialized = FALSE;
 
55
 
54
56
    if( !initialized )
55
57
    {
56
58
#ifdef WIN32
57
59
        WSADATA wsaData;
58
 
        WSAStartup(MAKEWORD(2,2), &wsaData);
 
60
        WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
59
61
#endif
60
62
        initialized = TRUE;
61
63
    }
63
65
 
64
66
/***********************************************************************
65
67
 * DNS resolution
66
 
 * 
 
68
 *
67
69
 * Synchronous "resolution": only works with character strings
68
70
 * representing numbers expressed in the Internet standard `.' notation.
69
71
 * Returns a non-zero value if an error occurs.
70
72
 **********************************************************************/
71
 
int tr_netResolve( const char * address, struct in_addr * addr )
 
73
int
 
74
tr_netResolve( const char *     address,
 
75
               struct in_addr * addr )
72
76
{
73
77
    addr->s_addr = inet_addr( address );
74
 
    return ( addr->s_addr == 0xFFFFFFFF );
 
78
    return addr->s_addr == 0xFFFFFFFF;
75
79
}
76
80
 
77
 
 
78
81
/***********************************************************************
79
82
 * TCP sockets
80
83
 **********************************************************************/
81
84
 
82
85
int
83
 
tr_netSetTOS( int s, int tos )
 
86
tr_netSetTOS( int s,
 
87
              int tos )
84
88
{
85
89
#ifdef IP_TOS
86
90
    return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
94
98
{
95
99
    if( fd >= 0 )
96
100
    {
97
 
#if defined(__BEOS__)
 
101
#if defined( __BEOS__ )
98
102
        int flags = 1;
99
103
        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
100
 
                        &flags, sizeof( int ) ) < 0 )
 
104
                       &flags, sizeof( int ) ) < 0 )
101
105
#else
102
106
        if( evutil_make_socket_nonblocking( fd ) )
103
107
#endif
104
108
        {
105
109
            tr_err( _( "Couldn't create socket: %s" ),
106
 
                    tr_strerror( sockerrno ) );
 
110
                   tr_strerror( sockerrno ) );
107
111
            tr_netClose( fd );
108
112
            fd = -1;
109
113
        }
113
117
}
114
118
 
115
119
static int
116
 
createSocket( int type, int priority )
 
120
createSocket( int type )
117
121
{
118
 
    int fd;
119
 
 
120
 
    fd = tr_fdSocketCreate( type, priority );
121
 
 
122
 
    if( fd >= 0 )
123
 
        fd = makeSocketNonBlocking( fd );
124
 
 
125
 
#if 0
126
 
    if( fd >= 0 ) {
127
 
        const int buffsize = 1500*3; /* 3x MTU for most ethernet/wireless */
128
 
        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) );
129
 
    }
130
 
#endif
131
 
 
132
 
    return fd;
 
122
    return makeSocketNonBlocking( tr_fdSocketCreate( type ) );
133
123
}
134
124
 
135
125
int
136
 
tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
 
126
tr_netOpenTCP( const struct in_addr * addr,
 
127
               tr_port_t              port )
137
128
{
138
 
    int s;
 
129
    int                s;
139
130
    struct sockaddr_in sock;
140
 
    const int type = SOCK_STREAM;
 
131
    const int          type = SOCK_STREAM;
141
132
 
142
 
    if( ( s = createSocket( type, priority ) ) < 0 )
143
 
    {
 
133
    if( ( s = createSocket( type ) ) < 0 )
144
134
        return -1;
145
 
    }
146
135
 
147
136
    memset( &sock, 0, sizeof( sock ) );
148
137
    sock.sin_family      = AF_INET;
150
139
    sock.sin_port        = port;
151
140
 
152
141
    if( ( connect( s, (struct sockaddr *) &sock,
153
 
                   sizeof( struct sockaddr_in ) ) < 0 )
 
142
                  sizeof( struct sockaddr_in ) ) < 0 )
154
143
#ifdef WIN32
155
 
        && ( sockerrno != WSAEWOULDBLOCK )
 
144
      && ( sockerrno != WSAEWOULDBLOCK )
156
145
#endif
157
 
        && ( sockerrno != EINPROGRESS ) )
 
146
      && ( sockerrno != EINPROGRESS ) )
158
147
    {
159
 
        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
160
 
                s, inet_ntoa(*addr), port,
161
 
                sockerrno, tr_strerror(sockerrno) );
 
148
        tr_err( _(
 
149
                   "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
 
150
               s, inet_ntoa( *addr ), port,
 
151
               sockerrno, tr_strerror( sockerrno ) );
162
152
        tr_netClose( s );
163
153
        s = -1;
164
154
    }
165
155
 
 
156
    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
 
157
               s, tr_peerIoAddrStr( addr, port ) );
 
158
 
166
159
    return s;
167
160
}
168
161
 
169
162
int
170
163
tr_netBindTCP( int port )
171
164
{
172
 
    int s;
 
165
    int                s;
173
166
    struct sockaddr_in sock;
174
 
    const int type = SOCK_STREAM;
 
167
    const int          type = SOCK_STREAM;
 
168
 
175
169
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
176
 
    int optval;
 
170
    int                optval;
177
171
#endif
178
172
 
179
 
    if( ( s = createSocket( type, 1 ) ) < 0 )
 
173
    if( ( s = createSocket( type ) ) < 0 )
180
174
        return -1;
181
175
 
182
176
#ifdef SO_REUSEADDR
190
184
    sock.sin_port        = htons( port );
191
185
 
192
186
    if( bind( s, (struct sockaddr *) &sock,
193
 
               sizeof( struct sockaddr_in ) ) )
 
187
             sizeof( struct sockaddr_in ) ) )
194
188
    {
195
 
        tr_err( _( "Couldn't bind port %d: %s" ), port, tr_strerror(sockerrno) );
 
189
        tr_err( _( "Couldn't bind port %d: %s" ), port,
 
190
               tr_strerror( sockerrno ) );
196
191
        tr_netClose( s );
197
192
        return -1;
198
193
    }
199
 
     
 
194
 
200
195
    tr_dbg(  "Bound socket %d to port %d", s, port );
201
196
    return s;
202
197
}
203
198
 
204
199
int
205
 
tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
 
200
tr_netAccept( int              b,
 
201
              struct in_addr * addr,
 
202
              tr_port_t *      port )
206
203
{
207
204
    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
208
205
}
214
211
}
215
212
 
216
213
void
217
 
tr_netNtop( const struct in_addr * addr, char * buf, int len )
 
214
tr_netNtop( const struct in_addr * addr,
 
215
            char *                 buf,
 
216
            int                    len )
218
217
{
219
218
    const uint8_t * cast;
220
219
 
221
220
    cast = (const uint8_t *)addr;
222
221
    tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",
223
 
                cast[0], cast[1], cast[2], cast[3] );
 
222
                 cast[0], cast[1], cast[2], cast[3] );
224
223
}
 
224