private bool VerifyCRC(ClientPacket packet) { bool encryptedChecksum = !packet.Header.HasFlag(PacketHeaderFlags.RequestRetransmit) && packet.Header.HasFlag(PacketHeaderFlags.EncryptedChecksum); if (encryptedChecksum) { #if NETDIAG int?gen = GetGeneration(ConnectionData.IssacClient, packet); if (gen != null) { // gen should always be 1 notch forward, but some programming errors have revealed gen to be 2 or more and since fixed // generational ISSAC helps immensely when troubleshooting certain kinds of protocol problems ConnectionData.IssacClient = GetGeneration(ConnectionData.IssacClient, gen.Value); if (gen.Value != 1) { packetLog.Warn($"Packet CRC encryption generation out of sequence for packet {packet.Header.Sequence} gen {gen} {UnfoldFlags(packet.Header.Flags)}"); } packetLog.Debug($"Verified encrypted CRC for packet {packet.Header.Sequence} gen {gen} {UnfoldFlags(packet.Header.Flags)}"); return(true); } #else if (packet.VerifyChecksum(ConnectionData.IssacClient.GetOffset())) { return(true); } #endif } else { if (packet.VerifyChecksum(0)) { packetLog.Debug($"Verified CRC for packet {packet.Header.Sequence} {UnfoldFlags(packet.Header.Flags)}"); return(true); } } NetworkStatistics.C2S_CRCErrors_Aggregate_Increment(); return(false); }
private bool GetFutureGeneration(Common.Cryptography.ISAAC state, ClientPacket packet, ref int Generation, int limit) { limit--; if (limit < 1) { return(false); } Generation++; uint x = state.GetOffset(); if (packet.VerifyChecksum(x)) { return(true); } else { return(GetFutureGeneration(state, packet, ref Generation, limit)); } }
/// <summary> /// Handles a packet, reading the flags and processing all fragments. /// </summary> /// <param name="packet">ClientPacket to handle</param> private void HandlePacket(ClientPacket packet) { packetLog.DebugFormat("[{0}] Handling packet {1}", session.LoggingIdentifier, packet.Header.Sequence); // Upon a client's request of packet retransmit the session CRC salt/offset becomes out of sync somehow. // This hack recovers the correct offset and makes WAN client sessions at least reliable enough to test with. // TODO: figure out why uint issacXor = !packet.Header.HasFlag(PacketHeaderFlags.RequestRetransmit) && packet.Header.HasFlag(PacketHeaderFlags.EncryptedChecksum) ? ConnectionData.IssacClient.GetOffset() : 0; if (!packet.VerifyChecksum(issacXor)) { if (issacXor != 0) { issacXor = ConnectionData.IssacClient.GetOffset(); packetLog.WarnFormat("[{0}] Packet {1} has invalid checksum, trying the next offset", session.LoggingIdentifier, packet.Header.Sequence); bool verified = packet.VerifyChecksum(issacXor); packetLog.WarnFormat("[{0}] Packet {1} improvised offset checksum result: {2}", session.LoggingIdentifier, packet.Header.Sequence, (verified) ? "successful" : "failed"); } else { packetLog.WarnFormat("[{0}] Packet {1} has invalid checksum", session.LoggingIdentifier, packet.Header.Sequence); } } // TODO: drop corrupted packets? // depending on the current session state: // Set the next timeout tick value, to compare against in the WorldManager // Sessions that have gone past the AuthLoginRequest step will stay active for a longer period of time (exposed via configuration) // Sessions that in the AuthLoginRequest will have a short timeout, as set in the AuthenticationHandler.DefaultAuthTimeout. // Example: Applications that check uptime will stay in the AuthLoginRequest state. session.Network.TimeoutTick = (session.State == Enum.SessionState.AuthLoginRequest) ? DateTime.UtcNow.AddSeconds(WorldManager.DefaultSessionTimeout).Ticks : DateTime.UtcNow.AddSeconds(AuthenticationHandler.DefaultAuthTimeout).Ticks; // If we have an EchoRequest flag, we should flag to respond with an echo response on next send. if (packet.Header.HasFlag(PacketHeaderFlags.EchoRequest)) { FlagEcho(packet.HeaderOptional.EchoRequestClientTime); } // If we have an AcknowledgeSequence flag, we can clear our cached packet buffer up to that sequence. if (packet.Header.HasFlag(PacketHeaderFlags.AckSequence)) { AcknowledgeSequence(packet.HeaderOptional.Sequence); } if (packet.Header.HasFlag(PacketHeaderFlags.TimeSync)) { packetLog.DebugFormat("[{0}] Incoming TimeSync TS: {1}", session.LoggingIdentifier, packet.HeaderOptional.TimeSynch); // Do something with this... // Based on network traces these are not 1:1. Server seems to send them every 20 seconds per port. // Client seems to send them alternatingly every 2 or 4 seconds per port. // We will send this at a 20 second time interval. I don't know what to do with these when we receive them at this point. } // If the client is requesting a retransmission, pull those packets from the queue and resend them. if (packet.Header.HasFlag(PacketHeaderFlags.RequestRetransmit)) { foreach (uint sequence in packet.HeaderOptional.RetransmitData) { Retransmit(sequence); } // TODO: calculate packet loss } // This should be set on the first packet to the server indicating the client is logging in. // This is the start of a three-way handshake between the client and server (LoginRequest, ConnectRequest, ConnectResponse) // Note this would be sent to each server a client would connect too (Login and each world). // In our current implimenation we handle all roles in this one server. if (packet.Header.HasFlag(PacketHeaderFlags.LoginRequest)) { packetLog.Debug($"[{session.LoggingIdentifier}] LoginRequest"); AuthenticationHandler.HandleLoginRequest(packet, session); return; } // This should be set on the second packet to the server from the client. // This completes the three-way handshake. if (packet.Header.HasFlag(PacketHeaderFlags.ConnectResponse)) { sendResync = true; AuthenticationHandler.HandleConnectResponse(packet, session); return; } // Process all fragments out of the packet foreach (ClientPacketFragment fragment in packet.Fragments) { ProcessFragment(fragment); } // Update the last received sequence. if (packet.Header.Sequence != 0) { lastReceivedPacketSequence = packet.Header.Sequence; } }