Skip to content

Instantly share code, notes, and snippets.

@xcvd
Created May 15, 2012 20:37
Show Gist options
  • Save xcvd/2704954 to your computer and use it in GitHub Desktop.
Save xcvd/2704954 to your computer and use it in GitHub Desktop.
Packet Sending
// -----------------------------------------------------------------------
// <copyright file="COutPacket.cs" company="Home">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace PacketSender
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// COutPacket struct. Type for maplestory outgoing packets.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct COutPacket
{
/// <summary>
/// Unknown attribute.
/// </summary>
public bool Loopback;
/// <summary>
/// Pointer to the packet buffer.
/// </summary>
public IntPtr BufferPointer;
/// <summary>
/// The length of the packet buffer.
/// </summary>
public int Length;
/// <summary>
/// Packet offset.
/// </summary>
public int Offset;
/// <summary>
/// Whether the packet is encrypted or not.
/// </summary>
public bool Encrypted;
}
}
// -----------------------------------------------------------------------
// <copyright file="MaplestoryNativeFunctions.cs" company="Home">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace PacketSender
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class MaplestoryNativeFunctions
{
/// <summary>
/// Address of the CClientSocket::SendPacket function.
/// </summary>
public static readonly IntPtr SendPacketAddress = (IntPtr)0x004E2C00;
/// <summary>
/// Gets SendPacket.
/// </summary>
public static readonly DSendPacket SendPacket =
(DSendPacket)Marshal.GetDelegateForFunctionPointer(SendPacketAddress, typeof(DSendPacket));
/// <summary>
/// Delegate type for CClientSocket::SendPacket function.
/// </summary>
/// <param name="this">
/// The this pointer (CClientSocket instance).
/// </param>
/// <param name="packetPointer">
/// The packet pointer.
/// </param>
/// <returns>
/// Unknown int.
/// </returns>
[UnmanagedFunctionPointer(CallingConvention.ThisCall, SetLastError = true)]
public delegate int DSendPacket(IntPtr @this, IntPtr packetPointer);
}
}
// -----------------------------------------------------------------------
// <copyright file="Packet.cs" company="Home">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace PacketSender
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Windows;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class Packet
{
/// <summary>
/// CClientSocket instance to send packets with.
/// </summary>
private static readonly IntPtr cclientSocketInstance = Marshal.ReadIntPtr((IntPtr)0x012EAB40);
/// <summary>
/// The packet bytes.
/// </summary>
private readonly byte[] packetBytes;
/// <summary>
/// Initializes a new instance of the <see cref="Packet"/> class.
/// </summary>
/// <param name="packetString">
/// The packet string.
/// </param>
public Packet(string packetString)
{
this.packetBytes = SoapHexBinary.Parse(packetString).Value;
}
/// <summary>
/// Sends the packet.
/// </summary>
public void Send()
{
unsafe
{
fixed (byte* packetBuffer = this.packetBytes)
{
var packet = new COutPacket
{
Length = this.packetBytes.Length,
BufferPointer = (IntPtr)packetBuffer,
Encrypted = false,
Loopback = false,
Offset = 0
};
var packetPointer = Marshal.AllocHGlobal(Marshal.SizeOf(packet));
Marshal.StructureToPtr(packet, packetPointer, false);
MaplestoryNativeFunctions.SendPacket(cclientSocketInstance, packetPointer);
Marshal.FreeHGlobal(packetPointer);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment