Changeset 7231
- Timestamp:
- Dec 2, 2008, 3:41:58 AM (13 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist-test.c
r7223 r7231 52 52 char * tmpfile_bin = "transmission-blocklist-test.bin"; 53 53 #endif 54 struct in_addraddr;54 struct tr_address addr; 55 55 int test = 0; 56 56 tr_blocklist * b; … … 64 64 65 65 /* now run some tests */ 66 check( !tr_netResolve( "216.16.1.143", &addr ) );66 check( tr_pton( "216.16.1.143", &addr ) ); 67 67 check( !_tr_blocklistHasAddress( b, &addr ) ); 68 check( !tr_netResolve( "216.16.1.144", &addr ) );68 check( tr_pton( "216.16.1.144", &addr ) ); 69 69 check( _tr_blocklistHasAddress( b, &addr ) ); 70 check( !tr_netResolve( "216.16.1.145", &addr ) );70 check( tr_pton( "216.16.1.145", &addr ) ); 71 71 check( _tr_blocklistHasAddress( b, &addr ) ); 72 check( !tr_netResolve( "216.16.1.146", &addr ) );72 check( tr_pton( "216.16.1.146", &addr ) ); 73 73 check( _tr_blocklistHasAddress( b, &addr ) ); 74 check( !tr_netResolve( "216.16.1.147", &addr ) );74 check( tr_pton( "216.16.1.147", &addr ) ); 75 75 check( _tr_blocklistHasAddress( b, &addr ) ); 76 check( !tr_netResolve( "216.16.1.148", &addr ) );76 check( tr_pton( "216.16.1.148", &addr ) ); 77 77 check( _tr_blocklistHasAddress( b, &addr ) ); 78 check( !tr_netResolve( "216.16.1.149", &addr ) );78 check( tr_pton( "216.16.1.149", &addr ) ); 79 79 check( _tr_blocklistHasAddress( b, &addr ) ); 80 check( !tr_netResolve( "216.16.1.150", &addr ) );80 check( tr_pton( "216.16.1.150", &addr ) ); 81 81 check( _tr_blocklistHasAddress( b, &addr ) ); 82 check( !tr_netResolve( "216.16.1.151", &addr ) );82 check( tr_pton( "216.16.1.151", &addr ) ); 83 83 check( _tr_blocklistHasAddress( b, &addr ) ); 84 check( !tr_netResolve( "216.16.1.152", &addr ) );84 check( tr_pton( "216.16.1.152", &addr ) ); 85 85 check( !_tr_blocklistHasAddress( b, &addr ) ); 86 check( !tr_netResolve( "216.16.1.153", &addr ) );86 check( tr_pton( "216.16.1.153", &addr ) ); 87 87 check( !_tr_blocklistHasAddress( b, &addr ) ); 88 check( !tr_netResolve( "217.0.0.1", &addr ) );88 check( tr_pton( "217.0.0.1", &addr ) ); 89 89 check( !_tr_blocklistHasAddress( b, &addr ) ); 90 check( !tr_netResolve( "255.0.0.1", &addr ) );90 check( tr_pton( "255.0.0.1", &addr ) ); 91 91 92 92 /* cleanup */ -
trunk/libtransmission/blocklist.c
r7223 r7231 32 32 #include "platform.h" 33 33 #include "blocklist.h" 34 #include "net.h" /* tr_netResolve() */34 #include "net.h" 35 35 #include "utils.h" 36 36 … … 199 199 200 200 int 201 _tr_blocklistHasAddress( tr_blocklist *b,202 const struct in_addr* addr )201 _tr_blocklistHasAddress( tr_blocklist * b, 202 const struct tr_address * addr ) 203 203 { 204 204 uint32_t needle; … … 212 212 return 0; 213 213 214 needle = ntohl( addr-> s_addr );214 needle = ntohl( addr->addr.addr4.s_addr ); 215 215 216 216 range = bsearch( &needle, … … 260 260 char * rangeBegin; 261 261 char * rangeEnd; 262 struct in_addr in_addr;262 struct tr_address addr; 263 263 struct tr_ip_range range; 264 264 … … 271 271 *rangeEnd++ = '\0'; 272 272 273 if( tr_netResolve( rangeBegin, &in_addr ) )273 if( !tr_pton( rangeBegin, &addr ) ) 274 274 tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin ); 275 range.begin = ntohl( in_addr.s_addr );276 277 if( tr_netResolve( rangeEnd, &in_addr ) )275 range.begin = ntohl( addr.addr.addr4.s_addr ); 276 277 if( !tr_pton( rangeEnd, &addr ) ) 278 278 tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd ); 279 range.end = ntohl( in_addr.s_addr );279 range.end = ntohl( addr.addr.addr4.s_addr ); 280 280 281 281 free( line ); -
trunk/libtransmission/blocklist.h
r7223 r7231 18 18 #define TR_BLOCKLIST_H 19 19 20 struct in_addr;20 struct tr_address; 21 21 typedef struct tr_blocklist tr_blocklist; 22 22 23 tr_blocklist* _tr_blocklistNew ( const char* filename,24 intisEnabled );23 tr_blocklist* _tr_blocklistNew ( const char * filename, 24 int isEnabled ); 25 25 26 int _tr_blocklistExists ( const tr_blocklist *);26 int _tr_blocklistExists ( const tr_blocklist * b ); 27 27 28 const char* _tr_blocklistGetFilename ( const tr_blocklist *);28 const char* _tr_blocklistGetFilename ( const tr_blocklist * b ); 29 29 30 int _tr_blocklistGetRuleCount( const tr_blocklist *);30 int _tr_blocklistGetRuleCount( const tr_blocklist * b ); 31 31 32 void _tr_blocklistFree ( tr_blocklist * );32 void _tr_blocklistFree ( tr_blocklist * ); 33 33 34 int _tr_blocklistIsEnabled ( tr_blocklist *);34 int _tr_blocklistIsEnabled ( tr_blocklist * b ); 35 35 36 void _tr_blocklistSetEnabled ( tr_blocklist *,37 intisEnabled );36 void _tr_blocklistSetEnabled ( tr_blocklist * b, 37 int isEnabled ); 38 38 39 int _tr_blocklistHasAddress( 40 tr_blocklist *, 41 const struct 42 in_addr * addr ); 39 int _tr_blocklistHasAddress ( tr_blocklist * b, 40 const struct tr_address * addr ); 43 41 44 int _tr_blocklistSetContent( 45 tr_blocklist *, 46 const char * filename ); 42 int _tr_blocklistSetContent ( tr_blocklist * b, 43 const char * filename ); 47 44 48 45 #endif -
trunk/libtransmission/fdlimit.c
r7224 r7231 472 472 473 473 int 474 tr_fdSocketAccept( int 475 struct in_addr* addr,476 tr_port 474 tr_fdSocketAccept( int b, 475 tr_address * addr, 476 tr_port * port ) 477 477 { 478 478 int s = -1; 479 479 unsigned int len; 480 struct sockaddr_ insock;480 struct sockaddr_storage sock; 481 481 482 482 assert( addr ); … … 486 486 if( gFd->socketCount < getSocketMax( gFd ) ) 487 487 { 488 len = sizeof( s ock);488 len = sizeof( struct sockaddr ); 489 489 s = accept( b, (struct sockaddr *) &sock, &len ); 490 490 } 491 491 if( s > -1 ) 492 492 { 493 *addr = sock.sin_addr; 494 *port = sock.sin_port; 493 /* "The ss_family field of the sockaddr_storage structure will always 494 * align with the family field of any protocol-specific structure." */ 495 if( sock.ss_family == AF_INET ) 496 { 497 struct sockaddr_in * sock4 = (struct sockaddr_in *)&sock; 498 addr->type = TR_AF_INET; 499 addr->addr.addr4.s_addr = sock4->sin_addr.s_addr; 500 *port = sock4->sin_port; 501 } 502 else 503 { 504 struct sockaddr_in6 * sock6 = (struct sockaddr_in6 *)&sock; 505 addr->type = TR_AF_INET6; 506 memcpy( &addr->addr, &sock6->sin6_addr, 507 sizeof( struct sockaddr_in6 ) ); 508 *port = sock6->sin6_port; 509 } 495 510 ++gFd->socketCount; 496 511 } -
trunk/libtransmission/fdlimit.h
r7224 r7231 88 88 int tr_fdSocketCreate( int type ); 89 89 90 int tr_fdSocketAccept( int 91 struct in_addr* addr,92 tr_port 90 int tr_fdSocketAccept( int b, 91 tr_address * addr, 92 tr_port * port ); 93 93 94 94 void tr_fdSocketClose( int s ); -
trunk/libtransmission/handshake.c
r7224 r7231 1197 1197 } 1198 1198 1199 const struct in_addr*1199 const tr_address * 1200 1200 tr_handshakeGetAddr( const struct tr_handshake * handshake, 1201 1201 tr_port * port ) -
trunk/libtransmission/handshake.h
r7224 r7231 20 20 #include "transmission.h" 21 21 22 struct in_addr;22 struct tr_address; 23 23 struct tr_peerIo; 24 24 typedef struct tr_handshake tr_handshake; … … 36 36 void * doneUserData ); 37 37 38 const struct in_addr * tr_handshakeGetAddr( const struct tr_handshake * handshake, 38 const struct tr_address * 39 tr_handshakeGetAddr( const struct tr_handshake * handshake, 39 40 tr_port * port ); 40 41 -
trunk/libtransmission/net.c
r7224 r7231 48 48 #include "utils.h" 49 49 50 const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; 51 const tr_address tr_inaddr_any = { TR_AF_INET, 52 { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; 53 50 54 51 55 void … … 64 68 } 65 69 66 /*********************************************************************** 67 * DNS resolution 68 * 69 * Synchronous "resolution": only works with character strings 70 * representing numbers expressed in the Internet standard `.' notation. 71 * Returns a non-zero value if an error occurs. 72 **********************************************************************/ 73 int 74 tr_netResolve( const char * address, 75 struct in_addr * addr ) 76 { 77 addr->s_addr = inet_addr( address ); 78 return addr->s_addr == 0xFFFFFFFF; 79 } 70 71 const char * 72 tr_ntop( const tr_address * src, char * dst, int size ) 73 { 74 if( src->type == TR_AF_INET ) 75 return inet_ntop( AF_INET, &src->addr, dst, size ); 76 else 77 return inet_ntop( AF_INET6, &src->addr, dst, size ); 78 } 79 80 /* 81 * Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer. 82 * This function is suitable to be called from libTransmission's networking code, 83 * which is single-threaded. 84 */ 85 const char * 86 tr_ntop_non_ts( const tr_address * src ) 87 { 88 static char buf[INET6_ADDRSTRLEN]; 89 return tr_ntop( src, buf, sizeof( buf ) ); 90 } 91 92 tr_address * 93 tr_pton( const char * src, tr_address * dst ) 94 { 95 int retval = inet_pton( AF_INET, src, &dst->addr ); 96 if( retval < 0 ) 97 return NULL; 98 else if( retval == 0 ) 99 retval = inet_pton( AF_INET6, src, &dst->addr ); 100 else 101 { 102 dst->type = TR_AF_INET; 103 return dst; 104 } 105 106 if( retval < 1 ) 107 return NULL; 108 dst->type = TR_AF_INET6; 109 return dst; 110 } 111 112 /* 113 * Compare two tr_address structures. 114 * Returns: 115 * <0 if a < b 116 * >0 if a > b 117 * 0 if a == b 118 */ 119 int 120 tr_compareAddresses( const tr_address * a, const tr_address * b) 121 { 122 int retval; 123 int addrlen; 124 125 /* IPv6 addresses are always "greater than" IPv4 */ 126 if( a->type == TR_AF_INET && b->type == TR_AF_INET6 ) 127 return 1; 128 if( a->type == TR_AF_INET6 && b->type == TR_AF_INET ) 129 return -1; 130 131 if( a->type == TR_AF_INET ) 132 addrlen = sizeof( struct in_addr ); 133 else 134 addrlen = sizeof( struct in6_addr ); 135 retval = memcmp( &a->addr, &b->addr, addrlen ); 136 if( retval == 0 ) 137 return 0; 138 139 return retval; 140 } 80 141 81 142 /*********************************************************************** … … 137 198 } 138 199 139 int 140 tr_netOpenTCP( tr_session * session, 141 const struct in_addr * addr, 142 tr_port port ) 143 { 144 int s; 145 struct sockaddr_in sock; 146 const int type = SOCK_STREAM; 200 static void 201 setup_sockaddr( const tr_address * addr, 202 tr_port port, 203 struct sockaddr_storage * sockaddr) 204 { 205 struct sockaddr_in sock4; 206 struct sockaddr_in6 sock6; 207 208 if( addr->type == TR_AF_INET ) 209 { 210 memset( &sock4, 0, sizeof( sock4 ) ); 211 sock4.sin_family = AF_INET; 212 sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; 213 sock4.sin_port = port; 214 memcpy( sockaddr, &sock4, sizeof( sock4 ) ); 215 } 216 else 217 { 218 memset( &sock6, 0, sizeof( sock6 ) ); 219 sock6.sin6_family = AF_INET6; 220 sock6.sin6_port = port; 221 memcpy( &sock6.sin6_addr, &addr->addr, sizeof( struct in6_addr ) ); 222 memcpy( sockaddr, &sock6, sizeof( sock6 ) ); 223 } 224 } 225 226 int 227 tr_netOpenTCP( tr_session * session, 228 const tr_address * addr, 229 tr_port port ) 230 { 231 int s; 232 struct sockaddr_storage sock; 233 const int type = SOCK_STREAM; 147 234 148 235 if( ( s = createSocket( type ) ) < 0 ) … … 151 238 setSndBuf( session, s ); 152 239 153 memset( &sock, 0, sizeof( sock ) ); 154 sock.sin_family = AF_INET; 155 sock.sin_addr.s_addr = addr->s_addr; 156 sock.sin_port = port; 240 setup_sockaddr( addr, port, &sock ); 157 241 158 242 if( ( connect( s, (struct sockaddr *) &sock, 159 sizeof( struct sockaddr _in) ) < 0 )243 sizeof( struct sockaddr ) ) < 0 ) 160 244 #ifdef WIN32 161 245 && ( sockerrno != WSAEWOULDBLOCK ) … … 164 248 { 165 249 tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ), 166 s, inet_ntoa( *addr ), (int)port, sockerrno, tr_strerror( sockerrno ) );250 s, tr_ntop_non_ts( addr ), (int)port, sockerrno, tr_strerror( sockerrno ) ); 167 251 tr_netClose( s ); 168 252 s = -1; … … 176 260 177 261 int 178 tr_netBindTCP( int port )179 { 180 int s;181 struct sockaddr_ insock;182 const int type = SOCK_STREAM;262 tr_netBindTCP( const tr_address * addr, tr_port port ) 263 { 264 int s; 265 struct sockaddr_storage sock; 266 const int type = SOCK_STREAM; 183 267 184 268 #if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) … … 194 278 #endif 195 279 196 memset( &sock, 0, sizeof( sock ) ); 197 sock.sin_family = AF_INET; 198 sock.sin_addr.s_addr = INADDR_ANY; 199 sock.sin_port = htons( port ); 280 setup_sockaddr( addr, htons( port ), &sock ); 200 281 201 282 if( bind( s, (struct sockaddr *) &sock, 202 sizeof( struct sockaddr _in) ) )203 { 204 tr_err( _( "Couldn't bind port %d : %s" ), port,205 tr_ strerror( sockerrno ) );283 sizeof( struct sockaddr ) ) ) 284 { 285 tr_err( _( "Couldn't bind port %d on %s: %s" ), port, 286 tr_ntop_non_ts( addr ), tr_strerror( sockerrno ) ); 206 287 tr_netClose( s ); 207 288 return -1; 208 289 } 209 290 210 tr_dbg( "Bound socket %d to port %d", s, port ); 291 tr_dbg( "Bound socket %d to port %d on %s", 292 s, port, tr_ntop_non_ts( addr ) ); 211 293 return s; 212 294 } 213 295 214 296 int 215 tr_netAccept( tr_session 216 int 217 struct in_addr* addr,218 tr_port 297 tr_netAccept( tr_session * session, 298 int b, 299 tr_address * addr, 300 tr_port * port ) 219 301 { 220 302 int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) ); … … 228 310 tr_fdSocketClose( s ); 229 311 } 230 231 void232 tr_netNtop( const struct in_addr * addr,233 char * buf,234 int len )235 {236 const uint8_t * cast;237 238 cast = (const uint8_t *)addr;239 tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu",240 cast[0], cast[1], cast[2], cast[3] );241 }242 -
trunk/libtransmission/net.h
r7224 r7231 58 58 #endif 59 59 60 struct in_addr;61 struct sockaddr_in;62 60 struct tr_session; 63 61 64 /*********************************************************************** 65 * DNS resolution 66 **********************************************************************/ 67 int tr_netResolve( const char *, 68 struct in_addr * ); 69 62 #define TR_AF_INET 0 63 #define TR_AF_INET6 1 64 65 typedef struct tr_address { 66 unsigned short type : 1; 67 union { 68 /* The order here is important for tr_in{,6}addr_any initialization, 69 * since we can't use C99 designated initializers */ 70 struct in6_addr addr6; 71 struct in_addr addr4; 72 } addr; 73 } tr_address; 74 75 extern const tr_address tr_inaddr_any; 76 extern const tr_address tr_in6addr_any; 77 78 const char *tr_ntop( const tr_address * src, 79 char * dst, 80 int size ); 81 const char *tr_ntop_non_ts( const tr_address * src ); 82 tr_address *tr_pton( const char * src, 83 tr_address * dst ); 84 int tr_compareAddresses( const tr_address * a, 85 const tr_address * b); 70 86 71 87 /*********************************************************************** 72 88 * Sockets 73 89 **********************************************************************/ 74 int tr_netOpenTCP( struct tr_handle 75 const struct in_addr* addr,76 tr_port 90 int tr_netOpenTCP( struct tr_handle * session, 91 const tr_address * addr, 92 tr_port port ); 77 93 78 int tr_netBindTCP( int port ); 94 int tr_netBindTCP( const tr_address * addr, 95 tr_port port ); 79 96 80 97 int tr_netAccept( struct tr_handle * session, 81 98 int bound, 82 struct in_addr* setme_addr,99 tr_address * setme_addr, 83 100 tr_port * setme_port ); 84 101 … … 88 105 void tr_netClose( int s ); 89 106 90 void tr_netNtop( const struct in_addr * addr,91 char * buf,92 int len );93 94 107 void tr_netInit( void ); 95 108 -
trunk/libtransmission/peer-io.c
r7224 r7231 20 20 #include <winsock2.h> 21 21 #else 22 #include <netinet/in.h> /* struct in_addr */23 22 #include <arpa/inet.h> /* inet_ntoa */ 24 23 #endif … … 98 97 tr_session * session; 99 98 100 struct in_addr in_addr;99 tr_address addr; 101 100 struct tr_iobuf * iobuf; 102 101 tr_list * output_datatypes; /* struct tr_datatype */ … … 239 238 240 239 static tr_peerIo* 241 tr_peerIoNew( tr_session *session,242 const struct in_addr * in_addr,243 tr_port 244 const uint8_t *torrentHash,245 int 246 int 240 tr_peerIoNew( tr_session * session, 241 const tr_address * addr, 242 tr_port port, 243 const uint8_t * torrentHash, 244 int isIncoming, 245 int socket ) 247 246 { 248 247 tr_peerIo * io; … … 255 254 io->crypto = tr_cryptoNew( torrentHash, isIncoming ); 256 255 io->session = session; 257 io-> in_addr = *in_addr;256 io->addr = *addr; 258 257 io->port = port; 259 258 io->socket = socket; … … 267 266 268 267 tr_peerIo* 269 tr_peerIoNewIncoming( tr_session *session,270 const struct in_addr * in_addr,271 tr_port 272 int 268 tr_peerIoNewIncoming( tr_session * session, 269 const tr_address * addr, 270 tr_port port, 271 int socket ) 273 272 { 274 273 assert( session ); 275 assert( in_addr );274 assert( addr ); 276 275 assert( socket >= 0 ); 277 276 278 return tr_peerIoNew( session, in_addr, port, 279 NULL, 1, 280 socket ); 277 return tr_peerIoNew( session, addr, port, NULL, 1, socket ); 281 278 } 282 279 283 280 tr_peerIo* 284 tr_peerIoNewOutgoing( tr_session *session,285 const struct in_addr * in_addr,286 intport,287 const uint8_t *torrentHash )281 tr_peerIoNewOutgoing( tr_session * session, 282 const tr_address * addr, 283 tr_port port, 284 const uint8_t * torrentHash ) 288 285 { 289 286 int socket; 290 287 291 288 assert( session ); 292 assert( in_addr ); 293 assert( port >= 0 ); 289 assert( addr ); 294 290 assert( torrentHash ); 295 291 296 socket = tr_netOpenTCP( session, in_addr, port );292 socket = tr_netOpenTCP( session, addr, port ); 297 293 298 294 return socket < 0 299 295 ? NULL 300 : tr_peerIoNew( session, in_addr, port, torrentHash, 0, socket );296 : tr_peerIoNew( session, addr, port, torrentHash, 0, socket ); 301 297 } 302 298 … … 337 333 } 338 334 339 const struct in_addr*335 const tr_address* 340 336 tr_peerIoGetAddress( const tr_peerIo * io, 341 337 tr_port * port ) … … 346 342 *port = io->port; 347 343 348 return &io-> in_addr;344 return &io->addr; 349 345 } 350 346 351 347 const char* 352 tr_peerIoAddrStr( const struct in_addr* addr,353 tr_port 348 tr_peerIoAddrStr( const tr_address * addr, 349 tr_port port ) 354 350 { 355 351 static char buf[512]; 356 352 357 tr_snprintf( buf, sizeof( buf ), "%s:%u", inet_ntoa( *addr ), 358 ntohs( port ) ); 353 if( addr->type == TR_AF_INET ) 354 tr_snprintf( buf, sizeof( buf ), "%s:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); 355 else 356 tr_snprintf( buf, sizeof( buf ), "[%s]:%u", tr_ntop_non_ts( addr ), ntohs( port ) ); 359 357 return buf; 360 358 } … … 363 361 tr_peerIoGetAddrStr( const tr_peerIo * io ) 364 362 { 365 return tr_peerIoAddrStr( &io-> in_addr, io->port );363 return tr_peerIoAddrStr( &io->addr, io->port ); 366 364 } 367 365 … … 402 400 tr_netClose( io->socket ); 403 401 404 io->socket = tr_netOpenTCP( io->session, &io-> in_addr, io->port );402 io->socket = tr_netOpenTCP( io->session, &io->addr, io->port ); 405 403 406 404 if( io->socket >= 0 ) -
trunk/libtransmission/peer-io.h
r7224 r7231 22 22 **/ 23 23 24 struct in_addr;25 24 struct evbuffer; 25 struct tr_address; 26 26 struct tr_bandwidth; 27 27 struct tr_crypto; … … 33 33 **/ 34 34 35 tr_peerIo* tr_peerIoNewOutgoing( struct tr_handle * session, 36 const struct in_addr * addr, 37 int port, 38 const uint8_t * torrentHash ); 39 40 tr_peerIo* tr_peerIoNewIncoming( struct tr_handle * session, 41 const struct in_addr * addr, 42 tr_port port, 43 int socket ); 44 45 void tr_peerIoFree( tr_peerIo * io ); 46 47 tr_session* tr_peerIoGetSession( tr_peerIo * io ); 48 49 /** 50 *** 51 **/ 52 53 void tr_peerIoEnableLTEP( tr_peerIo * io, 54 int flag ); 55 56 int tr_peerIoSupportsLTEP( const tr_peerIo * io ); 57 58 /** 59 *** 60 **/ 61 62 const char* tr_peerIoAddrStr( const struct in_addr * addr, 63 tr_port port ); 64 65 const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); 66 67 const struct in_addr*tr_peerIoGetAddress( const tr_peerIo * io, 68 tr_port * port ); 35 tr_peerIo* tr_peerIoNewOutgoing( struct tr_handle * session, 36 const struct tr_address * addr, 37 tr_port port, 38 const uint8_t * torrentHash ); 39 40 tr_peerIo* tr_peerIoNewIncoming( struct tr_handle * session, 41 const struct tr_address * addr, 42 tr_port port, 43 int socket ); 44 45 void tr_peerIoFree ( tr_peerIo * io ); 46 47 48 /** 49 *** 50 **/ 51 52 void tr_peerIoEnableLTEP( tr_peerIo * io, 53 int flag ); 54 55 int tr_peerIoSupportsLTEP( const tr_peerIo * io ); 56 57 /** 58 *** 59 **/ 60 61 tr_session* tr_peerIoGetSession ( tr_peerIo * io ); 62 63 const char* tr_peerIoAddrStr( const struct tr_address * addr, 64 tr_port port ); 65 66 const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); 67 68 const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io, 69 tr_port * port ); 69 70 70 71 const uint8_t* tr_peerIoGetTorrentHash( tr_peerIo * io ); -
trunk/libtransmission/peer-mgr-private.h
r7224 r7231 22 22 #ifdef WIN32 23 23 #include <winsock2.h> /* struct in_addr */ 24 #else25 #include <netinet/in.h> /* struct in_addr */26 24 #endif 27 25 … … 53 51 uint8_t encryption_preference; 54 52 tr_port port; 55 struct in_addr in_addr;53 tr_address addr; 56 54 struct tr_peerIo * io; 57 55 -
trunk/libtransmission/peer-mgr.c
r7224 r7231 96 96 struct peer_atom 97 97 { 98 uint8_t 99 uint8_t 100 uint8_t 101 tr_port 102 uint16_t 103 struct in_addraddr;104 time_t 105 time_t 98 uint8_t from; 99 uint8_t flags; /* these match the added_f flags */ 100 uint8_t myflags; /* flags that aren't defined in added_f */ 101 tr_port port; 102 uint16_t numFails; 103 tr_address addr; 104 time_t time; /* when the peer's connection status last changed */ 105 time_t piece_data_time; 106 106 }; 107 107 … … 185 185 186 186 static int 187 compareAddresses( const struct in_addr * a,188 const struct in_addr * b )189 {190 if( a->s_addr != b->s_addr )191 return a->s_addr < b->s_addr ? -1 : 1;192 193 return 0;194 }195 196 static int197 187 handshakeCompareToAddr( const void * va, 198 188 const void * vb ) … … 200 190 const tr_handshake * a = va; 201 191 202 return compareAddresses( tr_handshakeGetAddr( a, NULL ), vb );192 return tr_compareAddresses( tr_handshakeGetAddr( a, NULL ), vb ); 203 193 } 204 194 … … 211 201 212 202 static tr_handshake* 213 getExistingHandshake( tr_ptrArray * handshakes, 214 const struct in_addr * in_addr ) 215 { 216 return tr_ptrArrayFindSorted( handshakes, 217 in_addr, 218 handshakeCompareToAddr ); 219 } 220 221 static int 222 comparePeerAtomToAddress( const void * va, 223 const void * vb ) 203 getExistingHandshake( tr_ptrArray * handshakes, 204 const tr_address * addr ) 205 { 206 return tr_ptrArrayFindSorted( handshakes, addr, handshakeCompareToAddr ); 207 } 208 209 static int 210 comparePeerAtomToAddress( const void * va, const void * vb ) 224 211 { 225 212 const struct peer_atom * a = va; 226 213 227 return compareAddresses( &a->addr, vb ); 228 } 229 230 static int 231 comparePeerAtoms( const void * va, 232 const void * vb ) 214 return tr_compareAddresses( &a->addr, vb ); 215 } 216 217 static int 218 comparePeerAtoms( const void * va, const void * vb ) 233 219 { 234 220 const struct peer_atom * b = vb; … … 271 257 272 258 static int 273 peerCompare( const void * va, 274 const void * vb ) 259 peerCompare( const void * va, const void * vb ) 275 260 { 276 261 const tr_peer * a = va; 277 262 const tr_peer * b = vb; 278 263 279 return compareAddresses( &a->in_addr, &b->in_addr ); 280 } 281 282 static int 283 peerCompareToAddr( const void * va, 284 const void * vb ) 264 return tr_compareAddresses( &a->addr, &b->addr ); 265 } 266 267 static int 268 peerCompareToAddr( const void * va, const void * vb ) 285 269 { 286 270 const tr_peer * a = va; 287 271 288 return compareAddresses( &a->in_addr, vb );272 return tr_compareAddresses( &a->addr, vb ); 289 273 } 290 274 291 275 static tr_peer* 292 getExistingPeer( Torrent *torrent,293 const struct in_addr * in_addr )276 getExistingPeer( Torrent * torrent, 277 const tr_address * addr ) 294 278 { 295 279 assert( torrentIsLocked( torrent ) ); 296 assert( in_addr ); 297 298 return tr_ptrArrayFindSorted( torrent->peers, 299 in_addr, 300 peerCompareToAddr ); 280 assert( addr ); 281 282 return tr_ptrArrayFindSorted( torrent->peers, addr, peerCompareToAddr ); 301 283 } 302 284 303 285 static struct peer_atom* 304 getExistingAtom( const Torrent* t,305 const struct in_addr* addr )286 getExistingAtom( const Torrent * t, 287 const tr_address * addr ) 306 288 { 307 289 assert( torrentIsLocked( t ) ); … … 310 292 311 293 static int 312 peerIsInUse( const Torrent *ct,313 const struct in_addr* addr )294 peerIsInUse( const Torrent * ct, 295 const tr_address * addr ) 314 296 { 315 297 Torrent * t = (Torrent*) ct; … … 323 305 324 306 static tr_peer* 325 peerConstructor( tr_torrent * tor, const struct in_addr * in_addr )307 peerConstructor( tr_torrent * tor, const tr_address * addr ) 326 308 { 327 309 tr_peer * p; 328 310 329 311 p = tr_new0( tr_peer, 1 ); 330 memcpy( &p->in_addr, in_addr, sizeof( struct in_addr ) );312 p->addr = *addr; 331 313 p->bandwidth = tr_bandwidthNew( tor->session, tor->bandwidth ); 332 314 return p; … … 334 316 335 317 static tr_peer* 336 getPeer( Torrent *torrent,337 const struct in_addr * in_addr )318 getPeer( Torrent * torrent, 319 const tr_address * addr ) 338 320 { 339 321 tr_peer * peer; … … 341 323 assert( torrentIsLocked( torrent ) ); 342 324 343 peer = getExistingPeer( torrent, in_addr );325 peer = getExistingPeer( torrent, addr ); 344 326 345 327 if( peer == NULL ) 346 328 { 347 peer = peerConstructor( torrent->tor, in_addr );329 peer = peerConstructor( torrent->tor, addr ); 348 330 tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); 349 331 } … … 381 363 assert( torrentIsLocked( t ) ); 382 364 383 atom = getExistingAtom( t, &peer-> in_addr );365 atom = getExistingAtom( t, &peer->addr ); 384 366 assert( atom ); 385 367 atom->time = time( NULL ); … … 531 513 532 514 int 533 tr_peerMgrPeerIsSeed( const tr_peerMgr * 534 const uint8_t *torrentHash,535 const struct in_addr* addr )515 tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, 516 const uint8_t * torrentHash, 517 const tr_address * addr ) 536 518 { 537 519 int isSeed = FALSE; … … 906 888 { 907 889 tordbg( t, "increasing peer %s strike count to %d", 908 tr_peerIoAddrStr( &peer-> in_addr,890 tr_peerIoAddrStr( &peer->addr, 909 891 peer->port ), peer->strikes + 1 ); 910 892 911 893 if( ++peer->strikes >= MAX_BAD_PIECES_PER_PEER ) 912 894 { 913 struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );895 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 914 896 atom->myflags |= MYFLAG_BANNED; 915 897 peer->doPurge = 1; … … 976 958 /* update our atom */ 977 959 if( peer ) { 978 struct peer_atom * a = getExistingAtom( t, &peer-> in_addr );960 struct peer_atom * a = getExistingAtom( t, &peer->addr ); 979 961 a->piece_data_time = now; 980 962 } … … 1005 987 /* update our atom */ 1006 988 if( peer ) { 1007 struct peer_atom * a = getExistingAtom( t, &peer-> in_addr );989 struct peer_atom * a = getExistingAtom( t, &peer->addr ); 1008 990 a->piece_data_time = now; 1009 991 } … … 1016 998 if( peer ) 1017 999 { 1018 struct peer_atom * atom = getExistingAtom( t, 1019 &peer->in_addr ); 1000 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1020 1001 const int peerIsSeed = e->progress >= 1.0; 1021 1002 if( peerIsSeed ) … … 1112 1093 1113 1094 static void 1114 ensureAtomExists( Torrent *t,1115 const struct in_addr* addr,1116 tr_port 1117 uint8_t 1118 uint8_t 1095 ensureAtomExists( Torrent * t, 1096 const tr_address * addr, 1097 tr_port port, 1098 uint8_t flags, 1099 uint8_t from ) 1119 1100 { 1120 1101 if( getExistingAtom( t, addr ) == NULL ) … … 1153 1134 void * vmanager ) 1154 1135 { 1155 int 1156 int 1157 tr_port 1158 const struct in_addr* addr;1159 tr_peerMgr * manager = (tr_peerMgr*)vmanager;1160 Torrent *t;1161 tr_handshake *ours;1136 int ok = isConnected; 1137 int success = FALSE; 1138 tr_port port; 1139 const tr_address * addr; 1140 tr_peerMgr * manager = vmanager; 1141 Torrent * t; 1142 tr_handshake * ours; 1162 1143 1163 1144 assert( io ); … … 1254 1235 1255 1236 void 1256 tr_peerMgrAddIncoming( tr_peerMgr * 1257 struct in_addr* addr,1258 tr_port 1259 int 1237 tr_peerMgrAddIncoming( tr_peerMgr * manager, 1238 tr_address * addr, 1239 tr_port port, 1240 int socket ) 1260 1241 { 1261 1242 managerLock( manager ); … … 1264 1245 { 1265 1246 tr_dbg( "Banned IP address \"%s\" tried to connect to us", 1266 inet_ntoa( *addr ) );1247 tr_ntop_non_ts( addr ) ); 1267 1248 tr_netClose( socket ); 1268 1249 } … … 1301 1282 1302 1283 t = getExistingTorrent( manager, torrentHash ); 1303 if( !tr_sessionIsAddressBlocked( t->manager->session, &pex-> in_addr ) )1304 ensureAtomExists( t, &pex-> in_addr, pex->port, pex->flags, from );1284 if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->addr ) ) 1285 ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from ); 1305 1286 1306 1287 managerUnlock( manager ); … … 1321 1302 for( i = 0; i < n; ++i ) 1322 1303 { 1323 memcpy( &pex[i].in_addr, walk, 4 ); walk += 4; 1304 pex[i].addr.type = TR_AF_INET; 1305 memcpy( &pex[i].addr.addr, walk, 4 ); walk += 4; 1324 1306 memcpy( &pex[i].port, walk, 2 ); walk += 2; 1325 1307 if( added_f && ( n == added_f_len ) ) … … 1358 1340 t, 1359 1341 "peer %s contributed to corrupt piece (%d); now has %d strikes", 1360 tr_peerIoAddrStr( &peer-> in_addr, peer->port ),1342 tr_peerIoAddrStr( &peer->addr, peer->port ), 1361 1343 pieceIndex, (int)peer->strikes + 1 ); 1362 1344 addStrike( t, peer ); … … 1372 1354 const tr_pex * a = va; 1373 1355 const tr_pex * b = vb; 1374 int i = 1375 memcmp( &a->in_addr, &b->in_addr, sizeof( struct in_addr ) ); 1356 int i = tr_compareAddresses( &a->addr, &b->addr ); 1376 1357 1377 1358 if( i ) return i; … … 1421 1402 { 1422 1403 const tr_peer * peer = peers[i]; 1423 walk-> in_addr = peer->in_addr;1404 walk->addr = peer->addr; 1424 1405 walk->port = peer->port; 1425 1406 walk->flags = 0; … … 1658 1639 { 1659 1640 const tr_peer * peer = peers[i]; 1660 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1641 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1661 1642 1662 1643 if( peer->io == NULL ) /* not connected */ … … 1746 1727 char * pch; 1747 1728 const tr_peer * peer = peers[i]; 1748 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1729 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1749 1730 tr_peer_stat * stat = ret + i; 1750 1731 1751 tr_n etNtop( &peer->in_addr, stat->addr, sizeof( stat->addr ) );1732 tr_ntop( &peer->addr, stat->addr, sizeof( stat->addr ) ); 1752 1733 tr_strlcpy( stat->client, ( peer->client ? peer->client : "" ), 1753 1734 sizeof( stat->client ) ); … … 1945 1926 const tr_torrent * tor = t->tor; 1946 1927 const time_t now = time( NULL ); 1947 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1928 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1948 1929 1949 1930 /* if it's marked for purging, close it */ … … 2207 2188 { 2208 2189 tr_peer * peer = connections[i]; 2209 struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );2190 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 2210 2191 if( peer->pieceDataActivityDate ) 2211 2192 atom->numFails = 0; -
trunk/libtransmission/peer-mgr.h
r7224 r7231 22 22 #ifdef WIN32 23 23 #include <winsock2.h> /* struct in_addr */ 24 #else25 #include <netinet/in.h> /* struct in_addr */26 24 #endif 27 25 28 struct in_addr; 26 #include "net.h" 27 29 28 struct tr_handle; 30 29 struct tr_peer_stat; … … 43 42 typedef struct tr_pex 44 43 { 45 struct in_addr in_addr;46 tr_port 47 uint8_t 44 tr_address addr; 45 tr_port port; 46 uint8_t flags; 48 47 } 49 48 tr_pex; … … 55 54 void tr_peerMgrFree( tr_peerMgr * manager ); 56 55 57 int tr_peerMgrPeerIsSeed( const tr_peerMgr 58 const uint8_t 59 const struct in_addr* addr );56 int tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, 57 const uint8_t * torrentHash, 58 const tr_address * addr ); 60 59 61 void tr_peerMgrAddIncoming( tr_peerMgr 62 struct in_addr* addr,63 tr_port 64 int 60 void tr_peerMgrAddIncoming( tr_peerMgr * manager, 61 tr_address * addr, 62 tr_port port, 63 int socket ); 65 64 66 65 tr_pex * tr_peerMgrCompactToPex( const void * compact, -
trunk/libtransmission/peer-msgs.c
r7227 r7231 1765 1765 } 1766 1766 1767 /* TODO: ipv6 pex */ 1767 1768 static void 1768 1769 sendPex( tr_peermsgs * msgs ) … … 1816 1817 tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); 1817 1818 for( i = 0; i < diffs.addedCount; ++i ) { 1818 memcpy( walk, &diffs.added[i]. in_addr, 4 ); walk += 4;1819 memcpy( walk, &diffs.added[i].addr.addr, 4 ); walk += 4; 1819 1820 memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; 1820 1821 } … … 1834 1835 tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); 1835 1836 for( i = 0; i < diffs.droppedCount; ++i ) { 1836 memcpy( walk, &diffs.dropped[i]. in_addr, 4 ); walk += 4;1837 memcpy( walk, &diffs.dropped[i].addr.addr, 4 ); walk += 4; 1837 1838 memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; 1838 1839 } -
trunk/libtransmission/port-forwarding.c
r7224 r7231 115 115 int socket; 116 116 errno = 0; 117 socket = tr_netBindTCP( s->publicPort );117 socket = tr_netBindTCP( &tr_inaddr_any, s->publicPort ); 118 118 if( socket >= 0 ) 119 119 { … … 139 139 for( ; ; ) /* check for new incoming peer connections */ 140 140 { 141 int 142 tr_port 143 struct in_addraddr;141 int socket; 142 tr_port port; 143 tr_address addr; 144 144 145 145 if( s->bindSocket < 0 ) -
trunk/libtransmission/resume.c
r7223 r7231 62 62 ***/ 63 63 64 /* TODO: resume peers6 */ 64 65 static void 65 66 savePeers( tr_benc * dict, -
trunk/libtransmission/session.c
r7224 r7231 800 800 801 801 int 802 tr_sessionIsAddressBlocked( const tr_session * 803 const struct in_addr* addr )802 tr_sessionIsAddressBlocked( const tr_session * session, 803 const tr_address * addr ) 804 804 { 805 805 tr_list * l; -
trunk/libtransmission/session.h
r7223 r7231 53 53 }; 54 54 55 struct tr_address; 55 56 struct tr_bandwidth; 56 57 … … 122 123 const char * filename ); 123 124 124 struct in_addr; 125 126 int tr_sessionIsAddressBlocked( const tr_session * session, 127 const struct in_addr * addr ); 125 int tr_sessionIsAddressBlocked( const tr_session * session, 126 const struct tr_address * addr ); 128 127 129 128 -
trunk/libtransmission/tracker.c
r7224 r7231 281 281 size_t * byteCount ) 282 282 { 283 /* TODO: IPv6 wtf */ 283 284 int i; 284 285 uint8_t * compact, *walk; … … 291 292 for( i = 0, walk = compact; i < peerCount; ++i ) 292 293 { 293 const char * s; 294 int64_t itmp; 295 struct in_addr addr; 296 tr_port port; 297 tr_benc * peer = &bePeers->val.l.vals[i]; 298 299 if( !tr_bencDictFindStr( peer, "ip", 300 &s ) || tr_netResolve( s, &addr ) ) 301 continue; 302 303 memcpy( walk, &addr, 4 ); 294 const char * s; 295 int64_t itmp; 296 tr_address addr; 297 tr_port port; 298 tr_benc * peer = &bePeers->val.l.vals[i]; 299 300 if( tr_bencDictFindStr( peer, "ip", &s ) ) 301 { 302 if( tr_pton( s, &addr ) == NULL ) 303 continue; 304 if( addr.type != TR_AF_INET ) 305 continue; 306 } 307 308 memcpy( walk, &addr.addr.addr4.s_addr, 4 ); 304 309 walk += 4; 305 310 -
trunk/libtransmission/transmission.h
r7224 r7231 50 50 #endif 51 51 #include <time.h> /* time_t */ 52 #include <netinet/in.h> /* INET6_ADDRSTRLEN */ 52 53 53 54 #define SHA_DIGEST_LENGTH 20 … … 1063 1064 tr_port port; 1064 1065 1065 char addr[ 16];1066 char addr[INET6_ADDRSTRLEN]; 1066 1067 char client[80]; 1067 1068 char flagStr[32];
Note: See TracChangeset
for help on using the changeset viewer.