serverpackethandler.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /*
  2. Minetest
  3. Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "chatmessage.h"
  17. #include "server.h"
  18. #include "log.h"
  19. #include "emerge.h"
  20. #include "mapblock.h"
  21. #include "modchannels.h"
  22. #include "nodedef.h"
  23. #include "remoteplayer.h"
  24. #include "rollback_interface.h"
  25. #include "scripting_server.h"
  26. #include "settings.h"
  27. #include "tool.h"
  28. #include "version.h"
  29. #include "network/connection.h"
  30. #include "network/networkprotocol.h"
  31. #include "network/serveropcodes.h"
  32. #include "server/player_sao.h"
  33. #include "server/serverinventorymgr.h"
  34. #include "util/auth.h"
  35. #include "util/base64.h"
  36. #include "util/pointedthing.h"
  37. #include "util/serialize.h"
  38. #include "util/srp.h"
  39. void Server::handleCommand_Deprecated(NetworkPacket* pkt)
  40. {
  41. infostream << "Server: " << toServerCommandTable[pkt->getCommand()].name
  42. << " not supported anymore" << std::endl;
  43. }
  44. void Server::handleCommand_Init(NetworkPacket* pkt)
  45. {
  46. if(pkt->getSize() < 1)
  47. return;
  48. session_t peer_id = pkt->getPeerId();
  49. RemoteClient *client = getClient(peer_id, CS_Created);
  50. Address addr;
  51. std::string addr_s;
  52. try {
  53. addr = m_con->GetPeerAddress(peer_id);
  54. addr_s = addr.serializeString();
  55. } catch (con::PeerNotFoundException &e) {
  56. /*
  57. * no peer for this packet found
  58. * most common reason is peer timeout, e.g. peer didn't
  59. * respond for some time, your server was overloaded or
  60. * things like that.
  61. */
  62. infostream << "Server::ProcessData(): Canceling: peer " << peer_id <<
  63. " not found" << std::endl;
  64. return;
  65. }
  66. if (client->getState() > CS_Created) {
  67. verbosestream << "Server: Ignoring multiple TOSERVER_INITs from " <<
  68. addr_s << " (peer_id=" << peer_id << ")" << std::endl;
  69. return;
  70. }
  71. client->setCachedAddress(addr);
  72. verbosestream << "Server: Got TOSERVER_INIT from " << addr_s <<
  73. " (peer_id=" << peer_id << ")" << std::endl;
  74. // Do not allow multiple players in simple singleplayer mode.
  75. // This isn't a perfect way to do it, but will suffice for now
  76. if (m_simple_singleplayer_mode && !m_clients.getClientIDs().empty()) {
  77. infostream << "Server: Not allowing another client (" << addr_s <<
  78. ") to connect in simple singleplayer mode" << std::endl;
  79. DenyAccess(peer_id, SERVER_ACCESSDENIED_SINGLEPLAYER);
  80. return;
  81. }
  82. // First byte after command is maximum supported
  83. // serialization version
  84. u8 client_max;
  85. u16 supp_compr_modes;
  86. u16 min_net_proto_version = 0;
  87. u16 max_net_proto_version;
  88. std::string playerName;
  89. *pkt >> client_max >> supp_compr_modes >> min_net_proto_version
  90. >> max_net_proto_version >> playerName;
  91. u8 our_max = SER_FMT_VER_HIGHEST_READ;
  92. // Use the highest version supported by both
  93. u8 depl_serial_v = std::min(client_max, our_max);
  94. // If it's lower than the lowest supported, give up.
  95. #if SER_FMT_VER_LOWEST_READ > 0
  96. if (depl_serial_v < SER_FMT_VER_LOWEST_READ)
  97. depl_serial_v = SER_FMT_VER_INVALID;
  98. #endif
  99. if (depl_serial_v == SER_FMT_VER_INVALID) {
  100. actionstream << "Server: A mismatched client tried to connect from " <<
  101. addr_s << " ser_fmt_max=" << (int)client_max << std::endl;
  102. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_VERSION);
  103. return;
  104. }
  105. client->setPendingSerializationVersion(depl_serial_v);
  106. /*
  107. Read and check network protocol version
  108. */
  109. u16 net_proto_version = 0;
  110. // Figure out a working version if it is possible at all
  111. if (max_net_proto_version >= SERVER_PROTOCOL_VERSION_MIN ||
  112. min_net_proto_version <= SERVER_PROTOCOL_VERSION_MAX) {
  113. // If maximum is larger than our maximum, go with our maximum
  114. if (max_net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
  115. net_proto_version = SERVER_PROTOCOL_VERSION_MAX;
  116. // Else go with client's maximum
  117. else
  118. net_proto_version = max_net_proto_version;
  119. }
  120. verbosestream << "Server: " << addr_s << ": Protocol version: min: "
  121. << min_net_proto_version << ", max: " << max_net_proto_version
  122. << ", chosen: " << net_proto_version << std::endl;
  123. client->net_proto_version = net_proto_version;
  124. if ((g_settings->getBool("strict_protocol_version_checking") &&
  125. net_proto_version != LATEST_PROTOCOL_VERSION) ||
  126. net_proto_version < SERVER_PROTOCOL_VERSION_MIN ||
  127. net_proto_version > SERVER_PROTOCOL_VERSION_MAX) {
  128. actionstream << "Server: A mismatched client tried to connect from " <<
  129. addr_s << " proto_max=" << (int)max_net_proto_version << std::endl;
  130. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_VERSION);
  131. return;
  132. }
  133. /*
  134. Validate player name
  135. */
  136. const char* playername = playerName.c_str();
  137. size_t pns = playerName.size();
  138. if (pns == 0 || pns > PLAYERNAME_SIZE) {
  139. actionstream << "Server: Player with " <<
  140. ((pns > PLAYERNAME_SIZE) ? "a too long" : "an empty") <<
  141. " name tried to connect from " << addr_s << std::endl;
  142. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_NAME);
  143. return;
  144. }
  145. if (!string_allowed(playerName, PLAYERNAME_ALLOWED_CHARS)) {
  146. actionstream << "Server: Player with an invalid name tried to connect "
  147. "from " << addr_s << std::endl;
  148. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_CHARS_IN_NAME);
  149. return;
  150. }
  151. RemotePlayer *player = m_env->getPlayer(playername);
  152. // If player is already connected, cancel
  153. if (player && player->getPeerId() != PEER_ID_INEXISTENT) {
  154. actionstream << "Server: Player with name \"" << playername <<
  155. "\" tried to connect, but player with same name is already connected" << std::endl;
  156. DenyAccess(peer_id, SERVER_ACCESSDENIED_ALREADY_CONNECTED);
  157. return;
  158. }
  159. m_clients.setPlayerName(peer_id, playername);
  160. //TODO (later) case insensitivity
  161. std::string legacyPlayerNameCasing = playerName;
  162. if (!isSingleplayer() && strcasecmp(playername, "singleplayer") == 0) {
  163. actionstream << "Server: Player with the name \"singleplayer\" tried "
  164. "to connect from " << addr_s << std::endl;
  165. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_NAME);
  166. return;
  167. }
  168. {
  169. std::string reason;
  170. if (m_script->on_prejoinplayer(playername, addr_s, &reason)) {
  171. actionstream << "Server: Player with the name \"" << playerName <<
  172. "\" tried to connect from " << addr_s <<
  173. " but it was disallowed for the following reason: " << reason <<
  174. std::endl;
  175. DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING, reason);
  176. return;
  177. }
  178. }
  179. infostream << "Server: New connection: \"" << playerName << "\" from " <<
  180. addr_s << " (peer_id=" << peer_id << ")" << std::endl;
  181. // Enforce user limit.
  182. // Don't enforce for users that have some admin right or mod permits it.
  183. if (m_clients.isUserLimitReached() &&
  184. playername != g_settings->get("name") &&
  185. !m_script->can_bypass_userlimit(playername, addr_s)) {
  186. actionstream << "Server: " << playername << " tried to join from " <<
  187. addr_s << ", but there are already max_users=" <<
  188. g_settings->getU16("max_users") << " players." << std::endl;
  189. DenyAccess(peer_id, SERVER_ACCESSDENIED_TOO_MANY_USERS);
  190. return;
  191. }
  192. /*
  193. Compose auth methods for answer
  194. */
  195. std::string encpwd; // encrypted Password field for the user
  196. bool has_auth = m_script->getAuth(playername, &encpwd, nullptr);
  197. u32 auth_mechs = 0;
  198. client->chosen_mech = AUTH_MECHANISM_NONE;
  199. if (has_auth) {
  200. std::vector<std::string> pwd_components = str_split(encpwd, '#');
  201. if (pwd_components.size() == 4) {
  202. if (pwd_components[1] == "1") { // 1 means srp
  203. auth_mechs |= AUTH_MECHANISM_SRP;
  204. client->enc_pwd = encpwd;
  205. } else {
  206. actionstream << "User " << playername << " tried to log in, "
  207. "but password field was invalid (unknown mechcode)." <<
  208. std::endl;
  209. DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
  210. return;
  211. }
  212. } else if (base64_is_valid(encpwd)) {
  213. auth_mechs |= AUTH_MECHANISM_LEGACY_PASSWORD;
  214. client->enc_pwd = encpwd;
  215. } else {
  216. actionstream << "User " << playername << " tried to log in, but "
  217. "password field was invalid (invalid base64)." << std::endl;
  218. DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
  219. return;
  220. }
  221. } else {
  222. std::string default_password = g_settings->get("default_password");
  223. if (default_password.length() == 0) {
  224. auth_mechs |= AUTH_MECHANISM_FIRST_SRP;
  225. } else {
  226. // Take care of default passwords.
  227. client->enc_pwd = get_encoded_srp_verifier(playerName, default_password);
  228. auth_mechs |= AUTH_MECHANISM_SRP;
  229. // Allocate player in db, but only on successful login.
  230. client->create_player_on_auth_success = true;
  231. }
  232. }
  233. /*
  234. Answer with a TOCLIENT_HELLO
  235. */
  236. verbosestream << "Sending TOCLIENT_HELLO with auth method field: "
  237. << auth_mechs << std::endl;
  238. NetworkPacket resp_pkt(TOCLIENT_HELLO,
  239. 1 + 4 + legacyPlayerNameCasing.size(), peer_id);
  240. u16 depl_compress_mode = NETPROTO_COMPRESSION_NONE;
  241. resp_pkt << depl_serial_v << depl_compress_mode << net_proto_version
  242. << auth_mechs << legacyPlayerNameCasing;
  243. Send(&resp_pkt);
  244. client->allowed_auth_mechs = auth_mechs;
  245. client->setDeployedCompressionMode(depl_compress_mode);
  246. m_clients.event(peer_id, CSE_Hello);
  247. }
  248. void Server::handleCommand_Init2(NetworkPacket* pkt)
  249. {
  250. session_t peer_id = pkt->getPeerId();
  251. verbosestream << "Server: Got TOSERVER_INIT2 from " << peer_id << std::endl;
  252. m_clients.event(peer_id, CSE_GotInit2);
  253. u16 protocol_version = m_clients.getProtocolVersion(peer_id);
  254. std::string lang;
  255. if (pkt->getSize() > 0)
  256. *pkt >> lang;
  257. /*
  258. Send some initialization data
  259. */
  260. infostream << "Server: Sending content to " << getPlayerName(peer_id) <<
  261. std::endl;
  262. // Send item definitions
  263. SendItemDef(peer_id, m_itemdef, protocol_version);
  264. // Send node definitions
  265. SendNodeDef(peer_id, m_nodedef, protocol_version);
  266. m_clients.event(peer_id, CSE_SetDefinitionsSent);
  267. // Send media announcement
  268. sendMediaAnnouncement(peer_id, lang);
  269. RemoteClient *client = getClient(peer_id, CS_InitDone);
  270. // Keep client language for server translations
  271. client->setLangCode(lang);
  272. // Send active objects
  273. {
  274. PlayerSAO *sao = getPlayerSAO(peer_id);
  275. if (sao)
  276. SendActiveObjectRemoveAdd(client, sao);
  277. }
  278. // Send detached inventories
  279. sendDetachedInventories(peer_id, false);
  280. // Send player movement settings
  281. SendMovement(peer_id);
  282. // Send time of day
  283. u16 time = m_env->getTimeOfDay();
  284. float time_speed = g_settings->getFloat("time_speed");
  285. SendTimeOfDay(peer_id, time, time_speed);
  286. SendCSMRestrictionFlags(peer_id);
  287. // Warnings about protocol version can be issued here
  288. if (client->net_proto_version < LATEST_PROTOCOL_VERSION) {
  289. SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  290. L"# Server: WARNING: YOUR CLIENT'S VERSION MAY NOT BE FULLY COMPATIBLE "
  291. L"WITH THIS SERVER!"));
  292. }
  293. }
  294. void Server::handleCommand_RequestMedia(NetworkPacket* pkt)
  295. {
  296. std::vector<std::string> tosend;
  297. u16 numfiles;
  298. *pkt >> numfiles;
  299. session_t peer_id = pkt->getPeerId();
  300. infostream << "Sending " << numfiles << " files to " <<
  301. getPlayerName(peer_id) << std::endl;
  302. verbosestream << "TOSERVER_REQUEST_MEDIA: requested file(s)" << std::endl;
  303. for (u16 i = 0; i < numfiles; i++) {
  304. std::string name;
  305. *pkt >> name;
  306. tosend.emplace_back(name);
  307. verbosestream << " " << name << std::endl;
  308. }
  309. sendRequestedMedia(peer_id, tosend);
  310. }
  311. void Server::handleCommand_ClientReady(NetworkPacket* pkt)
  312. {
  313. session_t peer_id = pkt->getPeerId();
  314. // decode all information first
  315. u8 major_ver, minor_ver, patch_ver, reserved;
  316. u16 formspec_ver = 1; // v1 for clients older than 5.1.0-dev
  317. std::string full_ver;
  318. *pkt >> major_ver >> minor_ver >> patch_ver >> reserved >> full_ver;
  319. if (pkt->getRemainingBytes() >= 2)
  320. *pkt >> formspec_ver;
  321. m_clients.setClientVersion(peer_id, major_ver, minor_ver, patch_ver,
  322. full_ver);
  323. // Emerge player
  324. PlayerSAO* playersao = StageTwoClientInit(peer_id);
  325. if (!playersao) {
  326. errorstream << "Server: stage 2 client init failed "
  327. "peer_id=" << peer_id << std::endl;
  328. DisconnectPeer(peer_id);
  329. return;
  330. }
  331. playersao->getPlayer()->formspec_version = formspec_ver;
  332. m_clients.event(peer_id, CSE_SetClientReady);
  333. // Send player list to this client
  334. {
  335. const std::vector<std::string> &players = m_clients.getPlayerNames();
  336. NetworkPacket list_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, peer_id);
  337. list_pkt << (u8) PLAYER_LIST_INIT << (u16) players.size();
  338. for (const auto &player : players)
  339. list_pkt << player;
  340. Send(peer_id, &list_pkt);
  341. }
  342. s64 last_login;
  343. m_script->getAuth(playersao->getPlayer()->getName(), nullptr, nullptr, &last_login);
  344. m_script->on_joinplayer(playersao, last_login);
  345. // Send shutdown timer if shutdown has been scheduled
  346. if (m_shutdown_state.isTimerRunning())
  347. SendChatMessage(peer_id, m_shutdown_state.getShutdownTimerMessage());
  348. }
  349. void Server::handleCommand_GotBlocks(NetworkPacket* pkt)
  350. {
  351. if (pkt->getSize() < 1)
  352. return;
  353. /*
  354. [0] u16 command
  355. [2] u8 count
  356. [3] v3s16 pos_0
  357. [3+6] v3s16 pos_1
  358. ...
  359. */
  360. u8 count;
  361. *pkt >> count;
  362. if ((s16)pkt->getSize() < 1 + (int)count * 6) {
  363. throw con::InvalidIncomingDataException
  364. ("GOTBLOCKS length is too short");
  365. }
  366. ClientInterface::AutoLock lock(m_clients);
  367. RemoteClient *client = m_clients.lockedGetClientNoEx(pkt->getPeerId());
  368. for (u16 i = 0; i < count; i++) {
  369. v3s16 p;
  370. *pkt >> p;
  371. client->GotBlock(p);
  372. }
  373. }
  374. void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
  375. NetworkPacket *pkt)
  376. {
  377. if (pkt->getRemainingBytes() < 12 + 12 + 4 + 4 + 4 + 1 + 1)
  378. return;
  379. v3s32 ps, ss;
  380. s32 f32pitch, f32yaw;
  381. u8 f32fov;
  382. *pkt >> ps;
  383. *pkt >> ss;
  384. *pkt >> f32pitch;
  385. *pkt >> f32yaw;
  386. f32 pitch = (f32)f32pitch / 100.0f;
  387. f32 yaw = (f32)f32yaw / 100.0f;
  388. u32 keyPressed = 0;
  389. f32 fov = 0;
  390. u8 wanted_range = 0;
  391. *pkt >> keyPressed;
  392. *pkt >> f32fov;
  393. fov = (f32)f32fov / 80.0f;
  394. *pkt >> wanted_range;
  395. v3f position((f32)ps.X / 100.0f, (f32)ps.Y / 100.0f, (f32)ps.Z / 100.0f);
  396. v3f speed((f32)ss.X / 100.0f, (f32)ss.Y / 100.0f, (f32)ss.Z / 100.0f);
  397. pitch = modulo360f(pitch);
  398. yaw = wrapDegrees_0_360(yaw);
  399. if (!playersao->isAttached()) {
  400. // Only update player positions when moving freely
  401. // to not interfere with attachment handling
  402. playersao->setBasePosition(position);
  403. player->setSpeed(speed);
  404. }
  405. playersao->setLookPitch(pitch);
  406. playersao->setPlayerYaw(yaw);
  407. playersao->setFov(fov);
  408. playersao->setWantedRange(wanted_range);
  409. player->control.unpackKeysPressed(keyPressed);
  410. if (playersao->checkMovementCheat()) {
  411. // Call callbacks
  412. m_script->on_cheat(playersao, "moved_too_fast");
  413. SendMovePlayer(pkt->getPeerId());
  414. }
  415. }
  416. void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
  417. {
  418. session_t peer_id = pkt->getPeerId();
  419. RemotePlayer *player = m_env->getPlayer(peer_id);
  420. if (player == NULL) {
  421. errorstream <<
  422. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  423. peer_id << " disconnecting peer!" << std::endl;
  424. DisconnectPeer(peer_id);
  425. return;
  426. }
  427. PlayerSAO *playersao = player->getPlayerSAO();
  428. if (playersao == NULL) {
  429. errorstream <<
  430. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  431. peer_id << " disconnecting peer!" << std::endl;
  432. DisconnectPeer(peer_id);
  433. return;
  434. }
  435. // If player is dead we don't care of this packet
  436. if (playersao->isDead()) {
  437. verbosestream << "TOSERVER_PLAYERPOS: " << player->getName()
  438. << " is dead. Ignoring packet";
  439. return;
  440. }
  441. process_PlayerPos(player, playersao, pkt);
  442. }
  443. void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)
  444. {
  445. if (pkt->getSize() < 1)
  446. return;
  447. /*
  448. [0] u16 command
  449. [2] u8 count
  450. [3] v3s16 pos_0
  451. [3+6] v3s16 pos_1
  452. ...
  453. */
  454. u8 count;
  455. *pkt >> count;
  456. RemoteClient *client = getClient(pkt->getPeerId());
  457. if ((s16)pkt->getSize() < 1 + (int)count * 6) {
  458. throw con::InvalidIncomingDataException
  459. ("DELETEDBLOCKS length is too short");
  460. }
  461. for (u16 i = 0; i < count; i++) {
  462. v3s16 p;
  463. *pkt >> p;
  464. client->SetBlockNotSent(p);
  465. }
  466. }
  467. void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
  468. {
  469. session_t peer_id = pkt->getPeerId();
  470. RemotePlayer *player = m_env->getPlayer(peer_id);
  471. if (player == NULL) {
  472. errorstream <<
  473. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  474. peer_id << " disconnecting peer!" << std::endl;
  475. DisconnectPeer(peer_id);
  476. return;
  477. }
  478. PlayerSAO *playersao = player->getPlayerSAO();
  479. if (playersao == NULL) {
  480. errorstream <<
  481. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  482. peer_id << " disconnecting peer!" << std::endl;
  483. DisconnectPeer(peer_id);
  484. return;
  485. }
  486. // Strip command and create a stream
  487. std::string datastring(pkt->getString(0), pkt->getSize());
  488. verbosestream << "TOSERVER_INVENTORY_ACTION: data=" << datastring
  489. << std::endl;
  490. std::istringstream is(datastring, std::ios_base::binary);
  491. // Create an action
  492. std::unique_ptr<InventoryAction> a(InventoryAction::deSerialize(is));
  493. if (!a) {
  494. infostream << "TOSERVER_INVENTORY_ACTION: "
  495. << "InventoryAction::deSerialize() returned NULL"
  496. << std::endl;
  497. return;
  498. }
  499. // If something goes wrong, this player is to blame
  500. RollbackScopeActor rollback_scope(m_rollback,
  501. std::string("player:")+player->getName());
  502. /*
  503. Note: Always set inventory not sent, to repair cases
  504. where the client made a bad prediction.
  505. */
  506. const bool player_has_interact = checkPriv(player->getName(), "interact");
  507. auto check_inv_access = [player, player_has_interact, this] (
  508. const InventoryLocation &loc) -> bool {
  509. // Players without interact may modify their own inventory
  510. if (!player_has_interact && loc.type != InventoryLocation::PLAYER) {
  511. infostream << "Cannot modify foreign inventory: "
  512. << "No interact privilege" << std::endl;
  513. return false;
  514. }
  515. switch (loc.type) {
  516. case InventoryLocation::CURRENT_PLAYER:
  517. // Only used internally on the client, never sent
  518. return false;
  519. case InventoryLocation::PLAYER:
  520. // Allow access to own inventory in all cases
  521. return loc.name == player->getName();
  522. case InventoryLocation::NODEMETA:
  523. {
  524. // Check for out-of-range interaction
  525. v3f node_pos = intToFloat(loc.p, BS);
  526. v3f player_pos = player->getPlayerSAO()->getEyePosition();
  527. f32 d = player_pos.getDistanceFrom(node_pos);
  528. return checkInteractDistance(player, d, "inventory");
  529. }
  530. case InventoryLocation::DETACHED:
  531. return getInventoryMgr()->checkDetachedInventoryAccess(loc, player->getName());
  532. default:
  533. return false;
  534. }
  535. };
  536. /*
  537. Handle restrictions and special cases of the move action
  538. */
  539. if (a->getType() == IAction::Move) {
  540. IMoveAction *ma = (IMoveAction*)a.get();
  541. ma->from_inv.applyCurrentPlayer(player->getName());
  542. ma->to_inv.applyCurrentPlayer(player->getName());
  543. m_inventory_mgr->setInventoryModified(ma->from_inv);
  544. if (ma->from_inv != ma->to_inv)
  545. m_inventory_mgr->setInventoryModified(ma->to_inv);
  546. if (!check_inv_access(ma->from_inv) ||
  547. !check_inv_access(ma->to_inv))
  548. return;
  549. /*
  550. Disable moving items out of craftpreview
  551. */
  552. if (ma->from_list == "craftpreview") {
  553. infostream << "Ignoring IMoveAction from "
  554. << (ma->from_inv.dump()) << ":" << ma->from_list
  555. << " to " << (ma->to_inv.dump()) << ":" << ma->to_list
  556. << " because src is " << ma->from_list << std::endl;
  557. return;
  558. }
  559. /*
  560. Disable moving items into craftresult and craftpreview
  561. */
  562. if (ma->to_list == "craftpreview" || ma->to_list == "craftresult") {
  563. infostream << "Ignoring IMoveAction from "
  564. << (ma->from_inv.dump()) << ":" << ma->from_list
  565. << " to " << (ma->to_inv.dump()) << ":" << ma->to_list
  566. << " because dst is " << ma->to_list << std::endl;
  567. return;
  568. }
  569. }
  570. /*
  571. Handle restrictions and special cases of the drop action
  572. */
  573. else if (a->getType() == IAction::Drop) {
  574. IDropAction *da = (IDropAction*)a.get();
  575. da->from_inv.applyCurrentPlayer(player->getName());
  576. m_inventory_mgr->setInventoryModified(da->from_inv);
  577. /*
  578. Disable dropping items out of craftpreview
  579. */
  580. if (da->from_list == "craftpreview") {
  581. infostream << "Ignoring IDropAction from "
  582. << (da->from_inv.dump()) << ":" << da->from_list
  583. << " because src is " << da->from_list << std::endl;
  584. return;
  585. }
  586. // Disallow dropping items if not allowed to interact
  587. if (!player_has_interact || !check_inv_access(da->from_inv))
  588. return;
  589. // Disallow dropping items if dead
  590. if (playersao->isDead()) {
  591. infostream << "Ignoring IDropAction from "
  592. << (da->from_inv.dump()) << ":" << da->from_list
  593. << " because player is dead." << std::endl;
  594. return;
  595. }
  596. }
  597. /*
  598. Handle restrictions and special cases of the craft action
  599. */
  600. else if (a->getType() == IAction::Craft) {
  601. ICraftAction *ca = (ICraftAction*)a.get();
  602. ca->craft_inv.applyCurrentPlayer(player->getName());
  603. m_inventory_mgr->setInventoryModified(ca->craft_inv);
  604. // Disallow crafting if not allowed to interact
  605. if (!player_has_interact) {
  606. infostream << "Cannot craft: "
  607. << "No interact privilege" << std::endl;
  608. return;
  609. }
  610. if (!check_inv_access(ca->craft_inv))
  611. return;
  612. } else {
  613. // Unknown action. Ignored.
  614. return;
  615. }
  616. // Do the action
  617. a->apply(m_inventory_mgr.get(), playersao, this);
  618. }
  619. void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
  620. {
  621. std::wstring message;
  622. *pkt >> message;
  623. session_t peer_id = pkt->getPeerId();
  624. RemotePlayer *player = m_env->getPlayer(peer_id);
  625. if (player == NULL) {
  626. errorstream <<
  627. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  628. peer_id << " disconnecting peer!" << std::endl;
  629. DisconnectPeer(peer_id);
  630. return;
  631. }
  632. std::string name = player->getName();
  633. std::wstring answer_to_sender = handleChat(name, message, true, player);
  634. if (!answer_to_sender.empty()) {
  635. // Send the answer to sender
  636. SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  637. answer_to_sender));
  638. }
  639. }
  640. void Server::handleCommand_Damage(NetworkPacket* pkt)
  641. {
  642. u16 damage;
  643. *pkt >> damage;
  644. session_t peer_id = pkt->getPeerId();
  645. RemotePlayer *player = m_env->getPlayer(peer_id);
  646. if (player == NULL) {
  647. errorstream <<
  648. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  649. peer_id << " disconnecting peer!" << std::endl;
  650. DisconnectPeer(peer_id);
  651. return;
  652. }
  653. PlayerSAO *playersao = player->getPlayerSAO();
  654. if (playersao == NULL) {
  655. errorstream <<
  656. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  657. peer_id << " disconnecting peer!" << std::endl;
  658. DisconnectPeer(peer_id);
  659. return;
  660. }
  661. if (!playersao->isImmortal()) {
  662. if (playersao->isDead()) {
  663. verbosestream << "Server::ProcessData(): Info: "
  664. "Ignoring damage as player " << player->getName()
  665. << " is already dead." << std::endl;
  666. return;
  667. }
  668. actionstream << player->getName() << " damaged by "
  669. << (int)damage << " hp at " << PP(playersao->getBasePosition() / BS)
  670. << std::endl;
  671. PlayerHPChangeReason reason(PlayerHPChangeReason::FALL);
  672. playersao->setHP((s32)playersao->getHP() - (s32)damage, reason, true);
  673. }
  674. }
  675. void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
  676. {
  677. if (pkt->getSize() < 2)
  678. return;
  679. session_t peer_id = pkt->getPeerId();
  680. RemotePlayer *player = m_env->getPlayer(peer_id);
  681. if (player == NULL) {
  682. errorstream <<
  683. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  684. peer_id << " disconnecting peer!" << std::endl;
  685. DisconnectPeer(peer_id);
  686. return;
  687. }
  688. PlayerSAO *playersao = player->getPlayerSAO();
  689. if (playersao == NULL) {
  690. errorstream <<
  691. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  692. peer_id << " disconnecting peer!" << std::endl;
  693. DisconnectPeer(peer_id);
  694. return;
  695. }
  696. u16 item;
  697. *pkt >> item;
  698. if (item >= player->getHotbarItemcount()) {
  699. actionstream << "Player: " << player->getName()
  700. << " tried to access item=" << item
  701. << " out of hotbar_itemcount="
  702. << player->getHotbarItemcount()
  703. << "; ignoring." << std::endl;
  704. return;
  705. }
  706. playersao->getPlayer()->setWieldIndex(item);
  707. }
  708. void Server::handleCommand_Respawn(NetworkPacket* pkt)
  709. {
  710. session_t peer_id = pkt->getPeerId();
  711. RemotePlayer *player = m_env->getPlayer(peer_id);
  712. if (player == NULL) {
  713. errorstream <<
  714. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  715. peer_id << " disconnecting peer!" << std::endl;
  716. DisconnectPeer(peer_id);
  717. return;
  718. }
  719. PlayerSAO *playersao = player->getPlayerSAO();
  720. assert(playersao);
  721. if (!playersao->isDead())
  722. return;
  723. RespawnPlayer(peer_id);
  724. actionstream << player->getName() << " respawns at "
  725. << PP(playersao->getBasePosition() / BS) << std::endl;
  726. // ActiveObject is added to environment in AsyncRunStep after
  727. // the previous addition has been successfully removed
  728. }
  729. bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what)
  730. {
  731. ItemStack selected_item, hand_item;
  732. player->getWieldedItem(&selected_item, &hand_item);
  733. f32 max_d = BS * getToolRange(selected_item.getDefinition(m_itemdef),
  734. hand_item.getDefinition(m_itemdef));
  735. // Cube diagonal * 1.5 for maximal supported node extents:
  736. // sqrt(3) * 1.5 ≅ 2.6
  737. if (d > max_d + 2.6f * BS) {
  738. actionstream << "Player " << player->getName()
  739. << " tried to access " << what
  740. << " from too far: "
  741. << "d=" << d << ", max_d=" << max_d
  742. << "; ignoring." << std::endl;
  743. // Call callbacks
  744. m_script->on_cheat(player->getPlayerSAO(), "interacted_too_far");
  745. return false;
  746. }
  747. return true;
  748. }
  749. // Tiny helper to retrieve the selected item into an Optional
  750. static inline void getWieldedItem(const PlayerSAO *playersao, Optional<ItemStack> &ret)
  751. {
  752. ret = ItemStack();
  753. playersao->getWieldedItem(&(*ret));
  754. }
  755. void Server::handleCommand_Interact(NetworkPacket *pkt)
  756. {
  757. /*
  758. [0] u16 command
  759. [2] u8 action
  760. [3] u16 item
  761. [5] u32 length of the next item (plen)
  762. [9] serialized PointedThing
  763. [9 + plen] player position information
  764. */
  765. InteractAction action;
  766. u16 item_i;
  767. *pkt >> (u8 &)action;
  768. *pkt >> item_i;
  769. std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
  770. PointedThing pointed;
  771. pointed.deSerialize(tmp_is);
  772. verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item="
  773. << item_i << ", pointed=" << pointed.dump() << std::endl;
  774. session_t peer_id = pkt->getPeerId();
  775. RemotePlayer *player = m_env->getPlayer(peer_id);
  776. if (player == NULL) {
  777. errorstream <<
  778. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  779. peer_id << " disconnecting peer!" << std::endl;
  780. DisconnectPeer(peer_id);
  781. return;
  782. }
  783. PlayerSAO *playersao = player->getPlayerSAO();
  784. if (playersao == NULL) {
  785. errorstream <<
  786. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  787. peer_id << " disconnecting peer!" << std::endl;
  788. DisconnectPeer(peer_id);
  789. return;
  790. }
  791. if (playersao->isDead()) {
  792. actionstream << "Server: " << player->getName()
  793. << " tried to interact while dead; ignoring." << std::endl;
  794. if (pointed.type == POINTEDTHING_NODE) {
  795. // Re-send block to revert change on client-side
  796. RemoteClient *client = getClient(peer_id);
  797. v3s16 blockpos = getNodeBlockPos(pointed.node_undersurface);
  798. client->SetBlockNotSent(blockpos);
  799. }
  800. // Call callbacks
  801. m_script->on_cheat(playersao, "interacted_while_dead");
  802. return;
  803. }
  804. process_PlayerPos(player, playersao, pkt);
  805. v3f player_pos = playersao->getLastGoodPosition();
  806. // Update wielded item
  807. if (item_i >= player->getHotbarItemcount()) {
  808. actionstream << "Player: " << player->getName()
  809. << " tried to access item=" << item_i
  810. << " out of hotbar_itemcount="
  811. << player->getHotbarItemcount()
  812. << "; ignoring." << std::endl;
  813. return;
  814. }
  815. playersao->getPlayer()->setWieldIndex(item_i);
  816. // Get pointed to object (NULL if not POINTEDTYPE_OBJECT)
  817. ServerActiveObject *pointed_object = NULL;
  818. if (pointed.type == POINTEDTHING_OBJECT) {
  819. pointed_object = m_env->getActiveObject(pointed.object_id);
  820. if (pointed_object == NULL) {
  821. verbosestream << "TOSERVER_INTERACT: "
  822. "pointed object is NULL" << std::endl;
  823. return;
  824. }
  825. }
  826. /*
  827. Make sure the player is allowed to do it
  828. */
  829. if (!checkPriv(player->getName(), "interact")) {
  830. actionstream << player->getName() << " attempted to interact with " <<
  831. pointed.dump() << " without 'interact' privilege" << std::endl;
  832. if (pointed.type != POINTEDTHING_NODE)
  833. return;
  834. // Re-send block to revert change on client-side
  835. RemoteClient *client = getClient(peer_id);
  836. // Digging completed -> under
  837. if (action == INTERACT_DIGGING_COMPLETED) {
  838. v3s16 blockpos = getNodeBlockPos(pointed.node_undersurface);
  839. client->SetBlockNotSent(blockpos);
  840. }
  841. // Placement -> above
  842. else if (action == INTERACT_PLACE) {
  843. v3s16 blockpos = getNodeBlockPos(pointed.node_abovesurface);
  844. client->SetBlockNotSent(blockpos);
  845. }
  846. return;
  847. }
  848. /*
  849. Check that target is reasonably close
  850. */
  851. static thread_local const bool enable_anticheat =
  852. !g_settings->getBool("disable_anticheat");
  853. if ((action == INTERACT_START_DIGGING || action == INTERACT_DIGGING_COMPLETED ||
  854. action == INTERACT_PLACE || action == INTERACT_USE) &&
  855. enable_anticheat && !isSingleplayer()) {
  856. v3f target_pos = player_pos;
  857. if (pointed.type == POINTEDTHING_NODE) {
  858. target_pos = intToFloat(pointed.node_undersurface, BS);
  859. } else if (pointed.type == POINTEDTHING_OBJECT) {
  860. if (playersao->getId() == pointed_object->getId()) {
  861. actionstream << "Server: " << player->getName()
  862. << " attempted to interact with themselves" << std::endl;
  863. m_script->on_cheat(playersao, "interacted_with_self");
  864. return;
  865. }
  866. target_pos = pointed_object->getBasePosition();
  867. }
  868. float d = playersao->getEyePosition().getDistanceFrom(target_pos);
  869. if (!checkInteractDistance(player, d, pointed.dump())) {
  870. if (pointed.type == POINTEDTHING_NODE) {
  871. // Re-send block to revert change on client-side
  872. RemoteClient *client = getClient(peer_id);
  873. v3s16 blockpos = getNodeBlockPos(pointed.node_undersurface);
  874. client->SetBlockNotSent(blockpos);
  875. }
  876. return;
  877. }
  878. }
  879. /*
  880. If something goes wrong, this player is to blame
  881. */
  882. RollbackScopeActor rollback_scope(m_rollback,
  883. std::string("player:")+player->getName());
  884. switch (action) {
  885. // Start digging or punch object
  886. case INTERACT_START_DIGGING: {
  887. if (pointed.type == POINTEDTHING_NODE) {
  888. MapNode n(CONTENT_IGNORE);
  889. bool pos_ok;
  890. v3s16 p_under = pointed.node_undersurface;
  891. n = m_env->getMap().getNode(p_under, &pos_ok);
  892. if (!pos_ok) {
  893. infostream << "Server: Not punching: Node not found. "
  894. "Adding block to emerge queue." << std::endl;
  895. m_emerge->enqueueBlockEmerge(peer_id,
  896. getNodeBlockPos(pointed.node_abovesurface), false);
  897. }
  898. if (n.getContent() != CONTENT_IGNORE)
  899. m_script->node_on_punch(p_under, n, playersao, pointed);
  900. // Cheat prevention
  901. playersao->noCheatDigStart(p_under);
  902. return;
  903. }
  904. // Skip if the object can't be interacted with anymore
  905. if (pointed.type != POINTEDTHING_OBJECT || pointed_object->isGone())
  906. return;
  907. ItemStack selected_item, hand_item;
  908. ItemStack tool_item = playersao->getWieldedItem(&selected_item, &hand_item);
  909. ToolCapabilities toolcap =
  910. tool_item.getToolCapabilities(m_itemdef);
  911. v3f dir = (pointed_object->getBasePosition() -
  912. (playersao->getBasePosition() + playersao->getEyeOffset())
  913. ).normalize();
  914. float time_from_last_punch =
  915. playersao->resetTimeFromLastPunch();
  916. u32 wear = pointed_object->punch(dir, &toolcap, playersao,
  917. time_from_last_punch, tool_item.wear);
  918. // Callback may have changed item, so get it again
  919. playersao->getWieldedItem(&selected_item);
  920. bool changed = selected_item.addWear(wear, m_itemdef);
  921. if (changed)
  922. playersao->setWieldedItem(selected_item);
  923. return;
  924. } // action == INTERACT_START_DIGGING
  925. case INTERACT_STOP_DIGGING:
  926. // Nothing to do
  927. return;
  928. case INTERACT_DIGGING_COMPLETED: {
  929. // Only digging of nodes
  930. if (pointed.type != POINTEDTHING_NODE)
  931. return;
  932. bool pos_ok;
  933. v3s16 p_under = pointed.node_undersurface;
  934. MapNode n = m_env->getMap().getNode(p_under, &pos_ok);
  935. if (!pos_ok) {
  936. infostream << "Server: Not finishing digging: Node not found. "
  937. "Adding block to emerge queue." << std::endl;
  938. m_emerge->enqueueBlockEmerge(peer_id,
  939. getNodeBlockPos(pointed.node_abovesurface), false);
  940. }
  941. /* Cheat prevention */
  942. bool is_valid_dig = true;
  943. if (enable_anticheat && !isSingleplayer()) {
  944. v3s16 nocheat_p = playersao->getNoCheatDigPos();
  945. float nocheat_t = playersao->getNoCheatDigTime();
  946. playersao->noCheatDigEnd();
  947. // If player didn't start digging this, ignore dig
  948. if (nocheat_p != p_under) {
  949. infostream << "Server: " << player->getName()
  950. << " started digging "
  951. << PP(nocheat_p) << " and completed digging "
  952. << PP(p_under) << "; not digging." << std::endl;
  953. is_valid_dig = false;
  954. // Call callbacks
  955. m_script->on_cheat(playersao, "finished_unknown_dig");
  956. }
  957. // Get player's wielded item
  958. // See also: Game::handleDigging
  959. ItemStack selected_item, hand_item;
  960. playersao->getPlayer()->getWieldedItem(&selected_item, &hand_item);
  961. // Get diggability and expected digging time
  962. DigParams params = getDigParams(m_nodedef->get(n).groups,
  963. &selected_item.getToolCapabilities(m_itemdef),
  964. selected_item.wear);
  965. // If can't dig, try hand
  966. if (!params.diggable) {
  967. params = getDigParams(m_nodedef->get(n).groups,
  968. &hand_item.getToolCapabilities(m_itemdef));
  969. }
  970. // If can't dig, ignore dig
  971. if (!params.diggable) {
  972. infostream << "Server: " << player->getName()
  973. << " completed digging " << PP(p_under)
  974. << ", which is not diggable with tool; not digging."
  975. << std::endl;
  976. is_valid_dig = false;
  977. // Call callbacks
  978. m_script->on_cheat(playersao, "dug_unbreakable");
  979. }
  980. // Check digging time
  981. // If already invalidated, we don't have to
  982. if (!is_valid_dig) {
  983. // Well not our problem then
  984. }
  985. // Clean and long dig
  986. else if (params.time > 2.0 && nocheat_t * 1.2 > params.time) {
  987. // All is good, but grab time from pool; don't care if
  988. // it's actually available
  989. playersao->getDigPool().grab(params.time);
  990. }
  991. // Short or laggy dig
  992. // Try getting the time from pool
  993. else if (playersao->getDigPool().grab(params.time)) {
  994. // All is good
  995. }
  996. // Dig not possible
  997. else {
  998. infostream << "Server: " << player->getName()
  999. << " completed digging " << PP(p_under)
  1000. << "too fast; not digging." << std::endl;
  1001. is_valid_dig = false;
  1002. // Call callbacks
  1003. m_script->on_cheat(playersao, "dug_too_fast");
  1004. }
  1005. }
  1006. /* Actually dig node */
  1007. if (is_valid_dig && n.getContent() != CONTENT_IGNORE)
  1008. m_script->node_on_dig(p_under, n, playersao);
  1009. v3s16 blockpos = getNodeBlockPos(p_under);
  1010. RemoteClient *client = getClient(peer_id);
  1011. // Send unusual result (that is, node not being removed)
  1012. if (m_env->getMap().getNode(p_under).getContent() != CONTENT_AIR)
  1013. // Re-send block to revert change on client-side
  1014. client->SetBlockNotSent(blockpos);
  1015. else
  1016. client->ResendBlockIfOnWire(blockpos);
  1017. return;
  1018. } // action == INTERACT_DIGGING_COMPLETED
  1019. // Place block or right-click object
  1020. case INTERACT_PLACE: {
  1021. Optional<ItemStack> selected_item;
  1022. getWieldedItem(playersao, selected_item);
  1023. // Reset build time counter
  1024. if (pointed.type == POINTEDTHING_NODE &&
  1025. selected_item->getDefinition(m_itemdef).type == ITEM_NODE)
  1026. getClient(peer_id)->m_time_from_building = 0.0;
  1027. const bool had_prediction = !selected_item->getDefinition(m_itemdef).
  1028. node_placement_prediction.empty();
  1029. if (pointed.type == POINTEDTHING_OBJECT) {
  1030. // Right click object
  1031. // Skip if object can't be interacted with anymore
  1032. if (pointed_object->isGone())
  1033. return;
  1034. actionstream << player->getName() << " right-clicks object "
  1035. << pointed.object_id << ": "
  1036. << pointed_object->getDescription() << std::endl;
  1037. // Do stuff
  1038. if (m_script->item_OnSecondaryUse(selected_item, playersao, pointed)) {
  1039. if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
  1040. SendInventory(playersao, true);
  1041. }
  1042. pointed_object->rightClick(playersao);
  1043. } else if (m_script->item_OnPlace(selected_item, playersao, pointed)) {
  1044. // Placement was handled in lua
  1045. // Apply returned ItemStack
  1046. if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
  1047. SendInventory(playersao, true);
  1048. }
  1049. if (pointed.type != POINTEDTHING_NODE)
  1050. return;
  1051. // If item has node placement prediction, always send the
  1052. // blocks to make sure the client knows what exactly happened
  1053. RemoteClient *client = getClient(peer_id);
  1054. v3s16 blockpos = getNodeBlockPos(pointed.node_abovesurface);
  1055. v3s16 blockpos2 = getNodeBlockPos(pointed.node_undersurface);
  1056. if (had_prediction) {
  1057. client->SetBlockNotSent(blockpos);
  1058. if (blockpos2 != blockpos)
  1059. client->SetBlockNotSent(blockpos2);
  1060. } else {
  1061. client->ResendBlockIfOnWire(blockpos);
  1062. if (blockpos2 != blockpos)
  1063. client->ResendBlockIfOnWire(blockpos2);
  1064. }
  1065. return;
  1066. } // action == INTERACT_PLACE
  1067. case INTERACT_USE: {
  1068. Optional<ItemStack> selected_item;
  1069. getWieldedItem(playersao, selected_item);
  1070. actionstream << player->getName() << " uses " << selected_item->name
  1071. << ", pointing at " << pointed.dump() << std::endl;
  1072. if (m_script->item_OnUse(selected_item, playersao, pointed)) {
  1073. // Apply returned ItemStack
  1074. if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
  1075. SendInventory(playersao, true);
  1076. }
  1077. return;
  1078. }
  1079. // Rightclick air
  1080. case INTERACT_ACTIVATE: {
  1081. Optional<ItemStack> selected_item;
  1082. getWieldedItem(playersao, selected_item);
  1083. actionstream << player->getName() << " activates "
  1084. << selected_item->name << std::endl;
  1085. pointed.type = POINTEDTHING_NOTHING; // can only ever be NOTHING
  1086. if (m_script->item_OnSecondaryUse(selected_item, playersao, pointed)) {
  1087. // Apply returned ItemStack
  1088. if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
  1089. SendInventory(playersao, true);
  1090. }
  1091. return;
  1092. }
  1093. default:
  1094. warningstream << "Server: Invalid action " << action << std::endl;
  1095. }
  1096. }
  1097. void Server::handleCommand_RemovedSounds(NetworkPacket* pkt)
  1098. {
  1099. u16 num;
  1100. *pkt >> num;
  1101. for (u16 k = 0; k < num; k++) {
  1102. s32 id;
  1103. *pkt >> id;
  1104. std::unordered_map<s32, ServerPlayingSound>::iterator i =
  1105. m_playing_sounds.find(id);
  1106. if (i == m_playing_sounds.end())
  1107. continue;
  1108. ServerPlayingSound &psound = i->second;
  1109. psound.clients.erase(pkt->getPeerId());
  1110. if (psound.clients.empty())
  1111. m_playing_sounds.erase(i++);
  1112. }
  1113. }
  1114. void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
  1115. {
  1116. v3s16 p;
  1117. std::string formname;
  1118. u16 num;
  1119. *pkt >> p >> formname >> num;
  1120. StringMap fields;
  1121. for (u16 k = 0; k < num; k++) {
  1122. std::string fieldname;
  1123. *pkt >> fieldname;
  1124. fields[fieldname] = pkt->readLongString();
  1125. }
  1126. session_t peer_id = pkt->getPeerId();
  1127. RemotePlayer *player = m_env->getPlayer(peer_id);
  1128. if (player == NULL) {
  1129. errorstream <<
  1130. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  1131. peer_id << " disconnecting peer!" << std::endl;
  1132. DisconnectPeer(peer_id);
  1133. return;
  1134. }
  1135. PlayerSAO *playersao = player->getPlayerSAO();
  1136. if (playersao == NULL) {
  1137. errorstream <<
  1138. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  1139. peer_id << " disconnecting peer!" << std::endl;
  1140. DisconnectPeer(peer_id);
  1141. return;
  1142. }
  1143. // If something goes wrong, this player is to blame
  1144. RollbackScopeActor rollback_scope(m_rollback,
  1145. std::string("player:")+player->getName());
  1146. // Check the target node for rollback data; leave others unnoticed
  1147. RollbackNode rn_old(&m_env->getMap(), p, this);
  1148. m_script->node_on_receive_fields(p, formname, fields, playersao);
  1149. // Report rollback data
  1150. RollbackNode rn_new(&m_env->getMap(), p, this);
  1151. if (rollback() && rn_new != rn_old) {
  1152. RollbackAction action;
  1153. action.setSetNode(p, rn_old, rn_new);
  1154. rollback()->reportAction(action);
  1155. }
  1156. }
  1157. void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
  1158. {
  1159. std::string client_formspec_name;
  1160. u16 num;
  1161. *pkt >> client_formspec_name >> num;
  1162. StringMap fields;
  1163. for (u16 k = 0; k < num; k++) {
  1164. std::string fieldname;
  1165. *pkt >> fieldname;
  1166. fields[fieldname] = pkt->readLongString();
  1167. }
  1168. session_t peer_id = pkt->getPeerId();
  1169. RemotePlayer *player = m_env->getPlayer(peer_id);
  1170. if (player == NULL) {
  1171. errorstream <<
  1172. "Server::ProcessData(): Canceling: No player for peer_id=" <<
  1173. peer_id << " disconnecting peer!" << std::endl;
  1174. DisconnectPeer(peer_id);
  1175. return;
  1176. }
  1177. PlayerSAO *playersao = player->getPlayerSAO();
  1178. if (playersao == NULL) {
  1179. errorstream <<
  1180. "Server::ProcessData(): Canceling: No player object for peer_id=" <<
  1181. peer_id << " disconnecting peer!" << std::endl;
  1182. DisconnectPeer(peer_id);
  1183. return;
  1184. }
  1185. if (client_formspec_name.empty()) { // pass through inventory submits
  1186. m_script->on_playerReceiveFields(playersao, client_formspec_name, fields);
  1187. return;
  1188. }
  1189. // verify that we displayed the formspec to the user
  1190. const auto peer_state_iterator = m_formspec_state_data.find(peer_id);
  1191. if (peer_state_iterator != m_formspec_state_data.end()) {
  1192. const std::string &server_formspec_name = peer_state_iterator->second;
  1193. if (client_formspec_name == server_formspec_name) {
  1194. auto it = fields.find("quit");
  1195. if (it != fields.end() && it->second == "true")
  1196. m_formspec_state_data.erase(peer_state_iterator);
  1197. m_script->on_playerReceiveFields(playersao, client_formspec_name, fields);
  1198. return;
  1199. }
  1200. actionstream << "'" << player->getName()
  1201. << "' submitted formspec ('" << client_formspec_name
  1202. << "') but the name of the formspec doesn't match the"
  1203. " expected name ('" << server_formspec_name << "')";
  1204. } else {
  1205. actionstream << "'" << player->getName()
  1206. << "' submitted formspec ('" << client_formspec_name
  1207. << "') but server hasn't sent formspec to client";
  1208. }
  1209. actionstream << ", possible exploitation attempt" << std::endl;
  1210. }
  1211. void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
  1212. {
  1213. session_t peer_id = pkt->getPeerId();
  1214. RemoteClient *client = getClient(peer_id, CS_Invalid);
  1215. ClientState cstate = client->getState();
  1216. const std::string playername = client->getName();
  1217. std::string salt, verification_key;
  1218. std::string addr_s = getPeerAddress(peer_id).serializeString();
  1219. u8 is_empty;
  1220. *pkt >> salt >> verification_key >> is_empty;
  1221. verbosestream << "Server: Got TOSERVER_FIRST_SRP from " << addr_s
  1222. << ", with is_empty=" << (is_empty == 1) << std::endl;
  1223. const bool empty_disallowed = !isSingleplayer() && is_empty == 1 &&
  1224. g_settings->getBool("disallow_empty_password");
  1225. // Either this packet is sent because the user is new or to change the password
  1226. if (cstate == CS_HelloSent) {
  1227. if (!client->isMechAllowed(AUTH_MECHANISM_FIRST_SRP)) {
  1228. actionstream << "Server: Client from " << addr_s
  1229. << " tried to set password without being "
  1230. << "authenticated, or the username being new." << std::endl;
  1231. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1232. return;
  1233. }
  1234. if (empty_disallowed) {
  1235. actionstream << "Server: " << playername
  1236. << " supplied empty password from " << addr_s << std::endl;
  1237. DenyAccess(peer_id, SERVER_ACCESSDENIED_EMPTY_PASSWORD);
  1238. return;
  1239. }
  1240. std::string initial_ver_key;
  1241. initial_ver_key = encode_srp_verifier(verification_key, salt);
  1242. // It is possible for multiple connections to get this far with the same
  1243. // player name. In the end only one player with a given name will be emerged
  1244. // (see Server::StateTwoClientInit) but we still have to be careful here.
  1245. if (m_script->getAuth(playername, nullptr, nullptr)) {
  1246. // Another client beat us to it
  1247. actionstream << "Server: Client from " << addr_s
  1248. << " tried to register " << playername << " a second time."
  1249. << std::endl;
  1250. DenyAccess(peer_id, SERVER_ACCESSDENIED_ALREADY_CONNECTED);
  1251. return;
  1252. }
  1253. m_script->createAuth(playername, initial_ver_key);
  1254. m_script->on_authplayer(playername, addr_s, true);
  1255. acceptAuth(peer_id, false);
  1256. } else {
  1257. if (cstate < CS_SudoMode) {
  1258. infostream << "Server::ProcessData(): Ignoring TOSERVER_FIRST_SRP from "
  1259. << addr_s << ": " << "Client has wrong state " << cstate << "."
  1260. << std::endl;
  1261. return;
  1262. }
  1263. m_clients.event(peer_id, CSE_SudoLeave);
  1264. if (empty_disallowed) {
  1265. actionstream << "Server: " << playername
  1266. << " supplied empty password" << std::endl;
  1267. SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  1268. L"Changing to an empty password is not allowed."));
  1269. return;
  1270. }
  1271. std::string pw_db_field = encode_srp_verifier(verification_key, salt);
  1272. bool success = m_script->setPassword(playername, pw_db_field);
  1273. if (success) {
  1274. actionstream << playername << " changes password" << std::endl;
  1275. SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  1276. L"Password change successful."));
  1277. } else {
  1278. actionstream << playername <<
  1279. " tries to change password but it fails" << std::endl;
  1280. SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  1281. L"Password change failed or unavailable."));
  1282. }
  1283. }
  1284. }
  1285. void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
  1286. {
  1287. session_t peer_id = pkt->getPeerId();
  1288. RemoteClient *client = getClient(peer_id, CS_Invalid);
  1289. ClientState cstate = client->getState();
  1290. if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
  1291. actionstream << "Server: got SRP _A packet in wrong state " << cstate <<
  1292. " from " << getPeerAddress(peer_id).serializeString() <<
  1293. ". Ignoring." << std::endl;
  1294. return;
  1295. }
  1296. const bool wantSudo = (cstate == CS_Active);
  1297. if (client->chosen_mech != AUTH_MECHANISM_NONE) {
  1298. actionstream << "Server: got SRP _A packet, while auth is already "
  1299. "going on with mech " << client->chosen_mech << " from " <<
  1300. getPeerAddress(peer_id).serializeString() <<
  1301. " (wantSudo=" << wantSudo << "). Ignoring." << std::endl;
  1302. if (wantSudo) {
  1303. DenySudoAccess(peer_id);
  1304. return;
  1305. }
  1306. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1307. return;
  1308. }
  1309. std::string bytes_A;
  1310. u8 based_on;
  1311. *pkt >> bytes_A >> based_on;
  1312. infostream << "Server: TOSERVER_SRP_BYTES_A received with "
  1313. << "based_on=" << int(based_on) << " and len_A="
  1314. << bytes_A.length() << "." << std::endl;
  1315. AuthMechanism chosen = (based_on == 0) ?
  1316. AUTH_MECHANISM_LEGACY_PASSWORD : AUTH_MECHANISM_SRP;
  1317. if (wantSudo) {
  1318. if (!client->isSudoMechAllowed(chosen)) {
  1319. actionstream << "Server: Player \"" << client->getName() <<
  1320. "\" at " << getPeerAddress(peer_id).serializeString() <<
  1321. " tried to change password using unallowed mech " << chosen <<
  1322. "." << std::endl;
  1323. DenySudoAccess(peer_id);
  1324. return;
  1325. }
  1326. } else {
  1327. if (!client->isMechAllowed(chosen)) {
  1328. actionstream << "Server: Client tried to authenticate from " <<
  1329. getPeerAddress(peer_id).serializeString() <<
  1330. " using unallowed mech " << chosen << "." << std::endl;
  1331. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1332. return;
  1333. }
  1334. }
  1335. client->chosen_mech = chosen;
  1336. std::string salt, verifier;
  1337. if (based_on == 0) {
  1338. generate_srp_verifier_and_salt(client->getName(), client->enc_pwd,
  1339. &verifier, &salt);
  1340. } else if (!decode_srp_verifier_and_salt(client->enc_pwd, &verifier, &salt)) {
  1341. // Non-base64 errors should have been catched in the init handler
  1342. actionstream << "Server: User " << client->getName() <<
  1343. " tried to log in, but srp verifier field was invalid (most likely "
  1344. "invalid base64)." << std::endl;
  1345. DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
  1346. return;
  1347. }
  1348. char *bytes_B = 0;
  1349. size_t len_B = 0;
  1350. client->auth_data = srp_verifier_new(SRP_SHA256, SRP_NG_2048,
  1351. client->getName().c_str(),
  1352. (const unsigned char *) salt.c_str(), salt.size(),
  1353. (const unsigned char *) verifier.c_str(), verifier.size(),
  1354. (const unsigned char *) bytes_A.c_str(), bytes_A.size(),
  1355. NULL, 0,
  1356. (unsigned char **) &bytes_B, &len_B, NULL, NULL);
  1357. if (!bytes_B) {
  1358. actionstream << "Server: User " << client->getName()
  1359. << " tried to log in, SRP-6a safety check violated in _A handler."
  1360. << std::endl;
  1361. if (wantSudo) {
  1362. DenySudoAccess(peer_id);
  1363. client->resetChosenMech();
  1364. return;
  1365. }
  1366. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1367. return;
  1368. }
  1369. NetworkPacket resp_pkt(TOCLIENT_SRP_BYTES_S_B, 0, peer_id);
  1370. resp_pkt << salt << std::string(bytes_B, len_B);
  1371. Send(&resp_pkt);
  1372. }
  1373. void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
  1374. {
  1375. session_t peer_id = pkt->getPeerId();
  1376. RemoteClient *client = getClient(peer_id, CS_Invalid);
  1377. ClientState cstate = client->getState();
  1378. const std::string addr_s = client->getAddress().serializeString();
  1379. const std::string playername = client->getName();
  1380. const bool wantSudo = (cstate == CS_Active);
  1381. verbosestream << "Server: Received TOSERVER_SRP_BYTES_M." << std::endl;
  1382. if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
  1383. warningstream << "Server: got SRP_M packet in wrong state "
  1384. << cstate << " from " << addr_s << ". Ignoring." << std::endl;
  1385. return;
  1386. }
  1387. if (client->chosen_mech != AUTH_MECHANISM_SRP &&
  1388. client->chosen_mech != AUTH_MECHANISM_LEGACY_PASSWORD) {
  1389. warningstream << "Server: got SRP_M packet, while auth "
  1390. "is going on with mech " << client->chosen_mech << " from "
  1391. << addr_s << " (wantSudo=" << wantSudo << "). Denying." << std::endl;
  1392. if (wantSudo) {
  1393. DenySudoAccess(peer_id);
  1394. return;
  1395. }
  1396. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1397. return;
  1398. }
  1399. std::string bytes_M;
  1400. *pkt >> bytes_M;
  1401. if (srp_verifier_get_session_key_length((SRPVerifier *) client->auth_data)
  1402. != bytes_M.size()) {
  1403. actionstream << "Server: User " << playername << " at " << addr_s
  1404. << " sent bytes_M with invalid length " << bytes_M.size() << std::endl;
  1405. DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1406. return;
  1407. }
  1408. unsigned char *bytes_HAMK = 0;
  1409. srp_verifier_verify_session((SRPVerifier *) client->auth_data,
  1410. (unsigned char *)bytes_M.c_str(), &bytes_HAMK);
  1411. if (!bytes_HAMK) {
  1412. if (wantSudo) {
  1413. actionstream << "Server: User " << playername << " at " << addr_s
  1414. << " tried to change their password, but supplied wrong"
  1415. << " (SRP) password for authentication." << std::endl;
  1416. DenySudoAccess(peer_id);
  1417. client->resetChosenMech();
  1418. return;
  1419. }
  1420. actionstream << "Server: User " << playername << " at " << addr_s
  1421. << " supplied wrong password (auth mechanism: SRP)." << std::endl;
  1422. m_script->on_authplayer(playername, addr_s, false);
  1423. DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_PASSWORD);
  1424. return;
  1425. }
  1426. if (client->create_player_on_auth_success) {
  1427. m_script->createAuth(playername, client->enc_pwd);
  1428. if (!m_script->getAuth(playername, nullptr, nullptr)) {
  1429. errorstream << "Server: " << playername <<
  1430. " cannot be authenticated (auth handler does not work?)" <<
  1431. std::endl;
  1432. DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
  1433. return;
  1434. }
  1435. client->create_player_on_auth_success = false;
  1436. }
  1437. m_script->on_authplayer(playername, addr_s, true);
  1438. acceptAuth(peer_id, wantSudo);
  1439. }
  1440. /*
  1441. * Mod channels
  1442. */
  1443. void Server::handleCommand_ModChannelJoin(NetworkPacket *pkt)
  1444. {
  1445. std::string channel_name;
  1446. *pkt >> channel_name;
  1447. session_t peer_id = pkt->getPeerId();
  1448. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
  1449. 1 + 2 + channel_name.size(), peer_id);
  1450. // Send signal to client to notify join succeed or not
  1451. if (g_settings->getBool("enable_mod_channels") &&
  1452. m_modchannel_mgr->joinChannel(channel_name, peer_id)) {
  1453. resp_pkt << (u8) MODCHANNEL_SIGNAL_JOIN_OK;
  1454. infostream << "Peer " << peer_id << " joined channel " <<
  1455. channel_name << std::endl;
  1456. }
  1457. else {
  1458. resp_pkt << (u8)MODCHANNEL_SIGNAL_JOIN_FAILURE;
  1459. infostream << "Peer " << peer_id << " tried to join channel " <<
  1460. channel_name << ", but was already registered." << std::endl;
  1461. }
  1462. resp_pkt << channel_name;
  1463. Send(&resp_pkt);
  1464. }
  1465. void Server::handleCommand_ModChannelLeave(NetworkPacket *pkt)
  1466. {
  1467. std::string channel_name;
  1468. *pkt >> channel_name;
  1469. session_t peer_id = pkt->getPeerId();
  1470. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
  1471. 1 + 2 + channel_name.size(), peer_id);
  1472. // Send signal to client to notify join succeed or not
  1473. if (g_settings->getBool("enable_mod_channels") &&
  1474. m_modchannel_mgr->leaveChannel(channel_name, peer_id)) {
  1475. resp_pkt << (u8)MODCHANNEL_SIGNAL_LEAVE_OK;
  1476. infostream << "Peer " << peer_id << " left channel " << channel_name <<
  1477. std::endl;
  1478. } else {
  1479. resp_pkt << (u8) MODCHANNEL_SIGNAL_LEAVE_FAILURE;
  1480. infostream << "Peer " << peer_id << " left channel " << channel_name <<
  1481. ", but was not registered." << std::endl;
  1482. }
  1483. resp_pkt << channel_name;
  1484. Send(&resp_pkt);
  1485. }
  1486. void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
  1487. {
  1488. std::string channel_name, channel_msg;
  1489. *pkt >> channel_name >> channel_msg;
  1490. session_t peer_id = pkt->getPeerId();
  1491. verbosestream << "Mod channel message received from peer " << peer_id <<
  1492. " on channel " << channel_name << " message: " << channel_msg <<
  1493. std::endl;
  1494. // If mod channels are not enabled, discard message
  1495. if (!g_settings->getBool("enable_mod_channels")) {
  1496. return;
  1497. }
  1498. // If channel not registered, signal it and ignore message
  1499. if (!m_modchannel_mgr->channelRegistered(channel_name)) {
  1500. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
  1501. 1 + 2 + channel_name.size(), peer_id);
  1502. resp_pkt << (u8)MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED << channel_name;
  1503. Send(&resp_pkt);
  1504. return;
  1505. }
  1506. // @TODO: filter, rate limit
  1507. broadcastModChannelMessage(channel_name, channel_msg, peer_id);
  1508. }
  1509. void Server::handleCommand_HaveMedia(NetworkPacket *pkt)
  1510. {
  1511. std::vector<u32> tokens;
  1512. u8 numtokens;
  1513. *pkt >> numtokens;
  1514. for (u16 i = 0; i < numtokens; i++) {
  1515. u32 n;
  1516. *pkt >> n;
  1517. tokens.emplace_back(n);
  1518. }
  1519. const session_t peer_id = pkt->getPeerId();
  1520. auto player = m_env->getPlayer(peer_id);
  1521. for (const u32 token : tokens) {
  1522. auto it = m_pending_dyn_media.find(token);
  1523. if (it == m_pending_dyn_media.end())
  1524. continue;
  1525. if (it->second.waiting_players.count(peer_id)) {
  1526. it->second.waiting_players.erase(peer_id);
  1527. if (player)
  1528. getScriptIface()->on_dynamic_media_added(token, player->getName());
  1529. }
  1530. }
  1531. }