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/sockets.h"
34 #include "qemu/timer.h"
36 #include "qemu/config-file.h"
37 #include "qapi/qmp/qerror.h"
38 #include "qapi/qmp/types.h"
39 #include "qmp-commands.h"
41 #include "qapi-event.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_release_modifiers(VncState
*vs
);
63 static void vnc_update_throttle_offset(VncState
*vs
);
65 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
68 static const char *mn
[] = {
70 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
71 [VNC_SHARE_MODE_SHARED
] = "shared",
72 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
73 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
75 fprintf(stderr
, "%s/%p: %s -> %s\n", __func__
,
76 vs
->ioc
, mn
[vs
->share_mode
], mn
[mode
]);
79 switch (vs
->share_mode
) {
80 case VNC_SHARE_MODE_CONNECTING
:
81 vs
->vd
->num_connecting
--;
83 case VNC_SHARE_MODE_SHARED
:
86 case VNC_SHARE_MODE_EXCLUSIVE
:
87 vs
->vd
->num_exclusive
--;
93 vs
->share_mode
= mode
;
95 switch (vs
->share_mode
) {
96 case VNC_SHARE_MODE_CONNECTING
:
97 vs
->vd
->num_connecting
++;
99 case VNC_SHARE_MODE_SHARED
:
100 vs
->vd
->num_shared
++;
102 case VNC_SHARE_MODE_EXCLUSIVE
:
103 vs
->vd
->num_exclusive
++;
111 static void vnc_init_basic_info(SocketAddress
*addr
,
115 switch (addr
->type
) {
116 case SOCKET_ADDRESS_TYPE_INET
:
117 info
->host
= g_strdup(addr
->u
.inet
.host
);
118 info
->service
= g_strdup(addr
->u
.inet
.port
);
119 if (addr
->u
.inet
.ipv6
) {
120 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
122 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
126 case SOCKET_ADDRESS_TYPE_UNIX
:
127 info
->host
= g_strdup("");
128 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
129 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
132 case SOCKET_ADDRESS_TYPE_VSOCK
:
133 case SOCKET_ADDRESS_TYPE_FD
:
134 error_setg(errp
, "Unsupported socket address type %s",
135 SocketAddressType_str(addr
->type
));
144 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
148 SocketAddress
*addr
= NULL
;
151 error_setg(errp
, "No listener socket available");
155 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
160 vnc_init_basic_info(addr
, info
, errp
);
161 qapi_free_SocketAddress(addr
);
164 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
168 SocketAddress
*addr
= NULL
;
170 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
175 vnc_init_basic_info(addr
, info
, errp
);
176 qapi_free_SocketAddress(addr
);
179 static const char *vnc_auth_name(VncDisplay
*vd
) {
181 case VNC_AUTH_INVALID
:
197 case VNC_AUTH_VENCRYPT
:
198 switch (vd
->subauth
) {
199 case VNC_AUTH_VENCRYPT_PLAIN
:
200 return "vencrypt+plain";
201 case VNC_AUTH_VENCRYPT_TLSNONE
:
202 return "vencrypt+tls+none";
203 case VNC_AUTH_VENCRYPT_TLSVNC
:
204 return "vencrypt+tls+vnc";
205 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
206 return "vencrypt+tls+plain";
207 case VNC_AUTH_VENCRYPT_X509NONE
:
208 return "vencrypt+x509+none";
209 case VNC_AUTH_VENCRYPT_X509VNC
:
210 return "vencrypt+x509+vnc";
211 case VNC_AUTH_VENCRYPT_X509PLAIN
:
212 return "vencrypt+x509+plain";
213 case VNC_AUTH_VENCRYPT_TLSSASL
:
214 return "vencrypt+tls+sasl";
215 case VNC_AUTH_VENCRYPT_X509SASL
:
216 return "vencrypt+x509+sasl";
226 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
235 info
= g_malloc0(sizeof(*info
));
236 vnc_init_basic_info_from_server_addr(vd
->lsock
[0],
237 qapi_VncServerInfo_base(info
), &err
);
238 info
->has_auth
= true;
239 info
->auth
= g_strdup(vnc_auth_name(vd
));
241 qapi_free_VncServerInfo(info
);
248 static void vnc_client_cache_auth(VncState
*client
)
255 client
->info
->x509_dname
=
256 qcrypto_tls_session_get_peer_name(client
->tls
);
257 client
->info
->has_x509_dname
=
258 client
->info
->x509_dname
!= NULL
;
260 #ifdef CONFIG_VNC_SASL
261 if (client
->sasl
.conn
&&
262 client
->sasl
.username
) {
263 client
->info
->has_sasl_username
= true;
264 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
269 static void vnc_client_cache_addr(VncState
*client
)
273 client
->info
= g_malloc0(sizeof(*client
->info
));
274 vnc_init_basic_info_from_remote_addr(client
->sioc
,
275 qapi_VncClientInfo_base(client
->info
),
278 qapi_free_VncClientInfo(client
->info
);
284 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
292 si
= vnc_server_info_get(vs
->vd
);
298 case QAPI_EVENT_VNC_CONNECTED
:
299 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
),
302 case QAPI_EVENT_VNC_INITIALIZED
:
303 qapi_event_send_vnc_initialized(si
, vs
->info
, &error_abort
);
305 case QAPI_EVENT_VNC_DISCONNECTED
:
306 qapi_event_send_vnc_disconnected(si
, vs
->info
, &error_abort
);
312 qapi_free_VncServerInfo(si
);
315 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
320 info
= g_malloc0(sizeof(*info
));
322 vnc_init_basic_info_from_remote_addr(client
->sioc
,
323 qapi_VncClientInfo_base(info
),
327 qapi_free_VncClientInfo(info
);
331 info
->websocket
= client
->websocket
;
334 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
335 info
->has_x509_dname
= info
->x509_dname
!= NULL
;
337 #ifdef CONFIG_VNC_SASL
338 if (client
->sasl
.conn
&& client
->sasl
.username
) {
339 info
->has_sasl_username
= true;
340 info
->sasl_username
= g_strdup(client
->sasl
.username
);
347 static VncDisplay
*vnc_display_find(const char *id
)
352 return QTAILQ_FIRST(&vnc_displays
);
354 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
355 if (strcmp(id
, vd
->id
) == 0) {
362 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
364 VncClientInfoList
*cinfo
, *prev
= NULL
;
367 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
368 cinfo
= g_new0(VncClientInfoList
, 1);
369 cinfo
->value
= qmp_query_vnc_client(client
);
376 VncInfo
*qmp_query_vnc(Error
**errp
)
378 VncInfo
*info
= g_malloc0(sizeof(*info
));
379 VncDisplay
*vd
= vnc_display_find(NULL
);
380 SocketAddress
*addr
= NULL
;
382 if (vd
== NULL
|| !vd
->nlsock
) {
383 info
->enabled
= false;
385 info
->enabled
= true;
387 /* for compatibility with the original command */
388 info
->has_clients
= true;
389 info
->clients
= qmp_query_client_list(vd
);
391 if (vd
->lsock
== NULL
) {
395 addr
= qio_channel_socket_get_local_address(vd
->lsock
[0], errp
);
400 switch (addr
->type
) {
401 case SOCKET_ADDRESS_TYPE_INET
:
402 info
->host
= g_strdup(addr
->u
.inet
.host
);
403 info
->service
= g_strdup(addr
->u
.inet
.port
);
404 if (addr
->u
.inet
.ipv6
) {
405 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
407 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
411 case SOCKET_ADDRESS_TYPE_UNIX
:
412 info
->host
= g_strdup("");
413 info
->service
= g_strdup(addr
->u
.q_unix
.path
);
414 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
417 case SOCKET_ADDRESS_TYPE_VSOCK
:
418 case SOCKET_ADDRESS_TYPE_FD
:
419 error_setg(errp
, "Unsupported socket address type %s",
420 SocketAddressType_str(addr
->type
));
426 info
->has_host
= true;
427 info
->has_service
= true;
428 info
->has_family
= true;
430 info
->has_auth
= true;
431 info
->auth
= g_strdup(vnc_auth_name(vd
));
434 qapi_free_SocketAddress(addr
);
438 qapi_free_SocketAddress(addr
);
439 qapi_free_VncInfo(info
);
444 static void qmp_query_auth(int auth
, int subauth
,
445 VncPrimaryAuth
*qmp_auth
,
446 VncVencryptSubAuth
*qmp_vencrypt
,
447 bool *qmp_has_vencrypt
);
449 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
453 VncServerInfo2List
*prev
)
455 VncServerInfo2List
*list
;
456 VncServerInfo2
*info
;
460 addr
= qio_channel_socket_get_local_address(ioc
, &err
);
466 info
= g_new0(VncServerInfo2
, 1);
467 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
468 qapi_free_SocketAddress(addr
);
470 qapi_free_VncServerInfo2(info
);
474 info
->websocket
= websocket
;
476 qmp_query_auth(auth
, subauth
, &info
->auth
,
477 &info
->vencrypt
, &info
->has_vencrypt
);
479 list
= g_new0(VncServerInfo2List
, 1);
485 static void qmp_query_auth(int auth
, int subauth
,
486 VncPrimaryAuth
*qmp_auth
,
487 VncVencryptSubAuth
*qmp_vencrypt
,
488 bool *qmp_has_vencrypt
)
492 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
495 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
498 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
501 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
504 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
507 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
509 case VNC_AUTH_VENCRYPT
:
510 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
511 *qmp_has_vencrypt
= true;
513 case VNC_AUTH_VENCRYPT_PLAIN
:
514 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
516 case VNC_AUTH_VENCRYPT_TLSNONE
:
517 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
519 case VNC_AUTH_VENCRYPT_TLSVNC
:
520 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
522 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
523 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
525 case VNC_AUTH_VENCRYPT_X509NONE
:
526 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
528 case VNC_AUTH_VENCRYPT_X509VNC
:
529 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
531 case VNC_AUTH_VENCRYPT_X509PLAIN
:
532 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
534 case VNC_AUTH_VENCRYPT_TLSSASL
:
535 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
537 case VNC_AUTH_VENCRYPT_X509SASL
:
538 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
541 *qmp_has_vencrypt
= false;
546 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
550 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
555 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
557 VncInfo2List
*item
, *prev
= NULL
;
563 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
564 info
= g_new0(VncInfo2
, 1);
565 info
->id
= g_strdup(vd
->id
);
566 info
->clients
= qmp_query_client_list(vd
);
567 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
568 &info
->vencrypt
, &info
->has_vencrypt
);
570 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
572 info
->has_display
= true;
573 info
->display
= g_strdup(dev
->id
);
575 for (i
= 0; i
< vd
->nlsock
; i
++) {
576 info
->server
= qmp_query_server_entry(
577 vd
->lsock
[i
], false, vd
->auth
, vd
->subauth
, info
->server
);
579 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
580 info
->server
= qmp_query_server_entry(
581 vd
->lwebsock
[i
], true, vd
->ws_auth
,
582 vd
->ws_subauth
, info
->server
);
585 item
= g_new0(VncInfo2List
, 1);
594 1) Get the queue working for IO.
595 2) there is some weirdness when using the -S option (the screen is grey
596 and not totally invalidated
597 3) resolutions > 1024
600 static int vnc_update_client(VncState
*vs
, int has_dirty
);
601 static void vnc_disconnect_start(VncState
*vs
);
603 static void vnc_colordepth(VncState
*vs
);
604 static void framebuffer_update_request(VncState
*vs
, int incremental
,
605 int x_position
, int y_position
,
607 static void vnc_refresh(DisplayChangeListener
*dcl
);
608 static int vnc_refresh_server_surface(VncDisplay
*vd
);
610 static int vnc_width(VncDisplay
*vd
)
612 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
613 VNC_DIRTY_PIXELS_PER_BIT
));
616 static int vnc_height(VncDisplay
*vd
)
618 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
621 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
622 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
624 int x
, int y
, int w
, int h
)
626 int width
= vnc_width(vd
);
627 int height
= vnc_height(vd
);
629 /* this is needed this to ensure we updated all affected
630 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
631 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
632 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
636 w
= MIN(x
+ w
, width
) - x
;
637 h
= MIN(y
+ h
, height
);
640 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
641 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
645 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
646 int x
, int y
, int w
, int h
)
648 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
649 struct VncSurface
*s
= &vd
->guest
;
651 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
654 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
657 vnc_write_u16(vs
, x
);
658 vnc_write_u16(vs
, y
);
659 vnc_write_u16(vs
, w
);
660 vnc_write_u16(vs
, h
);
662 vnc_write_s32(vs
, encoding
);
666 static void vnc_desktop_resize(VncState
*vs
)
668 if (vs
->ioc
== NULL
|| !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
671 if (vs
->client_width
== pixman_image_get_width(vs
->vd
->server
) &&
672 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
675 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
676 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
678 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
680 vnc_write_u16(vs
, 1); /* number of rects */
681 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
682 VNC_ENCODING_DESKTOPRESIZE
);
683 vnc_unlock_output(vs
);
687 static void vnc_abort_display_jobs(VncDisplay
*vd
)
691 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
694 vnc_unlock_output(vs
);
696 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
699 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
702 vnc_unlock_output(vs
);
706 int vnc_server_fb_stride(VncDisplay
*vd
)
708 return pixman_image_get_stride(vd
->server
);
711 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
715 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
716 ptr
+= y
* vnc_server_fb_stride(vd
);
717 ptr
+= x
* VNC_SERVER_FB_BYTES
;
721 static void vnc_update_server_surface(VncDisplay
*vd
)
725 qemu_pixman_image_unref(vd
->server
);
728 if (QTAILQ_EMPTY(&vd
->clients
)) {
732 width
= vnc_width(vd
);
733 height
= vnc_height(vd
);
734 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
738 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
739 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
743 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
744 DisplaySurface
*surface
)
746 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
749 vnc_abort_display_jobs(vd
);
753 vnc_update_server_surface(vd
);
756 qemu_pixman_image_unref(vd
->guest
.fb
);
757 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
758 vd
->guest
.format
= surface
->format
;
760 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
762 vnc_desktop_resize(vs
);
763 if (vs
->vd
->cursor
) {
764 vnc_cursor_define(vs
);
766 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
767 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
770 vnc_update_throttle_offset(vs
);
775 static void vnc_write_pixels_copy(VncState
*vs
,
776 void *pixels
, int size
)
778 vnc_write(vs
, pixels
, size
);
781 /* slowest but generic code. */
782 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
786 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
787 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
788 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
789 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
791 # error need some bits here if you change VNC_SERVER_FB_FORMAT
793 v
= (r
<< vs
->client_pf
.rshift
) |
794 (g
<< vs
->client_pf
.gshift
) |
795 (b
<< vs
->client_pf
.bshift
);
796 switch (vs
->client_pf
.bytes_per_pixel
) {
826 static void vnc_write_pixels_generic(VncState
*vs
,
827 void *pixels1
, int size
)
831 if (VNC_SERVER_FB_BYTES
== 4) {
832 uint32_t *pixels
= pixels1
;
835 for (i
= 0; i
< n
; i
++) {
836 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
837 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
842 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
846 VncDisplay
*vd
= vs
->vd
;
848 row
= vnc_server_fb_ptr(vd
, x
, y
);
849 for (i
= 0; i
< h
; i
++) {
850 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
851 row
+= vnc_server_fb_stride(vd
);
856 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
859 bool encode_raw
= false;
860 size_t saved_offs
= vs
->output
.offset
;
862 switch(vs
->vnc_encoding
) {
863 case VNC_ENCODING_ZLIB
:
864 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
866 case VNC_ENCODING_HEXTILE
:
867 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
868 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
870 case VNC_ENCODING_TIGHT
:
871 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
873 case VNC_ENCODING_TIGHT_PNG
:
874 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
876 case VNC_ENCODING_ZRLE
:
877 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
879 case VNC_ENCODING_ZYWRLE
:
880 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
887 /* If the client has the same pixel format as our internal buffer and
888 * a RAW encoding would need less space fall back to RAW encoding to
889 * save bandwidth and processing power in the client. */
890 if (!encode_raw
&& vs
->write_pixels
== vnc_write_pixels_copy
&&
891 12 + h
* w
* VNC_SERVER_FB_BYTES
<= (vs
->output
.offset
- saved_offs
)) {
892 vs
->output
.offset
= saved_offs
;
897 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
898 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
904 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
905 int x
, int y
, int visible
)
907 /* can we ask the client(s) to move the pointer ??? */
910 static int vnc_cursor_define(VncState
*vs
)
912 QEMUCursor
*c
= vs
->vd
->cursor
;
915 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
917 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
918 vnc_write_u8(vs
, 0); /* padding */
919 vnc_write_u16(vs
, 1); /* # of rects */
920 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
921 VNC_ENCODING_RICH_CURSOR
);
922 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
923 vnc_write_pixels_generic(vs
, c
->data
, isize
);
924 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
925 vnc_unlock_output(vs
);
931 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
934 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
937 cursor_put(vd
->cursor
);
938 g_free(vd
->cursor_mask
);
941 cursor_get(vd
->cursor
);
942 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
943 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
944 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
946 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
947 vnc_cursor_define(vs
);
951 static int find_and_clear_dirty_height(VncState
*vs
,
952 int y
, int last_x
, int x
, int height
)
956 for (h
= 1; h
< (height
- y
); h
++) {
957 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
960 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
967 * Figure out how much pending data we should allow in the output
968 * buffer before we throttle incremental display updates, and/or
969 * drop audio samples.
971 * We allow for equiv of 1 full display's worth of FB updates,
972 * and 1 second of audio samples. If audio backlog was larger
973 * than that the client would already suffering awful audio
974 * glitches, so dropping samples is no worse really).
976 static void vnc_update_throttle_offset(VncState
*vs
)
979 vs
->client_width
* vs
->client_height
* vs
->client_pf
.bytes_per_pixel
;
982 int freq
= vs
->as
.freq
;
983 /* We don't limit freq when reading settings from client, so
984 * it could be upto MAX_INT in size. 48khz is a sensible
985 * upper bound for trustworthy clients */
990 switch (vs
->as
.fmt
) {
1005 offset
+= freq
* bps
* vs
->as
.nchannels
;
1008 /* Put a floor of 1MB on offset, so that if we have a large pending
1009 * buffer and the display is resized to a small size & back again
1010 * we don't suddenly apply a tiny send limit
1012 offset
= MAX(offset
, 1024 * 1024);
1014 if (vs
->throttle_output_offset
!= offset
) {
1015 trace_vnc_client_throttle_threshold(
1016 vs
, vs
->ioc
, vs
->throttle_output_offset
, offset
, vs
->client_width
,
1017 vs
->client_height
, vs
->client_pf
.bytes_per_pixel
, vs
->audio_cap
);
1020 vs
->throttle_output_offset
= offset
;
1023 static bool vnc_should_update(VncState
*vs
)
1025 switch (vs
->update
) {
1026 case VNC_STATE_UPDATE_NONE
:
1028 case VNC_STATE_UPDATE_INCREMENTAL
:
1029 /* Only allow incremental updates if the pending send queue
1030 * is less than the permitted threshold, and the job worker
1031 * is completely idle.
1033 if (vs
->output
.offset
< vs
->throttle_output_offset
&&
1034 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1037 trace_vnc_client_throttle_incremental(
1038 vs
, vs
->ioc
, vs
->job_update
, vs
->output
.offset
);
1040 case VNC_STATE_UPDATE_FORCE
:
1041 /* Only allow forced updates if the pending send queue
1042 * does not contain a previous forced update, and the
1043 * job worker is completely idle.
1045 * Note this means we'll queue a forced update, even if
1046 * the output buffer size is otherwise over the throttle
1049 if (vs
->force_update_offset
== 0 &&
1050 vs
->job_update
== VNC_STATE_UPDATE_NONE
) {
1053 trace_vnc_client_throttle_forced(
1054 vs
, vs
->ioc
, vs
->job_update
, vs
->force_update_offset
);
1060 static int vnc_update_client(VncState
*vs
, int has_dirty
)
1062 VncDisplay
*vd
= vs
->vd
;
1068 if (vs
->disconnecting
) {
1069 vnc_disconnect_finish(vs
);
1073 vs
->has_dirty
+= has_dirty
;
1074 if (!vnc_should_update(vs
)) {
1078 if (!vs
->has_dirty
&& vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
1083 * Send screen updates to the vnc client using the server
1084 * surface and server dirty map. guest surface updates
1085 * happening in parallel don't disturb us, the next pass will
1086 * send them to the client.
1088 job
= vnc_job_new(vs
);
1090 height
= pixman_image_get_height(vd
->server
);
1091 width
= pixman_image_get_width(vd
->server
);
1097 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1098 height
* VNC_DIRTY_BPL(vs
),
1099 y
* VNC_DIRTY_BPL(vs
));
1100 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1101 /* no more dirty bits */
1104 y
= offset
/ VNC_DIRTY_BPL(vs
);
1105 x
= offset
% VNC_DIRTY_BPL(vs
);
1106 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1107 VNC_DIRTY_BPL(vs
), x
);
1108 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1109 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1110 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1112 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1113 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1115 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1123 vs
->job_update
= vs
->update
;
1124 vs
->update
= VNC_STATE_UPDATE_NONE
;
1131 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1133 VncState
*vs
= opaque
;
1136 case AUD_CNOTIFY_DISABLE
:
1137 vnc_lock_output(vs
);
1138 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1139 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1140 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
1141 vnc_unlock_output(vs
);
1145 case AUD_CNOTIFY_ENABLE
:
1146 vnc_lock_output(vs
);
1147 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1148 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1149 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1150 vnc_unlock_output(vs
);
1156 static void audio_capture_destroy(void *opaque
)
1160 static void audio_capture(void *opaque
, void *buf
, int size
)
1162 VncState
*vs
= opaque
;
1164 vnc_lock_output(vs
);
1165 if (vs
->output
.offset
< vs
->throttle_output_offset
) {
1166 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1167 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1168 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1169 vnc_write_u32(vs
, size
);
1170 vnc_write(vs
, buf
, size
);
1172 trace_vnc_client_throttle_audio(vs
, vs
->ioc
, vs
->output
.offset
);
1174 vnc_unlock_output(vs
);
1178 static void audio_add(VncState
*vs
)
1180 struct audio_capture_ops ops
;
1182 if (vs
->audio_cap
) {
1183 error_report("audio already running");
1187 ops
.notify
= audio_capture_notify
;
1188 ops
.destroy
= audio_capture_destroy
;
1189 ops
.capture
= audio_capture
;
1191 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1192 if (!vs
->audio_cap
) {
1193 error_report("Failed to add audio capture");
1197 static void audio_del(VncState
*vs
)
1199 if (vs
->audio_cap
) {
1200 AUD_del_capture(vs
->audio_cap
, vs
);
1201 vs
->audio_cap
= NULL
;
1205 static void vnc_disconnect_start(VncState
*vs
)
1207 if (vs
->disconnecting
) {
1210 trace_vnc_client_disconnect_start(vs
, vs
->ioc
);
1211 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1213 g_source_remove(vs
->ioc_tag
);
1216 qio_channel_close(vs
->ioc
, NULL
);
1217 vs
->disconnecting
= TRUE
;
1220 void vnc_disconnect_finish(VncState
*vs
)
1224 trace_vnc_client_disconnect_finish(vs
, vs
->ioc
);
1226 vnc_jobs_join(vs
); /* Wait encoding jobs */
1228 vnc_lock_output(vs
);
1229 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1231 buffer_free(&vs
->input
);
1232 buffer_free(&vs
->output
);
1234 qapi_free_VncClientInfo(vs
->info
);
1237 vnc_tight_clear(vs
);
1240 #ifdef CONFIG_VNC_SASL
1241 vnc_sasl_client_cleanup(vs
);
1242 #endif /* CONFIG_VNC_SASL */
1244 vnc_release_modifiers(vs
);
1246 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1247 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1249 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1250 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1251 /* last client gone */
1252 vnc_update_server_surface(vs
->vd
);
1255 vnc_unlock_output(vs
);
1257 qemu_mutex_destroy(&vs
->output_mutex
);
1258 if (vs
->bh
!= NULL
) {
1259 qemu_bh_delete(vs
->bh
);
1261 buffer_free(&vs
->jobs_buffer
);
1263 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1264 g_free(vs
->lossy_rect
[i
]);
1266 g_free(vs
->lossy_rect
);
1268 object_unref(OBJECT(vs
->ioc
));
1270 object_unref(OBJECT(vs
->sioc
));
1275 size_t vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
**errp
)
1279 trace_vnc_client_eof(vs
, vs
->ioc
);
1280 vnc_disconnect_start(vs
);
1281 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1282 trace_vnc_client_io_error(vs
, vs
->ioc
,
1283 errp
? error_get_pretty(*errp
) :
1285 vnc_disconnect_start(vs
);
1298 void vnc_client_error(VncState
*vs
)
1300 VNC_DEBUG("Closing down client sock: protocol error\n");
1301 vnc_disconnect_start(vs
);
1306 * Called to write a chunk of data to the client socket. The data may
1307 * be the raw data, or may have already been encoded by SASL.
1308 * The data will be written either straight onto the socket, or
1309 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1311 * NB, it is theoretically possible to have 2 layers of encryption,
1312 * both SASL, and this TLS layer. It is highly unlikely in practice
1313 * though, since SASL encryption will typically be a no-op if TLS
1316 * Returns the number of bytes written, which may be less than
1317 * the requested 'datalen' if the socket would block. Returns
1318 * 0 on I/O error, and disconnects the client socket.
1320 size_t vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1324 ret
= qio_channel_write(
1325 vs
->ioc
, (const char *)data
, datalen
, &err
);
1326 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1327 return vnc_client_io_error(vs
, ret
, &err
);
1332 * Called to write buffered data to the client socket, when not
1333 * using any SASL SSF encryption layers. Will write as much data
1334 * as possible without blocking. If all buffered data is written,
1335 * will switch the FD poll() handler back to read monitoring.
1337 * Returns the number of bytes written, which may be less than
1338 * the buffered output data if the socket would block. Returns
1339 * 0 on I/O error, and disconnects the client socket.
1341 static size_t vnc_client_write_plain(VncState
*vs
)
1346 #ifdef CONFIG_VNC_SASL
1347 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1348 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1349 vs
->sasl
.waitWriteSSF
);
1351 if (vs
->sasl
.conn
&&
1353 vs
->sasl
.waitWriteSSF
) {
1354 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1356 vs
->sasl
.waitWriteSSF
-= ret
;
1358 #endif /* CONFIG_VNC_SASL */
1359 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1363 if (ret
>= vs
->force_update_offset
) {
1364 if (vs
->force_update_offset
!= 0) {
1365 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
1367 vs
->force_update_offset
= 0;
1369 vs
->force_update_offset
-= ret
;
1371 offset
= vs
->output
.offset
;
1372 buffer_advance(&vs
->output
, ret
);
1373 if (offset
>= vs
->throttle_output_offset
&&
1374 vs
->output
.offset
< vs
->throttle_output_offset
) {
1375 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
, vs
->output
.offset
);
1378 if (vs
->output
.offset
== 0) {
1380 g_source_remove(vs
->ioc_tag
);
1382 vs
->ioc_tag
= qio_channel_add_watch(
1383 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1391 * First function called whenever there is data to be written to
1392 * the client socket. Will delegate actual work according to whether
1393 * SASL SSF layers are enabled (thus requiring encryption calls)
1395 static void vnc_client_write_locked(VncState
*vs
)
1397 #ifdef CONFIG_VNC_SASL
1398 if (vs
->sasl
.conn
&&
1400 !vs
->sasl
.waitWriteSSF
) {
1401 vnc_client_write_sasl(vs
);
1403 #endif /* CONFIG_VNC_SASL */
1405 vnc_client_write_plain(vs
);
1409 static void vnc_client_write(VncState
*vs
)
1412 vnc_lock_output(vs
);
1413 if (vs
->output
.offset
) {
1414 vnc_client_write_locked(vs
);
1415 } else if (vs
->ioc
!= NULL
) {
1417 g_source_remove(vs
->ioc_tag
);
1419 vs
->ioc_tag
= qio_channel_add_watch(
1420 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1422 vnc_unlock_output(vs
);
1425 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1427 vs
->read_handler
= func
;
1428 vs
->read_handler_expect
= expecting
;
1433 * Called to read a chunk of data from the client socket. The data may
1434 * be the raw data, or may need to be further decoded by SASL.
1435 * The data will be read either straight from to the socket, or
1436 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1438 * NB, it is theoretically possible to have 2 layers of encryption,
1439 * both SASL, and this TLS layer. It is highly unlikely in practice
1440 * though, since SASL encryption will typically be a no-op if TLS
1443 * Returns the number of bytes read, which may be less than
1444 * the requested 'datalen' if the socket would block. Returns
1445 * 0 on I/O error or EOF, and disconnects the client socket.
1447 size_t vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1451 ret
= qio_channel_read(
1452 vs
->ioc
, (char *)data
, datalen
, &err
);
1453 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1454 return vnc_client_io_error(vs
, ret
, &err
);
1459 * Called to read data from the client socket to the input buffer,
1460 * when not using any SASL SSF encryption layers. Will read as much
1461 * data as possible without blocking.
1463 * Returns the number of bytes read, which may be less than
1464 * the requested 'datalen' if the socket would block. Returns
1465 * 0 on I/O error or EOF, and disconnects the client socket.
1467 static size_t vnc_client_read_plain(VncState
*vs
)
1470 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1471 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1472 buffer_reserve(&vs
->input
, 4096);
1473 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1476 vs
->input
.offset
+= ret
;
1480 static void vnc_jobs_bh(void *opaque
)
1482 VncState
*vs
= opaque
;
1484 vnc_jobs_consume_buffer(vs
);
1488 * First function called whenever there is more data to be read from
1489 * the client socket. Will delegate actual work according to whether
1490 * SASL SSF layers are enabled (thus requiring decryption calls)
1491 * Returns 0 on success, -1 if client disconnected
1493 static int vnc_client_read(VncState
*vs
)
1497 #ifdef CONFIG_VNC_SASL
1498 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1499 ret
= vnc_client_read_sasl(vs
);
1501 #endif /* CONFIG_VNC_SASL */
1502 ret
= vnc_client_read_plain(vs
);
1504 if (vs
->disconnecting
) {
1505 vnc_disconnect_finish(vs
);
1511 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1512 size_t len
= vs
->read_handler_expect
;
1515 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1516 if (vs
->disconnecting
) {
1517 vnc_disconnect_finish(vs
);
1522 buffer_advance(&vs
->input
, len
);
1524 vs
->read_handler_expect
= ret
;
1530 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1531 GIOCondition condition
, void *opaque
)
1533 VncState
*vs
= opaque
;
1534 if (condition
& G_IO_IN
) {
1535 if (vnc_client_read(vs
) < 0) {
1539 if (condition
& G_IO_OUT
) {
1540 vnc_client_write(vs
);
1547 * Scale factor to apply to vs->throttle_output_offset when checking for
1548 * hard limit. Worst case normal usage could be x2, if we have a complete
1549 * incremental update and complete forced update in the output buffer.
1550 * So x3 should be good enough, but we pick x5 to be conservative and thus
1551 * (hopefully) never trigger incorrectly.
1553 #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1555 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1557 if (vs
->disconnecting
) {
1560 /* Protection against malicious client/guest to prevent our output
1561 * buffer growing without bound if client stops reading data. This
1562 * should rarely trigger, because we have earlier throttling code
1563 * which stops issuing framebuffer updates and drops audio data
1564 * if the throttle_output_offset value is exceeded. So we only reach
1565 * this higher level if a huge number of pseudo-encodings get
1566 * triggered while data can't be sent on the socket.
1568 * NB throttle_output_offset can be zero during early protocol
1569 * handshake, or from the job thread's VncState clone
1571 if (vs
->throttle_output_offset
!= 0 &&
1572 vs
->output
.offset
> (vs
->throttle_output_offset
*
1573 VNC_THROTTLE_OUTPUT_LIMIT_SCALE
)) {
1574 trace_vnc_client_output_limit(vs
, vs
->ioc
, vs
->output
.offset
,
1575 vs
->throttle_output_offset
);
1576 vnc_disconnect_start(vs
);
1579 buffer_reserve(&vs
->output
, len
);
1581 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1583 g_source_remove(vs
->ioc_tag
);
1585 vs
->ioc_tag
= qio_channel_add_watch(
1586 vs
->ioc
, G_IO_IN
| G_IO_OUT
, vnc_client_io
, vs
, NULL
);
1589 buffer_append(&vs
->output
, data
, len
);
1592 void vnc_write_s32(VncState
*vs
, int32_t value
)
1594 vnc_write_u32(vs
, *(uint32_t *)&value
);
1597 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1601 buf
[0] = (value
>> 24) & 0xFF;
1602 buf
[1] = (value
>> 16) & 0xFF;
1603 buf
[2] = (value
>> 8) & 0xFF;
1604 buf
[3] = value
& 0xFF;
1606 vnc_write(vs
, buf
, 4);
1609 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1613 buf
[0] = (value
>> 8) & 0xFF;
1614 buf
[1] = value
& 0xFF;
1616 vnc_write(vs
, buf
, 2);
1619 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1621 vnc_write(vs
, (char *)&value
, 1);
1624 void vnc_flush(VncState
*vs
)
1626 vnc_lock_output(vs
);
1627 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1628 vnc_client_write_locked(vs
);
1630 vnc_unlock_output(vs
);
1633 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1635 return data
[offset
];
1638 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1640 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1643 static int32_t read_s32(uint8_t *data
, size_t offset
)
1645 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1646 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1649 uint32_t read_u32(uint8_t *data
, size_t offset
)
1651 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1652 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1655 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1659 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1661 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1662 int absolute
= qemu_input_is_absolute();
1664 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1665 vnc_lock_output(vs
);
1666 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1667 vnc_write_u8(vs
, 0);
1668 vnc_write_u16(vs
, 1);
1669 vnc_framebuffer_update(vs
, absolute
, 0,
1670 pixman_image_get_width(vs
->vd
->server
),
1671 pixman_image_get_height(vs
->vd
->server
),
1672 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1673 vnc_unlock_output(vs
);
1676 vs
->absolute
= absolute
;
1679 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1681 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1682 [INPUT_BUTTON_LEFT
] = 0x01,
1683 [INPUT_BUTTON_MIDDLE
] = 0x02,
1684 [INPUT_BUTTON_RIGHT
] = 0x04,
1685 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1686 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1688 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1689 int width
= pixman_image_get_width(vs
->vd
->server
);
1690 int height
= pixman_image_get_height(vs
->vd
->server
);
1692 if (vs
->last_bmask
!= button_mask
) {
1693 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1694 vs
->last_bmask
= button_mask
;
1698 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, 0, width
);
1699 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, 0, height
);
1700 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1701 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1702 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1704 if (vs
->last_x
!= -1) {
1705 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1706 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1711 qemu_input_event_sync();
1714 static void reset_keys(VncState
*vs
)
1717 for(i
= 0; i
< 256; i
++) {
1718 if (vs
->modifiers_state
[i
]) {
1719 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, i
, false);
1720 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1721 vs
->modifiers_state
[i
] = 0;
1726 static void press_key(VncState
*vs
, int keysym
)
1728 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1729 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, true);
1730 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1731 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, false);
1732 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1735 static void vnc_led_state_change(VncState
*vs
)
1737 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1741 vnc_lock_output(vs
);
1742 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1743 vnc_write_u8(vs
, 0);
1744 vnc_write_u16(vs
, 1);
1745 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1746 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1747 vnc_unlock_output(vs
);
1751 static void kbd_leds(void *opaque
, int ledstate
)
1753 VncDisplay
*vd
= opaque
;
1756 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1757 (ledstate
& QEMU_NUM_LOCK_LED
),
1758 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1760 if (ledstate
== vd
->ledstate
) {
1764 vd
->ledstate
= ledstate
;
1766 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1767 vnc_led_state_change(client
);
1771 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1773 /* QEMU console switch */
1775 case 0x2a: /* Left Shift */
1776 case 0x36: /* Right Shift */
1777 case 0x1d: /* Left CTRL */
1778 case 0x9d: /* Right CTRL */
1779 case 0x38: /* Left ALT */
1780 case 0xb8: /* Right ALT */
1782 vs
->modifiers_state
[keycode
] = 1;
1784 vs
->modifiers_state
[keycode
] = 0;
1786 case 0x02 ... 0x0a: /* '1' to '9' keys */
1787 if (vs
->vd
->dcl
.con
== NULL
&&
1788 down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1789 /* Reset the modifiers sent to the current console */
1791 console_select(keycode
- 0x02);
1795 case 0x3a: /* CapsLock */
1796 case 0x45: /* NumLock */
1798 vs
->modifiers_state
[keycode
] ^= 1;
1802 /* Turn off the lock state sync logic if the client support the led
1805 if (down
&& vs
->vd
->lock_key_sync
&&
1806 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1807 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1808 /* If the numlock state needs to change then simulate an additional
1809 keypress before sending this one. This will happen if the user
1810 toggles numlock away from the VNC window.
1812 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1813 if (!vs
->modifiers_state
[0x45]) {
1814 trace_vnc_key_sync_numlock(true);
1815 vs
->modifiers_state
[0x45] = 1;
1816 press_key(vs
, 0xff7f);
1819 if (vs
->modifiers_state
[0x45]) {
1820 trace_vnc_key_sync_numlock(false);
1821 vs
->modifiers_state
[0x45] = 0;
1822 press_key(vs
, 0xff7f);
1827 if (down
&& vs
->vd
->lock_key_sync
&&
1828 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1829 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1830 /* If the capslock state needs to change then simulate an additional
1831 keypress before sending this one. This will happen if the user
1832 toggles capslock away from the VNC window.
1834 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1835 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1836 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1838 if (uppercase
== shift
) {
1839 trace_vnc_key_sync_capslock(false);
1840 vs
->modifiers_state
[0x3a] = 0;
1841 press_key(vs
, 0xffe5);
1844 if (uppercase
!= shift
) {
1845 trace_vnc_key_sync_capslock(true);
1846 vs
->modifiers_state
[0x3a] = 1;
1847 press_key(vs
, 0xffe5);
1852 if (qemu_console_is_graphic(NULL
)) {
1853 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, down
);
1854 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1856 bool numlock
= vs
->modifiers_state
[0x45];
1857 bool control
= (vs
->modifiers_state
[0x1d] ||
1858 vs
->modifiers_state
[0x9d]);
1859 /* QEMU console emulation */
1862 case 0x2a: /* Left Shift */
1863 case 0x36: /* Right Shift */
1864 case 0x1d: /* Left CTRL */
1865 case 0x9d: /* Right CTRL */
1866 case 0x38: /* Left ALT */
1867 case 0xb8: /* Right ALT */
1870 kbd_put_keysym(QEMU_KEY_UP
);
1873 kbd_put_keysym(QEMU_KEY_DOWN
);
1876 kbd_put_keysym(QEMU_KEY_LEFT
);
1879 kbd_put_keysym(QEMU_KEY_RIGHT
);
1882 kbd_put_keysym(QEMU_KEY_DELETE
);
1885 kbd_put_keysym(QEMU_KEY_HOME
);
1888 kbd_put_keysym(QEMU_KEY_END
);
1891 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1894 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1898 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1901 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1904 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1907 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1910 kbd_put_keysym('5');
1913 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1916 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1919 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1922 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1925 kbd_put_keysym('0');
1928 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1932 kbd_put_keysym('/');
1935 kbd_put_keysym('*');
1938 kbd_put_keysym('-');
1941 kbd_put_keysym('+');
1944 kbd_put_keysym('\n');
1949 kbd_put_keysym(sym
& 0x1f);
1951 kbd_put_keysym(sym
);
1959 static void vnc_release_modifiers(VncState
*vs
)
1961 static const int keycodes
[] = {
1962 /* shift, control, alt keys, both left & right */
1963 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1967 if (!qemu_console_is_graphic(NULL
)) {
1970 for (i
= 0; i
< ARRAY_SIZE(keycodes
); i
++) {
1971 keycode
= keycodes
[i
];
1972 if (!vs
->modifiers_state
[keycode
]) {
1975 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, false);
1976 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1980 static const char *code2name(int keycode
)
1982 return QKeyCode_str(qemu_input_key_number_to_qcode(keycode
));
1985 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1990 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(NULL
)) {
1991 lsym
= lsym
- 'A' + 'a';
1994 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1995 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
1996 do_key_event(vs
, down
, keycode
, sym
);
1999 static void ext_key_event(VncState
*vs
, int down
,
2000 uint32_t sym
, uint16_t keycode
)
2002 /* if the user specifies a keyboard layout, always use it */
2003 if (keyboard_layout
) {
2004 key_event(vs
, down
, sym
);
2006 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
2007 do_key_event(vs
, down
, keycode
, sym
);
2011 static void framebuffer_update_request(VncState
*vs
, int incremental
,
2012 int x
, int y
, int w
, int h
)
2015 if (vs
->update
!= VNC_STATE_UPDATE_FORCE
) {
2016 vs
->update
= VNC_STATE_UPDATE_INCREMENTAL
;
2019 vs
->update
= VNC_STATE_UPDATE_FORCE
;
2020 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
2024 static void send_ext_key_event_ack(VncState
*vs
)
2026 vnc_lock_output(vs
);
2027 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2028 vnc_write_u8(vs
, 0);
2029 vnc_write_u16(vs
, 1);
2030 vnc_framebuffer_update(vs
, 0, 0,
2031 pixman_image_get_width(vs
->vd
->server
),
2032 pixman_image_get_height(vs
->vd
->server
),
2033 VNC_ENCODING_EXT_KEY_EVENT
);
2034 vnc_unlock_output(vs
);
2038 static void send_ext_audio_ack(VncState
*vs
)
2040 vnc_lock_output(vs
);
2041 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2042 vnc_write_u8(vs
, 0);
2043 vnc_write_u16(vs
, 1);
2044 vnc_framebuffer_update(vs
, 0, 0,
2045 pixman_image_get_width(vs
->vd
->server
),
2046 pixman_image_get_height(vs
->vd
->server
),
2047 VNC_ENCODING_AUDIO
);
2048 vnc_unlock_output(vs
);
2052 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2055 unsigned int enc
= 0;
2058 vs
->vnc_encoding
= 0;
2059 vs
->tight
.compression
= 9;
2060 vs
->tight
.quality
= -1; /* Lossless by default */
2064 * Start from the end because the encodings are sent in order of preference.
2065 * This way the preferred encoding (first encoding defined in the array)
2066 * will be set at the end of the loop.
2068 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2071 case VNC_ENCODING_RAW
:
2072 vs
->vnc_encoding
= enc
;
2074 case VNC_ENCODING_COPYRECT
:
2075 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
2077 case VNC_ENCODING_HEXTILE
:
2078 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
2079 vs
->vnc_encoding
= enc
;
2081 case VNC_ENCODING_TIGHT
:
2082 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
2083 vs
->vnc_encoding
= enc
;
2085 #ifdef CONFIG_VNC_PNG
2086 case VNC_ENCODING_TIGHT_PNG
:
2087 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
2088 vs
->vnc_encoding
= enc
;
2091 case VNC_ENCODING_ZLIB
:
2092 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
2093 vs
->vnc_encoding
= enc
;
2095 case VNC_ENCODING_ZRLE
:
2096 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
2097 vs
->vnc_encoding
= enc
;
2099 case VNC_ENCODING_ZYWRLE
:
2100 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
2101 vs
->vnc_encoding
= enc
;
2103 case VNC_ENCODING_DESKTOPRESIZE
:
2104 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
2106 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2107 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
2109 case VNC_ENCODING_RICH_CURSOR
:
2110 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
2111 if (vs
->vd
->cursor
) {
2112 vnc_cursor_define(vs
);
2115 case VNC_ENCODING_EXT_KEY_EVENT
:
2116 send_ext_key_event_ack(vs
);
2118 case VNC_ENCODING_AUDIO
:
2119 send_ext_audio_ack(vs
);
2121 case VNC_ENCODING_WMVi
:
2122 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
2124 case VNC_ENCODING_LED_STATE
:
2125 vs
->features
|= VNC_FEATURE_LED_STATE_MASK
;
2127 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2128 vs
->tight
.compression
= (enc
& 0x0F);
2130 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2131 if (vs
->vd
->lossy
) {
2132 vs
->tight
.quality
= (enc
& 0x0F);
2136 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2140 vnc_desktop_resize(vs
);
2141 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2142 vnc_led_state_change(vs
);
2145 static void set_pixel_conversion(VncState
*vs
)
2147 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2149 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2150 vs
->write_pixels
= vnc_write_pixels_copy
;
2151 vnc_hextile_set_pixel_conversion(vs
, 0);
2153 vs
->write_pixels
= vnc_write_pixels_generic
;
2154 vnc_hextile_set_pixel_conversion(vs
, 1);
2158 static void send_color_map(VncState
*vs
)
2162 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2163 vnc_write_u8(vs
, 0); /* padding */
2164 vnc_write_u16(vs
, 0); /* first color */
2165 vnc_write_u16(vs
, 256); /* # of colors */
2167 for (i
= 0; i
< 256; i
++) {
2168 PixelFormat
*pf
= &vs
->client_pf
;
2170 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2171 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2172 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2176 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2177 int big_endian_flag
, int true_color_flag
,
2178 int red_max
, int green_max
, int blue_max
,
2179 int red_shift
, int green_shift
, int blue_shift
)
2181 if (!true_color_flag
) {
2182 /* Expose a reasonable default 256 color map */
2192 switch (bits_per_pixel
) {
2198 vnc_client_error(vs
);
2202 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2203 vs
->client_pf
.rbits
= ctpopl(red_max
);
2204 vs
->client_pf
.rshift
= red_shift
;
2205 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2206 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2207 vs
->client_pf
.gbits
= ctpopl(green_max
);
2208 vs
->client_pf
.gshift
= green_shift
;
2209 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2210 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2211 vs
->client_pf
.bbits
= ctpopl(blue_max
);
2212 vs
->client_pf
.bshift
= blue_shift
;
2213 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2214 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2215 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2216 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2217 vs
->client_be
= big_endian_flag
;
2219 if (!true_color_flag
) {
2223 set_pixel_conversion(vs
);
2225 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2226 graphic_hw_update(vs
->vd
->dcl
.con
);
2229 static void pixel_format_message (VncState
*vs
) {
2230 char pad
[3] = { 0, 0, 0 };
2232 vs
->client_pf
= qemu_default_pixelformat(32);
2234 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2235 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2237 #ifdef HOST_WORDS_BIGENDIAN
2238 vnc_write_u8(vs
, 1); /* big-endian-flag */
2240 vnc_write_u8(vs
, 0); /* big-endian-flag */
2242 vnc_write_u8(vs
, 1); /* true-color-flag */
2243 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2244 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2245 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2246 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2247 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2248 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2249 vnc_write(vs
, pad
, 3); /* padding */
2251 vnc_hextile_set_pixel_conversion(vs
, 0);
2252 vs
->write_pixels
= vnc_write_pixels_copy
;
2255 static void vnc_colordepth(VncState
*vs
)
2257 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2258 /* Sending a WMVi message to notify the client*/
2259 vnc_lock_output(vs
);
2260 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2261 vnc_write_u8(vs
, 0);
2262 vnc_write_u16(vs
, 1); /* number of rects */
2263 vnc_framebuffer_update(vs
, 0, 0,
2264 pixman_image_get_width(vs
->vd
->server
),
2265 pixman_image_get_height(vs
->vd
->server
),
2267 pixel_format_message(vs
);
2268 vnc_unlock_output(vs
);
2271 set_pixel_conversion(vs
);
2275 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2279 VncDisplay
*vd
= vs
->vd
;
2282 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2286 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2290 set_pixel_format(vs
, read_u8(data
, 4),
2291 read_u8(data
, 6), read_u8(data
, 7),
2292 read_u16(data
, 8), read_u16(data
, 10),
2293 read_u16(data
, 12), read_u8(data
, 14),
2294 read_u8(data
, 15), read_u8(data
, 16));
2296 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2301 limit
= read_u16(data
, 2);
2303 return 4 + (limit
* 4);
2305 limit
= read_u16(data
, 2);
2307 for (i
= 0; i
< limit
; i
++) {
2308 int32_t val
= read_s32(data
, 4 + (i
* 4));
2309 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2312 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2314 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2318 framebuffer_update_request(vs
,
2319 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2320 read_u16(data
, 6), read_u16(data
, 8));
2322 case VNC_MSG_CLIENT_KEY_EVENT
:
2326 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2328 case VNC_MSG_CLIENT_POINTER_EVENT
:
2332 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2334 case VNC_MSG_CLIENT_CUT_TEXT
:
2339 uint32_t dlen
= read_u32(data
, 4);
2340 if (dlen
> (1 << 20)) {
2341 error_report("vnc: client_cut_text msg payload has %u bytes"
2342 " which exceeds our limit of 1MB.", dlen
);
2343 vnc_client_error(vs
);
2351 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2353 case VNC_MSG_CLIENT_QEMU
:
2357 switch (read_u8(data
, 1)) {
2358 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2362 ext_key_event(vs
, read_u16(data
, 2),
2363 read_u32(data
, 4), read_u32(data
, 8));
2365 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2369 switch (read_u16 (data
, 2)) {
2370 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2373 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2376 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2379 switch (read_u8(data
, 4)) {
2380 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2381 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2382 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2383 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2384 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2385 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2387 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2388 vnc_client_error(vs
);
2391 vs
->as
.nchannels
= read_u8(data
, 5);
2392 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2393 VNC_DEBUG("Invalid audio channel count %d\n",
2395 vnc_client_error(vs
);
2398 vs
->as
.freq
= read_u32(data
, 6);
2401 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 4));
2402 vnc_client_error(vs
);
2408 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2409 vnc_client_error(vs
);
2414 VNC_DEBUG("Msg: %d\n", data
[0]);
2415 vnc_client_error(vs
);
2419 vnc_update_throttle_offset(vs
);
2420 vnc_read_when(vs
, protocol_client_msg
, 1);
2424 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2430 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2431 switch (vs
->vd
->share_policy
) {
2432 case VNC_SHARE_POLICY_IGNORE
:
2434 * Ignore the shared flag. Nothing to do here.
2436 * Doesn't conform to the rfb spec but is traditional qemu
2437 * behavior, thus left here as option for compatibility
2441 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2443 * Policy: Allow clients ask for exclusive access.
2445 * Implementation: When a client asks for exclusive access,
2446 * disconnect all others. Shared connects are allowed as long
2447 * as no exclusive connection exists.
2449 * This is how the rfb spec suggests to handle the shared flag.
2451 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2453 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2457 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2458 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2461 vnc_disconnect_start(client
);
2464 if (mode
== VNC_SHARE_MODE_SHARED
) {
2465 if (vs
->vd
->num_exclusive
> 0) {
2466 vnc_disconnect_start(vs
);
2471 case VNC_SHARE_POLICY_FORCE_SHARED
:
2473 * Policy: Shared connects only.
2474 * Implementation: Disallow clients asking for exclusive access.
2476 * Useful for shared desktop sessions where you don't want
2477 * someone forgetting to say -shared when running the vnc
2478 * client disconnect everybody else.
2480 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2481 vnc_disconnect_start(vs
);
2486 vnc_set_share_mode(vs
, mode
);
2488 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2489 vnc_disconnect_start(vs
);
2493 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2494 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2495 vnc_write_u16(vs
, vs
->client_width
);
2496 vnc_write_u16(vs
, vs
->client_height
);
2498 pixel_format_message(vs
);
2501 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2502 if (size
> sizeof(buf
)) {
2506 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2509 vnc_write_u32(vs
, size
);
2510 vnc_write(vs
, buf
, size
);
2513 vnc_client_cache_auth(vs
);
2514 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2516 vnc_read_when(vs
, protocol_client_msg
, 1);
2521 void start_client_init(VncState
*vs
)
2523 vnc_read_when(vs
, protocol_client_init
, 1);
2526 static void make_challenge(VncState
*vs
)
2530 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2532 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2533 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2536 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2538 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2540 unsigned char key
[8];
2541 time_t now
= time(NULL
);
2542 QCryptoCipher
*cipher
= NULL
;
2545 if (!vs
->vd
->password
) {
2546 trace_vnc_auth_fail(vs
, vs
->auth
, "password is not set", "");
2549 if (vs
->vd
->expires
< now
) {
2550 trace_vnc_auth_fail(vs
, vs
->auth
, "password is expired", "");
2554 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2556 /* Calculate the expected challenge response */
2557 pwlen
= strlen(vs
->vd
->password
);
2558 for (i
=0; i
<sizeof(key
); i
++)
2559 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2561 cipher
= qcrypto_cipher_new(
2562 QCRYPTO_CIPHER_ALG_DES_RFB
,
2563 QCRYPTO_CIPHER_MODE_ECB
,
2564 key
, G_N_ELEMENTS(key
),
2567 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot create cipher",
2568 error_get_pretty(err
));
2573 if (qcrypto_cipher_encrypt(cipher
,
2576 VNC_AUTH_CHALLENGE_SIZE
,
2578 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot encrypt challenge response",
2579 error_get_pretty(err
));
2584 /* Compare expected vs actual challenge response */
2585 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2586 trace_vnc_auth_fail(vs
, vs
->auth
, "mis-matched challenge response", "");
2589 trace_vnc_auth_pass(vs
, vs
->auth
);
2590 vnc_write_u32(vs
, 0); /* Accept auth */
2593 start_client_init(vs
);
2596 qcrypto_cipher_free(cipher
);
2600 vnc_write_u32(vs
, 1); /* Reject auth */
2601 if (vs
->minor
>= 8) {
2602 static const char err
[] = "Authentication failed";
2603 vnc_write_u32(vs
, sizeof(err
));
2604 vnc_write(vs
, err
, sizeof(err
));
2607 vnc_client_error(vs
);
2608 qcrypto_cipher_free(cipher
);
2612 void start_auth_vnc(VncState
*vs
)
2615 /* Send client a 'random' challenge */
2616 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2619 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2623 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2625 /* We only advertise 1 auth scheme at a time, so client
2626 * must pick the one we sent. Verify this */
2627 if (data
[0] != vs
->auth
) { /* Reject auth */
2628 trace_vnc_auth_reject(vs
, vs
->auth
, (int)data
[0]);
2629 vnc_write_u32(vs
, 1);
2630 if (vs
->minor
>= 8) {
2631 static const char err
[] = "Authentication failed";
2632 vnc_write_u32(vs
, sizeof(err
));
2633 vnc_write(vs
, err
, sizeof(err
));
2635 vnc_client_error(vs
);
2636 } else { /* Accept requested auth */
2637 trace_vnc_auth_start(vs
, vs
->auth
);
2640 if (vs
->minor
>= 8) {
2641 vnc_write_u32(vs
, 0); /* Accept auth completion */
2644 trace_vnc_auth_pass(vs
, vs
->auth
);
2645 start_client_init(vs
);
2652 case VNC_AUTH_VENCRYPT
:
2653 start_auth_vencrypt(vs
);
2656 #ifdef CONFIG_VNC_SASL
2658 start_auth_sasl(vs
);
2660 #endif /* CONFIG_VNC_SASL */
2662 default: /* Should not be possible, but just in case */
2663 trace_vnc_auth_fail(vs
, vs
->auth
, "Unhandled auth method", "");
2664 vnc_write_u8(vs
, 1);
2665 if (vs
->minor
>= 8) {
2666 static const char err
[] = "Authentication failed";
2667 vnc_write_u32(vs
, sizeof(err
));
2668 vnc_write(vs
, err
, sizeof(err
));
2670 vnc_client_error(vs
);
2676 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2680 memcpy(local
, version
, 12);
2683 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2684 VNC_DEBUG("Malformed protocol version %s\n", local
);
2685 vnc_client_error(vs
);
2688 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2689 if (vs
->major
!= 3 ||
2695 VNC_DEBUG("Unsupported client version\n");
2696 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2698 vnc_client_error(vs
);
2701 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2702 * as equivalent to v3.3 by servers
2704 if (vs
->minor
== 4 || vs
->minor
== 5)
2707 if (vs
->minor
== 3) {
2708 trace_vnc_auth_start(vs
, vs
->auth
);
2709 if (vs
->auth
== VNC_AUTH_NONE
) {
2710 vnc_write_u32(vs
, vs
->auth
);
2712 trace_vnc_auth_pass(vs
, vs
->auth
);
2713 start_client_init(vs
);
2714 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2715 VNC_DEBUG("Tell client VNC auth\n");
2716 vnc_write_u32(vs
, vs
->auth
);
2720 trace_vnc_auth_fail(vs
, vs
->auth
,
2721 "Unsupported auth method for v3.3", "");
2722 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2724 vnc_client_error(vs
);
2727 vnc_write_u8(vs
, 1); /* num auth */
2728 vnc_write_u8(vs
, vs
->auth
);
2729 vnc_read_when(vs
, protocol_client_auth
, 1);
2736 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2738 struct VncSurface
*vs
= &vd
->guest
;
2740 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2743 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2747 w
= (x
+ w
) / VNC_STAT_RECT
;
2748 h
= (y
+ h
) / VNC_STAT_RECT
;
2752 for (j
= y
; j
<= h
; j
++) {
2753 for (i
= x
; i
<= w
; i
++) {
2754 vs
->lossy_rect
[j
][i
] = 1;
2759 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2762 int sty
= y
/ VNC_STAT_RECT
;
2763 int stx
= x
/ VNC_STAT_RECT
;
2766 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2767 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2769 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2772 /* kernel send buffers are full -> refresh later */
2773 if (vs
->output
.offset
) {
2777 if (!vs
->lossy_rect
[sty
][stx
]) {
2781 vs
->lossy_rect
[sty
][stx
] = 0;
2782 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2783 bitmap_set(vs
->dirty
[y
+ j
],
2784 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2785 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
2793 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2795 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2796 pixman_image_get_width(vd
->server
));
2797 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2798 pixman_image_get_height(vd
->server
));
2803 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2804 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2805 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2807 rect
->updated
= false;
2811 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2813 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2816 vd
->guest
.last_freq_check
= *tv
;
2818 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2819 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2820 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2821 int count
= ARRAY_SIZE(rect
->times
);
2822 struct timeval min
, max
;
2824 if (!timerisset(&rect
->times
[count
- 1])) {
2828 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2829 qemu_timersub(tv
, &max
, &res
);
2831 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2833 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2834 memset(rect
->times
, 0, sizeof (rect
->times
));
2838 min
= rect
->times
[rect
->idx
];
2839 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2840 qemu_timersub(&max
, &min
, &res
);
2842 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2843 rect
->freq
/= count
;
2844 rect
->freq
= 1. / rect
->freq
;
2850 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2856 x
= QEMU_ALIGN_DOWN(x
, VNC_STAT_RECT
);
2857 y
= QEMU_ALIGN_DOWN(y
, VNC_STAT_RECT
);
2859 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2860 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2861 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2873 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2877 rect
= vnc_stat_rect(vd
, x
, y
);
2878 if (rect
->updated
) {
2881 rect
->times
[rect
->idx
] = *tv
;
2882 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2883 rect
->updated
= true;
2886 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2888 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2889 pixman_image_get_width(vd
->server
));
2890 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2891 pixman_image_get_height(vd
->server
));
2892 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
2893 uint8_t *guest_row0
= NULL
, *server_row0
;
2896 pixman_image_t
*tmpbuf
= NULL
;
2898 struct timeval tv
= { 0, 0 };
2900 if (!vd
->non_adaptive
) {
2901 gettimeofday(&tv
, NULL
);
2902 has_dirty
= vnc_update_stats(vd
, &tv
);
2906 * Walk through the guest dirty map.
2907 * Check and copy modified bits from guest to server surface.
2908 * Update server dirty map.
2910 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
2911 server_stride
= guest_stride
= guest_ll
=
2912 pixman_image_get_stride(vd
->server
);
2913 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
2915 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2916 int width
= pixman_image_get_width(vd
->server
);
2917 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2920 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
2921 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2922 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
2923 guest_ll
= pixman_image_get_width(vd
->guest
.fb
) * (DIV_ROUND_UP(guest_bpp
, 8));
2925 line_bytes
= MIN(server_stride
, guest_ll
);
2929 uint8_t *guest_ptr
, *server_ptr
;
2930 unsigned long offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
2931 height
* VNC_DIRTY_BPL(&vd
->guest
),
2932 y
* VNC_DIRTY_BPL(&vd
->guest
));
2933 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
2934 /* no more dirty bits */
2937 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
2938 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
2940 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
2942 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2943 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
2944 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2946 guest_ptr
= guest_row0
+ y
* guest_stride
;
2948 guest_ptr
+= x
* cmp_bytes
;
2950 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
2951 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2952 int _cmp_bytes
= cmp_bytes
;
2953 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
2956 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
2957 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
2959 assert(_cmp_bytes
>= 0);
2960 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
2963 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
2964 if (!vd
->non_adaptive
) {
2965 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
2968 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2969 set_bit(x
, vs
->dirty
[y
]);
2976 qemu_pixman_image_unref(tmpbuf
);
2980 static void vnc_refresh(DisplayChangeListener
*dcl
)
2982 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
2984 int has_dirty
, rects
= 0;
2986 if (QTAILQ_EMPTY(&vd
->clients
)) {
2987 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
2991 graphic_hw_update(vd
->dcl
.con
);
2993 if (vnc_trylock_display(vd
)) {
2994 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2998 has_dirty
= vnc_refresh_server_surface(vd
);
2999 vnc_unlock_display(vd
);
3001 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
3002 rects
+= vnc_update_client(vs
, has_dirty
);
3003 /* vs might be free()ed here */
3006 if (has_dirty
&& rects
) {
3007 vd
->dcl
.update_interval
/= 2;
3008 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
3009 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
3012 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
3013 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
3014 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
3019 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
3020 bool skipauth
, bool websocket
)
3022 VncState
*vs
= g_new0(VncState
, 1);
3023 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
3026 trace_vnc_client_connect(vs
, sioc
);
3028 object_ref(OBJECT(vs
->sioc
));
3029 vs
->ioc
= QIO_CHANNEL(sioc
);
3030 object_ref(OBJECT(vs
->ioc
));
3033 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
3034 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
3035 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
3037 buffer_init(&vs
->tight
.tight
, "vnc-tight/%p", sioc
);
3038 buffer_init(&vs
->tight
.zlib
, "vnc-tight-zlib/%p", sioc
);
3039 buffer_init(&vs
->tight
.gradient
, "vnc-tight-gradient/%p", sioc
);
3040 #ifdef CONFIG_VNC_JPEG
3041 buffer_init(&vs
->tight
.jpeg
, "vnc-tight-jpeg/%p", sioc
);
3043 #ifdef CONFIG_VNC_PNG
3044 buffer_init(&vs
->tight
.png
, "vnc-tight-png/%p", sioc
);
3046 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3047 buffer_init(&vs
->zrle
.zrle
, "vnc-zrle/%p", sioc
);
3048 buffer_init(&vs
->zrle
.fb
, "vnc-zrle-fb/%p", sioc
);
3049 buffer_init(&vs
->zrle
.zlib
, "vnc-zrle-zlib/%p", sioc
);
3052 vs
->auth
= VNC_AUTH_NONE
;
3053 vs
->subauth
= VNC_AUTH_INVALID
;
3056 vs
->auth
= vd
->ws_auth
;
3057 vs
->subauth
= VNC_AUTH_INVALID
;
3059 vs
->auth
= vd
->auth
;
3060 vs
->subauth
= vd
->subauth
;
3063 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3064 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3066 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3067 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3068 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3071 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3072 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3073 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3075 g_source_remove(vs
->ioc_tag
);
3080 vs
->ioc_tag
= qio_channel_add_watch(
3081 vs
->ioc
, G_IO_IN
, vncws_tls_handshake_io
, vs
, NULL
);
3083 vs
->ioc_tag
= qio_channel_add_watch(
3084 vs
->ioc
, G_IO_IN
, vncws_handshake_io
, vs
, NULL
);
3087 vs
->ioc_tag
= qio_channel_add_watch(
3088 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
3091 vnc_client_cache_addr(vs
);
3092 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3093 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3098 vs
->as
.freq
= 44100;
3099 vs
->as
.nchannels
= 2;
3100 vs
->as
.fmt
= AUD_FMT_S16
;
3101 vs
->as
.endianness
= 0;
3103 qemu_mutex_init(&vs
->output_mutex
);
3104 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3106 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3108 vnc_update_server_surface(vd
);
3111 graphic_hw_update(vd
->dcl
.con
);
3113 if (!vs
->websocket
) {
3114 vnc_start_protocol(vs
);
3117 if (vd
->num_connecting
> vd
->connections_limit
) {
3118 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3119 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3120 vnc_disconnect_start(vs
);
3127 void vnc_start_protocol(VncState
*vs
)
3129 vnc_write(vs
, "RFB 003.008\n", 12);
3131 vnc_read_when(vs
, protocol_version
, 12);
3133 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3134 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3137 static gboolean
vnc_listen_io(QIOChannel
*ioc
,
3138 GIOCondition condition
,
3141 VncDisplay
*vd
= opaque
;
3142 QIOChannelSocket
*sioc
= NULL
;
3144 bool isWebsock
= false;
3147 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
3148 if (ioc
== QIO_CHANNEL(vd
->lwebsock
[i
])) {
3154 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
), &err
);
3156 qio_channel_set_name(QIO_CHANNEL(sioc
),
3157 isWebsock
? "vnc-ws-server" : "vnc-server");
3158 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
3159 vnc_connect(vd
, sioc
, false, isWebsock
);
3160 object_unref(OBJECT(sioc
));
3162 /* client probably closed connection before we got there */
3169 static const DisplayChangeListenerOps dcl_ops
= {
3171 .dpy_refresh
= vnc_refresh
,
3172 .dpy_gfx_update
= vnc_dpy_update
,
3173 .dpy_gfx_switch
= vnc_dpy_switch
,
3174 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3175 .dpy_mouse_set
= vnc_mouse_set
,
3176 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3179 void vnc_display_init(const char *id
)
3183 if (vnc_display_find(id
) != NULL
) {
3186 vd
= g_malloc0(sizeof(*vd
));
3188 vd
->id
= strdup(id
);
3189 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3191 QTAILQ_INIT(&vd
->clients
);
3192 vd
->expires
= TIME_MAX
;
3194 if (keyboard_layout
) {
3195 trace_vnc_key_map_init(keyboard_layout
);
3196 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
3198 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
3201 if (!vd
->kbd_layout
) {
3205 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3206 vd
->connections_limit
= 32;
3208 qemu_mutex_init(&vd
->mutex
);
3209 vnc_start_worker_thread();
3211 vd
->dcl
.ops
= &dcl_ops
;
3212 register_displaychangelistener(&vd
->dcl
);
3216 static void vnc_display_close(VncDisplay
*vd
)
3222 vd
->is_unix
= false;
3223 for (i
= 0; i
< vd
->nlsock
; i
++) {
3224 if (vd
->lsock_tag
[i
]) {
3225 g_source_remove(vd
->lsock_tag
[i
]);
3227 object_unref(OBJECT(vd
->lsock
[i
]));
3230 g_free(vd
->lsock_tag
);
3232 vd
->lsock_tag
= NULL
;
3235 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
3236 if (vd
->lwebsock_tag
[i
]) {
3237 g_source_remove(vd
->lwebsock_tag
[i
]);
3239 object_unref(OBJECT(vd
->lwebsock
[i
]));
3241 g_free(vd
->lwebsock
);
3242 g_free(vd
->lwebsock_tag
);
3243 vd
->lwebsock
= NULL
;
3244 vd
->lwebsock_tag
= NULL
;
3247 vd
->auth
= VNC_AUTH_INVALID
;
3248 vd
->subauth
= VNC_AUTH_INVALID
;
3250 object_unparent(OBJECT(vd
->tlscreds
));
3251 vd
->tlscreds
= NULL
;
3253 g_free(vd
->tlsaclname
);
3254 vd
->tlsaclname
= NULL
;
3255 if (vd
->lock_key_sync
) {
3256 qemu_remove_led_event_handler(vd
->led
);
3261 int vnc_display_password(const char *id
, const char *password
)
3263 VncDisplay
*vd
= vnc_display_find(id
);
3268 if (vd
->auth
== VNC_AUTH_NONE
) {
3269 error_printf_unless_qmp("If you want use passwords please enable "
3270 "password auth using '-vnc ${dpy},password'.\n");
3274 g_free(vd
->password
);
3275 vd
->password
= g_strdup(password
);
3280 int vnc_display_pw_expire(const char *id
, time_t expires
)
3282 VncDisplay
*vd
= vnc_display_find(id
);
3288 vd
->expires
= expires
;
3292 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3294 SocketAddress
*addr
;
3301 addr
= qio_channel_socket_get_local_address(vd
->lsock
[0], &err
);
3306 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
3307 qapi_free_SocketAddress(addr
);
3310 error_printf_unless_qmp("VNC server running on %s:%s\n",
3313 qapi_free_SocketAddress(addr
);
3316 static QemuOptsList qemu_vnc_opts
= {
3318 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3319 .implied_opt_name
= "vnc",
3323 .type
= QEMU_OPT_STRING
,
3325 .name
= "websocket",
3326 .type
= QEMU_OPT_STRING
,
3328 .name
= "tls-creds",
3329 .type
= QEMU_OPT_STRING
,
3331 /* Deprecated in favour of tls-creds */
3333 .type
= QEMU_OPT_STRING
,
3336 .type
= QEMU_OPT_STRING
,
3339 .type
= QEMU_OPT_STRING
,
3342 .type
= QEMU_OPT_NUMBER
,
3344 .name
= "connections",
3345 .type
= QEMU_OPT_NUMBER
,
3348 .type
= QEMU_OPT_NUMBER
,
3351 .type
= QEMU_OPT_BOOL
,
3354 .type
= QEMU_OPT_BOOL
,
3357 .type
= QEMU_OPT_BOOL
,
3360 .type
= QEMU_OPT_BOOL
,
3362 .name
= "lock-key-sync",
3363 .type
= QEMU_OPT_BOOL
,
3365 .name
= "key-delay-ms",
3366 .type
= QEMU_OPT_NUMBER
,
3369 .type
= QEMU_OPT_BOOL
,
3371 /* Deprecated in favour of tls-creds */
3373 .type
= QEMU_OPT_BOOL
,
3375 /* Deprecated in favour of tls-creds */
3376 .name
= "x509verify",
3377 .type
= QEMU_OPT_STRING
,
3380 .type
= QEMU_OPT_BOOL
,
3383 .type
= QEMU_OPT_BOOL
,
3385 .name
= "non-adaptive",
3386 .type
= QEMU_OPT_BOOL
,
3388 { /* end of list */ }
3394 vnc_display_setup_auth(int *auth
,
3396 QCryptoTLSCreds
*tlscreds
,
3403 * We have a choice of 3 authentication options
3409 * The channel can be run in 2 modes
3414 * And TLS can use 2 types of credentials
3419 * We thus have 9 possible logical combinations
3424 * 4. tls + anon + none
3425 * 5. tls + anon + vnc
3426 * 6. tls + anon + sasl
3427 * 7. tls + x509 + none
3428 * 8. tls + x509 + vnc
3429 * 9. tls + x509 + sasl
3431 * These need to be mapped into the VNC auth schemes
3432 * in an appropriate manner. In regular VNC, all the
3433 * TLS options get mapped into VNC_AUTH_VENCRYPT
3436 * In websockets, the https:// protocol already provides
3437 * TLS support, so there is no need to make use of the
3438 * VeNCrypt extension. Furthermore, websockets browser
3439 * clients could not use VeNCrypt even if they wanted to,
3440 * as they cannot control when the TLS handshake takes
3441 * place. Thus there is no option but to rely on https://,
3442 * meaning combinations 4->6 and 7->9 will be mapped to
3443 * VNC auth schemes in the same way as combos 1->3.
3445 * Regardless of fact that we have a different mapping to
3446 * VNC auth mechs for plain VNC vs websockets VNC, the end
3447 * result has the same security characteristics.
3449 if (websocket
|| !tlscreds
) {
3451 VNC_DEBUG("Initializing VNC server with password auth\n");
3452 *auth
= VNC_AUTH_VNC
;
3454 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3455 *auth
= VNC_AUTH_SASL
;
3457 VNC_DEBUG("Initializing VNC server with no auth\n");
3458 *auth
= VNC_AUTH_NONE
;
3460 *subauth
= VNC_AUTH_INVALID
;
3462 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3463 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3464 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3465 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3467 if (!is_x509
&& !is_anon
) {
3469 "Unsupported TLS cred type %s",
3470 object_get_typename(OBJECT(tlscreds
)));
3473 *auth
= VNC_AUTH_VENCRYPT
;
3476 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3477 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3479 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3480 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3485 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3486 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3488 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3489 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3493 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3494 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3496 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3497 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3506 * Handle back compat with old CLI syntax by creating some
3507 * suitable QCryptoTLSCreds objects
3509 static QCryptoTLSCreds
*
3510 vnc_display_create_creds(bool x509
,
3516 gchar
*credsid
= g_strdup_printf("tlsvnc%s", id
);
3517 Object
*parent
= object_get_objects_root();
3522 creds
= object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509
,
3526 "endpoint", "server",
3528 "verify-peer", x509verify
? "yes" : "no",
3531 creds
= object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON
,
3535 "endpoint", "server",
3542 error_propagate(errp
, err
);
3546 return QCRYPTO_TLS_CREDS(creds
);
3550 static int vnc_display_get_address(const char *addrstr
,
3559 SocketAddress
**retaddr
,
3563 SocketAddress
*addr
= NULL
;
3565 addr
= g_new0(SocketAddress
, 1);
3567 if (strncmp(addrstr
, "unix:", 5) == 0) {
3568 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
3569 addr
->u
.q_unix
.path
= g_strdup(addrstr
+ 5);
3572 error_setg(errp
, "UNIX sockets not supported with websock");
3577 error_setg(errp
, "Port range not support with UNIX socket");
3584 unsigned long long baseport
= 0;
3585 InetSocketAddress
*inet
;
3587 port
= strrchr(addrstr
, ':');
3593 error_setg(errp
, "no vnc port specified");
3597 hostlen
= port
- addrstr
;
3599 if (*port
== '\0') {
3600 error_setg(errp
, "vnc port cannot be empty");
3605 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
3606 inet
= &addr
->u
.inet
;
3607 if (addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3608 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3610 inet
->host
= g_strndup(addrstr
, hostlen
);
3612 /* plain VNC port is just an offset, for websocket
3613 * port is absolute */
3615 if (g_str_equal(addrstr
, "") ||
3616 g_str_equal(addrstr
, "on")) {
3617 if (displaynum
== -1) {
3618 error_setg(errp
, "explicit websocket port is required");
3621 inet
->port
= g_strdup_printf(
3622 "%d", displaynum
+ 5700);
3624 inet
->has_to
= true;
3625 inet
->to
= to
+ 5700;
3628 inet
->port
= g_strdup(port
);
3631 int offset
= reverse
? 0 : 5900;
3632 if (parse_uint_full(port
, &baseport
, 10) < 0) {
3633 error_setg(errp
, "can't convert to a number: %s", port
);
3636 if (baseport
> 65535 ||
3637 baseport
+ offset
> 65535) {
3638 error_setg(errp
, "port %s out of range", port
);
3641 inet
->port
= g_strdup_printf(
3642 "%d", (int)baseport
+ offset
);
3645 inet
->has_to
= true;
3646 inet
->to
= to
+ offset
;
3651 inet
->has_ipv4
= has_ipv4
;
3653 inet
->has_ipv6
= has_ipv6
;
3662 qapi_free_SocketAddress(addr
);
3667 static void vnc_free_addresses(SocketAddress
***retsaddr
,
3672 for (i
= 0; i
< *retnsaddr
; i
++) {
3673 qapi_free_SocketAddress((*retsaddr
)[i
]);
3681 static int vnc_display_get_addresses(QemuOpts
*opts
,
3683 SocketAddress
***retsaddr
,
3685 SocketAddress
***retwsaddr
,
3689 SocketAddress
*saddr
= NULL
;
3690 SocketAddress
*wsaddr
= NULL
;
3691 QemuOptsIter addriter
;
3693 int to
= qemu_opt_get_number(opts
, "to", 0);
3694 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3695 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3696 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3697 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3698 int displaynum
= -1;
3706 addr
= qemu_opt_get(opts
, "vnc");
3707 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3711 if (qemu_opt_get(opts
, "websocket") &&
3712 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3714 "SHA1 hash support is required for websockets");
3718 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3719 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3721 rv
= vnc_display_get_address(addr
, false, reverse
, 0, to
,
3728 /* Historical compat - first listen address can be used
3729 * to set the default websocket port
3731 if (displaynum
== -1) {
3734 *retsaddr
= g_renew(SocketAddress
*, *retsaddr
, *retnsaddr
+ 1);
3735 (*retsaddr
)[(*retnsaddr
)++] = saddr
;
3738 /* If we had multiple primary displays, we don't do defaults
3739 * for websocket, and require explicit config instead. */
3740 if (*retnsaddr
> 1) {
3744 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3745 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3746 if (vnc_display_get_address(addr
, true, reverse
, displaynum
, to
,
3749 &wsaddr
, errp
) < 0) {
3753 /* Historical compat - if only a single listen address was
3754 * provided, then this is used to set the default listen
3755 * address for websocket too
3757 if (*retnsaddr
== 1 &&
3758 (*retsaddr
)[0]->type
== SOCKET_ADDRESS_TYPE_INET
&&
3759 wsaddr
->type
== SOCKET_ADDRESS_TYPE_INET
&&
3760 g_str_equal(wsaddr
->u
.inet
.host
, "") &&
3761 !g_str_equal((*retsaddr
)[0]->u
.inet
.host
, "")) {
3762 g_free(wsaddr
->u
.inet
.host
);
3763 wsaddr
->u
.inet
.host
= g_strdup((*retsaddr
)[0]->u
.inet
.host
);
3766 *retwsaddr
= g_renew(SocketAddress
*, *retwsaddr
, *retnwsaddr
+ 1);
3767 (*retwsaddr
)[(*retnwsaddr
)++] = wsaddr
;
3773 vnc_free_addresses(retsaddr
, retnsaddr
);
3774 vnc_free_addresses(retwsaddr
, retnwsaddr
);
3779 static int vnc_display_connect(VncDisplay
*vd
,
3780 SocketAddress
**saddr
,
3782 SocketAddress
**wsaddr
,
3786 /* connect to viewer */
3787 QIOChannelSocket
*sioc
= NULL
;
3789 error_setg(errp
, "Cannot use websockets in reverse mode");
3793 error_setg(errp
, "Expected a single address in reverse mode");
3796 /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3797 vd
->is_unix
= saddr
[0]->type
== SOCKET_ADDRESS_TYPE_UNIX
;
3798 sioc
= qio_channel_socket_new();
3799 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3800 if (qio_channel_socket_connect_sync(sioc
, saddr
[0], errp
) < 0) {
3803 vnc_connect(vd
, sioc
, false, false);
3804 object_unref(OBJECT(sioc
));
3809 static int vnc_display_listen_addr(VncDisplay
*vd
,
3810 SocketAddress
*addr
,
3812 QIOChannelSocket
***lsock
,
3817 QIODNSResolver
*resolver
= qio_dns_resolver_get_instance();
3818 SocketAddress
**rawaddrs
= NULL
;
3819 size_t nrawaddrs
= 0;
3820 Error
*listenerr
= NULL
;
3821 bool listening
= false;
3824 if (qio_dns_resolver_lookup_sync(resolver
, addr
, &nrawaddrs
,
3825 &rawaddrs
, errp
) < 0) {
3829 for (i
= 0; i
< nrawaddrs
; i
++) {
3830 QIOChannelSocket
*sioc
= qio_channel_socket_new();
3832 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
3833 if (qio_channel_socket_listen_sync(
3834 sioc
, rawaddrs
[i
], listenerr
== NULL
? &listenerr
: NULL
) < 0) {
3835 object_unref(OBJECT(sioc
));
3840 *lsock
= g_renew(QIOChannelSocket
*, *lsock
, *nlsock
);
3841 *lsock_tag
= g_renew(guint
, *lsock_tag
, *nlsock
);
3843 (*lsock
)[*nlsock
- 1] = sioc
;
3844 (*lsock_tag
)[*nlsock
- 1] = 0;
3847 for (i
= 0; i
< nrawaddrs
; i
++) {
3848 qapi_free_SocketAddress(rawaddrs
[i
]);
3854 error_propagate(errp
, listenerr
);
3857 error_free(listenerr
);
3861 for (i
= 0; i
< *nlsock
; i
++) {
3862 (*lsock_tag
)[i
] = qio_channel_add_watch(
3863 QIO_CHANNEL((*lsock
)[i
]),
3864 G_IO_IN
, vnc_listen_io
, vd
, NULL
);
3871 static int vnc_display_listen(VncDisplay
*vd
,
3872 SocketAddress
**saddr
,
3874 SocketAddress
**wsaddr
,
3880 for (i
= 0; i
< nsaddr
; i
++) {
3881 if (vnc_display_listen_addr(vd
, saddr
[i
],
3890 for (i
= 0; i
< nwsaddr
; i
++) {
3891 if (vnc_display_listen_addr(vd
, wsaddr
[i
],
3905 void vnc_display_open(const char *id
, Error
**errp
)
3907 VncDisplay
*vd
= vnc_display_find(id
);
3908 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
3909 SocketAddress
**saddr
= NULL
, **wsaddr
= NULL
;
3910 size_t nsaddr
, nwsaddr
;
3911 const char *share
, *device_id
;
3913 bool password
= false;
3914 bool reverse
= false;
3917 #ifdef CONFIG_VNC_SASL
3921 int lock_key_sync
= 1;
3925 error_setg(errp
, "VNC display not active");
3928 vnc_display_close(vd
);
3934 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
3935 if (vnc_display_get_addresses(opts
, reverse
, &saddr
, &nsaddr
,
3936 &wsaddr
, &nwsaddr
, errp
) < 0) {
3940 password
= qemu_opt_get_bool(opts
, "password", false);
3942 if (fips_get_state()) {
3944 "VNC password auth disabled due to FIPS mode, "
3945 "consider using the VeNCrypt or SASL authentication "
3946 "methods as an alternative");
3949 if (!qcrypto_cipher_supports(
3950 QCRYPTO_CIPHER_ALG_DES_RFB
, QCRYPTO_CIPHER_MODE_ECB
)) {
3952 "Cipher backend does not support DES RFB algorithm");
3957 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
3958 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 10);
3959 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
3960 #ifndef CONFIG_VNC_SASL
3962 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
3965 #endif /* CONFIG_VNC_SASL */
3966 credid
= qemu_opt_get(opts
, "tls-creds");
3969 if (qemu_opt_get(opts
, "tls") ||
3970 qemu_opt_get(opts
, "x509") ||
3971 qemu_opt_get(opts
, "x509verify")) {
3973 "'tls-creds' parameter is mutually exclusive with "
3974 "'tls', 'x509' and 'x509verify' parameters");
3978 creds
= object_resolve_path_component(
3979 object_get_objects_root(), credid
);
3981 error_setg(errp
, "No TLS credentials with id '%s'",
3985 vd
->tlscreds
= (QCryptoTLSCreds
*)
3986 object_dynamic_cast(creds
,
3987 TYPE_QCRYPTO_TLS_CREDS
);
3988 if (!vd
->tlscreds
) {
3989 error_setg(errp
, "Object with id '%s' is not TLS credentials",
3993 object_ref(OBJECT(vd
->tlscreds
));
3995 if (vd
->tlscreds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
3997 "Expecting TLS credentials with a server endpoint");
4002 bool tls
= false, x509
= false, x509verify
= false;
4003 tls
= qemu_opt_get_bool(opts
, "tls", false);
4005 path
= qemu_opt_get(opts
, "x509");
4010 path
= qemu_opt_get(opts
, "x509verify");
4016 vd
->tlscreds
= vnc_display_create_creds(x509
,
4021 if (!vd
->tlscreds
) {
4026 acl
= qemu_opt_get_bool(opts
, "acl", false);
4028 share
= qemu_opt_get(opts
, "share");
4030 if (strcmp(share
, "ignore") == 0) {
4031 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
4032 } else if (strcmp(share
, "allow-exclusive") == 0) {
4033 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4034 } else if (strcmp(share
, "force-shared") == 0) {
4035 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
4037 error_setg(errp
, "unknown vnc share= option");
4041 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
4043 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
4045 #ifdef CONFIG_VNC_JPEG
4046 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
4048 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
4049 /* adaptive updates are only used with tight encoding and
4050 * if lossy updates are enabled so we can disable all the
4051 * calculations otherwise */
4053 vd
->non_adaptive
= true;
4057 if (strcmp(vd
->id
, "default") == 0) {
4058 vd
->tlsaclname
= g_strdup("vnc.x509dname");
4060 vd
->tlsaclname
= g_strdup_printf("vnc.%s.x509dname", vd
->id
);
4062 qemu_acl_init(vd
->tlsaclname
);
4064 #ifdef CONFIG_VNC_SASL
4068 if (strcmp(vd
->id
, "default") == 0) {
4069 aclname
= g_strdup("vnc.username");
4071 aclname
= g_strdup_printf("vnc.%s.username", vd
->id
);
4073 vd
->sasl
.acl
= qemu_acl_init(aclname
);
4078 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
4079 vd
->tlscreds
, password
,
4080 sasl
, false, errp
) < 0) {
4083 trace_vnc_auth_init(vd
, 0, vd
->auth
, vd
->subauth
);
4085 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
4086 vd
->tlscreds
, password
,
4087 sasl
, true, errp
) < 0) {
4090 trace_vnc_auth_init(vd
, 1, vd
->ws_auth
, vd
->ws_subauth
);
4092 #ifdef CONFIG_VNC_SASL
4093 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
4094 error_setg(errp
, "Failed to initialize SASL auth: %s",
4095 sasl_errstring(saslErr
, NULL
, NULL
));
4099 vd
->lock_key_sync
= lock_key_sync
;
4100 if (lock_key_sync
) {
4101 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
4104 vd
->key_delay_ms
= key_delay_ms
;
4106 device_id
= qemu_opt_get(opts
, "display");
4108 int head
= qemu_opt_get_number(opts
, "head", 0);
4111 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
4113 error_propagate(errp
, err
);
4120 if (con
!= vd
->dcl
.con
) {
4121 unregister_displaychangelistener(&vd
->dcl
);
4123 register_displaychangelistener(&vd
->dcl
);
4126 if (saddr
== NULL
) {
4131 if (vnc_display_connect(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4135 if (vnc_display_listen(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4140 if (qemu_opt_get(opts
, "to")) {
4141 vnc_display_print_local_addr(vd
);
4145 vnc_free_addresses(&saddr
, &nsaddr
);
4146 vnc_free_addresses(&wsaddr
, &nwsaddr
);
4150 vnc_display_close(vd
);
4154 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
4156 VncDisplay
*vd
= vnc_display_find(id
);
4157 QIOChannelSocket
*sioc
;
4163 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4165 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4166 vnc_connect(vd
, sioc
, skipauth
, false);
4167 object_unref(OBJECT(sioc
));
4171 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4176 id
= g_strdup("default");
4177 while (qemu_opts_find(olist
, id
)) {
4179 id
= g_strdup_printf("vnc%d", i
++);
4181 qemu_opts_set_id(opts
, id
);
4184 QemuOpts
*vnc_parse(const char *str
, Error
**errp
)
4186 QemuOptsList
*olist
= qemu_find_opts("vnc");
4187 QemuOpts
*opts
= qemu_opts_parse(olist
, str
, true, errp
);
4194 id
= qemu_opts_id(opts
);
4196 /* auto-assign id if not present */
4197 vnc_auto_assign_id(olist
, opts
);
4202 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4204 Error
*local_err
= NULL
;
4205 char *id
= (char *)qemu_opts_id(opts
);
4208 vnc_display_init(id
);
4209 vnc_display_open(id
, &local_err
);
4210 if (local_err
!= NULL
) {
4211 error_reportf_err(local_err
, "Failed to start VNC server: ");
4217 static void vnc_register_config(void)
4219 qemu_add_opts(&qemu_vnc_opts
);
4221 opts_init(vnc_register_config
);