IPv6 network packet: C++/STL parsing library

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.7

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.

Usage

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 a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.ipv6_packet", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #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++/STL source code to parse IPv6 network packet

ipv6_packet.h

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

#if KAITAI_STRUCT_VERSION < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 or later is required"
#endif
class udp_datagram_t;
class tcp_segment_t;
class ipv4_packet_t;

class ipv6_packet_t : public kaitai::kstruct {

public:
    class no_next_header_t;
    class option_hop_by_hop_t;

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

private:
    void _read();

public:
    ~ipv6_packet_t();

    class no_next_header_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~no_next_header_t();

    private:
        ipv6_packet_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        ipv6_packet_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    class option_hop_by_hop_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~option_hop_by_hop_t();

    private:
        uint8_t m_next_header_type;
        uint8_t m_hdr_ext_len;
        std::string m_body;
        kaitai::kstruct* m_next_header;
        bool n_next_header;

    public:
        bool _is_null_next_header() { next_header(); return n_next_header; };

    private:
        ipv6_packet_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        uint8_t next_header_type() const { return m_next_header_type; }
        uint8_t hdr_ext_len() const { return m_hdr_ext_len; }
        std::string body() const { return m_body; }
        kaitai::kstruct* next_header() const { return m_next_header; }
        ipv6_packet_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

private:
    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;
    kaitai::kstruct* m_next_header;
    bool n_next_header;

public:
    bool _is_null_next_header() { next_header(); return n_next_header; };

private:
    std::string m_rest;
    ipv6_packet_t* m__root;
    kaitai::kstruct* m__parent;

public:
    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; }
    kaitai::kstruct* 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_

ipv6_packet.cpp

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

#include "ipv6_packet.h"


#include "udp_datagram.h"
#include "tcp_segment.h"
#include "ipv4_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;
    _read();
}

void ipv6_packet_t::_read() {
    m_version = m__io->read_bits_int(4);
    m_traffic_class = m__io->read_bits_int(8);
    m_flow_label = m__io->read_bits_int(20);
    m__io->align_to_byte();
    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);
    n_next_header = true;
    switch (next_header_type()) {
    case 17: {
        n_next_header = false;
        m_next_header = new udp_datagram_t(m__io);
        break;
    }
    case 0: {
        n_next_header = false;
        m_next_header = new option_hop_by_hop_t(m__io, this, m__root);
        break;
    }
    case 4: {
        n_next_header = false;
        m_next_header = new ipv4_packet_t(m__io);
        break;
    }
    case 6: {
        n_next_header = false;
        m_next_header = new tcp_segment_t(m__io);
        break;
    }
    case 59: {
        n_next_header = false;
        m_next_header = new no_next_header_t(m__io, this, m__root);
        break;
    }
    }
    m_rest = m__io->read_bytes_full();
}

ipv6_packet_t::~ipv6_packet_t() {
    if (!n_next_header) {
        delete m_next_header;
    }
}

ipv6_packet_t::no_next_header_t::no_next_header_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, ipv6_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void ipv6_packet_t::no_next_header_t::_read() {
}

ipv6_packet_t::no_next_header_t::~no_next_header_t() {
}

ipv6_packet_t::option_hop_by_hop_t::option_hop_by_hop_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, ipv6_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void ipv6_packet_t::option_hop_by_hop_t::_read() {
    m_next_header_type = m__io->read_u1();
    m_hdr_ext_len = m__io->read_u1();
    m_body = m__io->read_bytes((hdr_ext_len() - 1));
    n_next_header = true;
    switch (next_header_type()) {
    case 0: {
        n_next_header = false;
        m_next_header = new option_hop_by_hop_t(m__io, this, m__root);
        break;
    }
    case 6: {
        n_next_header = false;
        m_next_header = new tcp_segment_t(m__io);
        break;
    }
    case 59: {
        n_next_header = false;
        m_next_header = new no_next_header_t(m__io, this, m__root);
        break;
    }
    }
}

ipv6_packet_t::option_hop_by_hop_t::~option_hop_by_hop_t() {
    if (!n_next_header) {
        delete m_next_header;
    }
}