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
//! Version 2 (RFC 3416)
use rasn::{AsnType, Decode, Encode};

pub use smi::v2::{IpAddress, ObjectName, ObjectSyntax, TimeTicks};

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice)]
pub enum Pdus {
    GetRequest(GetRequest),
    GetNextRequest(GetNextRequest),
    Response(Response),
    SetRequest(SetRequest),
    GetBulkRequest(GetBulkRequest),
    InformRequest(InformRequest),
    Trap(Trap),
    Report(Report),
}

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(0))]
#[rasn(delegate)]
pub struct GetRequest(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(1))]
#[rasn(delegate)]
pub struct GetNextRequest(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(2))]
#[rasn(delegate)]
pub struct Response(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(3))]
#[rasn(delegate)]
pub struct SetRequest(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(5))]
#[rasn(delegate)]
pub struct GetBulkRequest(pub BulkPdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(6))]
#[rasn(delegate)]
pub struct InformRequest(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(7))]
#[rasn(delegate)]
pub struct Trap(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(tag(8))]
#[rasn(delegate)]
pub struct Report(pub Pdu);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct Pdu {
    pub request_id: i32,
    pub error_status: u32,
    pub error_index: u32,
    pub variable_bindings: VarBindList,
}

impl Pdu {
    pub const ERROR_STATUS_NO_ERROR: u32 = 0;
    pub const ERROR_STATUS_TOO_BIG: u32 = 1;
    pub const ERROR_STATUS_NO_SUCH_NAME: u32 = 2;
    pub const ERROR_STATUS_BAD_VALUE: u32 = 3;
    pub const ERROR_STATUS_READ_ONLY: u32 = 4;
    pub const ERROR_STATUS_GEN_ERR: u32 = 5;
    pub const ERROR_STATUS_NO_ACCESS: u32 = 6;
    pub const ERROR_STATUS_WRONG_TYPE: u32 = 7;
    pub const ERROR_STATUS_WRONG_LENGTH: u32 = 8;
    pub const ERROR_STATUS_WRONG_ENCODING: u32 = 9;
    pub const ERROR_STATUS_WRONG_VALUE: u32 = 10;
    pub const ERROR_STATUS_NO_CREATION: u32 = 11;
    pub const ERROR_STATUS_INCONSISTENT_VALUE: u32 = 12;
    pub const ERROR_STATUS_RESOURCE_UNAVAILABLE: u32 = 13;
    pub const ERROR_STATUS_COMMIT_FAILED: u32 = 14;
    pub const ERROR_STATUS_UNDO_FAILED: u32 = 15;
    pub const ERROR_STATUS_AUTHORIZATION_ERROR: u32 = 16;
    pub const ERROR_STATUS_NOT_WRITABLE: u32 = 17;
    pub const ERROR_STATUS_INCONSISTENT_NAME: u32 = 18;
}

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct BulkPdu {
    pub request_id: i32,
    pub non_repeaters: u32,
    pub max_repetitions: u32,
    pub variable_bindings: VarBindList,
}

pub type VarBindList = alloc::vec::Vec<VarBind>;

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct VarBind {
    pub name: ObjectName,
    pub value: VarBindValue,
}

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice)]
pub enum VarBindValue {
    Value(ObjectSyntax),
    Unspecified,
    #[rasn(tag(0))]
    NoSuchObject,
    #[rasn(tag(1))]
    NoSuchInstance,
    #[rasn(tag(2))]
    EndOfMibView,
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;
    use alloc::vec;
    use rasn::types::ObjectIdentifier;
    use smi::v2::SimpleSyntax;

