Exemple #1
0
        public void RequestAllocate()
        {
            if (++turnAllocateCount > 2)
            {
                return;
            }

            MessageSTUN message = CreateMessageSTUN(STUNMethod.AllocateRequest);

            //message.WriteString(STUNAttribute.ServerName, "Coturn-4.5.1.1 'dan Eider'");

            message.WriteString(STUNAttribute.Username, message.username);
            message.WriteString(STUNAttribute.Realm, message.realm);
            //message.WriteString(STUNAttribute.Password, message.password);

            message.WriteUInt(STUNAttribute.Lifetime, lifeTime);
            message.WriteEmpty(STUNAttribute.DontFragment);
            message.WriteUInt(STUNAttribute.RequestedTransport, (uint)(17 << 24));//(uint)(17 << 24)

            if (nonce != null)
            {
                message.WriteBytes(STUNAttribute.Nonce, nonce);
                message.WriteRequestIntegrity();
            }

            byte[] pkg = message.WriteRequest();
            sender.Send(pkg);
        }
Exemple #2
0
        public void RequestChannelBind(STUNAddress stunAddress)
        {
            lock (_lock)
            {
                MessageSTUN message = CreateMessageSTUN(STUNMethod.ChannelBindRequest);
                // channel number [0x4000 through 0x4FFF](uint)random.Next(0x4001, 0x4FFF);
                uint channelNumber;
                if (!ip2ChannelNumber.ContainsKey(stunAddress.ToString()))
                {
                    GenerateChannelNumberForPeer(stunAddress);
                }
                channelNumber = ip2ChannelNumber[stunAddress.ToString()];
                Console.WriteLine("RequestChannelBind: {0} {1}:{2}", channelNumber, stunAddress.ToString(), stunAddress.port);
                channelNumber = (uint)(channelNumber << 16);
                message.WriteString(STUNAttribute.Username, message.username);
                message.WriteString(STUNAttribute.Realm, message.realm);

                message.WriteUInt(STUNAttribute.ChannelNumber, channelNumber);

                byte[] ret;
                if (stunAddress.IsIPv4)
                {
                    ret    = new byte[8];
                    ret[1] = (byte)(0x01); // family
                }
                else
                {
                    ret    = new byte[16];
                    ret[1] = (byte)(0x02); // family
                }
                ret[0] = (byte)(0x0);

                ret[3]  = (byte)(stunAddress.port & 0xff);
                ret[2]  = (byte)((stunAddress.port >> 8) & 0xff);
                ret[2] ^= transactionID[0];
                ret[3] ^= transactionID[1];

                for (int i = 0; i < stunAddress.address.Length; i++)
                {
                    ret[4 + i] = (byte)(stunAddress.address[i] ^ transactionID[i]);
                }

                message.WriteBytes(STUNAttribute.XorPeerAddress, ret);
                if (nonce != null)
                {
                    message.WriteBytes(STUNAttribute.Nonce, nonce);
                    message.WriteRequestIntegrity();
                }
                byte[] pkg = message.WriteRequest();
                sender.Send(pkg);
            }
        }
Exemple #3
0
        public void RequestRefreshAllocationeLifeTime(uint lifetime)
        {
            MessageSTUN message = CreateMessageSTUN(STUNMethod.RefreshRequest);

            message.WriteString(STUNAttribute.Username, message.username);
            message.WriteString(STUNAttribute.Realm, message.realm);
            message.WriteUInt(STUNAttribute.Lifetime, lifetime);
            //message.WriteUInt(STUNAttribute.RequestedTransport, (uint)(17 << 24));

            if (nonce != null)
            {
                message.WriteBytes(STUNAttribute.Nonce, nonce);
                message.WriteRequestIntegrity();
            }

            byte[] pkg = message.WriteRequest();
            sender.Send(pkg);
        }
Exemple #4
0
        public void RequestCreatePermission(STUNAddress stunAddress)
        {
            lock (_lock)
            {
                MessageSTUN message = CreateMessageSTUN(STUNMethod.CreatePermissionRequest);
                message.WriteString(STUNAttribute.Username, message.username);
                message.WriteString(STUNAttribute.Realm, message.realm);

                byte[] ret;
                if (stunAddress.IsIPv4)
                {
                    ret    = new byte[8];
                    ret[1] = (byte)(0x01); // family
                }
                else
                {
                    ret    = new byte[16];
                    ret[1] = (byte)(0x02); // family
                }
                ret[0] = (byte)(0x0);
                ushort xorFlag16 = (ushort)(magicCookie >> 16);

                ret[3]  = 0;
                ret[2]  = 0;
                ret[2] ^= transactionID[0];
                ret[3] ^= transactionID[1];

                for (int i = 0; i < stunAddress.address.Length; i++)
                {
                    ret[4 + i] = (byte)(stunAddress.address[i] ^ transactionID[i]);
                }

                message.WriteBytes(STUNAttribute.XorPeerAddress, ret);
                if (nonce != null)
                {
                    message.WriteBytes(STUNAttribute.Nonce, nonce);
                    message.WriteRequestIntegrity();
                }
                byte[] pkg = message.WriteRequest();
                sender.Send(pkg);
            }
        }