File: rx25.c

package info (click to toggle)
fbb 7.07-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,244 kB
  • ctags: 8,123
  • sloc: ansic: 87,810; sh: 2,324; makefile: 297
file content (500 lines) | stat: -rw-r--r-- 10,557 bytes parent folder | download | duplicates (3)
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
   /****************************************************************
    Copyright (C) 1986-2000 by

    F6FBB - Jean-Paul ROUBELAT
    6, rue George Sand
    31120 - Roquettes - France
	jpr@f6fbb.org

    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

    Parts of code have been taken from many other softwares.
    Thanks for the help.
    ****************************************************************/

/*
 * RX25.C : Decodage de trames au format ROSE X25
 *
 * D'apres KD.BAS de F1EBN
 *
 * FC1OAT@F6ABJ - 920820
 */

#include <serv.h>

/******************************************************************************
 * Dump ASCII avec Filtrage des caracteres non affichables
 ******************************************************************************/

static int decode_x25 (uchar *, int, char *);

static void decode_X121 (uchar *, int, char *);
static void decode_digi (uchar *, int, char *);
static void dump_hexa (uchar *, int, char *);

void put_rose (uchar * texte, int attr, int nbcar)
{
	uchar chaine[400];
	uchar *ptr = chaine;

	int decode = 1;				/* A patcher, au cas ou ... */

	if (decode)
	{

		if (nbcar > 256)
			nbcar = 256;

		strcpy (ptr, "X25 : ");
		ptr += 6;

		nbcar = 6 + decode_x25 (texte, nbcar, ptr);
	}
	else
		memcpy (chaine, texte, nbcar);


	put_ui (chaine, attr, nbcar);

}

/******************************************************************************
 * Les Causes
 *****************************************************************************/

static char *ResetCause (int cause)
{
	static char hex_cause[3];

	/* From ISO 8208 page 48 */

	switch (cause)
	{
	case 0:
		return "DTE Originated";
	case 0xC1:
		return "Gateway-detected Procedure Error";
	case 0xC3:
		return "Gateway Congestion";
	case 0xC7:
		return "Gateway Operational";
	default:
		switch (cause & 0x7F)
		{
		case 0x01:
			return "Out Of Order";
		case 0x03:
			return "Remote Procedure Error";
		case 0x05:
			return "Local Procedure Error";
		case 0x07:
			return "Network Congestion";
		case 0x09:
			return "Remote DTE Operational";
		case 0x0F:
			return "Network Operational";
		case 0x11:
			return "Incompatible Destination";
		case 0x1D:
			return "Network Out Of Order";
		default:
			sprintf (hex_cause, "%2.2X", cause);
			return hex_cause;
		}
	}
}

static char *ClearCause (int cause)
{
	static char hex_cause[3];

	/* From ISO 8208 page 43 */

	switch (cause)
	{
	case 0:
		return "DTE Originated";
	case 0xC1:
		return "Gateway-detected Procedure Error";
	case 0xC3:
		return "Gateway Congestion";
	case 0xC7:
		return "Gateway Operational";
	default:
		switch (cause & 0x7F)
		{
		case 0x01:
			return "Number Busy";
		case 0x09:
			return "Out Of Order";
		case 0x11:
			return "Remote Procedure Error";
		case 0x19:
			return "Reverse Charging Acceptance Not Subscribed";
		case 0x21:
			return "Incompatible Destination";
		case 0x29:
			return "Fast Select Acceptance Not Subscribed";
		case 0x39:
			return "Ship Absent";
		case 0x03:
			return "Invalid Facility Requested";
		case 0x0B:
			return "Access Barred";
		case 0x13:
			return "Local Procedure Error";
		case 0x05:
			return "Network Congestion";
		case 0x0D:
			return "Not Obtainable";
		case 0x15:
			return "RPOA Out Of Order";
		default:
			sprintf (hex_cause, "%2.2X", cause);
			return hex_cause;
		}
	}
}


