diff options
| author | Shulhan <ms@kilabit.info> | 2021-11-14 23:52:32 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-11-15 02:11:32 +0700 |
| commit | 98c9a8bc3c169a7cbbcdccdfab8bbfb617f32488 (patch) | |
| tree | 1745e49f1e43cd9d935788ea06d5351189176255 | |
| parent | 4e1e2f28af5af6639f035c3b903fb25b6e3e5807 (diff) | |
| download | pakakeh.go-98c9a8bc3c169a7cbbcdccdfab8bbfb617f32488.tar.xz | |
lib/dns: refactoring message question
Previously the type for message question section SectionQuestion.
This changes, rename the type to MessageQuestion.
| -rw-r--r-- | lib/dns/answer_test.go | 6 | ||||
| -rw-r--r-- | lib/dns/answers_test.go | 4 | ||||
| -rw-r--r-- | lib/dns/doh_client_test.go | 16 | ||||
| -rw-r--r-- | lib/dns/dot_client_test.go | 6 | ||||
| -rw-r--r-- | lib/dns/message.go | 12 | ||||
| -rw-r--r-- | lib/dns/message_question.go | 99 | ||||
| -rw-r--r-- | lib/dns/message_question_bench_test.go | 56 | ||||
| -rw-r--r-- | lib/dns/message_question_test.go | 105 | ||||
| -rw-r--r-- | lib/dns/message_test.go | 52 | ||||
| -rw-r--r-- | lib/dns/section_question.go | 117 | ||||
| -rw-r--r-- | lib/dns/tcp_client_test.go | 6 | ||||
| -rw-r--r-- | lib/dns/udp_client_example_test.go | 2 | ||||
| -rw-r--r-- | lib/dns/udp_client_test.go | 8 | ||||
| -rw-r--r-- | lib/dns/zone_file.go | 2 | ||||
| -rw-r--r-- | lib/dns/zone_file_test.go | 38 |
15 files changed, 336 insertions, 193 deletions
diff --git a/lib/dns/answer_test.go b/lib/dns/answer_test.go index 1c083233..10651123 100644 --- a/lib/dns/answer_test.go +++ b/lib/dns/answer_test.go @@ -19,7 +19,7 @@ func TestNewAnswer(t *testing.T) { Header: MessageHeader{ ID: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "test", Type: 1, Class: 1, @@ -125,7 +125,7 @@ func TestAnswerGet(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -184,7 +184,7 @@ func TestAnswerGet(t *testing.T) { test.Assert(t, "AccessedAt", an.AccessedAt >= at, true) got := &Message{ Header: MessageHeader{}, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: gotPacket, } err := got.Unpack() diff --git a/lib/dns/answers_test.go b/lib/dns/answers_test.go index f3f758c3..503141b5 100644 --- a/lib/dns/answers_test.go +++ b/lib/dns/answers_test.go @@ -47,7 +47,7 @@ func TestNewAnswers(t *testing.T) { func TestAnswersGet(t *testing.T) { msg := &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "test", Type: 1, Class: 1, @@ -98,7 +98,7 @@ func TestAnswersGet(t *testing.T) { func TestAnswersRemove(t *testing.T) { msg := &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "test", Type: 1, Class: 1, diff --git a/lib/dns/doh_client_test.go b/lib/dns/doh_client_test.go index 3309a1ff..d1da8dfc 100644 --- a/lib/dns/doh_client_test.go +++ b/lib/dns/doh_client_test.go @@ -37,7 +37,7 @@ func TestDoHClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -65,7 +65,7 @@ func TestDoHClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -100,7 +100,7 @@ func TestDoHClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeTXT, Class: RecordClassIN, @@ -127,7 +127,7 @@ func TestDoHClient_Lookup(t *testing.T) { RCode: RCodeErrServer, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeAAAA, Class: RecordClassIN, @@ -182,7 +182,7 @@ func TestDoHClient_Post(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -210,7 +210,7 @@ func TestDoHClient_Post(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -245,7 +245,7 @@ func TestDoHClient_Post(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeTXT, Class: RecordClassIN, @@ -272,7 +272,7 @@ func TestDoHClient_Post(t *testing.T) { RCode: RCodeErrServer, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeAAAA, Class: RecordClassIN, diff --git a/lib/dns/dot_client_test.go b/lib/dns/dot_client_test.go index ee7c55fb..17afca17 100644 --- a/lib/dns/dot_client_test.go +++ b/lib/dns/dot_client_test.go @@ -35,7 +35,7 @@ func TestDoTClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -63,7 +63,7 @@ func TestDoTClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -98,7 +98,7 @@ func TestDoTClient_Lookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeTXT, Class: RecordClassIN, diff --git a/lib/dns/message.go b/lib/dns/message.go index 0fa4532a..875736d8 100644 --- a/lib/dns/message.go +++ b/lib/dns/message.go @@ -52,13 +52,13 @@ import ( // type Message struct { Header MessageHeader - Question SectionQuestion + Question MessageQuestion Answer []ResourceRecord Authority []ResourceRecord Additional []ResourceRecord - // Slice that hold the result of packing the message or original - // message from unpacking. + // The raw message as result of packing or original packet from + // unpacking. packet []byte // offset of curret packet when packing, equal to len(packet). @@ -79,7 +79,7 @@ func NewMessage() *Message { IsRD: true, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Type: RecordTypeA, Class: RecordClassIN, }, @@ -125,7 +125,7 @@ func NewMessageAddress(hname []byte, addresses [][]byte) (msg *Message) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: string(hname), Type: rtype, Class: RecordClassIN, @@ -169,7 +169,7 @@ func NewMessageFromRR(rr *ResourceRecord) (msg *Message, err error) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: rr.Name, Type: rr.Type, Class: rr.Class, diff --git a/lib/dns/message_question.go b/lib/dns/message_question.go new file mode 100644 index 00000000..5e398d8d --- /dev/null +++ b/lib/dns/message_question.go @@ -0,0 +1,99 @@ +// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import ( + "fmt" + "strings" + + libbytes "github.com/shuLhan/share/lib/bytes" +) + +// +// MessageQuestion contains the "question" in most queries. +// +type MessageQuestion struct { + // The domain name to be queried. + Name string + + // The Type of query. + Type RecordType + + // The Class of the query. + Class RecordClass +} + +// +// Reset the message question field to it's default values for query. +// +func (qst *MessageQuestion) Reset() { + qst.Name = "" + qst.Type = RecordTypeA + qst.Class = RecordClassIN +} + +func (qst *MessageQuestion) String() string { + return fmt.Sprintf("{Name:%s Type:%s}", qst.Name, RecordTypeNames[qst.Type]) +} + +// +// size return the section question size, length of name + 2 (1 octet for +// beginning size plus 1 octet for end of label) + 2 octets of +// rtype + 2 octets of rclass +// +func (qst *MessageQuestion) size() int { + return len(qst.Name) + 6 +} + +// +// unpack the DNS question section from packet. +// +func (qst *MessageQuestion) unpack(packet []byte) (err error) { + if len(packet) == 0 { + return nil + } + + var ( + logp = "MessageQuestion.unpack" + sb strings.Builder + x int + y int + count int + ) + + for { + count = int(packet[x]) + if count == 0 { + x++ + break + } + if x+count+1 >= len(packet) { + return fmt.Errorf("%s: label length overflow at index %d", logp, x) + } + if sb.Len() > 0 { + sb.WriteByte('.') + } + for y = 0; y < count; y++ { + x++ + if packet[x] >= 'A' && packet[x] <= 'Z' { + sb.WriteByte(packet[x] + 32) + } else { + sb.WriteByte(packet[x]) + } + } + x++ + } + + if x+4 > len(packet) { + return fmt.Errorf("%s: packet too small, missing type and/or class", logp) + } + + qst.Name = sb.String() + qst.Type = RecordType(libbytes.ReadUint16(packet, uint(x))) + x += 2 + qst.Class = RecordClass(libbytes.ReadUint16(packet, uint(x))) + + return nil +} diff --git a/lib/dns/message_question_bench_test.go b/lib/dns/message_question_bench_test.go new file mode 100644 index 00000000..3ae8f44c --- /dev/null +++ b/lib/dns/message_question_bench_test.go @@ -0,0 +1,56 @@ +// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import "testing" + +/** +== 2021-11-15 + +goos: linux +goarch: amd64 +pkg: github.com/shuLhan/share/lib/dns +cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz +BenchmarkMessageQuestion_String-8 7138899 168.3 ns/op 56 B/op 3 allocs/op +**/ +func BenchmarkMessageQuestion_String(b *testing.B) { + mq := MessageQuestion{ + Name: "test", + Type: RecordTypeA, + } + for x := 0; x < b.N; x++ { + _ = mq.String() + } +} + +/** +== 2021-11-14 + +goos: linux +goarch: amd64 +pkg: github.com/shuLhan/share/lib/dns +cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz +BenchmarkMessageQuestion_unpack-8 35717178 35.78 ns/op 8 B/op 1 allocs/op +**/ +func BenchmarkMessageQuestion_unpack(b *testing.B) { + var ( + packet = []byte{ + 0x01, 'a', + 0x01, 'B', + 0x01, 'c', + 0x00, + 0x00, 0x01, + 0x00, 0x01, + } + err error + ) + mq := MessageQuestion{} + for x := 0; x < b.N; x++ { + err = mq.unpack(packet) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/lib/dns/message_question_test.go b/lib/dns/message_question_test.go new file mode 100644 index 00000000..3bde97a3 --- /dev/null +++ b/lib/dns/message_question_test.go @@ -0,0 +1,105 @@ +// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import ( + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestMessageQuestion_String(t *testing.T) { + cases := []struct { + desc string + mq MessageQuestion + exp string + }{{ + desc: "With unknown type", + mq: MessageQuestion{ + Name: "test", + Type: RecordType(17), + }, + exp: `{Name:test Type:}`, + }, { + desc: "With known type", + mq: MessageQuestion{ + Name: "test", + Type: RecordTypeA, + }, + exp: `{Name:test Type:A}`, + }} + + for _, c := range cases { + test.Assert(t, c.desc, c.exp, c.mq.String()) + } +} + +func TestMessageQuestion_unpack(t *testing.T) { + cases := []struct { + desc string + mq MessageQuestion + packet []byte + expErr string + }{{ + desc: "With empty packet", + mq: MessageQuestion{}, + }, { + desc: "With zero label", + packet: []byte{ + 0x00, + 0x00, 0x01, + 0x00, 0x01, + }, + mq: MessageQuestion{ + Name: "", + Type: RecordTypeA, + Class: RecordClassIN, + }, + }, { + desc: "With invalid label length", + packet: []byte{ + 0x06, 'a', + 0x0, + 0x00, 0x01, + 0x00, 0x01, + }, + expErr: "MessageQuestion.unpack: label length overflow at index 0", + }, { + desc: "With packet too small", + packet: []byte{ + 0x01, 'a', + 0x00, + 0x00, 0x01, + 0x00, + }, + expErr: "MessageQuestion.unpack: packet too small, missing type and/or class", + }, { + desc: "With label", + packet: []byte{ + 0x01, 'a', + 0x01, 'B', + 0x01, 'c', + 0x00, + 0x00, 0x01, + 0x00, 0x01, + }, + mq: MessageQuestion{ + Name: "a.b.c", + Type: RecordTypeA, + Class: RecordClassIN, + }, + }} + + for _, c := range cases { + t.Log(c.desc) + gotMQ := MessageQuestion{} + err := gotMQ.unpack(c.packet) + if err != nil { + test.Assert(t, c.desc, c.expErr, err.Error()) + continue + } + test.Assert(t, c.desc, c.mq, gotMQ) + } +} diff --git a/lib/dns/message_test.go b/lib/dns/message_test.go index 3b57d435..0a2ce360 100644 --- a/lib/dns/message_test.go +++ b/lib/dns/message_test.go @@ -84,7 +84,7 @@ func TestMessagePackQuestion(t *testing.T) { }{{ desc: "Empty name", msg: &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Type: RecordTypeA, Class: RecordClassIN, }, @@ -96,7 +96,7 @@ func TestMessagePackQuestion(t *testing.T) { }, { desc: "Single domain name", msg: &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilaBit", Type: RecordTypeA, Class: RecordClassIN, @@ -110,7 +110,7 @@ func TestMessagePackQuestion(t *testing.T) { }, { desc: "Two domain names", msg: &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -126,7 +126,7 @@ func TestMessagePackQuestion(t *testing.T) { }, { desc: "Three domain names", msg: &Message{ - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "MAIL.KILABIT.INFO", Type: RecordTypeA, Class: RecordClassIN, @@ -164,7 +164,7 @@ func TestMessagePack(t *testing.T) { IsQuery: true, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.INFO", Type: RecordTypeA, Class: RecordClassIN, @@ -197,7 +197,7 @@ func TestMessagePack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -286,7 +286,7 @@ func TestMessagePack(t *testing.T) { NSCount: 0, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeNS, Class: RecordClassIN, @@ -352,7 +352,7 @@ func TestMessagePack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "mail.kilabit.info", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -396,7 +396,7 @@ func TestMessagePack(t *testing.T) { IsRA: true, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -483,7 +483,7 @@ func TestMessagePack(t *testing.T) { ANCount: 5, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeMX, Class: RecordClassIN, @@ -581,7 +581,7 @@ func TestMessagePack(t *testing.T) { IsRA: true, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeTXT, Class: RecordClassIN, @@ -642,7 +642,7 @@ func TestMessagePack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeAAAA, Class: RecordClassIN, @@ -740,7 +740,7 @@ func TestMessagePack(t *testing.T) { ANCount: 5, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "_xmpp-server._tcp.google.com", Type: RecordTypeSRV, Class: RecordClassIN, @@ -843,7 +843,7 @@ func TestMessageSetAuthoritativeAnswer(t *testing.T) { IsAA: true, IsRD: true, }, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: make([]byte, maxUdpPacketSize), dnameOff: make(map[string]uint16), } @@ -860,7 +860,7 @@ func TestMessageSetAuthoritativeAnswer(t *testing.T) { IsRD: true, IsRA: true, }, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: make([]byte, maxUdpPacketSize), dnameOff: make(map[string]uint16), } @@ -912,7 +912,7 @@ func TestMessageSetQuery(t *testing.T) { IsAA: true, IsRD: true, }, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: make([]byte, maxUdpPacketSize), dnameOff: make(map[string]uint16), } @@ -955,7 +955,7 @@ func TestMessageSetRecursionDesired(t *testing.T) { IsAA: true, IsRD: true, }, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: make([]byte, maxUdpPacketSize), dnameOff: make(map[string]uint16), } @@ -998,7 +998,7 @@ func TestMessageSetResponseCode(t *testing.T) { IsAA: true, IsRD: true, }, - Question: SectionQuestion{}, + Question: MessageQuestion{}, packet: make([]byte, maxUdpPacketSize), dnameOff: make(map[string]uint16), } @@ -1069,7 +1069,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -1142,7 +1142,7 @@ func TestMessageUnpack(t *testing.T) { NSCount: 0, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeNS, Class: RecordClassIN, @@ -1220,7 +1220,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "mail.kilabit.info", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -1278,7 +1278,7 @@ func TestMessageUnpack(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -1375,7 +1375,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 5, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeMX, Class: RecordClassIN, @@ -1522,7 +1522,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 3, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeTXT, Class: RecordClassIN, @@ -1612,7 +1612,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 1, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "google.com", Type: RecordTypeAAAA, Class: RecordClassIN, @@ -1716,7 +1716,7 @@ func TestMessageUnpack(t *testing.T) { ANCount: 5, ARCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "_xmpp-server._tcp.google.com", Type: RecordTypeSRV, Class: RecordClassIN, diff --git a/lib/dns/section_question.go b/lib/dns/section_question.go deleted file mode 100644 index c4a2cd71..00000000 --- a/lib/dns/section_question.go +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package dns - -import ( - "fmt" - "strings" - - libbytes "github.com/shuLhan/share/lib/bytes" -) - -// -// SectionQuestion The question section is used to carry the "question" in -// most queries, i.e., the parameters that define what is being asked. The -// section contains QDCOUNT (usually 1) entries, each of the following format: -// -type SectionQuestion struct { - // A domain name represented as a sequence of labels, where each label - // consists of a length octet followed by that number of octets. The - // domain name terminates with the zero length octet for the null - // label of the root. Note that this field may be an odd number of - // octets; no padding is used. - Name string - - // A two octet code which specifies the type of the query. The values - // for this field include all codes valid for a TYPE field, together - // with some more general codes which can match more than one type of - // RR. - Type RecordType - - // A two octet code that specifies the class of the query. For - // example, the QCLASS field is IN for the Internet. - Class RecordClass -} - -// -// Reset the message question field to it's default values for query. -// -func (question *SectionQuestion) Reset() { - question.Name = question.Name[:0] - question.Type = RecordTypeA - question.Class = RecordClassIN -} - -// -// size return the section question size, length of name + 2 (1 octet for -// beginning size plus 1 octet for end of label) + 2 octets of -// rtype + 2 octets of rclass -// -func (question *SectionQuestion) size() int { - return len(question.Name) + 6 -} - -// -// String will return the string representation of section question structure. -// -func (question *SectionQuestion) String() string { - return fmt.Sprintf("&{Name:%s Type:%s}", question.Name, - RecordTypeNames[question.Type]) -} - -// -// unpack the DNS question section. -// -func (question *SectionQuestion) unpack(packet []byte) (err error) { - if len(packet) == 0 { - return nil - } - - var sb strings.Builder - count := packet[0] - x := 1 - - for { - if count == 0 { - sb.WriteByte('.') - count = packet[x] - x++ - if x >= len(packet) { - return fmt.Errorf("SectionQuestion.unpack: invalid question %q", packet) - } - continue - } - for y := byte(0); y < count; y++ { - if packet[x] >= 'A' && packet[x] <= 'Z' { - packet[x] += 32 - } - sb.WriteByte(packet[x]) - x++ - if x >= len(packet) { - return fmt.Errorf("SectionQuestion.unpack: invalid question %q", packet) - } - } - count = packet[x] - x++ - if count == 0 { - break - } - if x >= len(packet) { - return fmt.Errorf("SectionQuestion.unpack: invalid question %q", packet) - } - sb.WriteByte('.') - } - - if x+4 > len(packet) { - return fmt.Errorf("SectionQuestion.unpack: invalid question %q", packet) - } - - question.Name = sb.String() - question.Type = RecordType(libbytes.ReadUint16(packet, uint(x))) - x += 2 - question.Class = RecordClass(libbytes.ReadUint16(packet, uint(x))) - - return nil -} diff --git a/lib/dns/tcp_client_test.go b/lib/dns/tcp_client_test.go index 7726bfc3..460d81a5 100644 --- a/lib/dns/tcp_client_test.go +++ b/lib/dns/tcp_client_test.go @@ -35,7 +35,7 @@ func TestTCPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -63,7 +63,7 @@ func TestTCPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -98,7 +98,7 @@ func TestTCPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeTXT, Class: RecordClassIN, diff --git a/lib/dns/udp_client_example_test.go b/lib/dns/udp_client_example_test.go index 0ccd9951..ec179633 100644 --- a/lib/dns/udp_client_example_test.go +++ b/lib/dns/udp_client_example_test.go @@ -20,7 +20,7 @@ func ExampleUDPClient() { req := &dns.Message{ Header: dns.MessageHeader{}, - Question: dns.SectionQuestion{ + Question: dns.MessageQuestion{ Name: "kilabit.info", Type: dns.RecordTypeA, Class: dns.RecordClassIN, diff --git a/lib/dns/udp_client_test.go b/lib/dns/udp_client_test.go index 44489500..e00b1d24 100644 --- a/lib/dns/udp_client_test.go +++ b/lib/dns/udp_client_test.go @@ -35,7 +35,7 @@ func TestUDPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -63,7 +63,7 @@ func TestUDPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeSOA, Class: RecordClassIN, @@ -98,7 +98,7 @@ func TestUDPClientLookup(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeTXT, Class: RecordClassIN, @@ -125,7 +125,7 @@ func TestUDPClientLookup(t *testing.T) { RCode: RCodeErrServer, QDCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.info", Type: RecordTypeAAAA, Class: RecordClassIN, diff --git a/lib/dns/zone_file.go b/lib/dns/zone_file.go index 896f9df9..80909c39 100644 --- a/lib/dns/zone_file.go +++ b/lib/dns/zone_file.go @@ -166,7 +166,7 @@ func (zone *ZoneFile) Add(rr *ResourceRecord) (err error) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: rr.Name, Type: rr.Type, Class: rr.Class, diff --git a/lib/dns/zone_file_test.go b/lib/dns/zone_file_test.go index c69b1fa2..080cb6ca 100644 --- a/lib/dns/zone_file_test.go +++ b/lib/dns/zone_file_test.go @@ -169,7 +169,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "isi.edu", Type: RecordTypeSOA, Class: RecordClassIN, @@ -195,7 +195,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 3, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "isi.edu", Type: RecordTypeNS, Class: RecordClassIN, @@ -225,7 +225,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 2, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "isi.edu", Type: RecordTypeMX, Class: RecordClassIN, @@ -255,7 +255,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "a.isi.edu", Type: RecordTypeA, Class: RecordClassIN, @@ -273,7 +273,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 2, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "venera.isi.edu", Type: RecordTypeA, Class: RecordClassIN, @@ -297,7 +297,7 @@ VAXA A 10.2.0.27 QDCount: 1, ANCount: 2, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "vaxa.isi.edu", Type: RecordTypeA, Class: RecordClassIN, @@ -400,7 +400,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "pcguide.com", Type: RecordTypeSOA, Class: RecordClassIN, @@ -426,7 +426,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 2, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "pcguide.com", Type: RecordTypeNS, Class: RecordClassIN, @@ -450,7 +450,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "localhost.pcguide.com", Type: RecordTypeA, Class: RecordClassIN, @@ -468,7 +468,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "pcguide.com", Type: RecordTypeA, Class: RecordClassIN, @@ -486,7 +486,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "pcguide.com", Type: RecordTypeMX, Class: RecordClassIN, @@ -507,7 +507,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "www.pcguide.com", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -525,7 +525,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "ftp.pcguide.com", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -543,7 +543,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "mail.pcguide.com", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -561,7 +561,7 @@ relay IN CNAME relay.pair.com. QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "relay.pcguide.com", Type: RecordTypeCNAME, Class: RecordClassIN, @@ -646,7 +646,7 @@ angularjs.doc A 127.0.0.1 QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "dev.kilabit.info", Type: RecordTypeA, Class: RecordClassIN, @@ -664,7 +664,7 @@ angularjs.doc A 127.0.0.1 QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "dev.kilabit.com", Type: RecordTypeA, Class: RecordClassIN, @@ -682,7 +682,7 @@ angularjs.doc A 127.0.0.1 QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "angularjs.doc.localdomain", Type: RecordTypeA, Class: RecordClassIN, @@ -755,7 +755,7 @@ func TestZoneParseTXT(t *testing.T) { QDCount: 1, ANCount: 1, }, - Question: SectionQuestion{ + Question: MessageQuestion{ Name: "kilabit.local", Type: RecordTypeTXT, Class: RecordClassIN, |
