/
answers.go
70 lines (63 loc) · 1.59 KB
/
answers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2019, 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
// answers contains list of answer with the same query name but different
// query types.
type answers struct {
v []*Answer
}
// newAnswers create and initialize list of answer with one element.
func newAnswers(an *Answer) (ans *answers) {
ans = &answers{
v: make([]*Answer, 0, 1),
}
if an != nil && an.msg != nil {
ans.v = append(ans.v, an)
}
return
}
// get an answer with specific query type and class in slice.
// If found, it will return its element and index in slice; otherwise it will
// return nil on answer.
func (ans *answers) get(rtype RecordType, rclass RecordClass) (an *Answer, x int) {
for x = 0; x < len(ans.v); x++ {
if ans.v[x].RType != rtype {
continue
}
if ans.v[x].RClass != rclass {
continue
}
an = ans.v[x]
return
}
return
}
// remove the answer from list.
func (ans *answers) remove(rtype RecordType, rclass RecordClass) {
var (
an *Answer
x int
)
an, x = ans.get(rtype, rclass)
if an != nil {
ans.v[x] = ans.v[len(ans.v)-1]
ans.v[len(ans.v)-1] = nil
ans.v = ans.v[:len(ans.v)-1]
}
}
// upsert update or insert new answer to list.
// If new answer is updated, it will return the old answer.
// If new answer is inserted, it will return nil instead.
func (ans *answers) upsert(nu *Answer) (an *Answer) {
if nu == nil || nu.msg == nil {
return
}
an, _ = ans.get(nu.RType, nu.RClass)
if an != nil {
an.update(nu)
} else {
ans.v = append(ans.v, nu)
}
return
}