aboutsummaryrefslogtreecommitdiff
path: root/src/pap.rs
blob: ffa1a4fd856c67c36f1e156857b05eb6063e6a61 (plain) (blame)
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
use crate::{Deserialize, Error, Result, Serialize};

use std::io::{Read, Write};

use ppproperly_macros::{Deserialize, Serialize};

pub const PAP_AUTH_REQUEST: u8 = 1;
pub const PAP_AUTH_ACK: u8 = 2;
pub const PAP_AUTH_NAK: u8 = 3;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PapData {
    AuthenticateRequest(PapAuthenticateRequest),
    AuthenticateAck(PapAuthenticateAck),
    AuthenticateNak(PapAuthenticateNak),
}

impl Default for PapData {
    fn default() -> Self {
        Self::AuthenticateRequest(PapAuthenticateRequest::default())
    }
}

impl Serialize for PapData {
    fn serialize<W: Write>(&self, w: &mut W) -> Result<()> {
        match self {
            Self::AuthenticateRequest(payload) => payload.serialize(w),
            Self::AuthenticateAck(payload) => payload.serialize(w),
            Self::AuthenticateNak(payload) => payload.serialize(w),
        }
    }
}

impl PapData {
    fn discriminant(&self) -> u8 {
        match self {
            Self::AuthenticateRequest(_) => PAP_AUTH_REQUEST,
            Self::AuthenticateAck(_) => PAP_AUTH_ACK,
            Self::AuthenticateNak(_) => PAP_AUTH_NAK,
        }
    }

    fn len(&self) -> u16 {
        match self {
            Self::AuthenticateRequest(payload) => payload.len(),
            Self::AuthenticateAck(payload) => payload.len(),
            Self::AuthenticateNak(payload) => payload.len(),
        }
    }

    fn deserialize_with_discriminant<R: Read>(
        &mut self,
        r: &mut R,
        discriminant: &u8,
    ) -> Result<()> {
        match *discriminant {
            PAP_AUTH_REQUEST => {
                let mut tmp = PapAuthenticateRequest::default();

                tmp.deserialize(r)?;
                *self = Self::AuthenticateRequest(tmp);
            }
            PAP_AUTH_ACK => {
                let mut tmp = PapAuthenticateAck::default();

                tmp.deserialize(r)?;
                *self = Self::AuthenticateAck(tmp);
            }
            PAP_AUTH_NAK => {
                let mut tmp = PapAuthenticateNak::default();

                tmp.deserialize(r)?;
                *self = Self::AuthenticateNak(tmp);
            }
            _ => return Err(Error::InvalidPapCode(*discriminant)),
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PapPkt {
    #[ppproperly(discriminant_for(field = "data", data_type = "u8"))]
    pub identifier: u8,
    #[ppproperly(len_for(field = "data", offset = 4, data_type = "u16"))]
    pub data: PapData,
}

impl PapPkt {
    pub fn new_authenticate_request(identifier: u8, peer_id: String, passwd: String) -> Self {
        Self {
            identifier,
            data: PapData::AuthenticateRequest(PapAuthenticateRequest { peer_id, passwd }),
        }
    }

    pub fn new_authenticate_ack(identifier: u8, msg: String) -> Self {
        Self {
            identifier,
            data: PapData::AuthenticateAck(PapAuthenticateAck { msg }),
        }
    }

    pub fn new_authenticate_nak(identifier: u8, msg: String) -> Self {
        Self {
            identifier,
            data: PapData::AuthenticateNak(PapAuthenticateNak { msg }),
        }
    }

    pub fn len(&self) -> u16 {
        4 + self.data.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 4
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PapAuthenticateRequest {
    #[ppproperly(len_for(field = "peer_id", offset = 0, data_type = "u8"))]
    pub peer_id: String,
    #[ppproperly(len_for(field = "passwd", offset = 0, data_type = "u8"))]
    pub passwd: String,
}

impl PapAuthenticateRequest {
    pub fn len(&self) -> u16 {
        (2 + self.peer_id.len() + self.passwd.len())
            .try_into()
            .unwrap()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 2
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PapAuthenticateAck {
    #[ppproperly(len_for(field = "msg", offset = 0, data_type = "u8"))]
    pub msg: String,
}

impl PapAuthenticateAck {
    pub fn len(&self) -> u16 {
        (1 + self.msg.len()).try_into().unwrap()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 1
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PapAuthenticateNak {
    #[ppproperly(len_for(field = "msg", offset = 0, data_type = "u8"))]
    pub msg: String,
}

impl PapAuthenticateNak {
    pub fn len(&self) -> u16 {
        (1 + self.msg.len()).try_into().unwrap()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 1
    }
}