Fix up the decode of "greater than core and up to and including LANMAN
[metze/wireshark/wip.git] / packet-cdp.c
1 /* packet-cdp.c
2  * Routines for the disassembly of the "Cisco Discovery Protocol"
3  * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
4  *
5  * $Id: packet-cdp.c,v 1.8 1999/03/23 03:14:36 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  * 
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26  
27 #include "config.h"
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <glib.h>
37 #include "packet.h"
38
39 /* Offsets in TLV structure. */
40 #define TLV_TYPE        0
41 #define TLV_LENGTH      2
42
43 static void
44 add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
45   const gchar *prefix, const gchar *string);
46
47 void 
48 dissect_cdp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
49     proto_tree *cdp_tree = NULL;
50         proto_item *ti; 
51     
52     typedef struct _e_cdp_hdr{
53                 char version;
54                 char flags;
55                 gint16 ttl;
56         } e_cdp_hdr;
57
58     e_cdp_hdr *cdp_hdr;
59     char *stringmem;
60     gint16 type;
61     gint16 length;
62
63     if (check_col(fd, COL_PROTOCOL))
64         col_add_str(fd, COL_PROTOCOL, "CDP");
65     if (check_col(fd, COL_INFO))
66         col_add_str(fd, COL_INFO, "Cisco Discovery Protocol"); 
67
68     if(tree){
69         ti = proto_tree_add_item(tree, offset, (fd->cap_len - offset), 
70                                                           "Cisco Discovery Protocol");
71         cdp_tree = proto_tree_new(); 
72         proto_item_add_subtree(ti, cdp_tree, ETT_CDP);
73         
74         /* CDP header */
75         cdp_hdr = (e_cdp_hdr *) &pd[offset];
76         proto_tree_add_item(cdp_tree, offset, 1, "Version: %d", cdp_hdr->version);
77         proto_tree_add_item(cdp_tree, offset+1, 1, "Flags (unknown)");
78         proto_tree_add_item(cdp_tree, offset+2, 2, "TTL (unknown)");
79         offset+=4;
80
81         /* CVS -> exit here 
82         dissect_data(pd, offset, fd, cdp_tree);
83                 return;
84      */
85         
86         while( offset <= fd->cap_len ){
87                 type = pntohs(&pd[offset + TLV_TYPE]);
88                 length = pntohs(&pd[offset + TLV_LENGTH]);
89                 switch( type ){
90                         case 0: /* ??? Mgmt Addr */
91                                 offset+=length + 4;
92                                 break;
93                         case 1: /* ??? Chassis ID */
94                                 proto_tree_add_item(cdp_tree, offset + 4,
95                                     length - 4, "Chassis ID: %s", &pd[offset+4] );
96                                 offset+=length;
97                                 break;
98                         case 2:  
99                                 /* this is quite strange: this tlv contains no data itself but two tlvs which
100                  * calculate the length without the 2 byte type and 2 byte length field
101                  */
102                                 offset+=4; 
103                                 break;
104                         case 3: /* ??? Port  */    
105                                 proto_tree_add_item(cdp_tree, offset + 4,
106                                   length - 4, "Sent through Interface: %s", &pd[offset+4] );
107                                 offset+=length;
108                                 break;
109                         case 5: /* ??? IOS Version */
110                                 add_multi_line_string_to_tree(cdp_tree,
111                                     offset + 4, length - 4, "Software Version: ",
112                                     &pd[offset+4] );
113                                 offset+=length;
114                                 break;
115                         case 6: /* ??? platform */
116                                 
117                                 stringmem = malloc(length);
118                                 memset(stringmem, '\0', length);
119                                 memcpy(stringmem, &pd[offset+4], length - 4 );
120                                 proto_tree_add_item(cdp_tree, offset + 4, length - 4, 
121                                                      "Platform: %s", stringmem );
122                                 free(stringmem);
123                                 offset+=length;
124                                 break;
125                         case 0x01cc: /* ??? Mgmt Addr */
126                                 proto_tree_add_item(cdp_tree, offset + 4, length, 
127                                                      "Mgmt IP: %s",
128                                                      ip_to_str(&pd[offset+4]) );
129                                 offset+=length + 4;
130                                 break;
131                         default:
132 /*
133                                 if( type > 512){
134                                         dissect_data(pd, offset, fd, cdp_tree);
135                                         return;
136                                 }
137 */
138 /*
139                                 proto_tree_add_item(cdp_tree, offset + TLV_TYPE,
140                                     2, "Type: %d", type);
141                                 proto_tree_add_item(cdp_tree, offset + TLV_LENGTH,
142                                     2, "Length: %d", length);
143                                 proto_tree_add_item(cdp_tree, offset + 4,
144                                     length - 4, "Data");
145 */
146
147                                 offset+=length;
148                 }
149
150         }
151         dissect_data(pd, offset, fd, cdp_tree);
152     }
153 }
154
155 static void
156 add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
157   const gchar *prefix, const gchar *string)
158 {
159     int prefix_len;
160     int i;
161     char blanks[64+1];
162     const gchar *p, *q;
163     int line_len;
164     int data_len;
165
166     prefix_len = strlen(prefix);
167     if (prefix_len > 64)
168         prefix_len = 64;
169     for (i = 0; i < prefix_len; i++)
170         blanks[i] = ' ';
171     blanks[i] = '\0';
172     p = string;
173     for (;;) {
174         q = strchr(p, '\n');
175         if (q != NULL) {
176             line_len = q - p;
177             data_len = line_len + 1;
178         } else {
179             line_len = strlen(p);
180             data_len = line_len;
181         }
182         proto_tree_add_item(tree, start, data_len, "%s%.*s", prefix,
183            line_len, p);
184         if (q == NULL)
185             break;
186         p += data_len;
187         start += data_len;
188         prefix = blanks;
189     }
190 }