-
Notifications
You must be signed in to change notification settings - Fork 400
/
dnsrr.go
104 lines (94 loc) · 2.85 KB
/
dnsrr.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
package models
// methods that make RecordConfig meet the dns.RR interface.
import (
"fmt"
"strings"
"github.com/miekg/dns"
)
//// Header Header returns the header of an resource record.
//func (rc *RecordConfig) Header() *dns.RR_Header {
// log.Fatal("Header not implemented")
// return nil
//}
// String returns the text representation of the resource record.
func (rc *RecordConfig) String() string {
return rc.GetTargetCombined()
}
//// copy returns a copy of the RR
//func (rc *RecordConfig) copy() dns.RR {
// log.Fatal("Copy not implemented")
// return dns.TypeToRR[dns.TypeA]()
//}
//
//// len returns the length (in octets) of the uncompressed RR in wire format.
//func (rc *RecordConfig) len() int {
// log.Fatal("len not implemented")
// return 0
//}
//
//// pack packs an RR into wire format.
//func (rc *RecordConfig) pack([]byte, int, map[string]int, bool) (int, error) {
// log.Fatal("pack not implemented")
// return 0, nil
//}
// Conversions
// RRstoRCs converts []dns.RR to []RecordConfigs.
func RRstoRCs(rrs []dns.RR, origin string) (Records, error) {
rcs := make(Records, 0, len(rrs))
for _, r := range rrs {
rc, err := RRtoRC(r, origin)
if err != nil {
return nil, err
}
rcs = append(rcs, &rc)
}
return rcs, nil
}
// RRtoRC converts dns.RR to RecordConfig
func RRtoRC(rr dns.RR, origin string) (RecordConfig, error) {
// Convert's dns.RR into our native data type (RecordConfig).
// Records are translated directly with no changes.
header := rr.Header()
rc := new(RecordConfig)
rc.Type = dns.TypeToString[header.Rrtype]
rc.TTL = header.Ttl
rc.Original = rr
rc.SetLabelFromFQDN(strings.TrimSuffix(header.Name, "."), origin)
var err error
switch v := rr.(type) { // #rtype_variations
case *dns.A:
err = rc.SetTarget(v.A.String())
case *dns.AAAA:
err = rc.SetTarget(v.AAAA.String())
case *dns.CAA:
err = rc.SetTargetCAA(v.Flag, v.Tag, v.Value)
case *dns.CNAME:
err = rc.SetTarget(v.Target)
case *dns.DS:
err = rc.SetTargetDS(v.KeyTag, v.Algorithm, v.DigestType, v.Digest)
case *dns.MX:
err = rc.SetTargetMX(v.Preference, v.Mx)
case *dns.NS:
err = rc.SetTarget(v.Ns)
case *dns.PTR:
err = rc.SetTarget(v.Ptr)
case *dns.NAPTR:
err = rc.SetTargetNAPTR(v.Order, v.Preference, v.Flags, v.Service, v.Regexp, v.Replacement)
case *dns.SOA:
err = rc.SetTargetSOA(v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
case *dns.SRV:
err = rc.SetTargetSRV(v.Priority, v.Weight, v.Port, v.Target)
case *dns.SSHFP:
err = rc.SetTargetSSHFP(v.Algorithm, v.Type, v.FingerPrint)
case *dns.TLSA:
err = rc.SetTargetTLSA(v.Usage, v.Selector, v.MatchingType, v.Certificate)
case *dns.TXT:
err = rc.SetTargetTXTs(v.Txt)
default:
return *rc, fmt.Errorf("rrToRecord: Unimplemented zone record type=%s (%v)", rc.Type, rr)
}
if err != nil {
return *rc, fmt.Errorf("unparsable record received: %w", err)
}
return *rc, nil
}