Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / third_party / betocore / np_adv / src / header.rs

header.rs166 lines · 5.0 KiBRaw
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! NP version header (the first byte) support.
16//!
17//! The version header byte is 3 bits of version followed by 5 reserved bits.
18//!
19//! For V0 (`0b000`), the first 3 of the reserved bits identify the encoding
20//! scheme used, and the last 2 are still reserved.
21
22use nom::{combinator, number, Parser as _};
23
24// 3-bit versions (high 3 bits in version header)
25const PROTOCOL_VERSION_LEGACY: u8 = 0;
26const PROTOCOL_VERSION_EXTENDED: u8 = 1;
27
28// 3-bit encoding ids (3 bits after version, leaving 2 reserved bits)
29const V0_ENCODING_SCHEME_ID_UNENCRYPTED: u8 = 0;
30const V0_ENCODING_SCHEME_ID_LDT: u8 = 1;
31
32/// Version header byte for V1
33pub const VERSION_HEADER_V1: u8 = PROTOCOL_VERSION_EXTENDED << 5;
34
35/// Version header byte for V0 with the unencrypted encoding
36pub const VERSION_HEADER_V0_UNENCRYPTED: u8 =
37 (PROTOCOL_VERSION_LEGACY << 5) | (V0_ENCODING_SCHEME_ID_UNENCRYPTED << 2);
38
39/// Version header byte for V0 with the LDT encoding
40pub const VERSION_HEADER_V0_LDT: u8 =
41 (PROTOCOL_VERSION_LEGACY << 5) | (V0_ENCODING_SCHEME_ID_LDT << 2);
42
43/// The first byte in the NP svc data. It defines which version of the protocol
44/// is used for the rest of the svc data.
45#[derive(Debug, PartialEq, Eq, Clone)]
46pub(crate) enum NpVersionHeader {
47 V0(V0Encoding),
48 V1(V1AdvHeader),
49}
50
51impl NpVersionHeader {
52 /// Parse a NP advertisement header.
53 ///
54 /// This can be used on all versions of advertisements since it's the header that determines the
55 /// version.
56 ///
57 /// Returns a `nom::IResult` with the parsed header and the remaining bytes of the advertisement.
58 pub(crate) fn parse(adv: &[u8]) -> nom::IResult<&[u8], Self> {
59 combinator::map_opt(number::complete::u8, |version_header| match version_header {
60 VERSION_HEADER_V0_UNENCRYPTED => Some(NpVersionHeader::V0(V0Encoding::Unencrypted)),
61 VERSION_HEADER_V0_LDT => Some(NpVersionHeader::V0(V0Encoding::Ldt)),
62 VERSION_HEADER_V1 => Some(NpVersionHeader::V1(V1AdvHeader::new(version_header))),
63 _ => None,
64 })
65 .parse(adv)
66 }
67}
68
69#[derive(Debug, PartialEq, Eq, Clone)]
70pub(crate) enum V0Encoding {
71 Unencrypted,
72 Ldt,
73}
74
75/// A parsed NP Version Header that indicates the V1 protocol is in use.
76#[derive(Debug, PartialEq, Eq, Clone, Copy)]
77pub struct V1AdvHeader {
78 header_byte: u8,
79}
80
81impl V1AdvHeader {
82 pub(crate) fn new(header_byte: u8) -> Self {
83 Self { header_byte }
84 }
85
86 /// The version header byte
87 pub(crate) fn contents(&self) -> u8 {
88 self.header_byte
89 }
90}
91
92#[cfg(test)]
93#[allow(clippy::unwrap_used)]
94mod tests {
95 use super::*;
96
97 extern crate std;
98
99 use nom::error;
100
101 #[test]
102 fn parse_header_v0_unencrypted() {
103 let (_, header) = NpVersionHeader::parse(&[0x00]).unwrap();
104 assert_eq!(NpVersionHeader::V0(V0Encoding::Unencrypted), header);
105 }
106
107 #[test]
108 fn parse_header_v0_ldt() {
109 let (_, header) = NpVersionHeader::parse(&[0x04]).unwrap();
110 assert_eq!(NpVersionHeader::V0(V0Encoding::Ldt), header);
111 }
112
113 #[test]
114 fn parse_header_v0_nonzero_invalid_encoding() {
115 let input = &[0x08];
116 assert_eq!(
117 nom::Err::Error(error::Error {
118 input: input.as_slice(),
119 code: error::ErrorKind::MapOpt,
120 }),
121 NpVersionHeader::parse(input).unwrap_err()
122 );
123 }
124
125 #[test]
126 fn parse_header_v0_nonzero_reserved() {
127 let input = &[0x01];
128 assert_eq!(
129 nom::Err::Error(error::Error {
130 input: input.as_slice(),
131 code: error::ErrorKind::MapOpt,
132 }),
133 NpVersionHeader::parse(input).unwrap_err()
134 );
135 }
136
137 #[test]
138 fn parse_header_v1_nonzero_reserved() {
139 let input = &[0x30];
140 assert_eq!(
141 nom::Err::Error(error::Error {
142 input: input.as_slice(),
143 code: error::ErrorKind::MapOpt,
144 }),
145 NpVersionHeader::parse(input).unwrap_err()
146 );
147 }
148
149 #[test]
150 fn parse_header_bad_version() {
151 let input = &[0x80];
152 assert_eq!(
153 nom::Err::Error(error::Error {
154 input: input.as_slice(),
155 code: error::ErrorKind::MapOpt,
156 }),
157 NpVersionHeader::parse(input).unwrap_err()
158 );
159 }
160
161 #[test]
162 fn parse_header_v1() {
163 let (_, header) = NpVersionHeader::parse(&[0x20]).unwrap();
164 assert_eq!(NpVersionHeader::V1(V1AdvHeader::new(0x20)), header);
165 }
166}