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.
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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
ipv6_packet_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.version() // => get version
#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>
#include "protocol_body.h"
#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 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 = nullptr, ipv6_packet_t* p__root = nullptr);
private:
void _read();
void _clean_up();
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;
std::unique_ptr<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.get(); }
std::string rest() const { return m_rest; }
ipv6_packet_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
};
// 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 = nullptr;
_read();
}
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__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 = std::unique_ptr<protocol_body_t>(new protocol_body_t(next_header_type(), m__io));
m_rest = m__io->read_bytes_full();
}
ipv6_packet_t::~ipv6_packet_t() {
_clean_up();
}
void ipv6_packet_t::_clean_up() {
}