1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::ops::DerefMut;
use bincode::Options;
use serde::{Deserialize, Serialize};
use crate::buffer::{Buff, BuffMut};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum HandshakeFrame {
ClientHello {
long_pk: x25519_dalek::PublicKey,
eph_pk: x25519_dalek::PublicKey,
version: u64,
},
ServerHello {
long_pk: x25519_dalek::PublicKey,
eph_pk: x25519_dalek::PublicKey,
resume_token: Buff,
},
ClientResume {
resume_token: Buff,
shard_id: u8,
},
}
impl HandshakeFrame {
pub fn to_bytes(&self) -> Vec<u8> {
bincode::serialize(self).unwrap()
}
pub fn from_bytes(bts: &[u8]) -> anyhow::Result<Self> {
Ok(bincode::DefaultOptions::new()
.with_fixint_encoding()
.allow_trailing_bytes()
.with_limit(bts.len() as _)
.deserialize(bts)?)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum DataFrameV2 {
Data {
frame_no: u64,
high_recv_frame_no: u64,
total_recv_frames: u64,
body: Buff,
},
Parity {
data_frame_first: u64,
data_count: u8,
parity_count: u8,
parity_index: u8,
pad_size: usize,
body: Buff,
},
}
impl DataFrameV2 {
pub fn pad(&self, hidden_data: u8) -> Buff {
let options = bincode::DefaultOptions::new()
.with_little_endian()
.with_varint_encoding()
.allow_trailing_bytes();
let mut toret = BuffMut::new();
options.serialize_into(toret.deref_mut(), self).unwrap();
toret.extend_from_slice(&[hidden_data]);
toret.extend_from_slice(&vec![0xff; rand::random::<usize>() % 16]);
toret.into()
}
pub fn depad(bts: &[u8]) -> Option<(Self, u8)> {
let options = bincode::DefaultOptions::new()
.with_little_endian()
.with_varint_encoding()
.with_limit(bts.len() as _)
.allow_trailing_bytes();
let mut ptr = bts;
let res = options.deserialize_from(&mut ptr).ok()?;
Some((res, ptr.get(0).copied().unwrap_or(0xff)))
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DataFrameV1 {
pub frame_no: u64,
pub run_no: u64,
pub run_idx: u8,
pub data_shards: u8,
pub parity_shards: u8,
pub high_recv_frame_no: u64,
pub total_recv_frames: u64,
pub body: Buff,
}