1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io::Cursor;
use std::{fmt, mem, result, str};

#[derive(Debug)]
pub enum PacketErr {
    OverflowSize,
    InvalidOpCode,
    StrOutOfBounds,
    OpCodeOutOfBounds,
    ErrCodeOutOfBounds,
    Utf8Error(str::Utf8Error),
}

impl From<str::Utf8Error> for PacketErr {
    fn from(err: str::Utf8Error) -> PacketErr {
        PacketErr::Utf8Error(err)
    }
}

pub type Result<T> = result::Result<T, PacketErr>;

#[repr(u16)]
#[derive(PartialEq, Clone, Debug)]
pub enum OpCode {
    RRQ = 1,
    WRQ = 2,
    DATA = 3,
    ACK = 4,
    ERROR = 5,
}

impl OpCode {
    pub fn from_u16(i: u16) -> Result<OpCode> {
        if i >= OpCode::RRQ as u16 && i <= OpCode::ERROR as u16 {
            Ok(unsafe { mem::transmute(i) })
        } else {
            Err(PacketErr::OpCodeOutOfBounds)
        }
    }
}

#[repr(u16)]
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum ErrorCode {
    NotDefined = 0,
    FileNotFound = 1,
    AccessViolation = 2,
    DiskFull = 3,
    IllegalTFTP = 4,
    UnknownID = 5,
    FileExists = 6,
    NoUser = 7,
}

impl ErrorCode {
    pub fn from_u16(i: u16) -> Result<ErrorCode> {
        if i >= ErrorCode::NotDefined as u16 && i <= ErrorCode::NoUser as u16 {
            Ok(unsafe { mem::transmute(i) })
        } else {
            Err(PacketErr::ErrCodeOutOfBounds)
        }
    }

    /// Returns the ERROR packet with the error code and
    /// the default description as the error message.
    pub fn to_packet(self) -> Packet {
        Packet::ERROR {
            code: self,
            msg: self.to_string(),
        }
    }
}

impl fmt::Display for ErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match *self {
            ErrorCode::NotDefined => "Not defined, see error message (if any).",
            ErrorCode::FileNotFound => "File not found.",
            ErrorCode::AccessViolation => "Access violation.",
            ErrorCode::DiskFull => "Disk full or allocation exceeded.",
            ErrorCode::IllegalTFTP => "Illegal TFTP operation.",
            ErrorCode::UnknownID => "Unknown transfer ID.",
            ErrorCode::FileExists => "File already exists.",
            ErrorCode::NoUser => "No such user.",
        };
        write!(f, "{}", s)
    }
}

pub const MODES: [&str; 3] = ["netascii", "octet", "mail"];
pub const MAX_PACKET_SIZE: usize = 1024;
pub const MAX_DATA_SIZE: usize = 516;

/// The byte representation of a packet. Because many packets can
/// be smaller than the maximum packet size, it contains a length
/// parameter so that the actual packet size can be determined.
pub struct PacketData {
    bytes: [u8; MAX_PACKET_SIZE],
    len: usize,
}

impl PacketData {
    pub fn new(bytes: [u8; MAX_PACKET_SIZE], len: usize) -> PacketData {
        PacketData { bytes, len }
    }

    /// Returns a byte slice that can be sent through a socket.
    pub fn to_slice(&self) -> &[u8] {
        &self.bytes[0..self.len]
    }
}

impl Clone for PacketData {
    fn clone(&self) -> PacketData {
        PacketData {
            bytes: self.bytes,
            len: self.len,
        }
    }
}

/// A wrapper around the data that is to be sent in a TFTP DATA packet
/// so that the data can be cloned and compared for equality.
#[derive(Clone)]
pub struct DataBytes(pub [u8; 512]);

impl PartialEq for DataBytes {
    fn eq(&self, other: &DataBytes) -> bool {
        for i in 0..512 {
            if self.0[i] != other.0[i] {
                return false;
            }
        }

        true
    }
}

impl fmt::Debug for DataBytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(&self.0[..]))
    }
}

