pfilter

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 12 Imported by: 3

README

pfilter

Small Go package for filtering packets from a single net.PacketConn into multiple virtual net.PacketConn's based on some predicate.

Used to multiplex/weave in STUN packets on top of an existing UDP connection, where IP address based routing would not work due to STUN sending replies back from random addresses.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Conn net.PacketConn

	// Size of the byte array passed to the read operations of the underlying
	// socket. Buffer that is too small could result in truncated reads. Defaults to 15000
	BufferSize int

	// Backlog of how many packets we are happy to buffer in memory
	Backlog int

	// If non-zero, uses ipv4.PacketConn.ReadBatch, using the size of the batch given.
	// Defaults to 1 on Darwin/FreeBSD and 8 on Linux.
	BatchSize int
}

type Filter

type Filter interface {
	Outgoing([]byte, net.Addr)
	ClaimIncoming([]byte, net.Addr) bool
}

Filter object receives all data sent out on the Outgoing callback, and is expected to decide if it wants to receive the packet or not via the Receive callback

type PacketFilter

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

PacketFilter embeds a net.PacketConn to perform the filtering.

func NewPacketFilter

func NewPacketFilter(conn net.PacketConn) *PacketFilter

NewPacketFilter creates a packet filter object wrapping the given packet connection.

func NewPacketFilterWithConfig

func NewPacketFilterWithConfig(config Config) (*PacketFilter, error)

NewPacketFilterWithConfig creates a packet filter object with the configuration provided

func (*PacketFilter) Dropped

func (d *PacketFilter) Dropped() uint64

Dropped returns number of packets dropped due to nobody claiming them.

func (*PacketFilter) NewConn

func (d *PacketFilter) NewConn(priority int, filter Filter) net.PacketConn

NewConn returns a new net.PacketConn object which filters packets based on the provided filter. If filter is nil, the connection will receive all packets. Priority decides which connection gets the ability to claim the packet.

func (*PacketFilter) NumberOfConns

func (d *PacketFilter) NumberOfConns() int

NumberOfConns returns the number of currently active virtual connections

func (*PacketFilter) Overflow

func (d *PacketFilter) Overflow() uint64

Overflow returns number of packets were dropped due to receive buffers being full.

func (*PacketFilter) Start

func (d *PacketFilter) Start()

Start starts reading packets from the socket and forwarding them to connections. Should call this after creating all the expected connections using NewConn, otherwise the packets read will be dropped.

Jump to

Keyboard shortcuts

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