From 33a227420a980fca545d48a716a86e39ce63b3c6 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sat, 21 Aug 2021 09:47:25 +0700 Subject: [PATCH] Udp Layer rewrite Using a keep alive message Restructed udp into modules Using all interfaces broadcast address (ipv4) Using remote discovery if local fails --- crates/core/Cargo.toml | 2 + crates/core/src/bc/model.rs | 2 + crates/core/src/bc_protocol.rs | 27 +- .../connection/{udpconn.rs => _udpconn.rs} | 463 ++++++++-------- .../core/src/bc_protocol/connection/bcconn.rs | 25 +- .../connection/udpconn/aborthandle.rs | 25 + .../connection/udpconn/discover.rs | 520 ++++++++++++++++++ .../src/bc_protocol/connection/udpconn/mod.rs | 326 +++++++++++ .../connection/udpconn/transmit.rs | 222 ++++++++ crates/core/src/bc_protocol/ping.rs | 38 +- crates/core/src/bcmedia/de.rs | 5 +- crates/core/src/bcudp/de.rs | 12 +- crates/core/src/bcudp/model.rs | 4 +- dissector/baichuan.lua | 11 +- dissector/udp.md | 41 +- 15 files changed, 1467 insertions(+), 256 deletions(-) rename crates/core/src/bc_protocol/connection/{udpconn.rs => _udpconn.rs} (69%) create mode 100644 crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs create mode 100644 crates/core/src/bc_protocol/connection/udpconn/discover.rs create mode 100644 crates/core/src/bc_protocol/connection/udpconn/mod.rs create mode 100644 crates/core/src/bc_protocol/connection/udpconn/transmit.rs diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 83db767..5c70db6 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -21,6 +21,8 @@ yaserde_derive = "0.3.16" xml-rs = "0.8" rand = "0.8.4" regex = "1.5.4" +crossbeam-channel = "0.5.1" +get_if_addrs = "0.5.3" [dev-dependencies] assert_matches = "1.5.0" diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index e4f1215..b71bd3b 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -32,6 +32,8 @@ pub const MSG_ID_TALK: u32 = 202; pub const MSG_ID_GET_LED_STATUS: u32 = 208; /// Setting the LED status is done with this ID pub const MSG_ID_SET_LED_STATUS: u32 = 209; +/// UDP Keep alive +pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234; /// An empty password in legacy format pub const EMPTY_LEGACY_PASSWORD: &str = diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index fffe0bb..dfdc604 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -19,6 +19,7 @@ mod time; mod version; use super::RX_TIMEOUT; +use bc::model::*; pub(crate) use connection::*; pub use errors::Error; pub use ledstate::LightState; @@ -63,6 +64,7 @@ impl Credentials { impl Drop for BcCamera { fn drop(&mut self) { + debug!("Dropping camera"); self.disconnect(); } } @@ -98,6 +100,7 @@ impl BcCamera { BcSource::new_udp(&uid, RX_TIMEOUT)? } }; + let conn = match BcConnection::new(source) { Ok(conn) => conn, Err(err) => match err { @@ -110,13 +113,33 @@ impl BcCamera { }; debug!("Success"); - return Ok(Self { + let me = Self { connection: Some(conn), message_num: AtomicU16::new(0), channel_id, logged_in: false, credentials: None, - }); + }; + + if let Some(conn) = &me.connection { + let keep_alive_msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_UDP_KEEP_ALIVE, + channel_id: me.channel_id, + msg_num: me.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }; + + conn.set_keep_alive_msg(keep_alive_msg); + } + + return Ok(me); } Err(Error::Timeout) diff --git a/crates/core/src/bc_protocol/connection/udpconn.rs b/crates/core/src/bc_protocol/connection/_udpconn.rs similarity index 69% rename from crates/core/src/bc_protocol/connection/udpconn.rs rename to crates/core/src/bc_protocol/connection/_udpconn.rs index 736ad9e..8f3db4e 100644 --- a/crates/core/src/bc_protocol/connection/udpconn.rs +++ b/crates/core/src/bc_protocol/connection/_udpconn.rs @@ -4,6 +4,9 @@ use crate::bc::model::*; use crate::bcudp; use crate::bcudp::{model::*, xml::*}; use crate::RX_TIMEOUT; +use crossbeam_channel::{ + unbounded, Receiver, RecvError, RecvTimeoutError, SendError, SendTimeoutError, Sender, +}; use lazy_static::lazy_static; use log::*; use rand::{seq::SliceRandom, thread_rng, Rng}; @@ -16,7 +19,6 @@ use std::io::{BufRead, Error as IoError, Read, Write}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket}; use std::sync::{ atomic::{AtomicBool, AtomicU32, Ordering}, - mpsc::{channel, Receiver, Sender}, Arc, Mutex, }; use std::thread::JoinHandle; @@ -51,11 +53,12 @@ pub struct UdpSource { client_id: i32, camera_id: i32, mtu: u32, - outgoing: Arc>>, - incoming: Arc>>, + + camera_acknowledged: Arc, + client_sent: Arc, + client_wants: Arc, + unacknowledged: Arc>>>, aborter: AbortHandle, - next_to_consume: Arc, - next_send_message: Arc, // Buffered because a single read might not read a whole packet read_buffer: Buffered, write_buffer: Buffered, @@ -67,12 +70,6 @@ struct Buffered { consumed: usize, } -#[derive(Debug)] -struct QueuedMessage { - buf: Vec, - time_last_tried: Option, -} - struct DiscoveryResult { socket: UdpSocket, address: SocketAddr, @@ -103,7 +100,7 @@ impl AbortHandle { } impl UdpSource { - fn get_socket(timeout: Duration) -> Result { + fn get_socket(_timeout: Duration) -> Result { // Select a random port to bind to let mut ports: Vec = (53500..54000).into_iter().collect(); let mut rng = thread_rng(); @@ -116,6 +113,7 @@ impl UdpSource { let socket = UdpSocket::bind(&addrs[..])?; socket.set_read_timeout(Some(timeout))?; socket.set_write_timeout(Some(timeout))?; + // socket.set_nonblocking(true)?; socket.set_broadcast(true)?; Ok(socket) } @@ -276,6 +274,7 @@ impl UdpSource { let mtu = MTU; for p2p_relay in P2P_RELAY_HOSTNAMES.iter() { + debug!("Trying register: {}", p2p_relay); let msg = BcUdp::Discovery(UdpDiscovery { tid, payload: UdpXml { @@ -300,6 +299,18 @@ impl UdpSource { let mut buf = vec![0; mtu as usize]; if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { match BcUdp::deserialize(&buf[0..bytes_read]) { + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + m2c_q_r: Some(m2c_q_r), + .. + }, + })) if m2c_q_r.reg.port == 0 || m2c_q_r.reg.ip.is_empty() => { + // This register is empty + abort.abort(); + break; + } Ok(BcUdp::Discovery(UdpDiscovery { tid: _, payload: @@ -321,10 +332,11 @@ impl UdpSource { } else { abort.abort(); // This p2p server didn't have the data we need try another - continue; + break; } } } + debug!("All registers tried"); Err(Error::ConnectionUnavaliable) } @@ -344,10 +356,12 @@ impl UdpSource { let m2c_q_r = Self::get_register(uid, socket, timeout, tid)?; + debug!("Got this information from the register: {:?}", m2c_q_r); + let register_address = m2c_q_r.reg; let relay_address = m2c_q_r.relay; let log_address = m2c_q_r.log; - let device_address = m2c_q_r.t; + // let device_address = m2c_q_r.t; debug!("Register address found: {:?}", register_address); debug!("Registering this address: {:?}", local_addr); @@ -549,18 +563,17 @@ impl UdpSource { msg.serialize(&mut buf)?; // Just let this retry to max limit as we don't get a reply - let (amount_sent, _) = Self::retrying_send_to( - socket, - &buf[..], - format!("{}:{}", device_address.ip, device_address.port), - )?; + let (amount_sent, _) = + Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?; assert_eq!(amount_sent, buf.len()); - let camera_address: SocketAddr = format!("{}:{}", device_address.ip, device_address.port) + let camera_address: SocketAddr = format!("{}:{}", dev_loc.ip, dev_loc.port) .to_socket_addrs()? .next() .ok_or(Error::ConnectionUnavaliable)?; + socket.connect(camera_address)?; + Ok(DiscoveryResult { socket: socket.try_clone()?, address: camera_address, @@ -597,29 +610,33 @@ impl UdpSource { } pub fn new(uid: &str, timeout: Duration) -> Result { - Self::new_with_remote(uid, timeout, false) + Self::new_with_remote(uid, timeout, true) } fn new_with_remote(uid: &str, timeout: Duration, allow_remote: bool) -> Result { let discovery_result = Self::discover_from_uuid(uid, timeout, allow_remote)?; info!("UID {:?} found at {:?}", uid, discovery_result.address); + let (to_outgoing, outgoing) = unbounded(); + let (incoming, from_incoming) = unbounded(); let me = UdpSource { socket: discovery_result.socket, address: discovery_result.address, client_id: discovery_result.client_id, camera_id: discovery_result.camera_id, mtu: discovery_result.mtu, - incoming: Default::default(), - outgoing: Default::default(), + incoming: from_incoming, + outgoing: to_outgoing, + camera_acknowledged: Default::default(), + unacknowledged: Default::default(), + client_sent: Default::default(), + client_wants: Default::default(), aborter: AbortHandle::new(), - next_to_consume: Arc::new(AtomicU32::new(0)), - next_send_message: Arc::new(AtomicU32::new(0)), read_buffer: Default::default(), write_buffer: Default::default(), }; - me.start_polling()?; + me.start_polling(outgoing, incoming)?; Ok(me) } @@ -634,174 +651,141 @@ impl UdpSource { incoming: self.incoming.clone(), outgoing: self.outgoing.clone(), aborter: self.aborter.clone(), - next_to_consume: self.next_to_consume.clone(), - next_send_message: self.next_send_message.clone(), + camera_acknowledged: self.camera_acknowledged.clone(), + unacknowledged: self.unacknowledged.clone(), + client_sent: self.client_sent.clone(), + client_wants: self.client_wants.clone(), // These are not shared mutable so they don't pollute each other buffer - read_buffer: self.read_buffer.clone(), - write_buffer: self.write_buffer.clone(), + read_buffer: Default::default(), + write_buffer: Default::default(), }) } - fn start_polling(&self) -> IoResult<()> { - self.start_polling_recv()?; - self.start_polling_send()?; + fn start_polling( + &self, + outgoing: Receiver>, + incoming: Sender>, + ) -> IoResult<()> { Ok(()) } - fn handle_ack( - next_to_consume: u32, - camera_id: i32, - incoming: &mut HashMap, - socket: &UdpSocket, - ) { - // Send an acknoledge. - // we want the next packet that's missing after the - // last consumed packet. - let mut next_packet = next_to_consume; - loop { - if incoming.get(&next_packet).is_none() { - break; - } else { - next_packet += 1; - } - } - if next_packet > 0 { - let last_received_contigous_packet = next_packet - 1; - let ack_msg = BcUdp::Ack(UdpAck { - connection_id: camera_id, - packet_id: last_received_contigous_packet, - }); - debug!("Re ack packet: {}", last_received_contigous_packet); - let mut ack_buf = vec![]; - if ack_msg.serialize(&mut ack_buf).is_ok() { - if let Ok(amount_sent) = socket.send(&ack_buf[..]) { - assert_eq!(amount_sent, ack_buf.len()); - } else { - error!("Unable to send acknoledgement"); - } - } else { - error!("Unable to serialize acknoledgement"); - } - } - } - - fn start_polling_recv(&self) -> IoResult<()> { - let thread_aborter = self.aborter.clone(); - let thread_incoming = self.incoming.clone(); - let thread_outgoing = self.outgoing.clone(); + fn start_polling_outgoing(&self, outgoing: Receiver>) -> IoResult<()> { let thread_socket = self.socket.try_clone()?; - let thread_mtu = self.mtu; - let thread_client_id = self.client_id; + let thread_aborter = self.aborter.clone(); let thread_camera_id = self.camera_id; - let thread_next_consumed = self.next_to_consume.clone(); + let thread_client_sent = self.client_sent.clone(); std::thread::spawn(move || { while !thread_aborter.is_aborted() { - debug!("Poll UDP Read"); - // Reciving - let mut read_buf = vec![0_u8; thread_mtu as usize]; - if let Ok(bytes_read) = thread_socket.recv(&mut read_buf[..]) { - match BcUdp::deserialize(&read_buf[0..bytes_read]) { - Ok(BcUdp::Discovery(UdpDiscovery { - payload: - UdpXml { - d2c_disc: Some(D2cDisc { cid, did }), - .. - }, - .. - })) if cid == thread_client_id && did == thread_camera_id => { + match outgoing.recv_timeout(RX_TIMEOUT) { + Ok(msg) => { + let packet_id = thread_client_sent.fetch_add(1, Ordering::Relaxed); + let bcudp_msg = BcUdp::Data(UdpData { + connection_id: thread_camera_id, + packet_id, + payload: msg, + }); + + let mut buf = vec![]; + if let Err(e) = bcudp_msg.serialize(&mut buf) { + error!("Failed to serialize bcudp: {:?}, Err: {:?}", bcudp_msg, e); thread_aborter.abort(); - debug!("Client requested disconnect") + break; } - Ok(BcUdp::Discovery(packet)) => { - warn!("Got unexpected discovery packet: {:?}", packet) - } - Ok(BcUdp::Ack(UdpAck { - connection_id: cid, - packet_id, - })) if cid == thread_client_id => { - // Camera got our message remove older ones from the send queue - debug!("Got acknoledgment of {}", packet_id); - thread_outgoing - .lock() - .unwrap() - .retain(|&k, _| k > packet_id); - // Queue for resend now - thread_outgoing - .lock() - .unwrap() - .iter_mut() - .for_each(|(_, v)| v.time_last_tried = None); - Self::handle_ack( - thread_next_consumed.load(Ordering::Relaxed), - thread_camera_id, - &mut *thread_incoming.lock().unwrap(), - &thread_socket, - ); + if let Err(e) = thread_socket.send(&buf) { + error!("Failed to send udp data: {:?}, Err: {:?}", bcudp_msg, e); + thread_aborter.abort(); + break; } - Ok(BcUdp::Data(UdpData { - connection_id: cid, - packet_id, - payload, - })) if cid == thread_client_id => { - // Got some data add it to our buffer - let consumed_packet = thread_next_consumed.load(Ordering::Relaxed); - if packet_id >= consumed_packet { - debug!("Reciving UDP data packet with ID: {}", packet_id); - thread_incoming.lock().unwrap().insert( - packet_id, - QueuedMessage { - buf: payload, - time_last_tried: None, - }, - ); - - Self::handle_ack( - thread_next_consumed.load(Ordering::Relaxed), - thread_camera_id, - &mut *thread_incoming.lock().unwrap(), - &thread_socket, - ); - } - } - Ok(bcudp) => warn!("Unexpected bcudp received {:?}", bcudp), - Err(e) => error!("Unable to poll from udp socket {:?}", e), + } + Err(RecvTimeoutError::Timeout) => {} + Err(RecvTimeoutError::Disconnected) => { + break; } } } }); - Ok(()) } - fn start_polling_send(&self) -> IoResult<()> { - let thread_aborter = self.aborter.clone(); - let thread_outgoing = self.outgoing.clone(); + fn start_polling_ack(&self) -> IoResult<()> { let thread_socket = self.socket.try_clone()?; + let thread_aborter = self.aborter.clone(); + let thread_unacknowledged = self.unacknowledged.clone(); + std::thread::spawn(move || while !thread_aborter.is_aborted() {}); + Ok(()) + } + + fn start_polling_incoming(&self, incoming: Sender>) -> IoResult<()> { + let mut thread_socket = self.socket.try_clone()?; + let thread_aborter = self.aborter.clone(); + let thread_mtu = self.mtu; + let thread_camera_acknowledged = self.camera_acknowledged.clone(); + let thread_client_wants = self.client_wants.clone(); + let thread_client_id = self.client_id; + let thread_camera_id = self.camera_id; std::thread::spawn(move || { while !thread_aborter.is_aborted() { - // debug!("Poll UDP Send"); - let now = OffsetDateTime::now_utc(); - for (packet_id, message) in thread_outgoing.lock().unwrap().iter_mut() { - let mut should_send = false; - if let Some(&time_last_tried) = message.time_last_tried.as_ref() { - if (now - time_last_tried) >= WAIT_TIME { - should_send = true; + let mut buf = vec![0; thread_mtu as usize]; + if let Ok(new_size) = thread_socket.recv(&mut buf) { + match BcUdp::deserialize(&buf[0..new_size]) { + Ok(bcudp_msg) => { + match bcudp_msg { + BcUdp::Discovery(UdpDiscovery { + payload: + UdpXml { + d2c_disc: Some(D2cDisc { cid, did }), + .. + }, + .. + }) if cid == thread_client_id && did == thread_camera_id => { + thread_aborter.abort(); + debug!("Client requested disconnect") + } + BcUdp::Discovery(packet) => { + debug!("Got unexpected discovery packet: {:?}", packet) + } + BcUdp::Ack(UdpAck { + connection_id, + packet_id, + }) if connection_id == thread_client_id => { + thread_camera_acknowledged + .fetch_max(packet_id, Ordering::Acquire); + } + BcUdp::Data(UdpData { + connection_id: cid, + packet_id, + payload, + }) if cid == thread_client_id => { + if let Ok(_) = thread_client_wants.fetch_update( + Ordering::SeqCst, + Ordering::SeqCst, + |v| { + if v == packet_id { + Some(v + 1) + } else { + None + } + }, + ) { + match incoming.send(payload) { + Ok(msg) => {} + Err(_) => { + error!("Failed to get payload from UDP"); + thread_aborter.abort(); + break; + } + } + } + } + }; } - } else { - should_send = true; - } - if should_send { - debug!( - "Sending message ID {} with payload len {}", - packet_id, - message.buf.len() - ); - message.time_last_tried = Some(now); - if let Err(e) = thread_socket.send(&message.buf[..]) { - error!("Failed to send message on udp: {:?}", e); + Err(e) => { + error!("Cannot deser UDP msg: {:?}", e); + thread_aborter.abort(); + break; } } } @@ -851,35 +835,34 @@ impl Read for UdpSource { impl BufRead for UdpSource { fn fill_buf(&mut self) -> IoResult<&[u8]> { - const CLEAR_CONSUMED_AT: usize = 1024; - // This is a trade off between caching too much dead memory - // and calling the drain method too often - if self.read_buffer.consumed > CLEAR_CONSUMED_AT { - let _ = self - .read_buffer - .buffer - .drain(0..self.read_buffer.consumed) - .collect::>(); - self.read_buffer.consumed = 0; - } - if self.read_buffer.buffer.len() <= self.read_buffer.consumed { - // Get next packet of the read queue - let start_time = OffsetDateTime::now_utc(); - while (start_time - OffsetDateTime::now_utc()) < RX_TIMEOUT { - if let Some(msg) = self - .incoming - .lock() - .unwrap() - .remove(&self.next_to_consume.load(Ordering::Relaxed)) - { - self.next_to_consume.fetch_add(1, Ordering::Relaxed); - self.read_buffer.buffer.extend(msg.buf); - break; - } - } - } - - Ok(&self.read_buffer.buffer.as_slice()[self.read_buffer.consumed..]) + // const CLEAR_CONSUMED_AT: usize = 1024; + // // This is a trade off between caching too much dead memory + // // and calling the drain method too often + // if self.read_buffer.consumed > CLEAR_CONSUMED_AT { + // let _ = self + // .read_buffer + // .buffer + // .drain(0..self.read_buffer.consumed) + // .collect::>(); + // self.read_buffer.consumed = 0; + // } + // if self.read_buffer.buffer.len() <= self.read_buffer.consumed { + // // Get next packet of the read queue + // let start_time = OffsetDateTime::now_utc(); + // while (start_time - OffsetDateTime::now_utc()) < RX_TIMEOUT { + // let mut lock = self.incoming.lock().unwrap(); + // if (*lock).contains_key(&self.next_to_consume.load(Ordering::Relaxed)) { + // let next = self.next_to_consume.fetch_add(1, Ordering::Relaxed); + // debug!("Removing: {}", next); + // if let Some(msg) = lock.remove(&next) { + // self.read_buffer.buffer.extend(msg.buf); + // break; + // } + // } + // } + // } + // + // Ok(&self.read_buffer.buffer.as_slice()[self.read_buffer.consumed..]) } fn consume(&mut self, amt: usize) { @@ -891,53 +874,53 @@ impl BufRead for UdpSource { const UDPDATA_HEADER_SIZE: usize = 20; impl Write for UdpSource { fn write(&mut self, buf: &[u8]) -> IoResult { - if self.aborter.is_aborted() { - return Err(IoError::new( - std::io::ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - self.write_buffer.buffer.extend(buf.to_vec()); - if self.write_buffer.buffer.len() > self.mtu as usize - UDPDATA_HEADER_SIZE { - let _ = self.flush(); - } - Ok(buf.len()) + // if self.aborter.is_aborted() { + // return Err(IoError::new( + // std::io::ErrorKind::ConnectionAborted, + // "Connection dropped", + // )); + // } + // self.write_buffer.buffer.extend(buf.to_vec()); + // if self.write_buffer.buffer.len() > self.mtu as usize - UDPDATA_HEADER_SIZE { + // let _ = self.flush(); + // } + // Ok(buf.len()) } fn flush(&mut self) -> IoResult<()> { - if self.aborter.is_aborted() { - return Err(IoError::new( - std::io::ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - for chunk in self - .write_buffer - .buffer - .chunks(self.mtu as usize - UDPDATA_HEADER_SIZE) - { - let packet_id = self.next_send_message.fetch_add(1, Ordering::Relaxed); - let msg = BcUdp::Data(UdpData { - connection_id: self.camera_id, - packet_id, - payload: chunk.to_vec(), - }); - let mut buf = vec![]; - // If this errors it is unrecoverable - // - // It really shouldn't be able to Err though - msg.serialize(&mut buf) - .expect("Failed to serialize UDP Data"); - debug!("Writing"); - self.outgoing.lock().unwrap().insert( - packet_id, - QueuedMessage { - time_last_tried: None, - buf, - }, - ); - } - self.write_buffer.buffer.clear(); - Ok(()) + // if self.aborter.is_aborted() { + // return Err(IoError::new( + // std::io::ErrorKind::ConnectionAborted, + // "Connection dropped", + // )); + // } + // for chunk in self + // .write_buffer + // .buffer + // .chunks(self.mtu as usize - UDPDATA_HEADER_SIZE) + // { + // let packet_id = self.next_send_message.fetch_add(1, Ordering::Relaxed); + // let msg = BcUdp::Data(UdpData { + // connection_id: self.camera_id, + // packet_id, + // payload: chunk.to_vec(), + // }); + // let mut buf = vec![]; + // // If this errors it is unrecoverable + // // + // // It really shouldn't be able to Err though + // msg.serialize(&mut buf) + // .expect("Failed to serialize UDP Data"); + // debug!("Writing"); + // self.outgoing.lock().unwrap().insert( + // packet_id, + // QueuedMessage { + // time_last_tried: None, + // buf, + // }, + // ); + // } + // self.write_buffer.buffer.clear(); + // Ok(()) } } diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index f1c787c..4f606f2 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -26,6 +26,7 @@ pub struct BcConnection { // and connection for deserialisation and serialistion respectivly encryption_protocol: Arc>, poll_abort: Arc, + keep_alive_msg: Arc>>, } impl BcConnection { @@ -38,8 +39,11 @@ impl BcConnection { let connections_encryption_protocol = encryption_protocol.clone(); let poll_abort = Arc::new(AtomicBool::new(false)); let poll_abort_rx = poll_abort.clone(); - let conn = source.try_clone()?; + let mut conn = source.try_clone()?; + let keep_alive_msg: Arc>> = Arc::new(Mutex::new(None)); + let connections_keep_alive_msg = keep_alive_msg.clone(); let rx_thread = std::thread::spawn(move || { + let keep_alive_encryption_protocol = connections_encryption_protocol.clone(); let mut context = BcContext::new(connections_encryption_protocol); let mut result; loop { @@ -56,6 +60,14 @@ impl BcConnection { } break; } + // Send a udp keep alive if set + if let Ok(lock) = connections_keep_alive_msg.try_lock() { + if let Some(keep_alive_msg) = lock.as_ref() { + let _ = keep_alive_msg + .serialize(&conn, &keep_alive_encryption_protocol.lock().unwrap()); + let _ = conn.flush(); + } + } } }); @@ -65,6 +77,7 @@ impl BcConnection { rx_thread: Some(rx_thread), encryption_protocol, poll_abort, + keep_alive_msg, }) } @@ -92,6 +105,10 @@ impl BcConnection { Ok(()) } + pub fn set_keep_alive_msg(&self, msg: Bc) { + *self.keep_alive_msg.lock().unwrap() = Some(msg); + } + pub fn set_encrypted(&self, value: EncryptionProtocol) { *(self.encryption_protocol.lock().unwrap()) = value; } @@ -124,8 +141,10 @@ impl BcConnection { } } Entry::Vacant(_) => { - debug!("Ignoring uninteresting message ID {}", msg_id); - trace!("Contents: {:?}", response); + if msg_id != MSG_ID_UDP_KEEP_ALIVE { + debug!("Ignoring uninteresting message ID {}", msg_id); + trace!("Contents: {:?}", response); + } } } diff --git a/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs b/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs new file mode 100644 index 0000000..989405e --- /dev/null +++ b/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs @@ -0,0 +1,25 @@ +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +#[derive(Clone)] +pub struct AbortHandle { + aborted: Arc, +} + +impl AbortHandle { + pub fn new() -> Self { + Self { + aborted: Arc::new(AtomicBool::new(false)), + } + } + + pub fn abort(&self) { + self.aborted.store(true, Ordering::Relaxed); + } + + pub fn is_aborted(&self) -> bool { + self.aborted.load(Ordering::Relaxed) + } +} diff --git a/crates/core/src/bc_protocol/connection/udpconn/discover.rs b/crates/core/src/bc_protocol/connection/udpconn/discover.rs new file mode 100644 index 0000000..8e259a4 --- /dev/null +++ b/crates/core/src/bc_protocol/connection/udpconn/discover.rs @@ -0,0 +1,520 @@ +use super::{aborthandle::AbortHandle, Error, Result, MTU, P2P_RELAY_HOSTNAMES, WAIT_TIME}; +use crate::bcudp::{model::*, xml::*}; +use log::*; +use rand::{seq::SliceRandom, thread_rng, Rng}; +use std::io::{Error as IoError, ErrorKind, Result as IoResult}; +use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket}; +use std::time::Duration; +use time::OffsetDateTime; + +pub struct UdpDiscover { + pub client_id: i32, + pub camera_id: i32, + pub mtu: u32, + pub address: SocketAddr, +} + +impl UdpDiscover { + fn retrying_send_to( + socket: &UdpSocket, + buf: &[u8], + addr: T, + ) -> IoResult<(usize, AbortHandle)> { + let addr = addr + .to_socket_addrs()? + .next() + .ok_or_else(|| IoError::new(ErrorKind::NotFound, Error::ConnectionUnavaliable))?; + Self::retrying_send_to_multi(socket, buf, &[addr]) + } + + fn retrying_send_to_multi( + socket: &UdpSocket, + buf: &[u8], + addrs: &[T], + ) -> IoResult<(usize, AbortHandle)> { + let handle = AbortHandle::new(); + + let addrs: Vec = addrs + .iter() + .map(|a| { + a.to_socket_addrs() + .ok() + .and_then(|mut a_iter| a_iter.next()) + }) + .flatten() + .collect(); + + let mut bytes_send = 0; + for addr in addrs.iter() { + bytes_send = socket.send_to(buf, &addr)?; + } + + let thread_buffer = buf.to_vec(); + let thread_handle = handle.clone(); + let thread_socket = socket.try_clone()?; + let thread_addr: Vec = addrs; + + const MAX_RETRIES: usize = 10; + + std::thread::spawn(move || { + std::thread::sleep(WAIT_TIME); + for _ in 0..MAX_RETRIES { + if thread_handle.is_aborted() { + break; + } + for addr in thread_addr.iter() { + let _ = thread_socket.send_to(&thread_buffer[..], &addr); + } + std::thread::sleep(WAIT_TIME); + } + }); + + Ok((bytes_send, handle)) + } + + fn discover_from_uuid_local(socket: &UdpSocket, uid: &str, timeout: Duration) -> Result { + let mut rng = thread_rng(); + // If tid is too large it will overflow during encrypt so we just use a random u8 + let tid: u32 = (rng.gen::()) as u32; + let client_id: i32 = rng.gen(); + let local_addr = socket.local_addr()?; + let port = local_addr.port(); + + let mtu = MTU; + + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2d_c: Some(C2dC { + uid: uid.to_string(), + cli: ClientList { port: port as u32 }, + cid: client_id, + mtu, + debug: false, + os: "MAC".to_string(), + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + let mut broadcasts = vec![Ipv4Addr::BROADCAST]; + for iface in get_if_addrs::get_if_addrs()?.iter() { + if let get_if_addrs::IfAddr::V4(ifacev4) = &iface.addr { + if let Some(broadcast) = ifacev4.broadcast.as_ref() { + broadcasts.push(*broadcast); + } + } + } + let ports: [u16; 2] = [2015, 2018]; + let destinations: Vec<(Ipv4Addr, u16)> = broadcasts + .iter() + .map(|&addr| { + ports + .iter() + .map(|&port| (addr, port)) + .collect::>() + }) + .flatten() + .collect(); + debug!("Broadcasting to: {:?}", destinations); + + let (amount_sent, abort) = Self::retrying_send_to_multi(socket, &buf[..], &destinations)?; + assert_eq!(amount_sent, buf.len()); + + let start_time = OffsetDateTime::now_utc(); + let camera_address; + let camera_id; + loop { + if (OffsetDateTime::now_utc() - start_time) <= timeout { + let mut buf = vec![0; mtu as usize]; + if let Ok((bytes_read, socket_addr)) = socket.recv_from(&mut buf) { + match BcUdp::deserialize(&buf[0..bytes_read]) { + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_c_r: Some(D2cCr { did, cid, .. }), + .. + }, + })) if cid == client_id => { + abort.abort(); + camera_address = socket_addr; + camera_id = did; + break; + } + Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), + Err(_) => debug!( + "Udp Discovery got this unexpected binary: {:?}", + &buf[0..bytes_read] + ), + } + } + } else { + abort.abort(); + return Err(Error::Timeout); + } + } + + // Ensure we didn't forget to abort the sending + abort.abort(); + // Actual connection time! + socket.connect(camera_address)?; + + Ok(UdpDiscover { + address: camera_address, + client_id, + camera_id, + mtu, + }) + } + + fn get_register( + uid: &str, + socket: &UdpSocket, + timeout: Duration, + tid: u32, + // client_id: u32, + ) -> Result { + // let local_addr = socket.local_addr()?; + // let local_port = local_addr.port(); + + let mtu = MTU; + + for p2p_relay in P2P_RELAY_HOSTNAMES.iter() { + debug!("Trying register: {}", p2p_relay); + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2m_q: Some(C2mQ { + uid: uid.to_string(), + os: "MAC".to_string(), + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + let (amount_sent, abort) = + Self::retrying_send_to(socket, &buf[..], format!("{}:9999", p2p_relay))?; + assert_eq!(amount_sent, buf.len()); + + let start_time = OffsetDateTime::now_utc(); + loop { + if (OffsetDateTime::now_utc() - start_time) <= timeout { + let mut buf = vec![0; mtu as usize]; + if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { + match BcUdp::deserialize(&buf[0..bytes_read]) { + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + m2c_q_r: Some(m2c_q_r), + .. + }, + })) if m2c_q_r.reg.port == 0 || m2c_q_r.reg.ip.is_empty() => { + // This register is empty + abort.abort(); + break; + } + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + m2c_q_r: Some(m2c_q_r), + .. + }, + })) => { + abort.abort(); + return Ok(m2c_q_r); + } + Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), + Err(_) => debug!( + "Udp Discovery got this unexpected binary: {:?}", + &buf[0..bytes_read] + ), + } + } + } else { + abort.abort(); + // This p2p server didn't have the data we need try another + break; + } + } + } + debug!("All registers tried"); + Err(Error::ConnectionUnavaliable) + } + + fn discover_from_uuid_remote( + socket: &UdpSocket, + uid: &str, + timeout: Duration, + ) -> Result { + let local_addr = socket.local_addr()?; + let local_port = local_addr.port(); + + let mut rng = thread_rng(); + // If tid is too large it will overflow during encrypt so we just use a random u8 + let tid: u32 = (rng.gen::()) as u32; + let mtu = MTU; + let client_id: i32 = rng.gen(); + + let m2c_q_r = Self::get_register(uid, socket, timeout, tid)?; + + debug!("Got this information from the register: {:?}", m2c_q_r); + + let register_address = m2c_q_r.reg; + let relay_address = m2c_q_r.relay; + let log_address = m2c_q_r.log; + // let device_address = m2c_q_r.t; + + debug!("Register address found: {:?}", register_address); + debug!("Registering this address: {:?}", local_addr); + + let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 }; + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2r_c: Some(C2rC { + uid: uid.to_string(), + cli: IpPort { + ip: "10.251.254.10".to_string(), + port: local_port, + }, + relay: relay_address, + cid: client_id, + family: local_family, + debug: false, + os: "MAC".to_string(), + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + let (amount_sent, abort) = Self::retrying_send_to( + socket, + &buf[..], + format!("{}:{}", register_address.ip, register_address.port), + )?; + assert_eq!(amount_sent, buf.len()); + + let device_sid; + let dev_loc; + let start_time = OffsetDateTime::now_utc(); + loop { + if (OffsetDateTime::now_utc() - start_time) <= timeout { + let mut buf = vec![0; mtu as usize]; + if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { + match BcUdp::deserialize(&buf[0..bytes_read]) { + // Got camera data from register + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_t: Some(R2cT { dev, cid, sid, .. }), + .. + }, + })) if cid == client_id && !dev.ip.is_empty() && dev.port > 0 => { + abort.abort(); + // Make a local request to the camera with the dev info + device_sid = sid; + dev_loc = dev; + break; + } + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_t: Some(R2cT { cid, .. }), + .. + }, + })) if cid == client_id => { + // Got a reply but the ip/port was empty please wait + } + Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), + Err(_) => debug!( + "Udp Discovery got this unexpected binary: {:?}", + &buf[0..bytes_read] + ), + } + } + } else { + abort.abort(); + return Err(Error::Timeout); + } + } + + debug!("Register revealed address as SID: {:?}", device_sid); + debug!("Register revealed address as IP: {:?}", dev_loc); + + // ensure abort + abort.abort(); + + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2d_t: Some(C2dT { + sid: device_sid, + cid: client_id, + mtu, + conn: "local".to_string(), + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + let (amount_sent, abort) = + Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?; + assert_eq!(amount_sent, buf.len()); + + let device_id; + let start_time = OffsetDateTime::now_utc(); + loop { + if (OffsetDateTime::now_utc() - start_time) <= timeout { + let mut buf = vec![0; mtu as usize]; + if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { + match BcUdp::deserialize(&buf[0..bytes_read]) { + // Got camera data from camera + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_t: Some(D2cT { cid, did, .. }), + .. + }, + })) if cid == client_id => { + device_id = did; + break; + } + // Got camera data from camera CFM + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_cfm: Some(D2cCfm { cid, did, .. }), + .. + }, + })) if cid == client_id => { + device_id = did; + break; + } + // Got camera data from camera disc + Ok(BcUdp::Discovery(UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_disc: Some(D2cDisc { cid, did, .. }), + .. + }, + })) if cid == client_id => { + device_id = did; + break; + } + Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), + Err(_) => debug!( + "Udp Discovery got this unexpected binary: {:?}", + &buf[0..bytes_read] + ), + } + } + } else { + abort.abort(); + return Err(Error::Timeout); + } + } + + debug!("Got device ID as: {:?}", device_id); + + // Ensure aborted + abort.abort(); + + // Announce to the log that we will connect locally + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2r_cfm: Some(C2rCfm { + sid: device_sid, + cid: client_id, + did: device_id, + conn: "local".to_string(), + rsp: 0, + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + // Just let this retry to max limit as we don't get a reply + let (amount_sent, _) = Self::retrying_send_to( + socket, + &buf[..], + format!("{}:{}", log_address.ip, log_address.port), + )?; + assert_eq!(amount_sent, buf.len()); + + // Announce a map type connection (I think this means we could connect remotely) + let msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2d_t: Some(C2dT { + sid: device_sid, + cid: client_id, + mtu, + conn: "map".to_string(), + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + msg.serialize(&mut buf)?; + + // Just let this retry to max limit as we don't get a reply + let (amount_sent, _) = + Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?; + assert_eq!(amount_sent, buf.len()); + + let camera_address: SocketAddr = format!("{}:{}", dev_loc.ip, dev_loc.port) + .to_socket_addrs()? + .next() + .ok_or(Error::ConnectionUnavaliable)?; + + socket.connect(camera_address)?; + + Ok(UdpDiscover { + address: camera_address, + client_id, + camera_id: device_id, + mtu, + }) + } + + pub fn discover_from_uuid( + socket: &UdpSocket, + uid: &str, + timeout: Duration, + allow_remote: bool, + ) -> Result { + match Self::discover_from_uuid_local(socket, uid, timeout) { + Err(Error::Timeout) if allow_remote => { + info!("Trying remote discovery against reolink servers"); + Self::discover_from_uuid_remote(socket, uid, timeout) + } + Ok(result) => Ok(result), + Err(e) => Err(e), + } + } +} diff --git a/crates/core/src/bc_protocol/connection/udpconn/mod.rs b/crates/core/src/bc_protocol/connection/udpconn/mod.rs new file mode 100644 index 0000000..f781cd7 --- /dev/null +++ b/crates/core/src/bc_protocol/connection/udpconn/mod.rs @@ -0,0 +1,326 @@ +/// The module will handle a udp connection +/// +/// Because of the complexity it is seperated into modules +/// +/// discover module handles the initial handshake which discovers +/// the ip from the uid and reconnects +/// +/// transmit handles the sending and recieving of data through the socket +/// this includes the BcUdp wrapping and the acknoledgements +/// +use super::{Error, Result}; +use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender}; +use lazy_static::lazy_static; +use log::*; +use rand::{seq::SliceRandom, thread_rng, Rng}; +use std::{ + io::{BufRead, Error as IoError, ErrorKind, Read, Result as IoResult, Write}, + net::{SocketAddr, UdpSocket}, + sync::Arc, + time::Duration, +}; +use time::OffsetDateTime; + +mod aborthandle; +mod discover; +mod transmit; + +use aborthandle::*; +use discover::*; +use transmit::*; + +// How long to set the socket wait +// This is kept short so as to not block the read and write parts +const SOCKET_WAIT_TIME: Duration = Duration::from_millis(50); +// How long to wait between retransmits when no reply is recieved +const WAIT_TIME: Duration = Duration::from_millis(500); +// The maximum data size including header +// +// TODO: Maybe use path mtu discovery (although reolinks seems to just use this constant) +const MTU: u32 = 1030; + +lazy_static! { + static ref P2P_RELAY_HOSTNAMES: [&'static str; 10] = [ + "p2p.reolink.com", + "p2p1.reolink.com", + "p2p2.reolink.com", + "p2p3.reolink.com", + "p2p6.reolink.com", + "p2p7.reolink.com", + "p2p8.reolink.com", + "p2p9.reolink.com", + "p2p14.reolink.com", + "p2p15.reolink.com", + ]; +} + +pub struct UdpSource { + outgoing: Sender>, + incoming: Receiver>, + aborter: AbortHandle, + timeout: Duration, + mtu: u32, + + read_buffer: Buffered, + write_buffer: Buffered, +} + +impl UdpSource { + pub fn new(uid: &str, timeout: Duration) -> Result { + let (outgoing, from_outgoing) = unbounded(); + let (to_incoming, incoming) = unbounded(); + let aborter = AbortHandle::new(); + + Self::start_polling(uid, timeout, &aborter, to_incoming, from_outgoing)?; + + Ok(Self { + outgoing, + incoming, + aborter, + timeout, + mtu: MTU, + + read_buffer: Default::default(), + write_buffer: Default::default(), + }) + } + + fn start_polling( + uid: &str, + timeout: Duration, + aborter: &AbortHandle, + to_incoming: Sender>, + from_outgoing: Receiver>, + ) -> Result<()> { + let socket = Self::get_socket(SOCKET_WAIT_TIME)?; + let allow_remote = true; + let discovery_result = Arc::new(UdpDiscover::discover_from_uuid( + &socket, + uid, + timeout, + allow_remote, + )?); + socket.connect(discovery_result.address)?; + let transmit = Arc::new(UdpTransmit::new()); + + let thread_aborter = aborter.clone(); + let thread_transmit = transmit.clone(); + let thread_socket = socket.try_clone()?; + let thread_discovery_result = discovery_result.clone(); + + // Poll Read + std::thread::spawn(move || { + while !thread_aborter.is_aborted() { + if let Err(err) = thread_transmit.poll_read( + &thread_socket, + &(*thread_discovery_result), + &to_incoming, + ) { + if !thread_aborter.is_aborted() { + match err { + TransmitError::Disc => { + error!("Camera requested disconnect"); + thread_aborter.abort(); + } + e => { + error!("Error during Udp Transmit: {:?}", e); + thread_aborter.abort(); + } + } + } + } + } + error!("Udp read poll aborted"); + }); + + let thread_aborter = aborter.clone(); + let thread_socket = socket.try_clone()?; + let thread_transmit = transmit; + let thread_discovery_result = discovery_result; + + // Poll Write + std::thread::spawn(move || { + let mut outgoing_history = Default::default(); + while !thread_aborter.is_aborted() { + if let Err(err) = thread_transmit.poll_write( + &thread_socket, + &(*thread_discovery_result), + &from_outgoing, + &mut outgoing_history, + ) { + if !thread_aborter.is_aborted() { + match err { + TransmitError::Disc => { + error!("Camera requested disconnect"); + thread_aborter.abort(); + } + e => { + error!("Error during Udp Transmit: {:?}", e); + thread_aborter.abort(); + } + } + } + } + } + error!("Udp write poll aborted"); + }); + + Ok(()) + } + + pub fn try_clone(&self) -> IoResult { + Ok(Self { + outgoing: self.outgoing.clone(), + incoming: self.incoming.clone(), + aborter: self.aborter.clone(), + timeout: self.timeout, + mtu: self.mtu, + + // New buffer so they don't pollute each other + read_buffer: Default::default(), + write_buffer: Default::default(), + }) + } + + fn stop_polling(&self) { + self.aborter.abort(); + } + + fn get_socket(timeout: Duration) -> Result { + // Select a random port to bind to + let mut ports: Vec = (53500..54000).into_iter().collect(); + let mut rng = thread_rng(); + ports.shuffle(&mut rng); + + let addrs: Vec<_> = ports + .iter() + .map(|&port| SocketAddr::from(([0, 0, 0, 0], port))) + .collect(); + let socket = UdpSocket::bind(&addrs[..])?; + socket.set_read_timeout(Some(timeout))?; + socket.set_write_timeout(Some(timeout))?; + socket.set_nonblocking(false)?; + socket.set_broadcast(true)?; + Ok(socket) + } +} + +// Ensuring polling stops +impl Drop for UdpSource { + fn drop(&mut self) { + self.stop_polling(); + } +} + +#[derive(Default)] +struct Buffered { + buffer: Vec, + consumed: usize, +} + +impl Read for UdpSource { + fn read(&mut self, buf: &mut [u8]) -> IoResult { + if self.aborter.is_aborted() { + return Err(IoError::new( + ErrorKind::ConnectionAborted, + "Connection dropped", + )); + } + let buffer = self.fill_buf()?; + let amt = std::cmp::min(buf.len(), buffer.len()); + + // First check if the amount of bytes we want to read is small: + // `copy_from_slice` will generally expand to a call to `memcpy`, and + // for a single byte the overhead is significant. + if amt == 1 { + buf[0] = buffer[0]; + } else { + buf[..amt].copy_from_slice(&buffer[..amt]); + } + + self.consume(amt); + + Ok(amt) + } +} + +impl BufRead for UdpSource { + fn fill_buf(&mut self) -> IoResult<&[u8]> { + const CLEAR_CONSUMED_AT: usize = 1024; + // This is a trade off between caching too much dead memory + // and calling the drain method too often + if self.read_buffer.consumed > CLEAR_CONSUMED_AT { + let _ = self + .read_buffer + .buffer + .drain(0..self.read_buffer.consumed) + .collect::>(); + self.read_buffer.consumed = 0; + } + if self.read_buffer.buffer.len() <= self.read_buffer.consumed { + // Get next packet of the read queue + match self.incoming.recv_timeout(self.timeout) { + Ok(buf) => self.read_buffer.buffer.extend(buf), + Err(RecvTimeoutError::Timeout) => { + return Err(IoError::new( + ErrorKind::UnexpectedEof, + "Connection timedout when reading from udp channel", + )); + } + Err(RecvTimeoutError::Disconnected) => { + return Err(IoError::new( + ErrorKind::UnexpectedEof, + "Connection dropped when reading from udp channel", + )); + } + } + } + + Ok(&self.read_buffer.buffer.as_slice()[self.read_buffer.consumed..]) + } + + fn consume(&mut self, amt: usize) { + assert!(self.read_buffer.consumed + amt <= self.read_buffer.buffer.len()); + self.read_buffer.consumed += amt; + } +} + +const UDPDATA_HEADER_SIZE: usize = 20; +impl Write for UdpSource { + fn write(&mut self, buf: &[u8]) -> IoResult { + if self.aborter.is_aborted() { + return Err(IoError::new( + std::io::ErrorKind::ConnectionAborted, + "Connection dropped", + )); + } + self.write_buffer.buffer.extend(buf.to_vec()); + if self.write_buffer.buffer.len() > self.mtu as usize - UDPDATA_HEADER_SIZE { + let _ = self.flush(); + } + Ok(buf.len()) + } + + fn flush(&mut self) -> IoResult<()> { + if self.aborter.is_aborted() { + return Err(IoError::new( + std::io::ErrorKind::ConnectionAborted, + "Connection dropped", + )); + } + for chunk in self + .write_buffer + .buffer + .chunks(self.mtu as usize - UDPDATA_HEADER_SIZE) + { + if self.outgoing.send(chunk.to_vec()).is_err() { + return Err(IoError::new( + std::io::ErrorKind::ConnectionAborted, + "Connection dropped", + )); + } + } + self.write_buffer.buffer.clear(); + Ok(()) + } +} diff --git a/crates/core/src/bc_protocol/connection/udpconn/transmit.rs b/crates/core/src/bc_protocol/connection/udpconn/transmit.rs new file mode 100644 index 0000000..a925e71 --- /dev/null +++ b/crates/core/src/bc_protocol/connection/udpconn/transmit.rs @@ -0,0 +1,222 @@ +/// This module handles the data from Bc->BcUdp->UdpSocket +/// +/// - It recieves Bc as Vec (after serialize) +/// - It wraps it in BcUdp packet +/// - It retransmits when camera dosen't acknoledge +/// - It sends UdpAck to acknoledge recieved packets +/// +use super::{aborthandle::AbortHandle, discover::UdpDiscover, SOCKET_WAIT_TIME, WAIT_TIME}; +use crate::bcudp::{model::*, xml::*}; +use crossbeam_channel::{Receiver, SendError, Sender, TryRecvError}; +use err_derive::Error; +use log::*; +use std::collections::HashMap; +use std::io::{Error as IoError, ErrorKind}; +use std::net::UdpSocket; +use std::sync::atomic::{AtomicU32, Ordering}; +use time::OffsetDateTime; + +#[derive(Debug, Error)] +pub enum TransmitError { + /// Error raised when IO fails such as when the connection is lost + #[error(display = "Io error")] + Io(#[error(source)] IoError), + + /// Error raised when socket recv IO fails such as when the connection is lost + #[error(display = "Socket Recv error")] + SocketRecv { err: std::io::Error }, + + /// Error raised when socker IO fails such as when the connection is lost + #[error(display = "Socket Send error")] + SocketSend { err: std::io::Error }, + + /// Error raised when camera sends a disconnect request + #[error(display = "Camera Requested Disconnected")] + Disc, + + /// Error raised during deserlization + #[error(display = "BcUdp Deserialization error")] + BcUdpDeserialization(#[error(source)] crate::bcudp::de::Error), + + /// Error raised during serlization + #[error(display = "BcUdp Serialization error")] + BcUdpSerialization(#[error(source)] crate::bcudp::ser::Error), + + /// Error raised during send + #[error(display = "BcUdp Send to Channel Failed")] + BcUdpSend(#[error(source)] SendError>), + + /// Error raised during recv + #[error(display = "BcUdp Recv from Channel Failed")] + BcUdpRecv(#[error(source)] TryRecvError), +} + +type Result = std::result::Result; + +pub struct UdpTransmit { + client_wants: AtomicU32, + camera_acknowledged: AtomicU32, + client_sent: AtomicU32, +} + +impl UdpTransmit { + pub fn new() -> Self { + Self { + client_wants: Default::default(), + camera_acknowledged: Default::default(), + client_sent: Default::default(), + } + } + + pub fn poll_read( + &self, + socket: &UdpSocket, + discovery_result: &UdpDiscover, + send_to_incoming: &Sender>, + ) -> Result<()> { + // Handle recv from `socket` and send to `send_to_incoming` + let mut buf = vec![0; discovery_result.mtu as usize]; + match socket.recv(&mut buf) { + Ok(amount_recv) => { + let bcudp_msg = BcUdp::deserialize(&buf[0..amount_recv])?; + match bcudp_msg { + BcUdp::Discovery(UdpDiscovery { + tid, + payload: + UdpXml { + d2c_disc: Some(D2cDisc { cid, did }), + .. + }, + .. + }) if cid == discovery_result.client_id + && did == discovery_result.camera_id => + { + // Reply with out C2D_Disc and end + let bcudp_msg = BcUdp::Discovery(UdpDiscovery { + tid, + payload: UdpXml { + c2d_disc: Some(C2dDisc { + cid: discovery_result.client_id, + did: discovery_result.camera_id, + }), + ..Default::default() + }, + }); + + let mut buf = vec![]; + bcudp_msg.serialize(&mut buf)?; + socket + .send(&buf) + .map_err(|e| TransmitError::SocketSend { err: e })?; + return Err(TransmitError::Disc); + } + BcUdp::Discovery(packet) => { + debug!("Got unexpected discovery packet: {:?}", packet) + } + BcUdp::Ack(UdpAck { + connection_id, + packet_id, + .. + }) if connection_id == discovery_result.client_id => { + self.camera_acknowledged + .fetch_max(packet_id, Ordering::Acquire); + } + BcUdp::Ack(UdpAck { .. }) => { + debug!("Got a UdpAck for another client id"); + } + BcUdp::Data(UdpData { + connection_id: cid, + packet_id, + payload, + }) if cid == discovery_result.client_id => { + // Seperated as let/if due to clippy recommend + trace!("Recieving UdpData packet {}", packet_id); + let updated = self + .client_wants + .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |v| { + if v == packet_id { + Some(v + 1) + } else { + None + } + }) + .is_ok(); + if updated { + send_to_incoming.send(payload)?; + } + } + BcUdp::Data(UdpData { .. }) => { + debug!("Got UdpData for another client id"); + } + } + } + Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => { + // Just a timeout + } + Err(e) => { + // Should stop + return Err(TransmitError::SocketRecv { err: e }); + } + } + Ok(()) + } + + pub fn poll_write( + &self, + socket: &UdpSocket, + discovery_result: &UdpDiscover, + get_from_outgoing: &Receiver>, + outgoing_history: &mut HashMap>, + ) -> Result<()> { + // Handle Retransmit + let camera_has = self.camera_acknowledged.load(Ordering::Relaxed); + let client_sent = self.client_sent.load(Ordering::Relaxed); + if camera_has < client_sent { + for packet_id in camera_has..(client_sent + 1) { + if let Some(payload) = &outgoing_history.get(&packet_id) { + socket + .send(payload) + .map_err(|e| TransmitError::SocketSend { err: e })?; + } + std::thread::sleep(SOCKET_WAIT_TIME); + } + } + + // Handle recv from `send_to_incoming` and send to `socket` + match get_from_outgoing.try_recv() { + Ok(payload) => { + let packet_id = self.client_sent.fetch_add(1, Ordering::Relaxed); + let bcudp_msg = BcUdp::Data(UdpData { + connection_id: discovery_result.camera_id, + packet_id, + payload, + }); + + let mut buf = vec![]; + bcudp_msg.serialize(&mut buf)?; + socket + .send(&buf) + .map_err(|e| TransmitError::SocketSend { err: e })?; + outgoing_history.insert(packet_id, buf); + } + Err(TryRecvError::Empty) => {} + Err(e) => return Err(e.into()), + } + + // Handle acknoledgment + let client_wants = self.client_wants.load(Ordering::Relaxed); + if client_wants > 0 { + let bcack_msg = BcUdp::Ack(UdpAck { + connection_id: discovery_result.camera_id, + packet_id: client_wants - 1, + }); + + let mut buf = vec![]; + bcack_msg.serialize(&mut buf)?; + socket + .send(&buf) + .map_err(|e| TransmitError::SocketSend { err: e })?; + } + Ok(()) + } +} diff --git a/crates/core/src/bc_protocol/ping.rs b/crates/core/src/bc_protocol/ping.rs index 0c63b0e..0ef9331 100644 --- a/crates/core/src/bc_protocol/ping.rs +++ b/crates/core/src/bc_protocol/ping.rs @@ -1,4 +1,4 @@ -use super::{BcCamera, Result, RX_TIMEOUT}; +use super::{BcCamera, Error, Result, RX_TIMEOUT}; use crate::bc::model::*; impl BcCamera { @@ -28,4 +28,40 @@ impl BcCamera { Ok(()) } + + /// Send a ping repeatedly + pub fn ping_every(&self, milis: u64) -> Result<()> { + let wait = std::time::Duration::from_millis(milis); + + let connection = self.connection.as_ref().expect("Must be connected to ping"); + let sub_ping = connection.subscribe(MSG_ID_PING)?; + + loop { + let ping = Bc { + meta: BcMeta { + msg_id: MSG_ID_PING, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }; + + sub_ping.send(ping)?; + + let reply = sub_ping.rx.recv_timeout(RX_TIMEOUT)?; + + if reply.meta.response_code != 200 { + return Err(Error::UnintelligibleReply { + reply, + why: "Camera responded with an error status code to the ping", + }); + } + std::thread::sleep(wait); + } + } } diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index 04aacb8..42ffce8 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -68,7 +68,10 @@ where } } Ok(_) => break, - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + Err(e) + if e.kind() == std::io::ErrorKind::WouldBlock + || e.kind() == std::io::ErrorKind::TimedOut => + { // This is a temporaily unavaliable resource // We should just wait and try again } diff --git a/crates/core/src/bcudp/de.rs b/crates/core/src/bcudp/de.rs index d5f3820..9a52c08 100644 --- a/crates/core/src/bcudp/de.rs +++ b/crates/core/src/bcudp/de.rs @@ -135,9 +135,15 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> { let (buf, connection_id) = le_i32(buf)?; let (buf, _unknown_a) = verify(le_u32, |&x| x == 0)(buf)?; let (buf, _unknown_b) = verify(le_u32, |&x| x == 0)(buf)?; - let (buf, packet_id) = le_u32(buf)?; - let (buf, _unknown_c) = le_u32(buf)?; - let (buf, _unknown_d) = verify(le_u32, |&x| x == 0)(buf)?; + let (buf, packet_id) = le_u32(buf)?; // This is the point at which the camera has contigious + // packets to + let (buf, _unknown) = le_u32(buf)?; + let (buf, payload_size) = le_u32(buf)?; + let (buf, _payload) = take!(buf, payload_size)?; // It is a binary payload of + // `00 01 01 01 01 00 01` + // Probably a truth map of missing packets + // since last contigious packet_id up + // to the last packet we sent and it recieved let data = UdpAck { connection_id, diff --git a/crates/core/src/bcudp/model.rs b/crates/core/src/bcudp/model.rs index 80e11c4..39de3be 100644 --- a/crates/core/src/bcudp/model.rs +++ b/crates/core/src/bcudp/model.rs @@ -54,7 +54,9 @@ pub struct UdpAck { /// The ID of the last data packet [`UdpData`] pub packet_id: u32, // 2 Bytes Unknown: Observed values `00000000`, `d6010000`, `d7160000` `09e00000` - // 2 Bytes Unknown: Observed values `00000000` + // Unknown but seems to change randomly every second + // 2 Bytes size of a payload + // Payload of `00 01 01 01 01` where `01` is added after every repeat } /// Magic for the UDP Data packet diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 65b1a3f..a5f08a6 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -29,6 +29,7 @@ udp_tid = ProtoField.int32("baichuan.udp_tid", "udp_tid", base.DEC) udp_checksum = ProtoField.int32("baichuan.udp_checksum", "udp_checksum", base.DEC) udp_packet_count = ProtoField.int32("baichuan.udp_packet_count", "udp_packet_count", base.DEC) udp_last_ack_packet = ProtoField.int32("baichuan.udp_last_ack_packet", "udp_last_ack_packet", base.DEC) +udp_ack_payload_size = ProtoField.int32("baichuan.udp_ack_payload_size", "ack_payload_size", base.DEC) udp_size = ProtoField.int32("baichuan.udp_size", "udp_size", base.DEC) bc_protocol.fields = { @@ -55,6 +56,7 @@ bc_protocol.fields = { udp_checksum, udp_packet_count, udp_last_ack_packet, + udp_ack_payload_size, udp_size, } @@ -416,6 +418,7 @@ function get_udp_header(buffer) local udp_unknown3 = nil local udp_unknown4 = nil local udp_last_ack_packet = nil + local udp_ack_payload_size = nil local udp_connection_id = nil local udp_packet_count = nil if udp_class == 0x3a then @@ -434,7 +437,7 @@ function get_udp_header(buffer) udp_unknown2 = buffer(12, 4):le_uint() udp_last_ack_packet = buffer(16, 4):le_uint() udp_unknown3 = buffer(20, 4):le_uint() - udp_unknown4 = buffer(24, 4):le_uint() + udp_ack_payload_size = buffer(24, 4):le_uint() elseif udp_class == 0x10 then udp_connection_id = buffer(4, 4):le_uint() udp_unknown1 = buffer(8, 4):le_uint() @@ -455,6 +458,7 @@ function get_udp_header(buffer) connection_id = udp_connection_id, packet_count = udp_packet_count, last_ack_packet = udp_last_ack_packet, + ack_payload_size = udp_ack_payload_size } end @@ -480,7 +484,7 @@ function process_udp_header(buffer, headers_tree) header:add_le(udp_unknown, buffer(12, 4)) header:add_le(udp_last_ack_packet, buffer(16, 4)) header:add_le(udp_unknown, buffer(20, 4)) - header:add_le(udp_unknown, buffer(24, 4)) + header:add_le(udp_ack_payload_size, buffer(24, 4)) elseif header_data.class == 0x10 then header:add_le(udp_connection_id, buffer(4, 4)) header:add_le(udp_unknown, buffer(8, 4)) @@ -687,6 +691,9 @@ function bc_protocol.dissector(buffer, pinfo, tree) elseif udp_header.class == 0x20 then pinfo.cols.protocol = bc_protocol.name .. " UDP ACK" + if udp_header.ack_payload_size > 0 then + tree:add(bc_protocol, buffer(25,udp_header.ack_payload_size), "BcUdp Ack Payload") + end else subbuffer = buffer(udp_header_len, nil) end diff --git a/dissector/udp.md b/dissector/udp.md index 77edf3e..ad263c1 100644 --- a/dissector/udp.md +++ b/dissector/udp.md @@ -253,11 +253,46 @@ These messages are sent to acknowledge receipt of message. They are header only. - 4 Bytes unknown: Always `00000000` for for UDP Ack - 4 Bytes Last Packet ID: Last received packet id - 4 Bytes Unknown: Observed values `00000000`, `d6010000`, `d7160000` `09e00000` **NEEDS INFO** -- 4 Bytes Unknown: Observed values `00000000` +- Payload Size: **NEEDS INFO** +- Paload **NEEDS INFO** **To Investigate:** - - Why does the unknown byte change. It starts at zero and remains that way for a long - time. Then seems to change and remain at the new value for a long time + - Why does the unknown byte change. It starts at zero and remains that way for a second + time. Then seems to change and remain at the new value for another second + - Payload is usually 0 but occasionally it is non zero + - When this happens the payload becomes `00 01 01 01 01 01` always the first + byte `00` then just `01`/`00` bytes + - This payload then increases in size by `01` each ackpacket until a new + udpdata packet arrives. At which point the `Last received packet id` jumps + +Here's an example of a UDP Ack payload (size 203 bytes) + +```hex +0000 00 01 01 01 01 01 01 01 01 01 01 01 00 01 01 01 +0010 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +0020 01 01 01 01 00 01 01 01 01 01 01 01 01 01 01 01 +0030 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +0040 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +0050 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +0060 01 01 01 01 01 01 00 01 01 01 01 01 01 01 01 01 +0070 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +0080 01 01 01 01 01 01 01 01 01 01 00 01 01 01 01 01 +0090 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +00a0 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +00b0 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 +00c0 01 01 01 01 01 01 01 01 01 01 01 01 +``` + +I believe this is a truth table of false/true for missing packets + +In this example the last packet sent was number 221 but the camera only +acknowledged number 14 and sent this binary too and the unknown +value was `f8010000` (504). + +If this payload is allowed to grow to ~ 205 bytes the camera sends a +disconnect request and drops the connection + + # UDP Data