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
use std::{fmt, io};
/// Non-I/O errors that may occur during message decoding.
///
/// The parts of the library that perform I/O use [`std::io::Error`] instead. This error type can be
/// converted to [`std::io::Error`] via [`From`]/[`Into`].
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Error {
/// The end of the message was reached while more data was expected.
Eof,
/// A domain name pointer pointed into itself or further into the message.
PointerLoop,
/// A field was set to an invalid (reserved for future use or illegal) value.
InvalidValue,
/// Only returned from [`MessageEncoder::finish`], indicates that there was not enough space in
/// the provided buffer to fit the entire message.
///
/// [`MessageEncoder::finish`]: crate::packet::encoder::MessageEncoder::finish
Truncated,
/// An empty label was encountered where it is not allowed.
InvalidEmptyLabel,
/// A label exceeded the maximum allowable length of a label.
LabelTooLong,
}
impl Error {
fn description(&self) -> &str {
match self {
Error::Eof => "unexpected end of data",
Error::PointerLoop => "encountered domain name pointer loop",
Error::InvalidValue => "invalid value",
Error::Truncated => "packet truncated",
Error::InvalidEmptyLabel => "invalid empty label",
Error::LabelTooLong => "label too long",
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.description())
}
}
impl std::error::Error for Error {}
impl From<Error> for io::Error {
fn from(e: Error) -> io::Error {
match e {
Error::Eof => io::ErrorKind::UnexpectedEof.into(),
Error::PointerLoop => io::Error::new(
io::ErrorKind::InvalidData,
"a domain name pointer loop was encountered; this may indicate a malicious request",
),
Error::InvalidValue => io::ErrorKind::InvalidData.into(),
Error::InvalidEmptyLabel => io::Error::new(
io::ErrorKind::InvalidInput,
"invalid empty label in domain name",
),
Error::LabelTooLong => io::Error::new(
io::ErrorKind::InvalidInput,
"domain name label exceeds maximum label length",
),
Error::Truncated => io::ErrorKind::OutOfMemory.into(),
}
}
}