Example #1
0
        public virtual bool SendCustom(ICustomMessage message, DeliveryType deliveryType, int channel)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            ChannelDescriptor.CheckChannelValue(channel);

            using (var rawMessage = this.Parent.CreateMessage())
            {
                rawMessage.DeliveryType = deliveryType;
                rawMessage.Channel      = channel;

                using (WardenStreamWriter sw = new WardenStreamWriter(rawMessage.BaseStream, true))
                {
                    message.WriteTo(new WriteFormatterInfo(sw, this.configuration.Serializer));
                }

                UdpSendStatus result = SendRawMessage(new Warden.Networking.Udp.Messages.MessageInfo(rawMessage,
                                                                                                     deliveryType, channel));

                return(result != UdpSendStatus.Failed);
            }
        }
Example #2
0
        public bool SendReliable(ICustomMessage message)
        {
            if (!this.Connected)
            {
                return(false);
            }

            TcpRawMessage rawMessage = Parent.CreateMessage();

            using (WardenStreamWriter sw = new WardenStreamWriter(rawMessage.BaseStream, true))
            {
                message.WriteTo(new WriteFormatterInfo(sw, configuration.Serializer));
            }

            if (rawMessage.Length >= configuration.CompressionThreshold)
            {
                logger.Trace($"Message {message} size exceeds compression threshold {configuration.CompressionThreshold}, compressing it");
                using (rawMessage)
                {
                    var compressedMessage = rawMessage.Compress(CompressionLevel.Optimal);
                    logger.Trace($"Compressing {rawMessage} to {compressedMessage}");
                    compressedMessage.Position = 0;
                    SendRawMessage(compressedMessage);
                }
            }
            else
            {
                SendRawMessage(rawMessage);
            }

            return(true);
        }
        public void SendHandshakeRequest(Stream handshakeRequest)
        {
            if (status != DhStatus.None)
            {
                throw new InvalidOperationException($"Wrong status {status}, expected: {DhStatus.None}");
            }

            byte[] privateKeyBytes = new byte[64];
            byte[] publicKeyBytes  = new byte[64];
            rngCsp.GetBytes(privateKeyBytes);
            rngCsp.GetBytes(publicKeyBytes);
            privateKey = new BigInteger(privateKeyBytes);
            publicKey  = new BigInteger(publicKeyBytes);

            BigInteger clientMod = ((privateKey * publicKey) % mod);

            using (WardenStreamWriter writer = new WardenStreamWriter(handshakeRequest, true))
            {
                writer.Write(mark);
                writer.Write(publicKeyBytes.Length);
                writer.Write(publicKeyBytes);
                byte[] clientModBytes = clientMod.ToByteArray();
                writer.Write(clientModBytes.Length);
                writer.Write(clientModBytes);
            }

            status = DhStatus.WaitingForServerMod;
        }
Example #4
0
        void OnMtuExpand(Datagram datagram)
        {
            if (!CheckStatus(datagram, UdpConnectionStatus.Connected))
            {
                return;
            }

            int  size = datagram.GetTotalSize();
            byte fix  = 0;

            if (size > peer.Configuration.LimitMtu)
            {
                size = peer.Configuration.LimitMtu;
                fix  = 1;
            }

            logger.Debug($"MTU Successfully expanded to {size} by request from other side");
            var mtuDatagram = CreateSpecialDatagram(MessageType.ExpandMTUSuccess, 5);

            using (WardenStreamWriter writer = new WardenStreamWriter(mtuDatagram.BaseStream, true))
            {
                writer.WriteVarInt(size);
                writer.Write(fix);
            }
            if (size > this.Mtu)
            {
                this.Mtu = size;
            }
            _ = SendDatagramAsync(mtuDatagram);

            datagram.Dispose();
        }
        public void RecvHandshakeRequest(Stream handshakeRequest, Stream handshakeResponse)
        {
            if (status != DhStatus.None)
            {
                throw new InvalidOperationException($"Wrong status {status}, expected: {DhStatus.None}");
            }

            BigInteger clientMod;

            using (WardenStreamReader reader = new WardenStreamReader(handshakeRequest, true))
            {
                ushort gotMark = reader.ReadUInt16();
                if (mark != gotMark)
                {
                    throw new InvalidOperationException("Handshake failed, wrong mark. Perhaps the other peer is trying to connect with unsecure connection");
                }
                int publicKeySize = reader.ReadInt32();
                publicKey = new BigInteger(reader.ReadBytes(publicKeySize));
                int clientModSize = reader.ReadInt32();
                clientMod = new BigInteger(reader.ReadBytes(clientModSize));
            }

            byte[] keyBytes = new byte[64];
            rngCsp.GetBytes(keyBytes);
            privateKey = new BigInteger(keyBytes);
            BigInteger serverMod = (privateKey * publicKey) % mod;

            commonKey = ((privateKey * clientMod) % mod).ToByteArray();

            using (WardenStreamWriter writer = new WardenStreamWriter(handshakeResponse, true))
            {
                byte[] serverModBytes = serverMod.ToByteArray();
                writer.Write(serverModBytes.Length);
                writer.Write(serverModBytes);
            }

            SetCipherKey();
        }