UnknownStunAttributeValue.cs 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Microsoft;
  2. using STUN.Enums;
  3. using System.Buffers.Binary;
  4. namespace STUN.Messages.StunAttributeValues;
  5. /// <summary>
  6. /// https://tools.ietf.org/html/rfc5389#section-15.9
  7. /// </summary>
  8. public class UnknownStunAttributeValue : IStunAttributeValue
  9. {
  10. public List<AttributeType> Types { get; } = new();
  11. public int WriteTo(Span<byte> buffer)
  12. {
  13. int size = Types.Count << 1;
  14. Requires.Range(buffer.Length >= size, nameof(buffer));
  15. foreach (AttributeType attributeType in Types)
  16. {
  17. BinaryPrimitives.WriteUInt16BigEndian(buffer, (ushort)attributeType);
  18. buffer = buffer[sizeof(ushort)..];
  19. }
  20. return size;
  21. }
  22. public bool TryParse(ReadOnlySpan<byte> buffer)
  23. {
  24. if (buffer.Length < 2 || (buffer.Length & 1) == 1)
  25. {
  26. return false;
  27. }
  28. Types.Clear();
  29. while (!buffer.IsEmpty)
  30. {
  31. ushort type = BinaryPrimitives.ReadUInt16BigEndian(buffer);
  32. Types.Add((AttributeType)type);
  33. buffer = buffer[sizeof(ushort)..];
  34. }
  35. return true;
  36. }
  37. }