UDP (User Datagram Protocol) datagram: JavaScript 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.

Usage

Runtime library

All parsing code for JavaScript generated by Kaitai Struct depends on the JavaScript runtime library. You have to install it before you can parse data.

The JavaScript runtime library is available at npm:

npm install kaitai-struct

Code

See the usage examples in the JavaScript notes.

Parse structure from an ArrayBuffer:

var arrayBuffer = ...;
var data = new UdpDatagram(new KaitaiStream(arrayBuffer));

After that, one can get various attributes from the structure by accessing fields or properties like:

data.srcPort // => get src port

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

UdpDatagram.js

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'));
  } else {
    root.UdpDatagram = factory(root.KaitaiStream);
  }
}(typeof self !== 'undefined' ? self : this, function (KaitaiStream) {
/**
 * 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.
 */

var UdpDatagram = (function() {
  function UdpDatagram(_io, _parent, _root) {
    this._io = _io;
    this._parent = _parent;
    this._root = _root || this;

    this._read();
  }
  UdpDatagram.prototype._read = function() {
    this.srcPort = this._io.readU2be();
    this.dstPort = this._io.readU2be();
    this.length = this._io.readU2be();
    this.checksum = this._io.readU2be();
    this.body = this._io.readBytes((this.length - 8));
  }

  return UdpDatagram;
})();
return UdpDatagram;
}));