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/tlscredsanon.h"
49 #include "crypto/tlscredsx509.h"
50 #include "crypto/random.h"
51 #include "crypto/secret_common.h"
52 #include "qom/object_interfaces.h"
53 #include "qemu/cutils.h"
54 #include "qemu/help_option.h"
55 #include "io/dns-resolver.h"
57 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
58 #define VNC_REFRESH_INTERVAL_INC 50
59 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
60 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
61 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
63 #include "vnc_keysym.h"
64 #include "crypto/cipher.h"
66 static QTAILQ_HEAD(, VncDisplay
) vnc_displays
=
67 QTAILQ_HEAD_INITIALIZER(vnc_displays
);
69 static int vnc_cursor_define(VncState
*vs
);
70 static void vnc_update_throttle_offset(VncState
*vs
);
72 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
75 static const char *mn
[] = {
77 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
78 [VNC_SHARE_MODE_SHARED
] = "shared",
79 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
80 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
82 fprintf(stderr
, "%s/%p: %s -> %s\n", __func__
,
83 vs
->ioc
, mn
[vs
->share_mode
], mn
[mode
]);
86 switch (vs
->share_mode
) {
87 case VNC_SHARE_MODE_CONNECTING
:
88 vs
->vd
->num_connecting
--;
90 case VNC_SHARE_MODE_SHARED
:
93 case VNC_SHARE_MODE_EXCLUSIVE
:
94 vs
->vd
->num_exclusive
--;
100 vs
->share_mode
= mode
;
102 switch (vs
->share_mode
) {
103 case VNC_SHARE_MODE_CONNECTING
:
104 vs
->vd
->num_connecting
++;
106 case VNC_SHARE_MODE_SHARED
:
107 vs
->vd
->num_shared
++;
109 case VNC_SHARE_MODE_EXCLUSIVE
:
110 vs
->vd
->num_exclusive
++;
118 static void vnc_init_basic_info(SocketAddress
*addr
,
122 switch (addr
->type
) {
123 case SOCKET_ADDRESS_TYPE_INET
:
124 info
->host
= g_strdup(addr
->u
.inet
.host
);
125 info
->service
= g_strdup(addr
->u
.inet
.port
);
126 if (addr
->u
.inet
.ipv6
) {
127 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
129 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
133 case SOCKET_ADDRESS_TYPE_UNIX
:
134 info
->host
= g_strdup("");
135 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
136 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
139 case SOCKET_ADDRESS_TYPE_VSOCK
:
140 case SOCKET_ADDRESS_TYPE_FD
:
141 error_setg(errp
, "Unsupported socket address type %s",
142 SocketAddressType_str(addr
->type
));
151 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
155 SocketAddress
*addr
= NULL
;
158 error_setg(errp
, "No listener socket available");
162 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
167 vnc_init_basic_info(addr
, info
, errp
);
168 qapi_free_SocketAddress(addr
);
171 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
175 SocketAddress
*addr
= NULL
;
177 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
182 vnc_init_basic_info(addr
, info
, errp
);
183 qapi_free_SocketAddress(addr
);
186 static const char *vnc_auth_name(VncDisplay
*vd
) {
188 case VNC_AUTH_INVALID
:
204 case VNC_AUTH_VENCRYPT
:
205 switch (vd
->subauth
) {
206 case VNC_AUTH_VENCRYPT_PLAIN
:
207 return "vencrypt+plain";
208 case VNC_AUTH_VENCRYPT_TLSNONE
:
209 return "vencrypt+tls+none";
210 case VNC_AUTH_VENCRYPT_TLSVNC
:
211 return "vencrypt+tls+vnc";
212 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
213 return "vencrypt+tls+plain";
214 case VNC_AUTH_VENCRYPT_X509NONE
:
215 return "vencrypt+x509+none";
216 case VNC_AUTH_VENCRYPT_X509VNC
:
217 return "vencrypt+x509+vnc";
218 case VNC_AUTH_VENCRYPT_X509PLAIN
:
219 return "vencrypt+x509+plain";
220 case VNC_AUTH_VENCRYPT_TLSSASL
:
221 return "vencrypt+tls+sasl";
222 case VNC_AUTH_VENCRYPT_X509SASL
:
223 return "vencrypt+x509+sasl";
233 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
238 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
242 info
= g_malloc0(sizeof(*info
));
243 vnc_init_basic_info_from_server_addr(vd
->listener
->sioc
[0],
244 qapi_VncServerInfo_base(info
), &err
);
245 info
->has_auth
= true;
246 info
->auth
= g_strdup(vnc_auth_name(vd
));
248 qapi_free_VncServerInfo(info
);
255 static void vnc_client_cache_auth(VncState
*client
)
262 client
->info
->x509_dname
=
263 qcrypto_tls_session_get_peer_name(client
->tls
);
264 client
->info
->has_x509_dname
=
265 client
->info
->x509_dname
!= NULL
;
267 #ifdef CONFIG_VNC_SASL
268 if (client
->sasl
.conn
&&
269 client
->sasl
.username
) {
270 client
->info
->has_sasl_username
= true;
271 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
276 static void vnc_client_cache_addr(VncState
*client
)
280 client
->info
= g_malloc0(sizeof(*client
->info
));
281 vnc_init_basic_info_from_remote_addr(client
->sioc
,
282 qapi_VncClientInfo_base(client
->info
),
284 client
->info
->websocket
= client
->websocket
;
286 qapi_free_VncClientInfo(client
->info
);
292 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
300 si
= vnc_server_info_get(vs
->vd
);
306 case QAPI_EVENT_VNC_CONNECTED
:
307 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
));
309 case QAPI_EVENT_VNC_INITIALIZED
:
310 qapi_event_send_vnc_initialized(si
, vs
->info
);
312 case QAPI_EVENT_VNC_DISCONNECTED
:
313 qapi_event_send_vnc_disconnected(si
, vs
->info
);
319 qapi_free_VncServerInfo(si
);
322 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
327 info
= g_malloc0(sizeof(*info
));
329 vnc_init_basic_info_from_remote_addr(client
->sioc
,
330 qapi_VncClientInfo_base(info
),
334 qapi_free_VncClientInfo(info
);
338 info
->websocket
= client
->websocket
;
341 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
342 info
->has_x509_dname
= info
->x509_dname
!= NULL
;
344 #ifdef CONFIG_VNC_SASL
345 if (client
->sasl
.conn
&& client
->sasl
.username
) {
346 info
->has_sasl_username
= true;
347 info
->sasl_username
= g_strdup(client
->sasl
.username
);
354 static VncDisplay
*vnc_display_find(const char *id
)
359 return QTAILQ_FIRST(&vnc_displays
);
361 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
362 if (strcmp(id
, vd
->id
) == 0) {
369 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
371 VncClientInfoList
*prev
= NULL
;
374 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
375 QAPI_LIST_PREPEND(prev
, qmp_query_vnc_client(client
));
380 VncInfo
*qmp_query_vnc(Error
**errp
)
382 VncInfo
*info
= g_malloc0(sizeof(*info
));
383 VncDisplay
*vd
= vnc_display_find(NULL
);
384 SocketAddress
*addr
= NULL
;
386 if (vd
== NULL
|| !vd
->listener
|| !vd
->listener
->nsioc
) {
387 info
->enabled
= false;
389 info
->enabled
= true;
391 /* for compatibility with the original command */
392 info
->has_clients
= true;
393 info
->clients
= qmp_query_client_list(vd
);
395 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0],
401 switch (addr
->type
) {
402 case SOCKET_ADDRESS_TYPE_INET
:
403 info
->host
= g_strdup(addr
->u
.inet
.host
);
404 info
->service
= g_strdup(addr
->u
.inet
.port
);
405 if (addr
->u
.inet
.ipv6
) {
406 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
408 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
412 case SOCKET_ADDRESS_TYPE_UNIX
:
413 info
->host
= g_strdup("");
414 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
415 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
418 case SOCKET_ADDRESS_TYPE_VSOCK
:
419 case SOCKET_ADDRESS_TYPE_FD
:
420 error_setg(errp
, "Unsupported socket address type %s",
421 SocketAddressType_str(addr
->type
));
427 info
->has_host
= true;
428 info
->has_service
= true;
429 info
->has_family
= true;
431 info
->has_auth
= true;
432 info
->auth
= g_strdup(vnc_auth_name(vd
));
435 qapi_free_SocketAddress(addr
);
439 qapi_free_SocketAddress(addr
);
440 qapi_free_VncInfo(info
);
445 static void qmp_query_auth(int auth
, int subauth
,
446 VncPrimaryAuth
*qmp_auth
,
447 VncVencryptSubAuth
*qmp_vencrypt
,
448 bool *qmp_has_vencrypt
);
450 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
454 VncServerInfo2List
*prev
)
456 VncServerInfo2
*info
;
460 addr
= qio_channel_socket_get_local_address(ioc
, NULL
);
465 info
= g_new0(VncServerInfo2
, 1);
466 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
467 qapi_free_SocketAddress(addr
);
469 qapi_free_VncServerInfo2(info
);
473 info
->websocket
= websocket
;
475 qmp_query_auth(auth
, subauth
, &info
->auth
,
476 &info
->vencrypt
, &info
->has_vencrypt
);
478 QAPI_LIST_PREPEND(prev
, info
);
482 static void qmp_query_auth(int auth
, int subauth
,
483 VncPrimaryAuth
*qmp_auth
,
484 VncVencryptSubAuth
*qmp_vencrypt
,
485 bool *qmp_has_vencrypt
)
489 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
492 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
495 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
498 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
501 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
504 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
506 case VNC_AUTH_VENCRYPT
:
507 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
508 *qmp_has_vencrypt
= true;
510 case VNC_AUTH_VENCRYPT_PLAIN
:
511 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
513 case VNC_AUTH_VENCRYPT_TLSNONE
:
514 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
516 case VNC_AUTH_VENCRYPT_TLSVNC
:
517 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
519 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
520 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
522 case VNC_AUTH_VENCRYPT_X509NONE
:
523 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
525 case VNC_AUTH_VENCRYPT_X509VNC
:
526 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
528 case VNC_AUTH_VENCRYPT_X509PLAIN
:
529 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
531 case VNC_AUTH_VENCRYPT_TLSSASL
:
532 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
534 case VNC_AUTH_VENCRYPT_X509SASL
:
535 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
538 *qmp_has_vencrypt
= false;
543 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
547 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
552 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
554 VncInfo2List
*prev
= NULL
;
560 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
561 info
= g_new0(VncInfo2
, 1);
562 info
->id
= g_strdup(vd
->id
);
563 info
->clients
= qmp_query_client_list(vd
);
564 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
565 &info
->vencrypt
, &info
->has_vencrypt
);
567 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
568 "device", &error_abort
));
569 info
->has_display
= true;
570 info
->display
= g_strdup(dev
->id
);
572 for (i
= 0; vd
->listener
!= NULL
&& i
< vd
->listener
->nsioc
; i
++) {
573 info
->server
= qmp_query_server_entry(
574 vd
->listener
->sioc
[i
], false, vd
->auth
, vd
->subauth
,
577 for (i
= 0; vd
->wslistener
!= NULL
&& i
< vd
->wslistener
->nsioc
; i
++) {
578 info
->server
= qmp_query_server_entry(
579 vd
->wslistener
->sioc
[i
], true, vd
->ws_auth
,
580 vd
->ws_subauth
, info
->server
);
583 QAPI_LIST_PREPEND(prev
, info
);
588 bool vnc_display_reload_certs(const char *id
, Error
**errp
)
590 VncDisplay
*vd
= vnc_display_find(id
);
591 QCryptoTLSCredsClass
*creds
= NULL
;
594 error_setg(errp
, "Can not find vnc display");
599 error_setg(errp
, "vnc tls is not enable");
603 creds
= QCRYPTO_TLS_CREDS_GET_CLASS(OBJECT(vd
->tlscreds
));
604 if (creds
->reload
== NULL
) {
605 error_setg(errp
, "%s doesn't support to reload TLS credential",
606 object_get_typename(OBJECT(vd
->tlscreds
)));
609 if (!creds
->reload(vd
->tlscreds
, errp
)) {
617 1) Get the queue working for IO.
618 2) there is some weirdness when using the -S option (the screen is grey
619 and not totally invalidated
620 3) resolutions > 1024
623 static int vnc_update_client(VncState
*vs
, int has_dirty
);
624 static void vnc_disconnect_start(VncState
*vs
);
626 static void vnc_colordepth(VncState
*vs
);
627 static void framebuffer_update_request(VncState
*vs
, int incremental
,
628 int x_position
, int y_position
,
630 static void vnc_refresh(DisplayChangeListener
*dcl
);
631 static int vnc_refresh_server_surface(VncDisplay
*vd
);
633 static int vnc_width(VncDisplay
*vd
)
635 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
636 VNC_DIRTY_PIXELS_PER_BIT
));
639 static int vnc_true_width(VncDisplay
*vd
)
641 return MIN(VNC_MAX_WIDTH
, surface_width(vd
->ds
));
644 static int vnc_height(VncDisplay
*vd
)
646 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
649 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
650 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
652 int x
, int y
, int w
, int h
)
654 int width
= vnc_width(vd
);
655 int height
= vnc_height(vd
);
657 /* this is needed this to ensure we updated all affected
658 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
659 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
660 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
664 w
= MIN(x
+ w
, width
) - x
;
665 h
= MIN(y
+ h
, height
);
668 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
669 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
673 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
674 int x
, int y
, int w
, int h
)
676 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
677 struct VncSurface
*s
= &vd
->guest
;
679 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
682 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
685 vnc_write_u16(vs
, x
);
686 vnc_write_u16(vs
, y
);
687 vnc_write_u16(vs
, w
);
688 vnc_write_u16(vs
, h
);
690 vnc_write_s32(vs
, encoding
);
693 static void vnc_desktop_resize_ext(VncState
*vs
, int reject_reason
)
695 trace_vnc_msg_server_ext_desktop_resize(
696 vs
, vs
->ioc
, vs
->client_width
, vs
->client_height
, reject_reason
);
699 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
701 vnc_write_u16(vs
, 1); /* number of rects */
702 vnc_framebuffer_update(vs
,
703 reject_reason
? 1 : 0,
705 vs
->client_width
, vs
->client_height
,
706 VNC_ENCODING_DESKTOP_RESIZE_EXT
);
707 vnc_write_u8(vs
, 1); /* number of screens */
708 vnc_write_u8(vs
, 0); /* padding */
709 vnc_write_u8(vs
, 0); /* padding */
710 vnc_write_u8(vs
, 0); /* padding */
711 vnc_write_u32(vs
, 0); /* screen id */
712 vnc_write_u16(vs
, 0); /* screen x-pos */
713 vnc_write_u16(vs
, 0); /* screen y-pos */
714 vnc_write_u16(vs
, vs
->client_width
);
715 vnc_write_u16(vs
, vs
->client_height
);
716 vnc_write_u32(vs
, 0); /* screen flags */
717 vnc_unlock_output(vs
);
721 static void vnc_desktop_resize(VncState
*vs
)
723 if (vs
->ioc
== NULL
|| (!vnc_has_feature(vs
, VNC_FEATURE_RESIZE
) &&
724 !vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
))) {
727 if (vs
->client_width
== vs
->vd
->true_width
&&
728 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
732 assert(vs
->vd
->true_width
< 65536 &&
733 vs
->vd
->true_width
>= 0);
734 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
735 pixman_image_get_height(vs
->vd
->server
) >= 0);
736 vs
->client_width
= vs
->vd
->true_width
;
737 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
739 if (vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
)) {
740 vnc_desktop_resize_ext(vs
, 0);
744 trace_vnc_msg_server_desktop_resize(
745 vs
, vs
->ioc
, vs
->client_width
, vs
->client_height
);
748 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
750 vnc_write_u16(vs
, 1); /* number of rects */
751 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
752 VNC_ENCODING_DESKTOPRESIZE
);
753 vnc_unlock_output(vs
);
757 static void vnc_abort_display_jobs(VncDisplay
*vd
)
761 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
764 vnc_unlock_output(vs
);
766 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
769 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
771 if (vs
->update
== VNC_STATE_UPDATE_NONE
&&
772 vs
->job_update
!= VNC_STATE_UPDATE_NONE
) {
773 /* job aborted before completion */
774 vs
->update
= vs
->job_update
;
775 vs
->job_update
= VNC_STATE_UPDATE_NONE
;
778 vnc_unlock_output(vs
);
782 int vnc_server_fb_stride(VncDisplay
*vd
)
784 return pixman_image_get_stride(vd
->server
);
787 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
791 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
792 ptr
+= y
* vnc_server_fb_stride(vd
);
793 ptr
+= x
* VNC_SERVER_FB_BYTES
;
797 static void vnc_update_server_surface(VncDisplay
*vd
)
801 qemu_pixman_image_unref(vd
->server
);
804 if (QTAILQ_EMPTY(&vd
->clients
)) {
808 width
= vnc_width(vd
);
809 height
= vnc_height(vd
);
810 vd
->true_width
= vnc_true_width(vd
);
811 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
815 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
816 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
820 static bool vnc_check_pageflip(DisplaySurface
*s1
,
823 return (s1
!= NULL
&&
825 surface_width(s1
) == surface_width(s2
) &&
826 surface_height(s1
) == surface_height(s2
) &&
827 surface_format(s1
) == surface_format(s2
));
831 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
832 DisplaySurface
*surface
)
834 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
835 bool pageflip
= vnc_check_pageflip(vd
->ds
, surface
);
838 vnc_abort_display_jobs(vd
);
842 qemu_pixman_image_unref(vd
->guest
.fb
);
843 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
844 vd
->guest
.format
= surface
->format
;
848 trace_vnc_server_dpy_pageflip(vd
,
849 surface_width(surface
),
850 surface_height(surface
),
851 surface_format(surface
));
852 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
853 surface_width(surface
),
854 surface_height(surface
));
858 trace_vnc_server_dpy_recreate(vd
,
859 surface_width(surface
),
860 surface_height(surface
),
861 surface_format(surface
));
863 vnc_update_server_surface(vd
);
865 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
867 vnc_desktop_resize(vs
);
868 vnc_cursor_define(vs
);
869 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
870 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
873 vnc_update_throttle_offset(vs
);
878 static void vnc_write_pixels_copy(VncState
*vs
,
879 void *pixels
, int size
)
881 vnc_write(vs
, pixels
, size
);
884 /* slowest but generic code. */
885 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
889 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
890 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
891 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
892 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
894 # error need some bits here if you change VNC_SERVER_FB_FORMAT
896 v
= (r
<< vs
->client_pf
.rshift
) |
897 (g
<< vs
->client_pf
.gshift
) |
898 (b
<< vs
->client_pf
.bshift
);
899 switch (vs
->client_pf
.bytes_per_pixel
) {
929 static void vnc_write_pixels_generic(VncState
*vs
,
930 void *pixels1
, int size
)
934 if (VNC_SERVER_FB_BYTES
== 4) {
935 uint32_t *pixels
= pixels1
;
938 for (i
= 0; i
< n
; i
++) {
939 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
940 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
945 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
949 VncDisplay
*vd
= vs
->vd
;
951 row
= vnc_server_fb_ptr(vd
, x
, y
);
952 for (i
= 0; i
< h
; i
++) {
953 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
954 row
+= vnc_server_fb_stride(vd
);
959 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
963 switch(vs
->vnc_encoding
) {
964 case VNC_ENCODING_ZLIB
:
965 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
967 case VNC_ENCODING_HEXTILE
:
968 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
969 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
971 case VNC_ENCODING_TIGHT
:
972 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
974 case VNC_ENCODING_TIGHT_PNG
:
975 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
977 case VNC_ENCODING_ZRLE
:
978 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
980 case VNC_ENCODING_ZYWRLE
:
981 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
984 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
985 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
991 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
992 int x
, int y
, int visible
)
994 /* can we ask the client(s) to move the pointer ??? */
997 static int vnc_cursor_define(VncState
*vs
)
999 QEMUCursor
*c
= vs
->vd
->cursor
;
1002 if (!vs
->vd
->cursor
) {
1006 if (vnc_has_feature(vs
, VNC_FEATURE_ALPHA_CURSOR
)) {
1007 vnc_lock_output(vs
);
1008 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1009 vnc_write_u8(vs
, 0); /* padding */
1010 vnc_write_u16(vs
, 1); /* # of rects */
1011 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
1012 VNC_ENCODING_ALPHA_CURSOR
);
1013 vnc_write_s32(vs
, VNC_ENCODING_RAW
);
1014 vnc_write(vs
, c
->data
, c
->width
* c
->height
* 4);
1015 vnc_unlock_output(vs
);
1018 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
1019 vnc_lock_output(vs
);
1020 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1021 vnc_write_u8(vs
, 0); /* padding */
1022 vnc_write_u16(vs
, 1); /* # of rects */
1023 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
1024 VNC_ENCODING_RICH_CURSOR
);
1025 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
1026 vnc_write_pixels_generic(vs
, c
->data
, isize
);
1027 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
1028 vnc_unlock_output(vs
);
1034 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
1037 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
1040 cursor_put(vd
->cursor
);
1041 g_free(vd
->cursor_mask
);
1044 cursor_get(vd
->cursor
);
1045 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
1046 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
1047 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
1049 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
1050 vnc_cursor_define(vs
);
1054 static int find_and_clear_dirty_height(VncState
*vs
,
1055 int y
, int last_x
, int x
, int height
)
1059 for (h
= 1; h
< (height
- y
); h
++) {
1060 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
1063 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
1070 * Figure out how much pending data we should allow in the output
1071 * buffer before we throttle incremental display updates, and/or
1072 * drop audio samples.
1074 * We allow for equiv of 1 full display's worth of FB updates,
1075 * and 1 second of audio samples. If audio backlog was larger
1076 * than that the client would already suffering awful audio
1077 * glitches, so dropping samples is no worse really).
1079 static void vnc_update_throttle_offset(VncState
*vs
)
1082 vs
->client_width
* vs
->client_height
* vs
->client_pf
.bytes_per_pixel
;
1084 if (vs
->audio_cap
) {
1086 switch (vs
->as
.fmt
) {
1088 case AUDIO_FORMAT_U8
:
1089 case AUDIO_FORMAT_S8
:
1092 case AUDIO_FORMAT_U16
:
1093 case AUDIO_FORMAT_S16
:
1096 case AUDIO_FORMAT_U32
:
1097 case AUDIO_FORMAT_S32
:
1101 offset
+= vs
->as
.freq
* bps
* vs
->as
.nchannels
;
1104 /* Put a floor of 1MB on offset, so that if we have a large pending
1105 * buffer and the display is resized to a small size & back again
1106 * we don't suddenly apply a tiny send limit
1108 offset
= MAX(offset
, 1024 * 1024);
1110 if (vs
->throttle_output_offset
!= offset
) {
1111 trace_vnc_client_throttle_threshold(
1112 vs
, vs
->ioc
, vs
->throttle_output_offset
, offset
, vs
->client_width
,
1113 vs
->client_height
, vs
->client_pf
.bytes_per_pixel
, vs
->audio_cap
);
1116 vs
->throttle_output_offset
= offset
;
1119 static bool vnc_should_update(VncState
*vs
)
1121 switch (vs
->update
) {
1122 case VNC_STATE_UPDATE_NONE
:
1124 case VNC_STATE_UPDATE_INCREMENTAL
:
1125 /* Only allow incremental updates if the pending send queue
1126 * is less than the permitted threshold, and the job worker
1127 * is completely idle.
1129 if (vs
->output
.offset
< vs
->throttle_output_offset
&&
1130 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1133 trace_vnc_client_throttle_incremental(
1134 vs
, vs
->ioc
, vs
->job_update
, vs
->output
.offset
);
1136 case VNC_STATE_UPDATE_FORCE
:
1137 /* Only allow forced updates if the pending send queue
1138 * does not contain a previous forced update, and the
1139 * job worker is completely idle.
1141 * Note this means we'll queue a forced update, even if
1142 * the output buffer size is otherwise over the throttle
1145 if (vs
->force_update_offset
== 0 &&
1146 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1149 trace_vnc_client_throttle_forced(
1150 vs
, vs
->ioc
, vs
->job_update
, vs
->force_update_offset
);
1156 static int vnc_update_client(VncState
*vs
, int has_dirty
)
1158 VncDisplay
*vd
= vs
->vd
;
1164 if (vs
->disconnecting
) {
1165 vnc_disconnect_finish(vs
);
1169 vs
->has_dirty
+= has_dirty
;
1170 if (!vnc_should_update(vs
)) {
1174 if (!vs
->has_dirty
&& vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
1179 * Send screen updates to the vnc client using the server
1180 * surface and server dirty map. guest surface updates
1181 * happening in parallel don't disturb us, the next pass will
1182 * send them to the client.
1184 job
= vnc_job_new(vs
);
1186 height
= pixman_image_get_height(vd
->server
);
1187 width
= pixman_image_get_width(vd
->server
);
1193 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1194 height
* VNC_DIRTY_BPL(vs
),
1195 y
* VNC_DIRTY_BPL(vs
));
1196 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1197 /* no more dirty bits */
1200 y
= offset
/ VNC_DIRTY_BPL(vs
);
1201 x
= offset
% VNC_DIRTY_BPL(vs
);
1202 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1203 VNC_DIRTY_BPL(vs
), x
);
1204 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1205 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1206 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1208 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1209 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1211 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1219 vs
->job_update
= vs
->update
;
1220 vs
->update
= VNC_STATE_UPDATE_NONE
;
1227 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1229 VncState
*vs
= opaque
;
1231 assert(vs
->magic
== VNC_MAGIC
);
1233 case AUD_CNOTIFY_DISABLE
:
1234 trace_vnc_msg_server_audio_end(vs
, vs
->ioc
);
1235 vnc_lock_output(vs
);
1236 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1237 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1238 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
1239 vnc_unlock_output(vs
);
1243 case AUD_CNOTIFY_ENABLE
:
1244 trace_vnc_msg_server_audio_begin(vs
, vs
->ioc
);
1245 vnc_lock_output(vs
);
1246 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1247 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1248 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1249 vnc_unlock_output(vs
);
1255 static void audio_capture_destroy(void *opaque
)
1259 static void audio_capture(void *opaque
, const void *buf
, int size
)
1261 VncState
*vs
= opaque
;
1263 assert(vs
->magic
== VNC_MAGIC
);
1264 trace_vnc_msg_server_audio_data(vs
, vs
->ioc
, buf
, size
);
1265 vnc_lock_output(vs
);
1266 if (vs
->output
.offset
< vs
->throttle_output_offset
) {
1267 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1268 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1269 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1270 vnc_write_u32(vs
, size
);
1271 vnc_write(vs
, buf
, size
);
1273 trace_vnc_client_throttle_audio(vs
, vs
->ioc
, vs
->output
.offset
);
1275 vnc_unlock_output(vs
);
1279 static void audio_add(VncState
*vs
)
1281 struct audio_capture_ops ops
;
1283 if (vs
->audio_cap
) {
1284 error_report("audio already running");
1288 ops
.notify
= audio_capture_notify
;
1289 ops
.destroy
= audio_capture_destroy
;
1290 ops
.capture
= audio_capture
;
1292 vs
->audio_cap
= AUD_add_capture(vs
->vd
->audio_state
, &vs
->as
, &ops
, vs
);
1293 if (!vs
->audio_cap
) {
1294 error_report("Failed to add audio capture");
1298 static void audio_del(VncState
*vs
)
1300 if (vs
->audio_cap
) {
1301 AUD_del_capture(vs
->audio_cap
, vs
);
1302 vs
->audio_cap
= NULL
;
1306 static void vnc_disconnect_start(VncState
*vs
)
1308 if (vs
->disconnecting
) {
1311 trace_vnc_client_disconnect_start(vs
, vs
->ioc
);
1312 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1314 g_source_remove(vs
->ioc_tag
);
1317 qio_channel_close(vs
->ioc
, NULL
);
1318 vs
->disconnecting
= TRUE
;
1321 void vnc_disconnect_finish(VncState
*vs
)
1325 trace_vnc_client_disconnect_finish(vs
, vs
->ioc
);
1327 vnc_jobs_join(vs
); /* Wait encoding jobs */
1329 vnc_lock_output(vs
);
1330 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1332 buffer_free(&vs
->input
);
1333 buffer_free(&vs
->output
);
1335 qapi_free_VncClientInfo(vs
->info
);
1338 vnc_tight_clear(vs
);
1341 #ifdef CONFIG_VNC_SASL
1342 vnc_sasl_client_cleanup(vs
);
1343 #endif /* CONFIG_VNC_SASL */
1345 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1347 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1348 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1350 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1351 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1352 /* last client gone */
1353 vnc_update_server_surface(vs
->vd
);
1356 vnc_unlock_output(vs
);
1358 qemu_mutex_destroy(&vs
->output_mutex
);
1359 if (vs
->bh
!= NULL
) {
1360 qemu_bh_delete(vs
->bh
);
1362 buffer_free(&vs
->jobs_buffer
);
1364 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1365 g_free(vs
->lossy_rect
[i
]);
1367 g_free(vs
->lossy_rect
);
1369 object_unref(OBJECT(vs
->ioc
));
1371 object_unref(OBJECT(vs
->sioc
));
1379 size_t vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
*err
)
1383 trace_vnc_client_eof(vs
, vs
->ioc
);
1384 vnc_disconnect_start(vs
);
1385 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1386 trace_vnc_client_io_error(vs
, vs
->ioc
,
1387 err
? error_get_pretty(err
) : "Unknown");
1388 vnc_disconnect_start(vs
);
1398 void vnc_client_error(VncState
*vs
)
1400 VNC_DEBUG("Closing down client sock: protocol error\n");
1401 vnc_disconnect_start(vs
);
1406 * Called to write a chunk of data to the client socket. The data may
1407 * be the raw data, or may have already been encoded by SASL.
1408 * The data will be written either straight onto the socket, or
1409 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1411 * NB, it is theoretically possible to have 2 layers of encryption,
1412 * both SASL, and this TLS layer. It is highly unlikely in practice
1413 * though, since SASL encryption will typically be a no-op if TLS
1416 * Returns the number of bytes written, which may be less than
1417 * the requested 'datalen' if the socket would block. Returns
1418 * 0 on I/O error, and disconnects the client socket.
1420 size_t vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1424 ret
= qio_channel_write(vs
->ioc
, (const char *)data
, datalen
, &err
);
1425 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1426 return vnc_client_io_error(vs
, ret
, err
);
1431 * Called to write buffered data to the client socket, when not
1432 * using any SASL SSF encryption layers. Will write as much data
1433 * as possible without blocking. If all buffered data is written,
1434 * will switch the FD poll() handler back to read monitoring.
1436 * Returns the number of bytes written, which may be less than
1437 * the buffered output data if the socket would block. Returns
1438 * 0 on I/O error, and disconnects the client socket.
1440 static size_t vnc_client_write_plain(VncState
*vs
)
1445 #ifdef CONFIG_VNC_SASL
1446 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1447 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1448 vs
->sasl
.waitWriteSSF
);
1450 if (vs
->sasl
.conn
&&
1452 vs
->sasl
.waitWriteSSF
) {
1453 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1455 vs
->sasl
.waitWriteSSF
-= ret
;
1457 #endif /* CONFIG_VNC_SASL */
1458 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1462 if (ret
>= vs
->force_update_offset
) {
1463 if (vs
->force_update_offset
!= 0) {
1464 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
1466 vs
->force_update_offset
= 0;
1468 vs
->force_update_offset
-= ret
;
1470 offset
= vs
->output
.offset
;
1471 buffer_advance(&vs
->output
, ret
);
1472 if (offset
>= vs
->throttle_output_offset
&&
1473 vs
->output
.offset
< vs
->throttle_output_offset
) {
1474 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
, vs
->output
.offset
);
1477 if (vs
->output
.offset
== 0) {
1479 g_source_remove(vs
->ioc_tag
);
1481 vs
->ioc_tag
= qio_channel_add_watch(
1482 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
1483 vnc_client_io
, vs
, NULL
);
1491 * First function called whenever there is data to be written to
1492 * the client socket. Will delegate actual work according to whether
1493 * SASL SSF layers are enabled (thus requiring encryption calls)
1495 static void vnc_client_write_locked(VncState
*vs
)
1497 #ifdef CONFIG_VNC_SASL
1498 if (vs
->sasl
.conn
&&
1500 !vs
->sasl
.waitWriteSSF
) {
1501 vnc_client_write_sasl(vs
);
1503 #endif /* CONFIG_VNC_SASL */
1505 vnc_client_write_plain(vs
);
1509 static void vnc_client_write(VncState
*vs
)
1511 assert(vs
->magic
== VNC_MAGIC
);
1512 vnc_lock_output(vs
);
1513 if (vs
->output
.offset
) {
1514 vnc_client_write_locked(vs
);
1515 } else if (vs
->ioc
!= NULL
) {
1517 g_source_remove(vs
->ioc_tag
);
1519 vs
->ioc_tag
= qio_channel_add_watch(
1520 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
1521 vnc_client_io
, vs
, NULL
);
1523 vnc_unlock_output(vs
);
1526 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1528 vs
->read_handler
= func
;
1529 vs
->read_handler_expect
= expecting
;
1534 * Called to read a chunk of data from the client socket. The data may
1535 * be the raw data, or may need to be further decoded by SASL.
1536 * The data will be read either straight from to the socket, or
1537 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1539 * NB, it is theoretically possible to have 2 layers of encryption,
1540 * both SASL, and this TLS layer. It is highly unlikely in practice
1541 * though, since SASL encryption will typically be a no-op if TLS
1544 * Returns the number of bytes read, which may be less than
1545 * the requested 'datalen' if the socket would block. Returns
1546 * 0 on I/O error or EOF, and disconnects the client socket.
1548 size_t vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1552 ret
= qio_channel_read(vs
->ioc
, (char *)data
, datalen
, &err
);
1553 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1554 return vnc_client_io_error(vs
, ret
, err
);
1559 * Called to read data from the client socket to the input buffer,
1560 * when not using any SASL SSF encryption layers. Will read as much
1561 * data as possible without blocking.
1563 * Returns the number of bytes read, which may be less than
1564 * the requested 'datalen' if the socket would block. Returns
1565 * 0 on I/O error or EOF, and disconnects the client socket.
1567 static size_t vnc_client_read_plain(VncState
*vs
)
1570 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1571 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1572 buffer_reserve(&vs
->input
, 4096);
1573 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1576 vs
->input
.offset
+= ret
;
1580 static void vnc_jobs_bh(void *opaque
)
1582 VncState
*vs
= opaque
;
1584 assert(vs
->magic
== VNC_MAGIC
);
1585 vnc_jobs_consume_buffer(vs
);
1589 * First function called whenever there is more data to be read from
1590 * the client socket. Will delegate actual work according to whether
1591 * SASL SSF layers are enabled (thus requiring decryption calls)
1592 * Returns 0 on success, -1 if client disconnected
1594 static int vnc_client_read(VncState
*vs
)
1598 #ifdef CONFIG_VNC_SASL
1599 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1600 ret
= vnc_client_read_sasl(vs
);
1602 #endif /* CONFIG_VNC_SASL */
1603 ret
= vnc_client_read_plain(vs
);
1605 if (vs
->disconnecting
) {
1606 vnc_disconnect_finish(vs
);
1612 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1613 size_t len
= vs
->read_handler_expect
;
1616 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1617 if (vs
->disconnecting
) {
1618 vnc_disconnect_finish(vs
);
1623 buffer_advance(&vs
->input
, len
);
1625 vs
->read_handler_expect
= ret
;
1631 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1632 GIOCondition condition
, void *opaque
)
1634 VncState
*vs
= opaque
;
1636 assert(vs
->magic
== VNC_MAGIC
);
1638 if (condition
& (G_IO_HUP
| G_IO_ERR
)) {
1639 vnc_disconnect_start(vs
);
1643 if (condition
& G_IO_IN
) {
1644 if (vnc_client_read(vs
) < 0) {
1645 /* vs is free()ed here */
1649 if (condition
& G_IO_OUT
) {
1650 vnc_client_write(vs
);
1653 if (vs
->disconnecting
) {
1654 if (vs
->ioc_tag
!= 0) {
1655 g_source_remove(vs
->ioc_tag
);
1664 * Scale factor to apply to vs->throttle_output_offset when checking for
1665 * hard limit. Worst case normal usage could be x2, if we have a complete
1666 * incremental update and complete forced update in the output buffer.
1667 * So x3 should be good enough, but we pick x5 to be conservative and thus
1668 * (hopefully) never trigger incorrectly.
1670 #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1672 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1674 assert(vs
->magic
== VNC_MAGIC
);
1675 if (vs
->disconnecting
) {
1678 /* Protection against malicious client/guest to prevent our output
1679 * buffer growing without bound if client stops reading data. This
1680 * should rarely trigger, because we have earlier throttling code
1681 * which stops issuing framebuffer updates and drops audio data
1682 * if the throttle_output_offset value is exceeded. So we only reach
1683 * this higher level if a huge number of pseudo-encodings get
1684 * triggered while data can't be sent on the socket.
1686 * NB throttle_output_offset can be zero during early protocol
1687 * handshake, or from the job thread's VncState clone
1689 if (vs
->throttle_output_offset
!= 0 &&
1690 (vs
->output
.offset
/ VNC_THROTTLE_OUTPUT_LIMIT_SCALE
) >
1691 vs
->throttle_output_offset
) {
1692 trace_vnc_client_output_limit(vs
, vs
->ioc
, vs
->output
.offset
,
1693 vs
->throttle_output_offset
);
1694 vnc_disconnect_start(vs
);
1697 buffer_reserve(&vs
->output
, len
);
1699 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1701 g_source_remove(vs
->ioc_tag
);
1703 vs
->ioc_tag
= qio_channel_add_watch(
1704 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_OUT
,
1705 vnc_client_io
, vs
, NULL
);
1708 buffer_append(&vs
->output
, data
, len
);
1711 void vnc_write_s32(VncState
*vs
, int32_t value
)
1713 vnc_write_u32(vs
, *(uint32_t *)&value
);
1716 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1720 buf
[0] = (value
>> 24) & 0xFF;
1721 buf
[1] = (value
>> 16) & 0xFF;
1722 buf
[2] = (value
>> 8) & 0xFF;
1723 buf
[3] = value
& 0xFF;
1725 vnc_write(vs
, buf
, 4);
1728 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1732 buf
[0] = (value
>> 8) & 0xFF;
1733 buf
[1] = value
& 0xFF;
1735 vnc_write(vs
, buf
, 2);
1738 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1740 vnc_write(vs
, (char *)&value
, 1);
1743 void vnc_flush(VncState
*vs
)
1745 vnc_lock_output(vs
);
1746 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1747 vnc_client_write_locked(vs
);
1749 if (vs
->disconnecting
) {
1750 if (vs
->ioc_tag
!= 0) {
1751 g_source_remove(vs
->ioc_tag
);
1755 vnc_unlock_output(vs
);
1758 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1760 return data
[offset
];
1763 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1765 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1768 static int32_t read_s32(uint8_t *data
, size_t offset
)
1770 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1771 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1774 uint32_t read_u32(uint8_t *data
, size_t offset
)
1776 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1777 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1780 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1784 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1786 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1787 int absolute
= qemu_input_is_absolute();
1789 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1790 vnc_lock_output(vs
);
1791 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1792 vnc_write_u8(vs
, 0);
1793 vnc_write_u16(vs
, 1);
1794 vnc_framebuffer_update(vs
, absolute
, 0,
1795 pixman_image_get_width(vs
->vd
->server
),
1796 pixman_image_get_height(vs
->vd
->server
),
1797 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1798 vnc_unlock_output(vs
);
1801 vs
->absolute
= absolute
;
1804 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1806 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1807 [INPUT_BUTTON_LEFT
] = 0x01,
1808 [INPUT_BUTTON_MIDDLE
] = 0x02,
1809 [INPUT_BUTTON_RIGHT
] = 0x04,
1810 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1811 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1813 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1814 int width
= pixman_image_get_width(vs
->vd
->server
);
1815 int height
= pixman_image_get_height(vs
->vd
->server
);
1817 if (vs
->last_bmask
!= button_mask
) {
1818 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1819 vs
->last_bmask
= button_mask
;
1823 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, 0, width
);
1824 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, 0, height
);
1825 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1826 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1827 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1829 if (vs
->last_x
!= -1) {
1830 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1831 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1836 qemu_input_event_sync();
1839 static void press_key(VncState
*vs
, QKeyCode qcode
)
1841 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, true);
1842 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, false);
1845 static void vnc_led_state_change(VncState
*vs
)
1847 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1851 vnc_lock_output(vs
);
1852 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1853 vnc_write_u8(vs
, 0);
1854 vnc_write_u16(vs
, 1);
1855 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1856 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1857 vnc_unlock_output(vs
);
1861 static void kbd_leds(void *opaque
, int ledstate
)
1863 VncDisplay
*vd
= opaque
;
1866 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1867 (ledstate
& QEMU_NUM_LOCK_LED
),
1868 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1870 if (ledstate
== vd
->ledstate
) {
1874 vd
->ledstate
= ledstate
;
1876 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1877 vnc_led_state_change(client
);
1881 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1883 QKeyCode qcode
= qemu_input_key_number_to_qcode(keycode
);
1885 /* QEMU console switch */
1887 case Q_KEY_CODE_1
... Q_KEY_CODE_9
: /* '1' to '9' keys */
1888 if (vs
->vd
->dcl
.con
== NULL
&& down
&&
1889 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
) &&
1890 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_ALT
)) {
1891 /* Reset the modifiers sent to the current console */
1892 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1893 console_select(qcode
- Q_KEY_CODE_1
);
1900 /* Turn off the lock state sync logic if the client support the led
1903 if (down
&& vs
->vd
->lock_key_sync
&&
1904 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1905 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1906 /* If the numlock state needs to change then simulate an additional
1907 keypress before sending this one. This will happen if the user
1908 toggles numlock away from the VNC window.
1910 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1911 if (!qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1912 trace_vnc_key_sync_numlock(true);
1913 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1916 if (qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1917 trace_vnc_key_sync_numlock(false);
1918 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1923 if (down
&& vs
->vd
->lock_key_sync
&&
1924 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1925 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1926 /* If the capslock state needs to change then simulate an additional
1927 keypress before sending this one. This will happen if the user
1928 toggles capslock away from the VNC window.
1930 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1931 bool shift
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_SHIFT
);
1932 bool capslock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CAPSLOCK
);
1934 if (uppercase
== shift
) {
1935 trace_vnc_key_sync_capslock(false);
1936 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1939 if (uppercase
!= shift
) {
1940 trace_vnc_key_sync_capslock(true);
1941 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1946 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, down
);
1947 if (!qemu_console_is_graphic(NULL
)) {
1948 bool numlock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
);
1949 bool control
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
);
1950 /* QEMU console emulation */
1953 case 0x2a: /* Left Shift */
1954 case 0x36: /* Right Shift */
1955 case 0x1d: /* Left CTRL */
1956 case 0x9d: /* Right CTRL */
1957 case 0x38: /* Left ALT */
1958 case 0xb8: /* Right ALT */
1961 kbd_put_keysym(QEMU_KEY_UP
);
1964 kbd_put_keysym(QEMU_KEY_DOWN
);
1967 kbd_put_keysym(QEMU_KEY_LEFT
);
1970 kbd_put_keysym(QEMU_KEY_RIGHT
);
1973 kbd_put_keysym(QEMU_KEY_DELETE
);
1976 kbd_put_keysym(QEMU_KEY_HOME
);
1979 kbd_put_keysym(QEMU_KEY_END
);
1982 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1985 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1989 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1992 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1995 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1998 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
2001 kbd_put_keysym('5');
2004 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
2007 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
2010 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
2013 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
2016 kbd_put_keysym('0');
2019 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
2023 kbd_put_keysym('/');
2026 kbd_put_keysym('*');
2029 kbd_put_keysym('-');
2032 kbd_put_keysym('+');
2035 kbd_put_keysym('\n');
2040 kbd_put_keysym(sym
& 0x1f);
2042 kbd_put_keysym(sym
);
2050 static const char *code2name(int keycode
)
2052 return QKeyCode_str(qemu_input_key_number_to_qcode(keycode
));
2055 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
2060 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(NULL
)) {
2061 lsym
= lsym
- 'A' + 'a';
2064 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF,
2065 vs
->vd
->kbd
, down
) & SCANCODE_KEYMASK
;
2066 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
2067 do_key_event(vs
, down
, keycode
, sym
);
2070 static void ext_key_event(VncState
*vs
, int down
,
2071 uint32_t sym
, uint16_t keycode
)
2073 /* if the user specifies a keyboard layout, always use it */
2074 if (keyboard_layout
) {
2075 key_event(vs
, down
, sym
);
2077 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
2078 do_key_event(vs
, down
, keycode
, sym
);
2082 static void framebuffer_update_request(VncState
*vs
, int incremental
,
2083 int x
, int y
, int w
, int h
)
2086 if (vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
2087 vs
->update
= VNC_STATE_UPDATE_INCREMENTAL
;
2090 vs
->update
= VNC_STATE_UPDATE_FORCE
;
2091 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
2092 if (vnc_has_feature(vs
, VNC_FEATURE_RESIZE_EXT
)) {
2093 vnc_desktop_resize_ext(vs
, 0);
2098 static void send_ext_key_event_ack(VncState
*vs
)
2100 vnc_lock_output(vs
);
2101 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2102 vnc_write_u8(vs
, 0);
2103 vnc_write_u16(vs
, 1);
2104 vnc_framebuffer_update(vs
, 0, 0,
2105 pixman_image_get_width(vs
->vd
->server
),
2106 pixman_image_get_height(vs
->vd
->server
),
2107 VNC_ENCODING_EXT_KEY_EVENT
);
2108 vnc_unlock_output(vs
);
2112 static void send_ext_audio_ack(VncState
*vs
)
2114 vnc_lock_output(vs
);
2115 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2116 vnc_write_u8(vs
, 0);
2117 vnc_write_u16(vs
, 1);
2118 vnc_framebuffer_update(vs
, 0, 0,
2119 pixman_image_get_width(vs
->vd
->server
),
2120 pixman_image_get_height(vs
->vd
->server
),
2121 VNC_ENCODING_AUDIO
);
2122 vnc_unlock_output(vs
);
2126 static void send_xvp_message(VncState
*vs
, int code
)
2128 vnc_lock_output(vs
);
2129 vnc_write_u8(vs
, VNC_MSG_SERVER_XVP
);
2130 vnc_write_u8(vs
, 0); /* pad */
2131 vnc_write_u8(vs
, 1); /* version */
2132 vnc_write_u8(vs
, code
);
2133 vnc_unlock_output(vs
);
2137 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2140 unsigned int enc
= 0;
2143 vs
->vnc_encoding
= 0;
2144 vs
->tight
->compression
= 9;
2145 vs
->tight
->quality
= -1; /* Lossless by default */
2149 * Start from the end because the encodings are sent in order of preference.
2150 * This way the preferred encoding (first encoding defined in the array)
2151 * will be set at the end of the loop.
2153 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2156 case VNC_ENCODING_RAW
:
2157 vs
->vnc_encoding
= enc
;
2159 case VNC_ENCODING_HEXTILE
:
2160 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
2161 vs
->vnc_encoding
= enc
;
2163 case VNC_ENCODING_TIGHT
:
2164 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
2165 vs
->vnc_encoding
= enc
;
2167 #ifdef CONFIG_VNC_PNG
2168 case VNC_ENCODING_TIGHT_PNG
:
2169 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
2170 vs
->vnc_encoding
= enc
;
2173 case VNC_ENCODING_ZLIB
:
2175 * VNC_ENCODING_ZRLE compresses better than VNC_ENCODING_ZLIB.
2176 * So prioritize ZRLE, even if the client hints that it prefers
2179 if ((vs
->features
& VNC_FEATURE_ZRLE_MASK
) == 0) {
2180 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
2181 vs
->vnc_encoding
= enc
;
2184 case VNC_ENCODING_ZRLE
:
2185 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
2186 vs
->vnc_encoding
= enc
;
2188 case VNC_ENCODING_ZYWRLE
:
2189 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
2190 vs
->vnc_encoding
= enc
;
2192 case VNC_ENCODING_DESKTOPRESIZE
:
2193 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
2195 case VNC_ENCODING_DESKTOP_RESIZE_EXT
:
2196 vs
->features
|= VNC_FEATURE_RESIZE_EXT_MASK
;
2198 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2199 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
2201 case VNC_ENCODING_RICH_CURSOR
:
2202 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
2204 case VNC_ENCODING_ALPHA_CURSOR
:
2205 vs
->features
|= VNC_FEATURE_ALPHA_CURSOR_MASK
;
2207 case VNC_ENCODING_EXT_KEY_EVENT
:
2208 send_ext_key_event_ack(vs
);
2210 case VNC_ENCODING_AUDIO
:
2211 send_ext_audio_ack(vs
);
2213 case VNC_ENCODING_WMVi
:
2214 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
2216 case VNC_ENCODING_LED_STATE
:
2217 vs
->features
|= VNC_FEATURE_LED_STATE_MASK
;
2219 case VNC_ENCODING_XVP
:
2220 if (vs
->vd
->power_control
) {
2221 vs
->features
|= VNC_FEATURE_XVP
;
2222 send_xvp_message(vs
, VNC_XVP_CODE_INIT
);
2225 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2226 vs
->tight
->compression
= (enc
& 0x0F);
2228 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2229 if (vs
->vd
->lossy
) {
2230 vs
->tight
->quality
= (enc
& 0x0F);
2234 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2238 vnc_desktop_resize(vs
);
2239 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2240 vnc_led_state_change(vs
);
2241 vnc_cursor_define(vs
);
2244 static void set_pixel_conversion(VncState
*vs
)
2246 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2248 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2249 vs
->write_pixels
= vnc_write_pixels_copy
;
2250 vnc_hextile_set_pixel_conversion(vs
, 0);
2252 vs
->write_pixels
= vnc_write_pixels_generic
;
2253 vnc_hextile_set_pixel_conversion(vs
, 1);
2257 static void send_color_map(VncState
*vs
)
2261 vnc_lock_output(vs
);
2262 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2263 vnc_write_u8(vs
, 0); /* padding */
2264 vnc_write_u16(vs
, 0); /* first color */
2265 vnc_write_u16(vs
, 256); /* # of colors */
2267 for (i
= 0; i
< 256; i
++) {
2268 PixelFormat
*pf
= &vs
->client_pf
;
2270 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2271 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2272 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2274 vnc_unlock_output(vs
);
2277 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2278 int big_endian_flag
, int true_color_flag
,
2279 int red_max
, int green_max
, int blue_max
,
2280 int red_shift
, int green_shift
, int blue_shift
)
2282 if (!true_color_flag
) {
2283 /* Expose a reasonable default 256 color map */
2293 switch (bits_per_pixel
) {
2299 vnc_client_error(vs
);
2303 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2304 vs
->client_pf
.rbits
= ctpopl(red_max
);
2305 vs
->client_pf
.rshift
= red_shift
;
2306 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2307 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2308 vs
->client_pf
.gbits
= ctpopl(green_max
);
2309 vs
->client_pf
.gshift
= green_shift
;
2310 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2311 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2312 vs
->client_pf
.bbits
= ctpopl(blue_max
);
2313 vs
->client_pf
.bshift
= blue_shift
;
2314 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2315 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2316 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2317 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2318 vs
->client_be
= big_endian_flag
;
2320 if (!true_color_flag
) {
2324 set_pixel_conversion(vs
);
2326 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2327 graphic_hw_update(vs
->vd
->dcl
.con
);
2330 static void pixel_format_message (VncState
*vs
) {
2331 char pad
[3] = { 0, 0, 0 };
2333 vs
->client_pf
= qemu_default_pixelformat(32);
2335 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2336 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2338 #ifdef HOST_WORDS_BIGENDIAN
2339 vnc_write_u8(vs
, 1); /* big-endian-flag */
2341 vnc_write_u8(vs
, 0); /* big-endian-flag */
2343 vnc_write_u8(vs
, 1); /* true-color-flag */
2344 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2345 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2346 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2347 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2348 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2349 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2350 vnc_write(vs
, pad
, 3); /* padding */
2352 vnc_hextile_set_pixel_conversion(vs
, 0);
2353 vs
->write_pixels
= vnc_write_pixels_copy
;
2356 static void vnc_colordepth(VncState
*vs
)
2358 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2359 /* Sending a WMVi message to notify the client*/
2360 vnc_lock_output(vs
);
2361 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2362 vnc_write_u8(vs
, 0);
2363 vnc_write_u16(vs
, 1); /* number of rects */
2364 vnc_framebuffer_update(vs
, 0, 0,
2368 pixel_format_message(vs
);
2369 vnc_unlock_output(vs
);
2372 set_pixel_conversion(vs
);
2376 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2381 VncDisplay
*vd
= vs
->vd
;
2384 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2388 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2392 set_pixel_format(vs
, read_u8(data
, 4),
2393 read_u8(data
, 6), read_u8(data
, 7),
2394 read_u16(data
, 8), read_u16(data
, 10),
2395 read_u16(data
, 12), read_u8(data
, 14),
2396 read_u8(data
, 15), read_u8(data
, 16));
2398 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2403 limit
= read_u16(data
, 2);
2405 return 4 + (limit
* 4);
2407 limit
= read_u16(data
, 2);
2409 for (i
= 0; i
< limit
; i
++) {
2410 int32_t val
= read_s32(data
, 4 + (i
* 4));
2411 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2414 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2416 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2420 framebuffer_update_request(vs
,
2421 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2422 read_u16(data
, 6), read_u16(data
, 8));
2424 case VNC_MSG_CLIENT_KEY_EVENT
:
2428 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2430 case VNC_MSG_CLIENT_POINTER_EVENT
:
2434 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2436 case VNC_MSG_CLIENT_CUT_TEXT
:
2441 uint32_t dlen
= read_u32(data
, 4);
2442 if (dlen
> (1 << 20)) {
2443 error_report("vnc: client_cut_text msg payload has %u bytes"
2444 " which exceeds our limit of 1MB.", dlen
);
2445 vnc_client_error(vs
);
2453 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2455 case VNC_MSG_CLIENT_XVP
:
2456 if (!(vs
->features
& VNC_FEATURE_XVP
)) {
2457 error_report("vnc: xvp client message while disabled");
2458 vnc_client_error(vs
);
2465 uint8_t version
= read_u8(data
, 2);
2466 uint8_t action
= read_u8(data
, 3);
2469 error_report("vnc: xvp client message version %d != 1",
2471 vnc_client_error(vs
);
2476 case VNC_XVP_ACTION_SHUTDOWN
:
2477 qemu_system_powerdown_request();
2479 case VNC_XVP_ACTION_REBOOT
:
2480 send_xvp_message(vs
, VNC_XVP_CODE_FAIL
);
2482 case VNC_XVP_ACTION_RESET
:
2483 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
2486 send_xvp_message(vs
, VNC_XVP_CODE_FAIL
);
2491 case VNC_MSG_CLIENT_QEMU
:
2495 switch (read_u8(data
, 1)) {
2496 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2500 ext_key_event(vs
, read_u16(data
, 2),
2501 read_u32(data
, 4), read_u32(data
, 8));
2503 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2507 switch (read_u16 (data
, 2)) {
2508 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2509 trace_vnc_msg_client_audio_enable(vs
, vs
->ioc
);
2512 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2513 trace_vnc_msg_client_audio_disable(vs
, vs
->ioc
);
2516 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2519 switch (read_u8(data
, 4)) {
2520 case 0: vs
->as
.fmt
= AUDIO_FORMAT_U8
; break;
2521 case 1: vs
->as
.fmt
= AUDIO_FORMAT_S8
; break;
2522 case 2: vs
->as
.fmt
= AUDIO_FORMAT_U16
; break;
2523 case 3: vs
->as
.fmt
= AUDIO_FORMAT_S16
; break;
2524 case 4: vs
->as
.fmt
= AUDIO_FORMAT_U32
; break;
2525 case 5: vs
->as
.fmt
= AUDIO_FORMAT_S32
; break;
2527 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2528 vnc_client_error(vs
);
2531 vs
->as
.nchannels
= read_u8(data
, 5);
2532 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2533 VNC_DEBUG("Invalid audio channel count %d\n",
2535 vnc_client_error(vs
);
2538 freq
= read_u32(data
, 6);
2539 /* No official limit for protocol, but 48khz is a sensible
2540 * upper bound for trustworthy clients, and this limit
2541 * protects calculations involving 'vs->as.freq' later.
2544 VNC_DEBUG("Invalid audio frequency %u > 48000", freq
);
2545 vnc_client_error(vs
);
2549 trace_vnc_msg_client_audio_format(
2550 vs
, vs
->ioc
, vs
->as
.fmt
, vs
->as
.nchannels
, vs
->as
.freq
);
2553 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 4));
2554 vnc_client_error(vs
);
2560 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2561 vnc_client_error(vs
);
2565 case VNC_MSG_CLIENT_SET_DESKTOP_SIZE
:
2575 screens
= read_u8(data
, 6);
2576 size
= 8 + screens
* 16;
2580 w
= read_u16(data
, 2);
2581 h
= read_u16(data
, 4);
2583 trace_vnc_msg_client_set_desktop_size(vs
, vs
->ioc
, w
, h
, screens
);
2584 if (dpy_ui_info_supported(vs
->vd
->dcl
.con
)) {
2586 memset(&info
, 0, sizeof(info
));
2589 dpy_set_ui_info(vs
->vd
->dcl
.con
, &info
);
2590 vnc_desktop_resize_ext(vs
, 4 /* Request forwarded */);
2592 vnc_desktop_resize_ext(vs
, 3 /* Invalid screen layout */);
2598 VNC_DEBUG("Msg: %d\n", data
[0]);
2599 vnc_client_error(vs
);
2603 vnc_update_throttle_offset(vs
);
2604 vnc_read_when(vs
, protocol_client_msg
, 1);
2608 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2614 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2615 switch (vs
->vd
->share_policy
) {
2616 case VNC_SHARE_POLICY_IGNORE
:
2618 * Ignore the shared flag. Nothing to do here.
2620 * Doesn't conform to the rfb spec but is traditional qemu
2621 * behavior, thus left here as option for compatibility
2625 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2627 * Policy: Allow clients ask for exclusive access.
2629 * Implementation: When a client asks for exclusive access,
2630 * disconnect all others. Shared connects are allowed as long
2631 * as no exclusive connection exists.
2633 * This is how the rfb spec suggests to handle the shared flag.
2635 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2637 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2641 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2642 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2645 vnc_disconnect_start(client
);
2648 if (mode
== VNC_SHARE_MODE_SHARED
) {
2649 if (vs
->vd
->num_exclusive
> 0) {
2650 vnc_disconnect_start(vs
);
2655 case VNC_SHARE_POLICY_FORCE_SHARED
:
2657 * Policy: Shared connects only.
2658 * Implementation: Disallow clients asking for exclusive access.
2660 * Useful for shared desktop sessions where you don't want
2661 * someone forgetting to say -shared when running the vnc
2662 * client disconnect everybody else.
2664 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2665 vnc_disconnect_start(vs
);
2670 vnc_set_share_mode(vs
, mode
);
2672 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2673 vnc_disconnect_start(vs
);
2677 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
2678 pixman_image_get_width(vs
->vd
->server
) >= 0);
2679 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
2680 pixman_image_get_height(vs
->vd
->server
) >= 0);
2681 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2682 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2683 vnc_write_u16(vs
, vs
->client_width
);
2684 vnc_write_u16(vs
, vs
->client_height
);
2686 pixel_format_message(vs
);
2689 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2690 if (size
> sizeof(buf
)) {
2694 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2697 vnc_write_u32(vs
, size
);
2698 vnc_write(vs
, buf
, size
);
2701 vnc_client_cache_auth(vs
);
2702 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2704 vnc_read_when(vs
, protocol_client_msg
, 1);
2709 void start_client_init(VncState
*vs
)
2711 vnc_read_when(vs
, protocol_client_init
, 1);
2714 static void authentication_failed(VncState
*vs
)
2716 vnc_write_u32(vs
, 1); /* Reject auth */
2717 if (vs
->minor
>= 8) {
2718 static const char err
[] = "Authentication failed";
2719 vnc_write_u32(vs
, sizeof(err
));
2720 vnc_write(vs
, err
, sizeof(err
));
2723 vnc_client_error(vs
);
2726 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2728 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2730 unsigned char key
[8];
2731 time_t now
= time(NULL
);
2732 QCryptoCipher
*cipher
= NULL
;
2735 if (!vs
->vd
->password
) {
2736 trace_vnc_auth_fail(vs
, vs
->auth
, "password is not set", "");
2739 if (vs
->vd
->expires
< now
) {
2740 trace_vnc_auth_fail(vs
, vs
->auth
, "password is expired", "");
2744 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2746 /* Calculate the expected challenge response */
2747 pwlen
= strlen(vs
->vd
->password
);
2748 for (i
=0; i
<sizeof(key
); i
++)
2749 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2751 cipher
= qcrypto_cipher_new(
2752 QCRYPTO_CIPHER_ALG_DES_RFB
,
2753 QCRYPTO_CIPHER_MODE_ECB
,
2754 key
, G_N_ELEMENTS(key
),
2757 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot create cipher",
2758 error_get_pretty(err
));
2763 if (qcrypto_cipher_encrypt(cipher
,
2766 VNC_AUTH_CHALLENGE_SIZE
,
2768 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot encrypt challenge response",
2769 error_get_pretty(err
));
2774 /* Compare expected vs actual challenge response */
2775 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2776 trace_vnc_auth_fail(vs
, vs
->auth
, "mis-matched challenge response", "");
2779 trace_vnc_auth_pass(vs
, vs
->auth
);
2780 vnc_write_u32(vs
, 0); /* Accept auth */
2783 start_client_init(vs
);
2786 qcrypto_cipher_free(cipher
);
2790 authentication_failed(vs
);
2791 qcrypto_cipher_free(cipher
);
2795 void start_auth_vnc(VncState
*vs
)
2799 if (qcrypto_random_bytes(vs
->challenge
, sizeof(vs
->challenge
), &err
)) {
2800 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot get random bytes",
2801 error_get_pretty(err
));
2803 authentication_failed(vs
);
2807 /* Send client a 'random' challenge */
2808 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2811 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2815 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2817 /* We only advertise 1 auth scheme at a time, so client
2818 * must pick the one we sent. Verify this */
2819 if (data
[0] != vs
->auth
) { /* Reject auth */
2820 trace_vnc_auth_reject(vs
, vs
->auth
, (int)data
[0]);
2821 authentication_failed(vs
);
2822 } else { /* Accept requested auth */
2823 trace_vnc_auth_start(vs
, vs
->auth
);
2826 if (vs
->minor
>= 8) {
2827 vnc_write_u32(vs
, 0); /* Accept auth completion */
2830 trace_vnc_auth_pass(vs
, vs
->auth
);
2831 start_client_init(vs
);
2838 case VNC_AUTH_VENCRYPT
:
2839 start_auth_vencrypt(vs
);
2842 #ifdef CONFIG_VNC_SASL
2844 start_auth_sasl(vs
);
2846 #endif /* CONFIG_VNC_SASL */
2848 default: /* Should not be possible, but just in case */
2849 trace_vnc_auth_fail(vs
, vs
->auth
, "Unhandled auth method", "");
2850 authentication_failed(vs
);
2856 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2860 memcpy(local
, version
, 12);
2863 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2864 VNC_DEBUG("Malformed protocol version %s\n", local
);
2865 vnc_client_error(vs
);
2868 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2869 if (vs
->major
!= 3 ||
2875 VNC_DEBUG("Unsupported client version\n");
2876 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2878 vnc_client_error(vs
);
2881 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2882 * as equivalent to v3.3 by servers
2884 if (vs
->minor
== 4 || vs
->minor
== 5)
2887 if (vs
->minor
== 3) {
2888 trace_vnc_auth_start(vs
, vs
->auth
);
2889 if (vs
->auth
== VNC_AUTH_NONE
) {
2890 vnc_write_u32(vs
, vs
->auth
);
2892 trace_vnc_auth_pass(vs
, vs
->auth
);
2893 start_client_init(vs
);
2894 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2895 VNC_DEBUG("Tell client VNC auth\n");
2896 vnc_write_u32(vs
, vs
->auth
);
2900 trace_vnc_auth_fail(vs
, vs
->auth
,
2901 "Unsupported auth method for v3.3", "");
2902 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2904 vnc_client_error(vs
);
2907 vnc_write_u8(vs
, 1); /* num auth */
2908 vnc_write_u8(vs
, vs
->auth
);
2909 vnc_read_when(vs
, protocol_client_auth
, 1);
2916 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2918 struct VncSurface
*vs
= &vd
->guest
;
2920 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2923 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2927 w
= (x
+ w
) / VNC_STAT_RECT
;
2928 h
= (y
+ h
) / VNC_STAT_RECT
;
2932 for (j
= y
; j
<= h
; j
++) {
2933 for (i
= x
; i
<= w
; i
++) {
2934 vs
->lossy_rect
[j
][i
] = 1;
2939 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2942 int sty
= y
/ VNC_STAT_RECT
;
2943 int stx
= x
/ VNC_STAT_RECT
;
2946 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2947 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2949 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2952 /* kernel send buffers are full -> refresh later */
2953 if (vs
->output
.offset
) {
2957 if (!vs
->lossy_rect
[sty
][stx
]) {
2961 vs
->lossy_rect
[sty
][stx
] = 0;
2962 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2963 bitmap_set(vs
->dirty
[y
+ j
],
2964 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2965 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
2973 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2975 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2976 pixman_image_get_width(vd
->server
));
2977 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2978 pixman_image_get_height(vd
->server
));
2983 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2984 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2985 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2987 rect
->updated
= false;
2991 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2993 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2996 vd
->guest
.last_freq_check
= *tv
;
2998 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2999 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
3000 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
3001 int count
= ARRAY_SIZE(rect
->times
);
3002 struct timeval min
, max
;
3004 if (!timerisset(&rect
->times
[count
- 1])) {
3008 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
3009 qemu_timersub(tv
, &max
, &res
);
3011 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
3013 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
3014 memset(rect
->times
, 0, sizeof (rect
->times
));
3018 min
= rect
->times
[rect
->idx
];
3019 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
3020 qemu_timersub(&max
, &min
, &res
);
3022 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
3023 rect
->freq
/= count
;
3024 rect
->freq
= 1. / rect
->freq
;
3030 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
3036 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
3037 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
3039 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
3040 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
3041 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
3053 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
3057 rect
= vnc_stat_rect(vd
, x
, y
);
3058 if (rect
->updated
) {
3061 rect
->times
[rect
->idx
] = *tv
;
3062 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
3063 rect
->updated
= true;
3066 static int vnc_refresh_server_surface(VncDisplay
*vd
)
3068 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
3069 pixman_image_get_width(vd
->server
));
3070 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
3071 pixman_image_get_height(vd
->server
));
3072 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
3073 uint8_t *guest_row0
= NULL
, *server_row0
;
3076 pixman_image_t
*tmpbuf
= NULL
;
3078 struct timeval tv
= { 0, 0 };
3080 if (!vd
->non_adaptive
) {
3081 gettimeofday(&tv
, NULL
);
3082 has_dirty
= vnc_update_stats(vd
, &tv
);
3086 * Walk through the guest dirty map.
3087 * Check and copy modified bits from guest to server surface.
3088 * Update server dirty map.
3090 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
3091 server_stride
= guest_stride
= guest_ll
=
3092 pixman_image_get_stride(vd
->server
);
3093 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
3095 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
3096 int width
= pixman_image_get_width(vd
->server
);
3097 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
3100 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
3101 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
3102 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
3103 guest_ll
= pixman_image_get_width(vd
->guest
.fb
)
3104 * DIV_ROUND_UP(guest_bpp
, 8);
3106 line_bytes
= MIN(server_stride
, guest_ll
);
3110 uint8_t *guest_ptr
, *server_ptr
;
3111 unsigned long offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
3112 height
* VNC_DIRTY_BPL(&vd
->guest
),
3113 y
* VNC_DIRTY_BPL(&vd
->guest
));
3114 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
3115 /* no more dirty bits */
3118 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
3119 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
3121 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
3123 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
3124 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
3125 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
3127 guest_ptr
= guest_row0
+ y
* guest_stride
;
3129 guest_ptr
+= x
* cmp_bytes
;
3131 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
3132 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
3133 int _cmp_bytes
= cmp_bytes
;
3134 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
3137 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
3138 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
3140 assert(_cmp_bytes
>= 0);
3141 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
3144 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
3145 if (!vd
->non_adaptive
) {
3146 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
3149 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3150 set_bit(x
, vs
->dirty
[y
]);
3157 qemu_pixman_image_unref(tmpbuf
);
3161 static void vnc_refresh(DisplayChangeListener
*dcl
)
3163 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
3165 int has_dirty
, rects
= 0;
3167 if (QTAILQ_EMPTY(&vd
->clients
)) {
3168 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
3172 graphic_hw_update(vd
->dcl
.con
);
3174 if (vnc_trylock_display(vd
)) {
3175 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3179 has_dirty
= vnc_refresh_server_surface(vd
);
3180 vnc_unlock_display(vd
);
3182 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
3183 rects
+= vnc_update_client(vs
, has_dirty
);
3184 /* vs might be free()ed here */
3187 if (has_dirty
&& rects
) {
3188 vd
->dcl
.update_interval
/= 2;
3189 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
3190 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
3193 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
3194 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
3195 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
3200 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
3201 bool skipauth
, bool websocket
)
3203 VncState
*vs
= g_new0(VncState
, 1);
3204 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
3207 trace_vnc_client_connect(vs
, sioc
);
3208 vs
->zrle
= g_new0(VncZrle
, 1);
3209 vs
->tight
= g_new0(VncTight
, 1);
3210 vs
->magic
= VNC_MAGIC
;
3212 object_ref(OBJECT(vs
->sioc
));
3213 vs
->ioc
= QIO_CHANNEL(sioc
);
3214 object_ref(OBJECT(vs
->ioc
));
3217 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
3218 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
3219 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
3221 buffer_init(&vs
->tight
->tight
, "vnc-tight/%p", sioc
);
3222 buffer_init(&vs
->tight
->zlib
, "vnc-tight-zlib/%p", sioc
);
3223 buffer_init(&vs
->tight
->gradient
, "vnc-tight-gradient/%p", sioc
);
3224 #ifdef CONFIG_VNC_JPEG
3225 buffer_init(&vs
->tight
->jpeg
, "vnc-tight-jpeg/%p", sioc
);
3227 #ifdef CONFIG_VNC_PNG
3228 buffer_init(&vs
->tight
->png
, "vnc-tight-png/%p", sioc
);
3230 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3231 buffer_init(&vs
->zrle
->zrle
, "vnc-zrle/%p", sioc
);
3232 buffer_init(&vs
->zrle
->fb
, "vnc-zrle-fb/%p", sioc
);
3233 buffer_init(&vs
->zrle
->zlib
, "vnc-zrle-zlib/%p", sioc
);
3236 vs
->auth
= VNC_AUTH_NONE
;
3237 vs
->subauth
= VNC_AUTH_INVALID
;
3240 vs
->auth
= vd
->ws_auth
;
3241 vs
->subauth
= VNC_AUTH_INVALID
;
3243 vs
->auth
= vd
->auth
;
3244 vs
->subauth
= vd
->subauth
;
3247 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3248 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3250 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3251 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3252 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3255 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3256 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3257 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3259 g_source_remove(vs
->ioc_tag
);
3264 vs
->ioc_tag
= qio_channel_add_watch(
3265 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3266 vncws_tls_handshake_io
, vs
, NULL
);
3268 vs
->ioc_tag
= qio_channel_add_watch(
3269 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3270 vncws_handshake_io
, vs
, NULL
);
3273 vs
->ioc_tag
= qio_channel_add_watch(
3274 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
3275 vnc_client_io
, vs
, NULL
);
3278 vnc_client_cache_addr(vs
);
3279 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3280 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3285 vs
->as
.freq
= 44100;
3286 vs
->as
.nchannels
= 2;
3287 vs
->as
.fmt
= AUDIO_FORMAT_S16
;
3288 vs
->as
.endianness
= 0;
3290 qemu_mutex_init(&vs
->output_mutex
);
3291 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3293 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3295 vnc_update_server_surface(vd
);
3298 graphic_hw_update(vd
->dcl
.con
);
3300 if (!vs
->websocket
) {
3301 vnc_start_protocol(vs
);
3304 if (vd
->num_connecting
> vd
->connections_limit
) {
3305 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3306 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3307 vnc_disconnect_start(vs
);
3314 void vnc_start_protocol(VncState
*vs
)
3316 vnc_write(vs
, "RFB 003.008\n", 12);
3318 vnc_read_when(vs
, protocol_version
, 12);
3320 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3321 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3324 static void vnc_listen_io(QIONetListener
*listener
,
3325 QIOChannelSocket
*cioc
,
3328 VncDisplay
*vd
= opaque
;
3329 bool isWebsock
= listener
== vd
->wslistener
;
3331 qio_channel_set_name(QIO_CHANNEL(cioc
),
3332 isWebsock
? "vnc-ws-server" : "vnc-server");
3333 qio_channel_set_delay(QIO_CHANNEL(cioc
), false);
3334 vnc_connect(vd
, cioc
, false, isWebsock
);
3337 static const DisplayChangeListenerOps dcl_ops
= {
3339 .dpy_refresh
= vnc_refresh
,
3340 .dpy_gfx_update
= vnc_dpy_update
,
3341 .dpy_gfx_switch
= vnc_dpy_switch
,
3342 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3343 .dpy_mouse_set
= vnc_mouse_set
,
3344 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3347 void vnc_display_init(const char *id
, Error
**errp
)
3351 if (vnc_display_find(id
) != NULL
) {
3354 vd
= g_malloc0(sizeof(*vd
));
3356 vd
->id
= strdup(id
);
3357 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3359 QTAILQ_INIT(&vd
->clients
);
3360 vd
->expires
= TIME_MAX
;
3362 if (keyboard_layout
) {
3363 trace_vnc_key_map_init(keyboard_layout
);
3364 vd
->kbd_layout
= init_keyboard_layout(name2keysym
,
3365 keyboard_layout
, errp
);
3367 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us", errp
);
3370 if (!vd
->kbd_layout
) {
3374 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3375 vd
->connections_limit
= 32;
3377 qemu_mutex_init(&vd
->mutex
);
3378 vnc_start_worker_thread();
3380 vd
->dcl
.ops
= &dcl_ops
;
3381 register_displaychangelistener(&vd
->dcl
);
3382 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
3386 static void vnc_display_close(VncDisplay
*vd
)
3391 vd
->is_unix
= false;
3394 qio_net_listener_disconnect(vd
->listener
);
3395 object_unref(OBJECT(vd
->listener
));
3397 vd
->listener
= NULL
;
3399 if (vd
->wslistener
) {
3400 qio_net_listener_disconnect(vd
->wslistener
);
3401 object_unref(OBJECT(vd
->wslistener
));
3403 vd
->wslistener
= NULL
;
3405 vd
->auth
= VNC_AUTH_INVALID
;
3406 vd
->subauth
= VNC_AUTH_INVALID
;
3408 object_unref(OBJECT(vd
->tlscreds
));
3409 vd
->tlscreds
= NULL
;
3412 object_unparent(OBJECT(vd
->tlsauthz
));
3413 vd
->tlsauthz
= NULL
;
3415 g_free(vd
->tlsauthzid
);
3416 vd
->tlsauthzid
= NULL
;
3417 if (vd
->lock_key_sync
) {
3418 qemu_remove_led_event_handler(vd
->led
);
3421 #ifdef CONFIG_VNC_SASL
3422 if (vd
->sasl
.authz
) {
3423 object_unparent(OBJECT(vd
->sasl
.authz
));
3424 vd
->sasl
.authz
= NULL
;
3426 g_free(vd
->sasl
.authzid
);
3427 vd
->sasl
.authzid
= NULL
;
3431 int vnc_display_password(const char *id
, const char *password
)
3433 VncDisplay
*vd
= vnc_display_find(id
);
3438 if (vd
->auth
== VNC_AUTH_NONE
) {
3439 error_printf_unless_qmp("If you want use passwords please enable "
3440 "password auth using '-vnc ${dpy},password'.\n");
3444 g_free(vd
->password
);
3445 vd
->password
= g_strdup(password
);
3450 int vnc_display_pw_expire(const char *id
, time_t expires
)
3452 VncDisplay
*vd
= vnc_display_find(id
);
3458 vd
->expires
= expires
;
3462 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3464 SocketAddress
*addr
;
3466 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
3470 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0], NULL
);
3475 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
3476 qapi_free_SocketAddress(addr
);
3479 error_printf_unless_qmp("VNC server running on %s:%s\n",
3482 qapi_free_SocketAddress(addr
);
3485 static QemuOptsList qemu_vnc_opts
= {
3487 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3488 .implied_opt_name
= "vnc",
3492 .type
= QEMU_OPT_STRING
,
3494 .name
= "websocket",
3495 .type
= QEMU_OPT_STRING
,
3497 .name
= "tls-creds",
3498 .type
= QEMU_OPT_STRING
,
3501 .type
= QEMU_OPT_STRING
,
3504 .type
= QEMU_OPT_STRING
,
3507 .type
= QEMU_OPT_NUMBER
,
3509 .name
= "connections",
3510 .type
= QEMU_OPT_NUMBER
,
3513 .type
= QEMU_OPT_NUMBER
,
3516 .type
= QEMU_OPT_BOOL
,
3519 .type
= QEMU_OPT_BOOL
,
3522 .type
= QEMU_OPT_BOOL
,
3524 .name
= "password-secret",
3525 .type
= QEMU_OPT_STRING
,
3528 .type
= QEMU_OPT_BOOL
,
3530 .name
= "lock-key-sync",
3531 .type
= QEMU_OPT_BOOL
,
3533 .name
= "key-delay-ms",
3534 .type
= QEMU_OPT_NUMBER
,
3537 .type
= QEMU_OPT_BOOL
,
3539 .name
= "tls-authz",
3540 .type
= QEMU_OPT_STRING
,
3542 .name
= "sasl-authz",
3543 .type
= QEMU_OPT_STRING
,
3546 .type
= QEMU_OPT_BOOL
,
3548 .name
= "non-adaptive",
3549 .type
= QEMU_OPT_BOOL
,
3552 .type
= QEMU_OPT_STRING
,
3554 .name
= "power-control",
3555 .type
= QEMU_OPT_BOOL
,
3557 { /* end of list */ }
3563 vnc_display_setup_auth(int *auth
,
3565 QCryptoTLSCreds
*tlscreds
,
3572 * We have a choice of 3 authentication options
3578 * The channel can be run in 2 modes
3583 * And TLS can use 2 types of credentials
3588 * We thus have 9 possible logical combinations
3593 * 4. tls + anon + none
3594 * 5. tls + anon + vnc
3595 * 6. tls + anon + sasl
3596 * 7. tls + x509 + none
3597 * 8. tls + x509 + vnc
3598 * 9. tls + x509 + sasl
3600 * These need to be mapped into the VNC auth schemes
3601 * in an appropriate manner. In regular VNC, all the
3602 * TLS options get mapped into VNC_AUTH_VENCRYPT
3605 * In websockets, the https:// protocol already provides
3606 * TLS support, so there is no need to make use of the
3607 * VeNCrypt extension. Furthermore, websockets browser
3608 * clients could not use VeNCrypt even if they wanted to,
3609 * as they cannot control when the TLS handshake takes
3610 * place. Thus there is no option but to rely on https://,
3611 * meaning combinations 4->6 and 7->9 will be mapped to
3612 * VNC auth schemes in the same way as combos 1->3.
3614 * Regardless of fact that we have a different mapping to
3615 * VNC auth mechs for plain VNC vs websockets VNC, the end
3616 * result has the same security characteristics.
3618 if (websocket
|| !tlscreds
) {
3620 VNC_DEBUG("Initializing VNC server with password auth\n");
3621 *auth
= VNC_AUTH_VNC
;
3623 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3624 *auth
= VNC_AUTH_SASL
;
3626 VNC_DEBUG("Initializing VNC server with no auth\n");
3627 *auth
= VNC_AUTH_NONE
;
3629 *subauth
= VNC_AUTH_INVALID
;
3631 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3632 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3633 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3634 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3636 if (!is_x509
&& !is_anon
) {
3638 "Unsupported TLS cred type %s",
3639 object_get_typename(OBJECT(tlscreds
)));
3642 *auth
= VNC_AUTH_VENCRYPT
;
3645 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3646 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3648 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3649 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3654 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3655 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3657 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3658 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3662 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3663 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3665 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3666 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3674 static int vnc_display_get_address(const char *addrstr
,
3683 SocketAddress
**retaddr
,
3687 SocketAddress
*addr
= NULL
;
3689 addr
= g_new0(SocketAddress
, 1);
3691 if (strncmp(addrstr
, "unix:", 5) == 0) {
3692 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
3693 addr
->u
.q_unix
.path
= g_strdup(addrstr
+ 5);
3696 error_setg(errp
, "UNIX sockets not supported with websock");
3701 error_setg(errp
, "Port range not support with UNIX socket");
3708 unsigned long long baseport
= 0;
3709 InetSocketAddress
*inet
;
3711 port
= strrchr(addrstr
, ':');
3717 error_setg(errp
, "no vnc port specified");
3721 hostlen
= port
- addrstr
;
3723 if (*port
== '\0') {
3724 error_setg(errp
, "vnc port cannot be empty");
3729 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
3730 inet
= &addr
->u
.inet
;
3731 if (addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3732 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3734 inet
->host
= g_strndup(addrstr
, hostlen
);
3736 /* plain VNC port is just an offset, for websocket
3737 * port is absolute */
3739 if (g_str_equal(addrstr
, "") ||
3740 g_str_equal(addrstr
, "on")) {
3741 if (displaynum
== -1) {
3742 error_setg(errp
, "explicit websocket port is required");
3745 inet
->port
= g_strdup_printf(
3746 "%d", displaynum
+ 5700);
3748 inet
->has_to
= true;
3749 inet
->to
= to
+ 5700;
3752 inet
->port
= g_strdup(port
);
3755 int offset
= reverse
? 0 : 5900;
3756 if (parse_uint_full(port
, &baseport
, 10) < 0) {
3757 error_setg(errp
, "can't convert to a number: %s", port
);
3760 if (baseport
> 65535 ||
3761 baseport
+ offset
> 65535) {
3762 error_setg(errp
, "port %s out of range", port
);
3765 inet
->port
= g_strdup_printf(
3766 "%d", (int)baseport
+ offset
);
3769 inet
->has_to
= true;
3770 inet
->to
= to
+ offset
;
3775 inet
->has_ipv4
= has_ipv4
;
3777 inet
->has_ipv6
= has_ipv6
;
3786 qapi_free_SocketAddress(addr
);
3791 static void vnc_free_addresses(SocketAddress
***retsaddr
,
3796 for (i
= 0; i
< *retnsaddr
; i
++) {
3797 qapi_free_SocketAddress((*retsaddr
)[i
]);
3805 static int vnc_display_get_addresses(QemuOpts
*opts
,
3807 SocketAddress
***retsaddr
,
3809 SocketAddress
***retwsaddr
,
3813 SocketAddress
*saddr
= NULL
;
3814 SocketAddress
*wsaddr
= NULL
;
3815 QemuOptsIter addriter
;
3817 int to
= qemu_opt_get_number(opts
, "to", 0);
3818 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3819 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3820 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3821 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3822 int displaynum
= -1;
3830 addr
= qemu_opt_get(opts
, "vnc");
3831 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3835 if (qemu_opt_get(opts
, "websocket") &&
3836 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3838 "SHA1 hash support is required for websockets");
3842 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3843 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3845 rv
= vnc_display_get_address(addr
, false, reverse
, 0, to
,
3852 /* Historical compat - first listen address can be used
3853 * to set the default websocket port
3855 if (displaynum
== -1) {
3858 *retsaddr
= g_renew(SocketAddress
*, *retsaddr
, *retnsaddr
+ 1);
3859 (*retsaddr
)[(*retnsaddr
)++] = saddr
;
3862 /* If we had multiple primary displays, we don't do defaults
3863 * for websocket, and require explicit config instead. */
3864 if (*retnsaddr
> 1) {
3868 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3869 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3870 if (vnc_display_get_address(addr
, true, reverse
, displaynum
, to
,
3873 &wsaddr
, errp
) < 0) {
3877 /* Historical compat - if only a single listen address was
3878 * provided, then this is used to set the default listen
3879 * address for websocket too
3881 if (*retnsaddr
== 1 &&
3882 (*retsaddr
)[0]->type
== SOCKET_ADDRESS_TYPE_INET
&&
3883 wsaddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3884 g_str_equal(wsaddr
->u
.inet
.host
, "") &&
3885 !g_str_equal((*retsaddr
)[0]->u
.inet
.host
, "")) {
3886 g_free(wsaddr
->u
.inet
.host
);
3887 wsaddr
->u
.inet
.host
= g_strdup((*retsaddr
)[0]->u
.inet
.host
);
3890 *retwsaddr
= g_renew(SocketAddress
*, *retwsaddr
, *retnwsaddr
+ 1);
3891 (*retwsaddr
)[(*retnwsaddr
)++] = wsaddr
;
3897 vnc_free_addresses(retsaddr
, retnsaddr
);
3898 vnc_free_addresses(retwsaddr
, retnwsaddr
);
3903 static int vnc_display_connect(VncDisplay
*vd
,
3904 SocketAddress
**saddr
,
3906 SocketAddress
**wsaddr
,
3910 /* connect to viewer */
3911 QIOChannelSocket
*sioc
= NULL
;
3913 error_setg(errp
, "Cannot use websockets in reverse mode");
3917 error_setg(errp
, "Expected a single address in reverse mode");
3920 /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3921 vd
->is_unix
= saddr
[0]->type
== SOCKET_ADDRESS_TYPE_UNIX
;
3922 sioc
= qio_channel_socket_new();
3923 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3924 if (qio_channel_socket_connect_sync(sioc
, saddr
[0], errp
) < 0) {
3925 object_unref(OBJECT(sioc
));
3928 vnc_connect(vd
, sioc
, false, false);
3929 object_unref(OBJECT(sioc
));
3934 static int vnc_display_listen(VncDisplay
*vd
,
3935 SocketAddress
**saddr
,
3937 SocketAddress
**wsaddr
,
3944 vd
->listener
= qio_net_listener_new();
3945 qio_net_listener_set_name(vd
->listener
, "vnc-listen");
3946 for (i
= 0; i
< nsaddr
; i
++) {
3947 if (qio_net_listener_open_sync(vd
->listener
,
3954 qio_net_listener_set_client_func(vd
->listener
,
3955 vnc_listen_io
, vd
, NULL
);
3959 vd
->wslistener
= qio_net_listener_new();
3960 qio_net_listener_set_name(vd
->wslistener
, "vnc-ws-listen");
3961 for (i
= 0; i
< nwsaddr
; i
++) {
3962 if (qio_net_listener_open_sync(vd
->wslistener
,
3969 qio_net_listener_set_client_func(vd
->wslistener
,
3970 vnc_listen_io
, vd
, NULL
);
3977 void vnc_display_open(const char *id
, Error
**errp
)
3979 VncDisplay
*vd
= vnc_display_find(id
);
3980 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
3981 SocketAddress
**saddr
= NULL
, **wsaddr
= NULL
;
3982 size_t nsaddr
, nwsaddr
;
3983 const char *share
, *device_id
;
3985 bool password
= false;
3986 bool reverse
= false;
3989 const char *tlsauthz
;
3990 const char *saslauthz
;
3991 int lock_key_sync
= 1;
3993 const char *audiodev
;
3994 const char *passwordSecret
;
3997 error_setg(errp
, "VNC display not active");
4000 vnc_display_close(vd
);
4006 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
4007 if (vnc_display_get_addresses(opts
, reverse
, &saddr
, &nsaddr
,
4008 &wsaddr
, &nwsaddr
, errp
) < 0) {
4013 passwordSecret
= qemu_opt_get(opts
, "password-secret");
4014 if (passwordSecret
) {
4015 if (qemu_opt_get(opts
, "password")) {
4017 "'password' flag is redundant with 'password-secret'");
4020 vd
->password
= qcrypto_secret_lookup_as_utf8(passwordSecret
,
4022 if (!vd
->password
) {
4027 password
= qemu_opt_get_bool(opts
, "password", false);
4030 if (fips_get_state()) {
4032 "VNC password auth disabled due to FIPS mode, "
4033 "consider using the VeNCrypt or SASL authentication "
4034 "methods as an alternative");
4037 if (!qcrypto_cipher_supports(
4038 QCRYPTO_CIPHER_ALG_DES_RFB
, QCRYPTO_CIPHER_MODE_ECB
)) {
4040 "Cipher backend does not support DES RFB algorithm");
4045 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
4046 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 10);
4047 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
4048 #ifndef CONFIG_VNC_SASL
4050 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
4053 #endif /* CONFIG_VNC_SASL */
4054 credid
= qemu_opt_get(opts
, "tls-creds");
4057 creds
= object_resolve_path_component(
4058 object_get_objects_root(), credid
);
4060 error_setg(errp
, "No TLS credentials with id '%s'",
4064 vd
->tlscreds
= (QCryptoTLSCreds
*)
4065 object_dynamic_cast(creds
,
4066 TYPE_QCRYPTO_TLS_CREDS
);
4067 if (!vd
->tlscreds
) {
4068 error_setg(errp
, "Object with id '%s' is not TLS credentials",
4072 object_ref(OBJECT(vd
->tlscreds
));
4074 if (vd
->tlscreds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
4076 "Expecting TLS credentials with a server endpoint");
4080 tlsauthz
= qemu_opt_get(opts
, "tls-authz");
4081 if (tlsauthz
&& !vd
->tlscreds
) {
4082 error_setg(errp
, "'tls-authz' provided but TLS is not enabled");
4086 saslauthz
= qemu_opt_get(opts
, "sasl-authz");
4087 if (saslauthz
&& !sasl
) {
4088 error_setg(errp
, "'sasl-authz' provided but SASL auth is not enabled");
4092 share
= qemu_opt_get(opts
, "share");
4094 if (strcmp(share
, "ignore") == 0) {
4095 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
4096 } else if (strcmp(share
, "allow-exclusive") == 0) {
4097 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4098 } else if (strcmp(share
, "force-shared") == 0) {
4099 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
4101 error_setg(errp
, "unknown vnc share= option");
4105 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4107 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
4109 #ifdef CONFIG_VNC_JPEG
4110 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
4112 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
4113 /* adaptive updates are only used with tight encoding and
4114 * if lossy updates are enabled so we can disable all the
4115 * calculations otherwise */
4117 vd
->non_adaptive
= true;
4120 vd
->power_control
= qemu_opt_get_bool(opts
, "power-control", false);
4123 vd
->tlsauthzid
= g_strdup(tlsauthz
);
4125 #ifdef CONFIG_VNC_SASL
4128 vd
->sasl
.authzid
= g_strdup(saslauthz
);
4133 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
4134 vd
->tlscreds
, password
,
4135 sasl
, false, errp
) < 0) {
4138 trace_vnc_auth_init(vd
, 0, vd
->auth
, vd
->subauth
);
4140 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
4141 vd
->tlscreds
, password
,
4142 sasl
, true, errp
) < 0) {
4145 trace_vnc_auth_init(vd
, 1, vd
->ws_auth
, vd
->ws_subauth
);
4147 #ifdef CONFIG_VNC_SASL
4149 int saslErr
= sasl_server_init(NULL
, "qemu");
4151 if (saslErr
!= SASL_OK
) {
4152 error_setg(errp
, "Failed to initialize SASL auth: %s",
4153 sasl_errstring(saslErr
, NULL
, NULL
));
4158 vd
->lock_key_sync
= lock_key_sync
;
4159 if (lock_key_sync
) {
4160 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
4164 audiodev
= qemu_opt_get(opts
, "audiodev");
4166 vd
->audio_state
= audio_state_by_name(audiodev
);
4167 if (!vd
->audio_state
) {
4168 error_setg(errp
, "Audiodev '%s' not found", audiodev
);
4173 device_id
= qemu_opt_get(opts
, "display");
4175 int head
= qemu_opt_get_number(opts
, "head", 0);
4178 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
4180 error_propagate(errp
, err
);
4187 if (con
!= vd
->dcl
.con
) {
4188 qkbd_state_free(vd
->kbd
);
4189 unregister_displaychangelistener(&vd
->dcl
);
4191 register_displaychangelistener(&vd
->dcl
);
4192 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
4194 qkbd_state_set_delay(vd
->kbd
, key_delay_ms
);
4196 if (saddr
== NULL
) {
4201 if (vnc_display_connect(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4205 if (vnc_display_listen(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4210 if (qemu_opt_get(opts
, "to")) {
4211 vnc_display_print_local_addr(vd
);
4215 vnc_free_addresses(&saddr
, &nsaddr
);
4216 vnc_free_addresses(&wsaddr
, &nwsaddr
);
4220 vnc_display_close(vd
);
4224 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
4226 VncDisplay
*vd
= vnc_display_find(id
);
4227 QIOChannelSocket
*sioc
;
4233 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4235 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4236 vnc_connect(vd
, sioc
, skipauth
, false);
4237 object_unref(OBJECT(sioc
));
4241 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4246 id
= g_strdup("default");
4247 while (qemu_opts_find(olist
, id
)) {
4249 id
= g_strdup_printf("vnc%d", i
++);
4251 qemu_opts_set_id(opts
, id
);
4254 void vnc_parse(const char *str
)
4256 QemuOptsList
*olist
= qemu_find_opts("vnc");
4257 QemuOpts
*opts
= qemu_opts_parse_noisily(olist
, str
, !is_help_option(str
));
4264 id
= qemu_opts_id(opts
);
4266 /* auto-assign id if not present */
4267 vnc_auto_assign_id(olist
, opts
);
4271 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4273 Error
*local_err
= NULL
;
4274 char *id
= (char *)qemu_opts_id(opts
);
4277 vnc_display_init(id
, &local_err
);
4279 error_propagate(errp
, local_err
);
4282 vnc_display_open(id
, &local_err
);
4283 if (local_err
!= NULL
) {
4284 error_propagate(errp
, local_err
);
4290 static void vnc_register_config(void)
4292 qemu_add_opts(&qemu_vnc_opts
);
4294 opts_init(vnc_register_config
);