frame

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: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// V1 is frame version 1.
	V1 = 1
)

Variables

View Source
var (
	ErrInsufficientFrameData   = errors.New("insufficient frame data")
	ErrIncorrectLength         = errors.New("incorrect length")
	ErrUnsupportedFrameVersion = errors.New("unsupported frame version")
	ErrVerificationFailed      = errors.New("verification failed")
)

Errors.

Functions

This section is empty.

Types

type Builder

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

Builder builds and parses frames. It holds internal pools of frames and slices for efficiency.

func NewFrameBuilder

func NewFrameBuilder() *Builder

NewFrameBuilder returns a new frame builder.

func (*Builder) FrameMargins

func (b *Builder) FrameMargins() (offset, overhead int)

FrameMargins returns the currently required margins for frames.

func (*Builder) GetPooledSlice

func (b *Builder) GetPooledSlice(minSize int) (pooledSlice []byte)

GetPooledSlice returns a slice from the pool (or creates one) that has at least the specified size.

func (*Builder) NewFrameV1

func (b *Builder) NewFrameV1(
	src, dst netip.Addr,
	msgType MessageType,
	switchLabels, data, appendixData []byte,
) (*FrameV1, error)

NewFrameV1 returns a new frame (v1) with the given data set.

func (*Builder) ParseFrame

func (b *Builder) ParseFrame(data, pooledSlice []byte, dataOffset int) (Frame, error)

ParseFrame parses a frame.

func (*Builder) ParseFrameV1

func (b *Builder) ParseFrameV1(data, pooledSlice []byte, dataOffset int) (*FrameV1, error)

ParseFrameV1 parses a version 1 frame.

func (*Builder) ReturnPooledSlice

func (b *Builder) ReturnPooledSlice(pooledSlice []byte)

ReturnPooledSlice returns the give pooled slice to the pool. The provided slice must not be used anymore in any way.

func (*Builder) SetFrameMargins

func (b *Builder) SetFrameMargins(offset, overhead int)

SetFrameMargins sets new required margins for new frames. Values must be between 0 and 100 (inclusive).

type FlowControlFlag

type FlowControlFlag uint8

FlowControlFlag is a set of flow control flags.

const (
	FlowControlFlagDecreaseFlow FlowControlFlag = iota + 1
	FlowControlFlagHoldFlow
	FlowControlFlagIncreaseFlow
)

Flow Control Flags.

type Frame

type Frame interface {

	// Version returns the frame version.
	Version() uint8
	// TTL returns the TTL.
	TTL() uint8
	// SetTTL sets the TTL.
	SetTTL(ttl uint8)
	// ReduceTTL reduces the ttl by the given amount.
	// Will not drop below zero.
	ReduceTTL(by uint8)
	// HasFlowFlag returns whether the given flow control flag is set.
	HasFlowFlag(flag FlowControlFlag) bool
	// SetFlowFlag sets the given flow control flag.
	SetFlowFlag(flag FlowControlFlag)
	// RecvRate returns the recv rate.
	RecvRate() uint8
	// SetRecvRate sets the recv rate.
	SetRecvRate(rate uint8)

	// MessageType returns the message type.
	MessageType() MessageType
	// SetMessageType sets the message type.
	SetMessageType(msgType MessageType)
	// SequenceNum returns the sequence number.
	SequenceNum() uint32
	// SetSequenceNum sets the sequence number.
	SetSequenceNum(seq uint32)
	// SequenceAck returns the sequence ack number.
	SequenceAck() uint32
	// SetSequenceAck sets the sequence ack number.
	SetSequenceAck(seq uint32)
	// SequenceTime returns the sequence time.
	SequenceTime() time.Time
	// SetSequenceTime sets the sequence time.
	SetSequenceTime(t time.Time)

	// SrcIP returns the frame source IP.
	SrcIP() netip.Addr
	// DstIP returns the frame destination IP.
	DstIP() netip.Addr

	// SwitchBlock returns the switch block.
	SwitchBlock() []byte
	// SetSwitchBlock sets the switch block.
	// It must be the same size as the existing block.
	SetSwitchBlock(update []byte) error
	// MessageData returns the message data.
	MessageData() []byte

	// MessageDataWithAuth returns the message data, including the auth data.
	MessageDataWithAuth() []byte
	// MessageDataWithOffset returns the message data with the given offset.
	// If you change any of the data, consider the frame invalid.
	MessageDataWithOffset(offset int) ([]byte, error)
	// AuthData returns the authentication data.
	AuthData() []byte
	// AppendixData returns the appendix data.
	AppendixData() []byte
	// SetAppendixData sets the appendix data.
	SetAppendixData(appendix []byte) error
	// FrameDataWithMargins returns the whole frame, including the given offset and overhead.
	FrameDataWithMargins(offset, overhead int) ([]byte, error)
	// Clone return an exact copy of the frame.
	Clone() Frame

	// Seal signs or encrypts the frame using the given session.
	Seal(s *state.Session) error
	// Unseal verifies or decrypts the frame using the given session.
	Unseal(s *state.Session) error

	// RecvLink returns the receive link.
	RecvLink() LinkAccessor
	// SetRecvLink sets the receive link.
	SetRecvLink(recvLink LinkAccessor)

	// Reply transforms the frame into a reply.
	Reply(switchLabels, data, appendixData []byte) error
	// ReplyTo transforms the frame into a reply with the given src and dst addresses.
	ReplyTo(src, dst netip.Addr, switchLabels, data, appendixData []byte) error
	// ReturnToPool returns the frame and the included pooled slice to the pool.
	ReturnToPool()
}

