summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
Diffstat (limited to 'src/network')
-rw-r--r--src/network/clientopcodes.cpp20
-rw-r--r--src/network/clientpackethandler.cpp238
-rw-r--r--src/network/connection.cpp39
-rw-r--r--src/network/connection.h22
-rw-r--r--src/network/connectionthreads.cpp302
-rw-r--r--src/network/connectionthreads.h2
-rw-r--r--src/network/networkprotocol.h33
-rw-r--r--src/network/serveropcodes.cpp22
-rw-r--r--src/network/serverpackethandler.cpp640
9 files changed, 654 insertions, 664 deletions
diff --git a/src/network/clientopcodes.cpp b/src/network/clientopcodes.cpp
index 431455b76..f812a08a1 100644
--- a/src/network/clientopcodes.cpp
+++ b/src/network/clientopcodes.cpp
@@ -68,7 +68,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_TIME_OF_DAY", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_TimeOfDay }, // 0x29
{ "TOCLIENT_CSM_RESTRICTION_FLAGS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CSMRestrictionFlags }, // 0x2A
{ "TOCLIENT_PLAYER_SPEED", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_PlayerSpeed }, // 0x2B
- null_command_handler,
+ { "TOCLIENT_MEDIA_PUSH", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_MediaPush }, // 0x2C
null_command_handler,
null_command_handler,
{ "TOCLIENT_CHAT_MESSAGE", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_ChatMessage }, // 0x2F
@@ -126,6 +126,16 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
const static ServerCommandFactory null_command_factory = { "TOSERVER_NULL", 0, false };
+/*
+ Channels used for Client -> Server communication
+ 2: Notifications back to the server (e.g. GOTBLOCKS)
+ 1: Init and Authentication
+ 0: everything else
+
+ Packet order is only guaranteed inside a channel, so packets that operate on
+ the same objects are *required* to be in the same channel.
+*/
+
const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
{
null_command_factory, // 0x00
@@ -143,7 +153,7 @@ const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
null_command_factory, // 0x0c
null_command_factory, // 0x0d
null_command_factory, // 0x0e
- null_command_factory, // 0x0F
+ null_command_factory, // 0x0f
null_command_factory, // 0x10
{ "TOSERVER_INIT2", 1, true }, // 0x11
null_command_factory, // 0x12
@@ -186,7 +196,7 @@ const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
{ "TOSERVER_PLAYERITEM", 0, true }, // 0x37
{ "TOSERVER_RESPAWN", 0, true }, // 0x38
{ "TOSERVER_INTERACT", 0, true }, // 0x39
- { "TOSERVER_REMOVED_SOUNDS", 1, true }, // 0x3a
+ { "TOSERVER_REMOVED_SOUNDS", 2, true }, // 0x3a
{ "TOSERVER_NODEMETA_FIELDS", 0, true }, // 0x3b
{ "TOSERVER_INVENTORY_FIELDS", 0, true }, // 0x3c
null_command_factory, // 0x3d
@@ -194,8 +204,8 @@ const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
null_command_factory, // 0x3f
{ "TOSERVER_REQUEST_MEDIA", 1, true }, // 0x40
null_command_factory, // 0x41
- { "TOSERVER_BREATH", 0, true }, // 0x42 old TOSERVER_BREATH. Ignored by servers
- { "TOSERVER_CLIENT_READY", 0, true }, // 0x43
+ null_command_factory, // 0x42
+ { "TOSERVER_CLIENT_READY", 1, true }, // 0x43
null_command_factory, // 0x44
null_command_factory, // 0x45
null_command_factory, // 0x46
diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp
index 432fb415e..5934eaf8c 100644
--- a/src/network/clientpackethandler.cpp
+++ b/src/network/clientpackethandler.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/client.h"
#include "util/base64.h"
+#include "client/camera.h"
#include "chatmessage.h"
#include "client/clientmedia.h"
#include "log.h"
@@ -38,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "script/scripting_client.h"
#include "util/serialize.h"
#include "util/srp.h"
+#include "util/sha1.h"
#include "tileanimation.h"
#include "gettext.h"
#include "skyparams.h"
@@ -386,10 +388,10 @@ void Client::handleCommand_TimeOfDay(NetworkPacket* pkt)
m_env.setTimeOfDaySpeed(time_speed);
m_time_of_day_set = true;
- u32 dr = m_env.getDayNightRatio();
- infostream << "Client: time_of_day=" << time_of_day
- << " time_speed=" << time_speed
- << " dr=" << dr << std::endl;
+ //u32 dr = m_env.getDayNightRatio();
+ //infostream << "Client: time_of_day=" << time_of_day
+ // << " time_speed=" << time_speed
+ // << " dr=" << dr << std::endl;
}
void Client::handleCommand_ChatMessage(NetworkPacket *pkt)
@@ -530,11 +532,21 @@ void Client::handleCommand_Movement(NetworkPacket* pkt)
void Client::handleCommand_Fov(NetworkPacket *pkt)
{
f32 fov;
- bool is_multiplier;
+ bool is_multiplier = false;
+ f32 transition_time = 0.0f;
+
*pkt >> fov >> is_multiplier;
+ // Wrap transition_time extraction within a
+ // try-catch to preserve backwards compat
+ try {
+ *pkt >> transition_time;
+ } catch (PacketError &e) {};
+
LocalPlayer *player = m_env.getLocalPlayer();
- player->setFov({ fov, is_multiplier });
+ assert(player);
+ player->setFov({ fov, is_multiplier, transition_time });
+ m_camera->notifyFovChange();
}
void Client::handleCommand_HP(NetworkPacket *pkt)
@@ -947,114 +959,66 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt)
std::string datastring(pkt->getString(0), pkt->getSize());
std::istringstream is(datastring, std::ios_base::binary);
- v3f pos = readV3F32(is);
- v3f vel = readV3F32(is);
- v3f acc = readV3F32(is);
- float expirationtime = readF32(is);
- float size = readF32(is);
- bool collisiondetection = readU8(is);
- std::string texture = deSerializeLongString(is);
-
- bool vertical = false;
- bool collision_removal = false;
- TileAnimationParams animation;
- animation.type = TAT_NONE;
- u8 glow = 0;
- bool object_collision = false;
- try {
- vertical = readU8(is);
- collision_removal = readU8(is);
- animation.deSerialize(is, m_proto_ver);
- glow = readU8(is);
- object_collision = readU8(is);
- } catch (...) {}
+ ParticleParameters p;
+ p.deSerialize(is, m_proto_ver);
ClientEvent *event = new ClientEvent();
- event->type = CE_SPAWN_PARTICLE;
- event->spawn_particle.pos = new v3f (pos);
- event->spawn_particle.vel = new v3f (vel);
- event->spawn_particle.acc = new v3f (acc);
- event->spawn_particle.expirationtime = expirationtime;
- event->spawn_particle.size = size;
- event->spawn_particle.collisiondetection = collisiondetection;
- event->spawn_particle.collision_removal = collision_removal;
- event->spawn_particle.object_collision = object_collision;
- event->spawn_particle.vertical = vertical;
- event->spawn_particle.texture = new std::string(texture);
- event->spawn_particle.animation = animation;
- event->spawn_particle.glow = glow;
+ event->type = CE_SPAWN_PARTICLE;
+ event->spawn_particle = new ParticleParameters(p);
m_client_event_queue.push(event);
}
void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
{
- u16 amount;
- float spawntime;
- v3f minpos;
- v3f maxpos;
- v3f minvel;
- v3f maxvel;
- v3f minacc;
- v3f maxacc;
- float minexptime;
- float maxexptime;
- float minsize;
- float maxsize;
- bool collisiondetection;
- u32 server_id;
-
- *pkt >> amount >> spawntime >> minpos >> maxpos >> minvel >> maxvel
- >> minacc >> maxacc >> minexptime >> maxexptime >> minsize
- >> maxsize >> collisiondetection;
-
- std::string texture = pkt->readLongString();
-
- *pkt >> server_id;
-
- bool vertical = false;
- bool collision_removal = false;
- u16 attached_id = 0;
- TileAnimationParams animation;
- animation.type = TAT_NONE;
- u8 glow = 0;
- bool object_collision = false;
- try {
- *pkt >> vertical;
- *pkt >> collision_removal;
- *pkt >> attached_id;
+ std::string datastring(pkt->getString(0), pkt->getSize());
+ std::istringstream is(datastring, std::ios_base::binary);
- // This is horrible but required (why are there two ways to deserialize pkts?)
- std::string datastring(pkt->getRemainingString(), pkt->getRemainingBytes());
- std::istringstream is(datastring, std::ios_base::binary);
- animation.deSerialize(is, m_proto_ver);
- glow = readU8(is);
- object_collision = readU8(is);
- } catch (...) {}
+ ParticleSpawnerParameters p;
+ u32 server_id;
+ u16 attached_id = 0;
+
+ p.amount = readU16(is);
+ p.time = readF32(is);
+ p.minpos = readV3F32(is);
+ p.maxpos = readV3F32(is);
+ p.minvel = readV3F32(is);
+ p.maxvel = readV3F32(is);
+ p.minacc = readV3F32(is);
+ p.maxacc = readV3F32(is);
+ p.minexptime = readF32(is);
+ p.maxexptime = readF32(is);
+ p.minsize = readF32(is);
+ p.maxsize = readF32(is);
+ p.collisiondetection = readU8(is);
+ p.texture = deSerializeLongString(is);
+
+ server_id = readU32(is);
+
+ p.vertical = readU8(is);
+ p.collision_removal = readU8(is);
+
+ attached_id = readU16(is);
+
+ p.animation.deSerialize(is, m_proto_ver);
+ p.glow = readU8(is);
+ p.object_collision = readU8(is);
+
+ // This is kinda awful
+ do {
+ u16 tmp_param0 = readU16(is);
+ if (is.eof())
+ break;
+ p.node.param0 = tmp_param0;
+ p.node.param2 = readU8(is);
+ p.node_tile = readU8(is);
+ } while (0);
auto event = new ClientEvent();
- event->type = CE_ADD_PARTICLESPAWNER;
- event->add_particlespawner.amount = amount;
- event->add_particlespawner.spawntime = spawntime;
- event->add_particlespawner.minpos = new v3f (minpos);
- event->add_particlespawner.maxpos = new v3f (maxpos);
- event->add_particlespawner.minvel = new v3f (minvel);
- event->add_particlespawner.maxvel = new v3f (maxvel);
- event->add_particlespawner.minacc = new v3f (minacc);
- event->add_particlespawner.maxacc = new v3f (maxacc);
- event->add_particlespawner.minexptime = minexptime;
- event->add_particlespawner.maxexptime = maxexptime;
- event->add_particlespawner.minsize = minsize;
- event->add_particlespawner.maxsize = maxsize;
- event->add_particlespawner.collisiondetection = collisiondetection;
- event->add_particlespawner.collision_removal = collision_removal;
- event->add_particlespawner.object_collision = object_collision;
- event->add_particlespawner.attached_id = attached_id;
- event->add_particlespawner.vertical = vertical;
- event->add_particlespawner.texture = new std::string(texture);
- event->add_particlespawner.id = server_id;
- event->add_particlespawner.animation = animation;
- event->add_particlespawner.glow = glow;
+ event->type = CE_ADD_PARTICLESPAWNER;
+ event->add_particlespawner.p = new ParticleSpawnerParameters(p);
+ event->add_particlespawner.attached_id = attached_id;
+ event->add_particlespawner.id = server_id;
m_client_event_queue.push(event);
}
@@ -1091,22 +1055,16 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt)
v3f world_pos;
v2s32 size;
s16 z_index = 0;
+ std::string text2;
*pkt >> server_id >> type >> pos >> name >> scale >> text >> number >> item
>> dir >> align >> offset;
try {
*pkt >> world_pos;
- }
- catch(SerializationError &e) {};
-
- try {
*pkt >> size;
- } catch(SerializationError &e) {};
-
- try {
*pkt >> z_index;
- }
- catch(PacketError &e) {}
+ *pkt >> text2;
+ } catch(PacketError &e) {};
ClientEvent *event = new ClientEvent();
event->type = CE_HUDADD;
@@ -1124,6 +1082,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt)
event->hudadd.world_pos = new v3f(world_pos);
event->hudadd.size = new v2s32(size);
event->hudadd.z_index = z_index;
+ event->hudadd.text2 = new std::string(text2);
m_client_event_queue.push(event);
}
@@ -1160,7 +1119,7 @@ void Client::handleCommand_HudChange(NetworkPacket* pkt)
if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
*pkt >> v2fdata;
- else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT)
+ else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT || stat == HUD_STAT_TEXT2)
*pkt >> sdata;
else if (stat == HUD_STAT_WORLD_POS)
*pkt >> v3fdata;
@@ -1265,9 +1224,9 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
// Fix for "regular" skies, as color isn't kept:
if (skybox.type == "regular") {
skybox.sky_color = sky_defaults.getSkyColorDefaults();
- skybox.tint_type = "default";
- skybox.moon_tint = video::SColor(255, 255, 255, 255);
- skybox.sun_tint = video::SColor(255, 255, 255, 255);
+ skybox.fog_tint_type = "default";
+ skybox.fog_moon_tint = video::SColor(255, 255, 255, 255);
+ skybox.fog_sun_tint = video::SColor(255, 255, 255, 255);
}
else {
sun.visible = false;
@@ -1302,7 +1261,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
std::string texture;
*pkt >> skybox.bgcolor >> skybox.type >> skybox.clouds >>
- skybox.sun_tint >> skybox.moon_tint >> skybox.tint_type;
+ skybox.fog_sun_tint >> skybox.fog_moon_tint >> skybox.fog_tint_type;
if (skybox.type == "skybox") {
*pkt >> texture_count;
@@ -1513,6 +1472,51 @@ void Client::handleCommand_PlayerSpeed(NetworkPacket *pkt)
player->addVelocity(added_vel);
}
+void Client::handleCommand_MediaPush(NetworkPacket *pkt)
+{
+ std::string raw_hash, filename, filedata;
+ bool cached;
+
+ *pkt >> raw_hash >> filename >> cached;
+ filedata = pkt->readLongString();
+
+ if (raw_hash.size() != 20 || filedata.empty() || filename.empty() ||
+ !string_allowed(filename, TEXTURENAME_ALLOWED_CHARS)) {
+ throw PacketError("Illegal filename, data or hash");
+ }
+
+ verbosestream << "Server pushes media file \"" << filename << "\" with "
+ << filedata.size() << " bytes of data (cached=" << cached
+ << ")" << std::endl;
+
+ if (m_media_pushed_files.count(filename) != 0) {
+ // Silently ignore for synchronization purposes
+ return;
+ }
+
+ // Compute and check checksum of data
+ std::string computed_hash;
+ {
+ SHA1 ctx;
+ ctx.addBytes(filedata.c_str(), filedata.size());
+ unsigned char *buf = ctx.getDigest();
+ computed_hash.assign((char*) buf, 20);
+ free(buf);
+ }
+ if (raw_hash != computed_hash) {
+ verbosestream << "Hash of file data mismatches, ignoring." << std::endl;
+ return;
+ }
+
+ // Actually load media
+ loadMedia(filedata, filename, true);
+ m_media_pushed_files.insert(filename);
+
+ // Cache file for the next time when this client joins the same server
+ if (cached)
+ clientMediaUpdateCache(raw_hash, filedata);
+}
+
/*
* Mod channels
*/
diff --git a/src/network/connection.cpp b/src/network/connection.cpp
index 36124ce3c..3692e45a9 100644
--- a/src/network/connection.cpp
+++ b/src/network/connection.cpp
@@ -924,7 +924,7 @@ UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) :
Peer(a_address,a_id,connection)
{
for (Channel &channel : channels)
- channel.setWindowSize(g_settings->getU16("max_packets_per_iteration"));
+ channel.setWindowSize(START_RELIABLE_WINDOW_SIZE);
}
bool UDPPeer::getAddress(MTProtocols type,Address& toset)
@@ -975,22 +975,29 @@ void UDPPeer::PutReliableSendCommand(ConnectionCommand &c,
if (m_pending_disconnect)
return;
- if ( channels[c.channelnum].queued_commands.empty() &&
+ Channel &chan = channels[c.channelnum];
+
+ if (chan.queued_commands.empty() &&
/* don't queue more packets then window size */
- (channels[c.channelnum].queued_reliables.size()
- < (channels[c.channelnum].getWindowSize()/2))) {
+ (chan.queued_reliables.size() < chan.getWindowSize() / 2)) {
LOG(dout_con<<m_connection->getDesc()
<<" processing reliable command for peer id: " << c.peer_id
<<" data size: " << c.data.getSize() << std::endl);
if (!processReliableSendCommand(c,max_packet_size)) {
- channels[c.channelnum].queued_commands.push_back(c);
+ chan.queued_commands.push_back(c);
}
}
else {
LOG(dout_con<<m_connection->getDesc()
<<" Queueing reliable command for peer id: " << c.peer_id
<<" data size: " << c.data.getSize() <<std::endl);
- channels[c.channelnum].queued_commands.push_back(c);
+ chan.queued_commands.push_back(c);
+ if (chan.queued_commands.size() >= chan.getWindowSize() / 2) {
+ LOG(derr_con << m_connection->getDesc()
+ << "Possible packet stall to peer id: " << c.peer_id
+ << " queued_commands=" << chan.queued_commands.size()
+ << std::endl);
+ }
}
}
@@ -1001,6 +1008,8 @@ bool UDPPeer::processReliableSendCommand(
if (m_pending_disconnect)
return true;
+ Channel &chan = channels[c.channelnum];
+
u32 chunksize_max = max_packet_size
- BASE_HEADER_SIZE
- RELIABLE_HEADER_SIZE;
@@ -1008,13 +1017,13 @@ bool UDPPeer::processReliableSendCommand(
sanity_check(c.data.getSize() < MAX_RELIABLE_WINDOW_SIZE*512);
std::list<SharedBuffer<u8>> originals;
- u16 split_sequence_number = channels[c.channelnum].readNextSplitSeqNum();
+ u16 split_sequence_number = chan.readNextSplitSeqNum();
if (c.raw) {
originals.emplace_back(c.data);
} else {
makeAutoSplitPacket(c.data, chunksize_max,split_sequence_number, &originals);
- channels[c.channelnum].setNextSplitSeqNum(split_sequence_number);
+ chan.setNextSplitSeqNum(split_sequence_number);
}
bool have_sequence_number = true;
@@ -1023,7 +1032,7 @@ bool UDPPeer::processReliableSendCommand(
volatile u16 initial_sequence_number = 0;
for (SharedBuffer<u8> &original : originals) {
- u16 seqnum = channels[c.channelnum].getOutgoingSequenceNumber(have_sequence_number);
+ u16 seqnum = chan.getOutgoingSequenceNumber(have_sequence_number);
/* oops, we don't have enough sequence numbers to send this packet */
if (!have_sequence_number)
@@ -1055,10 +1064,10 @@ bool UDPPeer::processReliableSendCommand(
// << " channel: " << (c.channelnum&0xFF)
// << " seqnum: " << readU16(&p.data[BASE_HEADER_SIZE+1])
// << std::endl)
- channels[c.channelnum].queued_reliables.push(p);
+ chan.queued_reliables.push(p);
pcount++;
}
- sanity_check(channels[c.channelnum].queued_reliables.size() < 0xFFFF);
+ sanity_check(chan.queued_reliables.size() < 0xFFFF);
return true;
}
@@ -1073,7 +1082,7 @@ bool UDPPeer::processReliableSendCommand(
toadd.pop();
bool successfully_put_back_sequence_number
- = channels[c.channelnum].putBackSequenceNumber(
+ = chan.putBackSequenceNumber(
(initial_sequence_number+toadd.size() % (SEQNUM_MAX+1)));
FATAL_ERROR_IF(!successfully_put_back_sequence_number, "error");
@@ -1081,7 +1090,7 @@ bool UDPPeer::processReliableSendCommand(
// DO NOT REMOVE n_queued! It avoids a deadlock of async locked
// 'log_message_mutex' and 'm_list_mutex'.
- u32 n_queued = channels[c.channelnum].outgoing_reliables_sent.size();
+ u32 n_queued = chan.outgoing_reliables_sent.size();
LOG(dout_con<<m_connection->getDesc()
<< " Windowsize exceeded on reliable sending "
@@ -1164,7 +1173,9 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
m_bc_peerhandler(peerhandler)
{
- m_udpSocket.setTimeoutMs(5);
+ /* Amount of time Receive() will wait for data, this is entirely different
+ * from the connection timeout */
+ m_udpSocket.setTimeoutMs(500);
m_sendThread->setParent(this);
m_receiveThread->setParent(this);
diff --git a/src/network/connection.h b/src/network/connection.h
index 0b12bf701..47b0805ce 100644
--- a/src/network/connection.h
+++ b/src/network/connection.h
@@ -141,7 +141,6 @@ private:
=== NOTES ===
A packet is sent through a channel to a peer with a basic header:
-TODO: Should we have a receiver_peer_id also?
Header (7 bytes):
[0] u32 protocol_id
[4] session_t sender_peer_id
@@ -152,8 +151,7 @@ sender_peer_id:
value 1 (PEER_ID_SERVER) is reserved for server
these constants are defined in constants.h
channel:
- The lower the number, the higher the priority is.
- Only channels 0, 1 and 2 exist.
+ Channel numbers have no intrinsic meaning. Currently only 0, 1, 2 exist.
*/
#define BASE_HEADER_SIZE 7
#define CHANNEL_COUNT 3
@@ -386,12 +384,14 @@ struct ConnectionCommand
}
};
-/* maximum window size to use, 0xFFFF is theoretical maximum don't think about
+/* maximum window size to use, 0xFFFF is theoretical maximum. don't think about
* touching it, the less you're away from it the more likely data corruption
* will occur
*/
#define MAX_RELIABLE_WINDOW_SIZE 0x8000
- /* starting value for window size */
+/* starting value for window size */
+#define START_RELIABLE_WINDOW_SIZE 0x400
+/* minimum value for window size */
#define MIN_RELIABLE_WINDOW_SIZE 0x40
class Channel
@@ -555,15 +555,15 @@ class Peer {
bool isTimedOut(float timeout);
- unsigned int m_increment_packets_remaining = 9;
- unsigned int m_increment_bytes_remaining = 0;
+ unsigned int m_increment_packets_remaining = 0;
virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; };
virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {};
virtual SharedBuffer<u8> addSplitPacket(u8 channel, const BufferedPacket &toadd,
bool reliable)
{
- fprintf(stderr,"Peer: addSplitPacket called, this is supposed to be never called!\n");
+ errorstream << "Peer::addSplitPacket called,"
+ << " this is supposed to be never called!" << std::endl;
return SharedBuffer<u8>(0);
};
@@ -612,16 +612,16 @@ class Peer {
struct rttstats {
float jitter_min = FLT_MAX;
float jitter_max = 0.0f;
- float jitter_avg = -2.0f;
+ float jitter_avg = -1.0f;
float min_rtt = FLT_MAX;
float max_rtt = 0.0f;
- float avg_rtt = -2.0f;
+ float avg_rtt = -1.0f;
rttstats() = default;
};
rttstats m_rtt;
- float m_last_rtt = -2.0f;
+ float m_last_rtt = -1.0f;
// current usage count
unsigned int m_usage = 0;
diff --git a/src/network/connectionthreads.cpp b/src/network/connectionthreads.cpp
index f8b58c025..9a6617a1c 100644
--- a/src/network/connectionthreads.cpp
+++ b/src/network/connectionthreads.cpp
@@ -73,6 +73,7 @@ ConnectionSendThread::ConnectionSendThread(unsigned int max_packet_size,
m_timeout(timeout),
m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration"))
{
+ SANITY_CHECK(m_max_data_packets_per_iteration > 1);
}
void *ConnectionSendThread::run()
@@ -107,8 +108,13 @@ void *ConnectionSendThread::run()
curtime = porting::getTimeMs();
float dtime = CALC_DTIME(lasttime, curtime);
- /* first do all the reliable stuff */
+ /* first resend timed-out packets */
runTimeouts(dtime);
+ if (m_iteration_packets_avaialble == 0) {
+ LOG(warningstream << m_connection->getDesc()
+ << " Packet quota used up after re-sending packets, "
+ << "max=" << m_max_data_packets_per_iteration << std::endl);
+ }
/* translate commands to packets */
ConnectionCommand c = m_connection->m_command_queue.pop_frontNoEx(0);
@@ -121,7 +127,7 @@ void *ConnectionSendThread::run()
c = m_connection->m_command_queue.pop_frontNoEx(0);
}
- /* send non reliable packets */
+ /* send queued packets */
sendPackets(dtime);
END_DEBUG_EXCEPTION_HANDLER
@@ -193,7 +199,6 @@ void ConnectionSendThread::runTimeouts(float dtime)
infostream << m_connection->getDesc()
<< "RunTimeouts(): Peer " << peer->id
<< " has timed out."
- << " (source=peer->timeout_counter)"
<< std::endl;
// Add peer to the list
timeouted_peers.push_back(peer->id);
@@ -286,7 +291,7 @@ void ConnectionSendThread::runTimeouts(float dtime)
// Remove timed out peers
for (u16 timeouted_peer : timeouted_peers) {
- LOG(derr_con << m_connection->getDesc()
+ LOG(dout_con << m_connection->getDesc()
<< "RunTimeouts(): Removing peer " << timeouted_peer << std::endl);
m_connection->deletePeer(timeouted_peer, true);
}
@@ -331,11 +336,9 @@ bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
{
PeerHelper peer = m_connection->getPeerNoEx(peer_id);
if (!peer) {
- LOG(dout_con << m_connection->getDesc()
- << " INFO: dropped packet for non existent peer_id: "
- << peer_id << std::endl);
- FATAL_ERROR_IF(!reliable,
- "Trying to send raw packet reliable but no peer found!");
+ LOG(errorstream << m_connection->getDesc()
+ << " dropped " << (reliable ? "reliable " : "")
+ << "packet for non existent peer_id: " << peer_id << std::endl);
return false;
}
Channel *channel = &(dynamic_cast<UDPPeer *>(&peer)->channels[channelnum]);
@@ -644,6 +647,9 @@ void ConnectionSendThread::sendPackets(float dtime)
std::list<session_t> pendingDisconnect;
std::map<session_t, bool> pending_unreliable;
+ const unsigned int peer_packet_quota = m_iteration_packets_avaialble
+ / MYMAX(peerIds.size(), 1);
+
for (session_t peerId : peerIds) {
PeerHelper peer = m_connection->getPeerNoEx(peerId);
//peer may have been removed
@@ -653,8 +659,7 @@ void ConnectionSendThread::sendPackets(float dtime)
<< std::endl);
continue;
}
- peer->m_increment_packets_remaining =
- m_iteration_packets_avaialble / m_connection->m_peers.size();
+ peer->m_increment_packets_remaining = peer_packet_quota;
UDPPeer *udpPeer = dynamic_cast<UDPPeer *>(&peer);
@@ -751,23 +756,30 @@ void ConnectionSendThread::sendPackets(float dtime)
}
/* send acks immediately */
- if (packet.ack) {
+ if (packet.ack || peer->m_increment_packets_remaining > 0 || stopRequested()) {
rawSendAsPacket(packet.peer_id, packet.channelnum,
packet.data, packet.reliable);
- peer->m_increment_packets_remaining =
- MYMIN(0, peer->m_increment_packets_remaining--);
- } else if (
- (peer->m_increment_packets_remaining > 0) ||
- (stopRequested())) {
- rawSendAsPacket(packet.peer_id, packet.channelnum,
- packet.data, packet.reliable);
- peer->m_increment_packets_remaining--;
+ if (peer->m_increment_packets_remaining > 0)
+ peer->m_increment_packets_remaining--;
} else {
m_outgoing_queue.push(packet);
pending_unreliable[packet.peer_id] = true;
}
}
+ if (peer_packet_quota > 0) {
+ for (session_t peerId : peerIds) {
+ PeerHelper peer = m_connection->getPeerNoEx(peerId);
+ if (!peer)
+ continue;
+ if (peer->m_increment_packets_remaining == 0) {
+ LOG(warningstream << m_connection->getDesc()
+ << " Packet quota used up for peer_id=" << peerId
+ << ", was " << peer_packet_quota << " pkts" << std::endl);
+ }
+ }
+ }
+
for (session_t peerId : pendingDisconnect) {
if (!pending_unreliable[peerId]) {
m_connection->deletePeer(peerId, false);
@@ -798,6 +810,14 @@ void *ConnectionReceiveThread::run()
ThreadIdentifier);
PROFILE(ThreadIdentifier << "ConnectionReceive: [" << m_connection->getDesc() << "]");
+ // use IPv6 minimum allowed MTU as receive buffer size as this is
+ // theoretical reliable upper boundary of a udp packet for all IPv6 enabled
+ // infrastructure
+ const unsigned int packet_maxsize = 1500;
+ SharedBuffer<u8> packetdata(packet_maxsize);
+
+ bool packet_queued = true;
+
#ifdef DEBUG_CONNECTION_KBPS
u64 curtime = porting::getTimeMs();
u64 lasttime = curtime;
@@ -816,7 +836,7 @@ void *ConnectionReceiveThread::run()
#endif
/* receive packets */
- receive();
+ receive(packetdata, packet_queued);
#ifdef DEBUG_CONNECTION_KBPS
debug_print_timer += dtime;
@@ -878,157 +898,142 @@ void *ConnectionReceiveThread::run()
}
// Receive packets from the network and buffers and create ConnectionEvents
-void ConnectionReceiveThread::receive()
+void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,
+ bool &packet_queued)
{
- // use IPv6 minimum allowed MTU as receive buffer size as this is
- // theoretical reliable upper boundary of a udp packet for all IPv6 enabled
- // infrastructure
- unsigned int packet_maxsize = 1500;
- SharedBuffer<u8> packetdata(packet_maxsize);
-
- bool packet_queued = true;
-
- unsigned int loop_count = 0;
-
- /* first of all read packets from socket */
- /* check for incoming data available */
- while ((loop_count < 10) &&
- (m_connection->m_udpSocket.WaitData(50))) {
- loop_count++;
- try {
- if (packet_queued) {
- bool data_left = true;
- session_t peer_id;
- SharedBuffer<u8> resultdata;
- while (data_left) {
- try {
- data_left = getFromBuffers(peer_id, resultdata);
- if (data_left) {
- ConnectionEvent e;
- e.dataReceived(peer_id, resultdata);
- m_connection->putEvent(e);
- }
- }
- catch (ProcessedSilentlyException &e) {
- /* try reading again */
+ try {
+ // First, see if there any buffered packets we can process now
+ if (packet_queued) {
+ bool data_left = true;
+ session_t peer_id;
+ SharedBuffer<u8> resultdata;
+ while (data_left) {
+ try {
+ data_left = getFromBuffers(peer_id, resultdata);
+ if (data_left) {
+ ConnectionEvent e;
+ e.dataReceived(peer_id, resultdata);
+ m_connection->putEvent(e);
}
}
- packet_queued = false;
- }
-
- Address sender;
- s32 received_size = m_connection->m_udpSocket.Receive(sender, *packetdata,
- packet_maxsize);
-
- if ((received_size < BASE_HEADER_SIZE) ||
- (readU32(&packetdata[0]) != m_connection->GetProtocolID())) {
- LOG(derr_con << m_connection->getDesc()
- << "Receive(): Invalid incoming packet, "
- << "size: " << received_size
- << ", protocol: "
- << ((received_size >= 4) ? readU32(&packetdata[0]) : -1)
- << std::endl);
- continue;
+ catch (ProcessedSilentlyException &e) {
+ /* try reading again */
+ }
}
+ packet_queued = false;
+ }
- session_t peer_id = readPeerId(*packetdata);
- u8 channelnum = readChannel(*packetdata);
+ // Call Receive() to wait for incoming data
+ Address sender;
+ s32 received_size = m_connection->m_udpSocket.Receive(sender,
+ *packetdata, packetdata.getSize());
+ if (received_size < 0)
+ return;
- if (channelnum > CHANNEL_COUNT - 1) {
- LOG(derr_con << m_connection->getDesc()
- << "Receive(): Invalid channel " << (u32)channelnum << std::endl);
- throw InvalidIncomingDataException("Channel doesn't exist");
- }
+ if ((received_size < BASE_HEADER_SIZE) ||
+ (readU32(&packetdata[0]) != m_connection->GetProtocolID())) {
+ LOG(derr_con << m_connection->getDesc()
+ << "Receive(): Invalid incoming packet, "
+ << "size: " << received_size
+ << ", protocol: "
+ << ((received_size >= 4) ? readU32(&packetdata[0]) : -1)
+ << std::endl);
+ return;
+ }
- /* Try to identify peer by sender address (may happen on join) */
- if (peer_id == PEER_ID_INEXISTENT) {
- peer_id = m_connection->lookupPeer(sender);
- // We do not have to remind the peer of its
- // peer id as the CONTROLTYPE_SET_PEER_ID
- // command was sent reliably.
- }
+ session_t peer_id = readPeerId(*packetdata);
+ u8 channelnum = readChannel(*packetdata);
- /* The peer was not found in our lists. Add it. */
- if (peer_id == PEER_ID_INEXISTENT) {
- peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
- }
+ if (channelnum > CHANNEL_COUNT - 1) {
+ LOG(derr_con << m_connection->getDesc()
+ << "Receive(): Invalid channel " << (u32)channelnum << std::endl);
+ return;
+ }
- PeerHelper peer = m_connection->getPeerNoEx(peer_id);
+ /* Try to identify peer by sender address (may happen on join) */
+ if (peer_id == PEER_ID_INEXISTENT) {
+ peer_id = m_connection->lookupPeer(sender);
+ // We do not have to remind the peer of its
+ // peer id as the CONTROLTYPE_SET_PEER_ID
+ // command was sent reliably.
+ }
- if (!peer) {
- LOG(dout_con << m_connection->getDesc()
- << " got packet from unknown peer_id: "
- << peer_id << " Ignoring." << std::endl);
- continue;
- }
+ /* The peer was not found in our lists. Add it. */
+ if (peer_id == PEER_ID_INEXISTENT) {
+ peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
+ }
- // Validate peer address
+ PeerHelper peer = m_connection->getPeerNoEx(peer_id);
+ if (!peer) {
+ LOG(dout_con << m_connection->getDesc()
+ << " got packet from unknown peer_id: "
+ << peer_id << " Ignoring." << std::endl);
+ return;
+ }
- Address peer_address;
+ // Validate peer address
- if (peer->getAddress(MTP_UDP, peer_address)) {
- if (peer_address != sender) {
- LOG(derr_con << m_connection->getDesc()
- << m_connection->getDesc()
- << " Peer " << peer_id << " sending from different address."
- " Ignoring." << std::endl);
- continue;
- }
- } else {
-
- bool invalid_address = true;
- if (invalid_address) {
- LOG(derr_con << m_connection->getDesc()
- << m_connection->getDesc()
- << " Peer " << peer_id << " unknown."
- " Ignoring." << std::endl);
- continue;
- }
+ Address peer_address;
+ if (peer->getAddress(MTP_UDP, peer_address)) {
+ if (peer_address != sender) {
+ LOG(derr_con << m_connection->getDesc()
+ << " Peer " << peer_id << " sending from different address."
+ " Ignoring." << std::endl);
+ return;
}
+ } else {
+ LOG(derr_con << m_connection->getDesc()
+ << " Peer " << peer_id << " doesn't have an address?!"
+ " Ignoring." << std::endl);
+ return;
+ }
- peer->ResetTimeout();
-
- Channel *channel = 0;
+ peer->ResetTimeout();
- if (dynamic_cast<UDPPeer *>(&peer) != 0) {
- channel = &(dynamic_cast<UDPPeer *>(&peer)->channels[channelnum]);
- }
+ Channel *channel = nullptr;
+ if (dynamic_cast<UDPPeer *>(&peer)) {
+ channel = &dynamic_cast<UDPPeer *>(&peer)->channels[channelnum];
+ } else {
+ LOG(derr_con << m_connection->getDesc()
+ << "Receive(): peer_id=" << peer_id << " isn't an UDPPeer?!"
+ " Ignoring." << std::endl);
+ return;
+ }
- if (channel != 0) {
- channel->UpdateBytesReceived(received_size);
- }
+ channel->UpdateBytesReceived(received_size);
- // Throw the received packet to channel->processPacket()
+ // Throw the received packet to channel->processPacket()
- // Make a new SharedBuffer from the data without the base headers
- SharedBuffer<u8> strippeddata(received_size - BASE_HEADER_SIZE);
- memcpy(*strippeddata, &packetdata[BASE_HEADER_SIZE],
- strippeddata.getSize());
+ // Make a new SharedBuffer from the data without the base headers
+ SharedBuffer<u8> strippeddata(received_size - BASE_HEADER_SIZE);
+ memcpy(*strippeddata, &packetdata[BASE_HEADER_SIZE],
+ strippeddata.getSize());
- try {
- // Process it (the result is some data with no headers made by us)
- SharedBuffer<u8> resultdata = processPacket
- (channel, strippeddata, peer_id, channelnum, false);
+ try {
+ // Process it (the result is some data with no headers made by us)
+ SharedBuffer<u8> resultdata = processPacket
+ (channel, strippeddata, peer_id, channelnum, false);
- LOG(dout_con << m_connection->getDesc()
- << " ProcessPacket from peer_id: " << peer_id
- << ", channel: " << (u32)channelnum << ", returned "
- << resultdata.getSize() << " bytes" << std::endl);
+ LOG(dout_con << m_connection->getDesc()
+ << " ProcessPacket from peer_id: " << peer_id
+ << ", channel: " << (u32)channelnum << ", returned "
+ << resultdata.getSize() << " bytes" << std::endl);
- ConnectionEvent e;
- e.dataReceived(peer_id, resultdata);
- m_connection->putEvent(e);
- }
- catch (ProcessedSilentlyException &e) {
- }
- catch (ProcessedQueued &e) {
- packet_queued = true;
- }
- }
- catch (InvalidIncomingDataException &e) {
+ ConnectionEvent e;
+ e.dataReceived(peer_id, resultdata);
+ m_connection->putEvent(e);
}
catch (ProcessedSilentlyException &e) {
}
+ catch (ProcessedQueued &e) {
+ // we set it to true anyway (see below)
+ }
+
+ /* Every time we receive a packet it can happen that a previously
+ * buffered packet is now ready to process. */
+ packet_queued = true;
+ }
+ catch (InvalidIncomingDataException &e) {
}
}
@@ -1175,7 +1180,8 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
m_connection->TriggerSend();
} catch (NotFoundException &e) {
LOG(derr_con << m_connection->getDesc()
- << "WARNING: ACKed packet not in outgoing queue" << std::endl);
+ << "WARNING: ACKed packet not in outgoing queue"
+ << " seqnum=" << seqnum << std::endl);
channel->UpdatePacketTooLateCounter();
}
diff --git a/src/network/connectionthreads.h b/src/network/connectionthreads.h
index da4ea92f5..612407c3b 100644
--- a/src/network/connectionthreads.h
+++ b/src/network/connectionthreads.h
@@ -101,7 +101,7 @@ public:
}
private:
- void receive();
+ void receive(SharedBuffer<u8> &packetdata, bool &packet_queued);
// Returns next data from a buffer if possible
// If found, returns true; if not, false.
diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h
index d3799868b..fd683eac9 100644
--- a/src/network/networkprotocol.h
+++ b/src/network/networkprotocol.h
@@ -70,8 +70,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
PROTOCOL_VERSION 14:
Added transfer of player pressed keys to the server
Added new messages for mesh and bone animation, as well as attachments
- GENERIC_CMD_SET_ANIMATION
- GENERIC_CMD_SET_BONE_POSITION
+ AO_CMD_SET_ANIMATION
+ AO_CMD_SET_BONE_POSITION
GENERIC_CMD_SET_ATTACHMENT
PROTOCOL_VERSION 15:
Serialization format changes
@@ -87,7 +87,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
damageGroups added to ToolCapabilities
sound_place added to ItemDefinition
PROTOCOL_VERSION 19:
- GENERIC_CMD_SET_PHYSICS_OVERRIDE
+ AO_CMD_SET_PHYSICS_OVERRIDE
PROTOCOL_VERSION 20:
TOCLIENT_HUDADD
TOCLIENT_HUDRM
@@ -131,7 +131,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
Add TOCLIENT_HELLO for presenting server to client after client
presentation
Add TOCLIENT_AUTH_ACCEPT to accept connection from client
- Rename GENERIC_CMD_SET_ATTACHMENT to GENERIC_CMD_ATTACH_TO
+ Rename GENERIC_CMD_SET_ATTACHMENT to AO_CMD_ATTACH_TO
PROTOCOL_VERSION 26:
Add TileDef tileable_horizontal, tileable_vertical flags
PROTOCOL_VERSION 27:
@@ -237,6 +237,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
Formspec elements are drawn in the order of definition
bgcolor[]: use 3 parameters (bgcolor, formspec (now an enum), fbgcolor)
box[] and image[] elements enable clipping by default
+ new element: scroll_container[]
*/
#define FORMSPEC_API_VERSION 3
@@ -322,6 +323,15 @@ enum ToClientCommand
v3f added_vel
*/
+ TOCLIENT_MEDIA_PUSH = 0x2C,
+ /*
+ std::string raw_hash
+ std::string filename
+ bool should_be_cached
+ u32 len
+ char filedata[len]
+ */
+
// (oops, there is some gap here)
TOCLIENT_CHAT_MESSAGE = 0x2F,
@@ -383,8 +393,9 @@ enum ToClientCommand
/*
Sends an FOV override/multiplier to client.
- float fov
+ f32 fov
bool is_multiplier
+ f32 transition_time
*/
TOCLIENT_DEATHSCREEN = 0x37,
@@ -558,10 +569,10 @@ enum ToClientCommand
u32 id
u8 type
v2f1000 pos
- u32 len
+ u16 len
u8[len] name
v2f1000 scale
- u32 len2
+ u16 len2
u8[len2] text
u32 number
u32 item
@@ -571,6 +582,8 @@ enum ToClientCommand
v3f1000 world_pos
v2s32 size
s16 z_index
+ u16 len3
+ u8[len3] text2
*/
TOCLIENT_HUDRM = 0x4a,
@@ -632,9 +645,9 @@ enum ToClientCommand
u8[4] night_sky (ARGB)
u8[4] night_horizon (ARGB)
u8[4] indoors (ARGB)
- u8[4] sun_tint (ARGB)
- u8[4] moon_tint (ARGB)
- std::string tint_type
+ u8[4] fog_sun_tint (ARGB)
+ u8[4] fog_moon_tint (ARGB)
+ std::string fog_tint_type
*/
TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO = 0x50,
diff --git a/src/network/serveropcodes.cpp b/src/network/serveropcodes.cpp
index cca2e56ea..2fc3197c2 100644
--- a/src/network/serveropcodes.cpp
+++ b/src/network/serveropcodes.cpp
@@ -111,6 +111,16 @@ const ToServerCommandHandler toServerCommandTable[TOSERVER_NUM_MSG_TYPES] =
const static ClientCommandFactory null_command_factory = { "TOCLIENT_NULL", 0, false };
+/*
+ Channels used for Server -> Client communication
+ 2: Bulk data (mapblocks, media, ...)
+ 1: HUD packets
+ 0: everything else
+
+ Packet order is only guaranteed inside a channel, so packets that operate on
+ the same objects are *required* to be in the same channel.
+*/
+
const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{
null_command_factory, // 0x00
@@ -157,13 +167,13 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_TIME_OF_DAY", 0, true }, // 0x29
{ "TOCLIENT_CSM_RESTRICTION_FLAGS", 0, true }, // 0x2A
{ "TOCLIENT_PLAYER_SPEED", 0, true }, // 0x2B
- null_command_factory, // 0x2C
+ { "TOCLIENT_MEDIA_PUSH", 0, true }, // 0x2C (sent over channel 1 too)
null_command_factory, // 0x2D
null_command_factory, // 0x2E
{ "TOCLIENT_CHAT_MESSAGE", 0, true }, // 0x2F
null_command_factory, // 0x30
{ "TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD", 0, true }, // 0x31
- { "TOCLIENT_ACTIVE_OBJECT_MESSAGES", 0, true }, // 0x32 Special packet, sent by 0 (rel) and 1 (unrel) channel
+ { "TOCLIENT_ACTIVE_OBJECT_MESSAGES", 0, true }, // 0x32 (may be sent as unrel over channel 1 too)
{ "TOCLIENT_HP", 0, true }, // 0x33
{ "TOCLIENT_MOVE_PLAYER", 0, true }, // 0x34
{ "TOCLIENT_ACCESS_DENIED_LEGACY", 0, true }, // 0x35
@@ -176,7 +186,7 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_ANNOUNCE_MEDIA", 0, true }, // 0x3C
{ "TOCLIENT_ITEMDEF", 0, true }, // 0x3D
null_command_factory, // 0x3E
- { "TOCLIENT_PLAY_SOUND", 0, true }, // 0x3f
+ { "TOCLIENT_PLAY_SOUND", 0, true }, // 0x3f (may be sent as unrel too)
{ "TOCLIENT_STOP_SOUND", 0, true }, // 0x40
{ "TOCLIENT_PRIVILEGES", 0, true }, // 0x41
{ "TOCLIENT_INVENTORY_FORMSPEC", 0, true }, // 0x42
@@ -188,9 +198,9 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
null_command_factory, // 0x48
{ "TOCLIENT_HUDADD", 1, true }, // 0x49
{ "TOCLIENT_HUDRM", 1, true }, // 0x4a
- { "TOCLIENT_HUDCHANGE", 0, true }, // 0x4b
- { "TOCLIENT_HUD_SET_FLAGS", 0, true }, // 0x4c
- { "TOCLIENT_HUD_SET_PARAM", 0, true }, // 0x4d
+ { "TOCLIENT_HUDCHANGE", 1, true }, // 0x4b
+ { "TOCLIENT_HUD_SET_FLAGS", 1, true }, // 0x4c
+ { "TOCLIENT_HUD_SET_PARAM", 1, true }, // 0x4d
{ "TOCLIENT_BREATH", 0, true }, // 0x4e
{ "TOCLIENT_SET_SKY", 0, true }, // 0x4f
{ "TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO", 0, true }, // 0x50
diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp
index 23bcc867f..b3008bb50 100644
--- a/src/network/serverpackethandler.cpp
+++ b/src/network/serverpackethandler.cpp
@@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "chatmessage.h"
#include "server.h"
#include "log.h"
-#include "content_sao.h"
#include "emerge.h"
#include "mapblock.h"
#include "modchannels.h"
@@ -34,6 +33,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "network/connection.h"
#include "network/networkprotocol.h"
#include "network/serveropcodes.h"
+#include "server/player_sao.h"
+#include "server/serverinventorymgr.h"
#include "util/auth.h"
#include "util/base64.h"
#include "util/pointedthing.h"
@@ -52,11 +53,12 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
if(pkt->getSize() < 1)
return;
- RemoteClient* client = getClient(pkt->getPeerId(), CS_Created);
+ session_t peer_id = pkt->getPeerId();
+ RemoteClient *client = getClient(peer_id, CS_Created);
std::string addr_s;
try {
- Address address = getPeerAddress(pkt->getPeerId());
+ Address address = getPeerAddress(peer_id);
addr_s = address.serializeString();
}
catch (con::PeerNotFoundException &e) {
@@ -66,27 +68,27 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
* respond for some time, your server was overloaded or
* things like that.
*/
- infostream << "Server::ProcessData(): Canceling: peer "
- << pkt->getPeerId() << " not found" << std::endl;
+ infostream << "Server::ProcessData(): Canceling: peer " << peer_id <<
+ " not found" << std::endl;
return;
}
// If net_proto_version is set, this client has already been handled
if (client->getState() > CS_Created) {
- verbosestream << "Server: Ignoring multiple TOSERVER_INITs from "
- << addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
+ verbosestream << "Server: Ignoring multiple TOSERVER_INITs from " <<
+ addr_s << " (peer_id=" << peer_id << ")" << std::endl;
return;
}
- verbosestream << "Server: Got TOSERVER_INIT from " << addr_s << " (peer_id="
- << pkt->getPeerId() << ")" << std::endl;
+ verbosestream << "Server: Got TOSERVER_INIT from " << addr_s <<
+ " (peer_id=" << peer_id << ")" << std::endl;
// Do not allow multiple players in simple singleplayer mode.
// This isn't a perfect way to do it, but will suffice for now
if (m_simple_singleplayer_mode && m_clients.getClientIDs().size() > 1) {
- infostream << "Server: Not allowing another client (" << addr_s
- << ") to connect in simple singleplayer mode" << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SINGLEPLAYER);
+ infostream << "Server: Not allowing another client (" << addr_s <<
+ ") to connect in simple singleplayer mode" << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_SINGLEPLAYER);
return;
}
@@ -109,11 +111,9 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
depl_serial_v = SER_FMT_VER_INVALID;
if (depl_serial_v == SER_FMT_VER_INVALID) {
- actionstream << "Server: A mismatched client tried to connect from "
- << addr_s << std::endl;
- infostream<<"Server: Cannot negotiate serialization version with "
- << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_VERSION);
+ actionstream << "Server: A mismatched client tried to connect from " <<
+ addr_s << " ser_fmt_max=" << (int)client_max << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_VERSION);
return;
}
@@ -146,9 +146,9 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
net_proto_version != LATEST_PROTOCOL_VERSION) ||
net_proto_version < SERVER_PROTOCOL_VERSION_MIN ||
net_proto_version > SERVER_PROTOCOL_VERSION_MAX) {
- actionstream << "Server: A mismatched client tried to connect from "
- << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_VERSION);
+ actionstream << "Server: A mismatched client tried to connect from " <<
+ addr_s << " proto_max=" << (int)max_net_proto_version << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_VERSION);
return;
}
@@ -159,56 +159,56 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
size_t pns = playerName.size();
if (pns == 0 || pns > PLAYERNAME_SIZE) {
- actionstream << "Server: Player with "
- << ((pns > PLAYERNAME_SIZE) ? "a too long" : "an empty")
- << " name tried to connect from " << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_NAME);
+ actionstream << "Server: Player with " <<
+ ((pns > PLAYERNAME_SIZE) ? "a too long" : "an empty") <<
+ " name tried to connect from " << addr_s << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_NAME);
return;
}
if (!string_allowed(playerName, PLAYERNAME_ALLOWED_CHARS)) {
- actionstream << "Server: Player with an invalid name "
- << "tried to connect from " << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_CHARS_IN_NAME);
+ actionstream << "Server: Player with an invalid name tried to connect "
+ "from " << addr_s << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_CHARS_IN_NAME);
return;
}
- m_clients.setPlayerName(pkt->getPeerId(), playername);
+ m_clients.setPlayerName(peer_id, playername);
//TODO (later) case insensitivity
std::string legacyPlayerNameCasing = playerName;
if (!isSingleplayer() && strcasecmp(playername, "singleplayer") == 0) {
- actionstream << "Server: Player with the name \"singleplayer\" "
- << "tried to connect from " << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_NAME);
+ actionstream << "Server: Player with the name \"singleplayer\" tried "
+ "to connect from " << addr_s << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_NAME);
return;
}
{
std::string reason;
if (m_script->on_prejoinplayer(playername, addr_s, &reason)) {
- actionstream << "Server: Player with the name \"" << playerName << "\" "
- << "tried to connect from " << addr_s << " "
- << "but it was disallowed for the following reason: "
- << reason << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING, reason);
+ actionstream << "Server: Player with the name \"" << playerName <<
+ "\" tried to connect from " << addr_s <<
+ " but it was disallowed for the following reason: " << reason <<
+ std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING, reason);
return;
}
}
- infostream << "Server: New connection: \"" << playerName << "\" from "
- << addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
+ infostream << "Server: New connection: \"" << playerName << "\" from " <<
+ addr_s << " (peer_id=" << peer_id << ")" << std::endl;
// Enforce user limit.
// Don't enforce for users that have some admin right or mod permits it.
if (m_clients.isUserLimitReached() &&
playername != g_settings->get("name") &&
!m_script->can_bypass_userlimit(playername, addr_s)) {
- actionstream << "Server: " << playername << " tried to join from "
- << addr_s << ", but there" << " are already max_users="
- << g_settings->getU16("max_users") << " players." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_TOO_MANY_USERS);
+ actionstream << "Server: " << playername << " tried to join from " <<
+ addr_s << ", but there are already max_users=" <<
+ g_settings->getU16("max_users") << " players." << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_TOO_MANY_USERS);
return;
}
@@ -228,20 +228,19 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
auth_mechs |= AUTH_MECHANISM_SRP;
client->enc_pwd = encpwd;
} else {
- actionstream << "User " << playername
- << " tried to log in, but password field"
- << " was invalid (unknown mechcode)." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
+ actionstream << "User " << playername << " tried to log in, "
+ "but password field was invalid (unknown mechcode)." <<
+ std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
return;
}
} else if (base64_is_valid(encpwd)) {
auth_mechs |= AUTH_MECHANISM_LEGACY_PASSWORD;
client->enc_pwd = encpwd;
} else {
- actionstream << "User " << playername
- << " tried to log in, but password field"
- << " was invalid (invalid base64)." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
+ actionstream << "User " << playername << " tried to log in, but "
+ "password field was invalid (invalid base64)." << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
return;
}
} else {
@@ -264,8 +263,8 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
verbosestream << "Sending TOCLIENT_HELLO with auth method field: "
<< auth_mechs << std::endl;
- NetworkPacket resp_pkt(TOCLIENT_HELLO, 1 + 4
- + legacyPlayerNameCasing.size(), pkt->getPeerId());
+ NetworkPacket resp_pkt(TOCLIENT_HELLO,
+ 1 + 4 + legacyPlayerNameCasing.size(), peer_id);
u16 depl_compress_mode = NETPROTO_COMPRESSION_NONE;
resp_pkt << depl_serial_v << depl_compress_mode << net_proto_version
@@ -276,16 +275,16 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
client->allowed_auth_mechs = auth_mechs;
client->setDeployedCompressionMode(depl_compress_mode);
- m_clients.event(pkt->getPeerId(), CSE_Hello);
+ m_clients.event(peer_id, CSE_Hello);
}
void Server::handleCommand_Init2(NetworkPacket* pkt)
{
- verbosestream << "Server: Got TOSERVER_INIT2 from "
- << pkt->getPeerId() << std::endl;
+ session_t peer_id = pkt->getPeerId();
+ verbosestream << "Server: Got TOSERVER_INIT2 from " << peer_id << std::endl;
- m_clients.event(pkt->getPeerId(), CSE_GotInit2);
- u16 protocol_version = m_clients.getProtocolVersion(pkt->getPeerId());
+ m_clients.event(peer_id, CSE_GotInit2);
+ u16 protocol_version = m_clients.getProtocolVersion(peer_id);
std::string lang;
if (pkt->getSize() > 0)
@@ -295,45 +294,48 @@ void Server::handleCommand_Init2(NetworkPacket* pkt)
Send some initialization data
*/
- infostream << "Server: Sending content to "
- << getPlayerName(pkt->getPeerId()) << std::endl;
+ infostream << "Server: Sending content to " << getPlayerName(peer_id) <<
+ std::endl;
// Send item definitions
- SendItemDef(pkt->getPeerId(), m_itemdef, protocol_version);
+ SendItemDef(peer_id, m_itemdef, protocol_version);
// Send node definitions
- SendNodeDef(pkt->getPeerId(), m_nodedef, protocol_version);
+ SendNodeDef(peer_id, m_nodedef, protocol_version);
- m_clients.event(pkt->getPeerId(), CSE_SetDefinitionsSent);
+ m_clients.event(peer_id, CSE_SetDefinitionsSent);
// Send media announcement
- sendMediaAnnouncement(pkt->getPeerId(), lang);
+ sendMediaAnnouncement(peer_id, lang);
+
+ RemoteClient *client = getClient(peer_id, CS_InitDone);
- RemoteClient *client = getClient(pkt->getPeerId(), CS_InitDone);
+ // Keep client language for server translations
+ client->setLangCode(lang);
// Send active objects
{
- PlayerSAO *sao = getPlayerSAO(pkt->getPeerId());
+ PlayerSAO *sao = getPlayerSAO(peer_id);
if (client && sao)
SendActiveObjectRemoveAdd(client, sao);
}
// Send detached inventories
- sendDetachedInventories(pkt->getPeerId(), false);
+ sendDetachedInventories(peer_id, false);
// Send player movement settings
- SendMovement(pkt->getPeerId());
+ SendMovement(peer_id);
// Send time of day
u16 time = m_env->getTimeOfDay();
float time_speed = g_settings->getFloat("time_speed");
- SendTimeOfDay(pkt->getPeerId(), time, time_speed);
+ SendTimeOfDay(peer_id, time, time_speed);
- SendCSMRestrictionFlags(pkt->getPeerId());
+ SendCSMRestrictionFlags(peer_id);
// Warnings about protocol version can be issued here
if (client->net_proto_version < LATEST_PROTOCOL_VERSION) {
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
+ SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
L"# Server: WARNING: YOUR CLIENT'S VERSION MAY NOT BE FULLY COMPATIBLE "
L"WITH THIS SERVER!"));
}
@@ -346,8 +348,9 @@ void Server::handleCommand_RequestMedia(NetworkPacket* pkt)
*pkt >> numfiles;
- infostream << "Sending " << numfiles << " files to "
- << getPlayerName(pkt->getPeerId()) << std::endl;
+ session_t peer_id = pkt->getPeerId();
+ infostream << "Sending " << numfiles << " files to " <<
+ getPlayerName(peer_id) << std::endl;
verbosestream << "TOSERVER_REQUEST_MEDIA: " << std::endl;
for (u16 i = 0; i < numfiles; i++) {
@@ -360,7 +363,7 @@ void Server::handleCommand_RequestMedia(NetworkPacket* pkt)
<< name << std::endl;
}
- sendRequestedMedia(pkt->getPeerId(), tosend);
+ sendRequestedMedia(peer_id, tosend);
}
void Server::handleCommand_ClientReady(NetworkPacket* pkt)
@@ -370,18 +373,16 @@ void Server::handleCommand_ClientReady(NetworkPacket* pkt)
PlayerSAO* playersao = StageTwoClientInit(peer_id);
if (playersao == NULL) {
- actionstream
- << "TOSERVER_CLIENT_READY stage 2 client init failed for peer_id: "
- << peer_id << std::endl;
+ errorstream << "TOSERVER_CLIENT_READY stage 2 client init failed "
+ "peer_id=" << peer_id << std::endl;
DisconnectPeer(peer_id);
return;
}
if (pkt->getSize() < 8) {
- errorstream
- << "TOSERVER_CLIENT_READY client sent inconsistent data, disconnecting peer_id: "
- << peer_id << std::endl;
+ errorstream << "TOSERVER_CLIENT_READY client sent inconsistent data, "
+ "disconnecting peer_id: " << peer_id << std::endl;
DisconnectPeer(peer_id);
return;
}
@@ -390,9 +391,8 @@ void Server::handleCommand_ClientReady(NetworkPacket* pkt)
std::string full_ver;
*pkt >> major_ver >> minor_ver >> patch_ver >> reserved >> full_ver;
- m_clients.setClientVersion(
- peer_id, major_ver, minor_ver, patch_ver,
- full_ver);
+ m_clients.setClientVersion(peer_id, major_ver, minor_ver, patch_ver,
+ full_ver);
if (pkt->getRemainingBytes() >= 2)
*pkt >> playersao->getPlayer()->formspec_version;
@@ -409,12 +409,15 @@ void Server::handleCommand_ClientReady(NetworkPacket* pkt)
// (u16) 1 + std::string represents a pseudo vector serialization representation
notice_pkt << (u8) PLAYER_LIST_ADD << (u16) 1 << std::string(playersao->getPlayer()->getName());
m_clients.sendToAll(&notice_pkt);
-
m_clients.event(peer_id, CSE_SetClientReady);
- m_script->on_joinplayer(playersao);
+
+ s64 last_login;
+ m_script->getAuth(playersao->getPlayer()->getName(), nullptr, nullptr, &last_login);
+ m_script->on_joinplayer(playersao, last_login);
+
// Send shutdown timer if shutdown has been scheduled
if (m_shutdown_state.isTimerRunning()) {
- SendChatMessage(pkt->getPeerId(), m_shutdown_state.getShutdownTimerMessage());
+ SendChatMessage(peer_id, m_shutdown_state.getShutdownTimerMessage());
}
}
@@ -498,6 +501,7 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
player->control.sneak = (keyPressed & 64);
player->control.LMB = (keyPressed & 128);
player->control.RMB = (keyPressed & 256);
+ player->control.zoom = (keyPressed & 512);
if (playersao->checkMovementCheat()) {
// Call callbacks
@@ -508,21 +512,22 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
{
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -568,22 +573,23 @@ void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)
void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
{
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -619,9 +625,9 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
ma->from_inv.applyCurrentPlayer(player->getName());
ma->to_inv.applyCurrentPlayer(player->getName());
- setInventoryModified(ma->from_inv);
+ m_inventory_mgr->setInventoryModified(ma->from_inv);
if (ma->from_inv != ma->to_inv)
- setInventoryModified(ma->to_inv);
+ m_inventory_mgr->setInventoryModified(ma->to_inv);
bool from_inv_is_current_player =
(ma->from_inv.type == InventoryLocation::PLAYER) &&
@@ -686,7 +692,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
da->from_inv.applyCurrentPlayer(player->getName());
- setInventoryModified(da->from_inv);
+ m_inventory_mgr->setInventoryModified(da->from_inv);
/*
Disable dropping items out of craftpreview
@@ -722,7 +728,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
ca->craft_inv.applyCurrentPlayer(player->getName());
- setInventoryModified(ca->craft_inv);
+ m_inventory_mgr->setInventoryModified(ca->craft_inv);
//bool craft_inv_is_current_player =
// (ca->craft_inv.type == InventoryLocation::PLAYER) &&
@@ -738,7 +744,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
}
// Do the action
- a->apply(this, playersao, this);
+ a->apply(m_inventory_mgr.get(), playersao, this);
// Eat the action
delete a;
}
@@ -761,12 +767,13 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
message += (wchar_t)tmp_wchar;
}
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -777,8 +784,8 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
std::wstring answer_to_sender = handleChat(name, wname, message, true, player);
if (!answer_to_sender.empty()) {
// Send the answer to sender
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_NORMAL,
- answer_to_sender, wname));
+ SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_NORMAL,
+ answer_to_sender, wname));
}
}
@@ -788,22 +795,23 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)
*pkt >> damage;
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -825,105 +833,28 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)
}
}
-void Server::handleCommand_Password(NetworkPacket* pkt)
-{
- if (pkt->getSize() != PASSWORD_SIZE * 2)
- return;
-
- std::string oldpwd;
- std::string newpwd;
-
- // Deny for clients using the new protocol
- RemoteClient* client = getClient(pkt->getPeerId(), CS_Created);
- if (client->net_proto_version >= 25) {
- infostream << "Server::handleCommand_Password(): Denying change: "
- << " Client protocol version for peer_id=" << pkt->getPeerId()
- << " too new!" << std::endl;
- return;
- }
-
- for (u16 i = 0; i < PASSWORD_SIZE - 1; i++) {
- char c = pkt->getChar(i);
- if (c == 0)
- break;
- oldpwd += c;
- }
-
- for (u16 i = 0; i < PASSWORD_SIZE - 1; i++) {
- char c = pkt->getChar(PASSWORD_SIZE + i);
- if (c == 0)
- break;
- newpwd += c;
- }
-
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
- if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
- return;
- }
-
- if (!base64_is_valid(newpwd)) {
- infostream<<"Server: " << player->getName() <<
- " supplied invalid password hash" << std::endl;
- // Wrong old password supplied!!
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Invalid new password hash supplied. Password NOT changed."));
- return;
- }
-
- infostream << "Server: Client requests a password change from "
- << "'" << oldpwd << "' to '" << newpwd << "'" << std::endl;
-
- std::string playername = player->getName();
-
- std::string checkpwd;
- m_script->getAuth(playername, &checkpwd, NULL);
-
- if (oldpwd != checkpwd) {
- infostream << "Server: invalid old password" << std::endl;
- // Wrong old password supplied!!
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Invalid old password supplied. Password NOT changed."));
- return;
- }
-
- bool success = m_script->setPassword(playername, newpwd);
- if (success) {
- actionstream << player->getName() << " changes password" << std::endl;
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Password change successful."));
- } else {
- actionstream << player->getName() << " tries to change password but "
- << "it fails" << std::endl;
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Password change failed or unavailable."));
- }
-}
-
void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
{
if (pkt->getSize() < 2)
return;
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -936,12 +867,13 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
void Server::handleCommand_Respawn(NetworkPacket* pkt)
{
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -951,7 +883,7 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt)
if (!playersao->isDead())
return;
- RespawnPlayer(pkt->getPeerId());
+ RespawnPlayer(peer_id);
actionstream << player->getName() << " respawns at "
<< PP(playersao->getBasePosition() / BS) << std::endl;
@@ -973,8 +905,8 @@ bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std:
actionstream << "Player " << player->getName()
<< " tried to access " << what
<< " from too far: "
- << "d=" << d <<", max_d=" << max_d
- << ". ignoring." << std::endl;
+ << "d=" << d << ", max_d=" << max_d
+ << "; ignoring." << std::endl;
// Call callbacks
m_script->on_cheat(player->getPlayerSAO(), "interacted_too_far");
return false;
@@ -1006,31 +938,32 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item="
<< item_i << ", pointed=" << pointed.dump() << std::endl;
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
if (playersao->isDead()) {
- actionstream << "Server: NoCheat: " << player->getName()
+ actionstream << "Server: " << player->getName()
<< " tried to interact while dead; ignoring." << std::endl;
if (pointed.type == POINTEDTHING_NODE) {
// Re-send block to revert change on client-side
- RemoteClient *client = getClient(pkt->getPeerId());
+ RemoteClient *client = getClient(peer_id);
v3s16 blockpos = getNodeBlockPos(pointed.node_undersurface);
client->SetBlockNotSent(blockpos);
}
@@ -1081,7 +1014,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
pointed.dump() << " without 'interact' privilege" << std::endl;
// Re-send block to revert change on client-side
- RemoteClient *client = getClient(pkt->getPeerId());
+ RemoteClient *client = getClient(peer_id);
// Digging completed -> under
if (action == INTERACT_DIGGING_COMPLETED) {
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
@@ -1109,7 +1042,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
if (!checkInteractDistance(player, d, pointed.dump())) {
// Re-send block to revert change on client-side
- RemoteClient *client = getClient(pkt->getPeerId());
+ RemoteClient *client = getClient(peer_id);
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
client->SetBlockNotSent(blockpos);
return;
@@ -1132,11 +1065,10 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
n = m_env->getMap().getNode(p_under, &pos_ok);
if (!pos_ok) {
- infostream << "Server: Not punching: Node not found."
- << " Adding block to emerge queue."
- << std::endl;
- m_emerge->enqueueBlockEmerge(pkt->getPeerId(),
- getNodeBlockPos(p_above), false);
+ infostream << "Server: Not punching: Node not found. "
+ "Adding block to emerge queue." << std::endl;
+ m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above),
+ false);
}
if (n.getContent() != CONTENT_IGNORE)
@@ -1202,11 +1134,10 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
bool pos_ok;
MapNode n = m_env->getMap().getNode(p_under, &pos_ok);
if (!pos_ok) {
- infostream << "Server: Not finishing digging: Node not found."
- << " Adding block to emerge queue."
- << std::endl;
- m_emerge->enqueueBlockEmerge(pkt->getPeerId(),
- getNodeBlockPos(p_above), false);
+ infostream << "Server: Not finishing digging: Node not found. "
+ "Adding block to emerge queue." << std::endl;
+ m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above),
+ false);
}
/* Cheat prevention */
@@ -1217,7 +1148,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
playersao->noCheatDigEnd();
// If player didn't start digging this, ignore dig
if (nocheat_p != p_under) {
- infostream << "Server: NoCheat: " << player->getName()
+ infostream << "Server: " << player->getName()
<< " started digging "
<< PP(nocheat_p) << " and completed digging "
<< PP(p_under) << "; not digging." << std::endl;
@@ -1241,9 +1172,9 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
}
// If can't dig, ignore dig
if (!params.diggable) {
- infostream << "Server: NoCheat: " << player->getName()
+ infostream << "Server: " << player->getName()
<< " completed digging " << PP(p_under)
- << ", which is not diggable with tool. not digging."
+ << ", which is not diggable with tool; not digging."
<< std::endl;
is_valid_dig = false;
// Call callbacks
@@ -1267,7 +1198,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
}
// Dig not possible
else {
- infostream << "Server: NoCheat: " << player->getName()
+ infostream << "Server: " << player->getName()
<< " completed digging " << PP(p_under)
<< "too fast; not digging." << std::endl;
is_valid_dig = false;
@@ -1282,7 +1213,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
m_script->node_on_dig(p_under, n, playersao);
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
- RemoteClient *client = getClient(pkt->getPeerId());
+ RemoteClient *client = getClient(peer_id);
// Send unusual result (that is, node not being removed)
if (m_env->getMap().getNode(p_under).getContent() != CONTENT_AIR) {
// Re-send block to revert change on client-side
@@ -1304,7 +1235,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Reset build time counter
if (pointed.type == POINTEDTHING_NODE &&
selected_item.getDefinition(m_itemdef).type == ITEM_NODE)
- getClient(pkt->getPeerId())->m_time_from_building = 0.0;
+ getClient(peer_id)->m_time_from_building = 0.0;
if (pointed.type == POINTEDTHING_OBJECT) {
// Right click object
@@ -1338,7 +1269,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// If item has node placement prediction, always send the
// blocks to make sure the client knows what exactly happened
- RemoteClient *client = getClient(pkt->getPeerId());
+ RemoteClient *client = getClient(peer_id);
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
v3s16 blockpos2 = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
if (!selected_item.getDefinition(m_itemdef).node_placement_prediction.empty()) {
@@ -1441,22 +1372,23 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
fields[fieldname] = pkt->readLongString();
}
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -1492,22 +1424,23 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
fields[fieldname] = pkt->readLongString();
}
- RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ RemotePlayer *player = m_env->getPlayer(peer_id);
if (player == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
PlayerSAO *playersao = player->getPlayerSAO();
if (playersao == NULL) {
- errorstream << "Server::ProcessData(): Canceling: "
- "No player object for peer_id=" << pkt->getPeerId()
- << " disconnecting peer!" << std::endl;
- DisconnectPeer(pkt->getPeerId());
+ errorstream <<
+ "Server::ProcessData(): Canceling: No player object for peer_id=" <<
+ peer_id << " disconnecting peer!" << std::endl;
+ DisconnectPeer(peer_id);
return;
}
@@ -1517,7 +1450,7 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
}
// verify that we displayed the formspec to the user
- const auto peer_state_iterator = m_formspec_state_data.find(pkt->getPeerId());
+ const auto peer_state_iterator = m_formspec_state_data.find(peer_id);
if (peer_state_iterator != m_formspec_state_data.end()) {
const std::string &server_formspec_name = peer_state_iterator->second;
if (client_formspec_name == server_formspec_name) {
@@ -1543,7 +1476,8 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
{
- RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
+ session_t peer_id = pkt->getPeerId();
+ RemoteClient *client = getClient(peer_id, CS_Invalid);
ClientState cstate = client->getState();
std::string playername = client->getName();
@@ -1551,7 +1485,7 @@ void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
std::string salt;
std::string verification_key;
- std::string addr_s = getPeerAddress(pkt->getPeerId()).serializeString();
+ std::string addr_s = getPeerAddress(peer_id).serializeString();
u8 is_empty;
*pkt >> salt >> verification_key >> is_empty;
@@ -1565,7 +1499,7 @@ void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
actionstream << "Server: Client from " << addr_s
<< " tried to set password without being "
<< "authenticated, or the username being new." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
@@ -1574,7 +1508,7 @@ void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
is_empty == 1) {
actionstream << "Server: " << playername
<< " supplied empty password from " << addr_s << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_EMPTY_PASSWORD);
return;
}
@@ -1582,8 +1516,9 @@ void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
initial_ver_key = encode_srp_verifier(verification_key, salt);
m_script->createAuth(playername, initial_ver_key);
+ m_script->on_authplayer(playername, addr_s, true);
- acceptAuth(pkt->getPeerId(), false);
+ acceptAuth(peer_id, false);
} else {
if (cstate < CS_SudoMode) {
infostream << "Server::ProcessData(): Ignoring TOSERVER_FIRST_SRP from "
@@ -1591,48 +1526,48 @@ void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
<< std::endl;
return;
}
- m_clients.event(pkt->getPeerId(), CSE_SudoLeave);
+ m_clients.event(peer_id, CSE_SudoLeave);
std::string pw_db_field = encode_srp_verifier(verification_key, salt);
bool success = m_script->setPassword(playername, pw_db_field);
if (success) {
actionstream << playername << " changes password" << std::endl;
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Password change successful."));
+ SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
+ L"Password change successful."));
} else {
- actionstream << playername << " tries to change password but "
- << "it fails" << std::endl;
- SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
- L"Password change failed or unavailable."));
+ actionstream << playername <<
+ " tries to change password but it fails" << std::endl;
+ SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
+ L"Password change failed or unavailable."));
}
}
}
void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
{
- RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
+ session_t peer_id = pkt->getPeerId();
+ RemoteClient *client = getClient(peer_id, CS_Invalid);
ClientState cstate = client->getState();
bool wantSudo = (cstate == CS_Active);
if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
- actionstream << "Server: got SRP _A packet in wrong state "
- << cstate << " from "
- << getPeerAddress(pkt->getPeerId()).serializeString()
- << ". Ignoring." << std::endl;
+ actionstream << "Server: got SRP _A packet in wrong state " << cstate <<
+ " from " << getPeerAddress(peer_id).serializeString() <<
+ ". Ignoring." << std::endl;
return;
}
if (client->chosen_mech != AUTH_MECHANISM_NONE) {
- actionstream << "Server: got SRP _A packet, while auth"
- << "is already going on with mech " << client->chosen_mech
- << " from " << getPeerAddress(pkt->getPeerId()).serializeString()
- << " (wantSudo=" << wantSudo << "). Ignoring." << std::endl;
+ actionstream << "Server: got SRP _A packet, while auth is already "
+ "going on with mech " << client->chosen_mech << " from " <<
+ getPeerAddress(peer_id).serializeString() <<
+ " (wantSudo=" << wantSudo << "). Ignoring." << std::endl;
if (wantSudo) {
- DenySudoAccess(pkt->getPeerId());
+ DenySudoAccess(peer_id);
return;
}
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
@@ -1649,19 +1584,19 @@ void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
if (wantSudo) {
if (!client->isSudoMechAllowed(chosen)) {
- actionstream << "Server: Player \"" << client->getName()
- << "\" at " << getPeerAddress(pkt->getPeerId()).serializeString()
- << " tried to change password using unallowed mech "
- << chosen << "." << std::endl;
- DenySudoAccess(pkt->getPeerId());
+ actionstream << "Server: Player \"" << client->getName() <<
+ "\" at " << getPeerAddress(peer_id).serializeString() <<
+ " tried to change password using unallowed mech " << chosen <<
+ "." << std::endl;
+ DenySudoAccess(peer_id);
return;
}
} else {
if (!client->isMechAllowed(chosen)) {
- actionstream << "Server: Client tried to authenticate from "
- << getPeerAddress(pkt->getPeerId()).serializeString()
- << " using unallowed mech " << chosen << "." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ actionstream << "Server: Client tried to authenticate from " <<
+ getPeerAddress(peer_id).serializeString() <<
+ " using unallowed mech " << chosen << "." << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
}
@@ -1677,10 +1612,10 @@ void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
&verifier, &salt);
} else if (!decode_srp_verifier_and_salt(client->enc_pwd, &verifier, &salt)) {
// Non-base64 errors should have been catched in the init handler
- actionstream << "Server: User " << client->getName()
- << " tried to log in, but srp verifier field"
- << " was invalid (most likely invalid base64)." << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
+ actionstream << "Server: User " << client->getName() <<
+ " tried to log in, but srp verifier field was invalid (most likely "
+ "invalid base64)." << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
return;
}
@@ -1700,23 +1635,26 @@ void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
<< " tried to log in, SRP-6a safety check violated in _A handler."
<< std::endl;
if (wantSudo) {
- DenySudoAccess(pkt->getPeerId());
+ DenySudoAccess(peer_id);
return;
}
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
- NetworkPacket resp_pkt(TOCLIENT_SRP_BYTES_S_B, 0, pkt->getPeerId());
+ NetworkPacket resp_pkt(TOCLIENT_SRP_BYTES_S_B, 0, peer_id);
resp_pkt << salt << std::string(bytes_B, len_B);
Send(&resp_pkt);
}
void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
{
- RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
+ session_t peer_id = pkt->getPeerId();
+ RemoteClient *client = getClient(peer_id, CS_Invalid);
ClientState cstate = client->getState();
+ std::string addr_s = getPeerAddress(pkt->getPeerId()).serializeString();
+ std::string playername = client->getName();
bool wantSudo = (cstate == CS_Active);
@@ -1724,8 +1662,7 @@ void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
actionstream << "Server: got SRP _M packet in wrong state "
- << cstate << " from "
- << getPeerAddress(pkt->getPeerId()).serializeString()
+ << cstate << " from " << addr_s
<< ". Ignoring." << std::endl;
return;
}
@@ -1733,15 +1670,14 @@ void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
if (client->chosen_mech != AUTH_MECHANISM_SRP &&
client->chosen_mech != AUTH_MECHANISM_LEGACY_PASSWORD) {
actionstream << "Server: got SRP _M packet, while auth"
- << "is going on with mech " << client->chosen_mech
- << " from " << getPeerAddress(pkt->getPeerId()).serializeString()
- << " (wantSudo=" << wantSudo << "). Denying." << std::endl;
+ << "is going on with mech " << client->chosen_mech << " from "
+ << addr_s << " (wantSudo=" << wantSudo << "). Denying." << std::endl;
if (wantSudo) {
- DenySudoAccess(pkt->getPeerId());
+ DenySudoAccess(peer_id);
return;
}
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
@@ -1750,10 +1686,9 @@ void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
if (srp_verifier_get_session_key_length((SRPVerifier *) client->auth_data)
!= bytes_M.size()) {
- actionstream << "Server: User " << client->getName()
- << " at " << getPeerAddress(pkt->getPeerId()).serializeString()
+ actionstream << "Server: User " << playername << " at " << addr_s
<< " sent bytes_M with invalid length " << bytes_M.size() << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_UNEXPECTED_DATA);
return;
}
@@ -1764,39 +1699,36 @@ void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
if (!bytes_HAMK) {
if (wantSudo) {
- actionstream << "Server: User " << client->getName()
- << " at " << getPeerAddress(pkt->getPeerId()).serializeString()
+ actionstream << "Server: User " << playername << " at " << addr_s
<< " tried to change their password, but supplied wrong"
<< " (SRP) password for authentication." << std::endl;
- DenySudoAccess(pkt->getPeerId());
+ DenySudoAccess(peer_id);
return;
}
- std::string ip = getPeerAddress(pkt->getPeerId()).serializeString();
- actionstream << "Server: User " << client->getName()
- << " at " << ip
- << " supplied wrong password (auth mechanism: SRP)."
- << std::endl;
- m_script->on_auth_failure(client->getName(), ip);
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_PASSWORD);
+ actionstream << "Server: User " << playername << " at " << addr_s
+ << " supplied wrong password (auth mechanism: SRP)." << std::endl;
+ m_script->on_authplayer(playername, addr_s, false);
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_WRONG_PASSWORD);
return;
}
if (client->create_player_on_auth_success) {
- std::string playername = client->getName();
m_script->createAuth(playername, client->enc_pwd);
std::string checkpwd; // not used, but needed for passing something
if (!m_script->getAuth(playername, &checkpwd, NULL)) {
- actionstream << "Server: " << playername << " cannot be authenticated"
- << " (auth handler does not work?)" << std::endl;
- DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
+ actionstream << "Server: " << playername <<
+ " cannot be authenticated (auth handler does not work?)" <<
+ std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_SERVER_FAIL);
return;
}
client->create_player_on_auth_success = false;
}
- acceptAuth(pkt->getPeerId(), wantSudo);
+ m_script->on_authplayer(playername, addr_s, true);
+ acceptAuth(peer_id, wantSudo);
}
/*
@@ -1808,20 +1740,21 @@ void Server::handleCommand_ModChannelJoin(NetworkPacket *pkt)
std::string channel_name;
*pkt >> channel_name;
- NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
- pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
+ 1 + 2 + channel_name.size(), peer_id);
// Send signal to client to notify join succeed or not
if (g_settings->getBool("enable_mod_channels") &&
- m_modchannel_mgr->joinChannel(channel_name, pkt->getPeerId())) {
+ m_modchannel_mgr->joinChannel(channel_name, peer_id)) {
resp_pkt << (u8) MODCHANNEL_SIGNAL_JOIN_OK;
- infostream << "Peer " << pkt->getPeerId() << " joined channel " << channel_name
- << std::endl;
+ infostream << "Peer " << peer_id << " joined channel " <<
+ channel_name << std::endl;
}
else {
resp_pkt << (u8)MODCHANNEL_SIGNAL_JOIN_FAILURE;
- infostream << "Peer " << pkt->getPeerId() << " tried to join channel "
- << channel_name << ", but was already registered." << std::endl;
+ infostream << "Peer " << peer_id << " tried to join channel " <<
+ channel_name << ", but was already registered." << std::endl;
}
resp_pkt << channel_name;
Send(&resp_pkt);
@@ -1832,19 +1765,20 @@ void Server::handleCommand_ModChannelLeave(NetworkPacket *pkt)
std::string channel_name;
*pkt >> channel_name;
- NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
- pkt->getPeerId());
+ session_t peer_id = pkt->getPeerId();
+ NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
+ 1 + 2 + channel_name.size(), peer_id);
// Send signal to client to notify join succeed or not
if (g_settings->getBool("enable_mod_channels") &&
- m_modchannel_mgr->leaveChannel(channel_name, pkt->getPeerId())) {
+ m_modchannel_mgr->leaveChannel(channel_name, peer_id)) {
resp_pkt << (u8)MODCHANNEL_SIGNAL_LEAVE_OK;
- infostream << "Peer " << pkt->getPeerId() << " left channel " << channel_name
- << std::endl;
+ infostream << "Peer " << peer_id << " left channel " << channel_name <<
+ std::endl;
} else {
resp_pkt << (u8) MODCHANNEL_SIGNAL_LEAVE_FAILURE;
- infostream << "Peer " << pkt->getPeerId() << " left channel " << channel_name
- << ", but was not registered." << std::endl;
+ infostream << "Peer " << peer_id << " left channel " << channel_name <<
+ ", but was not registered." << std::endl;
}
resp_pkt << channel_name;
Send(&resp_pkt);
@@ -1855,8 +1789,10 @@ void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
std::string channel_name, channel_msg;
*pkt >> channel_name >> channel_msg;
- verbosestream << "Mod channel message received from peer " << pkt->getPeerId()
- << " on channel " << channel_name << " message: " << channel_msg << std::endl;
+ session_t peer_id = pkt->getPeerId();
+ verbosestream << "Mod channel message received from peer " << peer_id <<
+ " on channel " << channel_name << " message: " << channel_msg <<
+ std::endl;
// If mod channels are not enabled, discard message
if (!g_settings->getBool("enable_mod_channels")) {
@@ -1865,8 +1801,8 @@ void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
// If channel not registered, signal it and ignore message
if (!m_modchannel_mgr->channelRegistered(channel_name)) {
- NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
- pkt->getPeerId());
+ NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL,
+ 1 + 2 + channel_name.size(), peer_id);
resp_pkt << (u8)MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED << channel_name;
Send(&resp_pkt);
return;
@@ -1874,5 +1810,5 @@ void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
// @TODO: filter, rate limit
- broadcastModChannelMessage(channel_name, channel_msg, pkt->getPeerId());
+ broadcastModChannelMessage(channel_name, channel_msg, peer_id);
}