summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-uts.c
blob: bd8f5301a238d31978c1cf183f2fecc24bbf5a4b (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* packet-uts.c
 * Routines for UTS WAN protocol dissection
 * Copyright 2007, Fulko Hew, SITA INC Canada, Inc.
 *
 * Copied from packet-ipars.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/* Use tabstops = 4 */

#include "config.h"

#include <epan/packet.h>
#include <wiretap/wtap.h>
#include <wsutil/str_util.h>

#define	SOH	(0x01)
#define	STX	(0x02)
#define	ETX	(0x03)
#define	EOT	(0x04)
#define	ENQ	(0x05)
#define	BEL	(0x07)
#define	NAK	(0x15)
#define	DLE	(0x10)

#define GRID	(0x20)
#define GSID	(0x50)
#define GDID	(0x70)

#define MAX_POLL_TYPE_MSG_SIZE	(50)

void proto_register_uts(void);

static int	proto_uts;
static int	ett_uts;
static int	ett_header_uts;
static int	ett_trailer_uts;
static int	hf_rid;
static int	hf_sid;
static int	hf_did;
static int	hf_retxrequest;
static int	hf_ack;
static int	hf_replyrequest;
static int	hf_busy;
static int	hf_notbusy;
static int	hf_msgwaiting;
static int	hf_function;
static int	hf_data;

#define MATCH	(1)
#define FETCH	(2)

#define SRC	(1)
#define DST	(2)

static int testchar(tvbuff_t *tvb, packet_info *pinfo _U_, int offset, int op, char match, char *storage)
{
	char temp;

	if (tvb_bytes_exist(tvb, offset, 1)) {
		temp = tvb_get_uint8(tvb, offset) & 0x7f;
		if (op == FETCH || (op == MATCH && temp == match)) {
			if (storage != NULL)
				*storage = temp;
			return 1;
		} else {
			return 0;
		}
	} else {
		col_set_str(pinfo->cinfo, COL_INFO, "Unknown Message Format");
		return 0;
	}
}

static void
set_addr(packet_info *pinfo _U_ , int field, char rid, char sid, char did)
{
	if (field == SRC) {
		col_append_fstr(pinfo->cinfo, COL_DEF_SRC, " %2.2X:%2.2X:%2.2X", rid, sid, did);
	} else {
		col_append_fstr(pinfo->cinfo, COL_DEF_DST, " %2.2X:%2.2X:%2.2X", rid, sid, did);
	}
}

static int
dissect_uts(tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree, void* data _U_)
{
	proto_tree	*uts_tree		= NULL;
	proto_tree	*uts_header_tree	= NULL;
	proto_tree	*uts_trailer_tree	= NULL;
	proto_item	*ti;
	int		length;
	char		rid = 0, sid = 0, did = 0;
	int		offset			= 0;
	int		header_length		= -1;
	int		ack_start		= 0;
	int		busy_start		= 0;
	int		notbusy_start		= 0;
	int		replyrequest_start	= 0;
	int		function_start		= 0;
	int		msgwaiting_start	= 0;
	int		nak_start		= 0;
	int		etx_start		= 0;
	int		bcc_start		= 0;
	int		stx_start		= 0;
	char		function_code;
	uint8_t		*data_ptr;

	enum	{ NOTRAFFIC, OTHER }	msg_type = OTHER;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "UTS");

	if (testchar(tvb, pinfo, 0, MATCH, EOT, NULL)	  &&
		testchar(tvb, pinfo, 1, MATCH, EOT, NULL) &&
		testchar(tvb, pinfo, 2, MATCH, ETX, NULL)) {
		msg_type = NOTRAFFIC;
		col_set_str(pinfo->cinfo, COL_INFO, "No Traffic");
	} else {
		if (testchar(tvb, pinfo, 0, MATCH, SOH, NULL)		&&
		    testchar(tvb, pinfo, 1, FETCH, 0, (char *)&rid)	&&
		    testchar(tvb, pinfo, 2, FETCH, 0, (char *)&sid)	&&
		    testchar(tvb, pinfo, 3, FETCH, 0, (char *)&did)) {
			offset = 4;
			if (testchar(tvb, pinfo, offset, MATCH, ETX, NULL)) {
				col_set_str(pinfo->cinfo, COL_INFO, "General Poll");
				set_addr(pinfo, DST, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, '1', NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)) {
				ack_start = offset;
				if (sid == GSID && did == GDID) {
					col_set_str(pinfo->cinfo, COL_INFO, "General Poll + ACK");
					set_addr(pinfo, DST, rid, sid, did);
				} else if (sid != GSID && did == GDID) {
					col_set_str(pinfo->cinfo, COL_INFO, "Specific Poll + ACK");
					set_addr(pinfo, DST, rid, sid, did);
				} else if (sid != GSID && did != GDID) {
					col_set_str(pinfo->cinfo, COL_INFO, "No Traffic + ACK");
					set_addr(pinfo, SRC, rid, sid, did);
				} else {
					col_set_str(pinfo->cinfo, COL_INFO, "Unknown Message Format");
					if ((pinfo->pseudo_header->sita.sita_flags & SITA_FRAME_DIR) == SITA_FRAME_DIR_TXED) {
						set_addr(pinfo, DST, rid, sid, did);	/* if the ACN sent it, the address is of the destination... the terminal */
					} else {
						set_addr(pinfo, SRC, rid, sid, did);	/* if the ACN received it, the address if of the source... the terminal */
					}
				}
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, NAK, NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)	&&
				   sid != GSID && did == GDID) {
				nak_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Retransmit Request");
				set_addr(pinfo, DST, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, BEL, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, STX, NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)) {
				header_length = offset+2;
				msgwaiting_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Message Waiting");
				set_addr(pinfo, DST, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, '1', NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, STX, NULL)) {
				ack_start = offset;
				header_length = offset+3;
				stx_start = offset+2;
				col_set_str(pinfo->cinfo, COL_INFO, "Text + ACK");
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, STX, NULL)) {
				header_length = offset+1;
				stx_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Text");
				if ((pinfo->pseudo_header->sita.sita_flags & SITA_FRAME_DIR) == SITA_FRAME_DIR_TXED) {
					set_addr(pinfo, DST, rid, sid, did);		/* if the ACN sent it, the address is of the destination... the terminal */
				} else {
					set_addr(pinfo, SRC, rid, sid, did);		/* if the ACN received it, the address if of the source... the terminal */
				}
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, ENQ, NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)) {
				replyrequest_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Reply Request");
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, '?', NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)) {
				busy_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Busy");
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, ';', NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, ETX, NULL)) {
				notbusy_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Not Busy");
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, '1', NULL)	&&
				   testchar(tvb, pinfo, offset+2, MATCH, DLE, NULL)	&&
				   testchar(tvb, pinfo, offset+3, MATCH, ';', NULL)	&&
				   testchar(tvb, pinfo, offset+4, MATCH, ETX, NULL)) {
				notbusy_start = offset+2;
				ack_start = offset;
				col_set_str(pinfo->cinfo, COL_INFO, "Not Busy + ACK");
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, MATCH, DLE, NULL)		&&
				   testchar(tvb, pinfo, offset+1, MATCH, '1', NULL)		&&
				   testchar(tvb, pinfo, offset+2, FETCH, 0, &function_code)	&&
				   testchar(tvb, pinfo, offset+3, MATCH, ETX, NULL)) {
				ack_start = offset;
				function_start = offset + 2;
				col_add_fstr(pinfo->cinfo, COL_INFO, "Function Message '%c' + ACK", function_code);
				set_addr(pinfo, SRC, rid, sid, did);
			} else if (testchar(tvb, pinfo, offset, FETCH, 0, &function_code)	&&
				   testchar(tvb, pinfo, offset+1, MATCH, ETX, NULL)) {
				function_start = offset;
				col_add_fstr(pinfo->cinfo, COL_INFO, "Function Message '%c'", function_code);
				set_addr(pinfo, SRC, rid, sid, did);
			}
		}
	}

	while (tvb_reported_length_remaining(tvb, offset) > 0) {					/* now look for the ETX */
		if ((tvb_get_uint8(tvb, offset) & 0x7f) == ETX) {
			if (header_length == -1)
				header_length = offset;	/* the header ends at an STX, or if not found, the ETX */
			etx_start = offset;
			offset++;
			break;
		}
		offset++;
	}
	if (tvb_reported_length_remaining(tvb, offset))						/* if there is anything left, it could be the BCC and pads */
		bcc_start = offset;

	if (tree) {
		ti = proto_tree_add_protocol_format(tree, proto_uts, tvb, 0, -1, "UTS");
		uts_tree = proto_item_add_subtree(ti, ett_uts);

		if (msg_type == NOTRAFFIC) {
			proto_tree_add_protocol_format(uts_tree, proto_uts, tvb, 0, 2, "No Traffic");
			proto_tree_add_protocol_format(uts_tree, proto_uts, tvb, 2, -1, "ETX + padding");
		} else {
			uts_header_tree = proto_tree_add_subtree(uts_tree, tvb, 0, header_length, ett_header_uts, NULL, "Header");

			proto_tree_add_protocol_format(uts_header_tree, proto_uts, tvb, 0, 1, "SOH");

			if (rid == GRID)
				proto_tree_add_uint_format(uts_header_tree, hf_rid, tvb, 1, 1, rid, "RID (%02X) (General)", rid);
			else
				proto_tree_add_uint_format(uts_header_tree, hf_rid, tvb, 1, 1, rid, "RID (%02X)", rid);

			if (sid == GSID)
				proto_tree_add_uint_format(uts_header_tree, hf_sid, tvb, 2, 1, sid, "SID (%02X) (General)", sid);
			else
				proto_tree_add_uint_format(uts_header_tree, hf_sid, tvb, 2, 1, sid, "SID (%02X)", sid);

			if (did == GDID)
				proto_tree_add_uint_format(uts_header_tree, hf_did, tvb, 3, 1, did, "DID (%02X) (General)", did);
			else
				proto_tree_add_uint_format(uts_header_tree, hf_did, tvb, 3, 1, did, "DID (%02X)", did);

			if (nak_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_retxrequest,	tvb, nak_start,	2, 1, "Re-transmit Request");
			if (ack_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_ack, tvb, ack_start, 2, 1, "Ack");

			if (replyrequest_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_replyrequest,	tvb, replyrequest_start, 2, 1, "Reply Request");
			if (busy_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_busy,	tvb, busy_start, 2, 1, "Busy");

			if (notbusy_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_notbusy, tvb, notbusy_start, 2, 1, "Not Busy");

			if (msgwaiting_start)
				proto_tree_add_boolean_format(uts_header_tree, hf_msgwaiting, tvb, msgwaiting_start, 1, 1, "Message Waiting");

			if (function_start)
				proto_tree_add_uint_format(uts_header_tree, hf_function, tvb, function_start, 1, function_code, "Function '%c'", function_code	);

			if (stx_start) {
				proto_tree_add_protocol_format(uts_header_tree, proto_uts, tvb, stx_start, 1, "Start of Text");
				length = tvb_captured_length_remaining(tvb, stx_start+1);    /* find out how much message remains      */
				if (etx_start)
					length = (etx_start - stx_start - 1);       /* and the data part is the rest...       */
										    /* whatever precedes the ETX if it exists */
				data_ptr = tvb_get_string_enc(pinfo->pool, tvb, stx_start+1, length, ENC_ASCII);	/* copy the string for dissecting */
				proto_tree_add_string_format(uts_tree, hf_data, tvb, stx_start + 1, length, data_ptr,
							     "Text (%d byte%s)", length, plurality(length, "", "s"));
			}

			if (etx_start) {
				uts_trailer_tree = proto_tree_add_subtree(uts_tree, tvb, etx_start, -1, ett_trailer_uts, NULL, "Trailer");

				if (etx_start)
					proto_tree_add_protocol_format(uts_trailer_tree, proto_uts, tvb, etx_start, 1, "ETX");
				if (bcc_start)
					proto_tree_add_protocol_format(uts_trailer_tree, proto_uts, tvb, bcc_start, -1, "CCC + padding");
			}
		}
	}
	return tvb_captured_length(tvb);
}

