packet

package
v1.6.26 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IPv4 = IPVersion(4)
	IPv6 = IPVersion(6)

	InBound  = true
	OutBound = false

	ICMP    = IPProtocol(1)
	IGMP    = IPProtocol(2)
	TCP     = IPProtocol(6)
	UDP     = IPProtocol(17)
	ICMPv6  = IPProtocol(58)
	UDPLite = IPProtocol(136)
	RAW     = IPProtocol(255)

	AnyHostInternalProtocol61 = IPProtocol(61)
)

Basic Constants.

Variables

View Source
var ErrFailedToLoadPayload = errors.New("could not load packet payload")

ErrFailedToLoadPayload is returned by GetPayload if it failed for an unspecified reason, or is not implemented on the current system.

View Source
var ErrInfoOnlyPacket = errors.New("info-only packet")

ErrInfoOnlyPacket is returned for unsupported operations on an info-only packet.

Functions

func CreateConnectionID

func CreateConnectionID(protocol IPProtocol, src net.IP, srcPort uint16, dst net.IP, dstPort uint16, inbound bool) string

CreateConnectionID creates a connection ID.

func Parse

func Parse(packetData []byte, pktBase *Base) (err error)

Parse parses an IP packet and saves the information in the given packet object.

Types

type BandwidthUpdate

type BandwidthUpdate struct {
	ConnID        string
	BytesReceived uint64
	BytesSent     uint64
	Method        BandwidthUpdateMethod
}

BandwidthUpdate holds an update to the seen bandwidth of a connection.

func (*BandwidthUpdate) String

func (bu *BandwidthUpdate) String() string

type BandwidthUpdateMethod

type BandwidthUpdateMethod uint8

BandwidthUpdateMethod defines how the bandwidth data of a bandwidth update should be interpreted.

const (
	Absolute BandwidthUpdateMethod = iota
	Additive
)

Bandwidth Update Methods.

func (BandwidthUpdateMethod) String

func (bum BandwidthUpdateMethod) String() string

type Base

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

Base is a base structure for satisfying the Packet interface.

func (*Base) Ctx

func (pkt *Base) Ctx() context.Context

Ctx returns the packet context.

func (*Base) ExpectInfo

func (pkt *Base) ExpectInfo() bool

ExpectInfo returns whether the next packet is expected to be informational only.

func (*Base) FastTrackedByIntegration

func (pkt *Base) FastTrackedByIntegration() bool

FastTrackedByIntegration returns whether the packet has been fast-track accepted by the OS integration.

func (*Base) FmtPacket

func (pkt *Base) FmtPacket() string

FmtPacket returns the most important information about the packet as a string.

func (*Base) FmtProtocol

func (pkt *Base) FmtProtocol() string

FmtProtocol returns the protocol as a string.

func (*Base) FmtRemoteAddress

func (pkt *Base) FmtRemoteAddress() string

FmtRemoteAddress returns the full remote address (protocol, IP, port) as a string.

func (*Base) FmtRemoteIP

func (pkt *Base) FmtRemoteIP() string

FmtRemoteIP returns the remote IP address as a string.

func (*Base) FmtRemotePort

func (pkt *Base) FmtRemotePort() string

FmtRemotePort returns the remote port as a string.

func (*Base) GetConnectionID

func (pkt *Base) GetConnectionID() string

GetConnectionID returns the link ID for this packet.

func (*Base) HasPorts

func (pkt *Base) HasPorts() bool

HasPorts checks if the packet has a protocol that uses ports.

func (*Base) Info

func (pkt *Base) Info() *Info

Info returns the packet Info.

func (*Base) InfoOnly

func (pkt *Base) InfoOnly() bool

InfoOnly returns whether the packet is informational only and does not represent an actual packet.

func (*Base) IsInbound

func (pkt *Base) IsInbound() bool

IsInbound checks if the packet is inbound.

func (*Base) IsOutbound

func (pkt *Base) IsOutbound() bool

IsOutbound checks if the packet is outbound.

func (*Base) Layers

func (pkt *Base) Layers() gopacket.Packet

Layers returns the parsed layer data.

func (*Base) LoadPacketData

func (pkt *Base) LoadPacketData() error

LoadPacketData loads packet data from the integration, if not yet done.

func (*Base) MatchesAddress

func (pkt *Base) MatchesAddress(remote bool, protocol IPProtocol, network *net.IPNet, port uint16) bool

MatchesAddress checks if a the packet matches a given endpoint (remote or local) in protocol, network and port.

Comparison matrix:

====== IN OUT

Local Dst Src Remote Src Dst .

func (*Base) MatchesIP

func (pkt *Base) MatchesIP(endpoint bool, network *net.IPNet) bool

MatchesIP checks if a the packet matches a given endpoint (remote or local) IP.

Comparison matrix:

====== IN OUT

Local Dst Src Remote Src Dst .

func (*Base) Payload

func (pkt *Base) Payload() []byte

Payload returns the raw Layer 5 Network Data.

func (*Base) Raw

func (pkt *Base) Raw() []byte

Raw returns the raw Layer 3 Network Data.

func (*Base) SetCtx

func (pkt *Base) SetCtx(ctx context.Context)

SetCtx sets the packet context.

func (*Base) SetInbound

func (pkt *Base) SetInbound()

SetInbound sets a the packet direction to inbound. This must only used when initializing the packet structure.

