aboutsummaryrefslogtreecommitdiffstats
path: root/packet-cdp.c
blob: 8874e048e26210d530371268dc1bd74ffc1c9a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* packet-cdp.c
 * Routines for the disassembly of the "Cisco Discovery Protocol"
 * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
 *
 * $Id: packet-cdp.c,v 1.9 1999/07/07 22:51:41 gram Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * 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
 * 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"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include "packet.h"

/* Offsets in TLV structure. */
#define	TLV_TYPE	0
#define	TLV_LENGTH	2

static void
add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
  const gchar *prefix, const gchar *string);

void 
dissect_cdp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
    proto_tree *cdp_tree = NULL;
	proto_item *ti; 
    
    typedef struct _e_cdp_hdr{
		char version;
		char flags;
		gint16 ttl;
	} e_cdp_hdr;

    e_cdp_hdr *cdp_hdr;
    char *stringmem;
    gint16 type;
    gint16 length;

    if (check_col(fd, COL_PROTOCOL))
        col_add_str(fd, COL_PROTOCOL, "CDP");
    if (check_col(fd, COL_INFO))
        col_add_str(fd, COL_INFO, "Cisco Discovery Protocol"); 

    if(tree){
        ti = proto_tree_add_text(tree, offset, (fd->cap_len - offset), 
                                                          "Cisco Discovery Protocol");
	cdp_tree = proto_item_add_subtree(ti, ETT_CDP);
	
	/* CDP header */
	cdp_hdr = (e_cdp_hdr *) &pd[offset];
	proto_tree_add_text(cdp_tree, offset, 1, "Version: %d", cdp_hdr->version);
	proto_tree_add_text(cdp_tree, offset+1, 1, "Flags (unknown)");
	proto_tree_add_text(cdp_tree, offset+2, 2, "TTL (unknown)");
	offset+=4;

	/* CVS -> exit here 
    	dissect_data(pd, offset, fd, cdp_tree);
		return;
     */
	
	while( offset <= fd->cap_len ){
		type = pntohs(&pd[offset + TLV_TYPE]);
		length = pntohs(&pd[offset + TLV_LENGTH]);
		switch( type ){
			case 0: /* ??? Mgmt Addr */
				offset+=length + 4;
				break;
			case 1: /* ??? Chassis ID */
				proto_tree_add_text(cdp_tree, offset + 4,
				    length - 4, "Chassis ID: %s", &pd[offset+4] );
				offset+=length;
				break;
			case 2:  
				/* this is quite strange: this tlv contains no data itself but two tlvs which
                 * calculate the length without the 2 byte type and 2 byte length field
                 */
				offset+=4; 
				break;
			case 3: /* ??? Port  */    
				proto_tree_add_text(cdp_tree, offset + 4,
				  length - 4, "Sent through Interface: %s", &pd[offset+4] );
				offset+=length;
				break;
			case 5: /* ??? IOS Version */
				add_multi_line_string_to_tree(cdp_tree,
				    offset + 4, length - 4, "Software Version: ",
				    &pd[offset+4] );
				offset+=length;
				break;
			case 6: /* ??? platform */
				
				stringmem = malloc(length);
				memset(stringmem, '\0', length);
				memcpy(stringmem, &pd[offset+4], length - 4 );
				proto_tree_add_text(cdp_tree, offset + 4, length - 4, 
                                                     "Platform: %s", stringmem );
				free(stringmem);
				offset+=length;
				break;
			case 0x01cc: /* ??? Mgmt Addr */
				proto_tree_add_text(cdp_tree, offset + 4, length, 
                                                     "Mgmt IP: %s",
						     ip_to_str(&pd[offset+4]) );
				offset+=length + 4;
				break;
			default:
/*
				if( type > 512){
					dissect_data(pd, offset, fd, cdp_tree);
					return;
				}
*/
/*
				proto_tree_add_text(cdp_tree, offset + TLV_TYPE,
				    2, "Type: %d", type);
				proto_tree_add_text(cdp_tree, offset + TLV_LENGTH,
				    2, "Length: %d", length);
				proto_tree_add_text(cdp_tree, offset + 4,
				    length - 4, "Data");
*/

				offset+=length;
		}

	}
    	dissect_data(pd, offset, fd, cdp_tree);
    }
}

static void
add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
  const gchar *prefix, const gchar *string)
{
    int prefix_len;
    int i;
    char blanks[64+1];
    const gchar *p, *q;
    int line_len;
    int data_len;

    prefix_len = strlen(prefix);
    if (prefix_len > 64)
	prefix_len = 64;
    for (i = 0; i < prefix_len; i++)
	blanks[i] = ' ';
    blanks[i] = '\0';
    p = string;
    for (;;) {
	q = strchr(p, '\n');
	if (q != NULL) {
	    line_len = q - p;
	    data_len = line_len + 1;
	} else {
	    line_len = strlen(p);
	    data_len = line_len;
	}
	proto_tree_add_text(tree, start, data_len, "%s%.*s", prefix,
	   line_len, p);
	if (q == NULL)
	    break;
	p += data_len;
	start += data_len;
	prefix = blanks;
    }
}