-
Notifications
You must be signed in to change notification settings - Fork 12
/
record_le.go
170 lines (162 loc) · 4.04 KB
/
record_le.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
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
//go:build 386 || amd64 || arm || amd64p32 || arm64 || ppc64le || mipsle || mips64le || mips64p32le
// +build 386 amd64 arm amd64p32 arm64 ppc64le mipsle mips64le mips64p32le
package fastdns
import (
"net/netip"
"unsafe"
)
// AppendHOST1Record appends a Host records to dst and returns the resulting dst.
func AppendHOST1Record(dst []byte, req *Message, ttl uint32, ip netip.Addr) []byte {
b := (*[16]byte)(unsafe.Pointer(&ip))
if ip.Is4() {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x04,
// RDATA
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
} else {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeAAAA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x10,
// RDATA
b[7], b[6], b[5], b[4],
b[3], b[2], b[1], b[0],
b[15], b[14], b[13], b[12],
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
}
return dst
}
// AppendHOSTRecord appends the Host records to dst and returns the resulting dst.
func AppendHOSTRecord(dst []byte, req *Message, ttl uint32, ips []netip.Addr) []byte {
for _, ip := range ips {
b := (*[16]byte)(unsafe.Pointer(&ip))
if ip.Is4() {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x04,
// RDATA
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
} else {
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeAAAA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x10,
// RDATA
b[7], b[6], b[5], b[4],
b[3], b[2], b[1], b[0],
b[15], b[14], b[13], b[12],
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
}
}
return dst
}
// AppendCNAMERecord appends the CNAME and Host records to dst and returns the resulting dst.
func AppendCNAMERecord(dst []byte, req *Message, ttl uint32, cnames []string, ips []netip.Addr) []byte {
offset := 0x0c
// CName Records
for i, cname := range cnames {
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0 | byte(offset>>8), byte(offset),
// TYPE
0x00, byte(TypeCNAME),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, byte(len(cname) + 2),
}
dst = append(dst, answer[:]...)
// set offset
if i == 0 {
offset += len(req.Question.Name) + 2 + 2
} else {
offset += len(cname) + 2
}
offset += len(answer)
// RDATA
dst = EncodeDomain(dst, cname)
}
// Host Records
for _, ip := range ips {
b := (*[16]byte)(unsafe.Pointer(&ip))
if ip.Is4() {
answer := [...]byte{
// NAME
0xc0 | byte(offset>>8), byte(offset),
// TYPE
0x00, byte(TypeA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x04,
// RDATA
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
} else {
answer := [...]byte{
// NAME
0xc0 | byte(offset>>8), byte(offset),
// TYPE
0x00, byte(TypeAAAA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, 0x10,
// RDATA
b[7], b[6], b[5], b[4],
b[3], b[2], b[1], b[0],
b[15], b[14], b[13], b[12],
b[11], b[10], b[9], b[8],
}
dst = append(dst, answer[:]...)
}
}
return dst
}