Skip to content

Commit 49fcfbe

Browse files
committed
feat(onion_client): add tcp flag to OnionPathId
1 parent 793436a commit 49fcfbe

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

src/toxcore/onion/client/mod.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,10 @@ mod tests {
970970
let onion_node = OnionNode {
971971
pk: gen_keypair().0,
972972
saddr: "127.0.0.1:12345".parse().unwrap(),
973-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
973+
path_id: OnionPathId {
974+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
975+
tcp: false,
976+
},
974977
ping_id: None,
975978
data_pk: None,
976979
unsuccessful_pings: 0,
@@ -988,7 +991,10 @@ mod tests {
988991
let announce_request_data = AnnounceRequestData {
989992
pk: gen_keypair().0,
990993
saddr: "127.0.0.1:12345".parse().unwrap(),
991-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
994+
path_id: OnionPathId {
995+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
996+
tcp: false,
997+
},
992998
friend_pk: None,
993999
};
9941000

@@ -1042,7 +1048,10 @@ mod tests {
10421048
let onion_node = OnionNode {
10431049
pk,
10441050
saddr,
1045-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1051+
path_id: OnionPathId {
1052+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1053+
tcp: false,
1054+
},
10461055
ping_id: None,
10471056
data_pk: None,
10481057
unsuccessful_pings: 0,
@@ -1064,7 +1073,10 @@ mod tests {
10641073
let mut onion_node = OnionNode {
10651074
pk,
10661075
saddr: "127.0.0.1:12345".parse().unwrap(),
1067-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1076+
path_id: OnionPathId {
1077+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1078+
tcp: false,
1079+
},
10681080
ping_id: None,
10691081
data_pk: None,
10701082
unsuccessful_pings: 1,
@@ -1075,7 +1087,10 @@ mod tests {
10751087
};
10761088

10771089
let saddr = "127.0.0.1:12346".parse().unwrap();
1078-
let path_id = [gen_keypair().0, gen_keypair().0, gen_keypair().0];
1090+
let path_id = OnionPathId {
1091+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1092+
tcp: false,
1093+
};
10791094
let ping_id = sha256::hash(&[1, 2, 3]);
10801095
let data_pk = gen_keypair().0;
10811096
let new_now = now + Duration::from_secs(1);
@@ -1116,7 +1131,10 @@ mod tests {
11161131
let mut onion_node = OnionNode {
11171132
pk: gen_keypair().0,
11181133
saddr: "127.0.0.1:12345".parse().unwrap(),
1119-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1134+
path_id: OnionPathId {
1135+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1136+
tcp: false,
1137+
},
11201138
ping_id: None,
11211139
data_pk: None,
11221140
unsuccessful_pings: 0,
@@ -1569,7 +1587,10 @@ mod tests {
15691587
let request_data = AnnounceRequestData {
15701588
pk: sender_pk,
15711589
saddr,
1572-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1590+
path_id: OnionPathId {
1591+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1592+
tcp: false,
1593+
},
15731594
friend_pk: Some(friend_pk),
15741595
};
15751596
let request_id = state.announce_requests.new_ping_id(request_data);
@@ -1610,7 +1631,10 @@ mod tests {
16101631
let request_data = AnnounceRequestData {
16111632
pk: sender_pk,
16121633
saddr,
1613-
path_id: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1634+
path_id: OnionPathId {
1635+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
1636+
tcp: false,
1637+
},
16141638
friend_pk: Some(friend_pk),
16151639
};
16161640
let request_id = state.announce_requests.new_ping_id(request_data);

src/toxcore/onion/client/onion_path.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ use crate::toxcore::onion::packet::*;
1010
use crate::toxcore::tcp::packet::OnionRequest;
1111

1212
/// Onion path is identified by 3 public keys of nodes it consists of.
13-
pub type OnionPathId = [PublicKey; 3];
13+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14+
pub struct OnionPathId {
15+
/// Public keys of nodes the path consists of.
16+
pub keys: [PublicKey; 3],
17+
/// Whether first node is a TCP relay.
18+
pub tcp: bool,
19+
}
1420

1521
/// Node for onion path.
1622
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -65,11 +71,15 @@ impl OnionPath {
6571

6672
/// Array of 3 public keys of nodes the path consists of.
6773
pub fn id(&self) -> OnionPathId {
68-
[
74+
let keys = [
6975
self.nodes[0].public_key,
7076
self.nodes[1].public_key,
7177
self.nodes[2].public_key,
72-
]
78+
];
79+
OnionPathId {
80+
keys,
81+
tcp: self.tcp,
82+
}
7383
}
7484

7585
/// Create `OnionRequest0` packet from `InnerOnionRequest` that should be
@@ -166,7 +176,10 @@ mod tests {
166176
PackedNode::new(saddr_2, &pk_2),
167177
PackedNode::new(saddr_3, &pk_3),
168178
], /* TCP */ false);
169-
assert_eq!(path.id(), [pk_1, pk_2, pk_3]);
179+
assert_eq!(path.id(), OnionPathId {
180+
keys: [pk_1, pk_2, pk_3],
181+
tcp: false,
182+
});
170183
}
171184

172185
#[test]

src/toxcore/onion/client/paths_pool.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,10 @@ mod tests {
335335
paths_pool.path_nodes.put(node);
336336
}
337337

338-
let path_id = [
339-
gen_keypair().0,
340-
gen_keypair().0,
341-
gen_keypair().0,
342-
];
338+
let path_id = OnionPathId {
339+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
340+
tcp: false,
341+
};
343342
let path = paths_pool.get_or_random_path(&dht, &tcp_connections, path_id, $friends).unwrap();
344343
assert_ne!(path.id(), path_id);
345344
assert_eq!(path, paths_pool.$paths[0].path);
@@ -361,11 +360,10 @@ mod tests {
361360
paths_pool.path_nodes.put(node);
362361
}
363362

364-
let path_id = [
365-
gen_keypair().0,
366-
gen_keypair().0,
367-
gen_keypair().0,
368-
];
363+
let path_id = OnionPathId {
364+
keys: [gen_keypair().0, gen_keypair().0, gen_keypair().0],
365+
tcp: true,
366+
};
369367
let path = paths_pool.get_or_random_path(&dht, &tcp_connections, path_id, $friends).unwrap();
370368
assert_ne!(path.id(), path_id);
371369
assert_eq!(path, paths_pool.$paths[0].path);

0 commit comments

Comments
 (0)