فهرست منبع

Add RESPONSE-ADDRESS attribute

Bruce Wayne 5 سال پیش
والد
کامیت
9c0d33adda

+ 65 - 0
STUN/Message/Attributes/AddressAttribute.cs

@@ -0,0 +1,65 @@
+using STUN.Message.Enums;
+using STUN.Utils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+
+namespace STUN.Message.Attributes
+{
+    /// <summary>
+    /// https://tools.ietf.org/html/rfc5389#section-15.1
+    /// </summary>
+    public abstract class AddressAttribute : IAttribute
+    {
+        public virtual IEnumerable<byte> Bytes
+        {
+            get
+            {
+                if (Address == null)
+                {
+                    return Array.Empty<byte>();
+                }
+                var res = new List<byte> { 0, (byte)Family };
+                res.AddRange(Port.ToBe());
+                res.AddRange(Address.GetAddressBytes());
+                return res;
+            }
+        }
+
+        public IpFamily Family { get; set; }
+
+        public ushort Port { get; set; }
+
+        public IPAddress Address { get; set; }
+
+        public virtual bool TryParse(byte[] bytes)
+        {
+            var length = 4;
+
+            if (bytes.Length < length) return false;
+
+            Family = (IpFamily)bytes[1];
+
+            switch (Family)
+            {
+                case IpFamily.IPv4:
+                    length += 4;
+                    break;
+                case IpFamily.IPv6:
+                    length += 16;
+                    break;
+                default:
+                    return false;
+            }
+
+            if (bytes.Length == length) return false;
+
+            Port = BitUtils.FromBe(bytes[2], bytes[3]);
+
+            Address = new IPAddress(bytes.Skip(4).ToArray());
+
+            return true;
+        }
+    }
+}

+ 9 - 0
STUN/Message/Attributes/Attribute.cs

@@ -96,6 +96,15 @@ namespace STUN.Message.Attributes
                     }
                     break;
                 }
+                case AttributeType.ResponseAddress:
+                {
+                    var t = new ResponseAddressAttribute();
+                    if (t.TryParse(value))
+                    {
+                        Value = t;
+                    }
+                    break;
+                }
                 //TODO:Parse
                 default:
                     return 0;

+ 2 - 60
STUN/Message/Attributes/MappedAddressAttribute.cs

@@ -1,65 +1,7 @@
-using STUN.Message.Enums;
-using STUN.Utils;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-
-namespace STUN.Message.Attributes
+namespace STUN.Message.Attributes
 {
     /// <summary>
     /// https://tools.ietf.org/html/rfc5389#section-15.1
     /// </summary>
-    public class MappedAddressAttribute : IAttribute
-    {
-        public virtual IEnumerable<byte> Bytes
-        {
-            get
-            {
-                if (Address == null)
-                {
-                    return Array.Empty<byte>();
-                }
-                var res = new List<byte> { 0, (byte)Family };
-                res.AddRange(Port.ToBe());
-                res.AddRange(Address.GetAddressBytes());
-                return res;
-            }
-        }
-
-        public IpFamily Family { get; set; }
-
-        public ushort Port { get; set; }
-
-        public IPAddress Address { get; set; }
-
-        public virtual bool TryParse(byte[] bytes)
-        {
-            var length = 4;
-
-            if (bytes.Length < length) return false;
-
-            Family = (IpFamily)bytes[1];
-
-            switch (Family)
-            {
-                case IpFamily.IPv4:
-                    length += 4;
-                    break;
-                case IpFamily.IPv6:
-                    length += 16;
-                    break;
-                default:
-                    return false;
-            }
-
-            if (bytes.Length == length) return false;
-
-            Port = BitUtils.FromBe(bytes[2], bytes[3]);
-
-            Address = new IPAddress(bytes.Skip(4).ToArray());
-
-            return true;
-        }
-    }
+    public class MappedAddressAttribute : AddressAttribute { }
 }

+ 7 - 0
STUN/Message/Attributes/ResponseAddressAttribute.cs

@@ -0,0 +1,7 @@
+namespace STUN.Message.Attributes
+{
+    /// <summary>
+    /// https://tools.ietf.org/html/rfc3489#section-11.2.2
+    /// </summary>
+    public class ResponseAddressAttribute : AddressAttribute { }
+}

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

@@ -9,7 +9,7 @@ namespace STUN.Message.Attributes
     /// <summary>
     /// https://tools.ietf.org/html/rfc5389#section-15.2
     /// </summary>
-    public class XorMappedAddressAttribute : MappedAddressAttribute
+    public class XorMappedAddressAttribute : AddressAttribute
     {
         private readonly byte[] _magicCookie;
         private readonly byte[] _transactionId;