aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-indigocare-icall.c
blob: 65b0b8f0e1a540b3f062a2edee195c23bf2960df (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
/* packet-indigocare-icall.c
 * Dissector routines for the IndigoCare iCall protocol
 * By Erik de Jong <erikdejong@gmail.com>
 * Copyright 2016 Erik de Jong
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <range.h>
#include <wiretap/wtap.h>
#include <epan/packet.h>
#include <epan/expert.h>
#include <wsutil/strtoi.h>

#define INDIGOCARE_ICALL_SOH			0x01
#define INDIGOCARE_ICALL_STX			0x02
#define INDIGOCARE_ICALL_ETX			0x03
#define INDIGOCARE_ICALL_EOT			0x04
#define INDIGOCARE_ICALL_ACK			0x06
#define INDIGOCARE_ICALL_US			0x1F
#define INDIGOCARE_ICALL_RS			0x1E

#define INDIGOCARE_ICALL_CALL			0x0A

#define INDIGOCARE_ICALL_CALL_ROOM		0x01
#define INDIGOCARE_ICALL_CALL_TYPE		0x02
#define INDIGOCARE_ICALL_CALL_ADDITION		0x03
#define INDIGOCARE_ICALL_CALL_ID		0x04
#define INDIGOCARE_ICALL_CALL_TASK		0x05
#define INDIGOCARE_ICALL_CALL_LOCATION		0x06
#define INDIGOCARE_ICALL_CALL_NAME1		0x07
#define INDIGOCARE_ICALL_CALL_NAME2		0x08
#define INDIGOCARE_ICALL_CALL_TYPE_NUMERICAL	0x09
#define INDIGOCARE_ICALL_CALL_NURSE		0x0A

void proto_reg_handoff_icall(void);
void proto_register_icall(void);

static expert_field ei_icall_unexpected_header = EI_INIT;
static expert_field ei_icall_unexpected_record = EI_INIT;
static expert_field ei_icall_unexpected_end = EI_INIT;

static int proto_icall = -1;
static int hf_icall_header_type = -1;

static int hf_icall_call_room_type = -1;
static int hf_icall_call_type_type = -1;
static int hf_icall_call_addition_type = -1;
static int hf_icall_call_id_type = -1;
static int hf_icall_call_task_type = -1;
static int hf_icall_call_location_type = -1;
static int hf_icall_call_name1_type = -1;
static int hf_icall_call_name2_type = -1;
static int hf_icall_call_numerical_type = -1;
static int hf_icall_call_nurse_type = -1;

static int hf_icall_padding_type = -1;

static gint ett_icall = -1;
static gint ett_icall_call = -1;
static gint ett_icall_unknown = -1;

static const value_string icall_headertypenames[] = {
	{ INDIGOCARE_ICALL_CALL,		"Call Info" },
	{ 0, NULL }
};

static int
dissect_icall(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_)
{
	proto_item *ti;
	proto_item *header_item;
	proto_tree *icall_tree;
	proto_tree *icall_header_tree;
	gint32 current_offset = 0, header_offset, identifier_start, identifier_offset, data_start, data_offset, ett;
	gint32 header;
	gint32 record_identifier;
	const guint8 * record_data;

	/* Starts with SOH */
	if ( tvb_get_guint8(tvb, 0) != INDIGOCARE_ICALL_SOH )
		return 0;
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "iCall");
	col_clear(pinfo->cinfo,COL_INFO);
	ti = proto_tree_add_item(tree, proto_icall, tvb, 0, -1, ENC_NA);
	icall_tree = proto_item_add_subtree(ti, ett_icall);
	current_offset++;

	/* Read header */
	header_offset = tvb_find_guint8(tvb, current_offset, -1, INDIGOCARE_ICALL_STX);
	ws_strtoi32(tvb_get_string_enc(pinfo->pool, tvb, current_offset, header_offset - current_offset, ENC_ASCII|ENC_NA), NULL, &header);
	col_add_fstr(pinfo->cinfo, COL_INFO, "%s:", val_to_str(header, icall_headertypenames, "Unknown (%d)"));
	switch(header) {
		case INDIGOCARE_ICALL_CALL:
			ett = ett_icall_call;
		break;
		default:
			proto_tree_add_expert_format(icall_tree, pinfo, &ei_icall_unexpected_header, tvb, current_offset, header_offset -  current_offset, "Unexpected header %d", header);
			return 0;
		break;
	}
	header_item = proto_tree_add_uint_format(icall_tree, hf_icall_header_type, tvb, current_offset, header_offset - current_offset, header, "%s", val_to_str(header, icall_headertypenames, "Unknown (%d)"));
	icall_header_tree = proto_item_add_subtree(header_item, ett);
	current_offset = header_offset + 1;

	/* Read records */
	while (tvb_get_guint8(tvb, current_offset) != INDIGOCARE_ICALL_ETX) {
		identifier_start = current_offset;
		identifier_offset = tvb_find_guint8(tvb, current_offset, -1, INDIGOCARE_ICALL_US);
		ws_strtoi32(tvb_get_string_enc(pinfo->pool, tvb, current_offset, identifier_offset - current_offset, ENC_ASCII|ENC_NA), NULL, &record_identifier);
		current_offset = identifier_offset + 1;

		data_start = current_offset;
		data_offset = tvb_find_guint8(tvb, data_start, -1, INDIGOCARE_ICALL_RS);
		record_data = tvb_get_string_enc(pinfo->pool, tvb, current_offset, data_offset - data_start, ENC_ASCII|ENC_NA);

		current_offset = data_offset + 1;

		switch (header) {
			case INDIGOCARE_ICALL_CALL:
				switch (record_identifier) {
					case INDIGOCARE_ICALL_CALL_ROOM:
						proto_tree_add_item_ret_string(icall_header_tree, hf_icall_call_room_type, tvb, data_start, data_offset - data_start, ENC_ASCII|ENC_NA, pinfo->pool, &record_data);
						col_append_fstr(pinfo->cinfo, COL_INFO, " Room=%s", record_data);
					break;
					case INDIGOCARE_ICALL_CALL_TYPE:
						proto_tree_add_item_ret_string(icall_header_tree, hf_icall_call_type_type, tvb, data_start, data_offset - data_start, ENC_ASCII|ENC_NA, pinfo->pool, &record_data);
						col_append_fstr(pinfo->cinfo, COL_INFO, " Type=%s", record_data);
					break;
					case INDIGOCARE_ICALL_CALL_ADDITION:
						proto_tree_add_item(icall_header_tree, hf_icall_call_addition_type, tvb, data_start, data_offset - data_start, ENC_ASCII);
					break;
					case INDIGOCARE_ICALL_CALL_ID:
						proto_tree_add_item(icall_header_tree, hf_icall_call_id_type, tvb, data_start, data_offset - data_start, ENC_ASCII);
					break;
					case INDIGOCARE_ICALL_CALL_TASK:
						proto_tree_add_item(icall_header_tree, hf_icall_call_task_type, tvb, data_start, data_offset - data_start, ENC_ASCII);
					break;
					case INDIGOCARE_ICALL_CALL_LOCATION:
						proto_tree_add_item_ret_string(icall_header_tree, hf_icall_call_location_type, tvb, data_start, data_offset - data_start, ENC_ASCII|ENC_NA, pinfo->pool, &record_data);
						col_append_fstr(pinfo->cinfo, COL_INFO, " Location=%s", record_data);
					break;
					case INDIGOCARE_ICALL_CALL_NAME1:
						proto_tree_add_item_ret_string(icall_header_tree, hf_icall_call_name1_type, tvb, data_start, data_offset - data_start, ENC_ASCII|ENC_NA, pinfo->pool, &record_data);
						col_append_fstr(pinfo->cinfo, COL_INFO, " Name 1=%s", record_data);
					break;
					case INDIGOCARE_ICALL_CALL_NAME2:
						proto_tree_add_item_ret_string(icall_header_tree, hf_icall_call_name2_type, tvb, data_start, data_offset - data_start, ENC_ASCII|ENC_NA, pinfo->pool, &record_data);
						col_append_fstr(pinfo->cinfo, COL_INFO, " Name 2=%s", record_data);
					break;
					case INDIGOCARE_ICALL_CALL_TYPE_NUMERICAL:
						proto_tree_add_item(icall_header_tree, hf_icall_call_numerical_type, tvb, data_start, data_offset - data_start, ENC_ASCII);
					break;
					case INDIGOCARE_ICALL_CALL_NURSE:
						proto_tree_add_item(icall_header_tree, hf_icall_call_nurse_type, tvb, data_start, data_offset - data_start, ENC_ASCII);
					break;
					default:
						proto_tree_add_expert_format(icall_header_tree, pinfo, &ei_icall_unexpected_record, tvb, identifier_start, data_offset - identifier_start, "Unexpected record %d with value %s", record_identifier, record_data);
					break;
				}
			break;
		}
	}
	current_offset++;
	if (tvb_get_guint8(tvb, current_offset) != INDIGOCARE_ICALL_EOT) {
		/* Malformed packet terminator */
		proto_tree_add_expert(icall_header_tree, pinfo, &ei_icall_unexpected_end, tvb, current_offset, 1);
		return tvb_captured_length(tvb);
	}
	current_offset++;
	if (tvb_captured_length_remaining(tvb, current_offset)) {
		/* Padding */
		proto_tree_add_item(icall_header_tree, hf_icall_padding_type, tvb, current_offset, tvb_captured_length_remaining(tvb, current_offset), ENC_NA);
	}
	return tvb_captured_length(tvb);
}

