public static void HandleCharacterCreate(PacketStream P, ref LoginClient Client, 
            ref CityServerListener CServerListener)
        {
            byte PacketLength = (byte)P.ReadByte();
            //Length of the unencrypted data, excluding the header (ID, length, unencrypted length).
            byte UnencryptedLength = (byte)P.ReadByte();

            P.DecryptPacket(Client.EncKey, Client.CryptoService, UnencryptedLength);

            Logger.LogDebug("Received CharacterCreate!");

            string AccountName = P.ReadString();

            Sim Char = new Sim(P.ReadString());
            Char.Timestamp = P.ReadString();
            Char.Name = P.ReadString();
            Char.Sex = P.ReadString();
            Char.CreatedThisSession = true;

            Client.CurrentlyActiveSim = Char;

            switch (Character.CreateCharacter(Char))
            {
                case CharacterCreationStatus.NameAlreadyExisted:
                    //TODO: Send packet.
                    break;
                case CharacterCreationStatus.ExceededCharacterLimit:
                    //TODO: Send packet.
                    break;
            }
        }
        public static void Handle(PacketStream stream, LoginClient session)
        {
            byte ID = (byte)stream.ReadByte();

            if (m_Handlers.ContainsKey(ID))
            {
                m_Handlers[ID].Handler(ref session, stream);
            }
        }
        /// <summary>
        /// A cityserver logged in!
        /// </summary>
        public static void HandleCityServerLogin(PacketStream P, ref CityServerClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();

            string Name = P.ReadString();
            string Description = P.ReadString();
            ulong Thumbnail = P.ReadUInt64();
            string IP = P.ReadString();
            int Port = P.ReadInt32();

            CityServerInfo Info = new CityServerInfo(Name, Description, Thumbnail, IP, Port);
            Client.ServerInfo = Info;
        }
        /// <summary>
        /// A cityserver logged in!
        /// </summary>
        public static void HandleCityServerLogin(PacketStream P, ref CityServerClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();
            Logger.LogDebug("CityServer logged in!");

            string Name = P.ReadString();
            string Description = P.ReadString();
            ulong Thumbnail = P.ReadUInt64();
            Guid GUID = new Guid();
            string UUID = GUID.ToString();
            ulong Map = P.ReadUInt64();
            string IP = P.ReadString();
            int Port = P.ReadInt32();

            CityInfo Info = new CityInfo(Name, Description, Thumbnail, UUID, Map, IP, Port);
            Client.ServerInfo = Info;
        }
        /// <summary>
        /// A cityserver logged in!
        /// </summary>
        public static void HandleCityServerLogin(PacketStream P, ref CityServerClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();

            Logger.LogDebug("CityServer logged in!");

            string Name        = P.ReadString();
            string Description = P.ReadString();
            ulong  Thumbnail   = P.ReadUInt64();
            Guid   GUID        = new Guid();
            string UUID        = GUID.ToString();
            ulong  Map         = P.ReadUInt64();
            string IP          = P.ReadString();
            int    Port        = P.ReadInt32();

            CityInfo Info = new CityInfo(Name, Description, Thumbnail, UUID, Map, IP, Port);

            Client.ServerInfo = Info;
        }
        /// <summary>
        /// Handles incoming packets from a CityServer.
        /// </summary>
        private void m_CServerListener_OnReceiveEvent(PacketStream P, ref CityServerClient Client)
        {
            byte ID = (byte)P.ReadByte();

                switch (ID)
                {
                    case 0x00:
                        CityServerPacketHandlers.HandleCityServerLogin(P, ref Client);
                        break;
                    case 0x01:
                        CityServerPacketHandlers.HandleKeyFetch(ref NetworkFacade.ClientListener, P, Client);
                        break;
                    case 0x02:
                        CityServerPacketHandlers.HandlePulse(P, ref Client);
                        break;
                }
        }
        /// <summary>
        /// Handles incoming packets from connected clients.
        /// </summary>
        private void m_Listener_OnReceiveEvent(PacketStream P, LoginClient Client)
        {
            byte ID = (byte)P.ReadByte();

            switch (ID)
            {
                case 0x00:
                    PacketHandlers.HandleLoginRequest(P, ref Client);
                    break;
                case 0x05:
                    PacketHandlers.HandleCharacterInfoRequest(P, Client);
                    break;
                case 0x06:
                    PacketHandlers.HandleCityInfoRequest(P, Client);
                    break;
                case 0x07:
                    PacketHandlers.HandleCharacterCreate(P, ref Client, ref NetworkFacade.CServerListener);
                    break;
                default:
                    Logger.LogInfo("Received unhandled packet - ID: " + P.PacketID);
                    break;
            }
        }
        public static void HandleCharacterInfoRequest(PacketStream P, LoginClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();
            //Length of the unencrypted data, excluding the header (ID, length, unencrypted length).
            byte UnencryptedLength = (byte)P.ReadByte();

            P.DecryptPacket(Client.EncKey, Client.CryptoService, UnencryptedLength);

            Logger.LogDebug("Received CharacterInfoRequest!");

            byte Length = (byte)P.ReadByte();
            byte[] StrBuf = new byte[Length];
            P.Read(StrBuf, 0, Length - 1);
            DateTime Timestamp = DateTime.Parse(Encoding.ASCII.GetString(StrBuf));

            //Database.CheckCharacterTimestamp(Client.Username, Client, TimeStamp);

            Character[] Characters = Character.GetCharacters(Client.Username);

            if (Characters != null)
            {
                PacketStream Packet = new PacketStream(0x05, 0);

                MemoryStream PacketData = new MemoryStream();
                BinaryWriter PacketWriter = new BinaryWriter(PacketData);

                //The timestamp for all characters should be equal, so just check the first character.
                if (Timestamp < DateTime.Parse(Characters[0].LastCached) ||
                    Timestamp > DateTime.Parse(Characters[0].LastCached))
                {
                    //Write the characterdata into a temporary buffer.
                    if (Characters.Length == 1)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Flush();
                    }
                    else if (Characters.Length == 2)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Write(Characters[1].CharacterID);
                        PacketWriter.Write(Characters[1].GUID);
                        PacketWriter.Write(Characters[1].LastCached);
                        PacketWriter.Write(Characters[1].Name);
                        PacketWriter.Write(Characters[1].Sex);

                        PacketWriter.Flush();
                    }
                    else if (Characters.Length == 3)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Write(Characters[1].CharacterID);
                        PacketWriter.Write(Characters[1].GUID);
                        PacketWriter.Write(Characters[1].LastCached);
                        PacketWriter.Write(Characters[1].Name);
                        PacketWriter.Write(Characters[1].Sex);

                        PacketWriter.Write(Characters[2].CharacterID);
                        PacketWriter.Write(Characters[2].GUID);
                        PacketWriter.Write(Characters[2].LastCached);
                        PacketWriter.Write(Characters[2].Name);
                        PacketWriter.Write(Characters[2].Sex);

                        PacketWriter.Flush();
                    }

                    Packet.WriteByte((byte)Characters.Length);      //Total number of characters.
                    Packet.Write(PacketData.ToArray(), 0, (int)PacketData.Length);
                    PacketWriter.Close();

                    Client.SendEncrypted(0x05, Packet.ToArray());
                }
            }
            else //No characters existed for the account.
            {
                PacketStream Packet = new PacketStream(0x05, 0);
                Packet.WriteByte(0x00); //0 characters.

                Client.SendEncrypted(0x05, Packet.ToArray());
            }
        }
        public static void HandleLoginRequest(PacketStream P, ref LoginClient Client)
        {
            Logger.LogInfo("Received LoginRequest!\r\n");

            byte PacketLength = (byte)P.ReadByte();

            byte AccountStrLength = (byte)P.ReadByte();

            byte[] AccountNameBuf = new byte[AccountStrLength];
            P.Read(AccountNameBuf, 0, AccountStrLength);
            string AccountName = Encoding.ASCII.GetString(AccountNameBuf);
            Logger.LogInfo("Accountname: " + AccountName + "\r\n");

            byte HashLength = (byte)P.ReadByte();
            byte[] HashBuf = new byte[HashLength];
            P.Read(HashBuf, 0, HashLength);

            Client.Hash = HashBuf;

            byte KeyLength = (byte)P.ReadByte();
            Client.EncKey = new byte[KeyLength];
            P.Read(Client.EncKey, 0, KeyLength);

            byte Version1 = (byte)P.ReadByte();
            byte Version2 = (byte)P.ReadByte();
            byte Version3 = (byte)P.ReadByte();
            byte Version4 = (byte)P.ReadByte();

            Logger.LogInfo("Done reading LoginRequest, checking account...\r\n");

            //Database.CheckAccount(AccountName, Client, HashBuf);

            if (Account.DoesAccountExist(AccountName) && Account.IsCorrectPassword(AccountName, HashBuf))
            {
                //0x01 = InitLoginNotify
                PacketStream OutPacket = new PacketStream(0x01, 2);
                OutPacket.WriteByte(0x01);
                OutPacket.WriteByte(0x01);
                Client.Username = AccountName;
                //This is neccessary to encrypt packets.
                Client.Password = Account.GetPassword(AccountName);
                Client.Send(OutPacket.ToArray());

                Logger.LogInfo("Sent InitLoginNotify!\r\n");
            }
            else
            {
                PacketStream OutPacket = new PacketStream(0x02, 2);
                P.WriteByte(0x02);
                P.WriteByte(0x01);
                Client.Send(P.ToArray());

                Logger.LogInfo("Bad accountname - sent SLoginFailResponse!\r\n");
                Client.Disconnect();
            }
        }
        public static void HandleCityInfoRequest(PacketStream P, LoginClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();
            //Length of the unencrypted data, excluding the header (ID, length, unencrypted length).
            byte UnencryptedLength = (byte)P.ReadByte();

            P.DecryptPacket(Client.EncKey, Client.CryptoService, UnencryptedLength);

            //This packet only contains a dummy byte, don't bother reading it.

            PacketStream Packet = new PacketStream(0x06, 0);

            MemoryStream PacketData = new MemoryStream();
            BinaryWriter PacketWriter = new BinaryWriter(PacketData);

            PacketWriter.Write((byte)NetworkFacade.CServerListener.CityServers.Count);

            foreach (CityServerClient City in NetworkFacade.CServerListener.CityServers)
            {
                PacketWriter.Write(City.ServerInfo.Name);
                PacketWriter.Write(City.ServerInfo.Description);
                PacketWriter.Write(City.ServerInfo.IP);
                PacketWriter.Write(City.ServerInfo.Port);
                PacketWriter.Write((byte)City.ServerInfo.Status);
                PacketWriter.Write(City.ServerInfo.Thumbnail);
                PacketWriter.Write(City.ServerInfo.UUID);

                PacketWriter.Flush();
            }

            Packet.Write(PacketData.ToArray(), 0, PacketData.ToArray().Length);
            PacketWriter.Close();

            Client.SendEncrypted(0x06, Packet.ToArray());
        }
