summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-ax25-nol3.c
blob: 75931cda6c5b84713a557d5ed3b99ec8d6fdc754 (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
/* packet-ax25-nol3.c
 *
 * Routines for Amateur Packet Radio protocol dissection
 * Copyright 2007,2008,2009,2010,2012 R.W. Stearn <richard@rns-stearn.demon.co.uk>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * This dissector is for the "No Layer 3 protocol" PID of the AX.25 Amateur
 * Packet-Radio Link-Layer Protocol, Version 2.0, October 1984
 *
 * At the time of writing the specification could be found here:
 *   http://www.tapr.org/pub_ax25.html
 *
 * Information on the "protocols" recognised by this dissector are drawn from:
 *  DX cluster:
 *    A network capture kindly donated by Luca Melette.
 *  APRS:
 *    http://www.aprs.org/
 *
 * Inspiration on how to build the dissector drawn from
 *   packet-sdlc.c
 *   packet-x25.c
 *   packet-lapb.c
 *   paket-gprs-llc.c
 *   xdlc.c
 * with the base file built from README.developers.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/ax25_pids.h>

#define STRLEN	80

void proto_register_ax25_nol3(void);
void proto_reg_handoff_ax25_nol3(void);

/* Dissector handles - all the possibles are listed */
static dissector_handle_t aprs_handle;

/* Initialize the protocol and registered fields */
static int proto_ax25_nol3;
static int proto_dx;

static int hf_dx_report;

/* static int hf_text; */

/* Global preferences */
static bool gPREF_APRS;
static bool gPREF_DX;

/* Initialize the subtree pointers */
static int ett_ax25_nol3;

static int ett_dx;


/* Code to actually dissect the packets */
static int
dissect_dx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data _U_)
{
	proto_item *ti;
	proto_tree *dx_tree;

	int data_len;
	int offset;

	offset   = 0;
	data_len = tvb_reported_length_remaining( tvb, offset );

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

	col_add_str( pinfo->cinfo, COL_INFO, tvb_format_text( pinfo->pool, tvb, offset, 15 ) );

	if ( parent_tree )
		{
		/* create display subtree for the protocol */
		ti = proto_tree_add_protocol_format( parent_tree, proto_dx, tvb, 0, -1,
		    "DX (%s)", tvb_format_text( pinfo->pool, tvb, offset, 15 ) );
		dx_tree = proto_item_add_subtree( ti, ett_dx );
		offset = 0;

		proto_tree_add_item( dx_tree, hf_dx_report, tvb, offset, data_len, ENC_ASCII );
	}

	return tvb_captured_length(tvb);
}

static bool
isaprs( uint8_t dti )
{
	bool b = false;

	switch ( dti )
		{
		case 0x1c	:
		case 0x1d	:
		case '!'	:
		case '#'	:
		case '$'	:
		case '%'	:
		case '&'	:
		case ')'	:
		case '*'	:
		case '+'	:
		case ','	:
		case '.'	:
		case '/'	:
		case ':'	:
		case ';'	:
		case '<'	:
		case '='	:
		case '>'	:
		case '?'	:
		case '@'	:
		case 'T'	:
		case '['	:
		case '\''	:
		case '_'	:
		case '`'	:
		case '{'	:
		case '}'	: b = true; break;
		default		: break;
		}
	return b;
}

