TCP (Transmission Control Protocol) segment: C# 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:

var data = TcpSegment.FromFile("path/to/local/file.tcp_segment");

Or parse structure from a byte array:

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

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

data.SrcPort // => get src port

C# source code to parse TCP (Transmission Control Protocol) segment

TcpSegment.cs

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



namespace Kaitai
{

    /// <summary>
    /// 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.
    /// </summary>
    public partial class TcpSegment : KaitaiStruct
    {
        public static TcpSegment FromFile(string fileName)
        {
            return new TcpSegment(new KaitaiStream(fileName));
        }

        public TcpSegment(KaitaiStream p__io, KaitaiStruct p__parent = null, TcpSegment p__root = null) : base(p__io)
        {
            m_parent = p__parent;
            m_root = p__root ?? this;
            _read();
        }
        private void _read()
        {
            _srcPort = m_io.ReadU2be();
            _dstPort = m_io.ReadU2be();
            _seqNum = m_io.ReadU4be();
            _ackNum = m_io.ReadU4be();
            _b12 = m_io.ReadU1();
            _b13 = m_io.ReadU1();
            _windowSize = m_io.ReadU2be();
            _checksum = m_io.ReadU2be();
            _urgentPointer = m_io.ReadU2be();
            _body = m_io.ReadBytesFull();
        }
        private ushort _srcPort;
        private ushort _dstPort;
        private uint _seqNum;
        private uint _ackNum;
        private byte _b12;
        private byte _b13;
        private ushort _windowSize;
        private ushort _checksum;
        private ushort _urgentPointer;
        private byte[] _body;
        private TcpSegment m_root;
        private KaitaiStruct m_parent;
        public ushort SrcPort { get { return _srcPort; } }
        public ushort DstPort { get { return _dstPort; } }
        public uint SeqNum { get { return _seqNum; } }
        public uint AckNum { get { return _ackNum; } }
        public byte B12 { get { return _b12; } }
        public byte B13 { get { return _b13; } }
        public ushort WindowSize { get { return _windowSize; } }
        public ushort Checksum { get { return _checksum; } }
        public ushort UrgentPointer { get { return _urgentPointer; } }
        public byte[] Body { get { return _body; } }
        public TcpSegment M_Root { get { return m_root; } }
        public KaitaiStruct M_Parent { get { return m_parent; } }
    }
}