From b35a7b90949e19afca22c8cf5699bdc112ae2f6a Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sun, 24 Mar 2019 16:33:33 +0100 Subject: Support blksize2 option (non-standard) --- README | 3 +++ src/lib.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/README b/README index 08b08c7..44d584b 100644 --- a/README +++ b/README @@ -10,6 +10,9 @@ Currently supported: - RFC 2348 (Blocksize Option) - RFC 2349 (Timeout Interval and Transfer Size Options) +Non-standard options: +- blksize2: block size as a power of 2 + Use cargo to build the binaries (output dir is target/release/): $ cargo build --release diff --git a/src/lib.rs b/src/lib.rs index 9299eea..ec092a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,20 @@ fn octet_to_netascii(buf: &[u8]) -> Vec { out } +fn blksize2(mut size: usize) -> usize { + if size == 0 { + return 0; + } + + let mut msb = 0; + while size > 0 { + size >>= 1; + msb += 1; + } + 1 << (msb - 1) +} + + impl Default for Tftp { fn default() -> Tftp { Tftp { @@ -309,6 +323,14 @@ impl Tftp { } _ => false, }, + "blksize2" => match val.parse() { + Ok(b) if b >= 8 && b <= 32768 => { + /* select 2^x lower or equal the requested size */ + self.options.blksize = blksize2(b); + true + } + _ => false, + }, "timeout" => match val.parse() { Ok(t) if t >= 1 => { self.options.timeout = t; @@ -663,4 +685,13 @@ mod tests { assert_eq!(octet_to_netascii(b"\r\0\r\n"), b"\r\0\0\r\0\r\n"); assert_eq!(octet_to_netascii(b""), b""); } + + #[test] + fn test_blksize2() { + assert_eq!(blksize2(16), 16); + assert_eq!(blksize2(17), 16); + assert_eq!(blksize2(15), 8); + assert_eq!(blksize2(1), 1); + assert_eq!(blksize2(0), 0); + } } -- cgit v1.2.3