| 1 |
package dnstest |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"net/netip" |
| 6 |
|
| 7 |
"codeberg.org/miekg/dns" |
| 8 |
) |
| 9 |
|
| 10 |
const port = 40212 |
| 11 |
|
| 12 |
// ResponseWriter is useful for writing tests. It uses some fixed values for the client. The |
| 13 |
// remote will always be 198.51.100.1 ([IPv4], see RFC 5737) and port 40212. |
| 14 |
// The local address is always 127.0.0.1 and port 53. See [ResponseWriter6] for an IPv6 version. |
| 15 |
type ResponseWriter struct { |
| 16 |
TCP bool // if TCP is true, this is a TCP connection instead of an UDP one. |
| 17 |
} |
| 18 |
|
| 19 |
// ResponseWriter6 returns fixed client and remote address in IPv6. The remote |
| 20 |
// address is always and 2001:db8::1 ([IPv6], see RFC 5156) and port 40212. |
| 21 |
// The local address is always ::1 and port 53. See [ResponseWriter] for an IPv4 version. |
| 22 |
type ResponseWriter6 struct { |
| 23 |
ResponseWriter |
| 24 |
} |
| 25 |
|
| 26 |
// IP addresses for documentation and test purposes. |
| 27 |
var ( |
| 28 |
IPv4 = netip.MustParseAddr("198.51.100.1") // RFC 5737 |
| 29 |
IPv6 = netip.MustParseAddr("2001:db8::1") // RFC 5156 |
| 30 |
|
| 31 |
localhostv4 = net.ParseIP("127.0.0.1") |
| 32 |
localhostv6 = net.ParseIP("::1") |
| 33 |
) |
| 34 |
|
| 35 |
func (t *ResponseWriter6) LocalAddr() net.Addr { |
| 36 |
if t.TCP { |
| 37 |
return &net.TCPAddr{IP: localhostv6, Port: 53} |
| 38 |
} |
| 39 |
return &net.UDPAddr{IP: localhostv6, Port: 53} |
| 40 |
} |
| 41 |
|
| 42 |
func (t *ResponseWriter6) RemoteAddr() net.Addr { |
| 43 |
if t.TCP { |
| 44 |
return &net.TCPAddr{IP: IPv6.AsSlice(), Port: port} |
| 45 |
} |
| 46 |
return &net.UDPAddr{IP: IPv6.AsSlice(), Port: port} |
| 47 |
} |
| 48 |
|
| 49 |
func (t *ResponseWriter) LocalAddr() net.Addr { |
| 50 |
if t.TCP { |
| 51 |
return &net.TCPAddr{IP: localhostv4, Port: 53} |
| 52 |
} |
| 53 |
return &net.UDPAddr{IP: localhostv4, Port: 53} |
| 54 |
} |
| 55 |
|
| 56 |
func (t *ResponseWriter) RemoteAddr() net.Addr { |
| 57 |
if t.TCP { |
| 58 |
return &net.TCPAddr{IP: IPv4.AsSlice(), Port: port} |
| 59 |
} |
| 60 |
return &net.UDPAddr{IP: IPv4.AsSlice(), Port: port} |
| 61 |
} |
| 62 |
|
| 63 |
func (t *ResponseWriter) Write(buf []byte) (int, error) { return len(buf), nil } |
| 64 |
func (t *ResponseWriter) Conn() net.Conn { return nil } |
| 65 |
func (t *ResponseWriter) Session() *dns.Session { return nil } |
| 66 |
func (t *ResponseWriter) Close() error { return nil } |
| 67 |
func (t *ResponseWriter) Hijack() {} |