8.3 KiB
Source-Based Routing for BitChat Packets
This document specifies an optional source-based routing extension to the BitChat packet format. A sender may attach a hop-by-hop route (list of peer IDs) to instruct relays on the intended path. Relays that support this feature will try to forward to the next hop directly; otherwise, they fall back to regular broadcast relaying.
Status: optional and backward-compatible.
Layering Overview
- Outer packet: BitChat binary packet with unchanged fixed header (version/type/ttl/timestamp/flags/payloadLength).
- Flags: adds a new bit
HAS_ROUTE (0x08). This flag is only valid for packet version >= 2. - Variable sections (when present, in order):
SenderID(8 bytes)RecipientID(8 bytes) ifHAS_RECIPIENTRoute(ifHAS_ROUTEAND version >= 2):count(1 byte) +count * 8bytes hop IDsPayload(with optional compression preamble)Signature(64 bytes) ifHAS_SIGNATURE
Unknown flags are ignored by older implementations. For v1 packets, the HAS_ROUTE flag MUST be ignored even if set, and the route field MUST NOT be present. This ensures strict backward compatibility.
Detailed Packet Structure (v1 vs v2)
The Bitchat packet structure is designed to be compact and efficient for BLE transmission.
Fixed Header
The fixed header is present in all packets. Its size depends on the version.
| Field | Size (v1) | Size (v2) | Description |
|---|---|---|---|
| Version | 1 byte | 1 byte | Protocol version (0x01 or 0x02). |
| Type | 1 byte | 1 byte | Message Type (e.g., 0x01 Announce, 0x02 Message). |
| TTL | 1 byte | 1 byte | Time-To-Live (hop limit). |
| Timestamp | 8 bytes | 8 bytes | UInt64 (big-endian) creation time (ms since epoch). |
| Flags | 1 byte | 1 byte | Bitmask: HAS_RECIPIENT(0x01), HAS_SIGNATURE(0x02), IS_COMPRESSED(0x04), HAS_ROUTE(0x08). |
| Payload Length | 2 bytes | 4 bytes | UInt16 (v1) or UInt32 (v2) length of the Payload section only. Does NOT include route or other headers. |
| Total Header | 14 bytes | 16 bytes |
Variable Sections (In Order)
These fields follow the fixed header immediately.
-
Sender ID (Fixed 8 bytes)
- Present in ALL packets.
- Derived from the sender's public key.
-
Recipient ID (Optional, 8 bytes)
- Present only if
HAS_RECIPIENTflag is set. - Target peer ID for addressed messages.
- Present only if
-
Source Route (Optional, Variable Length)
- Condition: Present ONLY if
HAS_ROUTEflag is set ANDVersion >= 2. - Structure:
Count(1 byte): Number of hops (N).Hops(N * 8bytes): Sequence of 8-byte Peer IDs.
- Note: The size of this field (
1 + 8*Nbytes) is NOT included in thePayload Lengthfield in the fixed header. It exists structurally between the Recipient ID and the Payload.
- Condition: Present ONLY if
-
Payload (Variable Length)
- Size is exactly the value specified in the
Payload Lengthfield of the fixed header. - Contains the application data (e.g., encrypted message, announcement TLVs).
- If
IS_COMPRESSEDflag is set, the first 2 bytes are the original uncompressed size (UInt16), followed by the compressed bytes.
- Size is exactly the value specified in the
-
Signature (Optional, 64 bytes)
- Present only if
HAS_SIGNATUREflag is set. - Ed25519 signature covering the entire packet (with TTL=0 and Signature excluded).
- Present only if
Route Field Encoding
- Presence: Signaled by the
HAS_ROUTE (0x08)bit inflagsANDversion >= 2. - Layout (immediately after optional
RecipientID):count: 1 byte (0..255)hops: concatenation ofcountpeer IDs, each encoded as exactly 8 bytes
- Peer ID encoding (8 bytes): same as used elsewhere in BitChat (16 hex chars → 8 bytes; left-to-right conversion; pad with
0x00if shorter). This matches the on‑wiresenderID/recipientIDencoding. - Size impact:
1 + 8*Nbytes, whereN = count. - Empty route:
HAS_ROUTEwithcount = 0is treated as no route (relays ignore it).
Sender Behavior
- Applicability: Intended for addressed packets (i.e., where
recipientIDis set and is not the broadcast ID). For broadcast packets, omit the route. - Path computation: Use Dijkstra’s shortest path (unit weights) on your internal mesh topology to find a route from the sender (your peerID) to the recipient (the destination peerID). The
BitchatPacketalready contains dedicatedsenderIDandrecipientIDfields. TheRoutefield'shopslist SHOULD contain the sequence of intermediate peer IDs that the packet should traverse. It SHOULD NOT duplicate thesenderIDorrecipientIDif they are already present in theBitchatPacket's dedicated fields. Instead, thehopslist represents the explicit path between the sender and recipient, starting from the first relay and ending with the last relay before the recipient. - Encoding: Ensure the packet
versionis set to 2 or higher. SetHAS_ROUTE, writecount = path.length, then the 8‑byte hop IDs in order. Keepcount <= 255. - Signing: The route is covered by the Ed25519 signature (recommended):
- Signature input is the canonical encoding with
signatureomitted andttl = 0(TTL excluded to allow relay decrement) — same rule as base protocol.
- Signature input is the canonical encoding with
Relay Behavior
When receiving a packet that is not addressed to you:
- If
HAS_ROUTEis not set, or the route is empty, or the packetversion < 2, relay using your normal broadcast logic (subject to TTL/probability policies). - If
HAS_ROUTEis set ANDversion >= 2:- Route Sanity Check: Before processing, the relay MUST validate the route. If the route contains duplicate hops (i.e., the same peer ID appears more than once), the packet MUST be dropped to prevent loops.
- If your peer ID appears at index
iin the hop list:- If there is a next hop at
i+1, attempt a targeted unicast to that next hop if you have a direct connection to it.- If successful, do NOT broadcast this packet further.
- If not directly connected (or the send fails), fall back to broadcast relaying.
- If you are the last hop (no
i+1), the packet has reached the end of its explicit route. The relay should then attempt to deliver it to the finalrecipientIDif directly connected, but SHOULD NOT relay it further as a broadcast.
- If there is a next hop at
TTL handling remains unchanged: relays decrement TTL by 1 before forwarding (whether targeted or broadcast). If TTL reaches 0, do not relay.
Receiver Behavior (Destination)
- This extension does not change how addressed packets are handled by the final recipient. If the packet is addressed to you (
recipientID == myPeerID), process it normally (e.g., decrypt Noise payload, verify signatures, etc.). - Signature verification MUST include the route field when present; route tampering will invalidate the signature.
Compatibility
- Omission: If
HAS_ROUTEis omitted, legacy behavior applies. Relays that don’t implement this feature will ignore the route entirely. - Version Constraint: Implementations MUST NOT parse or act on the
Routefield in v1 packets, even if theHAS_ROUTEflag is set. This prevents potential parsing ambiguities with legacy clients. - Partial support: If any relay on the path cannot directly reach the next hop, it will fall back to broadcast relaying; delivery is still probabilistic like the base protocol.
Minimal Example (conceptual)
- Header (fixed 13 bytes): unchanged.
- Variable sections (ordered):
SenderID(8)RecipientID(8)(if present)HAS_ROUTEset →count=1,hops = [H1]whereH1is 8 bytes- Payload (optionally compressed)
- Signature (64)
In this example, SENDER_ID is the sender, RECIPIENT_ID is the final recipient, and H1 is the single intermediate relay. The hops list explicitly defines the path between the sender and recipient. The receiver verifies the signature over the packet encoding (with ttl = 0 and signature omitted), which includes the hops when HAS_ROUTE is set.
Operational Notes
- Routing optimality depends on the freshness and completeness of the topology your implementation has learned (e.g., via gossip of direct neighbors). Recompute routes as needed.
- Route length should be kept small to reduce overhead and the probability of missing a direct link at some hop.
- Implementations may introduce policy controls (e.g., disable source routing, cap max route length).