| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"net" |
| 6 |
"sync/atomic" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
// A ResponseWriter interface is used by a DNS [Handler] to construct a DNS response. Note that a response |
| 11 |
// writer may be used concurrently with TCP pipelining, so be aware that writes need to be atomic. If a write |
| 12 |
// is attmpted an the Data buffer in the message is empty the write method will call [Pack]. |
| 13 |
// |
| 14 |
// If a ResponseWriter also implements [ResponseController] a write deadline can be set, there is no default. |
| 15 |
// The default ResponseWriter uses a timeout of 2s. |
| 16 |
type ResponseWriter interface { |
| 17 |
// LocalAddr returns the net.Addr of the server. |
| 18 |
LocalAddr() net.Addr |
| 19 |
// RemoteAddr returns the net.Addr of the client that sent the current request. |
| 20 |
RemoteAddr() net.Addr |
| 21 |
// Conn returns the underlaying connection. You can get the connection's TLS state via |
| 22 |
// Conn().(*tls.Conn).ConnectionState(). |
| 23 |
Conn() net.Conn |
| 24 |
// ResponseWriter must also implement the io.Writer interface. |
| 25 |
Write([]byte) (int, error) |
| 26 |
// And the io.Closer interface, for use when hijacking a TCP connection. |
| 27 |
Close() error |
| 28 |
// Session returns the UDP oob session data to correctly route UDP packets. |
| 29 |
Session() *Session |
| 30 |
// Hijack lets the caller take over a TCP connection. For UDP this has no effect. The [Handler] is then |
| 31 |
// responsible for the connection. Packets will still be read and given to the handler, [MaxTCPQueries] will |
| 32 |
// be ignored, and the client needs to call [Close]. Use [Conn] to check the connection's state. |
| 33 |
Hijack() |
| 34 |
} |
| 35 |
|
| 36 |
// A ResponseController is used by an DNS handler to control the DNS response. |
| 37 |
type ResponseController interface { |
| 38 |
SetWriteDeadline() error // SetWriteDeadline sets the deadline for writing the response. |
| 39 |
} |
| 40 |
|
| 41 |
// response implements response.Writer. This struct is read-only execpt for hijacked. |
| 42 |
type response struct { |
| 43 |
session *Session // used for UDP reply routing. |
| 44 |
conn net.Conn |
| 45 |
hijacked atomic.Bool |
| 46 |
} |
| 47 |
|
| 48 |
// SetWriteDeadline implements the [ResponseController] interface. |
| 49 |
func (w *response) SetWriteDeadline() error { |
| 50 |
return w.conn.SetWriteDeadline(time.Now().Add(2 * time.Second)) |
| 51 |
} |
| 52 |
func (w *response) Conn() net.Conn { return w.conn } |
| 53 |
func (w *response) Session() *Session { return w.session } |
| 54 |
func (w *response) Write(p []byte) (n int, err error) { return w.conn.Write(p) } |
| 55 |
func (w *response) Read(p []byte) (n int, err error) { return w.conn.Read(p) } |
| 56 |
func (w *response) LocalAddr() net.Addr { return w.conn.LocalAddr() } |
| 57 |
func (w *response) Hijack() { w.hijacked.Store(true) } |
| 58 |
|
| 59 |
// RemoteAddr implements the [ResponseWriter] interface. |
| 60 |
func (w *response) RemoteAddr() net.Addr { |
| 61 |
if _, ok := w.conn.(*net.UDPConn); ok { |
| 62 |
return w.Session().Addr |
| 63 |
} |
| 64 |
return w.conn.RemoteAddr() |
| 65 |
} |
| 66 |
|
| 67 |
// Close implements the [ResponseWriter] interface. For UDP this is a noop. |
| 68 |
func (w *response) Close() error { |
| 69 |
if _, ok := w.conn.(*net.UDPConn); ok { |
| 70 |
return nil |
| 71 |
} |
| 72 |
sock, ok := w.conn.(io.Closer) |
| 73 |
if !ok { |
| 74 |
return nil |
| 75 |
} |
| 76 |
return sock.Close() |
| 77 |
} |