Skip to content

Commit

Permalink
[CSM] Add flavour limits controlled by server (#5930)
Browse files Browse the repository at this point in the history
* [CSM] Add flavour limits controlled by server

Server send flavour limits to client permitting to disable or limit some Lua calls

* Add limits for reading nodedefs and itemdefs

* flavour: Add lookup node limits

* Merge get_node_or_nil into get_node.

Sending fake node doesn't make sense in CSM, just return nil if node is not available for any reason

* Add node range customization when noderange flavour is enabled (default 8 nodes)

* Limit nodes range & disable chat message sending by default

* Bump protocol version
  • Loading branch information
nerzhul committed Jul 18, 2017
1 parent 7e3cdf7 commit 79f19b8
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 29 deletions.
13 changes: 13 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -960,6 +960,19 @@ block_send_optimize_distance (block send optimize distance) int 4 2
# so that the utility of noclip mode is reduced.
server_side_occlusion_culling (Server side occlusion culling) bool true

# Restricts the access of certain client-side functions on servers
# Combine these byteflags below to restrict more client-side features:
# LOOKUP_NODES_LIMIT: 1 (limits get_node call client-side to csm_flavour_noderange_limit)
# CHAT_MESSAGES: 2 (disable send_chat_message call client-side)
# READ_ITEMDEFS: 4 (disable get_item_def call client-side)
# READ_NODEDEFS: 8 (disable get_node_def call client-side)
# type: int
csm_flavour_limits (Client side modding flavour limits) int 3

# If the CSM flavour for node range is enabled, get_node is limited to
# this many nodes from the player.
csm_flavour_noderange_limit (Client side noderange flavour limit) int 8

[*Mapgen]

# Name of map generator to be used when creating a new world.
Expand Down
8 changes: 3 additions & 5 deletions doc/client_lua_api.md
Expand Up @@ -701,12 +701,10 @@ Call these functions only at load time!
* Returns the time of day: `0` for midnight, `0.5` for midday

### Map
* `minetest.get_node(pos)`
* Returns the node at the given position as table in the format
`{name="node_name", param1=0, param2=0}`, returns `{name="ignore", param1=0, param2=0}`
for unloaded areas.
* `minetest.get_node_or_nil(pos)`
* Same as `get_node` but returns `nil` for unloaded areas.
* Returns the node at the given position as table in the format
`{name="node_name", param1=0, param2=0}`, returns `nil`
for unloaded areas or flavour limited areas.
* `minetest.find_node_near(pos, radius, nodenames, [search_center])`: returns pos or `nil`
* `radius`: using a maximum metric
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
Expand Down
13 changes: 13 additions & 0 deletions minetest.conf.example
Expand Up @@ -1172,6 +1172,19 @@
# type: bool
# server_side_occlusion_culling = true

# Restricts the access of certain client-side functions on servers
# Combine these byteflags below to restrict more client-side features:
# LOOKUP_NODES_LIMIT: 1 (limits get_node call client-side to csm_flavour_noderange_limit)
# CHAT_MESSAGES: 2 (disable send_chat_message call client-side)
# READ_ITEMDEFS: 4 (disable get_item_def call client-side)
# READ_NODEDEFS: 8 (disable get_node_def call client-side)
# type: int
# csm_flavour_limits = 3

# If the CSM flavour for node range is enabled, get_node is limited to
# this many nodes from the player.
# csm_flavour_noderange_limit 8

## Mapgen

# Name of map generator to be used when creating a new world.
Expand Down
14 changes: 14 additions & 0 deletions src/client.cpp
Expand Up @@ -1361,8 +1361,22 @@ void Client::removeNode(v3s16 p)
}
}

/**
* Helper function for Client Side Modding
* Flavour is applied there, this should not be used for core engine
* @param p
* @param is_valid_position
* @return
*/
MapNode Client::getNode(v3s16 p, bool *is_valid_position)
{
if (checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_LOOKUP_NODES)) {
v3s16 ppos = floatToInt(m_env.getLocalPlayer()->getPosition(), BS);
if ((u32) ppos.getDistanceFrom(p) > m_csm_noderange_limit) {
*is_valid_position = false;
return MapNode();
}
}
return m_env.getMap().getNodeNoEx(p, is_valid_position);
}

