Skip to content

Server Query Protocol

fgsfds edited this page Jan 14, 2024 · 2 revisions

This only applies to the port-net branch.

It is possible to query game information from the server without connecting to it. This is done by sending a special UDP packet to the server, to the same port that is used for the game itself. For a basic example in Python, check out this script.

Request packet

"PDQM\x01" (bytes 50 44 51 4D 01)

Response packet

struct {
  char magic[5] = "PDQM\x01"; // same as query
  u16 size;                   // total size of this packet
  u32 protocol                // protocol version (NET_PROTOCOL_VER)
  u8 flags;                   // currently just 1 if game is in progress and 0 otherwise
  u8 numclients;              // currently connected clients
  u8 maxclients;              // max connected clients
  u8 stagenum;                // current stage number (see stagetable.txt)
  u8 scenario;                // combat sim scenario
  dstring hostname;           // nickname of hosting player
  dstring romname;            // ROM file name
  dstring moddir;             // mod directory if a mod is loaded, empty string otherwise
  u16 checksum;               // checksum of all the packet data before this point
};

where dstring is

u16 length;        // including null terminator
char data[length]; // null terminated

The checksum function used is a variant of CRC16:

u16 x;
u16 crc = 0xFFFF;
for (u32 i = 0; i < buf.wp; ++i) {
  x = crc >> 8 ^ buf.data[i];
  x ^= x >> 4;
  crc += (crc << 8) ^ (x << 12) ^ (x << 5) ^ x;
}