Debian Codegraph @

dnsutil/shared.go

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