Frame is a common interface to different frame versions.

type FrameV1

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

FrameV1 is a frame in version 1.

func (*FrameV1) AppendixData

func (f *FrameV1) AppendixData() []byte

AppendixData returns the appendix data.

func (*FrameV1) AuthData

func (f *FrameV1) AuthData() []byte

AuthData returns the authentication data.

func (*FrameV1) Clone

func (f *FrameV1) Clone() Frame

Clone return an exact copy of the frame.

func (*FrameV1) DstIP

func (f *FrameV1) DstIP() netip.Addr

DstIP returns the frame destination IP.

func (*FrameV1) FlowControl

func (f *FrameV1) FlowControl() uint8

FlowControl returns the flow control flag set.

func (*FrameV1) FrameDataWithMargins

func (f *FrameV1) FrameDataWithMargins(offset, overhead int) ([]byte, error)

FrameDataWithMargins returns the whole frame, including the given offset and overhead.

func (*FrameV1) HasFlowFlag

func (f *FrameV1) HasFlowFlag(flag FlowControlFlag) bool

HasFlowFlag returns whether the given flow control flag is set.

func (*FrameV1) MessageData

func (f *FrameV1) MessageData() []byte

MessageData returns the message data.

func (*FrameV1) MessageDataWithAuth

func (f *FrameV1) MessageDataWithAuth() []byte

MessageDataWithAuth returns the message data, including the auth data.

func (*FrameV1) MessageDataWithOffset

func (f *FrameV1) MessageDataWithOffset(offset int) ([]byte, error)

MessageDataWithOffset returns the message data with the given offset. If you change any of the data, consider the frame invalid.

func (*FrameV1) MessageType

func (f *FrameV1) MessageType() MessageType

MessageType returns the message type.

func (f *FrameV1) RecvLink() LinkAccessor

RecvLink returns the receive link.

func (*FrameV1) RecvRate

func (f *FrameV1) RecvRate() uint8

RecvRate returns the recv rate.

func (*FrameV1) ReduceTTL

func (f *FrameV1) ReduceTTL(by uint8)

ReduceTTL reduces the ttl by the given amount. Will not drop below zero.

func (*FrameV1) Reply

func (f *FrameV1) Reply(switchLabels, data, appendixData []byte) error

Reply transforms the frame into a reply.

func (*FrameV1) ReplyTo

func (f *FrameV1) ReplyTo(src, dst netip.Addr, switchLabels, data, appendixData []byte) error

ReplyTo transforms the frame into a reply with the given src and dst addresses.

func (*FrameV1) ReturnToPool

func (f *FrameV1) ReturnToPool()

ReturnToPool returns the frame and the included pooled slice to the pool.

func (*FrameV1) Seal

func (f *FrameV1) Seal(s *state.Session) error

Seal signs or encrypts the frame using the given session.

func (*FrameV1) SequenceAck

func (f *FrameV1) SequenceAck() uint32

SequenceAck returns the sequence ack number.

func (*FrameV1) SequenceNum

func (f *FrameV1) SequenceNum() uint32

