channel.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. fmt,
  20. sync::{
  21. atomic::{AtomicBool, Ordering::SeqCst},
  22. Arc,
  23. },
  24. time::UNIX_EPOCH,
  25. };
  26. use darkfi_serial::{async_trait, serialize, SerialDecodable, SerialEncodable};
  27. use log::{debug, error, info};
  28. use rand::{rngs::OsRng, Rng};
  29. use smol::{
  30. io::{self, ReadHalf, WriteHalf},
  31. lock::Mutex,
  32. Executor,
  33. };
  34. use url::Url;
  35. use super::{
  36. dnet::{self, dnetev, DnetEvent},
  37. hosts::HostColor,
  38. message,
  39. message::Packet,
  40. message_subscriber::{MessageSubscription, MessageSubsystem},
  41. p2p::P2pPtr,
  42. session::{Session, SessionBitFlag, SessionWeakPtr},
  43. transport::PtStream,
  44. };
  45. use crate::{
  46. system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
  47. util::time::NanoTimestamp,
  48. Error, Result,
  49. };
  50. /// Atomic pointer to async channel
  51. pub type ChannelPtr = Arc<Channel>;
  52. /// Channel debug info
  53. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  54. pub struct ChannelInfo {
  55. pub resolve_addr: Option<Url>,
  56. pub connect_addr: Url,
  57. pub id: u32,
  58. }
  59. impl ChannelInfo {
  60. fn new(resolve_addr: Option<Url>, connect_addr: Url) -> Self {
  61. Self { resolve_addr, connect_addr, id: OsRng.gen() }
  62. }
  63. }
  64. /// Async channel for communication between nodes.
  65. pub struct Channel {
  66. /// The reading half of the transport stream
  67. reader: Mutex<ReadHalf<Box<dyn PtStream>>>,
  68. /// The writing half of the transport stream
  69. writer: Mutex<WriteHalf<Box<dyn PtStream>>>,
  70. /// The message subsystem instance for this channel
  71. message_subsystem: MessageSubsystem,
  72. /// Subscriber listening for stop signal for closing this channel
  73. stop_subscriber: SubscriberPtr<Error>,
  74. /// Task that is listening for the stop signal
  75. receive_task: StoppableTaskPtr,
  76. /// A boolean marking if this channel is stopped
  77. stopped: AtomicBool,
  78. /// Weak pointer to respective session
  79. session: SessionWeakPtr,
  80. /// Channel debug info
  81. pub info: ChannelInfo,
  82. }
  83. impl Channel {
  84. /// Sets up a new channel. Creates a reader and writer [`PtStream`] and
  85. /// the message subscriber subsystem. Performs a network handshake on the
  86. /// subsystem dispatchers.
  87. pub async fn new(
  88. stream: Box<dyn PtStream>,
  89. resolve_addr: Option<Url>,
  90. connect_addr: Url,
  91. session: SessionWeakPtr,
  92. ) -> Arc<Self> {
  93. let (reader, writer) = io::split(stream);
  94. let reader = Mutex::new(reader);
  95. let writer = Mutex::new(writer);
  96. let message_subsystem = MessageSubsystem::new();
  97. Self::setup_dispatchers(&message_subsystem).await;
  98. let info = ChannelInfo::new(resolve_addr, connect_addr.clone());
  99. Arc::new(Self {
  100. reader,
  101. writer,
  102. message_subsystem,
  103. stop_subscriber: Subscriber::new(),
  104. receive_task: StoppableTask::new(),
  105. stopped: AtomicBool::new(false),
  106. session,
  107. info,
  108. })
  109. }
  110. /// Perform network handshake for message subsystem dispatchers.
  111. async fn setup_dispatchers(subsystem: &MessageSubsystem) {
  112. subsystem.add_dispatch::<message::VersionMessage>().await;
  113. subsystem.add_dispatch::<message::VerackMessage>().await;
  114. subsystem.add_dispatch::<message::PingMessage>().await;
  115. subsystem.add_dispatch::<message::PongMessage>().await;
  116. subsystem.add_dispatch::<message::GetAddrsMessage>().await;
  117. subsystem.add_dispatch::<message::AddrsMessage>().await;
  118. }
  119. /// Starts the channel. Runs a receive loop to start receiving messages
  120. /// or handles a network failure.
  121. pub fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) {
  122. debug!(target: "net::channel::start()", "START {:?}", self);
  123. let self_ = self.clone();
  124. self.receive_task.clone().start(
  125. self.clone().main_receive_loop(),
  126. |result| self_.handle_stop(result),
  127. Error::ChannelStopped,
  128. executor,
  129. );
  130. debug!(target: "net::channel::start()", "END {:?}", self);
  131. }
  132. /// Stops the channel.
  133. /// Notifies all subscribers that the channel has been closed in `handle_stop()`.
  134. pub async fn stop(&self) {
  135. debug!(target: "net::channel::stop()", "START {:?}", self);
  136. self.receive_task.stop().await;
  137. debug!(target: "net::channel::stop()", "END {:?}", self);
  138. }
  139. /// Creates a subscription to a stopped signal.
  140. /// If the channel is stopped then this will return a ChannelStopped error.
  141. pub async fn subscribe_stop(&self) -> Result<Subscription<Error>> {
  142. debug!(target: "net::channel::subscribe_stop()", "START {:?}", self);
  143. if self.is_stopped() {
  144. return Err(Error::ChannelStopped)
  145. }
  146. let sub = self.stop_subscriber.clone().subscribe().await;
  147. debug!(target: "net::channel::subscribe_stop()", "END {:?}", self);
  148. Ok(sub)
  149. }
  150. pub fn is_stopped(&self) -> bool {
  151. self.stopped.load(SeqCst)
  152. }
  153. /// Sends a message across a channel. Calls `send_message` that creates
  154. /// a new payload and sends it over the network transport as a packet.
  155. /// Returns an error if something goes wrong.
  156. pub async fn send<M: message::Message>(&self, message: &M) -> Result<()> {
  157. debug!(
  158. target: "net::channel::send()", "[START] command={} {:?}",
  159. M::NAME, self,
  160. );
  161. if self.is_stopped() {
  162. return Err(Error::ChannelStopped)
  163. }
  164. // Catch failure and stop channel, return a net error
  165. if let Err(e) = self.send_message(message).await {
  166. error!(
  167. target: "net::channel::send()", "[P2P] Channel send error for [{:?}]: {}",
  168. self, e
  169. );
  170. self.stop().await;
  171. return Err(Error::ChannelStopped)
  172. }
  173. debug!(
  174. target: "net::channel::send()", "[END] command={} {:?}",
  175. M::NAME, self
  176. );
  177. Ok(())
  178. }
  179. /// Implements send message functionality. Creates a new payload and
  180. /// encodes it. Then creates a message packet (the base type of the
  181. /// network) and copies the payload into it. Then we send the packet
  182. /// over the network stream.
  183. async fn send_message<M: message::Message>(&self, message: &M) -> Result<()> {
  184. let packet = Packet { command: M::NAME.to_string(), payload: serialize(message) };
  185. dnetev!(self, SendMessage, {
  186. chan: self.info.clone(),
  187. cmd: packet.command.clone(),
  188. time: NanoTimestamp::current_time(),
  189. });
  190. let stream = &mut *self.writer.lock().await;
  191. let _ = message::send_packet(stream, packet).await?;
  192. Ok(())
  193. }
  194. /// Subscribe to a message on the message subsystem.
  195. pub async fn subscribe_msg<M: message::Message>(&self) -> Result<MessageSubscription<M>> {
  196. debug!(
  197. target: "net::channel::subscribe_msg()", "[START] command={} {:?}",
  198. M::NAME, self
  199. );
  200. let sub = self.message_subsystem.subscribe::<M>().await;
  201. debug!(
  202. target: "net::channel::subscribe_msg()", "[END] command={} {:?}",
  203. M::NAME, self
  204. );
  205. sub
  206. }
  207. /// Handle network errors. Panic if error passes silently, otherwise
  208. /// broadcast the error.
  209. async fn handle_stop(self: Arc<Self>, result: Result<()>) {
  210. debug!(target: "net::channel::handle_stop()", "[START] {:?}", self);
  211. self.stopped.store(true, SeqCst);
  212. match result {
  213. Ok(()) => panic!("Channel task should never complete without error status"),
  214. // Send this error to all channel subscribers
  215. Err(e) => {
  216. self.stop_subscriber.notify(Error::ChannelStopped).await;
  217. self.message_subsystem.trigger_error(e).await;
  218. }
  219. }
  220. debug!(target: "net::channel::handle_stop()", "[END] {:?}", self);
  221. }
  222. /// Run the receive loop. Start receiving messages or handle network failure.
  223. async fn main_receive_loop(self: Arc<Self>) -> Result<()> {
  224. debug!(target: "net::channel::main_receive_loop()", "[START] {:?}", self);
  225. // Acquire reader lock
  226. let reader = &mut *self.reader.lock().await;
  227. // Run loop
  228. loop {
  229. let packet = match message::read_packet(reader).await {
  230. Ok(packet) => packet,
  231. Err(err) => {
  232. if Self::is_eof_error(&err) {
  233. info!(
  234. target: "net::channel::main_receive_loop()",
  235. "[P2P] Channel inbound connection {} disconnected",
  236. self.address(),
  237. );
  238. } else {
  239. error!(
  240. target: "net::channel::main_receive_loop()",
  241. "[P2P] Read error on channel {}: {}",
  242. self.address(), err,
  243. );
  244. }
  245. debug!(
  246. target: "net::channel::main_receive_loop()",
  247. "Stopping channel {:?}", self
  248. );
  249. return Err(Error::ChannelStopped)
  250. }
  251. };
  252. dnetev!(self, RecvMessage, {
  253. chan: self.info.clone(),
  254. cmd: packet.command.clone(),
  255. time: NanoTimestamp::current_time(),
  256. });
  257. // Send result to our subscribers
  258. match self.message_subsystem.notify(&packet.command, &packet.payload).await {
  259. Ok(()) => {}
  260. // If we're getting messages without dispatchers, it's spam.
  261. Err(Error::MissingDispatcher) => {
  262. debug!(target: "net::channel::main_receive_loop()", "Stopping channel {:?}", self);
  263. // We will reject further connections from this peer
  264. self.ban(self.address()).await;
  265. return Err(Error::ChannelStopped)
  266. }
  267. Err(_) => unreachable!("You added a new error in notify()"),
  268. }
  269. }
  270. }
  271. /// Ban a malicious peer and stop the channel.
  272. pub async fn ban(&self, peer: &Url) {
  273. debug!(target: "net::channel::ban()", "START {:?}", self);
  274. let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
  275. self.p2p().hosts().move_host(peer, last_seen, HostColor::Black).await.unwrap();
  276. self.stop().await;
  277. debug!(target: "net::channel::ban()", "STOP {:?}", self);
  278. }
  279. /// Returns the relevant socket address for this connection. If this is
  280. /// an outbound connection, the transport-processed resolve_addr will
  281. /// be returned. Otherwise for inbound connections it will default
  282. /// to connect_addr.
  283. pub fn address(&self) -> &Url {
  284. if self.info.resolve_addr.is_some() {
  285. self.info.resolve_addr.as_ref().unwrap()
  286. } else {
  287. &self.info.connect_addr
  288. }
  289. }
  290. /// Returns the socket address that has undergone transport
  291. /// processing, if it exists. Returns None otherwise.
  292. pub fn resolve_addr(&self) -> Option<Url> {
  293. self.info.resolve_addr.clone()
  294. }
  295. /// Return the socket address without transport processing.
  296. pub fn connect_addr(&self) -> &Url {
  297. &self.info.connect_addr
  298. }
  299. /// Returns the inner [`MessageSubsystem`] reference
  300. pub fn message_subsystem(&self) -> &MessageSubsystem {
  301. &self.message_subsystem
  302. }
  303. fn session(&self) -> Arc<dyn Session> {
  304. self.session.upgrade().unwrap()
  305. }
  306. pub fn session_type_id(&self) -> SessionBitFlag {
  307. let session = self.session();
  308. session.type_id()
  309. }
  310. fn p2p(&self) -> P2pPtr {
  311. self.session().p2p()
  312. }
  313. fn is_eof_error(err: &Error) -> bool {
  314. match err {
  315. Error::Io(ioerr) => ioerr == &std::io::ErrorKind::UnexpectedEof,
  316. _ => false,
  317. }
  318. }
  319. }
  320. impl fmt::Debug for Channel {
  321. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  322. write!(f, "<Channel addr='{}' id={}>", self.address(), self.info.id)
  323. }
  324. }