TCP (Transmission Control Protocol) segment: C++98/STL 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
Minimal Kaitai Struct required: 0.1

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

Runtime library

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

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    tcp_segment_t data(&ks);
    

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

data.src_port() // => Source port

C++98/STL source code to parse TCP (Transmission Control Protocol) segment

tcp_segment.h

#ifndef TCP_SEGMENT_H_
#define TCP_SEGMENT_H_

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

#include "kaitai/kaitaistruct.h"
#include <stdint.h>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * 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.
 */

class tcp_segment_t : public kaitai::kstruct {

public:
    class flags_t;

    tcp_segment_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, tcp_segment_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~tcp_segment_t();

    /**
     * TCP header flags as defined "TCP Header Flags" registry.
     */

    class flags_t : public kaitai::kstruct {

    public:

        flags_t(kaitai::kstream* p__io, tcp_segment_t* p__parent = 0, tcp_segment_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~flags_t();

    private:
        bool m_cwr;
        bool m_ece;
        bool m_urg;
        bool m_ack;
        bool m_psh;
        bool m_rst;
        bool m_syn;
        bool m_fin;
        tcp_segment_t* m__root;
        tcp_segment_t* m__parent;

    public:

        /**
         * Congestion Window Reduced
         */
        bool cwr() const { return m_cwr; }

        /**
         * ECN-Echo
         */
        bool ece() const { return m_ece; }

        /**
         * Urgent pointer field is significant
         */
        bool urg() const { return m_urg; }

        /**
         * Acknowledgment field is significant
         */
        bool ack() const { return m_ack; }

        /**
         * Push function
         */
        bool psh() const { return m_psh; }

        /**
         * Reset the connection
         */
        bool rst() const { return m_rst; }

        /**
         * Synchronize sequence numbers
         */
        bool syn() const { return m_syn; }

        /**
         * No more data from sender
         */
        bool fin() const { return m_fin; }
        tcp_segment_t* _root() const { return m__root; }
        tcp_segment_t* _parent() const { return m__parent; }
    };

private:
    uint16_t m_src_port;
    uint16_t m_dst_port;
    uint32_t m_seq_num;
    uint32_t m_ack_num;
    uint64_t m_data_offset;
    uint64_t m_reserved;
    flags_t* m_flags;
    uint16_t m_window_size;
    uint16_t m_checksum;
    uint16_t m_urgent_pointer;
    std::string m_options;
    bool n_options;

public:
    bool _is_null_options() { options(); return n_options; };

private:
    std::string m_body;
    tcp_segment_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * Source port
     */
    uint16_t src_port() const { return m_src_port; }

    /**
     * Destination port
     */
    uint16_t dst_port() const { return m_dst_port; }

    /**
     * Sequence number
     */
    uint32_t seq_num() const { return m_seq_num; }

    /**
     * Acknowledgment number
     */
    uint32_t ack_num() const { return m_ack_num; }

    /**
     * Data offset (in 32-bit words from the beginning of this type, normally 32 or can be extended if there are any TCP options or padding is present)
     */
    uint64_t data_offset() const { return m_data_offset; }
    uint64_t reserved() const { return m_reserved; }
    flags_t* flags() const { return m_flags; }
    uint16_t window_size() const { return m_window_size; }
    uint16_t checksum() const { return m_checksum; }
    uint16_t urgent_pointer() const { return m_urgent_pointer; }
    std::string options() const { return m_options; }
    std::string body() const { return m_body; }
    tcp_segment_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // TCP_SEGMENT_H_

tcp_segment.cpp

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

#include "tcp_segment.h"

tcp_segment_t::tcp_segment_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, tcp_segment_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_flags = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void tcp_segment_t::_read() {
    m_src_port = m__io->read_u2be();
    m_dst_port = m__io->read_u2be();
    m_seq_num = m__io->read_u4be();
    m_ack_num = m__io->read_u4be();
    m_data_offset = m__io->read_bits_int_be(4);
    m_reserved = m__io->read_bits_int_be(4);
    m__io->align_to_byte();
    m_flags = new flags_t(m__io, this, m__root);
    m_window_size = m__io->read_u2be();
    m_checksum = m__io->read_u2be();
    m_urgent_pointer = m__io->read_u2be();
    n_options = true;
    if (((data_offset() * 4) - 20) != 0) {
        n_options = false;
        m_options = m__io->read_bytes(((data_offset() * 4) - 20));
    }
    m_body = m__io->read_bytes_full();
}

tcp_segment_t::~tcp_segment_t() {
    _clean_up();
}

void tcp_segment_t::_clean_up() {
    if (m_flags) {
        delete m_flags; m_flags = 0;
    }
    if (!n_options) {
    }
}

tcp_segment_t::flags_t::flags_t(kaitai::kstream* p__io, tcp_segment_t* p__parent, tcp_segment_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void tcp_segment_t::flags_t::_read() {
    m_cwr = m__io->read_bits_int_be(1);
    m_ece = m__io->read_bits_int_be(1);
    m_urg = m__io->read_bits_int_be(1);
    m_ack = m__io->read_bits_int_be(1);
    m_psh = m__io->read_bits_int_be(1);
    m_rst = m__io->read_bits_int_be(1);
    m_syn = m__io->read_bits_int_be(1);
    m_fin = m__io->read_bits_int_be(1);
}

tcp_segment_t::flags_t::~flags_t() {
    _clean_up();
}

void tcp_segment_t::flags_t::_clean_up() {
}