@@ -27,6 +27,7 @@ use crate::toxcore::dht::packed_node::*;
2727use crate :: toxcore:: dht:: kbucket:: * ;
2828use crate :: toxcore:: dht:: ktree:: * ;
2929use crate :: toxcore:: dht:: precomputed_cache:: * ;
30+ use crate :: toxcore:: onion:: client:: * ;
3031use crate :: toxcore:: onion:: packet:: * ;
3132use crate :: toxcore:: onion:: onion_announce:: * ;
3233use 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