fixed bug in qname.write_to

This commit is contained in:
Ishan Jain 2023-12-12 05:11:03 +05:30
parent d9424bb885
commit 9cf1ade76a
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 24 additions and 1 deletions

View File

@ -36,6 +36,8 @@ fn main() {
let mut data = vec![];
packet.write_to(&mut data);
println!("written packet: {:?}", data);
upstream_socket
.send_to(&data, resolver)
.expect("error in sending data to upstream");

View File

@ -12,7 +12,28 @@ pub struct Qname<'a> {
impl<'a> Qname<'a> {
pub fn write_to(&self, buf: &mut Vec<u8>) {
buf.extend(self.name.iter());
let mut i = 0;
let mut lookup: &[u8] = &self.name;
while i < lookup.len() - 1 && lookup[i + 1] != 0 {
let is_pointer = (lookup[i] & 0xc0) == 0xc0;
if is_pointer {
let offset: u16 = (((lookup[i] & !0xc0) as u16) << 8) | lookup[i + 1] as u16;
i = offset as usize;
lookup = &self.original;
} else {
let length = lookup[i] as usize;
buf.push(length as u8);
buf.extend(&lookup[i + 1..i + 1 + length]);
i += length + 1;
}
}
buf.push(0x0);
}
// Assume data _starts_ at the question section