Skip to content

Commit

Permalink
remove record_type from RecordData impls
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejekyll committed Mar 24, 2023
1 parent 24f678c commit 14c7424
Show file tree
Hide file tree
Showing 25 changed files with 183 additions and 329 deletions.
8 changes: 2 additions & 6 deletions crates/proto/src/rr/dnssec/rdata/cdnskey.rs
Expand Up @@ -40,12 +40,8 @@ impl BinEncodable for CDNSKEY {
}

impl<'r> RecordDataDecodable<'r> for CDNSKEY {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
DNSKEY::read_data(decoder, record_type, length).map(Self)
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
DNSKEY::read_data(decoder, length).map(Self)
}
}

Expand Down
8 changes: 2 additions & 6 deletions crates/proto/src/rr/dnssec/rdata/cds.rs
Expand Up @@ -40,12 +40,8 @@ impl BinEncodable for CDS {
}

impl<'r> RecordDataDecodable<'r> for CDS {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
DS::read_data(decoder, record_type, length).map(Self)
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
DS::read_data(decoder, length).map(Self)
}
}

Expand Down
13 changes: 2 additions & 11 deletions crates/proto/src/rr/dnssec/rdata/dnskey.rs
Expand Up @@ -355,12 +355,7 @@ impl BinEncodable for DNSKEY {
}

