From bdaad94d48af93b0f0120a2394dc26038dded60a Mon Sep 17 00:00:00 2001 From: Akash Singh Date: Fri, 3 Jan 2025 17:19:32 +0530 Subject: [PATCH] feat : Fixed All Lint Errors --- src/main.rs | 10 +-- src/protocol/byte_packet_buffer.rs | 6 ++ src/protocol/dnsheader.rs | 10 ++- src/protocol/dnspacket.rs | 10 ++- src/protocol/dnsquestion.rs | 4 +- src/protocol/dnsrecord.rs | 114 ++++++++++++++--------------- src/protocol/main.rs | 6 +- src/protocol/querytype.rs | 34 ++++----- src/protocol/resultcode.rs | 25 ++++--- 9 files changed, 117 insertions(+), 102 deletions(-) diff --git a/src/main.rs b/src/main.rs index 42fe0a5..42ffaed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,13 +94,13 @@ fn handle_query(socket : &UdpSocket) -> Result<() , Box> } } else{ - packet.header.rescode = ResultCode::SERVFAIL; + packet.header.rescode = ResultCode::ServFail; } } //We need to make sure that a question is actually present in the packet //If not , we'll set the response code to `FORMERR` and return an error else{ - packet.header.rescode = ResultCode::FORMERR; + packet.header.rescode = ResultCode::FormErr; } //Now we can just encode our response and send it back to the client @@ -133,13 +133,13 @@ pub fn recursive_lookup(qname : &str, qtype : QueryType) -> Result Result Self { + BytePacketBuffer::new() + } +} + impl BytePacketBuffer { // Initialize a new buffer pub fn new() -> BytePacketBuffer { diff --git a/src/protocol/dnsheader.rs b/src/protocol/dnsheader.rs index eed3457..8f52264 100644 --- a/src/protocol/dnsheader.rs +++ b/src/protocol/dnsheader.rs @@ -24,6 +24,12 @@ pub struct DnsHeader { } //ye implementation mat puchna , its a lot of bit manipulation circus +impl Default for DnsHeader { + fn default() -> Self { + DnsHeader::new() + } +} + impl DnsHeader { pub fn new() -> DnsHeader { DnsHeader { @@ -35,7 +41,7 @@ impl DnsHeader { opcode: 0, response: false, - rescode: ResultCode::NOERROR, + rescode: ResultCode::NoError, checking_disabled: false, authed_data: false, z: false, @@ -81,7 +87,7 @@ impl DnsHeader { | ((self.truncated_message as u8) << 1) | ((self.authoritative_answer as u8) << 2) | (self.opcode << 3) - | ((self.response as u8) << 7) as u8, + | ((self.response as u8) << 7), )?; buffer.write_u8( diff --git a/src/protocol/dnspacket.rs b/src/protocol/dnspacket.rs index 7cc3ce5..c443659 100644 --- a/src/protocol/dnspacket.rs +++ b/src/protocol/dnspacket.rs @@ -5,7 +5,6 @@ use crate::protocol::dnsheader::DnsHeader; use crate::protocol::dnsquestion::DnsQuestion; use crate::protocol::dnsrecord::DnsRecord; use crate::protocol::querytype::QueryType; -use crate::protocol::resultcode::ResultCode; #[derive(Clone, Debug)] pub struct DnsPacket { @@ -16,6 +15,12 @@ pub struct DnsPacket { pub resources: Vec, } +impl Default for DnsPacket { + fn default() -> Self { + DnsPacket::new() + } +} + impl DnsPacket { pub fn new() -> DnsPacket { DnsPacket { @@ -32,7 +37,7 @@ impl DnsPacket { result.header.read(buffer)?; for _ in 0..result.header.questions { - let mut question = DnsQuestion::new("".to_string(), QueryType::UNKNOWN(0)); + let mut question = DnsQuestion::new("".to_string(), QueryType::Unknown(0)); question.read(buffer)?; result.questions.push(question); } @@ -119,7 +124,6 @@ impl DnsPacket { _ => None, }) }) - .map(| addr | addr) //Finally pick the first valid entry .next() } diff --git a/src/protocol/dnsquestion.rs b/src/protocol/dnsquestion.rs index a25c7e6..0fc13b7 100644 --- a/src/protocol/dnsquestion.rs +++ b/src/protocol/dnsquestion.rs @@ -11,8 +11,8 @@ pub struct DnsQuestion { impl DnsQuestion { pub fn new(name: String, qtype: QueryType) -> DnsQuestion { DnsQuestion { - name: name, - qtype: qtype, + name, + qtype, } } diff --git a/src/protocol/dnsrecord.rs b/src/protocol/dnsrecord.rs index ed3959f..099f7dc 100644 --- a/src/protocol/dnsrecord.rs +++ b/src/protocol/dnsrecord.rs @@ -5,10 +5,8 @@ use core::net::Ipv6Addr; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[allow(dead_code)] - - pub enum DnsRecord { - UNKNOWN { + Unknown { domain: String, qtype: u16, data_len: u16, @@ -24,12 +22,12 @@ pub enum DnsRecord { host: String, ttl: u32, }, // 2 - CNAME { + Cname { domain: String, host: String, ttl: u32, }, // 5 - SOA { + Soa { domain: String, mname: String, rname: String, @@ -46,12 +44,12 @@ pub enum DnsRecord { host: String, ttl: u32, }, // 15 - TXT { + Txt { domain: String, text: String, ttl: u32, }, // 16 - AAAA { + Aaaa { domain: String, addr: Ipv6Addr, ttl: u32, @@ -76,36 +74,36 @@ impl DnsRecord { ((raw_addr >> 24) & 0xFF) as u8, ((raw_addr >> 16) & 0xFF) as u8, ((raw_addr >> 8) & 0xFF) as u8, - ((raw_addr >> 0) & 0xFF) as u8, + ((raw_addr) & 0xFF) as u8, ); Ok(DnsRecord::A { - domain: domain, - addr: addr, - ttl: ttl, + domain, + addr, + ttl, }) } - QueryType::AAAA => { + QueryType::Aaaa => { let raw_addr1 = buffer.read_u32()?; let raw_addr2 = buffer.read_u32()?; let raw_addr3 = buffer.read_u32()?; let raw_addr4 = buffer.read_u32()?; let addr = Ipv6Addr::new( ((raw_addr1 >> 16) & 0xFFFF) as u16, - ((raw_addr1 >> 0) & 0xFFFF) as u16, + (raw_addr1 & 0xFFFF) as u16, ((raw_addr2 >> 16) & 0xFFFF) as u16, - ((raw_addr2 >> 0) & 0xFFFF) as u16, + ((raw_addr2) & 0xFFFF) as u16, ((raw_addr3 >> 16) & 0xFFFF) as u16, - ((raw_addr3 >> 0) & 0xFFFF) as u16, + ((raw_addr3) & 0xFFFF) as u16, ((raw_addr4 >> 16) & 0xFFFF) as u16, - ((raw_addr4 >> 0) & 0xFFFF) as u16, + ((raw_addr4) & 0xFFFF) as u16, ); - Ok(DnsRecord::AAAA { - domain: domain, - addr: addr, - ttl: ttl, + Ok(DnsRecord::Aaaa { + domain, + addr, + ttl, }) } @@ -114,24 +112,24 @@ impl DnsRecord { buffer.read_qname(&mut ns)?; Ok(DnsRecord::NS { - domain: domain, + domain, host: ns, - ttl: ttl, + ttl, }) } - QueryType::CNAME => { + QueryType::Cname => { let mut cname = String::new(); buffer.read_qname(&mut cname)?; - Ok(DnsRecord::CNAME { - domain: domain, + Ok(DnsRecord::Cname { + domain, host: cname, - ttl: ttl, + ttl, }) } - QueryType::SOA => { + QueryType::Soa => { let mut mname = String::new(); buffer.read_qname(&mut mname)?; @@ -144,16 +142,16 @@ impl DnsRecord { let expire = buffer.read_u32()?; let minimum = buffer.read_u32()?; - Ok(DnsRecord::SOA { - domain: domain, - mname: mname, - rname: rname, - serial: serial, - refresh: refresh, - retry: retry, - expire: expire, - minimum: minimum, - ttl: ttl, + Ok(DnsRecord::Soa { + domain, + mname, + rname, + serial, + refresh, + retry, + expire, + minimum, + ttl, }) } @@ -164,32 +162,32 @@ impl DnsRecord { buffer.read_qname(&mut mx)?; Ok(DnsRecord::MX { - domain: domain, - priority: priority, + domain, + priority, host: mx, - ttl: ttl, + ttl, }) } - QueryType::TXT => { + QueryType::Txt => { let mut txt = String::new(); buffer.read_qname(&mut txt)?; - Ok(DnsRecord::TXT { - domain: domain, + Ok(DnsRecord::Txt { + domain, text: txt, - ttl: ttl, + ttl, }) } - QueryType::UNKNOWN(_) => { + QueryType::Unknown(_) => { buffer.step(data_len as usize)?; - Ok(DnsRecord::UNKNOWN { - domain: domain, + Ok(DnsRecord::Unknown { + domain, qtype: qtype_num, - data_len: data_len, - ttl: ttl, + data_len, + ttl, }) } } @@ -215,7 +213,7 @@ impl DnsRecord { buffer.write_u8(octets[2])?; buffer.write_u8(octets[3])?; } - DnsRecord::UNKNOWN { .. } => { + DnsRecord::Unknown { .. } => { println!("Skipping record: {:?}", self); } DnsRecord::NS { @@ -236,13 +234,13 @@ impl DnsRecord { let size = buffer.pos() - (pos + 2); buffer.set_u16(pos, size as u16)?; } - DnsRecord::CNAME { + DnsRecord::Cname { ref domain, ref host, ttl, } => { buffer.write_qname(domain)?; - buffer.write_u16(QueryType::CNAME.to_num())?; + buffer.write_u16(QueryType::Cname.to_num())?; buffer.write_u16(1)?; buffer.write_u32(ttl)?; @@ -254,7 +252,7 @@ impl DnsRecord { let size = buffer.pos() - (pos + 2); buffer.set_u16(pos, size as u16)?; } - DnsRecord::SOA { + DnsRecord::Soa { ref domain, ref mname, ref rname, @@ -266,7 +264,7 @@ impl DnsRecord { ttl, } => { buffer.write_qname(domain)?; - buffer.write_u16(QueryType::SOA.to_num())?; + buffer.write_u16(QueryType::Soa.to_num())?; buffer.write_u16(1)?; buffer.write_u32(ttl)?; @@ -304,13 +302,13 @@ impl DnsRecord { let size = buffer.pos() - (pos + 2); buffer.set_u16(pos, size as u16)?; } - DnsRecord::TXT { + DnsRecord::Txt { ref domain, ref text, ttl, } => { buffer.write_qname(domain)?; - buffer.write_u16(QueryType::TXT.to_num())?; + buffer.write_u16(QueryType::Txt.to_num())?; buffer.write_u16(1)?; buffer.write_u32(ttl)?; @@ -322,13 +320,13 @@ impl DnsRecord { let size = buffer.pos() - (pos + 2); buffer.set_u16(pos, size as u16)?; } - DnsRecord::AAAA { + DnsRecord::Aaaa { ref domain, ref addr, ttl, } => { buffer.write_qname(domain)?; - buffer.write_u16(QueryType::AAAA.to_num())?; + buffer.write_u16(QueryType::Aaaa.to_num())?; buffer.write_u16(1)?; buffer.write_u32(ttl)?; buffer.write_u16(16)?; diff --git a/src/protocol/main.rs b/src/protocol/main.rs index 55e9d7f..c04bfc1 100644 --- a/src/protocol/main.rs +++ b/src/protocol/main.rs @@ -1,15 +1,15 @@ use std::error::Error; // use crate::File; use std::fs::File; +use std::io::Read; use crate::protocol::byte_packet_buffer::BytePacketBuffer; use crate::protocol::dnspacket::DnsPacket; -use std::io::Read; #[allow(dead_code)] fn test_with_response_packet() -> Result<(), Box> { - let mut f = File::open("response_packet.txt")?; + let mut f = File::open("response_packet.Txt")?; let mut buffer = BytePacketBuffer::new(); - f.read(&mut buffer.buf)?; + let _ = f.read(&mut buffer.buf)?; let packet = DnsPacket::from_buffer(&mut buffer)?; println!("{:#?}", packet.header); diff --git a/src/protocol/querytype.rs b/src/protocol/querytype.rs index f876371..3a8a272 100644 --- a/src/protocol/querytype.rs +++ b/src/protocol/querytype.rs @@ -1,26 +1,26 @@ #[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)] pub enum QueryType { - UNKNOWN(u16), + Unknown(u16), A, // 1 NS, // 2 - CNAME, // 5 - SOA, // 6 + Cname, // 5 + Soa, // 6 MX , // 15 - TXT, // 16 - AAAA, // 28 + Txt, // 16 + Aaaa, // 28 } impl QueryType { - pub fn to_num(&self) -> u16 { - match *self { - QueryType::UNKNOWN(x) => x, + pub fn to_num(self) -> u16 { + match self { + QueryType::Unknown(x) => x, QueryType::A => 1, QueryType::NS => 2, - QueryType::CNAME => 5, - QueryType::SOA => 6, + QueryType::Cname => 5, + QueryType::Soa => 6, QueryType::MX => 15, - QueryType::TXT => 16, - QueryType::AAAA => 28, + QueryType::Txt => 16, + QueryType::Aaaa => 28, } } @@ -28,12 +28,12 @@ impl QueryType { match num { 1 => QueryType::A, 2 => QueryType::NS, - 5 => QueryType::CNAME, - 6 => QueryType::SOA, + 5 => QueryType::Cname, + 6 => QueryType::Soa, 15 => QueryType::MX, - 16 => QueryType::TXT, - 28 => QueryType::AAAA, - _ => QueryType::UNKNOWN(num), + 16 => QueryType::Txt, + 28 => QueryType::Aaaa, + _ => QueryType::Unknown(num), } } } \ No newline at end of file diff --git a/src/protocol/resultcode.rs b/src/protocol/resultcode.rs index 66108b8..0114964 100644 --- a/src/protocol/resultcode.rs +++ b/src/protocol/resultcode.rs @@ -1,22 +1,23 @@ #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum ResultCode { - NOERROR = 0, - FORMERR = 1, - SERVFAIL = 2, - NXDOMAIN = 3, - NOTIMP = 4, - REFUSED = 5, + NoError = 0, + FormErr = 1, + ServFail = 2, + NXDomain = 3, + NotImp = 4, + Refused = 5, } impl ResultCode { pub fn from_num(num: u8) -> ResultCode { match num { - 1 => ResultCode::FORMERR, - 2 => ResultCode::SERVFAIL, - 3 => ResultCode::NXDOMAIN, - 4 => ResultCode::NOTIMP, - 5 => ResultCode::REFUSED, - 0 | _ => ResultCode::NOERROR, + 1 => ResultCode::FormErr, + 2 => ResultCode::ServFail, + 3 => ResultCode::NXDomain, + 4 => ResultCode::NotImp, + 5 => ResultCode::Refused, + 0 => ResultCode::NoError, + _ => ResultCode::NoError, } } } \ No newline at end of file