Documentation
¶
Overview ¶
Package packet encodes and decodes the IPv4, IPv6 and TCP headers of single packets, as a raw socket sends them and a packet socket captures them.
Decoding is a bounds-checked reading of the header fields, with no reassembly and no stream state: a packet is taken as it comes. Checksums are written on encode and not verified on decode, since a captured packet's checksum is often left to the network card to fill in. Errors carry no stack trace, so that a decoder fed every captured packet does not pay for one on each that does not parse.
Index ¶
Constants ¶
const ( ProtocolHopByHop uint8 = 0 ProtocolTcp uint8 = 6 ProtocolUdp uint8 = 17 ProtocolIpv6Routing uint8 = 43 ProtocolIpv6Fragment uint8 = 44 ProtocolEsp uint8 = 50 ProtocolAh uint8 = 51 ProtocolIpv6NoNextHeader uint8 = 59 ProtocolIpv6DestinationOptions uint8 = 60 ProtocolMobility uint8 = 135 ProtocolHip uint8 = 139 ProtocolShim6 uint8 = 140 ProtocolExperimental1 uint8 = 253 ProtocolExperimental2 uint8 = 254 )
IP protocol numbers, as an IPv4 header's protocol field and an IPv6 next header field carry them.
Variables ¶
var ( // ErrMalformed is reported for a packet whose headers do not parse: too short for what its // header says, or with a field no valid header carries. ErrMalformed = errors.New("malformed packet") // ErrUnsupported is reported for a packet that is well formed but that this package does not // handle, such as an IPv6 jumbogram. ErrUnsupported = errors.New("unsupported packet") // ErrInvalidArgument is reported when an encoder is given something it cannot encode. ErrInvalidArgument = errors.New("invalid argument") )
Functions ¶
func AppendIp ¶
AppendIp appends ip encoded as a header followed by its payload to b. An IPv4 header is written without options and with its checksum; an IPv6 header is written without extension headers, so Protocol is its next header and any extension headers wanted belong at the start of Payload. Source and Destination must both be of the version given.
Types ¶
type Ip ¶
type Ip struct {
// Version is 4 or 6.
Version int
Source netip.Addr
Destination netip.Addr
// Protocol is the protocol of Payload: for IPv6, the next header that follows the extension
// headers, which decoding skips. For a fragment other than the first, Payload is a piece of the
// original datagram's data and Protocol the first header that datagram carried, as the
// fragment header names it; nothing behind the fragment header is parsed.
Protocol uint8
// HopLimit is the IPv4 TTL or the IPv6 hop limit. Zero encodes as 64.
HopLimit uint8
// FragmentOffset is the offset of Payload within the original datagram, in bytes. A packet that
// is not a fragment, and the first fragment of one that is, have zero.
FragmentOffset int
// MoreFragments is set on every fragment but the last.
MoreFragments bool
// Payload is what follows the headers, trimmed to the length the header gives, so that link
// padding never reaches it. It aliases the decoded data.
Payload []byte
}
Ip is an IPv4 or IPv6 header and what it carries.
func ParseIp ¶
ParseIp decodes an IPv4 or IPv6 packet, choosing the version by the first nibble of data.
func (*Ip) Fragmented ¶
Fragmented reports whether the packet is part of a fragmented datagram.
type Tcp ¶
type Tcp struct {
SourcePort uint16
DestinationPort uint16
Sequence uint32
Acknowledgement uint32
Flags TcpFlags
Window uint16
UrgentPointer uint16
// Options are the raw option bytes. Encoding pads them with zeros (end of option list) to a
// multiple of four.
Options []byte
// Payload is the data the segment carries. Decoded options and payload alias the decoded data.
Payload []byte
}
Tcp is a TCP segment.