aboutsummaryrefslogtreecommitdiffstats
path: root/packet-tns.c
blob: 2b537c1e20050df57ca0c64eb567e9fc63cd3d3f (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* packet-tns.c
 * Routines for MSX tns packet dissection
 *
 * $Id: packet-tns.c,v 1.4 2000/01/07 22:05:41 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-tftp.c
 * 
 * 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.
 */

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

#include <stdio.h>

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

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

#include <string.h>
#include <glib.h>
#include "packet.h"
#include "packet-tns.h"

static int proto_tns = -1;
static int hf_tns_request = -1;
static int hf_tns_response = -1;
static int hf_tns_length = -1;
static int hf_tns_packet_checksum = -1;
static int hf_tns_header_checksum = -1;
static int hf_tns_packet_type = -1;
static int hf_tns_reserved_byte = -1;
static int hf_tns_data_flag = -1;
static int hf_tns_sns = -1;
static int hf_tns_connect = -1;
static int hf_tns_version = -1;
static int hf_tns_compat_version = -1;
static int hf_tns_service_options = -1;

static gint ett_tns = -1;
static gint ett_tns_sns = -1;
static gint ett_tns_connect = -1;
static gint ett_sql = -1;

static const value_string tns_type_vals[] = {
		{TNS_TYPE_CONNECT, "Connect" },
		{TNS_TYPE_ACCEPT, "Accept" },
		{TNS_TYPE_DATA, "Data" },
		{TNS_TYPE_RESEND, "Resend"},
        {0, NULL}
};


/* Handy macro for checking for truncated packet */
#define TRUNC(length) if ( ! BYTES_ARE_IN_FRAME(offset, length)) { \
			dissect_data(pd,offset,fd,tree); return; }

void dissect_tns_sns(const u_char *pd, int offset, frame_data *fd, 
	proto_tree *tree, proto_tree *tns_tree)
{
	proto_tree *sns_tree = NULL, *ti;

	if ( tree )
	{
		ti = proto_tree_add_text(tns_tree, offset, END_OF_FRAME, "Secure Network Services");
		sns_tree = proto_item_add_subtree(ti, ett_tns_sns);

		proto_tree_add_item_hidden(tns_tree, hf_tns_sns, 0, 0, TRUE);
	}
		
	if ( check_col(fd, COL_INFO) )
	{
		col_append_fstr(fd, COL_INFO, ", SNS");
	}

	if ( sns_tree )
	{
		dissect_data(pd,offset,fd,sns_tree);
	}
}

