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

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++11/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.tcp_segment", 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() // => get src port

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

tcp_segment.h

#pragma once

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

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

#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:

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

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

public:
    ~tcp_segment_t();

private:
    uint16_t m_src_port;
    uint16_t m_dst_port;
    uint32_t m_seq_num;
    uint32_t m_ack_num;
    uint8_t m_b12;
    uint8_t m_b13;
    uint16_t m_window_size;
    uint16_t m_checksum;
    uint16_t m_urgent_pointer;
    std::string m_body;
    tcp_segment_t* m__root;
    kaitai::kstruct* m__parent;

public:
    uint16_t src_port() const { return m_src_port; }
    uint16_t dst_port() const { return m_dst_port; }
    uint32_t seq_num() const { return m_seq_num; }
    uint32_t ack_num() const { return m_ack_num; }
    uint8_t b12() const { return m_b12; }
    uint8_t b13() const { return m_b13; }
    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 body() const { return m_body; }
    tcp_segment_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

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;
    _read();
}

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_b12 = m__io->read_u1();
    m_b13 = m__io->read_u1();
    m_window_size = m__io->read_u2be();
    m_checksum = m__io->read_u2be();
    m_urgent_pointer = m__io->read_u2be();
    m_body = m__io->read_bytes_full();
}

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

void tcp_segment_t::_clean_up() {
}