public void ParseCoturnSTUNResponseTestMethod() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); byte[] stunResp = new byte[]{ 0x01, 0x01, 0x00, 0x44, 0x21, 0x12, 0xa4, 0x42, 0x6b, 0x4c, 0xf3, 0x18, 0xd0, 0xa7, 0xf5, 0x40, 0x97, 0x30, 0x3a, 0x27, 0x00, 0x20, 0x00, 0x08, 0x00, 0x01, 0x9e, 0x90, 0x1a, 0xb5, 0x08, 0xf3, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0xbf, 0x82, 0x3b, 0xa7, 0xac, 0xb1, 0x80, 0x2b, 0x00, 0x08, 0x00, 0x01, 0x0d, 0x96, 0x67, 0x1d, 0x42, 0xf3, 0x80, 0x22, 0x00, 0x1a, 0x43, 0x6f, 0x74, 0x75, 0x72, 0x6e, 0x2d, 0x34, 0x2e, 0x35, 0x2e, 0x30, 0x2e, 0x33, 0x20, 0x27, 0x64, 0x61, 0x6e, 0x20, 0x45, 0x69, 0x64, 0x65, 0x72, 0x27, 0x77, 0x75}; STUNv2Message stunMessage = STUNv2Message.ParseSTUNMessage(stunResp, stunResp.Length); STUNv2Header stunHeader = stunMessage.Header; Console.WriteLine("Request type = " + stunHeader.MessageType + "."); Console.WriteLine("Length = " + stunHeader.MessageLength + "."); Console.WriteLine("Transaction ID = " + BitConverter.ToString(stunHeader.TransactionId) + "."); foreach (STUNv2Attribute attribute in stunMessage.Attributes) { if (attribute.AttributeType == STUNv2AttributeTypesEnum.MappedAddress) { STUNv2AddressAttribute addressAttribute = new STUNv2AddressAttribute(attribute.Value); Console.WriteLine(" " + attribute.AttributeType + " " + addressAttribute.Address + ":" + addressAttribute.Port + "."); Assert.AreEqual("59.167.172.177", addressAttribute.Address.ToString()); Assert.AreEqual(49026, addressAttribute.Port); } else if (attribute.AttributeType == STUNv2AttributeTypesEnum.XORMappedAddress) { STUNv2XORAddressAttribute xorAddressAttribute = new STUNv2XORAddressAttribute(STUNv2AttributeTypesEnum.XORMappedAddress, attribute.Value); Console.WriteLine(" " + attribute.AttributeType + " " + xorAddressAttribute.Address + ":" + xorAddressAttribute.Port + "."); Assert.AreEqual("59.167.172.177", xorAddressAttribute.Address.ToString()); Assert.AreEqual(49026, xorAddressAttribute.Port); } else { Console.WriteLine(" " + attribute.AttributeType + " " + attribute.Value + "."); } } Assert.AreEqual(STUNv2MessageTypesEnum.BindingSuccessResponse, stunHeader.MessageType); }
public static List<STUNv2Attribute> ParseMessageAttributes(byte[] buffer, int startIndex, int endIndex) { if (buffer != null && buffer.Length > startIndex && buffer.Length >= endIndex) { List<STUNv2Attribute> attributes = new List<STUNv2Attribute>(); int startAttIndex = startIndex; while (startAttIndex < endIndex) { UInt16 stunAttributeType = BitConverter.ToUInt16(buffer, startAttIndex); UInt16 stunAttributeLength = BitConverter.ToUInt16(buffer, startAttIndex + 2); byte[] stunAttributeValue = null; if (BitConverter.IsLittleEndian) { stunAttributeType = Utility.ReverseEndian(stunAttributeType); stunAttributeLength = Utility.ReverseEndian(stunAttributeLength); } if (stunAttributeLength > 0) { stunAttributeValue = new byte[stunAttributeLength]; Buffer.BlockCopy(buffer, startIndex + 4, stunAttributeValue, 0, stunAttributeLength); } STUNv2AttributeTypesEnum attributeType = STUNv2AttributeTypes.GetSTUNAttributeTypeForId(stunAttributeType); STUNv2Attribute attribute = null; if (attributeType == STUNv2AttributeTypesEnum.ChangeRequest) { attribute = new STUNv2ChangeRequestAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.MappedAddress) { attribute = new STUNv2AddressAttribute(stunAttributeValue); } else { attribute = new STUNv2Attribute(attributeType, stunAttributeLength, stunAttributeValue); } attributes.Add(attribute); startAttIndex = startAttIndex + 4 + stunAttributeLength; } return attributes; } else { return null; } }
public static List <STUNv2Attribute> ParseMessageAttributes(byte[] buffer, int startIndex, int endIndex) { if (buffer != null && buffer.Length > startIndex && buffer.Length >= endIndex) { List <STUNv2Attribute> attributes = new List <STUNv2Attribute>(); int startAttIndex = startIndex; while (startAttIndex < endIndex) { UInt16 stunAttributeType = BitConverter.ToUInt16(buffer, startAttIndex); UInt16 stunAttributeLength = BitConverter.ToUInt16(buffer, startAttIndex + 2); byte[] stunAttributeValue = null; if (BitConverter.IsLittleEndian) { stunAttributeType = Utility.ReverseEndian(stunAttributeType); stunAttributeLength = Utility.ReverseEndian(stunAttributeLength); } if (stunAttributeLength > 0) { if (stunAttributeLength + startIndex + 4 > endIndex) { logger.LogWarning("The attribute length on a STUN parameter was greater than the available number of bytes."); } else { stunAttributeValue = new byte[stunAttributeLength]; Buffer.BlockCopy(buffer, startAttIndex + 4, stunAttributeValue, 0, stunAttributeLength); } } STUNv2AttributeTypesEnum attributeType = STUNv2AttributeTypes.GetSTUNAttributeTypeForId(stunAttributeType); STUNv2Attribute attribute = null; if (attributeType == STUNv2AttributeTypesEnum.ChangeRequest) { attribute = new STUNv2ChangeRequestAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.MappedAddress) { attribute = new STUNv2AddressAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.ErrorCode) { attribute = new STUNv2ErrorCodeAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.XORMappedAddress || attributeType == STUNv2AttributeTypesEnum.XORPeerAddress || attributeType == STUNv2AttributeTypesEnum.XORRelayedAddress) { attribute = new STUNv2XORAddressAttribute(attributeType, stunAttributeValue); } else { attribute = new STUNv2Attribute(attributeType, stunAttributeValue); } attributes.Add(attribute); // Attributes start on 32 bit word boundaries so where an attribute length is not a multiple of 4 it gets padded. int padding = (stunAttributeLength % 4 != 0) ? 4 - (stunAttributeLength % 4) : 0; startAttIndex = startAttIndex + 4 + stunAttributeLength + padding; } return(attributes); } else { return(null); } }
public static List<STUNv2Attribute> ParseMessageAttributes(byte[] buffer, int startIndex, int endIndex) { if (buffer != null && buffer.Length > startIndex && buffer.Length >= endIndex) { List<STUNv2Attribute> attributes = new List<STUNv2Attribute>(); int startAttIndex = startIndex; while (startAttIndex < endIndex) { UInt16 stunAttributeType = BitConverter.ToUInt16(buffer, startAttIndex); UInt16 stunAttributeLength = BitConverter.ToUInt16(buffer, startAttIndex + 2); byte[] stunAttributeValue = null; if (BitConverter.IsLittleEndian) { stunAttributeType = Utility.ReverseEndian(stunAttributeType); stunAttributeLength = Utility.ReverseEndian(stunAttributeLength); } if (stunAttributeLength > 0) { if (stunAttributeLength + startIndex + 4 > endIndex) { logger.Warn("The attribute length on a STUN parameter was greater than the available number of bytes."); } else { stunAttributeValue = new byte[stunAttributeLength]; Buffer.BlockCopy(buffer, startAttIndex + 4, stunAttributeValue, 0, stunAttributeLength); } } STUNv2AttributeTypesEnum attributeType = STUNv2AttributeTypes.GetSTUNAttributeTypeForId(stunAttributeType); STUNv2Attribute attribute = null; if (attributeType == STUNv2AttributeTypesEnum.ChangeRequest) { attribute = new STUNv2ChangeRequestAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.MappedAddress) { attribute = new STUNv2AddressAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.ErrorCode) { attribute = new STUNv2ErrorCodeAttribute(stunAttributeValue); } else if (attributeType == STUNv2AttributeTypesEnum.XORMappedAddress || attributeType == STUNv2AttributeTypesEnum.XORPeerAddress || attributeType == STUNv2AttributeTypesEnum.XORRelayedAddress) { attribute = new STUNv2XORAddressAttribute(attributeType, stunAttributeValue); } else { attribute = new STUNv2Attribute(attributeType, stunAttributeValue); } attributes.Add(attribute); // Attributes start on 32 bit word boundaries so where an attribute length is not a multiple of 4 it gets padded. int padding = (stunAttributeLength % 4 != 0) ? 4 - (stunAttributeLength % 4) : 0; startAttIndex = startAttIndex + 4 + stunAttributeLength + padding; } return attributes; } else { return null; } }