packet

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 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) IsLinkLocalUnicast added in v1.2.0

func (ip IP) IsLinkLocalUnicast() bool

func (IP) IsMulticast added in v1.2.0

func (ip IP) IsMulticast() bool

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
	IGMP    IPProto = 0x02
	ICMPv6  IPProto = 0x3a
	TCP     IPProto = 0x06
	UDP     IPProto = 0x11
	// Fragment is a special value. It's not really an IPProto value
	// so we're using the unassigned 0xFF value.
	// TODO(dmytro): special values should be taken out of here.
	Fragment IPProto = 0xFF
)

func (IPProto) String

func (p IPProto) String() string

type NextHeader added in v1.0.0

type NextHeader uint8

NextHeader

type ParsedPacket added in v0.100.0

type ParsedPacket struct {
	IPVersion uint8   // 4, 6, or 0
	IPProto   IPProto // IP subprotocol (UDP, TCP, etc); the NextHeader field for IPv6
	SrcIP     IP      // IP source address (not used for IPv6)
	DstIP     IP      // IP destination address (not used for IPv6)
	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.

In general, it only supports IPv4. The IPv6 parsing is very minimal.

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 (p *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