SequenceNum returns the sequence number.

func (*FrameV1) SequenceTime

func (f *FrameV1) SequenceTime() time.Time

SequenceTime returns the sequence time.

func (*FrameV1) SetAppendixData

func (f *FrameV1) SetAppendixData(appendix []byte) error

SetAppendixData sets the appendix data.

func (*FrameV1) SetFlowControl

func (f *FrameV1) SetFlowControl(fc uint8)

SetFlowControl sets the flow control flag set to the given value.

func (*FrameV1) SetFlowFlag

func (f *FrameV1) SetFlowFlag(flag FlowControlFlag)

SetFlowFlag sets the given flow control flag.

func (*FrameV1) SetMessageType

func (f *FrameV1) SetMessageType(msgType MessageType)

SetMessageType sets the message type.

func (f *FrameV1) SetRecvLink(link LinkAccessor)

SetRecvLink sets the receive link.

func (*FrameV1) SetRecvRate

func (f *FrameV1) SetRecvRate(percent uint8)

SetRecvRate sets the recv rate.

func (*FrameV1) SetSequenceAck

func (f *FrameV1) SetSequenceAck(n uint32)

SetSequenceAck sets the sequence ack number.

func (*FrameV1) SetSequenceNum

func (f *FrameV1) SetSequenceNum(n uint32)

SetSequenceNum sets the sequence number.

func (*FrameV1) SetSequenceTime

func (f *FrameV1) SetSequenceTime(t time.Time)

SetSequenceTime sets the sequence time.

func (*FrameV1) SetSwitchBlock

func (f *FrameV1) SetSwitchBlock(update []byte) error

SetSwitchBlock sets the switch block. It must be the same size as the existing block.

func (*FrameV1) SetTTL

func (f *FrameV1) SetTTL(ttl uint8)

SetTTL sets the TTL.

func (*FrameV1) SignRaw

func (f *FrameV1) SignRaw(key ed25519.PrivateKey) error

SignRaw signs the raw frame, with any special handling.

func (*FrameV1) SrcIP

func (f *FrameV1) SrcIP() netip.Addr

SrcIP returns the frame source IP.

func (*FrameV1) SwitchBlock

func (f *FrameV1) SwitchBlock() []byte

SwitchBlock returns the switch block.

func (*FrameV1) TTL

func (f *FrameV1) TTL() uint8

TTL returns the TTL.

func (*FrameV1) Unseal

func (f *FrameV1) Unseal(s *state.Session) error

Unseal verifies or decrypts the frame using the given session.

func (*FrameV1) VerifyRaw

func (f *FrameV1) VerifyRaw(key ed25519.PublicKey) error

VerifyRaw verifies the raw frame, with any special handling.

func (*FrameV1) Version

func (f *FrameV1) Version() uint8

Version returns the frame version.

type LinkAccessor

type LinkAccessor 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

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

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

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

	// Send sends a frame to the peer.
	Send(f 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

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

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

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

LinkAccessor is an interface to access links from a frame.

type MessageClass

type MessageClass uint8

MessageClass is a class of messages with shared attributes.

const (
	MessageClassUnknown MessageClass = iota
	MessageClassSigned  MessageClass = iota
	MessageClassPriorityEncrypted
	MessageClassEncrypted
)

Message Classes.

type MessageType

type MessageType uint8

MessageType designates the message type of a frame.

const (
	RouterHopPingDeprecated MessageType = 0 // multicast, priority, signed
	RouterPing              MessageType = 1 // unicast, priority, signed
	RouterCtrl              MessageType = 2 // unicast, priority, encrypted
	RouterHopPing           MessageType = 3 // multicast, priority, signed

	NetworkTraffic MessageType = 8 // encrypted

	SessionCtrl MessageType = 16 // priority, encrypted
	SessionData MessageType = 17 // encrypted
)

Message Types.

func (MessageType) Class

func (mt MessageType) Class() MessageClass

Class returns whether the message class.

func (MessageType) IsEncrypted

func (mt MessageType) IsEncrypted() bool

IsEncrypted returns whether the message type is encrypted (instead of signing).

func (MessageType) IsPriority

func (mt MessageType) IsPriority() bool

IsPriority returns whether the message type is prioritized.

func (MessageType) String

func (mt MessageType) String() string

Jump to

Keyboard shortcuts

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