-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
xchg.go
140 lines (114 loc) · 2.68 KB
/
xchg.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright © by Jeff Foley 2021-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"fmt"
"strings"
"sync"
"time"
"github.com/miekg/dns"
)
// RcodeNoResponse is a special status code used to indicate no response or package error.
const RcodeNoResponse int = 50
// DefaultTimeout is the duration waited until a DNS query expires.
const DefaultTimeout = 2 * time.Second
var reqPool = sync.Pool{
New: func() interface{} {
return new(request)
},
}
type request struct {
Res *resolver
Timestamp time.Time
Msg, Resp *dns.Msg
Result chan *dns.Msg
}
func (r *request) errNoResponse() {
if r.Msg != nil {
r.Msg.Rcode = RcodeNoResponse
}
r.Result <- r.Msg
}
func (r *request) release() {
*r = request{} // Zero it out
reqPool.Put(r)
}
// The xchgMgr handles DNS message IDs and identifying messages that have timed out.
type xchgMgr struct {
sync.Mutex
timeout time.Duration
xchgs map[string]*request
}
func newXchgMgr(d time.Duration) *xchgMgr {
return &xchgMgr{
timeout: d,
xchgs: make(map[string]*request),
}
}
func xchgKey(id uint16, name string) string {
return fmt.Sprintf("%d:%s", id, strings.ToLower(RemoveLastDot(name)))
}
func (r *xchgMgr) setTimeout(d time.Duration) {
r.Lock()
defer r.Unlock()
r.timeout = d
}
func (r *xchgMgr) add(req *request) error {
r.Lock()
defer r.Unlock()
key := xchgKey(req.Msg.Id, req.Msg.Question[0].Name)
if _, found := r.xchgs[key]; found {
return fmt.Errorf("key %s is already in use", key)
}
r.xchgs[key] = req
return nil
}
func (r *xchgMgr) updateTimestamp(id uint16, name string) {
r.Lock()
defer r.Unlock()
key := xchgKey(id, name)
if _, found := r.xchgs[key]; !found {
return
}
r.xchgs[key].Timestamp = time.Now()
}
func (r *xchgMgr) remove(id uint16, name string) *request {
r.Lock()
defer r.Unlock()
key := xchgKey(id, name)
if _, found := r.xchgs[key]; found {
return r.delete([]string{key})[0]
}
return nil
}
func (r *xchgMgr) removeExpired() []*request {
r.Lock()
defer r.Unlock()
now := time.Now()
var keys []string
for key, req := range r.xchgs {
if !req.Timestamp.IsZero() && now.After(req.Timestamp.Add(r.timeout)) {
keys = append(keys, key)
}
}
return r.delete(keys)
}
func (r *xchgMgr) removeAll() []*request {
r.Lock()
defer r.Unlock()
var keys []string
for key := range r.xchgs {
keys = append(keys, key)
}
return r.delete(keys)
}
func (r *xchgMgr) delete(keys []string) []*request {
var removed []*request
for _, k := range keys {
removed = append(removed, r.xchgs[k])
r.xchgs[k] = nil
delete(r.xchgs, k)
}
return removed
}