hostnetwork

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2018 License: Apache-2.0 Imports: 32 Imported by: 7

README

Insolar – Host Network

Physical networking layer

Go Report Card GoDoc

Overview

We took Kademlia DHT original specifications and made significant improvements to make it ready for real world application by enterprises.

Key features of our blockchain network layer:
  • Support of heterogeneous network topology with different types of hosts being able to communicate with each other. In classic peer-to-peer networks, any host can communicate directly with any other host on the network. In a real enterprise environment, this condition is often unacceptable for a variety of reasons including security.
  • Network routing with a host or host group becoming relays for others hosts. The network can continue to function despite various network restrictions such as firewalls, NATs, etc.
  • Ability to limit number of gateways to corporate host group via relays to keep the host group secure while being able to interact with the rest of the network through relays. This feature mitigates the risk of DDoS attacks.

Key components

Transport

Network transport interface. It allows to abstract our network from physical transport. It can either be IP based network or any other kind of packet courier (e.g. an industrial packet bus).

Host

Host is a fundamental part of networking system. Each host has:

  • one real network address (IP or any other transport protocol address)
  • multiple abstract network IDs (either host's own or ones belonging to relayed hosts)
Routing

It is actually a Kademlia hash table used to store network hosts and calculate distances between them. See Kademlia whitepaper and XLattice design specification for details.

Packet

A set of data transferred by this module between hosts.

  • Request packet
  • Response packet

Now packets are serialized simply with encoding/gob. In future there will be a powerful robust serialization system based on Google's Protocol Buffers.

RPC

RPC module allows higher level components to register methods that can be called by other network hosts.

Configuration

Default Host Network config:

host:
  transport:
    protocol: UTP
    address: 127.0.0.1:0
    behindnat: true
  bootstraphosts: []
  isrelay: false

The listen port is zero(which means random) for unit test purpose. For host bootstrapping you need to fill bootstraphosts slice. See godoc for more details.

Usage

package main

import (
	"github.com/insolar/insolar/network/hostnetwork"
	"github.com/insolar/insolar/configuration"
)

func main() {
	cfg := configuration.NewConfiguration().Host
	cfg.Address = "0.0.0.0:31337"

	network, err := hostnetwork.NewHostNetwork(cfg)
	if err != nil {
		panic(err)
	}
	defer network.Disconnect()

	network.Listen()
}

For more detailed usage example see cmd/example/network/hostnetwork/main.go

Documentation

Overview

Package hostnetwork is an implementation of Kademlia DHT. It is mostly based on original specification but has multiple backward-incompatible changes.

Usage:

package main

import (
	"github.com/insolar/insolar/network/hostnetwork"
	"github.com/insolar/insolar/configuration"
)

func main() {
	cfg := configuration.NewConfiguration().Host
	cfg.Address = "0.0.0.0:31337"

	network, err := hostnetwork.NewHostNetwork(cfg)
	if err != nil {
		panic(err)
	}
	defer network.Disconnect()

	network.Listen()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthenticationRequest added in v0.2.0

func AuthenticationRequest(hostHandler hosthandler.HostHandler, command, targetID string) error

AuthenticationRequest sends an authentication request.

func BuildContext added in v0.2.0

func BuildContext(cb ContextBuilder, msg *packet.Packet) hosthandler.Context

BuildContext builds a context for packet.

func CascadeSendMessage added in v0.2.0

func CascadeSendMessage(hostHandler hosthandler.HostHandler, data core.Cascade, targetID string, method string, args [][]byte) error

CascadeSendMessage sends a message to the next cascade layer.

func CheckOriginRequest added in v0.2.0

func CheckOriginRequest(hostHandler hosthandler.HostHandler, targetID string) error

CheckOriginRequest send a request to check target host originality

func DispatchPacketType added in v0.2.0

func DispatchPacketType(
	hostHandler hosthandler.HostHandler,
	ctx hosthandler.Context,
	msg *packet.Packet,
	packetBuilder packet.Builder,
) (*packet.Packet, error)

DispatchPacketType checks message type.

func GetDefaultCtx added in v0.4.0

func GetDefaultCtx(hostHandler hosthandler.HostHandler) hosthandler.Context

GetDefaultCtx creates default context for the host handler.

func GetNonceRequest added in v0.6.1

func GetNonceRequest(hostHandler hosthandler.HostHandler, targetID string) ([]*core.ActiveNode, error)

func ObtainIPRequest added in v0.2.0

func ObtainIPRequest(hostHandler hosthandler.HostHandler, targetID string) error

ObtainIPRequest is request to self IP obtaining.

func ParseIncomingPacket added in v0.2.0

func ParseIncomingPacket(hostHandler hosthandler.HostHandler, ctx hosthandler.Context, msg *packet.Packet, packetBuilder packet.Builder) (*packet.Packet, error)

ParseIncomingPacket detects a packet type.

func RelayOwnershipRequest added in v0.2.0

func RelayOwnershipRequest(hostHandler hosthandler.HostHandler, targetID string) error

RelayOwnershipRequest sends a relay ownership request.

func RelayRequest added in v0.2.0

func RelayRequest(hostHandler hosthandler.HostHandler, command, targetID string) error

RelayRequest sends relay request to target.

func ResendPulseToKnownHosts added in v0.4.0

func ResendPulseToKnownHosts(hostHandler hosthandler.HostHandler, hosts []host.Host, pulse *packet.RequestPulse)

ResendPulseToKnownHosts resends received pulse to all known hosts

func SendRelayOwnership added in v0.3.0

func SendRelayOwnership(hostHandler hosthandler.HostHandler, subnetIDs []string)

SendRelayOwnership send a relay ownership request.

Types

type AuthInfo

type AuthInfo struct {
	// Sent/received unique auth keys.
	SentKeys     map[string][]byte
	ReceivedKeys map[string][]byte

	AuthenticatedHosts map[string]bool
}

AuthInfo collects some information about authentication.

type ContextBuilder

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

ContextBuilder allows to lazy configure and build new Context.

func NewContextBuilder

func NewContextBuilder(hostHandler hosthandler.HostHandler) ContextBuilder

NewContextBuilder creates new ContextBuilder.

func (ContextBuilder) Build

func (cb ContextBuilder) Build() (ctx hosthandler.Context, err error)

Build builds and returns new Context.

func (ContextBuilder) SetDefaultHost

func (cb ContextBuilder) SetDefaultHost() ContextBuilder

SetDefaultHost sets first host id in Context.

func (ContextBuilder) SetHostByID

func (cb ContextBuilder) SetHostByID(hostID id.ID) ContextBuilder

SetHostByID sets host id in Context.

type DHT

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

DHT represents the state of the local host in the distributed hash table.

func NewDHT

func NewDHT(
	store store.Store,
	origin *host.Origin,
	transport transport.Transport,
	ncf hosthandler.NetworkCommonFacade,
	options *Options,
	proxy relay.Proxy,
	timeout int,
	infbootstrap bool,
	nodeID core.RecordRef,
	majorityRule int,
	certificate core.Certificate,
) (dht *DHT, err error)

NewDHT initializes a new DHT host.

func NewHostNetwork added in v0.0.5

func NewHostNetwork(
	cfg configuration.HostNetwork,
	nn *nodenetwork.NodeNetwork,
	cascade *cascade.Cascade,
	certificate core.Certificate,
) (*DHT, error)

NewHostNetwork creates and returns DHT network.

func (*DHT) AddActiveNodes added in v0.5.0

func (dht *DHT) AddActiveNodes(activeNodes []*core.ActiveNode) error

AddActiveNodes adds an active nodes slice.

func (*DHT) AddAuthSentKey added in v0.2.0

func (dht *DHT) AddAuthSentKey(id string, key []byte)

AddAuthSentKey adds a sent key to auth.

func (*DHT) AddHost added in v0.2.0

func (dht *DHT) AddHost(ctx hosthandler.Context, host *routing.RouteHost)

AddHost adds a host into the appropriate k bucket we store these buckets in big-endian order so we look at the bits from right to left in order to find the appropriate bucket

func (*DHT) AddPossibleProxyID added in v0.2.0

func (dht *DHT) AddPossibleProxyID(id string)

AddPossibleProxyID adds an id which could be a proxy.

func (*DHT) AddPossibleRelayID added in v0.2.0

func (dht *DHT) AddPossibleRelayID(id string)

AddPossibleRelayID add a host id which can be a relay.

func (*DHT) AddProxyHost added in v0.2.0

func (dht *DHT) AddProxyHost(targetID string)

AddProxyHost adds a proxy host.

func (*DHT) AddReceivedKey added in v0.2.0

func (dht *DHT) AddReceivedKey(target string, key []byte)

AddReceivedKey adds a new received key from target.

func (*DHT) AddRelayClient added in v0.2.0

func (dht *DHT) AddRelayClient(host *host.Host) error

AddRelayClient adds a new relay client.

func (*DHT) AddSubnetID added in v0.2.0

func (dht *DHT) AddSubnetID(ip, targetID string)

AddSubnetID adds a subnet ID.

func (*DHT) AddUnsync added in v0.6.1

func (dht *DHT) AddUnsync(nodeID core.RecordRef, roles []core.NodeRole, address string,
	version string) (chan *core.ActiveNode, error)

func (*DHT) AnalyzeNetwork

func (dht *DHT) AnalyzeNetwork(ctx hosthandler.Context) error

AnalyzeNetwork is func to analyze the network after IP obtaining.

func (*DHT) Bootstrap

func (dht *DHT) Bootstrap() error

Bootstrap attempts to bootstrap the network using the BootstrapHosts provided to the Options struct. This will trigger an iterateBootstrap to the provided BootstrapHosts.

func (*DHT) CascadeSendMessage added in v0.2.0

func (dht *DHT) CascadeSendMessage(data core.Cascade, targetID string, method string, args [][]byte) error

CascadeSendMessage sends a message to the next cascade layer.

func (*DHT) CheckNodeRole added in v0.0.5

func (dht *DHT) CheckNodeRole(domainID string) error

CheckNodeRole starting a check all known nodes.

func (*DHT) ConfirmNodeRole added in v0.2.0

func (dht *DHT) ConfirmNodeRole(roleKey string) bool

ConfirmNodeRole is a node role confirmation.

func (*DHT) Disconnect

func (dht *DHT) Disconnect()

Disconnect will trigger a Stop from the network.

func (*DHT) EqualAuthSentKey added in v0.2.0

func (dht *DHT) EqualAuthSentKey(targetID string, key []byte) bool

EqualAuthSentKey returns true if a given key equals to targetID key.

func (*DHT) FindHost

func (dht *DHT) FindHost(ctx hosthandler.Context, key string) (*host.Host, bool, error)

FindHost returns target host's real network address.

func (*DHT) Get

func (dht *DHT) Get(ctx hosthandler.Context, key string) ([]byte, bool, error)

Get retrieves data from the transport using key. Key is the base58 encoded identifier of the data.

func (*DHT) GetActiveNodesList added in v0.5.0

func (dht *DHT) GetActiveNodesList() []*core.ActiveNode

GetActiveNodesList returns an active nodes list.

func (*DHT) GetExpirationTime added in v0.2.0

func (dht *DHT) GetExpirationTime(ctx hosthandler.Context, key []byte) time.Time

GetExpirationTime returns a expiration time after which a key/value pair expires.

func (*DHT) GetHighKnownHostID added in v0.2.0

func (dht *DHT) GetHighKnownHostID() string

GetHighKnownHostID returns a high known host ID.

func (*DHT) GetHostsFromBootstrap added in v0.4.0

func (dht *DHT) GetHostsFromBootstrap()

func (*DHT) GetNetworkCommonFacade added in v0.2.0

func (dht *DHT) GetNetworkCommonFacade() hosthandler.NetworkCommonFacade

GetNetworkCommonFacade returns a networkcommonfacade ptr.

func (*DHT) GetNodeID added in v0.5.0

func (dht *DHT) GetNodeID() core.RecordRef

GetNodeID returns a node ID.

func (*DHT) GetOriginHost added in v0.0.5

func (dht *DHT) GetOriginHost() *host.Origin

GetOriginHost returns the local host.

func (*DHT) GetOuterHostsCount added in v0.2.0

func (dht *DHT) GetOuterHostsCount() int

GetOuterHostsCount returns a outer hosts count.

func (*DHT) GetPacketTimeout added in v0.2.0

func (dht *DHT) GetPacketTimeout() time.Duration

GetPacketTimeout returns the maximum time to wait for a response to any packet.

func (*DHT) GetPrivateKey added in v0.6.1

func (dht *DHT) GetPrivateKey() *ecdsa.PrivateKey

GetPrivateKey returns a private key.

func (*DHT) GetProxyHostsCount added in v0.2.0

func (dht *DHT) GetProxyHostsCount() int

GetProxyHostsCount returns a proxy hosts count.

func (*DHT) GetReplicationTime added in v0.2.0

func (dht *DHT) GetReplicationTime() time.Duration

GetReplicationTime returns a interval between Kademlia replication events.

func (*DHT) GetSelfKnownOuterHosts added in v0.2.0

func (dht *DHT) GetSelfKnownOuterHosts() int

GetSelfKnownOuterHosts return a self known hosts count.

func (*DHT) HostIsAuthenticated added in v0.2.0

func (dht *DHT) HostIsAuthenticated(targetID string) bool

HostIsAuthenticated returns true if target ID is authenticated host.

func (*DHT) HtFromCtx added in v0.2.0

func (dht *DHT) HtFromCtx(ctx hosthandler.Context) *routing.HashTable

HtFromCtx returns a routing hashtable known by ctx.

func (*DHT) InvokeRPC added in v0.2.0

func (dht *DHT) InvokeRPC(sender *host.Host, method string, args [][]byte) ([]byte, error)

InvokeRPC - invoke a method to rpc.

func (*DHT) KeyIsReceived added in v0.2.0

func (dht *DHT) KeyIsReceived(key string) ([]byte, bool)

KeyIsReceived returns true and a key from targetID if exist.

func (*DHT) Listen

func (dht *DHT) Listen() error

Listen begins listening on the socket for incoming Packets.

func (*DHT) NumHosts

func (dht *DHT) NumHosts(ctx hosthandler.Context) int

NumHosts returns the total number of hosts stored in the local routing table.

func (*DHT) ObtainIP

func (dht *DHT) ObtainIP() error

ObtainIP starts to self IP obtaining.

func (*DHT) RemoteProcedureCall

func (dht *DHT) RemoteProcedureCall(ctx hosthandler.Context, targetID string, method string, args [][]byte) (result []byte, err error)

RemoteProcedureCall calls remote procedure on target host.

func (*DHT) RemoteProcedureRegister

func (dht *DHT) RemoteProcedureRegister(name string, method core.RemoteProcedure)

RemoteProcedureRegister registers procedure for remote call on this host

func (*DHT) RemoveAuthHost added in v0.2.0

func (dht *DHT) RemoveAuthHost(key string)

RemoveAuthHost removes a host from auth.

func (*DHT) RemoveAuthSentKeys added in v0.2.0

func (dht *DHT) RemoveAuthSentKeys(targetID string)

RemoveAuthSentKeys removes a targetID from sent keys.

func (*DHT) RemovePossibleProxyID added in v0.2.0

func (dht *DHT) RemovePossibleProxyID(id string)

RemovePossibleProxyID removes if from possible proxy ids list.

func (*DHT) RemoveProxyHost added in v0.2.0

func (dht *DHT) RemoveProxyHost(targetID string)

RemoveProxyHost removes host from proxy list.

func (*DHT) RemoveRelayClient added in v0.2.0

func (dht *DHT) RemoveRelayClient(host *host.Host) error

RemoveRelayClient removes a client from relay list.

func (*DHT) SendRequest added in v0.2.0

func (dht *DHT) SendRequest(packet *packet.Packet) (transport.Future, error)

SendRequest sends a packet.

func (*DHT) SetAuthStatus added in v0.2.0

func (dht *DHT) SetAuthStatus(targetID string, status bool)

SetAuthStatus sets a new auth status to targetID.

func (*DHT) SetHighKnownHostID added in v0.2.0

func (dht *DHT) SetHighKnownHostID(id string)

SetHighKnownHostID sets a new high known host ID.

func (*DHT) SetNodeKeeper added in v0.6.1

func (dht *DHT) SetNodeKeeper(keeper consensus.NodeKeeper)

func (*DHT) SetOuterHostsCount added in v0.2.0

func (dht *DHT) SetOuterHostsCount(hosts int)

SetOuterHostsCount sets a new value to outer hosts count.

func (*DHT) StartAuthorize added in v0.6.1

func (dht *DHT) StartAuthorize() error

StartAuthorize start authorize to discovery nodes.

func (*DHT) Store

func (dht *DHT) Store(key store.Key, data []byte, replication time.Time, expiration time.Time, publisher bool) error

Store should store a key/value pair for the local host with the given replication and expiration times.

func (*DHT) StoreData added in v0.2.0

func (dht *DHT) StoreData(ctx hosthandler.Context, data []byte) (id string, err error)

StoreData stores data on the network. This will trigger an iterateStore loop. The base58 encoded identifier will be returned if the store is successful.

func (*DHT) StoreRetrieve added in v0.2.0

func (dht *DHT) StoreRetrieve(key store.Key) ([]byte, bool)

StoreRetrieve should return the local key/value if it exists.

type HighKnownOuterHostsHost

type HighKnownOuterHostsHost struct {
	ID                  string
	OuterHosts          int // high known outer hosts by ID host
	SelfKnownOuterHosts int
}

HighKnownOuterHostsHost collects an information about host in home subnet which have a more known outer hosts.

type MockPulseManager added in v0.4.0

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

MockPulseManager mock struct to read and write pulse that implements core.PulseManager interface.

func (*MockPulseManager) Current added in v0.4.0

func (pm *MockPulseManager) Current() (*core.Pulse, error)

func (*MockPulseManager) Set added in v0.4.0

func (pm *MockPulseManager) Set(pulse core.Pulse) error

func (*MockPulseManager) SetCallback added in v0.4.0

func (pm *MockPulseManager) SetCallback(callback func(core.Pulse))

type Options

type Options struct {
	// The hosts being used to bootstrap the network. Without a bootstrap
	// host there is no way to connect to the network. NetworkHosts can be
	// initialized via host.NewHost().
	BootstrapHosts []*host.Host

	// The time after which a key/value pair expires;
	// this is a time-to-live (TTL) from the original publication date.
	ExpirationTime time.Duration

	// Seconds after which an otherwise unaccessed bucket must be refreshed.
	RefreshTime time.Duration

	// The interval between Kademlia replication events, when a host is
	// required to publish its entire database.
	ReplicateTime time.Duration

	// The time after which the original publisher must
	// republish a key/value pair. Currently not implemented.
	RepublishTime time.Duration

	// The maximum time to wait for a response from a host before discarding
	// it from the bucket.
	PingTimeout time.Duration

	// The maximum time to wait for a response to any packet.
	PacketTimeout time.Duration
}

Options contains configuration options for the local host.

type Subnet

type Subnet struct {
	SubnetIDs        map[string][]string // key - ip, value - id
	HomeSubnetKey    string              // key of home subnet fo SubnetIDs
	PossibleRelayIDs []string
	PossibleProxyIDs []string
	HighKnownHosts   HighKnownOuterHostsHost
}

Subnet collects some information about self network part

Directories

Path Synopsis
Package connection encapsulates connection creation process and provides connection factories.
Package connection encapsulates connection creation process and provides connection factories.
Package host is a fundamental part of networking system.
Package host is a fundamental part of networking system.
Package packet provides network messaging protocol and serialization layer.
Package packet provides network messaging protocol and serialization layer.
Package relay is an implementation of relay mechanism.
Package relay is an implementation of relay mechanism.
Package resolver provides interface (and default implementation) to retrieve public network address.
Package resolver provides interface (and default implementation) to retrieve public network address.
Package routing implements Kademlia hash tables with XOR distance metrics.
Package routing implements Kademlia hash tables with XOR distance metrics.
Package rpc allows higher level components to register methods that can be called by other network hosts.
Package rpc allows higher level components to register methods that can be called by other network hosts.
Package store provides interfaces and default in-memory implementation of storage for DHT metadata.
Package store provides interfaces and default in-memory implementation of storage for DHT metadata.
Package transport provides network transport interface.
Package transport provides network transport interface.

Jump to

Keyboard shortcuts

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