Skip to content

Commit

Permalink
Require addressbook to only store addresses with valid ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Nov 9, 2018
1 parent 1944d85 commit bddeef4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions p2p/netaddress.go
Expand Up @@ -218,10 +218,22 @@ func (na *NetAddress) Routable() bool {
// For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
// address or one that matches the RFC3849 documentation address format.
func (na *NetAddress) Valid() bool {
if string(na.ID) != "" {
data, err := hex.DecodeString(string(na.ID))
if err != nil || len(data) != IDByteLength {
return false
}
}
return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
na.IP.Equal(net.IPv4bcast))
}

// HasID returns true if the address has an ID.
// NOTE: It does not check whether the ID is valid or not.
func (na *NetAddress) HasID() bool {
return string(na.ID) != ""
}

// Local returns true if it is a local address.
func (na *NetAddress) Local() bool {
return na.IP.IsLoopback() || zero4.Contains(na.IP)
Expand Down
4 changes: 4 additions & 0 deletions p2p/pex/addrbook.go
Expand Up @@ -652,6 +652,10 @@ func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
return ErrAddrBookInvalidAddr{addr}
}

if !addr.HasID() {
return ErrAddrBookInvalidAddrNoID{addr}
}

// TODO: we should track ourAddrs by ID and by IP:PORT and refuse both.
if _, ok := a.ourAddrs[addr.String()]; ok {
return ErrAddrBookSelf{addr}
Expand Down
8 changes: 8 additions & 0 deletions p2p/pex/errors.go
Expand Up @@ -54,3 +54,11 @@ type ErrAddrBookInvalidAddr struct {
func (err ErrAddrBookInvalidAddr) Error() string {
return fmt.Sprintf("Cannot add invalid address %v", err.Addr)
}

type ErrAddrBookInvalidAddrNoID struct {
Addr *p2p.NetAddress
}

func (err ErrAddrBookInvalidAddrNoID) Error() string {
return fmt.Sprintf("Cannot add address with no ID %v", err.Addr)
}

0 comments on commit bddeef4

Please sign in to comment.