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

package proto

import 

// EvenPort represents EVEN-PORT attribute.
//
// This attribute allows the client to request that the port in the
// relayed transport address be even, and (optionally) that the server
// reserve the next-higher port number.
//
// RFC 5766 Section 14.6.
type EvenPort struct {
	// ReservePort means that the server is requested to reserve
	// the next-higher port number (on the same IP address)
	// for a subsequent allocation.
	ReservePort bool
}

func ( EvenPort) () string {
	if .ReservePort {
		return "reserve: true"
	}

	return "reserve: false"
}

const (
	evenPortSize = 1
	firstBitSet  = (1 << 8) - 1 // 0b100000000
)

// AddTo adds EVEN-PORT to message.
func ( EvenPort) ( *stun.Message) error {
	 := make([]byte, evenPortSize)
	if .ReservePort {
		// Set first bit to 1.
		[0] = firstBitSet
	}
	.Add(stun.AttrEvenPort, )

	return nil
}

// GetFrom decodes EVEN-PORT from message.
func ( *EvenPort) ( *stun.Message) error {
	,  := .Get(stun.AttrEvenPort)
	if  != nil {
		return 
	}
	if  = stun.CheckSize(stun.AttrEvenPort, len(), evenPortSize);  != nil {
		return 
	}
	if [0]&firstBitSet > 0 {
		.ReservePort = true
	}

	return nil
}