peering

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventStateUp   = "up"
	EventStateDown = "down"
)

Peering Event States.

View Source
const (
	FrameOffset   = 12
	FrameOverhead = 16
)

Required Frame Margins.

Variables

View Source
var (
	ErrUnsupportedVersion  = errors.New("unsupported version")
	ErrRemoteDeniedPeering = errors.New("remote denied peering")
)

Peering Errors.

View Source
var (
	ErrNetworkReadError  = errors.New("read i/o error")
	ErrNetworkWriteError = errors.New("write i/o error")
)

Errors.

View Source
var ProtocolTCP = NewProtocol(
	"tcp",
	tcpPeerWith,
	tcpStartListener,
)

ProtocolTCP uses plain TCP.

Functions

func NewConnectedPipeStacks

func NewConnectedPipeStacks() (Protocol, Protocol)

NewConnectedPipeStacks returns a new set of net.Pipe based connectivity stacks for testing.

Types

type EventPeering added in v0.4.0

type EventPeering struct {
	Peer  netip.Addr
	State EventState
}

EventPeering is a peering event.

type EventState added in v0.4.0

type EventState string

EventState describes a peering event state.

type Link interface {
	String() string

	// Peer returns the ID of the connected peer.
	Peer() netip.Addr

	// SwitchLabel returns the switch label of the link.
	SwitchLabel() m.SwitchLabel

	// GeoMark returns geo location of the peer, based on the router address.
	GeoMark() string

	// PeeringURL returns the used peering URL.
	PeeringURL() *m.PeeringURL

	// Outgoing returns whether the connection was initiated by this router.
	Outgoing() bool

	// Lite returns whether the connected router is in lite mode.
	Lite() bool

	// SendPriority sends a priority frame to the peer.
	SendPriority(f frame.Frame) error

	// Send sends a frame to the peer.
	Send(f frame.Frame) error

	// LocalAddr returns the underlying local net.Addr of the connection.
	LocalAddr() net.Addr

	// RemoteAddr returns the underlying remote net.Addr of the connection.
	RemoteAddr() net.Addr

	// Started returns when the link was created.
	Started() time.Time

	// Uptime returns how long the link has been up.
	Uptime() time.Duration

	// Latency returns the latency of the link in milliseconds.
	Latency() uint16

	// AddMeasuredLatency adds the given latency to the measured latencies and
	// calculates and sets the new average.
	AddMeasuredLatency(latency time.Duration)

	// BytesIn returns the total amount of bytes received via the link.
	BytesIn() uint64

	// BytesOut returns the total amount of bytes sent via the link.
	BytesOut() uint64

	// FlowControlIndicator returns a flow control flag that indicates the
	// pressure on the sending queue of this link.
	FlowControlIndicator() frame.FlowControlFlag

	// IsClosing returns whether the link is closing or has closed.
	IsClosing() bool

	// Close closes the link.
	Close(log func())
}

Link represents a network connection to another router.

type LinkBase

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

LinkBase implements common functions to comply with the Link interface.

func (*LinkBase) AddMeasuredLatency added in v0.3.0

func (link *LinkBase) AddMeasuredLatency(latency time.Duration)

AddMeasuredLatency adds the given latency to the measured latencies and calculates and sets the new average.

func (*LinkBase) BytesIn added in v0.4.0

func (link *LinkBase) BytesIn() uint64

BytesIn returns the total amount of bytes received via the link.

func (*LinkBase) BytesOut added in v0.4.0

func (link *LinkBase) BytesOut() uint64

BytesOut returns the total amount of bytes sent via the link.

func (*LinkBase) Close

func (link *LinkBase) Close(log func())

Close closes the link.

func (*LinkBase) FlowControlIndicator

func (link *LinkBase) FlowControlIndicator() frame.FlowControlFlag

FlowControlIndicator returns a flow control flag that indicates the pressure on the sending queue of this link.

func (*LinkBase) GeoMark added in v0.3.0

func (link *LinkBase) GeoMark() string

GeoMark returns geo location of the peer, based on the router address.

func (*LinkBase) IsClosing

func (link *LinkBase) IsClosing() bool

IsClosing returns whether the link is closing or has closed.

func (*LinkBase) Latency

func (link *LinkBase) Latency() uint16

Latency returns the latency of the link in milliseconds.

func (*LinkBase) Lite added in v0.3.0

func (link *LinkBase) Lite() bool

Lite returns whether the connected router is in lite mode.

