File: rosedump.c

package info (click to toggle)
ax25-apps 0.0.8-rc4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,168 kB
  • sloc: sh: 11,124; ansic: 10,264; makefile: 287
file content (470 lines) | stat: -rw-r--r-- 10,719 bytes parent folder | download
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright 1996 Jonathan Naylor G4KLX
 *
 * Added ROSE new facilities : Jean-Paul ROUBELAT 04-1998
 * Added ROSE multi-digi     : Jean-Paul ROUBELAT 05-1998
 * Added ROSE PID transport  : Jean-Paul ROUBELAT 07-1998
 */

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

#define	PID_SEGMENT	0x08
#define	PID_ARP		0xCD
#define	PID_NETROM	0xCF
#define	PID_IP		0xCC
#define	PID_X25		0x01
#define	PID_TEXNET	0xC3
#define	PID_FLEXNET	0xCE
#define	PID_NO_L3	0xF0

#define	ROSE_ADDR_LEN			5

#define	CALL_REQUEST			0x0B
#define	CALL_ACCEPTED			0x0F
#define	CLEAR_REQUEST			0x13
#define	CLEAR_CONFIRMATION		0x17
#define	INTERRUPT			0x23
#define	INTERRUPT_CONFIRMATION		0x27
#define	RESET_REQUEST			0x1B
#define	RESET_CONFIRMATION		0x1F
#define	RESTART_REQUEST			0xFB
#define	RESTART_CONFIRMATION		0xFF
#define	REGISTRATION_REQUEST		0xF3
#define	REGISTRATION_CONFIRMATION	0xF7
#define	DIAGNOSTIC			0xF1
#define	RR				0x01
#define	RNR				0x05
#define	REJ				0x09
#define	DATA				0x00

#define	QBIT				0x80
#define	DBIT				0x40
#define	MBIT				0x10

#define	AX25_HBIT			0x80

static char *dump_x25_addr(unsigned char *);
static char *clear_code(unsigned char);
static char *reset_code(unsigned char);
static char *restart_code(unsigned char);
static void facility(unsigned char *, int len);

void rose_dump(unsigned char *data, int length, int hexdump)
{
	unsigned int len, hlen;
	unsigned int lci = ((unsigned) (data[0] & 0x0F) << 8) + data[1];
	lprintf(T_ROSEHDR, "X.25: LCI %3.3X : ", lci);

	switch (data[2]) {
	case CALL_REQUEST:
		len = 4;
		hlen = (((data[3] >> 4) & 0x0F) + 1) / 2;
		hlen += (((data[3] >> 0) & 0x0F) + 1) / 2;
		len += hlen;
		data += len;
		length -= len;
		lprintf(T_ROSEHDR, "CALL REQUEST - ");
		if (length) {
			unsigned int flen = data[0] + 1;
			facility(data, length);
			length -= flen;
			data += flen;
			if (length > 0)
				data_dump(data, length, 1);
		} else {
			lprintf(T_ROSEHDR, "\n");
		}
		return;

	case CALL_ACCEPTED:
		lprintf(T_ROSEHDR, "CALL ACCEPTED\n");
		return;

	case CLEAR_REQUEST:
		lprintf(T_ROSEHDR, "CLEAR REQUEST - Cause %s - Diag %d\n",
			clear_code(data[3]), data[4]);
		if (length > 6) {
			facility(data + 6, length - 6);
		}
		return;

	case CLEAR_CONFIRMATION:
		lprintf(T_ROSEHDR, "CLEAR CONFIRMATION\n");
		return;

	case DIAGNOSTIC:
		lprintf(T_ROSEHDR, "DIAGNOSTIC - Diag %d\n", data[3]);
		return;

	case INTERRUPT:
		lprintf(T_ROSEHDR, "INTERRUPT\n");
		data_dump(data + 3, length - 3, hexdump);
		return;

	case INTERRUPT_CONFIRMATION:
		lprintf(T_ROSEHDR, "INTERRUPT CONFIRMATION\n");
		return;

	case RESET_REQUEST:
		lprintf(T_ROSEHDR, "RESET REQUEST - Cause %s - Diag %d\n",
			reset_code(data[3]), data[4]);
		return;

	case RESET_CONFIRMATION:
		lprintf(T_ROSEHDR, "RESET CONFIRMATION\n");
		return;

	case RESTART_REQUEST:
		lprintf(T_ROSEHDR,
			"RESTART REQUEST - Cause %s - Diag %d\n",
			restart_code(data[3]), data[4]);
		return;

	case RESTART_CONFIRMATION:
		lprintf(T_ROSEHDR, "RESTART CONFIRMATION\n");
		return;

	case REGISTRATION_REQUEST:
		lprintf(T_ROSEHDR, "REGISTRATION REQUEST\n");
		return;

	case REGISTRATION_CONFIRMATION:
		lprintf(T_ROSEHDR, "REGISTRATION CONFIRMATION\n");
		return;
	}

	if ((data[2] & 0x01) == DATA) {
		lprintf(T_ROSEHDR, "DATA R%d S%d %s%s%s",
			(data[2] >> 5) & 0x07, (data[2] >> 1) & 0x07,
			(data[0] & QBIT) ? "Q" : "",
			(data[0] & DBIT) ? "D" : "",
			(data[2] & MBIT) ? "M" : "");
		if ((length >= 5) && (data[0] & QBIT) && (data[3] == 0x7f)) {
			/* pid transport */
			int pid = data[4];
			data += 5;
			length -= 5;
			switch (pid) {
				case PID_SEGMENT:
					lprintf(T_ROSEHDR," len %d\n", length - 5);
					data_dump(data, length, hexdump);
					break;
				case PID_ARP:
					lprintf(T_ROSEHDR," pid=ARP len %d\n", length - 5);
					arp_dump(data, length);
					break;
				case PID_NETROM:
					lprintf(T_ROSEHDR," pid=NET/ROM len %d\n", length - 5);
					netrom_dump(data, length, hexdump, 0);
					break;
				case PID_IP:
					lprintf(T_ROSEHDR," pid=IP len %d\n", length - 5);
					ip_dump(data, length, hexdump);
					break;
				case PID_X25:
					lprintf(T_ROSEHDR, " pid=X.25 len %d\n", length - 5);
					rose_dump(data, length, hexdump);
					break;
				case PID_TEXNET:
					lprintf(T_ROSEHDR, " pid=TEXNET len %d\n", length - 5);
					data_dump(data, length, hexdump);
					break;
				case PID_FLEXNET:
					lprintf(T_ROSEHDR, " pid=FLEXNET len %d\n", length - 5);
					flexnet_dump(data, length, hexdump);
					break;
				case PID_NO_L3:
					lprintf(T_ROSEHDR, " pid=Text len %d\n", length - 5);
					data_dump(data, length, hexdump);
					break;
				default:
					lprintf(T_ROSEHDR, " pid=0x%x len %d\n", pid, length - 5);
					data_dump(data, length, hexdump);
					break;
			}
		} else {
			lprintf(T_ROSEHDR, " len %d\n", length - 3);
			data_dump(data + 3, length - 3, hexdump);
		}
		return;
	}

	switch (data[2] & 0x1F) {
	case RR:
		lprintf(T_ROSEHDR, "RR R%d\n", (data[2] >> 5) & 0x07);
		return;
	case RNR:
		lprintf(T_ROSEHDR, "RNR R%d\n", (data[2] >> 5) & 0x07);
		return;
	case REJ:
		lprintf(T_ROSEHDR, "REJ R%d\n", (data[2] >> 5) & 0x07);
		return;
	}

	lprintf(T_ROSEHDR, "UNKNOWN\n");
	data_dump(data, length, 1);
}

