Include <string.h> to get "strcmp()" declared.
[obnox/wireshark/wip.git] / packet-tftp.c
index 0a39e99bb78e4b3f349638acf3e781b1ada25f8a..493df31a96e2d1b04000f2c527cf372dbae1ff13 100644 (file)
@@ -5,24 +5,24 @@
  * Craig Newell <CraigN@cheque.uq.edu.au>
  *     RFC2347 TFTP Option Extension
  *
- * $Id: packet-tftp.c,v 1.26 2001/06/18 02:17:53 guy Exp $
+ * $Id: packet-tftp.c,v 1.40 2002/08/28 21:00:36 jmayer Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
  * Copied from packet-bootp.c
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif
-
 #include <glib.h>
-#include "packet.h"
-#include "conversation.h"
+#include <epan/packet.h>
+#include <epan/conversation.h>
 
 static int proto_tftp = -1;
 static int hf_tftp_opcode = -1;
@@ -55,6 +47,8 @@ static int hf_tftp_error_string = -1;
 
 static gint ett_tftp = -1;
 
+static dissector_handle_t tftp_handle;
+
 #define UDP_PORT_TFTP    69
 
 #define        TFTP_RRQ        1
@@ -83,6 +77,7 @@ static const value_string tftp_error_code_vals[] = {
   { 5, "Unknown transfer ID" },
   { 6, "File already exists" },
   { 7, "No such user" },
+  { 8, "Option negotiation failed" },
   { 0, NULL }
 };
 
@@ -91,16 +86,15 @@ static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
 static void
 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-       proto_tree      *tftp_tree;
+       proto_tree      *tftp_tree = NULL;
        proto_item      *ti;
        conversation_t  *conversation;
        gint            offset = 0;
        guint16         opcode;
