Noble Connect
Add relays and punchthrough to Mirror or UNet
NobleConnect.Stun.Attribute Class Reference

Represents a Stun attribute that can be added to a Stun message. More...

Inherited by NobleConnect.Stun.AttributeChangeRequest, NobleConnect.Stun.AttributeData, NobleConnect.Stun.AttributeErrorCode, NobleConnect.Stun.AttributeInt, NobleConnect.Stun.AttributeLong, NobleConnect.Stun.AttributeMappedAddress, NobleConnect.Stun.AttributeRaw, NobleConnect.Stun.AttributeString, NobleConnect.Turn.AttributeAddressFamily, NobleConnect.Turn.AttributeChannelNumber, and NobleConnect.Turn.AttributeProtocol.

Public Member Functions

 Attribute (AttributeType type)
 Construct an Attribute of the provided AttributeType More...
 
 Attribute (AttributeType type, ref int offset)
 
virtual void SerializeValues (BufferView buffer, ReadOnlyByteArray transactionID)
 Serialize Attribute values and add them to the buffer. More...
 
void Serialize (BufferView buffer, ReadOnlyByteArray transactionID)
 Convert this attribute to a bunch of bytes and add them to the provided buffer More...
 
virtual ushort GetValueSize ()
 Get the serialized size of the attribute values More...
 
ushort GetSize ()
 

Static Public Member Functions

static ushort DeserializeLength (byte[] buffer, ref int offset)
 Read the Attribute length from a byte array. More...
 
static ushort GetNumPaddingBytes (int length)
 Calculate padding based on length More...
 

Public Attributes

readonly AttributeType attributeType
 The AttributeType of this Attribute More...
 

Static Public Attributes

const int HEADER_SIZE = 4
 

Detailed Description

Represents a Stun attribute that can be added to a Stun message.

All Stun messages are essentially a header followed by zero or more attributes.

Stun attribute format is described in RFC 5389 Section 15

0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Value(variable) .... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Attribute Types are defined in RFC 5389 Section 18.2 and RFC 5780 Section 9.1

The format of the Value field is defined for each attribute in RFC 5389 Section 15 and RFC 5780 Section 7

Each STUN attribute ends on a 32-bit (4 byte) boundary. The value in the length field contains the length of the Value part of the attribute, prior to padding, measured in bytes. Attributes whose content is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes.

Attributes are immutable after creation. This is so that they can be safely shared between threads.

In order to maintain immutability Attributes: Do not expose any reference types unless the type is itself immutable. Do not expose arrays, instead they use ReadOnlyCollection. Can have public ValueType properties as long as they are readonly

Constructor & Destructor Documentation

◆ Attribute() [1/2]

NobleConnect.Stun.Attribute.Attribute ( AttributeType  type)

Construct an Attribute of the provided AttributeType

This is called when constructing a new Attribute to send to a Stun server and also when deserializing an attribute from a Stun server.

Parameters
type

◆ Attribute() [2/2]

NobleConnect.Stun.Attribute.Attribute ( AttributeType  type,
ref int  offset 
)

Member Function Documentation

◆ DeserializeLength()

static ushort NobleConnect.Stun.Attribute.DeserializeLength ( byte[]  buffer,
ref int  offset 
)
static

Read the Attribute length from a byte array.

Parameters
buffer
offset
Returns

◆ GetNumPaddingBytes()

static ushort NobleConnect.Stun.Attribute.GetNumPaddingBytes ( int  length)
static

Calculate padding based on length

Parameters
length

◆ GetSize()

ushort NobleConnect.Stun.Attribute.GetSize ( )

◆ GetValueSize()

◆ Serialize()

void NobleConnect.Stun.Attribute.Serialize ( BufferView  buffer,
ReadOnlyByteArray  transactionID 
)

Convert this attribute to a bunch of bytes and add them to the provided buffer

Note that unlike Deserialize, the attributeType is handled here. If the Length is not divisibly by 4 then extra padding is added to account for 32bit alignment as per RFC 5389 Section 15.

Parameters
buffer

◆ SerializeValues()

virtual void NobleConnect.Stun.Attribute.SerializeValues ( BufferView  buffer,
ReadOnlyByteArray  transactionID 
)
virtual

Member Data Documentation

◆ attributeType

readonly AttributeType NobleConnect.Stun.Attribute.attributeType

The AttributeType of this Attribute

◆ HEADER_SIZE

const int NobleConnect.Stun.Attribute.HEADER_SIZE = 4
static