mikrotik_rs/protocol/
sentence.rs

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
use super::word::{Word, WordError};

/// A parser for parsing bytes into sentences in the Mikrotik API sentence format.
///
/// The Mikrotik API uses a custom protocol to communicate. Each message is a sentence
/// composed of words. This structure represents a sentence and allows iterating over
/// its words.
///
/// Each word in a sentence is encoded with a length prefix, followed by the word's bytes.
/// The length is encoded in a variable number of bytes to save space for short words.
///
/// More details about the protocol can be found in the Mikrotik Wiki:
/// [Mikrotik API Protocol](https://wiki.mikrotik.com/wiki/Manual:API#Protocol)
#[derive(Debug)]
pub struct Sentence<'a> {
    data: &'a [u8],
    position: usize,
}

impl<'a> Sentence<'a> {
    /// Creates a new `Sentence` instance for parsing the given data slice.
    ///
    /// # Arguments
    ///
    /// * `data` - A slice of bytes representing the data of the Mikrotik sentence.
    pub fn new(data: &'a [u8]) -> Self {
        Self { data, position: 0 }
    }
}

impl<'a> Iterator for Sentence<'a> {
    type Item = Result<Word<'a>, SentenceError>;

    /// Advances the [`Iterator`] and returns the next [`Word`] in the [`Sentence`].
    ///
    /// The word is returned as a slice of the original data. This avoids copying
    /// data but means the lifetime of the returned slice is tied to the lifetime
    /// of the data passed to `Sentence::new`.
    ///
    /// # Errors
    ///
    /// Returns an `Err` if there's an issue decoding the length of the next word
    /// or if the data cannot be interpreted as a valid UTF-8 string slice.
    fn next(&mut self) -> Option<Self::Item> {
        if self.position >= self.data.len() {
            return None;
        }

        let mut start = self.position;

        match read_length(&self.data[start..]) {
            Ok((lenght, bytes_read)) => {
                // Last word is empty, so we are done.
                if lenght == 0 {
                    return None;
                }
                // Start reading the content skipping the length bytes
                start += bytes_read;

                // Will never run on architectures where usize is < 32 bits so converting to usize is safe.
                let end = start + lenght as usize;

                let word = || -> Result<Word, SentenceError> {
                    // Parse the word
                    let data = &self
                        .data
                        .get(start..end)
                        .ok_or(SentenceError::PrefixLength)?;
                    let word = Word::try_from(*data).map_err(SentenceError::from)?;

                    Ok(word)
                }();

                // Update the position for the next iteration
                self.position = end;

                Some(word)
            }
            Err(e) => Some(Err(e)),
        }
    }
}

/// Specific errors that can occur while processing a byte sequence into a [`Sentence`].
///
/// Provides information about issues related to converting a sequence of bytes into a [`Sentence`].
#[derive(Debug, PartialEq)]
pub enum SentenceError {
    /// Error indicating that a sequence of bytes could not be parsed into a [`Word`].
    WordError(WordError),
    /// Error indicating that the prefix lenght of a [`Sentence`] is incorrect.
    /// This could happen if the length of the word is invalid or the data is corrupted.
    PrefixLength,
    // Error indicating that the category of the sentence is missing.
    // This could happen if the sentence does not start with a recognized category.
    // Valid categories are `!done`, `!re`, `!trap`, and `!fatal`.
    //Category,
}

impl From<WordError> for SentenceError {
    fn from(e: WordError) -> Self {
        Self::WordError(e)
    }
}

/// Returns the length and the number of bytes read.
fn read_length(data: &[u8]) -> Result<(u32, usize), SentenceError> {
    let mut c: u32 = data[0] as u32;
    if c & 0x80 == 0x00 {
        Ok((c, 1))
    } else if c & 0xC0 == 0x80 {
        c &= !0xC0;
        c <<= 8;
        c += data[1] as u32;
        return Ok((c, 2));
    } else if c & 0xE0 == 0xC0 {
        c &= !0xE0;
        c <<= 8;
        c += data[1] as u32;
        c <<= 8;
        c += data[2] as u32;
        return Ok((c, 3));
    } else if c & 0xF0 == 0xE0 {
        c &= !0xF0;
        c <<= 8;
        c += data[1] as u32;
        c <<= 8;
        c += data[2] as u32;
        c <<= 8;
        c += data[3] as u32;
        return Ok((c, 4));
    } else if c & 0xF8 == 0xF0 {
        c = data[1] as u32;
        c <<= 8;
        c += data[2] as u32;
        c <<= 8;
        c += data[3] as u32;
        c <<= 8;
        c += data[4] as u32;
        return Ok((c, 5));
    } else {
        Err(SentenceError::PrefixLength)
    }
}

#[cfg(test)]
mod tests {
    use crate::protocol::word::{Word, WordCategory};

    use super::*;

