Rust receiver
A minimal receiver that uses only the Rust standard library. It binds your UDP port, accepts the stream and prints each shred's common header. Use it to commission delivery before you build a production receiver.
Build and run#
Save the program as receiver.rs. It takes the bind address and, optionally, a source address: once delivery is active, you can pass the one support gives you, and it then ignores datagrams from anywhere else. Leave it off while you verify your destination, because support only gives you that address once delivery is active. A datagram that is plain text, such as the verification token, is printed as a text from line instead of being parsed.
1use std::{env, io::{self, Write}, net::{Ipv4Addr, SocketAddr, UdpSocket}};23const MAX_DATAGRAM: usize = 1228;4const COMMON_HEADER: usize = 83;56fn u16le(b: &[u8]) -> u16 { u16::from_le_bytes(b.try_into().unwrap()) }7fn u32le(b: &[u8]) -> u32 { u32::from_le_bytes(b.try_into().unwrap()) }8fn u64le(b: &[u8]) -> u64 { u64::from_le_bytes(b.try_into().unwrap()) }910fn is_text(b: &[u8]) -> bool {11!b.is_empty() && b.iter().all(|&c| c == b'\t' || c == b'\n' || c == b'\r' || (0x20..0x7f).contains(&c))12}1314fn shred_kind(variant: u8) -> Option<&'static str> {15match variant {160xa5 => Some("legacy-data"),170x5a => Some("legacy-code"),18v => match v & 0xf0 {190x80 | 0x90 | 0xb0 => Some("merkle-data"),200x40 | 0x60 | 0x70 => Some("merkle-code"),21_ => None,22},23}24}2526fn main() -> io::Result<()> {27// usage: ./receiver 0.0.0.0:9000 <source-ip>28let bind = env::args().nth(1).unwrap_or_else(|| "0.0.0.0:9000".into());29let source: Option<Ipv4Addr> = env::args().nth(2).and_then(|s| s.parse().ok());30let socket = UdpSocket::bind(&bind)?;31eprintln!("listening on {bind}");3233let mut packet = [0u8; MAX_DATAGRAM];34let mut out = io::stdout().lock();35loop {36let (len, peer) = socket.recv_from(&mut packet)?;37if let (Some(want), SocketAddr::V4(v4)) = (source, peer) {38if *v4.ip() != want { continue; }39}40if is_text(&packet[..len]) {41writeln!(out, "text from {}: {}", peer.ip(), String::from_utf8_lossy(&packet[..len]).trim())?;42continue;43}44if len < COMMON_HEADER {45eprintln!("short datagram: len={len}");46continue;47}48let variant = packet[64];49let Some(kind) = shred_kind(variant) else {50eprintln!("unknown variant: {variant:#04x}");51continue;52};53let slot = u64le(&packet[65..73]);54let index = u32le(&packet[73..77]);55let version = u16le(&packet[77..79]);56let fec = u32le(&packet[79..83]);57writeln!(out, "slot={slot} type={kind} index={index} fec={fec} version={version} len={len}")?;58}59}
rustc --edition 2021 -O receiver.rs./receiver 0.0.0.0:9000
Use the UDP port you sent us. Behind static NAT, bind the private address or 0.0.0.0, not the public translated address.
The common header#
| Bytes | Field |
|---|---|
| 0 to 63 | Leader’s signature |
| 64 | Variant: data or coding, legacy or Merkle |
| 65 to 72 | Slot (u64, little-endian) |
| 73 to 76 | Index within the slot (u32) |
| 77 to 78 | Shred version (u16) |
| 79 to 82 | FEC set index (u32) |
Check the output#
Expect slot numbers to climb most of the time. They can step back now and then: UDP delivers out of order, and a fork can put two blocks in play for one slot. Data shreds and coding shreds keep separate index counters. The fec value repeats across many lines because it names the erasure set each shred belongs to. Nothing should arrive longer than 1,228 bytes.
If every line says unknown variant, write the values down and check them against the Agave release running on the network, rather than guessing a mask.
What it does not do#
The program does not verify the leader signature or the Merkle proof, recover missing shreds, or detect kernel receive-buffer overflow. It also writes a line per packet inside the receive loop, which can itself cause drops when the terminal is slow.
Towards production#
- Parse shreds with the ledger crate from the Agave release the network runs, rather than fixed offsets.
- Verify each shred’s signature against the scheduled leader for its slot.
- Group by
(slot, fec_set_index), recover missing data shreds, order them and deserialise entries. - Keep the receive thread doing nothing but receiving: raise
SO_RCVBUF, read in batches withrecvmmsg, and hand packets to other threads through a bounded queue.
Socket tuning is covered in the UDP wire contract. If you have not received a packet yet, start with the quickstart.