]> git.lizzy.rs Git - minetest.git/blob - src/clientiface.h
[CSM] Add flavour limits controlled by server (#5930)
[minetest.git] / src / clientiface.h
1 /*
2 Minetest
3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #ifndef _CLIENTIFACE_H_
20 #define _CLIENTIFACE_H_
21
22 #include "irr_v3d.h"                   // for irrlicht datatypes
23
24 #include "constants.h"
25 #include "serialization.h"             // for SER_FMT_VER_INVALID
26 #include "network/networkpacket.h"
27 #include "porting.h"
28
29 #include <list>
30 #include <vector>
31 #include <set>
32 #include <mutex>
33
34 class MapBlock;
35 class ServerEnvironment;
36 class EmergeManager;
37
38 /*
39  * State Transitions
40
41       Start
42   (peer connect)
43         |
44         v
45       /-----------------\
46       |                 |
47       |    Created      |
48       |                 |
49       \-----------------/
50                |                  depending of the incoming packet
51                +---------------------------------------
52                v                                      v
53 +-----------------------------+        +-----------------------------+
54 |IN:                          |        |IN:                          |
55 | TOSERVER_INIT_LEGACY        |-----   | TOSERVER_INIT               |      invalid playername,
56 +-----------------------------+    |   +-----------------------------+  password (for _LEGACY),
57                |                   |                  |                       or denied by mod
58                | Auth ok           -------------------+---------------------------------
59                v                                      v                                |
60 +-----------------------------+        +-----------------------------+                 |
61 |OUT:                         |        |OUT:                         |                 |
62 | TOCLIENT_INIT_LEGACY        |        | TOCLIENT_HELLO              |                 |
63 +-----------------------------+        +-----------------------------+                 |
64                |                                      |                                |
65                |                                      |                                |
66                v                                      v                                |
67       /-----------------\                    /-----------------\                       |
68       |                 |                    |                 |                       |
69       |  AwaitingInit2  |<---------          |    HelloSent    |                       |
70       |                 |         |          |                 |                       |
71       \-----------------/         |          \-----------------/                       |
72                |                  |                   |                                |
73 +-----------------------------+   |    *-----------------------------*     Auth fails  |
74 |IN:                          |   |    |Authentication, depending on |-----------------+
75 | TOSERVER_INIT2              |   |    | packet sent by client       |                 |
76 +-----------------------------+   |    *-----------------------------*                 |
77                |                  |                   |                                |
78                |                  |                   | Authentication                 |
79                v                  |                   |  successful                    |
80       /-----------------\         |                   v                                |
81       |                 |         |    +-----------------------------+                 |
82       |    InitDone     |         |    |OUT:                         |                 |
83       |                 |         |    | TOCLIENT_AUTH_ACCEPT        |                 |
84       \-----------------/         |    +-----------------------------+                 |
85                |                  |                   |                                |
86 +-----------------------------+   ---------------------                                |
87 |OUT:                         |                                                        |
88 | TOCLIENT_MOVEMENT           |                                                        |
89 | TOCLIENT_ITEMDEF            |                                                        |
90 | TOCLIENT_NODEDEF            |                                                        |
91 | TOCLIENT_ANNOUNCE_MEDIA     |                                                        |
92 | TOCLIENT_DETACHED_INVENTORY |                                                        |
93 | TOCLIENT_TIME_OF_DAY        |                                                        |
94 +-----------------------------+                                                        |
95                |                                                                       |
96                |                                                                       |
97                |      -----------------------------                                    |
98                v      |                           |                                    |
99       /-----------------\                         v                                    |
100       |                 |             +-----------------------------+                  |
101       | DefinitionsSent |             |IN:                          |                  |
102       |                 |             | TOSERVER_REQUEST_MEDIA      |                  |
103       \-----------------/             | TOSERVER_RECEIVED_MEDIA     |                  |
104                |                      +-----------------------------+                  |
105                |      ^                           |                                    |
106                |      -----------------------------                                    |
107                v                                                                       |
108 +-----------------------------+                        --------------------------------+
109 |IN:                          |                        |                               |
110 | TOSERVER_CLIENT_READY       |                        v                               |
111 +-----------------------------+        +-------------------------------+               |
112                |                       |OUT:                           |               |
113                v                       | TOCLIENT_ACCESS_DENIED_LEGAGY |               |
114 +-----------------------------+        +-------------------------------+               |
115 |OUT:                         |                        |                               |
116 | TOCLIENT_MOVE_PLAYER        |                        v                               |
117 | TOCLIENT_PRIVILEGES         |                /-----------------\                     |
118 | TOCLIENT_INVENTORY_FORMSPEC |                |                 |                     |
119 | UpdateCrafting              |                |     Denied      |                     |
120 | TOCLIENT_INVENTORY          |                |                 |                     |
121 | TOCLIENT_HP (opt)           |                \-----------------/                     |
122 | TOCLIENT_BREATH             |                                                        |
123 | TOCLIENT_DEATHSCREEN        |                                                        |
124 +-----------------------------+                                                        |
125               |                                                                        |
126               v                                                                        |
127       /-----------------\      async mod action (ban, kick)                            |
128       |                 |---------------------------------------------------------------
129  ---->|     Active      |
130  |    |                 |----------------------------------------------
131  |    \-----------------/      timeout                                v
132  |       |           |                                  +-----------------------------+
133  |       |           |                                  |OUT:                         |
134  |       |           |                                  | TOCLIENT_DISCONNECT         |
135  |       |           |                                  +-----------------------------+
136  |       |           |                                                |
137  |       |           v                                                v
138  |       |  +-----------------------------+                    /-----------------\
139  |       |  |IN:                          |                    |                 |
140  |       |  | TOSERVER_DISCONNECT         |------------------->|  Disconnecting  |
141  |       |  +-----------------------------+                    |                 |
142  |       |                                                     \-----------------/
143  |       | any auth packet which was
144  |       | allowed in TOCLIENT_AUTH_ACCEPT
145  |       v
146  |    *-----------------------------* Auth      +-------------------------------+
147  |    |Authentication, depending on | succeeds  |OUT:                           |
148  |    | packet sent by client       |---------->| TOCLIENT_ACCEPT_SUDO_MODE     |
149  |    *-----------------------------*           +-------------------------------+
150  |                  |                                            |
151  |                  | Auth fails                        /-----------------\
152  |                  v                                   |                 |
153  |    +-------------------------------+                 |    SudoMode     |
154  |    |OUT:                           |                 |                 |
155  |    | TOCLIENT_DENY_SUDO_MODE       |                 \-----------------/
156  |    +-------------------------------+                          |
157  |                  |                                            v
158  |                  |                               +-----------------------------+
159  |                  |    sets password accordingly  |IN:                          |
160  -------------------+-------------------------------| TOSERVER_FIRST_SRP          |
161                                                     +-----------------------------+
162
163 */
164 namespace con {
165         class Connection;
166 }
167
168
169 // Also make sure to update the ClientInterface::statenames
170 // array when modifying these enums
171
172 enum ClientState
173 {
174         CS_Invalid,
175         CS_Disconnecting,
176         CS_Denied,
177         CS_Created,
178         CS_AwaitingInit2,
179         CS_HelloSent,
180         CS_InitDone,
181         CS_DefinitionsSent,
182         CS_Active,
183         CS_SudoMode
184 };
185
186 enum ClientStateEvent
187 {
188         CSE_Hello,
189         CSE_AuthAccept,
190         CSE_InitLegacy,
191         CSE_GotInit2,
192         CSE_SetDenied,
193         CSE_SetDefinitionsSent,
194         CSE_SetClientReady,
195         CSE_SudoSuccess,
196         CSE_SudoLeave,
197         CSE_Disconnect
198 };
199
200 /*
201         Used for queueing and sorting block transfers in containers
202
203         Lower priority number means higher priority.
204 */
205 struct PrioritySortedBlockTransfer
206 {
207         PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
208         {
209                 priority = a_priority;
210                 pos = a_pos;
211                 peer_id = a_peer_id;
212         }
213         bool operator < (const PrioritySortedBlockTransfer &other) const
214         {
215                 return priority < other.priority;
216         }
217         float priority;
218         v3s16 pos;
219         u16 peer_id;
220 };
221
222 class RemoteClient
223 {
224 public:
225         // peer_id=0 means this client has no associated peer
226         // NOTE: If client is made allowed to exist while peer doesn't,
227         //       this has to be set to 0 when there is no peer.
228         //       Also, the client must be moved to some other container.
229         u16 peer_id = PEER_ID_INEXISTENT;
230         // The serialization version to use with the client
231         u8 serialization_version = SER_FMT_VER_INVALID;
232         //
233         u16 net_proto_version = 0;
234
235         /* Authentication information */
236         std::string enc_pwd = "";
237         bool create_player_on_auth_success = false;
238         AuthMechanism chosen_mech  = AUTH_MECHANISM_NONE;
239         void *auth_data = nullptr;
240         u32 allowed_auth_mechs = 0;
241         u32 allowed_sudo_mechs = 0;
242
243         bool isSudoMechAllowed(AuthMechanism mech)
244         { return allowed_sudo_mechs & mech; }
245         bool isMechAllowed(AuthMechanism mech)
246         { return allowed_auth_mechs & mech; }
247
248         RemoteClient() {}
249         ~RemoteClient() {}
250
251         /*
252                 Finds block that should be sent next to the client.
253                 Environment should be locked when this is called.
254                 dtime is used for resetting send radius at slow interval
255         */
256         void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
257                         float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
258
259         void GotBlock(v3s16 p);
260
261         void SentBlock(v3s16 p);
262
263         void SetBlockNotSent(v3s16 p);
264         void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
265
266         /**
267          * tell client about this block being modified right now.
268          * this information is required to requeue the block in case it's "on wire"
269          * while modification is processed by server
270          * @param p position of modified block
271          */
272         void ResendBlockIfOnWire(v3s16 p);
273
274         s32 SendingCount()
275         {
276                 return m_blocks_sending.size();
277         }
278
279         // Increments timeouts and removes timed-out blocks from list
280         // NOTE: This doesn't fix the server-not-sending-block bug
281         //       because it is related to emerging, not sending.
282         //void RunSendingTimeouts(float dtime, float timeout);
283
284         void PrintInfo(std::ostream &o)
285         {
286                 o<<"RemoteClient "<<peer_id<<": "
287                                 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
288                                 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
289                                 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
290                                 <<", m_excess_gotblocks="<<m_excess_gotblocks
291                                 <<std::endl;
292                 m_excess_gotblocks = 0;
293         }
294
295         // Time from last placing or removing blocks
296         float m_time_from_building = 9999;
297
298         /*
299                 List of active objects that the client knows of.
300         */
301         std::set<u16> m_known_objects;
302
303         ClientState getState() const { return m_state; }
304
305         std::string getName() const { return m_name; }
306
307         void setName(const std::string &name) { m_name = name; }
308
309         /* update internal client state */
310         void notifyEvent(ClientStateEvent event);
311
312         /* set expected serialization version */
313         void setPendingSerializationVersion(u8 version)
314                 { m_pending_serialization_version = version; }
315
316         void setDeployedCompressionMode(u16 byteFlag)
317                 { m_deployed_compression = byteFlag; }
318
319         void confirmSerializationVersion()
320                 { serialization_version = m_pending_serialization_version; }
321
322         /* get uptime */
323         u64 uptime() const;
324
325         /* set version information */
326         void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
327         {
328                 m_version_major = major;
329                 m_version_minor = minor;
330                 m_version_patch = patch;
331                 m_full_version = full;
332         }
333
334         /* read version information */
335         u8 getMajor() const { return m_version_major; }
336         u8 getMinor() const { return m_version_minor; }
337         u8 getPatch() const { return m_version_patch; }
338 private:
339         // Version is stored in here after INIT before INIT2
340         u8 m_pending_serialization_version = SER_FMT_VER_INVALID;
341
342         /* current state of client */
343         ClientState m_state = CS_Created;
344
345         /*
346                 Blocks that have been sent to client.
347                 - These don't have to be sent again.
348                 - A block is cleared from here when client says it has
349                   deleted it from it's memory
350
351                 List of block positions.
352                 No MapBlock* is stored here because the blocks can get deleted.
353         */
354         std::set<v3s16> m_blocks_sent;
355         s16 m_nearest_unsent_d = 0;
356         v3s16 m_last_center;
357         float m_nearest_unsent_reset_timer = 0.0f;
358
359         /*
360                 Blocks that are currently on the line.
361                 This is used for throttling the sending of blocks.
362                 - The size of this list is limited to some value
363                 Block is added when it is sent with BLOCKDATA.
364                 Block is removed when GOTBLOCKS is received.
365                 Value is time from sending. (not used at the moment)
366         */
367         std::map<v3s16, float> m_blocks_sending;
368
369         /*
370                 Blocks that have been modified since last sending them.
371                 These blocks will not be marked as sent, even if the
372                 client reports it has received them to account for blocks
373                 that are being modified while on the line.
374
375                 List of block positions.
376         */
377         std::set<v3s16> m_blocks_modified;
378
379         /*
380                 Count of excess GotBlocks().
381                 There is an excess amount because the client sometimes
382                 gets a block so late that the server sends it again,
383                 and the client then sends two GOTBLOCKs.
384                 This is resetted by PrintInfo()
385         */
386         u32 m_excess_gotblocks = 0;
387
388         // CPU usage optimization
389         float m_nothing_to_send_pause_timer = 0.0f;
390
391         /*
392                 name of player using this client
393         */
394         std::string m_name = "";
395
396         /*
397                 client information
398          */
399         u8 m_version_major = 0;
400         u8 m_version_minor = 0;
401         u8 m_version_patch = 0;
402
403         std::string m_full_version = "unknown";
404
405         u16 m_deployed_compression = 0;
406
407         /*
408                 time this client was created
409          */
410         const u64 m_connection_time = porting::getTimeS();
411 };
412
413 typedef std::unordered_map<u16, RemoteClient*> RemoteClientMap;
414
415 class ClientInterface {
416 public:
417
418         friend class Server;
419
420         ClientInterface(con::Connection* con);
421         ~ClientInterface();
422
423         /* run sync step */
424         void step(float dtime);
425
426         /* get list of active client id's */
427         std::vector<u16> getClientIDs(ClientState min_state=CS_Active);
428
429         /* get list of client player names */
430         const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
431
432         /* send message to client */
433         void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
434
435         /* send to all clients */
436         void sendToAll(NetworkPacket *pkt);
437         void sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt, u16 min_proto_ver);
438
439         /* delete a client */
440         void DeleteClient(u16 peer_id);
441
442         /* create client */
443         void CreateClient(u16 peer_id);
444
445         /* get a client by peer_id */
446         RemoteClient* getClientNoEx(u16 peer_id,  ClientState state_min=CS_Active);
447
448         /* get client by peer_id (make sure you have list lock before!*/
449         RemoteClient* lockedGetClientNoEx(u16 peer_id,  ClientState state_min=CS_Active);
450
451         /* get state of client by id*/
452         ClientState getClientState(u16 peer_id);
453
454         /* set client playername */
455         void setPlayerName(u16 peer_id,std::string name);
456
457         /* get protocol version of client */
458         u16 getProtocolVersion(u16 peer_id);
459
460         /* set client version */
461         void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
462
463         /* event to update client state */
464         void event(u16 peer_id, ClientStateEvent event);
465
466         /* Set environment. Do not call this function if environment is already set */
467         void setEnv(ServerEnvironment *env)
468         {
469                 assert(m_env == NULL); // pre-condition
470                 m_env = env;
471         }
472
473         static std::string state2Name(ClientState state);
474
475 protected:
476         //TODO find way to avoid this functions
477         void lock() { m_clients_mutex.lock(); }
478         void unlock() { m_clients_mutex.unlock(); }
479
480         RemoteClientMap& getClientList() { return m_clients; }
481
482 private:
483         /* update internal player list */
484         void UpdatePlayerList();
485
486         // Connection
487         con::Connection* m_con;
488         std::mutex m_clients_mutex;
489         // Connected clients (behind the con mutex)
490         RemoteClientMap m_clients;
491         std::vector<std::string> m_clients_names; //for announcing masterserver
492
493         // Environment
494         ServerEnvironment *m_env;
495         std::mutex m_env_mutex;
496
497         float m_print_info_timer;
498
499         static const char *statenames[];
500 };
501
502 #endif