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

Runtime library

All parsing code for C# generated by Kaitai Struct depends on the C# runtime library. You have to install it before you can parse data.

The C# runtime library is available in the NuGet Gallery. Installation instructions can also be found there.

Code

Parse a local file and get structure in memory:

var data = UdpDatagram.FromFile("path/to/local/file.bin");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
var data = new UdpDatagram(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 UDP (User Datagram Protocol) datagram

UdpDatagram.cs

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



namespace Kaitai
{

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

        public UdpDatagram(KaitaiStream p__io, KaitaiStruct p__parent = null, UdpDatagram 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();
            _length = m_io.ReadU2be();
            _checksum = m_io.ReadU2be();
            _body = m_io.ReadBytes((Length - 8));
        }
        private ushort _srcPort;
        private ushort _dstPort;
        private ushort _length;
        private ushort _checksum;
        private byte[] _body;
        private UdpDatagram m_root;
        private KaitaiStruct m_parent;
        public ushort SrcPort { get { return _srcPort; } }
        public ushort DstPort { get { return _dstPort; } }
        public ushort Length { get { return _length; } }
        public ushort Checksum { get { return _checksum; } }
        public byte[] Body { get { return _body; } }
        public UdpDatagram M_Root { get { return m_root; } }
        public KaitaiStruct M_Parent { get { return m_parent; } }
    }
}