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
// copy from https://github.com/drcoms/drcom-generic
use std::io;
use std::fmt::Debug;

use common::reader::{ReadBytesError, ReaderHelper};

pub mod pppoe;
pub mod wired;

#[cfg(test)]
mod tests;

const PASSWORD_MAX_LEN: usize = 16;
const USERNAME_MAX_LEN: usize = 16;
const PACKET_MAGIC_NUMBER: u16 = 0x0103u16;

pub trait DrCOMFlag: Debug {
    fn as_u32(&self) -> u32;
}

#[derive(Debug)]
pub enum DrCOMValidateError {
    CodeMismatch(u8),
    PacketReadError(ReadBytesError),
}

pub trait DrCOMCommon {
    fn code() -> u8 {
        7u8
    }
}

pub trait DrCOMResponseCommon {
    fn validate_stream<R, V>(input: &mut io::BufReader<R>,
                             validator: V)
                             -> Result<(), DrCOMValidateError>
        where R: io::Read,
              V: FnOnce(u8) -> bool
    {
        let code_bytes = input.read_bytes(1).map_err(DrCOMValidateError::PacketReadError)?;
        let code = code_bytes[0];
        if !validator(code) {
            return Err(DrCOMValidateError::CodeMismatch(code));
        }
        Ok(())
    }
}