Open a UDP socket listening on port 53, this will receive incoming DNS queries. Notice that UDP only needs 1 socket to handle multiple “connections”, meanwhile TCP is 1 socket per connection. So we will reuse conn throughout the program.

conn, _ = net.ListenUDP("udp", &net.UDPAddr{Port: 53}) defer conn.Close() for { buf := make([]byte, 512) _, addr, _ := conn.ReadFromUDP(buf) ...

}

Parse a packet to see if it’s a DNS message.

var m dnsmessage.Message err = m.Unpack(buf)

In case you are curious how a DNS message looks like: