UDP (User Datagram Protocol) datagram: Go parsing library

UDP is a simple stateless transport layer (AKA OSI layer 4) protocol, one of the core Internet protocols. It provides source and destination ports, basic checksumming, but provides not guarantees of delivery, order of packets, or duplicate delivery.

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of UDP (User Datagram Protocol) datagram using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Go source code to parse UDP (User Datagram Protocol) datagram

udp_datagram.go

// Code generated by kaitai-struct-compiler from a .ksy source file. DO NOT EDIT.

import "github.com/kaitai-io/kaitai_struct_go_runtime/kaitai"


/**
 * UDP is a simple stateless transport layer (AKA OSI layer 4)
 * protocol, one of the core Internet protocols. It provides source and
 * destination ports, basic checksumming, but provides not guarantees
 * of delivery, order of packets, or duplicate delivery.
 */
type UdpDatagram struct {
	SrcPort uint16
	DstPort uint16
	Length uint16
	Checksum uint16
	Body []byte
	_io *kaitai.Stream
	_root *UdpDatagram
	_parent interface{}
}
func NewUdpDatagram() *UdpDatagram {
	return &UdpDatagram{
	}
}

func (this *UdpDatagram) Read(io *kaitai.Stream, parent interface{}, root *UdpDatagram) (err error) {
	this._io = io
	this._parent = parent
	this._root = root

	tmp1, err := this._io.ReadU2be()
	if err != nil {
		return err
	}
	this.SrcPort = uint16(tmp1)
	tmp2, err := this._io.ReadU2be()
	if err != nil {
		return err
	}
	this.DstPort = uint16(tmp2)
	tmp3, err := this._io.ReadU2be()
	if err != nil {
		return err
	}
	this.Length = uint16(tmp3)
	tmp4, err := this._io.ReadU2be()
	if err != nil {
		return err
	}
	this.Checksum = uint16(tmp4)
	tmp5, err := this._io.ReadBytes(int((this.Length - 8)))
	if err != nil {
		return err
	}
	tmp5 = tmp5
	this.Body = tmp5
	return err
}