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