AntHocNet 2.0.0
Paper-faithful ant-colony ad hoc routing: the shared core and its adapters
Loading...
Searching...
No Matches
ant_message_codec.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (C) 2026 Daniel Henrique Joppi
3
4/**
5 * Canonical, simulator-agnostic wire format for an AntMessage.
6 *
7 * Both adapters reuse this so an NS-2 and an NS-3 node would interpret the
8 * same bytes identically (and so the round-trip is unit-testable without a
9 * simulator). Encoding is little-endian, length-prefixed for the variable
10 * arrays. The NS-2 PacketHeaderClass and the NS-3 ns3::Header both delegate
11 * their (de)serialization to these functions.
12 */
13#ifndef ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
14#define ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
15
16#include <cstddef>
17#include <cstdint>
18#include <vector>
19
21
22namespace anthocnet {
23namespace core {
24namespace codec {
25
26/// On-wire format version, written at offset 0 of every frame and checked
27/// before any other field (ADR-0006). Bump on any layout/semantic change.
28/// Both adapter headers must serialize this same value.
29/// 0x01: version byte + bounds/enum enforcement (item 12).
30/// 0x02: + AntMessage::broadcastBudget field, AntType::LinkFail (item 05).
31/// 0x03: backward-ant slim — prevHop/hops/pathTime/pheromone off the wire,
32/// reconstructed locally from `history` (item 02, ADR-0009).
33constexpr std::uint8_t kWireVersion = 0x03;
34
35/// Protocol bounds enforced on *decode* of untrusted input, mirroring the
36/// Config defaults so the codec stays standalone (golden rule #5). A frame
37/// declaring more elements than these is rejected, not just buffer-checked.
38constexpr std::uint16_t kMaxVisitedOnWire = 100; ///< == Config::maxPathLength
39constexpr std::uint16_t kMaxHistoryOnWire = 100; ///< retraced path ≤ path length
40constexpr std::uint16_t kMaxHelloOnWire = 64; ///< generous bound on adverts
41
42/// Number of bytes serialize() will produce for `msg`.
43std::size_t serializedSize(const AntMessage& msg);
44
45/// Encode `msg` into a fresh byte buffer.
46std::vector<std::uint8_t> serialize(const AntMessage& msg);
47
48/// Encode `msg` into `out` (resized to serializedSize(msg)).
49void serialize(const AntMessage& msg, std::vector<std::uint8_t>& out);
50
51/// Decode `bytes` into `msg`. Returns false if the buffer is truncated or
52/// internally inconsistent; on failure `msg` is left in an unspecified state.
53bool deserialize(const std::uint8_t* bytes, std::size_t len, AntMessage& msg);
54
55inline bool deserialize(const std::vector<std::uint8_t>& bytes, AntMessage& msg) {
56 return deserialize(bytes.data(), bytes.size(), msg);
57}
58
59} // namespace codec
60} // namespace core
61} // namespace anthocnet
62
63#endif // ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
std::size_t serializedSize(const AntMessage &msg)
Number of bytes serialize() will produce for msg.
std::vector< std::uint8_t > serialize(const AntMessage &msg)
Encode msg into a fresh byte buffer.
bool deserialize(const std::uint8_t *bytes, std::size_t len, AntMessage &msg)
Decode bytes into msg.
constexpr std::uint8_t kWireVersion
On-wire format version, written at offset 0 of every frame and checked before any other field (ADR-00...
constexpr std::uint16_t kMaxHelloOnWire
generous bound on adverts
constexpr std::uint16_t kMaxHistoryOnWire
retraced path ≤ path length
constexpr std::uint16_t kMaxVisitedOnWire
Protocol bounds enforced on decode of untrusted input, mirroring the Config defaults so the codec sta...
AntHistoryTracker: (src, seqNum) duplicate detection.
Definition ant_history.h:24
Complete, copyable description of an ant packet.
Definition ant_message.h:48