Skip to content

Commit

Permalink
Use kaspad's IsRoutable function (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
svarogg authored Feb 1, 2022
1 parent a441ac0 commit 43f1ada
Showing 1 changed file with 3 additions and 59 deletions.
62 changes: 3 additions & 59 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

"github.com/kaspanet/kaspad/infrastructure/network/addressmanager"

"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/miekg/dns"
Expand Down Expand Up @@ -62,64 +64,6 @@ const (
pruneExpireTimeout = time.Hour * 8
)

var (
// rfc1918Nets specifies the IPv4 private address blocks as defined by
// by RFC1918 (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16).
rfc1918Nets = []net.IPNet{
ipNet("10.0.0.0", 8, 32),
ipNet("172.16.0.0", 12, 32),
ipNet("192.168.0.0", 16, 32),
}

// rfc3964Net specifies the IPv6 to IPv4 encapsulation address block as
// defined by RFC3964 (2002::/16).
rfc3964Net = ipNet("2002::", 16, 128)

// rfc4380Net specifies the IPv6 teredo tunneling over UDP address block
// as defined by RFC4380 (2001::/32).
rfc4380Net = ipNet("2001::", 32, 128)

// rfc4843Net specifies the IPv6 ORCHID address block as defined by
// RFC4843 (2001:10::/28).
rfc4843Net = ipNet("2001:10::", 28, 128)

// rfc4862Net specifies the IPv6 stateless address autoconfiguration
// address block as defined by RFC4862 (FE80::/64).
rfc4862Net = ipNet("FE80::", 64, 128)

// rfc4193Net specifies the IPv6 unique local address block as defined
// by RFC4193 (FC00::/7).
rfc4193Net = ipNet("FC00::", 7, 128)
)

// ipNet returns a net.IPNet struct given the passed IP address string, number
// of one bits to include at the start of the mask, and the total number of bits
// for the mask.
func ipNet(ip string, ones, bits int) net.IPNet {
return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
}

func isRoutable(addr net.IP) bool {
if ActiveConfig().NetParams().AcceptUnroutable {
return true
}

for _, n := range rfc1918Nets {
if n.Contains(addr) {
return false
}
}
if rfc3964Net.Contains(addr) ||
rfc4380Net.Contains(addr) ||
rfc4843Net.Contains(addr) ||
rfc4862Net.Contains(addr) ||
rfc4193Net.Contains(addr) {
return false
}

return true
}

// NewManager constructs and returns a new dnsseeder manager, with the provided dataDir
func NewManager(dataDir string) (*Manager, error) {
amgr := Manager{
Expand Down Expand Up @@ -152,7 +96,7 @@ func (m *Manager) AddAddresses(addrs []*appmessage.NetAddress) int {

m.mtx.Lock()
for _, addr := range addrs {
if !isRoutable(addr.IP) {
if !addressmanager.IsRoutable(addr, ActiveConfig().NetParams().AcceptUnroutable) {
continue
}
addrStr := addr.IP.String()
Expand Down

0 comments on commit 43f1ada

Please sign in to comment.