// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package handshake

import (
	
)

// MessageHelloVerifyRequest is as follows:
//
//	struct {
//	  ProtocolVersion server_version;
//	  opaque cookie<0..2^8-1>;
//	} HelloVerifyRequest;
//
//	The HelloVerifyRequest message type is hello_verify_request(3).
//
//	When the client sends its ClientHello message to the server, the server
//	MAY respond with a HelloVerifyRequest message.  This message contains
//	a stateless cookie generated using the technique of [PHOTURIS].  The
//	client MUST retransmit the ClientHello with the cookie added.
//
//	https://tools.ietf.org/html/rfc6347#section-4.2.1
type MessageHelloVerifyRequest struct {
	Version protocol.Version
	Cookie  []byte
}

// Type returns the Handshake Type.
func ( MessageHelloVerifyRequest) () Type {
	return TypeHelloVerifyRequest
}

// Marshal encodes the Handshake.
func ( *MessageHelloVerifyRequest) () ([]byte, error) {
	if len(.Cookie) > 255 {
		return nil, errCookieTooLong
	}

	 := make([]byte, 3+len(.Cookie))
	[0] = .Version.Major
	[1] = .Version.Minor
	[2] = byte(len(.Cookie))
	copy([3:], .Cookie)

	return , nil
}

// Unmarshal populates the message from encoded data.
func ( *MessageHelloVerifyRequest) ( []byte) error {
	if len() < 3 {
		return errBufferTooSmall
	}
	.Version.Major = [0]
	.Version.Minor = [1]
	 := int([2])
	if len() < +3 {
		return errBufferTooSmall
	}
	.Cookie = make([]byte, )

	copy(.Cookie, [3:3+])

	return nil
}