func (*LinkBase) LocalAddr

func (link *LinkBase) LocalAddr() net.Addr

LocalAddr returns the underlying local net.Addr of the connection.

func (*LinkBase) Outgoing

func (link *LinkBase) Outgoing() bool

Outgoing returns whether the connection was initiated by this router.

func (*LinkBase) Peer

func (link *LinkBase) Peer() netip.Addr

Peer returns the ID of the connected peer.

func (*LinkBase) PeeringURL

func (link *LinkBase) PeeringURL() *m.PeeringURL

PeeringURL returns the used peering URL.

func (*LinkBase) RemoteAddr

func (link *LinkBase) RemoteAddr() net.Addr

RemoteAddr returns the underlying remote net.Addr of the connection.

func (*LinkBase) Send

func (link *LinkBase) Send(f frame.Frame) error

Send sends a frame to the peer.

func (*LinkBase) SendPriority

func (link *LinkBase) SendPriority(f frame.Frame) error

SendPriority sends a priority frame to the peer.

func (*LinkBase) Started added in v0.2.0

func (link *LinkBase) Started() time.Time

Started returns when the link was created.

func (*LinkBase) String

func (link *LinkBase) String() string

String returns a human readable summary.

func (*LinkBase) SwitchLabel

func (link *LinkBase) SwitchLabel() m.SwitchLabel

SwitchLabel returns the switch label of the link.

func (*LinkBase) Uptime added in v0.2.0

func (link *LinkBase) Uptime() time.Duration

Uptime returns how long the link has been up.

type LinkFrame

type LinkFrame []byte

LinkFrame is a minimal frame used to protect links.

func (LinkFrame) Length

func (f LinkFrame) Length() uint16

Length returns the full frame length.

func (LinkFrame) LinkData

func (f LinkFrame) LinkData() []byte

LinkData returns the data for the next layer.

func (LinkFrame) LinkDataWithAuth

func (f LinkFrame) LinkDataWithAuth() []byte

LinkDataWithAuth the link data and the authentication data.

func (LinkFrame) Nonce

func (f LinkFrame) Nonce() []byte

Nonce returns the slice of data used as the nonce.

func (LinkFrame) RecvRate

func (f LinkFrame) RecvRate() uint8

RecvRate returns the recv rate.

func (LinkFrame) Seal

func (f LinkFrame) Seal(encrypt *state.EncryptionSession) error

Seal sets the link frame metadata and encrypts everything.

func (LinkFrame) SequenceAck

func (f LinkFrame) SequenceAck() uint32

SequenceAck returns the acknowledge sequence number.

func (LinkFrame) SequenceNum

func (f LinkFrame) SequenceNum() uint32

SequenceNum returns the sequence number.

func (LinkFrame) SetLength

func (f LinkFrame) SetLength(n uint16)

SetLength sets the full frame length.

func (LinkFrame) SetRecvRate

func (f LinkFrame) SetRecvRate(percent uint8)

SetRecvRate sets the recv rate.

func (LinkFrame) SetSequenceAck

func (f LinkFrame) SetSequenceAck(n uint32)

SetSequenceAck sets the acknowledge sequence number.

func (LinkFrame) SetSequenceNum

func (f LinkFrame) SetSequenceNum(n uint32)

SetSequenceNum sets the sequence number.

func (LinkFrame) SetVersion

func (f LinkFrame) SetVersion(v uint8)

SetVersion sets the frame version.

func (LinkFrame) Unseal

func (f LinkFrame) Unseal(encrypt *state.EncryptionSession) error

Unseal decrypts the link frame.

func (LinkFrame) Version

func (f LinkFrame) Version() uint8

Version returns the frame version.

type Listener

type Listener interface {
	// ID returns the listener ID.
	ID() string

	// PeeringURL returns the used peering URL.
	PeeringURL() *m.PeeringURL

	// ListenAddress returns the used listen address.
	ListenAddress() net.Addr

	// Close closes the underlying listener and cleans up any related resources.
	Close(log func())
}

Listener represents a network connection listener.

type ListenerBase

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

ListenerBase implements common functions to comply with the Listener interface.

func (*ListenerBase) Close

func (ln *ListenerBase) Close(log func())

Close closes the listener.

func (*ListenerBase) ID

func (ln *ListenerBase) ID() string

ID returns the listener ID.

func (*ListenerBase) ListenAddress

func (ln *ListenerBase) ListenAddress() net.Addr

