pkt

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: 3

Documentation

Overview

The pkt package provides access to the packet internals.

Index

Constants

View Source
const (
	LinkLayer      = 0 // Index for OSI Data Link Layer in Pkt.Headers
	NetworkLayer   = 1 // Index for OSI Network Layer in Pkt.Headers
	TransportLayer = 2 // Index for OSI Transport Layer in Pkt.Headers
)

These indices can be used with the []Hdr generated by NewPacket to access common headers.

View Source
const (
	EtherTypeIPv4 = uint16(0x0800) // Internet Protocol version 4
	EtherTypeIPv6 = uint16(0x86DD) // Internet Protocol version 6
	EtherTypeARP  = uint16(0x0806) // Address Resolution Protocol
)

These two-octet constants can be compared with the captured value to indicate which protocol is encapsulated in the payload of an Ethernet Frame.

View Source
const (
	IpProtoTCP = uint8(0x06) // Transmission Control Protocol (TCP)
	IpProtoUDP = uint8(0x11) // User Datagram Protocol (UDP)
)

These IP protocol numbers are used in the Protocol field of the IPv4 header and the Next Header field of IPv6 header.

View Source
const (
	TCP_NULL = uint16(0x0000) // No flags set
	TCP_FIN  = uint16(0x0001) // No more data from sender
	TCP_SYN  = uint16(0x0002) // Synchronize sequence numbers
	TCP_RST  = uint16(0x0004) // Reset the connection
	TCP_PSH  = uint16(0x0008) // Push the buffered data
	TCP_ACK  = uint16(0x0010) // Acknowledgment field is significant
	TCP_URG  = uint16(0x0020) // Urgent pointer field is significant
	TCP_ECE  = uint16(0x0040) // ECN-Echo
	TCP_CWR  = uint16(0x0080) // Congestion Window Reduced
	TCP_NS   = uint16(0x0100) // ECN-nonce concealment protection
)

TCP flags: use these constants bitwise with the TcpHdr.Flags field to detect the presence of a particular TCP flag.

View Source
const BSD_LO_IPV4 = C.AF_INET
View Source
const BSD_LO_IPV6 = 24
View Source
const ETHERTYPE_IP = C.ETHERTYPE_IP>>8 | C.ETHERTYPE_IP&0xFF<<8

FIXME: we are assuming little endian arch... everywhere

View Source
const ETHERTYPE_IPV6 = C.ETHERTYPE_IPV6>>8 | C.ETHERTYPE_IPV6&0xFF<<8
View Source
const FBSD_LO_IPV6 = 28
View Source
const IPV6_HEADER_LEN = 40 // fixed, unlike IPv4's
View Source
const OSX_LO_IPV6 = 30

Variables

This section is empty.

Functions

func NewPacketAllocless

func NewPacketAllocless(pkthdr_ptr unsafe.Pointer, buf_ptr unsafe.Pointer, datalinkType int32, packet *TcpPacket) bool

NewPacketAllocless takes a libpcap buffer and extracts a TCP/IPv{4,6} packet into an existing TcpPacket. Payload isn't copied, it's mapped, use func Clone()/Save() to get a non-volatile copy. Returns false if error.

Types

type EthHdr

type EthHdr struct {
	SrcAddr   net.HardwareAddr // the sender's MAC address
	DstAddr   net.HardwareAddr // the receiver's MAC address
	EtherType uint16           // packet type ID field
	// contains filtered or unexported fields
}

The EthHdr struct is a wrapper for the ether_header struct in <net/ethernet.h>.

func NewEthHdr

func NewEthHdr(p unsafe.Pointer) (*EthHdr, unsafe.Pointer)

With an unsafe.Pointer to the block of C memory NewEthHdr returns a filled in EthHdr struct.

func (*EthHdr) CsvElement

func (h *EthHdr) CsvElement() string

CsvElement returns a CSV encoding of the EthHdr struct. The string "ETH" signifies the beginning of the EthHdr.

func (*EthHdr) JsonElement

func (h *EthHdr) JsonElement() string

JsonElement returns a JSON encoding of the EthHdr struct.

func (*EthHdr) String

func (h *EthHdr) String() string

String returns a minimal encoding of the EthHdr struct.

type Hdr

type Hdr interface {
	JsonElement() string
	CsvElement() string
	String() string
}

The Hdr interface allows us to deal with an array of headers.

type HttpHdr

type HttpHdr struct {
	Proto      string // e.g. "HTTP/1.0"
	Method     string // GET, POST, PUT, etc.
	RequestURI string // The unmodified Request-URI
	StatusCode int64  // e.g. 200
	Status     string // e.g. "OK"
}

The point of a HttpHdr is to ease the mapping of application level logs to pcap traces. Capturing the actual HTTP headers has not been implemented to keep this fast.

Since HTTP requests and responses can span multiple packets this is not perfect and as such your millage may vary.

func NewHttpHdr

func NewHttpHdr(b []byte) *HttpHdr

Given the payload of a transport layer packet NewHttpHdr will return a *HttpHdr struct is the bytes contain a valid HTTP header, or nil otherwise.

