Skip to content

Commit

Permalink
Implemented resolve of PTR records.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed May 16, 2021
1 parent f658fdd commit a9f2193
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/commons/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub fn is_yggdrasil_record(record: &DnsRecord) -> bool {
DnsRecord::NS { .. } => {}
DnsRecord::CNAME { .. } => {}
DnsRecord::SOA { .. } => {}
DnsRecord::PTR { .. } => {}
DnsRecord::MX { .. } => {}
DnsRecord::TXT { .. } => {}
DnsRecord::AAAA { addr, .. } => { return is_yggdrasil(&IpAddr::from(*addr))}
Expand Down
39 changes: 39 additions & 0 deletions src/dns/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum QueryType {
NS, // 2
CNAME, // 5
SOA, // 6
PTR, // 12
MX, // 15
TXT, // 16
AAAA, // 28
Expand All @@ -48,6 +49,7 @@ impl QueryType {
QueryType::NS => 2,
QueryType::CNAME => 5,
QueryType::SOA => 6,
QueryType::PTR => 12,
QueryType::MX => 15,
QueryType::TXT => 16,
QueryType::AAAA => 28,
Expand All @@ -62,6 +64,7 @@ impl QueryType {
2 => QueryType::NS,
5 => QueryType::CNAME,
6 => QueryType::SOA,
12 => QueryType::PTR,
15 => QueryType::MX,
16 => QueryType::TXT,
28 => QueryType::AAAA,
Expand Down Expand Up @@ -135,6 +138,11 @@ pub enum DnsRecord {
minimum: u32,
ttl: TransientTtl,
}, // 6
PTR {
domain: String,
data: String,
ttl: TransientTtl,
}, // 12
MX {
domain: String,
priority: u16,
Expand Down Expand Up @@ -252,6 +260,16 @@ impl DnsRecord {
ttl: TransientTtl(ttl),
})
}
QueryType::PTR => {
let mut ptr = String::new();
buffer.read_qname(&mut ptr)?;

Ok(DnsRecord::PTR {
domain,
data: ptr,
ttl: TransientTtl(ttl),
})
}
QueryType::MX => {
let priority = buffer.read_u16()?;
let mut mx = String::new();
Expand Down Expand Up @@ -429,6 +447,20 @@ impl DnsRecord {
let size = buffer.pos() - (pos + 2);
buffer.set_u16(pos, size as u16)?;
}
DnsRecord::PTR { ref domain, ref data, ttl: TransientTtl(ttl) } => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::PTR.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;

let pos = buffer.pos();
buffer.write_u16(0)?;

buffer.write_qname(data)?;

let size = buffer.pos() - (pos + 2);
buffer.set_u16(pos, size as u16)?;
}
DnsRecord::MX {
ref domain,
priority,
Expand Down Expand Up @@ -510,6 +542,7 @@ impl DnsRecord {
DnsRecord::NS { .. } => QueryType::NS,
DnsRecord::CNAME { .. } => QueryType::CNAME,
DnsRecord::SRV { .. } => QueryType::SRV,
DnsRecord::PTR { .. } => QueryType::PTR,
DnsRecord::MX { .. } => QueryType::MX,
DnsRecord::UNKNOWN { qtype, .. } => QueryType::UNKNOWN(qtype),
DnsRecord::SOA { .. } => QueryType::SOA,
Expand All @@ -525,6 +558,7 @@ impl DnsRecord {
| DnsRecord::NS { ref domain, .. }
| DnsRecord::CNAME { ref domain, .. }
| DnsRecord::SRV { ref domain, .. }
| DnsRecord::PTR { ref domain, .. }
| DnsRecord::MX { ref domain, .. }
| DnsRecord::UNKNOWN { ref domain, .. }
| DnsRecord::SOA { ref domain, .. }
Expand All @@ -540,6 +574,7 @@ impl DnsRecord {
DnsRecord::NS { ref host, .. } => Some(host.clone()),
DnsRecord::CNAME { ref host, .. } => Some(host.clone()),
DnsRecord::SRV { ref host, .. } => Some(host.clone()),
DnsRecord::PTR { ref data, .. } => Some(data.clone()),
DnsRecord::MX { ref host, .. } => Some(host.clone()),
DnsRecord::TXT { ref data, .. } => Some(data.clone()),
DnsRecord::SOA { ref m_name, ref r_name, .. } => {
Expand Down Expand Up @@ -575,6 +610,10 @@ impl DnsRecord {
ttl: TransientTtl(ttl),
..
}
| DnsRecord::PTR {
ttl: TransientTtl(ttl),
..
}
| DnsRecord::MX {
ttl: TransientTtl(ttl),
..
Expand Down

0 comments on commit a9f2193

Please sign in to comment.