dht: Don't contact nodes with an IP containing a leading octet of 0
This commit is contained in:
parent
2483c75f09
commit
88d21ce627
|
@ -115,10 +115,20 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
|
|||
return disc, nil
|
||||
}
|
||||
|
||||
func validNodeAddr(addr Addr) bool {
|
||||
ua := addr.UDPAddr()
|
||||
if ua.Port == 0 {
|
||||
return false
|
||||
}
|
||||
if ip4 := ua.IP.To4(); ip4 != nil && ip4[0] == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TODO: Merge this with maybeGetPeersFromAddr.
|
||||
func (a *Announce) gotNodeAddr(addr Addr) {
|
||||
if addr.UDPAddr().Port == 0 {
|
||||
// Not a contactable address.
|
||||
if !validNodeAddr(addr) {
|
||||
return
|
||||
}
|
||||
if a.triedAddrs.Test([]byte(addr.String())) {
|
||||
|
|
|
@ -221,3 +221,11 @@ func TestHook(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that address resolution doesn't rat out invalid SendTo addr
|
||||
// arguments.
|
||||
func TestResolveBadAddr(t *testing.T) {
|
||||
ua, err := net.ResolveUDPAddr("udp", "0.131.255.145:33085")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, validNodeAddr(NewAddr(ua)))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue