Skip to content

Commit

Permalink
add HOST1 helper
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Aug 7, 2022
1 parent 6b02eef commit b33b81b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (h *DNSHandler) ServeDNS(rw fastdns.ResponseWriter, req *fastdns.Message) {

switch req.Question.Type {
case fastdns.TypeA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 8, 8})})
fastdns.HOST1(rw, req, 60, netip.AddrFrom4([4]byte{8, 8, 8, 8}))
case fastdns.TypeAAAA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr("2001:4860:4860::8888")})
case fastdns.TypeCNAME:
fastdns.CNAME(rw, req, 60, []string{"dns.google"}, []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 4, 4})})
fastdns.CNAME(rw, req, 60, []string{"dns.google"}, []netip.Addr{netip.MustParseAddr("8.8.8.8")})
case fastdns.TypeSRV:
fastdns.SRV(rw, req, 60, []net.SRV{{"www.google.com", 443, 1000, 1000}})
case fastdns.TypeNS:
Expand Down
7 changes: 7 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ func Error(rw ResponseWriter, req *Message, rcode Rcode) {
_, _ = rw.Write(req.Raw)
}

// HOST1 replies to the request with the specified Host record.
func HOST1(rw ResponseWriter, req *Message, ttl uint32, ip netip.Addr) {
req.SetResponseHeader(RcodeNoError, 1)
req.Raw = AppendHOST1Record(req.Raw, req, ttl, ip)
_, _ = rw.Write(req.Raw)
}

// HOST replies to the request with the specified Host records.
func HOST(rw ResponseWriter, req *Message, ttl uint32, ips []netip.Addr) {
req.SetResponseHeader(RcodeNoError, uint16(len(ips)))
Expand Down
30 changes: 30 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func TestHandlerError(t *testing.T) {
}
}

func TestHandlerHost1(t *testing.T) {
var cases = []struct {
Hex string
IP netip.Addr
TTL uint32
}{
{
"00028180000100010000000002686b0470687573026c750000010001c00c000100010000012c000401020408",
netip.AddrFrom4([4]byte{1, 2, 4, 8}),
300,
},
}

rw, req := &MemResponseWriter{}, mockMessage()
for _, c := range cases {
HOST1(rw, req, c.TTL, c.IP)
if got, want := hex.EncodeToString(rw.Data), c.Hex; got != want {
t.Errorf("HOST1(%v) error got=%#v want=%#v", c.IP, got, want)
}
}
}

func TestHandlerHost(t *testing.T) {
var cases = []struct {
Hex string
Expand Down Expand Up @@ -243,6 +265,14 @@ func (rw *nilResponseWriter) LocalAddr() netip.AddrPort { return netip.AddrPort{

func (rw *nilResponseWriter) Write(p []byte) (n int, err error) { return len(p), nil }

func BenchmarkHOST1(b *testing.B) {
req := mockMessage()
ip := netip.AddrFrom4([4]byte{8, 8, 8, 8})
for i := 0; i < b.N; i++ {
HOST1(&nilResponseWriter{}, req, 3000, ip)
}
}

func BenchmarkHOST(b *testing.B) {
req := mockMessage()
ips := []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 8, 8})}
Expand Down
43 changes: 43 additions & 0 deletions record_be.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ func AppendHOSTRecord(dst []byte, req *Message, ttl uint32, ips []netip.Addr) []
return dst
}

// AppendHOST1Record appends a Host record to dst and returns the resulting dst.
func AppendHOST1Record(dst []byte, req *Message, ttl uint32, ip netip.Addr) []byte {
b := (*[16]byte)(unsafe.Pointer(&ip))
if ip.Is4() {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x04,
// RDATA
b[8], b[9], b[10], b[11],
}
dst = append(dst, answer[:]...)
} else {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeAAAA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x10,
// RDATA
b[8], b[9], b[10], b[11],
b[12], b[13], b[14], b[15],
b[0], b[1], b[2], b[3],
b[4], b[5], b[6], b[7],
}
dst = append(dst, answer[:]...)
}

return dst
}

// AppendCNAMERecord appends the CNAME and Host records to dst and returns the resulting dst.
func AppendCNAMERecord(dst []byte, req *Message, ttl uint32, cnames []string, ips []netip.Addr) []byte {
offset := 0x0c
Expand Down
43 changes: 43 additions & 0 deletions record_le.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,49 @@ import (
"unsafe"
)

// AppendHOST1Record appends a Host records to dst and returns the resulting dst.
func AppendHOST1Record(dst []byte, req *Message, ttl uint32, ip netip.Addr) []byte {
b := (*[16]byte)(unsafe.Pointer(&ip))
if ip.Is4() {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x04,
// RDATA
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
} else {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeAAAA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x10,
// RDATA
b[7], b[6], b[5], b[4],
b[3], b[2], b[1], b[0],
b[15], b[14], b[13], b[12],
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
}

return dst
}

// AppendHOSTRecord appends the Host records to dst and returns the resulting dst.
func AppendHOSTRecord(dst []byte, req *Message, ttl uint32, ips []netip.Addr) []byte {
for _, ip := range ips {
Expand Down

0 comments on commit b33b81b

Please sign in to comment.