func (*HttpHdr) CsvElement

func (h *HttpHdr) CsvElement() string

CsvElement returns a CSV encoding of the HttpHdr struct. The string "HTTP_REQ" signifies the beginning of the HttpHdr generated on a HTTP request. The string "HTTP_RESP" signifies the beginning of the HttpHdr generated by a HTTP response.

func (*HttpHdr) JsonElement

func (h *HttpHdr) JsonElement() string

JsonElement returns a JSON encoding of the HttpHdr struct.

func (*HttpHdr) String

func (h *HttpHdr) String() string

String returns a minimal encoding of the HttpHdr struct.

type InetProtoHdr

type InetProtoHdr interface {
	Src() net.IP
	Dst() net.IP
	Proto() uint8
	PL() uint16
}

The InetProtoHdr interface allows us to deal with IPv4 and IPv6 headers in aggregate.

type Ip6Hdr

type Ip6Hdr struct {
	SrcAddr    net.IP // the sender's ip6 address
	DstAddr    net.IP // the receiver's ipv6 address
	NextHeader uint8  // next header
	PayloadLen uint16 // payload length
	// contains filtered or unexported fields
}

The Ip6Hdr struct is a wrapper for the ip6_hdrstruct in <netinet/ip6.h>.

func NewIp6Hdr

func NewIp6Hdr(p unsafe.Pointer) (*Ip6Hdr, unsafe.Pointer)

With an unsafe.Pointer to the block of C memory NewIp6Hdr returns a filled in Ip6Hdr struct.

func (*Ip6Hdr) CsvElement

func (h *Ip6Hdr) CsvElement() string

CsvElement returns a CSV encoding of the Ip6Hdr struct. The string "IP6" signifies the beginning of the Ip6Hdr.

func (*Ip6Hdr) Dst

func (h *Ip6Hdr) Dst() net.IP

Dst returns the network layer destination address.

func (*Ip6Hdr) JsonElement

func (h *Ip6Hdr) JsonElement() string

JsonElement returns a JSON encoding of the Ip6Hdr struct.

func (*Ip6Hdr) PL

func (h *Ip6Hdr) PL() uint16

PL returns the Payload length.

func (*Ip6Hdr) Proto

func (h *Ip6Hdr) Proto() uint8

Proto returns the IP protocol number.

func (*Ip6Hdr) Src

func (h *Ip6Hdr) Src() net.IP

Src returns the network layer source address.

func (*Ip6Hdr) String

func (h *Ip6Hdr) String() string

String returns a minimal encoding of the Ip6Hdr struct.

type IpHdr

type IpHdr struct {
	Ihl        uint8  // header length (32bit words)
	Version    uint8  // version
	SrcAddr    net.IP // source address
	DstAddr    net.IP // dest address
	Protocol   uint8  // protocol
	TotLen     uint16 // total length (bytes)
	PayloadLen uint16 // payload length (bytes)
	// contains filtered or unexported fields
}

The IpHdr struct is a wrapper for the iphdr struct in <netinet/ip.h>.

func NewIpHdr

func NewIpHdr(p unsafe.Pointer) (*IpHdr, unsafe.Pointer)

With an unsafe.Pointer to the block of C memory NewIpHdr returns a filled in IpHdr struct.

func (*IpHdr) CsvElement

func (h *IpHdr) CsvElement() string

CsvElement returns a CSV encoding of the IpHdr struct. The string "IP4" signifies the beginning of the IpHdr.

func (*IpHdr) Dst

func (h *IpHdr) Dst() net.IP

Dst returns the network layer destination address.

func (*IpHdr) Id

func (h *IpHdr) Id() uint16

Id returns the identification of the IP flow.

func (*IpHdr) JsonElement

func (h *IpHdr) JsonElement() string

JsonElement returns a JSON encoding of the IpHdr struct.

func (*IpHdr) PL

func (h *IpHdr) PL() uint16

PL returns the Payload length.

func (*IpHdr) Proto

func (h *IpHdr) Proto() uint8

Proto returns the IP protocol number.

func (*IpHdr) Src

func (h *IpHdr) Src() net.IP

Src returns the network layer source address.

func (*IpHdr) String

func (h *IpHdr) String() string

String returns a minimal encoding of the IpHdr struct.

type Packet

type Packet struct {
	Time    time.Time // time stamp from the nic
	Caplen  uint32    // length of portion present
	Len     uint32    // length this packet (off wire)
	Headers []Hdr     // Go wrappers for C pkt headers
	// contains filtered or unexported fields
}

The Packet struct is a wrapper for the pcap_pkthdr struct in <pcap.h>.

func NewPacket

func NewPacket(pkthdr_ptr unsafe.Pointer, buf_ptr unsafe.Pointer) *Packet

NewPacket returns a parsed and decoded Packet. pkthdr_ptr should be a *C.struct_pcap_pkthdr buf_ptr should be a *C.u_char

func (*Packet) CsvString

func (p *Packet) CsvString() string

