-
Notifications
You must be signed in to change notification settings - Fork 31
/
dns.go
241 lines (211 loc) · 6.29 KB
/
dns.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/kaspanet/kaspad/app/appmessage"
"net"
"os"
"strings"
"sync/atomic"
"time"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
"github.com/kaspanet/kaspad/infrastructure/network/dnsseed"
"github.com/pkg/errors"
"github.com/miekg/dns"
)
// DNSServer struct
type DNSServer struct {
hostname string
listen string
nameserver string
}
// Start - starts server
func (d *DNSServer) Start() {
defer wg.Done()
rr := fmt.Sprintf("%s 86400 IN NS %s", d.hostname, d.nameserver)
authority, err := dns.NewRR(rr)
if err != nil {
log.Infof("NewRR: %v", err)
return
}
udpAddr, err := net.ResolveUDPAddr("udp4", d.listen)
if err != nil {
log.Infof("ResolveUDPAddr: %v", err)
return
}
udpListen, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Infof("ListenUDP: %v", err)
return
}
defer udpListen.Close()
for {
b := make([]byte, 512)
mainLoop:
err := udpListen.SetReadDeadline(time.Now().Add(time.Second))
if err != nil {
log.Infof("SetReadDeadline: %v", err)
os.Exit(1)
}
_, addr, err := udpListen.ReadFromUDP(b)
if err != nil {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
if atomic.LoadInt32(&systemShutdown) == 0 {
// use goto in order to do not re-allocate 'b' buffer
goto mainLoop
}
log.Infof("DNS server shutdown")
return
}
var opErr *net.OpError
if errors.As(err, &opErr) {
log.Infof("Read: %T", opErr.Err)
} else {
log.Errorf("Unknown error: %s", err)
}
continue
}
wg.Add(1)
spawn("DNSServer.Start-DNSServer.handleDNSRequest",
func() { d.handleDNSRequest(addr, authority, udpListen, b) })
}
}
// NewDNSServer - create DNS server
func NewDNSServer(hostname, nameserver, listen string) *DNSServer {
if hostname[len(hostname)-1] != '.' {
hostname = hostname + "."
}
if nameserver[len(nameserver)-1] != '.' {
nameserver = nameserver + "."
}
return &DNSServer{
hostname: hostname,
listen: listen,
nameserver: nameserver,
}
}
func (d *DNSServer) extractSubnetworkID(addr *net.UDPAddr, domainName string) (*externalapi.DomainSubnetworkID, bool, error) {
// Domain name may be in following format:
// [n[subnetwork].]hostname
// where connmgr.SubnetworkIDPrefixChar is a prefix
var subnetworkID *externalapi.DomainSubnetworkID
includeAllSubnetworks := true
if d.hostname != domainName {
labels := dns.SplitDomainName(domainName)
if labels[0][0] == dnsseed.SubnetworkIDPrefixChar {
includeAllSubnetworks = false
if len(labels[0]) > 1 {
subnetworkID, err := subnetworks.FromString(labels[0][1:])
if err != nil {
log.Infof("%s: subnetworkid.NewFromStr: %v", addr, err)
return subnetworkID, includeAllSubnetworks, err
}
}
}
}
return subnetworkID, includeAllSubnetworks, nil
}
func (d *DNSServer) validateDNSRequest(addr *net.UDPAddr, b []byte) (dnsMsg *dns.Msg, domainName string, atype string, err error) {
dnsMsg = new(dns.Msg)
err = dnsMsg.Unpack(b[:])
if err != nil {
log.Infof("%s: invalid dns message: %v", addr, err)
return nil, "", "", err
}
if len(dnsMsg.Question) != 1 {
str := fmt.Sprintf("%s sent more than 1 question: %d", addr, len(dnsMsg.Question))
log.Infof("%s", str)
return nil, "", "", errors.Errorf("%s", str)
}
domainName = strings.ToLower(dnsMsg.Question[0].Name)
ff := strings.LastIndex(domainName, d.hostname)
if ff < 0 {
str := fmt.Sprintf("invalid name: %s", dnsMsg.Question[0].Name)
log.Infof("%s", str)
return nil, "", "", errors.Errorf("%s", str)
}
atype, err = translateDNSQuestion(addr, dnsMsg)
return dnsMsg, domainName, atype, err
}
func translateDNSQuestion(addr *net.UDPAddr, dnsMsg *dns.Msg) (string, error) {
var atype string
qtype := dnsMsg.Question[0].Qtype
switch qtype {
case dns.TypeA:
atype = "A"
case dns.TypeAAAA:
atype = "AAAA"
case dns.TypeNS:
atype = "NS"
default:
str := fmt.Sprintf("%s: invalid qtype: %d", addr, dnsMsg.Question[0].Qtype)
log.Infof("%s", str)
return "", errors.Errorf("%s", str)
}
return atype, nil
}
func (d *DNSServer) buildDNSResponse(addr *net.UDPAddr, authority dns.RR, dnsMsg *dns.Msg, includeAllSubnetworks bool,
subnetworkID *externalapi.DomainSubnetworkID, atype string) ([]byte, error) {
respMsg := dnsMsg.Copy()
respMsg.Authoritative = true
respMsg.Response = true
qtype := dnsMsg.Question[0].Qtype
if qtype != dns.TypeNS {
respMsg.Ns = append(respMsg.Ns, authority)
addrs := amgr.GoodAddresses(qtype, includeAllSubnetworks, subnetworkID)
log.Infof("%s: Sending %d addresses", addr, len(addrs))
if len(addrs) == 0 && qtype == dns.TypeAAAA {
// Musl (Alpine) requires non-empty result (work-around):
addrs = append(addrs, appmessage.NewNetAddressIPPort(net.ParseIP("100::"), uint16(0)))
}
for _, a := range addrs {
rr := fmt.Sprintf("%s 30 IN %s %s", dnsMsg.Question[0].Name, atype, a.IP.String())
newRR, err := dns.NewRR(rr)
if err != nil {
log.Infof("%s: NewRR: %v", addr, err)
return nil, err
}
respMsg.Answer = append(respMsg.Answer, newRR)
}
} else {
rr := fmt.Sprintf("%s 86400 IN NS %s", dnsMsg.Question[0].Name, d.nameserver)
newRR, err := dns.NewRR(rr)
if err != nil {
log.Infof("%s: NewRR: %v", addr, err)
return nil, err
}
respMsg.Answer = append(respMsg.Answer, newRR)
}
sendBytes, err := respMsg.Pack()
if err != nil {
log.Infof("%s: failed to pack response: %v", addr, err)
return nil, err
}
return sendBytes, nil
}
func (d *DNSServer) handleDNSRequest(addr *net.UDPAddr, authority dns.RR, udpListen *net.UDPConn, b []byte) {
defer wg.Done()
dnsMsg, domainName, atype, err := d.validateDNSRequest(addr, b)
if err != nil {
return
}
subnetworkID, includeAllSubnetworks, err := d.extractSubnetworkID(addr, domainName)
if err != nil {
return
}
log.Infof("%s: query %d for subnetwork ID %v",
addr, dnsMsg.Question[0].Qtype, subnetworkID)
sendBytes, err := d.buildDNSResponse(addr, authority, dnsMsg, includeAllSubnetworks, subnetworkID, atype)
if err != nil {
return
}
_, err = udpListen.WriteToUDP(sendBytes, addr)
if err != nil {
log.Infof("%s: failed to write response: %v", addr, err)
return
}
}