static char *RestartCause (int cause)
{
	static char hex_cause[3];

	/* From ISO 8208 page 50 */

	switch (cause)
	{
	case 0:
		return "DTE Originated";
	case 1:
		return "Local Procedure Error";
	case 3:
		return "Network Congestion";
	case 7:
		return "Network Operational";
	case 0x7F:
		return "Registration/Cancellation Confirmed";
	default:
		sprintf (hex_cause, "%2.2X", cause);
		return hex_cause;
	}
}

/******************************************************************************
 * Decodage de trames X25
 *****************************************************************************/

/* Decodage d'une adresse X.121 */

static void decode_X121 (uchar * data, int lgaddr, char *result)
{
	char bcd;
	int pfo, pfa;

	while (lgaddr > 0)
	{
		pfo = (*data & 0xF0) >> 4;
		pfa = *data & 0x0F;
		bcd = pfo + '0';
		*result++ = bcd;
		if (--lgaddr == 0)
			break;
		bcd = pfa + '0';
		*result++ = bcd;
		lgaddr--;
		data++;
	}

	*result = '\0';
}

/* Dump Hexa */

static void dump_hexa (uchar * data, int l_data, char *result)
{
	int i;

	i = 0;
	while (l_data-- > 0)
	{
		sprintf (result, "%2.2X ", *data++);
		result += 3;
		if (++i == 16)
		{
			*result++ = '\r';
			i = 0;
		}
	}

	*result = '\0';
}

static char *rs_addr(char *addr)
{
	static char str[20];
	
	strncpy(str, addr, 4);
	str[4] = ',';
	strncpy(str+5, addr+4, 6);
	str[11] = '\0';
	
	return str;
}

/* Decodage d'un champ digi */

static void decode_digi (uchar * data, int l_data, char *result)
{
	while (l_data-- > 1)
	{
		*result++ = *data++ >> 1;
	}

	*result++ = '-';
	*result++ = (*data & 0x7F) >> 1;
	*result = 0;
}

/* Decodage d'une suite de champs facilite */

static void decodage_facilite (uchar * data, char *result)
{
	int lgfac, lg, fct, lgdigi, lgaddcall;
	int lgad, lgaddr, lgadind;
	static char digis[10], digid[10], icd[10];
	static char indorig[10], inddest[10];
	static char addstorig[20], addstdest[20];

	lgfac = *data++;
	lg = lgfac;

	digid[0] = digis[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:
			/* Utilise si appel par call digi au lieu du call N3 */
			sprintf (result, "\rFacility 3F%2.2X", *data++);
			lg--;
			break;
		case 0x7F:
			/* Nombre aleatoire pour eviter les rebouclages */
			sprintf (result, " NbAlea: %2.2X%2.2X",
					 *data, *(data + 1));
			data += 2;
			lg -= 2;
			break;
		case 0xE9:
			/* Digi destination */
			lgdigi = *data++;
			decode_digi (data, lgdigi, digid);
			data += lgdigi;
			lg -= 1 + lgdigi;
			break;
		case 0xEB:
			/* Digi origine */
			lgdigi = *data++;
			decode_digi (data, lgdigi, digis);
			data += lgdigi;
			lg -= 1 + lgdigi;
			break;
		case 0xC9:
			/* Adresse et indicatif du nodal destination */
		case 0xCB:
			/* Adresse et indicatif du nodal origine */
			lgaddcall = *data++;
			data++;
			dump_hexa (data, 3, icd);
			data += 3;
			lgad = *data++;
			lg -= 6;
			lgaddr = lgad;
			if (fct == 0xCB)
				decode_X121 (data, lgaddr, addstorig);
			else
				decode_X121 (data, lgaddr, addstdest);
			data += (lgad + 1) / 2;
			lg -= (lgad + 1) / 2;
			lgadind = lgaddcall - (lgad + 1) / 2 - 5;
			if (fct == 0xCB)
				strncpy (indorig, data, lgadind);
			else
				strncpy (inddest, data, lgadind);
			data += lgadind;
			lg -= lgadind;
			break;
		default:
			sprintf (result, "\rUnknown Facility Type %2.2X", fct);
			lg = 0;
			break;
		}

		result += strlen (result);
	}

	sprintf (result, "\r%s@%s", indorig, rs_addr(addstorig));
	result += strlen (result);
	if (*digis)
		sprintf (result, " via %s", digis);
	strcat (result, " -> ");
	result += strlen (result);
	sprintf (result, "%s@%s", inddest, rs_addr(addstdest));
	result += strlen (result);
	if (*digid)
		sprintf (result, " via %s", digid);
}

