Crossfire Protocol Handbook =========================== Crossfire Development Team :numbered: :toc: This handbook is the collective result of various authors compiled together over the course of many years, most of the time after the actual code was already written (for a long time). As such it will surely contain errors, omit certain important details, and possibly make life miserable. Nevertheless, any new developer or curious player should be able to find most of the relevant information here. If inconsistencies are found or this documentation proves to be inadequate, consider the latest server-side protocol code in the public source code repository as the authoritative reference. Introduction ------------ If you were ever curious enough to `telnet` or `netcat` to a Crossfire server, chances are you were sorely disappointed. While the protocol may seem to use plain text at first, it actually uses a mix of ASCII and binary data. This handbook attempts to document various aspects of the Crossfire protocol. As always, consult the 'README' file to find out how to get in touch with helpful people via mailing lists, IRC, and more. History ~~~~~~~ Originally, the communications plan was set to be a text based system. It was up to the server and client to parse these messages and determine what to do. These messages were assumed to be 1 line per message. At a reasonably early stage of development, Eric Anderson wrote a fairly (but not totally) complete client/server that used his eutl package. This package pretty much set up packets with subpackets - these subpackets would have a tag for the data type, then the data itself. Thus, you could send many data types, and after transmission, the other end could decode these commands. This works fairly well, but I think the creation of numerous sub packets has some performance hit. Also, the eutl was not especially well documented, so writing a client for a different platform became more difficult (you needed to first port over eutl.) An example such of this is the Java client. Also, Eric left to work on other products shortly after writing his client, which didn't really leave anyone with a full understanding of the socket code. I have decided to remove the eutl dependency. At least one advantage is that having this network related code directly in the client and server makes error handling a bit easier/cleaner. Packet Format ~~~~~~~~~~~~~ IMPORTANT: Instead of a straight text method, the outside packet method is: The is the size of the data packet, the 2 byte size for the size information is not included here. Eutl originally used 4 bytes for the size - to me, 2 bytes seems plenty (gives a maximum packet of 32767 - I can't see ever going beyond a few thousand, simply because in a fast action game, transmission size of such a packet would probably not make things playable.) While saving 2 bytes might not be much, it makes a least some sense. The actual data is something of the nature of the commands listed below. It is a text command, followed by possible other data. The remaining data can be binary - it is up to the client and server to decode what it sent. The commands as described below is just the data portion of the packet. If writing a new client, remember that you must take into account the size of the packet. There is no termination of packets, other than knowing how long it should be. For now, most everything that is sent is text. This is more or less how things worked under eutl, except it packed the ints into 4 bytes in a known order. In some cases, we handle ints as strings, in others, they are sent as binary information. How any command handles it is detailed below in the command description. The S and C represent the direction of the data (S->C represents something the server sends to the client, C->S represents something the client sends to the server.) In terms of all binary values, we use MSB order (same as eutl used). MSB order is also network byte order. This includes the initial length information, as well as any ints or shorts that get sent inside the packets. All packets are defined to have at least one word of text, followed by a space, then followed by optional data (which can be binary.) NOTE: Generally, the data the client sends to the server is text, but a fair amount of data the server sends to the client is binary. This has somewhat to do with who wrote what code, and also has to do that the S->C bandwidth is going to more the more serious limitation - the client generally won't be sending so much data that the its flow is much problem. Note that all the commands as detailed below are up to date descriptions I removed a lot of the old notes on this file, because they were out of date, and while might be good ideas, were not all that relevant to how things currently work. Summary: Packets sent back and forth have a 2 byte header (MSB order) which contains the length of the rest of the packet. Implementation Notes ~~~~~~~~~~~~~~~~~~~~ We are using a TCP/IP socket. Other methods could be used, but the present protocol does not make very good provisions for missing data, so it needs to be something that corrects errors/does resends automatically (or just doesn't get errors in the first place.) For now, we set non blocking output on the server side. This means we don't have to worry about internal buffering. If the connection is lost (which will also happen if the output buffer overflowing), the player is saved and the server cleans up. This does open up some abuses, but there is no perfect solution here. The server only reads data from the socket if the player has an action. This isn't really good, since many of the commands below might not be actual commands for the player. The alternative is to look at the data, and if it is a player command and there isn't time, store it away to be processed later. But this increases complexity, in that the server must start buffering the commands. Fortunately, for now, there are few such client commands. If it becomes a case where the client is requesting images/sounds, dual channels could probably be used, since requesting that data is not related to the actual playing of the game (or a special daemon that serves those requests could also be done.) Better solution might be for that data to be on a separate server that can be downloaded by more efficient mechanisms (eg, ftp) SUMMARY: TCP/IP sockets are used for exchange data. Server uses non blocking i/o when writing to the socket, and the server only reads from the socket when the player actually has time for an action. Data Type Conventions ~~~~~~~~~~~~~~~~~~~~~ In the listing of the protocol commands below, different terms are used to describe the data. This section is used to clarify what the different types are: string: A set of ASCII characters. If a protocol command has several string fields, then spaces are used to separate the strings. length prefixed string: An 8 bit length field prepends the string. EG: length prefixed strings are often used in cases where the string contains space, and the protocol command includes several strings or there are other fields following the string. The receiving end can decode the length, know how much data to copy and where the start of the next field is. string value of integer data: This notes that the data is stored on the server (and client) as integer data, but it is converted to string data before sending (eg, sprintf(buf,"%d", val)). It means that the receiver will need to use atoi or the like to convert it back into the appropriate type. binary X bit value (also X bit binary value): This notes that the data is sent as raw binary data of the given size. shorts would be 16 bit, ints 32 bit, long longs 64 bit, etc. Commands -------- Object tags: Many of the commands below refer to 'object tags'. Whenever the server creates an object, it creates a unique tag for that object (starting at 1 when the server is first run, and ever increasing.) Tags are unique, but are not consistent between runs. Thus, the client can not store tags when it exits and hope to re-use them when it joins the server at a later time - tags are only valid for the current connection. The protocol commands are broken into various sections which based somewhat on what the commands are for (ie, item related commands, map commands, image commands, etc.) In this way, all the commands related to similar functionality is in the same place. General Commands ~~~~~~~~~~~~~~~~ failure ^^^^^^^ S->C: failure All fields are strings. is the command (just the command portion, eg, version, accountlogin, not with any additional data) that generated the failure. is a message which notes the failure - it will be displayed to the player, so should be informative to the player so they can take corrective action (omit illegal characters from their username, etc) This command is used to denote failure in some way (as detailed in failure string) of a protocol command. This does not mean that there was an error in the protocol (which typically results in a command being dropped), but rather that the server was unable to complete the given protocol request. As of this writing (Jan 2010), this is used only in the account login code - if the user enters incorrect login information, it will generate a failure message. The use of this protocol command, and it including the protocol command which failed, better informs the client it needs to retry. For example, if the user enters wrong account information, the server will send `failure accountlogin wrong user name or password`. With this, the client can display the message in the correct location, but also knows it needs to go back to the point of asking for login information. draw_info provides no mechanism to tell the client to retry an event. This command is really meant for areas where the client is interacting with the users. This command is not especially good in cases where the client has sent several protocol commands of the same type - this command will only tell that a command of that name failed, but may not include enough information to note which of several commands failed. Note that the format of message, while text data, can contain other information. In particular, some messages may have the first 'word' be a number which denotes some type of status - this will be detailed in the protocol area for that command. For example: failure accountaddplayer 0 That name is too long failure accountaddplayer 1 That character is associated with another account The client should intercept that number and not display it, but this may indicate that the command is retriable by changing a parameter - in the case of accountaddplayer, if force is set, the second message there is not an error. version ^^^^^^^ C->S: version [scval [vinfo]] S->C: version [scval [vinfo]] All fields are strings. Through the version command, the client and server exchange what version of the protocol they understand. Neither send this in response to the other - they should both send this shortly after a connection is established. csval is the version level of C->S communications. scval is the version level of S->C communications. vinfo is a string that is purely for informative that general client/server info (ie, javaclient, x11client, winclient, sinix server, etc). It is purely of interest of server admins who can see what type of clients people are using. If a new command is added to the protocol in the C->S direction, then the version number in csval will get increased. Likewise, the same is true for the scval. As far as the client is concerned, its scval must be at least equal to the server, and its csval should not be newer than the server. The server does not care about the version command it receives right now - all it currently does is log mismatches. In theory, the server should keep track of what the client has, and adjust the commands it sends respectively in the S->C direction. The server is resilant enough that it won't crash with a version mismatch (however, client may end up sending commands that the server just ignores). It is really up to the client to enforce versioning and quit if the versions don't match. scval and vinfo was added starting in 1020. Before that version, there was only one version sent in the version command. The version are currently integers, in the form ABCD. A = 1, and will likely for quite a while. This will only really change if needed from rollover of B. B represents major protocol changes - if B mismatches, the clients will be totally unusable. Such an example would be change of map or item sending commands (either new commands or new format.) C represents more minor but still significant changes - clients might still work together, but some features that used to work may now fail due to the mismatch. An example may be a change in the meaning of some field in some command - providing the field is the same size, it still should be decoded properly, but the meaning won't be processed properly. D represents very minor changes or new commands. Things should work no worse if D does not match, however if they do match, some new features might be included. An example of the would be the C->S mark command to mark items. Server not understanding this just means that the server can not process it, and will ignore it. NOTE: Since all packets have the length as the first 2 bytes, all that either the client or server needs to be able to do is look at the first string and see if it understands it. If not, it knows how many bytes it can skip. As such, exact version matches should not be necessary for proper operation - however, both the client and server needs to be coded to handle such cases. NOTE: For the most part, this has been obsoleted by the setup command which always return status and whether it understood the command or not. However there are still some cases where using this versioning is useful - an example it the addition of the requestinfo/replyinfo commands - the client wants to wait for acknowledge of all the replyinfo commands it has issued before sending the addme command. However, if the server doesn't understand these options, the client will never get a response. With the versioning, the client can look at the version and know if it should wait for a response or if the server will never send back. accountlogin and accountnew ^^^^^^^^^^^^^^^^^^^^^^^^^^^ C->S: accountlogin C->S: accountnew S->C: failure is a length prefixed string of the account name. is a length prefixed string of the password. Under new login support (loginmethod >=1) the client will query the user for account information - this sends that information to the server. In the first place, the user is trying to log in with an established account, in the second, the user is trying to create a new account. It is up to the client to determine what is trying to be done - in general, this means that player should be hitting a 'new account' type button or a the like. It is up to the client to do double confirmation of passwords, etc. account name and account password are strings, with the first byte being the length of the string. If the login or creation fails, a failure protocol message is generated. That is documented up above. The client should retry in that case. accountplayers ^^^^^^^^^^^^^^ S->C: accountplayers ... [options="autowidth,header"] |=========================== |Field| Description || 8 bit binary value noting how many characters on this account. It may be zero. || length prefixed data on character. The ACL_... values in newclient.h define these. The format of each data value is: is an 8 bit value which is the length of type + value - in this way, the client can skip over the entire field in cases where it does not know the type. If len is 0, it notes that there is no more information for this character. is an 8 bit value which is an ACL_ type value. || type specific - if a string value, it is just a string value. If binary, its length can be determined. Except for noted values, all value types are strings. Exceptions: ACL_LEVEL:: is a 16 bit binary value. ACL_FACE_NUM:: is a 16 bit binary value. |=========================== NOTE: There is no requirement that the server sends all fields (may choose to not send empty fields.) Likewise, there is no requirement they get sent in a specific order. This is a list of the players (characters) associated with this account. In the case of new accounts, the num characters will be zero. This is sent after the server receives a successful login or accountnew request. The client can thus use the receipt of this command, even if empty, to note that the user has logged in. accountplay ^^^^^^^^^^^ C->S: accountplay string value of the character to play. Play character of the given name. This must be one of the character names returned by the accountplayers command - otherwise it will generate an failure protocol command. On choosing a character, the game will log in and play will start. Upon receipt of this command, the server will do necessary setup to start play. This removes the need for the addme command below. accountaddplayer ^^^^^^^^^^^^^^^^ C->S: accountaddplayer - 8 bit binary data to force this association (see description below. This is not an optional field - it must always be sent. length prefixed string of the character name. length prefixed string of the characters password. Add a character to the logged in account. Both the name and password are needed. In the event that the given character is already associated with an account (and not this one), a failure message will be generated unless force is on. The flow in that case would be user enter name/password and hitting submit, client sends to server, server sends failure event to client saying character is already associated with account, client asks player if he wants to override that setting, and if they hit yes, command is then sent with force set to 1. On success, the server will send a new accountplayers command, with all characters for this account. On failure, a failure message will be generated. The failure message for accountaddplayer does include the optional integer parameter - if 0, the command is not retriable - if set to 1, then this account may succeed if force is set. createplayer ^^^^^^^^^^^^ C->S: createplayer The player wants to make a new player - this is done on the client - this basically tells the server to send the client information regarding making new characters (races, classes, etc). Loginmethod 1 supports following: : Length prefixed string - desired character name. : Length prefixed string - password Loginmethod 2 adds the following (each of these blocks are length prefixed so the server can easily skip lines). To make processing simpler, the null byte is included in the processing here. race class (eg, Str 18) starting_map choice The client should query the player for these values through some method. The statvalues that are sent are the values _before_ race and class adjustments. The for starting_map is the value of INFO_MAP_ARCH_NAME as returned by the 'startingmap' requestinfo. The choice values are included in the race/class information as retrieved by requestinfo. If the server is providing choices, it must also accept them in return. createplayer can return failure in case of invalid name, name already in use, invalid race or class, stat value at of range, sum of stats too high, etc.etc. NOTE: Expected that this command will expand with new character creation method. accountpw ^^^^^^^^^ C->S: accountpw S->C: failure accountpw is a length prefixed string of the current password. is a length prefixed string of the new password. Tells the server to change the password for the current account. A failure message is generated if something is wrong. If all goes well the server will send the character list for the account. addme ^^^^^ C->S: addme Tells the server that it should add me (the client) to the game. Generally, the client will always send this command, but I suppose there can be actions the client wants to do before being added. Deprecated - this has been superseded by the accountplay command. S->C: addme_failed S->C: addme_success This are responses to the last addme command. I really think these should be discontinued, as they are one of the few messages which is just a confirmation of a previous message. The addme_failed should really be replaced with a terminate type of of message (player quits game, server could inform us nicely and exit out). addme_success is really of no use - client just throws it away. NOTE: With account based login method, server will send the addme_success in certain circumstances - this is really to tell the client that play has started and hide any login/character selection information. However the note above still applies - addme_failed could get replaced by a 'failure addme ...' type response, and there is probably some use for having a 'success' type protocol response to tell the client that something has worked - right now, this is done by the client seeing the next message in a protocol state (eg, accountplayers follows an accountlogin request) goodbye ^^^^^^^ S->C: goodbye (Added in SC protocol version 1022) Informs the client that the server has finished transmitting data to the client. This is a bit cleaner than the client detecting a read error. In theory, a C->S of the same type could be done, but I don't think it would make a big difference for the server (is going to do the same thing regardless of a clean connection drop or a bad one). Also see the `setfacemode` command below. Info Window Input/Output ~~~~~~~~~~~~~~~~~~~~~~~~ C->S: ncom (ncom = new command) is a 16 bit binary value the server will send back through comc to inform the client of execution of this command. It is up to the client to determine how it wants to number these, but incrementing by one is the easiest way. is a 32 bit binary value and is the repeat value, or a count value used for instance for dropping. string value of the actual command data (north, whatever). Client sends a command to the server. Ordinary commands (ie, north, west, apply, maps, etc), might be sent, commands with options may be sent (ie, 'invoke create food of booze', 'cast medium fireball'). There are a few special commands that can also be sent. 'fire' command is a special case. The server will handle repeat firing. 'fire_stop' will be sent to inform the server to stop firing. A different command name has been chosen to make things easier on the server ('fire_stop' should be a 0 time command, with the rest of the fire commands actually taking some time.) In some cases, 'fire_stop' may be sent almost immediately after the first fire (in cases where the player only wants to fire once). S->C: comc