discovery

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2020 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package discovery implements uses bitcoin-based addrbook to store network addresses and collects them by crawling the network using a simple protocol.

Index

Constants

View Source
const GetAddresses = 1

GetAddresses is the findnode protocol ID

View Source
const MessageBufSize = 1000

MessageBufSize is the buf size we give to the messages channel

View Source
const MessageTimeout = time.Second * 5 // TODO: Parametrize

MessageTimeout is the timeout we tolerate when waiting for a message reply

View Source
const Name = "/udp/v2/discovery"

Name is the name if the protocol.

View Source
const PingPong = 0

PingPong is the ping protocol ID

Variables

View Source
var (
	// ErrLookupFailed determines that we could'nt lookup this node in the routing table or network
	ErrLookupFailed = errors.New("failed to lookup node in the network")
	// ErrEmptyRoutingTable means that our routing table is empty thus we can't lookup any node (so we can't query any)
	ErrEmptyRoutingTable = errors.New("no nodes to query - routing table is empty")
)
View Source
var ErrBootAbort = errors.New("bootstrap canceled by signal")

ErrBootAbort is returned when when bootstrap is canceled by context cancel

Functions

func GroupKey

func GroupKey(na net.IP) string

GroupKey returns a string representing the network group an address is part of. This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string "localNode" for a localNode address, the string "tor:key" where key is the /4 of the onion address for Tor address, and the string "unroutable" for an unroutable address.

func IsIPv4

func IsIPv4(na net.IP) bool

IsIPv4 returns whether or not the given address is an IPv4 address.

func IsLocal

func IsLocal(na net.IP) bool

IsLocal returns whether or not the given address is a localNode address.

func IsOnionCatTor

func IsOnionCatTor(na net.IP) bool

IsOnionCatTor returns whether or not the passed address is in the IPv6 range used by bitcoin to support Tor (fd87:d87e:eb43::/48). Note that this range is the same range used by OnionCat, which is part of the RFC4193 unique localNode IPv6 range.

func IsRFC1918

func IsRFC1918(na net.IP) bool

IsRFC1918 returns whether or not the passed address is part of the IPv4 private network address space as defined by RFC1918 (10.0.0.0/8, 172.16.0.0/12, or 192.168.0.0/16).

func IsRFC2544

func IsRFC2544(na net.IP) bool

IsRFC2544 returns whether or not the passed address is part of the IPv4 address space as defined by RFC2544 (198.18.0.0/15)

func IsRFC3849

func IsRFC3849(na net.IP) bool

IsRFC3849 returns whether or not the passed address is part of the IPv6 documentation range as defined by RFC3849 (2001:DB8::/32).

func IsRFC3927

func IsRFC3927(na net.IP) bool

IsRFC3927 returns whether or not the passed address is part of the IPv4 autoconfiguration range as defined by RFC3927 (169.254.0.0/16).

func IsRFC3964

func IsRFC3964(na net.IP) bool

IsRFC3964 returns whether or not the passed address is part of the IPv6 to IPv4 encapsulation range as defined by RFC3964 (2002::/16).

func IsRFC4193

func IsRFC4193(na net.IP) bool

IsRFC4193 returns whether or not the passed address is part of the IPv6 unique localNode range as defined by RFC4193 (FC00::/7).

func IsRFC4380

func IsRFC4380(na net.IP) bool

IsRFC4380 returns whether or not the passed address is part of the IPv6 teredo tunneling over UDP range as defined by RFC4380 (2001::/32).

func IsRFC4843

func IsRFC4843(na net.IP) bool

IsRFC4843 returns whether or not the passed address is part of the IPv6 ORCHID range as defined by RFC4843 (2001:10::/28).

func IsRFC4862

func IsRFC4862(na net.IP) bool

IsRFC4862 returns whether or not the passed address is part of the IPv6 stateless address autoconfiguration range as defined by RFC4862 (FE80::/64).

func IsRFC5737

