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