Skip to content

Commit

Permalink
integrate srv_rdata to rdata
Browse files Browse the repository at this point in the history
  • Loading branch information
Litr0 committed Jul 17, 2024
1 parent a61a4bc commit b9ad8bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/message/rdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use dnskey_rdata::DnskeyRdata;
use nsec3_rdata::Nsec3Rdata;
use nsec3param_rdata::Nsec3ParamRdata;
use tsig_rdata::TSigRdata;
use srv_rdata::SrvRdata;

#[derive(Clone, PartialEq, Debug)]
/// Enumerates the differents types of `Rdata` struct.
Expand All @@ -52,6 +53,7 @@ pub enum Rdata {
CNAME(CnameRdata),
HINFO(HinfoRdata),
AAAA(AAAARdata),
SRV(SrvRdata),
OPT(OptRdata),
DS(DsRdata),
RRSIG(RRSIGRdata),
Expand Down Expand Up @@ -87,6 +89,7 @@ impl ToBytes for Rdata {
Rdata::SOA(val) => val.to_bytes(),
Rdata::TXT(val) => val.to_bytes(),
Rdata::AAAA(val) => val.to_bytes(),
Rdata::SRV(val) => val.to_bytes(),
Rdata::CNAME(val) => val.to_bytes(),
Rdata::HINFO(val) => val.to_bytes(),
Rdata::OPT(val) => val.to_bytes(),
Expand Down Expand Up @@ -229,6 +232,18 @@ impl FromBytes<Result<Rdata, &'static str>> for Rdata {

Ok(Rdata::AAAA(rdata.unwrap()))
}
33 => {
let rdata = SrvRdata::from_bytes(&bytes[..bytes.len() - 4], full_msg);

match rdata {
Ok(_) => {}
Err(e) => {
return Err(e);
}
}

Ok(Rdata::SRV(rdata.unwrap()))
}
39 => {
let rdata = CnameRdata::from_bytes(&bytes[..bytes.len() - 4], full_msg);

Expand Down Expand Up @@ -348,6 +363,7 @@ impl fmt::Display for Rdata {
Rdata::SOA(val) => write!(f, "{}", val),
Rdata::TXT(val) => write!(f, "{}", val),
Rdata::AAAA(val) => write!(f, "{}", val),
Rdata::SRV(val) => write!(f, "{}", val),
Rdata::CNAME(val) => write!(f, "{}", val),
Rdata::HINFO(val) => write!(f, "{}", val),
Rdata::OPT(val) => write!(f, "{}", val),
Expand Down
17 changes: 17 additions & 0 deletions src/message/rdata/srv_rdata.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use core::fmt;

use crate::domain_name::DomainName;
use crate::message::resource_record::{FromBytes, ToBytes};


/// RFC 2782: https://datatracker.ietf.org/doc/html/rfc2782
/// An struct that represents the `Rdata` for srv type.
#[derive(Clone, Debug, PartialEq)]
pub struct SrvRdata {
/// The priority of this target host. A client MUST attempt to contact the target host with the lowest-numbered priority it can reach; target hosts with the same priority SHOULD be tried in an order defined by the weight field.
priority: u16,
Expand Down Expand Up @@ -142,6 +145,20 @@ impl SrvRdata {
}
}

impl fmt::Display for SrvRdata {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Priority: {},
Weight: {},
Port: {},
Target: {}",
self.get_priority(),
self.get_weight(),
self.get_port(),
self.get_target())

}
}

#[cfg(test)]
mod srv_rdata_tests {
use super::*;
Expand Down

0 comments on commit b9ad8bd

Please sign in to comment.