This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
forked from skynetservices/skydns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
829 lines (788 loc) · 23.1 KB
/
server.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// Copyright (c) 2014 The SkyDNS Authors. All rights reserved.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"net"
"strings"
"sync"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/miekg/dns"
)
type server struct {
client *etcd.Client
config *Config
group *sync.WaitGroup
scache *cache
rcache *cache
}
// NewServer returns a new SkyDNS server.
func NewServer(config *Config, client *etcd.Client) *server {
return &server{client: client, config: config, group: new(sync.WaitGroup),
scache: NewCache(config.SCache, 0),
rcache: NewCache(config.RCache, config.RCacheTtl),
}
}
// Run is a blocking operation that starts the server listening on the DNS ports.
func (s *server) Run() error {
mux := dns.NewServeMux()
mux.Handle(".", s)
s.group.Add(2)
go runDNSServer(s.group, mux, "tcp", s.config.DnsAddr, s.config.ReadTimeout)
go runDNSServer(s.group, mux, "udp", s.config.DnsAddr, s.config.ReadTimeout)
if s.config.DNSSEC == "" {
s.config.log.Printf("ready for queries on %s for %s [rcache %d]", s.config.Domain, s.config.DnsAddr, s.config.RCache)
} else {
s.config.log.Printf("ready for queries on %s for %s [rcache %d], signing with %s [scache %d]", s.config.Domain, s.config.DnsAddr, s.config.RCache, s.config.DNSSEC, s.config.SCache)
}
s.group.Wait()
return nil
}
// Stop stops a server.
func (s *server) Stop() {
// TODO(miek)
//s.group.Add(-2)
}
func runDNSServer(group *sync.WaitGroup, mux *dns.ServeMux, net, addr string, readTimeout time.Duration) {
defer group.Done()
server := &dns.Server{
Addr: addr,
Net: net,
Handler: mux,
ReadTimeout: readTimeout,
}
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
// ServeDNS is the handler for DNS requests, responsible for parsing DNS request, possibly forwarding
// it to a real dns server and returning a response.
func (s *server) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
name := strings.ToLower(q.Name)
StatsRequestCount.Inc(1)
if verbose {
s.config.log.Infof("received DNS Request for %q from %q with type %d", q.Name, w.RemoteAddr(), q.Qtype)
}
// If the qname is local.dns.skydns.local. and s.config.Local != "", substitute that name.
if s.config.Local != "" && name == s.config.localDomain {
name = s.config.Local
}
cached := false
dnssec := uint16(0)
if o := req.IsEdns0(); o != nil && o.Do() {
dnssec = o.UDPSize()
}
if q.Qtype == dns.TypePTR && strings.HasSuffix(name, ".in-addr.arpa.") || strings.HasSuffix(name, ".ip6.arpa.") {
s.ServeDNSReverse(w, req)
return
}
if q.Qclass != dns.ClassCHAOS && !strings.HasSuffix(name, s.config.Domain) {
s.ServeDNSForward(w, req)
return
}
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = true
m.Compress = true
defer func() {
if m.Rcode == dns.RcodeServerFailure {
if err := w.WriteMsg(m); err != nil {
s.config.log.Errorf("failure to return reply %q", err)
}
return
}
// Set TTL to the minimum of the RRset.
minttl := s.config.Ttl
if len(m.Answer) > 1 {
for _, r := range m.Answer {
if r.Header().Ttl < minttl {
minttl = r.Header().Ttl
}
}
for _, r := range m.Answer {
r.Header().Ttl = minttl
}
}
if !cached {
s.rcache.InsertMsg(QuestionKey(req.Question[0]), m.Answer, m.Extra)
}
if dnssec > 0 {
StatsDnssecOkCount.Inc(1)
if s.config.PubKey != nil {
s.Denial(m)
s.sign(m, dnssec)
}
}
if err := w.WriteMsg(m); err != nil {
s.config.log.Errorf("failure to return reply %q", err)
}
}()
if name == s.config.Domain {
if q.Qtype == dns.TypeSOA {
m.Answer = []dns.RR{s.NewSOA()}
return
}
if q.Qtype == dns.TypeDNSKEY {
if s.config.PubKey != nil {
m.Answer = []dns.RR{s.config.PubKey}
return
}
}
}
if q.Qclass == dns.ClassCHAOS {
if q.Qtype == dns.TypeTXT {
switch name {
case "authors.bind.":
fallthrough
case s.config.Domain:
hdr := dns.RR_Header{Name: name, Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
authors := []string{"Erik St. Martin", "Brian Ketelsen", "Miek Gieben", "Michael Crosby"}
for _, a := range authors {
m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{a}})
}
for j := 0; j < len(authors)*(int(dns.Id())%4+1); j++ {
q := int(dns.Id()) % len(authors)
p := int(dns.Id()) % len(authors)
if q == p {
p = (p + 1) % len(authors)
}
m.Answer[q], m.Answer[p] = m.Answer[p], m.Answer[q]
}
return
case "version.bind.":
fallthrough
case "version.server.":
hdr := dns.RR_Header{Name: name, Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{"SkyDNS 2.0.0"}}}
return
case "hostname.bind.":
fallthrough
case "id.server.":
// TODO(miek): machine name to return
hdr := dns.RR_Header{Name: name, Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{"localhost"}}}
return
}
}
// still here, fail
m.SetReply(req)
m.SetRcode(req, dns.RcodeServerFailure)
return
}
key := QuestionKey(req.Question[0])
a1, e1, exp := s.rcache.Search(key)
if len(a1) > 0 {
// Cache hit! \o/
if time.Since(exp) < 0 {
m.Answer = a1
m.Extra = e1
cached = true
return
}
// Expired! /o\
s.rcache.Remove(key)
}
switch q.Qtype {
case dns.TypeNS:
if name != s.config.Domain {
break
}
// Lookup s.config.DnsDomain
records, extra, err := s.NSRecords(q, s.config.dnsDomain)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok {
if e.ErrorCode == 100 {
s.NameError(m, req)
return
}
}
}
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
case dns.TypeA, dns.TypeAAAA:
records, err := s.AddressRecords(q, name, nil)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok {
if e.ErrorCode == 100 {
s.NameError(m, req)
return
}
}
if err.Error() == "incomplete CNAME chain" {
// We can not complete the CNAME internally, *iff* there is a
// external name in the set, take it, and try to resolve it externally.
if len(records) == 0 {
s.NameError(m, req)
return
}
target := ""
for _, r := range records {
if v, ok := r.(*dns.CNAME); ok {
if !dns.IsSubDomain(s.config.Domain, v.Target) {
target = v.Target
break
}
}
}
if target == "" {
s.NameError(m, req)
return
}
m1, e1 := s.Lookup(target, req.Question[0].Qtype, dnssec)
if e1 != nil {
s.config.log.Errorf("%q", err)
s.NameError(m, req)
return
}
records = append(records, m1.Answer...)
}
}
m.Answer = append(m.Answer, records...)
case dns.TypeCNAME:
records, err := s.CNAMERecords(q, name)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok {
if e.ErrorCode == 100 {
s.NameError(m, req)
return
}
}
}
m.Answer = append(m.Answer, records...)
default:
fallthrough // also catch other types, so that they return NODATA
case dns.TypeSRV, dns.TypeANY:
records, extra, err := s.SRVRecords(q, name, dnssec)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok {
if e.ErrorCode == 100 {
s.NameError(m, req)
return
}
}
}
// if we are here again, check the types, because an answer may only
// be given for SRV or ANY. All other types should return NODATA, the
// NXDOMAIN part is handled in the above code. TODO(miek): yes this
// can be done in a more elegant manor.
if q.Qtype == dns.TypeSRV || q.Qtype == dns.TypeANY {
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
}
}
if len(m.Answer) == 0 { // NODATA response
StatsNoDataCount.Inc(1)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
}
}
// ServeDNSForward forwards a request to a nameservers and returns the response.
func (s *server) ServeDNSForward(w dns.ResponseWriter, req *dns.Msg) {
StatsForwardCount.Inc(1)
if len(s.config.Nameservers) == 0 {
s.config.log.Infof("no nameservers defined, can not forward")
m := new(dns.Msg)
m.SetReply(req)
m.SetRcode(req, dns.RcodeServerFailure)
m.Authoritative = false // no matter what set to false
m.RecursionAvailable = true // and this is still true
w.WriteMsg(m)
return
}
network := "udp"
if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
c := &dns.Client{Net: network, ReadTimeout: s.config.ReadTimeout}
// Use request Id for "random" nameserver selection.
nsid := int(req.Id) % len(s.config.Nameservers)
try := 0
Redo:
r, _, err := c.Exchange(req, s.config.Nameservers[nsid])
if err == nil {
r.Compress = true
w.WriteMsg(r)
return
}
// Seen an error, this can only mean, "server not reached", try again
// but only if we have not exausted our nameservers.
if try < len(s.config.Nameservers) {
try++
nsid = (nsid + 1) % len(s.config.Nameservers)
goto Redo
}
s.config.log.Errorf("failure to forward request %q", err)
m := new(dns.Msg)
m.SetReply(req)
m.SetRcode(req, dns.RcodeServerFailure)
w.WriteMsg(m)
}
// ServeDNSReverse is the handler for DNS requests for the reverse zone. If nothing is found
// locally the request is forwarded to the forwarder for resolution.
func (s *server) ServeDNSReverse(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Compress = true
m.Authoritative = false // Set to false, because I don't know what to do wrt DNSSEC.
m.RecursionAvailable = true
var err error
if m.Answer, err = s.PTRRecords(req.Question[0]); err == nil {
// TODO(miek): Reverse DNSSEC. We should sign this, but requires a key....and more
// Probably not worth the hassle?
if err := w.WriteMsg(m); err != nil {
s.config.log.Errorf("failure to return reply %q", err)
}
}
// Always forward if not found locally.
s.ServeDNSForward(w, req)
}
func (s *server) AddressRecords(q dns.Question, name string, previousRecords []dns.RR) (records []dns.RR, err error) {
path, star := Path(name)
r, err := s.client.Get(path, false, true)
if err != nil {
return nil, err
}
if !r.Node.Dir { // single element
serv := new(Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
serv.Ttl = ttl
serv.key = r.Node.Key
switch {
case ip == nil:
// Try to resolve as CNAME if it's not an IP.
newRecord := serv.NewCNAME(q.Name, dns.Fqdn(serv.Host))
if len(previousRecords) > 7 {
s.config.log.Errorf("CNAME lookup limit of 8 exceeded for %s", newRecord)
return nil, fmt.Errorf("exceeded CNAME lookup limit")
}
if s.isDuplicateCNAME(newRecord, previousRecords) {
s.config.log.Errorf("CNAME loop detected for record %s", newRecord)
return nil, fmt.Errorf("detected CNAME loop")
}
records = append(records, newRecord)
nextRecords, err := s.AddressRecords(dns.Question{Name: dns.Fqdn(serv.Host), Qtype: q.Qtype, Qclass: q.Qclass}, strings.ToLower(dns.Fqdn(serv.Host)), append(previousRecords, newRecord))
if err != nil {
// This means we can not complete the CNAME, this is OK, but
// if we return an error this will trigger an NXDOMAIN.
// We also don't want to return the CNAME, because of the
// no other data rule. So return nothing and let NODATA
// kick in (via a hack).
return records, fmt.Errorf("incomplete CNAME chain")
}
records = append(records, nextRecords...)
case ip.To4() != nil && q.Qtype == dns.TypeA:
records = append(records, serv.NewA(q.Name, ip.To4()))
case ip.To4() == nil && q.Qtype == dns.TypeAAAA:
records = append(records, serv.NewAAAA(q.Name, ip.To16()))
}
return records, nil
}
nodes, err := s.loopNodes(&r.Node.Nodes, strings.Split(PathNoWildcard(name), "/"), star, nil)
if err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
for _, serv := range nodes {
ip := net.ParseIP(serv.Host)
switch {
case ip == nil:
case ip.To4() != nil && q.Qtype == dns.TypeA:
records = append(records, serv.NewA(q.Name, ip.To4()))
case ip.To4() == nil && q.Qtype == dns.TypeAAAA:
records = append(records, serv.NewAAAA(q.Name, ip.To16()))
}
}
if s.config.RoundRobin {
switch l := len(records); l {
case 2:
if dns.Id()%2 == 0 {
records[0], records[1] = records[1], records[0]
}
default:
// Do a minimum of l swap, maximum of 4l swaps
for j := 0; j < l*(int(dns.Id())%4+1); j++ {
q := int(dns.Id()) % l
p := int(dns.Id()) % l
if q == p {
p = (p + 1) % l
}
records[q], records[p] = records[p], records[q]
}
}
}
return records, nil
}
// NSRecords returns NS records from etcd.
func (s *server) NSRecords(q dns.Question, name string) (records []dns.RR, extra []dns.RR, err error) {
path, star := Path(name)
r, err := s.client.Get(path, false, true)
if err != nil {
return nil, nil, err
}
if !r.Node.Dir { // single element
serv := new(Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
serv.key = r.Node.Key
serv.Ttl = ttl
switch {
case ip == nil:
return nil, nil, fmt.Errorf("NS record must be an IP address")
case ip.To4() != nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewNS(q.Name, serv.Host))
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
case ip.To4() == nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewNS(q.Name, serv.Host))
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
}
return records, extra, nil
}
sx, err := s.loopNodes(&r.Node.Nodes, strings.Split(PathNoWildcard(name), "/"), star, nil)
if err != nil || len(sx) == 0 {
return nil, nil, err
}
for _, serv := range sx {
ip := net.ParseIP(serv.Host)
switch {
case ip == nil:
return nil, nil, fmt.Errorf("NS record must be an IP address")
case ip.To4() != nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewNS(q.Name, serv.Host))
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
case ip.To4() == nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewNS(q.Name, serv.Host))
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
}
}
return records, extra, nil
}
// SRVRecords returns SRV records from etcd.
// If the Target is not an name but an IP address, an name is created .
func (s *server) SRVRecords(q dns.Question, name string, dnssec uint16) (records []dns.RR, extra []dns.RR, err error) {
path, star := Path(name)
r, err := s.client.Get(path, false, true)
if err != nil {
return nil, nil, err
}
if !r.Node.Dir { // single element
serv := new(Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
if serv.Priority == 0 {
serv.Priority = int(s.config.Priority)
}
serv.key = r.Node.Key
serv.Ttl = ttl
switch {
case ip == nil:
srv := serv.NewSRV(q.Name, uint16(100))
records = append(records, srv)
if !dns.IsSubDomain(s.config.Domain, srv.Target) {
m1, e1 := s.Lookup(srv.Target, dns.TypeA, dnssec)
if e1 == nil {
extra = append(extra, m1.Answer...)
}
m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, dnssec)
if e1 == nil {
// If we have seen CNAME's we *assume* that they already added.
for _, a := range m1.Answer {
if _, ok := a.(*dns.CNAME); !ok {
extra = append(extra, a)
}
}
}
}
case ip.To4() != nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewSRV(q.Name, uint16(100)))
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
case ip.To4() == nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewSRV(q.Name, uint16(100)))
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
}
return records, extra, nil
}
sx, err := s.loopNodes(&r.Node.Nodes, strings.Split(PathNoWildcard(name), "/"), star, nil)
if err != nil || len(sx) == 0 {
return nil, nil, err
}
// Looping twice to get the right weight vs priority
w := make(map[int]int)
for _, serv := range sx {
weight := 100
if serv.Weight != 0 {
weight = serv.Weight
}
if _, ok := w[serv.Priority]; !ok {
w[serv.Priority] = weight
continue
}
w[serv.Priority] += weight
}
lookup := make(map[string]bool)
for _, serv := range sx {
w1 := 100.0 / float64(w[serv.Priority])
if serv.Weight == 0 {
w1 *= 100
} else {
w1 *= float64(serv.Weight)
}
weight := uint16(math.Floor(w1))
ip := net.ParseIP(serv.Host)
switch {
case ip == nil:
srv := serv.NewSRV(q.Name, weight)
records = append(records, srv)
if _, ok := lookup[srv.Target]; !ok {
if !dns.IsSubDomain(s.config.Domain, srv.Target) {
m1, e1 := s.Lookup(srv.Target, dns.TypeA, dnssec)
if e1 == nil {
extra = append(extra, m1.Answer...)
}
m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, dnssec)
if e1 == nil {
// If we have seen CNAME's we *assume* that they are already added.
for _, a := range m1.Answer {
if _, ok := a.(*dns.CNAME); !ok {
extra = append(extra, a)
}
}
}
}
}
lookup[srv.Target] = true
case ip.To4() != nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewSRV(q.Name, weight))
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
case ip.To4() == nil:
serv.Host = Domain(serv.key)
records = append(records, serv.NewSRV(q.Name, weight))
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
}
}
return records, extra, nil
}
func (s *server) CNAMERecords(q dns.Question, name string) (records []dns.RR, err error) {
path, _ := Path(name) // no wildcards here
r, err := s.client.Get(path, false, true)
if err != nil {
return nil, err
}
if !r.Node.Dir {
serv := new(Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
serv.key = r.Node.Key
serv.Ttl = ttl
if ip == nil {
records = append(records, serv.NewCNAME(q.Name, dns.Fqdn(serv.Host)))
}
}
return records, nil
}
func (s *server) PTRRecords(q dns.Question) (records []dns.RR, err error) {
name := strings.ToLower(q.Name)
path, star := Path(name)
if star {
return nil, fmt.Errorf("reverse can not contain wildcards")
}
r, err := s.client.Get(path, false, false)
if err != nil {
// if server has a forward, forward the query
return nil, err
}
if r.Node.Dir {
return nil, fmt.Errorf("reverse should not be a directory")
}
serv := new(Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
ttl := uint32(r.Node.TTL)
if ttl == 0 {
ttl = s.config.Ttl
}
serv.key = r.Node.Key
// If serv.Host is parseble as a IP address we should not return anything.
// TODO(miek).
records = append(records, serv.NewPTR(q.Name, ttl))
return records, nil
}
// SOA returns a SOA record for this SkyDNS instance.
func (s *server) NewSOA() dns.RR {
return &dns.SOA{Hdr: dns.RR_Header{Name: s.config.Domain, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: s.config.Ttl},
Ns: "ns.dns." + s.config.Domain,
Mbox: s.config.Hostmaster,
Serial: uint32(time.Now().Truncate(time.Hour).Unix()),
Refresh: 28800,
Retry: 7200,
Expire: 604800,
Minttl: s.config.MinTtl,
}
}
type bareService struct {
Host string
Port int
Priority int
Weight int
}
// skydns/local/skydns/east/staging/web
// skydns/local/skydns/west/production/web
//
// skydns/local/skydns/*/*/web
// skydns/local/skydns/*/web
// loopNodes recursively loops through the nodes and returns all the values. The nodes' keyname
// will be match against any wildcards when star is true.
func (s *server) loopNodes(n *etcd.Nodes, nameParts []string, star bool, bx map[bareService]bool) (sx []*Service, err error) {
if bx == nil {
bx = make(map[bareService]bool)
}
Nodes:
for _, n := range *n {
if n.Dir {
nodes, err := s.loopNodes(&n.Nodes, nameParts, star, bx)
if err != nil {
return nil, err
}
sx = append(sx, nodes...)
continue
}
if star {
keyParts := strings.Split(n.Key, "/")
for i, n := range nameParts {
if i > len(keyParts)-1 {
// name is longer than key
continue Nodes
}
if n == "*" {
continue
}
if keyParts[i] != n {
continue Nodes
}
}
}
serv := new(Service)
if err := json.Unmarshal([]byte(n.Value), serv); err != nil {
return nil, err
}
b := bareService{serv.Host, serv.Port, serv.Priority, serv.Weight}
if _, ok := bx[b]; ok {
continue
}
bx[b] = true
serv.Ttl = s.calculateTtl(n, serv)
if serv.Priority == 0 {
serv.Priority = int(s.config.Priority)
}
serv.key = n.Key
sx = append(sx, serv)
}
return sx, nil
}
func (s *server) isDuplicateCNAME(r *dns.CNAME, records []dns.RR) bool {
for _, rec := range records {
if v, ok := rec.(*dns.CNAME); ok {
if v.Target == r.Target {
return true
}
}
}
return false
}
// calculateTtl returns the smaller of the etcd TTL and the service's
// TTL. If neither of these are set (have a zero value), the server
// default is used.
func (s *server) calculateTtl(node *etcd.Node, serv *Service) uint32 {
etcdTtl := uint32(node.TTL)
if etcdTtl == 0 && serv.Ttl == 0 {
return s.config.Ttl
}
if etcdTtl == 0 {
return serv.Ttl
}
if serv.Ttl == 0 {
return etcdTtl
}
if etcdTtl < serv.Ttl {
return etcdTtl
}
return serv.Ttl
}
// Lookup looks up name,type using the recursive nameserver defines
// in the server's config. If none defined it returns an error
func (s *server) Lookup(n string, t, dnssec uint16) (*dns.Msg, error) {
StatsLookupCount.Inc(1)
if len(s.config.Nameservers) == 0 {
return nil, fmt.Errorf("no nameservers configured can not lookup name")
}
m := new(dns.Msg)
m.SetQuestion(n, t)
if dnssec > 0 {
m.SetEdns0(dnssec, true)
}
c := &dns.Client{Net: "udp", ReadTimeout: 2 * s.config.ReadTimeout}
nsid := int(m.Id) % len(s.config.Nameservers)
try := 0
Redo:
r, _, err := c.Exchange(m, s.config.Nameservers[nsid])
if err == nil {
if r.Rcode != dns.RcodeSuccess {
return nil, fmt.Errorf("rcode is not equal to success")
}
// Reset TTLs to rcache TTL to make some of the other code
// and the tests not care about TTLs
for _, rr := range r.Answer {
rr.Header().Ttl = uint32(s.config.RCacheTtl)
}
for _, rr := range r.Extra {
rr.Header().Ttl = uint32(s.config.RCacheTtl)
}
return r, nil
}
// Seen an error, this can only mean, "server not reached", try again
// but only if we have not exausted our nameservers.
if try < len(s.config.Nameservers) {
try++
nsid = (nsid + 1) % len(s.config.Nameservers)
goto Redo
}
return nil, fmt.Errorf("failure to lookup name")
}
func (s *server) NameError(m, req *dns.Msg) {
m.SetRcode(req, dns.RcodeNameError)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
StatsNameErrorCount.Inc(1)
}