Debian Codegraph @

deleg/deleg.go

raw
1 // Package deleg deals with all the intricacies of the DELEG RR. All the sub-types ([Info]) used in the RR are defined here.
2 // As DELEG is derived from the SVCB RR so there are a lot of similarities. This implements draft version -07 and higher.
3 package deleg
4
5 import (
6 "net/netip"
7 "slices"
8 "strconv"
9 "strings"
10
11 "codeberg.org/miekg/dns/internal/reverse"
12 )
13
14 // Keys as defined in the DELEG draft.
15 const (
16 KeyServerIPv4 uint16 = iota + 1
17 KeyServerIPv6
18 KeyServerName
19 KeyIncludeDelegParam
20
21 KeyReserved uint16 = 65535
22 )
23
24 // Info defines a key=value pair for the DELEG/DELEGPARAM RR type. A DELEG RR can have multiple infos appended to it.
25 // The numerical key code is derived from the type, see [InfoToKey].
26 type Info interface {
27 String() string // String returns the string representation of the value.
28 Len() int // Len returns the length of value in the wire format.
29 Clone() Info // Clone returns a deep copy of the Info.
30 }
31
32 // KeyToString return the string representation for k. For KeyReserved the empty string is returned. For
33 // unknown keys "key"+value is returned, see section 2.1 of RFC 9460.
34 func KeyToString(k uint16) string {
35 if k == KeyReserved {
36 return ""
37 }
38 if s, ok := keyToString[k]; ok {
39 return s
40 }
41 return "key" + strconv.Itoa(int(k))
42 }
43
44 var keyToString = map[uint16]string{
45 KeyServerIPv4: "server-ipv4",
46 KeyServerIPv6: "server-ipv6",
47 KeyServerName: "server-name",
48 KeyIncludeDelegParam: "include-delegparam",
49 }
50
51 // StringtoKey is the reverse of KeyToString and takes keyXXXX into account.
52 func StringToKey(s string) uint16 {
53 if k, ok := stringToKey[s]; ok {
54 return k
55 }
56 if strings.HasPrefix(s, "key") {
57 k, _ := strconv.Atoi(s[3:])
58 return uint16(k)
59 }
60 return KeyReserved
61 }
62
63 var stringToKey = reverse.Map(keyToString)
64
65 // KeyToInfo convert the key value to a Info.
66 func KeyToInfo(k uint16) func() Info {
67 switch k {
68 case KeyServerIPv4:
69 return func() Info { return new(SERVERIPV4) }
70 case KeyServerIPv6:
71 return func() Info { return new(SERVERIPV6) }
72 case KeyServerName:
73 return func() Info { return new(SERVERNAME) }
74 case KeyIncludeDelegParam:
75 return func() Info { return new(INCLUDEDELEGPARAM) }
76 default:
77 return nil
78 }
79 }
80
81 // InfoToKey is the reverse of KeyToInfo.
82 func InfoToKey(i Info) uint16 {
83 switch i.(type) {
84 case *SERVERIPV4:
85 return KeyServerIPv4
86 case *SERVERIPV6:
87 return KeyServerIPv6
88 case *SERVERNAME:
89 return KeyServerName
90 case *INCLUDEDELEGPARAM:
91 return KeyIncludeDelegParam
92 }
93 return KeyReserved
94 }
95
96 // SERVERNAME info add nameserver hosts names to the DELEG RR.
97 type SERVERNAME struct {
98 Hostnames []string `dns:"name"`
99 }
100
101 func (s *SERVERNAME) String() string { return strings.Join(s.Hostnames, ",") }
102
103 func (s *SERVERNAME) Len() int {
104 l := tlv
105 for i := range s.Hostnames {
106 l += len(s.Hostnames[i]) + 1
107 }
108 return l
109 }
110
111 // INCLUDEDELEGPARAM info adds DELEGPARAM domains to the DELEG RR.
112 type INCLUDEDELEGPARAM struct {
113 Domains []string `dns:"name"`
114 }
115
116 func (s *INCLUDEDELEGPARAM) String() string { return strings.Join(s.Domains, ",") }
117
118 func (s *INCLUDEDELEGPARAM) Len() int {
119 l := tlv
120 for i := range s.Domains {
121 l += len(s.Domains[i]) + 1
122 }
123 return l
124 }
125
126 // SERVERIPV4 info adds IPv4 addresses to the DELEG RR.
127 type SERVERIPV4 struct {
128 IPs []netip.Addr
129 }
130
131 func (s *SERVERIPV4) Len() int { return tlv + 4*len(s.IPs) }
132
133 func (s *SERVERIPV4) String() string {
134 str := make([]string, len(s.IPs))
135 for i, e := range s.IPs {
136 str[i] = e.String()
137 }
138 return strings.Join(str, ",")
139 }
140
141 // SERVERIPV6 info adds IPv6 addresses to the DELEG RR.
142 type SERVERIPV6 struct {
143 IPs []netip.Addr
144 }
145
146 func (s *SERVERIPV6) Len() int { return tlv + 16*len(s.IPs) }
147
148 func (s *SERVERIPV6) String() string {
149 str := make([]string, len(s.IPs))
150 for i, e := range s.IPs {
151 str[i] = e.String()
152 }
153 return strings.Join(str, ",")
154 }
155
156 const tlv = 4
157
158 func (s *SERVERIPV4) Clone() Info { return &SERVERIPV4{slices.Clone(s.IPs)} }
159 func (s *SERVERIPV6) Clone() Info { return &SERVERIPV6{slices.Clone(s.IPs)} }
160 func (s *SERVERNAME) Clone() Info { return &SERVERNAME{slices.Clone(s.Hostnames)} }
161 func (s *INCLUDEDELEGPARAM) Clone() Info { return &INCLUDEDELEGPARAM{slices.Clone(s.Domains)} }