tor-cell: Remove ChanMsg methods that are duplicated in ChanMsgClass.

This commit is contained in:
Nick Mathewson 2023-02-07 08:48:34 -05:00
parent 71445f7ace
commit 3c7aea723b
11 changed files with 20 additions and 37 deletions

View File

@ -2,3 +2,4 @@ BREAKING: The interfaces for ChanMsg::Body and RelayMsg::Body have been made
more uniform.
BREAKING: RelayMsg no longer has any unit variants.
BREAKING: Renamed VPadding to Vpadding, for consistent snake case.
BREAKING: Moved ChanMsg methods into a trait.

View File

@ -1,7 +1,7 @@
//! Implementation for encoding and decoding of ChanCells.
use super::CELL_DATA_LEN;
use crate::chancell::{msg, ChanCell, ChanCmd, CircId};
use crate::chancell::{msg, ChanCell, ChanCmd, ChanMsgClass, CircId};
use crate::Error;
use arrayref::{array_mut_ref, array_ref};
use tor_bytes::{self, Reader, Writer};
@ -60,7 +60,7 @@ impl ChannelCodec {
// now write the cell body and handle the length.
if cmd.is_var_cell() {
dst.write_u16(0);
msg.write_body_onto(dst)?;
msg.encode_onto(dst)?;
let len = dst.len() - pos - 2;
if len > std::u16::MAX as usize {
return Err(Error::Internal(internal!("ran out of space for varcell")));
@ -68,7 +68,7 @@ impl ChannelCodec {
// go back and set the length.
*(array_mut_ref![&mut dst[pos..pos + 2], 0, 2]) = (len as u16).to_be_bytes();
} else {
msg.write_body_onto(dst)?;
msg.encode_onto(dst)?;
let len = dst.len() - pos;
if len > CELL_DATA_LEN {
return Err(Error::Internal(internal!("ran out of space for cell")));
@ -113,7 +113,7 @@ impl ChannelCodec {
let mut r = Reader::from_bytes(&cell);
let circid: CircId = r.take_u32().map_err(wrap_err)?.into();
r.advance(if varcell { 3 } else { 1 }).map_err(wrap_err)?;
let msg = msg::ChanMsg::take(&mut r, cmd).map_err(wrap_err)?;
let msg = msg::ChanMsg::decode_from_reader(cmd, &mut r).map_err(wrap_err)?;
if !cmd.accepts_circid_val(circid) {
return Err(Error::ChanProto(format!(

View File

@ -79,25 +79,6 @@ pub enum ChanMsg : ChanMsg {
}
}
impl ChanMsg {
/// Return the ChanCmd for this message.
pub fn cmd(&self) -> ChanCmd {
super::ChanMsgClass::cmd(self)
}
/// Write the body of this message (not including length or command).
pub fn write_body_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
super::ChanMsgClass::encode_onto(self, w)
}
/// Decode this message from a given reader, according to a specified
/// command value. The reader must be truncated to the exact length
/// of the body.
pub fn take(r: &mut Reader<'_>, cmd: ChanCmd) -> Result<Self> {
super::ChanMsgClass::decode_from_reader(cmd, r)
}
}
/// A Padding message is a fixed-length message on a channel that is
/// ignored.
///

View File

@ -3,7 +3,7 @@
// Reminder: you can think of a cell as an message plus a circuitid.
#![allow(clippy::uninlined_format_args)]
use tor_cell::chancell::{codec, msg, ChanCell, ChanCmd, CircId};
use tor_cell::chancell::{codec, msg, ChanCell, ChanCmd, ChanMsgClass, CircId};
use tor_cell::Error;
use bytes::BytesMut;

View File

@ -6,7 +6,7 @@ use tor_bytes::Error as BytesError;
/// Except where noted, these were taken by instrumenting Tor
/// 0.4.5.0-alpha-dev to dump all of its cells to the logs, and
/// running in a chutney network with "test-network-all".
use tor_cell::chancell::{msg, ChanCmd};
use tor_cell::chancell::{msg, ChanCmd, ChanMsgClass};
use tor_units::IntegerMilliseconds;
use std::net::IpAddr;
@ -29,13 +29,13 @@ fn unhex(s: &str, pad_to_len: bool) -> Vec<u8> {
fn decode_err(cmd: ChanCmd, s: &str, pad_to_len: bool) -> BytesError {
let body = unhex(s, pad_to_len);
let mut r = tor_bytes::Reader::from_slice(&body[..]);
msg::ChanMsg::take(&mut r, cmd).unwrap_err()
msg::ChanMsg::decode_from_reader(cmd, &mut r).unwrap_err()
}
fn test_decode(cmd: ChanCmd, s: &str, pad_to_len: bool) -> (Vec<u8>, msg::ChanMsg) {
let body = unhex(s, pad_to_len);
let mut r = tor_bytes::Reader::from_slice(&body[..]);
let msg = msg::ChanMsg::take(&mut r, cmd).unwrap();
let msg = msg::ChanMsg::decode_from_reader(cmd, &mut r).unwrap();
(body, msg)
}
@ -53,12 +53,8 @@ fn test_body(cmd: ChanCmd, s: &str, m: &msg::ChanMsg, pad_to_len: bool) {
let mut encoded1 = Vec::new();
let mut encoded2 = Vec::new();
decoded
.write_body_onto(&mut encoded1)
.expect("encode error");
m.clone()
.write_body_onto(&mut encoded2)
.expect("encode error");
decoded.encode_onto(&mut encoded1).expect("encode error");
m.clone().encode_onto(&mut encoded2).expect("encode error");
if pad_to_len {
assert!(encoded1.len() <= CELL_SIZE);
assert!(encoded2.len() <= CELL_SIZE);

View File

@ -75,6 +75,7 @@ use safelog::sensitive as sv;
use std::pin::Pin;
use std::sync::{Mutex, MutexGuard};
use std::time::Duration;
use tor_cell::chancell::ChanMsgClass;
use tor_cell::chancell::{msg, msg::PaddingNegotiate, ChanCell, CircId};
use tor_error::internal;
use tor_linkspec::{HasRelayIds, OwnedChanTarget};

View File

@ -72,7 +72,7 @@ pub(crate) mod test {
use std::pin::Pin;
use super::{futures_codec, ChannelCodec};
use tor_cell::chancell::{msg, ChanCell, ChanCmd, CircId};
use tor_cell::chancell::{msg, ChanCell, ChanCmd, ChanMsgClass, CircId};
/// Helper type for reading and writing bytes to/from buffers.
// TODO: We might want to move this

View File

@ -11,7 +11,7 @@ use crate::channel::codec::{ChannelCodec, CodecError};
use crate::channel::UniqId;
use crate::util::skew::ClockSkew;
use crate::{Error, Result};
use tor_cell::chancell::{msg, ChanCmd};
use tor_cell::chancell::{msg, ChanCmd, ChanMsgClass};
use tor_rtcompat::SleepProvider;
use std::net::SocketAddr;

View File

@ -12,6 +12,7 @@ use crate::util::err::{ChannelClosed, ReactorError};
use crate::{Error, Result};
use tor_basic_utils::futures::SinkExt as _;
use tor_cell::chancell::msg::{Destroy, DestroyReason, PaddingNegotiate};
use tor_cell::chancell::ChanMsgClass;
use tor_cell::chancell::{msg::ChanMsg, ChanCell, CircId};
use tor_rtcompat::SleepProvider;

View File

@ -6,7 +6,10 @@
use crate::{Error, Result};
use std::fmt::{self, Display};
use tor_cell::chancell::msg::{self as chanmsg, ChanMsg};
use tor_cell::chancell::{
msg::{self as chanmsg, ChanMsg},
ChanMsgClass,
};
/// A subclass of ChanMsg that can arrive in response to a CREATE* cell
/// that we send.

View File

@ -34,7 +34,7 @@ use crate::circuit::sendme::StreamSendWindow;
use crate::crypto::handshake::ntor::{NtorClient, NtorPublicKey};
use crate::crypto::handshake::{ClientHandshake, KeyGenerator};
use safelog::sensitive as sv;
use tor_cell::chancell;
use tor_cell::chancell::{self, ChanMsgClass};
use tor_cell::chancell::{ChanCell, CircId};
use tor_linkspec::{LinkSpec, OwnedChanTarget, RelayIds};
use tor_llcrypto::pk;