From 9c9c94a81d5fd746bde2491bb4d6aef1c5fae358 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 17 Jan 2009 23:14:35 +0000 Subject: [PATCH] (trunk libT) discard peers from the tracker or pex that have a port of 0 or a multicast address. --- libtransmission/net.c | 30 +++++++++++++++++++++++++----- libtransmission/net.h | 2 ++ libtransmission/peer-mgr.c | 3 ++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/libtransmission/net.c b/libtransmission/net.c index c30ecaa4b..618e49816 100644 --- a/libtransmission/net.c +++ b/libtransmission/net.c @@ -57,7 +57,6 @@ const tr_address tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } }; const tr_address tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY, 0x00, 0x00, 0x00 } } } } }; - #ifdef WIN32 static const char * inet_ntop(int af, const void *src, char *dst, socklen_t cnt) @@ -462,6 +461,30 @@ setup_sockaddr( const tr_address * addr, } } +static tr_bool +isMulticastAddress( const tr_address * addr ) +{ + if( addr->type == TR_AF_INET && IN_MULTICAST( htonl( addr->addr.addr4.s_addr ) ) ) + return TRUE; + + if( addr->type == TR_AF_INET6 && ( addr->addr.addr6.s6_addr[0] == 0xff ) ) + return TRUE; + + return FALSE; +} + +tr_bool +tr_isValidPeerAddress( const tr_address * addr, tr_port port ) +{ + if( isMulticastAddress( addr ) ) + return FALSE; + + if( port == 0 ) + return FALSE; + + return TRUE; +} + int tr_netOpenTCP( tr_session * session, const tr_address * addr, @@ -474,10 +497,7 @@ tr_netOpenTCP( tr_session * session, assert( tr_isAddress( addr ) ); - /* don't try to connect to multicast addresses */ - if( addr->type == TR_AF_INET && IN_MULTICAST( htonl( addr->addr.addr4.s_addr ) ) ) - return -EINVAL; - if( addr->type == TR_AF_INET6 && ( addr->addr.addr6.s6_addr[0] == 0xff ) ) + if( isMulticastAddress( addr ) ) return -EINVAL; if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ), type ) ) < 0 ) diff --git a/libtransmission/net.h b/libtransmission/net.h index 90bd63fcb..d8de8c5fb 100644 --- a/libtransmission/net.h +++ b/libtransmission/net.h @@ -88,6 +88,8 @@ int tr_compareAddresses( const tr_address * a, const tr_address * b); void tr_normalizeV4Mapped( tr_address * const addr ); +tr_bool tr_isValidPeerAddress( const tr_address * addr, tr_port port ); + void tr_suspectAddress( const tr_address * a, const char * source ); static TR_INLINE tr_bool tr_isAddress( const tr_address * a ) { return ( a != NULL ) && ( a->type==TR_AF_INET || a->type==TR_AF_INET6 ); } diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index aa85f4ff2..b9822740c 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -1315,7 +1315,8 @@ tr_peerMgrAddPex( tr_torrent * tor, managerLock( t->manager ); if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->addr ) ) - ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from ); + if( tr_isValidPeerAddress( &pex->addr, pex->port ) ) + ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from ); managerUnlock( t->manager ); } -- 2.40.0