Make the message popped up if you try to do a capture on a Win32 machine
[obnox/wireshark/wip.git] / packet-cdp.c
index 2b55293d5777d6f1bb83312eae52fa8811a9c929..6e516566bc4dd97dafdd0c29373eaba9e855c270 100644 (file)
@@ -2,13 +2,12 @@
  * Routines for the disassembly of the "Cisco Discovery Protocol"
  * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
  *
- * $Id: packet-cdp.c,v 1.28 2000/12/28 09:49:09 guy Exp $
+ * $Id: packet-cdp.c,v 1.39 2001/08/28 08:28:14 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  * 
- * 
  * 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
@@ -76,15 +75,24 @@ add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start,
 #define TYPE_CAPABILITIES      0x0004
 #define TYPE_IOS_VERSION       0x0005
 #define TYPE_PLATFORM          0x0006
+#define TYPE_IP_PREFIX         0x0007
+
+#define TYPE_VTP_MGMT_DOMAIN    0x0009 /* Guessed, from tcpdump */
+#define TYPE_NATIVE_VLAN        0x000a /* Guessed, from tcpdump */
+#define TYPE_DUPLEX             0x000b /* Guessed, from tcpdump */
 
 static const value_string type_vals[] = {
-       { TYPE_DEVICE_ID,    "Device ID" },
-       { TYPE_ADDRESS,      "Addresses" },
-       { TYPE_PORT_ID,      "Port ID" },
-       { TYPE_CAPABILITIES, "Capabilities" },
-       { TYPE_IOS_VERSION,  "Software version" },
-       { TYPE_PLATFORM,     "Platform" },
-       { 0,                 NULL },
+       { TYPE_DEVICE_ID,       "Device ID" },
+       { TYPE_ADDRESS,         "Addresses" },
+       { TYPE_PORT_ID,         "Port ID" },
+       { TYPE_CAPABILITIES,    "Capabilities" },
+       { TYPE_IOS_VERSION,     "Software version" },
+       { TYPE_PLATFORM,        "Platform" },
+       { TYPE_IP_PREFIX,       "IP Prefix (used for ODR)" },
+       { TYPE_VTP_MGMT_DOMAIN, "VTP Management Domain" },
+       { TYPE_NATIVE_VLAN,     "Native VLAN" },
+       { TYPE_DUPLEX,          "Duplex" },
+       { 0,                    NULL },
 };
        
 static void 
@@ -101,10 +109,6 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     guint32 naddresses;
     int addr_length;
 
-    CHECK_DISPLAY_AS_DATA(proto_cdp, tvb, pinfo, tree);
-
-    pinfo->current_proto = "CDP";
-
     if (check_col(pinfo->fd, COL_PROTOCOL))
         col_set_str(pinfo->fd, COL_PROTOCOL, "CDP");
     if (check_col(pinfo->fd, COL_INFO))
@@ -129,6 +133,18 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
        while (tvb_reported_length_remaining(tvb, offset) != 0) {
            type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
            length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
+           if (length < 4) {
+               tlvi = proto_tree_add_text(cdp_tree, tvb, offset, 4,
+                   "TLV with invalid length %u (< 4)",
+                   length);
+               tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
+                           offset + TLV_TYPE, 2, type);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
+                           offset + TLV_LENGTH, 2, length);
+               offset += 4;
+               break;
+           }
 
            switch (type) {
 
@@ -251,7 +267,76 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                            tvb_get_ptr(tvb, offset + 4, length - 4));
                offset += length;
                break;
+            case TYPE_IP_PREFIX:
+               tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
+                           length, "IP Prefixes: %d",length/5);
 
+                           /* the actual number of prefixes is (length-4)/5
+                           but if the variable is not a "float" but "integer"
+                           then length/5=(length-4)/5  :)  */
+                           
+               tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
+                           offset + TLV_TYPE, 2, type);  
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
+                           offset + TLV_LENGTH, 2, length);
+               offset += 4;
+               length -= 4;
+               while (length > 0) {
+                       proto_tree_add_text(tlv_tree, tvb, offset, 5,
+                               "IP Prefix = %s/%u",
+                               ip_to_str(tvb_get_ptr(tvb, offset, 4)),
+                               tvb_get_guint8(tvb,offset+4));
+                       offset += 5;
+                       length -= 5;
+               }
+               break;
+           case TYPE_VTP_MGMT_DOMAIN:
+               tlvi = proto_tree_add_text(cdp_tree, tvb,
+                           offset, length, "VTP Management Domain: %.*s",
+                           length - 4,
+                           tvb_get_ptr(tvb, offset + 4, length - 4));
+               tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
+                           offset + TLV_TYPE, 2, type);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
+                           offset + TLV_LENGTH, 2, length);
+               proto_tree_add_text(tlv_tree, tvb, offset + 4,
+                           length - 4, "VTP Management Domain: %.*s",
+                           length - 4,
+                           tvb_get_ptr(tvb, offset + 4, length - 4));
+               offset += length;
+               break;
+           case TYPE_NATIVE_VLAN:
+               tlvi = proto_tree_add_text(cdp_tree, tvb,
+                           offset, length, "Native VLAN: %u",
+                                          tvb_get_ntohs(tvb, offset + 4));
+               tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
+                           offset + TLV_TYPE, 2, type);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
+                           offset + TLV_LENGTH, 2, length);
+               proto_tree_add_text(tlv_tree, tvb, offset + 4,
+                           length - 4, "Native VLAN: %u",
+                                   tvb_get_ntohs(tvb, offset + 4));
+               offset += length;
+               break;
+           case TYPE_DUPLEX:
+               tlvi = proto_tree_add_text(cdp_tree, tvb,
+                           offset, length, "Duplex: %s",
+                                          tvb_get_guint8(tvb, offset + 4) ?
+                                          "Full" : "Half" );
+               tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
+                           offset + TLV_TYPE, 2, type);
+               proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
+                           offset + TLV_LENGTH, 2, length);
+               proto_tree_add_text(tlv_tree, tvb, offset + 4,
+                           length - 4, "Duplex: %s",
+                                   tvb_get_guint8(tvb, offset + 4) ?
+                                   "Full" : "Half" );
+               offset += length;
+               break;
            default:
                tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
                            length, "Type: %s, length: %u",
