# HELP (Rust) DNS #GT1: Forwarding Server

**URL:** <https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415>\
**Category:** Challenges\
**Tags:** challenge:dns-server\
**Created:** [May 16, 2024, 11:17pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415 "2024-05-16T23:17:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seg4lt](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/seg4lt/32/19701_2.png) [@seg4lt](https://forum.codecrafters.io/u/seg4lt)\
**Post date:** [May 16, 2024, 11:17pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/1 "2024-05-16T23:17:22Z")

</div>

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.

```rust
 /// 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?

```rust
/// 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](https://github.com/amantuladhar/rs-dns-server/blob/master/src/dns/resolver/mod.rs#L21)

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 🙂

---

<div class="post-metadata">

**Author:** ![seg4lt](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/seg4lt/32/19701_2.png) [@seg4lt](https://forum.codecrafters.io/u/seg4lt)\
**Post date:** [May 21, 2024, 6:02pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/2 "2024-05-21T18:02:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 27, 2024, 9:56pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/3 "2024-05-27T21:56:37Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 4, 2024, 1:57am UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/4 "2024-06-04T01:57:05Z")

</div>

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](https://forum.codecrafters.io/t/upcoming-change-stages-overhaul/375).

---

<div class="post-metadata">

**Author:** ![seg4lt](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/seg4lt/32/19701_2.png) [@seg4lt](https://forum.codecrafters.io/u/seg4lt)\
**Post date:** [June 4, 2024, 6:16pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/5 "2024-06-04T18:16:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 9, 2024, 6:16pm UTC](https://forum.codecrafters.io/t/help-rust-dns-gt1-forwarding-server/415/6 "2024-06-09T18:16:03Z")

</div>

Closed on June 9, 2024, 6:16pm UTC

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