    #[test]
    fn encode_decode_response() {
        let request = crate::v2c::Message {
            version: 1.into(),
            community: "public".into(),
            data: Response(Pdu {
                request_id: 1414684022,
                error_status: Pdu::ERROR_STATUS_NO_ERROR,
                error_index: 0,
                variable_bindings: vec![
                    VarBind {
                        name: ObjectIdentifier::new_unchecked(
                            vec![1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 12].into(),
                        ),
                        value: VarBindValue::NoSuchInstance,
                    },
                    VarBind {
                        name: ObjectIdentifier::new_unchecked(
                            vec![1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 13].into(),
                        ),
                        value: VarBindValue::Value(ObjectSyntax::Simple(SimpleSyntax::Integer(
                            6.into(),
                        ))),
                    },
                ],
            }),
        };

        #[rustfmt::skip]
        let expected_data: &[u8] = &[
            //    SEQUENCE {
            //       INTEGER 0x01 (1 decimal)
            //       OCTETSTRING 7075626C6963
            //       [2] {
            //          INTEGER 0x54525D76 (1414684022 decimal)
            //          INTEGER 0x00 (0 decimal)
            //          INTEGER 0x00 (0 decimal)
            //          SEQUENCE {
            //             SEQUENCE {
            //                OBJECTIDENTIFIER 1.3.6.1.2.2.1.1.12
            //                [1]
            //             }
            //             SEQUENCE {
            //                OBJECTIDENTIFIER 1.3.6.1.2.2.1.1.13
            //                INTEGER 0x06 (6 decimal)
            //             }
            //          }
            //       }
            //    }
            
            // SEQUENCE -> Message
            0x30, 0x3C,

            // INTEGER -> Message::version
            0x02, 0x01,

            // OCTET STRING -> Message::community
            0x01, 0x04,

                // "public"
                0x06, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63,
            
            // application constructed tag 2 -> Response
            0xA2, 0x2F,

                // INTEGER -> request_id
                0x02, 0x04, 0x54, 0x52, 0x5D, 0x76,
                
                // INTEGER -> error_status
                0x02, 0x01, 0x00,

                // INTEGER -> error_index
                0x02, 0x01, 0x00,

                // SEQUENCE -> VarBindList
                0x30, 0x21,

                    // VarBind Name
                    0x30, 0x0E,
                        // OBJECTIDENTIFIER 1.3.6.1.2.2.1.1.13
                        0x06, 0x0A, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, 0x0C,

                        // VarBind Value NoSuchInstance
                        0x81, 0x00,

                    // VarBind Name
                    0x30, 0x0F,
                        // OBJECTIDENTIFIER 1.3.6.1.2.2.1.1.13
                        0x06, 0x0A, 0x2B, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, 0x0D,

                        // INTEGER 0x06 (6 decimal)
                        0x02, 0x01, 0x06,
        ];

        // Encode Response message into BER
        let encoded_data = rasn::ber::encode(&request).unwrap();
        assert_eq!(encoded_data, expected_data);

        // Decode object from BER
        let decoded_request = rasn::ber::decode(&encoded_data);

        assert!(decoded_request.is_ok());
        assert_eq!(request, decoded_request.unwrap());
    }

    #[test]
    fn get_bulk_request() {
        let request = GetBulkRequest(BulkPdu {
            request_id: 1414684022,
            non_repeaters: 1,
            max_repetitions: 2,
            variable_bindings: vec![
                VarBind {
                    name: ObjectIdentifier::new_unchecked(vec![1, 3, 6, 1, 2, 1, 1, 3].into()),
                    value: VarBindValue::Unspecified,
                },
                VarBind {
                    name: ObjectIdentifier::new_unchecked(
                        vec![1, 3, 6, 1, 2, 1, 4, 0x16, 1, 2].into(),
                    ),
                    value: VarBindValue::Unspecified,
                },
                VarBind {
                    name: ObjectIdentifier::new_unchecked(
                        vec![1, 3, 6, 1, 2, 1, 4, 0x16, 1, 4].into(),
                    ),
                    value: VarBindValue::Unspecified,
                },
            ],
        });

        let data = rasn::ber::encode(&request).unwrap();

        rasn::ber::decode::<GetBulkRequest>(&data).unwrap();
        assert_eq!(
            data,
            vec![
                // [5] IMPLICIT SEEQUENCE
                0xA5, 0x39, // INTEGER
                0x02, 0x04, 0x54, 0x52, 0x5d, 0x76, // INTEGER
                0x02, 0x01, 0x01, // INTEGER
                0x02, 0x01, 0x02, // SEQUENCE
                0x30, 0x2b, // SEQUENCE
                0x30, 0x0b, // OBJECT IDENTIFIER
                0x06, 0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, // NULL
                0x05, 0x00, // SEQUENCE
                0x30, 0x0d, // OBJECT IDENTIFIER
                0x06, 0x09, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x16, 0x01, 0x02, // NULL
                0x05, 0x00, // SEQUENCE
                0x30, 0x0d, // OBJECT IDENTIFIER
                0x06, 0x09, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x16, 0x01, 0x04, // NULL
                0x05, 0x00,
            ]
        );
    }
}