static int
dissect_ax25_nol3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data _U_ )
{
	proto_item *ti;
	proto_tree *ax25_nol3_tree;
	char       *info_buffer;
	int         offset;
	tvbuff_t   *next_tvb = NULL;
	uint8_t     dti      = 0;
	bool        dissected;

	info_buffer = (char *)wmem_alloc( pinfo->pool, STRLEN );
	info_buffer[0] = '\0';

	col_set_str( pinfo->cinfo, COL_PROTOCOL, "AX.25-NoL3");

	col_clear( pinfo->cinfo, COL_INFO);

	offset = 0;
	snprintf( info_buffer, STRLEN, "Text" );

	if ( gPREF_APRS )
		{
		dti = tvb_get_uint8( tvb, offset );
		if ( isaprs( dti ) )
			snprintf( info_buffer, STRLEN, "APRS" );
		}
	if ( gPREF_DX )
		{
		if ( tvb_get_uint8( tvb, offset ) == 'D' && tvb_get_uint8( tvb, offset + 1 ) == 'X' )
		snprintf( info_buffer, STRLEN, "DX cluster" );
		}

	col_add_str( pinfo->cinfo, COL_INFO, info_buffer );

	/* Call sub-dissectors here */

	/* create display subtree for the protocol */
	ti = proto_tree_add_protocol_format( parent_tree,
						proto_ax25_nol3,
						tvb,
						0,
						-1,
						"AX.25 No Layer 3 - (%s)", info_buffer );
	ax25_nol3_tree = proto_item_add_subtree( ti, ett_ax25_nol3 );

	next_tvb = tvb_new_subset_remaining(tvb, offset);
	dissected = false;
	if ( gPREF_APRS )
		{
		if ( isaprs( dti ) )
			{
			dissected = true;
			call_dissector( aprs_handle , next_tvb, pinfo, ax25_nol3_tree );
			}
		}
	if ( gPREF_DX )
		{
		if ( tvb_get_uint8( tvb, offset ) == 'D' && tvb_get_uint8( tvb, offset + 1 ) == 'X' )
			{
			dissected = true;
			dissect_dx( next_tvb, pinfo, ax25_nol3_tree, NULL );
			}
		}
	if ( ! dissected )
		call_data_dissector(next_tvb, pinfo, ax25_nol3_tree );

	return tvb_captured_length(tvb);
}

void
proto_register_ax25_nol3(void)
{
	module_t *ax25_nol3_module;

	/* Setup list of header fields */
#if 0 /* not used ? */
	static hf_register_info hf[] = {
		{ &hf_text,
			{ "Text",			"ax25_nol3.text",
			FT_STRING, BASE_NONE, NULL, 0x0,
			NULL, HFILL }
		},
	};
#endif

	static hf_register_info hf_dx[] = {
		{ &hf_dx_report,
			{ "DX",				"ax25_nol3.dx",
			FT_STRING, BASE_NONE, NULL, 0x0,
			"DX cluster", HFILL }
		},
	};

	/* Setup protocol subtree array */
	static int *ett[] = {
		&ett_ax25_nol3,
		&ett_dx,
	};

	/* Register the protocol name and description */
	proto_ax25_nol3 = proto_register_protocol("AX.25 no Layer 3", "AX.25 no L3", "ax25_nol3");

	/* Required function calls to register the header fields and subtrees used */
	/* proto_register_field_array( proto_ax25_nol3, hf, array_length(hf ) ); */
	proto_register_subtree_array( ett, array_length( ett ) );

	/* Register preferences module */
	ax25_nol3_module = prefs_register_protocol( proto_ax25_nol3, NULL);

	/* Register any preference */
	prefs_register_bool_preference(ax25_nol3_module, "showaprs",
	     "Decode the APRS info field",
	     "Enable decoding of the payload as APRS.",
	     &gPREF_APRS );

	prefs_register_bool_preference(ax25_nol3_module, "showcluster",
	     "Decode DX cluster info field",
	     "Enable decoding of the payload as DX cluster info.",
	     &gPREF_DX );

	/* Register the sub-protocol name and description */
	proto_dx = proto_register_protocol("DX cluster", "DX", "dx");

	/* Register the dissector */
	register_dissector( "dx", dissect_dx, proto_dx);

	/* Register the header fields */
	proto_register_field_array( proto_dx, hf_dx, array_length( hf_dx ) );

	/* Register the subtrees used */
	/* proto_register_subtree_array( ett_dx, array_length( ett_dx ) ); */
}

void
proto_reg_handoff_ax25_nol3(void)
{
	dissector_add_uint( "ax25.pid", AX25_P_NO_L3, create_dissector_handle( dissect_ax25_nol3, proto_ax25_nol3 ) );

	/*
	 */
	aprs_handle     = find_dissector_add_dependency( "aprs", proto_ax25_nol3 );
}

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