packet

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 12 Imported by: 25

README

packet Test Status Go Reference Go Report Card

Package packet provides access to Linux packet sockets (AF_PACKET). MIT Licensed.

Stability

See the CHANGELOG file for a description of changes between releases.

This package has a stable v1 API and any future breaking changes will prompt the release of a new major version. Features and bug fixes will continue to occur in the v1.x.x series.

This package only supports the two most recent major versions of Go, mirroring Go's own release policy. Older versions of Go may lack critical features and bug fixes which are necessary for this package to function correctly.

History

One of my first major Go networking projects was github.com/mdlayher/raw, which provided access to Linux AF_PACKET sockets and *BSD equivalent mechanisms for sending and receiving Ethernet frames. However, the *BSD support languished and I lack the expertise and time to properly maintain code for operating systems I do not use on a daily basis.

Package packet is a successor to package raw, but exclusively focused on Linux and AF_PACKET sockets. The APIs are nearly identical, but with a few changes which take into account some of the lessons learned while working on raw.

Users are highly encouraged to migrate any existing Linux uses of raw to package packet instead. This package will be supported for the foreseeable future and will receive continued updates as necessary.

Documentation

Overview

Package packet provides access to Linux packet sockets (AF_PACKET).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addr

type Addr struct {
	HardwareAddr net.HardwareAddr
}

An Addr is a physical-layer address.

func (*Addr) Network

func (a *Addr) Network() string

Network returns the address's network name, "packet".

func (*Addr) String

func (a *Addr) String() string

String returns the string representation of an Addr.

type Config

type Config struct {
	// Filter is an optional assembled BPF filter which can be applied to the
	// Conn before bind(2) is called.
	//
	// The Conn.SetBPF method serves the same purpose once a Conn has already
	// been opened, but setting Filter applies the BPF filter before the Conn is
	// bound. This ensures that unexpected packets will not be captured before
	// the Conn is opened.
	Filter []bpf.RawInstruction
}

Config contains options for a Conn.

type Conn

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

A Conn is an Linux packet sockets (AF_PACKET) implementation of a net.PacketConn.

func Listen

func Listen(ifi *net.Interface, socketType Type, protocol int, cfg *Config) (*Conn, error)

Listen opens a packet sockets connection on the specified interface, using the given socket type and protocol values.

The socket type must be one of the Type constants: Raw or Datagram.

The Config specifies optional configuration for the Conn. A nil *Config applies the default configuration.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*Conn) ReadFrom

func (c *Conn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom implements the net.PacketConn ReadFrom method.

func (*Conn) SetBPF

func (c *Conn) SetBPF(filter []bpf.RawInstruction) error

SetBPF attaches an assembled BPF program to the Conn.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline implements the net.PacketConn SetDeadline method.

func (*Conn) SetPromiscuous

func (c *Conn) SetPromiscuous(enable bool) error

SetPromiscuous enables or disables promiscuous mode on the Conn, allowing it to receive traffic that is not addressed to the Conn's network interface.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.PacketConn SetReadDeadline method.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.PacketConn SetWriteDeadline method.

func (*Conn) Stats

func (c *Conn) Stats() (*Stats, error)

Stats retrieves statistics about the Conn from the Linux kernel.

Note that calling Stats will reset the kernel's internal counters for this Conn. If you want to maintain cumulative statistics by polling Stats over time, you must do so in your calling code.

func (*Conn) SyscallConn

func (c *Conn) SyscallConn() (syscall.RawConn, error)

SyscallConn returns a raw network connection. This implements the syscall.Conn interface.

func (*Conn) WriteTo

func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo implements the net.PacketConn WriteTo method.

type Stats

type Stats struct {
	// The total number of packets received.
	Packets uint32

	// The number of packets dropped.
	Drops uint32

	// The total number of times that a receive queue is frozen. May be zero if
	// the Linux kernel is not new enough to support TPACKET_V3 statistics.
	FreezeQueueCount uint32
}

Stats contains statistics about a Conn reported by the Linux kernel.

type Type

type Type int

Type is a socket type used when creating a Conn with Listen.

const (
	Raw Type
	Datagram
)

Possible Type values. Note that the zero value is not valid: callers must always specify one of Raw or Datagram when calling Listen.

Jump to

Keyboard shortcuts

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