coap

package
v0.331.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package coap decodes Constrained Application Protocol (RFC 7252) packets — the application-layer protocol used by constrained IoT devices (6LoWPAN, Thread, OpenThread, Zigbee IP). Pure offline parser; no transport, no hardware.

Wrap-vs-native judgement: CoAP is a fully public IETF specification (RFC 7252). The walker is bit-level decoding over a 4-byte fixed header + variable-length token + option list + optional payload. Wrapping a FAP for this would require an SD-card install + a firmware-fork dependency for a pure parser. Native delivers offline analysis — operators paste a captured CoAP packet from Wireshark / any UDP sniffer and inspect every field without re-running the capture.

Pairs with the existing IoT decoders:

  • mqtt_packet_decode for MQTT (the IP-side broker protocol)
  • zigbee_zcl_decode for the Zigbee application layer
  • ieee802154_decode + zigbee_nwk_decode + zigbee_aps_decode for the underlying mesh stack

Together they cover the IoT application-layer + mesh-network surface.

What this package covers:

  • Fixed header decode: 2-bit version + 2-bit type (Confirmable / Non-Confirmable / Acknowledgement / Reset)
  • 4-bit token length + 8-bit code + 16-bit message ID
  • Code decode: request methods (GET / POST / PUT / DELETE / FETCH / PATCH / iPATCH) + response codes (2.01 Created / 2.04 Changed / 2.05 Content / 4.04 Not Found / 5.00 Internal Server Error / etc.) with documented names
  • Token extraction (0-8 bytes)
  • Option list walking with delta + length nibble encoding (delta extension byte 13 = +1 byte extension, 14 = +2 byte extension; same encoding for length)
  • Per-option-number name lookup for the documented options (Uri-Host, Uri-Port, Uri-Path, Uri-Query, Content-Format, Accept, Max-Age, ETag, If-Match, If-None-Match, Location- Path, Location-Query, Observe, Block1/2, Size1/2, Proxy- Uri, Proxy-Scheme)
  • Payload extraction (after the 0xFF marker)

What this package does NOT cover (deliberately out of scope):

  • CoAP over DTLS unwrap (operators bring the decrypted CoAP payload post-DTLS)
  • Option-value type interpretation past the raw bytes (e.g. Block1/2 size+M+NUM extraction would be a follow-on)
  • Observe / OSCORE security extensions
  • CoAP over TCP (RFC 8323)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	Raw int `json:"raw"`
	// Version (bits 7..6 of byte 0) — should be 1.
	Version int `json:"version"`
	// Type (bits 5..4 of byte 0).
	Type     int    `json:"type"`
	TypeName string `json:"type_name"`
	// TokenLength (bits 3..0 of byte 0) — 0..8 bytes.
	TokenLength int `json:"token_length"`
	// Code (byte 1) — request method or response code packed as
	// (class << 5) | (detail). 0.01..0.04 = requests; 2.xx /
	// 4.xx / 5.xx = responses.
	Code     int    `json:"code"`
	CodeText string `json:"code_text"`
	CodeName string `json:"code_name"`
	// MessageID (bytes 2..3, big-endian).
	MessageID int `json:"message_id"`
}

Header is the decoded 4-byte CoAP fixed header.

type Option

type Option struct {
	// Number is the absolute option number (computed by summing
	// the delta nibbles across the option list).
	Number int `json:"number"`
	// Name is the canonical option name when in our catalog,
	// "" otherwise.
	Name string `json:"name,omitempty"`
	// Length is the option value length in bytes.
	Length int `json:"length"`
	// ValueHex is the operator-facing hex rendering of the
	// value bytes. Always populated.
	ValueHex string `json:"value_hex,omitempty"`
	// ValueString is the UTF-8 string interpretation when the
	// option is a documented string type (Uri-Host, Uri-Path,
	// Uri-Query, Location-Path, etc.).
	ValueString string `json:"value_string,omitempty"`
	// ValueUint is the unsigned-integer interpretation when the
	// option is a documented uint type (Uri-Port, Content-Format,
	// Accept, Max-Age, etc.). nil for non-uint options.
	ValueUint *uint64 `json:"value_uint,omitempty"`
}

Option is one decoded CoAP option entry.

type Packet

type Packet struct {
	Header Header `json:"header"`
	// TokenHex is the 0-8 byte token, hex-rendered.
	TokenHex string `json:"token_hex,omitempty"`
	// Options is the ordered list of decoded options.
	Options []Option `json:"options,omitempty"`
	// PayloadHex is the payload after the 0xFF marker.
	PayloadHex string `json:"payload_hex,omitempty"`
	// PayloadString is the UTF-8 string interpretation when
	// printable.
	PayloadString string `json:"payload_string,omitempty"`
}

Packet is the top-level decoded CoAP packet.

func Decode

func Decode(hexBlob string) (Packet, error)

Decode parses a hex-encoded CoAP packet. Tolerates ':' / '-' / '_' / whitespace separators.

func DecodeBytes

func DecodeBytes(b []byte) (Packet, error)

DecodeBytes is the byte-slice variant of Decode.

type Type

type Type int

Type is the 2-bit message type field at bits 5..4 of byte 0.

const (
	// TypeConfirmable — server must ACK; client retransmits.
	TypeConfirmable Type = 0
	// TypeNonConfirmable — fire-and-forget; no ACK.
	TypeNonConfirmable Type = 1
	// TypeAcknowledgement — response to a Confirmable request.
	TypeAcknowledgement Type = 2
	// TypeReset — message could not be processed; reset peer.
	TypeReset Type = 3
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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