IPv6 network packet: C++98/STL parsing library

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.8

This page hosts a formal specification of IPv6 network packet using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.


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.


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:
    ipv6_packet_t data(&ks);

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

data.version() // => get version

C++98/STL source code to parse IPv6 network packet


#ifndef IPV6_PACKET_H_
#define IPV6_PACKET_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>
#include "protocol_body.h"

#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
class protocol_body_t;

class ipv6_packet_t : public kaitai::kstruct {


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

    void _read();
    void _clean_up();


    uint64_t m_version;
    uint64_t m_traffic_class;
    uint64_t m_flow_label;
    uint16_t m_payload_length;
    uint8_t m_next_header_type;
    uint8_t m_hop_limit;
    std::string m_src_ipv6_addr;
    std::string m_dst_ipv6_addr;
    protocol_body_t* m_next_header;
    std::string m_rest;
    ipv6_packet_t* m__root;
    kaitai::kstruct* m__parent;

    uint64_t version() const { return m_version; }
    uint64_t traffic_class() const { return m_traffic_class; }
    uint64_t flow_label() const { return m_flow_label; }
    uint16_t payload_length() const { return m_payload_length; }
    uint8_t next_header_type() const { return m_next_header_type; }
    uint8_t hop_limit() const { return m_hop_limit; }
    std::string src_ipv6_addr() const { return m_src_ipv6_addr; }
    std::string dst_ipv6_addr() const { return m_dst_ipv6_addr; }
    protocol_body_t* next_header() const { return m_next_header; }
    std::string rest() const { return m_rest; }
    ipv6_packet_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }

#endif  // IPV6_PACKET_H_


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

#include "ipv6_packet.h"

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

    try {
    } catch(...) {

void ipv6_packet_t::_read() {
    m_version = m__io->read_bits_int_be(4);
    m_traffic_class = m__io->read_bits_int_be(8);
    m_flow_label = m__io->read_bits_int_be(20);
    m_payload_length = m__io->read_u2be();
    m_next_header_type = m__io->read_u1();
    m_hop_limit = m__io->read_u1();
    m_src_ipv6_addr = m__io->read_bytes(16);
    m_dst_ipv6_addr = m__io->read_bytes(16);
    m_next_header = new protocol_body_t(next_header_type(), m__io);
    m_rest = m__io->read_bytes_full();

ipv6_packet_t::~ipv6_packet_t() {

void ipv6_packet_t::_clean_up() {
    if (m_next_header) {
        delete m_next_header; m_next_header = 0;