github.com/pion/dtls/v2@v2.2.12/pkg/protocol/extension/renegotiation_info.go (about)

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