I’m stuck on stage 8 of dns resolver. I am trying to forward request to resolver and it doesn’t seem to work.
I’ve tried to use UdpSocket::send_to(..)
and UdpSocket::recv_from(..)
and this doesn’t seem to work. I get response here, but it looks like the response I receive is not from the resolver.
/// Try to resolve with existing socket
pub fn resolve(&self, socket: &UdpSocket, packets: Vec<Packet>) -> Vec<Packet> {
let mut buf = [0; 512];
let r_addr = &self.addr.parse::<SocketAddr>().unwrap();
let packets = packets
.iter()
.map(|p| {
tracing::debug!("Sending packet to resolver: HeaderId({})", p.header.id);
socket
.send_to(&p.as_bytes(), r_addr)
.context(fdbg!("Unable to send to resolver address"))
.unwrap();
let size = socket
.recv_from(&mut buf)
.context(fdbg!("Unable to receive from resolver"))
.unwrap();
let mut dns_reader = DnsReader::new(&buf);
let received_packet = Packet::parse(&mut dns_reader);
debug!("Received packet from resolver: {received_packet:#?}");
received_packet
})
.collect::<Vec<_>>();
packets
}
I tried to bind new UdpSocket and connect to resolver address. But when I do that I don’t even receive any message.
What am I missing?
/// Try to resolve by creating new UdpSocket
pub fn resolve_with_new_socket(&self, packets: Vec<Packet>) -> Vec<Packet> {
let mut buf = [0; 512];
let r_addr = &self
.addr
.parse::<SocketAddr>()
.context(fdbg!("Unable to parse address"))
.unwrap();
let packets = packets
.iter()
.map(|p| {
let r_socket = UdpSocket::bind("0.0.0.0:0")
.context(fdbg!("Unable to bind UDP socket"))
.unwrap();
r_socket
.connect(r_addr)
.context(fdbg!("Unable to connect to resolver"))
.unwrap();
r_socket
.send(&p.as_bytes())
.context(fdbg!("Unable to send to resolver address"))
.unwrap();
let size = r_socket
.recv(&mut buf)
.context(fdbg!("Unable to receive from resolver"))
.unwrap();
let mut dns_reader = DnsReader::new(&buf);
let received_packet = Packet::parse(&mut dns_reader);
debug!("received packet from resolver: {received_packet:#?}");
received_packet
})
.collect::<Vec<_>>();
packets
}
Here’s a github repo link if longer context is needed: rs-dns-server/src/dns/resolver/mod.rs at master · amantuladhar/rs-dns-server · GitHub
Any help from anyone would be greatly appreciated.
P.S. I am very new to Rust and definitely new to these byte / bit parsing. I might be doing some dumb mistake