IPv6 network packet: JavaScript parsing library

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.8

This page hosts a formal specification of IPv6 network packet using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All JavaScript code generated by Kaitai Struct depends on the Kaitai Struct runtime library for JavaScript. You must add this dependency to your project before you can parse or serialize any 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 Ipv6Packet(new KaitaiStream(arrayBuffer));

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

data.version // => get version

JavaScript source code to parse IPv6 network packet

Ipv6Packet.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(['exports', 'kaitai-struct/KaitaiStream', './ProtocolBody'], factory);
  } else if (typeof exports === 'object' && exports !== null && typeof exports.nodeType !== 'number') {
    factory(exports, require('kaitai-struct/KaitaiStream'), require('./ProtocolBody'));
  } else {
    factory(root.Ipv6Packet || (root.Ipv6Packet = {}), root.KaitaiStream, root.ProtocolBody || (root.ProtocolBody = {}));
  }
})(typeof self !== 'undefined' ? self : this, function (Ipv6Packet_, KaitaiStream, ProtocolBody_) {
var Ipv6Packet = (function() {
  function Ipv6Packet(_io, _parent, _root) {
    this._io = _io;
    this._parent = _parent;
    this._root = _root || this;

    this._read();
  }
  Ipv6Packet.prototype._read = function() {
    this.version = this._io.readBitsIntBe(4);
    this.trafficClass = this._io.readBitsIntBe(8);
    this.flowLabel = this._io.readBitsIntBe(20);
    this._io.alignToByte();
    this.payloadLength = this._io.readU2be();
    this.nextHeaderType = this._io.readU1();
    this.hopLimit = this._io.readU1();
    this.srcIpv6Addr = this._io.readBytes(16);
    this.dstIpv6Addr = this._io.readBytes(16);
    this.nextHeader = new ProtocolBody_.ProtocolBody(this._io, null, null, this.nextHeaderType);
    this.rest = this._io.readBytesFull();
  }

  return Ipv6Packet;
})();
Ipv6Packet_.Ipv6Packet = Ipv6Packet;
});