func IsRFC5737(na net.IP) bool

IsRFC5737 returns whether or not the passed address is part of the IPv4 documentation address space as defined by RFC5737 (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)

func IsRFC6052

func IsRFC6052(na net.IP) bool

IsRFC6052 returns whether or not the passed address is part of the IPv6 well-known prefix range as defined by RFC6052 (64:FF9B::/96).

func IsRFC6145

func IsRFC6145(na net.IP) bool

IsRFC6145 returns whether or not the passed address is part of the IPv6 to IPv4 translated address range as defined by RFC6145 (::FFFF:0:0:0/96).

func IsRFC6598

func IsRFC6598(na net.IP) bool

IsRFC6598 returns whether or not the passed address is part of the IPv4 shared address space specified by RFC6598 (100.64.0.0/10)

func IsRoutable

func IsRoutable(na net.IP) bool

IsRoutable returns whether or not the passed address is routable over the public internet. This is true as long as the address is valid and is not in any reserved ranges.

func IsValid

func IsValid(na net.IP) bool

IsValid returns whether or not the passed address is valid. The address is considered invalid under the following circumstances: IPv4: It is either a zero or all bits set address. IPv6: It is either a zero or RFC3849 documentation address.

Types

type Discovery

type Discovery struct {
	// contains filtered or unexported fields
}

Discovery is struct that holds the protocol components, the protocol definition, the addr book data structure and more.

func New

func New(ln node.LocalNode, config config.SwarmConfig, service server.Service, path string, logger log.Log) *Discovery

New creates a new Discovery

func (*Discovery) Attempt

func (d *Discovery) Attempt(key p2pcrypto.PublicKey)

Attempt marks an attempt on the node in the addrBook

func (*Discovery) Bootstrap

func (d *Discovery) Bootstrap(ctx context.Context) error

Bootstrap runs a refresh and tries to get a minimum number of nodes in the addrBook.

func (*Discovery) Good

func (d *Discovery) Good(key p2pcrypto.PublicKey)

Good marks a node as good in the addrBook.

func (*Discovery) IsLocalAddress

func (d *Discovery) IsLocalAddress(info *node.Info) bool

IsLocalAddress checks info against the addrBook to see if it's a local address.

func (*Discovery) Lookup

func (d *Discovery) Lookup(key p2pcrypto.PublicKey) (*node.Info, error)

Lookup searched a node in the address book. *NOTE* this returns a `Node` with the udpAddress as `Address()`. this is because Lookup is only used in the udp mux.

func (*Discovery) Remove

func (d *Discovery) Remove(key p2pcrypto.PublicKey)

Remove removes a record from the routing table

func (*Discovery) SelectPeers

func (d *Discovery) SelectPeers(ctx context.Context, qty int) []*node.Info

SelectPeers asks routing table to randomly select a slice of nodes in size `qty`

func (*Discovery) SetLocalAddresses

func (d *Discovery) SetLocalAddresses(tcp, udp int)

SetLocalAddresses sets the localNode addresses to be advertised.

func (*Discovery) Shutdown

func (d *Discovery) Shutdown()

Shutdown stops the discovery service

func (*Discovery) Size

func (d *Discovery) Size() int

Size returns the size of addrBook.

func (*Discovery) Update

func (d *Discovery) Update(addr, src *node.Info)

Update adds an addr to the addrBook

type KnownAddress

type KnownAddress struct {
	// contains filtered or unexported fields
}

KnownAddress tracks information about a known network address that is used to determine how viable an address is.

func (*KnownAddress) LastAttempt

func (ka *KnownAddress) LastAttempt() time.Time

LastAttempt returns the last time the known address was attempted.

func (*KnownAddress) NodeInfo

func (ka *KnownAddress) NodeInfo() *node.Info

NodeInfo returns the internal Info struct of this address.

type MockPeerStore

