Browse Source

Add CHANGE-REQUEST attribute

Bruce Wayne 5 years ago
parent
commit
0b1df6ed13

+ 1 - 1
STUN/Message/Attributes/AddressAttribute.cs

@@ -53,7 +53,7 @@ namespace STUN.Message.Attributes
                     return false;
             }
 
-            if (bytes.Length == length) return false;
+            if (bytes.Length != length) return false;
 
             Port = BitUtils.FromBe(bytes[2], bytes[3]);
 

+ 38 - 0
STUN/Message/Attributes/ChangeRequestAttribute.cs

@@ -0,0 +1,38 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace STUN.Message.Attributes
+{
+    /// <summary>
+    /// https://tools.ietf.org/html/rfc5780#section-7.2
+    /// </summary>
+    public class ChangeRequestAttribute : IAttribute
+    {
+        public IEnumerable<byte> Bytes
+        {
+            get
+            {
+                var bits = new BitArray(32, false) { [29] = ChangeIp, [30] = ChangePort };
+                var res = new byte[4];
+                bits.CopyTo(res, 0);
+                return res;
+            }
+        }
+
+        public bool ChangeIp { get; set; }
+
+        public bool ChangePort { get; set; }
+
+        public bool TryParse(byte[] bytes)
+        {
+            if (bytes.Length != 4) return false;
+
+            var bits = new BitArray(bytes);
+
+            ChangeIp = bits[29];
+            ChangePort = bits[30];
+
+            return true;
+        }
+    }
+}