Skip to content

Commit bdfedb4

Browse files
committed
feat(dht): handle onion client packets
1 parent 7840f8f commit bdfedb4

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/toxcore/dht/server/errors.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ error_kind! {
2525
#[doc = "Error indicates that sending response packet error."]
2626
#[fail(display = "Sending response error")]
2727
SendTo,
28-
#[doc = "Error indicates that received packet is not handled here."]
29-
#[fail(display = "This packet kind is not handled here error")]
30-
NotHandled,
3128
#[doc = "Error indicates that received packet's ping_id is zero."]
3229
#[fail(display = "Zero ping id error")]
3330
ZeroPingId,
@@ -40,9 +37,15 @@ error_kind! {
4037
#[doc = "Error indicates that NetCrypto is not initialized."]
4138
#[fail(display = "NetCrypto is not initialized error")]
4239
NetCrypto,
40+
#[doc = "Error indicates that OnionClient is not initialized."]
41+
#[fail(display = "OnionClient is not initialized error")]
42+
OnionClient,
4343
#[doc = "Error indicates that handling NetCrypto packet made an error."]
4444
#[fail(display = "Handling NetCrypto packet failed")]
4545
HandleNetCrypto,
46+
#[doc = "Error indicates that handling OnionClient packet made an error."]
47+
#[fail(display = "Handling OnionClient packet failed")]
48+
HandleOnionClient,
4649
#[doc = "Error indicates that onion or net crypto processing fails."]
4750
#[doc = "## This enum entry is temporary for onion or net crypto module's transition to failure"]
4851
#[fail(display = "Onion or NetCrypto related error")]

src/toxcore/dht/server/mod.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::toxcore::dht::packed_node::*;
2727
use crate::toxcore::dht::kbucket::*;
2828
use crate::toxcore::dht::ktree::*;
2929
use crate::toxcore::dht::precomputed_cache::*;
30+
use crate::toxcore::onion::client::*;
3031
use crate::toxcore::onion::packet::*;
3132
use crate::toxcore::onion::onion_announce::*;
3233
use crate::toxcore::dht::request_queue::*;
@@ -163,6 +164,10 @@ pub struct Server {
163164
/// pure bootstrap server when we don't have friends and therefore don't
164165
/// have to handle related packets.
165166
net_crypto: Option<NetCrypto>,
167+
/// Onion client that handles `OnionDataResponse` and
168+
/// `OnionAnnounceResponse` packets. It can be `None` in case of pure
169+
/// bootstrap server.
170+
onion_client: Option<Box<OnionClient>>,
166171
/// If LAN discovery is enabled `Server` will handle `LanDiscovery` packets
167172
/// and send `NodesRequest` packets in reply.
168173
lan_discovery_enabled: bool,
@@ -220,6 +225,7 @@ impl Server {
220225
bootstrap_info: None,
221226
tcp_onion_sink: None,
222227
net_crypto: None,
228+
onion_client: None,
223229
lan_discovery_enabled: true,
224230
is_ipv6_enabled: false,
225231
initial_bootstrap: Vec::new(),
@@ -708,14 +714,8 @@ impl Server {
708714
Packet::OnionResponse1(packet) => Box::new(self.handle_onion_response_1(packet)),
709715
Packet::BootstrapInfo(packet) => Box::new(self.handle_bootstrap_info(&packet, addr)),
710716
Packet::CryptoData(packet) => Box::new(self.handle_crypto_data(&packet, addr)),
711-
// This packet should be handled in client only
712-
Packet::OnionDataResponse(_packet) => Box::new(future::err(
713-
HandlePacketError::from(HandlePacketErrorKind::NotHandled)
714-
)),
715-
// This packet should be handled in client only
716-
Packet::OnionAnnounceResponse(_packet) => Box::new(future::err(
717-
HandlePacketError::from(HandlePacketErrorKind::NotHandled)
718-
)),
717+
Packet::OnionDataResponse(packet) => Box::new(self.handle_onion_data_response(&packet)),
718+
Packet::OnionAnnounceResponse(packet) => Box::new(self.handle_onion_announce_response(&packet, addr)),
719719
}
720720
}
721721

@@ -951,6 +951,32 @@ impl Server {
951951
}
952952
}
953953

954+
/// Handle received `OnionDataResponse` packet and pass it to `onion_client` module.
955+
fn handle_onion_data_response(&self, packet: &OnionDataResponse)
956+
-> impl Future<Item = (), Error = HandlePacketError> + Send {
957+
if let Some(ref onion_client) = self.onion_client {
958+
Either::A(onion_client.handle_data_response(packet)
959+
.map_err(|e| e.context(HandlePacketErrorKind::HandleOnionClient).into()))
960+
} else {
961+
Either::B( future::err(
962+
HandlePacketError::from(HandlePacketErrorKind::OnionClient)
963+
))
964+
}
965+
}
966+
967+
/// Handle received `OnionAnnounceResponse` packet and pass it to `onion_client` module.
968+
fn handle_onion_announce_response(&self, packet: &OnionAnnounceResponse, addr: SocketAddr)
969+
-> impl Future<Item = (), Error = HandlePacketError> + Send {
970+
if let Some(ref onion_client) = self.onion_client {
971+
Either::A(onion_client.handle_announce_response(packet, addr)
972+
.map_err(|e| e.context(HandlePacketErrorKind::HandleOnionClient).into()))
973+
} else {
974+
Either::B( future::err(
975+
HandlePacketError::from(HandlePacketErrorKind::OnionClient)
976+
))
977+
}
978+
}
979+
954980
/// Handle received `DhtRequest` packet, redirect it if it's sent for
955981
/// someone else or parse it and handle the payload if it's sent for us.
956982
fn handle_dht_req(&self, packet: DhtRequest, addr: SocketAddr)
@@ -1434,6 +1460,11 @@ impl Server {
14341460
self.net_crypto = Some(net_crypto);
14351461
}
14361462

1463+
/// Set `onion_client` module.
1464+
pub fn set_onion_client(&mut self, onion_client: OnionClient) {
1465+
self.onion_client = Some(Box::new(onion_client));
1466+
}
1467+
14371468
/// Set sink to send friend's `SocketAddr` when it gets known.
14381469
pub fn set_friend_saddr_sink(&mut self, friend_saddr_sink: mpsc::UnboundedSender<PackedNode>) {
14391470
self.friend_saddr_sink = Some(friend_saddr_sink);
@@ -3626,7 +3657,7 @@ mod tests {
36263657
}
36273658

36283659
#[test]
3629-
fn handle_onion_data_response() {
3660+
fn handle_onion_data_response_uninitialized() {
36303661
let (alice, _precomp, _bob_pk, _bob_sk, _rx, addr) = create_node();
36313662

36323663
let data = Packet::OnionDataResponse(OnionDataResponse {
@@ -3637,11 +3668,11 @@ mod tests {
36373668

36383669
let res = alice.handle_packet(data, addr).wait();
36393670
assert!(res.is_err());
3640-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::NotHandled);
3671+
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::OnionClient);
36413672
}
36423673

36433674
#[test]
3644-
fn handle_onion_announce_response() {
3675+
fn handle_onion_announce_response_uninitialized() {
36453676
let (alice, precomp, _bob_pk, _bob_sk, _rx, addr) = create_node();
36463677

36473678
let payload = OnionAnnounceResponsePayload {
@@ -3656,7 +3687,7 @@ mod tests {
36563687

36573688
let res = alice.handle_packet(data, addr).wait();
36583689
assert!(res.is_err());
3659-
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::NotHandled);
3690+
assert_eq!(*res.err().unwrap().kind(), HandlePacketErrorKind::OnionClient);
36603691
}
36613692

36623693
#[test]

0 commit comments

Comments
 (0)