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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.