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
/// Error when FQDN parsing goes wrong
#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]
pub enum Error {
/// The trailing dot of the FQDN string is missing.
///
/// A valid FQDN string should be ended by a dot (e.g. `github.com.`).
TrailingDotMissing,
/// The trailing nul byte of the FQDN bytes is missing.
///
/// A valid FQDN array of bytes should be ended by the nul byte (e.g. `b"\x06github\x03com\x00"`)
TrailingNulCharMissing,
/// An invalid character is found in a label of the FQDN.
///
/// The allowed characters in a FQDN label are letters, digits and `'-'`.
/// By default, this crate also accepts `'_'` in FQDN but this behavior could be deactivated with
/// the `strict-rfc-1035` feature.
InvalidLabelChar,
/// The analysed bytes are not consistent with a FQDN sequence of bytes.
///
/// Typically, the length bytes of labels are not consistent.
InvalidStructure,
/// The name of the domain is too long
///
/// By default, there is no limit except if the `strict-rfc-1035` feature is selected and
/// then, the domain name should be less than 255 characters (including the trailing dot).
TooLongDomainName,
/// One label of the FQDN is too long
///
/// The returned error contains the excessive length.
///
/// By default, the limit is set to 255 characters but if the `strict-rfc-1035` feature is selected,
/// then this limit is set to `63` (as said in the RFC).
TooLongLabel,
/// One label cannot start with a hyphen
///
/// The returned error contains the start position of the involved label
LabelCannotStartWithHyphen,
/// One label cannot end with a hyphen
///
/// The returned error contains the start position of the involved label
LabelCannotEndWithHyphen,
/// One label is empty (e.g. starting dot as `.github.com.` or two following dots as `github..com.`)
EmptyLabel
}
impl std::error::Error for Error { }
use std::fmt;
use std::fmt::Debug;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(
match self {
Error::TrailingDotMissing => "the trailing dot of the FQDN string is missing",
Error::TrailingNulCharMissing => "the trailing nul byte of the FQDN bytes is missing",
Error::InvalidLabelChar => "invalid char found in FQDN",
Error::InvalidStructure => "invalid FQDN byte sequence",
Error::TooLongDomainName => "too long FQDN",
Error::TooLongLabel => "too long label found in FQDN",
Error::LabelCannotStartWithHyphen => "FQDN label can’t start with a hyphen",
Error::LabelCannotEndWithHyphen => "FQDN label can’t end with a hyphen",
Error::EmptyLabel => "empty label found in FQDN",
})
}
}
// Checks if the bytes are really a FQDN (with lower cases and a trailing nul char)
pub(crate) fn check_byte_sequence(bytes: &[u8]) -> Result<(),Error>
{
// stop immediately if the trailing nul char is missing
match bytes.last() {
Some(0) => { /* ok, continue */ }
_ => return Err(Error::TrailingNulCharMissing)
}
#[cfg(feature="domain-name-length-limited-to-255")]
if bytes.len() > 255 {
return Err(Error::TooLongDomainName)
}
// if unlimited, then the radix trie limits it to u32::MAX
#[cfg(not(feature="domain-name-length-limited-to-255"))]
if bytes.len() > u32::MAX as usize {
return Err(Error::TooLongDomainName)
}
let mut iter = bytes.iter();
let mut remaining = bytes.len() - 1;
while remaining > 0 {
match iter.next() {
// sublen does not match with available bytes
None | Some(&0) => return Err(Error::InvalidStructure),
Some(&sublen) if sublen as usize > remaining => {
return Err(Error::InvalidStructure)
}
#[cfg(feature="domain-label-length-limited-to-63")]
Some(&sublen) if sublen > 63 => {
return Err(Error::TooLongLabel)
}
#[cfg(feature="domain-label-cannot-start-or-end-with-hyphen")]
Some(&1) => { // label with only one single char
if check_any_char(*iter.next().unwrap())? == b'-' {
return Err(Error::LabelCannotStartWithHyphen);
}
remaining -= 2;
}
#[cfg(feature="domain-label-cannot-start-or-end-with-hyphen")]
Some(&sublen) => {
if check_any_char(*iter.next().unwrap())? == b'-' {
return Err(Error::LabelCannotStartWithHyphen);
}
for _ in 1..sublen - 1 {
check_any_char(*iter.next().unwrap())?;
}
if check_any_char(*iter.next().unwrap())? == b'-' {
return Err(Error::LabelCannotEndWithHyphen);
}
remaining -= sublen as usize + 1;
}
#[cfg(not(feature="domain-label-cannot-start-or-end-with-hyphen"))]
Some(&sublen) => {
for _ in 0..sublen {
check_any_char(*iter.next().unwrap())?;
}
remaining -= sublen as usize + 1;
}
}
}
debug_assert_eq!( iter.next(), Some(&0));
debug_assert!( iter.next().is_none() );
Ok(())
}
fn check_any_char(c: u8) -> Result<u8,Error>
{
match c {
b'a'..=b'z' | b'-' | b'0'..=b'9' => Ok(c),
#[cfg(not(feature="domain-name-without-special-chars"))]
b'_' | b'#' => Ok(c),
_ => Err(Error::InvalidLabelChar),
}
}
pub(crate) fn check_and_lower_any_char(c: u8) -> Result<u8,Error>
{
/// If the 6th bit is set, ascii is lower case.
const ASCII_CASE_MASK: u8 = 0b0010_0000;
match c {
b'a'..=b'z' | b'-' | b'0'..=b'9' => Ok(c),
#[cfg(not(feature="domain-name-without-special-chars"))]
b'_' | b'#' => Ok(c),
b'A'..=b'Z' => Ok(c | ASCII_CASE_MASK), // to lowercase
_ => Err(Error::InvalidLabelChar),
}
}