2 * QEMU VNC display driver
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
30 #include "sysemu/sysemu.h"
31 #include "qemu/sockets.h"
32 #include "qemu/timer.h"
34 #include "qapi/qmp/types.h"
35 #include "qmp-commands.h"
36 #include "qemu/osdep.h"
38 #include "qapi-event.h"
40 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
41 #define VNC_REFRESH_INTERVAL_INC 50
42 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
43 static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
44 static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
46 #include "vnc_keysym.h"
49 static VncDisplay *vnc_display; /* needed for info vnc */
51 static int vnc_cursor_define(VncState *vs);
52 static void vnc_release_modifiers(VncState *vs);
54 static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
57 static const char *mn[] = {
59 [VNC_SHARE_MODE_CONNECTING] = "connecting",
60 [VNC_SHARE_MODE_SHARED] = "shared",
61 [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive",
62 [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
64 fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
65 vs->csock, mn[vs->share_mode], mn[mode]);
68 if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
69 vs->vd->num_exclusive--;
71 vs->share_mode = mode;
72 if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
73 vs->vd->num_exclusive++;
77 static char *addr_to_string(const char *format,
78 struct sockaddr_storage *sa,
81 char host[NI_MAXHOST];
82 char serv[NI_MAXSERV];
86 if ((err = getnameinfo((struct sockaddr *)sa, salen,
89 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
90 VNC_DEBUG("Cannot resolve address %d: %s\n",
91 err, gai_strerror(err));
95 /* Enough for the existing format + the 2 vars we're
97 addrlen = strlen(format) + strlen(host) + strlen(serv);
98 addr = g_malloc(addrlen + 1);
99 snprintf(addr, addrlen, format, host, serv);
100 addr[addrlen] = '\0';
106 char *vnc_socket_local_addr(const char *format, int fd) {
107 struct sockaddr_storage sa;
111 if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
114 return addr_to_string(format, &sa, salen);
117 char *vnc_socket_remote_addr(const char *format, int fd) {
118 struct sockaddr_storage sa;
122 if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
125 return addr_to_string(format, &sa, salen);
128 static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
132 char host[NI_MAXHOST];
133 char serv[NI_MAXSERV];
136 if ((err = getnameinfo((struct sockaddr *)sa, salen,
139 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
140 VNC_DEBUG("Cannot resolve address %d: %s\n",
141 err, gai_strerror(err));
145 info = g_malloc0(sizeof(VncBasicInfo));
146 info->host = g_strdup(host);
147 info->service = g_strdup(serv);
148 info->family = inet_netfamily(sa->ss_family);
152 static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd)
154 struct sockaddr_storage sa;
158 if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
162 return vnc_basic_info_get(&sa, salen);
165 static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd)
167 struct sockaddr_storage sa;
171 if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
175 return vnc_basic_info_get(&sa, salen);
178 static const char *vnc_auth_name(VncDisplay *vd) {
180 case VNC_AUTH_INVALID:
196 case VNC_AUTH_VENCRYPT:
197 #ifdef CONFIG_VNC_TLS
198 switch (vd->subauth) {
199 case VNC_AUTH_VENCRYPT_PLAIN:
200 return "vencrypt+plain";
201 case VNC_AUTH_VENCRYPT_TLSNONE:
202 return "vencrypt+tls+none";
203 case VNC_AUTH_VENCRYPT_TLSVNC:
204 return "vencrypt+tls+vnc";
205 case VNC_AUTH_VENCRYPT_TLSPLAIN:
206 return "vencrypt+tls+plain";
207 case VNC_AUTH_VENCRYPT_X509NONE:
208 return "vencrypt+x509+none";
209 case VNC_AUTH_VENCRYPT_X509VNC:
210 return "vencrypt+x509+vnc";
211 case VNC_AUTH_VENCRYPT_X509PLAIN:
212 return "vencrypt+x509+plain";
213 case VNC_AUTH_VENCRYPT_TLSSASL:
214 return "vencrypt+tls+sasl";
215 case VNC_AUTH_VENCRYPT_X509SASL:
216 return "vencrypt+x509+sasl";
229 static VncServerInfo *vnc_server_info_get(void)
232 VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vnc_display->lsock);
237 info = g_malloc(sizeof(*info));
239 info->has_auth = true;
240 info->auth = g_strdup(vnc_auth_name(vnc_display));
244 static void vnc_client_cache_auth(VncState *client)
250 #ifdef CONFIG_VNC_TLS
251 if (client->tls.session &&
253 client->info->has_x509_dname = true;
254 client->info->x509_dname = g_strdup(client->tls.dname);
257 #ifdef CONFIG_VNC_SASL
258 if (client->sasl.conn &&
259 client->sasl.username) {
260 client->info->has_sasl_username = true;
261 client->info->sasl_username = g_strdup(client->sasl.username);
266 static void vnc_client_cache_addr(VncState *client)
268 VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock);
271 client->info = g_malloc0(sizeof(*client->info));
272 client->info->base = bi;
276 static void vnc_qmp_event(VncState *vs, QAPIEvent event)
283 g_assert(vs->info->base);
285 si = vnc_server_info_get();
291 case QAPI_EVENT_VNC_CONNECTED:
292 qapi_event_send_vnc_connected(si, vs->info->base, &error_abort);
294 case QAPI_EVENT_VNC_INITIALIZED:
295 qapi_event_send_vnc_initialized(si, vs->info, &error_abort);
297 case QAPI_EVENT_VNC_DISCONNECTED:
298 qapi_event_send_vnc_disconnected(si, vs->info, &error_abort);
304 qapi_free_VncServerInfo(si);
307 static VncClientInfo *qmp_query_vnc_client(const VncState *client)
309 struct sockaddr_storage sa;
310 socklen_t salen = sizeof(sa);
311 char host[NI_MAXHOST];
312 char serv[NI_MAXSERV];
315 if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
319 if (getnameinfo((struct sockaddr *)&sa, salen,
322 NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
326 info = g_malloc0(sizeof(*info));
327 info->base = g_malloc0(sizeof(*info->base));
328 info->base->host = g_strdup(host);
329 info->base->service = g_strdup(serv);
330 info->base->family = inet_netfamily(sa.ss_family);
332 #ifdef CONFIG_VNC_TLS
333 if (client->tls.session && client->tls.dname) {
334 info->has_x509_dname = true;
335 info->x509_dname = g_strdup(client->tls.dname);
338 #ifdef CONFIG_VNC_SASL
339 if (client->sasl.conn && client->sasl.username) {
340 info->has_sasl_username = true;
341 info->sasl_username = g_strdup(client->sasl.username);
348 VncInfo *qmp_query_vnc(Error **errp)
350 VncInfo *info = g_malloc0(sizeof(*info));
352 if (vnc_display == NULL || vnc_display->display == NULL) {
353 info->enabled = false;
355 VncClientInfoList *cur_item = NULL;
356 struct sockaddr_storage sa;
357 socklen_t salen = sizeof(sa);
358 char host[NI_MAXHOST];
359 char serv[NI_MAXSERV];
362 info->enabled = true;
364 /* for compatibility with the original command */
365 info->has_clients = true;
367 QTAILQ_FOREACH(client, &vnc_display->clients, next) {
368 VncClientInfoList *cinfo = g_malloc0(sizeof(*info));
369 cinfo->value = qmp_query_vnc_client(client);
371 /* XXX: waiting for the qapi to support GSList */
373 info->clients = cur_item = cinfo;
375 cur_item->next = cinfo;
380 if (vnc_display->lsock == -1) {
384 if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa,
386 error_set(errp, QERR_UNDEFINED_ERROR);
390 if (getnameinfo((struct sockaddr *)&sa, salen,
393 NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
394 error_set(errp, QERR_UNDEFINED_ERROR);
398 info->has_host = true;
399 info->host = g_strdup(host);
401 info->has_service = true;
402 info->service = g_strdup(serv);
404 info->has_family = true;
405 info->family = inet_netfamily(sa.ss_family);
407 info->has_auth = true;
408 info->auth = g_strdup(vnc_auth_name(vnc_display));
414 qapi_free_VncInfo(info);
419 1) Get the queue working for IO.
420 2) there is some weirdness when using the -S option (the screen is grey
421 and not totally invalidated
422 3) resolutions > 1024
425 static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
426 static void vnc_disconnect_start(VncState *vs);
428 static void vnc_colordepth(VncState *vs);
429 static void framebuffer_update_request(VncState *vs, int incremental,
430 int x_position, int y_position,
432 static void vnc_refresh(DisplayChangeListener *dcl);
433 static int vnc_refresh_server_surface(VncDisplay *vd);
435 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
436 VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
437 int width, int height,
438 int x, int y, int w, int h) {
439 /* this is needed this to ensure we updated all affected
440 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
441 w += (x % VNC_DIRTY_PIXELS_PER_BIT);
442 x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
446 w = MIN(x + w, width) - x;
447 h = MIN(y + h, height);
450 bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
451 DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
455 static void vnc_dpy_update(DisplayChangeListener *dcl,
456 int x, int y, int w, int h)
458 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
459 struct VncSurface *s = &vd->guest;
460 int width = pixman_image_get_width(vd->server);
461 int height = pixman_image_get_height(vd->server);
463 vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
466 void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
469 vnc_write_u16(vs, x);
470 vnc_write_u16(vs, y);
471 vnc_write_u16(vs, w);
472 vnc_write_u16(vs, h);
474 vnc_write_s32(vs, encoding);
477 void buffer_reserve(Buffer *buffer, size_t len)
479 if ((buffer->capacity - buffer->offset) < len) {
480 buffer->capacity += (len + 1024);
481 buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
482 if (buffer->buffer == NULL) {
483 fprintf(stderr, "vnc: out of memory\n");
489 static int buffer_empty(Buffer *buffer)
491 return buffer->offset == 0;
494 uint8_t *buffer_end(Buffer *buffer)
496 return buffer->buffer + buffer->offset;
499 void buffer_reset(Buffer *buffer)
504 void buffer_free(Buffer *buffer)
506 g_free(buffer->buffer);
508 buffer->capacity = 0;
509 buffer->buffer = NULL;
512 void buffer_append(Buffer *buffer, const void *data, size_t len)
514 memcpy(buffer->buffer + buffer->offset, data, len);
515 buffer->offset += len;
518 void buffer_advance(Buffer *buf, size_t len)
520 memmove(buf->buffer, buf->buffer + len,
521 (buf->offset - len));
525 static void vnc_desktop_resize(VncState *vs)
527 if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
530 if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
531 vs->client_height == pixman_image_get_height(vs->vd->server)) {
534 vs->client_width = pixman_image_get_width(vs->vd->server);
535 vs->client_height = pixman_image_get_height(vs->vd->server);
537 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
539 vnc_write_u16(vs, 1); /* number of rects */
540 vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
541 VNC_ENCODING_DESKTOPRESIZE);
542 vnc_unlock_output(vs);
546 static void vnc_abort_display_jobs(VncDisplay *vd)
550 QTAILQ_FOREACH(vs, &vd->clients, next) {
553 vnc_unlock_output(vs);
555 QTAILQ_FOREACH(vs, &vd->clients, next) {
558 QTAILQ_FOREACH(vs, &vd->clients, next) {
561 vnc_unlock_output(vs);
565 int vnc_server_fb_stride(VncDisplay *vd)
567 return pixman_image_get_stride(vd->server);
570 void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
574 ptr = (uint8_t *)pixman_image_get_data(vd->server);
575 ptr += y * vnc_server_fb_stride(vd);
576 ptr += x * VNC_SERVER_FB_BYTES;
580 static void vnc_dpy_switch(DisplayChangeListener *dcl,
581 DisplaySurface *surface)
583 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
587 vnc_abort_display_jobs(vd);
590 qemu_pixman_image_unref(vd->server);
592 width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
593 VNC_DIRTY_PIXELS_PER_BIT));
594 height = MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
595 vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
596 width, height, NULL, 0);
600 if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
601 console_color_init(ds);
603 qemu_pixman_image_unref(vd->guest.fb);
604 vd->guest.fb = pixman_image_ref(surface->image);
605 vd->guest.format = surface->format;
606 memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
607 vnc_set_area_dirty(vd->guest.dirty, width, height, 0, 0,
610 QTAILQ_FOREACH(vs, &vd->clients, next) {
612 vnc_desktop_resize(vs);
613 if (vs->vd->cursor) {
614 vnc_cursor_define(vs);
616 memset(vs->dirty, 0x00, sizeof(vs->dirty));
617 vnc_set_area_dirty(vs->dirty, width, height, 0, 0,
623 static void vnc_write_pixels_copy(VncState *vs,
624 void *pixels, int size)
626 vnc_write(vs, pixels, size);
629 /* slowest but generic code. */
630 void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
634 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
635 r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
636 g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8;
637 b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8;
639 # error need some bits here if you change VNC_SERVER_FB_FORMAT
641 v = (r << vs->client_pf.rshift) |
642 (g << vs->client_pf.gshift) |
643 (b << vs->client_pf.bshift);
644 switch (vs->client_pf.bytes_per_pixel) {
674 static void vnc_write_pixels_generic(VncState *vs,
675 void *pixels1, int size)
679 if (VNC_SERVER_FB_BYTES == 4) {
680 uint32_t *pixels = pixels1;
683 for (i = 0; i < n; i++) {
684 vnc_convert_pixel(vs, buf, pixels[i]);
685 vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
690 int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
694 VncDisplay *vd = vs->vd;
696 row = vnc_server_fb_ptr(vd, x, y);
697 for (i = 0; i < h; i++) {
698 vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
699 row += vnc_server_fb_stride(vd);
704 int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
708 switch(vs->vnc_encoding) {
709 case VNC_ENCODING_ZLIB:
710 n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
712 case VNC_ENCODING_HEXTILE:
713 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
714 n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
716 case VNC_ENCODING_TIGHT:
717 n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
719 case VNC_ENCODING_TIGHT_PNG:
720 n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
722 case VNC_ENCODING_ZRLE:
723 n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
725 case VNC_ENCODING_ZYWRLE:
726 n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
729 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
730 n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
736 static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
738 /* send bitblit op to the vnc client */
740 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
742 vnc_write_u16(vs, 1); /* number of rects */
743 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
744 vnc_write_u16(vs, src_x);
745 vnc_write_u16(vs, src_y);
746 vnc_unlock_output(vs);
750 static void vnc_dpy_copy(DisplayChangeListener *dcl,
751 int src_x, int src_y,
752 int dst_x, int dst_y, int w, int h)
754 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
758 int i, x, y, pitch, inc, w_lim, s;
761 vnc_refresh_server_surface(vd);
762 QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
763 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
764 vs->force_update = 1;
765 vnc_update_client(vs, 1, true);
766 /* vs might be free()ed here */
770 /* do bitblit op on the local surface too */
771 pitch = vnc_server_fb_stride(vd);
772 src_row = vnc_server_fb_ptr(vd, src_x, src_y);
773 dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
778 src_row += pitch * (h-1);
779 dst_row += pitch * (h-1);
784 w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
788 w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
790 for (i = 0; i < h; i++) {
791 for (x = 0; x <= w_lim;
792 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
794 if ((s = w - w_lim) == 0)
797 s = (VNC_DIRTY_PIXELS_PER_BIT -
798 (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
801 s = VNC_DIRTY_PIXELS_PER_BIT;
803 cmp_bytes = s * VNC_SERVER_FB_BYTES;
804 if (memcmp(src_row, dst_row, cmp_bytes) == 0)
806 memmove(dst_row, src_row, cmp_bytes);
807 QTAILQ_FOREACH(vs, &vd->clients, next) {
808 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
809 set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
814 src_row += pitch - w * VNC_SERVER_FB_BYTES;
815 dst_row += pitch - w * VNC_SERVER_FB_BYTES;
819 QTAILQ_FOREACH(vs, &vd->clients, next) {
820 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
821 vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
826 static void vnc_mouse_set(DisplayChangeListener *dcl,
827 int x, int y, int visible)
829 /* can we ask the client(s) to move the pointer ??? */
832 static int vnc_cursor_define(VncState *vs)
834 QEMUCursor *c = vs->vd->cursor;
837 if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
839 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
840 vnc_write_u8(vs, 0); /* padding */
841 vnc_write_u16(vs, 1); /* # of rects */
842 vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
843 VNC_ENCODING_RICH_CURSOR);
844 isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
845 vnc_write_pixels_generic(vs, c->data, isize);
846 vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
847 vnc_unlock_output(vs);
853 static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
856 VncDisplay *vd = vnc_display;
859 cursor_put(vd->cursor);
860 g_free(vd->cursor_mask);
863 cursor_get(vd->cursor);
864 vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
865 vd->cursor_mask = g_malloc0(vd->cursor_msize);
866 cursor_get_mono_mask(c, 0, vd->cursor_mask);
868 QTAILQ_FOREACH(vs, &vd->clients, next) {
869 vnc_cursor_define(vs);
873 static int find_and_clear_dirty_height(struct VncState *vs,
874 int y, int last_x, int x, int height)
878 for (h = 1; h < (height - y); h++) {
879 if (!test_bit(last_x, vs->dirty[y + h])) {
882 bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
888 static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
890 if (vs->need_update && vs->csock != -1) {
891 VncDisplay *vd = vs->vd;
897 if (vs->output.offset && !vs->audio_cap && !vs->force_update)
898 /* kernel send buffers are full -> drop frames to throttle */
901 if (!has_dirty && !vs->audio_cap && !vs->force_update)
905 * Send screen updates to the vnc client using the server
906 * surface and server dirty map. guest surface updates
907 * happening in parallel don't disturb us, the next pass will
908 * send them to the client.
910 job = vnc_job_new(vs);
912 height = pixman_image_get_height(vd->server);
913 width = pixman_image_get_width(vd->server);
919 unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
920 height * VNC_DIRTY_BPL(vs),
921 y * VNC_DIRTY_BPL(vs));
922 if (offset == height * VNC_DIRTY_BPL(vs)) {
923 /* no more dirty bits */
926 y = offset / VNC_DIRTY_BPL(vs);
927 x = offset % VNC_DIRTY_BPL(vs);
928 x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
929 VNC_DIRTY_BPL(vs), x);
930 bitmap_clear(vs->dirty[y], x, x2 - x);
931 h = find_and_clear_dirty_height(vs, y, x, x2, height);
932 x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
934 n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
935 (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
943 vs->force_update = 0;
947 if (vs->csock == -1) {
948 vnc_disconnect_finish(vs);
957 static void audio_capture_notify(void *opaque, audcnotification_e cmd)
959 VncState *vs = opaque;
962 case AUD_CNOTIFY_DISABLE:
964 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
965 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
966 vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
967 vnc_unlock_output(vs);
971 case AUD_CNOTIFY_ENABLE:
973 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
974 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
975 vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
976 vnc_unlock_output(vs);
982 static void audio_capture_destroy(void *opaque)
986 static void audio_capture(void *opaque, void *buf, int size)
988 VncState *vs = opaque;
991 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
992 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
993 vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
994 vnc_write_u32(vs, size);
995 vnc_write(vs, buf, size);
996 vnc_unlock_output(vs);
1000 static void audio_add(VncState *vs)
1002 struct audio_capture_ops ops;
1004 if (vs->audio_cap) {
1005 error_report("audio already running");
1009 ops.notify = audio_capture_notify;
1010 ops.destroy = audio_capture_destroy;
1011 ops.capture = audio_capture;
1013 vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
1014 if (!vs->audio_cap) {
1015 error_report("Failed to add audio capture");
1019 static void audio_del(VncState *vs)
1021 if (vs->audio_cap) {
1022 AUD_del_capture(vs->audio_cap, vs);
1023 vs->audio_cap = NULL;
1027 static void vnc_disconnect_start(VncState *vs)
1029 if (vs->csock == -1)
1031 vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
1032 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
1033 closesocket(vs->csock);
1037 void vnc_disconnect_finish(VncState *vs)
1041 vnc_jobs_join(vs); /* Wait encoding jobs */
1043 vnc_lock_output(vs);
1044 vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
1046 buffer_free(&vs->input);
1047 buffer_free(&vs->output);
1048 #ifdef CONFIG_VNC_WS
1049 buffer_free(&vs->ws_input);
1050 buffer_free(&vs->ws_output);
1051 #endif /* CONFIG_VNC_WS */
1053 qapi_free_VncClientInfo(vs->info);
1056 vnc_tight_clear(vs);
1059 #ifdef CONFIG_VNC_TLS
1060 vnc_tls_client_cleanup(vs);
1061 #endif /* CONFIG_VNC_TLS */
1062 #ifdef CONFIG_VNC_SASL
1063 vnc_sasl_client_cleanup(vs);
1064 #endif /* CONFIG_VNC_SASL */
1066 vnc_release_modifiers(vs);
1068 if (vs->initialized) {
1069 QTAILQ_REMOVE(&vs->vd->clients, vs, next);
1070 qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
1073 if (vs->vd->lock_key_sync)
1074 qemu_remove_led_event_handler(vs->led);
1075 vnc_unlock_output(vs);
1077 qemu_mutex_destroy(&vs->output_mutex);
1078 if (vs->bh != NULL) {
1079 qemu_bh_delete(vs->bh);
1081 buffer_free(&vs->jobs_buffer);
1083 for (i = 0; i < VNC_STAT_ROWS; ++i) {
1084 g_free(vs->lossy_rect[i]);
1086 g_free(vs->lossy_rect);
1090 int vnc_client_io_error(VncState *vs, int ret, int last_errno)
1092 if (ret == 0 || ret == -1) {
1094 switch (last_errno) {
1098 case WSAEWOULDBLOCK:
1106 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1107 ret, ret < 0 ? last_errno : 0);
1108 vnc_disconnect_start(vs);
1116 void vnc_client_error(VncState *vs)
1118 VNC_DEBUG("Closing down client sock: protocol error\n");
1119 vnc_disconnect_start(vs);
1122 #ifdef CONFIG_VNC_TLS
1123 static long vnc_client_write_tls(gnutls_session_t *session,
1124 const uint8_t *data,
1127 long ret = gnutls_write(*session, data, datalen);
1129 if (ret == GNUTLS_E_AGAIN) {
1138 #endif /* CONFIG_VNC_TLS */
1141 * Called to write a chunk of data to the client socket. The data may
1142 * be the raw data, or may have already been encoded by SASL.
1143 * The data will be written either straight onto the socket, or
1144 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1146 * NB, it is theoretically possible to have 2 layers of encryption,
1147 * both SASL, and this TLS layer. It is highly unlikely in practice
1148 * though, since SASL encryption will typically be a no-op if TLS
1151 * Returns the number of bytes written, which may be less than
1152 * the requested 'datalen' if the socket would block. Returns
1153 * -1 on error, and disconnects the client socket.
1155 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
1158 #ifdef CONFIG_VNC_TLS
1159 if (vs->tls.session) {
1160 ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
1162 #ifdef CONFIG_VNC_WS
1163 if (vs->ws_tls.session) {
1164 ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen);
1166 #endif /* CONFIG_VNC_WS */
1167 #endif /* CONFIG_VNC_TLS */
1169 ret = send(vs->csock, (const void *)data, datalen, 0);
1171 #ifdef CONFIG_VNC_TLS
1173 #endif /* CONFIG_VNC_TLS */
1174 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
1175 return vnc_client_io_error(vs, ret, socket_error());
1180 * Called to write buffered data to the client socket, when not
1181 * using any SASL SSF encryption layers. Will write as much data
1182 * as possible without blocking. If all buffered data is written,
1183 * will switch the FD poll() handler back to read monitoring.
1185 * Returns the number of bytes written, which may be less than
1186 * the buffered output data if the socket would block. Returns
1187 * -1 on error, and disconnects the client socket.
1189 static long vnc_client_write_plain(VncState *vs)
1193 #ifdef CONFIG_VNC_SASL
1194 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1195 vs->output.buffer, vs->output.capacity, vs->output.offset,
1196 vs->sasl.waitWriteSSF);
1198 if (vs->sasl.conn &&
1200 vs->sasl.waitWriteSSF) {
1201 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
1203 vs->sasl.waitWriteSSF -= ret;
1205 #endif /* CONFIG_VNC_SASL */
1206 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
1210 buffer_advance(&vs->output, ret);
1212 if (vs->output.offset == 0) {
1213 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1221 * First function called whenever there is data to be written to
1222 * the client socket. Will delegate actual work according to whether
1223 * SASL SSF layers are enabled (thus requiring encryption calls)
1225 static void vnc_client_write_locked(void *opaque)
1227 VncState *vs = opaque;
1229 #ifdef CONFIG_VNC_SASL
1230 if (vs->sasl.conn &&
1232 !vs->sasl.waitWriteSSF) {
1233 vnc_client_write_sasl(vs);
1235 #endif /* CONFIG_VNC_SASL */
1237 #ifdef CONFIG_VNC_WS
1238 if (vs->encode_ws) {
1239 vnc_client_write_ws(vs);
1241 #endif /* CONFIG_VNC_WS */
1243 vnc_client_write_plain(vs);
1248 void vnc_client_write(void *opaque)
1250 VncState *vs = opaque;
1252 vnc_lock_output(vs);
1253 if (vs->output.offset
1254 #ifdef CONFIG_VNC_WS
1255 || vs->ws_output.offset
1258 vnc_client_write_locked(opaque);
1259 } else if (vs->csock != -1) {
1260 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1262 vnc_unlock_output(vs);
1265 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
1267 vs->read_handler = func;
1268 vs->read_handler_expect = expecting;
1271 #ifdef CONFIG_VNC_TLS
1272 static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
1275 long ret = gnutls_read(*session, data, datalen);
1277 if (ret == GNUTLS_E_AGAIN) {
1286 #endif /* CONFIG_VNC_TLS */
1289 * Called to read a chunk of data from the client socket. The data may
1290 * be the raw data, or may need to be further decoded by SASL.
1291 * The data will be read either straight from to the socket, or
1292 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1294 * NB, it is theoretically possible to have 2 layers of encryption,
1295 * both SASL, and this TLS layer. It is highly unlikely in practice
1296 * though, since SASL encryption will typically be a no-op if TLS
1299 * Returns the number of bytes read, which may be less than
1300 * the requested 'datalen' if the socket would block. Returns
1301 * -1 on error, and disconnects the client socket.
1303 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
1306 #ifdef CONFIG_VNC_TLS
1307 if (vs->tls.session) {
1308 ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
1310 #ifdef CONFIG_VNC_WS
1311 if (vs->ws_tls.session) {
1312 ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen);
1314 #endif /* CONFIG_VNC_WS */
1315 #endif /* CONFIG_VNC_TLS */
1317 ret = qemu_recv(vs->csock, data, datalen, 0);
1319 #ifdef CONFIG_VNC_TLS
1321 #endif /* CONFIG_VNC_TLS */
1322 VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
1323 return vnc_client_io_error(vs, ret, socket_error());
1328 * Called to read data from the client socket to the input buffer,
1329 * when not using any SASL SSF encryption layers. Will read as much
1330 * data as possible without blocking.
1332 * Returns the number of bytes read. Returns -1 on error, and
1333 * disconnects the client socket.
1335 static long vnc_client_read_plain(VncState *vs)
1338 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1339 vs->input.buffer, vs->input.capacity, vs->input.offset);
1340 buffer_reserve(&vs->input, 4096);
1341 ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
1344 vs->input.offset += ret;
1348 static void vnc_jobs_bh(void *opaque)
1350 VncState *vs = opaque;
1352 vnc_jobs_consume_buffer(vs);
1356 * First function called whenever there is more data to be read from
1357 * the client socket. Will delegate actual work according to whether
1358 * SASL SSF layers are enabled (thus requiring decryption calls)
1360 void vnc_client_read(void *opaque)
1362 VncState *vs = opaque;
1365 #ifdef CONFIG_VNC_SASL
1366 if (vs->sasl.conn && vs->sasl.runSSF)
1367 ret = vnc_client_read_sasl(vs);
1369 #endif /* CONFIG_VNC_SASL */
1370 #ifdef CONFIG_VNC_WS
1371 if (vs->encode_ws) {
1372 ret = vnc_client_read_ws(vs);
1374 vnc_disconnect_start(vs);
1376 } else if (ret == -2) {
1377 vnc_client_error(vs);
1381 #endif /* CONFIG_VNC_WS */
1383 ret = vnc_client_read_plain(vs);
1386 if (vs->csock == -1)
1387 vnc_disconnect_finish(vs);
1391 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
1392 size_t len = vs->read_handler_expect;
1395 ret = vs->read_handler(vs, vs->input.buffer, len);
1396 if (vs->csock == -1) {
1397 vnc_disconnect_finish(vs);
1402 buffer_advance(&vs->input, len);
1404 vs->read_handler_expect = ret;
1409 void vnc_write(VncState *vs, const void *data, size_t len)
1411 buffer_reserve(&vs->output, len);
1413 if (vs->csock != -1 && buffer_empty(&vs->output)) {
1414 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
1417 buffer_append(&vs->output, data, len);
1420 void vnc_write_s32(VncState *vs, int32_t value)
1422 vnc_write_u32(vs, *(uint32_t *)&value);
1425 void vnc_write_u32(VncState *vs, uint32_t value)
1429 buf[0] = (value >> 24) & 0xFF;
1430 buf[1] = (value >> 16) & 0xFF;
1431 buf[2] = (value >> 8) & 0xFF;
1432 buf[3] = value & 0xFF;
1434 vnc_write(vs, buf, 4);
1437 void vnc_write_u16(VncState *vs, uint16_t value)
1441 buf[0] = (value >> 8) & 0xFF;
1442 buf[1] = value & 0xFF;
1444 vnc_write(vs, buf, 2);
1447 void vnc_write_u8(VncState *vs, uint8_t value)
1449 vnc_write(vs, (char *)&value, 1);
1452 void vnc_flush(VncState *vs)
1454 vnc_lock_output(vs);
1455 if (vs->csock != -1 && (vs->output.offset
1456 #ifdef CONFIG_VNC_WS
1457 || vs->ws_output.offset
1460 vnc_client_write_locked(vs);
1462 vnc_unlock_output(vs);
1465 static uint8_t read_u8(uint8_t *data, size_t offset)
1467 return data[offset];
1470 static uint16_t read_u16(uint8_t *data, size_t offset)
1472 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
1475 static int32_t read_s32(uint8_t *data, size_t offset)
1477 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
1478 (data[offset + 2] << 8) | data[offset + 3]);
1481 uint32_t read_u32(uint8_t *data, size_t offset)
1483 return ((data[offset] << 24) | (data[offset + 1] << 16) |
1484 (data[offset + 2] << 8) | data[offset + 3]);
1487 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
1491 static void check_pointer_type_change(Notifier *notifier, void *data)
1493 VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
1494 int absolute = qemu_input_is_absolute();
1496 if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1497 vnc_lock_output(vs);
1498 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1499 vnc_write_u8(vs, 0);
1500 vnc_write_u16(vs, 1);
1501 vnc_framebuffer_update(vs, absolute, 0,
1502 pixman_image_get_width(vs->vd->server),
1503 pixman_image_get_height(vs->vd->server),
1504 VNC_ENCODING_POINTER_TYPE_CHANGE);
1505 vnc_unlock_output(vs);
1508 vs->absolute = absolute;
1511 static void pointer_event(VncState *vs, int button_mask, int x, int y)
1513 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1514 [INPUT_BUTTON_LEFT] = 0x01,
1515 [INPUT_BUTTON_MIDDLE] = 0x02,
1516 [INPUT_BUTTON_RIGHT] = 0x04,
1517 [INPUT_BUTTON_WHEEL_UP] = 0x08,
1518 [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
1520 QemuConsole *con = vs->vd->dcl.con;
1521 int width = pixman_image_get_width(vs->vd->server);
1522 int height = pixman_image_get_height(vs->vd->server);
1524 if (vs->last_bmask != button_mask) {
1525 qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
1526 vs->last_bmask = button_mask;
1530 qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
1531 qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
1532 } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
1533 qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
1534 qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
1536 if (vs->last_x != -1) {
1537 qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
1538 qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
1543 qemu_input_event_sync();
1546 static void reset_keys(VncState *vs)
1549 for(i = 0; i < 256; i++) {
1550 if (vs->modifiers_state[i]) {
1551 qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
1552 vs->modifiers_state[i] = 0;
1557 static void press_key(VncState *vs, int keysym)
1559 int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
1560 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
1561 qemu_input_event_send_key_delay(0);
1562 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
1563 qemu_input_event_send_key_delay(0);
1566 static int current_led_state(VncState *vs)
1570 if (vs->modifiers_state[0x46]) {
1571 ledstate |= QEMU_SCROLL_LOCK_LED;
1573 if (vs->modifiers_state[0x45]) {
1574 ledstate |= QEMU_NUM_LOCK_LED;
1576 if (vs->modifiers_state[0x3a]) {
1577 ledstate |= QEMU_CAPS_LOCK_LED;
1583 static void vnc_led_state_change(VncState *vs)
1587 if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1591 ledstate = current_led_state(vs);
1592 vnc_lock_output(vs);
1593 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1594 vnc_write_u8(vs, 0);
1595 vnc_write_u16(vs, 1);
1596 vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1597 vnc_write_u8(vs, ledstate);
1598 vnc_unlock_output(vs);
1602 static void kbd_leds(void *opaque, int ledstate)
1604 VncState *vs = opaque;
1606 bool has_changed = (ledstate != current_led_state(vs));
1608 trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
1609 (ledstate & QEMU_NUM_LOCK_LED),
1610 (ledstate & QEMU_SCROLL_LOCK_LED));
1612 caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
1613 num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0;
1614 scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
1616 if (vs->modifiers_state[0x3a] != caps) {
1617 vs->modifiers_state[0x3a] = caps;
1619 if (vs->modifiers_state[0x45] != num) {
1620 vs->modifiers_state[0x45] = num;
1622 if (vs->modifiers_state[0x46] != scr) {
1623 vs->modifiers_state[0x46] = scr;
1626 /* Sending the current led state message to the client */
1628 vnc_led_state_change(vs);
1632 static void do_key_event(VncState *vs, int down, int keycode, int sym)
1634 /* QEMU console switch */
1636 case 0x2a: /* Left Shift */
1637 case 0x36: /* Right Shift */
1638 case 0x1d: /* Left CTRL */
1639 case 0x9d: /* Right CTRL */
1640 case 0x38: /* Left ALT */
1641 case 0xb8: /* Right ALT */
1643 vs->modifiers_state[keycode] = 1;
1645 vs->modifiers_state[keycode] = 0;
1647 case 0x02 ... 0x0a: /* '1' to '9' keys */
1648 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
1649 /* Reset the modifiers sent to the current console */
1651 console_select(keycode - 0x02);
1655 case 0x3a: /* CapsLock */
1656 case 0x45: /* NumLock */
1658 vs->modifiers_state[keycode] ^= 1;
1662 /* Turn off the lock state sync logic if the client support the led
1665 if (down && vs->vd->lock_key_sync &&
1666 !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
1667 keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
1668 /* If the numlock state needs to change then simulate an additional
1669 keypress before sending this one. This will happen if the user
1670 toggles numlock away from the VNC window.
1672 if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
1673 if (!vs->modifiers_state[0x45]) {
1674 trace_vnc_key_sync_numlock(true);
1675 vs->modifiers_state[0x45] = 1;
1676 press_key(vs, 0xff7f);
1679 if (vs->modifiers_state[0x45]) {
1680 trace_vnc_key_sync_numlock(false);
1681 vs->modifiers_state[0x45] = 0;
1682 press_key(vs, 0xff7f);
1687 if (down && vs->vd->lock_key_sync &&
1688 !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
1689 ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
1690 /* If the capslock state needs to change then simulate an additional
1691 keypress before sending this one. This will happen if the user
1692 toggles capslock away from the VNC window.
1694 int uppercase = !!(sym >= 'A' && sym <= 'Z');
1695 int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
1696 int capslock = !!(vs->modifiers_state[0x3a]);
1698 if (uppercase == shift) {
1699 trace_vnc_key_sync_capslock(false);
1700 vs->modifiers_state[0x3a] = 0;
1701 press_key(vs, 0xffe5);
1704 if (uppercase != shift) {
1705 trace_vnc_key_sync_capslock(true);
1706 vs->modifiers_state[0x3a] = 1;
1707 press_key(vs, 0xffe5);
1712 if (qemu_console_is_graphic(NULL)) {
1713 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
1715 bool numlock = vs->modifiers_state[0x45];
1716 bool control = (vs->modifiers_state[0x1d] ||
1717 vs->modifiers_state[0x9d]);
1718 /* QEMU console emulation */
1721 case 0x2a: /* Left Shift */
1722 case 0x36: /* Right Shift */
1723 case 0x1d: /* Left CTRL */
1724 case 0x9d: /* Right CTRL */
1725 case 0x38: /* Left ALT */
1726 case 0xb8: /* Right ALT */
1729 kbd_put_keysym(QEMU_KEY_UP);
1732 kbd_put_keysym(QEMU_KEY_DOWN);
1735 kbd_put_keysym(QEMU_KEY_LEFT);
1738 kbd_put_keysym(QEMU_KEY_RIGHT);
1741 kbd_put_keysym(QEMU_KEY_DELETE);
1744 kbd_put_keysym(QEMU_KEY_HOME);
1747 kbd_put_keysym(QEMU_KEY_END);
1750 kbd_put_keysym(QEMU_KEY_PAGEUP);
1753 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
1757 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
1760 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
1763 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
1766 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
1769 kbd_put_keysym('5');
1772 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
1775 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
1778 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
1781 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
1784 kbd_put_keysym('0');
1787 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
1791 kbd_put_keysym('/');
1794 kbd_put_keysym('*');
1797 kbd_put_keysym('-');
1800 kbd_put_keysym('+');
1803 kbd_put_keysym('\n');
1808 kbd_put_keysym(sym & 0x1f);
1810 kbd_put_keysym(sym);
1818 static void vnc_release_modifiers(VncState *vs)
1820 static const int keycodes[] = {
1821 /* shift, control, alt keys, both left & right */
1822 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1826 if (!qemu_console_is_graphic(NULL)) {
1829 for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
1830 keycode = keycodes[i];
1831 if (!vs->modifiers_state[keycode]) {
1834 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
1838 static const char *code2name(int keycode)
1840 return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)];
1843 static void key_event(VncState *vs, int down, uint32_t sym)
1848 if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
1849 lsym = lsym - 'A' + 'a';
1852 keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
1853 trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
1854 do_key_event(vs, down, keycode, sym);
1857 static void ext_key_event(VncState *vs, int down,
1858 uint32_t sym, uint16_t keycode)
1860 /* if the user specifies a keyboard layout, always use it */
1861 if (keyboard_layout) {
1862 key_event(vs, down, sym);
1864 trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
1865 do_key_event(vs, down, keycode, sym);
1869 static void framebuffer_update_request(VncState *vs, int incremental,
1870 int x, int y, int w, int h)
1872 int width = pixman_image_get_width(vs->vd->server);
1873 int height = pixman_image_get_height(vs->vd->server);
1875 vs->need_update = 1;
1881 vnc_set_area_dirty(vs->dirty, width, height, x, y, w, h);
1884 static void send_ext_key_event_ack(VncState *vs)
1886 vnc_lock_output(vs);
1887 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1888 vnc_write_u8(vs, 0);
1889 vnc_write_u16(vs, 1);
1890 vnc_framebuffer_update(vs, 0, 0,
1891 pixman_image_get_width(vs->vd->server),
1892 pixman_image_get_height(vs->vd->server),
1893 VNC_ENCODING_EXT_KEY_EVENT);
1894 vnc_unlock_output(vs);
1898 static void send_ext_audio_ack(VncState *vs)
1900 vnc_lock_output(vs);
1901 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1902 vnc_write_u8(vs, 0);
1903 vnc_write_u16(vs, 1);
1904 vnc_framebuffer_update(vs, 0, 0,
1905 pixman_image_get_width(vs->vd->server),
1906 pixman_image_get_height(vs->vd->server),
1907 VNC_ENCODING_AUDIO);
1908 vnc_unlock_output(vs);
1912 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1915 unsigned int enc = 0;
1918 vs->vnc_encoding = 0;
1919 vs->tight.compression = 9;
1920 vs->tight.quality = -1; /* Lossless by default */
1924 * Start from the end because the encodings are sent in order of preference.
1925 * This way the preferred encoding (first encoding defined in the array)
1926 * will be set at the end of the loop.
1928 for (i = n_encodings - 1; i >= 0; i--) {
1931 case VNC_ENCODING_RAW:
1932 vs->vnc_encoding = enc;
1934 case VNC_ENCODING_COPYRECT:
1935 vs->features |= VNC_FEATURE_COPYRECT_MASK;
1937 case VNC_ENCODING_HEXTILE:
1938 vs->features |= VNC_FEATURE_HEXTILE_MASK;
1939 vs->vnc_encoding = enc;
1941 case VNC_ENCODING_TIGHT:
1942 vs->features |= VNC_FEATURE_TIGHT_MASK;
1943 vs->vnc_encoding = enc;
1945 #ifdef CONFIG_VNC_PNG
1946 case VNC_ENCODING_TIGHT_PNG:
1947 vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1948 vs->vnc_encoding = enc;
1951 case VNC_ENCODING_ZLIB:
1952 vs->features |= VNC_FEATURE_ZLIB_MASK;
1953 vs->vnc_encoding = enc;
1955 case VNC_ENCODING_ZRLE:
1956 vs->features |= VNC_FEATURE_ZRLE_MASK;
1957 vs->vnc_encoding = enc;
1959 case VNC_ENCODING_ZYWRLE:
1960 vs->features |= VNC_FEATURE_ZYWRLE_MASK;
1961 vs->vnc_encoding = enc;
1963 case VNC_ENCODING_DESKTOPRESIZE:
1964 vs->features |= VNC_FEATURE_RESIZE_MASK;
1966 case VNC_ENCODING_POINTER_TYPE_CHANGE:
1967 vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
1969 case VNC_ENCODING_RICH_CURSOR:
1970 vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
1972 case VNC_ENCODING_EXT_KEY_EVENT:
1973 send_ext_key_event_ack(vs);
1975 case VNC_ENCODING_AUDIO:
1976 send_ext_audio_ack(vs);
1978 case VNC_ENCODING_WMVi:
1979 vs->features |= VNC_FEATURE_WMVI_MASK;
1981 case VNC_ENCODING_LED_STATE:
1982 vs->features |= VNC_FEATURE_LED_STATE_MASK;
1984 case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1985 vs->tight.compression = (enc & 0x0F);
1987 case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
1988 if (vs->vd->lossy) {
1989 vs->tight.quality = (enc & 0x0F);
1993 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
1997 vnc_desktop_resize(vs);
1998 check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
1999 vnc_led_state_change(vs);
2002 static void set_pixel_conversion(VncState *vs)
2004 pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
2006 if (fmt == VNC_SERVER_FB_FORMAT) {
2007 vs->write_pixels = vnc_write_pixels_copy;
2008 vnc_hextile_set_pixel_conversion(vs, 0);
2010 vs->write_pixels = vnc_write_pixels_generic;
2011 vnc_hextile_set_pixel_conversion(vs, 1);
2015 static void set_pixel_format(VncState *vs,
2016 int bits_per_pixel, int depth,
2017 int big_endian_flag, int true_color_flag,
2018 int red_max, int green_max, int blue_max,
2019 int red_shift, int green_shift, int blue_shift)
2021 if (!true_color_flag) {
2022 vnc_client_error(vs);
2026 vs->client_pf.rmax = red_max;
2027 vs->client_pf.rbits = hweight_long(red_max);
2028 vs->client_pf.rshift = red_shift;
2029 vs->client_pf.rmask = red_max << red_shift;
2030 vs->client_pf.gmax = green_max;
2031 vs->client_pf.gbits = hweight_long(green_max);
2032 vs->client_pf.gshift = green_shift;
2033 vs->client_pf.gmask = green_max << green_shift;
2034 vs->client_pf.bmax = blue_max;
2035 vs->client_pf.bbits = hweight_long(blue_max);
2036 vs->client_pf.bshift = blue_shift;
2037 vs->client_pf.bmask = blue_max << blue_shift;
2038 vs->client_pf.bits_per_pixel = bits_per_pixel;
2039 vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
2040 vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
2041 vs->client_be = big_endian_flag;
2043 set_pixel_conversion(vs);
2045 graphic_hw_invalidate(NULL);
2046 graphic_hw_update(NULL);
2049 static void pixel_format_message (VncState *vs) {
2050 char pad[3] = { 0, 0, 0 };
2052 vs->client_pf = qemu_default_pixelformat(32);
2054 vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
2055 vnc_write_u8(vs, vs->client_pf.depth); /* depth */
2057 #ifdef HOST_WORDS_BIGENDIAN
2058 vnc_write_u8(vs, 1); /* big-endian-flag */
2060 vnc_write_u8(vs, 0); /* big-endian-flag */
2062 vnc_write_u8(vs, 1); /* true-color-flag */
2063 vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */
2064 vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */
2065 vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */
2066 vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */
2067 vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */
2068 vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */
2069 vnc_write(vs, pad, 3); /* padding */
2071 vnc_hextile_set_pixel_conversion(vs, 0);
2072 vs->write_pixels = vnc_write_pixels_copy;
2075 static void vnc_colordepth(VncState *vs)
2077 if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
2078 /* Sending a WMVi message to notify the client*/
2079 vnc_lock_output(vs);
2080 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
2081 vnc_write_u8(vs, 0);
2082 vnc_write_u16(vs, 1); /* number of rects */
2083 vnc_framebuffer_update(vs, 0, 0,
2084 pixman_image_get_width(vs->vd->server),
2085 pixman_image_get_height(vs->vd->server),
2087 pixel_format_message(vs);
2088 vnc_unlock_output(vs);
2091 set_pixel_conversion(vs);
2095 static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
2099 VncDisplay *vd = vs->vd;
2102 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2106 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
2110 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
2111 read_u8(data, 6), read_u8(data, 7),
2112 read_u16(data, 8), read_u16(data, 10),
2113 read_u16(data, 12), read_u8(data, 14),
2114 read_u8(data, 15), read_u8(data, 16));
2116 case VNC_MSG_CLIENT_SET_ENCODINGS:
2121 limit = read_u16(data, 2);
2123 return 4 + (limit * 4);
2125 limit = read_u16(data, 2);
2127 for (i = 0; i < limit; i++) {
2128 int32_t val = read_s32(data, 4 + (i * 4));
2129 memcpy(data + 4 + (i * 4), &val, sizeof(val));
2132 set_encodings(vs, (int32_t *)(data + 4), limit);
2134 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
2138 framebuffer_update_request(vs,
2139 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
2140 read_u16(data, 6), read_u16(data, 8));
2142 case VNC_MSG_CLIENT_KEY_EVENT:
2146 key_event(vs, read_u8(data, 1), read_u32(data, 4));
2148 case VNC_MSG_CLIENT_POINTER_EVENT:
2152 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
2154 case VNC_MSG_CLIENT_CUT_TEXT:
2159 uint32_t dlen = read_u32(data, 4);
2160 if (dlen > (1 << 20)) {
2161 error_report("vnc: client_cut_text msg payload has %u bytes"
2162 " which exceeds our limit of 1MB.", dlen);
2163 vnc_client_error(vs);
2171 client_cut_text(vs, read_u32(data, 4), data + 8);
2173 case VNC_MSG_CLIENT_QEMU:
2177 switch (read_u8(data, 1)) {
2178 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
2182 ext_key_event(vs, read_u16(data, 2),
2183 read_u32(data, 4), read_u32(data, 8));
2185 case VNC_MSG_CLIENT_QEMU_AUDIO:
2189 switch (read_u16 (data, 2)) {
2190 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
2193 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
2196 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
2199 switch (read_u8(data, 4)) {
2200 case 0: vs->as.fmt = AUD_FMT_U8; break;
2201 case 1: vs->as.fmt = AUD_FMT_S8; break;
2202 case 2: vs->as.fmt = AUD_FMT_U16; break;
2203 case 3: vs->as.fmt = AUD_FMT_S16; break;
2204 case 4: vs->as.fmt = AUD_FMT_U32; break;
2205 case 5: vs->as.fmt = AUD_FMT_S32; break;
2207 printf("Invalid audio format %d\n", read_u8(data, 4));
2208 vnc_client_error(vs);
2211 vs->as.nchannels = read_u8(data, 5);
2212 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
2213 printf("Invalid audio channel coount %d\n",
2215 vnc_client_error(vs);
2218 vs->as.freq = read_u32(data, 6);
2221 printf ("Invalid audio message %d\n", read_u8(data, 4));
2222 vnc_client_error(vs);
2228 printf("Msg: %d\n", read_u16(data, 0));
2229 vnc_client_error(vs);
2234 printf("Msg: %d\n", data[0]);
2235 vnc_client_error(vs);
2239 vnc_read_when(vs, protocol_client_msg, 1);
2243 static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
2249 mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
2250 switch (vs->vd->share_policy) {
2251 case VNC_SHARE_POLICY_IGNORE:
2253 * Ignore the shared flag. Nothing to do here.
2255 * Doesn't conform to the rfb spec but is traditional qemu
2256 * behavior, thus left here as option for compatibility
2260 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
2262 * Policy: Allow clients ask for exclusive access.
2264 * Implementation: When a client asks for exclusive access,
2265 * disconnect all others. Shared connects are allowed as long
2266 * as no exclusive connection exists.
2268 * This is how the rfb spec suggests to handle the shared flag.
2270 if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
2272 QTAILQ_FOREACH(client, &vs->vd->clients, next) {
2276 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
2277 client->share_mode != VNC_SHARE_MODE_SHARED) {
2280 vnc_disconnect_start(client);
2283 if (mode == VNC_SHARE_MODE_SHARED) {
2284 if (vs->vd->num_exclusive > 0) {
2285 vnc_disconnect_start(vs);
2290 case VNC_SHARE_POLICY_FORCE_SHARED:
2292 * Policy: Shared connects only.
2293 * Implementation: Disallow clients asking for exclusive access.
2295 * Useful for shared desktop sessions where you don't want
2296 * someone forgetting to say -shared when running the vnc
2297 * client disconnect everybody else.
2299 if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
2300 vnc_disconnect_start(vs);
2305 vnc_set_share_mode(vs, mode);
2307 vs->client_width = pixman_image_get_width(vs->vd->server);
2308 vs->client_height = pixman_image_get_height(vs->vd->server);
2309 vnc_write_u16(vs, vs->client_width);
2310 vnc_write_u16(vs, vs->client_height);
2312 pixel_format_message(vs);
2315 size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
2317 size = snprintf(buf, sizeof(buf), "QEMU");
2319 vnc_write_u32(vs, size);
2320 vnc_write(vs, buf, size);
2323 vnc_client_cache_auth(vs);
2324 vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
2326 vnc_read_when(vs, protocol_client_msg, 1);
2331 void start_client_init(VncState *vs)
2333 vnc_read_when(vs, protocol_client_init, 1);
2336 static void make_challenge(VncState *vs)
2340 srand(time(NULL)+getpid()+getpid()*987654+rand());
2342 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
2343 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
2346 static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
2348 unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
2350 unsigned char key[8];
2351 time_t now = time(NULL);
2353 if (!vs->vd->password) {
2354 VNC_DEBUG("No password configured on server");
2357 if (vs->vd->expires < now) {
2358 VNC_DEBUG("Password is expired");
2362 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
2364 /* Calculate the expected challenge response */
2365 pwlen = strlen(vs->vd->password);
2366 for (i=0; i<sizeof(key); i++)
2367 key[i] = i<pwlen ? vs->vd->password[i] : 0;
2369 for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
2370 des(response+j, response+j);
2372 /* Compare expected vs actual challenge response */
2373 if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
2374 VNC_DEBUG("Client challenge response did not match\n");
2377 VNC_DEBUG("Accepting VNC challenge response\n");
2378 vnc_write_u32(vs, 0); /* Accept auth */
2381 start_client_init(vs);
2386 vnc_write_u32(vs, 1); /* Reject auth */
2387 if (vs->minor >= 8) {
2388 static const char err[] = "Authentication failed";
2389 vnc_write_u32(vs, sizeof(err));
2390 vnc_write(vs, err, sizeof(err));
2393 vnc_client_error(vs);
2397 void start_auth_vnc(VncState *vs)
2400 /* Send client a 'random' challenge */
2401 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
2404 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
2408 static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
2410 /* We only advertise 1 auth scheme at a time, so client
2411 * must pick the one we sent. Verify this */
2412 if (data[0] != vs->auth) { /* Reject auth */
2413 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
2414 vnc_write_u32(vs, 1);
2415 if (vs->minor >= 8) {
2416 static const char err[] = "Authentication failed";
2417 vnc_write_u32(vs, sizeof(err));
2418 vnc_write(vs, err, sizeof(err));
2420 vnc_client_error(vs);
2421 } else { /* Accept requested auth */
2422 VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
2425 VNC_DEBUG("Accept auth none\n");
2426 if (vs->minor >= 8) {
2427 vnc_write_u32(vs, 0); /* Accept auth completion */
2430 start_client_init(vs);
2434 VNC_DEBUG("Start VNC auth\n");
2438 #ifdef CONFIG_VNC_TLS
2439 case VNC_AUTH_VENCRYPT:
2440 VNC_DEBUG("Accept VeNCrypt auth\n");
2441 start_auth_vencrypt(vs);
2443 #endif /* CONFIG_VNC_TLS */
2445 #ifdef CONFIG_VNC_SASL
2447 VNC_DEBUG("Accept SASL auth\n");
2448 start_auth_sasl(vs);
2450 #endif /* CONFIG_VNC_SASL */
2452 default: /* Should not be possible, but just in case */
2453 VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
2454 vnc_write_u8(vs, 1);
2455 if (vs->minor >= 8) {
2456 static const char err[] = "Authentication failed";
2457 vnc_write_u32(vs, sizeof(err));
2458 vnc_write(vs, err, sizeof(err));
2460 vnc_client_error(vs);
2466 static int protocol_version(VncState *vs, uint8_t *version, size_t len)
2470 memcpy(local, version, 12);
2473 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
2474 VNC_DEBUG("Malformed protocol version %s\n", local);
2475 vnc_client_error(vs);
2478 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
2479 if (vs->major != 3 ||
2485 VNC_DEBUG("Unsupported client version\n");
2486 vnc_write_u32(vs, VNC_AUTH_INVALID);
2488 vnc_client_error(vs);
2491 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2492 * as equivalent to v3.3 by servers
2494 if (vs->minor == 4 || vs->minor == 5)
2497 if (vs->minor == 3) {
2498 if (vs->auth == VNC_AUTH_NONE) {
2499 VNC_DEBUG("Tell client auth none\n");
2500 vnc_write_u32(vs, vs->auth);
2502 start_client_init(vs);
2503 } else if (vs->auth == VNC_AUTH_VNC) {
2504 VNC_DEBUG("Tell client VNC auth\n");
2505 vnc_write_u32(vs, vs->auth);
2509 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
2510 vnc_write_u32(vs, VNC_AUTH_INVALID);
2512 vnc_client_error(vs);
2515 VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
2516 vnc_write_u8(vs, 1); /* num auth */
2517 vnc_write_u8(vs, vs->auth);
2518 vnc_read_when(vs, protocol_client_auth, 1);
2525 static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2527 struct VncSurface *vs = &vd->guest;
2529 return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2532 void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
2536 w = (x + w) / VNC_STAT_RECT;
2537 h = (y + h) / VNC_STAT_RECT;
2541 for (j = y; j <= h; j++) {
2542 for (i = x; i <= w; i++) {
2543 vs->lossy_rect[j][i] = 1;
2548 static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
2551 int sty = y / VNC_STAT_RECT;
2552 int stx = x / VNC_STAT_RECT;
2555 y = y / VNC_STAT_RECT * VNC_STAT_RECT;
2556 x = x / VNC_STAT_RECT * VNC_STAT_RECT;
2558 QTAILQ_FOREACH(vs, &vd->clients, next) {
2561 /* kernel send buffers are full -> refresh later */
2562 if (vs->output.offset) {
2566 if (!vs->lossy_rect[sty][stx]) {
2570 vs->lossy_rect[sty][stx] = 0;
2571 for (j = 0; j < VNC_STAT_RECT; ++j) {
2572 bitmap_set(vs->dirty[y + j],
2573 x / VNC_DIRTY_PIXELS_PER_BIT,
2574 VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
2582 static int vnc_update_stats(VncDisplay *vd, struct timeval * tv)
2584 int width = pixman_image_get_width(vd->guest.fb);
2585 int height = pixman_image_get_height(vd->guest.fb);
2590 for (y = 0; y < height; y += VNC_STAT_RECT) {
2591 for (x = 0; x < width; x += VNC_STAT_RECT) {
2592 VncRectStat *rect = vnc_stat_rect(vd, x, y);
2594 rect->updated = false;
2598 qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2600 if (timercmp(&vd->guest.last_freq_check, &res, >)) {
2603 vd->guest.last_freq_check = *tv;
2605 for (y = 0; y < height; y += VNC_STAT_RECT) {
2606 for (x = 0; x < width; x += VNC_STAT_RECT) {
2607 VncRectStat *rect= vnc_stat_rect(vd, x, y);
2608 int count = ARRAY_SIZE(rect->times);
2609 struct timeval min, max;
2611 if (!timerisset(&rect->times[count - 1])) {
2615 max = rect->times[(rect->idx + count - 1) % count];
2616 qemu_timersub(tv, &max, &res);
2618 if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2620 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2621 memset(rect->times, 0, sizeof (rect->times));
2625 min = rect->times[rect->idx];
2626 max = rect->times[(rect->idx + count - 1) % count];
2627 qemu_timersub(&max, &min, &res);
2629 rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2630 rect->freq /= count;
2631 rect->freq = 1. / rect->freq;
2637 double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2643 x = (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2644 y = (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2646 for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2647 for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2648 total += vnc_stat_rect(vs->vd, i, j)->freq;
2660 static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2664 rect = vnc_stat_rect(vd, x, y);
2665 if (rect->updated) {
2668 rect->times[rect->idx] = *tv;
2669 rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2670 rect->updated = true;
2673 static int vnc_refresh_server_surface(VncDisplay *vd)
2675 int width = MIN(pixman_image_get_width(vd->guest.fb),
2676 pixman_image_get_width(vd->server));
2677 int height = MIN(pixman_image_get_height(vd->guest.fb),
2678 pixman_image_get_height(vd->server));
2679 int cmp_bytes, server_stride, min_stride, guest_stride, y = 0;
2680 uint8_t *guest_row0 = NULL, *server_row0;
2683 pixman_image_t *tmpbuf = NULL;
2685 struct timeval tv = { 0, 0 };
2687 if (!vd->non_adaptive) {
2688 gettimeofday(&tv, NULL);
2689 has_dirty = vnc_update_stats(vd, &tv);
2693 * Walk through the guest dirty map.
2694 * Check and copy modified bits from guest to server surface.
2695 * Update server dirty map.
2697 server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
2698 server_stride = guest_stride = pixman_image_get_stride(vd->server);
2699 cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
2701 if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2702 int width = pixman_image_get_width(vd->server);
2703 tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
2705 guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
2706 guest_stride = pixman_image_get_stride(vd->guest.fb);
2708 min_stride = MIN(server_stride, guest_stride);
2712 uint8_t *guest_ptr, *server_ptr;
2713 unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
2714 height * VNC_DIRTY_BPL(&vd->guest),
2715 y * VNC_DIRTY_BPL(&vd->guest));
2716 if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
2717 /* no more dirty bits */
2720 y = offset / VNC_DIRTY_BPL(&vd->guest);
2721 x = offset % VNC_DIRTY_BPL(&vd->guest);
2723 server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
2725 if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2726 qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
2727 guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
2729 guest_ptr = guest_row0 + y * guest_stride;
2731 guest_ptr += x * cmp_bytes;
2733 for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
2734 x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
2735 int _cmp_bytes = cmp_bytes;
2736 if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
2739 if ((x + 1) * cmp_bytes > min_stride) {
2740 _cmp_bytes = min_stride - x * cmp_bytes;
2742 if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
2745 memcpy(server_ptr, guest_ptr, _cmp_bytes);
2746 if (!vd->non_adaptive) {
2747 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
2750 QTAILQ_FOREACH(vs, &vd->clients, next) {
2751 set_bit(x, vs->dirty[y]);
2758 qemu_pixman_image_unref(tmpbuf);
2762 static void vnc_refresh(DisplayChangeListener *dcl)
2764 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
2766 int has_dirty, rects = 0;
2768 graphic_hw_update(NULL);
2770 if (vnc_trylock_display(vd)) {
2771 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2775 has_dirty = vnc_refresh_server_surface(vd);
2776 vnc_unlock_display(vd);
2778 QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
2779 rects += vnc_update_client(vs, has_dirty, false);
2780 /* vs might be free()ed here */
2783 if (QTAILQ_EMPTY(&vd->clients)) {
2784 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
2788 if (has_dirty && rects) {
2789 vd->dcl.update_interval /= 2;
2790 if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
2791 vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
2794 vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
2795 if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
2796 vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
2801 static void vnc_connect(VncDisplay *vd, int csock,
2802 bool skipauth, bool websocket)
2804 VncState *vs = g_malloc0(sizeof(VncState));
2810 vs->auth = VNC_AUTH_NONE;
2811 #ifdef CONFIG_VNC_TLS
2812 vs->subauth = VNC_AUTH_INVALID;
2815 vs->auth = vd->auth;
2816 #ifdef CONFIG_VNC_TLS
2817 vs->subauth = vd->subauth;
2821 vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
2822 for (i = 0; i < VNC_STAT_ROWS; ++i) {
2823 vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
2826 VNC_DEBUG("New client on socket %d\n", csock);
2827 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2828 qemu_set_nonblock(vs->csock);
2829 #ifdef CONFIG_VNC_WS
2832 #ifdef CONFIG_VNC_TLS
2833 if (vd->tls.x509cert) {
2834 qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek,
2837 #endif /* CONFIG_VNC_TLS */
2839 qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read,
2843 #endif /* CONFIG_VNC_WS */
2845 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
2848 vnc_client_cache_addr(vs);
2849 vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
2850 vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
2854 #ifdef CONFIG_VNC_WS
2862 void vnc_init_state(VncState *vs)
2864 vs->initialized = true;
2865 VncDisplay *vd = vs->vd;
2870 vs->as.freq = 44100;
2871 vs->as.nchannels = 2;
2872 vs->as.fmt = AUD_FMT_S16;
2873 vs->as.endianness = 0;
2875 qemu_mutex_init(&vs->output_mutex);
2876 vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
2878 QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
2880 graphic_hw_update(NULL);
2882 vnc_write(vs, "RFB 003.008\n", 12);
2884 vnc_read_when(vs, protocol_version, 12);
2886 if (vs->vd->lock_key_sync)
2887 vs->led = qemu_add_led_event_handler(kbd_leds, vs);
2889 vs->mouse_mode_notifier.notify = check_pointer_type_change;
2890 qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
2892 /* vs might be free()ed here */
2895 static void vnc_listen_read(void *opaque, bool websocket)
2897 VncDisplay *vs = opaque;
2898 struct sockaddr_in addr;
2899 socklen_t addrlen = sizeof(addr);
2903 graphic_hw_update(NULL);
2904 #ifdef CONFIG_VNC_WS
2906 csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
2908 #endif /* CONFIG_VNC_WS */
2910 csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
2914 vnc_connect(vs, csock, false, websocket);
2918 static void vnc_listen_regular_read(void *opaque)
2920 vnc_listen_read(opaque, false);
2923 #ifdef CONFIG_VNC_WS
2924 static void vnc_listen_websocket_read(void *opaque)
2926 vnc_listen_read(opaque, true);
2928 #endif /* CONFIG_VNC_WS */
2930 static const DisplayChangeListenerOps dcl_ops = {
2932 .dpy_refresh = vnc_refresh,
2933 .dpy_gfx_copy = vnc_dpy_copy,
2934 .dpy_gfx_update = vnc_dpy_update,
2935 .dpy_gfx_switch = vnc_dpy_switch,
2936 .dpy_mouse_set = vnc_mouse_set,
2937 .dpy_cursor_define = vnc_dpy_cursor_define,
2940 void vnc_display_init(DisplayState *ds)
2942 VncDisplay *vs = g_malloc0(sizeof(*vs));
2947 #ifdef CONFIG_VNC_WS
2951 QTAILQ_INIT(&vs->clients);
2952 vs->expires = TIME_MAX;
2954 if (keyboard_layout) {
2955 trace_vnc_key_map_init(keyboard_layout);
2956 vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
2958 vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
2961 if (!vs->kbd_layout)
2964 qemu_mutex_init(&vs->mutex);
2965 vnc_start_worker_thread();
2967 vs->dcl.ops = &dcl_ops;
2968 register_displaychangelistener(&vs->dcl);
2972 static void vnc_display_close(DisplayState *ds)
2974 VncDisplay *vs = vnc_display;
2978 g_free(vs->display);
2980 if (vs->lsock != -1) {
2981 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
2985 #ifdef CONFIG_VNC_WS
2986 g_free(vs->ws_display);
2987 vs->ws_display = NULL;
2988 if (vs->lwebsock != -1) {
2989 qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
2990 close(vs->lwebsock);
2993 #endif /* CONFIG_VNC_WS */
2994 vs->auth = VNC_AUTH_INVALID;
2995 #ifdef CONFIG_VNC_TLS
2996 vs->subauth = VNC_AUTH_INVALID;
2997 vs->tls.x509verify = 0;
3001 int vnc_display_password(DisplayState *ds, const char *password)
3003 VncDisplay *vs = vnc_display;
3008 if (vs->auth == VNC_AUTH_NONE) {
3009 error_printf_unless_qmp("If you want use passwords please enable "
3010 "password auth using '-vnc ${dpy},password'.");
3014 g_free(vs->password);
3015 vs->password = g_strdup(password);
3020 int vnc_display_pw_expire(DisplayState *ds, time_t expires)
3022 VncDisplay *vs = vnc_display;
3028 vs->expires = expires;
3032 char *vnc_display_local_addr(DisplayState *ds)
3034 VncDisplay *vs = vnc_display;
3036 return vnc_socket_local_addr("%s:%s", vs->lsock);
3039 void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
3041 VncDisplay *vs = vnc_display;
3042 const char *options;
3045 #ifdef CONFIG_VNC_TLS
3046 int tls = 0, x509 = 0;
3048 #ifdef CONFIG_VNC_SASL
3052 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
3055 int lock_key_sync = 1;
3058 error_setg(errp, "VNC display not active");
3061 vnc_display_close(ds);
3062 if (strcmp(display, "none") == 0)
3065 vs->display = g_strdup(display);
3066 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
3069 while ((options = strchr(options, ','))) {
3071 if (strncmp(options, "password", 8) == 0) {
3072 if (fips_get_state()) {
3074 "VNC password auth disabled due to FIPS mode, "
3075 "consider using the VeNCrypt or SASL authentication "
3076 "methods as an alternative");
3079 password = 1; /* Require password auth */
3080 } else if (strncmp(options, "reverse", 7) == 0) {
3082 } else if (strncmp(options, "no-lock-key-sync", 16) == 0) {
3084 #ifdef CONFIG_VNC_SASL
3085 } else if (strncmp(options, "sasl", 4) == 0) {
3086 sasl = 1; /* Require SASL auth */
3088 #ifdef CONFIG_VNC_WS
3089 } else if (strncmp(options, "websocket", 9) == 0) {
3093 /* Check for 'websocket=<port>' */
3094 start = strchr(options, '=');
3095 end = strchr(options, ',');
3096 if (start && (!end || (start < end))) {
3097 int len = end ? end-(start+1) : strlen(start+1);
3099 /* extract the host specification from display */
3100 char *host = NULL, *port = NULL, *host_end = NULL;
3101 port = g_strndup(start + 1, len);
3103 /* ipv6 hosts have colons */
3104 end = strchr(display, ',');
3105 host_end = g_strrstr_len(display, end - display, ":");
3108 host = g_strndup(display, host_end - display + 1);
3110 host = g_strndup(":", 1);
3112 vs->ws_display = g_strconcat(host, port, NULL);
3117 #endif /* CONFIG_VNC_WS */
3118 #ifdef CONFIG_VNC_TLS
3119 } else if (strncmp(options, "tls", 3) == 0) {
3120 tls = 1; /* Require TLS */
3121 } else if (strncmp(options, "x509", 4) == 0) {
3123 x509 = 1; /* Require x509 certificates */
3124 if (strncmp(options, "x509verify", 10) == 0)
3125 vs->tls.x509verify = 1; /* ...and verify client certs */
3127 /* Now check for 'x509=/some/path' postfix
3128 * and use that to setup x509 certificate/key paths */
3129 start = strchr(options, '=');
3130 end = strchr(options, ',');
3131 if (start && (!end || (start < end))) {
3132 int len = end ? end-(start+1) : strlen(start+1);
3133 char *path = g_strndup(start + 1, len);
3135 VNC_DEBUG("Trying certificate path '%s'\n", path);
3136 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
3137 error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
3143 error_setg(errp, "No certificate path provided");
3147 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
3148 } else if (strncmp(options, "acl", 3) == 0) {
3151 } else if (strncmp(options, "lossy", 5) == 0) {
3152 #ifdef CONFIG_VNC_JPEG
3155 } else if (strncmp(options, "non-adaptive", 12) == 0) {
3156 vs->non_adaptive = true;
3157 } else if (strncmp(options, "share=", 6) == 0) {
3158 if (strncmp(options+6, "ignore", 6) == 0) {
3159 vs->share_policy = VNC_SHARE_POLICY_IGNORE;
3160 } else if (strncmp(options+6, "allow-exclusive", 15) == 0) {
3161 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
3162 } else if (strncmp(options+6, "force-shared", 12) == 0) {
3163 vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
3165 error_setg(errp, "unknown vnc share= option");
3171 /* adaptive updates are only used with tight encoding and
3172 * if lossy updates are enabled so we can disable all the
3173 * calculations otherwise */
3175 vs->non_adaptive = true;
3178 #ifdef CONFIG_VNC_TLS
3179 if (acl && x509 && vs->tls.x509verify) {
3180 if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
3181 fprintf(stderr, "Failed to create x509 dname ACL\n");
3186 #ifdef CONFIG_VNC_SASL
3188 if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
3189 fprintf(stderr, "Failed to create username ACL\n");
3196 * Combinations we support here:
3198 * - no-auth (clear text, no auth)
3199 * - password (clear text, weak auth)
3200 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
3201 * - tls (encrypt, weak anonymous creds, no auth)
3202 * - tls + password (encrypt, weak anonymous creds, weak auth)
3203 * - tls + sasl (encrypt, weak anonymous creds, good auth)
3204 * - tls + x509 (encrypt, good x509 creds, no auth)
3205 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
3206 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
3208 * NB1. TLS is a stackable auth scheme.
3209 * NB2. the x509 schemes have option to validate a client cert dname
3212 #ifdef CONFIG_VNC_TLS
3214 vs->auth = VNC_AUTH_VENCRYPT;
3216 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3217 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
3219 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3220 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
3223 #endif /* CONFIG_VNC_TLS */
3224 VNC_DEBUG("Initializing VNC server with password auth\n");
3225 vs->auth = VNC_AUTH_VNC;
3226 #ifdef CONFIG_VNC_TLS
3227 vs->subauth = VNC_AUTH_INVALID;
3229 #endif /* CONFIG_VNC_TLS */
3230 #ifdef CONFIG_VNC_SASL
3232 #ifdef CONFIG_VNC_TLS
3234 vs->auth = VNC_AUTH_VENCRYPT;
3236 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3237 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
3239 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3240 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
3243 #endif /* CONFIG_VNC_TLS */
3244 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3245 vs->auth = VNC_AUTH_SASL;
3246 #ifdef CONFIG_VNC_TLS
3247 vs->subauth = VNC_AUTH_INVALID;
3249 #endif /* CONFIG_VNC_TLS */
3250 #endif /* CONFIG_VNC_SASL */
3252 #ifdef CONFIG_VNC_TLS
3254 vs->auth = VNC_AUTH_VENCRYPT;
3256 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3257 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
3259 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3260 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
3264 VNC_DEBUG("Initializing VNC server with no auth\n");
3265 vs->auth = VNC_AUTH_NONE;
3266 #ifdef CONFIG_VNC_TLS
3267 vs->subauth = VNC_AUTH_INVALID;
3272 #ifdef CONFIG_VNC_SASL
3273 if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
3274 error_setg(errp, "Failed to initialize SASL auth: %s",
3275 sasl_errstring(saslErr, NULL, NULL));
3279 vs->lock_key_sync = lock_key_sync;
3282 /* connect to viewer */
3285 #ifdef CONFIG_VNC_WS
3288 if (strncmp(display, "unix:", 5) == 0) {
3289 csock = unix_connect(display+5, errp);
3291 csock = inet_connect(display, errp);
3296 vnc_connect(vs, csock, false, false);
3298 /* listen for connects */
3300 dpy = g_malloc(256);
3301 if (strncmp(display, "unix:", 5) == 0) {
3302 pstrcpy(dpy, 256, "unix:");
3303 vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
3305 vs->lsock = inet_listen(display, dpy, 256,
3306 SOCK_STREAM, 5900, errp);
3307 if (vs->lsock < 0) {
3311 #ifdef CONFIG_VNC_WS
3312 if (vs->websocket) {
3313 if (vs->ws_display) {
3314 vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
3315 SOCK_STREAM, 0, errp);
3317 vs->lwebsock = inet_listen(vs->display, NULL, 256,
3318 SOCK_STREAM, 5700, errp);
3321 if (vs->lwebsock < 0) {
3330 #endif /* CONFIG_VNC_WS */
3332 g_free(vs->display);
3334 qemu_set_fd_handler2(vs->lsock, NULL,
3335 vnc_listen_regular_read, NULL, vs);
3336 #ifdef CONFIG_VNC_WS
3337 if (vs->websocket) {
3338 qemu_set_fd_handler2(vs->lwebsock, NULL,
3339 vnc_listen_websocket_read, NULL, vs);
3341 #endif /* CONFIG_VNC_WS */
3346 g_free(vs->display);
3348 #ifdef CONFIG_VNC_WS
3349 g_free(vs->ws_display);
3350 vs->ws_display = NULL;
3351 #endif /* CONFIG_VNC_WS */
3354 void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth)
3356 VncDisplay *vs = vnc_display;
3358 vnc_connect(vs, csock, skipauth, false);