-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx224.c
177 lines (151 loc) · 2.69 KB
/
x224.c
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
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
/*
* examine a packet header
* returns 1 if it's a TPKT-encapsulated TPDU (T.123 clause 8; RFC 1006)
* returns 0 if not - likely a Fast-Path Update PDU ([MS-RDPBCGR] 5.3.8 and 5.4.4)
*/
int
istpkt(uchar* p, uchar* ep)
{
int magic;
if(p+1>ep){
werrstr(Eshort);
return -1;
}
magic = p[0];
return (magic == 3);
}
int
tptype(uchar* p, uchar* ep)
{
if(p+5 >= ep){
werrstr(Eshort);
return -1;
}
return p[5];
}
/*
* read a PDU: either TPKT-encapsulated TPDU or Fast-Path Update PDU
*/
int
readpdu(int fd, uchar *buf, uint nbuf)
{
int n, len;
uchar *p;
p = buf;
n = readn(fd, p, TPKTFIXLEN);
if(n != TPKTFIXLEN){
werrstr("short read: %r");
return -1;
}
switch(istpkt(p, p+n)){
case -1:
return -1;
case 0:
/* Fast-Path Update PDU */
len = p[1];
if(len&(1<<7))
len = ((len^(1<<7))<<8) | p[2];
break;
default:
/* TPKT-encapsulated TPDU */
len = nhgets(p+2);
}
if(len <= n || len > nbuf){
werrstr("bad length in PDU header: %d", len);
return -1;
}
n += readn(fd, p+n, len-n);
if(n != len)
return -1;
return n;
}
uchar*
tpdat(uchar* p, uchar* ep)
{
uchar* q;
if(istpkt(p, ep) == 0){
werrstr("Fast-Path Update PDU is not expected");
return nil;
}
if(tptype(p,ep) == Data)
q = p+7;
else
q = p+11;
if(q > ep){
werrstr(Eshort);
return nil;
}
return q;
}
/* connect request */
int
mktpcr(uchar* buf, int nbuf, int ndata)
{
int size;
uchar *p;
p = buf;
size = TPKTFIXLEN+7+ndata;
if(size > nbuf){
werrstr(Esmall);
return -1;
}
/* TPKT header: version[1] unused[1] len[2] */
p[0] = 0x03;
p[1] = 0;
hnputs(p+2, size);
/* ConReq: hdlen[1] type[1] dstref[2] srcref[2] class[1] */
p[4+0] = 7-1+ndata;
p[4+1] = ConReq;
hnputs(p+4+2, 0);
hnputs(p+4+4, 0);
p[4+6] = 0;
return size;
}
/* data transfer */
int
mktpdat(uchar* buf, int nbuf, int ndata)
{
int size;
uchar *p;
p = buf;
size = TPDATAFIXLEN+ndata;
if(size > nbuf){
werrstr("buffer too small: provided %d need %d", nbuf, size);
return -1;
}
/* TPKT header: version[1] unused[1] len[2] */
p[0] = 0x03;
p[1] = 0;
hnputs(p+2, size);
/* TPDU: hdlen[1] type[1] seqno[1] */
p[4] = 2;
p[5] = Data;
p[6] = (1<<7); /* seqno (0 in Class 0) + EOT mark (1<<7) */
return size;
}
/* disconnection request */
int
mktpdr(uchar* buf, int nbuf, int ndata)
{
int size;
uchar *p;
p = buf;
size = TPDATAFIXLEN+ndata;
if(size > nbuf){
werrstr("buffer too small");
return -1;
}
/* TPKT header: version[1] unused[1] len[2] */
p[0] = 0x03;
p[1] = 0;
hnputs(p+2, size);
/* HupReq: hdlen[1] type[1] seqno[1] */
p[4] = 2;
p[5] = HupReq;
p[6] = (1<<7); /* seqno (0 in Class 0) + EOT mark (1<<7) */
return size;
}