Expand Down
23 changes: 23 additions & 0 deletions src/client.h
Expand Up @@ -364,6 +364,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void handleCommand_EyeOffset(NetworkPacket* pkt);
void handleCommand_UpdatePlayerList(NetworkPacket* pkt);
void handleCommand_SrpBytesSandB(NetworkPacket* pkt);
void handleCommand_CSMFlavourLimits(NetworkPacket *pkt);

void ProcessData(NetworkPacket *pkt);

Expand Down Expand Up @@ -396,6 +397,14 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef

// Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
void removeNode(v3s16 p);

/**
* Helper function for Client Side Modding
* Flavour is applied there, this should not be used for core engine
* @param p
* @param is_valid_position
* @return
*/
MapNode getNode(v3s16 p, bool *is_valid_position);
void addNode(v3s16 p, MapNode n, bool remove_metadata = true);

Expand Down Expand Up @@ -552,6 +561,16 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
return m_address_name;
}

inline bool checkCSMFlavourLimit(CSMFlavourLimit flag) const
{
return m_csm_flavour_limits & flag;
}

u32 getCSMNodeRangeLimit() const
{
return m_csm_noderange_limit;
}

private:

// Virtual methods from con::PeerHandler
Expand Down Expand Up @@ -705,6 +724,10 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
GameUIFlags *m_game_ui_flags;

bool m_shutdown = false;

// CSM flavour limits byteflag
u64 m_csm_flavour_limits = CSMFlavourLimit::CSM_FL_NONE;
u32 m_csm_noderange_limit = 8;
};

#endif // !CLIENT_HEADER
2 changes: 2 additions & 0 deletions src/defaultsettings.cpp
Expand Up @@ -294,6 +294,8 @@ void set_default_settings(Settings *settings)
settings->setDefault("max_block_send_distance", "9");
settings->setDefault("block_send_optimize_distance", "4");
settings->setDefault("server_side_occlusion_culling", "true");
settings->setDefault("csm_flavour_limits", "3");
settings->setDefault("csm_flavour_noderange_limit", "8");
settings->setDefault("max_clearobjects_extra_loaded_blocks", "4096");
settings->setDefault("time_speed", "72");
settings->setDefault("server_unload_unused_data_timeout", "29");
Expand Down
2 changes: 1 addition & 1 deletion src/network/clientopcodes.cpp
Expand Up @@ -66,7 +66,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_INVENTORY", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_Inventory }, // 0x27
null_command_handler,
{ "TOCLIENT_TIME_OF_DAY", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_TimeOfDay }, // 0x29
null_command_handler,
{ "TOCLIENT_CSM_FLAVOUR_LIMITS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CSMFlavourLimits }, // 0x2A
null_command_handler,
null_command_handler,
null_command_handler,
Expand Down
5 changes: 5 additions & 0 deletions src/network/clientpackethandler.cpp
Expand Up @@ -1376,3 +1376,8 @@ void Client::handleCommand_SrpBytesSandB(NetworkPacket* pkt)
resp_pkt << std::string(bytes_M, len_M);
Send(&resp_pkt);
}

void Client::handleCommand_CSMFlavourLimits(NetworkPacket *pkt)
{
*pkt >> m_csm_flavour_limits >> m_csm_noderange_limit;
}
19 changes: 16 additions & 3 deletions src/network/networkprotocol.h
Expand Up @@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef NETWORKPROTOCOL_HEADER
#define NETWORKPROTOCOL_HEADER
#pragma once

#include "util/string.h"

/*
Expand Down Expand Up @@ -168,6 +168,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
* sender
* type (RAW, NORMAL, ANNOUNCE, SYSTEM)
* content
Add TOCLIENT_CSM_FLAVOUR_LIMITS to define which CSM flavour should be limited
*/

#define LATEST_PROTOCOL_VERSION 35
Expand Down Expand Up @@ -313,6 +314,11 @@ enum ToClientCommand
f1000 time_speed
*/