#[derive(PartialEq, Clone, Debug)]
pub enum Packet {
    RRQ {
        filename: String,
        mode: String,
    },
    WRQ {
        filename: String,
        mode: String,
    },
    DATA {
        block_num: u16,
        data: DataBytes,
        len: usize,
    },
    ACK(u16),
    ERROR {
        code: ErrorCode,
        msg: String,
    },
}

impl Packet {
    /// Creates and returns a packet parsed from its byte representation.
    pub fn read(bytes: PacketData) -> Result<Packet> {
        let opcode = OpCode::from_u16(merge_bytes(bytes.bytes[0], bytes.bytes[1]))?;
        match opcode {
            OpCode::RRQ | OpCode::WRQ => read_rw_packet(opcode, bytes),
            OpCode::DATA => read_data_packet(bytes),
            OpCode::ACK => read_ack_packet(bytes),
            OpCode::ERROR => read_error_packet(bytes),
        }
    }

    /// Returns the packet's operation code.
    pub fn op_code(&self) -> OpCode {
        match *self {
            Packet::RRQ { .. } => OpCode::RRQ,
            Packet::WRQ { .. } => OpCode::WRQ,
            Packet::DATA { .. } => OpCode::DATA,
            Packet::ACK(_) => OpCode::ACK,
            Packet::ERROR { .. } => OpCode::ERROR,
        }
    }

    /// Consumes the packet and returns the packet in byte representation.
    pub fn bytes(self) -> Result<PacketData> {
        match self {
            Packet::RRQ { filename, mode } => rw_packet_bytes(OpCode::RRQ, filename, mode),
            Packet::WRQ { filename, mode } => rw_packet_bytes(OpCode::WRQ, filename, mode),
            Packet::DATA {
                block_num,
                data,
                len,
            } => data_packet_bytes(block_num, data.0, len),
            Packet::ACK(block_num) => ack_packet_bytes(block_num),
            Packet::ERROR { code, msg } => error_packet_bytes(code, msg),
        }
    }
}

/// Splits a two byte unsigned integer into two one byte unsigned integers.
fn split_into_bytes(num: u16) -> (u8, u8) {
    let mut wtr = vec![];
    wtr.write_u16::<BigEndian>(num).unwrap();

    (wtr[0], wtr[1])
}

/// Merges two 1 byte unsigned integers into a two byte unsigned integer.
fn merge_bytes(num1: u8, num2: u8) -> u16 {
    let mut rdr = Cursor::new(vec![num1, num2]);
    rdr.read_u16::<BigEndian>().unwrap()
}

/// Reads bytes from the packet bytes starting from the given index
/// until the zero byte and returns a string containing the bytes read.
fn read_string(bytes: &PacketData, start: usize) -> Result<(String, usize)> {
    let mut result_bytes = Vec::new();
    let mut counter = start;
    while bytes.bytes[counter] != 0 {
        result_bytes.push(bytes.bytes[counter]);

        counter += 1;
        if counter >= bytes.len {
            return Err(PacketErr::StrOutOfBounds);
        }
    }
    counter += 1;

    let result_str = str::from_utf8(result_bytes.as_slice())?.to_string();
    Ok((result_str, counter))
}

fn read_rw_packet(code: OpCode, bytes: PacketData) -> Result<Packet> {
    let (filename, end_pos) = read_string(&bytes, 2)?;
    let (mode, _) = read_string(&bytes, end_pos)?;

    match code {
        OpCode::RRQ => Ok(Packet::RRQ { filename, mode }),
        OpCode::WRQ => Ok(Packet::WRQ { filename, mode }),
        _ => Err(PacketErr::InvalidOpCode),
    }
}

fn read_data_packet(bytes: PacketData) -> Result<Packet> {
    let block_num = merge_bytes(bytes.bytes[2], bytes.bytes[3]);
    let mut data = [0; 512];
    data[0..512].clone_from_slice(&bytes.bytes[4..(512 + 4)]);

    Ok(Packet::DATA {
        block_num,
        data: DataBytes(data),
        len: bytes.len - 4,
    })
}

