fixed bug in qname.write_to
This commit is contained in:
parent
d9424bb885
commit
9cf1ade76a
|
@ -36,6 +36,8 @@ fn main() {
|
||||||
let mut data = vec![];
|
let mut data = vec![];
|
||||||
packet.write_to(&mut data);
|
packet.write_to(&mut data);
|
||||||
|
|
||||||
|
println!("written packet: {:?}", data);
|
||||||
|
|
||||||
upstream_socket
|
upstream_socket
|
||||||
.send_to(&data, resolver)
|
.send_to(&data, resolver)
|
||||||
.expect("error in sending data to upstream");
|
.expect("error in sending data to upstream");
|
||||||
|
|
23
src/qname.rs
23
src/qname.rs
|
@ -12,7 +12,28 @@ pub struct Qname<'a> {
|
||||||
|
|
||||||
impl<'a> Qname<'a> {
|
impl<'a> Qname<'a> {
|
||||||
pub fn write_to(&self, buf: &mut Vec<u8>) {
|
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
|
// Assume data _starts_ at the question section
|
||||||
|
|
Loading…
Reference in New Issue
Block a user