Added decompression/compression of packets. Cleaned up handshaking.

This commit is contained in:
Filip Maj 2016-08-24 15:41:54 -04:00
parent 4aae16e458
commit 364ab40b3f
8 changed files with 123 additions and 43 deletions

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using NLog; using NLog;
using NLog.Targets; using NLog.Targets;
using Ionic.Zlib;
namespace FFXIVClassic.Common namespace FFXIVClassic.Common
{ {
@ -346,6 +347,28 @@ namespace FFXIVClassic.Common
} }
} }
public static unsafe void DecompressPacket(ref BasePacket packet)
{
using (var compressedStream = new MemoryStream(packet.data))
using (var zipStream = new ZlibStream(compressedStream, Ionic.Zlib.CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
packet.data = resultStream.ToArray();
}
}
public static unsafe void CompressPacket(ref BasePacket packet)
{
using (var compressedStream = new MemoryStream(packet.data))
using (var zipStream = new ZlibStream(compressedStream, Ionic.Zlib.CompressionMode.Compress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
packet.data = resultStream.ToArray();
}
}
#endregion #endregion
} }

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="DotNetZip" version="1.10.1" targetFramework="net45" />
<package id="MySql.Data" version="6.9.8" targetFramework="net45" /> <package id="MySql.Data" version="6.9.8" targetFramework="net45" />
<package id="NLog" version="4.3.5" targetFramework="net45" /> <package id="NLog" version="4.3.5" targetFramework="net45" />
</packages> </packages>

View File

@ -11,16 +11,17 @@ namespace FFXIVClassic_World_Server.DataObjects
{ {
public enum Channel {ZONE, CHAT}; public enum Channel {ZONE, CHAT};
public readonly ulong sessionId; public readonly uint sessionId;
public readonly ClientConnection clientSocket; public readonly ClientConnection clientConnection;
public readonly Channel type; public readonly Channel type;
public ZoneServer routing1, routing2; public ZoneServer routing1, routing2;
public Session(ulong sessionId, ClientConnection socket, Channel type) public Session(ulong sessionId, ClientConnection connection, Channel type)
{ {
this.sessionId = sessionId; this.sessionId = sessionId;
this.clientSocket = socket; this.clientConnection = connection;
this.type = type; this.type = type;
connection.owner = this;
} }
} }

View File

@ -67,6 +67,9 @@
<Compile Include="DataObjects\ClientConnection.cs" /> <Compile Include="DataObjects\ClientConnection.cs" />
<Compile Include="DataObjects\ZoneServer.cs" /> <Compile Include="DataObjects\ZoneServer.cs" />
<Compile Include="DataObjects\Session.cs" /> <Compile Include="DataObjects\Session.cs" />
<Compile Include="PacketProcessor.cs" />
<Compile Include="Packets\Receive\HelloPacket.cs" />
<Compile Include="Packets\Send\0x7ResponsePacket.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" /> <Compile Include="Server.cs" />

View File

