a go dns packet parser

fix error in how rname and mname were being added to soa records

+2 -2
resource_record.go
···
}
func (soa *SOA) Encode(bytes []byte, offsets *map[string]uint16) []byte {
-
bytes = append(bytes, encode_domain(bytes, soa.MName, offsets)...)
-
bytes = append(bytes, encode_domain(bytes, soa.RName, offsets)...)
+
bytes = encode_domain(bytes, soa.MName, offsets)
+
bytes = encode_domain(bytes, soa.RName, offsets)
bytes = binary.BigEndian.AppendUint32(bytes, soa.Serial)
bytes = binary.BigEndian.AppendUint32(bytes, soa.Refresh)
bytes = binary.BigEndian.AppendUint32(bytes, soa.Retry)
+38
resource_record_test.go
···
+
package magna
+
+
import (
+
// "bytes"
+
"testing"
+
+
"github.com/stretchr/testify/assert"
+
)
+
+
func TestSOADecodeWithCompression(t *testing.T) {
+
input := []byte{0x69, 0x7b, 0x81, 0x83, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xf, 0x6e, 0x6f, 0x77, 0x61, 0x79, 0x74, 0x68, 0x69, 0x73, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x3, 0x63, 0x6f, 0x6d, 0x0, 0x0, 0x1, 0x0, 0x1, 0xc0, 0x1c, 0x0, 0x6, 0x0, 0x1, 0x0, 0x0, 0x3, 0x84, 0x0, 0x3d, 0x1, 0x61, 0xc, 0x67, 0x74, 0x6c, 0x64, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x3, 0x6e, 0x65, 0x74, 0x0, 0x5, 0x6e, 0x73, 0x74, 0x6c, 0x64, 0xc, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x67, 0x72, 0x73, 0xc0, 0x1c, 0x67, 0xaa, 0xc5, 0x6b, 0x0, 0x0, 0x7, 0x8, 0x0, 0x0, 0x3, 0x84, 0x0, 0x9, 0x3a, 0x80, 0x0, 0x0, 0x3, 0x84}
+
+
msg := &Message{}
+
err := msg.Decode(input)
+
assert.NoError(t, err)
+
+
assert.Equal(t, 1, len(msg.Authority))
+
+
rr := msg.Authority[0]
+
assert.Equal(t, DNSType(6), rr.RType)
+
assert.Equal(t, DNSClass(1), rr.RClass)
+
assert.Equal(t, uint32(900), rr.TTL)
+
assert.Equal(t, uint16(61), rr.RDLength)
+
+
soa, ok := msg.Authority[0].RData.(*SOA)
+
assert.True(t, ok)
+
+
assert.Equal(t, "a.gtld-servers.net", soa.MName)
+
assert.Equal(t, "nstld.verisign-grs.com", soa.RName)
+
assert.Equal(t, uint32(1739244907), soa.Serial)
+
assert.Equal(t, uint32(1800), soa.Refresh)
+
assert.Equal(t, uint32(900), soa.Retry)
+
assert.Equal(t, uint32(604800), soa.Expire)
+
assert.Equal(t, uint32(900), soa.Minimum)
+
+
encoded := msg.Encode()
+
assert.Equal(t, input, encoded)
+
}