Documentation ¶
Index ¶
- Constants
- func NewErrInvalidPcap(err error) error
- type BlockType
- type ErrInvalidPcap
- type NgInterface
- type NgInterfaceStatistics
- type NgReader
- func (r *NgReader) InterfaceDescriptor(block []byte) (NgInterface, error)
- func (r *NgReader) Offset() uint64
- func (r *NgReader) Packet(block []byte) ([]byte, nano.Ts, layers.LinkType, error)
- func (r *NgReader) Read() ([]byte, BlockType, error)
- func (r *NgReader) SectionHeader(block []byte) NgSectionInfo
- func (r *NgReader) SetWarningHandler(warner Warner)
- type NgResolution
- type NgSectionInfo
- type PcapReader
- func (r *PcapReader) Offset() uint64
- func (r *PcapReader) Packet(block []byte) ([]byte, nano.Ts, layers.LinkType, error)
- func (r *PcapReader) Read() ([]byte, BlockType, error)
- func (r *PcapReader) Resolution() gopacket.TimestampResolution
- func (r *PcapReader) SetSnaplen(newSnaplen uint32)
- func (r *PcapReader) Snaplen() uint32
- func (r *PcapReader) String() string
- func (r *PcapReader) TsFromHeader(hdr []byte) nano.Ts
- func (r *PcapReader) Version() string
- type Reader
- type Warner
Constants ¶
const NgNoValue64 = math.MaxUint64
NgNoValue64 is a placeholder for an empty numeric 64 bit value.
const PacketBlockHeaderLen = 28
Variables ¶
This section is empty.
Functions ¶
func NewErrInvalidPcap ¶
Types ¶
type ErrInvalidPcap ¶
type ErrInvalidPcap struct {
// contains filtered or unexported fields
}
func (*ErrInvalidPcap) Error ¶
func (e *ErrInvalidPcap) Error() string
func (*ErrInvalidPcap) Is ¶
func (e *ErrInvalidPcap) Is(target error) bool
func (*ErrInvalidPcap) Unwrap ¶
func (e *ErrInvalidPcap) Unwrap() error
type NgInterface ¶
type NgInterface struct { // Name is the name of the interface. This value might be empty if this option is missing. Name string // Comment can be an arbitrary comment. This value might be empty if this option is missing. Comment string // Description is a description of the interface. This value might be empty if this option is missing. Description string // Filter is the filter used during packet capture. This value might be empty if this option is missing. Filter string // OS is the operating system this interface was controlled by. This value might be empty if this option is missing. OS string // LinkType is the linktype of the interface. LinkType layers.LinkType // TimestampResolution is the timestamp resolution of the packets in the pcapng file belonging to this interface. TimestampResolution NgResolution // TimestampResolution is the timestamp offset in seconds of the packets in the pcapng file belonging to this interface. TimestampOffset uint64 // SnapLength is the maximum packet length captured by this interface. 0 for unlimited SnapLength uint32 // Statistics holds the interface statistics Statistics NgInterfaceStatistics // contains filtered or unexported fields }
NgInterface holds all the information of a pcapng interface.
func (NgInterface) Resolution ¶
func (i NgInterface) Resolution() gopacket.TimestampResolution
Resolution returns the timestamp resolution of acquired timestamps before scaling to NanosecondTimestampResolution.
type NgInterfaceStatistics ¶
type NgInterfaceStatistics struct { // LastUpdate is the last time the statistics were updated. LastUpdate time.Time // StartTime is the time packet capture started on this interface. This value might be zero if this option is missing. StartTime time.Time // EndTime is the time packet capture ended on this interface This value might be zero if this option is missing. EndTime time.Time // Comment can be an arbitrary comment. This value might be empty if this option is missing. Comment string // PacketsReceived are the number of received packets. This value might be NoValue64 if this option is missing. PacketsReceived uint64 // PacketsReceived are the number of received packets. This value might be NoValue64 if this option is missing. PacketsDropped uint64 }
NgInterfaceStatistics hold the statistic for an interface at a single point in time. These values are already supposed to be accumulated. Most pcapng files contain this information at the end of the file/section.
type NgReader ¶
NgReader wraps an underlying bufio.NgReader to read packet data in pcapng.
func NewNgReader ¶
NewNgReader initializes a new writer, reads the first section header, and if necessary according to the options the first interface.
func (*NgReader) InterfaceDescriptor ¶
func (r *NgReader) InterfaceDescriptor(block []byte) (NgInterface, error)
func (*NgReader) Packet ¶
Packet returns the captured portion of a packet from an enhanced packet block returned by Read() (i.e., with BlockType equal to TypePacket) beginning with the link-layer header. It also extracts the capture timestamp and link layer type and returns those values along with the packet. If an error is encountered, zero values are returned for the three values. PCAP-NG simple packet types aren't supported yet (we presume this type of trace is rare and does not fit our use case here as these traces do not include capture timestamps and the point here is to pull our ranges of packets from a large pcap based on timestamp). We also do not support the original deprecated PCAP-NG packet format but could add support if users request this (it would only be because old pcaps with this deprecated format are sitting around).
func (*NgReader) Read ¶
readPacketHeader looks for a packet (enhanced, simple, or packet) and parses the header. If an interface descriptor, an interface statistics block, or a section header is encountered, those are handled accordingly. All other block types are skipped. New block types must be added here.
func (*NgReader) SectionHeader ¶
func (r *NgReader) SectionHeader(block []byte) NgSectionInfo
func (*NgReader) SetWarningHandler ¶
type NgResolution ¶
type NgResolution uint8
NgResolution represents a pcapng timestamp resolution
func (NgResolution) Binary ¶
func (r NgResolution) Binary() bool
Binary returns true if the timestamp resolution is a negative power of two. Otherwise NgResolution is a negative power of 10.
func (NgResolution) Exponent ¶
func (r NgResolution) Exponent() uint8
Exponent returns the negative exponent of the resolution.
func (NgResolution) ToTimestampResolution ¶
func (r NgResolution) ToTimestampResolution() (ret gopacket.TimestampResolution)
ToTimestampResolution converts an NgResolution to a gopaket.TimestampResolution
type NgSectionInfo ¶
type NgSectionInfo struct { MajorVersion uint16 MinorVersion uint16 // Hardware is the hardware this file was generated on. This value might be empty if this option is missing. Hardware string // OS is the operating system this file was generated on. This value might be empty if this option is missing. OS string // Application is the user space application this file was generated with. This value might be empty if this option is missing. Application string // Comment can be an arbitrary comment. This value might be empty if this option is missing. Comment string }
NgSectionInfo contains additional information of a pcapng section
func (NgSectionInfo) Version ¶
func (n NgSectionInfo) Version() string
type PcapReader ¶
type PcapReader struct { *peeker.Reader LinkType layers.LinkType // contains filtered or unexported fields }
PcapReader implements the Reader interface to read packet data in PCAP format. See http://wiki.wireshark.org/Development/LibpcapFileFormat for information on the file format.
We currenty read v2.4 file format with nanosecond and microsecond timestamp resolution in little-endian and big-endian encoding.
If the PCAP data is gzip compressed it is transparently uncompressed by wrapping the given io.Reader with a gzip.Reader.
func NewPcapReader ¶
func NewPcapReader(r io.Reader) (*PcapReader, error)
NewPcapReader returns a new reader object, for reading packet data from the given reader. The reader must be open and header data is read from it at this point. If the file format is not supported an error is returned.
// Create new reader: f, _ := fs.Open("/tmp/file.pcap") defer f.Close() r, err := NewReader(f) data, info, err := r.Read()
func (*PcapReader) Offset ¶
func (r *PcapReader) Offset() uint64
func (*PcapReader) Resolution ¶
func (r *PcapReader) Resolution() gopacket.TimestampResolution
Resolution returns the timestamp resolution of acquired timestamps before scaling to NanosecondTimestampResolution.
func (*PcapReader) SetSnaplen ¶
func (r *PcapReader) SetSnaplen(newSnaplen uint32)
SetSnaplen sets the snapshot length of the capture file.
This is useful when a pcap file contains packets bigger than then snaplen. Pcapgo will error when reading packets bigger than snaplen, then it dumps those packets and reads the next 16 bytes, which are part of the "faulty" packet's payload, but pcapgo thinks it's the next header, which is probably also faulty because it's not really a packet header. This can lead to a lot of faulty reads.
The SetSnaplen function can be used to set a bigger snaplen to prevent those read errors.
This snaplen situation can happen when a pcap writer doesn't truncate packets to the snaplen size while writing packets to file. E.g. In Python, dpkt.pcap.Writer sets snaplen by default to 1500 (https://dpkt.readthedocs.io/en/latest/api/api_auto.html#dpkt.pcap.Writer) but doesn't enforce this when writing packets (https://dpkt.readthedocs.io/en/latest/_modules/dpkt/pcap.html#Writer.writepkt). When reading, tools like tcpdump, tcpslice, mergecap and wireshark ignore the snaplen and use their own defined snaplen. E.g. When reading packets, tcpdump defines MAXIMUM_SNAPLEN (https://github.com/the-tcpdump-group/tcpdump/blob/6e80fcdbe9c41366df3fa244ffe4ac8cce2ab597/netdissect.h#L290) and uses it (https://github.com/the-tcpdump-group/tcpdump/blob/66384fa15b04b47ad08c063d4728df3b9c1c0677/print.c#L343-L358).
For further reading:
- https://github.com/the-tcpdump-group/tcpdump/issues/389
- https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8808
- https://www.wireshark.org/lists/wireshark-dev/201307/msg00061.html
- https://github.com/wireshark/wireshark/blob/bfd51199e707c1d5c28732be34b44a9ee8a91cd8/wiretap/pcap-common.c#L723-L742
- https://github.com/wireshark/wireshark/blob/f07fb6cdfc0904905627707b88450054e921f092/wiretap/libpcap.c#L592-L598
- https://github.com/wireshark/wireshark/blob/f07fb6cdfc0904905627707b88450054e921f092/wiretap/libpcap.c#L714-L727
- https://github.com/the-tcpdump-group/tcpdump/commit/d033c1bc381c76d13e4aface97a4f4ec8c3beca2
- https://github.com/the-tcpdump-group/tcpdump/blob/88e87cb2cb74c5f939792171379acd9e0efd8b9a/netdissect.h#L263-L290
func (*PcapReader) Snaplen ¶
func (r *PcapReader) Snaplen() uint32
Snaplen returns the snapshot length of the capture file.
func (*PcapReader) TsFromHeader ¶
func (r *PcapReader) TsFromHeader(hdr []byte) nano.Ts
func (*PcapReader) Version ¶
func (r *PcapReader) Version() string
type Reader ¶
type Reader interface { Read() ([]byte, BlockType, error) Packet([]byte) ([]byte, nano.Ts, layers.LinkType, error) Offset() uint64 }
Reader is an interface for reading data blocks from a pcap, either a legacy pcap or a next-gen pcap. The Read method returns blocks of data that are one of: a pcap file header (TypeSection), a pcap packet including the capture header (TypePacket), a pcap-ng section block (TypeSection), a pcap-ng interface block (TypeInterface), or a pcap-ng packet block (TypePacket). For TypePacket, the capture timestamp and the link-layer type of the packet is indicated in the Info return value.
func NewReaderWithWarnings ¶
NewReaderWithWarnings returns a Reader by trying both pcap and pcap-ng formats and arranges for warning messages to be sent over the given channel. Different pcap implementations can have out-of-spec peculiarities that can be tolerated so we send warnings and try to keep going.