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

Parse a local file and get structure in memory:

UdpDatagram data = UdpDatagram.fromFile("path/to/local/file.UDP (User Datagram Protocol) datagram");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
UdpDatagram data = new UdpDatagram(new KaitaiStream(someArray));

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

data.srcPort() // => get src port

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

UdpDatagram.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;


/**
 * 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.
 */
public class UdpDatagram extends KaitaiStruct {
    public static UdpDatagram fromFile(String fileName) throws IOException {
        return new UdpDatagram(new ByteBufferKaitaiStream(fileName));
    }

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

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

    public UdpDatagram(KaitaiStream _io, KaitaiStruct _parent, UdpDatagram _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.srcPort = this._io.readU2be();
        this.dstPort = this._io.readU2be();
        this.length = this._io.readU2be();
        this.checksum = this._io.readU2be();
        this.body = this._io.readBytesFull();
    }
    private int srcPort;
    private int dstPort;
    private int length;
    private int checksum;
    private byte[] body;
    private UdpDatagram _root;
    private KaitaiStruct _parent;
    public int srcPort() { return srcPort; }
    public int dstPort() { return dstPort; }
    public int length() { return length; }
    public int checksum() { return checksum; }
    public byte[] body() { return body; }
    public UdpDatagram _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}