Debian Codegraph @

zdnsutil.go

raw
1 // Code generated by "go run dnsutil_generate.go"; DO NOT EDIT.
2
3 package dns
4
5 import (
6 "strings"
7 "time"
8
9 "codeberg.org/miekg/dns/internal/dnsstring"
10 )
11
12 // This is copied to zdnsutil.go in the main package to also have access to these functions and not have an
13 // import cycle. See dnsutil_generate.go.
14 //
15 // This file SHOULD NOT import dns things, as that leads to the impossibility to use it from svcb/ and/or
16 // deleg/.
17
18 // Labels returns the number of labels in the name s.
19 func dnsutilLabels(s string) (labels int) {
20 if s == "." {
21 return
22 }
23 off := 0
24 end := false
25 for {
26 off, end = dnsutilNext(s, off)
27 labels++
28 if end {
29 return
30 }
31 }
32 }
33
34 // Next returns the index of the start of the next label in the string s starting at offset. A negative offset
35 // will cause a panic. The bool end is true when the end of the string has been reached. For `www.miek.nl.`
36 // this returns:
37 //
38 // Next("www.miek.nl.", 0) -> 4, false
39 // Next("www.miek.nl.", 4) -> 9, false
40 // Next("www.miek.nl.", 9) -> 12, true
41 //
42 // s must be a fully qualified domain name. Also see [Prev].
43 func dnsutilNext(s string, offset int) (i int, end bool) {
44 switch len(s) {
45 case 0, 1:
46 return 0, true
47 }
48 for i = offset; i < len(s)-1; i++ {
49 if s[i] != '.' {
50 continue
51 }
52 return i + 1, false
53 }
54 return len(s), true
55 }
56
57 // Prev returns the index of the start of the previous label in the string s start at offset. An offset longer
58 // then the string will cause a panic. The bool start is true when the beginning of the string has been
59 // reached. For `www.miek.nl.` this returns:
60 //
61 // Prev("www.miek.nl.", 12) -> 9, false
62 // Prev("www.miek.nl.", 9) -> 4, false
63 // Prev("www.miek.nl.", 4) -> 0, true
64 //
65 // s must be a fully qualified domain name. Also see [Next].
66 func dnsutilPrev(s string, offset int) (i int, start bool) {
67 switch len(s) {
68 case 0, 1:
69 return 0, true
70 }
71 // if the s[offset] is a dot, we still need to go backwards more
72 if s[offset-1] == '.' {
73 offset -= 2
74 }
75
76 for i := offset; i >= 0; i-- {
77 if s[i] != '.' {
78 continue
79 }
80 return i + 1, false
81 }
82 return 0, true
83
84 }
85
86 // Fqdn return the fully qualified domain name from s. If s is already fully qualified, it behaves as the
87 // identity function.
88 func dnsutilFqdn(s string) string {
89 if dnsutilIsFqdn(s) {
90 return s
91 }
92 return s + "."
93 }
94
95 // IsFqdn checks if a domain name is fully qualified. As this library doesn't support escapes in names, this
96 // simply calls [strings.HasSuffix].
97 func dnsutilIsFqdn(s string) bool { return strings.HasSuffix(s, ".") }
98
99 // Canonical returns the domain name in canonical form. A name in canonical form is lowercase and fully qualified.
100 // Only US-ASCII letters are affected. See Section 6.2 in RFC 4034.
101 func dnsutilCanonical(s string) string {
102 return strings.Map(func(r rune) rune {
103 if r >= 'A' && r <= 'Z' {
104 r += 'a' - 'A'
105 }
106 return r
107 }, dnsutilFqdn(s))
108 }
109
110 // IsName checks if s is a valid domain name. A non fully qualified domain name is considered valid.
111 // Note that this function is extremely liberal; almost any string is a valid domain name as the DNS is 8 bit
112 // protocol. It checks if each label fits in 63 characters and that the entire name will fit into the 255
113 // octet wire-format limit.
114 func dnsutilIsName(s string) bool {
115 // XXX: The logic in this function was copied from pack.Name and should be kept in sync with that function.
116 const lenmsg = 256
117 ls := len(s)
118
119 if ls == 1 && s[0] == '.' {
120 return true
121 }
122
123 var (
124 off int
125 begin int
126 )
127 for begin < ls {
128 i := strings.IndexByte(s[begin:], '.')
129 if i == -1 {
130 break
131 }
132 i += begin
133
134 labelLen := i - begin
135 // top two bits of length must be clear and two dots back to back is not legal
136 if labelLen == 0 || labelLen >= 1<<6 {
137 return false
138 }
139 // off can already (we're in a loop) be bigger than lenmsg this happens when a name isn't fully qualified
140 off += 1 + labelLen
141 if off > lenmsg {
142 return false
143 }
144 begin = i + 1
145 }
146 return true
147 }
148
149 // compareLabel compares a and b while ignoring case. It returns 0 when equal, -1 when a is smaller than b,
150 // and +1 when a is greater then b. This ends up a compareLabel in the dns package too as generated by
151 // dnsutil_generate.go.
152 func compareLabel(a, b string) int {
153 la, lb := len(a), len(b)
154 for i := range min(la, lb) {
155 ai := a[i]
156 bi := b[i]
157 if ai >= 'A' && ai <= 'Z' {
158 ai |= 'a' - 'A'
159 }
160 if bi >= 'A' && bi <= 'Z' {
161 bi |= 'a' - 'A'
162 }
163 if ai < bi {
164 return -1
165 }
166 if ai > bi {
167 return +1
168 }
169 }
170 if la < lb {
171 return -1
172 }
173 if la > lb {
174 return +1
175 }
176 return 0
177 }
178
179 // TimeToString translates the RRSIG's incep. and expir. times to the
180 // string representation used when printing the record. It takes serial arithmetic (RFC 1982) into account.
181 // Inverse of [StringToTime].
182 func dnsutilTimeToString(t uint32) string {
183 mod := max((int64(t)-time.Now().Unix())/maxSerialIncrement-1, 0)
184 ti := time.Unix(int64(t)-mod*maxSerialIncrement, 0).UTC()
185 return ti.Format("20060102150405")
186 }
187
188 // StringToTime translates the RRSIG's incep. and expir. times from string values like "20110403154150" to an 32 bit integer.
189 // It takes serial arithmetic (RFC 1982) into account.
190 // Inverse of [TimeToString].
191 func dnsutilStringToTime(s string) (uint32, error) {
192 t, err := dnsstring.ToTime(s)
193 if err != nil {
194 return 0, err
195 }
196 mod := max(t/maxSerialIncrement-1, 0)
197 return uint32(t - mod*maxSerialIncrement), nil
198 }
199
200 // Absolute takes the name and origin and appends the origin to the name. This takes the RFC 1035 presentation
201 // format into account, i.e. "@" means the origin in a name.
202 // If s is not a valid domain name, the empty string is returned. If the origin is needed to be appended,
203 // but is empty the empty string is also returned.
204 func dnsutilAbsolute(s, origin string) string {
205 if s == "@" {
206 return origin
207 }
208 if dnsutilIsName(s) == false { // done to make the conversion via dnsutil_generate.go work, instead of !IsName(s)
209 return ""
210 }
211 if dnsutilIsFqdn(s) {
212 return s
213 }
214 if origin == "" {
215 return ""
216 }
217 if origin == "." {
218 return s + origin
219 }
220 return s + "." + origin
221 }
222
223 // maxSerialIncrement is the maximum difference between two serial numbers. See RFC 1982.
224 const maxSerialIncrement = 2147483647