Text   |  XML   |  ReML   |   Visible Warnings:

Ignored Return Value  at packet-mbtcp.c:267

No properties have been set. | edit properties
Jump to warning location ↓ warning details...
Show Events | Options

dissect_mbtcp

(/home/sate/Testcases/c/cve/wireshark-1.2.0/epan/dissectors/packet-mbtcp.c)expand/collapse
Show more  
 203  dissect_mbtcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 204  {
 205  /* Set up structures needed to add the protocol subtree and manage it */
 206          mbtcp_hdr       mh;
 207          proto_item      *mi, *mf;
 208          proto_tree      *mbtcp_tree, *modbus_tree, *group_tree;
 209          int             offset, group_offset, packet_type;
 210          guint           i;
 211          gint            packet_len, payload_start, payload_len;
 212          const char      *func_string = "";
 213          const char      *pkt_type_str = "";
 214          const char      *err_str = "";
 215          guint32         byte_cnt, group_byte_cnt, group_word_cnt;
 216          guint32         packet_num;     /* num to uniquely identify different mbtcp
 217                                           * packets in one packet */
 218          guint8          exception_code;
 219          gboolean        exception_returned;
 220          guint8          fc;
 221   
 222          mh.transaction_id = tvb_get_ntohs(tvb, 0);
 223          mh.protocol_id = tvb_get_ntohs(tvb, 2);
 224          mh.len = tvb_get_ntohs(tvb, 4);
 225          mh.mdbs_hdr.unit_id = tvb_get_guint8(tvb, 6);
 226          mh.mdbs_hdr.function_code = tvb_get_guint8(tvb, 7);
 227   
 228   
 229          /* check that it actually looks like Modbus/TCP */
 230          /* protocol id == 0 */
 231          if( mh.protocol_id != 0 ){
 232                  return 0;
 233          }
 234          /* length is at least 2 (unit_id + function_code) */
 235          if( mh.len < 2 ){
 236                  return 0;
 237          }
 238          /* function code is in the set 1-24, 40, 125-127.
 239           * Note that function code is only 7 bits.
 240           */
 241          fc=mh.mdbs_hdr.function_code&0x7f;
 242          if(!match_strval(fc, function_code_vals))
 243                  return 0;
 244   
 245   
 246          /* Make entries in Protocol column on summary display */
 247          if (check_col(pinfo->cinfo, COL_PROTOCOL))
 248                  col_set_str(pinfo->cinfo, COL_PROTOCOL, "Modbus/TCP");
 249   
 250          if (check_col(pinfo->cinfo, COL_INFO))
 251                  col_clear(pinfo->cinfo, COL_INFO);
 252   
 253   
 254          /* Make entries in Info column on summary display */
 255          offset = 0;
 256   
 257          if ( mh.mdbs_hdr.function_code & 0x80 ) {
 258                  exception_code = tvb_get_guint8(tvb, offset + sizeof(mbtcp_hdr));
 259                  mh.mdbs_hdr.function_code ^= 0x80;
 260                  exception_returned = TRUE;
 261          }
 262          else {
 263                  exception_code = 0;
 264                  exception_returned = FALSE;
 265          }
 266          func_string = val_to_str(mh.mdbs_hdr.function_code, function_code_vals,
 267              "Unknown function (%u)");
 268          if (check_col(pinfo->cinfo, COL_INFO))
 269          {
 270                  packet_type = classify_packet(pinfo);
 271                  switch ( packet_type ) {
 272                          case QUERY_PACKET :             pkt_type_str="query";
 273                                                                                                  break;
 274                          case RESPONSE_PACKET :  pkt_type_str="response";
 275                                                                                                  break;
 276                          case CANNOT_CLASSIFY :          err_str="Unable to classify as query or response.";
 277                                                                                  pkt_type_str="unknown";
 278                                                                                                  break;
 279                          default :
 280                                                                                                  break;
 281                  }
 282                  if ( exception_returned )
 283                          err_str="Exception returned ";
 284                  col_add_fstr(pinfo->cinfo, COL_INFO,
 285                                  "%8s [%2u pkt(s)]: trans: %5u; unit: %3u, func: %3u: %s. %s",
 286                                  pkt_type_str, 1, mh.transaction_id, (unsigned char) mh.mdbs_hdr.unit_id,
 287                                  (unsigned char) mh.mdbs_hdr.function_code, func_string, err_str);
 288          }
 289   
 290          /* build up protocol tree and iterate over multiple packets */
 291          packet_num = 0;
 292          while (1) {
 293                  packet_type = classify_packet(pinfo);
 294                  packet_len = sizeof(mbtcp_hdr) - sizeof(modbus_hdr) + mh.len;
 295   
 296                  /* if a tree exists, perform operations to add fields to it */
 297                  if (tree) {
 298                          mi = proto_tree_add_protocol_format(tree, proto_mbtcp, tvb, offset,
 299                                          packet_len, "Modbus/TCP");
 300                          mbtcp_tree = proto_item_add_subtree(mi, ett_mbtcp);
 301   
 302                          /* Add items to protocol tree specific to Modbus/TCP */
 303                          proto_tree_add_uint(mbtcp_tree, hf_mbtcp_transid, tvb, offset, 2,
 304                                          mh.transaction_id);
 305                          proto_tree_add_uint(mbtcp_tree, hf_mbtcp_protid, tvb, offset + 2, 2,
 306                                          mh.protocol_id);
 307                          proto_tree_add_uint(mbtcp_tree, hf_mbtcp_len, tvb, offset + 4, 2,
 308                                          mh.len);
 309                                           
 310                          proto_tree_add_uint(mbtcp_tree, hf_mbtcp_unitid, tvb, offset + 6, 1,
 311                                          mh.mdbs_hdr.unit_id);
 312   
 313   
 314                          /* Add items to protocol tree specific to Modbus generic */
 315                          mf = proto_tree_add_text(mbtcp_tree, tvb, offset + 7, mh.len - 1,
 316                                          "Modbus");
 317                          modbus_tree = proto_item_add_subtree(mf, ett_modbus_hdr);
 318                          mi = proto_tree_add_uint(modbus_tree, hf_mbtcp_functioncode, tvb, offset + 7, 1,
 319                                          mh.mdbs_hdr.function_code);
 320                                           
 321                          /** detail payload as a function of exception/function code */
 322                          func_string = val_to_str(mh.mdbs_hdr.function_code,
 323                              function_code_vals, "Unknown function");
 324                          payload_start = offset + 8;
 325                          payload_len = mh.len - sizeof(modbus_hdr);
 326                          if (exception_returned) {
 327                                  proto_item_set_text(mi, "function %u:  %s.  Exception: %s",
 328                                                  mh.mdbs_hdr.function_code,
 329                                                  func_string,
 330                                                  val_to_str(exception_code,
 331                                                      exception_code_vals,
 332                                                      "Unknown exception code (%u)"));
 333                                  proto_tree_add_uint(modbus_tree, hf_modbus_exceptioncode, tvb, payload_start, 1,
 334                                                  exception_code);
 335                          }
 336                          else {
 337                                  proto_item_set_text(mi, "function %u:  %s", mh.mdbs_hdr.function_code,
 338                                                  func_string);
 339                                  switch (mh.mdbs_hdr.function_code) {
 340                                           
 341                                          case READ_COILS:                        
 342                                          case READ_INPUT_DISCRETES:      
 343                                                  if (packet_type == QUERY_PACKET) {
 344                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 345                                                          proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, FALSE);
 346                                                  }
 347                                                  else if (packet_type == RESPONSE_PACKET) {
 348                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 349                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
 350                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 1, byte_cnt, "Data");
 351                                                  }
 352                                                  break;
 353                                                   
 354                                          case READ_MULT_REGS:            
 355                                          case READ_INPUT_REGS:            
 356                                                  if (packet_type == QUERY_PACKET) {
 357                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 358                                                          proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, FALSE);
 359                                                  }
 360                                                  else if (packet_type == RESPONSE_PACKET) {
 361                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 362                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
 363                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 1, byte_cnt, "Data");
 364                                                  }
 365                                                  break;
 366                                                   
 367                                          case WRITE_COIL:                        
 368                                                  if (packet_type == QUERY_PACKET) {
 369                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 370                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 2, 1, "Data");
 371                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
 372                                                  }
 373                                                  else if (packet_type == RESPONSE_PACKET) {
 374                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 375                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 2, 1, "Data");
 376                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
 377                                                  }
 378                                                  break;
 379                                                   
 380                                          case WRITE_SINGLE_REG:          
 381                                                  if (packet_type == QUERY_PACKET) {
 382                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 383                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 2, 2, "Data");
 384                                                  }
 385                                                  else if (packet_type == RESPONSE_PACKET) {
 386                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 387                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 2, 2, "Data");
 388                                                  }
 389                                                  break;
 390                                                   
 391                                          case READ_EXCEPT_STAT:          
 392                                                  if (packet_type == RESPONSE_PACKET)
 393                                                          proto_tree_add_text(modbus_tree, tvb, payload_start, 1, "Data");
 394                                                  break;
 395                                                   
 396                                          case FORCE_MULT_COILS:          
 397                                                  if (packet_type == QUERY_PACKET) {
 398                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 399                                                          proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, FALSE);
 400                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
 401                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
 402                                                                          byte_cnt);
 403                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 5, byte_cnt, "Data");
 404                                                  }
 405                                                  else if (packet_type == RESPONSE_PACKET) {
 406                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 407                                                          proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, FALSE);
 408                                                  }
 409                                                  break;
 410                                                   
 411                                          case WRITE_MULT_REGS:            
 412                                                  if (packet_type == QUERY_PACKET) {
 413                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 414                                                          proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, FALSE);
 415                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
 416                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
 417                                                                          byte_cnt);
 418                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 5, byte_cnt, "Data");
 419                                                  }
 420                                                  else if (packet_type == RESPONSE_PACKET) {
 421                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 422                                                          proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, FALSE);
 423                                                  }
 424                                                  break;
 425                                                   
 426                                          case READ_GENL_REF:                      
 427                                                  if (packet_type == QUERY_PACKET) {
 428                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 429                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
 430                                                                          byte_cnt);
 431                                                                           
 432                                                          /* add subtrees to describe each group of packet */
 433                                                          group_offset = payload_start + 1;
 434                                                          for (i = 0; i < byte_cnt / 7; i++) {
 435                                                                  mi = proto_tree_add_text( modbus_tree, tvb, group_offset, 7,
 436                                                                                  "Group %u", i);
 437                                                                  group_tree = proto_item_add_subtree(mi, ett_group_hdr);
 438                                                                  proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, FALSE);
 439                                                                  proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, FALSE);
 440                                                                  proto_tree_add_item(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2, FALSE);
 441                                                                  group_offset += 7;
 442                                                          }
 443                                                  }
 444                                                  else if (packet_type == RESPONSE_PACKET) {
 445                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 446                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
 447                                                                          byte_cnt);
 448                                                                           
 449                                                          /* add subtrees to describe each group of packet */
 450                                                          group_offset = payload_start + 1;
 451                                                          i = 0;
 452                                                          while (byte_cnt > 0) {
 453                                                                  group_byte_cnt = (guint32)tvb_get_guint8(tvb, group_offset);
 454                                                                  mi = proto_tree_add_text( modbus_tree, tvb, group_offset, group_byte_cnt + 1,
 455                                                                                  "Group %u", i);
 456                                                                  group_tree = proto_item_add_subtree(mi, ett_group_hdr);
 457                                                                  proto_tree_add_uint(group_tree, hf_modbus_bytecnt, tvb, group_offset, 1,
 458                                                                                  group_byte_cnt);
 459                                                                  proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset + 1, 1, FALSE);
 460                                                                  proto_tree_add_text(group_tree, tvb, group_offset + 2, group_byte_cnt - 1, "Data");
 461                                                                  group_offset += (group_byte_cnt + 1);
 462                                                                  byte_cnt -= (group_byte_cnt + 1);
 463                                                                  i++;
 464                                                          }
 465                                                  }
 466                                                  break;
 467                                                   
 468                                          case WRITE_GENL_REF:            
 469                                                  if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
 470                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 471                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
 472                                                                          byte_cnt);
 473                                                                           
 474                                                          /* add subtrees to describe each group of packet */
 475                                                          group_offset = payload_start + 1;
 476                                                          i = 0;
 477                                                          while (byte_cnt > 0) {
 478                                                                  group_word_cnt = tvb_get_ntohs(tvb, group_offset + 5);
 479                                                                  group_byte_cnt = (2 * group_word_cnt) + 7;
 480                                                                  mi = proto_tree_add_text( modbus_tree, tvb, group_offset,  
 481                                                                                  group_byte_cnt, "Group %u", i);
 482                                                                  group_tree = proto_item_add_subtree(mi, ett_group_hdr);
 483                                                                  proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, FALSE);
 484                                                                  proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, FALSE);
 485                                                                  proto_tree_add_uint(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2,  
 486                                                                                  group_word_cnt);
 487                                                                  proto_tree_add_text(group_tree, tvb, group_offset + 7, group_byte_cnt - 7, "Data");
 488                                                                  group_offset += group_byte_cnt;
 489                                                                  byte_cnt -= group_byte_cnt;
 490                                                                  i++;
 491                                                          }
 492                                                  }
 493                                                  break;
 494                                                   
 495                                          case MASK_WRITE_REG:            
 496                                                  if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
 497                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 498                                                          proto_tree_add_item(modbus_tree, hf_modbus_andmask, tvb, payload_start + 2, 2, FALSE);
 499                                                          proto_tree_add_item(modbus_tree, hf_modbus_ormask, tvb, payload_start + 4, 2, FALSE);
 500                                                  }
 501                                                  break;
 502                                                   
 503                                          case READ_WRITE_REG:            
 504                                                  if (packet_type == QUERY_PACKET) {
 505                                                          proto_tree_add_item(modbus_tree, hf_modbus_readref, tvb, payload_start, 2, FALSE);
 506                                                          proto_tree_add_item(modbus_tree, hf_modbus_readwordcnt, tvb, payload_start + 2, 2, FALSE);
 507                                                          proto_tree_add_item(modbus_tree, hf_modbus_writeref, tvb, payload_start + 4, 2, FALSE);
 508                                                          proto_tree_add_item(modbus_tree, hf_modbus_writewordcnt, tvb, payload_start + 6, 2, FALSE);
 509                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 8);
 510                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 8, 1,
 511                                                                          byte_cnt);
 512                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 9, byte_cnt, "Data");
 513                                                  }
 514                                                  else if (packet_type == RESPONSE_PACKET) {
 515                                                          byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
 516                                                          proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
 517                                                                          byte_cnt);
 518                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 1, byte_cnt, "Data");
 519                                                  }
 520                                                  break;
 521                                                   
 522                                          case READ_FIFO_QUEUE:            
 523                                                  if (packet_type == QUERY_PACKET)
 524                                                          proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, FALSE);
 525                                                  else if (packet_type == RESPONSE_PACKET) {
 526                                                          byte_cnt = (guint32)tvb_get_ntohs(tvb, payload_start);
 527                                                          proto_tree_add_uint(modbus_tree, hf_modbus_lbytecnt, tvb, payload_start, 2,
 528                                                                          byte_cnt);
 529                                                          proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, FALSE);
 530                                                          proto_tree_add_text(modbus_tree, tvb, payload_start + 4, byte_cnt - 2, "Data");
 531                                                  }
 532                                                  break;
 533                                                   
 534                                          case DIAGNOSTICS:                        
 535                                          case PROGRAM_484:                        
 536                                          case POLL_484:                          
 537                                          case GET_COMM_EVENT_CTRS:        
 538                                          case GET_COMM_EVENT_LOG:        
 539                                          case PROGRAM_584_984:            
 540                                          case POLL_584_984:                      
 541                                          case REPORT_SLAVE_ID:            
 542                                          case PROGRAM_884_U84:            
 543                                          case RESET_COMM_LINK:            
 544                                          case PROGRAM_CONCEPT:            
 545                                          case FIRMWARE_REPLACE:          
 546                                          case PROGRAM_584_984_2:          
 547                                          case REPORT_LOCAL_ADDR_MB:      
 548                                                  /* these function codes are not part of the Modbus/TCP specification */
 549                                          default:                                        
 550                                                  if (payload_len > 0)
 551                                                          proto_tree_add_text(modbus_tree, tvb, payload_start, payload_len, "Data");
 552                                                  break;
 553                                  }
 554                          }
 555                  }
 556                   
 557                  /* move onto next packet (if there) */
 558                  offset += packet_len;
 559                  packet_num++;
 560                  if (tvb_reported_length_remaining(tvb, offset) > 0) {
 561                           
 562                          /* load header structure for next packet */
 563                          mh.transaction_id = tvb_get_ntohs(tvb, offset+0);
 564                          mh.protocol_id = tvb_get_ntohs(tvb, offset+2);
 565                          mh.len = tvb_get_ntohs(tvb, offset+4);
 566                          mh.mdbs_hdr.unit_id = tvb_get_guint8(tvb, offset+6);
 567                          mh.mdbs_hdr.function_code = tvb_get_guint8(tvb, offset+7);
 568   
 569           
 570                          if ( mh.mdbs_hdr.function_code & 0x80 ) {
 571                                  exception_code = tvb_get_guint8(tvb, offset + sizeof(mbtcp_hdr));
 572                                  mh.mdbs_hdr.function_code ^= 0x80;
 573                                  exception_returned = TRUE;
 574                          } else 
 575                                  exception_returned = FALSE;
 576                  }
 577                  else 
 578                          break;
 579          }
 580   
 581          return tvb_length(tvb);
 582  }
Show more  




Change Warning 5481.35747 : Ignored Return Value

Priority:
State:
Finding:
Owner:
Note: