packet

package
v0.100.0-153 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TCPSyn    = 0x02
	TCPAck    = 0x10
	TCPSynAck = TCPSyn | TCPAck
)

Variables

This section is empty.

Functions

func Generate added in v0.100.0

func Generate(h Header, payload []byte) []byte

Generate generates a new packet with the given header and payload. Unlike Header.Marshal, this does allocate memory.

func Hexdump

func Hexdump(b []byte) string

Types

type Header interface {
	// Len returns the length of the header after marshaling.
	Len() int
	// Marshal serializes the header into buf in wire format.
	// It clobbers the header region, which is the first h.Length() bytes of buf.
	// It explicitly initializes every byte of the header region,
	// so pre-zeroing it on reuse is not required. It does not allocate memory.
	// It fails if and only if len(buf) < Length().
	Marshal(buf []byte) error
	// ToResponse transforms the header into one for a response packet.
	// For instance, this swaps the source and destination IPs.
	ToResponse()
}

Header is a packet header capable of marshaling itself into a byte buffer.

type ICMPCode added in v0.100.0

type ICMPCode uint8
const (
	ICMPNoCode ICMPCode = 0
)

type ICMPHeader added in v0.100.0

type ICMPHeader struct {
	IPHeader
	Type ICMPType
	Code ICMPCode
}

ICMPHeader represents an ICMP packet header.

func (ICMPHeader) Len added in v0.100.0

func (ICMPHeader) Len() int

func (ICMPHeader) Marshal added in v0.100.0

func (h ICMPHeader) Marshal(buf []byte) error

func (*ICMPHeader) ToResponse added in v0.100.0

func (h *ICMPHeader) ToResponse()

type ICMPType added in v0.100.0

type ICMPType uint8
const (
	ICMPEchoReply    ICMPType = 0x00
	ICMPEchoRequest  ICMPType = 0x08
	ICMPUnreachable  ICMPType = 0x03
	ICMPTimeExceeded ICMPType = 0x0b
)

func (ICMPType) String added in v0.100.0

func (t ICMPType) String() string

type IP

type IP uint32

IP is an IPv4 address.

func IPFromNetaddr added in v1.0.0

func IPFromNetaddr(ip netaddr.IP) IP

IPFromNetaddr converts a netaddr.IP to an IP.

func NewIP

func NewIP(b net.IP) IP

NewIP converts a standard library IP address into an IP. It panics if b is not an IPv4 address.

func (IP) Netaddr added in v1.0.0

func (ip IP) Netaddr() netaddr.IP

Netaddr converts an IP to a netaddr.IP.

func (IP) String

func (ip IP) String() string

type IPHeader added in v0.100.0

type IPHeader struct {
	IPProto IPProto
	IPID    uint16
	SrcIP   IP
	DstIP   IP
}

IPHeader represents an IP packet header.

func (IPHeader) Len added in v0.100.0

func (IPHeader) Len() int

func (IPHeader) Marshal added in v0.100.0

func (h IPHeader) Marshal(buf []byte) error

func (IPHeader) MarshalPseudo added in v0.100.0

func (h IPHeader) MarshalPseudo(buf []byte) error

MarshalPseudo serializes the header into buf in pseudo format. It clobbers the header region, which is the first h.Length() bytes of buf. It explicitly initializes every byte of the header region, so pre-zeroing it on reuse is not required. It does not allocate memory.

func (*IPHeader) ToResponse added in v0.100.0

func (h *IPHeader) ToResponse()

type IPProto

type IPProto uint8

IPProto is either a real IP protocol (ITCP, UDP, ...) or an special value like Unknown. If it is a real IP protocol, its value corresponds to its IP protocol number.

