public static void SendLoginRequest(LoginArgsContainer Args) { //Variable length... PacketStream Packet = new PacketStream((byte)PacketType.LOGIN_REQUEST, 0); Packet.WriteByte(0x00); SaltedHash Hash = new SaltedHash(new SHA512Managed(), Args.Username.Length); MemoryStream MemStream = new MemoryStream(); DecryptionArgsContainer DecryptionArgs = Args.Enc.GetDecryptionArgsContainer(); MemStream.WriteByte((byte)Args.Username.Length); MemStream.Write(Encoding.ASCII.GetBytes(Args.Username), 0, Encoding.ASCII.GetBytes(Args.Username).Length); byte[] HashBuf = Hash.ComputePasswordHash(Args.Username, Args.Password); PlayerAccount.Hash = HashBuf; MemStream.WriteByte((byte)HashBuf.Length); MemStream.Write(HashBuf, 0, HashBuf.Length); Packet.WriteUInt16((ushort)(PacketHeaders.UNENCRYPTED + MemStream.ToArray().Length + 4)); Packet.WriteBytes(MemStream.ToArray()); string[] Version = GlobalSettings.Default.ClientVersion.Split('.'); Packet.WriteByte((byte)int.Parse(Version[0])); //Version 1 Packet.WriteByte((byte)int.Parse(Version[1])); //Version 2 Packet.WriteByte((byte)int.Parse(Version[2])); //Version 3 Packet.WriteByte((byte)int.Parse(Version[3])); //Version 4 Args.Client.Send(Packet.ToArray()); }
/// <summary> /// Sends a CharacterCreate packet to a CityServer. /// </summary> /// <param name="LoginArgs">Arguments used to log onto the CityServer.</param> /// <param name="Character">The character to create on the CityServer.</param> public static void SendCharacterCreateCity(LoginArgsContainer LoginArgs, TSOClient.VM.Sim Character) { PacketStream Packet = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY, 0); Packet.WriteHeader(); byte[] EncryptionKey = LoginArgs.Enc.GetDecryptionArgsContainer().ARC4DecryptArgs.EncryptionKey; MemoryStream PacketData = new MemoryStream(); BinaryWriter Writer = new BinaryWriter(PacketData); Writer.Write((byte)LoginArgs.Username.Length); Writer.Write(Encoding.ASCII.GetBytes(LoginArgs.Username), 0, Encoding.ASCII.GetBytes(LoginArgs.Username).Length); //LoginArgs password is set to PlayerAccount.Hash for relogging to the cityserver. byte[] HashBuf = Convert.FromBase64String(LoginArgs.Password); Writer.Write((byte)HashBuf.Length); Writer.Write(HashBuf, 0, HashBuf.Length); Writer.Write((byte)EncryptionKey.Length); Writer.Write(EncryptionKey); Writer.Write(PlayerAccount.CityToken); Writer.Write(Character.Timestamp); Writer.Write(Character.Name); Writer.Write(Character.Sex); Writer.Write(Character.Description); Writer.Write((ulong)Character.HeadOutfitID); Writer.Write((ulong)Character.BodyOutfitID); Writer.Write((byte)Character.AppearanceType); Packet.WriteUInt16((ushort)((ushort)PacketHeaders.UNENCRYPTED + PacketData.Length)); Packet.WriteBytes(PacketData.ToArray()); Writer.Close(); LoginArgs.Client.Send(Packet.ToArray()); }
public static void SendCharacterInfoRequest(string TimeStamp) { PacketStream Packet = new PacketStream(0x05, 0); //If this timestamp is newer than the server's timestamp, it means //the client doesn't have a charactercache. If it's older, it means //the cache needs to be updated. If it matches, the server sends an //empty responsepacket. //Packet.WriteString(TimeStamp); Packet.WriteByte((byte)TimeStamp.Length); Packet.WriteBytes(Encoding.ASCII.GetBytes(TimeStamp)); byte[] PacketData = Packet.ToArray(); PlayerAccount.Client.Send(FinalizePacket(0x05, new DESCryptoServiceProvider(), PacketData)); }
public static void SendLoginRequest(NetworkClient Client, string Username, string Password) { //Variable length... PacketStream Packet = new PacketStream(0x00, 0); Packet.WriteByte(0x00); SaltedHash Hash = new SaltedHash(new SHA512Managed(), Username.Length); byte[] HashBuf = new byte[Encoding.ASCII.GetBytes(Password).Length + Encoding.ASCII.GetBytes(Username).Length]; MemoryStream MemStream = new MemoryStream(); PasswordDeriveBytes Pwd = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(Password), Encoding.ASCII.GetBytes("SALT"), "SHA1", 10); byte[] EncKey = Pwd.GetBytes(8); PlayerAccount.EncKey = EncKey; MemStream.WriteByte((byte)Username.Length); MemStream.Write(Encoding.ASCII.GetBytes(Username), 0, Encoding.ASCII.GetBytes(Username).Length); HashBuf = Hash.ComputePasswordHash(Username, Password); PlayerAccount.Hash = HashBuf; MemStream.WriteByte((byte)HashBuf.Length); MemStream.Write(HashBuf, 0, HashBuf.Length); MemStream.WriteByte((byte)EncKey.Length); MemStream.Write(EncKey, 0, EncKey.Length); Packet.WriteByte((byte)(2 + MemStream.ToArray().Length + 4)); Packet.WriteBytes(MemStream.ToArray()); //TODO: Change this to write a global client version. Packet.WriteByte(0x00); //Version 1 Packet.WriteByte(0x00); //Version 2 Packet.WriteByte(0x00); //Version 3 Packet.WriteByte(0x01); //Version 4 Client.Send(Packet.ToArray()); }
/// <summary> /// Sends a token to a CityServer, as received by a LoginServer. /// </summary> /// <param name="Client">A NetworkClient instance.</param> public static void SendCityToken(NetworkClient Client) { PacketStream Packet = new PacketStream((byte)PacketType.CITY_TOKEN, 0); Packet.WriteHeader(); MemoryStream PacketData = new MemoryStream(); BinaryWriter Writer = new BinaryWriter(PacketData); Writer.Write((byte)PlayerAccount.Hash.Length); Writer.Write(PlayerAccount.Hash, 0, PlayerAccount.Hash.Length); Writer.Write(PlayerAccount.CityToken); Packet.WriteUInt16((ushort)((ushort)PacketHeaders.UNENCRYPTED + PacketData.Length)); Packet.WriteBytes(PacketData.ToArray()); Writer.Close(); Client.Send(Packet.ToArray()); }
/// <summary> /// Sends a token to a CityServer, as received by a LoginServer. /// </summary> /// <param name="Client">A NetworkClient instance.</param> public static void SendCityToken(NetworkClient Client) { PacketStream Packet = new PacketStream((byte)PacketType.CITY_TOKEN, 0); Packet.WriteHeader(); byte[] EncryptionKey = Client.ClientEncryptor.GetDecryptionArgsContainer().ARC4DecryptArgs.EncryptionKey; MemoryStream PacketData = new MemoryStream(); BinaryWriter Writer = new BinaryWriter(PacketData); Writer.Write((byte)PlayerAccount.Hash.Length); Writer.Write(PlayerAccount.Hash, 0, PlayerAccount.Hash.Length); Writer.Write((byte)EncryptionKey.Length); Writer.Write(EncryptionKey); Writer.Write(PlayerAccount.CityToken); Packet.WriteUInt16((ushort)((ushort)PacketHeaders.UNENCRYPTED + PacketData.Length)); Packet.WriteBytes(PacketData.ToArray()); Writer.Close(); Client.Send(Packet.ToArray()); }
private void ReceiveCallback(IAsyncResult AR) { try { Socket Sock = (Socket)AR.AsyncState; int NumBytesRead = Sock.EndReceive(AR); Log.LogThis("Received: " + NumBytesRead + " bytes!", eloglevel.info); byte[] TmpBuf = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuf, 0, NumBytesRead); //The packet is given an ID of 0x00 because its ID is currently unknown. PacketStream TempPacket = new PacketStream(0x00, NumBytesRead, TmpBuf); byte ID = TempPacket.PeekByte(0); int PacketLength = 0; bool FoundMatchingID = false; FoundMatchingID = FindMatchingPacketID(ID); if (FoundMatchingID) { PacketLength = m_PacketIDs[ID]; Log.LogThis("PacketLength: " + PacketLength, eloglevel.info); Log.LogThis("Found matching PacketID (" + ID + ")!\r\n\r\n", eloglevel.info); if (NumBytesRead == PacketLength) { Log.LogThis("Got packet - exact length!\r\n\r\n", eloglevel.info); m_RecvBuf = new byte[11024]; OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray())); } else if (NumBytesRead < PacketLength) { m_TempPacket = new PacketStream(ID, PacketLength); byte[] TmpBuffer = new byte[NumBytesRead]; //Store the number of bytes that were read in the temporary buffer. Log.LogThis("Got data, but not a full packet - stored " + NumBytesRead.ToString() + "bytes!\r\n\r\n", eloglevel.info); Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead); m_TempPacket.WriteBytes(TmpBuffer); //And reset the buffers! m_RecvBuf = new byte[11024]; TmpBuffer = null; } else if (PacketLength == 0) { Log.LogThis("Received variable length packet!\r\n", eloglevel.info); if (NumBytesRead > 2) { PacketLength = TempPacket.PeekByte(1); if (NumBytesRead == PacketLength) { Log.LogThis("Received exact number of bytes for packet!\r\n", eloglevel.info); m_RecvBuf = new byte[11024]; m_TempPacket = null; OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray())); } else if (NumBytesRead < PacketLength) { Log.LogThis("Didn't receive entire packet - stored: " + PacketLength + " bytes!\r\n", eloglevel.info); TempPacket.SetLength(PacketLength); m_TempPacket = TempPacket; m_RecvBuf = new byte[11024]; } else if (NumBytesRead > PacketLength) { Log.LogThis("Received more bytes than needed for packet. Excess: " + (NumBytesRead - PacketLength) + "\r\n", eloglevel.info); byte[] TmpBuffer = new byte[NumBytesRead - PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, TmpBuffer, 0, TmpBuffer.Length); m_TempPacket = new PacketStream(TmpBuffer[0], NumBytesRead - PacketLength, TmpBuffer); byte[] PacketBuffer = new byte[PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, PacketBuffer, 0, PacketBuffer.Length); m_RecvBuf = new byte[11024]; OnReceivedData(new PacketStream(ID, PacketLength, PacketBuffer)); } } } } else { if (m_TempPacket != null) { if (m_TempPacket.Length < m_TempPacket.BufferLength) { //Received the exact number of bytes needed to complete the stored packet. if ((m_TempPacket.BufferLength + NumBytesRead) == m_TempPacket.Length) { byte[] TmpBuffer = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead); m_RecvBuf = new byte[11024]; TmpBuffer = null; } //Received more than the number of bytes needed to complete the packet! else if ((m_TempPacket.BufferLength + NumBytesRead) > m_TempPacket.Length) { int Target = (int)((m_TempPacket.BufferLength + NumBytesRead) - m_TempPacket.Length); byte[] TmpBuffer = new byte[Target]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, Target); m_TempPacket.WriteBytes(TmpBuffer); //Now we have a full packet, so call the received event! OnReceivedData(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray())); //Copy the remaining bytes in the receiving buffer. TmpBuffer = new byte[NumBytesRead - Target]; Buffer.BlockCopy(m_RecvBuf, Target, TmpBuffer, 0, (NumBytesRead - Target)); //Give the temporary packet an ID of 0x00 since we don't know its ID yet. TempPacket = new PacketStream(0x00, NumBytesRead - Target, TmpBuffer); ID = TempPacket.PeekByte(0); //This SHOULD be an existing ID, but let's sanity-check it... if (FindMatchingPacketID(ID)) { m_TempPacket = new PacketStream(ID, m_PacketIDs[ID], TempPacket.ToArray()); //Congratulations, you just received another packet! if (m_TempPacket.Length == m_TempPacket.BufferLength) { OnReceivedData(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray())); //No more data to store on this read, so reset everything... m_TempPacket = null; TmpBuffer = null; m_RecvBuf = new byte[11024]; } } else { //Houston, we have a problem (this should never occur)! } } } } } m_Sock.BeginReceive(m_RecvBuf, 0, m_RecvBuf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), m_Sock); } catch (SocketException E) { Log.LogThis("SocketException: " + E.ToString(), eloglevel.info); Disconnect(); } }
private void ReceiveCallback(IAsyncResult AR) { try { Socket Sock = (Socket)AR.AsyncState; int NumBytesRead = Sock.EndReceive(AR); /** Cant do anything with this! **/ if (NumBytesRead == 0) { return; } Log.LogThis("Received: " + NumBytesRead + " bytes!", eloglevel.info); byte[] TmpBuf = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuf, 0, NumBytesRead); //The packet is given an ID of 0x00 because its ID is currently unknown. PacketStream TempPacket = new PacketStream(0x00, NumBytesRead, TmpBuf); byte ID = TempPacket.PeekByte(0); int PacketLength = 0; var handler = FindPacketHandler(ID); if (handler != null) { PacketLength = handler.Length; Log.LogThis("PacketLength: " + PacketLength, eloglevel.info); Log.LogThis("Found matching PacketID (" + ID + ")!\r\n\r\n", eloglevel.info); if (NumBytesRead == PacketLength) { Log.LogThis("Got packet - exact length!\r\n\r\n", eloglevel.info); m_RecvBuf = new byte[11024]; OnPacket(new PacketStream(ID, PacketLength, TempPacket.ToArray()), handler); } else if (NumBytesRead < PacketLength) { m_TempPacket = new PacketStream(ID, PacketLength); byte[] TmpBuffer = new byte[NumBytesRead]; //Store the number of bytes that were read in the temporary buffer. Log.LogThis("Got data, but not a full packet - stored " + NumBytesRead.ToString() + "bytes!\r\n\r\n", eloglevel.info); Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead); m_TempPacket.WriteBytes(TmpBuffer); //And reset the buffers! m_RecvBuf = new byte[11024]; TmpBuffer = null; } else if (PacketLength == 0) { Log.LogThis("Received variable length packet!\r\n", eloglevel.info); if (NumBytesRead > (int)PacketHeaders.UNENCRYPTED) //Header is 3 bytes. { PacketLength = TempPacket.PeekUShort(1); if (NumBytesRead == PacketLength) { Log.LogThis("Received exact number of bytes for packet!\r\n", eloglevel.info); m_RecvBuf = new byte[11024]; m_TempPacket = null; OnPacket(new PacketStream(ID, PacketLength, TempPacket.ToArray()), handler); } else if (NumBytesRead < PacketLength) { Log.LogThis("Didn't receive entire packet - stored: " + PacketLength + " bytes!\r\n", eloglevel.info); TempPacket.SetLength(PacketLength); m_TempPacket = TempPacket; m_RecvBuf = new byte[11024]; } else if (NumBytesRead > PacketLength) { Log.LogThis("Received more bytes than needed for packet. Excess: " + (NumBytesRead - PacketLength) + "\r\n", eloglevel.info); byte[] TmpBuffer = new byte[NumBytesRead - PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, TmpBuffer, 0, TmpBuffer.Length); m_TempPacket = new PacketStream(TmpBuffer[0], NumBytesRead - PacketLength, TmpBuffer); byte[] PacketBuffer = new byte[PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, PacketBuffer, 0, PacketBuffer.Length); m_RecvBuf = new byte[11024]; OnPacket(new PacketStream(ID, PacketLength, PacketBuffer), handler); } } } } else { if (m_TempPacket != null) { if (m_TempPacket.Length < m_TempPacket.BufferLength) { //Received the exact number of bytes needed to complete the stored packet. if ((m_TempPacket.BufferLength + NumBytesRead) == m_TempPacket.Length) { byte[] TmpBuffer = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead); m_RecvBuf = new byte[11024]; TmpBuffer = null; } //Received more than the number of bytes needed to complete the packet! else if ((m_TempPacket.BufferLength + NumBytesRead) > m_TempPacket.Length) { int Target = (int)((m_TempPacket.BufferLength + NumBytesRead) - m_TempPacket.Length); byte[] TmpBuffer = new byte[Target]; Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, Target); m_TempPacket.WriteBytes(TmpBuffer); //Now we have a full packet, so call the received event! OnPacket(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray()), handler); //Copy the remaining bytes in the receiving buffer. TmpBuffer = new byte[NumBytesRead - Target]; Buffer.BlockCopy(m_RecvBuf, Target, TmpBuffer, 0, (NumBytesRead - Target)); //Give the temporary packet an ID of 0x00 since we don't know its ID yet. TempPacket = new PacketStream(0x00, NumBytesRead - Target, TmpBuffer); ID = TempPacket.PeekByte(0); handler = FindPacketHandler(ID); //This SHOULD be an existing ID, but let's sanity-check it... if (handler != null) { m_TempPacket = new PacketStream(ID, handler.Length, TempPacket.ToArray()); //Congratulations, you just received another packet! if (m_TempPacket.Length == m_TempPacket.BufferLength) { OnPacket(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray()), handler); //No more data to store on this read, so reset everything... m_TempPacket = null; TmpBuffer = null; m_RecvBuf = new byte[11024]; } } else { //Houston, we have a problem (this should never occur)! } } } } } m_Sock.BeginReceive(m_RecvBuf, 0, m_RecvBuf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), m_Sock); } catch (SocketException E) { Log.LogThis("SocketException: " + E.ToString(), eloglevel.info); Disconnect(); } }