ChangeRequestAttribute.cs 670 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace STUN.Message.Attributes
  5. {
  6. /// <summary>
  7. /// https://tools.ietf.org/html/rfc5780#section-7.2
  8. /// </summary>
  9. public class ChangeRequestAttribute : IAttribute
  10. {
  11. public IEnumerable<byte> Bytes => new byte[] { 0, 0, 0, (byte)(Convert.ToInt32(ChangeIp) << 2 | Convert.ToInt32(ChangePort) << 1) };
  12. public bool ChangeIp { get; set; }
  13. public bool ChangePort { get; set; }
  14. public bool TryParse(byte[] bytes)
  15. {
  16. if (bytes.Length != 4)
  17. {
  18. return false;
  19. }
  20. var bits = new BitArray(bytes);
  21. ChangeIp = bits[29];
  22. ChangePort = bits[30];
  23. return true;
  24. }
  25. }
  26. }