TOCLIENT_CSM_FLAVOUR_LIMITS = 0x2A,
/*
u32 CSMFlavourLimits byteflag
*/

// (oops, there is some gap here)

TOCLIENT_CHAT_MESSAGE = 0x2F,
Expand Down Expand Up @@ -1003,5 +1009,12 @@ enum PlayerListModifer: u8
PLAYER_LIST_REMOVE,
};

enum CSMFlavourLimit : u64 {
CSM_FL_NONE = 0x00000000,
CSM_FL_LOOKUP_NODES = 0x00000001, // Limit node lookups
CSM_FL_CHAT_MESSAGES = 0x00000002, // Disable chat message sending from CSM
CSM_FL_READ_ITEMDEFS = 0x00000004, // Disable itemdef lookups
CSM_FL_READ_NODEDEFS = 0x00000008, // Disable nodedef lookups
CSM_FL_ALL = 0xFFFFFFFF,
};

#endif
2 changes: 1 addition & 1 deletion src/network/serveropcodes.cpp
Expand Up @@ -155,7 +155,7 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_INVENTORY", 0, true }, // 0x27
null_command_factory,
{ "TOCLIENT_TIME_OF_DAY", 0, true }, // 0x29
null_command_factory,
{ "TOCLIENT_CSM_FLAVOUR_LIMITS", 0, true }, // 0x2A
null_command_factory,
null_command_factory,
null_command_factory,
Expand Down
2 changes: 2 additions & 0 deletions src/network/serverpackethandler.cpp
Expand Up @@ -643,6 +643,8 @@ void Server::handleCommand_Init2(NetworkPacket* pkt)
float time_speed = g_settings->getFloat("time_speed");
SendTimeOfDay(pkt->getPeerId(), time, time_speed);

SendCSMFlavourLimits(pkt->getPeerId());

// Warnings about protocol version can be issued here
if (getClient(pkt->getPeerId())->net_proto_version < LATEST_PROTOCOL_VERSION) {
SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
Expand Down
27 changes: 12 additions & 15 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -91,6 +91,11 @@ int ModApiClient::l_send_chat_message(lua_State *L)
{
if (!lua_isstring(L, 1))
return 0;

// If server disabled this API, discard
if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_CHAT_MESSAGES))
return 0;

std::string message = luaL_checkstring(L, 1);
getClient(L)->sendChatMessage(utf8_to_wide(message));
return 0;
Expand Down Expand Up @@ -166,24 +171,11 @@ int ModApiClient::l_gettext(lua_State *L)

// get_node(pos)
// pos = {x=num, y=num, z=num}
int ModApiClient::l_get_node(lua_State *L)
{
// pos
v3s16 pos = read_v3s16(L, 1);
// Do it
bool pos_ok;
MapNode n = getClient(L)->getNode(pos, &pos_ok);
// Return node
pushnode(L, n, getClient(L)->ndef());
return 1;
}

// get_node_or_nil(pos)
// pos = {x=num, y=num, z=num}
int ModApiClient::l_get_node_or_nil(lua_State *L)
{
// pos
v3s16 pos = read_v3s16(L, 1);

// Do it
bool pos_ok;
MapNode n = getClient(L)->getNode(pos, &pos_ok);
Expand Down Expand Up @@ -290,6 +282,9 @@ int ModApiClient::l_get_item_def(lua_State *L)
IItemDefManager *idef = gdef->idef();
assert(idef);

if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
return 0;

if (!lua_isstring(L, 1))
return 0;

Expand All @@ -315,6 +310,9 @@ int ModApiClient::l_get_node_def(lua_State *L)
if (!lua_isstring(L, 1))
return 0;

if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_NODEDEFS))
return 0;

const std::string &name = lua_tostring(L, 1);
const ContentFeatures &cf = ndef->get(ndef->getId(name));
if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
Expand Down Expand Up @@ -363,7 +361,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(show_formspec);
API_FCT(send_respawn);
API_FCT(gettext);
API_FCT(get_node);
API_FCT(get_node_or_nil);
API_FCT(get_wielded_item);
API_FCT(disconnect);
Expand Down
3 changes: 0 additions & 3 deletions src/script/lua_api/l_client.h
Expand Up @@ -65,9 +65,6 @@ class ModApiClient : public ModApiBase
static int l_set_last_run_mod(lua_State *L);

// get_node(pos)
static int l_get_node(lua_State *L);

// get_node_or_nil(pos)
static int l_get_node_or_nil(lua_State *L);

// get_wielded_item()
Expand Down
10 changes: 10 additions & 0 deletions src/script/lua_api/l_env.cpp
Expand Up @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_vmanip.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include <algorithm>
#include "scripting_server.h"
#include "environment.h"
#include "server.h"
Expand Down Expand Up @@ -726,6 +727,15 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
}

int start_radius = (lua_toboolean(L, 4)) ? 0 : 1;

#ifndef SERVER
// Client API limitations
if (getClient(L) &&
getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_LOOKUP_NODES)) {
radius = std::max<int>(radius, getClient(L)->getCSMNodeRangeLimit());
}
#endif

for (int d = start_radius; d <= radius; d++) {
std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
for (std::vector<v3s16>::iterator i = list.begin();
Expand Down
9 changes: 9 additions & 0 deletions src/server.cpp
Expand Up @@ -298,6 +298,8 @@ Server::Server(

m_liquid_transform_every = g_settings->getFloat("liquid_update");
m_max_chatmessage_length = g_settings->getU16("chat_message_max_size");
m_csm_flavour_limits = g_settings->getU64("csm_flavour_limits");
m_csm_noderange_limit = g_settings->getU32("csm_flavour_noderange_limit");
}

Server::~Server()
Expand Down Expand Up @@ -2017,7 +2019,14 @@ void Server::SendActiveObjectMessages(u16 peer_id, const std::string &datas, boo
m_clients.send(pkt.getPeerId(),
reliable ? clientCommandFactoryTable[pkt.getCommand()].channel : 1,
&pkt, reliable);
}

void Server::SendCSMFlavourLimits(u16 peer_id)
{
NetworkPacket pkt(TOCLIENT_CSM_FLAVOUR_LIMITS,
sizeof(m_csm_flavour_limits) + sizeof(m_csm_noderange_limit), peer_id);
pkt << m_csm_flavour_limits << m_csm_noderange_limit;
Send(&pkt);
}

s32 Server::playSound(const SimpleSoundSpec &spec,
Expand Down
7 changes: 6 additions & 1 deletion src/server.h
Expand Up @@ -466,6 +466,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,

u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas);
void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true);
void SendCSMFlavourLimits(u16 peer_id);

/*
Something random
*/
Expand Down Expand Up @@ -664,6 +666,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,

std::unordered_map<std::string, ModMetadata *> m_mod_storages;
float m_mod_storage_save_timer = 10.0f;

// CSM flavour limits byteflag
u64 m_csm_flavour_limits = CSMFlavourLimit::CSM_FL_NONE;
u32 m_csm_noderange_limit = 8;
};

/*
Expand All @@ -674,4 +680,3 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void dedicated_server_loop(Server &server, bool &kill);

#endif

5 changes: 5 additions & 0 deletions src/settings.cpp
Expand Up @@ -398,6 +398,11 @@ s16 Settings::getS16(const std::string &name) const
}


u32 Settings::getU32(const std::string &name) const
{
return (u32) stoi(get(name));
}

s32 Settings::getS32(const std::string &name) const
{
return stoi(get(name));
Expand Down
1 change: 1 addition & 0 deletions src/settings.h
Expand Up @@ -134,6 +134,7 @@ class Settings {
bool getBool(const std::string &name) const;
u16 getU16(const std::string &name) const;
s16 getS16(const std::string &name) const;
u32 getU32(const std::string &name) const;
s32 getS32(const std::string &name) const;
u64 getU64(const std::string &name) const;
float getFloat(const std::string &name) const;
Expand Down

0 comments on commit 79f19b8

Please sign in to comment.