root / vnc.c @ a7789382
History | View | Annotate | Download (76.9 kB)
1 |
/*
|
---|---|
2 |
* QEMU VNC display driver
|
3 |
*
|
4 |
* Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
5 |
* Copyright (C) 2006 Fabrice Bellard
|
6 |
* Copyright (C) 2009 Red Hat, Inc
|
7 |
*
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
10 |
* in the Software without restriction, including without limitation the rights
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
13 |
* furnished to do so, subject to the following conditions:
|
14 |
*
|
15 |
* The above copyright notice and this permission notice shall be included in
|
16 |
* all copies or substantial portions of the Software.
|
17 |
*
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 |
* THE SOFTWARE.
|
25 |
*/
|
26 |
|
27 |
#include "vnc.h" |
28 |
#include "sysemu.h" |
29 |
#include "qemu_socket.h" |
30 |
#include "qemu-timer.h" |
31 |
#include "acl.h" |
32 |
#include "qemu-objects.h" |
33 |
|
34 |
#define VNC_REFRESH_INTERVAL_BASE 30 |
35 |
#define VNC_REFRESH_INTERVAL_INC 50 |
36 |
#define VNC_REFRESH_INTERVAL_MAX 2000 |
37 |
|
38 |
#include "vnc_keysym.h" |
39 |
#include "d3des.h" |
40 |
|
41 |
#define count_bits(c, v) { \
|
42 |
for (c = 0; v; v >>= 1) \ |
43 |
{ \ |
44 |
c += v & 1; \
|
45 |
} \ |
46 |
} |
47 |
|
48 |
|
49 |
static VncDisplay *vnc_display; /* needed for info vnc */ |
50 |
static DisplayChangeListener *dcl;
|
51 |
|
52 |
static char *addr_to_string(const char *format, |
53 |
struct sockaddr_storage *sa,
|
54 |
socklen_t salen) { |
55 |
char *addr;
|
56 |
char host[NI_MAXHOST];
|
57 |
char serv[NI_MAXSERV];
|
58 |
int err;
|
59 |
size_t addrlen; |
60 |
|
61 |
if ((err = getnameinfo((struct sockaddr *)sa, salen, |
62 |
host, sizeof(host),
|
63 |
serv, sizeof(serv),
|
64 |
NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
|
65 |
VNC_DEBUG("Cannot resolve address %d: %s\n",
|
66 |
err, gai_strerror(err)); |
67 |
return NULL; |
68 |
} |
69 |
|
70 |
/* Enough for the existing format + the 2 vars we're
|
71 |
* substituting in. */
|
72 |
addrlen = strlen(format) + strlen(host) + strlen(serv); |
73 |
addr = qemu_malloc(addrlen + 1);
|
74 |
snprintf(addr, addrlen, format, host, serv); |
75 |
addr[addrlen] = '\0';
|
76 |
|
77 |
return addr;
|
78 |
} |
79 |
|
80 |
|
81 |
char *vnc_socket_local_addr(const char *format, int fd) { |
82 |
struct sockaddr_storage sa;
|
83 |
socklen_t salen; |
84 |
|
85 |
salen = sizeof(sa);
|
86 |
if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) |
87 |
return NULL; |
88 |
|
89 |
return addr_to_string(format, &sa, salen);
|
90 |
} |
91 |
|
92 |
char *vnc_socket_remote_addr(const char *format, int fd) { |
93 |
struct sockaddr_storage sa;
|
94 |
socklen_t salen; |
95 |
|
96 |
salen = sizeof(sa);
|
97 |
if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) |
98 |
return NULL; |
99 |
|
100 |
return addr_to_string(format, &sa, salen);
|
101 |
} |
102 |
|
103 |
static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa, |
104 |
socklen_t salen) |
105 |
{ |
106 |
char host[NI_MAXHOST];
|
107 |
char serv[NI_MAXSERV];
|
108 |
int err;
|
109 |
|
110 |
if ((err = getnameinfo((struct sockaddr *)sa, salen, |
111 |
host, sizeof(host),
|
112 |
serv, sizeof(serv),
|
113 |
NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
|
114 |
VNC_DEBUG("Cannot resolve address %d: %s\n",
|
115 |
err, gai_strerror(err)); |
116 |
return -1; |
117 |
} |
118 |
|
119 |
qdict_put(qdict, "host", qstring_from_str(host));
|
120 |
qdict_put(qdict, "service", qstring_from_str(serv));
|
121 |
|
122 |
return 0; |
123 |
} |
124 |
|
125 |
static int vnc_server_addr_put(QDict *qdict, int fd) |
126 |
{ |
127 |
struct sockaddr_storage sa;
|
128 |
socklen_t salen; |
129 |
|
130 |
salen = sizeof(sa);
|
131 |
if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { |
132 |
return -1; |
133 |
} |
134 |
|
135 |
return put_addr_qdict(qdict, &sa, salen);
|
136 |
} |
137 |
|
138 |
static int vnc_qdict_remote_addr(QDict *qdict, int fd) |
139 |
{ |
140 |
struct sockaddr_storage sa;
|
141 |
socklen_t salen; |
142 |
|
143 |
salen = sizeof(sa);
|
144 |
if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { |
145 |
return -1; |
146 |
} |
147 |
|
148 |
return put_addr_qdict(qdict, &sa, salen);
|
149 |
} |
150 |
|
151 |
static const char *vnc_auth_name(VncDisplay *vd) { |
152 |
switch (vd->auth) {
|
153 |
case VNC_AUTH_INVALID:
|
154 |
return "invalid"; |
155 |
case VNC_AUTH_NONE:
|
156 |
return "none"; |
157 |
case VNC_AUTH_VNC:
|
158 |
return "vnc"; |
159 |
case VNC_AUTH_RA2:
|
160 |
return "ra2"; |
161 |
case VNC_AUTH_RA2NE:
|
162 |
return "ra2ne"; |
163 |
case VNC_AUTH_TIGHT:
|
164 |
return "tight"; |
165 |
case VNC_AUTH_ULTRA:
|
166 |
return "ultra"; |
167 |
case VNC_AUTH_TLS:
|
168 |
return "tls"; |
169 |
case VNC_AUTH_VENCRYPT:
|
170 |
#ifdef CONFIG_VNC_TLS
|
171 |
switch (vd->subauth) {
|
172 |
case VNC_AUTH_VENCRYPT_PLAIN:
|
173 |
return "vencrypt+plain"; |
174 |
case VNC_AUTH_VENCRYPT_TLSNONE:
|
175 |
return "vencrypt+tls+none"; |
176 |
case VNC_AUTH_VENCRYPT_TLSVNC:
|
177 |
return "vencrypt+tls+vnc"; |
178 |
case VNC_AUTH_VENCRYPT_TLSPLAIN:
|
179 |
return "vencrypt+tls+plain"; |
180 |
case VNC_AUTH_VENCRYPT_X509NONE:
|
181 |
return "vencrypt+x509+none"; |
182 |
case VNC_AUTH_VENCRYPT_X509VNC:
|
183 |
return "vencrypt+x509+vnc"; |
184 |
case VNC_AUTH_VENCRYPT_X509PLAIN:
|
185 |
return "vencrypt+x509+plain"; |
186 |
case VNC_AUTH_VENCRYPT_TLSSASL:
|
187 |
return "vencrypt+tls+sasl"; |
188 |
case VNC_AUTH_VENCRYPT_X509SASL:
|
189 |
return "vencrypt+x509+sasl"; |
190 |
default:
|
191 |
return "vencrypt"; |
192 |
} |
193 |
#else
|
194 |
return "vencrypt"; |
195 |
#endif
|
196 |
case VNC_AUTH_SASL:
|
197 |
return "sasl"; |
198 |
} |
199 |
return "unknown"; |
200 |
} |
201 |
|
202 |
static int vnc_server_info_put(QDict *qdict) |
203 |
{ |
204 |
if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) { |
205 |
return -1; |
206 |
} |
207 |
|
208 |
qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
|
209 |
return 0; |
210 |
} |
211 |
|
212 |
static QDict *do_info_vnc_client(Monitor *mon, VncState *client)
|
213 |
{ |
214 |
QDict *qdict; |
215 |
|
216 |
qdict = qdict_new(); |
217 |
if (vnc_qdict_remote_addr(qdict, client->csock) < 0) { |
218 |
QDECREF(qdict); |
219 |
return NULL; |
220 |
} |
221 |
|
222 |
#ifdef CONFIG_VNC_TLS
|
223 |
if (client->tls.session &&
|
224 |
client->tls.dname) { |
225 |
qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
|
226 |
} |
227 |
#endif
|
228 |
#ifdef CONFIG_VNC_SASL
|
229 |
if (client->sasl.conn &&
|
230 |
client->sasl.username) { |
231 |
qdict_put(qdict, "username", qstring_from_str(client->sasl.username));
|
232 |
} |
233 |
#endif
|
234 |
|
235 |
return qdict;
|
236 |
} |
237 |
|
238 |
static void info_vnc_iter(QObject *obj, void *opaque) |
239 |
{ |
240 |
QDict *client; |
241 |
Monitor *mon = opaque; |
242 |
|
243 |
client = qobject_to_qdict(obj); |
244 |
monitor_printf(mon, "Client:\n");
|
245 |
monitor_printf(mon, " address: %s:%s\n",
|
246 |
qdict_get_str(client, "host"),
|
247 |
qdict_get_str(client, "service"));
|
248 |
|
249 |
#ifdef CONFIG_VNC_TLS
|
250 |
monitor_printf(mon, " x509_dname: %s\n",
|
251 |
qdict_haskey(client, "x509_dname") ?
|
252 |
qdict_get_str(client, "x509_dname") : "none"); |
253 |
#endif
|
254 |
#ifdef CONFIG_VNC_SASL
|
255 |
monitor_printf(mon, " username: %s\n",
|
256 |
qdict_haskey(client, "username") ?
|
257 |
qdict_get_str(client, "username") : "none"); |
258 |
#endif
|
259 |
} |
260 |
|
261 |
void do_info_vnc_print(Monitor *mon, const QObject *data) |
262 |
{ |
263 |
QDict *server; |
264 |
QList *clients; |
265 |
|
266 |
server = qobject_to_qdict(data); |
267 |
if (qdict_get_bool(server, "enabled") == 0) { |
268 |
monitor_printf(mon, "Server: disabled\n");
|
269 |
return;
|
270 |
} |
271 |
|
272 |
monitor_printf(mon, "Server:\n");
|
273 |
monitor_printf(mon, " address: %s:%s\n",
|
274 |
qdict_get_str(server, "host"),
|
275 |
qdict_get_str(server, "service"));
|
276 |
monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth")); |
277 |
|
278 |
clients = qdict_get_qlist(server, "clients");
|
279 |
if (qlist_empty(clients)) {
|
280 |
monitor_printf(mon, "Client: none\n");
|
281 |
} else {
|
282 |
qlist_iter(clients, info_vnc_iter, mon); |
283 |
} |
284 |
} |
285 |
|
286 |
/**
|
287 |
* do_info_vnc(): Show VNC server information
|
288 |
*
|
289 |
* Return a QDict with server information. Connected clients are returned
|
290 |
* as a QList of QDicts.
|
291 |
*
|
292 |
* The main QDict contains the following:
|
293 |
*
|
294 |
* - "enabled": true or false
|
295 |
* - "host": server's IP address
|
296 |
* - "service": server's port number
|
297 |
* - "auth": authentication method
|
298 |
* - "clients": a QList of all connected clients
|
299 |
*
|
300 |
* Clients are described by a QDict, with the following information:
|
301 |
*
|
302 |
* - "host": client's IP address
|
303 |
* - "service": client's port number
|
304 |
* - "x509_dname": TLS dname (optional)
|
305 |
* - "username": SASL username (optional)
|
306 |
*
|
307 |
* Example:
|
308 |
*
|
309 |
* { "enabled": true, "host": "0.0.0.0", "service": "50402", "auth": "vnc",
|
310 |
* "clients": [ { "host": "127.0.0.1", "service": "50401" } ] }
|
311 |
*/
|
312 |
void do_info_vnc(Monitor *mon, QObject **ret_data)
|
313 |
{ |
314 |
if (vnc_display == NULL || vnc_display->display == NULL) { |
315 |
*ret_data = qobject_from_jsonf("{ 'enabled': false }");
|
316 |
} else {
|
317 |
QDict *qdict; |
318 |
QList *clist; |
319 |
|
320 |
clist = qlist_new(); |
321 |
if (vnc_display->clients) {
|
322 |
VncState *client = vnc_display->clients; |
323 |
while (client) {
|
324 |
qdict = do_info_vnc_client(mon, client); |
325 |
if (qdict)
|
326 |
qlist_append(clist, qdict); |
327 |
client = client->next; |
328 |
} |
329 |
} |
330 |
|
331 |
*ret_data = qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
|
332 |
QOBJECT(clist)); |
333 |
assert(*ret_data != NULL);
|
334 |
|
335 |
if (vnc_server_info_put(qobject_to_qdict(*ret_data)) < 0) { |
336 |
qobject_decref(*ret_data); |
337 |
*ret_data = NULL;
|
338 |
} |
339 |
} |
340 |
} |
341 |
|
342 |
static inline uint32_t vnc_has_feature(VncState *vs, int feature) { |
343 |
return (vs->features & (1 << feature)); |
344 |
} |
345 |
|
346 |
/* TODO
|
347 |
1) Get the queue working for IO.
|
348 |
2) there is some weirdness when using the -S option (the screen is grey
|
349 |
and not totally invalidated
|
350 |
3) resolutions > 1024
|
351 |
*/
|
352 |
|
353 |
static int vnc_update_client(VncState *vs, int has_dirty); |
354 |
static void vnc_disconnect_start(VncState *vs); |
355 |
static void vnc_disconnect_finish(VncState *vs); |
356 |
static void vnc_init_timer(VncDisplay *vd); |
357 |
static void vnc_remove_timer(VncDisplay *vd); |
358 |
|
359 |
static void vnc_colordepth(VncState *vs); |
360 |
static void framebuffer_update_request(VncState *vs, int incremental, |
361 |
int x_position, int y_position, |
362 |
int w, int h); |
363 |
static void vnc_refresh(void *opaque); |
364 |
static int vnc_refresh_server_surface(VncDisplay *vd); |
365 |
|
366 |
static inline void vnc_set_bit(uint32_t *d, int k) |
367 |
{ |
368 |
d[k >> 5] |= 1 << (k & 0x1f); |
369 |
} |
370 |
|
371 |
static inline void vnc_clear_bit(uint32_t *d, int k) |
372 |
{ |
373 |
d[k >> 5] &= ~(1 << (k & 0x1f)); |
374 |
} |
375 |
|
376 |
static inline void vnc_set_bits(uint32_t *d, int n, int nb_words) |
377 |
{ |
378 |
int j;
|
379 |
|
380 |
j = 0;
|
381 |
while (n >= 32) { |
382 |
d[j++] = -1;
|
383 |
n -= 32;
|
384 |
} |
385 |
if (n > 0) |
386 |
d[j++] = (1 << n) - 1; |
387 |
while (j < nb_words)
|
388 |
d[j++] = 0;
|
389 |
} |
390 |
|
391 |
static inline int vnc_get_bit(const uint32_t *d, int k) |
392 |
{ |
393 |
return (d[k >> 5] >> (k & 0x1f)) & 1; |
394 |
} |
395 |
|
396 |
static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, |
397 |
int nb_words)
|
398 |
{ |
399 |
int i;
|
400 |
for(i = 0; i < nb_words; i++) { |
401 |
if ((d1[i] & d2[i]) != 0) |
402 |
return 1; |
403 |
} |
404 |
return 0; |
405 |
} |
406 |
|
407 |
static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h) |
408 |
{ |
409 |
int i;
|
410 |
VncDisplay *vd = ds->opaque; |
411 |
struct VncSurface *s = &vd->guest;
|
412 |
|
413 |
h += y; |
414 |
|
415 |
/* round x down to ensure the loop only spans one 16-pixel block per,
|
416 |
iteration. otherwise, if (x % 16) != 0, the last iteration may span
|
417 |
two 16-pixel blocks but we only mark the first as dirty
|
418 |
*/
|
419 |
w += (x % 16);
|
420 |
x -= (x % 16);
|
421 |
|
422 |
x = MIN(x, s->ds->width); |
423 |
y = MIN(y, s->ds->height); |
424 |
w = MIN(x + w, s->ds->width) - x; |
425 |
h = MIN(h, s->ds->height); |
426 |
|
427 |
for (; y < h; y++)
|
428 |
for (i = 0; i < w; i += 16) |
429 |
vnc_set_bit(s->dirty[y], (x + i) / 16);
|
430 |
} |
431 |
|
432 |
static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, |
433 |
int32_t encoding) |
434 |
{ |
435 |
vnc_write_u16(vs, x); |
436 |
vnc_write_u16(vs, y); |
437 |
vnc_write_u16(vs, w); |
438 |
vnc_write_u16(vs, h); |
439 |
|
440 |
vnc_write_s32(vs, encoding); |
441 |
} |
442 |
|
443 |
void buffer_reserve(Buffer *buffer, size_t len)
|
444 |
{ |
445 |
if ((buffer->capacity - buffer->offset) < len) {
|
446 |
buffer->capacity += (len + 1024);
|
447 |
buffer->buffer = qemu_realloc(buffer->buffer, buffer->capacity); |
448 |
if (buffer->buffer == NULL) { |
449 |
fprintf(stderr, "vnc: out of memory\n");
|
450 |
exit(1);
|
451 |
} |
452 |
} |
453 |
} |
454 |
|
455 |
int buffer_empty(Buffer *buffer)
|
456 |
{ |
457 |
return buffer->offset == 0; |
458 |
} |
459 |
|
460 |
uint8_t *buffer_end(Buffer *buffer) |
461 |
{ |
462 |
return buffer->buffer + buffer->offset;
|
463 |
} |
464 |
|
465 |
void buffer_reset(Buffer *buffer)
|
466 |
{ |
467 |
buffer->offset = 0;
|
468 |
} |
469 |
|
470 |
void buffer_append(Buffer *buffer, const void *data, size_t len) |
471 |
{ |
472 |
memcpy(buffer->buffer + buffer->offset, data, len); |
473 |
buffer->offset += len; |
474 |
} |
475 |
|
476 |
static void vnc_dpy_resize(DisplayState *ds) |
477 |
{ |
478 |
int size_changed;
|
479 |
VncDisplay *vd = ds->opaque; |
480 |
VncState *vs = vd->clients; |
481 |
|
482 |
/* server surface */
|
483 |
if (!vd->server)
|
484 |
vd->server = qemu_mallocz(sizeof(*vd->server));
|
485 |
if (vd->server->data)
|
486 |
qemu_free(vd->server->data); |
487 |
*(vd->server) = *(ds->surface); |
488 |
vd->server->data = qemu_mallocz(vd->server->linesize * |
489 |
vd->server->height); |
490 |
|
491 |
/* guest surface */
|
492 |
if (!vd->guest.ds)
|
493 |
vd->guest.ds = qemu_mallocz(sizeof(*vd->guest.ds));
|
494 |
if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
|
495 |
console_color_init(ds); |
496 |
size_changed = ds_get_width(ds) != vd->guest.ds->width || |
497 |
ds_get_height(ds) != vd->guest.ds->height; |
498 |
*(vd->guest.ds) = *(ds->surface); |
499 |
memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty)); |
500 |
|
501 |
while (vs != NULL) { |
502 |
vnc_colordepth(vs); |
503 |
if (size_changed) {
|
504 |
if (vs->csock != -1 && vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { |
505 |
vnc_write_u8(vs, 0); /* msg id */ |
506 |
vnc_write_u8(vs, 0);
|
507 |
vnc_write_u16(vs, 1); /* number of rects */ |
508 |
vnc_framebuffer_update(vs, 0, 0, ds_get_width(ds), ds_get_height(ds), |
509 |
VNC_ENCODING_DESKTOPRESIZE); |
510 |
vnc_flush(vs); |
511 |
} |
512 |
} |
513 |
memset(vs->dirty, 0xFF, sizeof(vs->dirty)); |
514 |
vs = vs->next; |
515 |
} |
516 |
} |
517 |
|
518 |
/* fastest code */
|
519 |
static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size) |
520 |
{ |
521 |
vnc_write(vs, pixels, size); |
522 |
} |
523 |
|
524 |
/* slowest but generic code. */
|
525 |
static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) |
526 |
{ |
527 |
uint8_t r, g, b; |
528 |
VncDisplay *vd = vs->vd; |
529 |
|
530 |
r = ((((v & vd->server->pf.rmask) >> vd->server->pf.rshift) << vs->clientds.pf.rbits) >> |
531 |
vd->server->pf.rbits); |
532 |
g = ((((v & vd->server->pf.gmask) >> vd->server->pf.gshift) << vs->clientds.pf.gbits) >> |
533 |
vd->server->pf.gbits); |
534 |
b = ((((v & vd->server->pf.bmask) >> vd->server->pf.bshift) << vs->clientds.pf.bbits) >> |
535 |
vd->server->pf.bbits); |
536 |
v = (r << vs->clientds.pf.rshift) | |
537 |
(g << vs->clientds.pf.gshift) | |
538 |
(b << vs->clientds.pf.bshift); |
539 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
540 |
case 1: |
541 |
buf[0] = v;
|
542 |
break;
|
543 |
case 2: |
544 |
if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
|
545 |
buf[0] = v >> 8; |
546 |
buf[1] = v;
|
547 |
} else {
|
548 |
buf[1] = v >> 8; |
549 |
buf[0] = v;
|
550 |
} |
551 |
break;
|
552 |
default:
|
553 |
case 4: |
554 |
if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
|
555 |
buf[0] = v >> 24; |
556 |
buf[1] = v >> 16; |
557 |
buf[2] = v >> 8; |
558 |
buf[3] = v;
|
559 |
} else {
|
560 |
buf[3] = v >> 24; |
561 |
buf[2] = v >> 16; |
562 |
buf[1] = v >> 8; |
563 |
buf[0] = v;
|
564 |
} |
565 |
break;
|
566 |
} |
567 |
} |
568 |
|
569 |
static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size) |
570 |
{ |
571 |
uint8_t buf[4];
|
572 |
VncDisplay *vd = vs->vd; |
573 |
|
574 |
if (vd->server->pf.bytes_per_pixel == 4) { |
575 |
uint32_t *pixels = pixels1; |
576 |
int n, i;
|
577 |
n = size >> 2;
|
578 |
for(i = 0; i < n; i++) { |
579 |
vnc_convert_pixel(vs, buf, pixels[i]); |
580 |
vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); |
581 |
} |
582 |
} else if (vd->server->pf.bytes_per_pixel == 2) { |
583 |
uint16_t *pixels = pixels1; |
584 |
int n, i;
|
585 |
n = size >> 1;
|
586 |
for(i = 0; i < n; i++) { |
587 |
vnc_convert_pixel(vs, buf, pixels[i]); |
588 |
vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); |
589 |
} |
590 |
} else if (vd->server->pf.bytes_per_pixel == 1) { |
591 |
uint8_t *pixels = pixels1; |
592 |
int n, i;
|
593 |
n = size; |
594 |
for(i = 0; i < n; i++) { |
595 |
vnc_convert_pixel(vs, buf, pixels[i]); |
596 |
vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); |
597 |
} |
598 |
} else {
|
599 |
fprintf(stderr, "vnc_write_pixels_generic: VncState color depth not supported\n");
|
600 |
} |
601 |
} |
602 |
|
603 |
static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h) |
604 |
{ |
605 |
int i;
|
606 |
uint8_t *row; |
607 |
VncDisplay *vd = vs->vd; |
608 |
|
609 |
row = vd->server->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds); |
610 |
for (i = 0; i < h; i++) { |
611 |
vs->write_pixels(vs, row, w * ds_get_bytes_per_pixel(vs->ds)); |
612 |
row += ds_get_linesize(vs->ds); |
613 |
} |
614 |
} |
615 |
|
616 |
static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h) |
617 |
{ |
618 |
ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F); |
619 |
ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F); |
620 |
} |
621 |
|
622 |
#define BPP 8 |
623 |
#include "vnchextile.h" |
624 |
#undef BPP
|
625 |
|
626 |
#define BPP 16 |
627 |
#include "vnchextile.h" |
628 |
#undef BPP
|
629 |
|
630 |
#define BPP 32 |
631 |
#include "vnchextile.h" |
632 |
#undef BPP
|
633 |
|
634 |
#define GENERIC
|
635 |
#define BPP 8 |
636 |
#include "vnchextile.h" |
637 |
#undef BPP
|
638 |
#undef GENERIC
|
639 |
|
640 |
#define GENERIC
|
641 |
#define BPP 16 |
642 |
#include "vnchextile.h" |
643 |
#undef BPP
|
644 |
#undef GENERIC
|
645 |
|
646 |
#define GENERIC
|
647 |
#define BPP 32 |
648 |
#include "vnchextile.h" |
649 |
#undef BPP
|
650 |
#undef GENERIC
|
651 |
|
652 |
static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h) |
653 |
{ |
654 |
int i, j;
|
655 |
int has_fg, has_bg;
|
656 |
uint8_t *last_fg, *last_bg; |
657 |
VncDisplay *vd = vs->vd; |
658 |
|
659 |
last_fg = (uint8_t *) qemu_malloc(vd->server->pf.bytes_per_pixel); |
660 |
last_bg = (uint8_t *) qemu_malloc(vd->server->pf.bytes_per_pixel); |
661 |
has_fg = has_bg = 0;
|
662 |
for (j = y; j < (y + h); j += 16) { |
663 |
for (i = x; i < (x + w); i += 16) { |
664 |
vs->send_hextile_tile(vs, i, j, |
665 |
MIN(16, x + w - i), MIN(16, y + h - j), |
666 |
last_bg, last_fg, &has_bg, &has_fg); |
667 |
} |
668 |
} |
669 |
free(last_fg); |
670 |
free(last_bg); |
671 |
|
672 |
} |
673 |
|
674 |
#define ZALLOC_ALIGNMENT 16 |
675 |
|
676 |
static void *zalloc(void *x, unsigned items, unsigned size) |
677 |
{ |
678 |
void *p;
|
679 |
|
680 |
size *= items; |
681 |
size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); |
682 |
|
683 |
p = qemu_mallocz(size); |
684 |
|
685 |
return (p);
|
686 |
} |
687 |
|
688 |
static void zfree(void *x, void *addr) |
689 |
{ |
690 |
qemu_free(addr); |
691 |
} |
692 |
|
693 |
static void vnc_zlib_init(VncState *vs) |
694 |
{ |
695 |
int i;
|
696 |
for (i=0; i<(sizeof(vs->zlib_stream) / sizeof(z_stream)); i++) |
697 |
vs->zlib_stream[i].opaque = NULL;
|
698 |
} |
699 |
|
700 |
static void vnc_zlib_start(VncState *vs) |
701 |
{ |
702 |
buffer_reset(&vs->zlib); |
703 |
|
704 |
// make the output buffer be the zlib buffer, so we can compress it later
|
705 |
vs->zlib_tmp = vs->output; |
706 |
vs->output = vs->zlib; |
707 |
} |
708 |
|
709 |
static int vnc_zlib_stop(VncState *vs, int stream_id) |
710 |
{ |
711 |
z_streamp zstream = &vs->zlib_stream[stream_id]; |
712 |
int previous_out;
|
713 |
|
714 |
// switch back to normal output/zlib buffers
|
715 |
vs->zlib = vs->output; |
716 |
vs->output = vs->zlib_tmp; |
717 |
|
718 |
// compress the zlib buffer
|
719 |
|
720 |
// initialize the stream
|
721 |
// XXX need one stream per session
|
722 |
if (zstream->opaque != vs) {
|
723 |
int err;
|
724 |
|
725 |
VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id);
|
726 |
VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
|
727 |
zstream->zalloc = zalloc; |
728 |
zstream->zfree = zfree; |
729 |
|
730 |
err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS, |
731 |
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
732 |
|
733 |
if (err != Z_OK) {
|
734 |
fprintf(stderr, "VNC: error initializing zlib\n");
|
735 |
return -1; |
736 |
} |
737 |
|
738 |
zstream->opaque = vs; |
739 |
} |
740 |
|
741 |
// XXX what to do if tight_compression changed in between?
|
742 |
|
743 |
// reserve memory in output buffer
|
744 |
buffer_reserve(&vs->output, vs->zlib.offset + 64);
|
745 |
|
746 |
// set pointers
|
747 |
zstream->next_in = vs->zlib.buffer; |
748 |
zstream->avail_in = vs->zlib.offset; |
749 |
zstream->next_out = vs->output.buffer + vs->output.offset; |
750 |
zstream->avail_out = vs->output.capacity - vs->output.offset; |
751 |
zstream->data_type = Z_BINARY; |
752 |
previous_out = zstream->total_out; |
753 |
|
754 |
// start encoding
|
755 |
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
|
756 |
fprintf(stderr, "VNC: error during zlib compression\n");
|
757 |
return -1; |
758 |
} |
759 |
|
760 |
vs->output.offset = vs->output.capacity - zstream->avail_out; |
761 |
return zstream->total_out - previous_out;
|
762 |
} |
763 |
|
764 |
static void send_framebuffer_update_zlib(VncState *vs, int x, int y, int w, int h) |
765 |
{ |
766 |
int old_offset, new_offset, bytes_written;
|
767 |
|
768 |
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB); |
769 |
|
770 |
// remember where we put in the follow-up size
|
771 |
old_offset = vs->output.offset; |
772 |
vnc_write_s32(vs, 0);
|
773 |
|
774 |
// compress the stream
|
775 |
vnc_zlib_start(vs); |
776 |
send_framebuffer_update_raw(vs, x, y, w, h); |
777 |
bytes_written = vnc_zlib_stop(vs, 0);
|
778 |
|
779 |
if (bytes_written == -1) |
780 |
return;
|
781 |
|
782 |
// hack in the size
|
783 |
new_offset = vs->output.offset; |
784 |
vs->output.offset = old_offset; |
785 |
vnc_write_u32(vs, bytes_written); |
786 |
vs->output.offset = new_offset; |
787 |
} |
788 |
|
789 |
static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h) |
790 |
{ |
791 |
switch(vs->vnc_encoding) {
|
792 |
case VNC_ENCODING_ZLIB:
|
793 |
send_framebuffer_update_zlib(vs, x, y, w, h); |
794 |
break;
|
795 |
case VNC_ENCODING_HEXTILE:
|
796 |
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); |
797 |
send_framebuffer_update_hextile(vs, x, y, w, h); |
798 |
break;
|
799 |
default:
|
800 |
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); |
801 |
send_framebuffer_update_raw(vs, x, y, w, h); |
802 |
break;
|
803 |
} |
804 |
} |
805 |
|
806 |
static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) |
807 |
{ |
808 |
/* send bitblit op to the vnc client */
|
809 |
vnc_write_u8(vs, 0); /* msg id */ |
810 |
vnc_write_u8(vs, 0);
|
811 |
vnc_write_u16(vs, 1); /* number of rects */ |
812 |
vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); |
813 |
vnc_write_u16(vs, src_x); |
814 |
vnc_write_u16(vs, src_y); |
815 |
vnc_flush(vs); |
816 |
} |
817 |
|
818 |
static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) |
819 |
{ |
820 |
VncDisplay *vd = ds->opaque; |
821 |
VncState *vs, *vn; |
822 |
uint8_t *src_row; |
823 |
uint8_t *dst_row; |
824 |
int i,x,y,pitch,depth,inc,w_lim,s;
|
825 |
int cmp_bytes;
|
826 |
|
827 |
vnc_refresh_server_surface(vd); |
828 |
for (vs = vd->clients; vs != NULL; vs = vn) { |
829 |
vn = vs->next; |
830 |
if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
|
831 |
vs->force_update = 1;
|
832 |
vnc_update_client(vs, 1);
|
833 |
/* vs might be free()ed here */
|
834 |
} |
835 |
} |
836 |
|
837 |
/* do bitblit op on the local surface too */
|
838 |
pitch = ds_get_linesize(vd->ds); |
839 |
depth = ds_get_bytes_per_pixel(vd->ds); |
840 |
src_row = vd->server->data + pitch * src_y + depth * src_x; |
841 |
dst_row = vd->server->data + pitch * dst_y + depth * dst_x; |
842 |
y = dst_y; |
843 |
inc = 1;
|
844 |
if (dst_y > src_y) {
|
845 |
/* copy backwards */
|
846 |
src_row += pitch * (h-1);
|
847 |
dst_row += pitch * (h-1);
|
848 |
pitch = -pitch; |
849 |
y = dst_y + h - 1;
|
850 |
inc = -1;
|
851 |
} |
852 |
w_lim = w - (16 - (dst_x % 16)); |
853 |
if (w_lim < 0) |
854 |
w_lim = w; |
855 |
else
|
856 |
w_lim = w - (w_lim % 16);
|
857 |
for (i = 0; i < h; i++) { |
858 |
for (x = 0; x <= w_lim; |
859 |
x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { |
860 |
if (x == w_lim) {
|
861 |
if ((s = w - w_lim) == 0) |
862 |
break;
|
863 |
} else if (!x) { |
864 |
s = (16 - (dst_x % 16)); |
865 |
s = MIN(s, w_lim); |
866 |
} else {
|
867 |
s = 16;
|
868 |
} |
869 |
cmp_bytes = s * depth; |
870 |
if (memcmp(src_row, dst_row, cmp_bytes) == 0) |
871 |
continue;
|
872 |
memmove(dst_row, src_row, cmp_bytes); |
873 |
vs = vd->clients; |
874 |
while (vs != NULL) { |
875 |
if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
|
876 |
vnc_set_bit(vs->dirty[y], ((x + dst_x) / 16));
|
877 |
vs = vs->next; |
878 |
} |
879 |
} |
880 |
src_row += pitch - w * depth; |
881 |
dst_row += pitch - w * depth; |
882 |
y += inc; |
883 |
} |
884 |
|
885 |
for (vs = vd->clients; vs != NULL; vs = vs->next) { |
886 |
if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
|
887 |
vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); |
888 |
} |
889 |
} |
890 |
|
891 |
static int find_and_clear_dirty_height(struct VncState *vs, |
892 |
int y, int last_x, int x) |
893 |
{ |
894 |
int h;
|
895 |
VncDisplay *vd = vs->vd; |
896 |
|
897 |
for (h = 1; h < (vd->server->height - y); h++) { |
898 |
int tmp_x;
|
899 |
if (!vnc_get_bit(vs->dirty[y + h], last_x))
|
900 |
break;
|
901 |
for (tmp_x = last_x; tmp_x < x; tmp_x++)
|
902 |
vnc_clear_bit(vs->dirty[y + h], tmp_x); |
903 |
} |
904 |
|
905 |
return h;
|
906 |
} |
907 |
|
908 |
static int vnc_update_client(VncState *vs, int has_dirty) |
909 |
{ |
910 |
if (vs->need_update && vs->csock != -1) { |
911 |
VncDisplay *vd = vs->vd; |
912 |
int y;
|
913 |
int n_rectangles;
|
914 |
int saved_offset;
|
915 |
|
916 |
if (vs->output.offset && !vs->audio_cap && !vs->force_update)
|
917 |
/* kernel send buffers are full -> drop frames to throttle */
|
918 |
return 0; |
919 |
|
920 |
if (!has_dirty && !vs->audio_cap && !vs->force_update)
|
921 |
return 0; |
922 |
|
923 |
/*
|
924 |
* Send screen updates to the vnc client using the server
|
925 |
* surface and server dirty map. guest surface updates
|
926 |
* happening in parallel don't disturb us, the next pass will
|
927 |
* send them to the client.
|
928 |
*/
|
929 |
n_rectangles = 0;
|
930 |
vnc_write_u8(vs, 0); /* msg id */ |
931 |
vnc_write_u8(vs, 0);
|
932 |
saved_offset = vs->output.offset; |
933 |
vnc_write_u16(vs, 0);
|
934 |
|
935 |
for (y = 0; y < vd->server->height; y++) { |
936 |
int x;
|
937 |
int last_x = -1; |
938 |
for (x = 0; x < vd->server->width / 16; x++) { |
939 |
if (vnc_get_bit(vs->dirty[y], x)) {
|
940 |
if (last_x == -1) { |
941 |
last_x = x; |
942 |
} |
943 |
vnc_clear_bit(vs->dirty[y], x); |
944 |
} else {
|
945 |
if (last_x != -1) { |
946 |
int h = find_and_clear_dirty_height(vs, y, last_x, x);
|
947 |
send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
948 |
n_rectangles++; |
949 |
} |
950 |
last_x = -1;
|
951 |
} |
952 |
} |
953 |
if (last_x != -1) { |
954 |
int h = find_and_clear_dirty_height(vs, y, last_x, x);
|
955 |
send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h); |
956 |
n_rectangles++; |
957 |
} |
958 |
} |
959 |
vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
960 |
vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
961 |
vnc_flush(vs); |
962 |
vs->force_update = 0;
|
963 |
return n_rectangles;
|
964 |
} |
965 |
|
966 |
if (vs->csock == -1) |
967 |
vnc_disconnect_finish(vs); |
968 |
|
969 |
return 0; |
970 |
} |
971 |
|
972 |
/* audio */
|
973 |
static void audio_capture_notify(void *opaque, audcnotification_e cmd) |
974 |
{ |
975 |
VncState *vs = opaque; |
976 |
|
977 |
switch (cmd) {
|
978 |
case AUD_CNOTIFY_DISABLE:
|
979 |
vnc_write_u8(vs, 255);
|
980 |
vnc_write_u8(vs, 1);
|
981 |
vnc_write_u16(vs, 0);
|
982 |
vnc_flush(vs); |
983 |
break;
|
984 |
|
985 |
case AUD_CNOTIFY_ENABLE:
|
986 |
vnc_write_u8(vs, 255);
|
987 |
vnc_write_u8(vs, 1);
|
988 |
vnc_write_u16(vs, 1);
|
989 |
vnc_flush(vs); |
990 |
break;
|
991 |
} |
992 |
} |
993 |
|
994 |
static void audio_capture_destroy(void *opaque) |
995 |
{ |
996 |
} |
997 |
|
998 |
static void audio_capture(void *opaque, void *buf, int size) |
999 |
{ |
1000 |
VncState *vs = opaque; |
1001 |
|
1002 |
vnc_write_u8(vs, 255);
|
1003 |
vnc_write_u8(vs, 1);
|
1004 |
vnc_write_u16(vs, 2);
|
1005 |
vnc_write_u32(vs, size); |
1006 |
vnc_write(vs, buf, size); |
1007 |
vnc_flush(vs); |
1008 |
} |
1009 |
|
1010 |
static void audio_add(VncState *vs) |
1011 |
{ |
1012 |
Monitor *mon = cur_mon; |
1013 |
struct audio_capture_ops ops;
|
1014 |
|
1015 |
if (vs->audio_cap) {
|
1016 |
monitor_printf(mon, "audio already running\n");
|
1017 |
return;
|
1018 |
} |
1019 |
|
1020 |
ops.notify = audio_capture_notify; |
1021 |
ops.destroy = audio_capture_destroy; |
1022 |
ops.capture = audio_capture; |
1023 |
|
1024 |
vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); |
1025 |
if (!vs->audio_cap) {
|
1026 |
monitor_printf(mon, "Failed to add audio capture\n");
|
1027 |
} |
1028 |
} |
1029 |
|
1030 |
static void audio_del(VncState *vs) |
1031 |
{ |
1032 |
if (vs->audio_cap) {
|
1033 |
AUD_del_capture(vs->audio_cap, vs); |
1034 |
vs->audio_cap = NULL;
|
1035 |
} |
1036 |
} |
1037 |
|
1038 |
static void vnc_disconnect_start(VncState *vs) |
1039 |
{ |
1040 |
if (vs->csock == -1) |
1041 |
return;
|
1042 |
qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); |
1043 |
closesocket(vs->csock); |
1044 |
vs->csock = -1;
|
1045 |
} |
1046 |
|
1047 |
static void vnc_disconnect_finish(VncState *vs) |
1048 |
{ |
1049 |
if (vs->input.buffer) {
|
1050 |
qemu_free(vs->input.buffer); |
1051 |
vs->input.buffer = NULL;
|
1052 |
} |
1053 |
if (vs->output.buffer) {
|
1054 |
qemu_free(vs->output.buffer); |
1055 |
vs->output.buffer = NULL;
|
1056 |
} |
1057 |
#ifdef CONFIG_VNC_TLS
|
1058 |
vnc_tls_client_cleanup(vs); |
1059 |
#endif /* CONFIG_VNC_TLS */ |
1060 |
#ifdef CONFIG_VNC_SASL
|
1061 |
vnc_sasl_client_cleanup(vs); |
1062 |
#endif /* CONFIG_VNC_SASL */ |
1063 |
audio_del(vs); |
1064 |
|
1065 |
VncState *p, *parent = NULL;
|
1066 |
for (p = vs->vd->clients; p != NULL; p = p->next) { |
1067 |
if (p == vs) {
|
1068 |
if (parent)
|
1069 |
parent->next = p->next; |
1070 |
else
|
1071 |
vs->vd->clients = p->next; |
1072 |
break;
|
1073 |
} |
1074 |
parent = p; |
1075 |
} |
1076 |
if (!vs->vd->clients)
|
1077 |
dcl->idle = 1;
|
1078 |
|
1079 |
vnc_remove_timer(vs->vd); |
1080 |
qemu_free(vs); |
1081 |
} |
1082 |
|
1083 |
int vnc_client_io_error(VncState *vs, int ret, int last_errno) |
1084 |
{ |
1085 |
if (ret == 0 || ret == -1) { |
1086 |
if (ret == -1) { |
1087 |
switch (last_errno) {
|
1088 |
case EINTR:
|
1089 |
case EAGAIN:
|
1090 |
#ifdef _WIN32
|
1091 |
case WSAEWOULDBLOCK:
|
1092 |
#endif
|
1093 |
return 0; |
1094 |
default:
|
1095 |
break;
|
1096 |
} |
1097 |
} |
1098 |
|
1099 |
VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
|
1100 |
ret, ret < 0 ? last_errno : 0); |
1101 |
vnc_disconnect_start(vs); |
1102 |
|
1103 |
return 0; |
1104 |
} |
1105 |
return ret;
|
1106 |
} |
1107 |
|
1108 |
|
1109 |
void vnc_client_error(VncState *vs)
|
1110 |
{ |
1111 |
VNC_DEBUG("Closing down client sock: protocol error\n");
|
1112 |
vnc_disconnect_start(vs); |
1113 |
} |
1114 |
|
1115 |
|
1116 |
/*
|
1117 |
* Called to write a chunk of data to the client socket. The data may
|
1118 |
* be the raw data, or may have already been encoded by SASL.
|
1119 |
* The data will be written either straight onto the socket, or
|
1120 |
* written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
|
1121 |
*
|
1122 |
* NB, it is theoretically possible to have 2 layers of encryption,
|
1123 |
* both SASL, and this TLS layer. It is highly unlikely in practice
|
1124 |
* though, since SASL encryption will typically be a no-op if TLS
|
1125 |
* is active
|
1126 |
*
|
1127 |
* Returns the number of bytes written, which may be less than
|
1128 |
* the requested 'datalen' if the socket would block. Returns
|
1129 |
* -1 on error, and disconnects the client socket.
|
1130 |
*/
|
1131 |
long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) |
1132 |
{ |
1133 |
long ret;
|
1134 |
#ifdef CONFIG_VNC_TLS
|
1135 |
if (vs->tls.session) {
|
1136 |
ret = gnutls_write(vs->tls.session, data, datalen); |
1137 |
if (ret < 0) { |
1138 |
if (ret == GNUTLS_E_AGAIN)
|
1139 |
errno = EAGAIN; |
1140 |
else
|
1141 |
errno = EIO; |
1142 |
ret = -1;
|
1143 |
} |
1144 |
} else
|
1145 |
#endif /* CONFIG_VNC_TLS */ |
1146 |
ret = send(vs->csock, (const void *)data, datalen, 0); |
1147 |
VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
|
1148 |
return vnc_client_io_error(vs, ret, socket_error());
|
1149 |
} |
1150 |
|
1151 |
|
1152 |
/*
|
1153 |
* Called to write buffered data to the client socket, when not
|
1154 |
* using any SASL SSF encryption layers. Will write as much data
|
1155 |
* as possible without blocking. If all buffered data is written,
|
1156 |
* will switch the FD poll() handler back to read monitoring.
|
1157 |
*
|
1158 |
* Returns the number of bytes written, which may be less than
|
1159 |
* the buffered output data if the socket would block. Returns
|
1160 |
* -1 on error, and disconnects the client socket.
|
1161 |
*/
|
1162 |
static long vnc_client_write_plain(VncState *vs) |
1163 |
{ |
1164 |
long ret;
|
1165 |
|
1166 |
#ifdef CONFIG_VNC_SASL
|
1167 |
VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
|
1168 |
vs->output.buffer, vs->output.capacity, vs->output.offset, |
1169 |
vs->sasl.waitWriteSSF); |
1170 |
|
1171 |
if (vs->sasl.conn &&
|
1172 |
vs->sasl.runSSF && |
1173 |
vs->sasl.waitWriteSSF) { |
1174 |
ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); |
1175 |
if (ret)
|
1176 |
vs->sasl.waitWriteSSF -= ret; |
1177 |
} else
|
1178 |
#endif /* CONFIG_VNC_SASL */ |
1179 |
ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); |
1180 |
if (!ret)
|
1181 |
return 0; |
1182 |
|
1183 |
memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret)); |
1184 |
vs->output.offset -= ret; |
1185 |
|
1186 |
if (vs->output.offset == 0) { |
1187 |
qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); |
1188 |
} |
1189 |
|
1190 |
return ret;
|
1191 |
} |
1192 |
|
1193 |
|
1194 |
/*
|
1195 |
* First function called whenever there is data to be written to
|
1196 |
* the client socket. Will delegate actual work according to whether
|
1197 |
* SASL SSF layers are enabled (thus requiring encryption calls)
|
1198 |
*/
|
1199 |
void vnc_client_write(void *opaque) |
1200 |
{ |
1201 |
long ret;
|
1202 |
VncState *vs = opaque; |
1203 |
|
1204 |
#ifdef CONFIG_VNC_SASL
|
1205 |
if (vs->sasl.conn &&
|
1206 |
vs->sasl.runSSF && |
1207 |
!vs->sasl.waitWriteSSF) |
1208 |
ret = vnc_client_write_sasl(vs); |
1209 |
else
|
1210 |
#endif /* CONFIG_VNC_SASL */ |
1211 |
ret = vnc_client_write_plain(vs); |
1212 |
} |
1213 |
|
1214 |
void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
|
1215 |
{ |
1216 |
vs->read_handler = func; |
1217 |
vs->read_handler_expect = expecting; |
1218 |
} |
1219 |
|
1220 |
|
1221 |
/*
|
1222 |
* Called to read a chunk of data from the client socket. The data may
|
1223 |
* be the raw data, or may need to be further decoded by SASL.
|
1224 |
* The data will be read either straight from to the socket, or
|
1225 |
* read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
|
1226 |
*
|
1227 |
* NB, it is theoretically possible to have 2 layers of encryption,
|
1228 |
* both SASL, and this TLS layer. It is highly unlikely in practice
|
1229 |
* though, since SASL encryption will typically be a no-op if TLS
|
1230 |
* is active
|
1231 |
*
|
1232 |
* Returns the number of bytes read, which may be less than
|
1233 |
* the requested 'datalen' if the socket would block. Returns
|
1234 |
* -1 on error, and disconnects the client socket.
|
1235 |
*/
|
1236 |
long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
|
1237 |
{ |
1238 |
long ret;
|
1239 |
#ifdef CONFIG_VNC_TLS
|
1240 |
if (vs->tls.session) {
|
1241 |
ret = gnutls_read(vs->tls.session, data, datalen); |
1242 |
if (ret < 0) { |
1243 |
if (ret == GNUTLS_E_AGAIN)
|
1244 |
errno = EAGAIN; |
1245 |
else
|
1246 |
errno = EIO; |
1247 |
ret = -1;
|
1248 |
} |
1249 |
} else
|
1250 |
#endif /* CONFIG_VNC_TLS */ |
1251 |
ret = recv(vs->csock, (void *)data, datalen, 0); |
1252 |
VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
|
1253 |
return vnc_client_io_error(vs, ret, socket_error());
|
1254 |
} |
1255 |
|
1256 |
|
1257 |
/*
|
1258 |
* Called to read data from the client socket to the input buffer,
|
1259 |
* when not using any SASL SSF encryption layers. Will read as much
|
1260 |
* data as possible without blocking.
|
1261 |
*
|
1262 |
* Returns the number of bytes read. Returns -1 on error, and
|
1263 |
* disconnects the client socket.
|
1264 |
*/
|
1265 |
static long vnc_client_read_plain(VncState *vs) |
1266 |
{ |
1267 |
int ret;
|
1268 |
VNC_DEBUG("Read plain %p size %zd offset %zd\n",
|
1269 |
vs->input.buffer, vs->input.capacity, vs->input.offset); |
1270 |
buffer_reserve(&vs->input, 4096);
|
1271 |
ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
|
1272 |
if (!ret)
|
1273 |
return 0; |
1274 |
vs->input.offset += ret; |
1275 |
return ret;
|
1276 |
} |
1277 |
|
1278 |
|
1279 |
/*
|
1280 |
* First function called whenever there is more data to be read from
|
1281 |
* the client socket. Will delegate actual work according to whether
|
1282 |
* SASL SSF layers are enabled (thus requiring decryption calls)
|
1283 |
*/
|
1284 |
void vnc_client_read(void *opaque) |
1285 |
{ |
1286 |
VncState *vs = opaque; |
1287 |
long ret;
|
1288 |
|
1289 |
#ifdef CONFIG_VNC_SASL
|
1290 |
if (vs->sasl.conn && vs->sasl.runSSF)
|
1291 |
ret = vnc_client_read_sasl(vs); |
1292 |
else
|
1293 |
#endif /* CONFIG_VNC_SASL */ |
1294 |
ret = vnc_client_read_plain(vs); |
1295 |
if (!ret) {
|
1296 |
if (vs->csock == -1) |
1297 |
vnc_disconnect_finish(vs); |
1298 |
return;
|
1299 |
} |
1300 |
|
1301 |
while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
|
1302 |
size_t len = vs->read_handler_expect; |
1303 |
int ret;
|
1304 |
|
1305 |
ret = vs->read_handler(vs, vs->input.buffer, len); |
1306 |
if (vs->csock == -1) { |
1307 |
vnc_disconnect_finish(vs); |
1308 |
return;
|
1309 |
} |
1310 |
|
1311 |
if (!ret) {
|
1312 |
memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); |
1313 |
vs->input.offset -= len; |
1314 |
} else {
|
1315 |
vs->read_handler_expect = ret; |
1316 |
} |
1317 |
} |
1318 |
} |
1319 |
|
1320 |
void vnc_write(VncState *vs, const void *data, size_t len) |
1321 |
{ |
1322 |
buffer_reserve(&vs->output, len); |
1323 |
|
1324 |
if (vs->csock != -1 && buffer_empty(&vs->output)) { |
1325 |
qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
|
1326 |
} |
1327 |
|
1328 |
buffer_append(&vs->output, data, len); |
1329 |
} |
1330 |
|
1331 |
void vnc_write_s32(VncState *vs, int32_t value)
|
1332 |
{ |
1333 |
vnc_write_u32(vs, *(uint32_t *)&value); |
1334 |
} |
1335 |
|
1336 |
void vnc_write_u32(VncState *vs, uint32_t value)
|
1337 |
{ |
1338 |
uint8_t buf[4];
|
1339 |
|
1340 |
buf[0] = (value >> 24) & 0xFF; |
1341 |
buf[1] = (value >> 16) & 0xFF; |
1342 |
buf[2] = (value >> 8) & 0xFF; |
1343 |
buf[3] = value & 0xFF; |
1344 |
|
1345 |
vnc_write(vs, buf, 4);
|
1346 |
} |
1347 |
|
1348 |
void vnc_write_u16(VncState *vs, uint16_t value)
|
1349 |
{ |
1350 |
uint8_t buf[2];
|
1351 |
|
1352 |
buf[0] = (value >> 8) & 0xFF; |
1353 |
buf[1] = value & 0xFF; |
1354 |
|
1355 |
vnc_write(vs, buf, 2);
|
1356 |
} |
1357 |
|
1358 |
void vnc_write_u8(VncState *vs, uint8_t value)
|
1359 |
{ |
1360 |
vnc_write(vs, (char *)&value, 1); |
1361 |
} |
1362 |
|
1363 |
void vnc_flush(VncState *vs)
|
1364 |
{ |
1365 |
if (vs->csock != -1 && vs->output.offset) |
1366 |
vnc_client_write(vs); |
1367 |
} |
1368 |
|
1369 |
uint8_t read_u8(uint8_t *data, size_t offset) |
1370 |
{ |
1371 |
return data[offset];
|
1372 |
} |
1373 |
|
1374 |
uint16_t read_u16(uint8_t *data, size_t offset) |
1375 |
{ |
1376 |
return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); |
1377 |
} |
1378 |
|
1379 |
int32_t read_s32(uint8_t *data, size_t offset) |
1380 |
{ |
1381 |
return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | |
1382 |
(data[offset + 2] << 8) | data[offset + 3]); |
1383 |
} |
1384 |
|
1385 |
uint32_t read_u32(uint8_t *data, size_t offset) |
1386 |
{ |
1387 |
return ((data[offset] << 24) | (data[offset + 1] << 16) | |
1388 |
(data[offset + 2] << 8) | data[offset + 3]); |
1389 |
} |
1390 |
|
1391 |
static void client_cut_text(VncState *vs, size_t len, uint8_t *text) |
1392 |
{ |
1393 |
} |
1394 |
|
1395 |
static void check_pointer_type_change(VncState *vs, int absolute) |
1396 |
{ |
1397 |
if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
|
1398 |
vnc_write_u8(vs, 0);
|
1399 |
vnc_write_u8(vs, 0);
|
1400 |
vnc_write_u16(vs, 1);
|
1401 |
vnc_framebuffer_update(vs, absolute, 0,
|
1402 |
ds_get_width(vs->ds), ds_get_height(vs->ds), |
1403 |
VNC_ENCODING_POINTER_TYPE_CHANGE); |
1404 |
vnc_flush(vs); |
1405 |
} |
1406 |
vs->absolute = absolute; |
1407 |
} |
1408 |
|
1409 |
static void pointer_event(VncState *vs, int button_mask, int x, int y) |
1410 |
{ |
1411 |
int buttons = 0; |
1412 |
int dz = 0; |
1413 |
|
1414 |
if (button_mask & 0x01) |
1415 |
buttons |= MOUSE_EVENT_LBUTTON; |
1416 |
if (button_mask & 0x02) |
1417 |
buttons |= MOUSE_EVENT_MBUTTON; |
1418 |
if (button_mask & 0x04) |
1419 |
buttons |= MOUSE_EVENT_RBUTTON; |
1420 |
if (button_mask & 0x08) |
1421 |
dz = -1;
|
1422 |
if (button_mask & 0x10) |
1423 |
dz = 1;
|
1424 |
|
1425 |
if (vs->absolute) {
|
1426 |
kbd_mouse_event(x * 0x7FFF / (ds_get_width(vs->ds) - 1), |
1427 |
y * 0x7FFF / (ds_get_height(vs->ds) - 1), |
1428 |
dz, buttons); |
1429 |
} else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { |
1430 |
x -= 0x7FFF;
|
1431 |
y -= 0x7FFF;
|
1432 |
|
1433 |
kbd_mouse_event(x, y, dz, buttons); |
1434 |
} else {
|
1435 |
if (vs->last_x != -1) |
1436 |
kbd_mouse_event(x - vs->last_x, |
1437 |
y - vs->last_y, |
1438 |
dz, buttons); |
1439 |
vs->last_x = x; |
1440 |
vs->last_y = y; |
1441 |
} |
1442 |
|
1443 |
check_pointer_type_change(vs, kbd_mouse_is_absolute()); |
1444 |
} |
1445 |
|
1446 |
static void reset_keys(VncState *vs) |
1447 |
{ |
1448 |
int i;
|
1449 |
for(i = 0; i < 256; i++) { |
1450 |
if (vs->modifiers_state[i]) {
|
1451 |
if (i & 0x80) |
1452 |
kbd_put_keycode(0xe0);
|
1453 |
kbd_put_keycode(i | 0x80);
|
1454 |
vs->modifiers_state[i] = 0;
|
1455 |
} |
1456 |
} |
1457 |
} |
1458 |
|
1459 |
static void press_key(VncState *vs, int keysym) |
1460 |
{ |
1461 |
kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) & 0x7f);
|
1462 |
kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) | 0x80);
|
1463 |
} |
1464 |
|
1465 |
static void do_key_event(VncState *vs, int down, int keycode, int sym) |
1466 |
{ |
1467 |
/* QEMU console switch */
|
1468 |
switch(keycode) {
|
1469 |
case 0x2a: /* Left Shift */ |
1470 |
case 0x36: /* Right Shift */ |
1471 |
case 0x1d: /* Left CTRL */ |
1472 |
case 0x9d: /* Right CTRL */ |
1473 |
case 0x38: /* Left ALT */ |
1474 |
case 0xb8: /* Right ALT */ |
1475 |
if (down)
|
1476 |
vs->modifiers_state[keycode] = 1;
|
1477 |
else
|
1478 |
vs->modifiers_state[keycode] = 0;
|
1479 |
break;
|
1480 |
case 0x02 ... 0x0a: /* '1' to '9' keys */ |
1481 |
if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { |
1482 |
/* Reset the modifiers sent to the current console */
|
1483 |
reset_keys(vs); |
1484 |
console_select(keycode - 0x02);
|
1485 |
return;
|
1486 |
} |
1487 |
break;
|
1488 |
case 0x3a: /* CapsLock */ |
1489 |
case 0x45: /* NumLock */ |
1490 |
if (!down)
|
1491 |
vs->modifiers_state[keycode] ^= 1;
|
1492 |
break;
|
1493 |
} |
1494 |
|
1495 |
if (keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
|
1496 |
/* If the numlock state needs to change then simulate an additional
|
1497 |
keypress before sending this one. This will happen if the user
|
1498 |
toggles numlock away from the VNC window.
|
1499 |
*/
|
1500 |
if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { |
1501 |
if (!vs->modifiers_state[0x45]) { |
1502 |
vs->modifiers_state[0x45] = 1; |
1503 |
press_key(vs, 0xff7f);
|
1504 |
} |
1505 |
} else {
|
1506 |
if (vs->modifiers_state[0x45]) { |
1507 |
vs->modifiers_state[0x45] = 0; |
1508 |
press_key(vs, 0xff7f);
|
1509 |
} |
1510 |
} |
1511 |
} |
1512 |
|
1513 |
if ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z')) { |
1514 |
/* If the capslock state needs to change then simulate an additional
|
1515 |
keypress before sending this one. This will happen if the user
|
1516 |
toggles capslock away from the VNC window.
|
1517 |
*/
|
1518 |
int uppercase = !!(sym >= 'A' && sym <= 'Z'); |
1519 |
int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); |
1520 |
int capslock = !!(vs->modifiers_state[0x3a]); |
1521 |
if (capslock) {
|
1522 |
if (uppercase == shift) {
|
1523 |
vs->modifiers_state[0x3a] = 0; |
1524 |
press_key(vs, 0xffe5);
|
1525 |
} |
1526 |
} else {
|
1527 |
if (uppercase != shift) {
|
1528 |
vs->modifiers_state[0x3a] = 1; |
1529 |
press_key(vs, 0xffe5);
|
1530 |
} |
1531 |
} |
1532 |
} |
1533 |
|
1534 |
if (is_graphic_console()) {
|
1535 |
if (keycode & 0x80) |
1536 |
kbd_put_keycode(0xe0);
|
1537 |
if (down)
|
1538 |
kbd_put_keycode(keycode & 0x7f);
|
1539 |
else
|
1540 |
kbd_put_keycode(keycode | 0x80);
|
1541 |
} else {
|
1542 |
/* QEMU console emulation */
|
1543 |
if (down) {
|
1544 |
int numlock = vs->modifiers_state[0x45]; |
1545 |
switch (keycode) {
|
1546 |
case 0x2a: /* Left Shift */ |
1547 |
case 0x36: /* Right Shift */ |
1548 |
case 0x1d: /* Left CTRL */ |
1549 |
case 0x9d: /* Right CTRL */ |
1550 |
case 0x38: /* Left ALT */ |
1551 |
case 0xb8: /* Right ALT */ |
1552 |
break;
|
1553 |
case 0xc8: |
1554 |
kbd_put_keysym(QEMU_KEY_UP); |
1555 |
break;
|
1556 |
case 0xd0: |
1557 |
kbd_put_keysym(QEMU_KEY_DOWN); |
1558 |
break;
|
1559 |
case 0xcb: |
1560 |
kbd_put_keysym(QEMU_KEY_LEFT); |
1561 |
break;
|
1562 |
case 0xcd: |
1563 |
kbd_put_keysym(QEMU_KEY_RIGHT); |
1564 |
break;
|
1565 |
case 0xd3: |
1566 |
kbd_put_keysym(QEMU_KEY_DELETE); |
1567 |
break;
|
1568 |
case 0xc7: |
1569 |
kbd_put_keysym(QEMU_KEY_HOME); |
1570 |
break;
|
1571 |
case 0xcf: |
1572 |
kbd_put_keysym(QEMU_KEY_END); |
1573 |
break;
|
1574 |
case 0xc9: |
1575 |
kbd_put_keysym(QEMU_KEY_PAGEUP); |
1576 |
break;
|
1577 |
case 0xd1: |
1578 |
kbd_put_keysym(QEMU_KEY_PAGEDOWN); |
1579 |
break;
|
1580 |
|
1581 |
case 0x47: |
1582 |
kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
|
1583 |
break;
|
1584 |
case 0x48: |
1585 |
kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
|
1586 |
break;
|
1587 |
case 0x49: |
1588 |
kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
|
1589 |
break;
|
1590 |
case 0x4b: |
1591 |
kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
|
1592 |
break;
|
1593 |
case 0x4c: |
1594 |
kbd_put_keysym('5');
|
1595 |
break;
|
1596 |
case 0x4d: |
1597 |
kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
|
1598 |
break;
|
1599 |
case 0x4f: |
1600 |
kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
|
1601 |
break;
|
1602 |
case 0x50: |
1603 |
kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
|
1604 |
break;
|
1605 |
case 0x51: |
1606 |
kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
|
1607 |
break;
|
1608 |
case 0x52: |
1609 |
kbd_put_keysym('0');
|
1610 |
break;
|
1611 |
case 0x53: |
1612 |
kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
|
1613 |
break;
|
1614 |
|
1615 |
case 0xb5: |
1616 |
kbd_put_keysym('/');
|
1617 |
break;
|
1618 |
case 0x37: |
1619 |
kbd_put_keysym('*');
|
1620 |
break;
|
1621 |
case 0x4a: |
1622 |
kbd_put_keysym('-');
|
1623 |
break;
|
1624 |
case 0x4e: |
1625 |
kbd_put_keysym('+');
|
1626 |
break;
|
1627 |
case 0x9c: |
1628 |
kbd_put_keysym('\n');
|
1629 |
break;
|
1630 |
|
1631 |
default:
|
1632 |
kbd_put_keysym(sym); |
1633 |
break;
|
1634 |
} |
1635 |
} |
1636 |
} |
1637 |
} |
1638 |
|
1639 |
static void key_event(VncState *vs, int down, uint32_t sym) |
1640 |
{ |
1641 |
int keycode;
|
1642 |
int lsym = sym;
|
1643 |
|
1644 |
if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) { |
1645 |
lsym = lsym - 'A' + 'a'; |
1646 |
} |
1647 |
|
1648 |
keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF);
|
1649 |
do_key_event(vs, down, keycode, sym); |
1650 |
} |
1651 |
|
1652 |
static void ext_key_event(VncState *vs, int down, |
1653 |
uint32_t sym, uint16_t keycode) |
1654 |
{ |
1655 |
/* if the user specifies a keyboard layout, always use it */
|
1656 |
if (keyboard_layout)
|
1657 |
key_event(vs, down, sym); |
1658 |
else
|
1659 |
do_key_event(vs, down, keycode, sym); |
1660 |
} |
1661 |
|
1662 |
static void framebuffer_update_request(VncState *vs, int incremental, |
1663 |
int x_position, int y_position, |
1664 |
int w, int h) |
1665 |
{ |
1666 |
if (x_position > ds_get_width(vs->ds))
|
1667 |
x_position = ds_get_width(vs->ds); |
1668 |
if (y_position > ds_get_height(vs->ds))
|
1669 |
y_position = ds_get_height(vs->ds); |
1670 |
if (x_position + w >= ds_get_width(vs->ds))
|
1671 |
w = ds_get_width(vs->ds) - x_position; |
1672 |
if (y_position + h >= ds_get_height(vs->ds))
|
1673 |
h = ds_get_height(vs->ds) - y_position; |
1674 |
|
1675 |
int i;
|
1676 |
vs->need_update = 1;
|
1677 |
if (!incremental) {
|
1678 |
vs->force_update = 1;
|
1679 |
for (i = 0; i < h; i++) { |
1680 |
vnc_set_bits(vs->dirty[y_position + i], |
1681 |
(ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
|
1682 |
} |
1683 |
} |
1684 |
} |
1685 |
|
1686 |
static void send_ext_key_event_ack(VncState *vs) |
1687 |
{ |
1688 |
vnc_write_u8(vs, 0);
|
1689 |
vnc_write_u8(vs, 0);
|
1690 |
vnc_write_u16(vs, 1);
|
1691 |
vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds), |
1692 |
VNC_ENCODING_EXT_KEY_EVENT); |
1693 |
vnc_flush(vs); |
1694 |
} |
1695 |
|
1696 |
static void send_ext_audio_ack(VncState *vs) |
1697 |
{ |
1698 |
vnc_write_u8(vs, 0);
|
1699 |
vnc_write_u8(vs, 0);
|
1700 |
vnc_write_u16(vs, 1);
|
1701 |
vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds), |
1702 |
VNC_ENCODING_AUDIO); |
1703 |
vnc_flush(vs); |
1704 |
} |
1705 |
|
1706 |
static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) |
1707 |
{ |
1708 |
int i;
|
1709 |
unsigned int enc = 0; |
1710 |
|
1711 |
vnc_zlib_init(vs); |
1712 |
vs->features = 0;
|
1713 |
vs->vnc_encoding = 0;
|
1714 |
vs->tight_compression = 9;
|
1715 |
vs->tight_quality = 9;
|
1716 |
vs->absolute = -1;
|
1717 |
|
1718 |
for (i = n_encodings - 1; i >= 0; i--) { |
1719 |
enc = encodings[i]; |
1720 |
switch (enc) {
|
1721 |
case VNC_ENCODING_RAW:
|
1722 |
vs->vnc_encoding = enc; |
1723 |
break;
|
1724 |
case VNC_ENCODING_COPYRECT:
|
1725 |
vs->features |= VNC_FEATURE_COPYRECT_MASK; |
1726 |
break;
|
1727 |
case VNC_ENCODING_HEXTILE:
|
1728 |
vs->features |= VNC_FEATURE_HEXTILE_MASK; |
1729 |
vs->vnc_encoding = enc; |
1730 |
break;
|
1731 |
case VNC_ENCODING_ZLIB:
|
1732 |
vs->features |= VNC_FEATURE_ZLIB_MASK; |
1733 |
vs->vnc_encoding = enc; |
1734 |
break;
|
1735 |
case VNC_ENCODING_DESKTOPRESIZE:
|
1736 |
vs->features |= VNC_FEATURE_RESIZE_MASK; |
1737 |
break;
|
1738 |
case VNC_ENCODING_POINTER_TYPE_CHANGE:
|
1739 |
vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; |
1740 |
break;
|
1741 |
case VNC_ENCODING_EXT_KEY_EVENT:
|
1742 |
send_ext_key_event_ack(vs); |
1743 |
break;
|
1744 |
case VNC_ENCODING_AUDIO:
|
1745 |
send_ext_audio_ack(vs); |
1746 |
break;
|
1747 |
case VNC_ENCODING_WMVi:
|
1748 |
vs->features |= VNC_FEATURE_WMVI_MASK; |
1749 |
break;
|
1750 |
case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: |
1751 |
vs->tight_compression = (enc & 0x0F);
|
1752 |
break;
|
1753 |
case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: |
1754 |
vs->tight_quality = (enc & 0x0F);
|
1755 |
break;
|
1756 |
default:
|
1757 |
VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
|
1758 |
break;
|
1759 |
} |
1760 |
} |
1761 |
|
1762 |
check_pointer_type_change(vs, kbd_mouse_is_absolute()); |
1763 |
} |
1764 |
|
1765 |
static void set_pixel_conversion(VncState *vs) |
1766 |
{ |
1767 |
if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
|
1768 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) && |
1769 |
!memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
|
1770 |
vs->write_pixels = vnc_write_pixels_copy; |
1771 |
switch (vs->ds->surface->pf.bits_per_pixel) {
|
1772 |
case 8: |
1773 |
vs->send_hextile_tile = send_hextile_tile_8; |
1774 |
break;
|
1775 |
case 16: |
1776 |
vs->send_hextile_tile = send_hextile_tile_16; |
1777 |
break;
|
1778 |
case 32: |
1779 |
vs->send_hextile_tile = send_hextile_tile_32; |
1780 |
break;
|
1781 |
} |
1782 |
} else {
|
1783 |
vs->write_pixels = vnc_write_pixels_generic; |
1784 |
switch (vs->ds->surface->pf.bits_per_pixel) {
|
1785 |
case 8: |
1786 |
vs->send_hextile_tile = send_hextile_tile_generic_8; |
1787 |
break;
|
1788 |
case 16: |
1789 |
vs->send_hextile_tile = send_hextile_tile_generic_16; |
1790 |
break;
|
1791 |
case 32: |
1792 |
vs->send_hextile_tile = send_hextile_tile_generic_32; |
1793 |
break;
|
1794 |
} |
1795 |
} |
1796 |
} |
1797 |
|
1798 |
static void set_pixel_format(VncState *vs, |
1799 |
int bits_per_pixel, int depth, |
1800 |
int big_endian_flag, int true_color_flag, |
1801 |
int red_max, int green_max, int blue_max, |
1802 |
int red_shift, int green_shift, int blue_shift) |
1803 |
{ |
1804 |
if (!true_color_flag) {
|
1805 |
vnc_client_error(vs); |
1806 |
return;
|
1807 |
} |
1808 |
|
1809 |
vs->clientds = *(vs->vd->guest.ds); |
1810 |
vs->clientds.pf.rmax = red_max; |
1811 |
count_bits(vs->clientds.pf.rbits, red_max); |
1812 |
vs->clientds.pf.rshift = red_shift; |
1813 |
vs->clientds.pf.rmask = red_max << red_shift; |
1814 |
vs->clientds.pf.gmax = green_max; |
1815 |
count_bits(vs->clientds.pf.gbits, green_max); |
1816 |
vs->clientds.pf.gshift = green_shift; |
1817 |
vs->clientds.pf.gmask = green_max << green_shift; |
1818 |
vs->clientds.pf.bmax = blue_max; |
1819 |
count_bits(vs->clientds.pf.bbits, blue_max); |
1820 |
vs->clientds.pf.bshift = blue_shift; |
1821 |
vs->clientds.pf.bmask = blue_max << blue_shift; |
1822 |
vs->clientds.pf.bits_per_pixel = bits_per_pixel; |
1823 |
vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
|
1824 |
vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; |
1825 |
vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
|
1826 |
|
1827 |
set_pixel_conversion(vs); |
1828 |
|
1829 |
vga_hw_invalidate(); |
1830 |
vga_hw_update(); |
1831 |
} |
1832 |
|
1833 |
static void pixel_format_message (VncState *vs) { |
1834 |
char pad[3] = { 0, 0, 0 }; |
1835 |
|
1836 |
vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
|
1837 |
vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
|
1838 |
|
1839 |
#ifdef HOST_WORDS_BIGENDIAN
|
1840 |
vnc_write_u8(vs, 1); /* big-endian-flag */ |
1841 |
#else
|
1842 |
vnc_write_u8(vs, 0); /* big-endian-flag */ |
1843 |
#endif
|
1844 |
vnc_write_u8(vs, 1); /* true-color-flag */ |
1845 |
vnc_write_u16(vs, vs->ds->surface->pf.rmax); /* red-max */
|
1846 |
vnc_write_u16(vs, vs->ds->surface->pf.gmax); /* green-max */
|
1847 |
vnc_write_u16(vs, vs->ds->surface->pf.bmax); /* blue-max */
|
1848 |
vnc_write_u8(vs, vs->ds->surface->pf.rshift); /* red-shift */
|
1849 |
vnc_write_u8(vs, vs->ds->surface->pf.gshift); /* green-shift */
|
1850 |
vnc_write_u8(vs, vs->ds->surface->pf.bshift); /* blue-shift */
|
1851 |
if (vs->ds->surface->pf.bits_per_pixel == 32) |
1852 |
vs->send_hextile_tile = send_hextile_tile_32; |
1853 |
else if (vs->ds->surface->pf.bits_per_pixel == 16) |
1854 |
vs->send_hextile_tile = send_hextile_tile_16; |
1855 |
else if (vs->ds->surface->pf.bits_per_pixel == 8) |
1856 |
vs->send_hextile_tile = send_hextile_tile_8; |
1857 |
vs->clientds = *(vs->ds->surface); |
1858 |
vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG; |
1859 |
vs->write_pixels = vnc_write_pixels_copy; |
1860 |
|
1861 |
vnc_write(vs, pad, 3); /* padding */ |
1862 |
} |
1863 |
|
1864 |
static void vnc_dpy_setdata(DisplayState *ds) |
1865 |
{ |
1866 |
/* We don't have to do anything */
|
1867 |
} |
1868 |
|
1869 |
static void vnc_colordepth(VncState *vs) |
1870 |
{ |
1871 |
if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
|
1872 |
/* Sending a WMVi message to notify the client*/
|
1873 |
vnc_write_u8(vs, 0); /* msg id */ |
1874 |
vnc_write_u8(vs, 0);
|
1875 |
vnc_write_u16(vs, 1); /* number of rects */ |
1876 |
vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), |
1877 |
ds_get_height(vs->ds), VNC_ENCODING_WMVi); |
1878 |
pixel_format_message(vs); |
1879 |
vnc_flush(vs); |
1880 |
} else {
|
1881 |
set_pixel_conversion(vs); |
1882 |
} |
1883 |
} |
1884 |
|
1885 |
static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) |
1886 |
{ |
1887 |
int i;
|
1888 |
uint16_t limit; |
1889 |
VncDisplay *vd = vs->vd; |
1890 |
|
1891 |
if (data[0] > 3) { |
1892 |
vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; |
1893 |
if (!qemu_timer_expired(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval))
|
1894 |
qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval); |
1895 |
} |
1896 |
|
1897 |
switch (data[0]) { |
1898 |
case 0: |
1899 |
if (len == 1) |
1900 |
return 20; |
1901 |
|
1902 |
set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), |
1903 |
read_u8(data, 6), read_u8(data, 7), |
1904 |
read_u16(data, 8), read_u16(data, 10), |
1905 |
read_u16(data, 12), read_u8(data, 14), |
1906 |
read_u8(data, 15), read_u8(data, 16)); |
1907 |
break;
|
1908 |
case 2: |
1909 |
if (len == 1) |
1910 |
return 4; |
1911 |
|
1912 |
if (len == 4) { |
1913 |
limit = read_u16(data, 2);
|
1914 |
if (limit > 0) |
1915 |
return 4 + (limit * 4); |
1916 |
} else
|
1917 |
limit = read_u16(data, 2);
|
1918 |
|
1919 |
for (i = 0; i < limit; i++) { |
1920 |
int32_t val = read_s32(data, 4 + (i * 4)); |
1921 |
memcpy(data + 4 + (i * 4), &val, sizeof(val)); |
1922 |
} |
1923 |
|
1924 |
set_encodings(vs, (int32_t *)(data + 4), limit);
|
1925 |
break;
|
1926 |
case 3: |
1927 |
if (len == 1) |
1928 |
return 10; |
1929 |
|
1930 |
framebuffer_update_request(vs, |
1931 |
read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), |
1932 |
read_u16(data, 6), read_u16(data, 8)); |
1933 |
break;
|
1934 |
case 4: |
1935 |
if (len == 1) |
1936 |
return 8; |
1937 |
|
1938 |
key_event(vs, read_u8(data, 1), read_u32(data, 4)); |
1939 |
break;
|
1940 |
case 5: |
1941 |
if (len == 1) |
1942 |
return 6; |
1943 |
|
1944 |
pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); |
1945 |
break;
|
1946 |
case 6: |
1947 |
if (len == 1) |
1948 |
return 8; |
1949 |
|
1950 |
if (len == 8) { |
1951 |
uint32_t dlen = read_u32(data, 4);
|
1952 |
if (dlen > 0) |
1953 |
return 8 + dlen; |
1954 |
} |
1955 |
|
1956 |
client_cut_text(vs, read_u32(data, 4), data + 8); |
1957 |
break;
|
1958 |
case 255: |
1959 |
if (len == 1) |
1960 |
return 2; |
1961 |
|
1962 |
switch (read_u8(data, 1)) { |
1963 |
case 0: |
1964 |
if (len == 2) |
1965 |
return 12; |
1966 |
|
1967 |
ext_key_event(vs, read_u16(data, 2),
|
1968 |
read_u32(data, 4), read_u32(data, 8)); |
1969 |
break;
|
1970 |
case 1: |
1971 |
if (len == 2) |
1972 |
return 4; |
1973 |
|
1974 |
switch (read_u16 (data, 2)) { |
1975 |
case 0: |
1976 |
audio_add(vs); |
1977 |
break;
|
1978 |
case 1: |
1979 |
audio_del(vs); |
1980 |
break;
|
1981 |
case 2: |
1982 |
if (len == 4) |
1983 |
return 10; |
1984 |
switch (read_u8(data, 4)) { |
1985 |
case 0: vs->as.fmt = AUD_FMT_U8; break; |
1986 |
case 1: vs->as.fmt = AUD_FMT_S8; break; |
1987 |
case 2: vs->as.fmt = AUD_FMT_U16; break; |
1988 |
case 3: vs->as.fmt = AUD_FMT_S16; break; |
1989 |
case 4: vs->as.fmt = AUD_FMT_U32; break; |
1990 |
case 5: vs->as.fmt = AUD_FMT_S32; break; |
1991 |
default:
|
1992 |
printf("Invalid audio format %d\n", read_u8(data, 4)); |
1993 |
vnc_client_error(vs); |
1994 |
break;
|
1995 |
} |
1996 |
vs->as.nchannels = read_u8(data, 5);
|
1997 |
if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { |
1998 |
printf("Invalid audio channel coount %d\n",
|
1999 |
read_u8(data, 5));
|
2000 |
vnc_client_error(vs); |
2001 |
break;
|
2002 |
} |
2003 |
vs->as.freq = read_u32(data, 6);
|
2004 |
break;
|
2005 |
default:
|
2006 |
printf ("Invalid audio message %d\n", read_u8(data, 4)); |
2007 |
vnc_client_error(vs); |
2008 |
break;
|
2009 |
} |
2010 |
break;
|
2011 |
|
2012 |
default:
|
2013 |
printf("Msg: %d\n", read_u16(data, 0)); |
2014 |
vnc_client_error(vs); |
2015 |
break;
|
2016 |
} |
2017 |
break;
|
2018 |
default:
|
2019 |
printf("Msg: %d\n", data[0]); |
2020 |
vnc_client_error(vs); |
2021 |
break;
|
2022 |
} |
2023 |
|
2024 |
vnc_read_when(vs, protocol_client_msg, 1);
|
2025 |
return 0; |
2026 |
} |
2027 |
|
2028 |
static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) |
2029 |
{ |
2030 |
char buf[1024]; |
2031 |
int size;
|
2032 |
|
2033 |
vnc_write_u16(vs, ds_get_width(vs->ds)); |
2034 |
vnc_write_u16(vs, ds_get_height(vs->ds)); |
2035 |
|
2036 |
pixel_format_message(vs); |
2037 |
|
2038 |
if (qemu_name)
|
2039 |
size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); |
2040 |
else
|
2041 |
size = snprintf(buf, sizeof(buf), "QEMU"); |
2042 |
|
2043 |
vnc_write_u32(vs, size); |
2044 |
vnc_write(vs, buf, size); |
2045 |
vnc_flush(vs); |
2046 |
|
2047 |
vnc_read_when(vs, protocol_client_msg, 1);
|
2048 |
|
2049 |
return 0; |
2050 |
} |
2051 |
|
2052 |
void start_client_init(VncState *vs)
|
2053 |
{ |
2054 |
vnc_read_when(vs, protocol_client_init, 1);
|
2055 |
} |
2056 |
|
2057 |
static void make_challenge(VncState *vs) |
2058 |
{ |
2059 |
int i;
|
2060 |
|
2061 |
srand(time(NULL)+getpid()+getpid()*987654+rand()); |
2062 |
|
2063 |
for (i = 0 ; i < sizeof(vs->challenge) ; i++) |
2064 |
vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); |
2065 |
} |
2066 |
|
2067 |
static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) |
2068 |
{ |
2069 |
unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; |
2070 |
int i, j, pwlen;
|
2071 |
unsigned char key[8]; |
2072 |
|
2073 |
if (!vs->vd->password || !vs->vd->password[0]) { |
2074 |
VNC_DEBUG("No password configured on server");
|
2075 |
vnc_write_u32(vs, 1); /* Reject auth */ |
2076 |
if (vs->minor >= 8) { |
2077 |
static const char err[] = "Authentication failed"; |
2078 |
vnc_write_u32(vs, sizeof(err));
|
2079 |
vnc_write(vs, err, sizeof(err));
|
2080 |
} |
2081 |
vnc_flush(vs); |
2082 |
vnc_client_error(vs); |
2083 |
return 0; |
2084 |
} |
2085 |
|
2086 |
memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); |
2087 |
|
2088 |
/* Calculate the expected challenge response */
|
2089 |
pwlen = strlen(vs->vd->password); |
2090 |
for (i=0; i<sizeof(key); i++) |
2091 |
key[i] = i<pwlen ? vs->vd->password[i] : 0;
|
2092 |
deskey(key, EN0); |
2093 |
for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8) |
2094 |
des(response+j, response+j); |
2095 |
|
2096 |
/* Compare expected vs actual challenge response */
|
2097 |
if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { |
2098 |
VNC_DEBUG("Client challenge reponse did not match\n");
|
2099 |
vnc_write_u32(vs, 1); /* Reject auth */ |
2100 |
if (vs->minor >= 8) { |
2101 |
static const char err[] = "Authentication failed"; |
2102 |
vnc_write_u32(vs, sizeof(err));
|
2103 |
vnc_write(vs, err, sizeof(err));
|
2104 |
} |
2105 |
vnc_flush(vs); |
2106 |
vnc_client_error(vs); |
2107 |
} else {
|
2108 |
VNC_DEBUG("Accepting VNC challenge response\n");
|
2109 |
vnc_write_u32(vs, 0); /* Accept auth */ |
2110 |
vnc_flush(vs); |
2111 |
|
2112 |
start_client_init(vs); |
2113 |
} |
2114 |
return 0; |
2115 |
} |
2116 |
|
2117 |
void start_auth_vnc(VncState *vs)
|
2118 |
{ |
2119 |
make_challenge(vs); |
2120 |
/* Send client a 'random' challenge */
|
2121 |
vnc_write(vs, vs->challenge, sizeof(vs->challenge));
|
2122 |
vnc_flush(vs); |
2123 |
|
2124 |
vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
|
2125 |
} |
2126 |
|
2127 |
|
2128 |
static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) |
2129 |
{ |
2130 |
/* We only advertise 1 auth scheme at a time, so client
|
2131 |
* must pick the one we sent. Verify this */
|
2132 |
if (data[0] != vs->vd->auth) { /* Reject auth */ |
2133 |
VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); |
2134 |
vnc_write_u32(vs, 1);
|
2135 |
if (vs->minor >= 8) { |
2136 |
static const char err[] = "Authentication failed"; |
2137 |
vnc_write_u32(vs, sizeof(err));
|
2138 |
vnc_write(vs, err, sizeof(err));
|
2139 |
} |
2140 |
vnc_client_error(vs); |
2141 |
} else { /* Accept requested auth */ |
2142 |
VNC_DEBUG("Client requested auth %d\n", (int)data[0]); |
2143 |
switch (vs->vd->auth) {
|
2144 |
case VNC_AUTH_NONE:
|
2145 |
VNC_DEBUG("Accept auth none\n");
|
2146 |
if (vs->minor >= 8) { |
2147 |
vnc_write_u32(vs, 0); /* Accept auth completion */ |
2148 |
vnc_flush(vs); |
2149 |
} |
2150 |
start_client_init(vs); |
2151 |
break;
|
2152 |
|
2153 |
case VNC_AUTH_VNC:
|
2154 |
VNC_DEBUG("Start VNC auth\n");
|
2155 |
start_auth_vnc(vs); |
2156 |
break;
|
2157 |
|
2158 |
#ifdef CONFIG_VNC_TLS
|
2159 |
case VNC_AUTH_VENCRYPT:
|
2160 |
VNC_DEBUG("Accept VeNCrypt auth\n");;
|
2161 |
start_auth_vencrypt(vs); |
2162 |
break;
|
2163 |
#endif /* CONFIG_VNC_TLS */ |
2164 |
|
2165 |
#ifdef CONFIG_VNC_SASL
|
2166 |
case VNC_AUTH_SASL:
|
2167 |
VNC_DEBUG("Accept SASL auth\n");
|
2168 |
start_auth_sasl(vs); |
2169 |
break;
|
2170 |
#endif /* CONFIG_VNC_SASL */ |
2171 |
|
2172 |
default: /* Should not be possible, but just in case */ |
2173 |
VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
|
2174 |
vnc_write_u8(vs, 1);
|
2175 |
if (vs->minor >= 8) { |
2176 |
static const char err[] = "Authentication failed"; |
2177 |
vnc_write_u32(vs, sizeof(err));
|
2178 |
vnc_write(vs, err, sizeof(err));
|
2179 |
} |
2180 |
vnc_client_error(vs); |
2181 |
} |
2182 |
} |
2183 |
return 0; |
2184 |
} |
2185 |
|
2186 |
static int protocol_version(VncState *vs, uint8_t *version, size_t len) |
2187 |
{ |
2188 |
char local[13]; |
2189 |
|
2190 |
memcpy(local, version, 12);
|
2191 |
local[12] = 0; |
2192 |
|
2193 |
if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { |
2194 |
VNC_DEBUG("Malformed protocol version %s\n", local);
|
2195 |
vnc_client_error(vs); |
2196 |
return 0; |
2197 |
} |
2198 |
VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
|
2199 |
if (vs->major != 3 || |
2200 |
(vs->minor != 3 &&
|
2201 |
vs->minor != 4 &&
|
2202 |
vs->minor != 5 &&
|
2203 |
vs->minor != 7 &&
|
2204 |
vs->minor != 8)) {
|
2205 |
VNC_DEBUG("Unsupported client version\n");
|
2206 |
vnc_write_u32(vs, VNC_AUTH_INVALID); |
2207 |
vnc_flush(vs); |
2208 |
vnc_client_error(vs); |
2209 |
return 0; |
2210 |
} |
2211 |
/* Some broken clients report v3.4 or v3.5, which spec requires to be treated
|
2212 |
* as equivalent to v3.3 by servers
|
2213 |
*/
|
2214 |
if (vs->minor == 4 || vs->minor == 5) |
2215 |
vs->minor = 3;
|
2216 |
|
2217 |
if (vs->minor == 3) { |
2218 |
if (vs->vd->auth == VNC_AUTH_NONE) {
|
2219 |
VNC_DEBUG("Tell client auth none\n");
|
2220 |
vnc_write_u32(vs, vs->vd->auth); |
2221 |
vnc_flush(vs); |
2222 |
start_client_init(vs); |
2223 |
} else if (vs->vd->auth == VNC_AUTH_VNC) { |
2224 |
VNC_DEBUG("Tell client VNC auth\n");
|
2225 |
vnc_write_u32(vs, vs->vd->auth); |
2226 |
vnc_flush(vs); |
2227 |
start_auth_vnc(vs); |
2228 |
} else {
|
2229 |
VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
|
2230 |
vnc_write_u32(vs, VNC_AUTH_INVALID); |
2231 |
vnc_flush(vs); |
2232 |
vnc_client_error(vs); |
2233 |
} |
2234 |
} else {
|
2235 |
VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
|
2236 |
vnc_write_u8(vs, 1); /* num auth */ |
2237 |
vnc_write_u8(vs, vs->vd->auth); |
2238 |
vnc_read_when(vs, protocol_client_auth, 1);
|
2239 |
vnc_flush(vs); |
2240 |
} |
2241 |
|
2242 |
return 0; |
2243 |
} |
2244 |
|
2245 |
static int vnc_refresh_server_surface(VncDisplay *vd) |
2246 |
{ |
2247 |
int y;
|
2248 |
uint8_t *guest_row; |
2249 |
uint8_t *server_row; |
2250 |
int cmp_bytes;
|
2251 |
uint32_t width_mask[VNC_DIRTY_WORDS]; |
2252 |
VncState *vs = NULL;
|
2253 |
int has_dirty = 0; |
2254 |
|
2255 |
/*
|
2256 |
* Walk through the guest dirty map.
|
2257 |
* Check and copy modified bits from guest to server surface.
|
2258 |
* Update server dirty map.
|
2259 |
*/
|
2260 |
vnc_set_bits(width_mask, (ds_get_width(vd->ds) / 16), VNC_DIRTY_WORDS);
|
2261 |
cmp_bytes = 16 * ds_get_bytes_per_pixel(vd->ds);
|
2262 |
guest_row = vd->guest.ds->data; |
2263 |
server_row = vd->server->data; |
2264 |
for (y = 0; y < vd->guest.ds->height; y++) { |
2265 |
if (vnc_and_bits(vd->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) {
|
2266 |
int x;
|
2267 |
uint8_t *guest_ptr; |
2268 |
uint8_t *server_ptr; |
2269 |
|
2270 |
guest_ptr = guest_row; |
2271 |
server_ptr = server_row; |
2272 |
|
2273 |
for (x = 0; x < vd->guest.ds->width; |
2274 |
x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
|
2275 |
if (!vnc_get_bit(vd->guest.dirty[y], (x / 16))) |
2276 |
continue;
|
2277 |
vnc_clear_bit(vd->guest.dirty[y], (x / 16));
|
2278 |
if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) |
2279 |
continue;
|
2280 |
memcpy(server_ptr, guest_ptr, cmp_bytes); |
2281 |
vs = vd->clients; |
2282 |
while (vs != NULL) { |
2283 |
vnc_set_bit(vs->dirty[y], (x / 16));
|
2284 |
vs = vs->next; |
2285 |
} |
2286 |
has_dirty++; |
2287 |
} |
2288 |
} |
2289 |
guest_row += ds_get_linesize(vd->ds); |
2290 |
server_row += ds_get_linesize(vd->ds); |
2291 |
} |
2292 |
return has_dirty;
|
2293 |
} |
2294 |
|
2295 |
static void vnc_refresh(void *opaque) |
2296 |
{ |
2297 |
VncDisplay *vd = opaque; |
2298 |
VncState *vs = NULL;
|
2299 |
int has_dirty = 0, rects = 0; |
2300 |
|
2301 |
vga_hw_update(); |
2302 |
|
2303 |
has_dirty = vnc_refresh_server_surface(vd); |
2304 |
|
2305 |
vs = vd->clients; |
2306 |
while (vs != NULL) { |
2307 |
rects += vnc_update_client(vs, has_dirty); |
2308 |
vs = vs->next; |
2309 |
} |
2310 |
/* vd->timer could be NULL now if the last client disconnected,
|
2311 |
* in this case don't update the timer */
|
2312 |
if (vd->timer == NULL) |
2313 |
return;
|
2314 |
|
2315 |
if (has_dirty && rects) {
|
2316 |
vd->timer_interval /= 2;
|
2317 |
if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE)
|
2318 |
vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; |
2319 |
} else {
|
2320 |
vd->timer_interval += VNC_REFRESH_INTERVAL_INC; |
2321 |
if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX)
|
2322 |
vd->timer_interval = VNC_REFRESH_INTERVAL_MAX; |
2323 |
} |
2324 |
qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval); |
2325 |
} |
2326 |
|
2327 |
static void vnc_init_timer(VncDisplay *vd) |
2328 |
{ |
2329 |
vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; |
2330 |
if (vd->timer == NULL && vd->clients != NULL) { |
2331 |
vd->timer = qemu_new_timer(rt_clock, vnc_refresh, vd); |
2332 |
vnc_refresh(vd); |
2333 |
} |
2334 |
} |
2335 |
|
2336 |
static void vnc_remove_timer(VncDisplay *vd) |
2337 |
{ |
2338 |
if (vd->timer != NULL && vd->clients == NULL) { |
2339 |
qemu_del_timer(vd->timer); |
2340 |
qemu_free_timer(vd->timer); |
2341 |
vd->timer = NULL;
|
2342 |
} |
2343 |
} |
2344 |
|
2345 |
static void vnc_connect(VncDisplay *vd, int csock) |
2346 |
{ |
2347 |
VncState *vs = qemu_mallocz(sizeof(VncState));
|
2348 |
vs->csock = csock; |
2349 |
|
2350 |
VNC_DEBUG("New client on socket %d\n", csock);
|
2351 |
dcl->idle = 0;
|
2352 |
socket_set_nonblock(vs->csock); |
2353 |
qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); |
2354 |
|
2355 |
vs->vd = vd; |
2356 |
vs->ds = vd->ds; |
2357 |
vs->last_x = -1;
|
2358 |
vs->last_y = -1;
|
2359 |
|
2360 |
vs->as.freq = 44100;
|
2361 |
vs->as.nchannels = 2;
|
2362 |
vs->as.fmt = AUD_FMT_S16; |
2363 |
vs->as.endianness = 0;
|
2364 |
|
2365 |
vs->next = vd->clients; |
2366 |
vd->clients = vs; |
2367 |
|
2368 |
vga_hw_update(); |
2369 |
|
2370 |
vnc_write(vs, "RFB 003.008\n", 12); |
2371 |
vnc_flush(vs); |
2372 |
vnc_read_when(vs, protocol_version, 12);
|
2373 |
reset_keys(vs); |
2374 |
|
2375 |
vnc_init_timer(vd); |
2376 |
|
2377 |
/* vs might be free()ed here */
|
2378 |
} |
2379 |
|
2380 |
static void vnc_listen_read(void *opaque) |
2381 |
{ |
2382 |
VncDisplay *vs = opaque; |
2383 |
struct sockaddr_in addr;
|
2384 |
socklen_t addrlen = sizeof(addr);
|
2385 |
|
2386 |
/* Catch-up */
|
2387 |
vga_hw_update(); |
2388 |
|
2389 |
int csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); |
2390 |
if (csock != -1) { |
2391 |
vnc_connect(vs, csock); |
2392 |
} |
2393 |
} |
2394 |
|
2395 |
void vnc_display_init(DisplayState *ds)
|
2396 |
{ |
2397 |
VncDisplay *vs = qemu_mallocz(sizeof(*vs));
|
2398 |
|
2399 |
dcl = qemu_mallocz(sizeof(DisplayChangeListener));
|
2400 |
|
2401 |
ds->opaque = vs; |
2402 |
dcl->idle = 1;
|
2403 |
vnc_display = vs; |
2404 |
|
2405 |
vs->lsock = -1;
|
2406 |
|
2407 |
vs->ds = ds; |
2408 |
|
2409 |
if (keyboard_layout)
|
2410 |
vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); |
2411 |
else
|
2412 |
vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
|
2413 |
|
2414 |
if (!vs->kbd_layout)
|
2415 |
exit(1);
|
2416 |
|
2417 |
dcl->dpy_copy = vnc_dpy_copy; |
2418 |
dcl->dpy_update = vnc_dpy_update; |
2419 |
dcl->dpy_resize = vnc_dpy_resize; |
2420 |
dcl->dpy_setdata = vnc_dpy_setdata; |
2421 |
register_displaychangelistener(ds, dcl); |
2422 |
} |
2423 |
|
2424 |
|
2425 |
void vnc_display_close(DisplayState *ds)
|
2426 |
{ |
2427 |
VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; |
2428 |
|
2429 |
if (!vs)
|
2430 |
return;
|
2431 |
if (vs->display) {
|
2432 |
qemu_free(vs->display); |
2433 |
vs->display = NULL;
|
2434 |
} |
2435 |
if (vs->lsock != -1) { |
2436 |
qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL); |
2437 |
close(vs->lsock); |
2438 |
vs->lsock = -1;
|
2439 |
} |
2440 |
vs->auth = VNC_AUTH_INVALID; |
2441 |
#ifdef CONFIG_VNC_TLS
|
2442 |
vs->subauth = VNC_AUTH_INVALID; |
2443 |
vs->tls.x509verify = 0;
|
2444 |
#endif
|
2445 |
} |
2446 |
|
2447 |
int vnc_display_password(DisplayState *ds, const char *password) |
2448 |
{ |
2449 |
VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; |
2450 |
|
2451 |
if (!vs) {
|
2452 |
return -1; |
2453 |
} |
2454 |
|
2455 |
if (vs->password) {
|
2456 |
qemu_free(vs->password); |
2457 |
vs->password = NULL;
|
2458 |
} |
2459 |
if (password && password[0]) { |
2460 |
if (!(vs->password = qemu_strdup(password)))
|
2461 |
return -1; |
2462 |
if (vs->auth == VNC_AUTH_NONE) {
|
2463 |
vs->auth = VNC_AUTH_VNC; |
2464 |
} |
2465 |
} else {
|
2466 |
vs->auth = VNC_AUTH_NONE; |
2467 |
} |
2468 |
|
2469 |
return 0; |
2470 |
} |
2471 |
|
2472 |
char *vnc_display_local_addr(DisplayState *ds)
|
2473 |
{ |
2474 |
VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; |
2475 |
|
2476 |
return vnc_socket_local_addr("%s:%s", vs->lsock); |
2477 |
} |
2478 |
|
2479 |
int vnc_display_open(DisplayState *ds, const char *display) |
2480 |
{ |
2481 |
VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; |
2482 |
const char *options; |
2483 |
int password = 0; |
2484 |
int reverse = 0; |
2485 |
int to_port = 0; |
2486 |
#ifdef CONFIG_VNC_TLS
|
2487 |
int tls = 0, x509 = 0; |
2488 |
#endif
|
2489 |
#ifdef CONFIG_VNC_SASL
|
2490 |
int sasl = 0; |
2491 |
int saslErr;
|
2492 |
#endif
|
2493 |
int acl = 0; |
2494 |
|
2495 |
if (!vnc_display)
|
2496 |
return -1; |
2497 |
vnc_display_close(ds); |
2498 |
if (strcmp(display, "none") == 0) |
2499 |
return 0; |
2500 |
|
2501 |
if (!(vs->display = strdup(display)))
|
2502 |
return -1; |
2503 |
|
2504 |
options = display; |
2505 |
while ((options = strchr(options, ','))) { |
2506 |
options++; |
2507 |
if (strncmp(options, "password", 8) == 0) { |
2508 |
password = 1; /* Require password auth */ |
2509 |
} else if (strncmp(options, "reverse", 7) == 0) { |
2510 |
reverse = 1;
|
2511 |
} else if (strncmp(options, "to=", 3) == 0) { |
2512 |
to_port = atoi(options+3) + 5900; |
2513 |
#ifdef CONFIG_VNC_SASL
|
2514 |
} else if (strncmp(options, "sasl", 4) == 0) { |
2515 |
sasl = 1; /* Require SASL auth */ |
2516 |
#endif
|
2517 |
#ifdef CONFIG_VNC_TLS
|
2518 |
} else if (strncmp(options, "tls", 3) == 0) { |
2519 |
tls = 1; /* Require TLS */ |
2520 |
} else if (strncmp(options, "x509", 4) == 0) { |
2521 |
char *start, *end;
|
2522 |
x509 = 1; /* Require x509 certificates */ |
2523 |
if (strncmp(options, "x509verify", 10) == 0) |
2524 |
vs->tls.x509verify = 1; /* ...and verify client certs */ |
2525 |
|
2526 |
/* Now check for 'x509=/some/path' postfix
|
2527 |
* and use that to setup x509 certificate/key paths */
|
2528 |
start = strchr(options, '=');
|
2529 |
end = strchr(options, ',');
|
2530 |
if (start && (!end || (start < end))) {
|
2531 |
int len = end ? end-(start+1) : strlen(start+1); |
2532 |
char *path = qemu_strndup(start + 1, len); |
2533 |
|
2534 |
VNC_DEBUG("Trying certificate path '%s'\n", path);
|
2535 |
if (vnc_tls_set_x509_creds_dir(vs, path) < 0) { |
2536 |
fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
|
2537 |
qemu_free(path); |
2538 |
qemu_free(vs->display); |
2539 |
vs->display = NULL;
|
2540 |
return -1; |
2541 |
} |
2542 |
qemu_free(path); |
2543 |
} else {
|
2544 |
fprintf(stderr, "No certificate path provided\n");
|
2545 |
qemu_free(vs->display); |
2546 |
vs->display = NULL;
|
2547 |
return -1; |
2548 |
} |
2549 |
#endif
|
2550 |
} else if (strncmp(options, "acl", 3) == 0) { |
2551 |
acl = 1;
|
2552 |
} |
2553 |
} |
2554 |
|
2555 |
#ifdef CONFIG_VNC_TLS
|
2556 |
if (acl && x509 && vs->tls.x509verify) {
|
2557 |
if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) { |
2558 |
fprintf(stderr, "Failed to create x509 dname ACL\n");
|
2559 |
exit(1);
|
2560 |
} |
2561 |
} |
2562 |
#endif
|
2563 |
#ifdef CONFIG_VNC_SASL
|
2564 |
if (acl && sasl) {
|
2565 |
if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) { |
2566 |
fprintf(stderr, "Failed to create username ACL\n");
|
2567 |
exit(1);
|
2568 |
} |
2569 |
} |
2570 |
#endif
|
2571 |
|
2572 |
/*
|
2573 |
* Combinations we support here:
|
2574 |
*
|
2575 |
* - no-auth (clear text, no auth)
|
2576 |
* - password (clear text, weak auth)
|
2577 |
* - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
|
2578 |
* - tls (encrypt, weak anonymous creds, no auth)
|
2579 |
* - tls + password (encrypt, weak anonymous creds, weak auth)
|
2580 |
* - tls + sasl (encrypt, weak anonymous creds, good auth)
|
2581 |
* - tls + x509 (encrypt, good x509 creds, no auth)
|
2582 |
* - tls + x509 + password (encrypt, good x509 creds, weak auth)
|
2583 |
* - tls + x509 + sasl (encrypt, good x509 creds, good auth)
|
2584 |
*
|
2585 |
* NB1. TLS is a stackable auth scheme.
|
2586 |
* NB2. the x509 schemes have option to validate a client cert dname
|
2587 |
*/
|
2588 |
if (password) {
|
2589 |
#ifdef CONFIG_VNC_TLS
|
2590 |
if (tls) {
|
2591 |
vs->auth = VNC_AUTH_VENCRYPT; |
2592 |
if (x509) {
|
2593 |
VNC_DEBUG("Initializing VNC server with x509 password auth\n");
|
2594 |
vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; |
2595 |
} else {
|
2596 |
VNC_DEBUG("Initializing VNC server with TLS password auth\n");
|
2597 |
vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; |
2598 |
} |
2599 |
} else {
|
2600 |
#endif /* CONFIG_VNC_TLS */ |
2601 |
VNC_DEBUG("Initializing VNC server with password auth\n");
|
2602 |
vs->auth = VNC_AUTH_VNC; |
2603 |
#ifdef CONFIG_VNC_TLS
|
2604 |
vs->subauth = VNC_AUTH_INVALID; |
2605 |
} |
2606 |
#endif /* CONFIG_VNC_TLS */ |
2607 |
#ifdef CONFIG_VNC_SASL
|
2608 |
} else if (sasl) { |
2609 |
#ifdef CONFIG_VNC_TLS
|
2610 |
if (tls) {
|
2611 |
vs->auth = VNC_AUTH_VENCRYPT; |
2612 |
if (x509) {
|
2613 |
VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
|
2614 |
vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; |
2615 |
} else {
|
2616 |
VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
|
2617 |
vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; |
2618 |
} |
2619 |
} else {
|
2620 |
#endif /* CONFIG_VNC_TLS */ |
2621 |
VNC_DEBUG("Initializing VNC server with SASL auth\n");
|
2622 |
vs->auth = VNC_AUTH_SASL; |
2623 |
#ifdef CONFIG_VNC_TLS
|
2624 |
vs->subauth = VNC_AUTH_INVALID; |
2625 |
} |
2626 |
#endif /* CONFIG_VNC_TLS */ |
2627 |
#endif /* CONFIG_VNC_SASL */ |
2628 |
} else {
|
2629 |
#ifdef CONFIG_VNC_TLS
|
2630 |
if (tls) {
|
2631 |
vs->auth = VNC_AUTH_VENCRYPT; |
2632 |
if (x509) {
|
2633 |
VNC_DEBUG("Initializing VNC server with x509 no auth\n");
|
2634 |
vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; |
2635 |
} else {
|
2636 |
VNC_DEBUG("Initializing VNC server with TLS no auth\n");
|
2637 |
vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; |
2638 |
} |
2639 |
} else {
|
2640 |
#endif
|
2641 |
VNC_DEBUG("Initializing VNC server with no auth\n");
|
2642 |
vs->auth = VNC_AUTH_NONE; |
2643 |
#ifdef CONFIG_VNC_TLS
|
2644 |
vs->subauth = VNC_AUTH_INVALID; |
2645 |
} |
2646 |
#endif
|
2647 |
} |
2648 |
|
2649 |
#ifdef CONFIG_VNC_SASL
|
2650 |
if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { |
2651 |
fprintf(stderr, "Failed to initialize SASL auth %s",
|
2652 |
sasl_errstring(saslErr, NULL, NULL)); |
2653 |
free(vs->display); |
2654 |
vs->display = NULL;
|
2655 |
return -1; |
2656 |
} |
2657 |
#endif
|
2658 |
|
2659 |
if (reverse) {
|
2660 |
/* connect to viewer */
|
2661 |
if (strncmp(display, "unix:", 5) == 0) |
2662 |
vs->lsock = unix_connect(display+5);
|
2663 |
else
|
2664 |
vs->lsock = inet_connect(display, SOCK_STREAM); |
2665 |
if (-1 == vs->lsock) { |
2666 |
free(vs->display); |
2667 |
vs->display = NULL;
|
2668 |
return -1; |
2669 |
} else {
|
2670 |
int csock = vs->lsock;
|
2671 |
vs->lsock = -1;
|
2672 |
vnc_connect(vs, csock); |
2673 |
} |
2674 |
return 0; |
2675 |
|
2676 |
} else {
|
2677 |
/* listen for connects */
|
2678 |
char *dpy;
|
2679 |
dpy = qemu_malloc(256);
|
2680 |
if (strncmp(display, "unix:", 5) == 0) { |
2681 |
pstrcpy(dpy, 256, "unix:"); |
2682 |
vs->lsock = unix_listen(display+5, dpy+5, 256-5); |
2683 |
} else {
|
2684 |
vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900); |
2685 |
} |
2686 |
if (-1 == vs->lsock) { |
2687 |
free(dpy); |
2688 |
return -1; |
2689 |
} else {
|
2690 |
free(vs->display); |
2691 |
vs->display = dpy; |
2692 |
} |
2693 |
} |
2694 |
return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs); |
2695 |
} |