static char *clear_code(unsigned char code)
{
	static char buffer[25];

	if (code == 0x00 || (code & 0x80) == 0x80)
		return "DTE Originated";
	if (code == 0x01)
		return "Number Busy";
	if (code == 0x09)
		return "Out Of Order";
	if (code == 0x11)
		return "Remote Procedure Error";
	if (code == 0x19)
		return "Reverse Charging Acceptance Not Subscribed";
	if (code == 0x21)
		return "Incompatible Destination";
	if (code == 0x29)
		return "Fast Select Acceptance Not Subscribed";
	if (code == 0x39)
		return "Destination Absent";
	if (code == 0x03)
		return "Invalid Facility Requested";
	if (code == 0x0B)
		return "Access Barred";
	if (code == 0x13)
		return "Local Procedure Error";
	if (code == 0x05)
		return "Network Congestion";
	if (code == 0x0D)
		return "Not Obtainable";
	if (code == 0x15)
		return "RPOA Out Of Order";

	sprintf(buffer, "Unknown %02X", code);

	return buffer;
}

static char *reset_code(unsigned char code)
{
	static char buffer[25];

	if (code == 0x00 || (code & 0x80) == 0x80)
		return "DTE Originated";
	if (code == 0x03)
		return "Remote Procedure Error";
	if (code == 0x11)
		return "Incompatible Destination";
	if (code == 0x05)
		return "Local Procedure Error";
	if (code == 0x07)
		return "Network Congestion";

	sprintf(buffer, "Unknown %02X", code);

	return buffer;
}

static char *restart_code(unsigned char code)
{
	static char buffer[25];

	if (code == 0x00 || (code & 0x80) == 0x80)
		return "DTE Originated";
	if (code == 0x01)
		return "Local Procedure Error";
	if (code == 0x03)
		return "Network Congestion";
	if (code == 0x07)
		return "Network Operational";

	sprintf(buffer, "Unknown %02X", code);

	return buffer;
}

static char *dump_x25_addr(unsigned char *data)
{
	static char buffer[25];

	sprintf(buffer, "%02X%02X,%02X%02X%02X", data[0], data[1], data[2],
		data[3], data[4]);

	return buffer;
}

