1 | /****************************************************************************** |
---|
2 | * $Id: net.c 6396 2008-07-24 20:58:58Z 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 | |
---|
50 | void |
---|
51 | tr_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 | **********************************************************************/ |
---|
71 | int 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 sockets |
---|
80 | **********************************************************************/ |
---|
81 | |
---|
82 | int |
---|
83 | tr_netSetTOS( int s, int tos ) |
---|
84 | { |
---|
85 | #ifdef IP_TOS |
---|
86 | return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) ); |
---|
87 | #else |
---|
88 | return 0; |
---|
89 | #endif |
---|
90 | } |
---|
91 | |
---|
92 | static int |
---|
93 | makeSocketNonBlocking( int fd ) |
---|
94 | { |
---|
95 | if( fd >= 0 ) |
---|
96 | { |
---|
97 | #if defined(__BEOS__) |
---|
98 | int flags = 1; |
---|
99 | if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK, |
---|
100 | &flags, sizeof( int ) ) < 0 ) |
---|
101 | #else |
---|
102 | if( evutil_make_socket_nonblocking( fd ) ) |
---|
103 | #endif |
---|
104 | { |
---|
105 | tr_err( _( "Couldn't create socket: %s" ), |
---|
106 | tr_strerror( sockerrno ) ); |
---|
107 | tr_netClose( fd ); |
---|
108 | fd = -1; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | return fd; |
---|
113 | } |
---|
114 | |
---|
115 | static int |
---|
116 | createSocket( int type, int priority ) |
---|
117 | { |
---|
118 | int fd; |
---|
119 | |
---|
120 | fd = tr_fdSocketCreate( type, priority ); |
---|
121 | |
---|
122 | if( fd >= 0 ) |
---|
123 | fd = makeSocketNonBlocking( fd ); |
---|
124 | |
---|
125 | if( fd >= 0 ) { |
---|
126 | const int buffsize = 1500*2; /* 2x MTU for most ethernet/wireless */ |
---|
127 | setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) ); |
---|
128 | } |
---|
129 | |
---|
130 | return fd; |
---|
131 | } |
---|
132 | |
---|
133 | int |
---|
134 | tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority ) |
---|
135 | { |
---|
136 | int s; |
---|
137 | struct sockaddr_in sock; |
---|
138 | const int type = SOCK_STREAM; |
---|
139 | |
---|
140 | if( ( s = createSocket( type, priority ) ) < 0 ) |
---|
141 | { |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | memset( &sock, 0, sizeof( sock ) ); |
---|
146 | sock.sin_family = AF_INET; |
---|
147 | sock.sin_addr.s_addr = addr->s_addr; |
---|
148 | sock.sin_port = port; |
---|
149 | |
---|
150 | if( ( connect( s, (struct sockaddr *) &sock, |
---|
151 | sizeof( struct sockaddr_in ) ) < 0 ) |
---|
152 | #ifdef WIN32 |
---|
153 | && ( sockerrno != WSAEWOULDBLOCK ) |
---|
154 | #endif |
---|
155 | && ( sockerrno != EINPROGRESS ) ) |
---|
156 | { |
---|
157 | tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ), |
---|
158 | s, inet_ntoa(*addr), port, |
---|
159 | sockerrno, tr_strerror(sockerrno) ); |
---|
160 | tr_netClose( s ); |
---|
161 | s = -1; |
---|
162 | } |
---|
163 | |
---|
164 | return s; |
---|
165 | } |
---|
166 | |
---|
167 | int |
---|
168 | tr_netBindTCP( int port ) |
---|
169 | { |
---|
170 | int s; |
---|
171 | struct sockaddr_in sock; |
---|
172 | const int type = SOCK_STREAM; |
---|
173 | #if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) |
---|
174 | int optval; |
---|
175 | #endif |
---|
176 | |
---|
177 | if( ( s = createSocket( type, 1 ) ) < 0 ) |
---|
178 | return -1; |
---|
179 | |
---|
180 | #ifdef SO_REUSEADDR |
---|
181 | optval = 1; |
---|
182 | setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) ); |
---|
183 | #endif |
---|
184 | |
---|
185 | memset( &sock, 0, sizeof( sock ) ); |
---|
186 | sock.sin_family = AF_INET; |
---|
187 | sock.sin_addr.s_addr = INADDR_ANY; |
---|
188 | sock.sin_port = htons( port ); |
---|
189 | |
---|
190 | if( bind( s, (struct sockaddr *) &sock, |
---|
191 | sizeof( struct sockaddr_in ) ) ) |
---|
192 | { |
---|
193 | tr_err( _( "Couldn't bind port %d: %s" ), port, tr_strerror(sockerrno) ); |
---|
194 | tr_netClose( s ); |
---|
195 | return -1; |
---|
196 | } |
---|
197 | |
---|
198 | tr_dbg( "Bound socket %d to port %d", s, port ); |
---|
199 | return s; |
---|
200 | } |
---|
201 | |
---|
202 | int |
---|
203 | tr_netAccept( int b, struct in_addr * addr, tr_port_t * port ) |
---|
204 | { |
---|
205 | return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) ); |
---|
206 | } |
---|
207 | |
---|
208 | void |
---|
209 | tr_netClose( int s ) |
---|
210 | { |
---|
211 | tr_fdSocketClose( s ); |
---|
212 | } |
---|
213 | |
---|
214 | void |
---|
215 | tr_netNtop( const struct in_addr * addr, char * buf, int len ) |
---|
216 | { |
---|
217 | const uint8_t * cast; |
---|
218 | |
---|
219 | cast = (const uint8_t *)addr; |
---|
220 | tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu", |
---|
221 | cast[0], cast[1], cast[2], cast[3] ); |
---|
222 | } |
---|