impl<'r> RecordDataDecodable<'r> for DNSKEY {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
assert_eq!(record_type, RecordType::DNSKEY);
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let flags: u16 = decoder.read_u16()?.unverified(/*used as a bitfield, this is safe*/);

// Bits 0-6 and 8-14 are reserved: these bits MUST have value 0 upon
Expand Down Expand Up @@ -510,11 +505,7 @@ mod tests {
println!("bytes: {bytes:?}");

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let read_rdata = DNSKEY::read_data(
&mut decoder,
RecordType::DNSKEY,
Restrict::new(bytes.len() as u16),
);
let read_rdata = DNSKEY::read_data(&mut decoder, Restrict::new(bytes.len() as u16));
let read_rdata = read_rdata.expect("error decoding");

assert_eq!(rdata, read_rdata);
Expand Down
9 changes: 2 additions & 7 deletions crates/proto/src/rr/dnssec/rdata/ds.rs
Expand Up @@ -204,11 +204,7 @@ impl BinEncodable for DS {
}

impl<'r> RecordDataDecodable<'r> for DS {
fn read_data(
decoder: &mut BinDecoder<'r>,
_record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let start_idx = decoder.index();

let key_tag: u16 = decoder.read_u16()?.unverified(/*key_tag is valid as any u16*/);
Expand Down Expand Up @@ -333,8 +329,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
DS::read_data(&mut decoder, RecordType::DS, restrict).expect("Decoding error");
let read_rdata = DS::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}

Expand Down
9 changes: 2 additions & 7 deletions crates/proto/src/rr/dnssec/rdata/key.rs
Expand Up @@ -773,11 +773,7 @@ impl BinEncodable for KEY {
}

impl<'r> RecordDataDecodable<'r> for KEY {
fn read_data(
decoder: &mut BinDecoder<'r>,
_record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<KEY> {
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<KEY> {
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// | A/C | Z | XT| Z | Z | NAMTYP| Z | Z | Z | Z | SIG |
Expand Down Expand Up @@ -951,8 +947,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
KEY::read_data(&mut decoder, RecordType::KEY, restrict).expect("Decoding error");
let read_rdata = KEY::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
// #[cfg(any(feature = "openssl", feature = "ring"))]
// assert!(rdata
Expand Down
20 changes: 10 additions & 10 deletions crates/proto/src/rr/dnssec/rdata/mod.rs
Expand Up @@ -582,47 +582,47 @@ impl DNSSECRData {
match record_type {
RecordType::CDNSKEY => {
trace!("reading CDNSKEY");
CDNSKEY::read_data(decoder, record_type, rdata_length).map(Self::CDNSKEY)
CDNSKEY::read_data(decoder, rdata_length).map(Self::CDNSKEY)
}
RecordType::CDS => {
trace!("reading CDS");
CDS::read_data(decoder, record_type, rdata_length).map(Self::CDS)
CDS::read_data(decoder, rdata_length).map(Self::CDS)
}
RecordType::DNSKEY => {
trace!("reading DNSKEY");
DNSKEY::read_data(decoder, record_type, rdata_length).map(Self::DNSKEY)
DNSKEY::read_data(decoder, rdata_length).map(Self::DNSKEY)
}
RecordType::DS => {
trace!("reading DS");
DS::read_data(decoder, record_type, rdata_length).map(Self::DS)
DS::read_data(decoder, rdata_length).map(Self::DS)
}
RecordType::KEY => {
trace!("reading KEY");
KEY::read_data(decoder, record_type, rdata_length).map(Self::KEY)
KEY::read_data(decoder, rdata_length).map(Self::KEY)
}
RecordType::NSEC => {
trace!("reading NSEC");
NSEC::read_data(decoder, record_type, rdata_length).map(Self::NSEC)
NSEC::read_data(decoder, rdata_length).map(Self::NSEC)
}
RecordType::NSEC3 => {
trace!("reading NSEC3");
NSEC3::read_data(decoder, record_type, rdata_length).map(Self::NSEC3)
NSEC3::read_data(decoder, rdata_length).map(Self::NSEC3)
}
RecordType::NSEC3PARAM => {
trace!("reading NSEC3PARAM");
NSEC3PARAM::read(decoder).map(Self::NSEC3PARAM)
}
RecordType::RRSIG => {
trace!("reading RRSIG");
RRSIG::read_data(decoder, record_type, rdata_length).map(Self::RRSIG)
RRSIG::read_data(decoder, rdata_length).map(Self::RRSIG)
}
RecordType::SIG => {
trace!("reading SIG");
SIG::read_data(decoder, record_type, rdata_length).map(Self::SIG)
SIG::read_data(decoder, rdata_length).map(Self::SIG)
}
RecordType::TSIG => {
trace!("reading TSIG");
TSIG::read_data(decoder, record_type, rdata_length).map(Self::TSIG)
TSIG::read_data(decoder, rdata_length).map(Self::TSIG)
}
r => {
panic!("not a dnssec RecordType: {}", r);
Expand Down
11 changes: 2 additions & 9 deletions crates/proto/src/rr/dnssec/rdata/nsec.rs
Expand Up @@ -146,13 +146,7 @@ impl BinEncodable for NSEC {
}

impl<'r> RecordDataDecodable<'r> for NSEC {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
assert_eq!(record_type, RecordType::NSEC);

fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let start_idx = decoder.index();

let next_domain_name = Name::read(decoder)?;
Expand Down Expand Up @@ -266,8 +260,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
NSEC::read_data(&mut decoder, RecordType::NSEC, restrict).expect("Decoding error");
let read_rdata = NSEC::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
}
14 changes: 3 additions & 11 deletions crates/proto/src/rr/dnssec/rdata/nsec3.rs
Expand Up @@ -265,13 +265,7 @@ impl BinEncodable for NSEC3 {
}

impl<'r> RecordDataDecodable<'r> for NSEC3 {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
assert_eq!(record_type, RecordType::NSEC3);

fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let start_idx = decoder.index();

let hash_algorithm = Nsec3HashAlgorithm::from_u8(
Expand Down Expand Up @@ -447,8 +441,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
NSEC3::read_data(&mut decoder, RecordType::NSEC3, restrict).expect("Decoding error");
let read_rdata = NSEC3::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}

Expand Down Expand Up @@ -494,8 +487,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
NSEC3::read_data(&mut decoder, RecordType::NSEC3, restrict).expect("Decoding error");
let read_rdata = NSEC3::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata_wo, read_rdata);
}
}
8 changes: 2 additions & 6 deletions crates/proto/src/rr/dnssec/rdata/rrsig.rs
Expand Up @@ -104,12 +104,8 @@ impl BinEncodable for RRSIG {
}

impl<'r> RecordDataDecodable<'r> for RRSIG {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
SIG::read_data(decoder, record_type, length).map(Self)
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
SIG::read_data(decoder, length).map(Self)
}
}

Expand Down
11 changes: 2 additions & 9 deletions crates/proto/src/rr/dnssec/rdata/sig.rs
Expand Up @@ -485,13 +485,7 @@ impl BinEncodable for SIG {
}

impl<'r> RecordDataDecodable<'r> for SIG {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
assert!(record_type == RecordType::SIG || record_type == RecordType::RRSIG);

fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let start_idx = decoder.index();

// TODO should we verify here? or elsewhere...
Expand Down Expand Up @@ -670,8 +664,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
SIG::read_data(&mut decoder, RecordType::SIG, restrict).expect("Decoding error");
let read_rdata = SIG::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
}
14 changes: 3 additions & 11 deletions crates/proto/src/rr/dnssec/rdata/tsig.rs
Expand Up @@ -394,11 +394,7 @@ impl<'r> RecordDataDecodable<'r> for TSIG {
/// / /
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
fn read_data(
decoder: &mut BinDecoder<'r>,
_record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let end_idx = length.map(|rdl| rdl as usize)
.checked_add(decoder.index())
.map_err(|_| ProtoError::from("rdata end position overflow"))? // no legal message is long enough to trigger that
Expand Down Expand Up @@ -829,12 +825,8 @@ mod tests {
println!("bytes: {bytes:?}");

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let read_rdata = TSIG::read_data(
&mut decoder,
RecordType::TSIG,
Restrict::new(bytes.len() as u16),
)
.expect("failed to read back");
let read_rdata = TSIG::read_data(&mut decoder, Restrict::new(bytes.len() as u16))
.expect("failed to read back");
assert_eq!(rdata, read_rdata);
}

Expand Down
12 changes: 2 additions & 10 deletions crates/proto/src/rr/mod.rs
Expand Up @@ -65,22 +65,14 @@ trait RecordDataDecodable<'r>: Sized {
/// * `decoder` - data stream from which the RData will be read
/// * `record_type` - specifies the RecordType that has already been read from the stream
/// * `length` - the data length that should be read from the stream for this RecordData
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self>;
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self>;
}

impl<'r, T> RecordDataDecodable<'r> for T
where
T: 'r + BinDecodable<'r> + Sized,
{
fn read_data(
decoder: &mut BinDecoder<'r>,
_record_type: RecordType,
_length: Restrict<u16>,
) -> ProtoResult<Self> {
fn read_data(decoder: &mut BinDecoder<'r>, _length: Restrict<u16>) -> ProtoResult<Self> {
T::read(decoder)
}
}
23 changes: 4 additions & 19 deletions crates/proto/src/rr/rdata/caa.rs
Expand Up @@ -804,13 +804,7 @@ impl<'r> RecordDataDecodable<'r> for CAA {
/// The length of the value field is specified implicitly as the
/// remaining length of the enclosing Resource Record data field.
/// ```
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<CAA> {
assert_eq!(record_type, RecordType::CAA);

fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<CAA> {
// the spec declares that other flags should be ignored for future compatibility...
let issuer_critical: bool =
decoder.read_u8()?.unverified(/*used as bitfield*/) & 0b1000_0000 != 0;
Expand Down Expand Up @@ -1061,12 +1055,8 @@ mod tests {
println!("bytes: {bytes:?}");

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let read_rdata = CAA::read_data(
&mut decoder,
RecordType::CAA,
Restrict::new(bytes.len() as u16),
)
.expect("failed to read back");
let read_rdata = CAA::read_data(&mut decoder, Restrict::new(bytes.len() as u16))
.expect("failed to read back");
assert_eq!(rdata, read_rdata);
}

Expand Down Expand Up @@ -1245,12 +1235,7 @@ mod tests {
];

let mut decoder = BinDecoder::new(MESSAGE);
let err = CAA::read_data(
&mut decoder,
RecordType::CAA,
Restrict::new(MESSAGE.len() as u16),
)
.unwrap_err();
let err = CAA::read_data(&mut decoder, Restrict::new(MESSAGE.len() as u16)).unwrap_err();
match err.kind() {
ProtoErrorKind::Msg(msg) => assert_eq!(msg, "bad character in CAA issuer key: ÿ"),
_ => panic!("unexpected error: {:?}", err),
Expand Down
10 changes: 2 additions & 8 deletions crates/proto/src/rr/rdata/csync.rs
Expand Up @@ -139,12 +139,7 @@ impl BinEncodable for CSYNC {
}

impl<'r> RecordDataDecodable<'r> for CSYNC {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
assert_eq!(record_type, RecordType::CSYNC);
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
let start_idx = decoder.index();

let soa_serial = decoder.read_u32()?.unverified();
Expand Down Expand Up @@ -229,8 +224,7 @@ mod tests {

let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata =
CSYNC::read_data(&mut decoder, RecordType::CSYNC, restrict).expect("Decoding error");
let read_rdata = CSYNC::read_data(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
}
8 changes: 2 additions & 6 deletions crates/proto/src/rr/rdata/https.rs
Expand Up @@ -40,12 +40,8 @@ impl BinEncodable for HTTPS {
}

impl<'r> RecordDataDecodable<'r> for HTTPS {
fn read_data(
decoder: &mut BinDecoder<'r>,
record_type: RecordType,
length: Restrict<u16>,
) -> ProtoResult<Self> {
SVCB::read_data(decoder, record_type, length).map(Self)
fn read_data(decoder: &mut BinDecoder<'r>, length: Restrict<u16>) -> ProtoResult<Self> {
SVCB::read_data(decoder, length).map(Self)
}
}

Expand Down

0 comments on commit 14c7424

Please sign in to comment.