public override void Send(IServerPacket packet) { try { packet.Write(); packet.Packet.Finish(); if (packet.Packet.Header != null) { PacketLog.Write <ServerMessage>(packet.Packet.Header.Message, packet.Packet.Data, client.RemoteEndPoint); } if (Crypt != null && Crypt.IsInitialized) { Encrypt(packet.Packet); } var socketEventargs = new SocketAsyncEventArgs(); socketEventargs.SetBuffer(packet.Packet.Data, 0, packet.Packet.Data.Length); socketEventargs.Completed += SendCompleted; socketEventargs.UserToken = packet; socketEventargs.RemoteEndPoint = client.RemoteEndPoint; socketEventargs.SocketFlags = SocketFlags.None; client.SendAsync(socketEventargs); } catch (SocketException ex) { Log.Message(LogType.Error, "{0}", ex.Message); client.Close(); } }
public void Send(AuthPacket packet) { try { packet.Finish(); if (packet.Header != null) { PacketLog.Write <AuthServerMessage>(packet.Header.Message, packet.Data, client.RemoteEndPoint); } if (Crypt != null && Crypt.IsInitialized) { Crypt.Encrypt(packet.Data, packet.Data.Length); } var socketEventargs = new SocketAsyncEventArgs(); socketEventargs.SetBuffer(packet.Data, 0, packet.Data.Length); socketEventargs.Completed += SendCompleted; socketEventargs.UserToken = packet; socketEventargs.RemoteEndPoint = client.RemoteEndPoint; socketEventargs.SocketFlags = SocketFlags.None; client.Send(packet.Data); } catch (SocketException ex) { Log.Error($"{ex}"); client.Close(); } }
public override async Task ProcessPacket(Packet packet) { if (packetQueue.Count > 0) packet = packetQueue.Dequeue(); PacketLog.Write<ClientMessage>(packet.Header.Message, packet.Data, client.RemoteEndPoint); await PacketManager.InvokeHandler<ClientMessage>(packet, this); }
public void SendPacket(ServerPacket packet) { if (!IsOpen()) { return; } packet.LogPacket(_worldSession); packet.WritePacketData(); var data = packet.GetData(); ServerOpcodes opcode = packet.GetOpcode(); PacketLog.Write(data, (uint)opcode, GetRemoteIpAddress(), GetRemotePort(), _connectType, false); ByteBuffer buffer = new ByteBuffer(); int packetSize = data.Length; if (packetSize > 0x400 && _worldCrypt.IsInitialized) { buffer.WriteInt32(packetSize + 2); buffer.WriteUInt32(ZLib.adler32(ZLib.adler32(0x9827D8F1, BitConverter.GetBytes((ushort)opcode), 2), data, (uint)packetSize)); byte[] compressedData; uint compressedSize = CompressPacket(data, opcode, out compressedData); buffer.WriteUInt32(ZLib.adler32(0x9827D8F1, compressedData, compressedSize)); buffer.WriteBytes(compressedData, compressedSize); packetSize = (ushort)(compressedSize + 12); opcode = ServerOpcodes.CompressedPacket; data = buffer.GetData(); } buffer = new ByteBuffer(); buffer.WriteUInt16((ushort)opcode); buffer.WriteBytes(data); packetSize += 2 /*opcode*/; data = buffer.GetData(); PacketHeader header = new PacketHeader(); header.Size = packetSize; _worldCrypt.Encrypt(ref data, ref header.Tag); ByteBuffer byteBuffer = new ByteBuffer(); header.Write(byteBuffer); byteBuffer.WriteBytes(data); AsyncWrite(byteBuffer.GetData()); }
void ProcessPacket(Packet packet) { if (packetQueue.Count > 0) { packet = packetQueue.Dequeue(); } PacketLog.Write <ClientMessages>(packet.Header.Message, packet.Data, client.RemoteEndPoint); PacketManager.InvokeHandler(packet, this); }
void ProcessPacket(int size) { var packet = new AuthPacket(dataBuffer, size); PacketLog.Write <AuthClientMessage>(packet.Header.Message, packet.Data, client.RemoteEndPoint, packet.Header.Channel); if (packet != null) { PacketManager.InvokeHandler(packet, this); } }
public static uint ClientReceive(IntPtr netMessage, IntPtr arg2, IntPtr arg3, IntPtr dataPtr, UIntPtr length) { var pkt = new Packet(dataPtr, (int)(length.ToUInt64() & 0xFFFFFFFF)); PacketLog.Write(pkt, "ServerMessage"); Memory.Write(originalFunction, originalInstruction); var ret = (uint)originalDelegate.DynamicInvoke(new object[] { netMessage, arg2, arg3, dataPtr, length }); Memory.Write(originalFunction, hookInstruction); return(ret); }
public static uint ClientSend(IntPtr ptr, ref CDataStore dataStore, int args) { var ds = dataStore.Clone(); var pkt = new Packet(ds); PacketLog.Write(pkt, "ClientMessage"); Memory.Write(originalFunction, originalInstruction); var ret = (uint)originalDelegate.DynamicInvoke(new object[] { ptr, dataStore, args }); Memory.Write(originalFunction, hookInstruction); return((uint)ret); }
void ProcessPacket(int size) { var packet = new AuthPacket(dataBuffer, size); PacketLog.Write <AuthClientMessage>(packet.Header.Message, packet.Data, client.RemoteEndPoint); if (packet != null) { var currentClient = Manager.SessionMgr.Clients.AsParallel().SingleOrDefault(c => c.Value.Session.Equals(this)); if (currentClient.Value != null) { PacketManager.InvokeHandler(packet, currentClient.Value); } } }
public override async Task Send(Framework.Network.Packets.ServerPacket packet) { try { packet.Write(); packet.Packet.Finish(); if (packet.Packet.Header != null) { if (packet.Packet.Header.Size > 0x100) { packet = await Compress(packet); } PacketLog.Write <ServerMessage>(packet.Packet.Header.Message, packet.Packet.Data, client.RemoteEndPoint); } if (Crypt != null && Crypt.IsInitialized) { Encrypt(packet.Packet); } var socketEventargs = new SocketAsyncEventArgs(); socketEventargs.SetBuffer(packet.Packet.Data, 0, packet.Packet.Data.Length); if (!client.Connected) { return; } socketEventargs.Completed += SendCompleted; socketEventargs.UserToken = packet; socketEventargs.RemoteEndPoint = client.RemoteEndPoint; socketEventargs.SocketFlags = SocketFlags.None; client.SendAsync(socketEventargs); } catch (Exception ex) { Dispose(); ExceptionLog.Write(ex); Log.Error(ex.ToString()); } }
public override void ReadHandler(int transferredBytes) { if (!IsOpen()) { return; } while (transferredBytes > 5) { PacketHeader header; if (!ReadHeader(out header)) { CloseSocket(); return; } var data = new byte[header.Size]; Buffer.BlockCopy(GetReceiveBuffer(), 16, data, 0, header.Size); if (!_worldCrypt.Decrypt(ref data, header.Tag)) { Log.outError(LogFilter.Network, $"WorldSocket.ReadHandler(): client {GetRemoteIpAddress().ToString()} failed to decrypt packet (size: {header.Size})"); return; } WorldPacket worldPacket = new WorldPacket(data); if (worldPacket.GetOpcode() >= (int)ClientOpcodes.Max) { Log.outError(LogFilter.Network, $"WorldSocket.ReadHandler(): client {GetRemoteIpAddress().ToString()} sent wrong opcode (opcode: {worldPacket.GetOpcode()})"); return; } PacketLog.Write(data, worldPacket.GetOpcode(), GetRemoteIpAddress(), GetRemotePort(), _connectType, true); if (!ProcessPacket(worldPacket)) { CloseSocket(); return; } transferredBytes -= header.Size + 16; Buffer.BlockCopy(GetReceiveBuffer(), header.Size + 16, GetReceiveBuffer(), 0, transferredBytes); } AsyncRead(); }
public void Send(Packet packet) { try { packet.Finish(); if (packet.Header != null) { PacketLog.Write <ServerMessages>(packet.Header.Message, packet.Data, client.RemoteEndPoint); } if (Crypt != null && Crypt.IsInitialized) { uint totalLength = (uint)packet.Header.Size - 2; totalLength <<= 13; totalLength |= ((uint)packet.Header.Message & 0x1FFF); var header = BitConverter.GetBytes(totalLength); Crypt.Encrypt(header, 4); packet.Data[0] = header[0]; packet.Data[1] = header[1]; packet.Data[2] = header[2]; packet.Data[3] = header[3]; } var socketEventargs = new SocketAsyncEventArgs(); socketEventargs.SetBuffer(packet.Data, 0, packet.Data.Length); socketEventargs.Completed += SendCompleted; socketEventargs.UserToken = packet; socketEventargs.RemoteEndPoint = client.RemoteEndPoint; socketEventargs.SocketFlags = SocketFlags.None; client.SendAsync(socketEventargs); } catch (SocketException ex) { Log.Message(LogType.Error, "{0}", ex.Message); client.Close(); } }
public void SendPacket(ServerPacket packet) { if (!IsOpen()) { return; } packet.LogPacket(_worldSession); packet.WritePacketData(); var data = packet.GetData(); uint packetSize = (uint)data.Length; ServerOpcodes opcode = packet.GetOpcode(); PacketLog.Write(data, opcode, GetRemoteIpAddress(), GetRemotePort(), _connectType); if (packetSize > 0x400 && worldCrypt.IsInitialized) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteUInt32(packetSize + 2); buffer.WriteUInt32(ZLib.adler32(ZLib.adler32(0x9827D8F1, BitConverter.GetBytes((ushort)opcode), 2), data, packetSize)); uint compressedSize = CompressPacket(data, opcode); buffer.WriteUInt32(ZLib.adler32(0x9827D8F1, data, compressedSize)); buffer.WriteBytes(data, compressedSize); packetSize = (ushort)(compressedSize + 12); opcode = ServerOpcodes.CompressedPacket; data = buffer.GetData(); } ServerPacketHeader header = new ServerPacketHeader(packetSize, opcode); if (worldCrypt.IsInitialized) { worldCrypt.Encrypt(header.data, 4); } AsyncWrite(header.data.Combine(data)); }
public override void ReadHandler(int transferredBytes) { if (!IsOpen()) { return; } while (transferredBytes > 5) { if (worldCrypt.IsInitialized) { worldCrypt.Decrypt(GetReceiveBuffer(), 4); } int size; uint opcode; if (!ReadHeader(out opcode, out size)) { CloseSocket(); return; } var data = new byte[size]; Buffer.BlockCopy(GetReceiveBuffer(), 6, data, 0, size); PacketLog.Write(data, opcode, GetRemoteIpAddress(), GetRemotePort(), _connectType, true); if (!ProcessPacket(new WorldPacket(data, opcode))) { CloseSocket(); return; } transferredBytes -= size + 6; Buffer.BlockCopy(GetReceiveBuffer(), size + 6, GetReceiveBuffer(), 0, transferredBytes); } AsyncRead(); }
bool ProcessPacket(WorldPacket packet) { ClientOpcodes opcode = (ClientOpcodes)packet.GetOpcode(); PacketLog.Write(packet.GetData(), opcode, GetRemoteIpAddress(), GetRemotePort(), _connectType); try { switch (opcode) { case ClientOpcodes.Ping: Ping ping = new Ping(packet); ping.Read(); return(HandlePing(ping)); case ClientOpcodes.AuthSession: if (_worldSession != null) { Log.outError(LogFilter.Network, "WorldSocket.ProcessPacket: received duplicate CMSG_AUTH_SESSION from {0}", _worldSession.GetPlayerInfo()); return(false); } AuthSession authSession = new AuthSession(packet); authSession.Read(); HandleAuthSession(authSession); break; case ClientOpcodes.AuthContinuedSession: if (_worldSession != null) { Log.outError(LogFilter.Network, "WorldSocket.ProcessPacket: received duplicate CMSG_AUTH_CONTINUED_SESSION from {0}", _worldSession.GetPlayerInfo()); return(false); } AuthContinuedSession authContinuedSession = new AuthContinuedSession(packet); authContinuedSession.Read(); HandleAuthContinuedSession(authContinuedSession); break; case ClientOpcodes.LogDisconnect: break; case ClientOpcodes.EnableNagle: Log.outDebug(LogFilter.Network, "Client {0} requested enabling nagle algorithm", GetRemoteIpAddress().ToString()); SetNoDelay(false); break; case ClientOpcodes.ConnectToFailed: ConnectToFailed connectToFailed = new ConnectToFailed(packet); connectToFailed.Read(); HandleConnectToFailed(connectToFailed); break; case ClientOpcodes.EnableEncryptionAck: HandleEnableEncryptionAck(); break; default: if (_worldSession == null) { Log.outError(LogFilter.Network, "ProcessIncoming: Client not authed opcode = {0}", opcode); return(false); } if (!PacketManager.ContainsHandler(opcode)) { Log.outError(LogFilter.Network, "No defined handler for opcode {0} sent by {1}", opcode, _worldSession.GetPlayerInfo()); break; } // Our Idle timer will reset on any non PING opcodes. // Catches people idling on the login screen and any lingering ingame connections. _worldSession.ResetTimeOutTime(); _worldSession.QueuePacket(packet); break; } } catch (IOException) { Log.outError(LogFilter.Network, "WorldSocket.ProcessPacket(): client {0} sent malformed {1}", GetRemoteIpAddress().ToString(), opcode); return(false); } return(true); }
ReadDataHandlerResult ReadData() { PacketHeader header = new PacketHeader(); header.Read(_headerBuffer.GetData()); if (!_worldCrypt.Decrypt(_packetBuffer.GetData(), header.Tag)) { Log.outError(LogFilter.Network, $"WorldSocket.ReadData(): client {GetRemoteIpAddress()} failed to decrypt packet (size: {header.Size})"); return(ReadDataHandlerResult.Error); } WorldPacket packet = new WorldPacket(_packetBuffer.GetData()); _packetBuffer.Reset(); if (packet.GetOpcode() >= (int)ClientOpcodes.Max) { Log.outError(LogFilter.Network, $"WorldSocket.ReadData(): client {GetRemoteIpAddress()} sent wrong opcode (opcode: {packet.GetOpcode()})"); return(ReadDataHandlerResult.Error); } PacketLog.Write(packet.GetData(), packet.GetOpcode(), GetRemoteIpAddress(), _connectType, true); ClientOpcodes opcode = (ClientOpcodes)packet.GetOpcode(); switch (opcode) { case ClientOpcodes.Ping: Ping ping = new Ping(packet); ping.Read(); if (!HandlePing(ping)) { return(ReadDataHandlerResult.Error); } break; case ClientOpcodes.AuthSession: if (_worldSession != null) { Log.outError(LogFilter.Network, $"WorldSocket.ReadData(): received duplicate CMSG_AUTH_SESSION from {_worldSession.GetPlayerInfo()}"); return(ReadDataHandlerResult.Error); } AuthSession authSession = new AuthSession(packet); authSession.Read(); HandleAuthSession(authSession); return(ReadDataHandlerResult.WaitingForQuery); case ClientOpcodes.AuthContinuedSession: if (_worldSession != null) { Log.outError(LogFilter.Network, $"WorldSocket.ReadData(): received duplicate CMSG_AUTH_CONTINUED_SESSION from {_worldSession.GetPlayerInfo()}"); return(ReadDataHandlerResult.Error); } AuthContinuedSession authContinuedSession = new AuthContinuedSession(packet); authContinuedSession.Read(); HandleAuthContinuedSession(authContinuedSession); return(ReadDataHandlerResult.WaitingForQuery); case ClientOpcodes.KeepAlive: if (_worldSession != null) { _worldSession.ResetTimeOutTime(true); } break; case ClientOpcodes.LogDisconnect: break; case ClientOpcodes.EnableNagle: SetNoDelay(false); break; case ClientOpcodes.ConnectToFailed: ConnectToFailed connectToFailed = new ConnectToFailed(packet); connectToFailed.Read(); HandleConnectToFailed(connectToFailed); break; case ClientOpcodes.EnterEncryptedModeAck: HandleEnterEncryptedModeAck(); break; default: if (_worldSession == null) { Log.outError(LogFilter.Network, $"ProcessIncoming: Client not authed opcode = {opcode}"); return(ReadDataHandlerResult.Error); } if (!PacketManager.ContainsHandler(opcode)) { Log.outError(LogFilter.Network, $"No defined handler for opcode {opcode} sent by {_worldSession.GetPlayerInfo()}"); break; } // Our Idle timer will reset on any non PING opcodes on login screen, allowing us to catch people idling. _worldSession.ResetTimeOutTime(false); // Copy the packet to the heap before enqueuing _worldSession.QueuePacket(packet); break; } return(ReadDataHandlerResult.Ok); }