Reinstate ChanCell as a meaningful type.

This commit is contained in:
Nick Mathewson 2020-09-06 17:15:07 -04:00
parent e749e2bc27
commit ae1c813a70
2 changed files with 24 additions and 24 deletions

View File

@ -11,7 +11,6 @@
pub mod codec; pub mod codec;
pub mod msg; pub mod msg;
use bytes;
use caret::caret_int; use caret::caret_int;
/// The amount of data sent in a fixed-length cell. /// The amount of data sent in a fixed-length cell.
@ -137,27 +136,26 @@ impl ChanCmd {
_ => true, _ => true,
} }
} }
pub fn accepts_circid_val(self, id: CircID) -> bool {
if self.is_recognized() {
self.allows_circid() == (id == 0.into())
} else {
true
}
}
} }
/// A single cell extracted from, or encodeable onto, a channel. #[derive(Debug)]
#[derive(Clone, Debug)]
pub struct ChanCell { pub struct ChanCell {
circ: CircID, circid: CircID,
cmd: ChanCmd, msg: msg::ChannelMessage,
body: bytes::Bytes,
} }
impl ChanCell { impl ChanCell {
/// Return the cell's circuit ID.
pub fn get_circid(&self) -> CircID { pub fn get_circid(&self) -> CircID {
self.circ self.circid
} }
/// Return the cell's channel ID pub fn get_msg(&self) -> &msg::ChannelMessage {
pub fn get_cmd(&self) -> ChanCmd { &self.msg
self.cmd
}
/// Return the body of this cell.
pub fn get_body(&self) -> &bytes::Bytes {
&self.body
} }
} }

View File

@ -1,7 +1,4 @@
use crate::chancell::{ use crate::chancell::{msg::ChanMsg, ChanCell, ChanCmd, CircID};
msg::{ChanMsg, ChannelMessage},
ChanCmd, CircID,
};
use crate::crypto::cell::CELL_BODY_LEN; use crate::crypto::cell::CELL_BODY_LEN;
use arrayref::{array_mut_ref, array_ref}; use arrayref::{array_mut_ref, array_ref};
use bytes; use bytes;
@ -34,11 +31,12 @@ pub struct ChannelCodec {
} }
impl futures_codec::Encoder for ChannelCodec { impl futures_codec::Encoder for ChannelCodec {
type Item = (CircID, ChannelMessage); type Item = ChanCell;
type Error = Error; type Error = Error;
fn encode(&mut self, item: Self::Item, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Self::Item, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> {
let (circid, msg) = item; let circid = item.get_circid();
let msg = item.get_msg();
let cmd = msg.get_cmd(); let cmd = msg.get_cmd();
dst.write_u32(circid.into()); dst.write_u32(circid.into());
dst.write_u8(cmd.into()); dst.write_u8(cmd.into());
@ -69,7 +67,7 @@ impl futures_codec::Encoder for ChannelCodec {
} }
impl futures_codec::Decoder for ChannelCodec { impl futures_codec::Decoder for ChannelCodec {
type Item = (CircID, ChannelMessage); type Item = ChanCell;
type Error = Error; type Error = Error;
fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> { fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
@ -91,9 +89,13 @@ impl futures_codec::Decoder for ChannelCodec {
let cell = src.split_to(cell_len).freeze(); let cell = src.split_to(cell_len).freeze();
let mut r = Reader::from_bytes(&cell); let mut r = Reader::from_bytes(&cell);
let circid = r.take_u32()?.into(); let circid: CircID = r.take_u32()?.into();
r.advance(if varcell { 1 } else { 3 })?; r.advance(if varcell { 1 } else { 3 })?;
let msg = r.extract()?; let msg = r.extract()?;
Ok(Some((circid, msg)))
if !cmd.accepts_circid_val(circid) {
return Err(Error::Misc());
}
Ok(Some(ChanCell { circid, msg }))
} }
} }