Class StunMessage
A STUN message: method, class, transaction ID and attributes, parsed from or encoded to the wire with optional MESSAGE-INTEGRITY and FINGERPRINT.
Assembly: Uniform.Stun.Protocol.dll
public sealed class StunMessage
Constructors
|
Edit this page
View Source
Create a message, with a random transaction ID unless given.
Declaration
public StunMessage(StunMethod method, StunClass messageClass, byte[]? transactionId = null)
Parameters
Fields
|
Edit this page
View Source
The magic cookie every STUN message carries.
Declaration
public const uint MagicCookie = 554869826
Field Value
Properties
|
Edit this page
View Source
Declaration
public List<StunAttribute> Attributes { get; }
Property Value
|
Edit this page
View Source
Declaration
public StunClass Class { get; }
Property Value
|
Edit this page
View Source
The code of the ERROR-CODE attribute, e.g. 401, or null when there is none.
Declaration
public int? ErrorCode { get; }
Property Value
|
Edit this page
View Source
Declaration
public StunMethod Method { get; }
Property Value
|
Edit this page
View Source
The 12-byte transaction ID that pairs a response with its request.
Declaration
public byte[] TransactionId { get; }
Property Value
Methods
|
Edit this page
View Source
Declaration
public StunMessage Add(ushort type, byte[] value)
Parameters
Returns
|
Edit this page
View Source
Add a UTF-8 text attribute.
Declaration
public StunMessage Add(ushort type, string value)
Parameters
Returns
|
Edit this page
View Source
Add a 32-bit number attribute, e.g. a LIFETIME.
Declaration
public StunMessage Add(ushort type, uint value)
Parameters
Returns
|
Edit this page
View Source
Add an ERROR-CODE with code and its reason phrase; see StunErrorCodes.
Declaration
public StunMessage AddError(int code, string? reason = null)
Parameters
Returns
|
Edit this page
View Source
Return whether datagram ends with a correct FINGERPRINT.
Declaration
public static bool CheckFingerprint(ReadOnlySpan<byte> datagram)
Parameters
Returns
|
Edit this page
View Source
Return whether datagram, this message as received, has a MESSAGE-INTEGRITY made with key.
Declaration
public static bool CheckIntegrity(ReadOnlySpan<byte> datagram, byte[] key)
Parameters
Returns
|
Edit this page
View Source
Return the message on the wire, followed by a MESSAGE-INTEGRITY with integrityKey when given, and a FINGERPRINT when asked.
Declaration
public byte[] Encode(byte[]? integrityKey = null, bool fingerprint = false)
Parameters
| Type |
Name |
Description |
| byte[] |
integrityKey |
Key of the HMAC-SHA1: an ICE password's bytes, or a long-term key from LongTermKey(string, string, string).
|
| bool |
fingerprint |
Whether to end with a FINGERPRINT, as ICE requires.
|
Returns
|
Edit this page
View Source
Return the first value of an attribute type, or null.
Declaration
public byte[]? Get(ushort type)
Parameters
| Type |
Name |
Description |
| ushort |
type |
|
Returns
|
Edit this page
View Source
GetText(ushort)
Return the first value of an attribute type as UTF-8 text, or null.
Declaration
public string? GetText(ushort type)
Parameters
| Type |
Name |
Description |
| ushort |
type |
|
Returns
|
Edit this page
View Source
Return the first value of an attribute type as a 32-bit number, or null when missing or of another length.
Declaration
public uint? GetUInt32(ushort type)
Parameters
| Type |
Name |
Description |
| ushort |
type |
|
Returns
|
Edit this page
View Source
Return whether a datagram looks like STUN: the first byte, as RFC 7983 tells it from DTLS and RTP, and the magic cookie.
Declaration
public static bool IsStun(ReadOnlySpan<byte> datagram)
Parameters
Returns
|
Edit this page
View Source
Return the key of long-term credentials, as TURN servers use: MD5 of username:realm:password. The inputs must already be SASLprep'ed; plain ASCII is.
Declaration
public static byte[] LongTermKey(string username, string realm, string password)
Parameters
Returns
|
Edit this page
View Source
Declaration
public static StunMessage? Parse(ReadOnlySpan<byte> datagram)
Parameters
Returns