| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"codeberg.org/miekg/dns/internal/pack" |
| 7 |
"codeberg.org/miekg/dns/internal/unpack" |
| 8 |
"golang.org/x/crypto/cryptobyte" |
| 9 |
) |
| 10 |
|
| 11 |
func unpackOPT(s *cryptobyte.String) ([]EDNS0, error) { |
| 12 |
edns0 := []EDNS0{} |
| 13 |
for !s.Empty() { |
| 14 |
var ( |
| 15 |
code uint16 |
| 16 |
data cryptobyte.String |
| 17 |
) |
| 18 |
if !s.ReadUint16(&code) || !s.ReadUint16LengthPrefixed(&data) { |
| 19 |
return nil, unpack.ErrOverflow |
| 20 |
} |
| 21 |
var option EDNS0 |
| 22 |
if newFn, ok := CodeToRR[code]; ok { |
| 23 |
option = newFn() |
| 24 |
} else { |
| 25 |
option = &ERFC3597{EDNS0Code: code} |
| 26 |
} |
| 27 |
if err := unpackOptionCode(option, &data); err != nil { |
| 28 |
return nil, err |
| 29 |
} |
| 30 |
edns0 = append(edns0, option) |
| 31 |
} |
| 32 |
return edns0, nil |
| 33 |
} |
| 34 |
|
| 35 |
func packOPT(options []EDNS0, msg []byte, off int) (int, error) { |
| 36 |
for i := range options { |
| 37 |
l := options[i].Len() |
| 38 |
if off+l >= len(msg) { |
| 39 |
return len(msg), pack.ErrBuf |
| 40 |
} |
| 41 |
code := RRToCode(options[i]) |
| 42 |
if code == CodeNone { |
| 43 |
if erfc3597, ok := options[i].(*ERFC3597); ok { |
| 44 |
code = erfc3597.EDNS0Code |
| 45 |
} else { |
| 46 |
// really the last option |
| 47 |
return len(msg), fmt.Errorf("unknown option code") |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
pack.Uint16(code, msg, off) |
| 52 |
pack.Uint16(uint16(l-tlv), msg, off+2) |
| 53 |
if /*optionoff*/ _, err := packOptionCode(options[i], msg, off+4); err != nil { |
| 54 |
return len(msg), err |
| 55 |
} |
| 56 |
// TODO(miek): if l != opentionoff ? We overestimated l, but that's the length we've packed |
| 57 |
off += l |
| 58 |
} |
| 59 |
return off, nil |
| 60 |
} |