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

A Stun Message More...

Inherits NobleConnect.IMessage.

Public Member Functions

 Message (MessageHeader header, List< Attribute > attributes=null, int retryCount=0)
 Construct a Message with the provided MessageHeader and optional Attributes More...
 
 Message (MessageClass messageClass, MessageMethod messageMethod, List< Attribute > attributes=null)
 Construct a Message with the provided MessageClass and MessageMethod with optional Attributes More...
 
GetAttribute< T > (AttributeType type)
 Get the first Attribute of the provided AttributeType from this Message. More...
 
AttributeRaw GetMessageIntegrity ()
 Get the first Attribute of type MessageIntegrity from this Message. More...
 
AttributeXORMappedAddress GetXORMappedAddress ()
 Get the first Attribute of type XORMappedAddress from this Message. More...
 
AttributeMappedAddress GetMappedAddress ()
 Get the first Attribute of type MappedAddress from this Message. More...
 
AttributeString GetUsername ()
 Get the first Attribute of type Username from this Message. More...
 
AttributeString GetRealm ()
 Get the first Attribute of type Realm from this Message. More...
 
AttributeString GetNonce ()
 Get the first Attribute of type Nonce from this Message. More...
 
AttributeErrorCode GetErrorCode ()
 Get the first Attribute of type ErrorCode from this Message. More...
 
void Serialize (BufferView buffer)
 Serialize the Message to the provided Buffer for sending to a Stun server More...
 

Static Public Member Functions

static void Serialize (BufferView buffer, MessageHeader header, IEnumerable< Attribute > attributes, ushort overrideValueLength=0)
 Serialize a MessageHeader and Attribute list to the provided buffer. More...
 
static void AddCredentials (Credentials credentials, MessageHeader header, List< Attribute > attributeList)
 Add long-term authentication credentials to list of attributes. More...
 
static ushort CalculateValueLength (IEnumerable< Attribute > attributeList)
 Calculate the total byte length of the provided attribute list. More...
 
static bool IsStunMessage (byte[] bytes, int offset, int bytesRead)
 Inspect some bytes and determine if they represent a Stun message More...
 

Public Attributes

readonly MessageHeader header
 This Message's MessageHeader. More...
 
readonly ReadOnlyCollection< Attributeattributes
 The message attributes. More...
 
int retryCount = 0
 

Properties

MessageType MessageType [get]
 The MessageType of this Message More...
 
MessageClass MessageClass [get]
 The MessageClass of this Message More...
 
MessageMethod MessageMethod [get]
 The MessageMethod of this Message More...
 
override ReadOnlyByteArray TransactionID [get]
 The transactionID of this Message More...
 
- Properties inherited from NobleConnect.IMessage
abstract ReadOnlyByteArray TransactionID [get]
 

Detailed Description

A Stun Message

Messages are comprised of a MessageHeader followed by zero or more Attributes This class also provides a convenient method for adding Credentials to a message. Messages are immutable so that they can be safely passed to another thread.

Constructor & Destructor Documentation

◆ Message() [1/2]

NobleConnect.Stun.Message.Message ( MessageHeader  header,
List< Attribute attributes = null,
int  retryCount = 0 
)

Construct a Message with the provided MessageHeader and optional Attributes

Parameters
header
attributes

◆ Message() [2/2]

NobleConnect.Stun.Message.Message ( MessageClass  messageClass,
MessageMethod  messageMethod,
List< Attribute attributes = null 
)

Construct a Message with the provided MessageClass and MessageMethod with optional Attributes

A MessageHeader is constructed from the provided MessageClass and MessageMethod.

Parameters
messageClass
messageMethod
attributes

Member Function Documentation

◆ AddCredentials()

static void NobleConnect.Stun.Message.AddCredentials ( Credentials  credentials,
MessageHeader  header,
List< Attribute attributeList 
)
static

Add long-term authentication credentials to list of attributes.

To use this first construct a MessageHeader and the full list of Attributes to send. Then pass your Credentials, MessageHeader, and Attribute list into this method. Several attributes will be added to the attribute list (username, realm, nonce, and message integrity). The header and attribute list can then be used to construct a new message that can be properly authenticated by the Stun server. This MUST be called after all other Attributes have already been added to the list. If you add an Attribute to the list after calling this method then the message integrity check will fail on the Stun server and the message will be rejected. See RFC 5389 Section 10.2 for details about the long-term authentication process.

