Crossfire Server, Trunk  1.75.0
bufferreader.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2021 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include "global.h"
18 #include "compat.h"
19 #include "bufferreader.h"
20 
21 struct BufferReader {
22  char *buf;
24  size_t allocated_size;
25  size_t buffer_length;
27  char *current_line;
28  size_t line_index;
29 };
30 
32  BufferReader *buffer = static_cast<BufferReader *>(calloc(1, sizeof(*buffer)));
33  if (!buffer) {
35  }
36 
37  return buffer;
38 }
39 
41  free(br->buf);
42  free(br);
43 }
44 
50 static void bufferreader_init_for_length(BufferReader *br, size_t length) {
51  size_t needed = length + 1;
52  if (needed > br->allocated_size) {
53  br->buf = static_cast<char *>(realloc(br->buf, needed));
54  if (!br->buf) {
56  }
57  br->allocated_size = needed;
58  }
59  br->buffer_length = length;
60  br->buf[br->buffer_length] = '\0';
61  br->current_line = br->buffer_length > 0 ? br->buf : NULL;
62  br->line_index = 0;
63 }
64 
65 BufferReader *bufferreader_init_from_file(BufferReader *br, const char *filepath, const char *failureMessage, LogLevel failureLevel) {
66  FILE *file = fopen(filepath, "rb");
67  if (!file) {
68  LOG(failureLevel, failureMessage, filepath, strerror(errno));
69  return NULL;
70  }
71  if (!br) {
72  br = bufferreader_create();
73  }
74 
75  fseek(file, 0L, SEEK_END);
76  bufferreader_init_for_length(br, ftell(file));
77  fseek(file, 0, SEEK_SET);
78 
79  size_t actual = fread(br->buf, 1, br->buffer_length, file);
80  if (actual != br->buffer_length) {
81  LOG(llevError, "Expected to read %zu bytes, only read %zu!\n", br->buffer_length, actual);
82  br->buffer_length = actual;
83  }
84  fclose(file);
85  return br;
86 }
87 
90  mtar_read_data(tar, br->buf, h->size);
91 }
92 
93 BufferReader *bufferreader_init_from_memory(BufferReader *br, const char *data, size_t length) {
94  if (!br) {
95  br = bufferreader_create();
96  }
97  bufferreader_init_for_length(br, length);
98  memcpy(br->buf, data, length);
99  return br;
100 }
101 
103  if (!br->current_line) {
104  return NULL;
105  }
106 
107  br->line_index++;
108  char *newline = strchr(br->current_line, '\n');
109  char *curr = br->current_line;
110  if (newline) {
111  br->current_line = newline + 1;
112  (*newline) = '\0';
113  } else {
114  br->current_line = NULL;
115  }
116  return curr;
117 }
118 
119 char *bufferreader_get_line(BufferReader *br, char *buffer, size_t length) {
120  if (!br->current_line) {
121  return NULL;
122  }
123 
124  br->line_index++;
125  char *newline = strchr(br->current_line, '\n');
126  char *curr = br->current_line;
127  if (newline) {
128  size_t cp = MIN(length, (size_t)(newline - curr + 1));
129  br->current_line = newline + 1;
130  strncpy(buffer, curr, cp);
131  buffer[cp] = '\0';
132  } else {
133  strncpy(buffer, curr, length);
134  br->current_line = NULL;
135  }
136 
137  return buffer;
138 }
139 
141  return br->line_index;
142 }
143 
145  return br->buffer_length;
146 }
147 
149  return br->buf;
150 }
global.h
BufferReader::current_line
char * current_line
Pointer to the next line of the buffer.
Definition: bufferreader.cpp:27
bufferreader_data
char * bufferreader_data(BufferReader *br)
Get the whole buffer, independently of the calls to bufferreader_next_line().
Definition: bufferreader.cpp:148
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Return the index of the last line returned by bufferreader_next_line().
Definition: bufferreader.cpp:140
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
mtar_t
Definition: microtar.h:51
mtar_header_t::size
unsigned size
Definition: microtar.h:41
MIN
#define MIN(x, y)
Definition: compat.h:21
bufferreader_destroy
void bufferreader_destroy(BufferReader *br)
Destroy a BufferReader.
Definition: bufferreader.cpp:40
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Return the length of the buffer data.
Definition: bufferreader.cpp:144
bufferreader_get_line
char * bufferreader_get_line(BufferReader *br, char *buffer, size_t length)
Copy the next line in the buffer, keeping the newline.
Definition: bufferreader.cpp:119
bufferreader_init_for_length
static void bufferreader_init_for_length(BufferReader *br, size_t length)
Ensure the buffer can hold the specified length, initialize all fields.
Definition: bufferreader.cpp:50
bufferreader_init_from_file
BufferReader * bufferreader_init_from_file(BufferReader *br, const char *filepath, const char *failureMessage, LogLevel failureLevel)
Initialize or create a BufferReader from a file path.
Definition: bufferreader.cpp:65
compat.h
BufferReader::line_index
size_t line_index
Current line index.
Definition: bufferreader.cpp:28
BufferReader::allocated_size
size_t allocated_size
Allocated size of buf.
Definition: bufferreader.cpp:24
bufferreader.h
mtar_header_t
Definition: microtar.h:38
fatal
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
bufferreader_init_from_memory
BufferReader * bufferreader_init_from_memory(BufferReader *br, const char *data, size_t length)
Initialize or create a BufferReader from a memory block.
Definition: bufferreader.cpp:93
buffer
if you malloc the data for the buffer
Definition: protocol.txt:2100
data
====Textual A command containing textual data has data fields separated by one ASCII space character. word::A sequence of ASCII characters that does not contain the space or nul character. This is to distinguish it from the _string_, which may contain space characters. Not to be confused with a machine word. int::A _word_ containing the textual representation of an integer. Not to be confused with any of the binary integers in the following section. Otherwise known as the "string value of integer data". Must be parsed, e.g. using `atoi()` to get the actual integer value. string::A sequence of ASCII characters. This must only appear at the end of a command, since spaces are used to separate fields of a textual message.=====Binary All multi-byte integers are transmitted in network byte order(MSB first). int8::1-byte(8-bit) integer int16::2-byte(16-bit) integer int32::4-byte(32-bit) integer lstring::A length-prefixed string, which consists of an `int8` followed by that many bytes of the actual string. This is used to transmit a string(that may contain spaces) in the middle of binary data. l2string::Like _lstring_, but is prefixed with an `int16` to support longer strings Implementation Notes ~~~~~~~~~~~~~~~~~~~~ - Typical implementations read two bytes to determine the length of the subsequent read for the actual message, then read and parse the data from each message according to the commands described below. To send a message, the sender builds the message in a buffer, counts the length of the message, sends the length, and finally sends the actual message. TIP:Incorrectly transmitting or receiving the `length` field can lead to apparent "no response" issues as the client or server blocks to read the entire length of the message. - Since the protocol is highly interactive, it may be useful to set `TCP_NODELAY` on both the client and server. - If you are using a language with a buffered output stream, remember to flush the stream after a complete message. - 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. Commands -------- In the documentation below, `S->C` represents a message to the client from the server, and `C->S` represents a message to the server from the client. Commands are documented in a brief format like:C->S:version< csval >[scval[vinfo]] Fields are enclosed like `< this >`. Optional fields are denoted like `[this]`. Spaces that appear in the command are literal, i.e. the<< _version > > command above uses spaces to separate its fields, but the command below does not:C->S:accountlogin< name >< password > As described in<< _messages > >, if a command contains data, then the command is separated from the data by a literal space. 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. Initialization ~~~~~~~~~~~~~~ version ^^^^^^^ C->S:version< csval >[scval[vinfo]] S->C:version< csval >[scval[vinfo]] Used by the client and server to exchange which version of the Crossfire protocol they understand. Neither send this in response to the other - they should both send this shortly after a connection is established. csval::int, version level of C->S communications scval::int, version level of S->C communications vinfo::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.=====Version ID 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. 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.=====Handling 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. 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.=====History _scval_ and _vinfo_ were added in version 1020. Before then, there was only one version sent in the version command. 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. setup ^^^^^ C->S, S->C:setup< option1 >< value1 >< option2 >< value2 > ... Sent by the client to request protocol option changes. This can be at any point during the life of a connection, but usually sent at least once right after the<< _version > > command. The server responds with a message in the same format confirming what configuration options were set. The server only sends a setup command in response to one from the client. The sc_version should be updated in the server if commands have been obsoleted such that old clients may not be able to play. option::word, name of configuration option value::word, value of configuration option. May need further parsing according to the setup options below=====Setup Options There are really 2 set of setup commands here:. Those that control preferences of the client(how big is the map, what faceset to use, etc). . Those that describe capabilities of the client(client supports this protocol command or that) .Setup Options[options="autowidth,header"]|===========================|Command|Description|beat|Ask the server to enable heartbeat support. When heartbeat is enabled, the client must send the server a command every three seconds. If no commands need to be sent, use the `beat` no-op command. Clients that do not contact the server within the interval are assumed to have a temporary connection failure.|bot(0/1 value)|If set to 1, the client will not be considered a player when updating information to the metaserver. This is to avoid having a server with many bots appear more crowded than others.|darkness(0/1 value)|If set to 1(default), the server will send darkness information in the map protocol commands. If 0, the server will not include darkness, thus saving a minor amount of bandwidth. Since the client is free to ignore the darkness information, this does not allow the client to cheat. In the case of the old 'map' protocol command, turning darkness off will result in the masking faces not getting sent to the client.|extended_stats(0/1 value)|If set to 1, the server will send the CS_STAT_RACE_xxx and CS_STAT_BASE_xxx values too, so the client can display various status related to statistics. Default is 0.|facecache(0/1)|Determines if the client is caching images(1) or wants the images sent to it without caching them(0). Default is 0. This replaces the setfacemode command.|faceset(8 bit)|Faceset the client wishes to use. If the faceset is not valid, the server returns the faceset the client will be using(default 0).|loginmethod(8 bit)|Client sends this to server to note login support. This is basically used as a subset of the csversion/scversion to find out what level of login support the server and client support. Current defined values:0:no advanced support - only legacy login method 1:account based login(described more below) 2:new character creation support This list may grow - for example, advanced character creation could become a feature.|map2cmd:(1)|This indicates client support for the map2 protocol command. See the map2 protocol details above for the main differences. Obsolete:This is the only supported mode now, but many clients use it as a sanity check for protocol versions, so the server still replies. It doesn 't do anything with the data|mapsize(int x) X(int y)|Sets the map size to x X y. Note the spaces here are only for clarity - there should be no spaces when actually sent(it should be 11x11 or 25x25). The default map size unless changed is 11x11. The minimum map size the server will allow is 9x9(no technical reason this could be smaller, but I don 't think the game would be smaller). The maximum map size supported in the current protocol is 63x63. However, each server can have its maximum map size sent to most any value. If the client sends an invalid mapsize command or a mapsize of 0x0, the server will respond with a mapsize that is the maximum size the server supports. Thus, if the client wants to know the maximum map size, it can just do a 'mapsize 0x0' or 'mapsize' and it will get the maximum size back. The server will constrain the provided mapsize x &y to the configured minumum and maximums. For example, if the maximum map size is 25x25, the minimum map size is 9x9, and the client sends a 31x7 mapsize request, the mapsize will be set to 25x9 and the server will send back a mapsize 25x9 setup command. When the values are valid, the server will send back a mapsize XxY setup command. Note that this is from its parsed values, so it may not match stringwise with what the client sent, but will match 0 wise. For example, the client may send a 'mapsize 025X025' command, in which case the server will respond with a 'mapsize 25x25' command - the data is functionally the same. The server will send an updated map view when this command is sent.|notifications(int value)|Value indicating what notifications the client accepts. It is incremental, a value means "all notifications till this level". The following levels are supported:1:quest-related notifications("addquest" and "updquest") 2:knowledge-related notifications("addknowledge") 3:character status flags(overloaded, blind,...)|num_look_objects(int value)|The maximum number of objects shown in the ground view. If more objects are present, fake objects are created for selecting the previous/next group of items. Defaults to 50 if not set. The server may adjust the given value to a suitable one data
Definition: protocol.txt:379
bufferreader_init_from_tar_file
void bufferreader_init_from_tar_file(BufferReader *br, mtar_t *tar, mtar_header_t *h)
Initialize a BufferReader from a tar file entry.
Definition: bufferreader.cpp:88
bufferreader_create
BufferReader * bufferreader_create()
Create a new BufferReader.
Definition: bufferreader.cpp:31
LogLevel
LogLevel
Log levels for the LOG() function.
Definition: logger.h:10
BufferReader::buffer_length
size_t buffer_length
Used size of buf.
Definition: bufferreader.cpp:25
BufferReader::buf
char * buf
Data buffer.
Definition: bufferreader.cpp:22
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
BufferReader
Definition: bufferreader.cpp:21
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.
Definition: bufferreader.cpp:102
mtar_read_data
int mtar_read_data(mtar_t *tar, void *ptr, unsigned size)
Definition: microtar.cpp:290