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

The Stun MessageHeader. The format of the header is specified in RFC 5389 Section 6. More...

Public Member Functions

 MessageHeader (MessageType messageType, ushort valueLength=0, ReadOnlyByteArray transactionID=null)
 Construct a MessageHeader with the provided MessageType and optional valueLength and transactionID More...
 
 MessageHeader (MessageType type, byte[] buffer, ref int bufferIndex, int bufferLength)
 Construct a MessageHeader from some bytes More...
 
void Serialize (BufferView buffer, ushort valueLength)
 Serialize this MessageHeader to the provided Buffer More...
 

Static Public Member Functions

static byte[] GenerateTransationID ()
 Generates the TransactionID More...
 

Public Attributes

readonly MessageType messageType
 Stores the MessageType that represents the MessageMethod and MessageClass of a Message More...
 
readonly ushort valueLength
 The length in bytes of the Attributes in the Message More...
 
readonly ReadOnlyByteArray transactionID
 The transactionID as bytes More...
 

Static Public Attributes

const ushort SIZE = 20
 Serialiazed size in bytes More...
 
const ushort TRANSACTION_ID_SIZE = 12
 
static RNGCryptoServiceProvider random = new RNGCryptoServiceProvider()
 Our cryptographic randomness provider More...
 

Detailed Description

The Stun MessageHeader. The format of the header is specified in RFC 5389 Section 6.

The layout is described by the diagram below: 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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0 0| STUN Message Type | Message Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Magic Cookie | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Transaction ID(96 bits) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

See also
MessageType

The message length contains the size, in bytes, of the message not including the 20-byte STUN header

The magic cookie field contains the COOKIE from Stun.Controller in network byte order

See also
transactionID

This class provides methods to generate the transaction ID as well as serialize and deserialize a MessageHeader for sending / receiving over the network.

Constructor & Destructor Documentation

◆ MessageHeader() [1/2]

NobleConnect.Stun.MessageHeader.MessageHeader ( MessageType  messageType,
ushort  valueLength = 0,
ReadOnlyByteArray  transactionID = null 
)

Construct a MessageHeader with the provided MessageType and optional valueLength and transactionID

This is called by the Message constructor when constructing a new outgoing Message for sending to a Stun server.

Parameters
messageType
valueLength
transactionID

◆ MessageHeader() [2/2]

NobleConnect.Stun.MessageHeader.MessageHeader ( MessageType  type,
byte[]  buffer,
ref int  bufferIndex,
int  bufferLength 
)

Construct a MessageHeader from some bytes

This is called by child classes when deserializing.

Parameters
buffer
bufferIndex
bufferLength

Member Function Documentation

◆ GenerateTransationID()

static byte [] NobleConnect.Stun.MessageHeader.GenerateTransationID ( )
static

Generates the TransactionID

Returns
A cryptographically random array of 12 bytes

◆ Serialize()

void NobleConnect.Stun.MessageHeader.Serialize ( BufferView  buffer,
ushort  valueLength 
)

Serialize this MessageHeader to the provided Buffer

Parameters
buffer
valueLength

Member Data Documentation

◆ messageType

readonly MessageType NobleConnect.Stun.MessageHeader.messageType

Stores the MessageType that represents the MessageMethod and MessageClass of a Message

◆ random

RNGCryptoServiceProvider NobleConnect.Stun.MessageHeader.random = new RNGCryptoServiceProvider()
static

Our cryptographic randomness provider

◆ SIZE

const ushort NobleConnect.Stun.MessageHeader.SIZE = 20
static

Serialiazed size in bytes

◆ TRANSACTION_ID_SIZE

const ushort NobleConnect.Stun.MessageHeader.TRANSACTION_ID_SIZE = 12
static

◆ transactionID

readonly ReadOnlyByteArray NobleConnect.Stun.MessageHeader.transactionID

The transactionID as bytes

The transaction ID is a 96-bit identifier, used to uniquely identify STUN transactions. For request/response transactions, the transaction ID is chosen by the STUN client for the request and echoed by the server in the response. For indications, it is chosen by the agent sending the indication. It primarily serves to correlate requests with responses, though it also plays a small role in helping to prevent certain types of attacks. The transaction ID is uniformly and randomly chosen from the interval 0 .. 2**96-1, and is cryptographically random.

◆ valueLength

readonly ushort NobleConnect.Stun.MessageHeader.valueLength

The length in bytes of the Attributes in the Message