Changeset 9778


Ignore:
Timestamp:
Dec 15, 2009, 5:39:19 PM (12 years ago)
Author:
charles
Message:

(trunk libT) #2667 "ignore martian addresses" -- modified this ticket. previously we ignored them from LTEP; now we ignore them regardless of the source. this commit implements this filtering change.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r9748 r9778  
    241241}
    242242
    243 static tr_bool
    244 isMulticastAddress( const tr_address * addr )
    245 {
    246     if( addr->type == TR_AF_INET && IN_MULTICAST( htonl( addr->addr.addr4.s_addr ) ) )
    247         return TRUE;
    248 
    249     if( addr->type == TR_AF_INET6 && ( addr->addr.addr6.s6_addr[0] == 0xff ) )
    250         return TRUE;
    251 
    252     return FALSE;
    253 }
    254 
    255 static TR_INLINE tr_bool
    256 isIPv4MappedOrCompatAddress( const tr_address * addr )
    257 {
    258     if( addr->type == TR_AF_INET6 )
    259     {
    260         if( IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ) ||
    261             IN6_IS_ADDR_V4COMPAT( &addr->addr.addr6 ) )
    262             return TRUE;
    263     }
    264     return FALSE;
    265 }
    266 
    267 static TR_INLINE tr_bool
    268 isIPv6LinkLocalAddress( const tr_address * addr )
    269 {
    270     if( addr->type == TR_AF_INET6 &&
    271         IN6_IS_ADDR_LINKLOCAL( &addr->addr.addr6 ) )
    272         return TRUE;
    273     return FALSE;
    274 }
    275 
    276 tr_bool
    277 tr_isValidPeerAddress( const tr_address * addr, tr_port port )
    278 {
    279     if( isMulticastAddress( addr ) || isIPv6LinkLocalAddress( addr ) ||
    280         isIPv4MappedOrCompatAddress( addr ) )
    281         return FALSE;
    282 
    283     if( port == 0 )
    284         return FALSE;
    285 
    286     return TRUE;
    287 }
    288 
    289243int
    290 tr_netOpenTCP( tr_session        * session,
    291                const tr_address  * addr,
    292                tr_port             port,
    293                tr_bool             isSeed )
     244tr_netOpenPeerSocket( tr_session        * session,
     245                      const tr_address  * addr,
     246                      tr_port             port,
     247                      tr_bool             clientIsSeed )
    294248{
    295249    static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
     
    303257    assert( tr_isAddress( addr ) );
    304258
    305     if( isMulticastAddress( addr ) || isIPv6LinkLocalAddress( addr ) )
     259    if( !tr_isValidPeerAddress( addr, port ) )
    306260        return -EINVAL;
    307261
     
    311265
    312266    /* seeds don't need much of a read buffer... */
    313     if( isSeed ) {
     267    if( clientIsSeed ) {
    314268        int n = 8192;
    315269        if( setsockopt( s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n) ) )
     
    635589}
    636590
    637 int
    638 tr_isMartian( int af, const unsigned char * address )
     591/***
     592****
     593****
     594***/
     595
     596static tr_bool
     597isMulticastAddress( const tr_address * addr )
     598{
     599    if( addr->type == TR_AF_INET && IN_MULTICAST( htonl( addr->addr.addr4.s_addr ) ) )
     600        return TRUE;
     601
     602    if( addr->type == TR_AF_INET6 && ( addr->addr.addr6.s6_addr[0] == 0xff ) )
     603        return TRUE;
     604
     605    return FALSE;
     606}
     607
     608static tr_bool
     609isIPv4MappedOrCompatAddress( const tr_address * addr )
     610{
     611    return ( ( addr->type == TR_AF_INET6 )
     612                  && ( IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 )
     613                        || IN6_IS_ADDR_V4COMPAT( &addr->addr.addr6 ) ) );
     614}
     615
     616static tr_bool
     617isIPv6LinkLocalAddress( const tr_address * addr )
     618{
     619    return ( ( addr->type == TR_AF_INET6 )
     620                  && IN6_IS_ADDR_LINKLOCAL( &addr->addr.addr6 ) );
     621}
     622
     623/* isMartianAddr was written by Juliusz Chroboczek,
     624   and is covered under the same license as third-party/dht/dht.c. */
     625static tr_bool
     626isMartianAddr( const struct tr_address * a )
    639627{
    640628    static const unsigned char v4prefix[16] =
     
    643631        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    644632
    645     switch( af )
    646     {
    647         case AF_INET:
     633    assert( tr_isAddress( a ) );
     634
     635    switch( a->type )
     636    {
     637        case TR_AF_INET: {
     638            const unsigned char * address = (const unsigned char*)&a->addr.addr4;
    648639            return (address[0] == 0) ||
    649640                   (address[0] == 127) ||
    650641                   ((address[0] & 0xE0) == 0xE0);
    651 
    652         case AF_INET6:
     642            break;
     643        }
     644
     645        case TR_AF_INET6: {
     646            const unsigned char * address = (const unsigned char*)&a->addr.addr6;
    653647            return (address[0] == 0xFF) ||
    654648                   (address[0] == 0xFE && (address[1] & 0xC0) == 0x80) ||
    655649                   (memcmp(address, zeroes, 15) == 0 && (address[15] == 0 || address[15] == 1)) ||
    656650                   (memcmp(address, v4prefix, 12) == 0);
     651            break;
     652        }
    657653
    658654        default:
    659             return FALSE;
    660     }
    661 }
     655            return TRUE;
     656    }
     657}
     658
     659tr_bool
     660tr_isValidPeerAddress( const tr_address * addr, tr_port port )
     661{
     662    return ( port != 0 )
     663        && ( tr_isAddress( addr ) )
     664        && ( !isMulticastAddress( addr ) )
     665        && ( !isIPv6LinkLocalAddress( addr ) )
     666        && ( !isIPv4MappedOrCompatAddress( addr ) )
     667        && ( !isMartianAddr( addr ) );
     668}
  • trunk/libtransmission/net.h

    r9748 r9778  
    9999 * Sockets
    100100 **********************************************************************/
    101 int  tr_netOpenTCP( tr_session       * session,
    102                     const tr_address * addr,
    103                     tr_port            port,
    104                     tr_bool            isSeed );
     101int  tr_netOpenPeerSocket( tr_session       * session,
     102                           const tr_address * addr,
     103                           tr_port            port,
     104                           tr_bool            clientIsSeed );
    105105
    106106int  tr_netBindTCP( const tr_address * addr,
     
    126126const unsigned char *tr_globalIPv6( void );
    127127
    128 int tr_isMartian( int af, const unsigned char * address );
    129 
    130128
    131129#endif /* _TR_NET_H_ */
  • trunk/libtransmission/peer-io.c

    r9704 r9778  
    432432    assert( torrentHash );
    433433
    434     fd = tr_netOpenTCP( session, addr, port, isSeed );
    435     dbgmsg( NULL, "tr_netOpenTCP returned fd %d", fd );
     434    fd = tr_netOpenPeerSocket( session, addr, port, isSeed );
     435    dbgmsg( NULL, "tr_netOpenPeerSocket returned fd %d", fd );
    436436
    437437    return fd < 0 ? NULL
     
    562562        tr_netClose( session, io->socket );
    563563
    564     io->socket = tr_netOpenTCP( session, &io->addr, io->port, io->isSeed );
     564    io->socket = tr_netOpenPeerSocket( session, &io->addr, io->port, io->isSeed );
    565565    if( io->socket >= 0 )
    566566    {
  • trunk/libtransmission/peer-msgs.c

    r9748 r9778  
    983983    if( tr_peerIoIsIncoming( msgs->peer->io )
    984984        && tr_bencDictFindRaw( &val, "ipv4", &addr, &addr_len )
    985         && ( addr_len == 4 )
    986         && ( !tr_isMartian( AF_INET, addr ) ) )
     985        && ( addr_len == 4 ) )
    987986    {
    988987        pex.addr.type = TR_AF_INET;
     
    993992    if( tr_peerIoIsIncoming( msgs->peer->io )
    994993        && tr_bencDictFindRaw( &val, "ipv6", &addr, &addr_len )
    995         && ( addr_len == 16 )
    996         && ( !tr_isMartian( AF_INET6, addr ) ) )
     994        && ( addr_len == 16 ) )
    997995    {
    998996        pex.addr.type = TR_AF_INET6;
Note: See TracChangeset for help on using the changeset viewer.