<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40593 >

Attached patch reduces code duplication by moving the
check whether a connection is allowed to edit out of
every server edit handler and into server_packet_input()
before the handlers are called.


-----------------------------------------------------------------------
禁転載
 common/packets.def |   20 ++++----
 server/edithand.c  |  136 +--------------------------------------------------
 server/srv_main.c  |   24 ++++++++-
 version.in         |    2 +-
 4 files changed, 37 insertions(+), 145 deletions(-)

diff --git a/common/packets.def b/common/packets.def
index 6dec219..4f69671 100644
--- a/common/packets.def
+++ b/common/packets.def
@@ -1425,11 +1425,11 @@ PACKET_RULESET_RESOURCE=124;sc,lsend
   STRING graphic_alt[MAX_LEN_NAME];
 end
 
-/************** Editing hash packets **********************/
+/************** Client Editor Packets **********************/
 
-/* Always keep this as the first edit type packet,
- * so that the test in server/srv_main.c +1203
- * is easy to write. */
+/* Always keep this as the first edit type packet sent by
+ * the client, so that the test in server/srv_main.c in
+ * the function is_client_edit_packet() is easy to write. */
 PACKET_EDIT_MODE=150;cs,handle-per-conn,dsend
   BOOL state;
 end
@@ -1616,14 +1616,16 @@ PACKET_EDIT_PLAYER_VISION=171;cs,handle-per-conn,dsend
   UINT8 size;
 end
 
-PACKET_EDIT_GAME=172;cs,handle-per-conn,handle-via-packet
+/* Always keep this as the last edit type packet sent by
+ * the client, so that the test in server/srv_main.c in
+ * the function is_client_edit_packet() is easy to write. */
+PACKET_EDIT_GAME=180;cs,handle-per-conn,handle-via-packet
   YEAR year;
 end
 
-/* Always keep this as the last edit type packet,
- * so that the test in server/srv_main.c +1213
- * is easy to write. */
-PACKET_EDIT_OBJECT_CREATED=173;sc,dsend
+/************** Server Editor Packets **********************/
+
+PACKET_EDIT_OBJECT_CREATED=181;sc,dsend
   SINT32 tag;
   SINT32 id;
 end
diff --git a/server/edithand.c b/server/edithand.c
index 04ca03e..3656184 100644
--- a/server/edithand.c
+++ b/server/edithand.c
@@ -140,12 +140,14 @@ void handle_edit_mode(struct connection *pc, bool is_edit_mode)
   if (!can_conn_enable_editing(pc)) {
     return;
   }
+
   if (!game.info.is_edit_mode && is_edit_mode) {
     /* Someone could be cheating! Warn people. */
     notify_conn(NULL, NULL, E_SETTING,
                 _(" *** Server set to edit mode by %s! *** "),
                 conn_description(pc));
   }
