|
| 1 | +use crate::dht::ip_port::IsGlobal; |
| 2 | +use crate::dht::server::Server as DhtServer; |
| 3 | +use crate::dht::server::errors::*; |
| 4 | +use crate::net_crypto::NetCrypto; |
| 5 | +use crate::onion::client::OnionClient; |
| 6 | +use failure::Fail; |
| 7 | +use tox_packet::dht::*; |
| 8 | +use tox_packet::onion::*; |
| 9 | +use std::net::SocketAddr; |
| 10 | + |
| 11 | +/// UDP server that handles DHT, onion and net_crypto packets. Onion and |
| 12 | +/// net_crypro handlers are optional since appropriate packets are not handled |
| 13 | +/// when operating in DHT server mode. |
| 14 | +pub struct Server { |
| 15 | + /// DHT server. |
| 16 | + pub dht: DhtServer, |
| 17 | + /// Onion client that handles `OnionDataResponse` and |
| 18 | + /// `OnionAnnounceResponse` packets. It can be `None` in case of pure |
| 19 | + /// bootstrap server. |
| 20 | + onion_client: Option<OnionClient>, |
| 21 | + /// Net crypto module that handles `CookieRequest`, `CookieResponse`, |
| 22 | + /// `CryptoHandshake` and `CryptoData` packets. It can be `None` in case of |
| 23 | + /// pure bootstrap server when we don't have friends and therefore don't |
| 24 | + /// have to handle related packets. |
| 25 | + net_crypto: Option<NetCrypto>, |
| 26 | +} |
| 27 | + |
| 28 | +impl Server { |
| 29 | + /// Create new `Server` instance. |
| 30 | + pub fn new(dht: DhtServer) -> Self { |
| 31 | + Self { |
| 32 | + dht, |
| 33 | + onion_client: None, |
| 34 | + net_crypto: None, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Function to handle incoming packets and send responses if necessary. |
| 39 | + pub async fn handle_packet(&self, packet: Packet, addr: SocketAddr) -> Result<(), HandlePacketError> { |
| 40 | + match packet { |
| 41 | + Packet::PingRequest(packet) => |
| 42 | + self.dht.handle_ping_req(packet, addr).await, |
| 43 | + Packet::PingResponse(packet) => |
| 44 | + self.dht.handle_ping_resp(packet, addr).await, |
| 45 | + Packet::NodesRequest(packet) => |
| 46 | + self.dht.handle_nodes_req(packet, addr).await, |
| 47 | + Packet::NodesResponse(packet) => |
| 48 | + self.dht.handle_nodes_resp(packet, addr).await, |
| 49 | + Packet::CookieRequest(packet) => |
| 50 | + self.handle_cookie_request(&packet, addr).await, |
| 51 | + Packet::CookieResponse(packet) => |
| 52 | + self.handle_cookie_response(&packet, addr).await, |
| 53 | + Packet::CryptoHandshake(packet) => |
| 54 | + self.handle_crypto_handshake(&packet, addr).await, |
| 55 | + Packet::DhtRequest(packet) => |
| 56 | + self.dht.handle_dht_req(packet, addr).await, |
| 57 | + Packet::LanDiscovery(packet) => |
| 58 | + self.dht.handle_lan_discovery(&packet, addr).await, |
| 59 | + Packet::OnionRequest0(packet) => |
| 60 | + self.dht.handle_onion_request_0(packet, addr).await, |
| 61 | + Packet::OnionRequest1(packet) => |
| 62 | + self.dht.handle_onion_request_1(packet, addr).await, |
| 63 | + Packet::OnionRequest2(packet) => |
| 64 | + self.dht.handle_onion_request_2(packet, addr).await, |
| 65 | + Packet::OnionAnnounceRequest(packet) => |
| 66 | + self.dht.handle_onion_announce_request(packet, addr).await, |
| 67 | + Packet::OnionDataRequest(packet) => |
| 68 | + self.dht.handle_onion_data_request(packet).await, |
| 69 | + Packet::OnionResponse3(packet) => |
| 70 | + self.dht.handle_onion_response_3(packet).await, |
| 71 | + Packet::OnionResponse2(packet) => |
| 72 | + self.dht.handle_onion_response_2(packet).await, |
| 73 | + Packet::OnionResponse1(packet) => |
| 74 | + self.dht.handle_onion_response_1(packet).await, |
| 75 | + Packet::BootstrapInfo(packet) => |
| 76 | + self.dht.handle_bootstrap_info(&packet, addr).await, |
| 77 | + Packet::CryptoData(packet) => |
| 78 | + self.handle_crypto_data(&packet, addr).await, |
| 79 | + Packet::OnionDataResponse(packet) => |
| 80 | + self.handle_onion_data_response(&packet).await, |
| 81 | + Packet::OnionAnnounceResponse(packet) => |
| 82 | + self.handle_onion_announce_response(&packet, addr).await, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Handle received `OnionDataResponse` packet and pass it to `onion_client` module. |
| 87 | + async fn handle_onion_data_response(&self, packet: &OnionDataResponse) -> Result<(), HandlePacketError> { |
| 88 | + if let Some(ref onion_client) = self.onion_client { |
| 89 | + onion_client.handle_data_response(packet).await |
| 90 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleOnionClient).into()) |
| 91 | + } else { |
| 92 | + Err(HandlePacketError::from(HandlePacketErrorKind::OnionClient)) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Handle received `OnionAnnounceResponse` packet and pass it to `onion_client` module. |
| 97 | + async fn handle_onion_announce_response(&self, packet: &OnionAnnounceResponse, addr: SocketAddr) -> Result<(), HandlePacketError> { |
| 98 | + if let Some(ref onion_client) = self.onion_client { |
| 99 | + onion_client.handle_announce_response(packet, IsGlobal::is_global(&addr.ip())).await |
| 100 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleOnionClient).into()) |
| 101 | + } else { |
| 102 | + Err(HandlePacketError::from(HandlePacketErrorKind::OnionClient)) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// Set `onion_client` module. |
| 107 | + pub fn set_onion_client(&mut self, onion_client: OnionClient) { |
| 108 | + self.onion_client = Some(onion_client); |
| 109 | + } |
| 110 | + |
| 111 | + /// Handle received `CookieRequest` packet and pass it to `net_crypto` |
| 112 | + /// module. |
| 113 | + pub async fn handle_cookie_request(&self, packet: &CookieRequest, addr: SocketAddr) |
| 114 | + -> Result<(), HandlePacketError> { |
| 115 | + if let Some(ref net_crypto) = self.net_crypto { |
| 116 | + net_crypto.handle_udp_cookie_request(packet, addr).await |
| 117 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleNetCrypto).into()) |
| 118 | + } else { |
| 119 | + Err( |
| 120 | + HandlePacketError::from(HandlePacketErrorKind::NetCrypto) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// Handle received `CookieResponse` packet and pass it to `net_crypto` |
| 126 | + /// module. |
| 127 | + pub async fn handle_cookie_response(&self, packet: &CookieResponse, addr: SocketAddr) |
| 128 | + -> Result<(), HandlePacketError> { |
| 129 | + if let Some(ref net_crypto) = self.net_crypto { |
| 130 | + net_crypto.handle_udp_cookie_response(packet, addr).await |
| 131 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleNetCrypto).into()) |
| 132 | + } else { |
| 133 | + Err(HandlePacketError::from(HandlePacketErrorKind::NetCrypto)) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// Handle received `CryptoHandshake` packet and pass it to `net_crypto` |
| 138 | + /// module. |
| 139 | + pub async fn handle_crypto_handshake(&self, packet: &CryptoHandshake, addr: SocketAddr) |
| 140 | + -> Result<(), HandlePacketError> { |
| 141 | + if let Some(ref net_crypto) = self.net_crypto { |
| 142 | + net_crypto.handle_udp_crypto_handshake(packet, addr).await |
| 143 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleNetCrypto).into()) |
| 144 | + } else { |
| 145 | + Err(HandlePacketError::from(HandlePacketErrorKind::NetCrypto)) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /// Handle received `CryptoData` packet and pass it to `net_crypto` module. |
| 150 | + pub async fn handle_crypto_data(&self, packet: &CryptoData, addr: SocketAddr) -> Result<(), HandlePacketError> { |
| 151 | + if let Some(ref net_crypto) = self.net_crypto { |
| 152 | + net_crypto.handle_udp_crypto_data(packet, addr).await |
| 153 | + .map_err(|e| e.context(HandlePacketErrorKind::HandleNetCrypto).into()) |
| 154 | + } else { |
| 155 | + Err(HandlePacketError::from(HandlePacketErrorKind::NetCrypto)) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// Set `net_crypto` module. |
| 160 | + pub fn set_net_crypto(&mut self, net_crypto: NetCrypto) { |
| 161 | + self.net_crypto = Some(net_crypto); |
| 162 | + } |
| 163 | +} |
0 commit comments