use super::Error;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[allow(clippy::upper_case_acronyms)]
pub enum Type {
A = 1,
NS = 2,
MF = 4,
CNAME = 5,
SOA = 6,
MB = 7,
MG = 8,
MR = 9,
NULL = 10,
WKS = 11,
PTR = 12,
HINFO = 13,
MINFO = 14,
MX = 15,
TXT = 16,
AAAA = 28,
SRV = 33,
OPT = 41,
DS = 43,
RRSIG = 46,
NSEC = 47,
DNSKEY = 48,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[allow(clippy::upper_case_acronyms)]
pub enum QueryType {
A = 1,
NS = 2,
MF = 4,
CNAME = 5,
SOA = 6,
MB = 7,
MG = 8,
MR = 9,
NULL = 10,
WKS = 11,
PTR = 12,
HINFO = 13,
MINFO = 14,
MX = 15,
TXT = 16,
AAAA = 28,
SRV = 33,
AXFR = 252,
MAILB = 253,
MAILA = 254,
All = 255,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Class {
IN = 1,
CS = 2,
CH = 3,
HS = 4,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum QueryClass {
IN = 1,
CS = 2,
CH = 3,
HS = 4,
Any = 255,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Opcode {
StandardQuery,
InverseQuery,
ServerStatusRequest,
Reserved(u16),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum ResponseCode {
NoError,
FormatError,
ServerFailure,
NameError,
NotImplemented,
Refused,
Reserved(u8),
}
impl From<u16> for Opcode {
fn from(code: u16) -> Opcode {
use self::Opcode::{InverseQuery, Reserved, ServerStatusRequest, StandardQuery};
match code {
0 => StandardQuery,
1 => InverseQuery,
2 => ServerStatusRequest,
x => Reserved(x),
}
}
}
impl From<Opcode> for u16 {
fn from(val: Opcode) -> Self {
use self::Opcode::{InverseQuery, Reserved, ServerStatusRequest, StandardQuery};
match val {
StandardQuery => 0,
InverseQuery => 1,
ServerStatusRequest => 2,
Reserved(x) => x,
}
}
}
impl From<u8> for ResponseCode {
fn from(code: u8) -> ResponseCode {
use self::ResponseCode::{
FormatError, NameError, NoError, NotImplemented, Refused, Reserved, ServerFailure,
};
match code {
0 => NoError,
1 => FormatError,
2 => ServerFailure,
3 => NameError,
4 => NotImplemented,
5 => Refused,
6..=15 => Reserved(code),
x => panic!("Invalid response code {}", x),
}
}
}
impl From<ResponseCode> for u8 {
fn from(val: ResponseCode) -> Self {
use self::ResponseCode::{
FormatError, NameError, NoError, NotImplemented, Refused, Reserved, ServerFailure,
};
match val {
NoError => 0,
FormatError => 1,
ServerFailure => 2,
NameError => 3,
NotImplemented => 4,
Refused => 5,
Reserved(code) => code,
}
}
}
impl QueryType {
pub fn parse(code: u16) -> Result<QueryType, Error> {
use self::QueryType::{
All, A, AAAA, AXFR, CNAME, HINFO, MAILA, MAILB, MB, MF, MG, MINFO, MR, MX, NS, NULL,
PTR, SOA, SRV, TXT, WKS,
};
match code {
1 => Ok(A),
2 => Ok(NS),
4 => Ok(MF),
5 => Ok(CNAME),
6 => Ok(SOA),
7 => Ok(MB),
8 => Ok(MG),
9 => Ok(MR),
10 => Ok(NULL),
11 => Ok(WKS),
12 => Ok(PTR),
13 => Ok(HINFO),
14 => Ok(MINFO),
15 => Ok(MX),
16 => Ok(TXT),
28 => Ok(AAAA),
33 => Ok(SRV),
252 => Ok(AXFR),
253 => Ok(MAILB),
254 => Ok(MAILA),
255 => Ok(All),
x => Err(Error::InvalidQueryType(x)),
}
}
}
impl QueryClass {
pub fn parse(code: u16) -> Result<QueryClass, Error> {
use self::QueryClass::{Any, CH, CS, HS, IN};
match code {
1 => Ok(IN),
2 => Ok(CS),
3 => Ok(CH),
4 => Ok(HS),
255 => Ok(Any),
x => Err(Error::InvalidQueryClass(x)),
}
}
}
impl Type {
pub fn parse(code: u16) -> Result<Type, Error> {
use self::Type::{
A, AAAA, CNAME, DNSKEY, DS, HINFO, MB, MF, MG, MINFO, MR, MX, NS, NSEC, NULL, OPT, PTR,
RRSIG, SOA, SRV, TXT, WKS,
};
match code {
1 => Ok(A),
2 => Ok(NS),
4 => Ok(MF),
5 => Ok(CNAME),
6 => Ok(SOA),
7 => Ok(MB),
8 => Ok(MG),
9 => Ok(MR),
10 => Ok(NULL),
11 => Ok(WKS),
12 => Ok(PTR),
13 => Ok(HINFO),
14 => Ok(MINFO),
15 => Ok(MX),
16 => Ok(TXT),
28 => Ok(AAAA),
33 => Ok(SRV),
41 => Ok(OPT),
43 => Ok(DS),
46 => Ok(RRSIG),
47 => Ok(NSEC),
48 => Ok(DNSKEY),
x => Err(Error::InvalidType(x)),
}
}
}
impl Class {
pub fn parse(code: u16) -> Result<Class, Error> {
use self::Class::{CH, CS, HS, IN};
match code {
1 => Ok(IN),
2 => Ok(CS),
3 => Ok(CH),
4 => Ok(HS),
x => Err(Error::InvalidClass(x)),
}
}
}