comparison rvinterf/libg23/fmtdispatch.c @ 332:28b4d3c9e85d

rvinterf/libg23: complete for now
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 22 Apr 2014 05:03:39 +0000
parents
children 42c91c51ca7f
comparison
equal deleted inserted replaced
331:91ea7a4a0b4d 332:28b4d3c9e85d
1 /*
2 * This libg23 module exports the format_g23_packet() function, which
3 * validates the packet, then dispatches it to format_g23_trace(),
4 * format_g23_sysprim() or format_g23_psprim() as appropriate, or
5 * prints it in raw hex if malformed.
6 */
7
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <strings.h>
12
13 static int
14 basic_checks(rxpkt, rxpkt_len)
15 u_char *rxpkt;
16 {
17 int i, c;
18
19 if (rxpkt_len < 17)
20 return(0);
21 /* check version bits in the header byte */
22 if ((rxpkt[1] & 0xC0) != 0x80)
23 return(0);
24 /* check the length */
25 c = rxpkt[2] | rxpkt[3] << 8;
26 if (c + 4 != rxpkt_len)
27 return(0);
28 /* ensure that the "from" and "to" are printable ASCII */
29 for (i = 8; i < 16; i++) {
30 c = rxpkt[i];
31 if (c < ' ' || c > '~')
32 return(0);
33 }
34 /* basic checks pass */
35 return(1);
36 }
37
38 static int
39 psprim_extra_checks(rxpkt, rxpkt_len)
40 u_char *rxpkt;
41 {
42 int i, c;
43
44 if (rxpkt_len < 24)
45 return(0);
46 /* "original rcvr" field needs to be printable ASCII */
47 for (i = 16; i < 20; i++) {
48 c = rxpkt[i];
49 if (c < ' ' || c > '~')
50 return(0);
51 }
52 /* checks pass */
53 return(1);
54 }
55
56 static void
57 print_malformed(rxpkt, rxpkt_len, outbuf)
58 u_char *rxpkt;
59 char *outbuf;
60 {
61 int i;
62 char *dp;
63
64 dp = outbuf;
65 strcpy(dp, "G23 UNK:");
66 dp += 8;
67 for (i = 1; i < rxpkt_len; i++) {
68 sprintf(dp, " %02X", rxpkt[i]);
69 dp += 3;
70 }
71 *dp = '\0';
72 }
73
74 void
75 format_g23_packet(rxpkt, rxpkt_len, outbuf)
76 u_char *rxpkt;
77 char *outbuf;
78 {
79 if (!basic_checks(rxpkt, rxpkt_len)) {
80 print_malformed(rxpkt, rxpkt_len, outbuf);
81 return;
82 }
83 /* dispatch by type */
84 switch (rxpkt[1] & 0x30) {
85 case 0x10:
86 /* PS primitive */
87 if (psprim_extra_checks(rxpkt, rxpkt_len))
88 format_g23_psprim(rxpkt, rxpkt_len, outbuf);
89 else
90 print_malformed(rxpkt, rxpkt_len, outbuf);
91 return;
92 case 0x20:
93 /* trace */
94 format_g23_trace(rxpkt, rxpkt_len, outbuf);
95 return;
96 case 0x30:
97 /* system primitive */
98 format_g23_sysprim(rxpkt, rxpkt_len, outbuf);
99 return;
100 default:
101 print_malformed(rxpkt, rxpkt_len, outbuf);
102 }
103 }