Skip to content

UDP DNS: Bind to specific IP is not retained #724

@jolakeng

Description

@jolakeng

On MAC, we used the code

    dns.HandleFunc(".", handleDNSRequest)
    defer dns.HandleRemove(".")
    // start serverUDP
    serverUDP := &dns.Server{Addr: "127.0.0.1:53", Net: "udp"}
    go serverUDP.ListenAndServe()
    defer serverUDP.Shutdown()

It bound properly on specific IP and PORT as shown below:

udp4 0 0 127.0.0.1.53 .

But as soon as we write DNS response from handler function like

    msg.Authoritative = true
    domain := msg.Question[0].Name
    msg.Answer = append(msg.Answer, &dns.A{
        Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
        A:   net.ParseIP("1.2.3.4"),
    })
    w.WriteMsg(&msg)

Binding changes to as shown below:

udp4 0 0 *.53 .

This impacts other DNS server which is running on another interface.

I debugged the UDP DNS code and figured out that after the below line is executed the binding changes

conn.WriteMsgUDP(b, oob, session.raddr)

Please let me know any more information is required such that this great code can be useful in case we wanted to bind to specific IP address and retain the binding

cc: @fastest963

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions