renegotiation_info.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package extension
  4. import "encoding/binary"
  5. const (
  6. renegotiationInfoHeaderSize = 5
  7. )
  8. // RenegotiationInfo allows a Client/Server to
  9. // communicate their renegotation support
  10. //
  11. // https://tools.ietf.org/html/rfc5746
  12. type RenegotiationInfo struct {
  13. RenegotiatedConnection uint8
  14. }
  15. // TypeValue returns the extension TypeValue
  16. func (r RenegotiationInfo) TypeValue() TypeValue {
  17. return RenegotiationInfoTypeValue
  18. }
  19. // Marshal encodes the extension
  20. func (r *RenegotiationInfo) Marshal() ([]byte, error) {
  21. out := make([]byte, renegotiationInfoHeaderSize)
  22. binary.BigEndian.PutUint16(out, uint16(r.TypeValue()))
  23. binary.BigEndian.PutUint16(out[2:], uint16(1)) // length
  24. out[4] = r.RenegotiatedConnection
  25. return out, nil
  26. }
  27. // Unmarshal populates the extension from encoded data
  28. func (r *RenegotiationInfo) Unmarshal(data []byte) error {
  29. if len(data) < renegotiationInfoHeaderSize {
  30. return errBufferTooSmall
  31. } else if TypeValue(binary.BigEndian.Uint16(data)) != r.TypeValue() {
  32. return errInvalidExtensionType
  33. }
  34. r.RenegotiatedConnection = data[4]
  35. return nil
  36. }