Exemple #1
0
 public STUNAttr(STUNAttribute type, ByteBuffer data, ByteBuffer stunMessage)
 {
     this.type        = type;
     this.data        = data;
     this.stunMessage = stunMessage;
     isValid          = true;
 }
Exemple #2
0
        public void ReadAttribute(out byte[] rcvData)
        {
            while (serializer.bytePos < serializer.byteLength && serializer.bytePos < methodLength)
            {
                STUNAddress   address;
                STUNAttribute attrType = (STUNAttribute)serializer.ReadUShort();
                attributeTypes.Add(attrType);
                switch (attrType)
                {
                case STUNAttribute.MappedAddress:
                case STUNAttribute.SourceAddress:
                case STUNAttribute.ChangedAddress:
                    address = ReadMappedAddress();
                    response.Add(attrType, address);
                    break;

                case STUNAttribute.XorPeerAddress:
                case STUNAttribute.XorMappedAddress:
                case STUNAttribute.XorRelayedAddress:
                    address = ReadXorMappedAddress();
                    response.Add(attrType, address);
                    break;

                case STUNAttribute.ErrorCode:
                    response.Add(attrType, ReadErrorCode());
                    break;

                case STUNAttribute.UnknownAttribute:
                    response.Add(attrType, ReadUnknownAttributes());
                    break;

                case STUNAttribute.ServerName:
                    response.Add(attrType, ReadString());
                    break;

                case STUNAttribute.Realm:
                    response.Add(attrType, ReadString());
                    break;

                case STUNAttribute.Username:
                    response.Add(attrType, ReadString());
                    break;

                default:
                    ushort attrLen = serializer.ReadUShort();
                    byte[] bytes   = serializer.ReadBytes(attrLen);
                    response.Add(attrType, bytes);
                    while (((attrLen++) % 4) != 0)
                    {
                        serializer.ReadByte();
                    }
                    break;
                }
            }
            rcvData = (byte[])Get(STUNAttribute.Data);
        }
Exemple #3
0
        public string GetString(STUNAttribute attr)
        {
            object obj = Get(attr);

            if (obj == null)
            {
                return("");
            }
            return(obj.ToString());
        }
Exemple #4
0
        public object Get(STUNAttribute attr)
        {
            if (!response.ContainsKey(attr))
            {
                return(null);
            }
            object value = response[attr];

            return(value);
        }