CsvString returns a CSV encoding of the Packet struct. Each header type has a unique string that marks the beginning of the CSV fields for that particular header.

func (*Packet) JsonString

func (p *Packet) JsonString() string

JsonString returns a JSON encoding of the Packet struct.

func (*Packet) String

func (p *Packet) String() string

String returns a minimal encoding of the Packet struct.

type TcpHdr

type TcpHdr struct {
	Source uint16 // source port
	Dest   uint16 // destination port
	Seq    uint32 // sequence number
	AckSeq uint32 // acknowledgement number
	Doff   uint8  // The length of the TCP header (data offset) in 32 bit words.
	Flags  uint16 // TCP flags per RFC 793, September, 1981
	Window uint16 // window advertisement
	Check  uint16 // checksum
	UrgPtr uint16 // urgent pointer
	// contains filtered or unexported fields
}

The TcpHdr struct is a wrapper for the tcphdr struct in <netinet/tcp.h>.

func NewTcpHdr

func NewTcpHdr(p unsafe.Pointer) (*TcpHdr, unsafe.Pointer)

With an unsafe.Pointer to the block of C memory NewTcpHdr returns a filled in TcpHdr struct.

func (*TcpHdr) CsvElement

func (h *TcpHdr) CsvElement() string

CsvElement returns a CSV encoding of the TcpHdr struct. The string "TCP" signifies the beginning of the TcpHdr.

func (*TcpHdr) GetPayloadBytes

func (h *TcpHdr) GetPayloadBytes(pl uint16) []byte

GetPayloadBytes returns the bytes from the packet's payload. This is a Go slice backed by the C bytes. The result is that the Go slice uses very little extra memory.

func (*TcpHdr) JsonElement

func (h *TcpHdr) JsonElement() string

JsonElement returns a JSON encoding of the TcpHdr struct.

func (*TcpHdr) PayloadLen

func (h *TcpHdr) PayloadLen(pl uint16) uint16

PayloadLen returns the length of the TCP packet's payload in bytes.

func (*TcpHdr) String

func (h *TcpHdr) String() string

String returns a minimal encoding of the TcpHdr struct.

type TcpPacket

type TcpPacket struct {
	DstAddr0  uint32 // IPv4 uses only this one, others are 0
	DstAddr1  uint32
	DstAddr2  uint32
	DstAddr3  uint32
	SrcAddr0  uint32 // IPv4 uses only this one, others are 0
	SrcAddr1  uint32
	SrcAddr2  uint32
	SrcAddr3  uint32
	AckSeq    uint32
	Seq       uint32
	Source    uint16
	Dest      uint16
	Flags     uint16
	Payload   []byte
	Timestamp time.Time
	IsRequest bool
	Saved     bool
}

func NewPacket2

func NewPacket2(pkthdr_ptr unsafe.Pointer, buf_ptr unsafe.Pointer, datalinkType int32) *TcpPacket

NewPacket2 takes a libpcap buffer and extracts a TCP/IPv{4,6} packet into a new TcpPacket without creating additional data in the heap. If the recipient of this packet needs to keep it after returning to sniffer, it should call func Save() so the packet's payload becomes private instead of mapped into sniffer's buffers. Returns TcpPacket or nil if error.

func (*TcpPacket) Clone

func (this *TcpPacket) Clone() *TcpPacket

func (*TcpPacket) IsIPv4

func (this *TcpPacket) IsIPv4() bool

func (*TcpPacket) Save

func (this *TcpPacket) Save()

type UdpHdr

type UdpHdr struct {
	Source uint16 // source port
	Dest   uint16 // destination port
	Len    uint16 // datagram length (header + payload) in bytes
	Check  uint16 // checksum
	// contains filtered or unexported fields
}

The UdpHdr struct is a wrapper for the udphdr struct in <netinet/udp.h>.

func NewUdpHdr

func NewUdpHdr(p unsafe.Pointer) (*UdpHdr, unsafe.Pointer)

With an unsafe.Pointer to the block of C memory NewUdpHdr returns a filled in UdpHdr struct.

func (*UdpHdr) CsvElement

func (h *UdpHdr) CsvElement() string

CsvElement returns a CSV encoding of the UdpHdr struct. The string "UDP" signifies the beginning of the UdpHdr.

func (*UdpHdr) GetPayloadBytes

func (h *UdpHdr) GetPayloadBytes(pl uint16) []byte

GetPayloadBytes returns the bytes from the packet's payload. This is a Go slice backed by the C bytes. The result is that the Go slice uses very little extra memory.

func (*UdpHdr) JsonElement

func (h *UdpHdr) JsonElement() string

JsonElement returns a JSON encoding of the UdpHdr struct.

func (*UdpHdr) PayloadLen

func (h *UdpHdr) PayloadLen(pl uint16) uint16

PayloadLen returns the length of the UDP packet's payload in bytes.

func (*UdpHdr) String

func (h *UdpHdr) String() string

String returns a minimal encoding of the UdpHdr struct.

Jump to

Keyboard shortcuts

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