void
proto_register_uts(void)
{
	static hf_register_info hf[] = {
		{ &hf_rid,
		  { "RID",	   "uts.rid",
		    FT_UINT8,	BASE_HEX,	NULL, 0, "Remote Identifier address",	HFILL }},
		{ &hf_sid,
		  { "SID",	   "uts.sid",
		    FT_UINT8,	BASE_HEX,	NULL, 0, "Site Identifier address",	HFILL }},
		{ &hf_did,
		  { "DID",	   "uts.did",
		    FT_UINT8,	BASE_HEX,	NULL, 0, "Device Identifier address",	HFILL }},
		{ &hf_retxrequest,
		  { "ReTxRequest",  "uts.retxrequest",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Re-transmit Request", HFILL }},
		{ &hf_ack,
		  { "Ack",	   "uts.ack",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Ack",		HFILL }},
		{ &hf_replyrequest,
		  { "ReplyRequest", "uts.replyrequest",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Reply Request",	HFILL }},
		{ &hf_busy,
		  { "Busy",	   "uts.busy",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Busy",		HFILL }},
		{ &hf_notbusy,
		  { "NotBusy",	   "uts.notbusy",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Not Busy",		HFILL }},
		{ &hf_msgwaiting,
		  { "MsgWaiting",  "uts.msgwaiting",
		    FT_BOOLEAN,	BASE_NONE,	NULL, 0x0, "true if Message Waiting",	HFILL }},
		{ &hf_function,
		  { "Function",    "uts.function",
		    FT_UINT8,	BASE_HEX,	NULL, 0, "Function Code value",		HFILL }},
		{ &hf_data,
		  { "Data",	   "uts.data",
		    FT_STRING,	BASE_NONE,	NULL, 0, "User Data Message",		HFILL }},
	};

	static int *ett[] = {
		&ett_uts,
		&ett_header_uts,
		&ett_trailer_uts,
	};

	proto_uts = proto_register_protocol("Unisys Transmittal System", "UTS", "uts");		/* name, short name, abbrev */
	proto_register_field_array(proto_uts, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	register_dissector("uts", dissect_uts, proto_uts);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */