IPv6 network packet: Java 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 parsing code for Java generated by Kaitai Struct depends on the Java runtime library. You have to install it before you can parse data.

The Java runtime library is published in the Maven Central Repository. Refer to the artifact page for instructions how to add it into your project with the build tool that you use.

Code

Parse a local file and get structure in memory:

Ipv6Packet data = Ipv6Packet.fromFile("path/to/local/file.bin");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
Ipv6Packet data = new Ipv6Packet(new ByteBufferKaitaiStream(someArray));

After that, one can get various attributes from the structure by invoking getter methods like:

data.version() // => get version

Java source code to parse IPv6 network packet

Ipv6Packet.java

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

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;

public class Ipv6Packet extends KaitaiStruct {
    public static Ipv6Packet fromFile(String fileName) throws IOException {
        return new Ipv6Packet(new ByteBufferKaitaiStream(fileName));
    }

    public Ipv6Packet(KaitaiStream _io) {
        this(_io, null, null);
    }

    public Ipv6Packet(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public Ipv6Packet(KaitaiStream _io, KaitaiStruct _parent, Ipv6Packet _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        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(this._io, nextHeaderType());
        this.rest = this._io.readBytesFull();
    }
    private long version;
    private long trafficClass;
    private long flowLabel;
    private int payloadLength;
    private int nextHeaderType;
    private int hopLimit;
    private byte[] srcIpv6Addr;
    private byte[] dstIpv6Addr;
    private ProtocolBody nextHeader;
    private byte[] rest;
    private Ipv6Packet _root;
    private KaitaiStruct _parent;
    public long version() { return version; }
    public long trafficClass() { return trafficClass; }
    public long flowLabel() { return flowLabel; }
    public int payloadLength() { return payloadLength; }
    public int nextHeaderType() { return nextHeaderType; }
    public int hopLimit() { return hopLimit; }
    public byte[] srcIpv6Addr() { return srcIpv6Addr; }
    public byte[] dstIpv6Addr() { return dstIpv6Addr; }
    public ProtocolBody nextHeader() { return nextHeader; }
    public byte[] rest() { return rest; }
    public Ipv6Packet _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}