Exemple #1
0
        public static void MessageTransmissionHandler(Message message)
        {
            byte[] messageBytes = ChatTwo_Protocol.MessageTransmissionHandler(message);

            string sharedSecret;

            if (message.Type == ChatTwo_Protocol.MessageType.CreateUser)
            {
                sharedSecret = ChatTwo_Protocol.DefaultSharedSecret;
            }
            else if (message.Type == ChatTwo_Protocol.MessageType.Login)
            {
                ServerSharedSecret = ByteHelper.GetHashString(messageBytes);
                sharedSecret       = ServerSharedSecret;
            }
            else if (message.To == ChatTwo_Protocol.ServerReserrvedUserID)
            {
                sharedSecret = ServerSharedSecret;
            }
            else
            {
                int userId = message.To;
                sharedSecret = _contacts.Find(x => x.ID == userId).Secret;

                // Testing!!!! REMOVE THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                sharedSecret = ChatTwo_Protocol.DefaultSharedSecret;
            }

            messageBytes = ChatTwo_Protocol.AddSignatureAndMac(messageBytes, sharedSecret);

            // Fire an OnMessageTransmission event.
            PacketTransmissionEventArgs args = new PacketTransmissionEventArgs();

            args.Destination   = message.Ip;
            args.PacketContent = messageBytes;
            OnMessageTransmission(args);
        }
Exemple #2
0
 private static string CreateMac(byte[] messageBytes, string sharedSecret)
 {
     return(ByteHelper.GetHashString(ByteHelper.ConcatinateArray(ByteHelper.GetHashBytes(messageBytes), Convert.FromBase64String(sharedSecret))));
 }
Exemple #3
0
        /// <summary>
        /// This is a threaded method that keeps looping while _online is true.
        /// It will receive UDP messages on the UdpClient's port number and forward them to the OnPacketReceived event.
        /// </summary>
        public void ReceivePacket() // Threaded looping method.
        {
            while (_online)
            {
                try
                {
                    IPEndPoint remoteSender  = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     receivedBytes = _client.Receive(ref remoteSender);
                    if (receivedBytes != null && receivedBytes.Length != 0)
                    {
                        if (receivedBytes.Length == ByteHelper.HashByteLength + 2 && receivedBytes[0] == 0xCE && receivedBytes[receivedBytes.Length - 1] == 0xCE)
                        {
                            if (_messageSendingControlList.Count != 0)
                            {
                                // The received message is a ACK message.
                                string           hash   = OpenAck(receivedBytes);
                                ControlledPacket packet = _messageSendingControlList.Find(x => x.Hash == hash);
                                if (packet != null)
                                {
                                    _messageSendingControlList.Remove(packet);
                                }
                            }
                        }
                        else if (receivedBytes.Length == 1 && receivedBytes[0] == 0xEC)
                        {
                            // Fire an OnEtherConnectionReply event.
                            OnEtherConnectionReply(null);
                        }
                        else
                        {
                            // Send back an ACK packet.
                            string hash     = ByteHelper.GetHashString(receivedBytes);
                            byte[] ackBytes = CreateAck(hash);
                            _client.Send(ackBytes, ackBytes.Length, remoteSender);

                            // Check if the message is a duplicate.
                            if (!_messageReceivingControlList.Any(x => x == hash))
                            {
                                // Add the message's hash to a list so we don't react on the same message twice.
                                _messageReceivingControlList.Add(hash);
                                if (_messageReceivingControlList.Count > 5) // Only keep the latest 5 messages.
                                {
                                    _messageReceivingControlList.RemoveAt(0);
                                }

                                // Fire an OnPacketReceived event.
                                PacketReceivedEventArgs args = new PacketReceivedEventArgs();
                                args.Sender = remoteSender;
                                args.Data   = receivedBytes;
                                OnPacketReceived(args);
                            }
                        }
                    }
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode != SocketError.TimedOut)
                    {
                        System.Diagnostics.Debug.WriteLine("### " + _threadPacketListener.Name + " has crashed:");
                        System.Diagnostics.Debug.WriteLine("### " + ex.Message);
                        System.Diagnostics.Debug.WriteLine("### " + ex.ToString());
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }