TCP (Transmission Control Protocol) segment: Java parsing library

TCP is one of the core Internet protocols on transport layer (AKA OSI layer 4), providing stateful connections with error checking, guarantees of delivery, order of segments and avoidance of duplicate delivery.

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of TCP (Transmission Control Protocol) segment 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:

TcpSegment data = TcpSegment.fromFile("path/to/local/file.TCP (Transmission Control Protocol) segment");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
TcpSegment data = new TcpSegment(new ByteBufferKaitaiStream(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 TCP (Transmission Control Protocol) segment

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


/**
 * TCP is one of the core Internet protocols on transport layer (AKA
 * OSI layer 4), providing stateful connections with error checking,
 * guarantees of delivery, order of segments and avoidance of duplicate
 * delivery.
 */
public class TcpSegment extends KaitaiStruct {
    public static TcpSegment fromFile(String fileName) throws IOException {
        return new TcpSegment(new ByteBufferKaitaiStream(fileName));
    }

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

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

    public TcpSegment(KaitaiStream _io, KaitaiStruct _parent, TcpSegment _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.seqNum = this._io.readU4be();
        this.ackNum = this._io.readU4be();
        this.b12 = this._io.readU1();
        this.b13 = this._io.readU1();
        this.windowSize = this._io.readU2be();
        this.checksum = this._io.readU2be();
        this.urgentPointer = this._io.readU2be();
        this.body = this._io.readBytesFull();
    }
    private int srcPort;
    private int dstPort;
    private long seqNum;
    private long ackNum;
    private int b12;
    private int b13;
    private int windowSize;
    private int checksum;
    private int urgentPointer;
    private byte[] body;
    private TcpSegment _root;
    private KaitaiStruct _parent;
    public int srcPort() { return srcPort; }
    public int dstPort() { return dstPort; }
    public long seqNum() { return seqNum; }
    public long ackNum() { return ackNum; }
    public int b12() { return b12; }
    public int b13() { return b13; }
    public int windowSize() { return windowSize; }
    public int checksum() { return checksum; }
    public int urgentPointer() { return urgentPointer; }
    public byte[] body() { return body; }
    public TcpSegment _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}