2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
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
27 #include "qemu/osdep.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/error-report.h"
33 #include "qemu/option.h"
34 #include "qemu/sockets.h"
35 #include "qemu/timer.h"
36 #include "authz/list.h"
37 #include "qemu/config-file.h"
38 #include "qapi/qapi-emit-events.h"
39 #include "qapi/qapi-events-ui.h"
40 #include "qapi/error.h"
41 #include "qapi/qapi-commands-ui.h"
43 #include "crypto/hash.h"
44 #include "crypto/tlscredsanon.h"
45 #include "crypto/tlscredsx509.h"
46 #include "qom/object_interfaces.h"
47 #include "qemu/cutils.h"
48 #include "io/dns-resolver.h"
50 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
51 #define VNC_REFRESH_INTERVAL_INC 50
52 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
53 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
54 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
56 #include "vnc_keysym.h"
57 #include "crypto/cipher.h"
59 static QTAILQ_HEAD(, VncDisplay
) vnc_displays
=
60 QTAILQ_HEAD_INITIALIZER(vnc_displays
);
62 static int vnc_cursor_define(VncState
*vs
);
63 static void vnc_update_throttle_offset(VncState
*vs
);
65 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
68 static const char *mn
[] = {
70 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
71 [VNC_SHARE_MODE_SHARED
] = "shared",
72 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
73 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
75 fprintf(stderr
, "%s/%p: %s -> %s\n", __func__
,
76 vs
->ioc
, mn
[vs
->share_mode
], mn
[mode
]);
79 switch (vs
->share_mode
) {
80 case VNC_SHARE_MODE_CONNECTING
:
81 vs
->vd
->num_connecting
--;
83 case VNC_SHARE_MODE_SHARED
:
86 case VNC_SHARE_MODE_EXCLUSIVE
:
87 vs
->vd
->num_exclusive
--;
93 vs
->share_mode
= mode
;
95 switch (vs
->share_mode
) {
96 case VNC_SHARE_MODE_CONNECTING
:
97 vs
->vd
->num_connecting
++;
99 case VNC_SHARE_MODE_SHARED
:
100 vs
->vd
->num_shared
++;
102 case VNC_SHARE_MODE_EXCLUSIVE
:
103 vs
->vd
->num_exclusive
++;
111 static void vnc_init_basic_info(SocketAddress
*addr
,
115 switch (addr
->type
) {
116 case SOCKET_ADDRESS_TYPE_INET
:
117 info
->host
= g_strdup(addr
->u
.inet
.host
);
118 info
->service
= g_strdup(addr
->u
.inet
.port
);
119 if (addr
->u
.inet
.ipv6
) {
120 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
122 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
126 case SOCKET_ADDRESS_TYPE_UNIX
:
127 info
->host
= g_strdup("");
128 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
129 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
132 case SOCKET_ADDRESS_TYPE_VSOCK
:
133 case SOCKET_ADDRESS_TYPE_FD
:
134 error_setg(errp
, "Unsupported socket address type %s",
135 SocketAddressType_str(addr
->type
));
144 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
148 SocketAddress
*addr
= NULL
;
151 error_setg(errp
, "No listener socket available");
155 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
160 vnc_init_basic_info(addr
, info
, errp
);
161 qapi_free_SocketAddress(addr
);
164 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
168 SocketAddress
*addr
= NULL
;
170 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
175 vnc_init_basic_info(addr
, info
, errp
);
176 qapi_free_SocketAddress(addr
);
179 static const char *vnc_auth_name(VncDisplay
*vd
) {
181 case VNC_AUTH_INVALID
:
197 case VNC_AUTH_VENCRYPT
:
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";
226 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
231 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
235 info
= g_malloc0(sizeof(*info
));
236 vnc_init_basic_info_from_server_addr(vd
->listener
->sioc
[0],
237 qapi_VncServerInfo_base(info
), &err
);
238 info
->has_auth
= true;
239 info
->auth
= g_strdup(vnc_auth_name(vd
));
241 qapi_free_VncServerInfo(info
);
248 static void vnc_client_cache_auth(VncState
*client
)
255 client
->info
->x509_dname
=
256 qcrypto_tls_session_get_peer_name(client
->tls
);
257 client
->info
->has_x509_dname
=
258 client
->info
->x509_dname
!= NULL
;
260 #ifdef CONFIG_VNC_SASL
261 if (client
->sasl
.conn
&&
262 client
->sasl
.username
) {
263 client
->info
->has_sasl_username
= true;
264 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
269 static void vnc_client_cache_addr(VncState
*client
)
273 client
->info
= g_malloc0(sizeof(*client
->info
));
274 vnc_init_basic_info_from_remote_addr(client
->sioc
,
275 qapi_VncClientInfo_base(client
->info
),
278 qapi_free_VncClientInfo(client
->info
);
284 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
292 si
= vnc_server_info_get(vs
->vd
);
298 case QAPI_EVENT_VNC_CONNECTED
:
299 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
));
301 case QAPI_EVENT_VNC_INITIALIZED
:
302 qapi_event_send_vnc_initialized(si
, vs
->info
);
304 case QAPI_EVENT_VNC_DISCONNECTED
:
305 qapi_event_send_vnc_disconnected(si
, vs
->info
);
311 qapi_free_VncServerInfo(si
);
314 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
319 info
= g_malloc0(sizeof(*info
));
321 vnc_init_basic_info_from_remote_addr(client
->sioc
,
322 qapi_VncClientInfo_base(info
),
326 qapi_free_VncClientInfo(info
);
330 info
->websocket
= client
->websocket
;
333 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
334 info
->has_x509_dname
= info
->x509_dname
!= NULL
;
336 #ifdef CONFIG_VNC_SASL
337 if (client
->sasl
.conn
&& client
->sasl
.username
) {
338 info
->has_sasl_username
= true;
339 info
->sasl_username
= g_strdup(client
->sasl
.username
);
346 static VncDisplay
*vnc_display_find(const char *id
)
351 return QTAILQ_FIRST(&vnc_displays
);
353 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
354 if (strcmp(id
, vd
->id
) == 0) {
361 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
363 VncClientInfoList
*cinfo
, *prev
= NULL
;
366 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
367 cinfo
= g_new0(VncClientInfoList
, 1);
368 cinfo
->value
= qmp_query_vnc_client(client
);
375 VncInfo
*qmp_query_vnc(Error
**errp
)
377 VncInfo
*info
= g_malloc0(sizeof(*info
));
378 VncDisplay
*vd
= vnc_display_find(NULL
);
379 SocketAddress
*addr
= NULL
;
381 if (vd
== NULL
|| !vd
->listener
|| !vd
->listener
->nsioc
) {
382 info
->enabled
= false;
384 info
->enabled
= true;
386 /* for compatibility with the original command */
387 info
->has_clients
= true;
388 info
->clients
= qmp_query_client_list(vd
);
390 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0],
396 switch (addr
->type
) {
397 case SOCKET_ADDRESS_TYPE_INET
:
398 info
->host
= g_strdup(addr
->u
.inet
.host
);
399 info
->service
= g_strdup(addr
->u
.inet
.port
);
400 if (addr
->u
.inet
.ipv6
) {
401 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
403 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
407 case SOCKET_ADDRESS_TYPE_UNIX
:
408 info
->host
= g_strdup("");
409 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
410 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
413 case SOCKET_ADDRESS_TYPE_VSOCK
:
414 case SOCKET_ADDRESS_TYPE_FD
:
415 error_setg(errp
, "Unsupported socket address type %s",
416 SocketAddressType_str(addr
->type
));
422 info
->has_host
= true;
423 info
->has_service
= true;
424 info
->has_family
= true;
426 info
->has_auth
= true;
427 info
->auth
= g_strdup(vnc_auth_name(vd
));
430 qapi_free_SocketAddress(addr
);
434 qapi_free_SocketAddress(addr
);
435 qapi_free_VncInfo(info
);
440 static void qmp_query_auth(int auth
, int subauth
,
441 VncPrimaryAuth
*qmp_auth
,
442 VncVencryptSubAuth
*qmp_vencrypt
,
443 bool *qmp_has_vencrypt
);
445 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
449 VncServerInfo2List
*prev
)
451 VncServerInfo2List
*list
;
452 VncServerInfo2
*info
;
456 addr
= qio_channel_socket_get_local_address(ioc
, &err
);
462 info
= g_new0(VncServerInfo2
, 1);
463 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
464 qapi_free_SocketAddress(addr
);
466 qapi_free_VncServerInfo2(info
);
470 info
->websocket
= websocket
;
472 qmp_query_auth(auth
, subauth
, &info
->auth
,
473 &info
->vencrypt
, &info
->has_vencrypt
);
475 list
= g_new0(VncServerInfo2List
, 1);
481 static void qmp_query_auth(int auth
, int subauth
,
482 VncPrimaryAuth
*qmp_auth
,
483 VncVencryptSubAuth
*qmp_vencrypt
,
484 bool *qmp_has_vencrypt
)
488 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
491 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
494 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
497 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
500 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
503 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
505 case VNC_AUTH_VENCRYPT
:
506 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
507 *qmp_has_vencrypt
= true;
509 case VNC_AUTH_VENCRYPT_PLAIN
:
510 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
512 case VNC_AUTH_VENCRYPT_TLSNONE
:
513 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
515 case VNC_AUTH_VENCRYPT_TLSVNC
:
516 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
518 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
519 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
521 case VNC_AUTH_VENCRYPT_X509NONE
:
522 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
524 case VNC_AUTH_VENCRYPT_X509VNC
:
525 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
527 case VNC_AUTH_VENCRYPT_X509PLAIN
:
528 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
530 case VNC_AUTH_VENCRYPT_TLSSASL
:
531 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
533 case VNC_AUTH_VENCRYPT_X509SASL
:
534 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
537 *qmp_has_vencrypt
= false;
542 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
546 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
551 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
553 VncInfo2List
*item
, *prev
= NULL
;
559 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
560 info
= g_new0(VncInfo2
, 1);
561 info
->id
= g_strdup(vd
->id
);
562 info
->clients
= qmp_query_client_list(vd
);
563 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
564 &info
->vencrypt
, &info
->has_vencrypt
);
566 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
568 info
->has_display
= true;
569 info
->display
= g_strdup(dev
->id
);
571 for (i
= 0; vd
->listener
!= NULL
&& i
< vd
->listener
->nsioc
; i
++) {
572 info
->server
= qmp_query_server_entry(
573 vd
->listener
->sioc
[i
], false, vd
->auth
, vd
->subauth
,
576 for (i
= 0; vd
->wslistener
!= NULL
&& i
< vd
->wslistener
->nsioc
; i
++) {
577 info
->server
= qmp_query_server_entry(
578 vd
->wslistener
->sioc
[i
], true, vd
->ws_auth
,
579 vd
->ws_subauth
, info
->server
);
582 item
= g_new0(VncInfo2List
, 1);
591 1) Get the queue working for IO.
592 2) there is some weirdness when using the -S option (the screen is grey
593 and not totally invalidated
594 3) resolutions > 1024
597 static int vnc_update_client(VncState
*vs
, int has_dirty
);
598 static void vnc_disconnect_start(VncState
*vs
);
600 static void vnc_colordepth(VncState
*vs
);
601 static void framebuffer_update_request(VncState
*vs
, int incremental
,
602 int x_position
, int y_position
,
604 static void vnc_refresh(DisplayChangeListener
*dcl
);
605 static int vnc_refresh_server_surface(VncDisplay
*vd
);
607 static int vnc_width(VncDisplay
*vd
)
609 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
610 VNC_DIRTY_PIXELS_PER_BIT
));
613 static int vnc_height(VncDisplay
*vd
)
615 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
618 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
619 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
621 int x
, int y
, int w
, int h
)
623 int width
= vnc_width(vd
);
624 int height
= vnc_height(vd
);
626 /* this is needed this to ensure we updated all affected
627 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
628 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
629 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
633 w
= MIN(x
+ w
, width
) - x
;
634 h
= MIN(y
+ h
, height
);
637 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
638 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
642 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
643 int x
, int y
, int w
, int h
)
645 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
646 struct VncSurface
*s
= &vd
->guest
;
648 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
651 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
654 vnc_write_u16(vs
, x
);
655 vnc_write_u16(vs
, y
);
656 vnc_write_u16(vs
, w
);
657 vnc_write_u16(vs
, h
);
659 vnc_write_s32(vs
, encoding
);
663 static void vnc_desktop_resize(VncState
*vs
)
665 if (vs
->ioc
== NULL
|| !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
668 if (vs
->client_width
== pixman_image_get_width(vs
->vd
->server
) &&
669 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
673 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
674 pixman_image_get_width(vs
->vd
->server
) >= 0);
675 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
676 pixman_image_get_height(vs
->vd
->server
) >= 0);
677 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
678 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
680 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
682 vnc_write_u16(vs
, 1); /* number of rects */
683 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
684 VNC_ENCODING_DESKTOPRESIZE
);
685 vnc_unlock_output(vs
);
689 static void vnc_abort_display_jobs(VncDisplay
*vd
)
693 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
696 vnc_unlock_output(vs
);
698 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
701 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
703 if (vs
->update
== VNC_STATE_UPDATE_NONE
&&
704 vs
->job_update
!= VNC_STATE_UPDATE_NONE
) {
705 /* job aborted before completion */
706 vs
->update
= vs
->job_update
;
707 vs
->job_update
= VNC_STATE_UPDATE_NONE
;
710 vnc_unlock_output(vs
);
714 int vnc_server_fb_stride(VncDisplay
*vd
)
716 return pixman_image_get_stride(vd
->server
);
719 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
723 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
724 ptr
+= y
* vnc_server_fb_stride(vd
);
725 ptr
+= x
* VNC_SERVER_FB_BYTES
;
729 static void vnc_update_server_surface(VncDisplay
*vd
)
733 qemu_pixman_image_unref(vd
->server
);
736 if (QTAILQ_EMPTY(&vd
->clients
)) {
740 width
= vnc_width(vd
);
741 height
= vnc_height(vd
);
742 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
746 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
747 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
751 static bool vnc_check_pageflip(DisplaySurface
*s1
,
754 return (s1
!= NULL
&&
756 surface_width(s1
) == surface_width(s2
) &&
757 surface_height(s1
) == surface_height(s2
) &&
758 surface_format(s1
) == surface_format(s2
));
762 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
763 DisplaySurface
*surface
)
765 static const char placeholder_msg
[] =
766 "Display output is not active.";
767 static DisplaySurface
*placeholder
;
768 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
769 bool pageflip
= vnc_check_pageflip(vd
->ds
, surface
);
772 if (surface
== NULL
) {
773 if (placeholder
== NULL
) {
774 placeholder
= qemu_create_message_surface(640, 480, placeholder_msg
);
776 surface
= placeholder
;
779 vnc_abort_display_jobs(vd
);
783 qemu_pixman_image_unref(vd
->guest
.fb
);
784 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
785 vd
->guest
.format
= surface
->format
;
788 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
789 surface_width(surface
),
790 surface_height(surface
));
795 vnc_update_server_surface(vd
);
797 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
799 vnc_desktop_resize(vs
);
800 if (vs
->vd
->cursor
) {
801 vnc_cursor_define(vs
);
803 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
804 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
807 vnc_update_throttle_offset(vs
);
812 static void vnc_write_pixels_copy(VncState
*vs
,
813 void *pixels
, int size
)
815 vnc_write(vs
, pixels
, size
);
818 /* slowest but generic code. */
819 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
823 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
824 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
825 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
826 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
828 # error need some bits here if you change VNC_SERVER_FB_FORMAT
830 v
= (r
<< vs
->client_pf
.rshift
) |
831 (g
<< vs
->client_pf
.gshift
) |
832 (b
<< vs
->client_pf
.bshift
);
833 switch (vs
->client_pf
.bytes_per_pixel
) {
863 static void vnc_write_pixels_generic(VncState
*vs
,
864 void *pixels1
, int size
)
868 if (VNC_SERVER_FB_BYTES
== 4) {
869 uint32_t *pixels
= pixels1
;
872 for (i
= 0; i
< n
; i
++) {
873 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
874 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
879 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
883 VncDisplay
*vd
= vs
->vd
;
885 row
= vnc_server_fb_ptr(vd
, x
, y
);
886 for (i
= 0; i
< h
; i
++) {
887 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
888 row
+= vnc_server_fb_stride(vd
);
893 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
896 bool encode_raw
= false;
897 size_t saved_offs
= vs
->output
.offset
;
899 switch(vs
->vnc_encoding
) {
900 case VNC_ENCODING_ZLIB
:
901 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
903 case VNC_ENCODING_HEXTILE
:
904 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
905 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
907 case VNC_ENCODING_TIGHT
:
908 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
910 case VNC_ENCODING_TIGHT_PNG
:
911 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
913 case VNC_ENCODING_ZRLE
:
914 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
916 case VNC_ENCODING_ZYWRLE
:
917 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
924 /* If the client has the same pixel format as our internal buffer and
925 * a RAW encoding would need less space fall back to RAW encoding to
926 * save bandwidth and processing power in the client. */
927 if (!encode_raw
&& vs
->write_pixels
== vnc_write_pixels_copy
&&
928 12 + h
* w
* VNC_SERVER_FB_BYTES
<= (vs
->output
.offset
- saved_offs
)) {
929 vs
->output
.offset
= saved_offs
;
934 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
935 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
941 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
942 int x
, int y
, int visible
)
944 /* can we ask the client(s) to move the pointer ??? */
947 static int vnc_cursor_define(VncState
*vs
)
949 QEMUCursor
*c
= vs
->vd
->cursor
;
952 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
954 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
955 vnc_write_u8(vs
, 0); /* padding */
956 vnc_write_u16(vs
, 1); /* # of rects */
957 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
958 VNC_ENCODING_RICH_CURSOR
);
959 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
960 vnc_write_pixels_generic(vs
, c
->data
, isize
);
961 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
962 vnc_unlock_output(vs
);
968 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
971 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
974 cursor_put(vd
->cursor
);
975 g_free(vd
->cursor_mask
);
978 cursor_get(vd
->cursor
);
979 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
980 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
981 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
983 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
984 vnc_cursor_define(vs
);
988 static int find_and_clear_dirty_height(VncState
*vs
,
989 int y
, int last_x
, int x
, int height
)
993 for (h
= 1; h
< (height
- y
); h
++) {
994 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
997 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
1004 * Figure out how much pending data we should allow in the output
1005 * buffer before we throttle incremental display updates, and/or
1006 * drop audio samples.
1008 * We allow for equiv of 1 full display's worth of FB updates,
1009 * and 1 second of audio samples. If audio backlog was larger
1010 * than that the client would already suffering awful audio
1011 * glitches, so dropping samples is no worse really).
1013 static void vnc_update_throttle_offset(VncState
*vs
)
1016 vs
->client_width
* vs
->client_height
* vs
->client_pf
.bytes_per_pixel
;
1018 if (vs
->audio_cap
) {
1020 switch (vs
->as
.fmt
) {
1022 case AUDIO_FORMAT_U8
:
1023 case AUDIO_FORMAT_S8
:
1026 case AUDIO_FORMAT_U16
:
1027 case AUDIO_FORMAT_S16
:
1030 case AUDIO_FORMAT_U32
:
1031 case AUDIO_FORMAT_S32
:
1035 offset
+= vs
->as
.freq
* bps
* vs
->as
.nchannels
;
1038 /* Put a floor of 1MB on offset, so that if we have a large pending
1039 * buffer and the display is resized to a small size & back again
1040 * we don't suddenly apply a tiny send limit
1042 offset
= MAX(offset
, 1024 * 1024);
1044 if (vs
->throttle_output_offset
!= offset
) {
1045 trace_vnc_client_throttle_threshold(
1046 vs
, vs
->ioc
, vs
->throttle_output_offset
, offset
, vs
->client_width
,
1047 vs
->client_height
, vs
->client_pf
.bytes_per_pixel
, vs
->audio_cap
);
1050 vs
->throttle_output_offset
= offset
;
1053 static bool vnc_should_update(VncState
*vs
)
1055 switch (vs
->update
) {
1056 case VNC_STATE_UPDATE_NONE
:
1058 case VNC_STATE_UPDATE_INCREMENTAL
:
1059 /* Only allow incremental updates if the pending send queue
1060 * is less than the permitted threshold, and the job worker
1061 * is completely idle.
1063 if (vs
->output
.offset
< vs
->throttle_output_offset
&&
1064 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1067 trace_vnc_client_throttle_incremental(
1068 vs
, vs
->ioc
, vs
->job_update
, vs
->output
.offset
);
1070 case VNC_STATE_UPDATE_FORCE
:
1071 /* Only allow forced updates if the pending send queue
1072 * does not contain a previous forced update, and the
1073 * job worker is completely idle.
1075 * Note this means we'll queue a forced update, even if
1076 * the output buffer size is otherwise over the throttle
1079 if (vs
->force_update_offset
== 0 &&
1080 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1083 trace_vnc_client_throttle_forced(
1084 vs
, vs
->ioc
, vs
->job_update
, vs
->force_update_offset
);
1090 static int vnc_update_client(VncState
*vs
, int has_dirty
)
1092 VncDisplay
*vd
= vs
->vd
;
1098 if (vs
->disconnecting
) {
1099 vnc_disconnect_finish(vs
);
1103 vs
->has_dirty
+= has_dirty
;
1104 if (!vnc_should_update(vs
)) {
1108 if (!vs
->has_dirty
&& vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
1113 * Send screen updates to the vnc client using the server
1114 * surface and server dirty map. guest surface updates
1115 * happening in parallel don't disturb us, the next pass will
1116 * send them to the client.
1118 job
= vnc_job_new(vs
);
1120 height
= pixman_image_get_height(vd
->server
);
1121 width
= pixman_image_get_width(vd
->server
);
1127 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1128 height
* VNC_DIRTY_BPL(vs
),
1129 y
* VNC_DIRTY_BPL(vs
));
1130 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1131 /* no more dirty bits */
1134 y
= offset
/ VNC_DIRTY_BPL(vs
);
1135 x
= offset
% VNC_DIRTY_BPL(vs
);
1136 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1137 VNC_DIRTY_BPL(vs
), x
);
1138 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1139 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1140 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1142 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1143 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1145 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1153 vs
->job_update
= vs
->update
;
1154 vs
->update
= VNC_STATE_UPDATE_NONE
;
1161 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1163 VncState
*vs
= opaque
;
1165 assert(vs
->magic
== VNC_MAGIC
);
1167 case AUD_CNOTIFY_DISABLE
:
1168 vnc_lock_output(vs
);
1169 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1170 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1171 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
1172 vnc_unlock_output(vs
);
1176 case AUD_CNOTIFY_ENABLE
:
1177 vnc_lock_output(vs
);
1178 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1179 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1180 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1181 vnc_unlock_output(vs
);
1187 static void audio_capture_destroy(void *opaque
)
1191 static void audio_capture(void *opaque
, void *buf
, int size
)
1193 VncState
*vs
= opaque
;
1195 assert(vs
->magic
== VNC_MAGIC
);
1196 vnc_lock_output(vs
);
1197 if (vs
->output
.offset
< vs
->throttle_output_offset
) {
1198 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1199 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1200 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1201 vnc_write_u32(vs
, size
);
1202 vnc_write(vs
, buf
, size
);
1204 trace_vnc_client_throttle_audio(vs
, vs
->ioc
, vs
->output
.offset
);
1206 vnc_unlock_output(vs
);
1210 static void audio_add(VncState
*vs
)
1212 struct audio_capture_ops ops
;
1214 if (vs
->audio_cap
) {
1215 error_report("audio already running");
1219 ops
.notify
= audio_capture_notify
;
1220 ops
.destroy
= audio_capture_destroy
;
1221 ops
.capture
= audio_capture
;
1223 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1224 if (!vs
->audio_cap
) {
1225 error_report("Failed to add audio capture");
1229 static void audio_del(VncState
*vs
)
1231 if (vs
->audio_cap
) {
1232 AUD_del_capture(vs
->audio_cap
, vs
);
1233 vs
->audio_cap
= NULL
;
1237 static void vnc_disconnect_start(VncState
*vs
)
1239 if (vs
->disconnecting
) {
1242 trace_vnc_client_disconnect_start(vs
, vs
->ioc
);
1243 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1245 g_source_remove(vs
->ioc_tag
);
1248 qio_channel_close(vs
->ioc
, NULL
);
1249 vs
->disconnecting
= TRUE
;
1252 void vnc_disconnect_finish(VncState
*vs
)
1256 trace_vnc_client_disconnect_finish(vs
, vs
->ioc
);
1258 vnc_jobs_join(vs
); /* Wait encoding jobs */
1260 vnc_lock_output(vs
);
1261 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1263 buffer_free(&vs
->input
);
1264 buffer_free(&vs
->output
);
1266 qapi_free_VncClientInfo(vs
->info
);
1269 vnc_tight_clear(vs
);
1272 #ifdef CONFIG_VNC_SASL
1273 vnc_sasl_client_cleanup(vs
);
1274 #endif /* CONFIG_VNC_SASL */
1276 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1278 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1279 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1281 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1282 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1283 /* last client gone */
1284 vnc_update_server_surface(vs
->vd
);
1287 vnc_unlock_output(vs
);
1289 qemu_mutex_destroy(&vs
->output_mutex
);
1290 if (vs
->bh
!= NULL
) {
1291 qemu_bh_delete(vs
->bh
);
1293 buffer_free(&vs
->jobs_buffer
);
1295 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1296 g_free(vs
->lossy_rect
[i
]);
1298 g_free(vs
->lossy_rect
);
1300 object_unref(OBJECT(vs
->ioc
));
1302 object_unref(OBJECT(vs
->sioc
));
1308 size_t vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
**errp
)
1312 trace_vnc_client_eof(vs
, vs
->ioc
);
1313 vnc_disconnect_start(vs
);
1314 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1315 trace_vnc_client_io_error(vs
, vs
->ioc
,
1316 errp
? error_get_pretty(*errp
) :
1318 vnc_disconnect_start(vs
);
1331 void vnc_client_error(VncState
*vs
)
1333 VNC_DEBUG("Closing down client sock: protocol error\n");
1334 vnc_disconnect_start(vs
);
1339 * Called to write a chunk of data to the client socket. The data may
1340 * be the raw data, or may have already been encoded by SASL.
1341 * The data will be written either straight onto the socket, or
1342 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1344 * NB, it is theoretically possible to have 2 layers of encryption,
1345 * both SASL, and this TLS layer. It is highly unlikely in practice
1346 * though, since SASL encryption will typically be a no-op if TLS
1349 * Returns the number of bytes written, which may be less than
1350 * the requested 'datalen' if the socket would block. Returns
1351 * 0 on I/O error, and disconnects the client socket.
1353 size_t vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1357 ret
= qio_channel_write(
1358 vs
->ioc
, (const char *)data
, datalen
, &err
);
1359 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1360 return vnc_client_io_error(vs
, ret
, &err
);
1365 * Called to write buffered data to the client socket, when not
1366 * using any SASL SSF encryption layers. Will write as much data
1367 * as possible without blocking. If all buffered data is written,
1368 * will switch the FD poll() handler back to read monitoring.
1370 * Returns the number of bytes written, which may be less than
1371 * the buffered output data if the socket would block. Returns
1372 * 0 on I/O error, and disconnects the client socket.
1374 static size_t vnc_client_write_plain(VncState
*vs
)
1379 #ifdef CONFIG_VNC_SASL
1380 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1381 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1382 vs
->sasl
.waitWriteSSF
);
1384 if (vs
->sasl
.conn
&&
1386 vs
->sasl
.waitWriteSSF
) {
1387 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1389 vs
->sasl
.waitWriteSSF
-= ret
;
1391 #endif /* CONFIG_VNC_SASL */
1392 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1396 if (ret
>= vs
->force_update_offset
) {
1397 if (vs
->force_update_offset
!= 0) {
1398 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
1400 vs
->force_update_offset
= 0;
1402 vs
->force_update_offset
-= ret
;
1404 offset
= vs
->output
.offset
;
1405 buffer_advance(&vs
->output
, ret
);
1406 if (offset
>= vs
->throttle_output_offset
&&
1407 vs
->output
.offset
< vs
->throttle_output_offset
) {
1408 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
, vs
->output
.offset
);
1411 if (vs
->output
.offset
== 0) {
1413 g_source_remove(vs
->ioc_tag
);
1415 vs
->ioc_tag
= qio_channel_add_watch(
1416 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1424 * First function called whenever there is data to be written to
1425 * the client socket. Will delegate actual work according to whether
1426 * SASL SSF layers are enabled (thus requiring encryption calls)
1428 static void vnc_client_write_locked(VncState
*vs
)
1430 #ifdef CONFIG_VNC_SASL
1431 if (vs
->sasl
.conn
&&
1433 !vs
->sasl
.waitWriteSSF
) {
1434 vnc_client_write_sasl(vs
);
1436 #endif /* CONFIG_VNC_SASL */
1438 vnc_client_write_plain(vs
);
1442 static void vnc_client_write(VncState
*vs
)
1444 assert(vs
->magic
== VNC_MAGIC
);
1445 vnc_lock_output(vs
);
1446 if (vs
->output
.offset
) {
1447 vnc_client_write_locked(vs
);
1448 } else if (vs
->ioc
!= NULL
) {
1450 g_source_remove(vs
->ioc_tag
);
1452 vs
->ioc_tag
= qio_channel_add_watch(
1453 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1455 vnc_unlock_output(vs
);
1458 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1460 vs
->read_handler
= func
;
1461 vs
->read_handler_expect
= expecting
;
1466 * Called to read a chunk of data from the client socket. The data may
1467 * be the raw data, or may need to be further decoded by SASL.
1468 * The data will be read either straight from to the socket, or
1469 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1471 * NB, it is theoretically possible to have 2 layers of encryption,
1472 * both SASL, and this TLS layer. It is highly unlikely in practice
1473 * though, since SASL encryption will typically be a no-op if TLS
1476 * Returns the number of bytes read, which may be less than
1477 * the requested 'datalen' if the socket would block. Returns
1478 * 0 on I/O error or EOF, and disconnects the client socket.
1480 size_t vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1484 ret
= qio_channel_read(
1485 vs
->ioc
, (char *)data
, datalen
, &err
);
1486 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1487 return vnc_client_io_error(vs
, ret
, &err
);
1492 * Called to read data from the client socket to the input buffer,
1493 * when not using any SASL SSF encryption layers. Will read as much
1494 * data as possible without blocking.
1496 * Returns the number of bytes read, which may be less than
1497 * the requested 'datalen' if the socket would block. Returns
1498 * 0 on I/O error or EOF, and disconnects the client socket.
1500 static size_t vnc_client_read_plain(VncState
*vs
)
1503 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1504 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1505 buffer_reserve(&vs
->input
, 4096);
1506 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1509 vs
->input
.offset
+= ret
;
1513 static void vnc_jobs_bh(void *opaque
)
1515 VncState
*vs
= opaque
;
1517 assert(vs
->magic
== VNC_MAGIC
);
1518 vnc_jobs_consume_buffer(vs
);
1522 * First function called whenever there is more data to be read from
1523 * the client socket. Will delegate actual work according to whether
1524 * SASL SSF layers are enabled (thus requiring decryption calls)
1525 * Returns 0 on success, -1 if client disconnected
1527 static int vnc_client_read(VncState
*vs
)
1531 #ifdef CONFIG_VNC_SASL
1532 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1533 ret
= vnc_client_read_sasl(vs
);
1535 #endif /* CONFIG_VNC_SASL */
1536 ret
= vnc_client_read_plain(vs
);
1538 if (vs
->disconnecting
) {
1539 vnc_disconnect_finish(vs
);
1545 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1546 size_t len
= vs
->read_handler_expect
;
1549 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1550 if (vs
->disconnecting
) {
1551 vnc_disconnect_finish(vs
);
1556 buffer_advance(&vs
->input
, len
);
1558 vs
->read_handler_expect
= ret
;
1564 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1565 GIOCondition condition
, void *opaque
)
1567 VncState
*vs
= opaque
;
1569 assert(vs
->magic
== VNC_MAGIC
);
1570 if (condition
& G_IO_IN
) {
1571 if (vnc_client_read(vs
) < 0) {
1572 /* vs is free()ed here */
1576 if (condition
& G_IO_OUT
) {
1577 vnc_client_write(vs
);
1580 if (vs
->disconnecting
) {
1581 if (vs
->ioc_tag
!= 0) {
1582 g_source_remove(vs
->ioc_tag
);
1591 * Scale factor to apply to vs->throttle_output_offset when checking for
1592 * hard limit. Worst case normal usage could be x2, if we have a complete
1593 * incremental update and complete forced update in the output buffer.
1594 * So x3 should be good enough, but we pick x5 to be conservative and thus
1595 * (hopefully) never trigger incorrectly.
1597 #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1599 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1601 assert(vs
->magic
== VNC_MAGIC
);
1602 if (vs
->disconnecting
) {
1605 /* Protection against malicious client/guest to prevent our output
1606 * buffer growing without bound if client stops reading data. This
1607 * should rarely trigger, because we have earlier throttling code
1608 * which stops issuing framebuffer updates and drops audio data
1609 * if the throttle_output_offset value is exceeded. So we only reach
1610 * this higher level if a huge number of pseudo-encodings get
1611 * triggered while data can't be sent on the socket.
1613 * NB throttle_output_offset can be zero during early protocol
1614 * handshake, or from the job thread's VncState clone
1616 if (vs
->throttle_output_offset
!= 0 &&
1617 (vs
->output
.offset
/ VNC_THROTTLE_OUTPUT_LIMIT_SCALE
) >
1618 vs
->throttle_output_offset
) {
1619 trace_vnc_client_output_limit(vs
, vs
->ioc
, vs
->output
.offset
,
1620 vs
->throttle_output_offset
);
1621 vnc_disconnect_start(vs
);
1624 buffer_reserve(&vs
->output
, len
);
1626 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1628 g_source_remove(vs
->ioc_tag
);
1630 vs
->ioc_tag
= qio_channel_add_watch(
1631 vs
->ioc
, G_IO_IN
| G_IO_OUT
, vnc_client_io
, vs
, NULL
);
1634 buffer_append(&vs
->output
, data
, len
);
1637 void vnc_write_s32(VncState
*vs
, int32_t value
)
1639 vnc_write_u32(vs
, *(uint32_t *)&value
);
1642 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1646 buf
[0] = (value
>> 24) & 0xFF;
1647 buf
[1] = (value
>> 16) & 0xFF;
1648 buf
[2] = (value
>> 8) & 0xFF;
1649 buf
[3] = value
& 0xFF;
1651 vnc_write(vs
, buf
, 4);
1654 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1658 buf
[0] = (value
>> 8) & 0xFF;
1659 buf
[1] = value
& 0xFF;
1661 vnc_write(vs
, buf
, 2);
1664 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1666 vnc_write(vs
, (char *)&value
, 1);
1669 void vnc_flush(VncState
*vs
)
1671 vnc_lock_output(vs
);
1672 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1673 vnc_client_write_locked(vs
);
1675 if (vs
->disconnecting
) {
1676 if (vs
->ioc_tag
!= 0) {
1677 g_source_remove(vs
->ioc_tag
);
1681 vnc_unlock_output(vs
);
1684 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1686 return data
[offset
];
1689 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1691 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1694 static int32_t read_s32(uint8_t *data
, size_t offset
)
1696 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1697 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1700 uint32_t read_u32(uint8_t *data
, size_t offset
)
1702 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1703 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1706 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1710 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1712 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1713 int absolute
= qemu_input_is_absolute();
1715 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1716 vnc_lock_output(vs
);
1717 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1718 vnc_write_u8(vs
, 0);
1719 vnc_write_u16(vs
, 1);
1720 vnc_framebuffer_update(vs
, absolute
, 0,
1721 pixman_image_get_width(vs
->vd
->server
),
1722 pixman_image_get_height(vs
->vd
->server
),
1723 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1724 vnc_unlock_output(vs
);
1727 vs
->absolute
= absolute
;
1730 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1732 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1733 [INPUT_BUTTON_LEFT
] = 0x01,
1734 [INPUT_BUTTON_MIDDLE
] = 0x02,
1735 [INPUT_BUTTON_RIGHT
] = 0x04,
1736 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1737 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1739 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1740 int width
= pixman_image_get_width(vs
->vd
->server
);
1741 int height
= pixman_image_get_height(vs
->vd
->server
);
1743 if (vs
->last_bmask
!= button_mask
) {
1744 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1745 vs
->last_bmask
= button_mask
;
1749 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, 0, width
);
1750 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, 0, height
);
1751 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1752 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1753 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1755 if (vs
->last_x
!= -1) {
1756 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1757 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1762 qemu_input_event_sync();
1765 static void press_key(VncState
*vs
, QKeyCode qcode
)
1767 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, true);
1768 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, false);
1771 static void vnc_led_state_change(VncState
*vs
)
1773 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1777 vnc_lock_output(vs
);
1778 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1779 vnc_write_u8(vs
, 0);
1780 vnc_write_u16(vs
, 1);
1781 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1782 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1783 vnc_unlock_output(vs
);
1787 static void kbd_leds(void *opaque
, int ledstate
)
1789 VncDisplay
*vd
= opaque
;
1792 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1793 (ledstate
& QEMU_NUM_LOCK_LED
),
1794 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1796 if (ledstate
== vd
->ledstate
) {
1800 vd
->ledstate
= ledstate
;
1802 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1803 vnc_led_state_change(client
);
1807 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1809 QKeyCode qcode
= qemu_input_key_number_to_qcode(keycode
);
1811 /* QEMU console switch */
1813 case Q_KEY_CODE_1
... Q_KEY_CODE_9
: /* '1' to '9' keys */
1814 if (vs
->vd
->dcl
.con
== NULL
&& down
&&
1815 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
) &&
1816 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_ALT
)) {
1817 /* Reset the modifiers sent to the current console */
1818 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1819 console_select(qcode
- Q_KEY_CODE_1
);
1826 /* Turn off the lock state sync logic if the client support the led
1829 if (down
&& vs
->vd
->lock_key_sync
&&
1830 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1831 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1832 /* If the numlock state needs to change then simulate an additional
1833 keypress before sending this one. This will happen if the user
1834 toggles numlock away from the VNC window.
1836 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1837 if (!qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1838 trace_vnc_key_sync_numlock(true);
1839 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1842 if (qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1843 trace_vnc_key_sync_numlock(false);
1844 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1849 if (down
&& vs
->vd
->lock_key_sync
&&
1850 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1851 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1852 /* If the capslock state needs to change then simulate an additional
1853 keypress before sending this one. This will happen if the user
1854 toggles capslock away from the VNC window.
1856 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1857 bool shift
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_SHIFT
);
1858 bool capslock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CAPSLOCK
);
1860 if (uppercase
== shift
) {
1861 trace_vnc_key_sync_capslock(false);
1862 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1865 if (uppercase
!= shift
) {
1866 trace_vnc_key_sync_capslock(true);
1867 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1872 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, down
);
1873 if (!qemu_console_is_graphic(NULL
)) {
1874 bool numlock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
);
1875 bool control
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
);
1876 /* QEMU console emulation */
1879 case 0x2a: /* Left Shift */
1880 case 0x36: /* Right Shift */
1881 case 0x1d: /* Left CTRL */
1882 case 0x9d: /* Right CTRL */
1883 case 0x38: /* Left ALT */
1884 case 0xb8: /* Right ALT */
1887 kbd_put_keysym(QEMU_KEY_UP
);
1890 kbd_put_keysym(QEMU_KEY_DOWN
);
1893 kbd_put_keysym(QEMU_KEY_LEFT
);
1896 kbd_put_keysym(QEMU_KEY_RIGHT
);
1899 kbd_put_keysym(QEMU_KEY_DELETE
);
1902 kbd_put_keysym(QEMU_KEY_HOME
);
1905 kbd_put_keysym(QEMU_KEY_END
);
1908 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1911 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1915 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1918 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1921 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1924 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1927 kbd_put_keysym('5');
1930 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1933 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1936 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1939 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1942 kbd_put_keysym('0');
1945 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1949 kbd_put_keysym('/');
1952 kbd_put_keysym('*');
1955 kbd_put_keysym('-');
1958 kbd_put_keysym('+');
1961 kbd_put_keysym('\n');
1966 kbd_put_keysym(sym
& 0x1f);
1968 kbd_put_keysym(sym
);
1976 static const char *code2name(int keycode
)
1978 return QKeyCode_str(qemu_input_key_number_to_qcode(keycode
));
1981 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1986 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(NULL
)) {
1987 lsym
= lsym
- 'A' + 'a';
1990 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF,
1991 vs
->vd
->kbd
, down
) & SCANCODE_KEYMASK
;
1992 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
1993 do_key_event(vs
, down
, keycode
, sym
);
1996 static void ext_key_event(VncState
*vs
, int down
,
1997 uint32_t sym
, uint16_t keycode
)
1999 /* if the user specifies a keyboard layout, always use it */
2000 if (keyboard_layout
) {
2001 key_event(vs
, down
, sym
);
2003 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
2004 do_key_event(vs
, down
, keycode
, sym
);
2008 static void framebuffer_update_request(VncState
*vs
, int incremental
,
2009 int x
, int y
, int w
, int h
)
2012 if (vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
2013 vs
->update
= VNC_STATE_UPDATE_INCREMENTAL
;
2016 vs
->update
= VNC_STATE_UPDATE_FORCE
;
2017 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
2021 static void send_ext_key_event_ack(VncState
*vs
)
2023 vnc_lock_output(vs
);
2024 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2025 vnc_write_u8(vs
, 0);
2026 vnc_write_u16(vs
, 1);
2027 vnc_framebuffer_update(vs
, 0, 0,
2028 pixman_image_get_width(vs
->vd
->server
),
2029 pixman_image_get_height(vs
->vd
->server
),
2030 VNC_ENCODING_EXT_KEY_EVENT
);
2031 vnc_unlock_output(vs
);
2035 static void send_ext_audio_ack(VncState
*vs
)
2037 vnc_lock_output(vs
);
2038 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2039 vnc_write_u8(vs
, 0);
2040 vnc_write_u16(vs
, 1);
2041 vnc_framebuffer_update(vs
, 0, 0,
2042 pixman_image_get_width(vs
->vd
->server
),
2043 pixman_image_get_height(vs
->vd
->server
),
2044 VNC_ENCODING_AUDIO
);
2045 vnc_unlock_output(vs
);
2049 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2052 unsigned int enc
= 0;
2055 vs
->vnc_encoding
= 0;
2056 vs
->tight
.compression
= 9;
2057 vs
->tight
.quality
= -1; /* Lossless by default */
2061 * Start from the end because the encodings are sent in order of preference.
2062 * This way the preferred encoding (first encoding defined in the array)
2063 * will be set at the end of the loop.
2065 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2068 case VNC_ENCODING_RAW
:
2069 vs
->vnc_encoding
= enc
;
2071 case VNC_ENCODING_COPYRECT
:
2072 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
2074 case VNC_ENCODING_HEXTILE
:
2075 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
2076 vs
->vnc_encoding
= enc
;
2078 case VNC_ENCODING_TIGHT
:
2079 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
2080 vs
->vnc_encoding
= enc
;
2082 #ifdef CONFIG_VNC_PNG
2083 case VNC_ENCODING_TIGHT_PNG
:
2084 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
2085 vs
->vnc_encoding
= enc
;
2088 case VNC_ENCODING_ZLIB
:
2089 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
2090 vs
->vnc_encoding
= enc
;
2092 case VNC_ENCODING_ZRLE
:
2093 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
2094 vs
->vnc_encoding
= enc
;
2096 case VNC_ENCODING_ZYWRLE
:
2097 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
2098 vs
->vnc_encoding
= enc
;
2100 case VNC_ENCODING_DESKTOPRESIZE
:
2101 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
2103 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2104 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
2106 case VNC_ENCODING_RICH_CURSOR
:
2107 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
2108 if (vs
->vd
->cursor
) {
2109 vnc_cursor_define(vs
);
2112 case VNC_ENCODING_EXT_KEY_EVENT
:
2113 send_ext_key_event_ack(vs
);
2115 case VNC_ENCODING_AUDIO
:
2116 send_ext_audio_ack(vs
);
2118 case VNC_ENCODING_WMVi
:
2119 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
2121 case VNC_ENCODING_LED_STATE
:
2122 vs
->features
|= VNC_FEATURE_LED_STATE_MASK
;
2124 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2125 vs
->tight
.compression
= (enc
& 0x0F);
2127 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2128 if (vs
->vd
->lossy
) {
2129 vs
->tight
.quality
= (enc
& 0x0F);
2133 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2137 vnc_desktop_resize(vs
);
2138 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2139 vnc_led_state_change(vs
);
2142 static void set_pixel_conversion(VncState
*vs
)
2144 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2146 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2147 vs
->write_pixels
= vnc_write_pixels_copy
;
2148 vnc_hextile_set_pixel_conversion(vs
, 0);
2150 vs
->write_pixels
= vnc_write_pixels_generic
;
2151 vnc_hextile_set_pixel_conversion(vs
, 1);
2155 static void send_color_map(VncState
*vs
)
2159 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2160 vnc_write_u8(vs
, 0); /* padding */
2161 vnc_write_u16(vs
, 0); /* first color */
2162 vnc_write_u16(vs
, 256); /* # of colors */
2164 for (i
= 0; i
< 256; i
++) {
2165 PixelFormat
*pf
= &vs
->client_pf
;
2167 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2168 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2169 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2173 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2174 int big_endian_flag
, int true_color_flag
,
2175 int red_max
, int green_max
, int blue_max
,
2176 int red_shift
, int green_shift
, int blue_shift
)
2178 if (!true_color_flag
) {
2179 /* Expose a reasonable default 256 color map */
2189 switch (bits_per_pixel
) {
2195 vnc_client_error(vs
);
2199 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2200 vs
->client_pf
.rbits
= ctpopl(red_max
);
2201 vs
->client_pf
.rshift
= red_shift
;
2202 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2203 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2204 vs
->client_pf
.gbits
= ctpopl(green_max
);
2205 vs
->client_pf
.gshift
= green_shift
;
2206 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2207 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2208 vs
->client_pf
.bbits
= ctpopl(blue_max
);
2209 vs
->client_pf
.bshift
= blue_shift
;
2210 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2211 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2212 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2213 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2214 vs
->client_be
= big_endian_flag
;
2216 if (!true_color_flag
) {
2220 set_pixel_conversion(vs
);
2222 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2223 graphic_hw_update(vs
->vd
->dcl
.con
);
2226 static void pixel_format_message (VncState
*vs
) {
2227 char pad
[3] = { 0, 0, 0 };
2229 vs
->client_pf
= qemu_default_pixelformat(32);
2231 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2232 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2234 #ifdef HOST_WORDS_BIGENDIAN
2235 vnc_write_u8(vs
, 1); /* big-endian-flag */
2237 vnc_write_u8(vs
, 0); /* big-endian-flag */
2239 vnc_write_u8(vs
, 1); /* true-color-flag */
2240 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2241 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2242 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2243 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2244 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2245 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2246 vnc_write(vs
, pad
, 3); /* padding */
2248 vnc_hextile_set_pixel_conversion(vs
, 0);
2249 vs
->write_pixels
= vnc_write_pixels_copy
;
2252 static void vnc_colordepth(VncState
*vs
)
2254 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2255 /* Sending a WMVi message to notify the client*/
2256 vnc_lock_output(vs
);
2257 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2258 vnc_write_u8(vs
, 0);
2259 vnc_write_u16(vs
, 1); /* number of rects */
2260 vnc_framebuffer_update(vs
, 0, 0,
2261 pixman_image_get_width(vs
->vd
->server
),
2262 pixman_image_get_height(vs
->vd
->server
),
2264 pixel_format_message(vs
);
2265 vnc_unlock_output(vs
);
2268 set_pixel_conversion(vs
);
2272 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2277 VncDisplay
*vd
= vs
->vd
;
2280 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2284 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2288 set_pixel_format(vs
, read_u8(data
, 4),
2289 read_u8(data
, 6), read_u8(data
, 7),
2290 read_u16(data
, 8), read_u16(data
, 10),
2291 read_u16(data
, 12), read_u8(data
, 14),
2292 read_u8(data
, 15), read_u8(data
, 16));
2294 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2299 limit
= read_u16(data
, 2);
2301 return 4 + (limit
* 4);
2303 limit
= read_u16(data
, 2);
2305 for (i
= 0; i
< limit
; i
++) {
2306 int32_t val
= read_s32(data
, 4 + (i
* 4));
2307 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2310 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2312 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2316 framebuffer_update_request(vs
,
2317 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2318 read_u16(data
, 6), read_u16(data
, 8));
2320 case VNC_MSG_CLIENT_KEY_EVENT
:
2324 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2326 case VNC_MSG_CLIENT_POINTER_EVENT
:
2330 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2332 case VNC_MSG_CLIENT_CUT_TEXT
:
2337 uint32_t dlen
= read_u32(data
, 4);
2338 if (dlen
> (1 << 20)) {
2339 error_report("vnc: client_cut_text msg payload has %u bytes"
2340 " which exceeds our limit of 1MB.", dlen
);
2341 vnc_client_error(vs
);
2349 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2351 case VNC_MSG_CLIENT_QEMU
:
2355 switch (read_u8(data
, 1)) {
2356 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2360 ext_key_event(vs
, read_u16(data
, 2),
2361 read_u32(data
, 4), read_u32(data
, 8));
2363 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2367 switch (read_u16 (data
, 2)) {
2368 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2371 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2374 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2377 switch (read_u8(data
, 4)) {
2378 case 0: vs
->as
.fmt
= AUDIO_FORMAT_U8
; break;
2379 case 1: vs
->as
.fmt
= AUDIO_FORMAT_S8
; break;
2380 case 2: vs
->as
.fmt
= AUDIO_FORMAT_U16
; break;
2381 case 3: vs
->as
.fmt
= AUDIO_FORMAT_S16
; break;
2382 case 4: vs
->as
.fmt
= AUDIO_FORMAT_U32
; break;
2383 case 5: vs
->as
.fmt
= AUDIO_FORMAT_S32
; break;
2385 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2386 vnc_client_error(vs
);
2389 vs
->as
.nchannels
= read_u8(data
, 5);
2390 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2391 VNC_DEBUG("Invalid audio channel count %d\n",
2393 vnc_client_error(vs
);
2396 freq
= read_u32(data
, 6);
2397 /* No official limit for protocol, but 48khz is a sensible
2398 * upper bound for trustworthy clients, and this limit
2399 * protects calculations involving 'vs->as.freq' later.
2402 VNC_DEBUG("Invalid audio frequency %u > 48000", freq
);
2403 vnc_client_error(vs
);
2409 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 4));
2410 vnc_client_error(vs
);
2416 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2417 vnc_client_error(vs
);
2422 VNC_DEBUG("Msg: %d\n", data
[0]);
2423 vnc_client_error(vs
);
2427 vnc_update_throttle_offset(vs
);
2428 vnc_read_when(vs
, protocol_client_msg
, 1);
2432 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2438 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2439 switch (vs
->vd
->share_policy
) {
2440 case VNC_SHARE_POLICY_IGNORE
:
2442 * Ignore the shared flag. Nothing to do here.
2444 * Doesn't conform to the rfb spec but is traditional qemu
2445 * behavior, thus left here as option for compatibility
2449 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2451 * Policy: Allow clients ask for exclusive access.
2453 * Implementation: When a client asks for exclusive access,
2454 * disconnect all others. Shared connects are allowed as long
2455 * as no exclusive connection exists.
2457 * This is how the rfb spec suggests to handle the shared flag.
2459 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2461 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2465 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2466 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2469 vnc_disconnect_start(client
);
2472 if (mode
== VNC_SHARE_MODE_SHARED
) {
2473 if (vs
->vd
->num_exclusive
> 0) {
2474 vnc_disconnect_start(vs
);
2479 case VNC_SHARE_POLICY_FORCE_SHARED
:
2481 * Policy: Shared connects only.
2482 * Implementation: Disallow clients asking for exclusive access.
2484 * Useful for shared desktop sessions where you don't want
2485 * someone forgetting to say -shared when running the vnc
2486 * client disconnect everybody else.
2488 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2489 vnc_disconnect_start(vs
);
2494 vnc_set_share_mode(vs
, mode
);
2496 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2497 vnc_disconnect_start(vs
);
2501 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
2502 pixman_image_get_width(vs
->vd
->server
) >= 0);
2503 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
2504 pixman_image_get_height(vs
->vd
->server
) >= 0);
2505 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2506 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2507 vnc_write_u16(vs
, vs
->client_width
);
2508 vnc_write_u16(vs
, vs
->client_height
);
2510 pixel_format_message(vs
);
2513 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2514 if (size
> sizeof(buf
)) {
2518 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2521 vnc_write_u32(vs
, size
);
2522 vnc_write(vs
, buf
, size
);
2525 vnc_client_cache_auth(vs
);
2526 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2528 vnc_read_when(vs
, protocol_client_msg
, 1);
2533 void start_client_init(VncState
*vs
)
2535 vnc_read_when(vs
, protocol_client_init
, 1);
2538 static void make_challenge(VncState
*vs
)
2542 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2544 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2545 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2548 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2550 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2552 unsigned char key
[8];
2553 time_t now
= time(NULL
);
2554 QCryptoCipher
*cipher
= NULL
;
2557 if (!vs
->vd
->password
) {
2558 trace_vnc_auth_fail(vs
, vs
->auth
, "password is not set", "");
2561 if (vs
->vd
->expires
< now
) {
2562 trace_vnc_auth_fail(vs
, vs
->auth
, "password is expired", "");
2566 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2568 /* Calculate the expected challenge response */
2569 pwlen
= strlen(vs
->vd
->password
);
2570 for (i
=0; i
<sizeof(key
); i
++)
2571 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2573 cipher
= qcrypto_cipher_new(
2574 QCRYPTO_CIPHER_ALG_DES_RFB
,
2575 QCRYPTO_CIPHER_MODE_ECB
,
2576 key
, G_N_ELEMENTS(key
),
2579 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot create cipher",
2580 error_get_pretty(err
));
2585 if (qcrypto_cipher_encrypt(cipher
,
2588 VNC_AUTH_CHALLENGE_SIZE
,
2590 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot encrypt challenge response",
2591 error_get_pretty(err
));
2596 /* Compare expected vs actual challenge response */
2597 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2598 trace_vnc_auth_fail(vs
, vs
->auth
, "mis-matched challenge response", "");
2601 trace_vnc_auth_pass(vs
, vs
->auth
);
2602 vnc_write_u32(vs
, 0); /* Accept auth */
2605 start_client_init(vs
);
2608 qcrypto_cipher_free(cipher
);
2612 vnc_write_u32(vs
, 1); /* Reject auth */
2613 if (vs
->minor
>= 8) {
2614 static const char err
[] = "Authentication failed";
2615 vnc_write_u32(vs
, sizeof(err
));
2616 vnc_write(vs
, err
, sizeof(err
));
2619 vnc_client_error(vs
);
2620 qcrypto_cipher_free(cipher
);
2624 void start_auth_vnc(VncState
*vs
)
2627 /* Send client a 'random' challenge */
2628 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2631 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2635 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2637 /* We only advertise 1 auth scheme at a time, so client
2638 * must pick the one we sent. Verify this */
2639 if (data
[0] != vs
->auth
) { /* Reject auth */
2640 trace_vnc_auth_reject(vs
, vs
->auth
, (int)data
[0]);
2641 vnc_write_u32(vs
, 1);
2642 if (vs
->minor
>= 8) {
2643 static const char err
[] = "Authentication failed";
2644 vnc_write_u32(vs
, sizeof(err
));
2645 vnc_write(vs
, err
, sizeof(err
));
2647 vnc_client_error(vs
);
2648 } else { /* Accept requested auth */
2649 trace_vnc_auth_start(vs
, vs
->auth
);
2652 if (vs
->minor
>= 8) {
2653 vnc_write_u32(vs
, 0); /* Accept auth completion */
2656 trace_vnc_auth_pass(vs
, vs
->auth
);
2657 start_client_init(vs
);
2664 case VNC_AUTH_VENCRYPT
:
2665 start_auth_vencrypt(vs
);
2668 #ifdef CONFIG_VNC_SASL
2670 start_auth_sasl(vs
);
2672 #endif /* CONFIG_VNC_SASL */
2674 default: /* Should not be possible, but just in case */
2675 trace_vnc_auth_fail(vs
, vs
->auth
, "Unhandled auth method", "");
2676 vnc_write_u8(vs
, 1);
2677 if (vs
->minor
>= 8) {
2678 static const char err
[] = "Authentication failed";
2679 vnc_write_u32(vs
, sizeof(err
));
2680 vnc_write(vs
, err
, sizeof(err
));
2682 vnc_client_error(vs
);
2688 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2692 memcpy(local
, version
, 12);
2695 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2696 VNC_DEBUG("Malformed protocol version %s\n", local
);
2697 vnc_client_error(vs
);
2700 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2701 if (vs
->major
!= 3 ||
2707 VNC_DEBUG("Unsupported client version\n");
2708 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2710 vnc_client_error(vs
);
2713 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2714 * as equivalent to v3.3 by servers
2716 if (vs
->minor
== 4 || vs
->minor
== 5)
2719 if (vs
->minor
== 3) {
2720 trace_vnc_auth_start(vs
, vs
->auth
);
2721 if (vs
->auth
== VNC_AUTH_NONE
) {
2722 vnc_write_u32(vs
, vs
->auth
);
2724 trace_vnc_auth_pass(vs
, vs
->auth
);
2725 start_client_init(vs
);
2726 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2727 VNC_DEBUG("Tell client VNC auth\n");
2728 vnc_write_u32(vs
, vs
->auth
);
2732 trace_vnc_auth_fail(vs
, vs
->auth
,
2733 "Unsupported auth method for v3.3", "");
2734 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2736 vnc_client_error(vs
);
2739 vnc_write_u8(vs
, 1); /* num auth */
2740 vnc_write_u8(vs
, vs
->auth
);
2741 vnc_read_when(vs
, protocol_client_auth
, 1);
2748 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2750 struct VncSurface
*vs
= &vd
->guest
;
2752 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2755 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2759 w
= (x
+ w
) / VNC_STAT_RECT
;
2760 h
= (y
+ h
) / VNC_STAT_RECT
;
2764 for (j
= y
; j
<= h
; j
++) {
2765 for (i
= x
; i
<= w
; i
++) {
2766 vs
->lossy_rect
[j
][i
] = 1;
2771 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2774 int sty
= y
/ VNC_STAT_RECT
;
2775 int stx
= x
/ VNC_STAT_RECT
;
2778 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2779 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2781 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2784 /* kernel send buffers are full -> refresh later */
2785 if (vs
->output
.offset
) {
2789 if (!vs
->lossy_rect
[sty
][stx
]) {
2793 vs
->lossy_rect
[sty
][stx
] = 0;
2794 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2795 bitmap_set(vs
->dirty
[y
+ j
],
2796 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2797 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
2805 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2807 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2808 pixman_image_get_width(vd
->server
));
2809 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2810 pixman_image_get_height(vd
->server
));
2815 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2816 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2817 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2819 rect
->updated
= false;
2823 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2825 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2828 vd
->guest
.last_freq_check
= *tv
;
2830 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2831 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2832 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2833 int count
= ARRAY_SIZE(rect
->times
);
2834 struct timeval min
, max
;
2836 if (!timerisset(&rect
->times
[count
- 1])) {
2840 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2841 qemu_timersub(tv
, &max
, &res
);
2843 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2845 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2846 memset(rect
->times
, 0, sizeof (rect
->times
));
2850 min
= rect
->times
[rect
->idx
];
2851 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2852 qemu_timersub(&max
, &min
, &res
);
2854 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2855 rect
->freq
/= count
;
2856 rect
->freq
= 1. / rect
->freq
;
2862 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2868 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2869 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2871 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2872 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2873 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2885 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2889 rect
= vnc_stat_rect(vd
, x
, y
);
2890 if (rect
->updated
) {
2893 rect
->times
[rect
->idx
] = *tv
;
2894 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2895 rect
->updated
= true;
2898 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2900 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2901 pixman_image_get_width(vd
->server
));
2902 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2903 pixman_image_get_height(vd
->server
));
2904 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
2905 uint8_t *guest_row0
= NULL
, *server_row0
;
2908 pixman_image_t
*tmpbuf
= NULL
;
2910 struct timeval tv
= { 0, 0 };
2912 if (!vd
->non_adaptive
) {
2913 gettimeofday(&tv
, NULL
);
2914 has_dirty
= vnc_update_stats(vd
, &tv
);
2918 * Walk through the guest dirty map.
2919 * Check and copy modified bits from guest to server surface.
2920 * Update server dirty map.
2922 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
2923 server_stride
= guest_stride
= guest_ll
=
2924 pixman_image_get_stride(vd
->server
);
2925 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
2927 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2928 int width
= pixman_image_get_width(vd
->server
);
2929 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2932 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
2933 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2934 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
2935 guest_ll
= pixman_image_get_width(vd
->guest
.fb
)
2936 * DIV_ROUND_UP(guest_bpp
, 8);
2938 line_bytes
= MIN(server_stride
, guest_ll
);
2942 uint8_t *guest_ptr
, *server_ptr
;
2943 unsigned long offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
2944 height
* VNC_DIRTY_BPL(&vd
->guest
),
2945 y
* VNC_DIRTY_BPL(&vd
->guest
));
2946 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
2947 /* no more dirty bits */
2950 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
2951 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
2953 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
2955 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2956 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
2957 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2959 guest_ptr
= guest_row0
+ y
* guest_stride
;
2961 guest_ptr
+= x
* cmp_bytes
;
2963 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
2964 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2965 int _cmp_bytes
= cmp_bytes
;
2966 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
2969 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
2970 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
2972 assert(_cmp_bytes
>= 0);
2973 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
2976 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
2977 if (!vd
->non_adaptive
) {
2978 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
2981 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2982 set_bit(x
, vs
->dirty
[y
]);
2989 qemu_pixman_image_unref(tmpbuf
);
2993 static void vnc_refresh(DisplayChangeListener
*dcl
)
2995 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
2997 int has_dirty
, rects
= 0;
2999 if (QTAILQ_EMPTY(&vd
->clients
)) {
3000 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
3004 graphic_hw_update(vd
->dcl
.con
);
3006 if (vnc_trylock_display(vd
)) {
3007 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3011 has_dirty
= vnc_refresh_server_surface(vd
);
3012 vnc_unlock_display(vd
);
3014 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
3015 rects
+= vnc_update_client(vs
, has_dirty
);
3016 /* vs might be free()ed here */
3019 if (has_dirty
&& rects
) {
3020 vd
->dcl
.update_interval
/= 2;
3021 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
3022 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
3025 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
3026 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
3027 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
3032 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
3033 bool skipauth
, bool websocket
)
3035 VncState
*vs
= g_new0(VncState
, 1);
3036 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
3039 trace_vnc_client_connect(vs
, sioc
);
3040 vs
->magic
= VNC_MAGIC
;
3042 object_ref(OBJECT(vs
->sioc
));
3043 vs
->ioc
= QIO_CHANNEL(sioc
);
3044 object_ref(OBJECT(vs
->ioc
));
3047 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
3048 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
3049 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
3051 buffer_init(&vs
->tight
.tight
, "vnc-tight/%p", sioc
);
3052 buffer_init(&vs
->tight
.zlib
, "vnc-tight-zlib/%p", sioc
);
3053 buffer_init(&vs
->tight
.gradient
, "vnc-tight-gradient/%p", sioc
);
3054 #ifdef CONFIG_VNC_JPEG
3055 buffer_init(&vs
->tight
.jpeg
, "vnc-tight-jpeg/%p", sioc
);
3057 #ifdef CONFIG_VNC_PNG
3058 buffer_init(&vs
->tight
.png
, "vnc-tight-png/%p", sioc
);
3060 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3061 buffer_init(&vs
->zrle
.zrle
, "vnc-zrle/%p", sioc
);
3062 buffer_init(&vs
->zrle
.fb
, "vnc-zrle-fb/%p", sioc
);
3063 buffer_init(&vs
->zrle
.zlib
, "vnc-zrle-zlib/%p", sioc
);
3066 vs
->auth
= VNC_AUTH_NONE
;
3067 vs
->subauth
= VNC_AUTH_INVALID
;
3070 vs
->auth
= vd
->ws_auth
;
3071 vs
->subauth
= VNC_AUTH_INVALID
;
3073 vs
->auth
= vd
->auth
;
3074 vs
->subauth
= vd
->subauth
;
3077 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3078 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3080 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3081 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3082 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3085 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3086 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3087 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3089 g_source_remove(vs
->ioc_tag
);
3094 vs
->ioc_tag
= qio_channel_add_watch(
3095 vs
->ioc
, G_IO_IN
, vncws_tls_handshake_io
, vs
, NULL
);
3097 vs
->ioc_tag
= qio_channel_add_watch(
3098 vs
->ioc
, G_IO_IN
, vncws_handshake_io
, vs
, NULL
);
3101 vs
->ioc_tag
= qio_channel_add_watch(
3102 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
3105 vnc_client_cache_addr(vs
);
3106 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3107 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3112 vs
->as
.freq
= 44100;
3113 vs
->as
.nchannels
= 2;
3114 vs
->as
.fmt
= AUDIO_FORMAT_S16
;
3115 vs
->as
.endianness
= 0;
3117 qemu_mutex_init(&vs
->output_mutex
);
3118 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3120 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3122 vnc_update_server_surface(vd
);
3125 graphic_hw_update(vd
->dcl
.con
);
3127 if (!vs
->websocket
) {
3128 vnc_start_protocol(vs
);
3131 if (vd
->num_connecting
> vd
->connections_limit
) {
3132 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3133 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3134 vnc_disconnect_start(vs
);
3141 void vnc_start_protocol(VncState
*vs
)
3143 vnc_write(vs
, "RFB 003.008\n", 12);
3145 vnc_read_when(vs
, protocol_version
, 12);
3147 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3148 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3151 static void vnc_listen_io(QIONetListener
*listener
,
3152 QIOChannelSocket
*cioc
,
3155 VncDisplay
*vd
= opaque
;
3156 bool isWebsock
= listener
== vd
->wslistener
;
3158 qio_channel_set_name(QIO_CHANNEL(cioc
),
3159 isWebsock
? "vnc-ws-server" : "vnc-server");
3160 qio_channel_set_delay(QIO_CHANNEL(cioc
), false);
3161 vnc_connect(vd
, cioc
, false, isWebsock
);
3164 static const DisplayChangeListenerOps dcl_ops
= {
3166 .dpy_refresh
= vnc_refresh
,
3167 .dpy_gfx_update
= vnc_dpy_update
,
3168 .dpy_gfx_switch
= vnc_dpy_switch
,
3169 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3170 .dpy_mouse_set
= vnc_mouse_set
,
3171 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3174 void vnc_display_init(const char *id
, Error
**errp
)
3178 if (vnc_display_find(id
) != NULL
) {
3181 vd
= g_malloc0(sizeof(*vd
));
3183 vd
->id
= strdup(id
);
3184 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3186 QTAILQ_INIT(&vd
->clients
);
3187 vd
->expires
= TIME_MAX
;
3189 if (keyboard_layout
) {
3190 trace_vnc_key_map_init(keyboard_layout
);
3191 vd
->kbd_layout
= init_keyboard_layout(name2keysym
,
3192 keyboard_layout
, errp
);
3194 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us", errp
);
3197 if (!vd
->kbd_layout
) {
3201 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3202 vd
->connections_limit
= 32;
3204 qemu_mutex_init(&vd
->mutex
);
3205 vnc_start_worker_thread();
3207 vd
->dcl
.ops
= &dcl_ops
;
3208 register_displaychangelistener(&vd
->dcl
);
3209 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
3213 static void vnc_display_close(VncDisplay
*vd
)
3218 vd
->is_unix
= false;
3221 qio_net_listener_disconnect(vd
->listener
);
3222 object_unref(OBJECT(vd
->listener
));
3224 vd
->listener
= NULL
;
3226 if (vd
->wslistener
) {
3227 qio_net_listener_disconnect(vd
->wslistener
);
3228 object_unref(OBJECT(vd
->wslistener
));
3230 vd
->wslistener
= NULL
;
3232 vd
->auth
= VNC_AUTH_INVALID
;
3233 vd
->subauth
= VNC_AUTH_INVALID
;
3235 object_unparent(OBJECT(vd
->tlscreds
));
3236 vd
->tlscreds
= NULL
;
3239 object_unparent(OBJECT(vd
->tlsauthz
));
3240 vd
->tlsauthz
= NULL
;
3242 g_free(vd
->tlsauthzid
);
3243 vd
->tlsauthzid
= NULL
;
3244 if (vd
->lock_key_sync
) {
3245 qemu_remove_led_event_handler(vd
->led
);
3248 #ifdef CONFIG_VNC_SASL
3249 if (vd
->sasl
.authz
) {
3250 object_unparent(OBJECT(vd
->sasl
.authz
));
3251 vd
->sasl
.authz
= NULL
;
3253 g_free(vd
->sasl
.authzid
);
3254 vd
->sasl
.authzid
= NULL
;
3258 int vnc_display_password(const char *id
, const char *password
)
3260 VncDisplay
*vd
= vnc_display_find(id
);
3265 if (vd
->auth
== VNC_AUTH_NONE
) {
3266 error_printf_unless_qmp("If you want use passwords please enable "
3267 "password auth using '-vnc ${dpy},password'.\n");
3271 g_free(vd
->password
);
3272 vd
->password
= g_strdup(password
);
3277 int vnc_display_pw_expire(const char *id
, time_t expires
)
3279 VncDisplay
*vd
= vnc_display_find(id
);
3285 vd
->expires
= expires
;
3289 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3291 SocketAddress
*addr
;
3294 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
3298 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0], &err
);
3303 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
3304 qapi_free_SocketAddress(addr
);
3307 error_printf_unless_qmp("VNC server running on %s:%s\n",
3310 qapi_free_SocketAddress(addr
);
3313 static QemuOptsList qemu_vnc_opts
= {
3315 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3316 .implied_opt_name
= "vnc",
3320 .type
= QEMU_OPT_STRING
,
3322 .name
= "websocket",
3323 .type
= QEMU_OPT_STRING
,
3325 .name
= "tls-creds",
3326 .type
= QEMU_OPT_STRING
,
3329 .type
= QEMU_OPT_STRING
,
3332 .type
= QEMU_OPT_STRING
,
3335 .type
= QEMU_OPT_NUMBER
,
3337 .name
= "connections",
3338 .type
= QEMU_OPT_NUMBER
,
3341 .type
= QEMU_OPT_NUMBER
,
3344 .type
= QEMU_OPT_BOOL
,
3347 .type
= QEMU_OPT_BOOL
,
3350 .type
= QEMU_OPT_BOOL
,
3353 .type
= QEMU_OPT_BOOL
,
3355 .name
= "lock-key-sync",
3356 .type
= QEMU_OPT_BOOL
,
3358 .name
= "key-delay-ms",
3359 .type
= QEMU_OPT_NUMBER
,
3362 .type
= QEMU_OPT_BOOL
,
3365 .type
= QEMU_OPT_BOOL
,
3367 .name
= "tls-authz",
3368 .type
= QEMU_OPT_STRING
,
3370 .name
= "sasl-authz",
3371 .type
= QEMU_OPT_STRING
,
3374 .type
= QEMU_OPT_BOOL
,
3376 .name
= "non-adaptive",
3377 .type
= QEMU_OPT_BOOL
,
3379 { /* end of list */ }
3385 vnc_display_setup_auth(int *auth
,
3387 QCryptoTLSCreds
*tlscreds
,
3394 * We have a choice of 3 authentication options
3400 * The channel can be run in 2 modes
3405 * And TLS can use 2 types of credentials
3410 * We thus have 9 possible logical combinations
3415 * 4. tls + anon + none
3416 * 5. tls + anon + vnc
3417 * 6. tls + anon + sasl
3418 * 7. tls + x509 + none
3419 * 8. tls + x509 + vnc
3420 * 9. tls + x509 + sasl
3422 * These need to be mapped into the VNC auth schemes
3423 * in an appropriate manner. In regular VNC, all the
3424 * TLS options get mapped into VNC_AUTH_VENCRYPT
3427 * In websockets, the https:// protocol already provides
3428 * TLS support, so there is no need to make use of the
3429 * VeNCrypt extension. Furthermore, websockets browser
3430 * clients could not use VeNCrypt even if they wanted to,
3431 * as they cannot control when the TLS handshake takes
3432 * place. Thus there is no option but to rely on https://,
3433 * meaning combinations 4->6 and 7->9 will be mapped to
3434 * VNC auth schemes in the same way as combos 1->3.
3436 * Regardless of fact that we have a different mapping to
3437 * VNC auth mechs for plain VNC vs websockets VNC, the end
3438 * result has the same security characteristics.
3440 if (websocket
|| !tlscreds
) {
3442 VNC_DEBUG("Initializing VNC server with password auth\n");
3443 *auth
= VNC_AUTH_VNC
;
3445 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3446 *auth
= VNC_AUTH_SASL
;
3448 VNC_DEBUG("Initializing VNC server with no auth\n");
3449 *auth
= VNC_AUTH_NONE
;
3451 *subauth
= VNC_AUTH_INVALID
;
3453 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3454 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3455 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3456 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3458 if (!is_x509
&& !is_anon
) {
3460 "Unsupported TLS cred type %s",
3461 object_get_typename(OBJECT(tlscreds
)));
3464 *auth
= VNC_AUTH_VENCRYPT
;
3467 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3468 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3470 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3471 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3476 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3477 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3479 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3480 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3484 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3485 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3487 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3488 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3496 static int vnc_display_get_address(const char *addrstr
,
3505 SocketAddress
**retaddr
,
3509 SocketAddress
*addr
= NULL
;
3511 addr
= g_new0(SocketAddress
, 1);
3513 if (strncmp(addrstr
, "unix:", 5) == 0) {
3514 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
3515 addr
->u
.q_unix
.path
= g_strdup(addrstr
+ 5);
3518 error_setg(errp
, "UNIX sockets not supported with websock");
3523 error_setg(errp
, "Port range not support with UNIX socket");
3530 unsigned long long baseport
= 0;
3531 InetSocketAddress
*inet
;
3533 port
= strrchr(addrstr
, ':');
3539 error_setg(errp
, "no vnc port specified");
3543 hostlen
= port
- addrstr
;
3545 if (*port
== '\0') {
3546 error_setg(errp
, "vnc port cannot be empty");
3551 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
3552 inet
= &addr
->u
.inet
;
3553 if (addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3554 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3556 inet
->host
= g_strndup(addrstr
, hostlen
);
3558 /* plain VNC port is just an offset, for websocket
3559 * port is absolute */
3561 if (g_str_equal(addrstr
, "") ||
3562 g_str_equal(addrstr
, "on")) {
3563 if (displaynum
== -1) {
3564 error_setg(errp
, "explicit websocket port is required");
3567 inet
->port
= g_strdup_printf(
3568 "%d", displaynum
+ 5700);
3570 inet
->has_to
= true;
3571 inet
->to
= to
+ 5700;
3574 inet
->port
= g_strdup(port
);
3577 int offset
= reverse
? 0 : 5900;
3578 if (parse_uint_full(port
, &baseport
, 10) < 0) {
3579 error_setg(errp
, "can't convert to a number: %s", port
);
3582 if (baseport
> 65535 ||
3583 baseport
+ offset
> 65535) {
3584 error_setg(errp
, "port %s out of range", port
);
3587 inet
->port
= g_strdup_printf(
3588 "%d", (int)baseport
+ offset
);
3591 inet
->has_to
= true;
3592 inet
->to
= to
+ offset
;
3597 inet
->has_ipv4
= has_ipv4
;
3599 inet
->has_ipv6
= has_ipv6
;
3608 qapi_free_SocketAddress(addr
);
3613 static void vnc_free_addresses(SocketAddress
***retsaddr
,
3618 for (i
= 0; i
< *retnsaddr
; i
++) {
3619 qapi_free_SocketAddress((*retsaddr
)[i
]);
3627 static int vnc_display_get_addresses(QemuOpts
*opts
,
3629 SocketAddress
***retsaddr
,
3631 SocketAddress
***retwsaddr
,
3635 SocketAddress
*saddr
= NULL
;
3636 SocketAddress
*wsaddr
= NULL
;
3637 QemuOptsIter addriter
;
3639 int to
= qemu_opt_get_number(opts
, "to", 0);
3640 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3641 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3642 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3643 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3644 int displaynum
= -1;
3652 addr
= qemu_opt_get(opts
, "vnc");
3653 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3657 if (qemu_opt_get(opts
, "websocket") &&
3658 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3660 "SHA1 hash support is required for websockets");
3664 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3665 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3667 rv
= vnc_display_get_address(addr
, false, reverse
, 0, to
,
3674 /* Historical compat - first listen address can be used
3675 * to set the default websocket port
3677 if (displaynum
== -1) {
3680 *retsaddr
= g_renew(SocketAddress
*, *retsaddr
, *retnsaddr
+ 1);
3681 (*retsaddr
)[(*retnsaddr
)++] = saddr
;
3684 /* If we had multiple primary displays, we don't do defaults
3685 * for websocket, and require explicit config instead. */
3686 if (*retnsaddr
> 1) {
3690 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3691 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3692 if (vnc_display_get_address(addr
, true, reverse
, displaynum
, to
,
3695 &wsaddr
, errp
) < 0) {
3699 /* Historical compat - if only a single listen address was
3700 * provided, then this is used to set the default listen
3701 * address for websocket too
3703 if (*retnsaddr
== 1 &&
3704 (*retsaddr
)[0]->type
== SOCKET_ADDRESS_TYPE_INET
&&
3705 wsaddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3706 g_str_equal(wsaddr
->u
.inet
.host
, "") &&
3707 !g_str_equal((*retsaddr
)[0]->u
.inet
.host
, "")) {
3708 g_free(wsaddr
->u
.inet
.host
);
3709 wsaddr
->u
.inet
.host
= g_strdup((*retsaddr
)[0]->u
.inet
.host
);
3712 *retwsaddr
= g_renew(SocketAddress
*, *retwsaddr
, *retnwsaddr
+ 1);
3713 (*retwsaddr
)[(*retnwsaddr
)++] = wsaddr
;
3719 vnc_free_addresses(retsaddr
, retnsaddr
);
3720 vnc_free_addresses(retwsaddr
, retnwsaddr
);
3725 static int vnc_display_connect(VncDisplay
*vd
,
3726 SocketAddress
**saddr
,
3728 SocketAddress
**wsaddr
,
3732 /* connect to viewer */
3733 QIOChannelSocket
*sioc
= NULL
;
3735 error_setg(errp
, "Cannot use websockets in reverse mode");
3739 error_setg(errp
, "Expected a single address in reverse mode");
3742 /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3743 vd
->is_unix
= saddr
[0]->type
== SOCKET_ADDRESS_TYPE_UNIX
;
3744 sioc
= qio_channel_socket_new();
3745 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3746 if (qio_channel_socket_connect_sync(sioc
, saddr
[0], errp
) < 0) {
3749 vnc_connect(vd
, sioc
, false, false);
3750 object_unref(OBJECT(sioc
));
3755 static int vnc_display_listen(VncDisplay
*vd
,
3756 SocketAddress
**saddr
,
3758 SocketAddress
**wsaddr
,
3765 vd
->listener
= qio_net_listener_new();
3766 qio_net_listener_set_name(vd
->listener
, "vnc-listen");
3767 for (i
= 0; i
< nsaddr
; i
++) {
3768 if (qio_net_listener_open_sync(vd
->listener
,
3775 qio_net_listener_set_client_func(vd
->listener
,
3776 vnc_listen_io
, vd
, NULL
);
3780 vd
->wslistener
= qio_net_listener_new();
3781 qio_net_listener_set_name(vd
->wslistener
, "vnc-ws-listen");
3782 for (i
= 0; i
< nwsaddr
; i
++) {
3783 if (qio_net_listener_open_sync(vd
->wslistener
,
3790 qio_net_listener_set_client_func(vd
->wslistener
,
3791 vnc_listen_io
, vd
, NULL
);
3798 void vnc_display_open(const char *id
, Error
**errp
)
3800 VncDisplay
*vd
= vnc_display_find(id
);
3801 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
3802 SocketAddress
**saddr
= NULL
, **wsaddr
= NULL
;
3803 size_t nsaddr
, nwsaddr
;
3804 const char *share
, *device_id
;
3806 bool password
= false;
3807 bool reverse
= false;
3811 const char *tlsauthz
;
3812 const char *saslauthz
;
3813 int lock_key_sync
= 1;
3817 error_setg(errp
, "VNC display not active");
3820 vnc_display_close(vd
);
3826 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
3827 if (vnc_display_get_addresses(opts
, reverse
, &saddr
, &nsaddr
,
3828 &wsaddr
, &nwsaddr
, errp
) < 0) {
3832 password
= qemu_opt_get_bool(opts
, "password", false);
3834 if (fips_get_state()) {
3836 "VNC password auth disabled due to FIPS mode, "
3837 "consider using the VeNCrypt or SASL authentication "
3838 "methods as an alternative");
3841 if (!qcrypto_cipher_supports(
3842 QCRYPTO_CIPHER_ALG_DES_RFB
, QCRYPTO_CIPHER_MODE_ECB
)) {
3844 "Cipher backend does not support DES RFB algorithm");
3849 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
3850 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 10);
3851 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
3852 #ifndef CONFIG_VNC_SASL
3854 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
3857 #endif /* CONFIG_VNC_SASL */
3858 credid
= qemu_opt_get(opts
, "tls-creds");
3861 creds
= object_resolve_path_component(
3862 object_get_objects_root(), credid
);
3864 error_setg(errp
, "No TLS credentials with id '%s'",
3868 vd
->tlscreds
= (QCryptoTLSCreds
*)
3869 object_dynamic_cast(creds
,
3870 TYPE_QCRYPTO_TLS_CREDS
);
3871 if (!vd
->tlscreds
) {
3872 error_setg(errp
, "Object with id '%s' is not TLS credentials",
3876 object_ref(OBJECT(vd
->tlscreds
));
3878 if (vd
->tlscreds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
3880 "Expecting TLS credentials with a server endpoint");
3884 if (qemu_opt_get(opts
, "acl")) {
3885 error_report("The 'acl' option to -vnc is deprecated. "
3886 "Please use the 'tls-authz' and 'sasl-authz' "
3889 acl
= qemu_opt_get_bool(opts
, "acl", false);
3890 tlsauthz
= qemu_opt_get(opts
, "tls-authz");
3891 if (acl
&& tlsauthz
) {
3892 error_setg(errp
, "'acl' option is mutually exclusive with the "
3893 "'tls-authz' option");
3896 if (tlsauthz
&& !vd
->tlscreds
) {
3897 error_setg(errp
, "'tls-authz' provided but TLS is not enabled");
3901 saslauthz
= qemu_opt_get(opts
, "sasl-authz");
3902 if (acl
&& saslauthz
) {
3903 error_setg(errp
, "'acl' option is mutually exclusive with the "
3904 "'sasl-authz' option");
3907 if (saslauthz
&& !sasl
) {
3908 error_setg(errp
, "'sasl-authz' provided but SASL auth is not enabled");
3912 share
= qemu_opt_get(opts
, "share");
3914 if (strcmp(share
, "ignore") == 0) {
3915 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
3916 } else if (strcmp(share
, "allow-exclusive") == 0) {
3917 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3918 } else if (strcmp(share
, "force-shared") == 0) {
3919 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
3921 error_setg(errp
, "unknown vnc share= option");
3925 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3927 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
3929 #ifdef CONFIG_VNC_JPEG
3930 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
3932 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
3933 /* adaptive updates are only used with tight encoding and
3934 * if lossy updates are enabled so we can disable all the
3935 * calculations otherwise */
3937 vd
->non_adaptive
= true;
3941 vd
->tlsauthzid
= g_strdup(tlsauthz
);
3943 if (strcmp(vd
->id
, "default") == 0) {
3944 vd
->tlsauthzid
= g_strdup("vnc.x509dname");
3946 vd
->tlsauthzid
= g_strdup_printf("vnc.%s.x509dname", vd
->id
);
3948 vd
->tlsauthz
= QAUTHZ(qauthz_list_new(vd
->tlsauthzid
,
3949 QAUTHZ_LIST_POLICY_DENY
,
3952 #ifdef CONFIG_VNC_SASL
3955 vd
->sasl
.authzid
= g_strdup(saslauthz
);
3957 if (strcmp(vd
->id
, "default") == 0) {
3958 vd
->sasl
.authzid
= g_strdup("vnc.username");
3960 vd
->sasl
.authzid
= g_strdup_printf("vnc.%s.username", vd
->id
);
3962 vd
->sasl
.authz
= QAUTHZ(qauthz_list_new(vd
->sasl
.authzid
,
3963 QAUTHZ_LIST_POLICY_DENY
,
3969 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
3970 vd
->tlscreds
, password
,
3971 sasl
, false, errp
) < 0) {
3974 trace_vnc_auth_init(vd
, 0, vd
->auth
, vd
->subauth
);
3976 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
3977 vd
->tlscreds
, password
,
3978 sasl
, true, errp
) < 0) {
3981 trace_vnc_auth_init(vd
, 1, vd
->ws_auth
, vd
->ws_subauth
);
3983 #ifdef CONFIG_VNC_SASL
3985 int saslErr
= sasl_server_init(NULL
, "qemu");
3987 if (saslErr
!= SASL_OK
) {
3988 error_setg(errp
, "Failed to initialize SASL auth: %s",
3989 sasl_errstring(saslErr
, NULL
, NULL
));
3994 vd
->lock_key_sync
= lock_key_sync
;
3995 if (lock_key_sync
) {
3996 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
4000 device_id
= qemu_opt_get(opts
, "display");
4002 int head
= qemu_opt_get_number(opts
, "head", 0);
4005 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
4007 error_propagate(errp
, err
);
4014 if (con
!= vd
->dcl
.con
) {
4015 qkbd_state_free(vd
->kbd
);
4016 unregister_displaychangelistener(&vd
->dcl
);
4018 register_displaychangelistener(&vd
->dcl
);
4019 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
4021 qkbd_state_set_delay(vd
->kbd
, key_delay_ms
);
4023 if (saddr
== NULL
) {
4028 if (vnc_display_connect(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4032 if (vnc_display_listen(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4037 if (qemu_opt_get(opts
, "to")) {
4038 vnc_display_print_local_addr(vd
);
4042 vnc_free_addresses(&saddr
, &nsaddr
);
4043 vnc_free_addresses(&wsaddr
, &nwsaddr
);
4047 vnc_display_close(vd
);
4051 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
4053 VncDisplay
*vd
= vnc_display_find(id
);
4054 QIOChannelSocket
*sioc
;
4060 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4062 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4063 vnc_connect(vd
, sioc
, skipauth
, false);
4064 object_unref(OBJECT(sioc
));
4068 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4073 id
= g_strdup("default");
4074 while (qemu_opts_find(olist
, id
)) {
4076 id
= g_strdup_printf("vnc%d", i
++);
4078 qemu_opts_set_id(opts
, id
);
4081 QemuOpts
*vnc_parse(const char *str
, Error
**errp
)
4083 QemuOptsList
*olist
= qemu_find_opts("vnc");
4084 QemuOpts
*opts
= qemu_opts_parse(olist
, str
, true, errp
);
4091 id
= qemu_opts_id(opts
);
4093 /* auto-assign id if not present */
4094 vnc_auto_assign_id(olist
, opts
);
4099 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4101 Error
*local_err
= NULL
;
4102 char *id
= (char *)qemu_opts_id(opts
);
4105 vnc_display_init(id
, &local_err
);
4107 error_propagate(errp
, local_err
);
4110 vnc_display_open(id
, &local_err
);
4111 if (local_err
!= NULL
) {
4112 error_propagate(errp
, local_err
);
4118 static void vnc_register_config(void)
4120 qemu_add_opts(&qemu_vnc_opts
);
4122 opts_init(vnc_register_config
);