From 11d3224142c0531879fb8e415daf9639a4eace66 Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Wed, 5 Oct 2016 17:01:57 +0200 Subject: s7comm: use ws_stroi function. Change-Id: I0d27c2aa7d896c4c6048dff963910ad7f88b07ee Reviewed-on: https://code.wireshark.org/review/18085 Petri-Dish: Dario Lombardo Tested-by: Petri Dish Buildbot Reviewed-by: Michael Mann --- epan/dissectors/packet-s7comm.c | 81 ++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 18 deletions(-) (limited to 'epan/dissectors/packet-s7comm.c') diff --git a/epan/dissectors/packet-s7comm.c b/epan/dissectors/packet-s7comm.c index 5b1dccbe17..7757c3b803 100644 --- a/epan/dissectors/packet-s7comm.c +++ b/epan/dissectors/packet-s7comm.c @@ -26,6 +26,8 @@ #include #include +#include +#include #include "packet-s7comm.h" #include "packet-s7comm_szl_ids.h" @@ -1180,6 +1182,9 @@ static const int *s7comm_diagdata_registerflag_fields[] = { NULL }; +static expert_field ei_s7comm_data_blockcontrol_block_num_invalid = EI_INIT; +static expert_field ei_s7comm_ud_blockinfo_block_num_ascii_invalid = EI_INIT; + /* PI service name IDs. Index represents the index in pi_service_names */ typedef enum { @@ -2807,6 +2812,9 @@ s7comm_decode_pi_service(tvbuff_t *tvb, proto_tree *param_tree = NULL; proto_tree *file_tree = NULL; + gint32 num = -1; + gboolean num_valid; + startoffset = offset; /* The first byte is checked and inserted to tree outside, so skip it here */ @@ -2864,12 +2872,19 @@ s7comm_decode_pi_service(tvbuff_t *tvb, paramoffset += 2; proto_tree_add_item_ret_string(file_tree, hf_s7comm_data_blockcontrol_block_num, tvb, paramoffset, 5, ENC_ASCII|ENC_NA, wmem_packet_scope(), &str); paramoffset += 5; - proto_item_append_text(file_tree, " [%s %d]", - val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(str)); - col_append_fstr(pinfo->cinfo, COL_INFO, "%s%d", - val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(str)); + num_valid = ws_strtoi32(str, NULL, &num); + proto_item_append_text(file_tree, " [%s ", + val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x")); + col_append_fstr(pinfo->cinfo, COL_INFO, "%s", + val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x")); + if (num_valid) { + proto_item_append_text(file_tree, "%d]", num); + col_append_fstr(pinfo->cinfo, COL_INFO, "%d", num); + } else { + expert_add_info(pinfo, file_tree, &ei_s7comm_data_blockcontrol_block_num_invalid); + proto_item_append_text(file_tree, "NaN]"); + col_append_fstr(pinfo->cinfo, COL_INFO, "NaN"); + } if (i+1 < count) { col_append_fstr(pinfo->cinfo, COL_INFO, ", "); } @@ -3206,6 +3221,8 @@ s7comm_decode_plc_controls_filename(tvbuff_t *tvb, if (len == 9) { blocktype = tvb_get_ntohs(tvb, offset + 1); if ((tvb_get_guint8(tvb, offset) == '_') && (blocktype >= S7COMM_BLOCKTYPE_OB) && (blocktype <= S7COMM_BLOCKTYPE_SFB)) { + gint32 num = 1; + gboolean num_valid; is_plcfilename = TRUE; file_tree = proto_item_add_subtree(item, ett_s7comm_plcfilename); itemadd = proto_tree_add_item(file_tree, hf_s7comm_data_blockcontrol_file_ident, tvb, offset, 1, ENC_ASCII|ENC_NA); @@ -3216,12 +3233,19 @@ s7comm_decode_plc_controls_filename(tvbuff_t *tvb, offset += 2; proto_tree_add_item_ret_string(file_tree, hf_s7comm_data_blockcontrol_block_num, tvb, offset, 5, ENC_ASCII|ENC_NA, wmem_packet_scope(), &str); offset += 5; - proto_item_append_text(file_tree, " [%s %d]", - val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(str)); - col_append_fstr(pinfo->cinfo, COL_INFO, " -> Block:[%s %d]", - val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(str)); + num_valid = ws_strtoi32(str, NULL, &num); + proto_item_append_text(file_tree, " [%s", + val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x")); + col_append_fstr(pinfo->cinfo, COL_INFO, " -> Block:[%s", + val_to_str(blocktype, blocktype_names, "Unknown Block type: 0x%04x")); + if (num_valid) { + proto_item_append_text(file_tree, "%d]", num); + col_append_fstr(pinfo->cinfo, COL_INFO, "%d]", num); + } else { + expert_add_info(pinfo, file_tree, &ei_s7comm_data_blockcontrol_block_num_invalid); + proto_item_append_text(file_tree, "NaN]"); + col_append_fstr(pinfo->cinfo, COL_INFO, "NaN]"); + } itemadd = proto_tree_add_item(file_tree, hf_s7comm_data_blockcontrol_dest_filesys, tvb, offset, 1, ENC_ASCII|ENC_NA); proto_item_append_text(itemadd, " (%s)", val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys: %c")); offset += 1; @@ -4357,18 +4381,27 @@ s7comm_decode_ud_block_subfunc(tvbuff_t *tvb, case S7COMM_UD_SUBF_BLOCK_BLOCKINFO: if (type == S7COMM_UD_TYPE_REQ) { /*** Request ***/ if (tsize != S7COMM_DATA_TRANSPORT_SIZE_NULL) { + gint32 num = -1; + gboolean num_valid; /* 8 Bytes of Data follow, 1./ 2. type, 3-7 blocknumber as ascii number */ blocktype16 = tvb_get_ntohs(tvb, offset); itemadd = proto_tree_add_item(data_tree, hf_s7comm_ud_blockinfo_block_type, tvb, offset, 2, ENC_ASCII|ENC_NA); proto_item_append_text(itemadd, " (%s)", val_to_str(blocktype16, blocktype_names, "Unknown Block type: 0x%04x")); offset += 2; proto_tree_add_item_ret_string(data_tree, hf_s7comm_ud_blockinfo_block_num_ascii, tvb, offset, 5, ENC_ASCII|ENC_NA, wmem_packet_scope(), &pBlocknumber); - proto_item_append_text(data_tree, " [%s %d]", - val_to_str(blocktype16, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(pBlocknumber)); - col_append_fstr(pinfo->cinfo, COL_INFO, " -> Block:[%s %d]", - val_to_str(blocktype16, blocktype_names, "Unknown Block type: 0x%04x"), - atoi(pBlocknumber)); + num_valid = ws_strtoi32(pBlocknumber, NULL, &num); + proto_item_append_text(data_tree, " [%s ", + val_to_str(blocktype16, blocktype_names, "Unknown Block type: 0x%04x")); + col_append_fstr(pinfo->cinfo, COL_INFO, " -> Block:[%s ", + val_to_str(blocktype16, blocktype_names, "Unknown Block type: 0x%04x")); + if (num_valid) { + proto_item_append_text(data_tree, "%d]", num); + col_append_fstr(pinfo->cinfo, COL_INFO, "%d]", num); + } else { + expert_add_info(pinfo, data_tree, &ei_s7comm_ud_blockinfo_block_num_ascii_invalid); + proto_item_append_text(data_tree, "NaN]"); + col_append_fstr(pinfo->cinfo, COL_INFO, "NaN]"); + } offset += 5; itemadd = proto_tree_add_item(data_tree, hf_s7comm_ud_blockinfo_filesys, tvb, offset, 1, ENC_ASCII|ENC_NA); proto_item_append_text(itemadd, " (%s)", val_to_str(tvb_get_guint8(tvb, offset), blocktype_attribute2_names, "Unknown filesys: %c")); @@ -5095,6 +5128,8 @@ dissect_s7comm(tvbuff_t *tvb, void proto_register_s7comm (void) { + expert_module_t* expert_s7comm; + /* format: * {&(field id), {name, abbrev, type, display, strings, bitmask, blurb, HFILL}}. */ @@ -6059,6 +6094,13 @@ proto_register_s7comm (void) NULL, HFILL }}, }; + static ei_register_info ei[] = { + { &ei_s7comm_data_blockcontrol_block_num_invalid, { "s7comm.data.blockcontrol.block_number.invalid", PI_MALFORMED, PI_ERROR, + "Block number must be a string containing an integer", EXPFILL }}, + { &ei_s7comm_ud_blockinfo_block_num_ascii_invalid, { "s7comm.data.blockinfo.block_number.invalid", PI_MALFORMED, PI_ERROR, + "Block info must be a string containing an integer", EXPFILL }} + }; + static gint *ett[] = { &ett_s7comm, &ett_s7comm_header, @@ -6094,6 +6136,9 @@ proto_register_s7comm (void) s7comm_register_szl_types(proto_s7comm); proto_register_subtree_array(ett, array_length (ett)); + + expert_s7comm = expert_register_protocol(proto_s7comm); + expert_register_field_array(expert_s7comm, ei, array_length(ei)); } /* Register this protocol */ -- cgit v1.2.3