void
proto_reg_handoff_icall(void)
{
	dissector_handle_t icall_handle;

	icall_handle = create_dissector_handle(dissect_icall, proto_icall);
	dissector_add_for_decode_as("udp.port", icall_handle);
	dissector_add_for_decode_as("tcp.port", icall_handle);
}

void
proto_register_icall(void)
{
	static hf_register_info hf[] = {
	{ &hf_icall_header_type,
		{ "Header Type", "icall.header",
		FT_UINT32, BASE_DEC,
		VALS(icall_headertypenames), 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_room_type,
		{ "Room", "icall.call.room",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_type_type,
		{ "Type", "icall.call.type",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_addition_type,
		{ "Addition", "icall.call.addition",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_id_type,
		{ "ID", "icall.call.id",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_task_type,
		{ "Task", "icall.call.task",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_location_type,
		{ "Location", "icall.call.location",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_name1_type,
		{ "Name 1", "icall.call.name1",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_name2_type,
		{ "Name 2", "icall.call.name2",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_numerical_type,
		{ "Type Numerical", "icall.call.type_numerical",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_call_nurse_type,
		{ "Nurse", "icall.call.nurse",
		FT_STRING, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	},
	{ &hf_icall_padding_type,
		{ "Padding", "icall.padding",
		FT_BYTES, BASE_NONE,
		NULL, 0x0,
		NULL, HFILL }
	}
	};

	static ei_register_info ei[] = {
		{ &ei_icall_unexpected_header, { "icall.unexpected.header", PI_MALFORMED, PI_WARN, "Unexpected header", EXPFILL }},
		{ &ei_icall_unexpected_record, { "icall.unexpected.record", PI_MALFORMED, PI_WARN, "Unexpected record", EXPFILL }},
		{ &ei_icall_unexpected_end, { "icall.unexpected.end", PI_MALFORMED, PI_WARN, "Unexpected end of packet", EXPFILL }}
	};

	expert_module_t* expert_icall;

	/* Setup protocol subtree array */
	static gint *ett[] = {
		&ett_icall,
		&ett_icall_call,
		&ett_icall_unknown
	};

	proto_icall = proto_register_protocol (
		"iCall Communication Protocol",	/* name */
		"iCall",			/* short name */
		"icall"				/* abbrev */
	);

	proto_register_field_array(proto_icall, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	expert_icall = expert_register_protocol(proto_icall);
	expert_register_field_array(expert_icall, ei, array_length(ei));
}

/*
 * 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:
 */