/// <summary> /// Create a parser from the input stream. /// </summary> /// <param name="buffer">The input stream, must not be null</param> public STUNMessageParser(ByteBuffer buffer, bool skipValidTest, List <STUNAttr> attrs = null) : this() { if (!skipValidTest && !IsValidSTUN(buffer)) { return; } ushort messageType = buffer.GetUShort(); stunClass = (STUNClass)(STUNClassConst.Mask & messageType); stunMethod = (STUNMethod)(STUNMethodConst.Mask & messageType); length = buffer.GetUShort(); uint magickCookie = buffer.GetUInt(); transaction = new Transaction(buffer.GetBuffer(Transaction.Length)); // Set buffer already, needed for FillAttributesArray and future working with this object this.buffer = buffer; if (null != attrs) { FillAttributesArray(attrs); } isValid = true; }
/// <summary> /// Create a parser from the input stream. /// </summary> /// <param name="inputStream">The input stream, must not be null</param> public STUNMessageParser(ByteBuffer inputStream, List <STUNAttr> attrs) : this() { if (null == attrs) { return; } if (inputStream.Length < STUNMessageBuilder.HEADER_LENGTH) { Logger.Error("The message is not long enough"); return; } buffer = inputStream; ushort messageType = buffer.GetUShort(); stunClass = (STUNClass)(STUNClassConst.Mask & messageType); stunMethod = (STUNMethod)(STUNMethodConst.Mask & messageType); length = buffer.GetUShort(); if (0 != length % 4) { Logger.Warn("STUN header reports a length that is not a multiple of 4"); return; } uint magickCookie = buffer.GetUInt(); if (STUNHeader.MAGIC_COOKIE != magickCookie) { Logger.Warn("Wrong Magic Cookie"); return; } transaction = new Transaction(new ByteBuffer(buffer.data, buffer.absPosition)); buffer.Position += transaction.Length; if (STUNMessageBuilder.HEADER_LENGTH + length != buffer.Length) { return; } FillAttributesArray(attrs); // check CRC if any, and fingerprint. TO DO isValid = true; }
public STUNMessageBuilder(ByteBuffer buffer, STUNClass stunClass, STUNMethod stunMethod, Transaction transaction) { if (!buffer.HasData() || buffer.Length < MINIMUM_BUFFER_SIZE) { this.buffer = new ByteBuffer(new byte[MINIMUM_BUFFER_SIZE]); Logger.Warn("The buffer is null or not large enough (" + MINIMUM_BUFFER_SIZE + " bytes). A different internal buffer has been allocated"); } else { this.buffer = buffer; } this.buffer.absPosition = HEADER_LENGTH; SetMessageType(stunClass, stunMethod); this.buffer.Put(4, STUNHeader.MAGIC_COOKIE); SetTransaction(transaction); }
/// <summary> /// Create a parser from the input stream. /// </summary> /// <param name="inputStream">The input stream, must not be null</param> public STUNMessageParser(ByteBuffer inputStream, List <STUNAttr> attrs = null) : this() { if (inputStream.Length < STUNMessageBuilder.HEADER_LENGTH) { Logger.TraceVerbose("The message is not long enough"); return; } buffer = inputStream; ushort messageType = buffer.GetUShort(); stunClass = (STUNClass)(STUNClassConst.Mask & messageType); stunMethod = (STUNMethod)(STUNMethodConst.Mask & messageType); length = buffer.GetUShort(); if (0 != length % 4 || STUNMessageBuilder.HEADER_LENGTH + length != buffer.Length) { Logger.TraceVerbose("STUN header reports a wrong length"); return; } uint magickCookie = buffer.GetUInt(); if (STUNHeader.MAGIC_COOKIE != magickCookie) { Logger.Trace("Wrong Magic Cookie"); return; } transaction = new Transaction(new ByteBuffer(buffer.data, buffer.absPosition)); buffer.Position += transaction.Length; if (null != attrs) { FillAttributesArray(attrs); } isValid = true; }
/// <summary> /// Set the message type. /// </summary> /// <param name="group">The STUN class</param> /// <param name="method">The STUN method</param> /// <returns>This builder, never null</returns> private void SetMessageType(STUNClass stunClass, STUNMethod stunMethod) { ushort stunMessageType = (ushort)(0x3FFF & ((int)stunClass | (int)stunMethod)); buffer.Put(0, stunMessageType); }
public STUNMessageBuilder(byte[] buffer, STUNClass stunClass, STUNMethod stunMethod, Transaction transaction) : this(new ByteBuffer(buffer), stunClass, stunMethod, transaction) { }