inet256

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2021 License: GPL-3.0, MPL-2.0 Imports: 11 Imported by: 16

Documentation

Index

Constants

View Source
const (
	// MinMTU is the minimum MTU a network can provide to any address.
	// Applications should be designed to operate correctly if they can only send messages up to this size.
	MinMTU = 1 << 15
	// MaxMTU is the size of largest message that a network will ever receive from any address.
	// Applications should be prepared to receieve this much data at a time or they may encounter io.ErrShortBuffer
	MaxMTU = 1 << 16
)
View Source
const (
	// TransportMTU is the guaranteed MTU presented to networks.
	TransportMTU = (1 << 16) - 1
)

Variables

View Source
var (
	ErrPublicKeyNotFound = p2p.ErrPublicKeyNotFound
	ErrNoAddrWithPrefix  = errors.New("no address with prefix")
	ErrClosed            = net.ErrClosed
)

Functions

func HasPrefix

func HasPrefix(x []byte, prefix []byte, nbits int) bool

func IsErrClosed

func IsErrClosed(err error) bool

func IsPublicKeyNotFound

func IsPublicKeyNotFound(err error) bool

func IsUnreachable

func IsUnreachable(err error) bool

func MarshalPublicKey

func MarshalPublicKey(pubKey PublicKey) []byte

func NewPacketConn

func NewPacketConn(n Node) net.PacketConn

NewPacketConn wraps a node with the net.PacketConn interface

func Receive

func Receive(ctx context.Context, n Node, msg *Message) error

Receive is a utility function for copying a message from the Node n into msg

Types

type Addr

type Addr [32]byte

Addr is an address in an INET256 Network. It uniquely identifies a Node.

func AddrFromBytes

func AddrFromBytes(x []byte) Addr

AddrFromBytes creates a new address by reading up to 32 bytes from x Note that these bytes are not interpretted as a public key, they are interpretted as the raw address. To derive an address from a PublicKey use NewAddr

func NewAddr

func NewAddr(pubKey PublicKey) Addr

NewAddr creates a new Addr from a PublicKey

func (Addr) GetPeerID

func (a Addr) GetPeerID() p2p.PeerID

GetPeerID implements p2p.HasPeerID

func (Addr) IsZero

func (a Addr) IsZero() bool

func (Addr) MarshalText

func (a Addr) MarshalText() ([]byte, error)

func (Addr) Network

func (a Addr) Network() string

Network implements net.Addr.Network

func (Addr) String

func (a Addr) String() string

String implements net.Addr.String

func (*Addr) UnmarshalText

func (a *Addr) UnmarshalText(x []byte) error

type ErrAddrUnreachable

type ErrAddrUnreachable struct {
	Addr Addr
}

func (ErrAddrUnreachable) Error

func (e ErrAddrUnreachable) Error() string

type ID

type ID = Addr

ID is an alias for Addr

type Logger

type Logger = logrus.Logger

type Message

type Message struct {
	Src     Addr
	Dst     Addr
	Payload []byte
}

Message is the essential information carried by Tell and Receive provided as a struct for use in queues or other APIs

type Network

type Network interface {
	Tell(ctx context.Context, addr Addr, data []byte) error
	Receive(ctx context.Context, fn ReceiveFunc) error

	MTU(ctx context.Context, addr Addr) int
	LookupPublicKey(ctx context.Context, addr Addr) (PublicKey, error)
	FindAddr(ctx context.Context, prefix []byte, nbits int) (Addr, error)

	LocalAddr() Addr
	PublicKey() PublicKey

	Bootstrap(ctx context.Context) error
	Close() error
}

Network is an instantiated network routing algorithm

This interface is not described in the spec, and is incidental to the implementation.

type NetworkFactory

type NetworkFactory func(NetworkParams) Network

NetworkFactory is a constructor for a network

type NetworkParams

type NetworkParams struct {
	PrivateKey PrivateKey
	Swarm      Swarm
	Peers      PeerSet

	Logger *Logger
}

NetworkParams are passed to a NetworkFactory to create a Network. This type really defines the problem domain quite well. Essentially it is a set of one-hop peers and a means to send messages to them.

type Node

type Node interface {
	// Tell sends a message containing data to the node at addr.
	// The message will be delivered at most once.
	Tell(ctx context.Context, addr Addr, data []byte) error
	// Receive calls fn with a message sent to this node.
	// The message fields, and payload must not be accessed outside fn.
	Receive(ctx context.Context, fn ReceiveFunc) error

	// MTU finds the maximum message size that can be sent to addr.
	// If the context expires, a reasonable default (normally a significant underestimate) will be returned.
	MTU(ctx context.Context, addr Addr) int
	// LookupPublicKey attempts to find the public key corresponding to addr.
	// If it can't find it, ErrPublicKeyNotFound is returned.
	LookupPublicKey(ctx context.Context, addr Addr) (PublicKey, error)
	// FindAddr looks for an address with nbits leading bits in common with prefix.
	FindAddr(ctx context.Context, prefix []byte, nbits int) (Addr, error)

	// LocalAddr returns this Node's address
	LocalAddr() Addr
	// PublicKey returns this Node's public key
	PublicKey() PublicKey

	// Close indicates no more messages should be sent or received from this node
	// and releases any resources allocated for this node.
	Close() error
}

Node is a single host in an INET256 network. Nodes send and receive messages to and from other nodes in the network. Nodes are usually created and managed by a Service. Nodes have an single ID or address corresponding to their public key.

This interface is compatible with the INET256 specification.

type PeerSet

type PeerSet interface {
	ListPeers() []Addr
	Contains(Addr) bool
}

PeerSet represents a set of peers

type PeerStore

type PeerStore interface {
	Add(x Addr)
	Remove(x Addr)
	SetAddrs(x Addr, addrs []p2p.Addr)
	ListAddrs(x Addr) []p2p.Addr

	PeerSet
}

PeerStore stores information about peers

type PrivateKey

type PrivateKey = p2p.PrivateKey

type PublicKey

type PublicKey = p2p.PublicKey

func ParsePublicKey

func ParsePublicKey(data []byte) (PublicKey, error)

type ReceiveFunc

type ReceiveFunc = func(Message)

ReceiveFunc is passed as a callback to Node.Receive

type Service

type Service interface {
	CreateNode(ctx context.Context, privKey p2p.PrivateKey) (Node, error)
	DeleteNode(ctx context.Context, privKey p2p.PrivateKey) error

	LookupPublicKey(ctx context.Context, addr Addr) (p2p.PublicKey, error)
	FindAddr(ctx context.Context, prefix []byte, nbits int) (Addr, error)
	MTU(ctx context.Context, addr Addr) int
}

Service is the top level INET256 object. It manages a set of nodes which can be created and deleted.

This interface is compatible with the INET256 specification.

type Swarm

type Swarm interface {
	Tell(ctx context.Context, dst Addr, m p2p.IOVec) error
	Receive(ctx context.Context, th ReceiveFunc) error

	Close() error
	LookupPublicKey(ctx context.Context, addr Addr) (PublicKey, error)
	PublicKey() PublicKey
	LocalAddr() Addr
}

Swarm is similar to a p2p.Swarm, but uses inet256.Addrs instead of p2p.Addrs

This interface is not described in the spec, and is incidental to the implementation.

Jump to

Keyboard shortcuts

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