1 | /****************************************************************************** |
---|
2 | * |
---|
3 | * $Id: net.c 11971 2011-02-18 16:01:52Z jch $ |
---|
4 | * |
---|
5 | * Copyright (c) Transmission authors and contributors |
---|
6 | * |
---|
7 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
8 | * copy of this software and associated documentation files (the "Software"), |
---|
9 | * to deal in the Software without restriction, including without limitation |
---|
10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
11 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
12 | * Software is furnished to do so, subject to the following conditions: |
---|
13 | * |
---|
14 | * The above copyright notice and this permission notice shall be included in |
---|
15 | * all copies or substantial portions of the Software. |
---|
16 | * |
---|
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
23 | * DEALINGS IN THE SOFTWARE. |
---|
24 | *****************************************************************************/ |
---|
25 | |
---|
26 | #include <errno.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <string.h> |
---|
30 | #include <assert.h> |
---|
31 | |
---|
32 | #include <sys/types.h> |
---|
33 | |
---|
34 | #ifdef WIN32 |
---|
35 | #define _WIN32_WINNT 0x0501 |
---|
36 | #include <ws2tcpip.h> |
---|
37 | #else |
---|
38 | #include <sys/socket.h> |
---|
39 | #include <netinet/in.h> |
---|
40 | #include <netinet/tcp.h> |
---|
41 | #include <arpa/inet.h> /* inet_addr */ |
---|
42 | #include <netdb.h> |
---|
43 | #include <fcntl.h> |
---|
44 | #endif |
---|
45 | #include <unistd.h> |
---|
46 | |
---|
47 | #include <event2/util.h> |
---|
48 | #include <libutp/utp.h> |
---|
49 | |
---|
50 | #include "transmission.h" |
---|
51 | #include "fdlimit.h" |
---|
52 | #include "natpmp.h" |
---|
53 | #include "net.h" |
---|
54 | #include "peer-io.h" |
---|
55 | #include "platform.h" |
---|
56 | #include "session.h" |
---|
57 | #include "tr-utp.h" |
---|
58 | #include "utils.h" |
---|
59 | |
---|
60 | #ifndef IN_MULTICAST |
---|
61 | #define IN_MULTICAST( a ) ( ( ( a ) & 0xf0000000 ) == 0xe0000000 ) |
---|
62 | #endif |
---|
63 | |
---|
64 | const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; |
---|
65 | const tr_address tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; |
---|
66 | |
---|
67 | #ifdef WIN32 |
---|
68 | const char * |
---|
69 | inet_ntop( int af, const void * src, char * dst, socklen_t cnt ) |
---|
70 | { |
---|
71 | if (af == AF_INET) |
---|
72 | { |
---|
73 | struct sockaddr_in in; |
---|
74 | memset( &in, 0, sizeof( in ) ); |
---|
75 | in.sin_family = AF_INET; |
---|
76 | memcpy( &in.sin_addr, src, sizeof( struct in_addr ) ); |
---|
77 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), |
---|
78 | dst, cnt, NULL, 0, NI_NUMERICHOST); |
---|
79 | return dst; |
---|
80 | } |
---|
81 | else if (af == AF_INET6) |
---|
82 | { |
---|
83 | struct sockaddr_in6 in; |
---|
84 | memset( &in, 0, sizeof( in ) ); |
---|
85 | in.sin6_family = AF_INET6; |
---|
86 | memcpy( &in.sin6_addr, src, sizeof( struct in_addr6 ) ); |
---|
87 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), |
---|
88 | dst, cnt, NULL, 0, NI_NUMERICHOST); |
---|
89 | return dst; |
---|
90 | } |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | inet_pton( int af, const char * src, void * dst ) |
---|
96 | { |
---|
97 | struct addrinfo hints, *res, *ressave; |
---|
98 | struct sockaddr_in * s4; |
---|
99 | struct sockaddr_in6 * s6; |
---|
100 | |
---|
101 | memset( &hints, 0, sizeof( struct addrinfo )); |
---|
102 | hints.ai_family = af; |
---|
103 | hints.ai_flags = AI_NUMERICHOST; |
---|
104 | |
---|
105 | if( getaddrinfo( src, NULL, &hints, &res ) ) { |
---|
106 | if( WSAGetLastError() == WSAHOST_NOT_FOUND ) |
---|
107 | return 0; |
---|
108 | else { |
---|
109 | errno = EAFNOSUPPORT; |
---|
110 | return -1; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | ressave = res; |
---|
115 | while( res ) { |
---|
116 | switch (res->ai_family) { |
---|
117 | case AF_INET: |
---|
118 | s4 = (struct sockaddr_in *) res->ai_addr; |
---|
119 | memcpy( dst, &s4->sin_addr, sizeof( struct in_addr ) ); |
---|
120 | break; |
---|
121 | case AF_INET6: |
---|
122 | s6 = (struct sockaddr_in6 *) res->ai_addr; |
---|
123 | memcpy( dst, &s6->sin6_addr, sizeof( struct in6_addr ) ); |
---|
124 | break; |
---|
125 | default: /* AF_UNSPEC, AF_NETBIOS */ |
---|
126 | break; |
---|
127 | } |
---|
128 | res = res->ai_next; |
---|
129 | } |
---|
130 | |
---|
131 | freeaddrinfo(ressave); |
---|
132 | return 1; |
---|
133 | } |
---|
134 | #endif |
---|
135 | |
---|
136 | void |
---|
137 | tr_netInit( void ) |
---|
138 | { |
---|
139 | static int initialized = FALSE; |
---|
140 | |
---|
141 | if( !initialized ) |
---|
142 | { |
---|
143 | #ifdef WIN32 |
---|
144 | WSADATA wsaData; |
---|
145 | WSAStartup( MAKEWORD( 2, 2 ), &wsaData ); |
---|
146 | #endif |
---|
147 | initialized = TRUE; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | char * |
---|
152 | tr_net_strerror( char * buf, size_t buflen, int err ) |
---|
153 | { |
---|
154 | *buf = '\0'; |
---|
155 | #ifdef WIN32 |
---|
156 | FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL ); |
---|
157 | #else |
---|
158 | tr_strlcpy( buf, tr_strerror( err ), buflen ); |
---|
159 | #endif |
---|
160 | return buf; |
---|
161 | } |
---|
162 | |
---|
163 | const char * |
---|
164 | tr_ntop( const tr_address * src, char * dst, int size ) |
---|
165 | { |
---|
166 | assert( tr_isAddress( src ) ); |
---|
167 | |
---|
168 | if( src->type == TR_AF_INET ) |
---|
169 | return inet_ntop( AF_INET, &src->addr, dst, size ); |
---|
170 | else |
---|
171 | return inet_ntop( AF_INET6, &src->addr, dst, size ); |
---|
172 | } |
---|
173 | |
---|
174 | /* |
---|
175 | * Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer. |
---|
176 | * This function is suitable to be called from libTransmission's networking code, |
---|
177 | * which is single-threaded. |
---|
178 | */ |
---|
179 | const char * |
---|
180 | tr_ntop_non_ts( const tr_address * src ) |
---|
181 | { |
---|
182 | static char buf[INET6_ADDRSTRLEN]; |
---|
183 | return tr_ntop( src, buf, sizeof( buf ) ); |
---|
184 | } |
---|
185 | |
---|
186 | tr_address * |
---|
187 | tr_pton( const char * src, tr_address * dst ) |
---|
188 | { |
---|
189 | int retval = inet_pton( AF_INET, src, &dst->addr ); |
---|
190 | assert( dst ); |
---|
191 | if( retval < 0 ) |
---|
192 | return NULL; |
---|
193 | else if( retval == 0 ) |
---|
194 | retval = inet_pton( AF_INET6, src, &dst->addr ); |
---|
195 | else |
---|
196 | { |
---|
197 | dst->type = TR_AF_INET; |
---|
198 | return dst; |
---|
199 | } |
---|
200 | |
---|
201 | if( retval < 1 ) |
---|
202 | return NULL; |
---|
203 | dst->type = TR_AF_INET6; |
---|
204 | return dst; |
---|
205 | } |
---|
206 | |
---|
207 | /* |
---|
208 | * Compare two tr_address structures. |
---|
209 | * Returns: |
---|
210 | * <0 if a < b |
---|
211 | * >0 if a > b |
---|
212 | * 0 if a == b |
---|
213 | */ |
---|
214 | int |
---|
215 | tr_compareAddresses( const tr_address * a, const tr_address * b) |
---|
216 | { |
---|
217 | static const int sizes[2] = { sizeof(struct in_addr), sizeof(struct in6_addr) }; |
---|
218 | |
---|
219 | /* IPv6 addresses are always "greater than" IPv4 */ |
---|
220 | if( a->type != b->type ) |
---|
221 | return a->type == TR_AF_INET ? 1 : -1; |
---|
222 | |
---|
223 | return memcmp( &a->addr, &b->addr, sizes[a->type] ); |
---|
224 | } |
---|
225 | |
---|
226 | /*********************************************************************** |
---|
227 | * TCP sockets |
---|
228 | **********************************************************************/ |
---|
229 | |
---|
230 | int |
---|
231 | tr_netSetTOS( int s, int tos ) |
---|
232 | { |
---|
233 | #ifdef IP_TOS |
---|
234 | return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) ); |
---|
235 | #else |
---|
236 | return 0; |
---|
237 | #endif |
---|
238 | } |
---|
239 | |
---|
240 | int |
---|
241 | tr_netSetCongestionControl( int s UNUSED, const char *algorithm UNUSED ) |
---|
242 | { |
---|
243 | #ifdef TCP_CONGESTION |
---|
244 | return setsockopt( s, IPPROTO_TCP, TCP_CONGESTION, |
---|
245 | algorithm, strlen(algorithm) + 1 ); |
---|
246 | #else |
---|
247 | errno = ENOSYS; |
---|
248 | return -1; |
---|
249 | #endif |
---|
250 | } |
---|
251 | |
---|
252 | static socklen_t |
---|
253 | setup_sockaddr( const tr_address * addr, |
---|
254 | tr_port port, |
---|
255 | struct sockaddr_storage * sockaddr) |
---|
256 | { |
---|
257 | assert( tr_isAddress( addr ) ); |
---|
258 | |
---|
259 | if( addr->type == TR_AF_INET ) |
---|
260 | { |
---|
261 | struct sockaddr_in sock4; |
---|
262 | memset( &sock4, 0, sizeof( sock4 ) ); |
---|
263 | sock4.sin_family = AF_INET; |
---|
264 | sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; |
---|
265 | sock4.sin_port = port; |
---|
266 | memcpy( sockaddr, &sock4, sizeof( sock4 ) ); |
---|
267 | return sizeof( struct sockaddr_in ); |
---|
268 | } |
---|
269 | else |
---|
270 | { |
---|
271 | struct sockaddr_in6 sock6; |
---|
272 | memset( &sock6, 0, sizeof( sock6 ) ); |
---|
273 | sock6.sin6_family = AF_INET6; |
---|
274 | sock6.sin6_port = port; |
---|
275 | sock6.sin6_flowinfo = 0; |
---|
276 | sock6.sin6_addr = addr->addr.addr6; |
---|
277 | memcpy( sockaddr, &sock6, sizeof( sock6 ) ); |
---|
278 | return sizeof( struct sockaddr_in6 ); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | int |
---|
283 | tr_netOpenPeerSocket( tr_session * session, |
---|
284 | const tr_address * addr, |
---|
285 | tr_port port, |
---|
286 | tr_bool clientIsSeed ) |
---|
287 | { |
---|
288 | static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 }; |
---|
289 | int s; |
---|
290 | struct sockaddr_storage sock; |
---|
291 | socklen_t addrlen; |
---|
292 | const tr_address * source_addr; |
---|
293 | socklen_t sourcelen; |
---|
294 | struct sockaddr_storage source_sock; |
---|
295 | |
---|
296 | assert( tr_isAddress( addr ) ); |
---|
297 | |
---|
298 | if( !tr_isValidPeerAddress( addr, port ) ) |
---|
299 | return -EINVAL; |
---|
300 | |
---|
301 | s = tr_fdSocketCreate( session, domains[addr->type], SOCK_STREAM ); |
---|
302 | if( s < 0 ) |
---|
303 | return -1; |
---|
304 | |
---|
305 | /* seeds don't need much of a read buffer... */ |
---|
306 | if( clientIsSeed ) { |
---|
307 | int n = 8192; |
---|
308 | if( setsockopt( s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n) ) ) |
---|
309 | tr_inf( "Unable to set SO_RCVBUF on socket %d: %s", s, tr_strerror( sockerrno ) ); |
---|
310 | } |
---|
311 | |
---|
312 | if( evutil_make_socket_nonblocking( s ) < 0 ) { |
---|
313 | tr_netClose( session, s ); |
---|
314 | return -1; |
---|
315 | } |
---|
316 | |
---|
317 | addrlen = setup_sockaddr( addr, port, &sock ); |
---|
318 | |
---|
319 | /* set source address */ |
---|
320 | source_addr = tr_sessionGetPublicAddress( session, addr->type, NULL ); |
---|
321 | assert( source_addr ); |
---|
322 | sourcelen = setup_sockaddr( source_addr, 0, &source_sock ); |
---|
323 | if( bind( s, ( struct sockaddr * ) &source_sock, sourcelen ) ) |
---|
324 | { |
---|
325 | tr_err( _( "Couldn't set source address %s on %d: %s" ), |
---|
326 | tr_ntop_non_ts( source_addr ), s, tr_strerror( errno ) ); |
---|
327 | return -errno; |
---|
328 | } |
---|
329 | |
---|
330 | if( ( connect( s, (struct sockaddr *) &sock, |
---|
331 | addrlen ) < 0 ) |
---|
332 | #ifdef WIN32 |
---|
333 | && ( sockerrno != WSAEWOULDBLOCK ) |
---|
334 | #endif |
---|
335 | && ( sockerrno != EINPROGRESS ) ) |
---|
336 | { |
---|
337 | int tmperrno; |
---|
338 | tmperrno = sockerrno; |
---|
339 | if( ( tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH ) |
---|
340 | || addr->type == TR_AF_INET ) |
---|
341 | tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ), |
---|
342 | s, tr_ntop_non_ts( addr ), (int)ntohs( port ), tmperrno, |
---|
343 | tr_strerror( tmperrno ) ); |
---|
344 | tr_netClose( session, s ); |
---|
345 | s = -tmperrno; |
---|
346 | } |
---|
347 | |
---|
348 | tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)", |
---|
349 | s, tr_peerIoAddrStr( addr, port ) ); |
---|
350 | |
---|
351 | return s; |
---|
352 | } |
---|
353 | |
---|
354 | struct UTPSocket * |
---|
355 | tr_netOpenPeerUTPSocket( tr_session * session, |
---|
356 | const tr_address * addr, |
---|
357 | tr_port port, |
---|
358 | tr_bool clientIsSeed UNUSED ) |
---|
359 | { |
---|
360 | struct sockaddr_storage ss; |
---|
361 | socklen_t sslen; |
---|
362 | sslen = setup_sockaddr( addr, port, &ss ); |
---|
363 | |
---|
364 | return UTP_Create( tr_utpSendTo, (void*)session, |
---|
365 | (struct sockaddr*)&ss, sslen ); |
---|
366 | } |
---|
367 | |
---|
368 | static int |
---|
369 | tr_netBindTCPImpl( const tr_address * addr, tr_port port, tr_bool suppressMsgs, int * errOut ) |
---|
370 | { |
---|
371 | static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 }; |
---|
372 | struct sockaddr_storage sock; |
---|
373 | int fd; |
---|
374 | int addrlen; |
---|
375 | int optval; |
---|
376 | |
---|
377 | assert( tr_isAddress( addr ) ); |
---|
378 | |
---|
379 | fd = socket( domains[addr->type], SOCK_STREAM, 0 ); |
---|
380 | if( fd < 0 ) { |
---|
381 | *errOut = sockerrno; |
---|
382 | return -1; |
---|
383 | } |
---|
384 | |
---|
385 | if( evutil_make_socket_nonblocking( fd ) < 0 ) { |
---|
386 | *errOut = sockerrno; |
---|
387 | tr_netCloseSocket( fd ); |
---|
388 | return -1; |
---|
389 | } |
---|
390 | |
---|
391 | optval = 1; |
---|
392 | setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) ); |
---|
393 | setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) ); |
---|
394 | |
---|
395 | #ifdef IPV6_V6ONLY |
---|
396 | if( addr->type == TR_AF_INET6 ) |
---|
397 | if( setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof( optval ) ) == -1 ) |
---|
398 | if( sockerrno != ENOPROTOOPT ) { /* if the kernel doesn't support it, ignore it */ |
---|
399 | *errOut = sockerrno; |
---|
400 | return -1; |
---|
401 | } |
---|
402 | #endif |
---|
403 | |
---|
404 | addrlen = setup_sockaddr( addr, htons( port ), &sock ); |
---|
405 | if( bind( fd, (struct sockaddr *) &sock, addrlen ) ) { |
---|
406 | const int err = sockerrno; |
---|
407 | if( !suppressMsgs ) |
---|
408 | { |
---|
409 | const char * fmt; |
---|
410 | const char * hint; |
---|
411 | |
---|
412 | if( err == EADDRINUSE ) |
---|
413 | hint = _( "Is another copy of Transmission already running?" ); |
---|
414 | else |
---|
415 | hint = NULL; |
---|
416 | |
---|
417 | if( hint == NULL ) |
---|
418 | fmt = _( "Couldn't bind port %d on %s: %s" ); |
---|
419 | else |
---|
420 | fmt = _( "Couldn't bind port %d on %s: %s (%s)" ); |
---|
421 | |
---|
422 | tr_err( fmt, port, tr_ntop_non_ts( addr ), tr_strerror( err ), hint ); |
---|
423 | } |
---|
424 | tr_netCloseSocket( fd ); |
---|
425 | *errOut = err; |
---|
426 | return -1; |
---|
427 | } |
---|
428 | |
---|
429 | if( !suppressMsgs ) |
---|
430 | tr_dbg( "Bound socket %d to port %d on %s", fd, port, tr_ntop_non_ts( addr ) ); |
---|
431 | |
---|
432 | if( listen( fd, 128 ) == -1 ) { |
---|
433 | *errOut = sockerrno; |
---|
434 | tr_netCloseSocket( fd ); |
---|
435 | return -1; |
---|
436 | } |
---|
437 | |
---|
438 | return fd; |
---|
439 | } |
---|
440 | |
---|
441 | int |
---|
442 | tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs ) |
---|
443 | { |
---|
444 | int unused; |
---|
445 | return tr_netBindTCPImpl( addr, port, suppressMsgs, &unused ); |
---|
446 | } |
---|
447 | |
---|
448 | tr_bool |
---|
449 | tr_net_hasIPv6( tr_port port ) |
---|
450 | { |
---|
451 | static tr_bool result = FALSE; |
---|
452 | static tr_bool alreadyDone = FALSE; |
---|
453 | |
---|
454 | if( !alreadyDone ) |
---|
455 | { |
---|
456 | int err; |
---|
457 | int fd = tr_netBindTCPImpl( &tr_in6addr_any, port, TRUE, &err ); |
---|
458 | if( fd >= 0 || err != EAFNOSUPPORT ) /* we support ipv6 */ |
---|
459 | result = TRUE; |
---|
460 | if( fd >= 0 ) |
---|
461 | tr_netCloseSocket( fd ); |
---|
462 | alreadyDone = TRUE; |
---|
463 | } |
---|
464 | |
---|
465 | return result; |
---|
466 | } |
---|
467 | |
---|
468 | int |
---|
469 | tr_netAccept( tr_session * session, |
---|
470 | int b, |
---|
471 | tr_address * addr, |
---|
472 | tr_port * port ) |
---|
473 | { |
---|
474 | int fd = tr_fdSocketAccept( session, b, addr, port ); |
---|
475 | |
---|
476 | if( fd>=0 && evutil_make_socket_nonblocking(fd)<0 ) { |
---|
477 | tr_netClose( session, fd ); |
---|
478 | fd = -1; |
---|
479 | } |
---|
480 | |
---|
481 | return fd; |
---|
482 | } |
---|
483 | |
---|
484 | void |
---|
485 | tr_netCloseSocket( int fd ) |
---|
486 | { |
---|
487 | EVUTIL_CLOSESOCKET( fd ); |
---|
488 | } |
---|
489 | |
---|
490 | void |
---|
491 | tr_netClose( tr_session * session, int s ) |
---|
492 | { |
---|
493 | tr_fdSocketClose( session, s ); |
---|
494 | } |
---|
495 | |
---|
496 | /* |
---|
497 | get_source_address() and global_unicast_address() were written by |
---|
498 | Juliusz Chroboczek, and are covered under the same license as dht.c. |
---|
499 | Please feel free to copy them into your software if it can help |
---|
500 | unbreaking the double-stack Internet. */ |
---|
501 | |
---|
502 | /* Get the source address used for a given destination address. Since |
---|
503 | there is no official interface to get this information, we create |
---|
504 | a connected UDP socket (connected UDP... hmm...) and check its source |
---|
505 | address. */ |
---|
506 | static int |
---|
507 | get_source_address( const struct sockaddr * dst, |
---|
508 | socklen_t dst_len, |
---|
509 | struct sockaddr * src, |
---|
510 | socklen_t * src_len ) |
---|
511 | { |
---|
512 | int s, rc, save; |
---|
513 | |
---|
514 | s = socket(dst->sa_family, SOCK_DGRAM, 0); |
---|
515 | if(s < 0) |
---|
516 | goto fail; |
---|
517 | |
---|
518 | /* Since it's a UDP socket, this doesn't actually send any packets. */ |
---|
519 | rc = connect(s, dst, dst_len); |
---|
520 | if(rc < 0) |
---|
521 | goto fail; |
---|
522 | |
---|
523 | rc = getsockname(s, src, src_len); |
---|
524 | if(rc < 0) |
---|
525 | goto fail; |
---|
526 | |
---|
527 | EVUTIL_CLOSESOCKET( s ); |
---|
528 | |
---|
529 | return rc; |
---|
530 | |
---|
531 | fail: |
---|
532 | save = errno; |
---|
533 | EVUTIL_CLOSESOCKET( s ); |
---|
534 | errno = save; |
---|
535 | return -1; |
---|
536 | } |
---|
537 | |
---|
538 | /* We all hate NATs. */ |
---|
539 | static int |
---|
540 | global_unicast_address(struct sockaddr *sa) |
---|
541 | { |
---|
542 | if(sa->sa_family == AF_INET) { |
---|
543 | const unsigned char *a = |
---|
544 | (unsigned char*)&((struct sockaddr_in*)sa)->sin_addr; |
---|
545 | if(a[0] == 0 || a[0] == 127 || a[0] >= 224 || |
---|
546 | a[0] == 10 || (a[0] == 172 && a[1] >= 16 && a[1] <= 31) || |
---|
547 | (a[0] == 192 && a[1] == 168)) |
---|
548 | return 0; |
---|
549 | return 1; |
---|
550 | } else if(sa->sa_family == AF_INET6) { |
---|
551 | const unsigned char *a = |
---|
552 | (unsigned char*)&((struct sockaddr_in6*)sa)->sin6_addr; |
---|
553 | /* 2000::/3 */ |
---|
554 | return (a[0] & 0xE0) == 0x20; |
---|
555 | } else { |
---|
556 | errno = EAFNOSUPPORT; |
---|
557 | return -1; |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | static int |
---|
562 | tr_globalAddress( int af, void *addr, int *addr_len ) |
---|
563 | { |
---|
564 | struct sockaddr_storage ss; |
---|
565 | socklen_t sslen = sizeof(ss); |
---|
566 | struct sockaddr_in sin; |
---|
567 | struct sockaddr_in6 sin6; |
---|
568 | struct sockaddr *sa; |
---|
569 | socklen_t salen; |
---|
570 | int rc; |
---|
571 | |
---|
572 | switch(af) { |
---|
573 | case AF_INET: |
---|
574 | memset(&sin, 0, sizeof(sin)); |
---|
575 | sin.sin_family = AF_INET; |
---|
576 | inet_pton(AF_INET, "91.121.74.28", &sin.sin_addr); |
---|
577 | sin.sin_port = htons(6969); |
---|
578 | sa = (struct sockaddr*)&sin; |
---|
579 | salen = sizeof(sin); |
---|
580 | break; |
---|
581 | case AF_INET6: |
---|
582 | memset(&sin6, 0, sizeof(sin6)); |
---|
583 | sin6.sin6_family = AF_INET6; |
---|
584 | /* In order for address selection to work right, this should be |
---|
585 | a native IPv6 address, not Teredo or 6to4. */ |
---|
586 | inet_pton(AF_INET6, "2001:1890:1112:1::20", &sin6.sin6_addr); |
---|
587 | sin6.sin6_port = htons(6969); |
---|
588 | sa = (struct sockaddr*)&sin6; |
---|
589 | salen = sizeof(sin6); |
---|
590 | break; |
---|
591 | default: |
---|
592 | return -1; |
---|
593 | } |
---|
594 | |
---|
595 | rc = get_source_address( sa, salen, (struct sockaddr*)&ss, &sslen ); |
---|
596 | |
---|
597 | if( rc < 0 ) |
---|
598 | return -1; |
---|
599 | |
---|
600 | if( !global_unicast_address( (struct sockaddr*)&ss) ) |
---|
601 | return -1; |
---|
602 | |
---|
603 | switch(af) { |
---|
604 | case AF_INET: |
---|
605 | if(*addr_len < 4) |
---|
606 | return -1; |
---|
607 | memcpy(addr, &((struct sockaddr_in*)&ss)->sin_addr, 4); |
---|
608 | *addr_len = 4; |
---|
609 | return 1; |
---|
610 | case AF_INET6: |
---|
611 | if(*addr_len < 16) |
---|
612 | return -1; |
---|
613 | memcpy(addr, &((struct sockaddr_in6*)&ss)->sin6_addr, 16); |
---|
614 | *addr_len = 16; |
---|
615 | return 1; |
---|
616 | default: |
---|
617 | return -1; |
---|
618 | } |
---|
619 | } |
---|
620 | |
---|
621 | /* Return our global IPv6 address, with caching. */ |
---|
622 | |
---|
623 | const unsigned char * |
---|
624 | tr_globalIPv6( void ) |
---|
625 | { |
---|
626 | static unsigned char ipv6[16]; |
---|
627 | static time_t last_time = 0; |
---|
628 | static int have_ipv6 = 0; |
---|
629 | const time_t now = tr_time( ); |
---|
630 | |
---|
631 | /* Re-check every half hour */ |
---|
632 | if( last_time < now - 1800 ) |
---|
633 | { |
---|
634 | int addrlen = 16; |
---|
635 | const int rc = tr_globalAddress( AF_INET6, ipv6, &addrlen ); |
---|
636 | have_ipv6 = ( rc >= 0 ) && ( addrlen == 16 ); |
---|
637 | last_time = now; |
---|
638 | } |
---|
639 | |
---|
640 | return have_ipv6 ? ipv6 : NULL; |
---|
641 | } |
---|
642 | |
---|
643 | /*** |
---|
644 | **** |
---|
645 | **** |
---|
646 | ***/ |
---|
647 | |
---|
648 | static tr_bool |
---|
649 | isIPv4MappedAddress( const tr_address * addr ) |
---|
650 | { |
---|
651 | return ( addr->type == TR_AF_INET6 ) && IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ); |
---|
652 | } |
---|
653 | |
---|
654 | static tr_bool |
---|
655 | isIPv6LinkLocalAddress( const tr_address * addr ) |
---|
656 | { |
---|
657 | return ( ( addr->type == TR_AF_INET6 ) |
---|
658 | && IN6_IS_ADDR_LINKLOCAL( &addr->addr.addr6 ) ); |
---|
659 | } |
---|
660 | |
---|
661 | /* isMartianAddr was written by Juliusz Chroboczek, |
---|
662 | and is covered under the same license as third-party/dht/dht.c. */ |
---|
663 | static tr_bool |
---|
664 | isMartianAddr( const struct tr_address * a ) |
---|
665 | { |
---|
666 | static const unsigned char zeroes[16] = |
---|
667 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
---|
668 | |
---|
669 | assert( tr_isAddress( a ) ); |
---|
670 | |
---|
671 | switch( a->type ) |
---|
672 | { |
---|
673 | case TR_AF_INET: { |
---|
674 | const unsigned char * address = (const unsigned char*)&a->addr.addr4; |
---|
675 | return (address[0] == 0) || |
---|
676 | (address[0] == 127) || |
---|
677 | ((address[0] & 0xE0) == 0xE0); |
---|
678 | break; |
---|
679 | } |
---|
680 | |
---|
681 | case TR_AF_INET6: { |
---|
682 | const unsigned char * address = (const unsigned char*)&a->addr.addr6; |
---|
683 | return (address[0] == 0xFF) || |
---|
684 | (memcmp(address, zeroes, 15) == 0 && |
---|
685 | (address[15] == 0 || address[15] == 1)) || |
---|
686 | /* Addresses outside of 2000::/3 are currently reserved, |
---|
687 | but might be allocated at some future time. Since |
---|
688 | there are a lot of buggy peers pushing around such |
---|
689 | addresses over PEX, we reject them until the end of |
---|
690 | the 13th Baktun. */ |
---|
691 | (tr_time() < 1356130800 && (address[0] & 0xE0) != 0x20); |
---|
692 | break; |
---|
693 | } |
---|
694 | |
---|
695 | default: |
---|
696 | return TRUE; |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | tr_bool |
---|
701 | tr_isValidPeerAddress( const tr_address * addr, tr_port port ) |
---|
702 | { |
---|
703 | return ( port != 0 ) |
---|
704 | && ( tr_isAddress( addr ) ) |
---|
705 | && ( !isIPv6LinkLocalAddress( addr ) ) |
---|
706 | && ( !isIPv4MappedAddress( addr ) ) |
---|
707 | && ( !isMartianAddr( addr ) ); |
---|
708 | } |
---|