diff options
Diffstat (limited to 'rust/vendor/ntp-parser')
-rw-r--r-- | rust/vendor/ntp-parser/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/Cargo.toml | 30 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/LICENSE-MIT | 25 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/README.md | 63 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/src/lib.rs | 23 | ||||
-rw-r--r-- | rust/vendor/ntp-parser/src/ntp.rs | 299 |
7 files changed, 642 insertions, 0 deletions
diff --git a/rust/vendor/ntp-parser/.cargo-checksum.json b/rust/vendor/ntp-parser/.cargo-checksum.json new file mode 100644 index 0000000..bc95103 --- /dev/null +++ b/rust/vendor/ntp-parser/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"d1c72e0b70f240d4c6c9b53978fa8be56abdf5611b0b9cd83799c9096a4565ee","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"a5c61b93b6ee1d104af9920cf020ff3c7efe818e31fe562c72261847a728f513","README.md":"72bd9cabfea6bb5713935544fdfdbce71ce0fb3b0446f588ea95a94c89bf3ad5","src/lib.rs":"0f60a9da195122cbb609cd9b67e8b3c0e3223358f3de390bcc6afad4ee951a76","src/ntp.rs":"d5390ed4e7a942f7c4d73a9296dbca622ba190bf4f092d045aeb3412d6e975d3"},"package":"76084be9bf432d487336dd4e39b31ad93f94aecb14b81f08724f4a37b9abb7a5"}
\ No newline at end of file diff --git a/rust/vendor/ntp-parser/Cargo.toml b/rust/vendor/ntp-parser/Cargo.toml new file mode 100644 index 0000000..b705ae6 --- /dev/null +++ b/rust/vendor/ntp-parser/Cargo.toml @@ -0,0 +1,30 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "ntp-parser" +version = "0.6.0" +authors = ["Pierre Chifflier <chifflier@wzdftpd.net>"] +description = "Parser for the NTP protocol" +homepage = "https://github.com/rusticata/ntp-parser" +readme = "README.md" +keywords = ["NTP", "time", "protocol", "parser", "nom"] +categories = ["network-programming", "parser-implementations"] +license = "MIT/Apache-2.0" +repository = "https://github.com/rusticata/ntp-parser.git" +[dependencies.nom] +version = "7.0" + +[dependencies.nom-derive] +version = "0.10" +[badges.travis-ci] +repository = "rusticata/ntp-parser" diff --git a/rust/vendor/ntp-parser/LICENSE-APACHE b/rust/vendor/ntp-parser/LICENSE-APACHE new file mode 100644 index 0000000..16fe87b --- /dev/null +++ b/rust/vendor/ntp-parser/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rust/vendor/ntp-parser/LICENSE-MIT b/rust/vendor/ntp-parser/LICENSE-MIT new file mode 100644 index 0000000..290e7b9 --- /dev/null +++ b/rust/vendor/ntp-parser/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2017 Pierre Chifflier + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/rust/vendor/ntp-parser/README.md b/rust/vendor/ntp-parser/README.md new file mode 100644 index 0000000..40c3958 --- /dev/null +++ b/rust/vendor/ntp-parser/README.md @@ -0,0 +1,63 @@ +<!-- cargo-sync-readme start --> + +# ntp-parser + +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE-MIT) +[![Apache License 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE) +[![Build Status](https://travis-ci.org/rusticata/ntp-parser.svg?branch=master)](https://travis-ci.org/rusticata/ntp-parser) +[![Crates.io Version](https://img.shields.io/crates/v/ntp-parser.svg)](https://crates.io/crates/ntp-parser) + +## Overview + +ntp-parser is a parser for the NTP protocol. + +This crate mostly serves as a demo/example crate for network protocol parsers written using nom, and nom-derive. +<!-- cargo-sync-readme end --> + +## Changes + +### 0.6.0 + +- Upgrade to nom 7 + +### 0.5.0 + +- Upgrade to nom 6 +- Convert all macro-based parsers to functions +- Convert to nom-derive +- Add support for NTP version 3, update function names to match version + +### 0.4.0 + +- Set edition to 2018 +- Upgrade to nom 5 + +### 0.3.0 + +- Upgrade to nom 4 + +### 0.2.1 + +- Fix parsing of extensions + +### 0.2.0 + +- Use newtype for NtpMode, remove enum_primitive +- Switch license to MIT + APLv2 + +## License + +Licensed under either of + + * Apache License, Version 2.0 + ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license + ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. diff --git a/rust/vendor/ntp-parser/src/lib.rs b/rust/vendor/ntp-parser/src/lib.rs new file mode 100644 index 0000000..b51ab8a --- /dev/null +++ b/rust/vendor/ntp-parser/src/lib.rs @@ -0,0 +1,23 @@ +//! # ntp-parser +//! +//! [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE-MIT) +//! [![Apache License 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE) +//! [![Build Status](https://travis-ci.org/rusticata/ntp-parser.svg?branch=master)](https://travis-ci.org/rusticata/ntp-parser) +//! [![Crates.io Version](https://img.shields.io/crates/v/ntp-parser.svg)](https://crates.io/crates/ntp-parser) +//! +//! ## Overview +//! +//! ntp-parser is a parser for the NTP protocol. +//! +//! This crate mostly serves as a demo/example crate for network protocol parsers written using nom, and nom-derive. + +// add missing_docs +#![deny( + unsafe_code, + unstable_features, + unused_import_braces, + unused_qualifications +)] + +pub use ntp::*; +pub mod ntp; diff --git a/rust/vendor/ntp-parser/src/ntp.rs b/rust/vendor/ntp-parser/src/ntp.rs new file mode 100644 index 0000000..9d59736 --- /dev/null +++ b/rust/vendor/ntp-parser/src/ntp.rs @@ -0,0 +1,299 @@ +use nom::bytes::streaming::take; +use nom::combinator::{complete, map, map_parser, opt}; +use nom::error::{make_error, ErrorKind}; +use nom::multi::many1; +use nom::number::streaming::be_u8; +pub use nom::{Err, IResult}; +use nom_derive::*; + +#[derive(Debug, PartialEq)] +pub enum NtpPacket<'a> { + V3(NtpV3Packet<'a>), + V4(NtpV4Packet<'a>), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, NomBE)] +pub struct NtpMode(pub u8); + +#[allow(non_upper_case_globals)] +impl NtpMode { + pub const Reserved: NtpMode = NtpMode(0); + pub const SymmetricActive: NtpMode = NtpMode(1); + pub const SymmetricPassive: NtpMode = NtpMode(2); + pub const Client: NtpMode = NtpMode(3); + pub const Server: NtpMode = NtpMode(4); + pub const Broadcast: NtpMode = NtpMode(5); + pub const NtpControlMessage: NtpMode = NtpMode(6); + pub const Private: NtpMode = NtpMode(7); +} + +/// An NTP version 3 packet +#[derive(Debug, PartialEq, NomBE)] +pub struct NtpV3Packet<'a> { + #[nom(PreExec = "let (i, b0) = be_u8(i)?;")] + #[nom(Value(b0 >> 6))] + pub li: u8, + #[nom(Value((b0 >> 3) & 0b111))] + pub version: u8, + #[nom(Value(NtpMode(b0 & 0b111)))] + pub mode: NtpMode, + pub stratum: u8, + pub poll: i8, + pub precision: i8, + pub root_delay: u32, + pub root_dispersion: u32, + pub ref_id: u32, + pub ts_ref: u64, + pub ts_orig: u64, + pub ts_recv: u64, + pub ts_xmit: u64, + + #[nom(Parse = "opt(complete(take(12usize)))")] + pub authenticator: Option<&'a [u8]>, +} + +/// An NTP version 4 packet +#[derive(Debug, PartialEq, NomBE)] +pub struct NtpV4Packet<'a> { + #[nom(PreExec = "let (i, b0) = be_u8(i)?;")] + #[nom(Value(b0 >> 6))] + pub li: u8, + #[nom(Value((b0 >> 3) & 0b111))] + pub version: u8, + #[nom(Value(NtpMode(b0 & 0b111)))] + pub mode: NtpMode, + pub stratum: u8, + pub poll: i8, + pub precision: i8, + pub root_delay: u32, + pub root_dispersion: u32, + pub ref_id: u32, + pub ts_ref: u64, + pub ts_orig: u64, + pub ts_recv: u64, + pub ts_xmit: u64, + + #[nom(Parse = "try_parse_extensions")] + pub extensions: Vec<NtpExtension<'a>>, + #[nom(Cond(!i.is_empty()))] + pub auth: Option<NtpMac<'a>>, +} + +impl<'a> NtpV4Packet<'a> { + pub fn get_precision(&self) -> f32 { + 2.0_f32.powf(self.precision as f32) + } +} + +#[derive(Debug, PartialEq, NomBE)] +pub struct NtpExtension<'a> { + pub field_type: u16, + pub length: u16, + #[nom(Parse = "take(length)")] + pub value: &'a [u8], + /*padding*/ +} + +#[derive(Debug, PartialEq, NomBE)] +pub struct NtpMac<'a> { + pub key_id: u32, + #[nom(Parse = "take(16usize)")] + pub mac: &'a [u8], +} + +#[inline] +pub fn parse_ntp_extension(i: &[u8]) -> IResult<&[u8], NtpExtension> { + NtpExtension::parse(i) +} + +// Attempt to parse extensions. +// +// See section 7.5 of [RFC5905] and [RFC7822]: +// In NTPv4, one or more extension fields can be inserted after the +// header and before the MAC, which is always present when an extension +// field is present. +// +// So: +// if == 20, only MAC +// if > 20, ext + MAC +// if == 0, nothing +// else error +fn try_parse_extensions(i: &[u8]) -> IResult<&[u8], Vec<NtpExtension>> { + if i.is_empty() || i.len() == 20 { + // if empty, or if remaining length is exactly the MAC length (20), assume we do not have + // extensions + return Ok((i, Vec::new())); + } + if i.len() < 20 { + return Err(Err::Error(make_error(i, ErrorKind::Eof))); + } + map_parser(take(i.len() - 20), many1(complete(parse_ntp_extension)))(i) +} + +/// Parse an NTP version 3 packet (RFC 1305) +#[inline] +pub fn parse_ntpv3(i: &[u8]) -> IResult<&[u8], NtpV3Packet> { + NtpV3Packet::parse(i) +} + +/// Parse an NTP version 4 packet (RFC 1305) +#[inline] +pub fn parse_ntpv4(i: &[u8]) -> IResult<&[u8], NtpV4Packet> { + NtpV4Packet::parse(i) +} + +/// Parse an NTP packet, version 3 or 4 +#[inline] +pub fn parse_ntp(i: &[u8]) -> IResult<&[u8], NtpPacket> { + let (_, b0) = be_u8(i)?; + match (b0 >> 3) & 0b111 { + 3 => map(NtpV3Packet::parse, NtpPacket::V3)(i), + 4 => map(NtpV4Packet::parse, NtpPacket::V4)(i), + _ => Err(Err::Error(make_error(i, ErrorKind::Tag))), + } +} + +#[cfg(test)] +mod tests { + use crate::ntp::*; + + static NTP_REQ1: &[u8] = &[ + 0xd9, 0x00, 0x0a, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x90, 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, 0xc5, 0x02, 0x04, 0xec, 0xec, + 0x42, 0xee, 0x92, + ]; + + #[test] + fn test_ntp_packet_simple() { + let empty = &b""[..]; + let bytes = NTP_REQ1; + let expected = NtpV4Packet { + li: 3, + version: 3, + mode: NtpMode::SymmetricActive, + stratum: 0, + poll: 10, + precision: -6, + root_delay: 0, + root_dispersion: 0x010290, + ref_id: 0, + ts_ref: 0, + ts_orig: 0, + ts_recv: 0, + ts_xmit: 14195914391047827090u64, + extensions: Vec::new(), + auth: None, + }; + let res = parse_ntpv4(&bytes); + assert_eq!(res, Ok((empty, expected))); + } + + static NTP_REQ2: &[u8] = &[ + 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 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, 0xcc, 0x25, 0xcc, 0x13, 0x2b, + 0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x52, 0x80, 0x0c, 0x2b, 0x59, 0x00, 0x64, 0x66, + 0x84, 0xf4, 0x4c, 0xa4, 0xee, 0xce, 0x12, 0xb8, + ]; + + #[test] + fn test_ntp_packet_mac() { + let empty = &b""[..]; + let bytes = NTP_REQ2; + let expected = NtpV4Packet { + li: 0, + version: 4, + mode: NtpMode::Client, + stratum: 0, + poll: 0, + precision: 0, + root_delay: 12, + root_dispersion: 0, + ref_id: 0, + ts_ref: 0, + ts_orig: 0, + ts_recv: 0, + ts_xmit: 14710388140573593600, + extensions: Vec::new(), + auth: Some(NtpMac { + key_id: 1, + mac: &bytes[52..], + }), + }; + let res = parse_ntpv4(&bytes); + assert_eq!(res, Ok((empty, expected))); + } + + static NTP_REQ2B: &[u8] = &[ + 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 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, 0xcc, 0x25, 0xcc, 0x13, 0x2b, + 0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x52, 0x80, 0x0c, 0x2b, + 0x59, 0x00, 0x64, 0x66, 0x84, 0xf4, 0x4c, 0xa4, 0xee, 0xce, 0x12, 0xb8, + ]; + + #[test] + fn test_ntp_packet_extension() { + let empty = &b""[..]; + let bytes = NTP_REQ2B; + let expected = NtpV4Packet { + li: 0, + version: 4, + mode: NtpMode::Client, + stratum: 0, + poll: 0, + precision: 0, + root_delay: 12, + root_dispersion: 0, + ref_id: 0, + ts_ref: 0, + ts_orig: 0, + ts_recv: 0, + ts_xmit: 14710388140573593600, + extensions: vec![NtpExtension { + field_type: 0, + length: 0, + value: empty, + }], + auth: Some(NtpMac { + key_id: 1, + mac: &bytes[56..], + }), + }; + let res = parse_ntpv4(&bytes); + assert_eq!(res, Ok((empty, expected))); + } + + // from wireshark test captures 'ntp.pcap' + static NTPV3_REQ: &[u8] = &[ + 0x1b, 0x04, 0x06, 0xf5, 0x00, 0x00, 0x10, 0x0d, 0x00, 0x00, 0x05, 0x57, 0x82, 0xdc, 0x18, + 0x18, 0xba, 0x29, 0x66, 0x36, 0x7d, 0xd0, 0x00, 0x00, 0xba, 0x29, 0x66, 0x36, 0x7d, 0x58, + 0x40, 0x00, 0xba, 0x29, 0x66, 0x36, 0x7d, 0xd0, 0x00, 0x00, 0xba, 0x29, 0x66, 0x76, 0x7d, + 0x50, 0x50, 0x00, + ]; + + #[test] + fn test_ntp_packet_v3() { + let empty = &b""[..]; + let bytes = NTPV3_REQ; + let expected = NtpV3Packet { + li: 0, + version: 3, + mode: NtpMode::Client, + stratum: 4, + poll: 6, + precision: -11, + root_delay: 4109, + root_dispersion: 0x0557, + ref_id: 0x82dc1818, + ts_ref: 0xba296636_7dd00000, + ts_orig: 0xba296636_7d584000, + ts_recv: 0xba296636_7dd00000, + ts_xmit: 0xba296676_7d505000, + authenticator: None, + }; + let res = NtpV3Packet::parse(&bytes); + assert_eq!(res, Ok((empty, expected))); + } +} |