    #[test]
    fn test_sentence_iterator() {
        let data: &[u8] = &[
            0x05, b'!', b'd', b'o', b'n', b'e', // Word: !done
            0x08, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Word: .tag=123
            0x0C, b'=', b'n', b'a', b'm', b'e', b'=', b'e', b't', b'h', b'e', b'r',
            b'1', // Word: =name=ether1
            0x00, // End of sentence
        ];

        let mut sentence = Sentence::new(data);

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Category(WordCategory::Done)
        );

        assert_eq!(sentence.next().unwrap().unwrap(), Word::Tag(123));

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Attribute(("name", Some("ether1")).into())
        );

        assert_eq!(sentence.next(), None);
    }

    #[test]
    fn test_sentence_category_error() {
        // Test case where the first word is not a category
        let data: &[u8] = &[
            0x0A, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Word: .tag=123
            0x0D, b'=', b'n', b'a', b'm', b'e', b'=', b'e', b't', b'h', b'e', b'r',
            b'1', // Word: =name=ether1
        ];

        let mut sentence = Sentence::new(data);

        assert!(sentence.next().unwrap().is_err());
    }

    #[test]
    fn test_sentence_length_error() {
        // Test case where length is invalid
        let data: &[u8] = &[
            0xF8, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Invalid length prefix
        ];

        let mut sentence = Sentence::new(data);

        assert!(sentence.next().unwrap().is_err());
    }

    #[test]
    fn test_complete_sentence_parsing() {
        let data: &[u8] = &[
            0x05, b'!', b'd', b'o', b'n', b'e', // Word: !done
            0x08, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Word: .tag=123
            0x0C, b'=', b'n', b'a', b'm', b'e', b'=', b'e', b't', b'h', b'e', b'r',
            b'1', // Word: =name=ether1
            0x00, // End of sentence
        ];

        let mut sentence = Sentence::new(data);

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Category(WordCategory::Done)
        );

        assert_eq!(sentence.next().unwrap().unwrap(), Word::Tag(123));

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Attribute(("name", Some("ether1")).into())
        );

        assert_eq!(sentence.next(), None);
    }

    #[test]
    fn test_sentence_with_invalid_length() {
        let data: &[u8] = &[
            0xF8, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Invalid length prefix
        ];

        let mut sentence = Sentence::new(data);

        assert!(sentence.next().unwrap().is_err());
    }

    #[test]
    fn test_sentence_without_category() {
        let data: &[u8] = &[
            0x0A, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Word: .tag=123
            0x0D, b'=', b'n', b'a', b'm', b'e', b'=', b'e', b't', b'h', b'e', b'r',
            b'1', // Word: =name=ether1
        ];

        let mut sentence = Sentence::new(data);

        assert!(sentence.next().unwrap().is_err());
    }

    #[test]
    fn test_mixed_words_sentence() {
        let data: &[u8] = &[
            0x03, b'!', b'r', b'e', // Word: !re
            0x04, b'=', b'a', b'=', b'b', // Word: =a=b
            0x08, b'.', b't', b'a', b'g', b'=', b'4', b'5', b'6', // Word: .tag=456
            0x00, // End of sentence
        ];

        let mut sentence = Sentence::new(data);

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Category(WordCategory::Reply)
        );

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Attribute(("a", Some("b")).into())
        );

        assert_eq!(sentence.next().unwrap().unwrap(), Word::Tag(456));

        assert_eq!(sentence.next(), None);
    }

    #[test]
    fn test_sentence_with_fatal_message() {
        let data: &[u8] = &[
            0x06, b'!', b'f', b'a', b't', b'a', b'l', 0x0B, b's', b'e', b'r', b'v', b'e', b'r',
            b' ', b'd', b'o', b'w', b'n', // Word: !fatal server down
            0x00, // End of sentence
        ];

        let mut sentence = Sentence::new(data);

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Category(WordCategory::Fatal)
        );

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Message("server down")
        );

        assert_eq!(sentence.next(), None);
    }

    #[test]
    fn test_complete_sentence_with_extra_data() {
        let data: &[u8] = &[
            0x05, b'!', b'd', b'o', b'n', b'e', // Word: !done
            0x08, b'.', b't', b'a', b'g', b'=', b'1', b'2', b'3', // Word: .tag=123
            0x0C, b'=', b'n', b'a', b'm', b'e', b'=', b'e', b't', b'h', b'e', b'r',
            b'1', // Word: =name=ether1
            0x00, // End of sentence
            0x07, b'!', b'd', b'o', b'n', b'e', // Extra data: !done
        ];

        let mut sentence = Sentence::new(data);

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Category(WordCategory::Done)
        );

        assert_eq!(sentence.next().unwrap().unwrap(), Word::Tag(123));

        assert_eq!(
            sentence.next().unwrap().unwrap(),
            Word::Attribute(("name", Some("ether1")).into())
        );

        assert_eq!(sentence.next(), None);

        // Confirm that extra data is ignored after the end of the sentence
        assert_eq!(sentence.next(), None);
    }
}