static char *dump_ax25_call(unsigned char *data, int l_data)
{
	static char buffer[25];
	char *ptr = buffer;
	int ssid;

	while (l_data-- > 1) {
		*ptr = *data++ >> 1;
		if (*ptr != ' ')
			++ptr;
	}

	*ptr++ = '-';
	ssid = (*data & 0x1F) >> 1;
	if (ssid >= 10) {
		*ptr++ = '1';
		ssid -= 10;
	}
	*ptr++ = ssid + '0';
	*ptr = 0;

	return buffer;
}

static void facility(unsigned char *data, int lgtot)
{
	int lgfac, l, lg, fct, lgdigi, lgaddcall;
	int lgad, lgadind, digi_fac;
	char digis[80], digid[80];
	char indorig[10], inddest[10];
	char addstorig[20], addstdest[20];
	char *d, *factot;
	char buf[512];
	char *result = buf;

	factot = data;

	lgfac = *data++;
	lg = lgfac;

	digi_fac = 0;
	digid[0] = digis[0] = '\0';
	indorig[0] = inddest[0] = '\0';

	while (lg > 0) {
		fct = *data++;
		lg--;
		switch (fct) {
		case 0:
			/* Marker=0 National Fac ou Marker=15 CCITT */
			data++;
			lg--;
			break;
		case 0x3F:
			/* Used if call request via L2 digi instead of L3 */
			lprintf(T_ROSEHDR, "Facility 3F%2.2X\n", *data++);
			lg--;
			break;
		case 0x7F:
			/* Random number to avoid loops */
			lprintf(T_ROSEHDR, "NbAlea: %2.2X%2.2X\n", *data,
				*(data + 1));
			data += 2;
			lg -= 2;
			break;
		case 0xE9:
			/* Destination digi (for compatibility) */
			lgdigi = *data++;
			if (!digi_fac)
				strcpy(digid,
				       dump_ax25_call(data, lgdigi));
			data += lgdigi;
			lg -= 1 + lgdigi;
			break;
		case 0xEB:
			/* Origin digi (for compatibility) */
			lgdigi = *data++;
			if (!digi_fac)
				strcpy(digis,
				       dump_ax25_call(data, lgdigi));
			data += lgdigi;
			lg -= 1 + lgdigi;
			break;
		case 0xED:
			/* Out of order : callsign */
			lgaddcall = *data++;
			lprintf(T_ROSEHDR, "at %s",
				dump_ax25_call(data, lgaddcall));
			data += lgaddcall;
			lg -= 1 + lgaddcall;
			break;
		case 0xEE:
			/* Out of order : address */
			lgaddcall = *data++;
			++data;	/* Don't know... */
			lprintf(T_ROSEHDR, " @%s\n", dump_x25_addr(data));
			/* data_dump(data, lgaddcall, 1); */
			data += lgaddcall;
			lg -= 1 + lgaddcall;
			break;
		case 0xEF:
			lgaddcall = *data++;
			for (d = data, l = 0; l < lgaddcall;
			     l += 7, d += 7) {
				if (l > (6 * 7)) {
					/* 6 digis maximum */
					break;
				}
				if (d[6] & AX25_HBIT) {
					strcat(digis,
					       dump_ax25_call(d, 7));
					strcat(digis, " ");
				} else {
					strcat(digid,
					       dump_ax25_call(d, 7));
					strcat(digid, " ");
				}
			}
			data += lgaddcall;
			lg -= 1 + lgaddcall;
			digi_fac = 1;
			break;
		case 0xC9:
			/* Address and callsign of the remote node */
		case 0xCB:
			/* Address and callsign of the local node */
			lgaddcall = *data++;
			data++;
			data += 3;
			lgad = *data++;
			lg -= 6;

			if (fct == 0xCB)
				strcpy(addstorig, dump_x25_addr(data));
			else
				strcpy(addstdest, dump_x25_addr(data));

			data += (lgad + 1) / 2;
			lg -= (lgad + 1) / 2;
			lgadind = lgaddcall - (lgad + 1) / 2 - 5;

			if (fct == 0xCB) {
				strncpy(indorig, data, lgadind);
				indorig[lgadind] = '\0';
			} else {
				strncpy(inddest, data, lgadind);
				inddest[lgadind] = '\0';
			}

			data += lgadind;
			lg -= lgadind;
			break;
		default:
			lprintf(T_ROSEHDR, "Unknown Facility Type %2.2X\n",
				fct);
			data_dump(factot, lgtot, 1);
			lg = 0;
			break;
		}

		result += strlen(result);
	}

	if (*indorig && *inddest) {
		/* Build the displayed string */
		lprintf(T_ROSEHDR, "fm %-9s @%s", indorig, addstorig);
		if (*digis)
			lprintf(T_ROSEHDR, " via %s", digis);
		lprintf(T_ROSEHDR, "\n");
		lprintf(T_ROSEHDR, "to %-9s @%s", inddest, addstdest);
		if (*digid)
			lprintf(T_ROSEHDR, " via %s", digid);
		lprintf(T_ROSEHDR, "\n");
	}
}