Changeset 7195
- Timestamp:
- Nov 30, 2008, 12:47:18 AM (12 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist-test.c
r6795 r7195 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
r7173 r7195 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 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 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
r7151 r7195 18 18 #define TR_BLOCKLIST_H 19 19 20 struct in_addr; 20 struct tr_address; 21 21 22 typedef struct tr_blocklist tr_blocklist; 22 23 … … 34 35 int _tr_blocklistIsEnabled( tr_blocklist * ); 35 36 36 void _tr_blocklistSetEnabled( tr_blocklist *,37 37 void _tr_blocklistSetEnabled( tr_blocklist * , 38 int isEnabled ); 38 39 39 int _tr_blocklistHasAddress( 40 tr_blocklist *, 41 const struct 42 in_addr * addr ); 40 int _tr_blocklistHasAddress( tr_blocklist *, 41 const tr_address * addr ); 43 42 44 int _tr_blocklistSetContent( 45 tr_blocklist *, 46 const char * filename ); 43 int _tr_blocklistSetContent( tr_blocklist *, 44 const char * filename ); 47 45 48 46 #endif -
trunk/libtransmission/fdlimit.c
r7173 r7195 472 472 473 473 int 474 tr_fdSocketAccept( int 475 struct in_addr* addr,476 tr_port_t *port )477 { 478 int s = -1;479 unsigned int len;480 struct sockaddr_ insock;474 tr_fdSocketAccept( int b, 475 tr_address * addr, 476 tr_port_t * port ) 477 { 478 int s = -1; 479 unsigned int len; 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
r7151 r7195 88 88 int tr_fdSocketCreate( int type ); 89 89 90 int tr_fdSocketAccept( int 91 struct in_addr* addr,92 tr_port_t *port );90 int tr_fdSocketAccept( int b, 91 tr_address * addr, 92 tr_port_t * port ); 93 93 94 94 void tr_fdSocketClose( int s ); -
trunk/libtransmission/handshake.c
r7188 r7195 1197 1197 } 1198 1198 1199 const struct in_addr*1199 const tr_address * 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
r7151 r7195 20 20 #include "transmission.h" 21 21 22 struct in_addr;23 22 struct tr_peerIo; 24 23 typedef struct tr_handshake tr_handshake; … … 36 35 void * doneUserData ); 37 36 38 const struct in_addr * tr_handshakeGetAddr( 39 const struct tr_handshake * handshake, 40 uint16_t 41 * setme_port ); 37 const tr_address * tr_handshakeGetAddr( const struct tr_handshake * handshake, 38 uint16_t * setme_port ); 42 39 43 40 void tr_handshakeAbort( tr_handshake * handshake ); -
trunk/libtransmission/net.c
r7190 r7195 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 } } } } }; 50 53 51 54 void … … 64 67 } 65 68 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; 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; 79 142 } 80 143 … … 137 200 } 138 201 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; 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; 147 235 148 236 if( ( s = createSocket( type ) ) < 0 ) … … 151 239 setSndBuf( session, s ); 152 240 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; 241 setup_sockaddr( addr, port, &sock ); 157 242 158 243 if( ( connect( s, (struct sockaddr *) &sock, 159 sizeof( struct sockaddr _in) ) < 0 )244 sizeof( struct sockaddr ) ) < 0 ) 160 245 #ifdef WIN32 161 246 && ( sockerrno != WSAEWOULDBLOCK ) … … 165 250 tr_err( _( 166 251 "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ), 167 s, inet_ntoa( *addr ), port,252 s, tr_ntop_non_ts( addr ), port, 168 253 sockerrno, tr_strerror( sockerrno ) ); 169 254 tr_netClose( s ); … … 178 263 179 264 int 180 tr_netBindTCP( int port ) 181 { 182 int s; 183 struct sockaddr_in sock; 184 const int type = SOCK_STREAM; 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; 185 271 186 272 #if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) … … 195 281 setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof( optval ) ); 196 282 #endif 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 ); 283 284 setup_sockaddr( addr, htons( port ), &sock ); 202 285 203 286 if( bind( s, (struct sockaddr *) &sock, 204 sizeof( struct sockaddr _in) ) )205 { 206 tr_err( _( "Couldn't bind port %d : %s" ), port,207 tr_ strerror( sockerrno ) );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 ) ); 208 291 tr_netClose( s ); 209 292 return -1; 210 293 } 211 294 212 tr_dbg( "Bound socket %d to port %d", s, port ); 295 tr_dbg( "Bound socket %d to port %d on %s", s, port, 296 tr_ntop_non_ts( addr ) ); 213 297 return s; 214 298 } 215 299 216 300 int 217 tr_netAccept( tr_session 218 int 219 struct in_addr* addr,220 tr_port_t 301 tr_netAccept( tr_session * session, 302 int b, 303 tr_address * addr, 304 tr_port_t * port ) 221 305 { 222 306 int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) ); … … 230 314 tr_fdSocketClose( s ); 231 315 } 232 233 void234 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
r7151 r7195 61 61 #endif 62 62 63 struct in_addr;64 struct sockaddr_in;65 63 struct tr_session; 66 64 67 /*********************************************************************** 68 * DNS resolution 69 **********************************************************************/ 70 int tr_netResolve( const char *, 71 struct in_addr * ); 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); 72 89 73 90 … … 75 92 * Sockets 76 93 **********************************************************************/ 77 int tr_netOpenTCP( struct tr_handle 78 const struct in_addr* addr,79 tr_port_t 94 int tr_netOpenTCP( struct tr_handle * session, 95 const tr_address * addr, 96 tr_port_t port ); 80 97 81 int tr_netBindTCP( int port ); 98 int tr_netBindTCP( const tr_address * addr, 99 int port ); 82 100 83 int tr_netAccept( struct tr_handle 84 int 85 struct in_addr* setme_addr,86 tr_port_t 101 int tr_netAccept( struct tr_handle * session, 102 int bound, 103 tr_address * setme_addr, 104 tr_port_t * setme_port ); 87 105 88 106 int tr_netSetTOS( int s, … … 91 109 void tr_netClose( int s ); 92 110 93 void tr_netNtop( const struct in_addr * addr,94 char * buf,95 int len );96 97 111 void tr_netInit( void ); 98 112 -
trunk/libtransmission/peer-io.c
r7186 r7195 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 uint16_t 244 const uint8_t * 245 int 246 int 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 ) 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 uint16_t 272 int 268 tr_peerIoNewIncoming( tr_session * session, 269 const tr_address * addr, 270 uint16_t 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,277 return tr_peerIoNew( session, addr, port, 279 278 NULL, 1, 280 279 socket ); … … 282 281 283 282 tr_peerIo* 284 tr_peerIoNewOutgoing( tr_session *session,285 const struct in_addr * in_addr,286 int 287 const uint8_t *torrentHash )283 tr_peerIoNewOutgoing( tr_session * session, 284 const tr_address * addr, 285 int port, 286 const uint8_t * torrentHash ) 288 287 { 289 288 int socket; 290 289 291 290 assert( session ); 292 assert( in_addr );291 assert( addr ); 293 292 assert( port >= 0 ); 294 293 assert( torrentHash ); 295 294 296 socket = tr_netOpenTCP( session, in_addr, port );295 socket = tr_netOpenTCP( session, addr, port ); 297 296 298 297 return socket < 0 299 298 ? NULL 300 : tr_peerIoNew( session, in_addr, port, torrentHash, 0, socket );299 : tr_peerIoNew( session, addr, port, torrentHash, 0, socket ); 301 300 } 302 301 … … 337 336 } 338 337 339 const struct in_addr*338 const tr_address* 340 339 tr_peerIoGetAddress( const tr_peerIo * io, 341 uint16_t * port )340 uint16_t * port ) 342 341 { 343 342 assert( isPeerIo( io ) ); … … 346 345 *port = io->port; 347 346 348 return &io-> in_addr;347 return &io->addr; 349 348 } 350 349 351 350 const char* 352 tr_peerIoAddrStr( const struct in_addr* addr,353 uint16_t 351 tr_peerIoAddrStr( const tr_address * addr, 352 uint16_t port ) 354 353 { 355 354 static char buf[512]; 356 355 357 tr_snprintf( buf, sizeof( buf ), "%s:%u", inet_ntoa( *addr ), 358 ntohs( port ) ); 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 ) ); 359 362 return buf; 360 363 } … … 363 366 tr_peerIoGetAddrStr( const tr_peerIo * io ) 364 367 { 365 return tr_peerIoAddrStr( &io-> in_addr, io->port );368 return tr_peerIoAddrStr( &io->addr, io->port ); 366 369 } 367 370 … … 402 405 tr_netClose( io->socket ); 403 406 404 io->socket = tr_netOpenTCP( io->session, &io-> in_addr, io->port );407 io->socket = tr_netOpenTCP( io->session, &io->addr, io->port ); 405 408 406 409 if( io->socket >= 0 ) -
trunk/libtransmission/peer-io.h
r7186 r7195 22 22 **/ 23 23 24 struct in_addr;25 24 struct evbuffer; 26 25 struct tr_bandwidth; … … 33 32 **/ 34 33 35 tr_peerIo* tr_peerIoNewOutgoing( struct tr_handle 36 const struct in_addr* addr,37 int 38 const uint8_t 39 40 tr_peerIo* tr_peerIoNewIncoming( struct tr_handle 41 const struct in_addr* addr,42 uint16_t 43 int 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 ); 44 43 45 44 void tr_peerIoFree( tr_peerIo * io ); … … 60 59 **/ 61 60 62 const char* tr_peerIoAddrStr( const struct in_addr* addr,63 uint16_t 61 const char* tr_peerIoAddrStr( const tr_address * addr, 62 uint16_t port ); 64 63 65 64 const char* tr_peerIoGetAddrStr( const tr_peerIo * io ); 66 65 67 const struct in_addr*tr_peerIoGetAddress( const tr_peerIo * io,66 const tr_address * tr_peerIoGetAddress( const tr_peerIo * io, 68 67 uint16_t * port ); 69 68 -
trunk/libtransmission/peer-mgr-private.h
r7173 r7195 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 uint16_t port; 55 struct in_addr in_addr;53 tr_address addr; 56 54 struct tr_peerIo * io; 57 55 -
trunk/libtransmission/peer-mgr.c
r7186 r7195 96 96 struct peer_atom 97 97 { 98 uint8_t 99 uint8_t 100 uint8_t 101 uint16_t 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 uint16_t 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 )203 getExistingHandshake( tr_ptrArray * handshakes, 204 const tr_address * addr ) 215 205 { 216 206 return tr_ptrArrayFindSorted( handshakes, 217 in_addr,207 addr, 218 208 handshakeCompareToAddr ); 219 209 } … … 225 215 const struct peer_atom * a = va; 226 216 227 return compareAddresses( &a->addr, vb );217 return tr_compareAddresses( &a->addr, vb ); 228 218 } 229 219 … … 277 267 const tr_peer * b = vb; 278 268 279 return compareAddresses( &a->in_addr, &b->in_addr );269 return tr_compareAddresses( &a->addr, &b->addr ); 280 270 } 281 271 … … 286 276 const tr_peer * a = va; 287 277 288 return compareAddresses( &a->in_addr, vb );278 return tr_compareAddresses( &a->addr, vb ); 289 279 } 290 280 291 281 static tr_peer* 292 getExistingPeer( Torrent *torrent,293 const struct in_addr * in_addr )282 getExistingPeer( Torrent * torrent, 283 const tr_address * addr ) 294 284 { 295 285 assert( torrentIsLocked( torrent ) ); 296 assert( in_addr );286 assert( addr ); 297 287 298 288 return tr_ptrArrayFindSorted( torrent->peers, 299 in_addr,289 addr, 300 290 peerCompareToAddr ); 301 291 } 302 292 303 293 static struct peer_atom* 304 getExistingAtom( const Torrent* t,305 const struct in_addr* addr )294 getExistingAtom( const Torrent * t, 295 const tr_address * addr ) 306 296 { 307 297 assert( torrentIsLocked( t ) ); … … 310 300 311 301 static int 312 peerIsInUse( const Torrent *ct,313 const struct in_addr* addr )302 peerIsInUse( const Torrent * ct, 303 const tr_address * addr ) 314 304 { 315 305 Torrent * t = (Torrent*) ct; … … 323 313 324 314 static tr_peer* 325 peerConstructor( tr_torrent * tor, const struct in_addr * in_addr )315 peerConstructor( tr_torrent * tor, const tr_address * addr ) 326 316 { 327 317 tr_peer * p; 328 318 329 319 p = tr_new0( tr_peer, 1 ); 330 memcpy( &p-> in_addr, in_addr, sizeof( struct in_addr) );320 memcpy( &p->addr, addr, sizeof( tr_address ) ); 331 321 p->bandwidth = tr_bandwidthNew( tor->session, tor->bandwidth ); 332 322 return p; … … 334 324 335 325 static tr_peer* 336 getPeer( Torrent *torrent,337 const struct in_addr * in_addr )326 getPeer( Torrent * torrent, 327 const tr_address * addr ) 338 328 { 339 329 tr_peer * peer; … … 341 331 assert( torrentIsLocked( torrent ) ); 342 332 343 peer = getExistingPeer( torrent, in_addr );333 peer = getExistingPeer( torrent, addr ); 344 334 345 335 if( peer == NULL ) 346 336 { 347 peer = peerConstructor( torrent->tor, in_addr );337 peer = peerConstructor( torrent->tor, addr ); 348 338 tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); 349 339 } … … 381 371 assert( torrentIsLocked( t ) ); 382 372 383 atom = getExistingAtom( t, &peer-> in_addr );373 atom = getExistingAtom( t, &peer->addr ); 384 374 assert( atom ); 385 375 atom->time = time( NULL ); … … 531 521 532 522 int 533 tr_peerMgrPeerIsSeed( const tr_peerMgr * 534 const uint8_t *torrentHash,535 const struct in_addr* addr )523 tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, 524 const uint8_t * torrentHash, 525 const tr_address * addr ) 536 526 { 537 527 int isSeed = FALSE; … … 906 896 { 907 897 tordbg( t, "increasing peer %s strike count to %d", 908 tr_peerIoAddrStr( &peer-> in_addr,898 tr_peerIoAddrStr( &peer->addr, 909 899 peer->port ), peer->strikes + 1 ); 910 900 911 901 if( ++peer->strikes >= MAX_BAD_PIECES_PER_PEER ) 912 902 { 913 struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );903 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 914 904 atom->myflags |= MYFLAG_BANNED; 915 905 peer->doPurge = 1; … … 976 966 /* update our atom */ 977 967 if( peer ) { 978 struct peer_atom * a = getExistingAtom( t, &peer-> in_addr );968 struct peer_atom * a = getExistingAtom( t, &peer->addr ); 979 969 a->piece_data_time = now; 980 970 } … … 1005 995 /* update our atom */ 1006 996 if( peer ) { 1007 struct peer_atom * a = getExistingAtom( t, &peer-> in_addr );997 struct peer_atom * a = getExistingAtom( t, &peer->addr ); 1008 998 a->piece_data_time = now; 1009 999 } … … 1016 1006 if( peer ) 1017 1007 { 1018 struct peer_atom * atom = getExistingAtom( t, 1019 &peer->in_addr ); 1008 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1020 1009 const int peerIsSeed = e->progress >= 1.0; 1021 1010 if( peerIsSeed ) … … 1112 1101 1113 1102 static void 1114 ensureAtomExists( Torrent *t,1115 const struct in_addr* addr,1116 uint16_t 1117 uint8_t 1118 uint8_t 1103 ensureAtomExists( Torrent * t, 1104 const tr_address * addr, 1105 uint16_t port, 1106 uint8_t flags, 1107 uint8_t from ) 1119 1108 { 1120 1109 if( getExistingAtom( t, addr ) == NULL ) … … 1153 1142 void * vmanager ) 1154 1143 { 1155 int 1156 int 1157 uint16_t 1158 const struct in_addr* addr;1159 tr_peerMgr * 1160 Torrent * 1161 tr_handshake * 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; 1162 1151 1163 1152 assert( io ); … … 1254 1243 1255 1244 void 1256 tr_peerMgrAddIncoming( tr_peerMgr * 1257 struct in_addr* addr,1258 uint16_t 1259 int 1245 tr_peerMgrAddIncoming( tr_peerMgr * manager, 1246 tr_address * addr, 1247 uint16_t port, 1248 int socket ) 1260 1249 { 1261 1250 managerLock( manager ); … … 1264 1253 { 1265 1254 tr_dbg( "Banned IP address \"%s\" tried to connect to us", 1266 inet_ntoa( *addr ) );1255 tr_ntop_non_ts( addr ) ); 1267 1256 tr_netClose( socket ); 1268 1257 } … … 1301 1290 1302 1291 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 );1292 if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->addr ) ) 1293 ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from ); 1305 1294 1306 1295 managerUnlock( manager ); … … 1321 1310 for( i = 0; i < n; ++i ) 1322 1311 { 1323 memcpy( &pex[i].in_addr, walk, 4 ); walk += 4; 1312 pex[i].addr.type = TR_AF_INET; 1313 memcpy( &pex[i].addr.addr, walk, 4 ); walk += 4; 1324 1314 memcpy( &pex[i].port, walk, 2 ); walk += 2; 1325 1315 if( added_f && ( n == added_f_len ) ) … … 1358 1348 t, 1359 1349 "peer %s contributed to corrupt piece (%d); now has %d strikes", 1360 tr_peerIoAddrStr( &peer-> in_addr, peer->port ),1350 tr_peerIoAddrStr( &peer->addr, peer->port ), 1361 1351 pieceIndex, (int)peer->strikes + 1 ); 1362 1352 addStrike( t, peer ); … … 1372 1362 const tr_pex * a = va; 1373 1363 const tr_pex * b = vb; 1374 int i = 1375 memcmp( &a->in_addr, &b->in_addr, sizeof( struct in_addr ) ); 1364 int i = tr_compareAddresses( &a->addr, &b->addr ); 1376 1365 1377 1366 if( i ) return i; … … 1421 1410 { 1422 1411 const tr_peer * peer = peers[i]; 1423 walk-> in_addr = peer->in_addr;1412 walk->addr = peer->addr; 1424 1413 walk->port = peer->port; 1425 1414 walk->flags = 0; … … 1659 1648 { 1660 1649 const tr_peer * peer = peers[i]; 1661 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1650 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1662 1651 1663 1652 if( peer->io == NULL ) /* not connected */ … … 1747 1736 char * pch; 1748 1737 const tr_peer * peer = peers[i]; 1749 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1738 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1750 1739 tr_peer_stat * stat = ret + i; 1751 1740 1752 tr_n etNtop( &peer->in_addr, stat->addr, sizeof( stat->addr ) );1741 tr_ntop( &peer->addr, stat->addr, sizeof( stat->addr ) ); 1753 1742 tr_strlcpy( stat->client, ( peer->client ? peer->client : "" ), 1754 1743 sizeof( stat->client ) ); … … 1946 1935 const tr_torrent * tor = t->tor; 1947 1936 const time_t now = time( NULL ); 1948 const struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );1937 const struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 1949 1938 1950 1939 /* if it's marked for purging, close it */ … … 2208 2197 { 2209 2198 tr_peer * peer = connections[i]; 2210 struct peer_atom * atom = getExistingAtom( t, &peer-> in_addr );2199 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 2211 2200 if( peer->pieceDataActivityDate ) 2212 2201 atom->numFails = 0; -
trunk/libtransmission/peer-mgr.h
r7186 r7195 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 uint16_t 47 uint8_t 44 tr_address addr; 45 uint16_t 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 uint16_t 64 int 60 void tr_peerMgrAddIncoming( tr_peerMgr * manager, 61 tr_address * addr, 62 uint16_t port, 63 int socket ); 65 64 66 65 tr_pex * tr_peerMgrCompactToPex( const void * compact, -
trunk/libtransmission/peer-msgs.c
r7186 r7195 1760 1760 } 1761 1761 1762 /* TODO: ipv6 pex */ 1762 1763 static void 1763 1764 sendPex( tr_peermsgs * msgs ) … … 1807 1808 tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); 1808 1809 for( i = 0; i < diffs.addedCount; ++i ) { 1809 memcpy( walk, &diffs.added[i]. in_addr, 4 ); walk += 4;1810 memcpy( walk, &diffs.added[i].addr.addr, 4 ); walk += 4; 1810 1811 memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; 1811 1812 } … … 1825 1826 tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); 1826 1827 for( i = 0; i < diffs.droppedCount; ++i ) { 1827 memcpy( walk, &diffs.dropped[i]. in_addr, 4 ); walk += 4;1828 memcpy( walk, &diffs.dropped[i].addr.addr, 4 ); walk += 4; 1828 1829 memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; 1829 1830 } -
trunk/libtransmission/port-forwarding.c
r7173 r7195 115 115 int socket; 116 116 errno = 0; 117 socket = tr_netBindTCP( s->publicPort ); 117 /* TODO: this is where we want to listen on another socket */ 118 socket = tr_netBindTCP( &tr_inaddr_any, s->publicPort ); 118 119 if( socket >= 0 ) 119 120 { … … 139 140 for( ; ; ) /* check for new incoming peer connections */ 140 141 { 141 int 142 uint16_t 143 struct in_addraddr;142 int socket; 143 uint16_t port; 144 tr_address addr; 144 145 145 146 if( s->bindSocket < 0 ) -
trunk/libtransmission/resume.c
r6840 r7195 62 62 ***/ 63 63 64 /* TODO: fast resume is done with pex */ 64 65 static void 65 66 savePeers( tr_benc * dict, -
trunk/libtransmission/session.c
r7154 r7195 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
r7173 r7195 39 39 #endif 40 40 #endif 41 42 #include "net.h" 41 43 42 44 … … 122 124 const char * filename ); 123 125 124 struct in_addr; 125 126 int tr_sessionIsAddressBlocked( const tr_session * session, 127 const struct in_addr * addr ); 126 int tr_sessionIsAddressBlocked( const tr_session * session, 127 const tr_address * addr ); 128 128 129 129 -
trunk/libtransmission/tracker.c
r7173 r7195 281 281 size_t * byteCount ) 282 282 { 283 /* TODO: 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_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 ); 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 ); 304 309 walk += 4; 305 310 -
trunk/libtransmission/transmission.h
r7173 r7195 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 … … 1062 1063 uint16_t port; 1063 1064 1064 char addr[ 16];1065 char addr[INET6_ADDRSTRLEN]; 1065 1066 char client[80]; 1066 1067 char flagStr[32];
Note: See TracChangeset
for help on using the changeset viewer.