Exemple #11
0
        /// <summary>
        /// Handles incoming packets from a CityServer.
        /// </summary>
        void m_CServerListener_OnReceiveEvent(PacketStream P, CityServerClient Client)
        {
            byte ID = (byte)P.ReadByte();

                switch (ID)
                {
                    case 0x00:
                        CityServerPacketHandlers.HandleCityServerLogin(P, ref Client);
                        break;
                    case 0x01:
                        CityServerPacketHandlers.HandleKeyFetch(ref m_Listener, P, Client);
                        break;
                }
        }
        /**
         * Actual packet handlers
         */
        public static void HandleLoginRequest(ref LoginClient Client, PacketStream P)
        {
            Logger.LogInfo("Received LoginRequest!\r\n");
            ushort PacketLength = (ushort)P.ReadUShort();

            byte AccountStrLength = (byte)P.ReadByte();
            byte[] AccountNameBuf = new byte[AccountStrLength];
            P.Read(AccountNameBuf, 0, AccountStrLength);
            string AccountName = Encoding.ASCII.GetString(AccountNameBuf);
            Logger.LogInfo("Accountname: " + AccountName + "\r\n");

            byte HashLength = (byte)P.ReadByte();
            byte[] HashBuf = new byte[HashLength];
            P.Read(HashBuf, 0, HashLength);

            Client.Hash = HashBuf;

            byte KeyLength = (byte)P.ReadByte();
            Client.EncKey = new byte[KeyLength];
            P.Read(Client.EncKey, 0, KeyLength);

            byte Version1 = (byte)P.ReadByte();
            byte Version2 = (byte)P.ReadByte();
            byte Version3 = (byte)P.ReadByte();
            byte Version4 = (byte)P.ReadByte();

            Logger.LogInfo("Done reading LoginRequest, checking account...\r\n");

            using (var db = DataAccess.Get())
            {
                var account = db.Accounts.GetByUsername(AccountName);
                if (account == null)
                {
                    PacketStream OutPacket = new PacketStream(0x02, 2);
                    OutPacket.WriteHeader();
                    OutPacket.WriteByte(0x01);
                    Client.Send(OutPacket);

                    Logger.LogInfo("Bad accountname - sent SLoginFailResponse!\r\n");
                    Client.Disconnect();
                    return;
                }

                if (account.IsCorrectPassword(AccountName, HashBuf))
                {
                    //0x01 = InitLoginNotify
                    PacketStream OutPacket = new PacketStream(0x01, 1);
                    OutPacket.WriteHeader();
                    Client.Username = AccountName;
                    //This is neccessary to encrypt packets.
                    //TODO: Put something else here
                    //Client.Password = Account.GetPassword(AccountName);
                    Client.Send(OutPacket.ToArray());

                    Logger.LogInfo("Sent InitLoginNotify!\r\n");
                }
            }
        }
 public static void Handle(PacketStream stream, LoginClient session)
 {
     byte ID = (byte)stream.ReadByte();
     if (m_Handlers.ContainsKey(ID))
     {
         m_Handlers[ID].Handler(ref session, stream);
     }
 }
        /**
         * Actual packet handlers
         */
        public static void HandleLoginRequest(ref LoginClient Client, PacketStream P)
        {
            Logger.LogInfo("Received LoginRequest!\r\n");
            ushort PacketLength = (ushort)P.ReadUShort();

            byte AccountStrLength = (byte)P.ReadByte();

            byte[] AccountNameBuf = new byte[AccountStrLength];
            P.Read(AccountNameBuf, 0, AccountStrLength);
            string AccountName = Encoding.ASCII.GetString(AccountNameBuf);

            Logger.LogInfo("Accountname: " + AccountName + "\r\n");

            byte HashLength = (byte)P.ReadByte();

            byte[] HashBuf = new byte[HashLength];
            P.Read(HashBuf, 0, HashLength);

            Client.Hash = HashBuf;

            byte KeyLength = (byte)P.ReadByte();

            Client.EncKey = new byte[KeyLength];
            P.Read(Client.EncKey, 0, KeyLength);

            byte Version1 = (byte)P.ReadByte();
            byte Version2 = (byte)P.ReadByte();
            byte Version3 = (byte)P.ReadByte();
            byte Version4 = (byte)P.ReadByte();

            Logger.LogInfo("Done reading LoginRequest, checking account...\r\n");

            using (var db = DataAccess.Get())
            {
                var account = db.Accounts.GetByUsername(AccountName);
                if (account == null)
                {
                    PacketStream OutPacket = new PacketStream(0x02, 2);
                    OutPacket.WriteHeader();
                    OutPacket.WriteByte(0x01);
                    Client.Send(OutPacket);

                    Logger.LogInfo("Bad accountname - sent SLoginFailResponse!\r\n");
                    Client.Disconnect();
                    return;
                }

                if (account.IsCorrectPassword(AccountName, HashBuf))
                {
                    //0x01 = InitLoginNotify
                    PacketStream OutPacket = new PacketStream(0x01, 1);
                    OutPacket.WriteHeader();
                    Client.Username = AccountName;
                    //This is neccessary to encrypt packets.
                    //TODO: Put something else here
                    //Client.Password = Account.GetPassword(AccountName);
                    Client.Send(OutPacket.ToArray());

                    Logger.LogInfo("Sent InitLoginNotify!\r\n");
                }
            }
        }