Parameters
credentials

◆ CalculateValueLength()

static ushort NobleConnect.Stun.Message.CalculateValueLength ( IEnumerable< Attribute attributeList)
static

Calculate the total byte length of the provided attribute list.

This accounts for any padding required to keep bytes on 32bit alignment.

Returns

◆ GetAttribute< T >()

T NobleConnect.Stun.Message.GetAttribute< T > ( AttributeType  type)

Get the first Attribute of the provided AttributeType from this Message.

Template Parameters
TThe class to cast the Attribute as.
Parameters
typeThe AttributeType to search for
Returns
Type Constraints
T :Attribute 

◆ GetErrorCode()

AttributeErrorCode NobleConnect.Stun.Message.GetErrorCode ( )

Get the first Attribute of type ErrorCode from this Message.

Returns

◆ GetMappedAddress()

AttributeMappedAddress NobleConnect.Stun.Message.GetMappedAddress ( )

Get the first Attribute of type MappedAddress from this Message.

Returns

◆ GetMessageIntegrity()

AttributeRaw NobleConnect.Stun.Message.GetMessageIntegrity ( )

Get the first Attribute of type MessageIntegrity from this Message.

Returns

◆ GetNonce()

AttributeString NobleConnect.Stun.Message.GetNonce ( )

Get the first Attribute of type Nonce from this Message.

Returns

◆ GetRealm()

AttributeString NobleConnect.Stun.Message.GetRealm ( )

Get the first Attribute of type Realm from this Message.

Returns

◆ GetUsername()

AttributeString NobleConnect.Stun.Message.GetUsername ( )

Get the first Attribute of type Username from this Message.

Returns

◆ GetXORMappedAddress()

AttributeXORMappedAddress NobleConnect.Stun.Message.GetXORMappedAddress ( )

Get the first Attribute of type XORMappedAddress from this Message.

Returns

◆ IsStunMessage()

static bool NobleConnect.Stun.Message.IsStunMessage ( byte[]  bytes,
int  offset,
int  bytesRead 
)
static

Inspect some bytes and determine if they represent a Stun message

This is accomplished by checking that: The first two bits are 0. The COOKIE is present and correct. TODO: Check that the fingerprint is present and correct (when enabled)

Parameters
bytes
bytesRead
Returns

◆ Serialize() [1/2]

void NobleConnect.Stun.Message.Serialize ( BufferView  buffer)

Serialize the Message to the provided Buffer for sending to a Stun server

Parameters
buffer
payloadLength

◆ Serialize() [2/2]

static void NobleConnect.Stun.Message.Serialize ( BufferView  buffer,
MessageHeader  header,
IEnumerable< Attribute attributes,
ushort  overrideValueLength = 0 
)
static

Serialize a MessageHeader and Attribute list to the provided buffer.

Calling the Serialize() method on a message instance will call this static method and pass in the Message's MessageHeader and attributes in order to serialize them to the provided buffer.

This method is also called directly while crafting the AttributeMessageIntegrity to partially serialize a Message as part of generating the integrity hash. The value length can be overridden because AttributeMessageIntegrity requires the length in the header to include the length of the AttributeMessageIntegrity itself even though it is not included in the attribute list for partial serialization.

Parameters
buffer
header
attributes
overrideValueLength

Member Data Documentation

◆ attributes

readonly ReadOnlyCollection<Attribute> NobleConnect.Stun.Message.attributes

The message attributes.

Attribute

◆ header

readonly MessageHeader NobleConnect.Stun.Message.header

◆ retryCount

int NobleConnect.Stun.Message.retryCount = 0

Property Documentation

◆ MessageClass

MessageClass NobleConnect.Stun.Message.MessageClass
get

The MessageClass of this Message

◆ MessageMethod

MessageMethod NobleConnect.Stun.Message.MessageMethod
get

The MessageMethod of this Message

◆ MessageType

MessageType NobleConnect.Stun.Message.MessageType
get

The MessageType of this Message

◆ TransactionID

override ReadOnlyByteArray NobleConnect.Stun.Message.TransactionID
get

The transactionID of this Message

MessageHeader.transactionID