Skip to main content

sanitize_full_sync/
sanitize_full_sync.rs

1//! Sanitize a captured TCP full-sync frame for safe use as a test fixture.
2//!
3//! Every property value is replaced with a type-preserving neutral value,
4//! except the non-sensitive model discriminator and firmware version. Node and
5//! property names, ordering, and tree topology remain unchanged.
6//!
7//! Usage:
8//! `cargo run --example sanitize_full_sync -- raw-frame.bin sanitized-frame.bin`
9
10use std::env;
11use std::fs;
12
13use rodecaster_protocol::change_frame::{decode, encode_full_sync, ChangeFrame};
14use rodecaster_protocol::frame::Packet;
15use rodecaster_protocol::{Layout, Node, Value};
16
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18    let mut args = env::args_os().skip(1);
19    let input = args.next().ok_or("missing input frame path")?;
20    let output = args.next().ok_or("missing output frame path")?;
21    if args.next().is_some() {
22        return Err("expected exactly two paths: input and output".into());
23    }
24
25    let bytes = fs::read(&input)?;
26    let (packet, consumed) =
27        Packet::from_bytes(&bytes).ok_or("input is not a complete TCP frame")?;
28    if consumed != bytes.len() {
29        return Err(format!("input contains {} trailing bytes", bytes.len() - consumed).into());
30    }
31    let ChangeFrame::FullSync { mut root } =
32        decode(&packet.payload).ok_or("invalid change frame")?
33    else {
34        return Err("input frame is not a full sync".into());
35    };
36
37    let layout = Layout::from_full_sync(&root)?;
38    let firmware = find_property(&root, "systemFirmwareVersion")
39        .and_then(|value| match value {
40            Value::String(value) => Some(value.as_str()),
41            _ => None,
42        })
43        .unwrap_or("unknown")
44        .to_string();
45    let (nodes, properties) = tree_size(&root);
46
47    sanitize_node(&mut root);
48    let sanitized = Packet::new(encode_full_sync(&root)).to_bytes();
49    fs::write(&output, &sanitized)?;
50
51    eprintln!("model={}", layout.model());
52    eprintln!("firmware={firmware}");
53    eprintln!("faders={}", layout.fader_count());
54    eprintln!("channels={}", layout.channel_count());
55    eprintln!("sources={}", layout.source_count());
56    eprintln!("mixes_per_source={}", layout.mix_count_per_source());
57    eprintln!("input_sources={}", layout.input_source_count());
58    eprintln!("headphones={}", layout.headphone_count());
59    eprintln!("effects={}", layout.effects_count());
60    eprintln!("pads={}", layout.pad_count());
61    eprintln!("sip_call_slots={}", layout.sip_call_slots_count());
62    eprintln!("sip_registrations={}", layout.sip_registration_count());
63    eprintln!("nodes={nodes}");
64    eprintln!("properties={properties}");
65    eprintln!("frame_bytes={}", sanitized.len());
66    Ok(())
67}
68
69fn sanitize_node(node: &mut Node) {
70    for property in &mut node.properties {
71        property.value = sanitize_value(&property.name, &property.value);
72    }
73    for child in &mut node.children {
74        sanitize_node(child);
75    }
76}
77
78fn sanitize_value(name: &str, value: &Value) -> Value {
79    match value {
80        Value::Bool(_) => Value::Bool(false),
81        Value::Int(value) if name == "boardType" => Value::Int(*value),
82        Value::Int(_) => Value::Int(0),
83        Value::Int64(_) => Value::Int64(0),
84        Value::Double(_) => Value::Double(0.0),
85        Value::String(value) if name == "systemFirmwareVersion" => Value::String(value.clone()),
86        Value::String(_) if name == "mixLevelWithAnchor" => Value::String("0.0|0.0".into()),
87        Value::String(_) => Value::String("<redacted>".into()),
88        Value::Array(values) => Value::Array(
89            values
90                .iter()
91                .map(|value| sanitize_value(name, value))
92                .collect(),
93        ),
94        Value::Binary(data) => Value::Binary(vec![0; data.len()]),
95        Value::Undefined => Value::Undefined,
96        Value::Unknown { type_id, data } => Value::Unknown {
97            type_id: *type_id,
98            data: vec![0; data.len()],
99        },
100    }
101}
102
103fn find_property<'a>(node: &'a Node, name: &str) -> Option<&'a Value> {
104    node.properties
105        .iter()
106        .find(|property| property.name == name)
107        .map(|property| &property.value)
108        .or_else(|| {
109            node.children
110                .iter()
111                .find_map(|child| find_property(child, name))
112        })
113}
114
115fn tree_size(node: &Node) -> (usize, usize) {
116    node.children
117        .iter()
118        .fold((1, node.properties.len()), |(nodes, properties), child| {
119            let (child_nodes, child_properties) = tree_size(child);
120            (nodes + child_nodes, properties + child_properties)
121        })
122}