Documentation
¶
Overview ¶
Package addressmanager implements concurrency safe Kaspa address manager.
Address Manager Overview ¶
In order maintain the peer-to-peer Kaspa network, there needs to be a source of addresses to connect to as nodes come and go. The Kaspa protocol provides the getaddr and addr messages to allow peers to communicate known addresses with each other. However, there needs to a mechanism to store those results and select peers from them. It is also important to note that remote peers can't be trusted to send valid peers nor attempt to provide you with only peers they control with malicious intent.
With that in mind, this package provides a concurrency safe address manager for caching and selecting peers in a non-deterministic manner. The general idea is the caller adds addresses to the address manager and notifies it when addresses are connected, known good, and attempted. The caller also requests addresses as it needs them.
The address manager internally segregates the addresses into groups and non-deterministically selects groups in a cryptographically random manner. This reduce the chances multiple addresses from the same nets are selected which generally helps provide greater peer diversity, and perhaps more importantly, drastically reduces the chances an attacker is able to coerce your peer into only connecting to nodes they control.
The address manager also understands routability and tries hard to only return routable addresses. In addition, it uses the information provided by the caller about connected, known good, and attempted addresses to periodically purge peers which no longer appear to be good peers as well as bias the selection toward known good peers. The general idea is to make a best effort at only providing usable addresses.
Index ¶
- Constants
- Variables
- func IsIPv4(na *appmessage.NetAddress) bool
- func IsLocal(na *appmessage.NetAddress) bool
- func IsRFC1918(na *appmessage.NetAddress) bool
- func IsRFC2544(na *appmessage.NetAddress) bool
- func IsRFC3849(na *appmessage.NetAddress) bool
- func IsRFC3927(na *appmessage.NetAddress) bool
- func IsRFC3964(na *appmessage.NetAddress) bool
- func IsRFC4193(na *appmessage.NetAddress) bool
- func IsRFC4380(na *appmessage.NetAddress) bool
- func IsRFC4843(na *appmessage.NetAddress) bool
- func IsRFC4862(na *appmessage.NetAddress) bool
- func IsRFC5737(na *appmessage.NetAddress) bool
- func IsRFC6052(na *appmessage.NetAddress) bool
- func IsRFC6145(na *appmessage.NetAddress) bool
- func IsRFC6598(na *appmessage.NetAddress) bool
- func IsValid(na *appmessage.NetAddress) bool
- type AddressKey
- type AddressManager
- func (am *AddressManager) AddAddress(address, sourceAddress *appmessage.NetAddress, ...)
- func (am *AddressManager) AddAddressByIP(addressIP string, subnetworkID *subnetworkid.SubnetworkID) error
- func (am *AddressManager) AddAddresses(addresses []*appmessage.NetAddress, sourceAddress *appmessage.NetAddress, ...)
- func (am *AddressManager) AddLocalAddress(netAddress *appmessage.NetAddress, priority AddressPriority) error
- func (am *AddressManager) AddressCache(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) []*appmessage.NetAddress
- func (am *AddressManager) Attempt(address *appmessage.NetAddress)
- func (am *AddressManager) Ban(address *appmessage.NetAddress) error
- func (am *AddressManager) Connected(address *appmessage.NetAddress)
- func (am *AddressManager) DeserializeNetAddress(addressKey AddressKey) (*appmessage.NetAddress, error)
- func (am *AddressManager) GetAddress() *KnownAddress
- func (am *AddressManager) GetBestLocalAddress(remoteAddress *appmessage.NetAddress) *appmessage.NetAddress
- func (am *AddressManager) Good(address *appmessage.NetAddress, subnetworkID *subnetworkid.SubnetworkID)
- func (am *AddressManager) GroupKey(na *appmessage.NetAddress) string
- func (am *AddressManager) HostToNetAddress(host string, port uint16, services appmessage.ServiceFlag) (*appmessage.NetAddress, error)
- func (am *AddressManager) IsBanned(address *appmessage.NetAddress) (bool, error)
- func (am *AddressManager) IsRoutable(na *appmessage.NetAddress) bool
- func (am *AddressManager) NeedMoreAddresses() bool
- func (am *AddressManager) PeersStateForSerialization() (*PeersStateForSerialization, error)
- func (am *AddressManager) Start() error
- func (am *AddressManager) Stop() error
- func (am *AddressManager) TotalNumAddresses() int
- func (am *AddressManager) Unban(address *appmessage.NetAddress) error
- type AddressPriority
- type KnownAddress
- type PeersStateForSerialization
Constants ¶
const ( // TriedBucketCount is the number of buckets we split tried // addresses over. TriedBucketCount = 64 // NewBucketCount is the number of buckets that we spread new addresses // over. NewBucketCount = 1024 // GetAddressesMax is the most addresses that we will send in response // to a getAddress (in practise the most addresses we will return from a // call to AddressCache()). GetAddressesMax = 2500 )
Variables ¶
var ErrAddressNotFound = errors.New("address not found")
ErrAddressNotFound is an error returned from some functions when a given address is not found in the address manager
Functions ¶
func IsIPv4 ¶
func IsIPv4(na *appmessage.NetAddress) bool
IsIPv4 returns whether or not the given address is an IPv4 address.
func IsLocal ¶
func IsLocal(na *appmessage.NetAddress) bool
IsLocal returns whether or not the given address is a local address.
func IsRFC1918 ¶
func IsRFC1918(na *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) bool
IsRFC4193 returns whether or not the passed address is part of the IPv6 unique local range as defined by RFC4193 (FC00::/7).
func IsRFC4380 ¶
func IsRFC4380(na *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 *appmessage.NetAddress) 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 IsValid ¶
func IsValid(na *appmessage.NetAddress) 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 AddressKey ¶
type AddressKey string
AddressKey represents a "string" key in the form of ip:port for IPv4 addresses or [ip]:port for IPv6 addresses for use as keys in maps.
func NetAddressKey ¶
func NetAddressKey(netAddress *appmessage.NetAddress) AddressKey
NetAddressKey returns a key in the form of ip:port for IPv4 addresses or [ip]:port for IPv6 addresses for use as keys in maps.
type AddressManager ¶
type AddressManager struct {
// contains filtered or unexported fields
}
AddressManager provides a concurrency safe address manager for caching potential peers on the Kaspa network.
func New ¶
func New(cfg *config.Config, databaseContext *dbaccess.DatabaseContext) *AddressManager
New returns a new Kaspa address manager.
func (*AddressManager) AddAddress ¶
func (am *AddressManager) AddAddress(address, sourceAddress *appmessage.NetAddress, subnetworkID *subnetworkid.SubnetworkID)
AddAddress adds a new address to the address manager. It enforces a max number of addresses and silently ignores duplicate addresses. It is safe for concurrent access.
func (*AddressManager) AddAddressByIP ¶
func (am *AddressManager) AddAddressByIP(addressIP string, subnetworkID *subnetworkid.SubnetworkID) error
AddAddressByIP adds an address where we are given an ip:port and not a appmessage.NetAddress.
func (*AddressManager) AddAddresses ¶
func (am *AddressManager) AddAddresses(addresses []*appmessage.NetAddress, sourceAddress *appmessage.NetAddress, subnetworkID *subnetworkid.SubnetworkID)
AddAddresses adds new addresses to the address manager. It enforces a max number of addresses and silently ignores duplicate addresses. It is safe for concurrent access.
func (*AddressManager) AddLocalAddress ¶
func (am *AddressManager) AddLocalAddress(netAddress *appmessage.NetAddress, priority AddressPriority) error
AddLocalAddress adds netAddress to the list of known local addresses to advertise with the given priority.
func (*AddressManager) AddressCache ¶
func (am *AddressManager) AddressCache(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) []*appmessage.NetAddress
AddressCache returns the current address cache. It must be treated as read-only (but since it is a copy now, this is not as dangerous).
func (*AddressManager) Attempt ¶
func (am *AddressManager) Attempt(address *appmessage.NetAddress)
Attempt increases the given address' attempt counter and updates the last attempt time.
func (*AddressManager) Ban ¶
func (am *AddressManager) Ban(address *appmessage.NetAddress) error
Ban marks the given address as banned
func (*AddressManager) Connected ¶
func (am *AddressManager) Connected(address *appmessage.NetAddress)
Connected Marks the given address as currently connected and working at the current time. The address must already be known to AddressManager else it will be ignored.
func (*AddressManager) DeserializeNetAddress ¶
func (am *AddressManager) DeserializeNetAddress(addressKey AddressKey) (*appmessage.NetAddress, error)
DeserializeNetAddress converts a given address string to a *appmessage.NetAddress
func (*AddressManager) GetAddress ¶
func (am *AddressManager) GetAddress() *KnownAddress
GetAddress returns a single address that should be routable. It picks a random one from the possible addresses with preference given to ones that have not been used recently and should not pick 'close' addresses consecutively.
func (*AddressManager) GetBestLocalAddress ¶
func (am *AddressManager) GetBestLocalAddress(remoteAddress *appmessage.NetAddress) *appmessage.NetAddress
GetBestLocalAddress returns the most appropriate local address to use for the given remote address.
func (*AddressManager) Good ¶
func (am *AddressManager) Good(address *appmessage.NetAddress, subnetworkID *subnetworkid.SubnetworkID)
Good marks the given address as good. To be called after a successful connection and version exchange. If the address is unknown to the address manager it will be ignored.
func (*AddressManager) GroupKey ¶
func (am *AddressManager) GroupKey(na *appmessage.NetAddress) 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 "local" for a local address, and the string "unroutable" for an unroutable address.
func (*AddressManager) HostToNetAddress ¶
func (am *AddressManager) HostToNetAddress(host string, port uint16, services appmessage.ServiceFlag) (*appmessage.NetAddress, error)
HostToNetAddress returns a netaddress given a host address. If the host is not an IP address it will be resolved.
func (*AddressManager) IsBanned ¶
func (am *AddressManager) IsBanned(address *appmessage.NetAddress) (bool, error)
IsBanned returns whether the given address is banned
func (*AddressManager) IsRoutable ¶
func (am *AddressManager) IsRoutable(na *appmessage.NetAddress) 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 (*AddressManager) NeedMoreAddresses ¶
func (am *AddressManager) NeedMoreAddresses() bool
NeedMoreAddresses returns whether or not the address manager needs more addresses.
func (*AddressManager) PeersStateForSerialization ¶
func (am *AddressManager) PeersStateForSerialization() (*PeersStateForSerialization, error)
PeersStateForSerialization returns the data model that is used to serialize the peers state to any encoding.
func (*AddressManager) Start ¶
func (am *AddressManager) Start() error
Start begins the core address handler which manages a pool of known addresses, timeouts, and interval based writes.
func (*AddressManager) Stop ¶
func (am *AddressManager) Stop() error
Stop gracefully shuts down the address manager by stopping the main handler.
func (*AddressManager) TotalNumAddresses ¶
func (am *AddressManager) TotalNumAddresses() int
TotalNumAddresses returns the number of addresses known to the address manager.
func (*AddressManager) Unban ¶
func (am *AddressManager) Unban(address *appmessage.NetAddress) error
Unban marks the given address as not banned
type AddressPriority ¶
type AddressPriority int
AddressPriority type is used to describe the hierarchy of local address discovery methods.
const ( // InterfacePrio signifies the address is on a local interface InterfacePrio AddressPriority = iota // BoundPrio signifies the address has been explicitly bounded to. BoundPrio // UpnpPrio signifies the address was obtained from UPnP. UpnpPrio // HTTPPrio signifies the address was obtained from an external HTTP service. HTTPPrio // ManualPrio signifies the address was provided by --externalip. ManualPrio )
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() mstime.Time
LastAttempt returns the last time the known address was attempted.
func (*KnownAddress) NetAddress ¶
func (ka *KnownAddress) NetAddress() *appmessage.NetAddress
NetAddress returns the underlying appmessage.NetAddress associated with the known address.
func (*KnownAddress) SubnetworkID ¶
func (ka *KnownAddress) SubnetworkID() *subnetworkid.SubnetworkID
SubnetworkID returns the subnetwork ID of the known address.
type PeersStateForSerialization ¶
type PeersStateForSerialization struct { Version int Key [32]byte Addresses []*serializedKnownAddress SubnetworkNewAddressBucketArrays map[string]*serializedNewAddressBucketArray // string is Subnetwork ID FullNodeNewAddressBucketArray serializedNewAddressBucketArray SubnetworkTriedAddressBucketArrays map[string]*serializedTriedAddressBucketArray // string is Subnetwork ID FullNodeTriedAddressBucketArray serializedTriedAddressBucketArray }
PeersStateForSerialization is the data model that is used to serialize the peers state to any encoding.