Sort linkspecs in the same order as tor does

This commit is contained in:
Nick Mathewson 2020-09-17 15:33:18 -04:00
parent ba546a798d
commit f256847f82
4 changed files with 27 additions and 4 deletions

View File

@ -1,4 +1,6 @@
use std::cmp::Ordering;
use std::net::{IpAddr, SocketAddr};
use tor_bytes::{Error, Readable, Reader, Result, Writeable, Writer};
use tor_llcrypto::pk::ed25519;
use tor_llcrypto::pk::rsa::RSAIdentity;
@ -7,7 +9,7 @@ use tor_llcrypto::pk::rsa::RSAIdentity;
///
/// TODO: move this. It's used in a bunch of other places.
#[non_exhaustive]
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum LinkSpec {
/// The TCP address of an OR Port for a relay
OrPort(IpAddr, u16),
@ -112,3 +114,24 @@ impl From<ed25519::PublicKey> for LinkSpec {
LinkSpec::Ed25519Id(id)
}
}
/// Helper for partial_cmd: return the position in the list of identifiers
/// in which a given linkspec should occur
impl LinkSpec {
fn sort_pos(&self) -> u8 {
use LinkSpec::*;
match self {
OrPort(IpAddr::V4(_), _) => 0,
RSAId(_) => 1,
Ed25519Id(_) => 2,
OrPort(IpAddr::V6(_), _) => 3,
Unrecognized(n, _) => *n,
}
}
}
impl PartialOrd for LinkSpec {
fn partial_cmp(&self, other: &LinkSpec) -> Option<Ordering> {
Some(self.sort_pos().cmp(&other.sort_pos()))
}
}

View File

@ -25,7 +25,6 @@ pub trait ExtendTarget: ChanTarget {
// TODO: bad API
fn get_linkspecs(&self) -> Vec<crate::LinkSpec> {
let mut result = Vec::new();
// TODO: return these in the same order as Tor.
result.push(self.get_ed_identity().clone().into());
result.push(self.get_rsa_identity().clone().into());
for addr in self.get_addrs().iter() {

View File

@ -174,7 +174,6 @@ where
let hop = (self.crypto.n_layers() - 1) as u8;
let (state, msg) = H::client1(rng, &key)?;
// XXXX sort the linkspecs into canonical order
let extend_msg = Extend2::new(linkspecs, handshake_id, msg);
let cell = RelayCell::new(0.into(), extend_msg.as_message());

View File

@ -505,7 +505,9 @@ pub struct Extend2 {
}
impl Extend2 {
/// Create a new Extend2 cell.
pub fn new(linkspec: Vec<LinkSpec>, handshake_type: u16, handshake: Vec<u8>) -> Self {
pub fn new(mut linkspec: Vec<LinkSpec>, handshake_type: u16, handshake: Vec<u8>) -> Self {
linkspec.sort_by(|a, b| a.partial_cmp(b).unwrap());
Extend2 {
linkspec,
handshake_type,