aboutsummaryrefslogtreecommitdiffstats
path: root/packet-tpkt.c
blob: a1e11103ddd2561bb0fbbd871826f599b2b3997f (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
/* packet-tpkt.c
 *
 * Routines for TPKT dissection
 * Copyright 2000, Philips Electronics N.V.
 * Andreas Sikkema <andreas.sikkema@philips.com>
 *
 * 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.
 */

/*
 * This dissector tries to dissect the TPKT protocol according to
 * RFC 1006
 *
 * IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT 
 *
 * Please examine the dissector. It is NOT defined in the normal way!
 * Some variables are references and the dissector also returns a 
 * value! And no, this is not a heuristic dissector!
 * 
 * IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT 
 */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

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

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif

#include <stdio.h>
#include <string.h>

#include "packet-tpkt.h"

/* TPKT header fields             */
static int proto_tpkt          = -1;
static int hf_tpkt_version     = -1;
static int hf_tpkt_reserved    = -1;
static int hf_tpkt_length      = -1;

/* TPKT fields defining a sub tree */
static gint ett_tpkt           = -1;

int 
is_tpkt( tvbuff_t *tvb, unsigned int* offset )
{
	if ( (*offset) + 4 > tvb_length( tvb ) ) return FALSE;
	if ( ! ( ( tvb_get_guint8( tvb, ( *offset ) ) == 3 ) && 
		       ( tvb_get_guint8( tvb, ( *offset ) + 1 ) == 0 ) ) ) return FALSE;

  return TRUE;
}

int
dissect_tpkt( tvbuff_t *tvb, unsigned int* offset, packet_info *pinfo, proto_tree *tree )
{
	proto_item *ti            = NULL;
	proto_tree *tpkt_tree     = NULL;
	unsigned int data_len = 0;


	pinfo->current_proto = "TPKT";

	/* There should at least be 4 bytes left in the frame */
	if ( (*offset) + 4 > tvb_length( tvb ) ) return -1;
	/* 
	 * The first octet should be 3 and the second one should be 0 
	 * The H.323 implementers guide suggests that this migh not 
	 * always be the case....
	 */
	if ( ! ( ( tvb_get_guint8( tvb, ( *offset ) ) == 3 ) && 
		       ( tvb_get_guint8( tvb, ( *offset ) + 1 ) == 0 ) ) ) return -1;

	if ( check_col( pinfo->fd, COL_PROTOCOL ) ) {
		col_set_str( pinfo->fd, COL_PROTOCOL, "TPKT" );
	}
	
	if ( check_col( pinfo->fd, COL_INFO) ) {
		/*data_len = pntohs( &pd[ (*offset) + 2 ] );*/
		data_len = tvb_get_ntohs( tvb, (*offset) + 2 );

		col_add_fstr( pinfo->fd, COL_INFO, "TPKT Data length = %d", data_len );
	}

	if ( tree ) {
		ti = proto_tree_add_item( tree, proto_tpkt, tvb, (*offset), 4, FALSE );
		tpkt_tree = proto_item_add_subtree( ti, ett_tpkt );
		/* Version 1st octet */
		proto_tree_add_item( tpkt_tree, hf_tpkt_version, tvb, (*offset), 1, FALSE );
		(*offset)++;
		/* Reserved octet*/
		proto_tree_add_item( tpkt_tree, hf_tpkt_reserved, tvb, (*offset), 1, FALSE );
		(*offset)++;
	}
	else {
		(*offset) += 2;
	}
	/* Length, two octets */
	/*data_len = pntohs( &pd[ (*offset) ] );*/
	data_len = tvb_get_ntohs( tvb, (*offset) );

	if ( tree )
		proto_tree_add_uint_format( tpkt_tree, hf_tpkt_length, tvb, (*offset), 2, data_len, "Length: %d", data_len );

	(*offset) += 2;
	return data_len;
}

void
proto_register_tpkt(void)
{
	static hf_register_info hf[] = 
	{
		{ 
			&hf_tpkt_version,
			{ 
				"Version", 
				"tpkt.version", 
				FT_UINT8, 
				BASE_DEC, 
				NULL, 
				0x0,
				"" 
			}
		},
		{ 
			&hf_tpkt_reserved,
			{ 
				"Reserved", 
				"tpkt.reserved", 
				FT_UINT8, 
				BASE_DEC, 
				NULL, 
				0x0,
				"" 
			}
		},
		{ 
			&hf_tpkt_length,
			{ 
				"Length", 
				"tpkt.length", 
				FT_UINT16, 
				BASE_DEC, 
				NULL, 
				0x0,
				"" 
			}
		},
};
	
	static gint *ett[] = 
	{
		&ett_tpkt,
	};


	proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
	proto_register_field_array(proto_tpkt, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}