~kf5jwc/dns-server-rs

66a267dd6e627af898d4f16148fbdb5536270829 — Kyle Jones 6 months ago e8eb1ab
Use a less error-prone way to specify shifts
2 files changed, 15 insertions(+), 13 deletions(-)

M src/dns_packet/byte_packet_buffer.rs
M src/dns_packet/dns_record.rs
M src/dns_packet/byte_packet_buffer.rs => src/dns_packet/byte_packet_buffer.rs +9 -8
@@ 124,22 124,23 @@ impl BytePacketBuffer {
    }

    pub fn write_u8(&mut self, val: u8) -> Result<()> {
        self.write(val)?;
        Ok(())
        self.write(val)
    }

    pub fn write_u16(&mut self, val: u16) -> Result<()> {
        self.write((val >> 8) as u8)?;
        self.write((val & 0xFF) as u8)?;
        let shifts: &[u8] = &[8, 0];
        for shift in shifts.into_iter() {
            self.write(((val >> shift) & 0xFF) as u8)?;
        }
        Ok(())
    }

    #[allow(exceeding_bitshifts)]
    pub fn write_u32(&mut self, val: u32) -> Result<()> {
        self.write(((val >> 24) & 0xFF) as u8)?;
        self.write(((val >> 16) & 0xFF) as u8)?;
        self.write(((val >>  8) & 0xFF) as u8)?;
        self.write(((val >>  0) & 0xFF) as u8)?;
        let shifts: &[u8] = &[24, 16, 8, 0];
        for shift in shifts.into_iter() {
            self.write(((val >> shift) & 0xFF) as u8)?;
        }
        Ok(())
    }


M src/dns_packet/dns_record.rs => src/dns_packet/dns_record.rs +6 -5
@@ 29,11 29,12 @@ impl DnsRecord {

        match qtype {
            QueryType::A => {
                let raw_addr = buffer.read_u32()?;
                let addr = Ipv4Addr::new(((raw_addr >> 24) & 0xFF) as u8,
                                         ((raw_addr >> 16) & 0xFF) as u8,
                                         ((raw_addr >>  8) & 0xFF) as u8,
                                         ((raw_addr >>  0) & 0xFF) as u8);
                let raw_addrs: Vec<u32> = (0..4).map(|_| buffer.read_u32().expect("Next raw address group")).collect();
                let mut raw_addrs_iter = raw_addrs.iter();
                let addr_groups: Vec<u8> = [24u8, 16, 8, 0].iter()
                    .map(|shift| ((raw_addrs_iter.next().expect("Next address group") >> shift) & 0xFF) as u8)
                    .collect();
                let addr = Ipv4Addr::new(addr_groups[0], addr_groups[1], addr_groups[2], addr_groups[3]);

                Ok(DnsRecord::A {
                    domain: domain,