-       u_int           i1;
-
-       CHECK_DISPLAY_AS_DATA(proto_tftp, tvb, pinfo, tree);
-
-       pinfo->current_proto = "TFTP";
+       guint16         bytes;
+       guint16         blocknum;
+       guint           i1;
+       guint16         error;
 
        /*
         * The first TFTP packet goes to the TFTP port; the second one
@@ -125,90 +119,157 @@ dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                                           pinfo->srcport, 0, NO_PORT_B);
          if (conversation == NULL) {
            conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
-                                           pinfo->srcport, 0, NULL,
-                                           NO_PORT2);
-           conversation_set_dissector(conversation, dissect_tftp);
+                                           pinfo->srcport, 0, NO_PORT2);
+           conversation_set_dissector(conversation, tftp_handle);
          }
        }
 
-       if (check_col(pinfo->fd, COL_PROTOCOL))
-               col_set_str(pinfo->fd, COL_PROTOCOL, "TFTP");
+       if (check_col(pinfo->cinfo, COL_PROTOCOL))
+               col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");
 
        opcode = tvb_get_ntohs(tvb, offset);
 
-       if (check_col(pinfo->fd, COL_INFO)) {
+       if (check_col(pinfo->cinfo, COL_INFO)) {
 
-         col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
+         col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
            val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
 
        }
 
        if (tree) {
 
-         ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
-                           tvb_length_remaining(tvb, offset), FALSE);
+         ti = proto_tree_add_item(tree, proto_tftp, tvb, offset, -1, FALSE);
          tftp_tree = proto_item_add_subtree(ti, ett_tftp);
 
          proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
                            offset, 2, opcode);
-         offset += 2;
-           
-         switch (opcode) {
-         case TFTP_RRQ:
-           i1 = tvb_strsize(tvb, offset);
+       }
+       offset += 2;
+
+       switch (opcode) {
+
+       case TFTP_RRQ:
+         i1 = tvb_strsize(tvb, offset);
+         if (tree) {
            proto_tree_add_item(tftp_tree, hf_tftp_source_file,
                            tvb, offset, i1, FALSE);
-           offset += i1;
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
+                           tvb_get_ptr(tvb, offset, i1));
+         }
+         offset += i1;
 
-           i1 = tvb_strsize(tvb, offset);
+         i1 = tvb_strsize(tvb, offset);
+         if (tree) {
            ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
                            tvb, offset, i1, FALSE);
-           offset += i1;
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
+                           tvb_get_ptr(tvb, offset, i1));
+         }
+         offset += i1;
 
+         if (tree)
            tftp_dissect_options(tvb, offset, tftp_tree);
-           break;
-         case TFTP_WRQ:
-           i1 = tvb_strsize(tvb, offset);
+         break;
+
+       case TFTP_WRQ:
+         i1 = tvb_strsize(tvb, offset);
+         if (tree) {
            proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
                            tvb, offset, i1, FALSE);
-           offset += i1;
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
+                           tvb_get_ptr(tvb, offset, i1));
+         }
+         offset += i1;
 
-           i1 = tvb_strsize(tvb, offset);
+         i1 = tvb_strsize(tvb, offset);
+         if (tree) {
            ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
                            tvb, offset, i1, FALSE);
-           offset += i1;
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
+                           tvb_get_ptr(tvb, offset, i1));
+         }
+         offset += i1;
 
+         if (tree)
            tftp_dissect_options(tvb, offset, tftp_tree);
-           break;
-         case TFTP_DATA:
-           proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
-                           FALSE);
-           offset += 2;
-
-           proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
-               "Data (%d bytes)", tvb_length_remaining(tvb, offset));
-           break;
-         case TFTP_ACK:
-           proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
-                           FALSE);
-           break;
-         case TFTP_ERROR:
-           proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
-                           FALSE);
-           offset += 2;
-
-           i1 = tvb_strsize(tvb, offset);
+         break;
+
+       case TFTP_DATA:
+         blocknum = tvb_get_ntohs(tvb, offset);
+         if (tree) {
+           proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
+                           blocknum);
+         }
+         offset += 2;
+
+         bytes = tvb_reported_length_remaining(tvb, offset);
+
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i%s",
+                   blocknum,
+                   (bytes < 512)?" (last)":"" );
+         }
+
+         if (tree) {
+           proto_tree_add_text(tftp_tree, tvb, offset, -1,
+               "Data (%d bytes)", bytes);
+         }
+         break;
+
+       case TFTP_ACK:
+         blocknum = tvb_get_ntohs(tvb, offset);
+         if (tree) {
+           proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
+                           blocknum);
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
+                           blocknum);
+         }
+         break;
+
+       case TFTP_ERROR:
+         error = tvb_get_ntohs(tvb, offset);
+         if (tree) {
+           proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
+                           error);
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
+                           val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));
+         }
+         offset += 2;
+
+         i1 = tvb_strsize(tvb, offset);
+         if (tree) {
            proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
                i1, FALSE);
-           break;
-         case TFTP_OACK:
+         }
+         if (check_col(pinfo->cinfo, COL_INFO)) {
+           col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
+                           tvb_get_ptr(tvb, offset, i1));
+         }
+         break;
+
+       case TFTP_OACK:
+         if (tree)
            tftp_dissect_options(tvb, offset, tftp_tree);
-           break;
-         default:
-           proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
-               "Data (%d bytes)", tvb_length_remaining(tvb, offset));
-           break;
+         break;
+
+       default:
+         if (tree) {
+           proto_tree_add_text(tftp_tree, tvb, offset, -1,
+               "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
          }
+         break;
 
        }
 }
@@ -278,10 +339,12 @@ proto_register_tftp(void)
                                       "TFTP", "tftp");
   proto_register_field_array(proto_tftp, hf, array_length(hf));
   proto_register_subtree_array(ett, array_length(ett));
+
+  tftp_handle = create_dissector_handle(dissect_tftp, proto_tftp);
 }
 
 void
 proto_reg_handoff_tftp(void)
 {
-  dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp, proto_tftp);
+  dissector_add("udp.port", UDP_PORT_TFTP, tftp_handle);
 }