UPD Packet Header
The communication between the Final Fantasy XI client and server uses the UDP protocol. With this, a sub-protocol must be implemented to create any type of secure transmission that data has properly reached its destination. Final Fantasy XI accomplishes this by implementing a small header to every packet sent between the client and server. This header cotains the following information:
- Packet Id
- Packet Size
- Packet Sync
The packet id informs us of what this packet does. It is the unique identifier to this package.
The packet size informs us of how large the packets payload is.
The packet sync is the counter between the client and server that increments on successful transmissions of packets to help ensure packets are handled in order.
The packet layout can be seen like this:
|Packet Id||Size||Packet Sync||Packet Payload|
Packet ID and Size Information
Because packets have gotten over the id of 255, the game needed a way to manage packets over this id range. To remedy this, Final Fantasy XI packs the upper value of the packet id into the size for packets that go over the 255 id threshold. In order to obtain packets true id and size, we must do some minor bit shifting to obtain the proper information packed data. The following can be performed on any packet to obtain its true id and size:
* To obtain the packet id: (*(unsigned short*)packet) & 0x01FF * To obtain the packet size: packet size = *(unsigned char*)(packet + 1) & 0x0FE
Also, keep in mind that packet sizes are divided by two in the packet to account for space for packet ids that go over the 255 threshold. So when obtaining the packet size from the header, be sure to multiply it by two like this:
* To obtain the true packet size: (*(unsigned char*)(packet + 1) & 0x0FE) * 2