packet

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageTypeRequest = iota
	MessageTypeResponse
)
View Source
const (
	// random nonce + unix epoch timestamp + type + AEAD tag
	RequestPacketSize = chacha20poly1305.NonceSizeX + 8 + 1 + chacha20poly1305.Overhead

	// random nonce + unix epoch timestamp + type + IP + port + AEAD tag
	ResponsePacketSize = chacha20poly1305.NonceSizeX + 8 + 1 + 16 + 2 + chacha20poly1305.Overhead
)
View Source
const (
	// MaxEpochDiff is the maximum allowed time difference between a received timestamp and system time.
	MaxEpochDiff = 30

	// MaxTimeDiff is the maximum allowed time difference between a received timestamp and system time.
	MaxTimeDiff = MaxEpochDiff * time.Second

	// ReplayWindowDuration defines the amount of time during which a nonce check is necessary.
	ReplayWindowDuration = MaxTimeDiff * 2
)

Variables

View Source
var (
	ErrBadPacketSize  = errors.New("bad packet size")
	ErrRepeatedNonce  = errors.New("repeated nonce")
	ErrBadTimestamp   = errors.New("time offset too large")
	ErrBadMessageType = errors.New("bad message type")
)

Functions

func CheckUnixEpochTimestamp

func CheckUnixEpochTimestamp(b []byte) error

CheckUnixEpochTimestamp checks the Unix Epoch timestamp in the buffer and returns an error if the timestamp exceeds the allowed time difference from system time.

This function does not check buffer length. Make sure it's at least 8 bytes long.

Types

type Client

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

Client generates request packets and parses response packets.

func NewClient

func NewClient(psk []byte) (*Client, error)

NewClient creates a new client with the given PSK.

func (*Client) ParseResponse

func (c *Client) ParseResponse(resp []byte) (netip.AddrPort, error)

ParseResponse parses the response packet and returns the client IP and port.

func (*Client) PutRequest

func (c *Client) PutRequest(req []byte)

PutRequest writes a request packet to the first RequestPacketSize bytes of the given buffer.

type Server

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

Server generates responses to request packets.

func NewServer

func NewServer(psk []byte) (*Server, error)

NewServer creates a new server with the given PSK.

func (*Server) Handle

func (s *Server) Handle(clientAddrPort netip.AddrPort, req []byte, resp []byte) error

Handle processes the request packet and writes the response packet to the first ResponsePacketSize bytes of the given buffer.

Jump to

Keyboard shortcuts

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