HELP (Rust) DNS #GT1: Forwarding Server

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 :slight_smile:

1 Like

This is very odd. I reviewed code from other members and looks like they are doing same stuff as mine. Anyone has hints on what I might be doing wrong?

Sorry we haven’t been able to help here! I’m not a Rust expert so unfortunately got to wait for someone to hopefully land on this and share some insight :slight_smile:

Note: I’ve updated the title of this post to include the stage ID (#GT1). You can learn about the stages rename here: Upcoming change: Stages overhaul.

Finalllllyyyyy… I wish test could somehow find out these small details.

Looks like the issue was I was sending qr flag as ‘reply’ (1) when forwarding the value to another dns resolver.
After I made an update to send a qr value as (0) to indicate the packet is for query it worked.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.