use super::{Ask, Blob, BlobId, PieceSize};
pub(crate) const VERSION_1: u8 = 1;
pub(crate) const VERSION_2: u8 = 2;
const DECLINE: u8 = 0;
const LIST: u8 = 1;
const HAVE: u8 = 2;
const PIECE: u8 = 3;
const SHORT: usize = 43;
pub(crate) const MAX_ASK_BYTES: usize = 47;
pub(crate) fn encode(blob: &Blob, ask: Ask) -> Vec<u8> {
let mut out = Vec::with_capacity(MAX_ASK_BYTES);
out.push(VERSION_1);
out.push(match ask {
Ask::List => LIST,
Ask::Have => HAVE,
Ask::Piece(_) => PIECE,
});
out.push(blob.piece().log2());
out.extend_from_slice(blob.root().as_bytes());
out.extend_from_slice(&blob.size().to_be_bytes());
if let Ask::Piece(index) = ask {
out.extend_from_slice(&index.to_be_bytes());
}
out
}
pub(crate) fn decode(bytes: &[u8]) -> Option<(Blob, Ask)> {
if bytes.len() < SHORT || bytes[0] != VERSION_1 {
return None;
}
let kind = bytes[1];
let expected = match kind {
LIST | HAVE => SHORT,
PIECE => MAX_ASK_BYTES,
_ => return None,
};
if bytes.len() != expected {
return None;
}
let piece = PieceSize::from_log2(bytes[2])?;
let root = BlobId::from_bytes(bytes[3..35].try_into().ok()?);
let size = u64::from_be_bytes(bytes[35..43].try_into().ok()?);
if size == 0 {
return None;
}
let blob = Blob::new(root, size, piece)?;
let ask = match kind {
LIST if blob.count() >= 2 => Ask::List,
LIST => return None,
HAVE => Ask::Have,
_ => {
let index = u32::from_be_bytes(bytes[43..47].try_into().ok()?);
if index >= blob.count() {
return None;
}
Ask::Piece(index)
}
};
Some((blob, ask))
}
pub(crate) fn reply_len(blob: &Blob, ask: Ask) -> u64 {
match ask {
Ask::List => u64::from(blob.count()) * 32,
Ask::Have => u64::from(blob.count()).div_ceil(8),
Ask::Piece(index) => blob.span(index).map_or(0, |span| span.end - span.start),
}
}
pub(crate) const OPEN_BYTES: usize = 48;
pub(crate) const ORDER_BYTES: usize = 5;
pub(crate) const FRAME_HEAD_BYTES: usize = 9;
pub(crate) fn encode_open(blob: &Blob) -> [u8; OPEN_BYTES] {
let mut out = [0; OPEN_BYTES];
out[0] = VERSION_2;
out[1] = blob.piece().log2();
out[2..34].copy_from_slice(blob.root().as_bytes());
out[34..42].copy_from_slice(&blob.size().to_be_bytes());
out
}
pub(crate) fn decode_open(bytes: &[u8; OPEN_BYTES]) -> Option<Blob> {
if bytes[0] != VERSION_2 || bytes[42..].iter().any(|&b| b != 0) {
return None;
}
let piece = PieceSize::from_log2(bytes[1])?;
let root = BlobId::from_bytes(bytes[2..34].try_into().ok()?);
let size = u64::from_be_bytes(bytes[34..42].try_into().ok()?);
if size == 0 {
return None;
}
Blob::new(root, size, piece)
}
fn kind_and_index(ask: Ask) -> (u8, u32) {
match ask {
Ask::List => (LIST, 0),
Ask::Have => (HAVE, 0),
Ask::Piece(index) => (PIECE, index),
}
}
pub(crate) fn encode_order(ask: Ask) -> [u8; ORDER_BYTES] {
let (kind, index) = kind_and_index(ask);
let mut out = [0; ORDER_BYTES];
out[0] = kind;
out[1..].copy_from_slice(&index.to_be_bytes());
out
}
pub(crate) fn decode_order(blob: &Blob, bytes: &[u8; ORDER_BYTES]) -> Option<Ask> {
let index = u32::from_be_bytes(bytes[1..].try_into().ok()?);
match bytes[0] {
LIST if index == 0 && blob.count() >= 2 => Some(Ask::List),
HAVE if index == 0 => Some(Ask::Have),
PIECE if index < blob.count() => Some(Ask::Piece(index)),
_ => None,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Head {
Answer { ask: Ask, len: u32 },
Decline,
}
pub(crate) fn encode_head(head: Head) -> [u8; FRAME_HEAD_BYTES] {
let mut out = [0; FRAME_HEAD_BYTES];
if let Head::Answer { ask, len } = head {
let (kind, index) = kind_and_index(ask);
out[0] = kind;
out[1..5].copy_from_slice(&index.to_be_bytes());
out[5..].copy_from_slice(&len.to_be_bytes());
}
out
}
pub(crate) fn decode_head(bytes: &[u8; FRAME_HEAD_BYTES]) -> Option<Head> {
let index = u32::from_be_bytes(bytes[1..5].try_into().ok()?);
let len = u32::from_be_bytes(bytes[5..].try_into().ok()?);
let ask = match bytes[0] {
DECLINE if index == 0 && len == 0 => return Some(Head::Decline),
LIST if index == 0 => Ask::List,
HAVE if index == 0 => Ask::Have,
PIECE => Ask::Piece(index),
_ => return None,
};
Some(Head::Answer { ask, len })
}
#[cfg(test)]
mod tests {
use super::*;
fn blob(size: u64) -> Blob {
Blob::new(BlobId::from_bytes([7; 32]), size, PieceSize::MIN).expect("a valid blob")
}
#[test]
fn every_ask_round_trips() {
let b = blob(3 * 1024 + 5);
for ask in [Ask::List, Ask::Have, Ask::Piece(0), Ask::Piece(3)] {
let bytes = encode(&b, ask);
assert_eq!(
bytes.len(),
if matches!(ask, Ask::Piece(_)) { 47 } else { 43 }
);
assert_eq!(decode(&bytes), Some((b, ask)), "{ask:?}");
}
assert_eq!(reply_len(&b, Ask::List), 4 * 32);
assert_eq!(reply_len(&b, Ask::Have), 1);
assert_eq!(reply_len(&b, Ask::Piece(0)), 1024);
assert_eq!(reply_len(&b, Ask::Piece(3)), 5);
}
#[test]
fn every_malformed_ask_is_refused() {
let b = blob(3 * 1024 + 5);
let have = encode(&b, Ask::Have);
let piece = encode(&b, Ask::Piece(1));
let edit = |mut bytes: Vec<u8>, at: usize, value: u8| {
bytes[at] = value;
bytes
};
let with_size = |mut bytes: Vec<u8>, size: u64| {
bytes[35..43].copy_from_slice(&size.to_be_bytes());
bytes
};
let with_index = |mut bytes: Vec<u8>, index: u32| {
bytes[43..47].copy_from_slice(&index.to_be_bytes());
bytes
};
let cases: Vec<(&str, Vec<u8>)> = vec![
("empty", Vec::new()),
("one byte short", have[..42].to_vec()),
("HAVE with an index", edit(piece.clone(), 1, HAVE)),
("PIECE without an index", edit(have.clone(), 1, PIECE)),
("one trailing byte", [piece.clone(), vec![0]].concat()),
("version 0", edit(have.clone(), 0, 0)),
("version 2", edit(have.clone(), 0, 2)),
("ask 0", edit(have.clone(), 1, 0)),
("ask 4", edit(have.clone(), 1, 4)),
("piece log2 9", edit(have.clone(), 2, 9)),
("piece log2 25", edit(have.clone(), 2, 25)),
("size 0", with_size(have.clone(), 0)),
(
"more than MAX_PIECES",
with_size(
have.clone(),
(u64::from(super::super::MAX_PIECES) + 1) * 1024,
),
),
("LIST of one piece", with_size(encode(&b, Ask::List), 1024)),
("PIECE at count", with_index(piece.clone(), 4)),
("PIECE at u32::MAX", with_index(piece, u32::MAX)),
];
for (name, bytes) in cases {
assert_eq!(decode(&bytes), None, "{name}");
}
}
#[test]
fn every_session_unit_round_trips() {
let b = blob(3 * 1024 + 5);
let open = encode_open(&b);
assert_eq!(open.len(), MAX_ASK_BYTES + 1);
assert_eq!(decode_open(&open), Some(b));
for ask in [Ask::List, Ask::Have, Ask::Piece(0), Ask::Piece(3)] {
assert_eq!(decode_order(&b, &encode_order(ask)), Some(ask), "{ask:?}");
let head = Head::Answer { ask, len: 1234 };
assert_eq!(decode_head(&encode_head(head)), Some(head), "{ask:?}");
}
assert_eq!(
decode_head(&encode_head(Head::Decline)),
Some(Head::Decline)
);
assert_eq!(encode_order(Ask::Piece(3)), [PIECE, 0, 0, 0, 3]);
assert_eq!(
encode_head(Head::Answer {
ask: Ask::Piece(2),
len: 5
}),
[PIECE, 0, 0, 0, 2, 0, 0, 0, 5]
);
}
#[test]
fn every_malformed_session_unit_is_refused() {
let b = blob(3 * 1024 + 5);
let open = encode_open(&b);
let edit_open = |at: usize, value: u8| {
let mut bytes = open;
bytes[at] = value;
bytes
};
let with_size = |size: u64| {
let mut bytes = open;
bytes[34..42].copy_from_slice(&size.to_be_bytes());
bytes
};
let opens: Vec<(&str, [u8; OPEN_BYTES])> = vec![
("version 1", edit_open(0, VERSION_1)),
("version 3", edit_open(0, 3)),
("padding", edit_open(47, 1)),
("piece log2 9", edit_open(1, 9)),
("piece log2 25", edit_open(1, 25)),
("size 0", with_size(0)),
(
"more than MAX_PIECES",
with_size((u64::from(super::super::MAX_PIECES) + 1) * 1024),
),
];
for (name, bytes) in opens {
assert_eq!(decode_open(&bytes), None, "open: {name}");
}
let one_piece = blob(1024);
let orders: Vec<(&str, Blob, [u8; ORDER_BYTES])> = vec![
("kind 0", b, [0, 0, 0, 0, 0]),
("kind 4", b, [4, 0, 0, 0, 0]),
("LIST with an index", b, [LIST, 0, 0, 0, 1]),
("HAVE with an index", b, [HAVE, 0, 0, 0, 1]),
("LIST of one piece", one_piece, encode_order(Ask::List)),
("PIECE at count", b, encode_order(Ask::Piece(4))),
("PIECE at u32::MAX", b, encode_order(Ask::Piece(u32::MAX))),
];
for (name, blob, bytes) in orders {
assert_eq!(decode_order(&blob, &bytes), None, "order: {name}");
}
let heads: Vec<(&str, [u8; FRAME_HEAD_BYTES])> = vec![
("kind 4", [4, 0, 0, 0, 0, 0, 0, 0, 0]),
("DECLINE with an index", [DECLINE, 0, 0, 0, 1, 0, 0, 0, 0]),
("DECLINE with a length", [DECLINE, 0, 0, 0, 0, 0, 0, 0, 1]),
("LIST with an index", [LIST, 0, 0, 0, 1, 0, 0, 0, 32]),
("HAVE with an index", [HAVE, 0, 0, 0, 1, 0, 0, 0, 1]),
];
for (name, bytes) in heads {
assert_eq!(decode_head(&bytes), None, "head: {name}");
}
}
}