Debian Codegraph @

zednspack.go

raw
1 package dns
2
3 // should be generated, it is not...
4
5 import (
6 "encoding/hex"
7 "net"
8 "net/netip"
9
10 "codeberg.org/miekg/dns/internal/pack"
11 "codeberg.org/miekg/dns/internal/unpack"
12 "golang.org/x/crypto/cryptobyte"
13 )
14
15 func (o *LLQ) pack(msg []byte, off int) (off1 int, err error) {
16 off, err = pack.Uint16(o.Version, msg, off)
17 if err != nil {
18 return off, err
19 }
20 off, err = pack.Uint16(o.Opcode, msg, off)
21 if err != nil {
22 return off, err
23 }
24 off, err = pack.Uint16(o.Error, msg, off)
25 if err != nil {
26 return off, err
27 }
28 off, err = pack.Uint64(o.ID, msg, off)
29 if err != nil {
30 return off, err
31 }
32 off, err = pack.Uint32(o.LeaseLife, msg, off)
33 if err != nil {
34 return off, err
35 }
36 return off, nil
37 }
38
39 func (o *LLQ) unpack(s *cryptobyte.String) error {
40 if !s.ReadUint16(&o.Version) {
41 return unpack.ErrOverflow
42 }
43 if !s.ReadUint16(&o.Opcode) {
44 return unpack.ErrOverflow
45 }
46 if !s.ReadUint16(&o.Error) {
47 return unpack.ErrOverflow
48 }
49 if !s.ReadUint64(&o.ID) {
50 return unpack.ErrOverflow
51 }
52 if !s.ReadUint32(&o.LeaseLife) {
53 return unpack.ErrOverflow
54 }
55 return nil
56 }
57
58 func (o *UPDATELEASE) pack(msg []byte, off int) (int, error) {
59 off, err := pack.Uint32(o.Lease, msg, off)
60 if err != nil {
61 return off, err
62 }
63 off, err = pack.Uint32(o.KeyLease, msg, off)
64 if err != nil {
65 return off, err
66 }
67 return off, nil
68 }
69
70 func (o *UPDATELEASE) unpack(s *cryptobyte.String) error {
71 if !s.ReadUint32(&o.Lease) {
72 return unpack.ErrOverflow
73 }
74 if !s.ReadUint32(&o.KeyLease) {
75 return unpack.ErrOverflow
76 }
77 return nil
78 }
79
80 func (o *NSID) unpack(s *cryptobyte.String) error {
81 o.Nsid = hex.EncodeToString(*s)
82 return nil
83 }
84
85 func (o *NSID) pack(msg []byte, off int) (int, error) {
86 return hex.Decode(msg[off:], []byte(o.Nsid))
87 }
88
89 func (o *COOKIE) pack(msg []byte, off int) (int, error) {
90 return hex.Decode(msg[off:], []byte(o.Cookie))
91 }
92
93 func (o *COOKIE) unpack(s *cryptobyte.String) error {
94 o.Cookie = hex.EncodeToString(*s)
95 return nil
96 }
97
98 func (o *PADDING) unpack(s *cryptobyte.String) error {
99 o.Padding = hex.EncodeToString(*s)
100 return nil
101 }
102
103 func (o *PADDING) pack(msg []byte, off int) (int, error) {
104 return hex.Decode(msg[off:], []byte(o.Padding))
105 }
106
107 func (o *DAU) pack(msg []byte, off int) (off1 int, err error) {
108 for i := range o.AlgCode {
109 if off, err = pack.Uint8(o.AlgCode[i], msg, off); err != nil {
110 return off, err
111 }
112 }
113 return off, nil
114 }
115
116 func (o *DAU) unpack(s *cryptobyte.String) error {
117 for !s.Empty() {
118 var a uint8
119 s.ReadUint8(&a)
120 o.AlgCode = append(o.AlgCode, a)
121 }
122 return nil
123 }
124
125 func (o *DHU) pack(msg []byte, off int) (off1 int, err error) {
126 for i := range o.AlgCode {
127 if off, err = pack.Uint8(o.AlgCode[i], msg, off); err != nil {
128 return off, err
129 }
130 }
131 return off, nil
132 }
133
134 func (o *DHU) unpack(s *cryptobyte.String) error {
135 for !s.Empty() {
136 var a uint8
137 s.ReadUint8(&a)
138 o.AlgCode = append(o.AlgCode, a)
139 }
140 return nil
141 }
142
143 func (o *N3U) pack(msg []byte, off int) (off1 int, err error) {
144 for i := range o.AlgCode {
145 if off, err = pack.Uint8(o.AlgCode[i], msg, off); err != nil {
146 return off, err
147 }
148 }
149 return off, nil
150 }
151
152 func (o *N3U) unpack(s *cryptobyte.String) error {
153 for !s.Empty() {
154 var a uint8
155 s.ReadUint8(&a)
156 o.AlgCode = append(o.AlgCode, a)
157 }
158 return nil
159 }
160
161 func (o *EDE) unpack(s *cryptobyte.String) (err error) {
162 if !s.ReadUint16(&o.InfoCode) {
163 return unpack.ErrOverflow
164 }
165 if o.ExtraText, err = unpack.StringAny(s, len(*s)); err != nil {
166 return unpack.Errorf("overflow EDE option")
167 }
168 return nil
169 }
170
171 func (o *EDE) pack(msg []byte, off int) (int, error) {
172 off, err := pack.Uint16(o.InfoCode, msg, off)
173 if err != nil {
174 return off, err
175 }
176 copy(msg[off:], []byte(o.ExtraText))
177 return off + len(o.ExtraText), nil
178 }
179
180 func (e *REPORTING) unpack(s *cryptobyte.String) (err error) {
181 e.AgentDomain, err = unpack.Name(s, nil) // TODO: unpackNAme with nil buffer, no compression pointers..
182 if err != nil {
183 return unpack.Errorf("overflow REPORTING AgentDomain")
184 }
185 return nil
186 }
187
188 func (e *REPORTING) pack(msg []byte, off int) (int, error) {
189 return pack.Name(e.AgentDomain, msg, off, nil, false)
190 }
191
192 func (o *EXPIRE) pack(msg []byte, off int) (int, error) {
193 if o.Expire == 0 {
194 return off, nil
195 }
196 return pack.Uint32(o.Expire, msg, off)
197 }
198
199 func (o *EXPIRE) unpack(s *cryptobyte.String) error {
200 if s.Empty() { // zero-length EXPIRE query, see RFC 7314 Section 2
201 o.Expire = 0
202 return nil
203 }
204 if !s.ReadUint32(&o.Expire) {
205 return unpack.ErrOverflow
206 }
207 return nil
208 }
209
210 func (o *TCPKEEPALIVE) pack(msg []byte, off int) (int, error) {
211 if o.Timeout > 0 {
212 return pack.Uint16(o.Timeout, msg, off)
213 }
214 return off, nil
215 }
216
217 func (o *TCPKEEPALIVE) unpack(s *cryptobyte.String) error {
218 if s.Empty() {
219 return nil
220 }
221 if !s.ReadUint16(&o.Timeout) {
222 return unpack.ErrOverflow
223 }
224 return nil
225 }
226
227 func (o *SUBNET) pack(msg []byte, off int) (int, error) {
228 var err error
229 if off, err = pack.Uint16(o.Family, msg, off); err != nil {
230 return off, err
231 }
232 if off, err = pack.Uint8(o.Netmask, msg, off); err != nil {
233 return off, err
234 }
235 if off, err = pack.Uint8(o.Scope, msg, off); err != nil {
236 return off, err
237 }
238
239 n := int((o.Netmask + 7) / 8)
240 switch o.Family {
241 case 1:
242 if n > net.IPv4len {
243 return off, pack.Errorf("overflow SUBNET a Netmask")
244 }
245 if n == 0 {
246 return off, nil
247 }
248 addr := o.Address.Unmap()
249 if !addr.IsValid() || !addr.Is4() {
250 return off, pack.Errorf("bad address family")
251 }
252 if off+n > len(msg) {
253 return len(msg), pack.ErrBuf
254 }
255 a := addr.As4()
256 copy(msg[off:off+n], a[:n])
257 off += n
258 case 2:
259 if n > net.IPv6len {
260 return off, pack.Errorf("overflow SUBNET aaaa Netmask")
261 }
262 if n == 0 {
263 return off, nil
264 }
265 if !o.Address.IsValid() || !o.Address.Is6() {
266 return off, pack.Errorf("bad address family")
267 }
268 if off+n > len(msg) {
269 return len(msg), pack.ErrBuf
270 }
271 a := o.Address.As16()
272 copy(msg[off:off+n], a[:n])
273 off += n
274 default:
275 return off, pack.Errorf("bad address family")
276 }
277 return off, nil
278 }
279
280 func (o *SUBNET) unpack(s *cryptobyte.String) (err error) {
281 if !s.ReadUint16(&o.Family) {
282 return unpack.ErrOverflow
283 }
284 if !s.ReadUint8(&o.Netmask) {
285 return unpack.ErrOverflow
286 }
287 if !s.ReadUint8(&o.Scope) {
288 return unpack.ErrOverflow
289 }
290 n := o.Netmask / 8
291 switch o.Family {
292 case 0:
293 // TODO(miek): make something that does not do a full parse.
294 o.Address = netip.MustParseAddr("0.0.0.0")
295 case 1:
296 if n > net.IPv4len {
297 return unpack.Errorf("overflow SUBNET a Netmask")
298 }
299 in := make([]byte, net.IPv4len)
300 if !s.CopyBytes(in[:n]) {
301 return unpack.Errorf("overflow SUBNET a")
302 }
303 var ok bool
304 if o.Address, ok = netip.AddrFromSlice(in); !ok {
305 return unpack.Errorf("overflow SUBNET a")
306 }
307 case 2:
308 if n > net.IPv6len {
309 return unpack.Errorf("overflow SUBNET aaaa Netmask")
310 }
311 in := make([]byte, net.IPv6len)
312 if !s.CopyBytes(in[:n]) {
313 return unpack.Errorf("overflow SUBNET aaaa")
314 }
315 var ok bool
316 if o.Address, ok = netip.AddrFromSlice(in); !ok {
317 return unpack.Errorf("overflow SUBNET aaaa")
318 }
319 default:
320 return unpack.Errorf("bad address family")
321 }
322 return nil
323 }
324
325 func (o *ESU) pack(msg []byte, off int) (int, error) {
326 return pack.StringAny(o.URI, msg, off)
327 }
328
329 func (o *ESU) unpack(s *cryptobyte.String) (err error) {
330 o.URI, err = unpack.StringAny(s, len(*s))
331 return err
332 }
333
334 func (o *ZONEVERSION) pack(msg []byte, off int) (int, error) {
335 off, err := pack.Uint8(o.Labels, msg, off)
336 if err != nil {
337 return off, err
338 }
339 off, err = pack.Uint8(o.Type, msg, off)
340 if err != nil {
341 return off, err
342 }
343 return pack.StringAny(string(o.Version), msg, off)
344 }
345
346 func (o *ZONEVERSION) unpack(s *cryptobyte.String) (err error) {
347 if !s.ReadUint8(&o.Labels) {
348 return unpack.ErrOverflow
349 }
350 if !s.ReadUint8(&o.Type) {
351 return unpack.ErrOverflow
352 }
353 v, err := unpack.StringAny(s, len(*s))
354 o.Version = []byte(v)
355 return err
356 }
357
358 func (o *ERFC3597) pack(msg []byte, off int) (int, error) {
359 return hex.Decode(msg[off:], []byte(o.Code))
360 }
361
362 func (o *ERFC3597) unpack(s *cryptobyte.String) error {
363 o.Code = hex.EncodeToString(*s)
364 return nil
365 }
366
367 func (o *MQRESPONSE) pack(msg []byte, off int) (int, error) {
368 var err error
369 for _, t := range o.Types {
370 if off, err = pack.Uint16(t, msg, off); err != nil {
371 return len(msg), err
372 }
373 }
374 return off, nil
375 }
376
377 func (o *MQRESPONSE) unpack(s *cryptobyte.String) error {
378 for !s.Empty() {
379 var t uint16
380 if !s.ReadUint16(&t) {
381 return unpack.ErrTrailingData
382 }
383 o.Types = append(o.Types, t)
384 }
385 return nil
386 }
387
388 func (o *MQQUERY) pack(msg []byte, off int) (int, error) {
389 var err error
390 for _, t := range o.Types {
391 if off, err = pack.Uint16(t, msg, off); err != nil {
392 return len(msg), err
393 }
394 }
395 return off, nil
396 }
397
398 func (o *MQQUERY) unpack(s *cryptobyte.String) error {
399 for !s.Empty() {
400 var t uint16
401 if !s.ReadUint16(&t) {
402 return unpack.ErrTrailingData
403 }
404 o.Types = append(o.Types, t)
405 }
406 return nil
407 }
408
409 func (o *KEYTAG) pack(msg []byte, off int) (int, error) {
410 var err error
411 for _, t := range o.Types {
412 if off, err = pack.Uint16(t, msg, off); err != nil {
413 return len(msg), err
414 }
415 }
416 return off, nil
417 }
418
419 func (o *KEYTAG) unpack(s *cryptobyte.String) error {
420 for !s.Empty() {
421 var t uint16
422 if !s.ReadUint16(&t) {
423 return unpack.ErrTrailingData
424 }
425 o.Types = append(o.Types, t)
426 }
427 return nil
428 }