Exemple #15
0
        public static void HandleCharacterInfoRequest(PacketStream P, LoginClient Client)
        {
            byte PacketLength = (byte)P.ReadByte();
            //Length of the unencrypted data, excluding the header (ID, length, unencrypted length).
            byte UnencryptedLength = (byte)P.ReadByte();

            P.DecryptPacket(Client.EncKey, Client.CryptoService, UnencryptedLength);

            Logger.LogDebug("Received CharacterInfoRequest!");

            byte Length = (byte)P.ReadByte();

            byte[] StrBuf = new byte[Length];
            P.Read(StrBuf, 0, Length - 1);
            DateTime Timestamp = DateTime.Parse(Encoding.ASCII.GetString(StrBuf));

            //Database.CheckCharacterTimestamp(Client.Username, Client, TimeStamp);

            Character[] Characters = Character.GetCharacters(Client.Username);

            if (Characters != null)
            {
                PacketStream Packet = new PacketStream(0x05, 0);

                MemoryStream PacketData   = new MemoryStream();
                BinaryWriter PacketWriter = new BinaryWriter(PacketData);

                //The timestamp for all characters should be equal, so just check the first character.
                if (Timestamp < DateTime.Parse(Characters[0].LastCached) ||
                    Timestamp > DateTime.Parse(Characters[0].LastCached))
                {
                    //Write the characterdata into a temporary buffer.
                    if (Characters.Length == 1)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Flush();
                    }
                    else if (Characters.Length == 2)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Write(Characters[1].CharacterID);
                        PacketWriter.Write(Characters[1].GUID);
                        PacketWriter.Write(Characters[1].LastCached);
                        PacketWriter.Write(Characters[1].Name);
                        PacketWriter.Write(Characters[1].Sex);

                        PacketWriter.Flush();
                    }
                    else if (Characters.Length == 3)
                    {
                        PacketWriter.Write(Characters[0].CharacterID);
                        PacketWriter.Write(Characters[0].GUID);
                        PacketWriter.Write(Characters[0].LastCached);
                        PacketWriter.Write(Characters[0].Name);
                        PacketWriter.Write(Characters[0].Sex);

                        PacketWriter.Write(Characters[1].CharacterID);
                        PacketWriter.Write(Characters[1].GUID);
                        PacketWriter.Write(Characters[1].LastCached);
                        PacketWriter.Write(Characters[1].Name);
                        PacketWriter.Write(Characters[1].Sex);

                        PacketWriter.Write(Characters[2].CharacterID);
                        PacketWriter.Write(Characters[2].GUID);
                        PacketWriter.Write(Characters[2].LastCached);
                        PacketWriter.Write(Characters[2].Name);
                        PacketWriter.Write(Characters[2].Sex);

                        PacketWriter.Flush();
                    }

                    Packet.WriteByte((byte)Characters.Length);      //Total number of characters.
                    Packet.Write(PacketData.ToArray(), 0, (int)PacketData.Length);
                    PacketWriter.Close();

                    Client.SendEncrypted(0x05, Packet.ToArray());
                }
            }
            else //No characters existed for the account.
            {
                PacketStream Packet = new PacketStream(0x05, 0);
                Packet.WriteByte(0x00); //0 characters.

                Client.SendEncrypted(0x05, Packet.ToArray());
            }
        }