summaryrefslogtreecommitdiff
path: root/sys/src/cmd/vnc/proto.c
blob: 277525034bdaf1d9528720a8e916c2324f17e05b (plain)
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
#include "vnc.h"

#define SHORT(p) (((p)[0]<<8)|((p)[1]))
#define LONG(p) ((SHORT(p)<<16)|SHORT(p+2))

uchar zero[64];

Vnc*
vncinit(int fd, int cfd, Vnc *v)
{
        if(v == nil)
		v = mallocz(sizeof(*v), 1);
	Binit(&v->in, fd, OREAD);
	Binit(&v->out, fd, OWRITE);
	v->datafd = fd;
	v->ctlfd = cfd;
	return v;
}

void
vncterm(Vnc *v)
{
	Bterm(&v->out);
	Bterm(&v->in);
}

void
vncflush(Vnc *v)
{
	if(Bflush(&v->out) < 0){
		if(verbose > 1)
			fprint(2, "hungup while sending flush: %r\n");
		vnchungup(v);
	}
}

uchar
vncrdchar(Vnc *v)
{
	uchar buf[1];

	vncrdbytes(v, buf, 1);
	return buf[0];
}

ushort
vncrdshort(Vnc *v)
{
	uchar buf[2];

	vncrdbytes(v, buf, 2);
	return SHORT(buf);
}

ulong
vncrdlong(Vnc *v)
{
	uchar buf[4];

	vncrdbytes(v, buf, 4);
	return LONG(buf);
}

Point
vncrdpoint(Vnc *v)
{
	Point p;

	p.x = vncrdshort(v);
	p.y = vncrdshort(v);
	return p;
}

Rectangle
vncrdrect(Vnc *v)
{
	Rectangle r;

	r.min.x = vncrdshort(v);
	r.min.y = vncrdshort(v);
	r.max.x = r.min.x + vncrdshort(v);
	r.max.y = r.min.y + vncrdshort(v);
	return r;
}

Rectangle
vncrdcorect(Vnc *v)
{
	Rectangle r;

	r.min.x = vncrdchar(v);
	r.min.y = vncrdchar(v);
	r.max.x = r.min.x + vncrdchar(v);
	r.max.y = r.min.y + vncrdchar(v);
	return r;
}

void
vncrdbytes(Vnc *v, void *a, int n)
{
	if(Bread(&v->in, a, n) != n){
		if(verbose > 1)
			fprint(2, "hungup while reading\n");
		vnchungup(v);
	}
}

Pixfmt
vncrdpixfmt(Vnc *v)
{
	Pixfmt fmt;
	uchar pad[3];

	fmt.bpp = vncrdchar(v);
	fmt.depth = vncrdchar(v);
	fmt.bigendian = vncrdchar(v);
	fmt.truecolor = vncrdchar(v);
	fmt.red.max = vncrdshort(v);
	fmt.green.max = vncrdshort(v);
	fmt.blue.max = vncrdshort(v);
	fmt.red.shift = vncrdchar(v);
	fmt.green.shift = vncrdchar(v);
	fmt.blue.shift = vncrdchar(v);
	vncrdbytes(v, pad, 3);
	return fmt;
}

char*
vncrdstring(Vnc *v)
{
	ulong len;
	char *s;

	len = vncrdlong(v);
	s = malloc(len+1);
	assert(s != nil);

	vncrdbytes(v, s, len);
	s[len] = '\0';
	return s;
}

/*
 * on the server side of the negotiation protocol, we read
 * the client response and then run the negotiated function.
 * in some cases (e.g., TLS) the negotiated function needs to
 * use v->datafd directly and be sure that no data has been
 * buffered away in the Bio.  since we know the client is waiting
 * for our response, it won't have sent any until we respond.
 * thus we read the response with vncrdstringx, which goes
 * behind bio's back.
 */
char*
vncrdstringx(Vnc *v)
{
	char tmp[4];
	char *s;
	ulong len;

	assert(Bbuffered(&v->in) == 0);
	if(readn(v->datafd, tmp, 4) != 4){
		fprint(2, "cannot rdstringx: %r");
		vnchungup(v);
	}
	len = LONG(tmp);
	s = malloc(len+1);
	assert(s != nil);
	if(readn(v->datafd, s, len) != len){
		fprint(2, "cannot rdstringx len %lud: %r", len);
		vnchungup(v);
	}
	s[len] = '\0';
	return s;
}

void
vncwrstring(Vnc *v, char *s)
{
	ulong len;

	len = strlen(s);
	vncwrlong(v, len);
	vncwrbytes(v, s, len);
}

void
vncwrbytes(Vnc *v, void *a, int n)
{
	if(Bwrite(&v->out, a, n) < 0){
		if(verbose > 1) 
			fprint(2, "hungup while writing bytes\n");
		vnchungup(v);
	}
}

void
vncwrlong(Vnc *v, ulong u)
{
	uchar buf[4];

	buf[0] = u>>24;
	buf[1] = u>>16;
	buf[2] = u>>8;
	buf[3] = u;
	vncwrbytes(v, buf, 4);
}

void
vncwrshort(Vnc *v, ushort u)
{
	uchar buf[2];

	buf[0] = u>>8;
	buf[1] = u;
	vncwrbytes(v, buf, 2);
}

void
vncwrchar(Vnc *v, uchar c)
{
	vncwrbytes(v, &c, 1);
}

void
vncwrpixfmt(Vnc *v, Pixfmt *fmt)
{
	vncwrchar(v, fmt->bpp);
	vncwrchar(v, fmt->depth);
	vncwrchar(v, fmt->bigendian);
	vncwrchar(v, fmt->truecolor);
	vncwrshort(v, fmt->red.max);
	vncwrshort(v, fmt->green.max);
	vncwrshort(v, fmt->blue.max);
	vncwrchar(v, fmt->red.shift);
	vncwrchar(v, fmt->green.shift);
	vncwrchar(v, fmt->blue.shift);
	vncwrbytes(v, zero, 3);
}

void
vncwrrect(Vnc *v, Rectangle r)
{
	vncwrshort(v, r.min.x);
	vncwrshort(v, r.min.y);
	vncwrshort(v, r.max.x-r.min.x);
	vncwrshort(v, r.max.y-r.min.y);
}

void
vncwrpoint(Vnc *v, Point p)
{
	vncwrshort(v, p.x);
	vncwrshort(v, p.y);
}

void
vnclock(Vnc *v)
{
	qlock(v);
}

void
vncunlock(Vnc *v)
{
	qunlock(v);
}

void
hexdump(void *a, int n)
{
	uchar *p, *ep;

	p = a;
	ep = p+n;

	for(; p<ep; p++) 
		print("%.2ux ", *p);
	print("\n");
}

void
vncgobble(Vnc *v, long n)
{
	uchar buf[8192];
	long m;

	while(n > 0){
		m = n;
		if(m > sizeof(buf))
			m = sizeof(buf);
		vncrdbytes(v, buf, m);
		n -= m;
	}
}