2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu/osdep.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/error-report.h"
33 #include "qemu/option.h"
34 #include "qemu/sockets.h"
35 #include "qemu/timer.h"
37 #include "qemu/config-file.h"
38 #include "qapi/qapi-events.h"
39 #include "qapi/error.h"
40 #include "qapi/qapi-commands-ui.h"
42 #include "crypto/hash.h"
43 #include "crypto/tlscredsanon.h"
44 #include "crypto/tlscredsx509.h"
45 #include "qom/object_interfaces.h"
46 #include "qemu/cutils.h"
47 #include "io/dns-resolver.h"
49 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
50 #define VNC_REFRESH_INTERVAL_INC 50
51 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
52 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
53 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
55 #include "vnc_keysym.h"
56 #include "crypto/cipher.h"
58 static QTAILQ_HEAD(, VncDisplay
) vnc_displays
=
59 QTAILQ_HEAD_INITIALIZER(vnc_displays
);
61 static int vnc_cursor_define(VncState
*vs
);
62 static void vnc_update_throttle_offset(VncState
*vs
);
64 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
67 static const char *mn
[] = {
69 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
70 [VNC_SHARE_MODE_SHARED
] = "shared",
71 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
72 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
74 fprintf(stderr
, "%s/%p: %s -> %s\n", __func__
,
75 vs
->ioc
, mn
[vs
->share_mode
], mn
[mode
]);
78 switch (vs
->share_mode
) {
79 case VNC_SHARE_MODE_CONNECTING
:
80 vs
->vd
->num_connecting
--;
82 case VNC_SHARE_MODE_SHARED
:
85 case VNC_SHARE_MODE_EXCLUSIVE
:
86 vs
->vd
->num_exclusive
--;
92 vs
->share_mode
= mode
;
94 switch (vs
->share_mode
) {
95 case VNC_SHARE_MODE_CONNECTING
:
96 vs
->vd
->num_connecting
++;
98 case VNC_SHARE_MODE_SHARED
:
101 case VNC_SHARE_MODE_EXCLUSIVE
:
102 vs
->vd
->num_exclusive
++;
110 static void vnc_init_basic_info(SocketAddress
*addr
,
114 switch (addr
->type
) {
115 case SOCKET_ADDRESS_TYPE_INET
:
116 info
->host
= g_strdup(addr
->u
.inet
.host
);
117 info
->service
= g_strdup(addr
->u
.inet
.port
);
118 if (addr
->u
.inet
.ipv6
) {
119 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
121 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
125 case SOCKET_ADDRESS_TYPE_UNIX
:
126 info
->host
= g_strdup("");
127 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
128 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
131 case SOCKET_ADDRESS_TYPE_VSOCK
:
132 case SOCKET_ADDRESS_TYPE_FD
:
133 error_setg(errp
, "Unsupported socket address type %s",
134 SocketAddressType_str(addr
->type
));
143 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
147 SocketAddress
*addr
= NULL
;
150 error_setg(errp
, "No listener socket available");
154 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
159 vnc_init_basic_info(addr
, info
, errp
);
160 qapi_free_SocketAddress(addr
);
163 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
167 SocketAddress
*addr
= NULL
;
169 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
174 vnc_init_basic_info(addr
, info
, errp
);
175 qapi_free_SocketAddress(addr
);
178 static const char *vnc_auth_name(VncDisplay
*vd
) {
180 case VNC_AUTH_INVALID
:
196 case VNC_AUTH_VENCRYPT
:
197 switch (vd
->subauth
) {
198 case VNC_AUTH_VENCRYPT_PLAIN
:
199 return "vencrypt+plain";
200 case VNC_AUTH_VENCRYPT_TLSNONE
:
201 return "vencrypt+tls+none";
202 case VNC_AUTH_VENCRYPT_TLSVNC
:
203 return "vencrypt+tls+vnc";
204 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
205 return "vencrypt+tls+plain";
206 case VNC_AUTH_VENCRYPT_X509NONE
:
207 return "vencrypt+x509+none";
208 case VNC_AUTH_VENCRYPT_X509VNC
:
209 return "vencrypt+x509+vnc";
210 case VNC_AUTH_VENCRYPT_X509PLAIN
:
211 return "vencrypt+x509+plain";
212 case VNC_AUTH_VENCRYPT_TLSSASL
:
213 return "vencrypt+tls+sasl";
214 case VNC_AUTH_VENCRYPT_X509SASL
:
215 return "vencrypt+x509+sasl";
225 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
230 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
234 info
= g_malloc0(sizeof(*info
));
235 vnc_init_basic_info_from_server_addr(vd
->listener
->sioc
[0],
236 qapi_VncServerInfo_base(info
), &err
);
237 info
->has_auth
= true;
238 info
->auth
= g_strdup(vnc_auth_name(vd
));
240 qapi_free_VncServerInfo(info
);
247 static void vnc_client_cache_auth(VncState
*client
)
254 client
->info
->x509_dname
=
255 qcrypto_tls_session_get_peer_name(client
->tls
);
256 client
->info
->has_x509_dname
=
257 client
->info
->x509_dname
!= NULL
;
259 #ifdef CONFIG_VNC_SASL
260 if (client
->sasl
.conn
&&
261 client
->sasl
.username
) {
262 client
->info
->has_sasl_username
= true;
263 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
268 static void vnc_client_cache_addr(VncState
*client
)
272 client
->info
= g_malloc0(sizeof(*client
->info
));
273 vnc_init_basic_info_from_remote_addr(client
->sioc
,
274 qapi_VncClientInfo_base(client
->info
),
277 qapi_free_VncClientInfo(client
->info
);
283 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
291 si
= vnc_server_info_get(vs
->vd
);
297 case QAPI_EVENT_VNC_CONNECTED
:
298 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
));
300 case QAPI_EVENT_VNC_INITIALIZED
:
301 qapi_event_send_vnc_initialized(si
, vs
->info
);
303 case QAPI_EVENT_VNC_DISCONNECTED
:
304 qapi_event_send_vnc_disconnected(si
, vs
->info
);
310 qapi_free_VncServerInfo(si
);
313 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
318 info
= g_malloc0(sizeof(*info
));
320 vnc_init_basic_info_from_remote_addr(client
->sioc
,
321 qapi_VncClientInfo_base(info
),
325 qapi_free_VncClientInfo(info
);
329 info
->websocket
= client
->websocket
;
332 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
333 info
->has_x509_dname
= info
->x509_dname
!= NULL
;
335 #ifdef CONFIG_VNC_SASL
336 if (client
->sasl
.conn
&& client
->sasl
.username
) {
337 info
->has_sasl_username
= true;
338 info
->sasl_username
= g_strdup(client
->sasl
.username
);
345 static VncDisplay
*vnc_display_find(const char *id
)
350 return QTAILQ_FIRST(&vnc_displays
);
352 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
353 if (strcmp(id
, vd
->id
) == 0) {
360 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
362 VncClientInfoList
*cinfo
, *prev
= NULL
;
365 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
366 cinfo
= g_new0(VncClientInfoList
, 1);
367 cinfo
->value
= qmp_query_vnc_client(client
);
374 VncInfo
*qmp_query_vnc(Error
**errp
)
376 VncInfo
*info
= g_malloc0(sizeof(*info
));
377 VncDisplay
*vd
= vnc_display_find(NULL
);
378 SocketAddress
*addr
= NULL
;
380 if (vd
== NULL
|| !vd
->listener
|| !vd
->listener
->nsioc
) {
381 info
->enabled
= false;
383 info
->enabled
= true;
385 /* for compatibility with the original command */
386 info
->has_clients
= true;
387 info
->clients
= qmp_query_client_list(vd
);
389 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0],
395 switch (addr
->type
) {
396 case SOCKET_ADDRESS_TYPE_INET
:
397 info
->host
= g_strdup(addr
->u
.inet
.host
);
398 info
->service
= g_strdup(addr
->u
.inet
.port
);
399 if (addr
->u
.inet
.ipv6
) {
400 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
402 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
406 case SOCKET_ADDRESS_TYPE_UNIX
:
407 info
->host
= g_strdup("");
408 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
409 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
412 case SOCKET_ADDRESS_TYPE_VSOCK
:
413 case SOCKET_ADDRESS_TYPE_FD
:
414 error_setg(errp
, "Unsupported socket address type %s",
415 SocketAddressType_str(addr
->type
));
421 info
->has_host
= true;
422 info
->has_service
= true;
423 info
->has_family
= true;
425 info
->has_auth
= true;
426 info
->auth
= g_strdup(vnc_auth_name(vd
));
429 qapi_free_SocketAddress(addr
);
433 qapi_free_SocketAddress(addr
);
434 qapi_free_VncInfo(info
);
439 static void qmp_query_auth(int auth
, int subauth
,
440 VncPrimaryAuth
*qmp_auth
,
441 VncVencryptSubAuth
*qmp_vencrypt
,
442 bool *qmp_has_vencrypt
);
444 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
448 VncServerInfo2List
*prev
)
450 VncServerInfo2List
*list
;
451 VncServerInfo2
*info
;
455 addr
= qio_channel_socket_get_local_address(ioc
, &err
);
461 info
= g_new0(VncServerInfo2
, 1);
462 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
463 qapi_free_SocketAddress(addr
);
465 qapi_free_VncServerInfo2(info
);
469 info
->websocket
= websocket
;
471 qmp_query_auth(auth
, subauth
, &info
->auth
,
472 &info
->vencrypt
, &info
->has_vencrypt
);
474 list
= g_new0(VncServerInfo2List
, 1);
480 static void qmp_query_auth(int auth
, int subauth
,
481 VncPrimaryAuth
*qmp_auth
,
482 VncVencryptSubAuth
*qmp_vencrypt
,
483 bool *qmp_has_vencrypt
)
487 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
490 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
493 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
496 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
499 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
502 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
504 case VNC_AUTH_VENCRYPT
:
505 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
506 *qmp_has_vencrypt
= true;
508 case VNC_AUTH_VENCRYPT_PLAIN
:
509 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
511 case VNC_AUTH_VENCRYPT_TLSNONE
:
512 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
514 case VNC_AUTH_VENCRYPT_TLSVNC
:
515 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
517 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
518 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
520 case VNC_AUTH_VENCRYPT_X509NONE
:
521 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
523 case VNC_AUTH_VENCRYPT_X509VNC
:
524 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
526 case VNC_AUTH_VENCRYPT_X509PLAIN
:
527 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
529 case VNC_AUTH_VENCRYPT_TLSSASL
:
530 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
532 case VNC_AUTH_VENCRYPT_X509SASL
:
533 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
536 *qmp_has_vencrypt
= false;
541 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
545 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
550 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
552 VncInfo2List
*item
, *prev
= NULL
;
558 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
559 info
= g_new0(VncInfo2
, 1);
560 info
->id
= g_strdup(vd
->id
);
561 info
->clients
= qmp_query_client_list(vd
);
562 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
563 &info
->vencrypt
, &info
->has_vencrypt
);
565 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
567 info
->has_display
= true;
568 info
->display
= g_strdup(dev
->id
);
570 for (i
= 0; vd
->listener
!= NULL
&& i
< vd
->listener
->nsioc
; i
++) {
571 info
->server
= qmp_query_server_entry(
572 vd
->listener
->sioc
[i
], false, vd
->auth
, vd
->subauth
,
575 for (i
= 0; vd
->wslistener
!= NULL
&& i
< vd
->wslistener
->nsioc
; i
++) {
576 info
->server
= qmp_query_server_entry(
577 vd
->wslistener
->sioc
[i
], true, vd
->ws_auth
,
578 vd
->ws_subauth
, info
->server
);
581 item
= g_new0(VncInfo2List
, 1);
590 1) Get the queue working for IO.
591 2) there is some weirdness when using the -S option (the screen is grey
592 and not totally invalidated
593 3) resolutions > 1024
596 static int vnc_update_client(VncState
*vs
, int has_dirty
);
597 static void vnc_disconnect_start(VncState
*vs
);
599 static void vnc_colordepth(VncState
*vs
);
600 static void framebuffer_update_request(VncState
*vs
, int incremental
,
601 int x_position
, int y_position
,
603 static void vnc_refresh(DisplayChangeListener
*dcl
);
604 static int vnc_refresh_server_surface(VncDisplay
*vd
);
606 static int vnc_width(VncDisplay
*vd
)
608 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
609 VNC_DIRTY_PIXELS_PER_BIT
));
612 static int vnc_height(VncDisplay
*vd
)
614 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
617 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
618 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
620 int x
, int y
, int w
, int h
)
622 int width
= vnc_width(vd
);
623 int height
= vnc_height(vd
);
625 /* this is needed this to ensure we updated all affected
626 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
627 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
628 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
632 w
= MIN(x
+ w
, width
) - x
;
633 h
= MIN(y
+ h
, height
);
636 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
637 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
641 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
642 int x
, int y
, int w
, int h
)
644 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
645 struct VncSurface
*s
= &vd
->guest
;
647 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
650 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
653 vnc_write_u16(vs
, x
);
654 vnc_write_u16(vs
, y
);
655 vnc_write_u16(vs
, w
);
656 vnc_write_u16(vs
, h
);
658 vnc_write_s32(vs
, encoding
);
662 static void vnc_desktop_resize(VncState
*vs
)
664 if (vs
->ioc
== NULL
|| !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
667 if (vs
->client_width
== pixman_image_get_width(vs
->vd
->server
) &&
668 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
672 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
673 pixman_image_get_width(vs
->vd
->server
) >= 0);
674 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
675 pixman_image_get_height(vs
->vd
->server
) >= 0);
676 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
677 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
679 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
681 vnc_write_u16(vs
, 1); /* number of rects */
682 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
683 VNC_ENCODING_DESKTOPRESIZE
);
684 vnc_unlock_output(vs
);
688 static void vnc_abort_display_jobs(VncDisplay
*vd
)
692 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
695 vnc_unlock_output(vs
);
697 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
700 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
703 vnc_unlock_output(vs
);
707 int vnc_server_fb_stride(VncDisplay
*vd
)
709 return pixman_image_get_stride(vd
->server
);
712 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
716 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
717 ptr
+= y
* vnc_server_fb_stride(vd
);
718 ptr
+= x
* VNC_SERVER_FB_BYTES
;
722 static void vnc_update_server_surface(VncDisplay
*vd
)
726 qemu_pixman_image_unref(vd
->server
);
729 if (QTAILQ_EMPTY(&vd
->clients
)) {
733 width
= vnc_width(vd
);
734 height
= vnc_height(vd
);
735 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
739 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
740 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
744 static bool vnc_check_pageflip(DisplaySurface
*s1
,
747 return (s1
!= NULL
&&
749 surface_width(s1
) == surface_width(s2
) &&
750 surface_height(s1
) == surface_height(s2
) &&
751 surface_format(s1
) == surface_format(s2
));
755 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
756 DisplaySurface
*surface
)
758 static const char placeholder_msg
[] =
759 "Display output is not active.";
760 static DisplaySurface
*placeholder
;
761 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
762 bool pageflip
= vnc_check_pageflip(vd
->ds
, surface
);
765 if (surface
== NULL
) {
766 if (placeholder
== NULL
) {
767 placeholder
= qemu_create_message_surface(640, 480, placeholder_msg
);
769 surface
= placeholder
;
772 vnc_abort_display_jobs(vd
);
776 qemu_pixman_image_unref(vd
->guest
.fb
);
777 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
778 vd
->guest
.format
= surface
->format
;
781 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
782 surface_width(surface
),
783 surface_height(surface
));
788 vnc_update_server_surface(vd
);
790 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
792 vnc_desktop_resize(vs
);
793 if (vs
->vd
->cursor
) {
794 vnc_cursor_define(vs
);
796 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
797 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
800 vnc_update_throttle_offset(vs
);
805 static void vnc_write_pixels_copy(VncState
*vs
,
806 void *pixels
, int size
)
808 vnc_write(vs
, pixels
, size
);
811 /* slowest but generic code. */
812 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
816 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
817 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
818 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
819 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
821 # error need some bits here if you change VNC_SERVER_FB_FORMAT
823 v
= (r
<< vs
->client_pf
.rshift
) |
824 (g
<< vs
->client_pf
.gshift
) |
825 (b
<< vs
->client_pf
.bshift
);
826 switch (vs
->client_pf
.bytes_per_pixel
) {
856 static void vnc_write_pixels_generic(VncState
*vs
,
857 void *pixels1
, int size
)
861 if (VNC_SERVER_FB_BYTES
== 4) {
862 uint32_t *pixels
= pixels1
;
865 for (i
= 0; i
< n
; i
++) {
866 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
867 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
872 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
876 VncDisplay
*vd
= vs
->vd
;
878 row
= vnc_server_fb_ptr(vd
, x
, y
);
879 for (i
= 0; i
< h
; i
++) {
880 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
881 row
+= vnc_server_fb_stride(vd
);
886 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
889 bool encode_raw
= false;
890 size_t saved_offs
= vs
->output
.offset
;
892 switch(vs
->vnc_encoding
) {
893 case VNC_ENCODING_ZLIB
:
894 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
896 case VNC_ENCODING_HEXTILE
:
897 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
898 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
900 case VNC_ENCODING_TIGHT
:
901 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
903 case VNC_ENCODING_TIGHT_PNG
:
904 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
906 case VNC_ENCODING_ZRLE
:
907 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
909 case VNC_ENCODING_ZYWRLE
:
910 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
917 /* If the client has the same pixel format as our internal buffer and
918 * a RAW encoding would need less space fall back to RAW encoding to
919 * save bandwidth and processing power in the client. */
920 if (!encode_raw
&& vs
->write_pixels
== vnc_write_pixels_copy
&&
921 12 + h
* w
* VNC_SERVER_FB_BYTES
<= (vs
->output
.offset
- saved_offs
)) {
922 vs
->output
.offset
= saved_offs
;
927 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
928 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
934 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
935 int x
, int y
, int visible
)
937 /* can we ask the client(s) to move the pointer ??? */
940 static int vnc_cursor_define(VncState
*vs
)
942 QEMUCursor
*c
= vs
->vd
->cursor
;
945 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
947 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
948 vnc_write_u8(vs
, 0); /* padding */
949 vnc_write_u16(vs
, 1); /* # of rects */
950 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
951 VNC_ENCODING_RICH_CURSOR
);
952 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
953 vnc_write_pixels_generic(vs
, c
->data
, isize
);
954 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
955 vnc_unlock_output(vs
);
961 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
964 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
967 cursor_put(vd
->cursor
);
968 g_free(vd
->cursor_mask
);
971 cursor_get(vd
->cursor
);
972 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
973 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
974 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
976 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
977 vnc_cursor_define(vs
);
981 static int find_and_clear_dirty_height(VncState
*vs
,
982 int y
, int last_x
, int x
, int height
)
986 for (h
= 1; h
< (height
- y
); h
++) {
987 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
990 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
997 * Figure out how much pending data we should allow in the output
998 * buffer before we throttle incremental display updates, and/or
999 * drop audio samples.
1001 * We allow for equiv of 1 full display's worth of FB updates,
1002 * and 1 second of audio samples. If audio backlog was larger
1003 * than that the client would already suffering awful audio
1004 * glitches, so dropping samples is no worse really).
1006 static void vnc_update_throttle_offset(VncState
*vs
)
1009 vs
->client_width
* vs
->client_height
* vs
->client_pf
.bytes_per_pixel
;
1011 if (vs
->audio_cap
) {
1013 switch (vs
->as
.fmt
) {
1028 offset
+= vs
->as
.freq
* bps
* vs
->as
.nchannels
;
1031 /* Put a floor of 1MB on offset, so that if we have a large pending
1032 * buffer and the display is resized to a small size & back again
1033 * we don't suddenly apply a tiny send limit
1035 offset
= MAX(offset
, 1024 * 1024);
1037 if (vs
->throttle_output_offset
!= offset
) {
1038 trace_vnc_client_throttle_threshold(
1039 vs
, vs
->ioc
, vs
->throttle_output_offset
, offset
, vs
->client_width
,
1040 vs
->client_height
, vs
->client_pf
.bytes_per_pixel
, vs
->audio_cap
);
1043 vs
->throttle_output_offset
= offset
;
1046 static bool vnc_should_update(VncState
*vs
)
1048 switch (vs
->update
) {
1049 case VNC_STATE_UPDATE_NONE
:
1051 case VNC_STATE_UPDATE_INCREMENTAL
:
1052 /* Only allow incremental updates if the pending send queue
1053 * is less than the permitted threshold, and the job worker
1054 * is completely idle.
1056 if (vs
->output
.offset
< vs
->throttle_output_offset
&&
1057 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1060 trace_vnc_client_throttle_incremental(
1061 vs
, vs
->ioc
, vs
->job_update
, vs
->output
.offset
);
1063 case VNC_STATE_UPDATE_FORCE
:
1064 /* Only allow forced updates if the pending send queue
1065 * does not contain a previous forced update, and the
1066 * job worker is completely idle.
1068 * Note this means we'll queue a forced update, even if
1069 * the output buffer size is otherwise over the throttle
1072 if (vs
->force_update_offset
== 0 &&
1073 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1076 trace_vnc_client_throttle_forced(
1077 vs
, vs
->ioc
, vs
->job_update
, vs
->force_update_offset
);
1083 static int vnc_update_client(VncState
*vs
, int has_dirty
)
1085 VncDisplay
*vd
= vs
->vd
;
1091 if (vs
->disconnecting
) {
1092 vnc_disconnect_finish(vs
);
1096 vs
->has_dirty
+= has_dirty
;
1097 if (!vnc_should_update(vs
)) {
1101 if (!vs
->has_dirty
&& vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
1106 * Send screen updates to the vnc client using the server
1107 * surface and server dirty map. guest surface updates
1108 * happening in parallel don't disturb us, the next pass will
1109 * send them to the client.
1111 job
= vnc_job_new(vs
);
1113 height
= pixman_image_get_height(vd
->server
);
1114 width
= pixman_image_get_width(vd
->server
);
1120 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1121 height
* VNC_DIRTY_BPL(vs
),
1122 y
* VNC_DIRTY_BPL(vs
));
1123 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1124 /* no more dirty bits */
1127 y
= offset
/ VNC_DIRTY_BPL(vs
);
1128 x
= offset
% VNC_DIRTY_BPL(vs
);
1129 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1130 VNC_DIRTY_BPL(vs
), x
);
1131 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1132 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1133 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1135 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1136 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1138 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1146 vs
->job_update
= vs
->update
;
1147 vs
->update
= VNC_STATE_UPDATE_NONE
;
1154 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1156 VncState
*vs
= opaque
;
1158 assert(vs
->magic
== VNC_MAGIC
);
1160 case AUD_CNOTIFY_DISABLE
:
1161 vnc_lock_output(vs
);
1162 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1163 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1164 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
1165 vnc_unlock_output(vs
);
1169 case AUD_CNOTIFY_ENABLE
:
1170 vnc_lock_output(vs
);
1171 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1172 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1173 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1174 vnc_unlock_output(vs
);
1180 static void audio_capture_destroy(void *opaque
)
1184 static void audio_capture(void *opaque
, void *buf
, int size
)
1186 VncState
*vs
= opaque
;
1188 assert(vs
->magic
== VNC_MAGIC
);
1189 vnc_lock_output(vs
);
1190 if (vs
->output
.offset
< vs
->throttle_output_offset
) {
1191 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1192 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1193 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1194 vnc_write_u32(vs
, size
);
1195 vnc_write(vs
, buf
, size
);
1197 trace_vnc_client_throttle_audio(vs
, vs
->ioc
, vs
->output
.offset
);
1199 vnc_unlock_output(vs
);
1203 static void audio_add(VncState
*vs
)
1205 struct audio_capture_ops ops
;
1207 if (vs
->audio_cap
) {
1208 error_report("audio already running");
1212 ops
.notify
= audio_capture_notify
;
1213 ops
.destroy
= audio_capture_destroy
;
1214 ops
.capture
= audio_capture
;
1216 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1217 if (!vs
->audio_cap
) {
1218 error_report("Failed to add audio capture");
1222 static void audio_del(VncState
*vs
)
1224 if (vs
->audio_cap
) {
1225 AUD_del_capture(vs
->audio_cap
, vs
);
1226 vs
->audio_cap
= NULL
;
1230 static void vnc_disconnect_start(VncState
*vs
)
1232 if (vs
->disconnecting
) {
1235 trace_vnc_client_disconnect_start(vs
, vs
->ioc
);
1236 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1238 g_source_remove(vs
->ioc_tag
);
1241 qio_channel_close(vs
->ioc
, NULL
);
1242 vs
->disconnecting
= TRUE
;
1245 void vnc_disconnect_finish(VncState
*vs
)
1249 trace_vnc_client_disconnect_finish(vs
, vs
->ioc
);
1251 vnc_jobs_join(vs
); /* Wait encoding jobs */
1253 vnc_lock_output(vs
);
1254 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1256 buffer_free(&vs
->input
);
1257 buffer_free(&vs
->output
);
1259 qapi_free_VncClientInfo(vs
->info
);
1262 vnc_tight_clear(vs
);
1265 #ifdef CONFIG_VNC_SASL
1266 vnc_sasl_client_cleanup(vs
);
1267 #endif /* CONFIG_VNC_SASL */
1269 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1271 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1272 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1274 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1275 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1276 /* last client gone */
1277 vnc_update_server_surface(vs
->vd
);
1280 vnc_unlock_output(vs
);
1282 qemu_mutex_destroy(&vs
->output_mutex
);
1283 if (vs
->bh
!= NULL
) {
1284 qemu_bh_delete(vs
->bh
);
1286 buffer_free(&vs
->jobs_buffer
);
1288 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1289 g_free(vs
->lossy_rect
[i
]);
1291 g_free(vs
->lossy_rect
);
1293 object_unref(OBJECT(vs
->ioc
));
1295 object_unref(OBJECT(vs
->sioc
));
1301 size_t vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
**errp
)
1305 trace_vnc_client_eof(vs
, vs
->ioc
);
1306 vnc_disconnect_start(vs
);
1307 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1308 trace_vnc_client_io_error(vs
, vs
->ioc
,
1309 errp
? error_get_pretty(*errp
) :
1311 vnc_disconnect_start(vs
);
1324 void vnc_client_error(VncState
*vs
)
1326 VNC_DEBUG("Closing down client sock: protocol error\n");
1327 vnc_disconnect_start(vs
);
1332 * Called to write a chunk of data to the client socket. The data may
1333 * be the raw data, or may have already been encoded by SASL.
1334 * The data will be written either straight onto the socket, or
1335 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1337 * NB, it is theoretically possible to have 2 layers of encryption,
1338 * both SASL, and this TLS layer. It is highly unlikely in practice
1339 * though, since SASL encryption will typically be a no-op if TLS
1342 * Returns the number of bytes written, which may be less than
1343 * the requested 'datalen' if the socket would block. Returns
1344 * 0 on I/O error, and disconnects the client socket.
1346 size_t vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1350 ret
= qio_channel_write(
1351 vs
->ioc
, (const char *)data
, datalen
, &err
);
1352 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1353 return vnc_client_io_error(vs
, ret
, &err
);
1358 * Called to write buffered data to the client socket, when not
1359 * using any SASL SSF encryption layers. Will write as much data
1360 * as possible without blocking. If all buffered data is written,
1361 * will switch the FD poll() handler back to read monitoring.
1363 * Returns the number of bytes written, which may be less than
1364 * the buffered output data if the socket would block. Returns
1365 * 0 on I/O error, and disconnects the client socket.
1367 static size_t vnc_client_write_plain(VncState
*vs
)
1372 #ifdef CONFIG_VNC_SASL
1373 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1374 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1375 vs
->sasl
.waitWriteSSF
);
1377 if (vs
->sasl
.conn
&&
1379 vs
->sasl
.waitWriteSSF
) {
1380 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1382 vs
->sasl
.waitWriteSSF
-= ret
;
1384 #endif /* CONFIG_VNC_SASL */
1385 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1389 if (ret
>= vs
->force_update_offset
) {
1390 if (vs
->force_update_offset
!= 0) {
1391 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
1393 vs
->force_update_offset
= 0;
1395 vs
->force_update_offset
-= ret
;
1397 offset
= vs
->output
.offset
;
1398 buffer_advance(&vs
->output
, ret
);
1399 if (offset
>= vs
->throttle_output_offset
&&
1400 vs
->output
.offset
< vs
->throttle_output_offset
) {
1401 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
, vs
->output
.offset
);
1404 if (vs
->output
.offset
== 0) {
1406 g_source_remove(vs
->ioc_tag
);
1408 vs
->ioc_tag
= qio_channel_add_watch(
1409 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1417 * First function called whenever there is data to be written to
1418 * the client socket. Will delegate actual work according to whether
1419 * SASL SSF layers are enabled (thus requiring encryption calls)
1421 static void vnc_client_write_locked(VncState
*vs
)
1423 #ifdef CONFIG_VNC_SASL
1424 if (vs
->sasl
.conn
&&
1426 !vs
->sasl
.waitWriteSSF
) {
1427 vnc_client_write_sasl(vs
);
1429 #endif /* CONFIG_VNC_SASL */
1431 vnc_client_write_plain(vs
);
1435 static void vnc_client_write(VncState
*vs
)
1437 assert(vs
->magic
== VNC_MAGIC
);
1438 vnc_lock_output(vs
);
1439 if (vs
->output
.offset
) {
1440 vnc_client_write_locked(vs
);
1441 } else if (vs
->ioc
!= NULL
) {
1443 g_source_remove(vs
->ioc_tag
);
1445 vs
->ioc_tag
= qio_channel_add_watch(
1446 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1448 vnc_unlock_output(vs
);
1451 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1453 vs
->read_handler
= func
;
1454 vs
->read_handler_expect
= expecting
;
1459 * Called to read a chunk of data from the client socket. The data may
1460 * be the raw data, or may need to be further decoded by SASL.
1461 * The data will be read either straight from to the socket, or
1462 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1464 * NB, it is theoretically possible to have 2 layers of encryption,
1465 * both SASL, and this TLS layer. It is highly unlikely in practice
1466 * though, since SASL encryption will typically be a no-op if TLS
1469 * Returns the number of bytes read, which may be less than
1470 * the requested 'datalen' if the socket would block. Returns
1471 * 0 on I/O error or EOF, and disconnects the client socket.
1473 size_t vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1477 ret
= qio_channel_read(
1478 vs
->ioc
, (char *)data
, datalen
, &err
);
1479 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1480 return vnc_client_io_error(vs
, ret
, &err
);
1485 * Called to read data from the client socket to the input buffer,
1486 * when not using any SASL SSF encryption layers. Will read as much
1487 * data as possible without blocking.
1489 * Returns the number of bytes read, which may be less than
1490 * the requested 'datalen' if the socket would block. Returns
1491 * 0 on I/O error or EOF, and disconnects the client socket.
1493 static size_t vnc_client_read_plain(VncState
*vs
)
1496 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1497 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1498 buffer_reserve(&vs
->input
, 4096);
1499 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1502 vs
->input
.offset
+= ret
;
1506 static void vnc_jobs_bh(void *opaque
)
1508 VncState
*vs
= opaque
;
1510 assert(vs
->magic
== VNC_MAGIC
);
1511 vnc_jobs_consume_buffer(vs
);
1515 * First function called whenever there is more data to be read from
1516 * the client socket. Will delegate actual work according to whether
1517 * SASL SSF layers are enabled (thus requiring decryption calls)
1518 * Returns 0 on success, -1 if client disconnected
1520 static int vnc_client_read(VncState
*vs
)
1524 #ifdef CONFIG_VNC_SASL
1525 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1526 ret
= vnc_client_read_sasl(vs
);
1528 #endif /* CONFIG_VNC_SASL */
1529 ret
= vnc_client_read_plain(vs
);
1531 if (vs
->disconnecting
) {
1532 vnc_disconnect_finish(vs
);
1538 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1539 size_t len
= vs
->read_handler_expect
;
1542 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1543 if (vs
->disconnecting
) {
1544 vnc_disconnect_finish(vs
);
1549 buffer_advance(&vs
->input
, len
);
1551 vs
->read_handler_expect
= ret
;
1557 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1558 GIOCondition condition
, void *opaque
)
1560 VncState
*vs
= opaque
;
1562 assert(vs
->magic
== VNC_MAGIC
);
1563 if (condition
& G_IO_IN
) {
1564 if (vnc_client_read(vs
) < 0) {
1565 /* vs is free()ed here */
1569 if (condition
& G_IO_OUT
) {
1570 vnc_client_write(vs
);
1573 if (vs
->disconnecting
) {
1574 if (vs
->ioc_tag
!= 0) {
1575 g_source_remove(vs
->ioc_tag
);
1584 * Scale factor to apply to vs->throttle_output_offset when checking for
1585 * hard limit. Worst case normal usage could be x2, if we have a complete
1586 * incremental update and complete forced update in the output buffer.
1587 * So x3 should be good enough, but we pick x5 to be conservative and thus
1588 * (hopefully) never trigger incorrectly.
1590 #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1592 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1594 assert(vs
->magic
== VNC_MAGIC
);
1595 if (vs
->disconnecting
) {
1598 /* Protection against malicious client/guest to prevent our output
1599 * buffer growing without bound if client stops reading data. This
1600 * should rarely trigger, because we have earlier throttling code
1601 * which stops issuing framebuffer updates and drops audio data
1602 * if the throttle_output_offset value is exceeded. So we only reach
1603 * this higher level if a huge number of pseudo-encodings get
1604 * triggered while data can't be sent on the socket.
1606 * NB throttle_output_offset can be zero during early protocol
1607 * handshake, or from the job thread's VncState clone
1609 if (vs
->throttle_output_offset
!= 0 &&
1610 (vs
->output
.offset
/ VNC_THROTTLE_OUTPUT_LIMIT_SCALE
) >
1611 vs
->throttle_output_offset
) {
1612 trace_vnc_client_output_limit(vs
, vs
->ioc
, vs
->output
.offset
,
1613 vs
->throttle_output_offset
);
1614 vnc_disconnect_start(vs
);
1617 buffer_reserve(&vs
->output
, len
);
1619 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1621 g_source_remove(vs
->ioc_tag
);
1623 vs
->ioc_tag
= qio_channel_add_watch(
1624 vs
->ioc
, G_IO_IN
| G_IO_OUT
, vnc_client_io
, vs
, NULL
);
1627 buffer_append(&vs
->output
, data
, len
);
1630 void vnc_write_s32(VncState
*vs
, int32_t value
)
1632 vnc_write_u32(vs
, *(uint32_t *)&value
);
1635 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1639 buf
[0] = (value
>> 24) & 0xFF;
1640 buf
[1] = (value
>> 16) & 0xFF;
1641 buf
[2] = (value
>> 8) & 0xFF;
1642 buf
[3] = value
& 0xFF;
1644 vnc_write(vs
, buf
, 4);
1647 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1651 buf
[0] = (value
>> 8) & 0xFF;
1652 buf
[1] = value
& 0xFF;
1654 vnc_write(vs
, buf
, 2);
1657 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1659 vnc_write(vs
, (char *)&value
, 1);
1662 void vnc_flush(VncState
*vs
)
1664 vnc_lock_output(vs
);
1665 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1666 vnc_client_write_locked(vs
);
1668 if (vs
->disconnecting
) {
1669 if (vs
->ioc_tag
!= 0) {
1670 g_source_remove(vs
->ioc_tag
);
1674 vnc_unlock_output(vs
);
1677 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1679 return data
[offset
];
1682 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1684 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1687 static int32_t read_s32(uint8_t *data
, size_t offset
)
1689 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1690 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1693 uint32_t read_u32(uint8_t *data
, size_t offset
)
1695 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1696 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1699 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1703 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1705 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1706 int absolute
= qemu_input_is_absolute();
1708 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1709 vnc_lock_output(vs
);
1710 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1711 vnc_write_u8(vs
, 0);
1712 vnc_write_u16(vs
, 1);
1713 vnc_framebuffer_update(vs
, absolute
, 0,
1714 pixman_image_get_width(vs
->vd
->server
),
1715 pixman_image_get_height(vs
->vd
->server
),
1716 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1717 vnc_unlock_output(vs
);
1720 vs
->absolute
= absolute
;
1723 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1725 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1726 [INPUT_BUTTON_LEFT
] = 0x01,
1727 [INPUT_BUTTON_MIDDLE
] = 0x02,
1728 [INPUT_BUTTON_RIGHT
] = 0x04,
1729 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1730 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1732 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1733 int width
= pixman_image_get_width(vs
->vd
->server
);
1734 int height
= pixman_image_get_height(vs
->vd
->server
);
1736 if (vs
->last_bmask
!= button_mask
) {
1737 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1738 vs
->last_bmask
= button_mask
;
1742 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, 0, width
);
1743 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, 0, height
);
1744 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1745 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1746 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1748 if (vs
->last_x
!= -1) {
1749 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1750 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1755 qemu_input_event_sync();
1758 static void press_key(VncState
*vs
, QKeyCode qcode
)
1760 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, true);
1761 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, false);
1764 static void vnc_led_state_change(VncState
*vs
)
1766 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1770 vnc_lock_output(vs
);
1771 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1772 vnc_write_u8(vs
, 0);
1773 vnc_write_u16(vs
, 1);
1774 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1775 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1776 vnc_unlock_output(vs
);
1780 static void kbd_leds(void *opaque
, int ledstate
)
1782 VncDisplay
*vd
= opaque
;
1785 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1786 (ledstate
& QEMU_NUM_LOCK_LED
),
1787 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1789 if (ledstate
== vd
->ledstate
) {
1793 vd
->ledstate
= ledstate
;
1795 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1796 vnc_led_state_change(client
);
1800 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1802 QKeyCode qcode
= qemu_input_key_number_to_qcode(keycode
);
1804 /* QEMU console switch */
1806 case Q_KEY_CODE_1
... Q_KEY_CODE_9
: /* '1' to '9' keys */
1807 if (vs
->vd
->dcl
.con
== NULL
&& down
&&
1808 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
) &&
1809 qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_ALT
)) {
1810 /* Reset the modifiers sent to the current console */
1811 qkbd_state_lift_all_keys(vs
->vd
->kbd
);
1812 console_select(qcode
- Q_KEY_CODE_1
);
1819 /* Turn off the lock state sync logic if the client support the led
1822 if (down
&& vs
->vd
->lock_key_sync
&&
1823 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1824 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1825 /* If the numlock state needs to change then simulate an additional
1826 keypress before sending this one. This will happen if the user
1827 toggles numlock away from the VNC window.
1829 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1830 if (!qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1831 trace_vnc_key_sync_numlock(true);
1832 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1835 if (qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
)) {
1836 trace_vnc_key_sync_numlock(false);
1837 press_key(vs
, Q_KEY_CODE_NUM_LOCK
);
1842 if (down
&& vs
->vd
->lock_key_sync
&&
1843 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1844 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1845 /* If the capslock state needs to change then simulate an additional
1846 keypress before sending this one. This will happen if the user
1847 toggles capslock away from the VNC window.
1849 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1850 bool shift
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_SHIFT
);
1851 bool capslock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CAPSLOCK
);
1853 if (uppercase
== shift
) {
1854 trace_vnc_key_sync_capslock(false);
1855 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1858 if (uppercase
!= shift
) {
1859 trace_vnc_key_sync_capslock(true);
1860 press_key(vs
, Q_KEY_CODE_CAPS_LOCK
);
1865 qkbd_state_key_event(vs
->vd
->kbd
, qcode
, down
);
1866 if (!qemu_console_is_graphic(NULL
)) {
1867 bool numlock
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_NUMLOCK
);
1868 bool control
= qkbd_state_modifier_get(vs
->vd
->kbd
, QKBD_MOD_CTRL
);
1869 /* QEMU console emulation */
1872 case 0x2a: /* Left Shift */
1873 case 0x36: /* Right Shift */
1874 case 0x1d: /* Left CTRL */
1875 case 0x9d: /* Right CTRL */
1876 case 0x38: /* Left ALT */
1877 case 0xb8: /* Right ALT */
1880 kbd_put_keysym(QEMU_KEY_UP
);
1883 kbd_put_keysym(QEMU_KEY_DOWN
);
1886 kbd_put_keysym(QEMU_KEY_LEFT
);
1889 kbd_put_keysym(QEMU_KEY_RIGHT
);
1892 kbd_put_keysym(QEMU_KEY_DELETE
);
1895 kbd_put_keysym(QEMU_KEY_HOME
);
1898 kbd_put_keysym(QEMU_KEY_END
);
1901 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1904 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1908 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1911 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1914 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1917 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1920 kbd_put_keysym('5');
1923 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1926 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1929 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1932 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1935 kbd_put_keysym('0');
1938 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1942 kbd_put_keysym('/');
1945 kbd_put_keysym('*');
1948 kbd_put_keysym('-');
1951 kbd_put_keysym('+');
1954 kbd_put_keysym('\n');
1959 kbd_put_keysym(sym
& 0x1f);
1961 kbd_put_keysym(sym
);
1969 static const char *code2name(int keycode
)
1971 return QKeyCode_str(qemu_input_key_number_to_qcode(keycode
));
1974 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1979 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(NULL
)) {
1980 lsym
= lsym
- 'A' + 'a';
1983 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF,
1984 vs
->vd
->kbd
, down
) & SCANCODE_KEYMASK
;
1985 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
1986 do_key_event(vs
, down
, keycode
, sym
);
1989 static void ext_key_event(VncState
*vs
, int down
,
1990 uint32_t sym
, uint16_t keycode
)
1992 /* if the user specifies a keyboard layout, always use it */
1993 if (keyboard_layout
) {
1994 key_event(vs
, down
, sym
);
1996 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
1997 do_key_event(vs
, down
, keycode
, sym
);
2001 static void framebuffer_update_request(VncState
*vs
, int incremental
,
2002 int x
, int y
, int w
, int h
)
2005 if (vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
2006 vs
->update
= VNC_STATE_UPDATE_INCREMENTAL
;
2009 vs
->update
= VNC_STATE_UPDATE_FORCE
;
2010 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
2014 static void send_ext_key_event_ack(VncState
*vs
)
2016 vnc_lock_output(vs
);
2017 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2018 vnc_write_u8(vs
, 0);
2019 vnc_write_u16(vs
, 1);
2020 vnc_framebuffer_update(vs
, 0, 0,
2021 pixman_image_get_width(vs
->vd
->server
),
2022 pixman_image_get_height(vs
->vd
->server
),
2023 VNC_ENCODING_EXT_KEY_EVENT
);
2024 vnc_unlock_output(vs
);
2028 static void send_ext_audio_ack(VncState
*vs
)
2030 vnc_lock_output(vs
);
2031 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2032 vnc_write_u8(vs
, 0);
2033 vnc_write_u16(vs
, 1);
2034 vnc_framebuffer_update(vs
, 0, 0,
2035 pixman_image_get_width(vs
->vd
->server
),
2036 pixman_image_get_height(vs
->vd
->server
),
2037 VNC_ENCODING_AUDIO
);
2038 vnc_unlock_output(vs
);
2042 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2045 unsigned int enc
= 0;
2048 vs
->vnc_encoding
= 0;
2049 vs
->tight
.compression
= 9;
2050 vs
->tight
.quality
= -1; /* Lossless by default */
2054 * Start from the end because the encodings are sent in order of preference.
2055 * This way the preferred encoding (first encoding defined in the array)
2056 * will be set at the end of the loop.
2058 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2061 case VNC_ENCODING_RAW
:
2062 vs
->vnc_encoding
= enc
;
2064 case VNC_ENCODING_COPYRECT
:
2065 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
2067 case VNC_ENCODING_HEXTILE
:
2068 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
2069 vs
->vnc_encoding
= enc
;
2071 case VNC_ENCODING_TIGHT
:
2072 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
2073 vs
->vnc_encoding
= enc
;
2075 #ifdef CONFIG_VNC_PNG
2076 case VNC_ENCODING_TIGHT_PNG
:
2077 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
2078 vs
->vnc_encoding
= enc
;
2081 case VNC_ENCODING_ZLIB
:
2082 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
2083 vs
->vnc_encoding
= enc
;
2085 case VNC_ENCODING_ZRLE
:
2086 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
2087 vs
->vnc_encoding
= enc
;
2089 case VNC_ENCODING_ZYWRLE
:
2090 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
2091 vs
->vnc_encoding
= enc
;
2093 case VNC_ENCODING_DESKTOPRESIZE
:
2094 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
2096 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2097 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
2099 case VNC_ENCODING_RICH_CURSOR
:
2100 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
2101 if (vs
->vd
->cursor
) {
2102 vnc_cursor_define(vs
);
2105 case VNC_ENCODING_EXT_KEY_EVENT
:
2106 send_ext_key_event_ack(vs
);
2108 case VNC_ENCODING_AUDIO
:
2109 send_ext_audio_ack(vs
);
2111 case VNC_ENCODING_WMVi
:
2112 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
2114 case VNC_ENCODING_LED_STATE
:
2115 vs
->features
|= VNC_FEATURE_LED_STATE_MASK
;
2117 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2118 vs
->tight
.compression
= (enc
& 0x0F);
2120 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2121 if (vs
->vd
->lossy
) {
2122 vs
->tight
.quality
= (enc
& 0x0F);
2126 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2130 vnc_desktop_resize(vs
);
2131 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2132 vnc_led_state_change(vs
);
2135 static void set_pixel_conversion(VncState
*vs
)
2137 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2139 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2140 vs
->write_pixels
= vnc_write_pixels_copy
;
2141 vnc_hextile_set_pixel_conversion(vs
, 0);
2143 vs
->write_pixels
= vnc_write_pixels_generic
;
2144 vnc_hextile_set_pixel_conversion(vs
, 1);
2148 static void send_color_map(VncState
*vs
)
2152 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2153 vnc_write_u8(vs
, 0); /* padding */
2154 vnc_write_u16(vs
, 0); /* first color */
2155 vnc_write_u16(vs
, 256); /* # of colors */
2157 for (i
= 0; i
< 256; i
++) {
2158 PixelFormat
*pf
= &vs
->client_pf
;
2160 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2161 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2162 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2166 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2167 int big_endian_flag
, int true_color_flag
,
2168 int red_max
, int green_max
, int blue_max
,
2169 int red_shift
, int green_shift
, int blue_shift
)
2171 if (!true_color_flag
) {
2172 /* Expose a reasonable default 256 color map */
2182 switch (bits_per_pixel
) {
2188 vnc_client_error(vs
);
2192 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2193 vs
->client_pf
.rbits
= ctpopl(red_max
);
2194 vs
->client_pf
.rshift
= red_shift
;
2195 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2196 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2197 vs
->client_pf
.gbits
= ctpopl(green_max
);
2198 vs
->client_pf
.gshift
= green_shift
;
2199 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2200 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2201 vs
->client_pf
.bbits
= ctpopl(blue_max
);
2202 vs
->client_pf
.bshift
= blue_shift
;
2203 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2204 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2205 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2206 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2207 vs
->client_be
= big_endian_flag
;
2209 if (!true_color_flag
) {
2213 set_pixel_conversion(vs
);
2215 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2216 graphic_hw_update(vs
->vd
->dcl
.con
);
2219 static void pixel_format_message (VncState
*vs
) {
2220 char pad
[3] = { 0, 0, 0 };
2222 vs
->client_pf
= qemu_default_pixelformat(32);
2224 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2225 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2227 #ifdef HOST_WORDS_BIGENDIAN
2228 vnc_write_u8(vs
, 1); /* big-endian-flag */
2230 vnc_write_u8(vs
, 0); /* big-endian-flag */
2232 vnc_write_u8(vs
, 1); /* true-color-flag */
2233 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2234 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2235 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2236 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2237 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2238 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2239 vnc_write(vs
, pad
, 3); /* padding */
2241 vnc_hextile_set_pixel_conversion(vs
, 0);
2242 vs
->write_pixels
= vnc_write_pixels_copy
;
2245 static void vnc_colordepth(VncState
*vs
)
2247 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2248 /* Sending a WMVi message to notify the client*/
2249 vnc_lock_output(vs
);
2250 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2251 vnc_write_u8(vs
, 0);
2252 vnc_write_u16(vs
, 1); /* number of rects */
2253 vnc_framebuffer_update(vs
, 0, 0,
2254 pixman_image_get_width(vs
->vd
->server
),
2255 pixman_image_get_height(vs
->vd
->server
),
2257 pixel_format_message(vs
);
2258 vnc_unlock_output(vs
);
2261 set_pixel_conversion(vs
);
2265 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2270 VncDisplay
*vd
= vs
->vd
;
2273 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2277 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2281 set_pixel_format(vs
, read_u8(data
, 4),
2282 read_u8(data
, 6), read_u8(data
, 7),
2283 read_u16(data
, 8), read_u16(data
, 10),
2284 read_u16(data
, 12), read_u8(data
, 14),
2285 read_u8(data
, 15), read_u8(data
, 16));
2287 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2292 limit
= read_u16(data
, 2);
2294 return 4 + (limit
* 4);
2296 limit
= read_u16(data
, 2);
2298 for (i
= 0; i
< limit
; i
++) {
2299 int32_t val
= read_s32(data
, 4 + (i
* 4));
2300 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2303 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2305 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2309 framebuffer_update_request(vs
,
2310 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2311 read_u16(data
, 6), read_u16(data
, 8));
2313 case VNC_MSG_CLIENT_KEY_EVENT
:
2317 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2319 case VNC_MSG_CLIENT_POINTER_EVENT
:
2323 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2325 case VNC_MSG_CLIENT_CUT_TEXT
:
2330 uint32_t dlen
= read_u32(data
, 4);
2331 if (dlen
> (1 << 20)) {
2332 error_report("vnc: client_cut_text msg payload has %u bytes"
2333 " which exceeds our limit of 1MB.", dlen
);
2334 vnc_client_error(vs
);
2342 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2344 case VNC_MSG_CLIENT_QEMU
:
2348 switch (read_u8(data
, 1)) {
2349 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2353 ext_key_event(vs
, read_u16(data
, 2),
2354 read_u32(data
, 4), read_u32(data
, 8));
2356 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2360 switch (read_u16 (data
, 2)) {
2361 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2364 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2367 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2370 switch (read_u8(data
, 4)) {
2371 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2372 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2373 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2374 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2375 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2376 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2378 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2379 vnc_client_error(vs
);
2382 vs
->as
.nchannels
= read_u8(data
, 5);
2383 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2384 VNC_DEBUG("Invalid audio channel count %d\n",
2386 vnc_client_error(vs
);
2389 freq
= read_u32(data
, 6);
2390 /* No official limit for protocol, but 48khz is a sensible
2391 * upper bound for trustworthy clients, and this limit
2392 * protects calculations involving 'vs->as.freq' later.
2395 VNC_DEBUG("Invalid audio frequency %u > 48000", freq
);
2396 vnc_client_error(vs
);
2402 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 4));
2403 vnc_client_error(vs
);
2409 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2410 vnc_client_error(vs
);
2415 VNC_DEBUG("Msg: %d\n", data
[0]);
2416 vnc_client_error(vs
);
2420 vnc_update_throttle_offset(vs
);
2421 vnc_read_when(vs
, protocol_client_msg
, 1);
2425 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2431 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2432 switch (vs
->vd
->share_policy
) {
2433 case VNC_SHARE_POLICY_IGNORE
:
2435 * Ignore the shared flag. Nothing to do here.
2437 * Doesn't conform to the rfb spec but is traditional qemu
2438 * behavior, thus left here as option for compatibility
2442 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2444 * Policy: Allow clients ask for exclusive access.
2446 * Implementation: When a client asks for exclusive access,
2447 * disconnect all others. Shared connects are allowed as long
2448 * as no exclusive connection exists.
2450 * This is how the rfb spec suggests to handle the shared flag.
2452 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2454 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2458 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2459 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2462 vnc_disconnect_start(client
);
2465 if (mode
== VNC_SHARE_MODE_SHARED
) {
2466 if (vs
->vd
->num_exclusive
> 0) {
2467 vnc_disconnect_start(vs
);
2472 case VNC_SHARE_POLICY_FORCE_SHARED
:
2474 * Policy: Shared connects only.
2475 * Implementation: Disallow clients asking for exclusive access.
2477 * Useful for shared desktop sessions where you don't want
2478 * someone forgetting to say -shared when running the vnc
2479 * client disconnect everybody else.
2481 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2482 vnc_disconnect_start(vs
);
2487 vnc_set_share_mode(vs
, mode
);
2489 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2490 vnc_disconnect_start(vs
);
2494 assert(pixman_image_get_width(vs
->vd
->server
) < 65536 &&
2495 pixman_image_get_width(vs
->vd
->server
) >= 0);
2496 assert(pixman_image_get_height(vs
->vd
->server
) < 65536 &&
2497 pixman_image_get_height(vs
->vd
->server
) >= 0);
2498 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2499 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2500 vnc_write_u16(vs
, vs
->client_width
);
2501 vnc_write_u16(vs
, vs
->client_height
);
2503 pixel_format_message(vs
);
2506 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2507 if (size
> sizeof(buf
)) {
2511 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2514 vnc_write_u32(vs
, size
);
2515 vnc_write(vs
, buf
, size
);
2518 vnc_client_cache_auth(vs
);
2519 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2521 vnc_read_when(vs
, protocol_client_msg
, 1);
2526 void start_client_init(VncState
*vs
)
2528 vnc_read_when(vs
, protocol_client_init
, 1);
2531 static void make_challenge(VncState
*vs
)
2535 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2537 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2538 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2541 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2543 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2545 unsigned char key
[8];
2546 time_t now
= time(NULL
);
2547 QCryptoCipher
*cipher
= NULL
;
2550 if (!vs
->vd
->password
) {
2551 trace_vnc_auth_fail(vs
, vs
->auth
, "password is not set", "");
2554 if (vs
->vd
->expires
< now
) {
2555 trace_vnc_auth_fail(vs
, vs
->auth
, "password is expired", "");
2559 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2561 /* Calculate the expected challenge response */
2562 pwlen
= strlen(vs
->vd
->password
);
2563 for (i
=0; i
<sizeof(key
); i
++)
2564 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2566 cipher
= qcrypto_cipher_new(
2567 QCRYPTO_CIPHER_ALG_DES_RFB
,
2568 QCRYPTO_CIPHER_MODE_ECB
,
2569 key
, G_N_ELEMENTS(key
),
2572 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot create cipher",
2573 error_get_pretty(err
));
2578 if (qcrypto_cipher_encrypt(cipher
,
2581 VNC_AUTH_CHALLENGE_SIZE
,
2583 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot encrypt challenge response",
2584 error_get_pretty(err
));
2589 /* Compare expected vs actual challenge response */
2590 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2591 trace_vnc_auth_fail(vs
, vs
->auth
, "mis-matched challenge response", "");
2594 trace_vnc_auth_pass(vs
, vs
->auth
);
2595 vnc_write_u32(vs
, 0); /* Accept auth */
2598 start_client_init(vs
);
2601 qcrypto_cipher_free(cipher
);
2605 vnc_write_u32(vs
, 1); /* Reject auth */
2606 if (vs
->minor
>= 8) {
2607 static const char err
[] = "Authentication failed";
2608 vnc_write_u32(vs
, sizeof(err
));
2609 vnc_write(vs
, err
, sizeof(err
));
2612 vnc_client_error(vs
);
2613 qcrypto_cipher_free(cipher
);
2617 void start_auth_vnc(VncState
*vs
)
2620 /* Send client a 'random' challenge */
2621 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2624 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2628 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2630 /* We only advertise 1 auth scheme at a time, so client
2631 * must pick the one we sent. Verify this */
2632 if (data
[0] != vs
->auth
) { /* Reject auth */
2633 trace_vnc_auth_reject(vs
, vs
->auth
, (int)data
[0]);
2634 vnc_write_u32(vs
, 1);
2635 if (vs
->minor
>= 8) {
2636 static const char err
[] = "Authentication failed";
2637 vnc_write_u32(vs
, sizeof(err
));
2638 vnc_write(vs
, err
, sizeof(err
));
2640 vnc_client_error(vs
);
2641 } else { /* Accept requested auth */
2642 trace_vnc_auth_start(vs
, vs
->auth
);
2645 if (vs
->minor
>= 8) {
2646 vnc_write_u32(vs
, 0); /* Accept auth completion */
2649 trace_vnc_auth_pass(vs
, vs
->auth
);
2650 start_client_init(vs
);
2657 case VNC_AUTH_VENCRYPT
:
2658 start_auth_vencrypt(vs
);
2661 #ifdef CONFIG_VNC_SASL
2663 start_auth_sasl(vs
);
2665 #endif /* CONFIG_VNC_SASL */
2667 default: /* Should not be possible, but just in case */
2668 trace_vnc_auth_fail(vs
, vs
->auth
, "Unhandled auth method", "");
2669 vnc_write_u8(vs
, 1);
2670 if (vs
->minor
>= 8) {
2671 static const char err
[] = "Authentication failed";
2672 vnc_write_u32(vs
, sizeof(err
));
2673 vnc_write(vs
, err
, sizeof(err
));
2675 vnc_client_error(vs
);
2681 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2685 memcpy(local
, version
, 12);
2688 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2689 VNC_DEBUG("Malformed protocol version %s\n", local
);
2690 vnc_client_error(vs
);
2693 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2694 if (vs
->major
!= 3 ||
2700 VNC_DEBUG("Unsupported client version\n");
2701 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2703 vnc_client_error(vs
);
2706 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2707 * as equivalent to v3.3 by servers
2709 if (vs
->minor
== 4 || vs
->minor
== 5)
2712 if (vs
->minor
== 3) {
2713 trace_vnc_auth_start(vs
, vs
->auth
);
2714 if (vs
->auth
== VNC_AUTH_NONE
) {
2715 vnc_write_u32(vs
, vs
->auth
);
2717 trace_vnc_auth_pass(vs
, vs
->auth
);
2718 start_client_init(vs
);
2719 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2720 VNC_DEBUG("Tell client VNC auth\n");
2721 vnc_write_u32(vs
, vs
->auth
);
2725 trace_vnc_auth_fail(vs
, vs
->auth
,
2726 "Unsupported auth method for v3.3", "");
2727 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2729 vnc_client_error(vs
);
2732 vnc_write_u8(vs
, 1); /* num auth */
2733 vnc_write_u8(vs
, vs
->auth
);
2734 vnc_read_when(vs
, protocol_client_auth
, 1);
2741 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2743 struct VncSurface
*vs
= &vd
->guest
;
2745 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2748 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2752 w
= (x
+ w
) / VNC_STAT_RECT
;
2753 h
= (y
+ h
) / VNC_STAT_RECT
;
2757 for (j
= y
; j
<= h
; j
++) {
2758 for (i
= x
; i
<= w
; i
++) {
2759 vs
->lossy_rect
[j
][i
] = 1;
2764 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2767 int sty
= y
/ VNC_STAT_RECT
;
2768 int stx
= x
/ VNC_STAT_RECT
;
2771 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2772 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2774 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2777 /* kernel send buffers are full -> refresh later */
2778 if (vs
->output
.offset
) {
2782 if (!vs
->lossy_rect
[sty
][stx
]) {
2786 vs
->lossy_rect
[sty
][stx
] = 0;
2787 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2788 bitmap_set(vs
->dirty
[y
+ j
],
2789 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2790 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
2798 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2800 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2801 pixman_image_get_width(vd
->server
));
2802 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2803 pixman_image_get_height(vd
->server
));
2808 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2809 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2810 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2812 rect
->updated
= false;
2816 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2818 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2821 vd
->guest
.last_freq_check
= *tv
;
2823 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2824 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2825 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2826 int count
= ARRAY_SIZE(rect
->times
);
2827 struct timeval min
, max
;
2829 if (!timerisset(&rect
->times
[count
- 1])) {
2833 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2834 qemu_timersub(tv
, &max
, &res
);
2836 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2838 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2839 memset(rect
->times
, 0, sizeof (rect
->times
));
2843 min
= rect
->times
[rect
->idx
];
2844 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2845 qemu_timersub(&max
, &min
, &res
);
2847 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2848 rect
->freq
/= count
;
2849 rect
->freq
= 1. / rect
->freq
;
2855 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2861 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2862 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2864 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2865 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2866 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2878 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2882 rect
= vnc_stat_rect(vd
, x
, y
);
2883 if (rect
->updated
) {
2886 rect
->times
[rect
->idx
] = *tv
;
2887 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2888 rect
->updated
= true;
2891 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2893 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2894 pixman_image_get_width(vd
->server
));
2895 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2896 pixman_image_get_height(vd
->server
));
2897 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
2898 uint8_t *guest_row0
= NULL
, *server_row0
;
2901 pixman_image_t
*tmpbuf
= NULL
;
2903 struct timeval tv
= { 0, 0 };
2905 if (!vd
->non_adaptive
) {
2906 gettimeofday(&tv
, NULL
);
2907 has_dirty
= vnc_update_stats(vd
, &tv
);
2911 * Walk through the guest dirty map.
2912 * Check and copy modified bits from guest to server surface.
2913 * Update server dirty map.
2915 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
2916 server_stride
= guest_stride
= guest_ll
=
2917 pixman_image_get_stride(vd
->server
);
2918 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
2920 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2921 int width
= pixman_image_get_width(vd
->server
);
2922 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2925 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
2926 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2927 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
2928 guest_ll
= pixman_image_get_width(vd
->guest
.fb
)
2929 * DIV_ROUND_UP(guest_bpp
, 8);
2931 line_bytes
= MIN(server_stride
, guest_ll
);
2935 uint8_t *guest_ptr
, *server_ptr
;
2936 unsigned long offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
2937 height
* VNC_DIRTY_BPL(&vd
->guest
),
2938 y
* VNC_DIRTY_BPL(&vd
->guest
));
2939 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
2940 /* no more dirty bits */
2943 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
2944 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
2946 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
2948 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2949 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
2950 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2952 guest_ptr
= guest_row0
+ y
* guest_stride
;
2954 guest_ptr
+= x
* cmp_bytes
;
2956 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
2957 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2958 int _cmp_bytes
= cmp_bytes
;
2959 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
2962 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
2963 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
2965 assert(_cmp_bytes
>= 0);
2966 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
2969 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
2970 if (!vd
->non_adaptive
) {
2971 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
2974 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2975 set_bit(x
, vs
->dirty
[y
]);
2982 qemu_pixman_image_unref(tmpbuf
);
2986 static void vnc_refresh(DisplayChangeListener
*dcl
)
2988 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
2990 int has_dirty
, rects
= 0;
2992 if (QTAILQ_EMPTY(&vd
->clients
)) {
2993 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
2997 graphic_hw_update(vd
->dcl
.con
);
2999 if (vnc_trylock_display(vd
)) {
3000 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3004 has_dirty
= vnc_refresh_server_surface(vd
);
3005 vnc_unlock_display(vd
);
3007 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
3008 rects
+= vnc_update_client(vs
, has_dirty
);
3009 /* vs might be free()ed here */
3012 if (has_dirty
&& rects
) {
3013 vd
->dcl
.update_interval
/= 2;
3014 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
3015 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
3018 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
3019 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
3020 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
3025 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
3026 bool skipauth
, bool websocket
)
3028 VncState
*vs
= g_new0(VncState
, 1);
3029 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
3032 trace_vnc_client_connect(vs
, sioc
);
3033 vs
->magic
= VNC_MAGIC
;
3035 object_ref(OBJECT(vs
->sioc
));
3036 vs
->ioc
= QIO_CHANNEL(sioc
);
3037 object_ref(OBJECT(vs
->ioc
));
3040 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
3041 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
3042 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
3044 buffer_init(&vs
->tight
.tight
, "vnc-tight/%p", sioc
);
3045 buffer_init(&vs
->tight
.zlib
, "vnc-tight-zlib/%p", sioc
);
3046 buffer_init(&vs
->tight
.gradient
, "vnc-tight-gradient/%p", sioc
);
3047 #ifdef CONFIG_VNC_JPEG
3048 buffer_init(&vs
->tight
.jpeg
, "vnc-tight-jpeg/%p", sioc
);
3050 #ifdef CONFIG_VNC_PNG
3051 buffer_init(&vs
->tight
.png
, "vnc-tight-png/%p", sioc
);
3053 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3054 buffer_init(&vs
->zrle
.zrle
, "vnc-zrle/%p", sioc
);
3055 buffer_init(&vs
->zrle
.fb
, "vnc-zrle-fb/%p", sioc
);
3056 buffer_init(&vs
->zrle
.zlib
, "vnc-zrle-zlib/%p", sioc
);
3059 vs
->auth
= VNC_AUTH_NONE
;
3060 vs
->subauth
= VNC_AUTH_INVALID
;
3063 vs
->auth
= vd
->ws_auth
;
3064 vs
->subauth
= VNC_AUTH_INVALID
;
3066 vs
->auth
= vd
->auth
;
3067 vs
->subauth
= vd
->subauth
;
3070 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3071 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3073 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3074 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3075 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3078 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3079 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3080 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3082 g_source_remove(vs
->ioc_tag
);
3087 vs
->ioc_tag
= qio_channel_add_watch(
3088 vs
->ioc
, G_IO_IN
, vncws_tls_handshake_io
, vs
, NULL
);
3090 vs
->ioc_tag
= qio_channel_add_watch(
3091 vs
->ioc
, G_IO_IN
, vncws_handshake_io
, vs
, NULL
);
3094 vs
->ioc_tag
= qio_channel_add_watch(
3095 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
3098 vnc_client_cache_addr(vs
);
3099 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3100 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3105 vs
->as
.freq
= 44100;
3106 vs
->as
.nchannels
= 2;
3107 vs
->as
.fmt
= AUD_FMT_S16
;
3108 vs
->as
.endianness
= 0;
3110 qemu_mutex_init(&vs
->output_mutex
);
3111 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3113 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3115 vnc_update_server_surface(vd
);
3118 graphic_hw_update(vd
->dcl
.con
);
3120 if (!vs
->websocket
) {
3121 vnc_start_protocol(vs
);
3124 if (vd
->num_connecting
> vd
->connections_limit
) {
3125 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3126 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3127 vnc_disconnect_start(vs
);
3134 void vnc_start_protocol(VncState
*vs
)
3136 vnc_write(vs
, "RFB 003.008\n", 12);
3138 vnc_read_when(vs
, protocol_version
, 12);
3140 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3141 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3144 static void vnc_listen_io(QIONetListener
*listener
,
3145 QIOChannelSocket
*cioc
,
3148 VncDisplay
*vd
= opaque
;
3149 bool isWebsock
= listener
== vd
->wslistener
;
3151 qio_channel_set_name(QIO_CHANNEL(cioc
),
3152 isWebsock
? "vnc-ws-server" : "vnc-server");
3153 qio_channel_set_delay(QIO_CHANNEL(cioc
), false);
3154 vnc_connect(vd
, cioc
, false, isWebsock
);
3157 static const DisplayChangeListenerOps dcl_ops
= {
3159 .dpy_refresh
= vnc_refresh
,
3160 .dpy_gfx_update
= vnc_dpy_update
,
3161 .dpy_gfx_switch
= vnc_dpy_switch
,
3162 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3163 .dpy_mouse_set
= vnc_mouse_set
,
3164 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3167 void vnc_display_init(const char *id
, Error
**errp
)
3171 if (vnc_display_find(id
) != NULL
) {
3174 vd
= g_malloc0(sizeof(*vd
));
3176 vd
->id
= strdup(id
);
3177 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3179 QTAILQ_INIT(&vd
->clients
);
3180 vd
->expires
= TIME_MAX
;
3182 if (keyboard_layout
) {
3183 trace_vnc_key_map_init(keyboard_layout
);
3184 vd
->kbd_layout
= init_keyboard_layout(name2keysym
,
3185 keyboard_layout
, errp
);
3187 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us", errp
);
3190 if (!vd
->kbd_layout
) {
3194 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3195 vd
->connections_limit
= 32;
3197 qemu_mutex_init(&vd
->mutex
);
3198 vnc_start_worker_thread();
3200 vd
->dcl
.ops
= &dcl_ops
;
3201 register_displaychangelistener(&vd
->dcl
);
3202 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
3206 static void vnc_display_close(VncDisplay
*vd
)
3211 vd
->is_unix
= false;
3214 qio_net_listener_disconnect(vd
->listener
);
3215 object_unref(OBJECT(vd
->listener
));
3217 vd
->listener
= NULL
;
3219 if (vd
->wslistener
) {
3220 qio_net_listener_disconnect(vd
->wslistener
);
3221 object_unref(OBJECT(vd
->wslistener
));
3223 vd
->wslistener
= NULL
;
3225 vd
->auth
= VNC_AUTH_INVALID
;
3226 vd
->subauth
= VNC_AUTH_INVALID
;
3228 object_unparent(OBJECT(vd
->tlscreds
));
3229 vd
->tlscreds
= NULL
;
3231 g_free(vd
->tlsaclname
);
3232 vd
->tlsaclname
= NULL
;
3233 if (vd
->lock_key_sync
) {
3234 qemu_remove_led_event_handler(vd
->led
);
3239 int vnc_display_password(const char *id
, const char *password
)
3241 VncDisplay
*vd
= vnc_display_find(id
);
3246 if (vd
->auth
== VNC_AUTH_NONE
) {
3247 error_printf_unless_qmp("If you want use passwords please enable "
3248 "password auth using '-vnc ${dpy},password'.\n");
3252 g_free(vd
->password
);
3253 vd
->password
= g_strdup(password
);
3258 int vnc_display_pw_expire(const char *id
, time_t expires
)
3260 VncDisplay
*vd
= vnc_display_find(id
);
3266 vd
->expires
= expires
;
3270 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3272 SocketAddress
*addr
;
3275 if (!vd
->listener
|| !vd
->listener
->nsioc
) {
3279 addr
= qio_channel_socket_get_local_address(vd
->listener
->sioc
[0], &err
);
3284 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
3285 qapi_free_SocketAddress(addr
);
3288 error_printf_unless_qmp("VNC server running on %s:%s\n",
3291 qapi_free_SocketAddress(addr
);
3294 static QemuOptsList qemu_vnc_opts
= {
3296 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3297 .implied_opt_name
= "vnc",
3301 .type
= QEMU_OPT_STRING
,
3303 .name
= "websocket",
3304 .type
= QEMU_OPT_STRING
,
3306 .name
= "tls-creds",
3307 .type
= QEMU_OPT_STRING
,
3310 .type
= QEMU_OPT_STRING
,
3313 .type
= QEMU_OPT_STRING
,
3316 .type
= QEMU_OPT_NUMBER
,
3318 .name
= "connections",
3319 .type
= QEMU_OPT_NUMBER
,
3322 .type
= QEMU_OPT_NUMBER
,
3325 .type
= QEMU_OPT_BOOL
,
3328 .type
= QEMU_OPT_BOOL
,
3331 .type
= QEMU_OPT_BOOL
,
3334 .type
= QEMU_OPT_BOOL
,
3336 .name
= "lock-key-sync",
3337 .type
= QEMU_OPT_BOOL
,
3339 .name
= "key-delay-ms",
3340 .type
= QEMU_OPT_NUMBER
,
3343 .type
= QEMU_OPT_BOOL
,
3346 .type
= QEMU_OPT_BOOL
,
3349 .type
= QEMU_OPT_BOOL
,
3351 .name
= "non-adaptive",
3352 .type
= QEMU_OPT_BOOL
,
3354 { /* end of list */ }
3360 vnc_display_setup_auth(int *auth
,
3362 QCryptoTLSCreds
*tlscreds
,
3369 * We have a choice of 3 authentication options
3375 * The channel can be run in 2 modes
3380 * And TLS can use 2 types of credentials
3385 * We thus have 9 possible logical combinations
3390 * 4. tls + anon + none
3391 * 5. tls + anon + vnc
3392 * 6. tls + anon + sasl
3393 * 7. tls + x509 + none
3394 * 8. tls + x509 + vnc
3395 * 9. tls + x509 + sasl
3397 * These need to be mapped into the VNC auth schemes
3398 * in an appropriate manner. In regular VNC, all the
3399 * TLS options get mapped into VNC_AUTH_VENCRYPT
3402 * In websockets, the https:// protocol already provides
3403 * TLS support, so there is no need to make use of the
3404 * VeNCrypt extension. Furthermore, websockets browser
3405 * clients could not use VeNCrypt even if they wanted to,
3406 * as they cannot control when the TLS handshake takes
3407 * place. Thus there is no option but to rely on https://,
3408 * meaning combinations 4->6 and 7->9 will be mapped to
3409 * VNC auth schemes in the same way as combos 1->3.
3411 * Regardless of fact that we have a different mapping to
3412 * VNC auth mechs for plain VNC vs websockets VNC, the end
3413 * result has the same security characteristics.
3415 if (websocket
|| !tlscreds
) {
3417 VNC_DEBUG("Initializing VNC server with password auth\n");
3418 *auth
= VNC_AUTH_VNC
;
3420 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3421 *auth
= VNC_AUTH_SASL
;
3423 VNC_DEBUG("Initializing VNC server with no auth\n");
3424 *auth
= VNC_AUTH_NONE
;
3426 *subauth
= VNC_AUTH_INVALID
;
3428 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3429 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3430 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3431 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3433 if (!is_x509
&& !is_anon
) {
3435 "Unsupported TLS cred type %s",
3436 object_get_typename(OBJECT(tlscreds
)));
3439 *auth
= VNC_AUTH_VENCRYPT
;
3442 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3443 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3445 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3446 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3451 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3452 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3454 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3455 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3459 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3460 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3462 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3463 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3471 static int vnc_display_get_address(const char *addrstr
,
3480 SocketAddress
**retaddr
,
3484 SocketAddress
*addr
= NULL
;
3486 addr
= g_new0(SocketAddress
, 1);
3488 if (strncmp(addrstr
, "unix:", 5) == 0) {
3489 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
3490 addr
->u
.q_unix
.path
= g_strdup(addrstr
+ 5);
3493 error_setg(errp
, "UNIX sockets not supported with websock");
3498 error_setg(errp
, "Port range not support with UNIX socket");
3505 unsigned long long baseport
= 0;
3506 InetSocketAddress
*inet
;
3508 port
= strrchr(addrstr
, ':');
3514 error_setg(errp
, "no vnc port specified");
3518 hostlen
= port
- addrstr
;
3520 if (*port
== '\0') {
3521 error_setg(errp
, "vnc port cannot be empty");
3526 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
3527 inet
= &addr
->u
.inet
;
3528 if (addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3529 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3531 inet
->host
= g_strndup(addrstr
, hostlen
);
3533 /* plain VNC port is just an offset, for websocket
3534 * port is absolute */
3536 if (g_str_equal(addrstr
, "") ||
3537 g_str_equal(addrstr
, "on")) {
3538 if (displaynum
== -1) {
3539 error_setg(errp
, "explicit websocket port is required");
3542 inet
->port
= g_strdup_printf(
3543 "%d", displaynum
+ 5700);
3545 inet
->has_to
= true;
3546 inet
->to
= to
+ 5700;
3549 inet
->port
= g_strdup(port
);
3552 int offset
= reverse
? 0 : 5900;
3553 if (parse_uint_full(port
, &baseport
, 10) < 0) {
3554 error_setg(errp
, "can't convert to a number: %s", port
);
3557 if (baseport
> 65535 ||
3558 baseport
+ offset
> 65535) {
3559 error_setg(errp
, "port %s out of range", port
);
3562 inet
->port
= g_strdup_printf(
3563 "%d", (int)baseport
+ offset
);
3566 inet
->has_to
= true;
3567 inet
->to
= to
+ offset
;
3572 inet
->has_ipv4
= has_ipv4
;
3574 inet
->has_ipv6
= has_ipv6
;
3583 qapi_free_SocketAddress(addr
);
3588 static void vnc_free_addresses(SocketAddress
***retsaddr
,
3593 for (i
= 0; i
< *retnsaddr
; i
++) {
3594 qapi_free_SocketAddress((*retsaddr
)[i
]);
3602 static int vnc_display_get_addresses(QemuOpts
*opts
,
3604 SocketAddress
***retsaddr
,
3606 SocketAddress
***retwsaddr
,
3610 SocketAddress
*saddr
= NULL
;
3611 SocketAddress
*wsaddr
= NULL
;
3612 QemuOptsIter addriter
;
3614 int to
= qemu_opt_get_number(opts
, "to", 0);
3615 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3616 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3617 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3618 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3619 int displaynum
= -1;
3627 addr
= qemu_opt_get(opts
, "vnc");
3628 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3632 if (qemu_opt_get(opts
, "websocket") &&
3633 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3635 "SHA1 hash support is required for websockets");
3639 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3640 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3642 rv
= vnc_display_get_address(addr
, false, reverse
, 0, to
,
3649 /* Historical compat - first listen address can be used
3650 * to set the default websocket port
3652 if (displaynum
== -1) {
3655 *retsaddr
= g_renew(SocketAddress
*, *retsaddr
, *retnsaddr
+ 1);
3656 (*retsaddr
)[(*retnsaddr
)++] = saddr
;
3659 /* If we had multiple primary displays, we don't do defaults
3660 * for websocket, and require explicit config instead. */
3661 if (*retnsaddr
> 1) {
3665 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3666 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3667 if (vnc_display_get_address(addr
, true, reverse
, displaynum
, to
,
3670 &wsaddr
, errp
) < 0) {
3674 /* Historical compat - if only a single listen address was
3675 * provided, then this is used to set the default listen
3676 * address for websocket too
3678 if (*retnsaddr
== 1 &&
3679 (*retsaddr
)[0]->type
== SOCKET_ADDRESS_TYPE_INET
&&
3680 wsaddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3681 g_str_equal(wsaddr
->u
.inet
.host
, "") &&
3682 !g_str_equal((*retsaddr
)[0]->u
.inet
.host
, "")) {
3683 g_free(wsaddr
->u
.inet
.host
);
3684 wsaddr
->u
.inet
.host
= g_strdup((*retsaddr
)[0]->u
.inet
.host
);
3687 *retwsaddr
= g_renew(SocketAddress
*, *retwsaddr
, *retnwsaddr
+ 1);
3688 (*retwsaddr
)[(*retnwsaddr
)++] = wsaddr
;
3694 vnc_free_addresses(retsaddr
, retnsaddr
);
3695 vnc_free_addresses(retwsaddr
, retnwsaddr
);
3700 static int vnc_display_connect(VncDisplay
*vd
,
3701 SocketAddress
**saddr
,
3703 SocketAddress
**wsaddr
,
3707 /* connect to viewer */
3708 QIOChannelSocket
*sioc
= NULL
;
3710 error_setg(errp
, "Cannot use websockets in reverse mode");
3714 error_setg(errp
, "Expected a single address in reverse mode");
3717 /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3718 vd
->is_unix
= saddr
[0]->type
== SOCKET_ADDRESS_TYPE_UNIX
;
3719 sioc
= qio_channel_socket_new();
3720 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3721 if (qio_channel_socket_connect_sync(sioc
, saddr
[0], errp
) < 0) {
3724 vnc_connect(vd
, sioc
, false, false);
3725 object_unref(OBJECT(sioc
));
3730 static int vnc_display_listen(VncDisplay
*vd
,
3731 SocketAddress
**saddr
,
3733 SocketAddress
**wsaddr
,
3740 vd
->listener
= qio_net_listener_new();
3741 qio_net_listener_set_name(vd
->listener
, "vnc-listen");
3742 for (i
= 0; i
< nsaddr
; i
++) {
3743 if (qio_net_listener_open_sync(vd
->listener
,
3750 qio_net_listener_set_client_func(vd
->listener
,
3751 vnc_listen_io
, vd
, NULL
);
3755 vd
->wslistener
= qio_net_listener_new();
3756 qio_net_listener_set_name(vd
->wslistener
, "vnc-ws-listen");
3757 for (i
= 0; i
< nwsaddr
; i
++) {
3758 if (qio_net_listener_open_sync(vd
->wslistener
,
3765 qio_net_listener_set_client_func(vd
->wslistener
,
3766 vnc_listen_io
, vd
, NULL
);
3773 void vnc_display_open(const char *id
, Error
**errp
)
3775 VncDisplay
*vd
= vnc_display_find(id
);
3776 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
3777 SocketAddress
**saddr
= NULL
, **wsaddr
= NULL
;
3778 size_t nsaddr
, nwsaddr
;
3779 const char *share
, *device_id
;
3781 bool password
= false;
3782 bool reverse
= false;
3786 int lock_key_sync
= 1;
3790 error_setg(errp
, "VNC display not active");
3793 vnc_display_close(vd
);
3799 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
3800 if (vnc_display_get_addresses(opts
, reverse
, &saddr
, &nsaddr
,
3801 &wsaddr
, &nwsaddr
, errp
) < 0) {
3805 password
= qemu_opt_get_bool(opts
, "password", false);
3807 if (fips_get_state()) {
3809 "VNC password auth disabled due to FIPS mode, "
3810 "consider using the VeNCrypt or SASL authentication "
3811 "methods as an alternative");
3814 if (!qcrypto_cipher_supports(
3815 QCRYPTO_CIPHER_ALG_DES_RFB
, QCRYPTO_CIPHER_MODE_ECB
)) {
3817 "Cipher backend does not support DES RFB algorithm");
3822 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
3823 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 10);
3824 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
3825 #ifndef CONFIG_VNC_SASL
3827 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
3830 #endif /* CONFIG_VNC_SASL */
3831 credid
= qemu_opt_get(opts
, "tls-creds");
3834 creds
= object_resolve_path_component(
3835 object_get_objects_root(), credid
);
3837 error_setg(errp
, "No TLS credentials with id '%s'",
3841 vd
->tlscreds
= (QCryptoTLSCreds
*)
3842 object_dynamic_cast(creds
,
3843 TYPE_QCRYPTO_TLS_CREDS
);
3844 if (!vd
->tlscreds
) {
3845 error_setg(errp
, "Object with id '%s' is not TLS credentials",
3849 object_ref(OBJECT(vd
->tlscreds
));
3851 if (vd
->tlscreds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
3853 "Expecting TLS credentials with a server endpoint");
3857 acl
= qemu_opt_get_bool(opts
, "acl", false);
3859 share
= qemu_opt_get(opts
, "share");
3861 if (strcmp(share
, "ignore") == 0) {
3862 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
3863 } else if (strcmp(share
, "allow-exclusive") == 0) {
3864 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3865 } else if (strcmp(share
, "force-shared") == 0) {
3866 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
3868 error_setg(errp
, "unknown vnc share= option");
3872 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3874 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
3876 #ifdef CONFIG_VNC_JPEG
3877 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
3879 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
3880 /* adaptive updates are only used with tight encoding and
3881 * if lossy updates are enabled so we can disable all the
3882 * calculations otherwise */
3884 vd
->non_adaptive
= true;
3888 if (strcmp(vd
->id
, "default") == 0) {
3889 vd
->tlsaclname
= g_strdup("vnc.x509dname");
3891 vd
->tlsaclname
= g_strdup_printf("vnc.%s.x509dname", vd
->id
);
3893 qemu_acl_init(vd
->tlsaclname
);
3895 #ifdef CONFIG_VNC_SASL
3899 if (strcmp(vd
->id
, "default") == 0) {
3900 aclname
= g_strdup("vnc.username");
3902 aclname
= g_strdup_printf("vnc.%s.username", vd
->id
);
3904 vd
->sasl
.acl
= qemu_acl_init(aclname
);
3909 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
3910 vd
->tlscreds
, password
,
3911 sasl
, false, errp
) < 0) {
3914 trace_vnc_auth_init(vd
, 0, vd
->auth
, vd
->subauth
);
3916 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
3917 vd
->tlscreds
, password
,
3918 sasl
, true, errp
) < 0) {
3921 trace_vnc_auth_init(vd
, 1, vd
->ws_auth
, vd
->ws_subauth
);
3923 #ifdef CONFIG_VNC_SASL
3925 int saslErr
= sasl_server_init(NULL
, "qemu");
3927 if (saslErr
!= SASL_OK
) {
3928 error_setg(errp
, "Failed to initialize SASL auth: %s",
3929 sasl_errstring(saslErr
, NULL
, NULL
));
3934 vd
->lock_key_sync
= lock_key_sync
;
3935 if (lock_key_sync
) {
3936 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
3940 device_id
= qemu_opt_get(opts
, "display");
3942 int head
= qemu_opt_get_number(opts
, "head", 0);
3945 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
3947 error_propagate(errp
, err
);
3954 if (con
!= vd
->dcl
.con
) {
3955 qkbd_state_free(vd
->kbd
);
3956 unregister_displaychangelistener(&vd
->dcl
);
3958 register_displaychangelistener(&vd
->dcl
);
3959 vd
->kbd
= qkbd_state_init(vd
->dcl
.con
);
3961 qkbd_state_set_delay(vd
->kbd
, key_delay_ms
);
3963 if (saddr
== NULL
) {
3968 if (vnc_display_connect(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
3972 if (vnc_display_listen(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
3977 if (qemu_opt_get(opts
, "to")) {
3978 vnc_display_print_local_addr(vd
);
3982 vnc_free_addresses(&saddr
, &nsaddr
);
3983 vnc_free_addresses(&wsaddr
, &nwsaddr
);
3987 vnc_display_close(vd
);
3991 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
3993 VncDisplay
*vd
= vnc_display_find(id
);
3994 QIOChannelSocket
*sioc
;
4000 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4002 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4003 vnc_connect(vd
, sioc
, skipauth
, false);
4004 object_unref(OBJECT(sioc
));
4008 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4013 id
= g_strdup("default");
4014 while (qemu_opts_find(olist
, id
)) {
4016 id
= g_strdup_printf("vnc%d", i
++);
4018 qemu_opts_set_id(opts
, id
);
4021 QemuOpts
*vnc_parse(const char *str
, Error
**errp
)
4023 QemuOptsList
*olist
= qemu_find_opts("vnc");
4024 QemuOpts
*opts
= qemu_opts_parse(olist
, str
, true, errp
);
4031 id
= qemu_opts_id(opts
);
4033 /* auto-assign id if not present */
4034 vnc_auto_assign_id(olist
, opts
);
4039 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4041 Error
*local_err
= NULL
;
4042 char *id
= (char *)qemu_opts_id(opts
);
4045 vnc_display_init(id
, &local_err
);
4047 error_propagate(errp
, local_err
);
4050 vnc_display_open(id
, &local_err
);
4051 if (local_err
!= NULL
) {
4052 error_propagate(errp
, local_err
);
4058 static void vnc_register_config(void)
4060 qemu_add_opts(&qemu_vnc_opts
);
4062 opts_init(vnc_register_config
);