fwdpacket

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package fwdpacket contains routines and types for manipulating packets.

Index

Constants

View Source
const (
	FirstField = 0            // The first (outermost) instance of a field.
	LastField  = (1 << 8) - 1 // The last (innermost) instance of a field.
)

Constants used to indicate relative instances.

View Source
const (
	OpInc = iota // Increment a field with the specified value.
	OpDec        // Decrement a field with the specified value.
	OpSet        // Set a field to the specified value.
)

Operations that can be applied to a field.

View Source
const (
	LogDebugMessage = iota // log a debug message
	LogDebugFrame          // log a debug message with the frame
	LogErrorFrame          // log an error message with the frame
	LogDesc                // sets the packet's description (prefixed to all messages)
)

Enumeration of various operations that can be performed on the packet log.

Variables

View Source
var AttrPacketDebug = fwdattribute.ID("PacketDebug")

AttrPacketDebug controls packet debugging.

Functions

func Log

func Log(packet Packet)

Log logs the packet's buffer if it is not empty.

func MaxSize

func MaxSize(id FieldID) int

MaxSize returns the maximum size of the specified field.

func Pad

func Pad(id FieldID, field []byte) []byte

Pad pads the field to the maximum size.

func Register

func Register(p Parser)

Register registers a parser for Lucius.

func SetNID

func SetNID(packet Packet, nid fwdobject.NID, num fwdpb.PacketFieldNum) error

SetNID sets a NID in the specified field.

func Truncate

func Truncate(field []byte, size int) []byte

Truncate truncates a padded field to the specified size.

func Validate

func Validate(id FieldID, size int) error

Validate validates the size of the specified field.

Types

type FieldID

type FieldID struct {
	IsUDF    bool                    // true if the field is a UDF.
	Instance uint8                   // Instance of the field in a packet.
	Num      fwdpb.PacketFieldNum    // Field number.
	Header   fwdpb.PacketHeaderGroup // Header type of UDF.
	Offset   uint8                   // UDF's offset within the header.
	Size     uint8                   // UDF size in bytes.
}

A FieldID identifies a field in a packet. It supports fields using field numbers and user defined fields (UDF).

func NewFieldID

func NewFieldID(field *fwdpb.PacketFieldId) FieldID

NewFieldID creates a new packet field id.

func NewFieldIDFromBytes

func NewFieldIDFromBytes(header fwdpb.PacketHeaderGroup, offset, size, instance uint32) FieldID

NewFieldIDFromBytes creates a new field id from a set of bytes.

func NewFieldIDFromNum

func NewFieldIDFromNum(num fwdpb.PacketFieldNum, instance uint32) FieldID

NewFieldIDFromNum creates a new field id from a field number.

type Packet

type Packet interface {
	// Field returns the bytes associated with a field ID.
	Field(id FieldID) ([]byte, error)

	// Update updates a field in the packet.
	Update(id FieldID, op int, arg []byte) error

	// Decap removes the outermost header of the specified type.
	Decap(id fwdpb.PacketHeaderId) error

	// Encap adds an outermost header of the specified type.
	Encap(id fwdpb.PacketHeaderId) error

	// Reparse reparses the current packet to start from the specified packet header
	// id. Note that the current packet is rebuilt before it is reparsed. Note that
	// by default reparsing creates a new packet, so metadata fields will be lost.
	// Additional fields specified during reparsing ensures that the field values
	// are copied from the old packet to the new packet. This can be used to retain
	// metadata field values across a packet reparse. It can also prepend the
	// rebuilt packet with the specified set of bytes before reparsing the packet.
	Reparse(id fwdpb.PacketHeaderId, fields []FieldID, prepend []byte) error

	// Mirror creates a new packet from the current packet. Note that the current
	// packet is rebuilt before it is mirrored. Note that by default the metadata
	// fields are lost. Additional fields specified during the mirror ensures that
	// the field values are copied from the old packet to the new packet. This can
	// be used to retain  metadata field values across a mirror.
	Mirror(fields []FieldID) (Packet, error)

	// String formats the packet into a string.
	String() string

	// Length returns the length of the packet in bytes.
	Length() int

	// Frame returns the packet as a slice of bytes.
	Frame() []byte

	// Debug control debugging for the packet.
	Debug(enable bool)

	// Logf controls the packet's message log.
	Logf(level int, fmt string, args ...interface{})

	// Log returns the contents of the packet's log.
	Log() []string

	// Attributes returns the attributes associated with the packet.
	Attributes() fwdattribute.Set

	// StartHeader returns the first header of the packet.
	StartHeader() fwdpb.PacketHeaderId
}

Packet is a network packet that can be queried and manipulated.

Packet repesents a network packet that has been parsed and verfied. It supports queries on various fields. Additionally it can change the network packet by adding and removing headers or setting packet fields.

func New

func New(hid fwdpb.PacketHeaderId, bytes []byte) (Packet, error)

New builds a new packet from a stream of bytes.

func NewNID

func NewNID(hid fwdpb.PacketHeaderId, frame []byte, nid fwdobject.NID) (Packet, error)

NewNID creates a new packet from the specified frame and sets up its input port if specified.

type Parser

type Parser interface {
	// New creates a new packet using the specified first header id.
	New(hid fwdpb.PacketHeaderId, bytes []byte) (Packet, error)

	// Validate validates the size of the specified field.
	Validate(id FieldID, size int) error

	// MaxSize returns the maximum size of the specified field.
	MaxSize(id FieldID) int
}

Parser can create a Packet from a slice of bytes containing the packet.

Jump to

Keyboard shortcuts

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