-
Notifications
You must be signed in to change notification settings - Fork 1
/
vchan.c
156 lines (133 loc) · 2.19 KB
/
vchan.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
/*
* [MS-RDPBCGR] 3.1.5.2 Static Virtual Channels
* http://msdn.microsoft.com/en-us/library/cc240926.aspx
*/
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
enum
{
/* 2.2.1.3.4.1 Channel Definition Structure */
Inited= 1<<31,
/* 2.2.6.1 Virtual Channel PDU */
MTU= 1600,
/* 2.2.6.1.1 Channel PDU Header */
First= 1<<0,
Last= 1<<1,
Vis= 1<<4,
};
static Vchan vctab[] =
{
{
.mcsid = GLOBALCHAN+1, /* iota */
.name = "CLIPRDR",
.fn = clipvcfn,
.flags = Inited,
},
{
.mcsid = GLOBALCHAN+2,
.name = "RDPSND",
.fn = audiovcfn,
.flags = Inited,
},
};
static uint nvc = nelem(vctab);
void
initvc(Rdp* c)
{
c->vc = vctab;
c->nvc = nvc;
}
static Vchan*
lookupvc(int mcsid)
{
int i;
for(i=0; i<nvc; i++)
if(vctab[i].mcsid == mcsid)
return &vctab[i];
return nil;
}
static Vchan*
namevc(char* name)
{
int i;
for(i=0; i<nvc; i++)
if(strcmp(vctab[i].name, name) == 0)
return &vctab[i];
return nil;
}
int
defragvc(Rdp* c, Msg* m)
{
Vchan* vc;
int n;
USED(c);
vc = lookupvc(m->chanid);
if(vc == nil){
fprint(2, "defragvc: bad chanid\n");
return -1;
}
if(m->flags&First)
vc->pos = 0;
if(m->len > vc->nb){
vc->buf = erealloc(vc->buf, m->len);
vc->nb = m->len;
}
n = m->len - vc->pos;
if(n > m->ndata)
n = m->ndata;
memcpy(vc->buf+vc->pos, m->data, n);
vc->pos += n;
if(m->flags&Last){
m->data = vc->buf;
m->ndata = m->len;
return m->len;
}
return 0;
}
void
callvcfunc(Rdp *c, Msg* m)
{
Vchan* vc;
vc = lookupvc(m->chanid);
if(vc == nil || vc->fn==nil){
fprint(2, "unhandled virtual channel[%d] msg\n", m->chanid);
return;
}
vc->fn(c, m->data, m->ndata);
}
int
sendvc(Rdp* c, char* cname, uchar* a, int n)
{
int sofar, chunk;
Vchan* vc;
Msg t;
if(n < 0)
return -1;
vc = namevc(cname);
if(vc == nil){
werrstr("%s: no such vchannel", cname);
return -1;
}
if(vc->mcsid < 0)
return -1;
t.type = Mvchan;
t.originid = c->userchan;
t.chanid = vc->mcsid;
t.flags = First | Vis;
t.len = n;
t.data = a;
for(sofar=0; sofar<n; sofar += chunk){
chunk = n-sofar;
if(chunk > MTU)
chunk = MTU;
else
t.flags |= Last;
t.data = a+sofar;
t.ndata = chunk;
writemsg(c, &t);
t.flags &= ~First;
}
return n;
}