Changeset 7223
- Timestamp:
- Dec 1, 2008, 8:21:06 PM (12 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist-test.c
r7195 r7223 52 52 char * tmpfile_bin = "transmission-blocklist-test.bin"; 53 53 #endif 54 struct tr_addressaddr;54 struct in_addr addr; 55 55 int test = 0; 56 56 tr_blocklist * b; … … 64 64 65 65 /* now run some tests */ 66 check( tr_pton( "216.16.1.143", &addr ) );66 check( !tr_netResolve( "216.16.1.143", &addr ) ); 67 67 check( !_tr_blocklistHasAddress( b, &addr ) ); 68 check( tr_pton( "216.16.1.144", &addr ) );68 check( !tr_netResolve( "216.16.1.144", &addr ) ); 69 69 check( _tr_blocklistHasAddress( b, &addr ) ); 70 check( tr_pton( "216.16.1.145", &addr ) );70 check( !tr_netResolve( "216.16.1.145", &addr ) ); 71 71 check( _tr_blocklistHasAddress( b, &addr ) ); 72 check( tr_pton( "216.16.1.146", &addr ) );72 check( !tr_netResolve( "216.16.1.146", &addr ) ); 73 73 check( _tr_blocklistHasAddress( b, &addr ) ); 74 check( tr_pton( "216.16.1.147", &addr ) );74 check( !tr_netResolve( "216.16.1.147", &addr ) ); 75 75 check( _tr_blocklistHasAddress( b, &addr ) ); 76 check( tr_pton( "216.16.1.148", &addr ) );76 check( !tr_netResolve( "216.16.1.148", &addr ) ); 77 77 check( _tr_blocklistHasAddress( b, &addr ) ); 78 check( tr_pton( "216.16.1.149", &addr ) );78 check( !tr_netResolve( "216.16.1.149", &addr ) ); 79 79 check( _tr_blocklistHasAddress( b, &addr ) ); 80 check( tr_pton( "216.16.1.150", &addr ) );80 check( !tr_netResolve( "216.16.1.150", &addr ) ); 81 81 check( _tr_blocklistHasAddress( b, &addr ) ); 82 check( tr_pton( "216.16.1.151", &addr ) );82 check( !tr_netResolve( "216.16.1.151", &addr ) ); 83 83 check( _tr_blocklistHasAddress( b, &addr ) ); 84 check( tr_pton( "216.16.1.152", &addr ) );84 check( !tr_netResolve( "216.16.1.152", &addr ) ); 85 85 check( !_tr_blocklistHasAddress( b, &addr ) ); 86 check( tr_pton( "216.16.1.153", &addr ) );86 check( !tr_netResolve( "216.16.1.153", &addr ) ); 87 87 check( !_tr_blocklistHasAddress( b, &addr ) ); 88 check( tr_pton( "217.0.0.1", &addr ) );88 check( !tr_netResolve( "217.0.0.1", &addr ) ); 89 89 check( !_tr_blocklistHasAddress( b, &addr ) ); 90 check( tr_pton( "255.0.0.1", &addr ) );90 check( !tr_netResolve( "255.0.0.1", &addr ) ); 91 91 92 92 /* cleanup */ -
trunk/libtransmission/blocklist.c
r7195 r7223 32 32 #include "platform.h" 33 33 #include "blocklist.h" 34 #include "net.h" 34 #include "net.h" /* tr_netResolve() */ 35 35 #include "utils.h" 36 36 … … 199 199 200 200 int 201 _tr_blocklistHasAddress( tr_blocklist *b,202 const tr_address* addr )201 _tr_blocklistHasAddress( tr_blocklist * b, 202 const struct in_addr * addr ) 203 203 { 204 204 uint32_t needle; … … 212 212 return 0; 213 213 214 needle = ntohl( addr-> addr.addr4.s_addr );214 needle = ntohl( addr->s_addr ); 215 215 216 216 range = bsearch( &needle, … … 260 260 char * rangeBegin; 261 261 char * rangeEnd; 262 tr_addressaddr;262 struct in_addr in_addr; 263 263 struct tr_ip_range range; 264 264 … … 271 271 *rangeEnd++ = '\0'; 272 272 273 if( !tr_pton( rangeBegin, &addr ) )273 if( tr_netResolve( rangeBegin, &in_addr ) ) 274 274 tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin ); 275 range.begin = ntohl( addr.addr.addr4.s_addr );276 277 if( !tr_pton( rangeEnd, &addr ) )275 range.begin = ntohl( in_addr.s_addr ); 276 277 if( tr_netResolve( rangeEnd, &in_addr ) ) 278 278 tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd ); 279 range.end = ntohl( addr.addr.addr4.s_addr );279 range.end = ntohl( in_addr.s_addr ); 280 280 281 281 free( line ); -
trunk/libtransmission/blocklist.h
r7195 r7223 18 18 #define TR_BLOCKLIST_H 19 19 20 struct tr_address; 21 20 struct in_addr; 22 21 typedef struct tr_blocklist tr_blocklist; 23 22 … … 35 34 int _tr_blocklistIsEnabled( tr_blocklist * ); 36 35 37 void _tr_blocklistSetEnabled( tr_blocklist *,38 int isEnabled );36 void _tr_blocklistSetEnabled( tr_blocklist *, 37 int isEnabled ); 39 38 40 int _tr_blocklistHasAddress( tr_blocklist *, 41 const tr_address * addr ); 39 int _tr_blocklistHasAddress( 40 tr_blocklist *, 41 const struct 42 in_addr * addr ); 42 43 43 int _tr_blocklistSetContent( tr_blocklist *, 44 const char * filename ); 44 int _tr_blocklistSetContent( 45 tr_blocklist *, 46 const char * filename ); 45 47 46 48 #endif -
trunk/libtransmission/fdlimit.c
r7195 r7223 472 472 473 473 int 474 tr_fdSocketAccept( int b,475 tr_address* addr,476 tr_port_t *port )477 { 478 int 479 unsigned int 480 struct sockaddr_ storagesock;474 tr_fdSocketAccept( int b, 475 struct in_addr * addr, 476 tr_port_t * port ) 477 { 478 int s = -1; 479 unsigned int len; 480 struct sockaddr_in sock; 481 481 482 482 assert( addr ); … … 486 486 if( gFd->socketCount < getSocketMax( gFd ) ) 487 487 { 488 len = sizeof( s truct sockaddr);488 len = sizeof( sock ); 489 489 s = accept( b, (struct sockaddr *) &sock, &len ); 490 490 } 491 491 if( s > -1 ) 492 492 { 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 } 493 *addr = sock.sin_addr; 494 *port = sock.sin_port; 510 495 ++gFd->socketCount; 511 496 } -
trunk/libtransmission/fdlimit.h
r7195 r7223 88 88 int tr_fdSocketCreate( int type ); 89 89 90 int tr_fdSocketAccept( int b,91 tr_address* addr,92 tr_port_t *port );90 int tr_fdSocketAccept( int b, 91 struct in_addr * addr, 92 tr_port_t * port ); 93 93 94 94 void tr_fdSocketClose( int s ); -
trunk/libtransmission/handshake.c
r7195 r7223 1197 1197 } 1198 1198 1199 const tr_address*1199 const struct in_addr * 1200 1200 tr_handshakeGetAddr( const struct tr_handshake * handshake, 1201 uint16_t* port )1201 uint16_t * port ) 1202 1202 { 1203 1203 assert( handshake ); -
trunk/libtransmission/handshake.h
r7195 r7223 20 20 #include "transmission.h" 21 21 22 struct in_addr; 22 23 struct tr_peerIo; 23 24 typedef struct tr_handshake tr_handshake; … … 35 36 void * doneUserData ); 36 37 37 const tr_address * tr_handshakeGetAddr( const struct tr_handshake * handshake, 38 uint16_t * setme_port ); 38 const struct in_addr * tr_handshakeGetAddr( 39 const struct tr_handshake * handshake, 40 uint16_t 41 * setme_port ); 39 42 40 43 void tr_handshakeAbort( tr_handshake * handshake ); -
trunk/libtransmission/net.c
r7195 r7223 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 void … … 67 64 } 68 65 69 const char * 70 tr_ntop( const tr_address * src, 71 char * dst, 72 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, 94 tr_address * dst ) 95 { 96 int retval = inet_pton( AF_INET, src, &dst->addr ); 97 if( retval < 0 ) 98 return NULL; 99 else if( retval == 0 ) 100 retval = inet_pton( AF_INET6, src, &dst->addr ); 101 else 102 { 103 dst->type = TR_AF_INET; 104 return dst; 105 } 106 107 if( retval < 1 ) 108 return NULL; 109 dst->type = TR_AF_INET6; 110 return dst; 111 } 112 113 /* 114 * Compare two tr_address structures. 115 * Returns: 116 * <0 if a < b 117 * >0 if a > b 118 * 0 if a == b 119 */ 120 int 121 tr_compareAddresses( const tr_address * a, 122 const tr_address * b) 123 { 124 int retval; 125 int addrlen; 126 /* IPv6 addresses are always "greater than" IPv4 */ 127 if( a->type == TR_AF_INET && b->type == TR_AF_INET6 ) 128 return 1; 129 if( a->type == TR_AF_INET6 && b->type == TR_AF_INET ) 130 return -1; 131 132 if( a->type == TR_AF_INET ) 133 addrlen = sizeof( struct in_addr ); 134 else 135 addrlen = sizeof( struct in6_addr ); 136 137 retval = memcmp( &a->addr, &b->addr, addrlen ); 138 if( retval == 0 ) 139 return 0; 140 141 return retval; 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; 142 79 } 143 80 … … 200 137 } 201 138 202 static void 203 setup_sockaddr( const tr_address * addr, 204 tr_port_t port, 205 struct sockaddr_storage * sockaddr) 206 { 207 struct sockaddr_in sock4; 208 struct sockaddr_in6 sock6; 209 if( addr->type == TR_AF_INET ) 210 { 211 memset( &sock4, 0, sizeof( sock4 ) ); 212 sock4.sin_family = AF_INET; 213 sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; 214 sock4.sin_port = port; 215 memcpy( sockaddr, &sock4, sizeof( sock4 ) ); 216 } 217 else 218 { 219 memset( &sock6, 0, sizeof( sock6 ) ); 220 sock6.sin6_family = AF_INET6; 221 sock6.sin6_port = port; 222 memcpy( &sock6.sin6_addr, &addr->addr, sizeof( struct in6_addr ) ); 223 memcpy( sockaddr, &sock6, sizeof( sock6 ) ); 224 } 225 } 226 227 int 228 tr_netOpenTCP( tr_session * session, 229 const tr_address * addr, 230 tr_port_t port ) 231 { 232 int s; 233 struct sockaddr_storage sock; 234 const int type = SOCK_STREAM; 139 int 140 tr_netOpenTCP( tr_session * session, 141 const struct in_addr * addr, 142 tr_port_t port ) 143 { 144 int s; 145 struct sockaddr_in sock; 146 const int type = SOCK_STREAM; 235 147 236 148 if( ( s = createSocket( type ) ) < 0 ) … … 239 151 setSndBuf( session, s ); 240 152 241 setup_sockaddr( addr, port, &sock ); 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; 242 157 243 158 if( ( connect( s, (struct sockaddr *) &sock, 244 sizeof( struct sockaddr ) ) < 0 )159 sizeof( struct sockaddr_in ) ) < 0 ) 245 160 #ifdef WIN32 246 161 && ( sockerrno != WSAEWOULDBLOCK ) … … 250 165 tr_err( _( 251 166 "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ), 252 s, tr_ntop_non_ts(addr ), port,167 s, inet_ntoa( *addr ), port, 253 168 sockerrno, tr_strerror( sockerrno ) ); 254 169 tr_netClose( s ); … … 263 178 264 179 int 265 tr_netBindTCP( const tr_address * addr, 266 int port ) 267 { 268 int s; 269 struct sockaddr_storage sock; 270 const int type = SOCK_STREAM; 180 tr_netBindTCP( int port ) 181 { 182 int s; 183 struct sockaddr_in sock; 184 const int type = SOCK_STREAM; 271 185 272 186 #if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) … … 281 195 setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) ); 282 196 #endif 283 284 setup_sockaddr( addr, htons( port ), &sock ); 197 198 memset( &sock, 0, sizeof( sock ) ); 199 sock.sin_family = AF_INET; 200 sock.sin_addr.s_addr = INADDR_ANY; 201 sock.sin_port = htons( port ); 285 202 286 203 if( bind( s, (struct sockaddr *) &sock, 287 sizeof( struct sockaddr ) ) )288 { 289 tr_err( _( "Couldn't bind port %d on %s: %s" ), port,290 tr_ ntop_non_ts( addr ), tr_strerror( sockerrno ) );204 sizeof( struct sockaddr_in ) ) ) 205 { 206 tr_err( _( "Couldn't bind port %d: %s" ), port, 207 tr_strerror( sockerrno ) ); 291 208 tr_netClose( s ); 292 209 return -1; 293 210 } 294 211 295 tr_dbg( "Bound socket %d to port %d on %s", s, port, 296 tr_ntop_non_ts( addr ) ); 212 tr_dbg( "Bound socket %d to port %d", s, port ); 297 213 return s; 298 214 } 299 215 300 216 int 301 tr_netAccept( tr_session * session,302 int b,303 tr_address* addr,304 tr_port_t * port )217 tr_netAccept( tr_session * session, 218 int b, 219 struct in_addr * addr, 220 tr_port_t * port ) 305 221 { 306 222 int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) ); … … 314 230 tr_fdSocketClose( s ); 315 231 } 232 233 void 234 tr_netNtop( const struct in_addr * addr, 235 char * buf, 236 int len ) 237 { 238 const uint8_t * cast; 239 240 cast = (const uint8_t *)addr; 241 tr_snprintf( buf, len, "%hhu.%hhu.%hhu.%hhu", 242 cast[0], cast[1], cast[2], cast[3] ); 243 } 244 -
trunk/libtransmission/net.h
r7195 r7223 61 61 #endif 62 62 63 struct in_addr; 64 struct sockaddr_in; 63 65 struct tr_session; 64 66 65 #define TR_AF_INET 0 66 #define TR_AF_INET6 1 67 68 typedef struct tr_address { 69 unsigned short type : 1; 70 union { 71 /* The order here is important for tr_in{,6}addr_any initialization, 72 * since we can't use C99 designated initializers */ 73 struct in6_addr addr6; 74 struct in_addr addr4; 75 } addr; 76 } tr_address; 77 78 extern const tr_address tr_inaddr_any; 79 extern const tr_address tr_in6addr_any; 80 81 const char *tr_ntop( const tr_address * src, 82 char * dst, 83 int size ); 84 const char *tr_ntop_non_ts( const tr_address * src ); 85 tr_address *tr_pton( const char * src, 86 tr_address * dst ); 87 int tr_compareAddresses( const tr_address * a, 88 const tr_address * b); 67 /*********************************************************************** 68 * DNS resolution 69 **********************************************************************/ 70 int tr_netResolve( const char *, 71 struct in_addr * ); 89 72 90 73 … … 92 75 * Sockets 93 76 **********************************************************************/ 94 int tr_netOpenTCP( struct tr_handle * session,95 const tr_address* addr,96 tr_port_t port );77 int tr_netOpenTCP( struct tr_handle * session, 78 const struct in_addr * addr, 79 tr_port_t port ); 97 80 98 int tr_netBindTCP( const tr_address * addr, 99 int port ); 81 int tr_netBindTCP( int port ); 100 82 101 int tr_netAccept( struct tr_handle * session,102 int bound,103 tr_address* setme_addr,104 tr_port_t * setme_port );83 int tr_netAccept( struct tr_handle * session, 84 int bound, 85 struct in_addr * setme_addr, 86 tr_port_t * setme_port ); 105 87 106 88 int tr_netSetTOS( int s, … … 109 91 void tr_netClose( int s ); 110 92 93 void tr_netNtop( const struct in_addr * addr, 94 char * buf, 95 int len ); 96 111 97 void tr_netInit( void ); 112 98 -
trunk/libtransmission/peer-io.c
r7195 r7223 20 20 #include <winsock2.h> 21 21 #else 22 #include <netinet/in.h> /* struct in_addr */ 22 23 #include <arpa/inet.h> /* inet_ntoa */ 23 24 #endif … … 97 98 tr_session * session; 98 99 99 tr_addressaddr;100 struct in_addr in_addr; 100 101 struct tr_iobuf * iobuf; 101 102 tr_list * output_datatypes; /* struct tr_datatype */ … … 238 239 239 240 static tr_peerIo* 240 tr_peerIoNew( tr_session *session,241 const tr_address *addr,242 uint16_t port,243 const uint8_t * torrentHash,244 int isIncoming,245 int socket )241 tr_peerIoNew( tr_session * session, 242 const struct in_addr * in_addr, 243 uint16_t port, 244 const uint8_t * torrentHash, 245 int isIncoming, 246 int socket ) 246 247 { 247 248 tr_peerIo * io; … … 254 255 io->crypto = tr_cryptoNew( torrentHash, isIncoming ); 255 256 io->session = session; 256 io-> addr = *addr;257 io->in_addr = *in_addr; 257 258 io->port = port; 258 259 io->socket = socket; … … 266 267 267 268 tr_peerIo* 268 tr_peerIoNewIncoming( tr_session *session,269 const tr_address *addr,270 uint16_t port,271 int socket )269 tr_peerIoNewIncoming( tr_session * session, 270 const struct in_addr * in_addr, 271 uint16_t port, 272 int socket ) 272 273 { 273 274 assert( session ); 274 assert( addr );275 assert( in_addr ); 275 276 assert( socket >= 0 ); 276 277 277 return tr_peerIoNew( session, addr, port,278 return tr_peerIoNew( session, in_addr, port, 278 279 NULL, 1, 279 280 socket ); … … 281 282 282 283 tr_peerIo* 283 tr_peerIoNewOutgoing( tr_session *session,284 const tr_address *addr,285 int port,286 const uint8_t *torrentHash )284 tr_peerIoNewOutgoing( tr_session * session, 285 const struct in_addr * in_addr, 286 int port, 287 const uint8_t * torrentHash ) 287 288 { 288 289 int socket; 289 290 290 291 assert( session ); 291 assert( addr );292 assert( in_addr ); 292 293 assert( port >= 0 ); 293 294 assert( torrentHash ); 294 295 295 socket = tr_netOpenTCP( session, addr, port );296 socket = tr_netOpenTCP( session, in_addr, port ); 296 297 297 298 return socket < 0 298 299 ? NULL 299 : tr_peerIoNew( session, addr, port, torrentHash, 0, socket );300 : tr_peerIoNew( session, in_addr, port, torrentHash, 0, socket ); 300 301 } 301 302 … … 336 337 } 337 338 338 const tr_address*339 const struct in_addr* 339 340 tr_peerIoGetAddress( const tr_peerIo * io, 340 uint16_t 341 uint16_t * port ) 341 342 { 342 343 assert( isPeerIo( io ) ); … … 345 346 *port = io->port; 346 347 347 return &io-> addr;348 return &io->in_addr; 348 349 } 349 350 350 351 const char* 351 tr_peerIoAddrStr( const tr_address* addr,352 uint16_t port )352 tr_peerIoAddrStr( const struct in_addr * addr, 353 uint16_t port ) 353 354 { 354 355 static char buf[512]; 355 356 356 if( addr->type == TR_AF_INET ) 357 tr_snprintf( buf, sizeof( buf ), "%s:%u", tr_ntop_non_ts( addr ), 358 ntohs( port ) ); 359 else 360 tr_snprintf( buf, sizeof( buf ), "[%s]:%u", tr_ntop_non_ts( addr ), 361 ntohs( port ) ); 357 tr_snprintf( buf, sizeof( buf ), "%s:%u", inet_ntoa( *addr ), 358 ntohs( port ) ); 362 359 return buf; 363 360 } … … 366 363 tr_peerIoGetAddrStr( const tr_peerIo * io ) 367 364 { 368 return tr_peerIoAddrStr( &io-> addr, io->port );365 return tr_peerIoAddrStr( &io->in_addr, io->port ); 369 366 } 370 367 … … 405 402 tr_netClose( io->socket ); 406 403 407 io->socket = tr_netOpenTCP( io->session, &io-> addr, io->port );404 io->socket = tr_netOpenTCP( io->session, &io->in_addr, io->port ); 408 405 409 406 if( io->socket >= 0 ) -
trunk/libtransmission/peer-io.h
r7195 r7223 22 22 **/ 23 23 24 struct in_addr; 24 25 struct evbuffer; 25 26 struct tr_bandwidth; … … 32 33 **/ 33 34 34 tr_peerIo* tr_peerIoNewOutgoing( struct tr_handle * session,35 const tr_address* addr,36 int port,37 const uint8_t * torrentHash );38 39 tr_peerIo* tr_peerIoNewIncoming( struct tr_handle * session,40 const tr_address* addr,41 uint16_t port,42 int socket );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 uint16_t port, 43 int socket ); 43 44 44 45 void tr_peerIoFree( tr_peerIo * io ); … … 59 60 **/ 60 61 61 const char* tr_peerIoAddrStr( const tr_address* addr,62 uint16_t port );62 const char* tr_peerIoAddrStr( const struct in_addr * addr, 63 uint16_t port ); 63 64 64 65 const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); 65 66 66 const tr_address *tr_peerIoGetAddress( const tr_peerIo * io,67 const struct in_addr*tr_peerIoGetAddress( const tr_peerIo * io, 67 68 uint16_t * port ); 68 69 -
trunk/libtransmission/peer-mgr-private.h
r7195 r7223 22 22 #ifdef WIN32 23 23 #include <winsock2.h> /* struct in_addr */ 24 #else 25 #include <netinet/in.h> /* struct in_addr */ 24 26 #endif 25 27 … … 51 53 uint8_t encryption_preference; 52 54 uint16_t port; 53 tr_addressaddr;55 struct in_addr in_addr; 54 56 struct tr_peerIo * io; 55 57 -
trunk/libtransmission/peer-mgr.c
r7199 r7223 96 96 struct peer_atom 97 97 { 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 uint16_t port;102 uint16_t numFails;103 tr_addressaddr;104 time_t time; /* when the peer's connection status last changed */105 time_t piece_data_time;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 uint16_t port; 102 uint16_t numFails; 103 struct in_addr 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 int 187 197 handshakeCompareToAddr( const void * va, 188 198 const void * vb ) … … 190 200 const tr_handshake * a = va; 191 201 192 return tr_compareAddresses( tr_handshakeGetAddr( a, NULL ), vb );202 return compareAddresses( tr_handshakeGetAddr( a, NULL ), vb ); 193 203 } 194 204 … … 201 211 202 212 static tr_handshake* 203 getExistingHandshake( tr_ptrArray *handshakes,204 const tr_address *addr )213 getExistingHandshake( tr_ptrArray * handshakes, 214 const struct in_addr * in_addr ) 205 215 { 206 216 return tr_ptrArrayFindSorted( handshakes, 207 addr,217 in_addr, 208 218 handshakeCompareToAddr ); 209 219 } … … 215 225 const struct peer_atom * a = va; 216 226 217 return tr_compareAddresses( &a->addr, vb );227 return compareAddresses( &a->addr, vb ); 218 228 } 219 229 … … 267 277 const tr_peer * b = vb; 268 278 269 return tr_compareAddresses( &a->addr, &b->addr );279 return compareAddresses( &a->in_addr, &b->in_addr ); 270 280 } 271 281 … … 276 286 const tr_peer * a = va; 277 287 278 return tr_compareAddresses( &a->addr, vb );288 return compareAddresses( &a->in_addr, vb ); 279 289 } 280 290 281 291 static tr_peer* 282 getExistingPeer( Torrent *torrent,283 const tr_address *addr )292 getExistingPeer( Torrent * torrent, 293 const struct in_addr * in_addr ) 284 294 { 285 295 assert( torrentIsLocked( torrent ) ); 286 assert( addr );296 assert( in_addr ); 287 297 288 298 return tr_ptrArrayFindSorted( torrent->peers, 289 addr,299 in_addr, 290 300 peerCompareToAddr ); 291 301 } 292 302 293 303 static struct peer_atom* 294 getExistingAtom( const Torrent* t,295 const tr_address* addr )304 getExistingAtom( const Torrent * t, 305 const struct in_addr * addr ) 296 306 { 297 307 assert( torrentIsLocked( t ) ); … … 300 310 301 311 static int 302 peerIsInUse( const Torrent *ct,303 const tr_address* addr )312 peerIsInUse( const Torrent * ct, 313 const struct in_addr * addr ) 304 314 { 305 315 Torrent * t = (Torrent*) ct; … … 313 323 314 324 static tr_peer* 315 peerConstructor( tr_torrent * tor, const tr_address *addr )325 peerConstructor( tr_torrent * tor, const struct in_addr * in_addr ) 316 326 { 317 327 tr_peer * p; 318 328 319 329 p = tr_new0( tr_peer, 1 ); 320 memcpy( &p-> addr, addr, sizeof( tr_address) );330 memcpy( &p->in_addr, in_addr, sizeof( struct in_addr ) ); 321 331 p->bandwidth = tr_bandwidthNew( tor->session, tor->bandwidth ); 322 332 return p; … … 324 334 325 335 static tr_peer* 326 getPeer( Torrent *torrent,327 const tr_address *addr )336 getPeer( Torrent * torrent, 337 const struct in_addr * in_addr ) 328 338 { 329 339 tr_peer * peer; … … 331 341 assert( torrentIsLocked( torrent ) ); 332 342 333 peer = getExistingPeer( torrent, addr );343 peer = getExistingPeer( torrent, in_addr ); 334 344 335 345 if( peer == NULL ) 336 346 { 337 peer = peerConstructor( torrent->tor, addr );347 peer = peerConstructor( torrent->tor, in_addr ); 338 348 tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); 339 349 } … … 371 381 assert( torrentIsLocked( t ) ); 372 382 373 atom = getExistingAtom( t, &peer-> addr );383 atom = getExistingAtom( t, &peer->in_addr ); 374 384 assert( atom ); 375 385 atom->time = time( NULL ); … … 521 531 522 532 int 523 tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,524 const uint8_t *torrentHash,525 const tr_address* addr )533 tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, 534 const uint8_t * torrentHash, 535 const struct in_addr * addr ) 526 536 { 527 537 int isSeed = FALSE; … … 896 906 { 897 907 tordbg( t, "increasing peer %s strike count to %d", 898 tr_peerIoAddrStr( &peer-> addr,908 tr_peerIoAddrStr( &peer->in_addr, 899 909 peer->port ), peer->strikes + 1 ); 900 910 901 911 if( ++peer->strikes >= MAX_BAD_PIECES_PER_PEER ) 902 912 { 903 struct peer_atom * atom = getExistingAtom( t, &peer-> addr );913 struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 904 914 atom->myflags |= MYFLAG_BANNED; 905 915 peer->doPurge = 1; … … 966 976 /* update our atom */ 967 977 if( peer ) { 968 struct peer_atom * a = getExistingAtom( t, &peer-> addr );978 struct peer_atom * a = getExistingAtom( t, &peer->in_addr ); 969 979 a->piece_data_time = now; 970 980 } … … 995 1005 /* update our atom */ 996 1006 if( peer ) { 997 struct peer_atom * a = getExistingAtom( t, &peer-> addr );1007 struct peer_atom * a = getExistingAtom( t, &peer->in_addr ); 998 1008 a->piece_data_time = now; 999 1009 } … … 1006 1016 if( peer ) 1007 1017 { 1008 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1018 struct peer_atom * atom = getExistingAtom( t, 1019 &peer->in_addr ); 1009 1020 const int peerIsSeed = e->progress >= 1.0; 1010 1021 if( peerIsSeed ) … … 1101 1112 1102 1113 static void 1103 ensureAtomExists( Torrent *t,1104 const tr_address* addr,1105 uint16_t port,1106 uint8_t flags,1107 uint8_t from )1114 ensureAtomExists( Torrent * t, 1115 const struct in_addr * addr, 1116 uint16_t port, 1117 uint8_t flags, 1118 uint8_t from ) 1108 1119 { 1109 1120 if( getExistingAtom( t, addr ) == NULL ) … … 1142 1153 void * vmanager ) 1143 1154 { 1144 int ok = isConnected;1145 int success = FALSE;1146 uint16_t port;1147 const tr_address* addr;1148 tr_peerMgr * manager = (tr_peerMgr*) vmanager;1149 Torrent * t;1150 tr_handshake * ours;1155 int ok = isConnected; 1156 int success = FALSE; 1157 uint16_t port; 1158 const struct in_addr * addr; 1159 tr_peerMgr * manager = (tr_peerMgr*) vmanager; 1160 Torrent * t; 1161 tr_handshake * ours; 1151 1162 1152 1163 assert( io ); … … 1243 1254 1244 1255 void 1245 tr_peerMgrAddIncoming( tr_peerMgr * manager,1246 tr_address* addr,1247 uint16_t port,1248 int socket )1256 tr_peerMgrAddIncoming( tr_peerMgr * manager, 1257 struct in_addr * addr, 1258 uint16_t port, 1259 int socket ) 1249 1260 { 1250 1261 managerLock( manager ); … … 1253 1264 { 1254 1265 tr_dbg( "Banned IP address \"%s\" tried to connect to us", 1255 tr_ntop_non_ts(addr ) );1266 inet_ntoa( *addr ) ); 1256 1267 tr_netClose( socket ); 1257 1268 } … … 1290 1301 1291 1302 t = getExistingTorrent( manager, torrentHash ); 1292 if( !tr_sessionIsAddressBlocked( t->manager->session, &pex-> addr ) )1293 ensureAtomExists( t, &pex-> addr, pex->port, pex->flags, from );1303 if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->in_addr ) ) 1304 ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from ); 1294 1305 1295 1306 managerUnlock( manager ); … … 1310 1321 for( i = 0; i < n; ++i ) 1311 1322 { 1312 pex[i].addr.type = TR_AF_INET; 1313 memcpy( &pex[i].addr.addr, walk, 4 ); walk += 4; 1323 memcpy( &pex[i].in_addr, walk, 4 ); walk += 4; 1314 1324 memcpy( &pex[i].port, walk, 2 ); walk += 2; 1315 1325 if( added_f && ( n == added_f_len ) ) … … 1348 1358 t, 1349 1359 "peer %s contributed to corrupt piece (%d); now has %d strikes", 1350 tr_peerIoAddrStr( &peer-> addr, peer->port ),1360 tr_peerIoAddrStr( &peer->in_addr, peer->port ), 1351 1361 pieceIndex, (int)peer->strikes + 1 ); 1352 1362 addStrike( t, peer ); … … 1362 1372 const tr_pex * a = va; 1363 1373 const tr_pex * b = vb; 1364 int i = tr_compareAddresses( &a->addr, &b->addr ); 1374 int i = 1375 memcmp( &a->in_addr, &b->in_addr, sizeof( struct in_addr ) ); 1365 1376 1366 1377 if( i ) return i; … … 1410 1421 { 1411 1422 const tr_peer * peer = peers[i]; 1412 walk-> addr = peer->addr;1423 walk->in_addr = peer->in_addr; 1413 1424 walk->port = peer->port; 1414 1425 walk->flags = 0; … … 1647 1658 { 1648 1659 const tr_peer * peer = peers[i]; 1649 const struct peer_atom * atom = getExistingAtom( t, &peer-> addr );1660 const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 1650 1661 1651 1662 if( peer->io == NULL ) /* not connected */ … … 1735 1746 char * pch; 1736 1747 const tr_peer * peer = peers[i]; 1737 const struct peer_atom * atom = getExistingAtom( t, &peer-> addr );1748 const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 1738 1749 tr_peer_stat * stat = ret + i; 1739 1750 1740 tr_n top( &peer->addr, stat->addr, sizeof( stat->addr ) );1751 tr_netNtop( &peer->in_addr, stat->addr, sizeof( stat->addr ) ); 1741 1752 tr_strlcpy( stat->client, ( peer->client ? peer->client : "" ), 1742 1753 sizeof( stat->client ) ); … … 1934 1945 const tr_torrent * tor = t->tor; 1935 1946 const time_t now = time( NULL ); 1936 const struct peer_atom * atom = getExistingAtom( t, &peer-> addr );1947 const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 1937 1948 1938 1949 /* if it's marked for purging, close it */ … … 2196 2207 { 2197 2208 tr_peer * peer = connections[i]; 2198 struct peer_atom * atom = getExistingAtom( t, &peer-> addr );2209 struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 2199 2210 if( peer->pieceDataActivityDate ) 2200 2211 atom->numFails = 0; -
trunk/libtransmission/peer-mgr.h
r7195 r7223 22 22 #ifdef WIN32 23 23 #include <winsock2.h> /* struct in_addr */ 24 #else 25 #include <netinet/in.h> /* struct in_addr */ 24 26 #endif 25 27 26 #include "net.h" 27 28 struct in_addr; 28 29 struct tr_handle; 29 30 struct tr_peer_stat; … … 42 43 typedef struct tr_pex 43 44 { 44 tr_addressaddr;45 uint16_t port;46 uint8_t flags;45 struct in_addr in_addr; 46 uint16_t port; 47 uint8_t flags; 47 48 } 48 49 tr_pex; … … 54 55 void tr_peerMgrFree( tr_peerMgr * manager ); 55 56 56 int tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,57 const uint8_t * torrentHash,58 const tr_address* addr );57 int tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, 58 const uint8_t * torrentHash, 59 const struct in_addr * addr ); 59 60 60 void tr_peerMgrAddIncoming( tr_peerMgr * manager,61 tr_address* addr,62 uint16_t port,63 int socket );61 void tr_peerMgrAddIncoming( tr_peerMgr * manager, 62 struct in_addr * addr, 63 uint16_t port, 64 int socket ); 64 65 65 66 tr_pex * tr_peerMgrCompactToPex( const void * compact, -
trunk/libtransmission/peer-msgs.c
r7219 r7223 1764 1764 } 1765 1765 1766 /* TODO: ipv6 pex */1767 1766 static void 1768 1767 sendPex( tr_peermsgs * msgs ) … … 1816 1815 tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); 1817 1816 for( i = 0; i < diffs.addedCount; ++i ) { 1818 memcpy( walk, &diffs.added[i]. addr.addr, 4 ); walk += 4;1817 memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4; 1819 1818 memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; 1820 1819 } … … 1834 1833 tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); 1835 1834 for( i = 0; i < diffs.droppedCount; ++i ) { 1836 memcpy( walk, &diffs.dropped[i]. addr.addr, 4 ); walk += 4;1835 memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4; 1837 1836 memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; 1838 1837 } -
trunk/libtransmission/port-forwarding.c
r7195 r7223 115 115 int socket; 116 116 errno = 0; 117 /* TODO: this is where we want to listen on another socket */ 118 socket = tr_netBindTCP( &tr_inaddr_any, s->publicPort ); 117 socket = tr_netBindTCP( s->publicPort ); 119 118 if( socket >= 0 ) 120 119 { … … 140 139 for( ; ; ) /* check for new incoming peer connections */ 141 140 { 142 int socket;143 uint16_t port;144 tr_addressaddr;141 int socket; 142 uint16_t port; 143 struct in_addr addr; 145 144 146 145 if( s->bindSocket < 0 ) -
trunk/libtransmission/resume.c
r7195 r7223 62 62 ***/ 63 63 64 /* TODO: fast resume is done with pex */65 64 static void 66 65 savePeers( tr_benc * dict, -
trunk/libtransmission/session.c
r7195 r7223 800 800 801 801 int 802 tr_sessionIsAddressBlocked( const tr_session * session,803 const tr_address* addr )802 tr_sessionIsAddressBlocked( const tr_session * session, 803 const struct in_addr * addr ) 804 804 { 805 805 tr_list * l; -
trunk/libtransmission/session.h
r7195 r7223 39 39 #endif 40 40 #endif 41 42 #include "net.h"43 41 44 42 … … 124 122 const char * filename ); 125 123 126 int tr_sessionIsAddressBlocked( const tr_session * session, 127 const tr_address * addr ); 124 struct in_addr; 125 126 int tr_sessionIsAddressBlocked( const tr_session * session, 127 const struct in_addr * addr ); 128 128 129 129 -
trunk/libtransmission/tracker.c
r7195 r7223 281 281 size_t * byteCount ) 282 282 { 283 /* TODO: wtf */284 283 int i; 285 284 uint8_t * compact, *walk; … … 292 291 for( i = 0, walk = compact; i < peerCount; ++i ) 293 292 { 294 const char * s; 295 int64_t itmp; 296 tr_address addr; 297 tr_port_t 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 ); 293 const char * s; 294 int64_t itmp; 295 struct in_addr addr; 296 tr_port_t 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 ); 309 304 walk += 4; 310 305 -
trunk/libtransmission/transmission.h
r7199 r7223 50 50 #endif 51 51 #include <time.h> /* time_t */ 52 #include <netinet/in.h> /* INET6_ADDRSTRLEN */53 52 54 53 #define SHA_DIGEST_LENGTH 20 … … 1063 1062 uint16_t port; 1064 1063 1065 char addr[ INET6_ADDRSTRLEN];1064 char addr[16]; 1066 1065 char client[80]; 1067 1066 char flagStr[32];
Note: See TracChangeset
for help on using the changeset viewer.