/* Decodage des donnees accompagnant un CALL REQUEST */

static void decode_call_req (uchar * data, int l_data, char *result)
{
	int lgdest, lgorig, lgaddr;

	/* Decodage du champ adresse */

	lgdest = *(data + 3) & 0x0F;
	lgorig = (*(data + 3) & 0xF0) >> 4;
	lgaddr = lgorig + lgdest;
	/* 
	decode_X121 (data + 4 + (lgdest / 2), lgorig, result);
	strcat (result, " -> ");
	decode_X121 (data + 4, lgdest, result + strlen (result));
	*/
	data += 4 + (lgaddr / 2);
	l_data -= 4 + (lgaddr / 2);
	/* result += strlen (result); */

	/* Decodage des champs facilite */

	decodage_facilite (data, result);
}

/* Point d'entree du decodage des trames X25 */

static int decode_x25 (uchar * data, int l_data, char *result)
{
	int tpaq, id_format, r, s, lci, lg;

	char *ptemp = result;


	lci = ((*data & 0x0f) << 4) | *(data + 1);
	id_format = *data;
	tpaq = *(data + 2);
	r = (tpaq & 0xE0) >> 5;
	s = (tpaq & 0x0E) >> 1;

	sprintf (result, "LCI %03d : ", lci);
	result += strlen (result);

	if (id_format & 0x80)
		strcat (result, "Q ");
	if (id_format & 0x40)
		strcat (result, "D ");

	if (tpaq & 1)
	{
		switch (tpaq)
		{
		case 0x0B:
			sprintf (result, "CALL REQUEST ");
			result += strlen (result);
			decode_call_req (data, l_data, result);
			break;
		case 0x0F:
			sprintf (result, "CALL ACCEPTED");
			break;
		case 0x13:
			sprintf (result,
					 "CLEAR REQUEST - Cause %s - Diag %d",
					 ClearCause (*(data + 3)), *(data + 4));
			break;
		case 0x17:
			sprintf (result, "CLEAR CONFIRMATION");
			break;
		case 0x23:
			sprintf (result, "INTERRUPT");
			break;
		case 0x27:
			sprintf (result, "INTERRUPT CONFIRMATION");
			break;
		case 0x1B:
			sprintf (result,
					 "RESET REQUEST - Cause %s - Diag %d",
					 ResetCause (*(data + 3)), *(data + 4));
			break;
		case 0x1F:
			sprintf (result, "RESET CONFIRMATION");
			break;
		case 0xF3:
			sprintf (result, "REGISTRATION REQUEST");
			break;
		case 0xF7:
			sprintf (result, "REGISTRATION CONFIRMATION");
			break;
		case 0xFB:
			sprintf (result,
					 "RESTART REQUEST - Cause %s - Diag %d",
					 RestartCause (*(data + 3)), *(data + 4));
			break;
		case 0xFF:
			sprintf (result, "RESTART CONFIRMATION");
			break;
		default:
			switch (tpaq & 0x0F)
			{
			case 0x01:
				sprintf (result, "RR R%d", r);
				break;
			case 0x05:
				sprintf (result, "RNR R%d", r);
				break;
			case 0x09:
				sprintf (result, "REJ R%d", r);
				break;
			}
			break;
		}
		l_data = 0;
		lg = strlen (ptemp);
	}
	else
	{
		l_data -= 3;

		if (tpaq & 0x10)
			strcat (result, "M ");
		sprintf (result, "DATA R%d S%d L=%d", r, s, l_data);
		strcat (result, "\r");
		lg = strlen (ptemp);

		result += strlen (result);
		if (l_data < 0)
			l_data = 0;
		memcpy (result, data + 3, l_data);
	}

	return (lg + l_data);
}