void dissect_tns_data(const u_char *pd, int offset, frame_data *fd, 
	proto_tree *tree, proto_tree *tns_tree)
{

	TRUNC(2);
	if ( tree )
	{
		proto_tree_add_item(tns_tree, hf_tns_data_flag,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;

	if ( BYTES_ARE_IN_FRAME(offset, 4) )
	{
		if ( pd[offset] == 0xDE && pd[offset+1] == 0xAD &&
			pd[offset+2] == 0xBE && pd[offset+3] == 0xEF )
		{
			dissect_tns_sns(pd,offset,fd,tree,tns_tree);
			return;
		}
	}
	
	dissect_data(pd,offset,fd,tree);
	return;
}

void dissect_tns_connect(const u_char *pd, int offset, frame_data *fd, 
	proto_tree *tree, proto_tree *tns_tree)
{
	proto_tree *connect_tree = NULL, *ti;

	if ( tree )
	{
		ti = proto_tree_add_text(tns_tree, offset, END_OF_FRAME, "Connect");
		connect_tree = proto_item_add_subtree(ti, ett_tns_connect);

		proto_tree_add_item_hidden(tns_tree, hf_tns_connect, 0, 0, TRUE);
	}
		
	if ( check_col(fd, COL_INFO) )
	{
		col_append_fstr(fd, COL_INFO, ", Connect");
	}

	TRUNC(2);
	if ( connect_tree )
	{
		proto_tree_add_item(connect_tree, hf_tns_version,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;
	
	TRUNC(2);
	if ( connect_tree )
	{
		proto_tree_add_item(connect_tree, hf_tns_compat_version,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;

	TRUNC(2);
	if ( connect_tree )
	{
		/* need to break down w/ bitfield */
		proto_tree_add_item(connect_tree, hf_tns_service_options,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;

	if ( connect_tree )
	{
		dissect_data(pd,offset,fd,connect_tree);
	}
	return;
}

void dissect_tns_accept(const u_char *pd, int offset, frame_data *fd, 
	proto_tree *tree, proto_tree *tns_tree)
{
	dissect_data(pd,offset,fd,tns_tree);
	return;
}


void
dissect_tns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
{
	proto_tree      *tns_tree = NULL, *ti;
	guint16 length;
	guint16 type;

	if (check_col(fd, COL_PROTOCOL))
	col_add_str(fd, COL_PROTOCOL, "TNS");

	if (check_col(fd, COL_INFO))
	{
		col_add_fstr(fd, COL_INFO, "%s", 
			(pi.match_port == pi.destport) ? "Request" : "Response");	  
	}

	if (tree) 
	{
		ti = proto_tree_add_item(tree, proto_tns, offset, END_OF_FRAME, NULL);
		tns_tree = proto_item_add_subtree(ti, ett_tns);

		if (pi.match_port == pi.destport)
		{
			proto_tree_add_item_hidden(tns_tree, hf_tns_request,
			   offset, END_OF_FRAME, TRUE);
			proto_tree_add_text(tns_tree, offset, 
				END_OF_FRAME, "Request: <opaque data>" );
		}
		else
		{
			proto_tree_add_item_hidden(tns_tree, hf_tns_response,
				offset, END_OF_FRAME, TRUE);
			proto_tree_add_text(tns_tree, offset, 
				END_OF_FRAME, "Response: <opaque data>");
		}
	}

		/* check to make sure length is present */
	if ( ! BYTES_ARE_IN_FRAME(offset, 2)) return;

	length = pntohs(&pd[offset]);
	if (tree)
	{
		proto_tree_add_item(tns_tree, hf_tns_length,
			offset, 2, length);
	}
	TRUNC(length);
	offset += 2;

	TRUNC(2);
	if ( tree )
	{
		proto_tree_add_item(tns_tree, hf_tns_packet_checksum,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;

	TRUNC(2);
	type = pd[offset];
	if ( tree )
	{
		proto_tree_add_item(tns_tree, hf_tns_packet_type,
			offset, 1, type);
	}
	offset += 1;

	if ( check_col(fd, COL_INFO))
	{
		col_append_fstr(fd, COL_INFO, ", %s (%d)",
			val_to_str(type, tns_type_vals, "Unknown"), type);
	}

	TRUNC(1);
	if ( tree )
	{
		proto_tree_add_item(tns_tree, hf_tns_reserved_byte,
			offset, 1, &pd[offset]);
	}
	offset += 1;

	TRUNC(2);
	if ( tree )
	{
		proto_tree_add_item(tns_tree, hf_tns_header_checksum,
			offset, 2, pntohs(&pd[offset]));
	}
	offset += 2;

	switch (type)
	{
		case TNS_TYPE_CONNECT:
			dissect_tns_connect(pd,offset,fd,tree,tns_tree);
			break;
		case TNS_TYPE_ACCEPT:
			dissect_tns_accept(pd,offset,fd,tree,tns_tree);
			break;
		case TNS_TYPE_DATA:
			dissect_tns_data(pd,offset,fd,tree,tns_tree);
			break;
		default:
			dissect_data(pd,offset,fd,tns_tree);
	}
}

void proto_register_tns(void)
{
	static hf_register_info hf[] = {
		{ &hf_tns_sns, { 
			"Secure Network Services", "tns.sns", FT_BOOLEAN, BASE_NONE, 
			NULL, 0x0, "Secure Network Services" }},
		{ &hf_tns_connect, { 
			"Connect", "tns.connect", FT_BOOLEAN, BASE_NONE, 
			NULL, 0x0, "Connect" }},
		{ &hf_tns_response, { 
			"Response", "tns.response", FT_BOOLEAN, BASE_NONE, 
			NULL, 0x0, "TRUE if TNS response" }},
		{ &hf_tns_request, { 
			"Request", "tns.request", FT_BOOLEAN, BASE_NONE, 
			NULL, 0x0, "TRUE if TNS request" }},
		{ &hf_tns_length, { 	
			"Packet Length", "tns.length", FT_UINT32, BASE_NONE, 
			NULL, 0x0, "Length of TNS packet" }},
		{ &hf_tns_packet_checksum, { 	
			"Packet Checksum", "tns.packet_checksum", FT_UINT16, BASE_HEX, 
			NULL, 0x0, "Checksum of Packet Data" }},
		{ &hf_tns_header_checksum, { 	
			"Header Checksum", "tns.header_checksum", FT_UINT16, BASE_HEX, 
			NULL, 0x0, "Checksum of Header Data" }},
		{ &hf_tns_data_flag, { 	
			"Data Flag", "tns.data_flag", FT_UINT16, BASE_HEX, 
			NULL, 0x0, "Data Flag" }},
		{ &hf_tns_version, { 	
			"Version", "tns.version", FT_UINT16, BASE_DEC, 
			NULL, 0x0, "Version" }},
		{ &hf_tns_compat_version, { 	
			"Version (Compatible)", "tns.compat_version", FT_UINT16, BASE_DEC, 
			NULL, 0x0, "Version (Compatible)" }},
		{ &hf_tns_service_options, { 	
			"Service Options", "tns.service_options", FT_UINT16, BASE_HEX, 
			NULL, 0x0, "Service Options" }},
		{ &hf_tns_reserved_byte, { 	
			"Reserved Byte", "tns.reserved_byte", FT_BYTES, BASE_HEX, 
			NULL, 0x0, "Reserved Byte" }},
		{ &hf_tns_packet_type, { 	
			"Packet Type", "tns.type", FT_UINT8, BASE_NONE, 
			VALS(tns_type_vals), 0x0, "Type of TNS packet" }}	
	};

	static gint *ett[] = {
		&ett_tns,
		&ett_tns_sns,
		&ett_tns_connect,
		&ett_sql
	};
	proto_tns = proto_register_protocol(
		"Transparent Network Substrate Protocol", "tns");
	proto_register_field_array(proto_tns, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}