UDP is a simple stateless transport layer (AKA OSI layer 4) protocol, one of the core Internet protocols. It provides source and destination ports, basic checksumming, but provides not guarantees of delivery, order of packets, or duplicate delivery.
This page hosts a formal specification of UDP (User Datagram Protocol) datagram 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);
udp_datagram_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.src_port() // => get src port
#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
/**
* UDP is a simple stateless transport layer (AKA OSI layer 4)
* protocol, one of the core Internet protocols. It provides source and
* destination ports, basic checksumming, but provides not guarantees
* of delivery, order of packets, or duplicate delivery.
*/
class udp_datagram_t : public kaitai::kstruct {
public:
udp_datagram_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, udp_datagram_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~udp_datagram_t();
private:
uint16_t m_src_port;
uint16_t m_dst_port;
uint16_t m_length;
uint16_t m_checksum;
std::string m_body;
udp_datagram_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; }
uint16_t length() const { return m_length; }
uint16_t checksum() const { return m_checksum; }
std::string body() const { return m_body; }
udp_datagram_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 "udp_datagram.h"
udp_datagram_t::udp_datagram_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, udp_datagram_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = this;
_read();
}
void udp_datagram_t::_read() {
m_src_port = m__io->read_u2be();
m_dst_port = m__io->read_u2be();
m_length = m__io->read_u2be();
m_checksum = m__io->read_u2be();
m_body = m__io->read_bytes((length() - 8));
}
udp_datagram_t::~udp_datagram_t() {
_clean_up();
}
void udp_datagram_t::_clean_up() {
}