fn read_ack_packet(bytes: PacketData) -> Result<Packet> {
    let block_num = merge_bytes(bytes.bytes[2], bytes.bytes[3]);
    Ok(Packet::ACK(block_num))
}

fn read_error_packet(bytes: PacketData) -> Result<Packet> {
    let error_code = ErrorCode::from_u16(merge_bytes(bytes.bytes[2], bytes.bytes[3]))?;
    let (msg, _) = read_string(&bytes, 4)?;

    Ok(Packet::ERROR {
        code: error_code,
        msg,
    })
}

fn rw_packet_bytes(packet: OpCode, filename: String, mode: String) -> Result<PacketData> {
    if filename.len() + mode.len() > MAX_PACKET_SIZE {
        return Err(PacketErr::OverflowSize);
    }

    let mut bytes = [0; MAX_PACKET_SIZE];

    let (b1, b2) = split_into_bytes(packet as u16);
    bytes[0] = b1;
    bytes[1] = b2;

    let mut index = 2;
    for byte in filename.bytes() {
        bytes[index] = byte;
        index += 1;
    }

    index += 1;
    for byte in mode.bytes() {
        bytes[index] = byte;
        index += 1;
    }
    index += 1;

    Ok(PacketData::new(bytes, index))
}

fn data_packet_bytes(block_num: u16, data: [u8; 512], data_len: usize) -> Result<PacketData> {
    let mut bytes = [0; MAX_PACKET_SIZE];

    let (b1, b2) = split_into_bytes(OpCode::DATA as u16);
    bytes[0] = b1;
    bytes[1] = b2;

    let (b3, b4) = split_into_bytes(block_num);
    bytes[2] = b3;
    bytes[3] = b4;

    bytes[4..(data_len + 4)].clone_from_slice(&data[0..data_len]);

    Ok(PacketData::new(bytes, data_len + 4))
}

fn ack_packet_bytes(block_num: u16) -> Result<PacketData> {
    let mut bytes = [0; MAX_PACKET_SIZE];

    let (b1, b2) = split_into_bytes(OpCode::ACK as u16);
    bytes[0] = b1;
    bytes[1] = b2;

    let (b3, b4) = split_into_bytes(block_num);
    bytes[2] = b3;
    bytes[3] = b4;

    Ok(PacketData::new(bytes, 4))
}

fn error_packet_bytes(code: ErrorCode, msg: String) -> Result<PacketData> {
    if msg.len() + 5 > MAX_PACKET_SIZE {
        return Err(PacketErr::OverflowSize);
    }

    let mut bytes = [0; MAX_PACKET_SIZE];

    let (b1, b2) = split_into_bytes(OpCode::ERROR as u16);
    bytes[0] = b1;
    bytes[1] = b2;

    let (b3, b4) = split_into_bytes(code as u16);
    bytes[2] = b3;
    bytes[3] = b4;

    let mut index = 4;
    for byte in msg.bytes() {
        bytes[index] = byte;
        index += 1;
    }
    index += 1;

    Ok(PacketData::new(bytes, index))
}

macro_rules! read_string {
    ($name:ident, $bytes:expr, $start_pos:expr, $string:expr, $end_pos:expr) => {
        #[test]
        fn $name() {
            let mut bytes = [0; MAX_PACKET_SIZE];
            let seed_bytes = $bytes.chars().collect::<Vec<_>>();
            for i in 0..$bytes.len() {
                bytes[i] = seed_bytes[i] as u8;
            }

            let result = read_string(&PacketData::new(bytes, seed_bytes.len()), $start_pos);
            assert!(result.is_ok());
            let _ = result.map(|(string, end_pos)| {
                assert_eq!(string, $string);
                assert_eq!(end_pos, $end_pos);
            });
        }
    };
}

read_string!(
    test_read_string_normal,
    "hello world!\0",
    0,
    "hello world!",
    13
);
read_string!(
    test_read_string_zero_in_mid,
    "hello wor\0ld!",
    0,
    "hello wor",
    10
);
read_string!(
    test_read_string_diff_start_pos,
    "hello world!\0",
    6,
    "world!",
    13
);