Exemple #5
0
 public static int AttributeLastIndexOf(List <STUNAttr> attrs, STUNAttribute type, int startIndex = 0)
 {
     for (int i = attrs.Count - 1 - startIndex; i >= 0; i--)
     {
         if (type == attrs[i].type)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #6
0
 public static int AttributeIndexOf(List <STUNAttr> attrs, STUNAttribute type, int startIndex = 0)
 {
     for (int i = startIndex; i < attrs.Count; i++)
     {
         if (type == attrs[i].type)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #7
0
 private static int AttributeIndexOf(IList <STUNAttr> attributes, STUNAttribute type)
 {
     for (int i = 0; i < attributes.Count; i++)
     {
         if (type == attributes[i].type)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #8
0
        public void WriteEmpty(STUNAttribute attr)
        {
            serializer.SetBufferLength(0);

            serializer.Write((ushort)attr);
            serializer.Write((ushort)0);

            response.Add(attr, "");
            attributeTypes.Add(attr);
            attributeBytes.Add(serializer.ToArray());
        }
Exemple #9
0
        public void WriteUInt(STUNAttribute attr, uint value)
        {
            serializer.SetBufferLength(0);

            serializer.Write((ushort)attr);
            serializer.Write((ushort)4);
            serializer.Write(value);

            response.Add(attr, value);
            attributeTypes.Add(attr);
            attributeBytes.Add(serializer.ToArray());
        }
Exemple #10
0
        public void WriteBytes(STUNAttribute attr, byte[] bytes)
        {
            serializer.SetBufferLength(0);
            serializer.Write((ushort)attr);
            serializer.Write((ushort)bytes.Length);
            serializer.Write(bytes);

            //pad to multiple of 4
            PadTo32Bits(bytes.Length, serializer);

            Console.WriteLine("Attribute: " + Enum.GetName(typeof(STUNAttribute), attr) + " = " + NetworkSerializer.ByteArrayToHexString((byte[])bytes));
            response.Add(attr, bytes);
            attributeTypes.Add(attr);
            attributeBytes.Add(serializer.ToArray());
        }
Exemple #11
0
        public void WriteBytes(STUNAttribute attr, byte[] bytes)
        {
            serializer.SetBufferLength(0);
            serializer.Write((ushort)attr);
            serializer.Write((ushort)bytes.Length);
            serializer.Write(bytes);

            //pad to multiple of 4
            PadTo32Bits(bytes.Length, serializer);


            response.Add(attr, bytes);
            attributeTypes.Add(attr);
            attributeBytes.Add(serializer.ToArray());
        }
Exemple #12
0
        public void WriteString(STUNAttribute attr, string text)
        {
            serializer.SetBufferLength(0);

            int len = Encoding.UTF8.GetByteCount(text);

            serializer.Write((ushort)attr);
            serializer.Write(text, len);

            //pad to multiple of 4
            PadTo32Bits(len, serializer);

            response.Add(attr, text);
            attributeTypes.Add(attr);
            attributeBytes.Add(serializer.ToArray());
        }
Exemple #13
0
        public void LogAttribute(int index)
        {
            STUNAttribute attr     = attributeTypes[index];
            string        valueStr = "";

            if (response.ContainsKey(attr))
            {
                object value = response[attr];

                if (value is string v)
                {
                    valueStr = v;
                }
                else if (value is byte[] b)
                {
                    valueStr = NetworkSerializer.ByteArrayToHexString(b);
                }
                else
                {
                    valueStr = value.ToString();
                }
            }
            Console.WriteLine("Write Attribute: " + Enum.GetName(typeof(STUNAttribute), attr) + " = " + valueStr);
        }
Exemple #14
0
        public override void ReadResponse(NetworkPacket packet)
        {
            methodId      = (ushort)((uint)packet.ReadUShort() & 0x3FFF); //0x3F (0011 1111) sets left most 2 bits to 00
            methodLength  = packet.ReadUShort();
            transactionID = packet.ReadBytes(16);
            header.ackkey = BitConverter.ToUInt32(transactionID, 0);

            method = (STUNMethod)methodId;

            //Console.WriteLine("STUN Response address: " + packet.remoteEndPoint.ToString());
            //Console.WriteLine("STUN Response Method: " + Enum.GetName(typeof(STUNMethod), method));
            //Console.WriteLine("STUN Response Length: " + methodLength);

            while (packet.bytePos < packet.byteLength)
            {
                STUNAddress   address;
                STUNAttribute attrType = (STUNAttribute)packet.ReadUShort();
                attributeTypes.Add(attrType);
                //Console.WriteLine("STUN Attr Type: " + Enum.GetName(typeof(STUNAttribute), attrType));
                switch (attrType)
                {
                case STUNAttribute.MappedAddress:
                case STUNAttribute.SourceAddress:
                case STUNAttribute.ChangedAddress:
                    address = ReadMappedAddress(packet);
                    response.Add(attrType, address);
                    break;

                case STUNAttribute.XorMappedAddress:
                case STUNAttribute.XorRelayedAddress:
                    address = ReadXorMappedAddress(packet);
                    response.Add(attrType, address);
                    break;

                case STUNAttribute.ErrorCode:
                    response.Add(attrType, ReadErrorCode(packet));
                    break;

                case STUNAttribute.UnknownAttribute:
                    response.Add(attrType, ReadUnknownAttributes(packet));
                    break;

                case STUNAttribute.ServerName:
                    response.Add(attrType, ReadString(packet));
                    break;

                case STUNAttribute.Realm:
                    response.Add(attrType, ReadString(packet));
                    break;

                case STUNAttribute.Username:
                    response.Add(attrType, ReadString(packet));
                    break;

                default:
                    ushort attrLen = packet.ReadUShort();
                    byte[] bytes   = packet.ReadBytes(attrLen);
                    response.Add(attrType, bytes);
                    while (((attrLen++) % 4) != 0)
                    {
                        packet.ReadByte();
                    }
                    break;
                }
            }
        }