pcap

package
v0.0.0-...-33a618f Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

The pcap package provides support for using the C pcap library from within a Go program. Not all of the functions exported by <pacp.h> have been included in this package and more will be added on request.

Index

Constants

View Source
const (
	DefaultBuffer  = int32(102400) // buffer limit
	DefaultPromisc = int32(1)      // 0->false, 1->true
	DefaultSnaplen = int32(65535)  // number of bytes to capture per packet.
	DefaultTimeout = int32(0)      // ms 0->no timeout
)

Safe default values. These are probably far from optimal for most systems.

Variables

View Source
var (
	ChanBuffSize    = 5000 // How many packets can we buffer in our channel.
	OptimizeFilters = 1    // Tells the bpf compiler to optimize filters.
)

Functions

func LibVersion

func LibVersion() string

LibVersion returns information about the version of libpcap being used. Note that it contains more information than just a version number.

func Statustostr

func Statustostr(errnum int32) string

Statustostr returns error strings for PCAP_ERROR_ and PCAP_WARNING_ values.

Types

type Pcap

type Pcap struct {
	FileName string           // Used for pcap_open_offline
	Device   string           // Used for pcap_open_live
	Snaplen  int32            // Specifies the maximum number of bytes to capture
	Promisc  int32            // 0->false, 1->true
	Timeout  int32            // ms
	Filters  []string         // track filters applied to the capture
	Pchan    chan *pkt.Packet // Channel for passing Packet pointers

	Packet pkt.TcpPacket // used by alloc-less version of loop
	// contains filtered or unexported fields
}

Pcap is the wrapper for the pcap_t struct in <pcap.h>.

func Create

func Create(device string) (*Pcap, error)

Create will construct a pcap that can be used to set custom settings like a larger buffer. The resulting Pcap must then be started with a call to Activate. See Open for an example list of calls that should be made.

func OpenLive

func OpenLive(device string, snaplen int32, promisc bool, timeout_ms int32) (*Pcap, error)

OpenLive returns a *Pcap and opens it to listen to live network traffic.

func OpenOffline

func OpenOffline(file string) (*Pcap, error)

OpenOffline returns a *Pcap and opens it to read pcap packets from a save file.

func (*Pcap) Activate

func (p *Pcap) Activate() error

Activate should only be called on a Pcap that was obtained through Create.

func (*Pcap) BreakLoop

func (p *Pcap) BreakLoop()

BreakLoop stops the reading of packets.

func (*Pcap) Close

func (p *Pcap) Close()

Close closes the files associated with p and deallocates C resources.

func (p *Pcap) Datalink() int32

Datalink returns the link layer type. For a list of possible DLT values see <pcap/bpf.h>.

func (*Pcap) DelayBreakLoop

func (p *Pcap) DelayBreakLoop(t int)

DelayBreakLoop stops the reading of packets after t seconds.

func (*Pcap) GetErr

func (p *Pcap) GetErr() error

GetErr returns an error based on the error text returned by pcap_geterr().

func (*Pcap) Getstats

func (p *Pcap) Getstats() (*stat.Stat, error)

Getstats returns a filled in Stat struct.

func (*Pcap) Listen

func (p *Pcap) Listen(r chan *[]*pkt.Packet)

Listen will accumulate packets until it is stopped by a nil packet pointer.

func (*Pcap) Loop

func (p *Pcap) Loop(cnt int)

Loop keeps reading packets until cnt packets are processed or an error occurs.

func (*Pcap) LoopWithCallback

func (p *Pcap) LoopWithCallback(cnt int, callback func(*pkt.TcpPacket) bool)

Callback version of Loop(). CB signals to quit returning true. CB may keep packet if it calls Save().

func (*Pcap) LoopWithCallbackAllocless

func (p *Pcap) LoopWithCallbackAllocless(cnt int, callback func(*pkt.TcpPacket) bool)

Callback+allocless version of Loop(). CB signals to quit returning true. CB must not keep a ref to packet. It can use Clone() to get its own copy.

func (*Pcap) NewPktTrace

func (p *Pcap) NewPktTrace(data *[]*pkt.Packet) (*trace.PktTrace, error)

NewPktTrace is a beta function and should be treated as such.

func (*Pcap) Next

func (p *Pcap) Next() *pkt.Packet

Next is a wrapper for NextEx().

func (*Pcap) NextEx

func (p *Pcap) NextEx() (*pkt.Packet, int32)

NextEx reads the next packet and returns a success/failure indication:

1	the packet was read without problems
0	packets are being read from a live capture, and the timeout expired

-1 an error occurred while reading the packet -2 packets are being read from a file, and there are no more packets to read

func (*Pcap) NextEx2

func (p *Pcap) NextEx2() (pkt.TcpPacket, int32)

NextEx2 is just like NextEx, but returns only TCP/IPv4 packets. This packet creates no new heap allocations unless the previous packet was "saved" (it's Save() member was called)

func (*Pcap) Open

func (p *Pcap) Open() error

Open creates a packet capture descriptor to look at packets on the network. Calling C.pcap_open_live is the equivalent of calling:

C.pcap_create
C.pcap_set_snaplen
C.pcap_set_promisc
C.pcap_set_timeout
C.pcap_activate

In that order. So if you want to use custom values for any thing that has to be set before pcap is active you should use Create instead of Open or OpenLive.

func (*Pcap) OpenFile

func (p *Pcap) OpenFile() error

OpenFile opens a savefile for reading.

func (*Pcap) SetBufferSize

func (p *Pcap) SetBufferSize(bufferSize int32) error

SetBufferSize should only be called on a Pcap that was obtained through Create. If the buffer size is too small then Activate will fail with a generic PCAP_ERROR.

func (*Pcap) SetPromisc

func (p *Pcap) SetPromisc(promisc bool) error

SetPromisc should only be called on a Pcap that was obtained through Create.

func (*Pcap) SetSnaplen

func (p *Pcap) SetSnaplen(snaplen int32) error

SetSnaplen should only be called on a Pcap that was obtained through Create.

func (*Pcap) SetTimeout

func (p *Pcap) SetTimeout(timeout_ms int32) error

SetTimeout should only be called on a Pcap that was obtained through Create.

func (*Pcap) Setfilter

func (p *Pcap) Setfilter(expr string) error

Setfilter compiles a filter string into a bpf program and sets the filter.

Directories

Path Synopsis
The filter package provides support for assembling fell formed bpf filters.
The filter package provides support for assembling fell formed bpf filters.
The pkt package provides access to the packet internals.
The pkt package provides access to the packet internals.
The stat package provides support for collecting libpcap stats.
The stat package provides support for collecting libpcap stats.

Jump to

Keyboard shortcuts

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