ListenAddress returns the listen address.

func (*ListenerBase) PeeringURL

func (ln *ListenerBase) PeeringURL() *m.PeeringURL

PeeringURL returns the used peering URL.

type Peering

type Peering struct {
	PeeringEvents *mgr.EventMgr[*EventPeering]
	// contains filtered or unexported fields
}

Peering is a peering manager.

func New

func New(instance instance, frameHandler chan frame.Frame) *Peering

New returns a new peering manager.

func (p *Peering) AddLink(link Link) error

AddLink adds the link to the peering list.

func (*Peering) AddListener

func (p *Peering) AddListener(id string, listener Listener)

AddListener adds the listener to the listener list.

func (*Peering) AddProtocol

func (p *Peering) AddProtocol(id string, prot Protocol)

AddProtocol adds a new protocol.

func (p *Peering) CloseLink(ip netip.Addr)

CloseLink closes the link to the given peer.

func (*Peering) CloseListener

func (p *Peering) CloseListener(id string)

CloseListener closes the listener with the given ID.

func (p *Peering) GetLink(ip netip.Addr) Link

GetLink returns the link to the given peer, if available.

func (*Peering) GetLinkByLabel

func (p *Peering) GetLinkByLabel(label m.SwitchLabel) Link

GetLinkByLabel returns the link to the given peer by switch label, if available.

func (*Peering) GetLinkByRemoteHost added in v0.2.0

func (p *Peering) GetLinkByRemoteHost(peeringHost string) Link

GetLinkByRemoteHost returns the link with the given peering host.

func (p *Peering) GetLinks() []Link

GetLinks returns a list of all links.

func (*Peering) GetListener

func (p *Peering) GetListener(id string) Listener

GetListener returns the listener with the given ID.

func (*Peering) GetProtocol

func (p *Peering) GetProtocol(id string) Protocol

GetProtocol adds a new protocol.

func (*Peering) LinkCnt

func (p *Peering) LinkCnt() int

LinkCnt returns the current amount of active peering links.

func (*Peering) PeerWith

func (p *Peering) PeerWith(peeringURL *m.PeeringURL, ip netip.Addr) (Link, error)

PeerWith establishes a connection with the given peering URL. The IP address is optional, but may be required by different protocols. If the peering URL has no IP defined, the IP address is required.

func (p *Peering) RemoveLink(link Link)

RemoveLink removes the link from the peering list. The link is not closed by this function!

func (*Peering) RemoveListener

func (p *Peering) RemoveListener(id string)

RemoveListener removes the listener from the listener list.

func (*Peering) Start

func (p *Peering) Start(m *mgr.Manager) error

Start starts the peering manager. It: - Starts configured listeners. - Connects to configured peers.

func (*Peering) StartListener

func (p *Peering) StartListener(peeringURL *m.PeeringURL, ip netip.Addr) (Listener, error)

StartListener starts a new listener with the given peering URL. The IP address is optional, but may be required by different protocols.

func (*Peering) Stop

func (p *Peering) Stop(mgr *mgr.Manager) error

Stop stops all listeners and links.

func (*Peering) TriggerPeering added in v0.4.0

func (p *Peering) TriggerPeering()

TriggerPeering triggers checking peers and connecting to new peers if needed.

type Protocol

type Protocol interface {
	// Name returns the protocol name/scheme.
	Name() string
	// PeerWith connects to the given router.
	PeerWith(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Link, error)
	// StartListener starts a listener for peering requests.
	StartListener(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Listener, error)
}

Protocol defines how to create a listener or connect to a peer using a certain protocol.

type ProtocolFunctions

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

ProtocolFunctions implements Protocol with saved functions.

func NewProtocol

func NewProtocol(
	name string,
	peerWith func(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Link, error),
	startListener func(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Listener, error),
) *ProtocolFunctions

NewProtocol returns a new protocol using the given functions.

func (*ProtocolFunctions) Name

func (p *ProtocolFunctions) Name() string

Name returns the protocol name/scheme.

func (*ProtocolFunctions) PeerWith

func (p *ProtocolFunctions) PeerWith(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Link, error)

PeerWith connects to the given router.

func (*ProtocolFunctions) StartListener

func (p *ProtocolFunctions) StartListener(peering *Peering, peeringURL *m.PeeringURL, ip netip.Addr) (Listener, error)

StartListener starts a listener for peering requests.

Jump to

Keyboard shortcuts

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