-
Notifications
You must be signed in to change notification settings - Fork 193
/
types.go
81 lines (63 loc) · 1.5 KB
/
types.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
package main
import (
"github.com/miekg/dns"
)
type Options struct {
Serial int
Ttl int
}
type Record struct {
RR dns.RR
Weight int
}
type Records []Record
func (s Records) Len() int { return len(s) }
func (s Records) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type RecordsByWeight struct{ Records }
func (s RecordsByWeight) Less(i, j int) bool { return s.Records[i].Weight > s.Records[j].Weight }
type Label struct {
Label string
MaxHosts int
Ttl int
Records map[uint16]Records
Weight map[uint16]int
}
type labels map[string]*Label
type Zones map[string]*Zone
type Zone struct {
Origin string
Labels labels
LenLabels int
Options Options
}
func (l *Label) firstRR(dnsType uint16) dns.RR {
return l.Records[dnsType][0].RR
}
func (z *Zone) SoaRR() dns.RR {
return z.Labels[""].firstRR(dns.TypeSOA)
}
func (z *Zone) findLabels(s, cc string, qtype uint16) *Label {
if qtype == dns.TypeANY {
// short-circuit mostly to avoid subtle bugs later
return z.Labels[s]
}
selectors := []string{}
if len(cc) > 0 {
if len(s) > 0 {
cc = s + "." + cc
}
selectors = append(selectors, cc)
}
// TODO(ask) Add continent, see https://github.com/abh/geodns/issues/1
selectors = append(selectors, s)
for _, name := range selectors {
if label, ok := z.Labels[name]; ok {
// return the label if it has the right records
// TODO(ask) Should this also look for CNAME or aliases?
if label.Records[qtype] != nil {
return label
}
}
}
return z.Labels[s]
}