+
   if (game.info.is_edit_mode && !is_edit_mode) {
     notify_conn(NULL, NULL, E_SETTING,
                 _(" *** Edit mode cancelled by %s. *** "),
@@ -153,6 +155,7 @@ void handle_edit_mode(struct connection *pc, bool is_edit_mode)
 
     check_leaving_edit_mode();
   }
+
   if (game.info.is_edit_mode != is_edit_mode) {
     game.info.is_edit_mode = is_edit_mode;
     send_game_info(NULL);
@@ -172,12 +175,6 @@ void handle_edit_tile_terrain(struct connection *pc, int x, int y,
   struct terrain *pterrain;
   struct tile *ptile_center;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -219,13 +216,6 @@ void handle_edit_tile_resource(struct connection *pc, int x, int y,
   struct resource *presource;
   struct tile *ptile_center;
   
-
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -258,12 +248,6 @@ void handle_edit_tile_special(struct connection *pc, int x, int y,
   struct tile *ptile_center;
   bool changed = FALSE;
   
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -308,12 +292,6 @@ void handle_edit_tile_base(struct connection *pc, int x, int y,
   struct base_type *pbase;
   bool changed = FALSE;
   
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -360,12 +338,6 @@ void handle_edit_tile(struct connection *pc,
   int id;
   bool changed = FALSE;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   id = packet->id;
   ptile = index_to_tile(id);
 
@@ -425,12 +397,6 @@ void handle_edit_unit_create(struct connection *pc,
   bool coastal;
   int id, i;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   owner = packet->owner;
   x = packet->x;
   y = packet->y;
@@ -518,12 +484,6 @@ void handle_edit_unit_remove(struct connection *pc, int owner,
   struct player *pplayer;
   int i;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile = map_pos_to_tile(x, y);
   if (!ptile) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -571,12 +531,6 @@ void handle_edit_unit_remove_by_id(struct connection *pc, Unit_type_id id)
 {
   struct unit *punit;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   punit = game_find_unit_by_number(id);
   if (!punit) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -601,12 +555,6 @@ void handle_edit_unit(struct connection *pc,
   bool changed = FALSE;
   int moves_left, fuel, hp;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   id = packet->id;
   punit = game_find_unit_by_number(id);
   if (!punit) {
@@ -680,12 +628,6 @@ void handle_edit_city_create(struct connection *pc, int owner, int x, int y,
   struct city *pcity;
   struct player *pplayer;
   
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile = map_pos_to_tile(x, y);
   if (!ptile) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -750,12 +692,6 @@ void handle_edit_city(struct connection *pc,
   int id;
   bool changed = FALSE;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   pcity = game_find_city_by_number(packet->id);
   if (!pcity) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -891,12 +827,6 @@ void handle_edit_player_create(struct connection *pc, int tag)
   struct player *pplayer;
   struct nation_type *pnation;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   if (player_count() >= player_slot_count()) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
                 _("No more players can be added because the maximum "
@@ -958,12 +888,6 @@ void handle_edit_player_remove(struct connection *pc, int id)
   struct player *pplayer;
   struct conn_list *conns;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   pplayer = valid_player_by_number(id);
   if (pplayer == NULL) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1002,12 +926,6 @@ void handle_edit_player(struct connection *pc,
   struct player_research *research;
   enum tech_state known;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   pplayer = valid_player_by_number(packet->id);
   if (!pplayer) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1140,12 +1058,6 @@ void handle_edit_player_vision(struct connection *pc, int plr_no,
   struct player *pplayer;
   struct tile *ptile_center;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1218,12 +1130,6 @@ void handle_edit_player_vision(struct connection *pc, int plr_no,
 ****************************************************************************/
 void handle_edit_recalculate_borders(struct connection *pc)
 {
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   map_calculate_borders();
 }
 
@@ -1234,12 +1140,6 @@ void handle_edit_city_remove(struct connection *pc, int id)
 {
   struct city *pcity;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   pcity = game_find_city_by_number(id);
   if (pcity == NULL) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1255,12 +1155,6 @@ void handle_edit_city_remove(struct connection *pc, int id)
 ****************************************************************************/
 void handle_edit_check_tiles(struct connection *pc)
 {
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   check_edited_tile_terrains();
 }
 
@@ -1273,12 +1167,6 @@ void handle_edit_toggle_fogofwar(struct connection *pc, int plr_no)
 {
   struct player *pplayer;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   if (!game.info.fogofwar) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
                 _("Cannot toggle fog-of-war when it is already "
@@ -1314,12 +1202,6 @@ void handle_edit_territory(struct connection *pc, int x, int y, int owner,
   struct player *pplayer;
   struct tile *ptile_center;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile_center = map_pos_to_tile(x, y);
   if (!ptile_center) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1354,12 +1236,6 @@ void handle_edit_startpos(struct connection *pc, int x, int y,
   struct tile *ptile;
   const struct nation_type *pnation, *old;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   ptile = map_pos_to_tile(x, y);
   if (!ptile) {
     notify_conn(pc->self, NULL, E_BAD_COMMAND,
@@ -1386,12 +1262,6 @@ void handle_edit_game(struct connection *pc,
 {
   bool changed = FALSE;
 
-  if (!can_conn_edit(pc)) {
-    notify_conn(pc->self, NULL, E_BAD_COMMAND,
-                _("You are not allowed to edit."));
-    return;
-  }
-
   if (packet->year != game.info.year) {
 
     /* 'year' is stored in a signed short. */
diff --git a/server/srv_main.c b/server/srv_main.c
index 54a9dbc..3ec018c 100644
--- a/server/srv_main.c
+++ b/server/srv_main.c
@@ -1140,6 +1140,17 @@ int identity_number(void)
 }
 
 /**************************************************************************
+  Returns TRUE if the packet type is an edit packet sent by the client.
+
+  NB: The first and last client edit packets here must match those
+  defined in common/packets.def.
+**************************************************************************/
+static bool is_client_edit_packet(int type)
+{
+  return PACKET_EDIT_MODE <= type && type <= PACKET_EDIT_GAME;
+}
+
+/**************************************************************************
 Returns 0 if connection should be closed (because the clients was
 rejected). Returns 1 else.
 **************************************************************************/
@@ -1219,8 +1230,17 @@ bool server_packet_input(struct connection *pconn, void *packet, int type)
       || type == PACKET_SINGLE_WANT_HACK_REQ
       || type == PACKET_NATION_SELECT_REQ
       || type == PACKET_REPORT_REQ
-      || (PACKET_EDIT_MODE <= type
-          && type < PACKET_EDIT_OBJECT_CREATED)) {
+      || is_client_edit_packet(type)) {
+
+    /* Except for PACKET_EDIT_MODE (used to set edit mode), check
+     * that the client is allowed to send the given edit packet. */
+    if (is_client_edit_packet(type) && type != PACKET_EDIT_MODE
+        && !can_conn_edit(pconn)) {
+      notify_conn(pconn->self, NULL, E_BAD_COMMAND,
+                  _("You are not allowed to edit."));
+      return TRUE;
+    }
+
     if (!server_handle_packet(type, packet, NULL, pconn)) {
       freelog(LOG_ERROR, "Received unknown packet %d from %s",
 	      type, conn_description(pconn));
diff --git a/version.in b/version.in
index e6a4ee4..69f3f06 100644
--- a/version.in
+++ b/version.in
@@ -23,5 +23,5 @@ RELEASE_MONTH=3
 #   - Avoid adding a new mandatory capability to the development branch for
 #     as long as possible.  We want to maintain network compatibility with
 #     the stable branch for as long as possible.
-NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel.2008.Dec.04"
+NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel.2008.Dec.09"
 NETWORK_CAPSTRING_OPTIONAL=""
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to