Repository navigation
Expand file tree
/
Copy pathdomain.go
More file actions
272 lines (226 loc) · 6.17 KB
/
Copy pathdomain.go
File metadata and controls
272 lines (226 loc) · 6.17 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// CGo binding for Avahi
//
// Copyright (C) 2024 and up by Alexander Pevzner (pzz@apevzner.com)
// See LICENSE for license terms and conditions
//
// Domain name string
//
//go:build linux || freebsd
package avahi
// #include <stdlib.h>
// #include <avahi-common/domain.h>
import "C"
import (
"strings"
"unsafe"
)
// DomainFrom makes escaped domain name string from a sequence of unescaped
// labels:
//
// ["Ex.Ample", "com"] -> "Ex\.Ample.com"
//
// Note, this function is not guaranteed to escape labels exactly
// as Avahi does, but output is anyway correct.
func DomainFrom(labels []string) string {
buf := make([]byte, 0, 256)
for n, label := range labels {
if n != 0 {
buf = append(buf, '.')
}
for i := 0; i < len(label); i++ {
c := label[i]
switch c {
case '.', '\\':
buf = append(buf, '\\', c)
default:
buf = append(buf, c)
}
}
}
return string(buf)
}
// DomainSlice splits escaped domain name into a sequence of unescaped
// labels:
//
// "Ex\.Ample.com" -> ["Ex.Ample", "com"]
//
// In a case of error it returns nil.
func DomainSlice(d string) []string {
// Convert input from Go to C
in := C.CString(d)
defer C.free(unsafe.Pointer(in))
// Allocate decode buffer. len(d) must be enough.
buflen := C.size_t(len(d))
buf := C.malloc(buflen + 1)
defer C.free(unsafe.Pointer(buf))
// Decode label by label
labels := []string{}
next := in
for *next != 0 {
clabel := C.avahi_unescape_label(&next, (*C.char)(buf), buflen)
if clabel == nil {
return nil
}
labels = append(labels, C.GoString(clabel))
}
return labels
}
// DomainEqual reports if two domain names are equal.
//
// Note, invalid domain names are never equal to anything
// else, including itself.
func DomainEqual(d1, d2 string) bool {
labels1 := DomainSlice(d1)
labels2 := DomainSlice(d2)
if labels1 == nil || labels2 == nil {
return false
}
if len(labels1) != len(labels2) {
return false
}
for i := range labels1 {
if !strcaseequal(labels1[i], labels2[i]) {
return false
}
}
return true
}
// DomainNormalize normalizes the domain name by removing unneeded escaping.
//
// In a case of error it returns empty string.
func DomainNormalize(d string) string {
return DomainFrom(DomainSlice(d))
}
// DomainToLower converts domain name to upper case.
//
// It only touches ASCII uppercase letters "a" to "z",
// leaving all other bytes untouched.
//
// See [RFC6762, 16. Multicast DNS Character Set] for details.
//
// [RFC6762, 16. Multicast DNS Character Set]: https://datatracker.ietf.org/doc/html/rfc6762#section-16
func DomainToLower(d string) string {
buf := []byte(d)
for i := range buf {
if c := buf[i]; 'A' <= c && c <= 'Z' {
buf[i] = c - 'A' + 'a'
}
}
return string(buf)
}
// DomainToUpper converts domain name to upper case.
//
// It only touches ASCII lowercase letters "a" to "z",
// leaving all other bytes untouched.
//
// See [RFC6762, 16. Multicast DNS Character Set] for details.
//
// [RFC6762, 16. Multicast DNS Character Set]: https://datatracker.ietf.org/doc/html/rfc6762#section-16
func DomainToUpper(d string) string {
buf := []byte(d)
for i := range buf {
if c := buf[i]; 'a' <= c && c <= 'z' {
buf[i] = c - 'a' + 'A'
}
}
return string(buf)
}
// DomainServiceNameSplit splits a full service name into its instance, service
// type, and domain components.
//
// It accepts an escaped service name and returns unescaped components:
//
// Kyocera\032ECOSYS\032M2040dn._ipp._tcp.local
// --> "Kyocera ECOSYS M2040dn" + "_ipp._tcp" + "local"
//
// This function is the inverse of [DomainServiceNameJoin].
//
// When the service name includes a service subtype (e.g.
// _universal._sub._ipp._tcp), the subtype syntax is strictly checked.
// It must have the following form, as specified by RFC 6763:
//
// _<subtype>._sub._<service>._<transport>
func DomainServiceNameSplit(name string) (
instance, svctype, domain string, err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var cinstance [C.AVAHI_LABEL_MAX]C.char
var csvctype [C.AVAHI_LABEL_MAX]C.char
var cdomain [C.AVAHI_LABEL_MAX]C.char
rc := C.avahi_service_name_split(cname,
&cinstance[0], C.AVAHI_LABEL_MAX,
&csvctype[0], C.AVAHI_LABEL_MAX,
&cdomain[0], C.AVAHI_LABEL_MAX)
if rc < 0 {
err = ErrCode(rc)
return
}
instance = C.GoString(&cinstance[0])
svctype = C.GoString(&csvctype[0])
domain = C.GoString(&cdomain[0])
return
}
// DomainServiceNameJoin joins the instance name, service type, and domain
// name into a full service name.
//
// It accepts unescaped input and returns an escaped service name:
//
// "Kyocera ECOSYS M2040dn" + "_ipp._tcp" + "local"
// --> Kyocera\032ECOSYS\032M2040dn._ipp._tcp.local
//
// This function is the inverse of [DomainServiceNameSplit].
func DomainServiceNameJoin(instance, svctype, domain string) (string, error) {
cinstance := C.CString(instance)
defer C.free(unsafe.Pointer(cinstance))
csvctype := C.CString(svctype)
defer C.free(unsafe.Pointer(csvctype))
cdomain := C.CString(domain)
defer C.free(unsafe.Pointer(cdomain))
var buf [C.AVAHI_DOMAIN_NAME_MAX]C.char
rc := C.avahi_service_name_join(&buf[0], C.AVAHI_DOMAIN_NAME_MAX,
cinstance, csvctype, cdomain)
if rc < 0 {
return "", ErrCode(rc)
}
return C.GoString(&buf[0]), nil
}
// DomainServiceNameUnescape unescapes Domain service name:
//
// Kyocera\032ECOSYS\032M2040dn._ipp._tcp.local
// --> "Kyocera ECOSYS M2040dn._ipp._tcp.local"
//
// This function is not as strict as Avahi's avahi_unescape_label.
// It interprets input as follows:
//
// \NNN - replaced with byte with decimal value NNN
// \X - replaced with X, if X is not digit
func DomainServiceNameUnescape(name string) string {
b := strings.Builder{}
bytes := []byte(name)
for len(bytes) > 0 {
if bytes[0] == '\\' {
switch {
case len(bytes) >= 4 &&
isdigit(bytes[1]) &&
isdigit(bytes[2]) &&
isdigit(bytes[3]):
c := 100 * int(bytes[1]-'0')
c += 10 * int(bytes[2]-'0')
c += int(bytes[3] - '0')
if c <= 255 {
b.WriteByte(byte(c))
}
bytes = bytes[4:]
case len(bytes) >= 2:
b.WriteByte(bytes[1])
bytes = bytes[2:]
default:
bytes = bytes[1:]
}
} else {
b.WriteByte(bytes[0])
bytes = bytes[1:]
}
}
return b.String()
}