private ushort Xor(ushort port)
        {
            var b     = port.ToBe().ToArray();
            var xPort = BitUtils.FromBe((byte)(_magicCookie[0] ^ b[0]), (byte)(_magicCookie[1] ^ b[1]));

            return(xPort);
        }
Esempio n. 2
0
        public bool TryParse(byte[] bytes)
        {
            if (bytes.Length < 20)
            {
                return(false);                // Check length
            }

            StunMessageType = (StunMessageType)BitUtils.FromBe((byte)(bytes[0] & 0b0011_1111), bytes[1]);

            if (!Enum.IsDefined(typeof(StunMessageType), StunMessageType))
            {
                return(false);
            }

            var length = BitUtils.FromBe(bytes[2], bytes[3]);

            MagicCookie = BitUtils.FromBeToInt(bytes.Skip(4).Take(4));

            TransactionId = bytes.Skip(8).Take(12).ToArray();

            if (bytes.Length != length + 20)
            {
                return(false);                // Check length
            }

            var list = new List <Attribute>();

            var b = bytes.Skip(20).ToArray();

            while (b.Length > 0)
            {
                var attribute = new Attribute(MagicCookieBytes.ToArray(), TransactionId);
                var offset    = attribute.TryParse(b);
                if (offset > 0)
                {
                    list.Add(attribute);
                    b = b.Skip(offset).ToArray();
                }
                else
                {
                    Debug.WriteLine($@"[Warning] Ignore wrong attribute: {BitConverter.ToString(b)}");
                    break;
                }
            }

            Attributes = list;

            return(true);
        }
Esempio n. 3
0
        public bool TryParse(byte[] bytes)
        {
            if (bytes.Length < 2 || (bytes.Length & 1) == 1)
            {
                return(false);
            }

            var list = new List <AttributeType>();

            for (var i = 0; i < bytes.Length >> 1; ++i)
            {
                var b = bytes.Skip(i << 1).Take(2);
                list.Add((AttributeType)BitUtils.FromBe(b));
            }
            Types = list;

            return(true);
        }
Esempio n. 4
0
        /// <returns>
        /// Parse 成功字节,0 则表示 Parse 失败
        /// </returns>
        public int TryParse(byte[] bytes)
        {
            if (bytes.Length < 4)
            {
                return(0);
            }

            Type = (AttributeType)BitUtils.FromBe(bytes[0], bytes[1]);

            Length = BitUtils.FromBe(bytes[2], bytes[3]);

            if (bytes.Length < 4 + Length)
            {
                return(0);
            }

            var value = bytes.Skip(4).Take(Length).ToArray();

            IAttribute t = Type switch
            {
                AttributeType.MappedAddress => new MappedAddressAttribute(),
                AttributeType.XorMappedAddress => new XorMappedAddressAttribute(_magicCookie, _transactionId),
                AttributeType.ResponseAddress => new ResponseAddressAttribute(),
                AttributeType.ChangeRequest => new ChangeRequestAttribute(),
                AttributeType.SourceAddress => new SourceAddressAttribute(),
                AttributeType.ChangedAddress => new ChangedAddressAttribute(),
                AttributeType.OtherAddress => new OtherAddressAttribute(),
                AttributeType.ReflectedFrom => new ReflectedFromAttribute(),
                AttributeType.ErrorCode => new ErrorCodeAttribute(),
                _ => new UselessAttribute()
            };

            if (t.TryParse(value))
            {
                Value = t;
            }

            return(4 + Length + (4 - Length % 4) % 4);            // 对齐
        }
    }
Esempio n. 5
0
        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);
        }