~kf5jwc/dns-server-rs

133f5da898f7ff8370b9d7d0d92813a5bc61b9a6 — Kyle Jones 6 months ago de6654c
Avoid new() construction by preferring impl Default
M src/bin/response_parser.rs => src/bin/response_parser.rs +1 -1
@@ 13,7 13,7 @@ fn main() {
    env_logger::init();

    let mut f = File::open("packets/response_packet").unwrap();
    let mut res_buffer = BytePacketBuffer::new();
    let mut res_buffer = BytePacketBuffer::default();
    f.read(&mut res_buffer.buf).unwrap();

    let res_packet: DnsPacket = res_buffer.into();

M src/bin/stub_resolver.rs => src/bin/stub_resolver.rs +2 -2
@@ 21,7 21,7 @@ fn main() {
    let req_buffer: BytePacketBuffer = packet.into();
    socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).expect("Sending message to upstream server");

    let mut res_buffer = BytePacketBuffer::new();
    let mut res_buffer = BytePacketBuffer::default();
    socket.recv_from(&mut res_buffer.buf).unwrap();

    let res_packet: DnsPacket = res_buffer.into();


@@ 46,7 46,7 @@ fn make_dns_packet() -> DnsPacket {
    let qtype = QueryType::A;

    let mut rng = rand::thread_rng();
    let mut packet = DnsPacket::new();
    let mut packet = DnsPacket::default();
    packet.header.id = rng.gen();
    packet.header.questions = 1;
    packet.header.recursion_desired = true;

M src/dns_packet.rs => src/dns_packet.rs +3 -15
@@ 11,7 11,7 @@ pub use dns_question::DnsQuestion;
pub use dns_record::DnsRecord;
pub use query_type::QueryType;

#[derive(Clone, Debug)]
#[derive(Default, Clone, Debug)]
pub struct DnsPacket {
    pub header: DnsHeader,
    pub questions: Vec<DnsQuestion>,


@@ 20,21 20,9 @@ pub struct DnsPacket {
    pub resources: Vec<DnsRecord>,
}

impl DnsPacket {
    pub fn new() -> Self {
        Self {
            header: DnsHeader::new(),
            questions: Vec::new(),
            answers: Vec::new(),
            authorities: Vec::new(),
            resources: Vec::new(),
        }
    }
}

impl Into<BytePacketBuffer> for DnsPacket {
    fn into(mut self) -> BytePacketBuffer {
        let mut buffer = BytePacketBuffer::new();
        let mut buffer = BytePacketBuffer::default();
        debug!("DnsPacket -> BytePacketBuffer");
        self.header.questions = self.questions.len() as u16;
        debug!("Questions: {}", self.header.questions);


@@ 65,7 53,7 @@ impl Into<BytePacketBuffer> for DnsPacket {

impl From<BytePacketBuffer> for DnsPacket {
    fn from(mut buffer: BytePacketBuffer) -> Self {
        let mut result = DnsPacket::new();
        let mut result = DnsPacket::default();
        result.header.read(&mut buffer).expect("Error reading header from packet buffer!");

        for _ in 0..result.header.questions {

M src/dns_packet/byte_packet_buffer.rs => src/dns_packet/byte_packet_buffer.rs +5 -5
@@ 6,16 6,16 @@ pub struct BytePacketBuffer {
    pub pos: usize,
}

impl BytePacketBuffer {

    // This gives us a fresh packet buffer, with position initialized to 0.
    pub fn new() -> BytePacketBuffer {
        BytePacketBuffer {
impl Default for BytePacketBuffer {
    fn default() -> Self {
        Self {
            buf: [0; 512],
            pos: 0,
        }
    }
}

impl BytePacketBuffer {
    pub fn step(&mut self, steps: usize) -> Result<()> {
        self.pos += steps;
        Ok(())

M src/dns_packet/dns_header.rs => src/dns_packet/dns_header.rs +1 -24
@@ 4,7 4,7 @@ use std::io::Result;
use super::BytePacketBuffer;
use result_code::ResultCode;

#[derive(Clone,Debug)]
#[derive(Default, Clone, Debug)]
pub struct DnsHeader {
    pub id: u16,
    pub recursion_desired: bool,


@@ 26,29 26,6 @@ pub struct DnsHeader {
}

impl DnsHeader {
    pub fn new() -> Self {
        Self {
            id: 0,

            recursion_desired: false,
            truncated_message: false,
            authoritative_answer: false,
            opcode: 0,
            response: false,

            rescode: ResultCode::NOERROR,
            checking_disabled: false,
            authed_data: false,
            z: false,
            recursion_available: false,

            questions: 0,
            answers: 0,
            authoritative_entries: 0,
            resource_entries: 0,
        }
    }

    pub fn read(&mut self, buffer: &mut BytePacketBuffer) -> Result<()> {
        self.id = buffer.read_u16()?;


M src/dns_packet/dns_header/result_code.rs => src/dns_packet/dns_header/result_code.rs +6 -0
@@ 8,6 8,12 @@ pub enum ResultCode {
    REFUSED = 5,
}

impl Default for ResultCode {
    fn default() -> Self {
        Self::NOERROR
    }
}

impl From<u8> for ResultCode {
    fn from(num: u8) -> Self {
        match num {