protocol

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: AGPL-3.0, GPL-2.0, LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PacketSize    = 1500
	NamespaceSize = 64
	DataValueSize = 1419

	CmdCount          byte = 'C'
	CmdPut            byte = 'P'
	CmdPutReplicate   byte = 'R'
	CmdCountNamespace byte = 'N'
	CmdCountServer    byte = 'S'

	CmdTCPOnlyKeys     byte = 'K'
	CmdTCPOnlyValues   byte = 'V'
	CmdTCPOnlyStore    byte = 'T'
	CmdTCPOnlyRetrieve byte = 'I'

	// ResError is a Cmd
	ResError byte = 'E'
)

Variables

View Source
var (
	ErrInvalidPacketSizeTooSmall = errors.New("bad packet: too small, size must be 1500 bytes")
	ErrInvalidPacketSizeTooLarge = errors.New("bad packet: too large, size must be 1500 bytes")
	ErrInvalidCommandByte        = errors.New("bad packet: invalid command byte")
	ErrProtocolSpace1            = errors.New("bad packet: expected space 1")
	ErrProtocolSpace2            = errors.New("bad packet: expected space 1")
	ErrProtocolSpace3            = errors.New("bad packet: expected space 3")
	ErrBadHash                   = errors.New("auth failed: packet hash invalid")
	ErrBadOutputSize             = errors.New("wrong data size during packet construction")
)
View Source
var StopSymbol = []byte("\n.\n")

Functions

func HashPacket

func HashPacket(p *Packet, preSharedKey []byte) []byte

HashPacket returns an 8 byte slice

func IsRequestCmd

func IsRequestCmd(c byte) bool

IsRequestCmd indicates if the server should accept this as a command

func IsResponseCmd

func IsResponseCmd(c byte) bool

IsResponseCmd indicates if the client should accept this as a command

func IsTcpOnlyCmd added in v0.5.1

func IsTcpOnlyCmd(c byte) bool

func PadRight added in v0.5.1

func PadRight(in *[]byte, finalSize int) *[]byte

PadRight adds char space to make buffer reach desired size. If `in` is larger than `finalSize`, nothing happens.

func Uint32FromBytes

func Uint32FromBytes(fourBytes []byte) uint32

func Uint32ToBytes

func Uint32ToBytes(u uint32) []byte

Uint32ToBytes returns a 4 byte slice

func Uint64FromBytes

func Uint64FromBytes(eightBytes []byte) uint64

func Uint64ToBytes

func Uint64ToBytes(u uint64) []byte

Uint64ToBytes returns an 8 byte slice

Types

type Packet

type Packet struct {
	Command        byte
	HashBytes      []byte // fixed 8 byte number
	Hash           uint64 // fixed 8 byte number
	MessageIDBytes []byte // fixed 4 byte number
	MessageID      uint32 // fixed 4 byte number
	Namespace      []byte // fixed 64 byte string
	DataValue      []byte // fixed 1419 byte string

	RequestClient *net.TCPConn
}

func NewPacket

func NewPacket(command byte, messageID uint32, namespace, dataValue, preSharedKey string) *Packet

NewPacket is a friendlier way to construct a packet and will provide conversions inline

func NewPacketFromParts

func NewPacketFromParts(command byte, messageID, namespace, dataValue, preSharedKey []byte) *Packet

func ParsePacket

func ParsePacket(buf []byte) (*Packet, error)

ParsePacket parses a packet like:

[Command char][space][xxhash of pre shared key + id + ns + data][space][Message ID uint32][space][Namespace 64 bytes][space][data remaining bytes]

The MTU of 1500 is the maximum allowed packet size. That means the data key can only be 1419 bytes max.

The goal here is fast and simple tracking of expirable keys, so the trade off is that the keys have limited length.

An invalid packet may still be returned it can be parsed partially, in which case error will not be nil. If a packet is way too small, the pointer will be nil.

func (*Packet) Bytes

func (p *Packet) Bytes() ([]byte, error)

Bytes formats the packet for transport. If the outputted size is wrong for UDP, it will return the packet an allow the client to perhaps truncate.

func (*Packet) DataValueString

func (p *Packet) DataValueString() string

func (*Packet) NamespaceString

func (p *Packet) NamespaceString() string

func (*Packet) SetHash

func (p *Packet) SetHash(preSharedKey []byte)

SetHash puts the hash on a packet

func (*Packet) Validate

func (p *Packet) Validate(preSharedKey []byte) error

Validate returns an error is the packet's hash does not authenticate against the preSharedKey.

Jump to

Keyboard shortcuts

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