示例#1
0
        public static byte[] AssembleMessagePacket(PacketEnvelope envelope, bool encrypt, string encryptPacketKey, string keySalt)
        {
            try
            {
                byte[] payloadBody = Zip(Serialization.ObjectToByteArray(envelope));

                if (encrypt)
                {
                    payloadBody = Encrypt(encryptPacketKey, keySalt, payloadBody);
                }

                int grossPacketSize = payloadBody.Length + Constants.PayloadHeaderSize;

                byte[] packetBytes = new byte[grossPacketSize];

                UInt16 payloadCrc = Crc16.ComputeChecksum(payloadBody);

                Buffer.BlockCopy(BitConverter.GetBytes(Constants.PayloadDelimiter), 0, packetBytes, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(grossPacketSize), 0, packetBytes, 4, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(payloadCrc), 0, packetBytes, 8, 2);
                Buffer.BlockCopy(payloadBody, 0, packetBytes, Constants.PayloadHeaderSize, payloadBody.Length);

                return(packetBytes);
            }
            catch (Exception ex)
            {
                //TODO: allow this to be logged.
            }

            return(null);
        }
示例#2
0
        // the other end just sent us a message
        // and it told us the latest message it got
        // and the ack mask
        private void AckPackets(ushort receiveSequence, ulong ackMask)
        {
            while (sendWindow.Count > 0)
            {
                PacketEnvelope envelope = sendWindow.Peek();

                int distance = (int)sequencer.Distance(envelope.Sequence, receiveSequence);

                if (distance > 0)
                {
                    break;
                }

                sendWindow.Dequeue();

                // if any of these cases trigger, packet is most likely lost
                if ((distance <= -ACK_MASK_BITS) || ((ackMask & (1UL << -distance)) == 0UL))
                {
                    NotifyLost?.Invoke(this, envelope.Token);
                }
                else
                {
                    NotifyDelivered?.Invoke(this, envelope.Token);
                }
            }
        }
示例#3
0
        public static byte[] AssembleMessagePacket(byte[] payload, int payloadLength, bool encrypt, string encryptPacketKey, string keySalt)
        {
            var envelope = new PacketEnvelope();

            envelope.Label   = null;
            envelope.Payload = payload.Take(payloadLength).ToArray();

            return(AssembleMessagePacket(envelope, encrypt, encryptPacketKey, keySalt));
        }
示例#4
0
        private void ProcessPeerCommand(SocketState connection, PacketEnvelope envelope)
        {
            if (envelope.Label == IntraServiceLables.ApplyNegotiationToken)
            {
                connection.KeyNegotiator = new SecureKeyExchange.SecureKeyNegotiator();
                byte[] replyToken = connection.KeyNegotiator.ApplyNegotiationToken(envelope.Payload);

                SendPacketEnvelope(connection, new PacketEnvelope
                {
                    Label   = IntraServiceLables.ApplyResponseNegotiationToken,
                    Payload = replyToken
                });

                SendPacketEnvelope(connection, new PacketEnvelope
                {
                    Label = IntraServiceLables.EncryptionNegotationComplete
                });
            }
            else if (envelope.Label == IntraServiceLables.ApplyResponseNegotiationToken)
            {
                connection.KeyNegotiator.ApplyNegotiationResponseToken(envelope.Payload);

                SendPacketEnvelope(connection, new PacketEnvelope
                {
                    Label = IntraServiceLables.EncryptionNegotationComplete
                });
            }
            else if (envelope.Label == IntraServiceLables.EncryptionNegotationComplete)
            {
                connection.IsEncryptionNegotationComplete = true;
                //Console.WriteLine("--{0} Shared Secret: {1}", connection.Route.Name, connection.KeyNegotiator.SharedSecretString);

                string sharedSecretString = connection.IsEncryptionNegotationComplete ? connection.KeyNegotiator.SharedSecretString : null;

                string commonSalt = null;
                if (connection.IsIncomming && connection.IsEncryptionNegotationComplete)
                {
                    commonSalt = _route.BindingPreSharedKey;
                }
                else if (connection.IsOutgoing && connection.IsEncryptionNegotationComplete)
                {
                    commonSalt = _route.EndpointPreSharedKey;
                }

                SendPacketEnvelope(connection, new PacketEnvelope
                {
                    Label = IntraServiceLables.TunnelNegotationComplete,
                }, sharedSecretString, commonSalt);
            }
            else if (envelope.Label == IntraServiceLables.TunnelNegotationComplete)
            {
                connection.SetTunnelNegotationComplete();
                //Console.WriteLine("--{0} TunnelNegotationComplete", connection.Route.Name);
                //Console.WriteLine("--{0} Shared Secret: {1}", connection.Route.Name, connection.KeyNegotiator.SharedSecretString);
            }
        }
示例#5
0
 private void ProcessPeerCommand(SocketState connection, PacketEnvelope envelope)
 {
     /*
      * if (envelope.Label == IntraServiceLables.ApplyResponseNegotiationToken)
      * {
      *  connection.KeyNegotiator.ApplyNegotiationResponseToken(envelope.Payload);
      *
      *  SendPacketEnvelope(connection, new PacketEnvelope
      *  {
      *      Label = IntraServiceLables.EncryptionNegotationComplete
      *  });
      * }
      */
 }
示例#6
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, connection.UseCompression, false, null, null);
     Stats.BytesSent += (UInt64)sendBuffer.Length;
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }
示例#7
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope, string encryptionKey, string salt)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, connection.UseCompression, true, encryptionKey, salt);
     Stats.BytesSent += (UInt64)sendBuffer.Length;
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }
示例#8
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, false, null, null);
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }
示例#9
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope, string encryptionKey, string salt)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, true, encryptionKey, salt);
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }