]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
NTP: change parse function to return the number of parsed messages
authorPierre Chifflier <chifflier@wzdftpd.net>
Mon, 19 Jun 2017 11:40:42 +0000 (13:40 +0200)
committerVictor Julien <victor@inliniac.net>
Tue, 27 Jun 2017 14:52:23 +0000 (16:52 +0200)
rust/src/ntp/ntp.rs

index de6544977cf41a1a6e7b63f2ad5ab0d32624b485..3405ec5e26d24441333c1c6dc2eb2cb172a81ba3 100644 (file)
@@ -84,6 +84,9 @@ impl NTPState {
 }
 
 impl NTPState {
+    /// Parse an NTP request message
+    ///
+    /// Returns The number of messages parsed, or -1 on error
     fn parse(&mut self, i: &[u8], _direction: u8) -> i8 {
         match parse_ntp(i) {
             IResult::Done(_,ref msg) => {
@@ -94,7 +97,7 @@ impl NTPState {
                     tx.xid = msg.ref_id;
                     self.transactions.push(tx);
                 }
-                0
+                1
             },
             IResult::Incomplete(_) => {
                 SCLogDebug!("Insufficient data while parsing NTP data");
@@ -367,3 +370,24 @@ pub extern "C" fn rs_ntp_state_get_event_info(event_name: *const libc::c_char,
     };
     0
 }
+
+#[cfg(test)]
+mod tests {
+    use super::NTPState;
+
+    #[test]
+    fn test_ntp_parse_request_valid() {
+        // A UDP NTP v4 request, in client mode
+        const REQ : &[u8] = &[
+            0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x18, 0x57, 0xab, 0xc3, 0x4a, 0x5f, 0x2c, 0xfe
+        ];
+
+        let mut state = NTPState::new();
+        assert_eq!(1, state.parse(REQ, 0));
+    }
+}