Add suppport for Microsoft Network Monitor Ethernet capture files.
[obnox/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.5 1999/01/06 23:07:42 guy 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 #include <gtk/gtk.h>
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41
42 #include "ethereal.h"
43 #include "packet.h"
44
45 /* Offsets in TLV structure. */
46 #define TLV_TYPE        0
47 #define TLV_LENGTH      2
48
49 static void
50 add_multi_line_string_to_tree(GtkWidget *tree, gint start, gint len,
51   const gchar *prefix, const gchar *string);
52
53 void 
54 dissect_cdp(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
55     GtkWidget *cdp_tree = NULL, *ti; 
56     
57     typedef struct _e_cdp_hdr{
58                 char version;
59                 char flags;
60                 gint16 ttl;
61         } e_cdp_hdr;
62
63     e_cdp_hdr *cdp_hdr;
64     char *stringmem;
65     gint16 type;
66     gint16 length;
67
68     if (check_col(fd, COL_PROTOCOL))
69         col_add_str(fd, COL_PROTOCOL, "CDP");
70     if (check_col(fd, COL_INFO))
71         col_add_str(fd, COL_INFO, "Cisco Discovery Protocol"); 
72
73     if(tree){
74         ti = add_item_to_tree(GTK_WIDGET(tree), offset, (fd->cap_len - offset), 
75                                                           "Cisco Discovery Protocol");
76         cdp_tree = gtk_tree_new(); 
77         add_subtree(ti, cdp_tree, ETT_CDP);
78         
79         /* CDP header */
80         cdp_hdr = (e_cdp_hdr *) &pd[offset];
81         add_item_to_tree(cdp_tree, offset, 0, "under development (hannes@boehm.org)");
82         add_item_to_tree(cdp_tree, offset, 1, "Version: %d", cdp_hdr->version);
83         add_item_to_tree(cdp_tree, offset+1, 1, "Flags (unknown)");
84         add_item_to_tree(cdp_tree, offset+2, 2, "TTL (unknown)");
85         offset+=4;
86
87         /* CVS -> exit here 
88         dissect_data(pd, offset, fd, (GtkTree *) cdp_tree);
89                 return;
90      */
91         
92         while( offset <= fd->cap_len ){
93                 type = pntohs(&pd[offset + TLV_TYPE]);
94                 length = pntohs(&pd[offset + TLV_LENGTH]);
95                 switch( type ){
96                         case 0: /* ??? Mgmt Addr */
97                                 offset+=length + 4;
98                                 break;
99                         case 1: /* ??? Chassis ID */
100                                 add_item_to_tree(cdp_tree, offset + 4,
101                                     length - 4, "Chassis ID: %s", &pd[offset+4] );
102                                 offset+=length;
103                                 break;
104                         case 2:  
105                                 /* this is quite strange: this tlv contains no data itself but two tlvs which
106                  * calculate the length without the 2 byte type and 2 byte length field
107                  */
108                                 offset+=4; 
109                                 break;
110                         case 3: /* ??? Port  */    
111                                 add_item_to_tree(cdp_tree, offset + 4,
112                                   length - 4, "Sent through Interface: %s", &pd[offset+4] );
113                                 offset+=length;
114                                 break;
115                         case 5: /* ??? IOS Version */
116                                 add_multi_line_string_to_tree(cdp_tree,
117                                     offset + 4, length - 4, "Software Version: ",
118                                     &pd[offset+4] );
119                                 offset+=length;
120                                 break;
121                         case 6: /* ??? platform */
122                                 
123                                 stringmem = malloc(length);
124                                 memset(stringmem, '\0', length);
125                                 memcpy(stringmem, &pd[offset+4], length - 4 );
126                                 add_item_to_tree(cdp_tree, offset + 4, length - 4, 
127                                                      "Platform: %s", stringmem );
128                                 free(stringmem);
129                                 offset+=length;
130                                 break;
131                         case 0x01cc: /* ??? Mgmt Addr */
132                                 add_item_to_tree(cdp_tree, offset + 4, length, 
133                                                      "Mgmt IP: %s",
134                                                      ip_to_str(&pd[offset+4]) );
135                                 offset+=length + 4;
136                                 break;
137                         default:
138 /*
139                                 if( type > 512){
140                                         dissect_data(pd, offset, fd, (GtkTree *) cdp_tree);
141                                         return;
142                                 }
143 */
144 /*
145                                 add_item_to_tree(cdp_tree, offset + TLV_TYPE,
146                                     2, "Type: %d", type);
147                                 add_item_to_tree(cdp_tree, offset + TLV_LENGTH,
148                                     2, "Length: %d", length);
149                                 add_item_to_tree(cdp_tree, offset + 4,
150                                     length - 4, "Data");
151 */
152
153                                 offset+=length;
154                 }
155
156         }
157         dissect_data(pd, offset, fd, (GtkTree *) cdp_tree);
158     }
159 }
160
161 static void
162 add_multi_line_string_to_tree(GtkWidget *tree, gint start, gint len,
163   const gchar *prefix, const gchar *string)
164 {
165     int prefix_len;
166     int i;
167     char blanks[64+1];
168     const gchar *p, *q;
169     int line_len;
170     int data_len;
171
172     prefix_len = strlen(prefix);
173     if (prefix_len > 64)
174         prefix_len = 64;
175     for (i = 0; i < prefix_len; i++)
176         blanks[i] = ' ';
177     blanks[i] = '\0';
178     p = string;
179     for (;;) {
180         q = strchr(p, '\n');
181         if (q != NULL) {
182             line_len = q - p;
183             data_len = line_len + 1;
184         } else {
185             line_len = strlen(p);
186             data_len = line_len;
187         }
188         add_item_to_tree(tree, start, data_len, "%s%.*s", prefix,
189            line_len, p);
190         if (q == NULL)
191             break;
192         p += data_len;
193         start += data_len;
194         prefix = blanks;
195     }
196 }