type MockPeerStore struct {
	UpdateFunc func(n, src *node.Info)

	SelectPeersFunc func(ctx context.Context, qty int) []*node.Info

	LookupFunc func(p2pcrypto.PublicKey) (*node.Info, error)

	IsLocalAddressFunc func(info *node.Info) bool

	RemoveFunc  func(key p2pcrypto.PublicKey)
	GoodFunc    func(key p2pcrypto.PublicKey)
	AttemptFunc func(key p2pcrypto.PublicKey)
	// contains filtered or unexported fields
}

MockPeerStore is a mocked discovery

func (*MockPeerStore) Attempt

func (m *MockPeerStore) Attempt(key p2pcrypto.PublicKey)

Attempt is a mock.

func (*MockPeerStore) Bootstrap

func (m *MockPeerStore) Bootstrap(ctx context.Context) error

Bootstrap is a discovery bootstrap operation function it update the bootstrap count

func (*MockPeerStore) BootstrapCount

func (m *MockPeerStore) BootstrapCount() int

BootstrapCount returns the number of times bootstrap was called

func (*MockPeerStore) Good

func (m *MockPeerStore) Good(key p2pcrypto.PublicKey)

Good is a mock.

func (*MockPeerStore) IsLocalAddress

func (m *MockPeerStore) IsLocalAddress(info *node.Info) bool

IsLocalAddress mock

func (*MockPeerStore) Lookup

func (m *MockPeerStore) Lookup(pubkey p2pcrypto.PublicKey) (*node.Info, error)

Lookup is a discovery lookup operation

func (*MockPeerStore) Remove

func (m *MockPeerStore) Remove(key p2pcrypto.PublicKey)

Remove mock

func (*MockPeerStore) SelectPeers

func (m *MockPeerStore) SelectPeers(ctx context.Context, qty int) []*node.Info

SelectPeers mocks selecting peers.

func (*MockPeerStore) SetBootstrap

func (m *MockPeerStore) SetBootstrap(err error)

SetBootstrap set the bootstrap result

func (*MockPeerStore) SetLocalAddresses

func (m *MockPeerStore) SetLocalAddresses(tcp, udp int)

SetLocalAddresses to satisfy the iface

func (*MockPeerStore) SetLookupResult

func (m *MockPeerStore) SetLookupResult(node *node.Info, err error)

SetLookupResult sets the result ok a lookup operation

func (*MockPeerStore) SetUpdate

func (m *MockPeerStore) SetUpdate(f func(n, addr *node.Info))

SetUpdate sets the function to run on an issued update

func (*MockPeerStore) Shutdown

func (m *MockPeerStore) Shutdown()

Shutdown is a mock.

func (*MockPeerStore) Size

func (m *MockPeerStore) Size() int

Size returns the size of peers in the discovery

func (*MockPeerStore) Update

func (m *MockPeerStore) Update(n, src *node.Info)

Update is a discovery update operation it updates the updatecount

func (*MockPeerStore) UpdateCount

func (m *MockPeerStore) UpdateCount() int

UpdateCount returns the number of times update was called

type PeerStore

type PeerStore interface {
	Remove(pubkey p2pcrypto.PublicKey)
	Lookup(pubkey p2pcrypto.PublicKey) (*node.Info, error)
	Update(addr, src *node.Info)

	SelectPeers(ctx context.Context, qty int) []*node.Info
	Bootstrap(ctx context.Context) error
	Size() int

	Shutdown()

	IsLocalAddress(info *node.Info) bool
	SetLocalAddresses(tcp, udp int)

	Good(key p2pcrypto.PublicKey)
	Attempt(key p2pcrypto.PublicKey)
}

PeerStore is an interface to the discovery protocol

type Protocol

type Protocol interface {
	Ping(p p2pcrypto.PublicKey) error
	GetAddresses(server p2pcrypto.PublicKey) ([]*node.Info, error)
	SetLocalAddresses(tcp, udp int)
	Close()
}

Protocol is the API of node messages used to discover new nodes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL