IPv6 network packet: C++/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.

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 local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.ipv6_packet", 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++/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 protocol_body_t;

class ipv6_packet_t : public kaitai::kstruct {

public:

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

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;
    protocol_body_t* m_next_header;
    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; }
    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_

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 "protocol_body.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);
    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() {
    delete m_next_header;
}