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 "hw/qdev-core.h"
32 #include "sysemu/sysemu.h"
33 #include "sysemu/runstate.h"
34 #include "qemu/error-report.h"
35 #include "qemu/main-loop.h"
36 #include "qemu/module.h"
37 #include "qemu/option.h"
38 #include "qemu/sockets.h"
39 #include "qemu/timer.h"
40 #include "authz/list.h"
41 #include "qemu/config-file.h"
42 #include "qapi/qapi-emit-events.h"
43 #include "qapi/qapi-events-ui.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-ui.h"
47 #include "crypto/hash.h"
48 #include "crypto/tlscreds.h"
49 #include "crypto/tlscredsanon.h"
50 #include "crypto/tlscredsx509.h"
51 #include "crypto/random.h"
52 #include "crypto/secret_common.h"
53 #include "qom/object_interfaces.h"
54 #include "qemu/cutils.h"
55 #include "qemu/help_option.h"
56 #include "io/dns-resolver.h"
57 #include "monitor/monitor.h"
59 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
60 #define VNC_REFRESH_INTERVAL_INC 50
61 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
62 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
63 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
65 #include "vnc_keysym.h"
66 #include "crypto/cipher.h"
68 static QTAILQ_HEAD(, VncDisplay
) vnc_displays
=
69 QTAILQ_HEAD_INITIALIZER(vnc_displays
);
71 static int vnc_cursor_define(VncState
*vs
);
72 static void vnc_update_throttle_offset(VncState
*vs
);
74 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
77 static const char *mn
[] = {
79 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
80 [VNC_SHARE_MODE_SHARED
] = "shared",
81 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
82 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
84 fprintf(stderr
, "%s/%p: %s -> %s\n", __func__
,
85 vs
->ioc
, mn
[vs
->share_mode
], mn
[mode
]);
88 switch (vs
->share_mode
) {
89 case VNC_SHARE_MODE_CONNECTING
:
90 vs
->vd
->num_connecting
--;
92 case VNC_SHARE_MODE_SHARED
:
95 case VNC_SHARE_MODE_EXCLUSIVE
:
96 vs
->vd
->num_exclusive
--;
102 vs
->share_mode
= mode
;
104 switch (vs
->share_mode
) {
105 case VNC_SHARE_MODE_CONNECTING
:
106 vs
->vd
->num_connecting
++;
108 case VNC_SHARE_MODE_SHARED
:
109 vs
->vd
->num_shared
++;
111 case VNC_SHARE_MODE_EXCLUSIVE
:
112 vs
->vd
->num_exclusive
++;
120 static void vnc_init_basic_info(SocketAddress
*addr
,
124 switch (addr
->type
) {
125 case SOCKET_ADDRESS_TYPE_INET
:
126 info
->host
= g_strdup(addr
->u
.inet
.host
);
127 info
->service
= g_strdup(addr
->u
.inet
.port
);
128 if (addr
->u
.inet
.ipv6
) {
129 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
131 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
135 case SOCKET_ADDRESS_TYPE_UNIX
:
136 info
->host
= g_strdup("");
137 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
138 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
141 case SOCKET_ADDRESS_TYPE_VSOCK
:
142 case SOCKET_ADDRESS_TYPE_FD
:
143 error_setg(errp
, "Unsupported socket address type %s",
144 SocketAddressType_str(addr
->type
));
153 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
157 SocketAddress
*addr
= NULL
;
160 error_setg(errp
, "No listener socket available");
164 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
169 vnc_init_basic_info(addr
, info
, errp
);
170 qapi_free_SocketAddress(addr
);
173 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
177 SocketAddress
*addr
= NULL
;
179 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
184 vnc_init_basic_info(addr
, info
, errp
);
185 qapi_free_SocketAddress(addr
);
188 static const char *vnc_auth_name(VncDisplay
*vd
) {
190 case VNC_AUTH_INVALID
:
206 case VNC_AUTH_VENCRYPT
:
207 switch (vd
->subauth
) {
208 case VNC_AUTH_VENCRYPT_PLAIN
:
209 return "vencrypt+plain";
210 case VNC_AUTH_VENCRYPT_TLSNONE
:
211 return "vencrypt+tls+none";
212 case VNC_AUTH_VENCRYPT_TLSVNC
:
213 return "vencrypt+tls+vnc";
214 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
215 return "vencrypt+tls+plain";
216 case VNC_AUTH_VENCRYPT_X509NONE
:
217 return "vencrypt+x509+none";
218 case VNC_AUTH_VENCRYPT_X509VNC
:
219 return "vencrypt+x509+vnc";
220 case VNC_AUTH_VENCRYPT_X509PLAIN
:
221 return "vencrypt+x509+plain";
222 case VNC_AUTH_VENCRYPT_TLSSASL
:
223 return "vencrypt+tls+sasl";
224 case VNC_AUTH_VENCRYPT_X509SASL
:
225 return "vencrypt+x509+sasl";
235 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
240 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
244 info
= g_malloc0(sizeof(*info
));
245 vnc_init_basic_info_from_server_addr(vd
->listener
->sioc
[0],
246 qapi_VncServerInfo_base(info
), &err
);
247 info
->auth
= g_strdup(vnc_auth_name(vd
));
249 qapi_free_VncServerInfo(info
);
256 static void vnc_client_cache_auth(VncState
*client
)
263 client
->info
->x509_dname
=
264 qcrypto_tls_session_get_peer_name(client
->tls
);
266 #ifdef CONFIG_VNC_SASL
267 if (client
->sasl
.conn
&&
268 client
->sasl
.username
) {
269 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
274 static void vnc_client_cache_addr(VncState
*client
)
278 client
->info
= g_malloc0(sizeof(*client
->info
));
279 vnc_init_basic_info_from_remote_addr(client
->sioc
,
280 qapi_VncClientInfo_base(client
->info
),
282 client
->info
->websocket
= client
->websocket
;
284 qapi_free_VncClientInfo(client
->info
);
290 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
298 si
= vnc_server_info_get(vs
->vd
);
304 case QAPI_EVENT_VNC_CONNECTED
:
305 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
));
307 case QAPI_EVENT_VNC_INITIALIZED
:
308 qapi_event_send_vnc_initialized(si
, vs
->info
);
310 case QAPI_EVENT_VNC_DISCONNECTED
:
311 qapi_event_send_vnc_disconnected(si
, vs
->info
);
317 qapi_free_VncServerInfo(si
);
320 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
325 info
= g_malloc0(sizeof(*info
));
327 vnc_init_basic_info_from_remote_addr(client
->sioc
,
328 qapi_VncClientInfo_base(info
),
332 qapi_free_VncClientInfo(info
);
336 info
->websocket
= client
->websocket
;
339 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
341 #ifdef CONFIG_VNC_SASL
342 if (client
->sasl
.conn
&& client
->sasl
.username
) {
343 info
->sasl_username
= g_strdup(client
->sasl
.username
);
350 static VncDisplay
*vnc_display_find(const char *id
)
355 return QTAILQ_FIRST(&vnc_displays
);
357 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
358 if (strcmp(id
, vd
->id
) == 0) {
365 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
367 VncClientInfoList
*prev
= NULL
;
370 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
371 QAPI_LIST_PREPEND(prev
, qmp_query_vnc_client(client
));
376 VncInfo
*qmp_query_vnc(Error
**errp
)
378 VncInfo
*info
= g_malloc0(sizeof(*info
));
379 VncDisplay
*vd
= vnc_display_find(NULL
);
380 SocketAddress
*addr
= NULL
;
382 if (vd
== NULL
|| !vd
->listener
|| !vd
->listener
->nsioc
) {
383 info
->enabled
= false;
385 info
->enabled
= true;
387 /* for compatibility with the original command */
388 info
->has_clients
= true;
389 info
->clients
= qmp_query_client_list(vd
);
391 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0],
397 switch (addr
->type
) {
398 case SOCKET_ADDRESS_TYPE_INET
:
399 info
->host
= g_strdup(addr
->u
.inet
.host
);
400 info
->service
= g_strdup(addr
->u
.inet
.port
);
401 if (addr
->u
.inet
.ipv6
) {
402 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
404 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
408 case SOCKET_ADDRESS_TYPE_UNIX
:
409 info
->host
= g_strdup("");
410 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
411 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
414 case SOCKET_ADDRESS_TYPE_VSOCK
:
415 case SOCKET_ADDRESS_TYPE_FD
:
416 error_setg(errp
, "Unsupported socket address type %s",
417 SocketAddressType_str(addr
->type
));
423 info
->has_family
= true;
425 info
->auth
= g_strdup(vnc_auth_name(vd
));
428 qapi_free_SocketAddress(addr
);
432 qapi_free_SocketAddress(addr
);
433 qapi_free_VncInfo(info
);
438 static void qmp_query_auth(int auth
, int subauth
,
439 VncPrimaryAuth
*qmp_auth
,
440 VncVencryptSubAuth
*qmp_vencrypt
,
441 bool *qmp_has_vencrypt
);
443 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
447 VncServerInfo2List
*prev
)
449 VncServerInfo2
*info
;
453 addr
= qio_channel_socket_get_local_address(ioc
, NULL
);
458 info
= g_new0(VncServerInfo2
, 1);
459 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
460 qapi_free_SocketAddress(addr
);
462 qapi_free_VncServerInfo2(info
);
466 info
->websocket
= websocket
;
468 qmp_query_auth(auth
, subauth
, &info
->auth
,
469 &info
->vencrypt
, &info
->has_vencrypt
);
471 QAPI_LIST_PREPEND(prev
, info
);
475 static void qmp_query_auth(int auth
, int subauth
,
476 VncPrimaryAuth
*qmp_auth
,
477 VncVencryptSubAuth
*qmp_vencrypt
,
478 bool *qmp_has_vencrypt
)
482 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
485 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
488 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
491 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
494 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
497 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
499 case VNC_AUTH_VENCRYPT
:
500 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
501 *qmp_has_vencrypt
= true;
503 case VNC_AUTH_VENCRYPT_PLAIN
:
504 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
506 case VNC_AUTH_VENCRYPT_TLSNONE
:
507 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
509 case VNC_AUTH_VENCRYPT_TLSVNC
:
510 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
512 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
513 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
515 case VNC_AUTH_VENCRYPT_X509NONE
:
516 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
518 case VNC_AUTH_VENCRYPT_X509VNC
:
519 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
521 case VNC_AUTH_VENCRYPT_X509PLAIN
:
522 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
524 case VNC_AUTH_VENCRYPT_TLSSASL
:
525 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
527 case VNC_AUTH_VENCRYPT_X509SASL
:
528 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
531 *qmp_has_vencrypt
= false;
536 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
540 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
545 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
547 VncInfo2List
*prev
= NULL
;
553 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
554 info
= g_new0(VncInfo2
, 1);
555 info
->id
= g_strdup(vd
->id
);
556 info
->clients
= qmp_query_client_list(vd
);
557 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
558 &info
->vencrypt
, &info
->has_vencrypt
);
560 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
561 "device", &error_abort
));
562 info
->display
= g_strdup(dev
->id
);
564 for (i
= 0; vd
->listener
!= NULL
&& i
< vd
->listener
->nsioc
; i
++) {
565 info
->server
= qmp_query_server_entry(
566 vd
->listener
->sioc
[i
], false, vd
->auth
, vd
->subauth
,
569 for (i
= 0; vd
->wslistener
!= NULL
&& i
< vd
->wslistener
->nsioc
; i
++) {
570 info
->server
= qmp_query_server_entry(
571 vd
->wslistener
->sioc
[i
], true, vd
->ws_auth
,
572 vd
->ws_subauth
, info
->server
);
575 QAPI_LIST_PREPEND(prev
, info
);
580 bool vnc_display_reload_certs(const char *id
, Error
**errp
)
582 VncDisplay
*vd
= vnc_display_find(id
);
583 QCryptoTLSCredsClass
*creds
= NULL
;
586 error_setg(errp
, "Can not find vnc display");
591 error_setg(errp
, "vnc tls is not enabled");
595 creds
= QCRYPTO_TLS_CREDS_GET_CLASS(OBJECT(vd
->tlscreds
));
596 if (creds
->reload
== NULL
) {
597 error_setg(errp
, "%s doesn't support to reload TLS credential",
598 object_get_typename(OBJECT(vd
->tlscreds
)));
601 if (!creds
->reload(vd
->tlscreds
, errp
)) {
609 1) Get the queue working for IO.
610 2) there is some weirdness when using the -S option (the screen is grey
611 and not totally invalidated
612 3) resolutions > 1024
615 static int vnc_update_client(VncState
*vs
, int has_dirty
);
616 static void vnc_disconnect_start(VncState
*vs
);
618 static void vnc_colordepth(VncState
*vs
);
619 static void framebuffer_update_request(VncState
*vs
, int incremental
,
620 int x_position
, int y_position
,
622 static void vnc_refresh(DisplayChangeListener
*dcl
);
623 static int vnc_refresh_server_surface(VncDisplay
*vd
);
625 static int vnc_width(VncDisplay
*vd
)
627 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
628 VNC_DIRTY_PIXELS_PER_BIT
));
631 static int vnc_true_width(VncDisplay
*vd
)
633 return MIN(VNC_MAX_WIDTH
, surface_width(vd
->ds
));
636 static int vnc_height(VncDisplay
*vd
)
638 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
641 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
642 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
644 int x
, int y
, int w
, int h
)
646 int width
= vnc_width(vd
);
647 int height
= vnc_height(vd
);
649 /* this is needed this to ensure we updated all affected
650 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
651 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
652 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
656 w
= MIN(x
+ w
, width
) - x
;
657 h
= MIN(y
+ h
, height
);
660 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
661 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
665 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
666 int x
, int y
, int w
, int h
)
668 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
669 struct VncSurface
*s
= &vd
->guest
;
671 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
674 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
677 vnc_write_u16(vs
, x
);
678 vnc_write_u16(vs
, y
);
679 vnc_write_u16(vs
, w
);
680 vnc_write_u16(vs
, h
);
682 vnc_write_s32(vs
, encoding
);
685 static void vnc_desktop_resize_ext(VncState
*vs
, int reject_reason
)
687 trace_vnc_msg_server_ext_desktop_resize(
688 vs
, vs
->ioc
, vs
->client_width
, vs
->client_height
, reject_reason
);
691 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
693 vnc_write_u16(vs
, 1); /* number of rects */
694 vnc_framebuffer_update(vs
,
695 reject_reason
? 1 : 0,
697 vs
->client_width
, vs
->client_height
,
698 VNC_ENCODING_DESKTOP_RESIZE_EXT
);
699 vnc_write_u8(vs
, 1); /* number of screens */
700 vnc_write_u8(vs
, 0); /* padding */
701 vnc_write_u8(vs
, 0); /* padding */
702 vnc_write_u8(vs
, 0); /* padding */
703 vnc_write_u32(vs
, 0); /* screen id */
704 vnc_write_u16(vs
, 0); /* screen x-pos */
705 vnc_write_u16(vs
, 0); /* screen y-pos */
706 vnc_write_u16(vs
, vs
->client_width
);
707 vnc_write_u16(vs
, vs
->client_height
);
708 vnc_write_u32(vs
, 0); /* screen flags */
709 vnc_unlock_output(vs
);
713 static void vnc_desktop_resize(VncState
*vs
)
715 if (vs
->ioc
== NULL
|| (!vnc_has_feature(vs
, VNC_FEATURE_RESIZE
) &&
716 !vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
))) {
719 if (vs
->client_width
== vs
->vd
->true_width
&&
720 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
724 assert(vs
->vd
->true_width
< 65536 &&
725 vs
->vd
->true_width
>= 0);
726 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
727 pixman_image_get_height(vs
->vd
->server
) >= 0);
728 vs
->client_width
= vs
->vd
->true_width
;
729 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
731 if (vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
)) {
732 vnc_desktop_resize_ext(vs
, 0);
736 trace_vnc_msg_server_desktop_resize(
737 vs
, vs
->ioc
, vs
->client_width
, vs
->client_height
);
740 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
742 vnc_write_u16(vs
, 1); /* number of rects */
743 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
744 VNC_ENCODING_DESKTOPRESIZE
);
745 vnc_unlock_output(vs
);
749 static void vnc_abort_display_jobs(VncDisplay
*vd
)
753 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
756 vnc_unlock_output(vs
);
758 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
761 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
763 if (vs
->update
== VNC_STATE_UPDATE_NONE
&&
764 vs
->job_update
!= VNC_STATE_UPDATE_NONE
) {
765 /* job aborted before completion */
766 vs
->update
= vs
->job_update
;
767 vs
->job_update
= VNC_STATE_UPDATE_NONE
;
770 vnc_unlock_output(vs
);
774 int vnc_server_fb_stride(VncDisplay
*vd
)
776 return pixman_image_get_stride(vd
->server
);
779 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
783 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
784 ptr
+= y
* vnc_server_fb_stride(vd
);
785 ptr
+= x
* VNC_SERVER_FB_BYTES
;
789 static void vnc_update_server_surface(VncDisplay
*vd
)
793 qemu_pixman_image_unref(vd
->server
);
796 if (QTAILQ_EMPTY(&vd
->clients
)) {
800 width
= vnc_width(vd
);
801 height
= vnc_height(vd
);
802 vd
->true_width
= vnc_true_width(vd
);
803 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
807 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
808 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
812 static bool vnc_check_pageflip(DisplaySurface
*s1
,
815 return (s1
!= NULL
&&
817 surface_width(s1
) == surface_width(s2
) &&
818 surface_height(s1
) == surface_height(s2
) &&
819 surface_format(s1
) == surface_format(s2
));
823 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
824 DisplaySurface
*surface
)
826 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
827 bool pageflip
= vnc_check_pageflip(vd
->ds
, surface
);
830 vnc_abort_display_jobs(vd
);
834 qemu_pixman_image_unref(vd
->guest
.fb
);
835 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
836 vd
->guest
.format
= surface_format(surface
);
840 trace_vnc_server_dpy_pageflip(vd
,
841 surface_width(surface
),
842 surface_height(surface
),
843 surface_format(surface
));
844 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
845 surface_width(surface
),
846 surface_height(surface
));
850 trace_vnc_server_dpy_recreate(vd
,
851 surface_width(surface
),
852 surface_height(surface
),
853 surface_format(surface
));
855 vnc_update_server_surface(vd
);
857 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
859 vnc_desktop_resize(vs
);
860 vnc_cursor_define(vs
);
861 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
862 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
865 vnc_update_throttle_offset(vs
);
870 static void vnc_write_pixels_copy(VncState
*vs
,
871 void *pixels
, int size
)
873 vnc_write(vs
, pixels
, size
);
876 /* slowest but generic code. */
877 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
881 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
882 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
883 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
884 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
886 # error need some bits here if you change VNC_SERVER_FB_FORMAT
888 v
= (r
<< vs
->client_pf
.rshift
) |
889 (g
<< vs
->client_pf
.gshift
) |
890 (b
<< vs
->client_pf
.bshift
);
891 switch (vs
->client_pf
.bytes_per_pixel
) {
921 static void vnc_write_pixels_generic(VncState
*vs
,
922 void *pixels1
, int size
)
926 if (VNC_SERVER_FB_BYTES
== 4) {
927 uint32_t *pixels
= pixels1
;
930 for (i
= 0; i
< n
; i
++) {
931 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
932 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
937 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
941 VncDisplay
*vd
= vs
->vd
;
943 row
= vnc_server_fb_ptr(vd
, x
, y
);
944 for (i
= 0; i
< h
; i
++) {
945 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
946 row
+= vnc_server_fb_stride(vd
);
951 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
955 switch(vs
->vnc_encoding
) {
956 case VNC_ENCODING_ZLIB
:
957 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
959 case VNC_ENCODING_HEXTILE
:
960 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
961 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
963 case VNC_ENCODING_TIGHT
:
964 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
966 case VNC_ENCODING_TIGHT_PNG
:
967 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
969 case VNC_ENCODING_ZRLE
:
970 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
972 case VNC_ENCODING_ZYWRLE
:
973 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
976 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
977 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
983 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
984 int x
, int y
, bool visible
)
986 /* can we ask the client(s) to move the pointer ??? */
989 static int vnc_cursor_define(VncState
*vs
)
991 QEMUCursor
*c
= qemu_console_get_cursor(vs
->vd
->dcl
.con
);
998 if (vnc_has_feature(vs
, VNC_FEATURE_ALPHA_CURSOR
)) {
1000 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1001 vnc_write_u8(vs
, 0); /* padding */
1002 vnc_write_u16(vs
, 1); /* # of rects */
1003 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
1004 VNC_ENCODING_ALPHA_CURSOR
);
1005 vnc_write_s32(vs
, VNC_ENCODING_RAW
);
1006 vnc_write(vs
, c
->data
, c
->width
* c
->height
* 4);
1007 vnc_unlock_output(vs
);
1010 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
1011 vnc_lock_output(vs
);
1012 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1013 vnc_write_u8(vs
, 0); /* padding */
1014 vnc_write_u16(vs
, 1); /* # of rects */
1015 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
1016 VNC_ENCODING_RICH_CURSOR
);
1017 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
1018 vnc_write_pixels_generic(vs
, c
->data
, isize
);
1019 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
1020 vnc_unlock_output(vs
);
1026 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
1029 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
1032 g_free(vd
->cursor_mask
);
1033 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
1034 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
1035 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
1037 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
1038 vnc_cursor_define(vs
);
1042 static int find_and_clear_dirty_height(VncState
*vs
,
1043 int y
, int last_x
, int x
, int height
)
1047 for (h
= 1; h
< (height
- y
); h
++) {
1048 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
1051 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
1058 * Figure out how much pending data we should allow in the output
1059 * buffer before we throttle incremental display updates, and/or
1060 * drop audio samples.
1062 * We allow for equiv of 1 full display's worth of FB updates,
1063 * and 1 second of audio samples. If audio backlog was larger
1064 * than that the client would already suffering awful audio
1065 * glitches, so dropping samples is no worse really).
1067 static void vnc_update_throttle_offset(VncState
*vs
)
1070 vs
->client_width
* vs
->client_height
* vs
->client_pf
.bytes_per_pixel
;
1072 if (vs
->audio_cap
) {
1074 switch (vs
->as
.fmt
) {
1076 case AUDIO_FORMAT_U8
:
1077 case AUDIO_FORMAT_S8
:
1080 case AUDIO_FORMAT_U16
:
1081 case AUDIO_FORMAT_S16
:
1084 case AUDIO_FORMAT_U32
:
1085 case AUDIO_FORMAT_S32
:
1089 offset
+= vs
->as
.freq
* bps
* vs
->as
.nchannels
;
1092 /* Put a floor of 1MB on offset, so that if we have a large pending
1093 * buffer and the display is resized to a small size & back again
1094 * we don't suddenly apply a tiny send limit
1096 offset
= MAX(offset
, 1024 * 1024);
1098 if (vs
->throttle_output_offset
!= offset
) {
1099 trace_vnc_client_throttle_threshold(
1100 vs
, vs
->ioc
, vs
->throttle_output_offset
, offset
, vs
->client_width
,
1101 vs
->client_height
, vs
->client_pf
.bytes_per_pixel
, vs
->audio_cap
);
1104 vs
->throttle_output_offset
= offset
;
1107 static bool vnc_should_update(VncState
*vs
)
1109 switch (vs
->update
) {
1110 case VNC_STATE_UPDATE_NONE
:
1112 case VNC_STATE_UPDATE_INCREMENTAL
:
1113 /* Only allow incremental updates if the pending send queue
1114 * is less than the permitted threshold, and the job worker
1115 * is completely idle.
1117 if (vs
->output
.offset
< vs
->throttle_output_offset
&&
1118 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1121 trace_vnc_client_throttle_incremental(
1122 vs
, vs
->ioc
, vs
->job_update
, vs
->output
.offset
);
1124 case VNC_STATE_UPDATE_FORCE
:
1125 /* Only allow forced updates if the pending send queue
1126 * does not contain a previous forced update, and the
1127 * job worker is completely idle.
1129 * Note this means we'll queue a forced update, even if
1130 * the output buffer size is otherwise over the throttle
1133 if (vs
->force_update_offset
== 0 &&
1134 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1137 trace_vnc_client_throttle_forced(
1138 vs
, vs
->ioc
, vs
->job_update
, vs
->force_update_offset
);
1144 static int vnc_update_client(VncState
*vs
, int has_dirty
)
1146 VncDisplay
*vd
= vs
->vd
;
1152 if (vs
->disconnecting
) {
1153 vnc_disconnect_finish(vs
);
1157 vs
->has_dirty
+= has_dirty
;
1158 if (!vnc_should_update(vs
)) {
1162 if (!vs
->has_dirty
&& vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
1167 * Send screen updates to the vnc client using the server
1168 * surface and server dirty map. guest surface updates
1169 * happening in parallel don't disturb us, the next pass will
1170 * send them to the client.
1172 job
= vnc_job_new(vs
);
1174 height
= pixman_image_get_height(vd
->server
);
1175 width
= pixman_image_get_width(vd
->server
);
1181 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1182 height
* VNC_DIRTY_BPL(vs
),
1183 y
* VNC_DIRTY_BPL(vs
));
1184 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1185 /* no more dirty bits */
1188 y
= offset
/ VNC_DIRTY_BPL(vs
);
1189 x
= offset
% VNC_DIRTY_BPL(vs
);
1190 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1191 VNC_DIRTY_BPL(vs
), x
);
1192 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1193 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1194 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1196 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1197 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1199 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1207 vs
->job_update
= vs
->update
;
1208 vs
->update
= VNC_STATE_UPDATE_NONE
;
1215 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1217 VncState
*vs
= opaque
;
1219 assert(vs
->magic
== VNC_MAGIC
);
1221 case AUD_CNOTIFY_DISABLE
:
1222 trace_vnc_msg_server_audio_end(vs
, vs
->ioc
);
1223 vnc_lock_output(vs
);
1224 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1225 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1226 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
1227 vnc_unlock_output(vs
);
1231 case AUD_CNOTIFY_ENABLE
:
1232 trace_vnc_msg_server_audio_begin(vs
, vs
->ioc
);
1233 vnc_lock_output(vs
);
1234 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1235 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1236 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1237 vnc_unlock_output(vs
);
1243 static void audio_capture_destroy(void *opaque
)
1247 static void audio_capture(void *opaque
, const void *buf
, int size
)
1249 VncState
*vs
= opaque
;
1251 assert(vs
->magic
== VNC_MAGIC
);
1252 trace_vnc_msg_server_audio_data(vs
, vs
->ioc
, buf
, size
);
1253 vnc_lock_output(vs
);
1254 if (vs
->output
.offset
< vs
->throttle_output_offset
) {
1255 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1256 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1257 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1258 vnc_write_u32(vs
, size
);
1259 vnc_write(vs
, buf
, size
);
1261 trace_vnc_client_throttle_audio(vs
, vs
->ioc
, vs
->output
.offset
);
1263 vnc_unlock_output(vs
);
1267 static void audio_add(VncState
*vs
)
1269 struct audio_capture_ops ops
;
1271 if (vs
->audio_cap
) {
1272 error_report("audio already running");
1276 ops
.notify
= audio_capture_notify
;
1277 ops
.destroy
= audio_capture_destroy
;
1278 ops
.capture
= audio_capture
;
1280 vs
->audio_cap
= AUD_add_capture(vs
->vd
->audio_state
, &vs
->as
, &ops
, vs
);
1281 if (!vs
->audio_cap
) {
1282 error_report("Failed to add audio capture");
1286 static void audio_del(VncState
*vs
)
1288 if (vs
->audio_cap
) {
1289 AUD_del_capture(vs
->audio_cap
, vs
);
1290 vs
->audio_cap
= NULL
;
1294 static void vnc_disconnect_start(VncState
*vs
)
1296 if (vs
->disconnecting
) {
1299 trace_vnc_client_disconnect_start(vs
, vs
->ioc
);
1300 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1302 g_source_remove(vs
->ioc_tag
);
1305 qio_channel_close(vs
->ioc
, NULL
);
1306 vs
->disconnecting
= TRUE
;
1309 void vnc_disconnect_finish(VncState
*vs
)
1313 trace_vnc_client_disconnect_finish(vs
, vs
->ioc
);
1315 vnc_jobs_join(vs
); /* Wait encoding jobs */
1317 vnc_lock_output(vs
);
1318 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1320 buffer_free(&vs
->input
);
1321 buffer_free(&vs
->output
);
1323 qapi_free_VncClientInfo(vs
->info
);
1326 vnc_tight_clear(vs
);
1329 #ifdef CONFIG_VNC_SASL
1330 vnc_sasl_client_cleanup(vs
);
1331 #endif /* CONFIG_VNC_SASL */
1333 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1335 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1336 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1338 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1339 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1340 /* last client gone */
1341 vnc_update_server_surface(vs
->vd
);
1343 vnc_unlock_output(vs
);
1345 if (vs
->cbpeer
.notifier
.notify
) {
1346 qemu_clipboard_peer_unregister(&vs
->cbpeer
);
1349 qemu_mutex_destroy(&vs
->output_mutex
);
1350 if (vs
->bh
!= NULL
) {
1351 qemu_bh_delete(vs
->bh
);
1353 buffer_free(&vs
->jobs_buffer
);
1355 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1356 g_free(vs
->lossy_rect
[i
]);
1358 g_free(vs
->lossy_rect
);
1360 object_unref(OBJECT(vs
->ioc
));
1362 object_unref(OBJECT(vs
->sioc
));
1370 size_t vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
*err
)
1374 trace_vnc_client_eof(vs
, vs
->ioc
);
1375 vnc_disconnect_start(vs
);
1376 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1377 trace_vnc_client_io_error(vs
, vs
->ioc
,
1378 err
? error_get_pretty(err
) : "Unknown");
1379 vnc_disconnect_start(vs
);
1389 void vnc_client_error(VncState
*vs
)
1391 VNC_DEBUG("Closing down client sock: protocol error\n");
1392 vnc_disconnect_start(vs
);
1397 * Called to write a chunk of data to the client socket. The data may
1398 * be the raw data, or may have already been encoded by SASL.
1399 * The data will be written either straight onto the socket, or
1400 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1402 * NB, it is theoretically possible to have 2 layers of encryption,
1403 * both SASL, and this TLS layer. It is highly unlikely in practice
1404 * though, since SASL encryption will typically be a no-op if TLS
1407 * Returns the number of bytes written, which may be less than
1408 * the requested 'datalen' if the socket would block. Returns
1409 * 0 on I/O error, and disconnects the client socket.
1411 size_t vnc_client_write_buf(VncState
*vs
, const void *data
, size_t datalen
)
1415 ret
= qio_channel_write(vs
->ioc
, (const char *)data
, datalen
, &err
);
1416 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1417 return vnc_client_io_error(vs
, ret
, err
);
1422 * Called to write buffered data to the client socket, when not
1423 * using any SASL SSF encryption layers. Will write as much data
1424 * as possible without blocking. If all buffered data is written,
1425 * will switch the FD poll() handler back to read monitoring.
1427 * Returns the number of bytes written, which may be less than
1428 * the buffered output data if the socket would block. Returns
1429 * 0 on I/O error, and disconnects the client socket.
1431 static size_t vnc_client_write_plain(VncState
*vs
)
1436 #ifdef CONFIG_VNC_SASL
1437 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1438 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1439 vs
->sasl
.waitWriteSSF
);
1441 if (vs
->sasl
.conn
&&
1443 vs
->sasl
.waitWriteSSF
) {
1444 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1446 vs
->sasl
.waitWriteSSF
-= ret
;
1448 #endif /* CONFIG_VNC_SASL */
1449 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1453 if (ret
>= vs
->force_update_offset
) {
1454 if (vs
->force_update_offset
!= 0) {
1455 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
1457 vs
->force_update_offset
= 0;
1459 vs
->force_update_offset
-= ret
;
1461 offset
= vs
->output
.offset
;
1462 buffer_advance(&vs
->output
, ret
);
1463 if (offset
>= vs
->throttle_output_offset
&&
1464 vs
->output
.offset
< vs
->throttle_output_offset
) {
1465 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
, vs
->output
.offset
);
1468 if (vs
->output
.offset
== 0) {
1470 g_source_remove(vs
->ioc_tag
);
1472 vs
->ioc_tag
= qio_channel_add_watch(
1473 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
1474 vnc_client_io
, vs
, NULL
);
1482 * First function called whenever there is data to be written to
1483 * the client socket. Will delegate actual work according to whether
1484 * SASL SSF layers are enabled (thus requiring encryption calls)
1486 static void vnc_client_write_locked(VncState
*vs
)
1488 #ifdef CONFIG_VNC_SASL
1489 if (vs
->sasl
.conn
&&
1491 !vs
->sasl
.waitWriteSSF
) {
1492 vnc_client_write_sasl(vs
);
1494 #endif /* CONFIG_VNC_SASL */
1496 vnc_client_write_plain(vs
);
1500 static void vnc_client_write(VncState
*vs
)
1502 assert(vs
->magic
== VNC_MAGIC
);
1503 vnc_lock_output(vs
);
1504 if (vs
->output
.offset
) {
1505 vnc_client_write_locked(vs
);
1506 } else if (vs
->ioc
!= NULL
) {
1508 g_source_remove(vs
->ioc_tag
);
1510 vs
->ioc_tag
= qio_channel_add_watch(
1511 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
1512 vnc_client_io
, vs
, NULL
);
1514 vnc_unlock_output(vs
);
1517 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1519 vs
->read_handler
= func
;
1520 vs
->read_handler_expect
= expecting
;
1525 * Called to read a chunk of data from the client socket. The data may
1526 * be the raw data, or may need to be further decoded by SASL.
1527 * The data will be read either straight from to the socket, or
1528 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1530 * NB, it is theoretically possible to have 2 layers of encryption,
1531 * both SASL, and this TLS layer. It is highly unlikely in practice
1532 * though, since SASL encryption will typically be a no-op if TLS
1535 * Returns the number of bytes read, which may be less than
1536 * the requested 'datalen' if the socket would block. Returns
1537 * 0 on I/O error or EOF, and disconnects the client socket.
1539 size_t vnc_client_read_buf(VncState
*vs
, void *data
, size_t datalen
)
1543 ret
= qio_channel_read(vs
->ioc
, (char *)data
, datalen
, &err
);
1544 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1545 return vnc_client_io_error(vs
, ret
, err
);
1550 * Called to read data from the client socket to the input buffer,
1551 * when not using any SASL SSF encryption layers. Will read as much
1552 * data as possible without blocking.
1554 * Returns the number of bytes read, which may be less than
1555 * the requested 'datalen' if the socket would block. Returns
1556 * 0 on I/O error or EOF, and disconnects the client socket.
1558 static size_t vnc_client_read_plain(VncState
*vs
)
1561 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1562 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1563 buffer_reserve(&vs
->input
, 4096);
1564 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1567 vs
->input
.offset
+= ret
;
1571 static void vnc_jobs_bh(void *opaque
)
1573 VncState
*vs
= opaque
;
1575 assert(vs
->magic
== VNC_MAGIC
);
1576 vnc_jobs_consume_buffer(vs
);
1580 * First function called whenever there is more data to be read from
1581 * the client socket. Will delegate actual work according to whether
1582 * SASL SSF layers are enabled (thus requiring decryption calls)
1583 * Returns 0 on success, -1 if client disconnected
1585 static int vnc_client_read(VncState
*vs
)
1589 #ifdef CONFIG_VNC_SASL
1590 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1591 sz
= vnc_client_read_sasl(vs
);
1593 #endif /* CONFIG_VNC_SASL */
1594 sz
= vnc_client_read_plain(vs
);
1596 if (vs
->disconnecting
) {
1597 vnc_disconnect_finish(vs
);
1603 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1604 size_t len
= vs
->read_handler_expect
;
1607 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1608 if (vs
->disconnecting
) {
1609 vnc_disconnect_finish(vs
);
1614 buffer_advance(&vs
->input
, len
);
1616 vs
->read_handler_expect
= ret
;
1622 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1623 GIOCondition condition
, void *opaque
)
1625 VncState
*vs
= opaque
;
1627 assert(vs
->magic
== VNC_MAGIC
);
1629 if (condition
& (G_IO_HUP
| G_IO_ERR
)) {
1630 vnc_disconnect_start(vs
);
1634 if (condition
& G_IO_IN
) {
1635 if (vnc_client_read(vs
) < 0) {
1636 /* vs is free()ed here */
1640 if (condition
& G_IO_OUT
) {
1641 vnc_client_write(vs
);
1644 if (vs
->disconnecting
) {
1645 if (vs
->ioc_tag
!= 0) {
1646 g_source_remove(vs
->ioc_tag
);
1655 * Scale factor to apply to vs->throttle_output_offset when checking for
1656 * hard limit. Worst case normal usage could be x2, if we have a complete
1657 * incremental update and complete forced update in the output buffer.
1658 * So x3 should be good enough, but we pick x5 to be conservative and thus
1659 * (hopefully) never trigger incorrectly.
1661 #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1663 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1665 assert(vs
->magic
== VNC_MAGIC
);
1666 if (vs
->disconnecting
) {
1669 /* Protection against malicious client/guest to prevent our output
1670 * buffer growing without bound if client stops reading data. This
1671 * should rarely trigger, because we have earlier throttling code
1672 * which stops issuing framebuffer updates and drops audio data
1673 * if the throttle_output_offset value is exceeded. So we only reach
1674 * this higher level if a huge number of pseudo-encodings get
1675 * triggered while data can't be sent on the socket.
1677 * NB throttle_output_offset can be zero during early protocol
1678 * handshake, or from the job thread's VncState clone
1680 if (vs
->throttle_output_offset
!= 0 &&
1681 (vs
->output
.offset
/ VNC_THROTTLE_OUTPUT_LIMIT_SCALE
) >
1682 vs
->throttle_output_offset
) {
1683 trace_vnc_client_output_limit(vs
, vs
->ioc
, vs
->output
.offset
,
1684 vs
->throttle_output_offset
);
1685 vnc_disconnect_start(vs
);
1688 buffer_reserve(&vs
->output
, len
);
1690 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1692 g_source_remove(vs
->ioc_tag
);
1694 vs
->ioc_tag
= qio_channel_add_watch(
1695 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_OUT
,
1696 vnc_client_io
, vs
, NULL
);
1699 buffer_append(&vs
->output
, data
, len
);
1702 void vnc_write_s32(VncState
*vs
, int32_t value
)
1704 vnc_write_u32(vs
, *(uint32_t *)&value
);
1707 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1711 buf
[0] = (value
>> 24) & 0xFF;
1712 buf
[1] = (value
>> 16) & 0xFF;
1713 buf
[2] = (value
>> 8) & 0xFF;
1714 buf
[3] = value
& 0xFF;
1716 vnc_write(vs
, buf
, 4);
1719 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1723 buf
[0] = (value
>> 8) & 0xFF;
1724 buf
[1] = value
& 0xFF;
1726 vnc_write(vs
, buf
, 2);
1729 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1731 vnc_write(vs
, (char *)&value
, 1);
1734 void vnc_flush(VncState
*vs
)
1736 vnc_lock_output(vs
);
1737 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1738 vnc_client_write_locked(vs
);
1740 if (vs
->disconnecting
) {
1741 if (vs
->ioc_tag
!= 0) {
1742 g_source_remove(vs
->ioc_tag
);
1746 vnc_unlock_output(vs
);
1749 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1751 return data
[offset
];
1754 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1756 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1759 static int32_t read_s32(uint8_t *data
, size_t offset
)
1761 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1762 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1765 uint32_t read_u32(uint8_t *data
, size_t offset
)
1767 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1768 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1771 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1773 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1774 int absolute
= qemu_input_is_absolute(vs
->vd
->dcl
.con
);
1776 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
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
, absolute
, 0,
1782 pixman_image_get_width(vs
->vd
->server
),
1783 pixman_image_get_height(vs
->vd
->server
),
1784 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1785 vnc_unlock_output(vs
);
1788 vs
->absolute
= absolute
;
1791 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1793 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1794 [INPUT_BUTTON_LEFT
] = 0x01,
1795 [INPUT_BUTTON_MIDDLE
] = 0x02,
1796 [INPUT_BUTTON_RIGHT
] = 0x04,
1797 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1798 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1800 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1801 int width
= pixman_image_get_width(vs
->vd
->server
);
1802 int height
= pixman_image_get_height(vs
->vd
->server
);
1804 if (vs
->last_bmask
!= button_mask
) {
1805 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1806 vs
->last_bmask
= button_mask
;
1810 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, 0, width
);
1811 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, 0, height
);
1812 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1813 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1814 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1816 if (vs
->last_x
!= -1) {
1817 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1818 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1823 qemu_input_event_sync();
1826 static void press_key(VncState
*vs
, QKeyCode qcode
)
1828 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, true);
1829 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, false);
1832 static void vnc_led_state_change(VncState
*vs
)
1834 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1838 vnc_lock_output(vs
);
1839 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1840 vnc_write_u8(vs
, 0);
1841 vnc_write_u16(vs
, 1);
1842 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1843 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1844 vnc_unlock_output(vs
);
1848 static void kbd_leds(void *opaque
, int ledstate
)
1850 VncDisplay
*vd
= opaque
;
1853 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1854 (ledstate
& QEMU_NUM_LOCK_LED
),
1855 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1857 if (ledstate
== vd
->ledstate
) {
1861 vd
->ledstate
= ledstate
;
1863 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1864 vnc_led_state_change(client
);
1868 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1870 QKeyCode qcode
= qemu_input_key_number_to_qcode(keycode
);
1872 /* QEMU console switch */
1874 case Q_KEY_CODE_1
... Q_KEY_CODE_9
: /* '1' to '9' keys */
1876 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
) &&
1877 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_ALT
)) {
1878 QemuConsole
*con
= qemu_console_lookup_by_index(qcode
- Q_KEY_CODE_1
);
1880 unregister_displaychangelistener(&vs
->vd
->dcl
);
1881 qkbd_state_switch_console(vs
->vd
->kbd
, con
);
1882 vs
->vd
->dcl
.con
= con
;
1883 register_displaychangelistener(&vs
->vd
->dcl
);
1891 /* Turn off the lock state sync logic if the client support the led
1894 if (down
&& vs
->vd
->lock_key_sync
&&
1895 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1896 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1897 /* If the numlock state needs to change then simulate an additional
1898 keypress before sending this one. This will happen if the user
1899 toggles numlock away from the VNC window.
1901 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1902 if (!qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1903 trace_vnc_key_sync_numlock(true);
1904 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1907 if (qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1908 trace_vnc_key_sync_numlock(false);
1909 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1914 if (down
&& vs
->vd
->lock_key_sync
&&
1915 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1916 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1917 /* If the capslock state needs to change then simulate an additional
1918 keypress before sending this one. This will happen if the user
1919 toggles capslock away from the VNC window.
1921 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1922 bool shift
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_SHIFT
);
1923 bool capslock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CAPSLOCK
);
1925 if (uppercase
== shift
) {
1926 trace_vnc_key_sync_capslock(false);
1927 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1930 if (uppercase
!= shift
) {
1931 trace_vnc_key_sync_capslock(true);
1932 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1937 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, down
);
1938 if (!qemu_console_is_graphic(vs
->vd
->dcl
.con
)) {
1939 QemuTextConsole
*con
= QEMU_TEXT_CONSOLE(vs
->vd
->dcl
.con
);
1940 bool numlock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
);
1941 bool control
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
);
1942 /* QEMU console emulation */
1945 case 0x2a: /* Left Shift */
1946 case 0x36: /* Right Shift */
1947 case 0x1d: /* Left CTRL */
1948 case 0x9d: /* Right CTRL */
1949 case 0x38: /* Left ALT */
1950 case 0xb8: /* Right ALT */
1953 qemu_text_console_put_keysym(con
, QEMU_KEY_UP
);
1956 qemu_text_console_put_keysym(con
, QEMU_KEY_DOWN
);
1959 qemu_text_console_put_keysym(con
, QEMU_KEY_LEFT
);
1962 qemu_text_console_put_keysym(con
, QEMU_KEY_RIGHT
);
1965 qemu_text_console_put_keysym(con
, QEMU_KEY_DELETE
);
1968 qemu_text_console_put_keysym(con
, QEMU_KEY_HOME
);
1971 qemu_text_console_put_keysym(con
, QEMU_KEY_END
);
1974 qemu_text_console_put_keysym(con
, QEMU_KEY_PAGEUP
);
1977 qemu_text_console_put_keysym(con
, QEMU_KEY_PAGEDOWN
);
1981 qemu_text_console_put_keysym(con
, numlock
? '7' : QEMU_KEY_HOME
);
1984 qemu_text_console_put_keysym(con
, numlock
? '8' : QEMU_KEY_UP
);
1987 qemu_text_console_put_keysym(con
, numlock
? '9' : QEMU_KEY_PAGEUP
);
1990 qemu_text_console_put_keysym(con
, numlock
? '4' : QEMU_KEY_LEFT
);
1993 qemu_text_console_put_keysym(con
, '5');
1996 qemu_text_console_put_keysym(con
, numlock
? '6' : QEMU_KEY_RIGHT
);
1999 qemu_text_console_put_keysym(con
, numlock
? '1' : QEMU_KEY_END
);
2002 qemu_text_console_put_keysym(con
, numlock
? '2' : QEMU_KEY_DOWN
);
2005 qemu_text_console_put_keysym(con
, numlock
? '3' : QEMU_KEY_PAGEDOWN
);
2008 qemu_text_console_put_keysym(con
, '0');
2011 qemu_text_console_put_keysym(con
, numlock
? '.' : QEMU_KEY_DELETE
);
2015 qemu_text_console_put_keysym(con
, '/');
2018 qemu_text_console_put_keysym(con
, '*');
2021 qemu_text_console_put_keysym(con
, '-');
2024 qemu_text_console_put_keysym(con
, '+');
2027 qemu_text_console_put_keysym(con
, '\n');
2032 qemu_text_console_put_keysym(con
, sym
& 0x1f);
2034 qemu_text_console_put_keysym(con
, sym
);
2042 static const char *code2name(int keycode
)
2044 return QKeyCode_str(qemu_input_key_number_to_qcode(keycode
));
2047 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
2052 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(vs
->vd
->dcl
.con
)) {
2053 lsym
= lsym
- 'A' + 'a';
2056 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF,
2057 vs
->vd
->kbd
, down
) & SCANCODE_KEYMASK
;
2058 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
2059 do_key_event(vs
, down
, keycode
, sym
);
2062 static void ext_key_event(VncState
*vs
, int down
,
2063 uint32_t sym
, uint16_t keycode
)
2065 /* if the user specifies a keyboard layout, always use it */
2066 if (keyboard_layout
) {
2067 key_event(vs
, down
, sym
);
2069 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
2070 do_key_event(vs
, down
, keycode
, sym
);
2074 static void framebuffer_update_request(VncState
*vs
, int incremental
,
2075 int x
, int y
, int w
, int h
)
2078 if (vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
2079 vs
->update
= VNC_STATE_UPDATE_INCREMENTAL
;
2082 vs
->update
= VNC_STATE_UPDATE_FORCE
;
2083 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
2084 if (vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
)) {
2085 vnc_desktop_resize_ext(vs
, 0);
2090 static void send_ext_key_event_ack(VncState
*vs
)
2092 vnc_lock_output(vs
);
2093 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2094 vnc_write_u8(vs
, 0);
2095 vnc_write_u16(vs
, 1);
2096 vnc_framebuffer_update(vs
, 0, 0,
2097 pixman_image_get_width(vs
->vd
->server
),
2098 pixman_image_get_height(vs
->vd
->server
),
2099 VNC_ENCODING_EXT_KEY_EVENT
);
2100 vnc_unlock_output(vs
);
2104 static void send_ext_audio_ack(VncState
*vs
)
2106 vnc_lock_output(vs
);
2107 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2108 vnc_write_u8(vs
, 0);
2109 vnc_write_u16(vs
, 1);
2110 vnc_framebuffer_update(vs
, 0, 0,
2111 pixman_image_get_width(vs
->vd
->server
),
2112 pixman_image_get_height(vs
->vd
->server
),
2113 VNC_ENCODING_AUDIO
);
2114 vnc_unlock_output(vs
);
2118 static void send_xvp_message(VncState
*vs
, int code
)
2120 vnc_lock_output(vs
);
2121 vnc_write_u8(vs
, VNC_MSG_SERVER_XVP
);
2122 vnc_write_u8(vs
, 0); /* pad */
2123 vnc_write_u8(vs
, 1); /* version */
2124 vnc_write_u8(vs
, code
);
2125 vnc_unlock_output(vs
);
2129 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2132 unsigned int enc
= 0;
2135 vs
->vnc_encoding
= 0;
2136 vs
->tight
->compression
= 9;
2137 vs
->tight
->quality
= -1; /* Lossless by default */
2141 * Start from the end because the encodings are sent in order of preference.
2142 * This way the preferred encoding (first encoding defined in the array)
2143 * will be set at the end of the loop.
2145 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2148 case VNC_ENCODING_RAW
:
2149 vs
->vnc_encoding
= enc
;
2151 case VNC_ENCODING_HEXTILE
:
2152 vnc_set_feature(vs
, VNC_FEATURE_HEXTILE
);
2153 vs
->vnc_encoding
= enc
;
2155 case VNC_ENCODING_TIGHT
:
2156 vnc_set_feature(vs
, VNC_FEATURE_TIGHT
);
2157 vs
->vnc_encoding
= enc
;
2160 case VNC_ENCODING_TIGHT_PNG
:
2161 vnc_set_feature(vs
, VNC_FEATURE_TIGHT_PNG
);
2162 vs
->vnc_encoding
= enc
;
2165 case VNC_ENCODING_ZLIB
:
2167 * VNC_ENCODING_ZRLE compresses better than VNC_ENCODING_ZLIB.
2168 * So prioritize ZRLE, even if the client hints that it prefers
2171 if (!vnc_has_feature(vs
, VNC_FEATURE_ZRLE
)) {
2172 vnc_set_feature(vs
, VNC_FEATURE_ZLIB
);
2173 vs
->vnc_encoding
= enc
;
2176 case VNC_ENCODING_ZRLE
:
2177 vnc_set_feature(vs
, VNC_FEATURE_ZRLE
);
2178 vs
->vnc_encoding
= enc
;
2180 case VNC_ENCODING_ZYWRLE
:
2181 vnc_set_feature(vs
, VNC_FEATURE_ZYWRLE
);
2182 vs
->vnc_encoding
= enc
;
2184 case VNC_ENCODING_DESKTOPRESIZE
:
2185 vnc_set_feature(vs
, VNC_FEATURE_RESIZE
);
2187 case VNC_ENCODING_DESKTOP_RESIZE_EXT
:
2188 vnc_set_feature(vs
, VNC_FEATURE_RESIZE_EXT
);
2190 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2191 vnc_set_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
);
2193 case VNC_ENCODING_RICH_CURSOR
:
2194 vnc_set_feature(vs
, VNC_FEATURE_RICH_CURSOR
);
2196 case VNC_ENCODING_ALPHA_CURSOR
:
2197 vnc_set_feature(vs
, VNC_FEATURE_ALPHA_CURSOR
);
2199 case VNC_ENCODING_EXT_KEY_EVENT
:
2200 send_ext_key_event_ack(vs
);
2202 case VNC_ENCODING_AUDIO
:
2203 if (vs
->vd
->audio_state
) {
2204 vnc_set_feature(vs
, VNC_FEATURE_AUDIO
);
2205 send_ext_audio_ack(vs
);
2208 case VNC_ENCODING_WMVi
:
2209 vnc_set_feature(vs
, VNC_FEATURE_WMVI
);
2211 case VNC_ENCODING_LED_STATE
:
2212 vnc_set_feature(vs
, VNC_FEATURE_LED_STATE
);
2214 case VNC_ENCODING_XVP
:
2215 if (vs
->vd
->power_control
) {
2216 vnc_set_feature(vs
, VNC_FEATURE_XVP
);
2217 send_xvp_message(vs
, VNC_XVP_CODE_INIT
);
2220 case VNC_ENCODING_CLIPBOARD_EXT
:
2221 vnc_set_feature(vs
, VNC_FEATURE_CLIPBOARD_EXT
);
2222 vnc_server_cut_text_caps(vs
);
2224 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2225 vs
->tight
->compression
= (enc
& 0x0F);
2227 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2228 if (vs
->vd
->lossy
) {
2229 vs
->tight
->quality
= (enc
& 0x0F);
2233 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2237 vnc_desktop_resize(vs
);
2238 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2239 vnc_led_state_change(vs
);
2240 vnc_cursor_define(vs
);
2243 static void set_pixel_conversion(VncState
*vs
)
2245 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2247 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2248 vs
->write_pixels
= vnc_write_pixels_copy
;
2249 vnc_hextile_set_pixel_conversion(vs
, 0);
2251 vs
->write_pixels
= vnc_write_pixels_generic
;
2252 vnc_hextile_set_pixel_conversion(vs
, 1);
2256 static void send_color_map(VncState
*vs
)
2260 vnc_lock_output(vs
);
2261 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2262 vnc_write_u8(vs
, 0); /* padding */
2263 vnc_write_u16(vs
, 0); /* first color */
2264 vnc_write_u16(vs
, 256); /* # of colors */
2266 for (i
= 0; i
< 256; i
++) {
2267 PixelFormat
*pf
= &vs
->client_pf
;
2269 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2270 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2271 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2273 vnc_unlock_output(vs
);
2276 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2277 int big_endian_flag
, int true_color_flag
,
2278 int red_max
, int green_max
, int blue_max
,
2279 int red_shift
, int green_shift
, int blue_shift
)
2281 if (!true_color_flag
) {
2282 /* Expose a reasonable default 256 color map */
2292 switch (bits_per_pixel
) {
2298 vnc_client_error(vs
);
2302 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2303 vs
->client_pf
.rbits
= ctpopl(red_max
);
2304 vs
->client_pf
.rshift
= red_shift
;
2305 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2306 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2307 vs
->client_pf
.gbits
= ctpopl(green_max
);
2308 vs
->client_pf
.gshift
= green_shift
;
2309 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2310 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2311 vs
->client_pf
.bbits
= ctpopl(blue_max
);
2312 vs
->client_pf
.bshift
= blue_shift
;
2313 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2314 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2315 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2316 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2317 vs
->client_be
= big_endian_flag
;
2319 if (!true_color_flag
) {
2323 set_pixel_conversion(vs
);
2325 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2326 graphic_hw_update(vs
->vd
->dcl
.con
);
2329 static void pixel_format_message (VncState
*vs
) {
2330 char pad
[3] = { 0, 0, 0 };
2332 vs
->client_pf
= qemu_default_pixelformat(32);
2334 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2335 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2338 vnc_write_u8(vs
, 1); /* big-endian-flag */
2340 vnc_write_u8(vs
, 0); /* big-endian-flag */
2342 vnc_write_u8(vs
, 1); /* true-color-flag */
2343 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2344 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2345 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2346 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2347 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2348 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2349 vnc_write(vs
, pad
, 3); /* padding */
2351 vnc_hextile_set_pixel_conversion(vs
, 0);
2352 vs
->write_pixels
= vnc_write_pixels_copy
;
2355 static void vnc_colordepth(VncState
*vs
)
2357 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2358 /* Sending a WMVi message to notify the client*/
2359 vnc_lock_output(vs
);
2360 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2361 vnc_write_u8(vs
, 0);
2362 vnc_write_u16(vs
, 1); /* number of rects */
2363 vnc_framebuffer_update(vs
, 0, 0,
2367 pixel_format_message(vs
);
2368 vnc_unlock_output(vs
);
2371 set_pixel_conversion(vs
);
2375 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2380 VncDisplay
*vd
= vs
->vd
;
2383 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2387 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2391 set_pixel_format(vs
, read_u8(data
, 4),
2392 read_u8(data
, 6), read_u8(data
, 7),
2393 read_u16(data
, 8), read_u16(data
, 10),
2394 read_u16(data
, 12), read_u8(data
, 14),
2395 read_u8(data
, 15), read_u8(data
, 16));
2397 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2402 limit
= read_u16(data
, 2);
2404 return 4 + (limit
* 4);
2406 limit
= read_u16(data
, 2);
2408 for (i
= 0; i
< limit
; i
++) {
2409 int32_t val
= read_s32(data
, 4 + (i
* 4));
2410 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2413 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2415 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2419 framebuffer_update_request(vs
,
2420 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2421 read_u16(data
, 6), read_u16(data
, 8));
2423 case VNC_MSG_CLIENT_KEY_EVENT
:
2427 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2429 case VNC_MSG_CLIENT_POINTER_EVENT
:
2433 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2435 case VNC_MSG_CLIENT_CUT_TEXT
:
2439 uint32_t dlen
= abs(read_s32(data
, 4));
2441 if (dlen
> (1 << 20)) {
2442 error_report("vnc: client_cut_text msg payload has %u bytes"
2443 " which exceeds our limit of 1MB.", dlen
);
2444 vnc_client_error(vs
);
2452 if (read_s32(data
, 4) < 0) {
2453 if (!vnc_has_feature(vs
, VNC_FEATURE_CLIPBOARD_EXT
)) {
2454 error_report("vnc: extended clipboard message while disabled");
2455 vnc_client_error(vs
);
2459 error_report("vnc: malformed payload (header less than 4 bytes)"
2460 " in extended clipboard pseudo-encoding.");
2461 vnc_client_error(vs
);
2464 vnc_client_cut_text_ext(vs
, dlen
, read_u32(data
, 8), data
+ 12);
2467 vnc_client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2469 case VNC_MSG_CLIENT_XVP
:
2470 if (!vnc_has_feature(vs
, VNC_FEATURE_XVP
)) {
2471 error_report("vnc: xvp client message while disabled");
2472 vnc_client_error(vs
);
2479 uint8_t version
= read_u8(data
, 2);
2480 uint8_t action
= read_u8(data
, 3);
2483 error_report("vnc: xvp client message version %d != 1",
2485 vnc_client_error(vs
);
2490 case VNC_XVP_ACTION_SHUTDOWN
:
2491 qemu_system_powerdown_request();
2493 case VNC_XVP_ACTION_REBOOT
:
2494 send_xvp_message(vs
, VNC_XVP_CODE_FAIL
);
2496 case VNC_XVP_ACTION_RESET
:
2497 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
2500 send_xvp_message(vs
, VNC_XVP_CODE_FAIL
);
2505 case VNC_MSG_CLIENT_QEMU
:
2509 switch (read_u8(data
, 1)) {
2510 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2514 ext_key_event(vs
, read_u16(data
, 2),
2515 read_u32(data
, 4), read_u32(data
, 8));
2517 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2518 if (!vnc_has_feature(vs
, VNC_FEATURE_AUDIO
)) {
2519 error_report("Audio message %d with audio disabled", read_u8(data
, 2));
2520 vnc_client_error(vs
);
2527 switch (read_u16 (data
, 2)) {
2528 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2529 trace_vnc_msg_client_audio_enable(vs
, vs
->ioc
);
2532 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2533 trace_vnc_msg_client_audio_disable(vs
, vs
->ioc
);
2536 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2539 switch (read_u8(data
, 4)) {
2540 case 0: vs
->as
.fmt
= AUDIO_FORMAT_U8
; break;
2541 case 1: vs
->as
.fmt
= AUDIO_FORMAT_S8
; break;
2542 case 2: vs
->as
.fmt
= AUDIO_FORMAT_U16
; break;
2543 case 3: vs
->as
.fmt
= AUDIO_FORMAT_S16
; break;
2544 case 4: vs
->as
.fmt
= AUDIO_FORMAT_U32
; break;
2545 case 5: vs
->as
.fmt
= AUDIO_FORMAT_S32
; break;
2547 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2548 vnc_client_error(vs
);
2551 vs
->as
.nchannels
= read_u8(data
, 5);
2552 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2553 VNC_DEBUG("Invalid audio channel count %d\n",
2555 vnc_client_error(vs
);
2558 freq
= read_u32(data
, 6);
2559 /* No official limit for protocol, but 48khz is a sensible
2560 * upper bound for trustworthy clients, and this limit
2561 * protects calculations involving 'vs->as.freq' later.
2564 VNC_DEBUG("Invalid audio frequency %u > 48000", freq
);
2565 vnc_client_error(vs
);
2569 trace_vnc_msg_client_audio_format(
2570 vs
, vs
->ioc
, vs
->as
.fmt
, vs
->as
.nchannels
, vs
->as
.freq
);
2573 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 2));
2574 vnc_client_error(vs
);
2580 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2581 vnc_client_error(vs
);
2585 case VNC_MSG_CLIENT_SET_DESKTOP_SIZE
:
2595 screens
= read_u8(data
, 6);
2596 size
= 8 + screens
* 16;
2600 w
= read_u16(data
, 2);
2601 h
= read_u16(data
, 4);
2603 trace_vnc_msg_client_set_desktop_size(vs
, vs
->ioc
, w
, h
, screens
);
2604 if (dpy_ui_info_supported(vs
->vd
->dcl
.con
)) {
2606 memset(&info
, 0, sizeof(info
));
2609 dpy_set_ui_info(vs
->vd
->dcl
.con
, &info
, false);
2610 vnc_desktop_resize_ext(vs
, 4 /* Request forwarded */);
2612 vnc_desktop_resize_ext(vs
, 3 /* Invalid screen layout */);
2618 VNC_DEBUG("Msg: %d\n", data
[0]);
2619 vnc_client_error(vs
);
2623 vnc_update_throttle_offset(vs
);
2624 vnc_read_when(vs
, protocol_client_msg
, 1);
2628 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2634 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2635 switch (vs
->vd
->share_policy
) {
2636 case VNC_SHARE_POLICY_IGNORE
:
2638 * Ignore the shared flag. Nothing to do here.
2640 * Doesn't conform to the rfb spec but is traditional qemu
2641 * behavior, thus left here as option for compatibility
2645 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2647 * Policy: Allow clients ask for exclusive access.
2649 * Implementation: When a client asks for exclusive access,
2650 * disconnect all others. Shared connects are allowed as long
2651 * as no exclusive connection exists.
2653 * This is how the rfb spec suggests to handle the shared flag.
2655 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2657 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2661 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2662 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2665 vnc_disconnect_start(client
);
2668 if (mode
== VNC_SHARE_MODE_SHARED
) {
2669 if (vs
->vd
->num_exclusive
> 0) {
2670 vnc_disconnect_start(vs
);
2675 case VNC_SHARE_POLICY_FORCE_SHARED
:
2677 * Policy: Shared connects only.
2678 * Implementation: Disallow clients asking for exclusive access.
2680 * Useful for shared desktop sessions where you don't want
2681 * someone forgetting to say -shared when running the vnc
2682 * client disconnect everybody else.
2684 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2685 vnc_disconnect_start(vs
);
2690 vnc_set_share_mode(vs
, mode
);
2692 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2693 vnc_disconnect_start(vs
);
2697 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
2698 pixman_image_get_width(vs
->vd
->server
) >= 0);
2699 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
2700 pixman_image_get_height(vs
->vd
->server
) >= 0);
2701 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2702 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2703 vnc_write_u16(vs
, vs
->client_width
);
2704 vnc_write_u16(vs
, vs
->client_height
);
2706 pixel_format_message(vs
);
2709 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2710 if (size
> sizeof(buf
)) {
2714 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2717 vnc_write_u32(vs
, size
);
2718 vnc_write(vs
, buf
, size
);
2721 vnc_client_cache_auth(vs
);
2722 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2724 vnc_read_when(vs
, protocol_client_msg
, 1);
2729 void start_client_init(VncState
*vs
)
2731 vnc_read_when(vs
, protocol_client_init
, 1);
2734 static void authentication_failed(VncState
*vs
)
2736 vnc_write_u32(vs
, 1); /* Reject auth */
2737 if (vs
->minor
>= 8) {
2738 static const char err
[] = "Authentication failed";
2739 vnc_write_u32(vs
, sizeof(err
));
2740 vnc_write(vs
, err
, sizeof(err
));
2743 vnc_client_error(vs
);
2747 vnc_munge_des_rfb_key(unsigned char *key
, size_t nkey
)
2750 for (i
= 0; i
< nkey
; i
++) {
2752 r
= (r
& 0xf0) >> 4 | (r
& 0x0f) << 4;
2753 r
= (r
& 0xcc) >> 2 | (r
& 0x33) << 2;
2754 r
= (r
& 0xaa) >> 1 | (r
& 0x55) << 1;
2759 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2761 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2763 unsigned char key
[8];
2764 time_t now
= time(NULL
);
2765 QCryptoCipher
*cipher
= NULL
;
2768 if (!vs
->vd
->password
) {
2769 trace_vnc_auth_fail(vs
, vs
->auth
, "password is not set", "");
2772 if (vs
->vd
->expires
< now
) {
2773 trace_vnc_auth_fail(vs
, vs
->auth
, "password is expired", "");
2777 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2779 /* Calculate the expected challenge response */
2780 pwlen
= strlen(vs
->vd
->password
);
2781 for (i
=0; i
<sizeof(key
); i
++)
2782 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2783 vnc_munge_des_rfb_key(key
, sizeof(key
));
2785 cipher
= qcrypto_cipher_new(
2786 QCRYPTO_CIPHER_ALG_DES
,
2787 QCRYPTO_CIPHER_MODE_ECB
,
2788 key
, G_N_ELEMENTS(key
),
2791 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot create cipher",
2792 error_get_pretty(err
));
2797 if (qcrypto_cipher_encrypt(cipher
,
2800 VNC_AUTH_CHALLENGE_SIZE
,
2802 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot encrypt challenge response",
2803 error_get_pretty(err
));
2808 /* Compare expected vs actual challenge response */
2809 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2810 trace_vnc_auth_fail(vs
, vs
->auth
, "mis-matched challenge response", "");
2813 trace_vnc_auth_pass(vs
, vs
->auth
);
2814 vnc_write_u32(vs
, 0); /* Accept auth */
2817 start_client_init(vs
);
2820 qcrypto_cipher_free(cipher
);
2824 authentication_failed(vs
);
2825 qcrypto_cipher_free(cipher
);
2829 void start_auth_vnc(VncState
*vs
)
2833 if (qcrypto_random_bytes(vs
->challenge
, sizeof(vs
->challenge
), &err
)) {
2834 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot get random bytes",
2835 error_get_pretty(err
));
2837 authentication_failed(vs
);
2841 /* Send client a 'random' challenge */
2842 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2845 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2849 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2851 /* We only advertise 1 auth scheme at a time, so client
2852 * must pick the one we sent. Verify this */
2853 if (data
[0] != vs
->auth
) { /* Reject auth */
2854 trace_vnc_auth_reject(vs
, vs
->auth
, (int)data
[0]);
2855 authentication_failed(vs
);
2856 } else { /* Accept requested auth */
2857 trace_vnc_auth_start(vs
, vs
->auth
);
2860 if (vs
->minor
>= 8) {
2861 vnc_write_u32(vs
, 0); /* Accept auth completion */
2864 trace_vnc_auth_pass(vs
, vs
->auth
);
2865 start_client_init(vs
);
2872 case VNC_AUTH_VENCRYPT
:
2873 start_auth_vencrypt(vs
);
2876 #ifdef CONFIG_VNC_SASL
2878 start_auth_sasl(vs
);
2880 #endif /* CONFIG_VNC_SASL */
2882 default: /* Should not be possible, but just in case */
2883 trace_vnc_auth_fail(vs
, vs
->auth
, "Unhandled auth method", "");
2884 authentication_failed(vs
);
2890 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2894 memcpy(local
, version
, 12);
2897 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2898 VNC_DEBUG("Malformed protocol version %s\n", local
);
2899 vnc_client_error(vs
);
2902 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2903 if (vs
->major
!= 3 ||
2909 VNC_DEBUG("Unsupported client version\n");
2910 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2912 vnc_client_error(vs
);
2915 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2916 * as equivalent to v3.3 by servers
2918 if (vs
->minor
== 4 || vs
->minor
== 5)
2921 if (vs
->minor
== 3) {
2922 trace_vnc_auth_start(vs
, vs
->auth
);
2923 if (vs
->auth
== VNC_AUTH_NONE
) {
2924 vnc_write_u32(vs
, vs
->auth
);
2926 trace_vnc_auth_pass(vs
, vs
->auth
);
2927 start_client_init(vs
);
2928 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2929 VNC_DEBUG("Tell client VNC auth\n");
2930 vnc_write_u32(vs
, vs
->auth
);
2934 trace_vnc_auth_fail(vs
, vs
->auth
,
2935 "Unsupported auth method for v3.3", "");
2936 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2938 vnc_client_error(vs
);
2941 vnc_write_u8(vs
, 1); /* num auth */
2942 vnc_write_u8(vs
, vs
->auth
);
2943 vnc_read_when(vs
, protocol_client_auth
, 1);
2950 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2952 struct VncSurface
*vs
= &vd
->guest
;
2954 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2957 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2961 w
= (x
+ w
) / VNC_STAT_RECT
;
2962 h
= (y
+ h
) / VNC_STAT_RECT
;
2966 for (j
= y
; j
<= h
; j
++) {
2967 for (i
= x
; i
<= w
; i
++) {
2968 vs
->lossy_rect
[j
][i
] = 1;
2973 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2976 int sty
= y
/ VNC_STAT_RECT
;
2977 int stx
= x
/ VNC_STAT_RECT
;
2980 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2981 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2983 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2986 /* kernel send buffers are full -> refresh later */
2987 if (vs
->output
.offset
) {
2991 if (!vs
->lossy_rect
[sty
][stx
]) {
2995 vs
->lossy_rect
[sty
][stx
] = 0;
2996 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2997 bitmap_set(vs
->dirty
[y
+ j
],
2998 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2999 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
3007 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
3009 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
3010 pixman_image_get_width(vd
->server
));
3011 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
3012 pixman_image_get_height(vd
->server
));
3017 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
3018 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
3019 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
3021 rect
->updated
= false;
3025 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
3027 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
3030 vd
->guest
.last_freq_check
= *tv
;
3032 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
3033 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
3034 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
3035 int count
= ARRAY_SIZE(rect
->times
);
3036 struct timeval min
, max
;
3038 if (!timerisset(&rect
->times
[count
- 1])) {
3042 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
3043 qemu_timersub(tv
, &max
, &res
);
3045 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
3047 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
3048 memset(rect
->times
, 0, sizeof (rect
->times
));
3052 min
= rect
->times
[rect
->idx
];
3053 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
3054 qemu_timersub(&max
, &min
, &res
);
3056 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
3057 rect
->freq
/= count
;
3058 rect
->freq
= 1. / rect
->freq
;
3064 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
3070 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
3071 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
3073 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
3074 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
3075 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
3087 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
3091 rect
= vnc_stat_rect(vd
, x
, y
);
3092 if (rect
->updated
) {
3095 rect
->times
[rect
->idx
] = *tv
;
3096 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
3097 rect
->updated
= true;
3100 static int vnc_refresh_server_surface(VncDisplay
*vd
)
3102 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
3103 pixman_image_get_width(vd
->server
));
3104 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
3105 pixman_image_get_height(vd
->server
));
3106 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
3107 uint8_t *guest_row0
= NULL
, *server_row0
;
3110 pixman_image_t
*tmpbuf
= NULL
;
3111 unsigned long offset
;
3113 uint8_t *guest_ptr
, *server_ptr
;
3115 struct timeval tv
= { 0, 0 };
3117 if (!vd
->non_adaptive
) {
3118 gettimeofday(&tv
, NULL
);
3119 has_dirty
= vnc_update_stats(vd
, &tv
);
3122 offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
3123 height
* VNC_DIRTY_BPL(&vd
->guest
), 0);
3124 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
3125 /* no dirty bits in guest surface */
3130 * Walk through the guest dirty map.
3131 * Check and copy modified bits from guest to server surface.
3132 * Update server dirty map.
3134 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
3135 server_stride
= guest_stride
= guest_ll
=
3136 pixman_image_get_stride(vd
->server
);
3137 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
3139 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
3140 int w
= pixman_image_get_width(vd
->server
);
3141 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, w
);
3144 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
3145 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
3146 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
3147 guest_ll
= pixman_image_get_width(vd
->guest
.fb
)
3148 * DIV_ROUND_UP(guest_bpp
, 8);
3150 line_bytes
= MIN(server_stride
, guest_ll
);
3153 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
3154 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
3156 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
3158 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
3159 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
3160 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
3162 guest_ptr
= guest_row0
+ y
* guest_stride
;
3164 guest_ptr
+= x
* cmp_bytes
;
3166 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
3167 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
3168 int _cmp_bytes
= cmp_bytes
;
3169 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
3172 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
3173 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
3175 assert(_cmp_bytes
>= 0);
3176 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
3179 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
3180 if (!vd
->non_adaptive
) {
3181 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
3184 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3185 set_bit(x
, vs
->dirty
[y
]);
3191 offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
3192 height
* VNC_DIRTY_BPL(&vd
->guest
),
3193 y
* VNC_DIRTY_BPL(&vd
->guest
));
3194 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
3195 /* no more dirty bits */
3199 qemu_pixman_image_unref(tmpbuf
);
3203 static void vnc_refresh(DisplayChangeListener
*dcl
)
3205 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
3207 int has_dirty
, rects
= 0;
3209 if (QTAILQ_EMPTY(&vd
->clients
)) {
3210 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
3214 graphic_hw_update(vd
->dcl
.con
);
3216 if (vnc_trylock_display(vd
)) {
3217 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3221 has_dirty
= vnc_refresh_server_surface(vd
);
3222 vnc_unlock_display(vd
);
3224 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
3225 rects
+= vnc_update_client(vs
, has_dirty
);
3226 /* vs might be free()ed here */
3229 if (has_dirty
&& rects
) {
3230 vd
->dcl
.update_interval
/= 2;
3231 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
3232 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
3235 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
3236 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
3237 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
3242 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
3243 bool skipauth
, bool websocket
)
3245 VncState
*vs
= g_new0(VncState
, 1);
3246 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
3249 trace_vnc_client_connect(vs
, sioc
);
3250 vs
->zrle
= g_new0(VncZrle
, 1);
3251 vs
->tight
= g_new0(VncTight
, 1);
3252 vs
->magic
= VNC_MAGIC
;
3254 object_ref(OBJECT(vs
->sioc
));
3255 vs
->ioc
= QIO_CHANNEL(sioc
);
3256 object_ref(OBJECT(vs
->ioc
));
3259 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
3260 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
3261 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
3263 buffer_init(&vs
->tight
->tight
, "vnc-tight/%p", sioc
);
3264 buffer_init(&vs
->tight
->zlib
, "vnc-tight-zlib/%p", sioc
);
3265 buffer_init(&vs
->tight
->gradient
, "vnc-tight-gradient/%p", sioc
);
3266 #ifdef CONFIG_VNC_JPEG
3267 buffer_init(&vs
->tight
->jpeg
, "vnc-tight-jpeg/%p", sioc
);
3270 buffer_init(&vs
->tight
->png
, "vnc-tight-png/%p", sioc
);
3272 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3273 buffer_init(&vs
->zrle
->zrle
, "vnc-zrle/%p", sioc
);
3274 buffer_init(&vs
->zrle
->fb
, "vnc-zrle-fb/%p", sioc
);
3275 buffer_init(&vs
->zrle
->zlib
, "vnc-zrle-zlib/%p", sioc
);
3278 vs
->auth
= VNC_AUTH_NONE
;
3279 vs
->subauth
= VNC_AUTH_INVALID
;
3282 vs
->auth
= vd
->ws_auth
;
3283 vs
->subauth
= VNC_AUTH_INVALID
;
3285 vs
->auth
= vd
->auth
;
3286 vs
->subauth
= vd
->subauth
;
3289 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3290 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3292 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3293 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3294 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3297 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3298 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3299 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3301 g_source_remove(vs
->ioc_tag
);
3306 vs
->ioc_tag
= qio_channel_add_watch(
3307 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3308 vncws_tls_handshake_io
, vs
, NULL
);
3310 vs
->ioc_tag
= qio_channel_add_watch(
3311 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3312 vncws_handshake_io
, vs
, NULL
);
3315 vs
->ioc_tag
= qio_channel_add_watch(
3316 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3317 vnc_client_io
, vs
, NULL
);
3320 vnc_client_cache_addr(vs
);
3321 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3322 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3327 vs
->as
.freq
= 44100;
3328 vs
->as
.nchannels
= 2;
3329 vs
->as
.fmt
= AUDIO_FORMAT_S16
;
3330 vs
->as
.endianness
= 0;
3332 qemu_mutex_init(&vs
->output_mutex
);
3333 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3335 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3337 vnc_update_server_surface(vd
);
3340 graphic_hw_update(vd
->dcl
.con
);
3342 if (!vs
->websocket
) {
3343 vnc_start_protocol(vs
);
3346 if (vd
->num_connecting
> vd
->connections_limit
) {
3347 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3348 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3349 vnc_disconnect_start(vs
);
3356 void vnc_start_protocol(VncState
*vs
)
3358 vnc_write(vs
, "RFB 003.008\n", 12);
3360 vnc_read_when(vs
, protocol_version
, 12);
3362 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3363 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3366 static void vnc_listen_io(QIONetListener
*listener
,
3367 QIOChannelSocket
*cioc
,
3370 VncDisplay
*vd
= opaque
;
3371 bool isWebsock
= listener
== vd
->wslistener
;
3373 qio_channel_set_name(QIO_CHANNEL(cioc
),
3374 isWebsock
? "vnc-ws-server" : "vnc-server");
3375 qio_channel_set_delay(QIO_CHANNEL(cioc
), false);
3376 vnc_connect(vd
, cioc
, false, isWebsock
);
3379 static const DisplayChangeListenerOps dcl_ops
= {
3381 .dpy_refresh
= vnc_refresh
,
3382 .dpy_gfx_update
= vnc_dpy_update
,
3383 .dpy_gfx_switch
= vnc_dpy_switch
,
3384 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3385 .dpy_mouse_set
= vnc_mouse_set
,
3386 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3389 void vnc_display_init(const char *id
, Error
**errp
)
3393 if (vnc_display_find(id
) != NULL
) {
3396 vd
= g_malloc0(sizeof(*vd
));
3398 vd
->id
= strdup(id
);
3399 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3401 QTAILQ_INIT(&vd
->clients
);
3402 vd
->expires
= TIME_MAX
;
3404 if (keyboard_layout
) {
3405 trace_vnc_key_map_init(keyboard_layout
);
3406 vd
->kbd_layout
= init_keyboard_layout(name2keysym
,
3407 keyboard_layout
, errp
);
3409 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us", errp
);
3412 if (!vd
->kbd_layout
) {
3416 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3417 vd
->connections_limit
= 32;
3419 qemu_mutex_init(&vd
->mutex
);
3420 vnc_start_worker_thread();
3422 vd
->dcl
.ops
= &dcl_ops
;
3423 register_displaychangelistener(&vd
->dcl
);
3424 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
3428 static void vnc_display_close(VncDisplay
*vd
)
3433 vd
->is_unix
= false;
3436 qio_net_listener_disconnect(vd
->listener
);
3437 object_unref(OBJECT(vd
->listener
));
3439 vd
->listener
= NULL
;
3441 if (vd
->wslistener
) {
3442 qio_net_listener_disconnect(vd
->wslistener
);
3443 object_unref(OBJECT(vd
->wslistener
));
3445 vd
->wslistener
= NULL
;
3447 vd
->auth
= VNC_AUTH_INVALID
;
3448 vd
->subauth
= VNC_AUTH_INVALID
;
3450 object_unref(OBJECT(vd
->tlscreds
));
3451 vd
->tlscreds
= NULL
;
3454 object_unparent(OBJECT(vd
->tlsauthz
));
3455 vd
->tlsauthz
= NULL
;
3457 g_free(vd
->tlsauthzid
);
3458 vd
->tlsauthzid
= NULL
;
3459 if (vd
->lock_key_sync
) {
3460 qemu_remove_led_event_handler(vd
->led
);
3463 #ifdef CONFIG_VNC_SASL
3464 if (vd
->sasl
.authz
) {
3465 object_unparent(OBJECT(vd
->sasl
.authz
));
3466 vd
->sasl
.authz
= NULL
;
3468 g_free(vd
->sasl
.authzid
);
3469 vd
->sasl
.authzid
= NULL
;
3473 int vnc_display_password(const char *id
, const char *password
)
3475 VncDisplay
*vd
= vnc_display_find(id
);
3480 if (vd
->auth
== VNC_AUTH_NONE
) {
3481 error_printf_unless_qmp("If you want use passwords please enable "
3482 "password auth using '-vnc ${dpy},password'.\n");
3486 g_free(vd
->password
);
3487 vd
->password
= g_strdup(password
);
3492 int vnc_display_pw_expire(const char *id
, time_t expires
)
3494 VncDisplay
*vd
= vnc_display_find(id
);
3500 vd
->expires
= expires
;
3504 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3506 SocketAddress
*addr
;
3508 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
3512 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0], NULL
);
3517 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
3518 qapi_free_SocketAddress(addr
);
3521 error_printf_unless_qmp("VNC server running on %s:%s\n",
3524 qapi_free_SocketAddress(addr
);
3527 static QemuOptsList qemu_vnc_opts
= {
3529 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3530 .implied_opt_name
= "vnc",
3534 .type
= QEMU_OPT_STRING
,
3536 .name
= "websocket",
3537 .type
= QEMU_OPT_STRING
,
3539 .name
= "tls-creds",
3540 .type
= QEMU_OPT_STRING
,
3543 .type
= QEMU_OPT_STRING
,
3546 .type
= QEMU_OPT_STRING
,
3549 .type
= QEMU_OPT_NUMBER
,
3551 .name
= "connections",
3552 .type
= QEMU_OPT_NUMBER
,
3555 .type
= QEMU_OPT_NUMBER
,
3558 .type
= QEMU_OPT_BOOL
,
3561 .type
= QEMU_OPT_BOOL
,
3564 .type
= QEMU_OPT_BOOL
,
3566 .name
= "password-secret",
3567 .type
= QEMU_OPT_STRING
,
3570 .type
= QEMU_OPT_BOOL
,
3572 .name
= "lock-key-sync",
3573 .type
= QEMU_OPT_BOOL
,
3575 .name
= "key-delay-ms",
3576 .type
= QEMU_OPT_NUMBER
,
3579 .type
= QEMU_OPT_BOOL
,
3581 .name
= "tls-authz",
3582 .type
= QEMU_OPT_STRING
,
3584 .name
= "sasl-authz",
3585 .type
= QEMU_OPT_STRING
,
3588 .type
= QEMU_OPT_BOOL
,
3590 .name
= "non-adaptive",
3591 .type
= QEMU_OPT_BOOL
,
3594 .type
= QEMU_OPT_STRING
,
3596 .name
= "power-control",
3597 .type
= QEMU_OPT_BOOL
,
3599 { /* end of list */ }
3605 vnc_display_setup_auth(int *auth
,
3607 QCryptoTLSCreds
*tlscreds
,
3614 * We have a choice of 3 authentication options
3620 * The channel can be run in 2 modes
3625 * And TLS can use 2 types of credentials
3630 * We thus have 9 possible logical combinations
3635 * 4. tls + anon + none
3636 * 5. tls + anon + vnc
3637 * 6. tls + anon + sasl
3638 * 7. tls + x509 + none
3639 * 8. tls + x509 + vnc
3640 * 9. tls + x509 + sasl
3642 * These need to be mapped into the VNC auth schemes
3643 * in an appropriate manner. In regular VNC, all the
3644 * TLS options get mapped into VNC_AUTH_VENCRYPT
3647 * In websockets, the https:// protocol already provides
3648 * TLS support, so there is no need to make use of the
3649 * VeNCrypt extension. Furthermore, websockets browser
3650 * clients could not use VeNCrypt even if they wanted to,
3651 * as they cannot control when the TLS handshake takes
3652 * place. Thus there is no option but to rely on https://,
3653 * meaning combinations 4->6 and 7->9 will be mapped to
3654 * VNC auth schemes in the same way as combos 1->3.
3656 * Regardless of fact that we have a different mapping to
3657 * VNC auth mechs for plain VNC vs websockets VNC, the end
3658 * result has the same security characteristics.
3660 if (websocket
|| !tlscreds
) {
3662 VNC_DEBUG("Initializing VNC server with password auth\n");
3663 *auth
= VNC_AUTH_VNC
;
3665 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3666 *auth
= VNC_AUTH_SASL
;
3668 VNC_DEBUG("Initializing VNC server with no auth\n");
3669 *auth
= VNC_AUTH_NONE
;
3671 *subauth
= VNC_AUTH_INVALID
;
3673 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3674 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3675 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3676 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3678 if (!is_x509
&& !is_anon
) {
3680 "Unsupported TLS cred type %s",
3681 object_get_typename(OBJECT(tlscreds
)));
3684 *auth
= VNC_AUTH_VENCRYPT
;
3687 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3688 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3690 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3691 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3696 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3697 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3699 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3700 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3704 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3705 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3707 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3708 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3716 static int vnc_display_get_address(const char *addrstr
,
3725 SocketAddress
**retaddr
,
3729 SocketAddress
*addr
= NULL
;
3731 addr
= g_new0(SocketAddress
, 1);
3733 if (strncmp(addrstr
, "unix:", 5) == 0) {
3734 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
3735 addr
->u
.q_unix
.path
= g_strdup(addrstr
+ 5);
3738 error_setg(errp
, "Port range not support with UNIX socket");
3745 uint64_t baseport
= 0;
3746 InetSocketAddress
*inet
;
3748 port
= strrchr(addrstr
, ':');
3754 error_setg(errp
, "no vnc port specified");
3758 hostlen
= port
- addrstr
;
3760 if (*port
== '\0') {
3761 error_setg(errp
, "vnc port cannot be empty");
3766 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
3767 inet
= &addr
->u
.inet
;
3768 if (hostlen
&& addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3769 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3771 inet
->host
= g_strndup(addrstr
, hostlen
);
3773 /* plain VNC port is just an offset, for websocket
3774 * port is absolute */
3776 if (g_str_equal(addrstr
, "") ||
3777 g_str_equal(addrstr
, "on")) {
3778 if (displaynum
== -1) {
3779 error_setg(errp
, "explicit websocket port is required");
3782 inet
->port
= g_strdup_printf(
3783 "%d", displaynum
+ 5700);
3785 inet
->has_to
= true;
3786 inet
->to
= to
+ 5700;
3789 inet
->port
= g_strdup(port
);
3792 int offset
= reverse
? 0 : 5900;
3793 if (parse_uint_full(port
, 10, &baseport
) < 0) {
3794 error_setg(errp
, "can't convert to a number: %s", port
);
3797 if (baseport
> 65535 ||
3798 baseport
+ offset
> 65535) {
3799 error_setg(errp
, "port %s out of range", port
);
3802 inet
->port
= g_strdup_printf(
3803 "%d", (int)baseport
+ offset
);
3806 inet
->has_to
= true;
3807 inet
->to
= to
+ offset
;
3812 inet
->has_ipv4
= has_ipv4
;
3814 inet
->has_ipv6
= has_ipv6
;
3823 qapi_free_SocketAddress(addr
);
3828 static int vnc_display_get_addresses(QemuOpts
*opts
,
3830 SocketAddressList
**saddr_list_ret
,
3831 SocketAddressList
**wsaddr_list_ret
,
3834 SocketAddress
*saddr
= NULL
;
3835 SocketAddress
*wsaddr
= NULL
;
3836 g_autoptr(SocketAddressList
) saddr_list
= NULL
;
3837 SocketAddressList
**saddr_tail
= &saddr_list
;
3838 SocketAddress
*single_saddr
= NULL
;
3839 g_autoptr(SocketAddressList
) wsaddr_list
= NULL
;
3840 SocketAddressList
**wsaddr_tail
= &wsaddr_list
;
3841 QemuOptsIter addriter
;
3843 int to
= qemu_opt_get_number(opts
, "to", 0);
3844 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3845 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3846 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3847 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3848 int displaynum
= -1;
3850 addr
= qemu_opt_get(opts
, "vnc");
3851 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3854 if (qemu_opt_get(opts
, "websocket") &&
3855 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3857 "SHA1 hash support is required for websockets");
3861 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3862 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3864 rv
= vnc_display_get_address(addr
, false, reverse
, 0, to
,
3871 /* Historical compat - first listen address can be used
3872 * to set the default websocket port
3874 if (displaynum
== -1) {
3877 QAPI_LIST_APPEND(saddr_tail
, saddr
);
3880 if (saddr_list
&& !saddr_list
->next
) {
3881 single_saddr
= saddr_list
->value
;
3884 * If we had multiple primary displays, we don't do defaults
3885 * for websocket, and require explicit config instead.
3890 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3891 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3892 if (vnc_display_get_address(addr
, true, reverse
, displaynum
, to
,
3895 &wsaddr
, errp
) < 0) {
3899 /* Historical compat - if only a single listen address was
3900 * provided, then this is used to set the default listen
3901 * address for websocket too
3904 single_saddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3905 wsaddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3906 g_str_equal(wsaddr
->u
.inet
.host
, "") &&
3907 !g_str_equal(single_saddr
->u
.inet
.host
, "")) {
3908 g_free(wsaddr
->u
.inet
.host
);
3909 wsaddr
->u
.inet
.host
= g_strdup(single_saddr
->u
.inet
.host
);
3912 QAPI_LIST_APPEND(wsaddr_tail
, wsaddr
);
3915 *saddr_list_ret
= g_steal_pointer(&saddr_list
);
3916 *wsaddr_list_ret
= g_steal_pointer(&wsaddr_list
);
3920 static int vnc_display_connect(VncDisplay
*vd
,
3921 SocketAddressList
*saddr_list
,
3922 SocketAddressList
*wsaddr_list
,
3925 /* connect to viewer */
3926 QIOChannelSocket
*sioc
= NULL
;
3928 error_setg(errp
, "Cannot use websockets in reverse mode");
3931 if (!saddr_list
|| saddr_list
->next
) {
3932 error_setg(errp
, "Expected a single address in reverse mode");
3935 /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3936 vd
->is_unix
= saddr_list
->value
->type
== SOCKET_ADDRESS_TYPE_UNIX
;
3937 sioc
= qio_channel_socket_new();
3938 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3939 if (qio_channel_socket_connect_sync(sioc
, saddr_list
->value
, errp
) < 0) {
3940 object_unref(OBJECT(sioc
));
3943 vnc_connect(vd
, sioc
, false, false);
3944 object_unref(OBJECT(sioc
));
3949 static int vnc_display_listen(VncDisplay
*vd
,
3950 SocketAddressList
*saddr_list
,
3951 SocketAddressList
*wsaddr_list
,
3954 SocketAddressList
*el
;
3957 vd
->listener
= qio_net_listener_new();
3958 qio_net_listener_set_name(vd
->listener
, "vnc-listen");
3959 for (el
= saddr_list
; el
; el
= el
->next
) {
3960 if (qio_net_listener_open_sync(vd
->listener
,
3967 qio_net_listener_set_client_func(vd
->listener
,
3968 vnc_listen_io
, vd
, NULL
);
3972 vd
->wslistener
= qio_net_listener_new();
3973 qio_net_listener_set_name(vd
->wslistener
, "vnc-ws-listen");
3974 for (el
= wsaddr_list
; el
; el
= el
->next
) {
3975 if (qio_net_listener_open_sync(vd
->wslistener
,
3982 qio_net_listener_set_client_func(vd
->wslistener
,
3983 vnc_listen_io
, vd
, NULL
);
3989 bool vnc_display_update(DisplayUpdateOptionsVNC
*arg
, Error
**errp
)
3991 VncDisplay
*vd
= vnc_display_find(NULL
);
3994 error_setg(errp
, "Can not find vnc display");
3998 if (arg
->has_addresses
) {
4000 qio_net_listener_disconnect(vd
->listener
);
4001 object_unref(OBJECT(vd
->listener
));
4002 vd
->listener
= NULL
;
4005 if (vnc_display_listen(vd
, arg
->addresses
, NULL
, errp
) < 0) {
4013 void vnc_display_open(const char *id
, Error
**errp
)
4015 VncDisplay
*vd
= vnc_display_find(id
);
4016 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
4017 g_autoptr(SocketAddressList
) saddr_list
= NULL
;
4018 g_autoptr(SocketAddressList
) wsaddr_list
= NULL
;
4019 const char *share
, *device_id
;
4021 bool password
= false;
4022 bool reverse
= false;
4025 const char *tlsauthz
;
4026 const char *saslauthz
;
4027 int lock_key_sync
= 1;
4029 const char *audiodev
;
4030 const char *passwordSecret
;
4033 error_setg(errp
, "VNC display not active");
4036 vnc_display_close(vd
);
4042 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
4043 if (vnc_display_get_addresses(opts
, reverse
, &saddr_list
, &wsaddr_list
,
4049 passwordSecret
= qemu_opt_get(opts
, "password-secret");
4050 if (passwordSecret
) {
4051 if (qemu_opt_get(opts
, "password")) {
4053 "'password' flag is redundant with 'password-secret'");
4056 vd
->password
= qcrypto_secret_lookup_as_utf8(passwordSecret
,
4058 if (!vd
->password
) {
4063 password
= qemu_opt_get_bool(opts
, "password", false);
4066 if (!qcrypto_cipher_supports(
4067 QCRYPTO_CIPHER_ALG_DES
, QCRYPTO_CIPHER_MODE_ECB
)) {
4069 "Cipher backend does not support DES algorithm");
4074 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
4075 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 10);
4076 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
4077 #ifndef CONFIG_VNC_SASL
4079 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
4082 #endif /* CONFIG_VNC_SASL */
4083 credid
= qemu_opt_get(opts
, "tls-creds");
4086 creds
= object_resolve_path_component(
4087 object_get_objects_root(), credid
);
4089 error_setg(errp
, "No TLS credentials with id '%s'",
4093 vd
->tlscreds
= (QCryptoTLSCreds
*)
4094 object_dynamic_cast(creds
,
4095 TYPE_QCRYPTO_TLS_CREDS
);
4096 if (!vd
->tlscreds
) {
4097 error_setg(errp
, "Object with id '%s' is not TLS credentials",
4101 object_ref(OBJECT(vd
->tlscreds
));
4103 if (!qcrypto_tls_creds_check_endpoint(vd
->tlscreds
,
4104 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
4109 tlsauthz
= qemu_opt_get(opts
, "tls-authz");
4110 if (tlsauthz
&& !vd
->tlscreds
) {
4111 error_setg(errp
, "'tls-authz' provided but TLS is not enabled");
4115 saslauthz
= qemu_opt_get(opts
, "sasl-authz");
4116 if (saslauthz
&& !sasl
) {
4117 error_setg(errp
, "'sasl-authz' provided but SASL auth is not enabled");
4121 share
= qemu_opt_get(opts
, "share");
4123 if (strcmp(share
, "ignore") == 0) {
4124 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
4125 } else if (strcmp(share
, "allow-exclusive") == 0) {
4126 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4127 } else if (strcmp(share
, "force-shared") == 0) {
4128 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
4130 error_setg(errp
, "unknown vnc share= option");
4134 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4136 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
4138 #ifdef CONFIG_VNC_JPEG
4139 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
4141 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
4142 /* adaptive updates are only used with tight encoding and
4143 * if lossy updates are enabled so we can disable all the
4144 * calculations otherwise */
4146 vd
->non_adaptive
= true;
4149 vd
->power_control
= qemu_opt_get_bool(opts
, "power-control", false);
4152 vd
->tlsauthzid
= g_strdup(tlsauthz
);
4154 #ifdef CONFIG_VNC_SASL
4157 vd
->sasl
.authzid
= g_strdup(saslauthz
);
4162 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
4163 vd
->tlscreds
, password
,
4164 sasl
, false, errp
) < 0) {
4167 trace_vnc_auth_init(vd
, 0, vd
->auth
, vd
->subauth
);
4169 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
4170 vd
->tlscreds
, password
,
4171 sasl
, true, errp
) < 0) {
4174 trace_vnc_auth_init(vd
, 1, vd
->ws_auth
, vd
->ws_subauth
);
4176 #ifdef CONFIG_VNC_SASL
4177 if (sasl
&& !vnc_sasl_server_init(errp
)) {
4181 vd
->lock_key_sync
= lock_key_sync
;
4182 if (lock_key_sync
) {
4183 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
4187 audiodev
= qemu_opt_get(opts
, "audiodev");
4189 vd
->audio_state
= audio_state_by_name(audiodev
, errp
);
4190 if (!vd
->audio_state
) {
4194 vd
->audio_state
= audio_get_default_audio_state(NULL
);
4197 device_id
= qemu_opt_get(opts
, "display");
4199 int head
= qemu_opt_get_number(opts
, "head", 0);
4202 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
4204 error_propagate(errp
, err
);
4208 con
= qemu_console_lookup_default();
4211 if (con
!= vd
->dcl
.con
) {
4212 qkbd_state_free(vd
->kbd
);
4213 unregister_displaychangelistener(&vd
->dcl
);
4215 register_displaychangelistener(&vd
->dcl
);
4216 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
4218 qkbd_state_set_delay(vd
->kbd
, key_delay_ms
);
4220 if (saddr_list
== NULL
) {
4225 if (vnc_display_connect(vd
, saddr_list
, wsaddr_list
, errp
) < 0) {
4229 if (vnc_display_listen(vd
, saddr_list
, wsaddr_list
, errp
) < 0) {
4234 if (qemu_opt_get(opts
, "to")) {
4235 vnc_display_print_local_addr(vd
);
4242 vnc_display_close(vd
);
4245 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
4247 VncDisplay
*vd
= vnc_display_find(id
);
4248 QIOChannelSocket
*sioc
;
4254 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4256 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4257 vnc_connect(vd
, sioc
, skipauth
, false);
4258 object_unref(OBJECT(sioc
));
4262 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4267 id
= g_strdup("default");
4268 while (qemu_opts_find(olist
, id
)) {
4270 id
= g_strdup_printf("vnc%d", i
++);
4272 qemu_opts_set_id(opts
, id
);
4275 void vnc_parse(const char *str
)
4277 QemuOptsList
*olist
= qemu_find_opts("vnc");
4278 QemuOpts
*opts
= qemu_opts_parse_noisily(olist
, str
, !is_help_option(str
));
4285 id
= qemu_opts_id(opts
);
4287 /* auto-assign id if not present */
4288 vnc_auto_assign_id(olist
, opts
);
4292 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4294 Error
*local_err
= NULL
;
4295 char *id
= (char *)qemu_opts_id(opts
);
4298 vnc_display_init(id
, &local_err
);
4300 error_propagate(errp
, local_err
);
4303 vnc_display_open(id
, &local_err
);
4304 if (local_err
!= NULL
) {
4305 error_propagate(errp
, local_err
);
4311 static void vnc_register_config(void)
4313 qemu_add_opts(&qemu_vnc_opts
);
4315 opts_init(vnc_register_config
);