|
| 1 | +/*! File transfer packets |
| 2 | +*/ |
| 3 | + |
| 4 | +use nom::{le_u8, be_u32, be_u64}; |
| 5 | + |
| 6 | +use crate::toxcore::crypto_core::*; |
| 7 | +use crate::toxcore::binary_io::*; |
| 8 | + |
| 9 | +mod file_control; |
| 10 | +mod file_data; |
| 11 | +mod file_send_request; |
| 12 | + |
| 13 | +pub use self::file_control::*; |
| 14 | +pub use self::file_data::*; |
| 15 | +pub use self::file_send_request::*; |
| 16 | + |
| 17 | +/// Maximum size in bytes of chunk of file data |
| 18 | +const MAX_FILE_DATA_SIZE: usize = 1371; |
| 19 | + |
| 20 | +/// Whether I am a sender or receiver of file data packet |
| 21 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 22 | +pub enum TransferDirection { |
| 23 | + /// I am a sender |
| 24 | + Send = 0, |
| 25 | + /// I am a receiver |
| 26 | + Receive |
| 27 | +} |
| 28 | + |
| 29 | +impl FromBytes for TransferDirection { |
| 30 | + named!(from_bytes<TransferDirection>, |
| 31 | + switch!(le_u8, |
| 32 | + 0 => value!(TransferDirection::Send) | |
| 33 | + 1 => value!(TransferDirection::Receive) |
| 34 | + ) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/// Control types for transferring file data |
| 39 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 40 | +pub enum ControlType { |
| 41 | + /// Accept a request of transferring file from a peer |
| 42 | + Accept, |
| 43 | + /// Pause transferring |
| 44 | + Pause , |
| 45 | + /// Stop transferring and quit session |
| 46 | + Kill, |
| 47 | + /// Seek to position of file stream and holds seek parameter |
| 48 | + Seek(u64) |
| 49 | +} |
| 50 | + |
| 51 | +impl ToBytes for ControlType { |
| 52 | + fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> { |
| 53 | + match self { |
| 54 | + ControlType::Accept => do_gen!(buf, gen_be_u8!(0x00)), |
| 55 | + ControlType::Pause => do_gen!(buf, gen_be_u8!(0x01)), |
| 56 | + ControlType::Kill => do_gen!(buf, gen_be_u8!(0x02)), |
| 57 | + ControlType::Seek(seek_param) => do_gen!(buf, |
| 58 | + gen_be_u8!(0x03) >> |
| 59 | + gen_be_u64!(*seek_param)) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl FromBytes for ControlType { |
| 65 | + named!(from_bytes<ControlType>, |
| 66 | + switch!(le_u8, |
| 67 | + 0 => value!(ControlType::Accept) | |
| 68 | + 1 => value!(ControlType::Pause) | |
| 69 | + 2 => value!(ControlType::Kill) | |
| 70 | + 3 => do_parse!( |
| 71 | + seek_param: be_u64 >> |
| 72 | + (ControlType::Seek(seek_param))) |
| 73 | + ) |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/// Type of file to transfer |
| 78 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 79 | +pub enum FileType { |
| 80 | + /// Normal data file. |
| 81 | + Data = 0, |
| 82 | + /// Avatar image file. |
| 83 | + Avatar, |
| 84 | +} |
| 85 | + |
| 86 | +/// Maximum file name size in bytes |
| 87 | +const MAX_FILESEND_FILENAME_LENGTH: usize = 255; |
| 88 | + |
| 89 | +impl FromBytes for FileType { |
| 90 | + named!(from_bytes<FileType>, |
| 91 | + switch!(be_u32, |
| 92 | + 0 => value!(FileType::Data) | |
| 93 | + 1 => value!(FileType::Avatar) |
| 94 | + ) |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +const FILE_UID_BYTES: usize = 32; |
| 99 | + |
| 100 | +/// A type for random 32 bytes which is used as file unique id. |
| 101 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 102 | +pub struct FileUID([u8; FILE_UID_BYTES]); |
| 103 | + |
| 104 | +impl FileUID { |
| 105 | + /// Create new object |
| 106 | + pub fn new() -> FileUID { |
| 107 | + let mut array = [0; FILE_UID_BYTES]; |
| 108 | + randombytes_into(&mut array); |
| 109 | + FileUID(array) |
| 110 | + } |
| 111 | + |
| 112 | + fn from_slice(bs: &[u8]) -> Option<FileUID> { |
| 113 | + if bs.len() != FILE_UID_BYTES { |
| 114 | + return None |
| 115 | + } |
| 116 | + let mut n = [0; FILE_UID_BYTES]; |
| 117 | + n.clone_from_slice(bs); |
| 118 | + |
| 119 | + Some(FileUID(n)) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl FromBytes for FileUID { |
| 124 | + named!(from_bytes<FileUID>, map_opt!(take!(FILE_UID_BYTES), FileUID::from_slice)); |
| 125 | +} |
| 126 | + |
| 127 | +/** FileTransfer packet enum that encapsulates all types of FileTransfer packets. |
| 128 | +*/ |
| 129 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 130 | +pub enum Packet { |
| 131 | + /// [`FileControl`](./struct.FileControl.html) structure. |
| 132 | + FileControl(FileControl), |
| 133 | + /// [`FileData`](./struct.FileData.html) structure. |
| 134 | + FileData(FileData), |
| 135 | + /// [`FileSendRequest`](./struct.FileSendRequest.html) structure. |
| 136 | + FileSendRequest(FileSendRequest), |
| 137 | +} |
| 138 | + |
| 139 | +impl ToBytes for Packet { |
| 140 | + fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> { |
| 141 | + match *self { |
| 142 | + Packet::FileControl(ref p) => p.to_bytes(buf), |
| 143 | + Packet::FileData(ref p) => p.to_bytes(buf), |
| 144 | + Packet::FileSendRequest(ref p) => p.to_bytes(buf), |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl FromBytes for Packet { |
| 150 | + named!(from_bytes<Packet>, alt!( |
| 151 | + map!(FileControl::from_bytes, Packet::FileControl) | |
| 152 | + map!(FileData::from_bytes, Packet::FileData) | |
| 153 | + map!(FileSendRequest::from_bytes, Packet::FileSendRequest) |
| 154 | + )); |
| 155 | +} |
| 156 | + |
| 157 | +#[cfg(test)] |
| 158 | +mod tests { |
| 159 | + use super::*; |
| 160 | + |
| 161 | + encode_decode_test!( |
| 162 | + packet_file_control_encode_decode, |
| 163 | + Packet::FileControl(FileControl::new(TransferDirection::Send, 1, ControlType::Seek(100))) |
| 164 | + ); |
| 165 | + |
| 166 | + encode_decode_test!( |
| 167 | + packet_file_data_encode_decode, |
| 168 | + Packet::FileData(FileData::new(1, vec![1,2,3,4])) |
| 169 | + ); |
| 170 | + |
| 171 | + encode_decode_test!( |
| 172 | + packet_file_send_request_encode_decode, |
| 173 | + Packet::FileSendRequest(FileSendRequest::new(1, FileType::Avatar, 4, FileUID::new(), "data".to_string())) |
| 174 | + ); |
| 175 | +} |
0 commit comments