const (
	// Unknown represents an unknown or unsupported protocol; it's deliberately the zero value.
	Unknown IPProto = 0x00
	ICMP    IPProto = 0x01
	TCP     IPProto = 0x06
	UDP     IPProto = 0x11
	// IPv6 and Fragment are special values. They're not really IPProto values
	// so we're using the unassigned 0xFE and 0xFF values for them.
	// TODO(dmytro): special values should be taken out of here.
	IPv6     IPProto = 0xFE
	Fragment IPProto = 0xFF
)

func (IPProto) String

func (p IPProto) String() string

type ParsedPacket added in v0.100.0

type ParsedPacket struct {
	IPProto  IPProto // IP subprotocol (UDP, TCP, etc)
	SrcIP    IP      // IP source address
	DstIP    IP      // IP destination address
	SrcPort  uint16  // TCP/UDP source port
	DstPort  uint16  // TCP/UDP destination port
	TCPFlags uint8   // TCP flags (SYN, ACK, etc)
	// contains filtered or unexported fields
}

ParsedPacket is a minimal decoding of a packet suitable for use in filters.

func (*ParsedPacket) Buffer added in v0.100.0

func (q *ParsedPacket) Buffer() []byte

Buffer returns the entire packet buffer. This is a read-only view; that is, q retains the ownership of the buffer.

func (*ParsedPacket) Decode added in v0.100.0

func (q *ParsedPacket) Decode(b []byte)

Decode extracts data from the packet in b into q. It performs extremely simple packet decoding for basic IPv4 packet types. It extracts only the subprotocol id, IP addresses, and (if any) ports, and shouldn't need any memory allocation.

func (*ParsedPacket) ICMPHeader added in v0.100.0

func (q *ParsedPacket) ICMPHeader() ICMPHeader

func (*ParsedPacket) IPHeader added in v0.100.0

func (q *ParsedPacket) IPHeader() IPHeader

func (*ParsedPacket) IsEchoRequest added in v0.100.0

func (q *ParsedPacket) IsEchoRequest() bool

IsEchoRequest reports whether q is an IPv4 ICMP Echo Request.

func (*ParsedPacket) IsEchoResponse added in v0.100.0

func (q *ParsedPacket) IsEchoResponse() bool

IsEchoRequest reports whether q is an IPv4 ICMP Echo Response.

func (*ParsedPacket) IsError added in v0.100.0

func (q *ParsedPacket) IsError() bool

IsError reports whether q is an IPv4 ICMP "Error" packet.

func (*ParsedPacket) IsTCPSyn added in v0.100.0

func (q *ParsedPacket) IsTCPSyn() bool

IsTCPSyn reports whether q is a TCP SYN packet (i.e. the first packet in a new connection).

func (*ParsedPacket) Payload added in v0.100.0

func (q *ParsedPacket) Payload() []byte

Payload returns the payload of the IP subprotocol section. This is a read-only view; that is, q retains the ownership of the buffer.

func (*ParsedPacket) String added in v0.100.0

func (q *ParsedPacket) String() string

func (*ParsedPacket) Sub added in v0.100.0

func (q *ParsedPacket) Sub(begin, n int) []byte

Sub returns the IP subprotocol section. This is a read-only view; that is, q retains the ownership of the buffer.

func (*ParsedPacket) Trim added in v0.100.0

func (q *ParsedPacket) Trim() []byte

Trim trims the buffer to its IPv4 length. Sometimes packets arrive from an interface with extra bytes on the end. This removes them.

func (*ParsedPacket) UDPHeader added in v0.100.0

func (q *ParsedPacket) UDPHeader() UDPHeader

type UDPHeader added in v0.100.0

type UDPHeader struct {
	IPHeader
	SrcPort uint16
	DstPort uint16
}

UDPHeader represents an UDP packet header.

func (UDPHeader) Len added in v0.100.0

func (UDPHeader) Len() int

func (UDPHeader) Marshal added in v0.100.0

func (h UDPHeader) Marshal(buf []byte) error

func (*UDPHeader) ToResponse added in v0.100.0

func (h *UDPHeader) ToResponse()

Jump to

Keyboard shortcuts

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