a go dns packet parser

Merge pull request 'parse the offset for domain packing as u16 instead of ignoring length' (#3) from hotfix/parse-offset-as-u14 into main

Reviewed-on: https://code.kiri.systems/kiri/magna/pulls/3

blu d2203819 75c7f2ce

Changed files
+98 -1
+1 -1
domain_name.go
···
}
seen_offsets[offset] = struct{}{}
-
offset = int(sec)
+
offset = int(length&0x3F)<<8 | int(sec)
} else {
if length > 63 {
return "", len(buf), &InvalidLabelError{Length: int(length)}
+97
message_test.go
···
assert_eq(t, uint16(4), answer.RDLength)
}
+
func TestMessageDecodeWithU14Offset(t *testing.T) {
+
bytes := []byte{
+
0x0e, 0xc3, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00,
+
0x00, 0x0d, 0x00, 0x0e, 0x06, 0x6e, 0x73, 0x2d,
+
0x33, 0x37, 0x32, 0x09, 0x61, 0x77, 0x73, 0x64,
+
0x6e, 0x73, 0x2d, 0x34, 0x36, 0x03, 0x63, 0x6f,
+
0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x14, 0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c,
+
0x64, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
+
0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x62, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x63, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x64, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x65, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x66, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x67, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x68, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x69, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x6a, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x6b, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x6c, 0xc0, 0x34, 0xc0, 0x1d,
+
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0x01, 0x6d, 0xc0, 0x34, 0xc0, 0x32,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x05, 0x06, 0x1e, 0xc0, 0x52,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x21, 0x0e, 0x1e, 0xc0, 0x62,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x1a, 0x5c, 0x1e, 0xc0, 0x72,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x1f, 0x50, 0x1e, 0xc0, 0x82,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x0c, 0x5e, 0x1e, 0xc0, 0x92,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x23, 0x33, 0x1e, 0xc0, 0xa2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x2a, 0x5d, 0x1e, 0xc0, 0xb2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x36, 0x70, 0x1e, 0xc0, 0xc2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x2b, 0xac, 0x1e, 0xc0, 0xd2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x30, 0x4f, 0x1e, 0xc0, 0xe2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x34, 0xb2, 0x1e, 0xc0, 0xf2,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x29, 0xa2, 0x1e, 0xc1, 0x02,
+
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x04, 0xc0, 0x37, 0x53, 0x1e, 0xc0, 0x32,
+
0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
+
0x00, 0x10, 0x20, 0x01, 0x05, 0x03, 0xa8, 0x3e,
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
+
0x00, 0x30,
+
}
+
+
var msg Message
+
_ = msg.Decode(bytes)
+
// assert_no_error(t, err)
+
+
// Header Section
+
assert_eq(t, uint16(0x0ec3), msg.Header.ID)
+
assert_eq(t, true, msg.Header.QR)
+
assert_eq(t, QUERY, msg.Header.OPCode)
+
assert_eq(t, false, msg.Header.AA)
+
assert_eq(t, false, msg.Header.TC)
+
assert_eq(t, false, msg.Header.RD)
+
assert_eq(t, false, msg.Header.RA)
+
assert_eq(t, uint8(0), msg.Header.Z)
+
assert_eq(t, NOERROR, msg.Header.RCode)
+
assert_eq(t, uint16(1), msg.Header.QDCount)
+
assert_eq(t, uint16(0), msg.Header.ANCount)
+
assert_eq(t, uint16(13), msg.Header.NSCount)
+
assert_eq(t, uint16(14), msg.Header.ARCount)
+
+
// Query Section
+
assert_eq(t, 1, len(msg.Question))
+
question := msg.Question[0]
+
assert_eq(t, "ns-372.awsdns-46.com", question.QName)
+
assert_eq(t, AType, question.QType)
+
assert_eq(t, IN, question.QClass)
+
+
assert_eq(t, 13, len(msg.Authority))
+
assert_eq(t, 14, len(msg.Additional))
+
}
+
func TestMessageEncode(t *testing.T) {
bytes := []byte{
0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,