func (*Base) SetOutbound

func (pkt *Base) SetOutbound()

SetOutbound sets a the packet direction to outbound. This must only used when initializing the packet structure.

func (*Base) SetPacketInfo

func (pkt *Base) SetPacketInfo(packetInfo Info)

SetPacketInfo sets a new packet Info. This must only used when initializing the packet structure.

func (*Base) String

func (pkt *Base) String() string

type IPProtocol

type IPProtocol uint8

IPProtocol represents an IP protocol.

func (IPProtocol) String

func (p IPProtocol) String() string

String returns the string representation (abbreviation) of the protocol.

type IPVersion

type IPVersion uint8

IPVersion represents an IP version.

func (IPVersion) ByteSize

func (v IPVersion) ByteSize() int

ByteSize returns the byte size of the ip (IPv4 = 4 bytes, IPv6 = 16).

func (IPVersion) String

func (v IPVersion) String() string

String returns the string representation of the IP version: "IPv4" or "IPv6".

type Info

type Info struct {
	Inbound  bool
	InTunnel bool

	Version          IPVersion
	Protocol         IPProtocol
	SrcPort, DstPort uint16
	Src, Dst         net.IP

	PID    int
	SeenAt time.Time
}

Info holds IP and TCP/UDP header information.

func (*Info) CreateConnectionID

func (pi *Info) CreateConnectionID() string

CreateConnectionID creates a connection ID. In most circumstances, this method should not be used directly, but packet.GetConnectionID() should be called instead.

func (*Info) LocalIP

func (pi *Info) LocalIP() net.IP

LocalIP returns the local IP of the packet.

func (*Info) LocalPort

func (pi *Info) LocalPort() uint16

LocalPort returns the local port of the packet.

func (*Info) RemoteIP

func (pi *Info) RemoteIP() net.IP

RemoteIP returns the remote IP of the packet.

func (*Info) RemotePort

func (pi *Info) RemotePort() uint16

RemotePort returns the remote port of the packet.

type InfoPacket

type InfoPacket struct {
	Base
}

InfoPacket does not represent an actual packet, but only holds metadata. Implements the packet.Packet interface.

func NewInfoPacket

func NewInfoPacket(info Info) *InfoPacket

NewInfoPacket returns a new InfoPacket with the given info.

func (*InfoPacket) Accept

func (pkt *InfoPacket) Accept() error

Accept does nothing on an info-only packet.

func (*InfoPacket) Block

func (pkt *InfoPacket) Block() error

Block does nothing on an info-only packet.

func (*InfoPacket) Drop

func (pkt *InfoPacket) Drop() error

Drop does nothing on an info-only packet.

func (*InfoPacket) InfoOnly

func (pkt *InfoPacket) InfoOnly() bool

InfoOnly returns whether the packet is informational only and does not represent an actual packet.

func (*InfoPacket) LoadPacketData

func (pkt *InfoPacket) LoadPacketData() error

LoadPacketData does nothing on Linux, as data is always fully parsed.

func (*InfoPacket) PermanentAccept

func (pkt *InfoPacket) PermanentAccept() error

PermanentAccept does nothing on an info-only packet.

func (*InfoPacket) PermanentBlock

func (pkt *InfoPacket) PermanentBlock() error

PermanentBlock does nothing on an info-only packet.

func (*InfoPacket) PermanentDrop

func (pkt *InfoPacket) PermanentDrop() error

PermanentDrop does nothing on an info-only packet.

func (*InfoPacket) RerouteToNameserver

func (pkt *InfoPacket) RerouteToNameserver() error

RerouteToNameserver does nothing on an info-only packet.

func (*InfoPacket) RerouteToTunnel

func (pkt *InfoPacket) RerouteToTunnel() error

RerouteToTunnel does nothing on an info-only packet.

type Packet

type Packet interface {
	// Verdicts.
	Accept() error
	Block() error
	Drop() error
	PermanentAccept() error
	PermanentBlock() error
	PermanentDrop() error
	RerouteToNameserver() error
	RerouteToTunnel() error
	FastTrackedByIntegration() bool
	InfoOnly() bool
	ExpectInfo() bool

	// Info.
	SetCtx(ctx context.Context)
	Ctx() context.Context
	Info() *Info
	SetPacketInfo(info Info)
	IsInbound() bool
	IsOutbound() bool
	SetInbound()
	SetOutbound()
	HasPorts() bool
	GetConnectionID() string

	// Payload.
	LoadPacketData() error
	Layers() gopacket.Packet
	Raw() []byte
	Payload() []byte

	// Matching.
	MatchesAddress(remote bool, protocol IPProtocol, network *net.IPNet, port uint16) bool
	MatchesIP(endpoint bool, network *net.IPNet) bool

	// Formatting.
	String() string
	FmtPacket() string
	FmtProtocol() string
	FmtRemoteIP() string
	FmtRemotePort() string
	FmtRemoteAddress() string
}

Packet is an interface to a network packet to provide object behavior the same across all systems.

type Verdict

type Verdict uint8

Verdict describes the decision on a packet.

const (
	DROP Verdict = iota
	BLOCK
	ACCEPT
	STOLEN
	QUEUE
	REPEAT
	STOP
)

Verdicts.

func (Verdict) String

func (v Verdict) String() string

String returns the string representation of the verdict.

Jump to

Keyboard shortcuts

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