@ -1,11 +1,11 @@
using FFXIVClassic.Common; using FFXIVClassic.Common;
using FFXIVClassic_World_Server.DataObjects; using FFXIVClassic_World_Server.DataObjects;
using FFXIVClassic_World_Server.Packets.Receive; using FFXIVClassic_World_Server.Packets.Receive;
using FFXIVClassic_World_Server.Packets.Send;
using FFXIVClassic_World_Server.Packets.Send.Login; using FFXIVClassic_World_Server.Packets.Send.Login;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
namespace FFXIVClassic_World_Server namespace FFXIVClassic_World_Server
{ {
@ -27,7 +27,6 @@ namespace FFXIVClassic_World_Server
Server mServer; Server mServer;
List<ClientConnection> mConnections;
public PacketProcessor(Server server) public PacketProcessor(Server server)
{ {
@ -36,8 +35,8 @@ namespace FFXIVClassic_World_Server
public void ProcessPacket(ClientConnection client, BasePacket packet) public void ProcessPacket(ClientConnection client, BasePacket packet)
{ {
//if (packet.header.isCompressed == 0x01) if (packet.header.isCompressed == 0x01)
// BasePacket.DecryptPacket(client.blowfish, ref packet); BasePacket.DecompressPacket(ref packet);
List<SubPacket> subPackets = packet.GetSubpackets(); List<SubPacket> subPackets = packet.GetSubpackets();
foreach (SubPacket subpacket in subPackets) foreach (SubPacket subpacket in subPackets)
@ -45,48 +44,20 @@ namespace FFXIVClassic_World_Server
//Initial Connect Packet, Create session //Initial Connect Packet, Create session
if (subpacket.header.type == 0x01) if (subpacket.header.type == 0x01)
{ {
#region Hardcoded replies
packet.DebugPrintPacket();
byte[] reply1Data = {
0x01, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFD, 0xFF, 0xFF,
0xE5, 0x6E, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x0
};
byte[] reply2Data = {
0x01, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x38, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x2B, 0x5F, 0x26,
0x66, 0x00, 0x00, 0x00, 0xC8, 0xD6, 0xAF, 0x2B, 0x38, 0x2B, 0x5F, 0x26, 0xB8, 0x8D, 0xF0, 0x2B,
0xC8, 0xFD, 0x85, 0xFE, 0xA8, 0x7C, 0x5B, 0x09, 0x38, 0x2B, 0x5F, 0x26, 0xC8, 0xD6, 0xAF, 0x2B,
0xB8, 0x8D, 0xF0, 0x2B, 0x88, 0xAF, 0x5E, 0x26
};
BasePacket reply1 = new BasePacket(reply1Data);
BasePacket reply2 = new BasePacket(reply2Data);
//Write Timestamp into Reply1
using (MemoryStream mem = new MemoryStream(reply1.data))
{
using (BinaryWriter binReader = new BinaryWriter(mem))
{
binReader.BaseStream.Seek(0x14, SeekOrigin.Begin);
binReader.Write((UInt32)Utils.UnixTimeStampUTC());
}
}
#endregion
HelloPacket hello = new HelloPacket(packet.data); HelloPacket hello = new HelloPacket(packet.data);
if (packet.header.connectionType == BasePacket.TYPE_ZONE) if (packet.header.connectionType == BasePacket.TYPE_ZONE)
mServer.AddSession(client, Session.Channel.ZONE, hello.sessionId); mServer.AddSession(client, Session.Channel.ZONE, hello.sessionId);
else if (packet.header.connectionType == BasePacket.TYPE_CHAT) else if (packet.header.connectionType == BasePacket.TYPE_CHAT)
mServer.AddSession(client, Session.Channel.CHAT, hello.sessionId); mServer.AddSession(client, Session.Channel.CHAT, hello.sessionId);
client.QueuePacket(_0x7Packet.BuildPacket(0x0E016EE5), true, false);
client.QueuePacket(_0x2Packet.BuildPacket(hello.sessionId), true, false);
} }
//Ping from World Server //Ping from World Server
else if (subpacket.header.type == 0x07) else if (subpacket.header.type == 0x07)
{ {
SubPacket init = Login0x7ResponsePacket.BuildPacket(0x50); SubPacket init = _0x8PingPacket.BuildPacket(client.owner.sessionId);
client.QueuePacket(BasePacket.CreatePacket(init, true, false)); client.QueuePacket(BasePacket.CreatePacket(init, true, false));
} }
//Zoning Related //Zoning Related

View File

@ -0,0 +1,44 @@
using FFXIVClassic.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.Packets.Send
{
class _0x2Packet
{
public const ushort OPCODE = 0x0002;
public const uint PACKET_SIZE = 0x38;
public static SubPacket BuildPacket(uint actorID)
{
byte[] data = new byte[PACKET_SIZE];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((UInt32)actorID);
}
catch (Exception)
{ }
}
}
byte[] reply2Data = {
0x66, 0x00, 0x00, 0x00, 0xC8, 0xD6, 0xAF, 0x2B, 0x38, 0x2B, 0x5F, 0x26, 0xB8, 0x8D, 0xF0, 0x2B,
0xC8, 0xFD, 0x85, 0xFE, 0xA8, 0x7C, 0x5B, 0x09, 0x38, 0x2B, 0x5F, 0x26, 0xC8, 0xD6, 0xAF, 0x2B,
0xB8, 0x8D, 0xF0, 0x2B, 0x88, 0xAF, 0x5E, 0x26
};
data = reply2Data;
return new SubPacket(false, OPCODE, 0, 0, data);
}
}
}

View File

@ -0,0 +1,37 @@
using FFXIVClassic.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.Packets.Send
{
class _0x7Packet
{
public const ushort OPCODE = 0x0007;
public const uint PACKET_SIZE = 0x18;
public static SubPacket BuildPacket(uint actorID)
{
byte[] data = new byte[PACKET_SIZE];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((UInt32)actorID);
binWriter.Write((UInt32)Utils.UnixTimeStampUTC());
}
catch (Exception)
{ }
}
}
return new SubPacket(false, OPCODE, 0, 0, data);
}
}
}

View File

@ -4,7 +4,7 @@ using System.IO;
namespace FFXIVClassic_World_Server.Packets.Send.Login namespace FFXIVClassic_World_Server.Packets.Send.Login
{ {
class Login0x7ResponsePacket class _0x8PingPacket
{ {
public const ushort OPCODE = 0x0008; public const ushort OPCODE = 0x0008;
public const uint PACKET_SIZE = 0x18; public const uint PACKET_SIZE = 0x18;