@@ -298,7 +383,7 @@ dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
 
     if (length < 1)
         return -1;
-    ti = proto_tree_add_notext(tree, tvb, offset, length);
+    ti = proto_tree_add_text(tree, tvb, offset, length, "Truncated address");
     address_tree = proto_item_add_subtree(ti, ett_cdp_address);
     protocol_type = tvb_get_guint8(tvb, offset);
     proto_tree_add_text(address_tree, tvb, offset, 1, "Protocol type: %s",
@@ -306,10 +391,8 @@ dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
     offset += 1;
     length -= 1;
 
-    if (length < 1) {
-        proto_item_set_text(ti, "Truncated address");
+    if (length < 1)
        return -1;
-    }
     protocol_length = tvb_get_guint8(tvb, offset);
     proto_tree_add_text(address_tree, tvb, offset, 1, "Protocol length: %u",
                        protocol_length);
@@ -317,7 +400,6 @@ dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
     length -= 1;
 
     if (length < protocol_length) {
-        proto_item_set_text(ti, "Truncated address");
         if (length != 0) {
             proto_tree_add_text(address_tree, tvb, offset, length,
               "Protocol: %s (truncated)",
@@ -338,10 +420,8 @@ dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
     offset += protocol_length;
     length -= protocol_length;
 
-    if (length < 2) {
-        proto_item_set_text(ti, "Truncated address");
+    if (length < 2)
        return -1;
-    }
     address_length = tvb_get_ntohs(tvb, offset);
     proto_tree_add_text(address_tree, tvb, offset, 2, "Address length: %u",
                        address_length);
@@ -349,7 +429,6 @@ dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
     length -= 2;
 
     if (length < address_length) {
-        proto_item_set_text(ti, "Truncated address");
         if (length != 0) {
             proto_tree_add_text(address_tree, tvb, offset, length,
               "Address: %s (truncated)",
@@ -465,23 +544,23 @@ proto_register_cdp(void)
     static hf_register_info hf[] = {
        { &hf_cdp_version,
        { "Version",            "cdp.version",  FT_UINT8, BASE_DEC, NULL, 0x0,
-         "" }},
+         "", HFILL }},
 
        { &hf_cdp_ttl,
        { "TTL",                "cdp.ttl", FT_UINT16, BASE_DEC, NULL, 0x0,
-         "" }},
+         "", HFILL }},
 
        { &hf_cdp_checksum,
        { "Checksum",           "cdp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
-         "" }},
+         "", HFILL }},
 
        { &hf_cdp_tlvtype,
        { "Type",               "cdp.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
-         "" }},
+         "", HFILL }},
 
        { &hf_cdp_tlvlength,
        { "Length",             "cdp.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
-         "" }},
+         "", HFILL }},
     };
     static gint *ett[] = {
        &ett_cdp,
@@ -490,7 +569,8 @@ proto_register_cdp(void)
        &ett_cdp_capabilities,
     };
 
-    proto_cdp = proto_register_protocol("Cisco Discovery Protocol", "cdp");
+    proto_cdp = proto_register_protocol("Cisco Discovery Protocol",
+                                       "CDP", "cdp");
     proto_register_field_array(proto_cdp, hf, array_length(hf));
     proto_register_subtree_array(ett, array_length(ett));
 }
@@ -498,5 +578,6 @@ proto_register_cdp(void)
 void
 proto_reg_handoff_cdp(void)
 {
-    dissector_add("llc.cisco_pid", 0x2000, dissect_cdp);
+    dissector_add("llc.cisco_pid", 0x2000, dissect_cdp, proto_cdp);
+    dissector_add("chdlctype", 0x2000, dissect_cdp, proto_cdp);
 }