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"
32 #include "sysemu/sysemu.h"
33 #include "qemu/error-report.h"
34 #include "qemu/sockets.h"
35 #include "qemu/timer.h"
37 #include "qemu/config-file.h"
38 #include "qapi/qmp/qerror.h"
39 #include "qapi/qmp/types.h"
40 #include "qmp-commands.h"
42 #include "qapi-event.h"
43 #include "crypto/hash.h"
44 #include "crypto/tlscredsanon.h"
45 #include "crypto/tlscredsx509.h"
46 #include "qom/object_interfaces.h"
47 #include "qemu/cutils.h"
48 #include "io/dns-resolver.h"
50 #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
51 #define VNC_REFRESH_INTERVAL_INC 50
52 #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
53 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
54 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
56 #include "vnc_keysym.h"
57 #include "crypto/cipher.h"
59 static QTAILQ_HEAD(, VncDisplay
) vnc_displays
=
60 QTAILQ_HEAD_INITIALIZER(vnc_displays
);
62 static int vnc_cursor_define(VncState
*vs
);
63 static void vnc_release_modifiers(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_KIND_INET
:
117 info
->host
= g_strdup(addr
->u
.inet
.data
->host
);
118 info
->service
= g_strdup(addr
->u
.inet
.data
->port
);
119 if (addr
->u
.inet
.data
->ipv6
) {
120 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
122 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
126 case SOCKET_ADDRESS_KIND_UNIX
:
127 info
->host
= g_strdup("");
128 info
->service
= g_strdup(addr
->u
.q_unix
.data
->path
);
129 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
133 error_setg(errp
, "Unsupported socket kind %d",
141 static void vnc_init_basic_info_from_server_addr(QIOChannelSocket
*ioc
,
145 SocketAddress
*addr
= NULL
;
148 error_setg(errp
, "No listener socket available");
152 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
157 vnc_init_basic_info(addr
, info
, errp
);
158 qapi_free_SocketAddress(addr
);
161 static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket
*ioc
,
165 SocketAddress
*addr
= NULL
;
167 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
172 vnc_init_basic_info(addr
, info
, errp
);
173 qapi_free_SocketAddress(addr
);
176 static const char *vnc_auth_name(VncDisplay
*vd
) {
178 case VNC_AUTH_INVALID
:
194 case VNC_AUTH_VENCRYPT
:
195 switch (vd
->subauth
) {
196 case VNC_AUTH_VENCRYPT_PLAIN
:
197 return "vencrypt+plain";
198 case VNC_AUTH_VENCRYPT_TLSNONE
:
199 return "vencrypt+tls+none";
200 case VNC_AUTH_VENCRYPT_TLSVNC
:
201 return "vencrypt+tls+vnc";
202 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
203 return "vencrypt+tls+plain";
204 case VNC_AUTH_VENCRYPT_X509NONE
:
205 return "vencrypt+x509+none";
206 case VNC_AUTH_VENCRYPT_X509VNC
:
207 return "vencrypt+x509+vnc";
208 case VNC_AUTH_VENCRYPT_X509PLAIN
:
209 return "vencrypt+x509+plain";
210 case VNC_AUTH_VENCRYPT_TLSSASL
:
211 return "vencrypt+tls+sasl";
212 case VNC_AUTH_VENCRYPT_X509SASL
:
213 return "vencrypt+x509+sasl";
223 static VncServerInfo
*vnc_server_info_get(VncDisplay
*vd
)
232 info
= g_malloc0(sizeof(*info
));
233 vnc_init_basic_info_from_server_addr(vd
->lsock
[0],
234 qapi_VncServerInfo_base(info
), &err
);
235 info
->has_auth
= true;
236 info
->auth
= g_strdup(vnc_auth_name(vd
));
238 qapi_free_VncServerInfo(info
);
245 static void vnc_client_cache_auth(VncState
*client
)
252 client
->info
->x509_dname
=
253 qcrypto_tls_session_get_peer_name(client
->tls
);
254 client
->info
->has_x509_dname
=
255 client
->info
->x509_dname
!= NULL
;
257 #ifdef CONFIG_VNC_SASL
258 if (client
->sasl
.conn
&&
259 client
->sasl
.username
) {
260 client
->info
->has_sasl_username
= true;
261 client
->info
->sasl_username
= g_strdup(client
->sasl
.username
);
266 static void vnc_client_cache_addr(VncState
*client
)
270 client
->info
= g_malloc0(sizeof(*client
->info
));
271 vnc_init_basic_info_from_remote_addr(client
->sioc
,
272 qapi_VncClientInfo_base(client
->info
),
275 qapi_free_VncClientInfo(client
->info
);
281 static void vnc_qmp_event(VncState
*vs
, QAPIEvent event
)
289 si
= vnc_server_info_get(vs
->vd
);
295 case QAPI_EVENT_VNC_CONNECTED
:
296 qapi_event_send_vnc_connected(si
, qapi_VncClientInfo_base(vs
->info
),
299 case QAPI_EVENT_VNC_INITIALIZED
:
300 qapi_event_send_vnc_initialized(si
, vs
->info
, &error_abort
);
302 case QAPI_EVENT_VNC_DISCONNECTED
:
303 qapi_event_send_vnc_disconnected(si
, vs
->info
, &error_abort
);
309 qapi_free_VncServerInfo(si
);
312 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
317 info
= g_malloc0(sizeof(*info
));
319 vnc_init_basic_info_from_remote_addr(client
->sioc
,
320 qapi_VncClientInfo_base(info
),
324 qapi_free_VncClientInfo(info
);
328 info
->websocket
= client
->websocket
;
331 info
->x509_dname
= qcrypto_tls_session_get_peer_name(client
->tls
);
332 info
->has_x509_dname
= info
->x509_dname
!= NULL
;
334 #ifdef CONFIG_VNC_SASL
335 if (client
->sasl
.conn
&& client
->sasl
.username
) {
336 info
->has_sasl_username
= true;
337 info
->sasl_username
= g_strdup(client
->sasl
.username
);
344 static VncDisplay
*vnc_display_find(const char *id
)
349 return QTAILQ_FIRST(&vnc_displays
);
351 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
352 if (strcmp(id
, vd
->id
) == 0) {
359 static VncClientInfoList
*qmp_query_client_list(VncDisplay
*vd
)
361 VncClientInfoList
*cinfo
, *prev
= NULL
;
364 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
365 cinfo
= g_new0(VncClientInfoList
, 1);
366 cinfo
->value
= qmp_query_vnc_client(client
);
373 VncInfo
*qmp_query_vnc(Error
**errp
)
375 VncInfo
*info
= g_malloc0(sizeof(*info
));
376 VncDisplay
*vd
= vnc_display_find(NULL
);
377 SocketAddress
*addr
= NULL
;
379 if (vd
== NULL
|| !vd
->nlsock
) {
380 info
->enabled
= false;
382 info
->enabled
= true;
384 /* for compatibility with the original command */
385 info
->has_clients
= true;
386 info
->clients
= qmp_query_client_list(vd
);
388 if (vd
->lsock
== NULL
) {
392 addr
= qio_channel_socket_get_local_address(vd
->lsock
[0], errp
);
397 switch (addr
->type
) {
398 case SOCKET_ADDRESS_KIND_INET
:
399 info
->host
= g_strdup(addr
->u
.inet
.data
->host
);
400 info
->service
= g_strdup(addr
->u
.inet
.data
->port
);
401 if (addr
->u
.inet
.data
->ipv6
) {
402 info
->family
= NETWORK_ADDRESS_FAMILY_IPV6
;
404 info
->family
= NETWORK_ADDRESS_FAMILY_IPV4
;
408 case SOCKET_ADDRESS_KIND_UNIX
:
409 info
->host
= g_strdup("");
410 info
->service
= g_strdup(addr
->u
.q_unix
.data
->path
);
411 info
->family
= NETWORK_ADDRESS_FAMILY_UNIX
;
415 error_setg(errp
, "Unsupported socket kind %d",
420 info
->has_host
= true;
421 info
->has_service
= true;
422 info
->has_family
= true;
424 info
->has_auth
= true;
425 info
->auth
= g_strdup(vnc_auth_name(vd
));
428 qapi_free_SocketAddress(addr
);
432 qapi_free_SocketAddress(addr
);
433 qapi_free_VncInfo(info
);
438 static void qmp_query_auth(int auth
, int subauth
,
439 VncPrimaryAuth
*qmp_auth
,
440 VncVencryptSubAuth
*qmp_vencrypt
,
441 bool *qmp_has_vencrypt
);
443 static VncServerInfo2List
*qmp_query_server_entry(QIOChannelSocket
*ioc
,
447 VncServerInfo2List
*prev
)
449 VncServerInfo2List
*list
;
450 VncServerInfo2
*info
;
454 addr
= qio_channel_socket_get_local_address(ioc
, &err
);
460 info
= g_new0(VncServerInfo2
, 1);
461 vnc_init_basic_info(addr
, qapi_VncServerInfo2_base(info
), &err
);
462 qapi_free_SocketAddress(addr
);
464 qapi_free_VncServerInfo2(info
);
468 info
->websocket
= websocket
;
470 qmp_query_auth(auth
, subauth
, &info
->auth
,
471 &info
->vencrypt
, &info
->has_vencrypt
);
473 list
= g_new0(VncServerInfo2List
, 1);
479 static void qmp_query_auth(int auth
, int subauth
,
480 VncPrimaryAuth
*qmp_auth
,
481 VncVencryptSubAuth
*qmp_vencrypt
,
482 bool *qmp_has_vencrypt
)
486 *qmp_auth
= VNC_PRIMARY_AUTH_VNC
;
489 *qmp_auth
= VNC_PRIMARY_AUTH_RA2
;
492 *qmp_auth
= VNC_PRIMARY_AUTH_RA2NE
;
495 *qmp_auth
= VNC_PRIMARY_AUTH_TIGHT
;
498 *qmp_auth
= VNC_PRIMARY_AUTH_ULTRA
;
501 *qmp_auth
= VNC_PRIMARY_AUTH_TLS
;
503 case VNC_AUTH_VENCRYPT
:
504 *qmp_auth
= VNC_PRIMARY_AUTH_VENCRYPT
;
505 *qmp_has_vencrypt
= true;
507 case VNC_AUTH_VENCRYPT_PLAIN
:
508 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_PLAIN
;
510 case VNC_AUTH_VENCRYPT_TLSNONE
:
511 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_NONE
;
513 case VNC_AUTH_VENCRYPT_TLSVNC
:
514 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_VNC
;
516 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
517 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN
;
519 case VNC_AUTH_VENCRYPT_X509NONE
:
520 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_NONE
;
522 case VNC_AUTH_VENCRYPT_X509VNC
:
523 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_VNC
;
525 case VNC_AUTH_VENCRYPT_X509PLAIN
:
526 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_PLAIN
;
528 case VNC_AUTH_VENCRYPT_TLSSASL
:
529 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_TLS_SASL
;
531 case VNC_AUTH_VENCRYPT_X509SASL
:
532 *qmp_vencrypt
= VNC_VENCRYPT_SUB_AUTH_X509_SASL
;
535 *qmp_has_vencrypt
= false;
540 *qmp_auth
= VNC_PRIMARY_AUTH_SASL
;
544 *qmp_auth
= VNC_PRIMARY_AUTH_NONE
;
549 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
551 VncInfo2List
*item
, *prev
= NULL
;
557 QTAILQ_FOREACH(vd
, &vnc_displays
, next
) {
558 info
= g_new0(VncInfo2
, 1);
559 info
->id
= g_strdup(vd
->id
);
560 info
->clients
= qmp_query_client_list(vd
);
561 qmp_query_auth(vd
->auth
, vd
->subauth
, &info
->auth
,
562 &info
->vencrypt
, &info
->has_vencrypt
);
564 dev
= DEVICE(object_property_get_link(OBJECT(vd
->dcl
.con
),
566 info
->has_display
= true;
567 info
->display
= g_strdup(dev
->id
);
569 for (i
= 0; i
< vd
->nlsock
; i
++) {
570 info
->server
= qmp_query_server_entry(
571 vd
->lsock
[i
], false, vd
->auth
, vd
->subauth
, info
->server
);
573 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
574 info
->server
= qmp_query_server_entry(
575 vd
->lwebsock
[i
], true, vd
->ws_auth
,
576 vd
->ws_subauth
, info
->server
);
579 item
= g_new0(VncInfo2List
, 1);
588 1) Get the queue working for IO.
589 2) there is some weirdness when using the -S option (the screen is grey
590 and not totally invalidated
591 3) resolutions > 1024
594 static int vnc_update_client(VncState
*vs
, int has_dirty
, bool sync
);
595 static void vnc_disconnect_start(VncState
*vs
);
597 static void vnc_colordepth(VncState
*vs
);
598 static void framebuffer_update_request(VncState
*vs
, int incremental
,
599 int x_position
, int y_position
,
601 static void vnc_refresh(DisplayChangeListener
*dcl
);
602 static int vnc_refresh_server_surface(VncDisplay
*vd
);
604 static int vnc_width(VncDisplay
*vd
)
606 return MIN(VNC_MAX_WIDTH
, ROUND_UP(surface_width(vd
->ds
),
607 VNC_DIRTY_PIXELS_PER_BIT
));
610 static int vnc_height(VncDisplay
*vd
)
612 return MIN(VNC_MAX_HEIGHT
, surface_height(vd
->ds
));
615 static void vnc_set_area_dirty(DECLARE_BITMAP(dirty
[VNC_MAX_HEIGHT
],
616 VNC_MAX_WIDTH
/ VNC_DIRTY_PIXELS_PER_BIT
),
618 int x
, int y
, int w
, int h
)
620 int width
= vnc_width(vd
);
621 int height
= vnc_height(vd
);
623 /* this is needed this to ensure we updated all affected
624 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
625 w
+= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
626 x
-= (x
% VNC_DIRTY_PIXELS_PER_BIT
);
630 w
= MIN(x
+ w
, width
) - x
;
631 h
= MIN(y
+ h
, height
);
634 bitmap_set(dirty
[y
], x
/ VNC_DIRTY_PIXELS_PER_BIT
,
635 DIV_ROUND_UP(w
, VNC_DIRTY_PIXELS_PER_BIT
));
639 static void vnc_dpy_update(DisplayChangeListener
*dcl
,
640 int x
, int y
, int w
, int h
)
642 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
643 struct VncSurface
*s
= &vd
->guest
;
645 vnc_set_area_dirty(s
->dirty
, vd
, x
, y
, w
, h
);
648 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
651 vnc_write_u16(vs
, x
);
652 vnc_write_u16(vs
, y
);
653 vnc_write_u16(vs
, w
);
654 vnc_write_u16(vs
, h
);
656 vnc_write_s32(vs
, encoding
);
660 static void vnc_desktop_resize(VncState
*vs
)
662 if (vs
->ioc
== NULL
|| !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
665 if (vs
->client_width
== pixman_image_get_width(vs
->vd
->server
) &&
666 vs
->client_height
== pixman_image_get_height(vs
->vd
->server
)) {
669 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
670 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
672 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
674 vnc_write_u16(vs
, 1); /* number of rects */
675 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
676 VNC_ENCODING_DESKTOPRESIZE
);
677 vnc_unlock_output(vs
);
681 static void vnc_abort_display_jobs(VncDisplay
*vd
)
685 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
688 vnc_unlock_output(vs
);
690 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
693 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
696 vnc_unlock_output(vs
);
700 int vnc_server_fb_stride(VncDisplay
*vd
)
702 return pixman_image_get_stride(vd
->server
);
705 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
709 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
710 ptr
+= y
* vnc_server_fb_stride(vd
);
711 ptr
+= x
* VNC_SERVER_FB_BYTES
;
715 static void vnc_update_server_surface(VncDisplay
*vd
)
719 qemu_pixman_image_unref(vd
->server
);
722 if (QTAILQ_EMPTY(&vd
->clients
)) {
726 width
= vnc_width(vd
);
727 height
= vnc_height(vd
);
728 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
732 memset(vd
->guest
.dirty
, 0x00, sizeof(vd
->guest
.dirty
));
733 vnc_set_area_dirty(vd
->guest
.dirty
, vd
, 0, 0,
737 static void vnc_dpy_switch(DisplayChangeListener
*dcl
,
738 DisplaySurface
*surface
)
740 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
743 vnc_abort_display_jobs(vd
);
747 vnc_update_server_surface(vd
);
750 qemu_pixman_image_unref(vd
->guest
.fb
);
751 vd
->guest
.fb
= pixman_image_ref(surface
->image
);
752 vd
->guest
.format
= surface
->format
;
754 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
756 vnc_desktop_resize(vs
);
757 if (vs
->vd
->cursor
) {
758 vnc_cursor_define(vs
);
760 memset(vs
->dirty
, 0x00, sizeof(vs
->dirty
));
761 vnc_set_area_dirty(vs
->dirty
, vd
, 0, 0,
768 static void vnc_write_pixels_copy(VncState
*vs
,
769 void *pixels
, int size
)
771 vnc_write(vs
, pixels
, size
);
774 /* slowest but generic code. */
775 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
779 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
780 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
781 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
782 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
784 # error need some bits here if you change VNC_SERVER_FB_FORMAT
786 v
= (r
<< vs
->client_pf
.rshift
) |
787 (g
<< vs
->client_pf
.gshift
) |
788 (b
<< vs
->client_pf
.bshift
);
789 switch (vs
->client_pf
.bytes_per_pixel
) {
819 static void vnc_write_pixels_generic(VncState
*vs
,
820 void *pixels1
, int size
)
824 if (VNC_SERVER_FB_BYTES
== 4) {
825 uint32_t *pixels
= pixels1
;
828 for (i
= 0; i
< n
; i
++) {
829 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
830 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
835 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
839 VncDisplay
*vd
= vs
->vd
;
841 row
= vnc_server_fb_ptr(vd
, x
, y
);
842 for (i
= 0; i
< h
; i
++) {
843 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
844 row
+= vnc_server_fb_stride(vd
);
849 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
852 bool encode_raw
= false;
853 size_t saved_offs
= vs
->output
.offset
;
855 switch(vs
->vnc_encoding
) {
856 case VNC_ENCODING_ZLIB
:
857 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
859 case VNC_ENCODING_HEXTILE
:
860 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
861 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
863 case VNC_ENCODING_TIGHT
:
864 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
866 case VNC_ENCODING_TIGHT_PNG
:
867 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
869 case VNC_ENCODING_ZRLE
:
870 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
872 case VNC_ENCODING_ZYWRLE
:
873 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
880 /* If the client has the same pixel format as our internal buffer and
881 * a RAW encoding would need less space fall back to RAW encoding to
882 * save bandwidth and processing power in the client. */
883 if (!encode_raw
&& vs
->write_pixels
== vnc_write_pixels_copy
&&
884 12 + h
* w
* VNC_SERVER_FB_BYTES
<= (vs
->output
.offset
- saved_offs
)) {
885 vs
->output
.offset
= saved_offs
;
890 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
891 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
897 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
899 /* send bitblit op to the vnc client */
901 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
903 vnc_write_u16(vs
, 1); /* number of rects */
904 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
905 vnc_write_u16(vs
, src_x
);
906 vnc_write_u16(vs
, src_y
);
907 vnc_unlock_output(vs
);
911 static void vnc_dpy_copy(DisplayChangeListener
*dcl
,
912 int src_x
, int src_y
,
913 int dst_x
, int dst_y
, int w
, int h
)
915 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
919 int i
, x
, y
, pitch
, inc
, w_lim
, s
;
923 /* no client connected */
927 vnc_refresh_server_surface(vd
);
928 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
929 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
930 vs
->force_update
= 1;
931 vnc_update_client(vs
, 1, true);
932 /* vs might be free()ed here */
937 /* no client connected */
940 /* do bitblit op on the local surface too */
941 pitch
= vnc_server_fb_stride(vd
);
942 src_row
= vnc_server_fb_ptr(vd
, src_x
, src_y
);
943 dst_row
= vnc_server_fb_ptr(vd
, dst_x
, dst_y
);
948 src_row
+= pitch
* (h
-1);
949 dst_row
+= pitch
* (h
-1);
954 w_lim
= w
- (VNC_DIRTY_PIXELS_PER_BIT
- (dst_x
% VNC_DIRTY_PIXELS_PER_BIT
));
958 w_lim
= w
- (w_lim
% VNC_DIRTY_PIXELS_PER_BIT
);
960 for (i
= 0; i
< h
; i
++) {
961 for (x
= 0; x
<= w_lim
;
962 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
964 if ((s
= w
- w_lim
) == 0)
967 s
= (VNC_DIRTY_PIXELS_PER_BIT
-
968 (dst_x
% VNC_DIRTY_PIXELS_PER_BIT
));
971 s
= VNC_DIRTY_PIXELS_PER_BIT
;
973 cmp_bytes
= s
* VNC_SERVER_FB_BYTES
;
974 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
976 memmove(dst_row
, src_row
, cmp_bytes
);
977 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
978 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
979 set_bit(((x
+ dst_x
) / VNC_DIRTY_PIXELS_PER_BIT
),
984 src_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
985 dst_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
989 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
990 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
991 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
996 static void vnc_mouse_set(DisplayChangeListener
*dcl
,
997 int x
, int y
, int visible
)
999 /* can we ask the client(s) to move the pointer ??? */
1002 static int vnc_cursor_define(VncState
*vs
)
1004 QEMUCursor
*c
= vs
->vd
->cursor
;
1007 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
1008 vnc_lock_output(vs
);
1009 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1010 vnc_write_u8(vs
, 0); /* padding */
1011 vnc_write_u16(vs
, 1); /* # of rects */
1012 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
1013 VNC_ENCODING_RICH_CURSOR
);
1014 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
1015 vnc_write_pixels_generic(vs
, c
->data
, isize
);
1016 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
1017 vnc_unlock_output(vs
);
1023 static void vnc_dpy_cursor_define(DisplayChangeListener
*dcl
,
1026 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
1029 cursor_put(vd
->cursor
);
1030 g_free(vd
->cursor_mask
);
1033 cursor_get(vd
->cursor
);
1034 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
1035 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
1036 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
1038 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
1039 vnc_cursor_define(vs
);
1043 static int find_and_clear_dirty_height(VncState
*vs
,
1044 int y
, int last_x
, int x
, int height
)
1048 for (h
= 1; h
< (height
- y
); h
++) {
1049 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
1052 bitmap_clear(vs
->dirty
[y
+ h
], last_x
, x
- last_x
);
1058 static int vnc_update_client(VncState
*vs
, int has_dirty
, bool sync
)
1060 if (vs
->disconnecting
) {
1061 vnc_disconnect_finish(vs
);
1065 vs
->has_dirty
+= has_dirty
;
1066 if (vs
->need_update
&& !vs
->disconnecting
) {
1067 VncDisplay
*vd
= vs
->vd
;
1073 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
1074 /* kernel send buffers are full -> drop frames to throttle */
1077 if (!vs
->has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
1081 * Send screen updates to the vnc client using the server
1082 * surface and server dirty map. guest surface updates
1083 * happening in parallel don't disturb us, the next pass will
1084 * send them to the client.
1086 job
= vnc_job_new(vs
);
1088 height
= pixman_image_get_height(vd
->server
);
1089 width
= pixman_image_get_width(vd
->server
);
1095 unsigned long offset
= find_next_bit((unsigned long *) &vs
->dirty
,
1096 height
* VNC_DIRTY_BPL(vs
),
1097 y
* VNC_DIRTY_BPL(vs
));
1098 if (offset
== height
* VNC_DIRTY_BPL(vs
)) {
1099 /* no more dirty bits */
1102 y
= offset
/ VNC_DIRTY_BPL(vs
);
1103 x
= offset
% VNC_DIRTY_BPL(vs
);
1104 x2
= find_next_zero_bit((unsigned long *) &vs
->dirty
[y
],
1105 VNC_DIRTY_BPL(vs
), x
);
1106 bitmap_clear(vs
->dirty
[y
], x
, x2
- x
);
1107 h
= find_and_clear_dirty_height(vs
, y
, x
, x2
, height
);
1108 x2
= MIN(x2
, width
/ VNC_DIRTY_PIXELS_PER_BIT
);
1110 n
+= vnc_job_add_rect(job
, x
* VNC_DIRTY_PIXELS_PER_BIT
, y
,
1111 (x2
- x
) * VNC_DIRTY_PIXELS_PER_BIT
, h
);
1113 if (!x
&& x2
== width
/ VNC_DIRTY_PIXELS_PER_BIT
) {
1125 vs
->force_update
= 0;
1130 if (vs
->disconnecting
) {
1131 vnc_disconnect_finish(vs
);
1140 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
1142 VncState
*vs
= opaque
;
1145 case AUD_CNOTIFY_DISABLE
:
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_END
);
1150 vnc_unlock_output(vs
);
1154 case AUD_CNOTIFY_ENABLE
:
1155 vnc_lock_output(vs
);
1156 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1157 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1158 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
1159 vnc_unlock_output(vs
);
1165 static void audio_capture_destroy(void *opaque
)
1169 static void audio_capture(void *opaque
, void *buf
, int size
)
1171 VncState
*vs
= opaque
;
1173 vnc_lock_output(vs
);
1174 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
1175 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
1176 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
1177 vnc_write_u32(vs
, size
);
1178 vnc_write(vs
, buf
, size
);
1179 vnc_unlock_output(vs
);
1183 static void audio_add(VncState
*vs
)
1185 struct audio_capture_ops ops
;
1187 if (vs
->audio_cap
) {
1188 error_report("audio already running");
1192 ops
.notify
= audio_capture_notify
;
1193 ops
.destroy
= audio_capture_destroy
;
1194 ops
.capture
= audio_capture
;
1196 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1197 if (!vs
->audio_cap
) {
1198 error_report("Failed to add audio capture");
1202 static void audio_del(VncState
*vs
)
1204 if (vs
->audio_cap
) {
1205 AUD_del_capture(vs
->audio_cap
, vs
);
1206 vs
->audio_cap
= NULL
;
1210 static void vnc_disconnect_start(VncState
*vs
)
1212 if (vs
->disconnecting
) {
1215 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1217 g_source_remove(vs
->ioc_tag
);
1219 qio_channel_close(vs
->ioc
, NULL
);
1220 vs
->disconnecting
= TRUE
;
1223 void vnc_disconnect_finish(VncState
*vs
)
1227 vnc_jobs_join(vs
); /* Wait encoding jobs */
1229 vnc_lock_output(vs
);
1230 vnc_qmp_event(vs
, QAPI_EVENT_VNC_DISCONNECTED
);
1232 buffer_free(&vs
->input
);
1233 buffer_free(&vs
->output
);
1235 qapi_free_VncClientInfo(vs
->info
);
1238 vnc_tight_clear(vs
);
1241 #ifdef CONFIG_VNC_SASL
1242 vnc_sasl_client_cleanup(vs
);
1243 #endif /* CONFIG_VNC_SASL */
1245 vnc_release_modifiers(vs
);
1247 if (vs
->mouse_mode_notifier
.notify
!= NULL
) {
1248 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1250 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1251 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1252 /* last client gone */
1253 vnc_update_server_surface(vs
->vd
);
1256 vnc_unlock_output(vs
);
1258 qemu_mutex_destroy(&vs
->output_mutex
);
1259 if (vs
->bh
!= NULL
) {
1260 qemu_bh_delete(vs
->bh
);
1262 buffer_free(&vs
->jobs_buffer
);
1264 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1265 g_free(vs
->lossy_rect
[i
]);
1267 g_free(vs
->lossy_rect
);
1269 object_unref(OBJECT(vs
->ioc
));
1271 object_unref(OBJECT(vs
->sioc
));
1276 ssize_t
vnc_client_io_error(VncState
*vs
, ssize_t ret
, Error
**errp
)
1280 VNC_DEBUG("Closing down client sock: EOF\n");
1281 vnc_disconnect_start(vs
);
1282 } else if (ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1283 VNC_DEBUG("Closing down client sock: ret %zd (%s)\n",
1284 ret
, errp
? error_get_pretty(*errp
) : "Unknown");
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 * -1 on error, and disconnects the client socket.
1320 ssize_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 * -1 on error, and disconnects the client socket.
1341 static ssize_t
vnc_client_write_plain(VncState
*vs
)
1345 #ifdef CONFIG_VNC_SASL
1346 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1347 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1348 vs
->sasl
.waitWriteSSF
);
1350 if (vs
->sasl
.conn
&&
1352 vs
->sasl
.waitWriteSSF
) {
1353 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1355 vs
->sasl
.waitWriteSSF
-= ret
;
1357 #endif /* CONFIG_VNC_SASL */
1358 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1362 buffer_advance(&vs
->output
, ret
);
1364 if (vs
->output
.offset
== 0) {
1366 g_source_remove(vs
->ioc_tag
);
1368 vs
->ioc_tag
= qio_channel_add_watch(
1369 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1377 * First function called whenever there is data to be written to
1378 * the client socket. Will delegate actual work according to whether
1379 * SASL SSF layers are enabled (thus requiring encryption calls)
1381 static void vnc_client_write_locked(VncState
*vs
)
1383 #ifdef CONFIG_VNC_SASL
1384 if (vs
->sasl
.conn
&&
1386 !vs
->sasl
.waitWriteSSF
) {
1387 vnc_client_write_sasl(vs
);
1389 #endif /* CONFIG_VNC_SASL */
1391 vnc_client_write_plain(vs
);
1395 static void vnc_client_write(VncState
*vs
)
1398 vnc_lock_output(vs
);
1399 if (vs
->output
.offset
) {
1400 vnc_client_write_locked(vs
);
1401 } else if (vs
->ioc
!= NULL
) {
1403 g_source_remove(vs
->ioc_tag
);
1405 vs
->ioc_tag
= qio_channel_add_watch(
1406 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
1408 vnc_unlock_output(vs
);
1411 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1413 vs
->read_handler
= func
;
1414 vs
->read_handler_expect
= expecting
;
1419 * Called to read a chunk of data from the client socket. The data may
1420 * be the raw data, or may need to be further decoded by SASL.
1421 * The data will be read either straight from to the socket, or
1422 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1424 * NB, it is theoretically possible to have 2 layers of encryption,
1425 * both SASL, and this TLS layer. It is highly unlikely in practice
1426 * though, since SASL encryption will typically be a no-op if TLS
1429 * Returns the number of bytes read, which may be less than
1430 * the requested 'datalen' if the socket would block. Returns
1431 * -1 on error, and disconnects the client socket.
1433 ssize_t
vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1437 ret
= qio_channel_read(
1438 vs
->ioc
, (char *)data
, datalen
, &err
);
1439 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1440 return vnc_client_io_error(vs
, ret
, &err
);
1445 * Called to read data from the client socket to the input buffer,
1446 * when not using any SASL SSF encryption layers. Will read as much
1447 * data as possible without blocking.
1449 * Returns the number of bytes read. Returns -1 on error, and
1450 * disconnects the client socket.
1452 static ssize_t
vnc_client_read_plain(VncState
*vs
)
1455 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1456 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1457 buffer_reserve(&vs
->input
, 4096);
1458 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1461 vs
->input
.offset
+= ret
;
1465 static void vnc_jobs_bh(void *opaque
)
1467 VncState
*vs
= opaque
;
1469 vnc_jobs_consume_buffer(vs
);
1473 * First function called whenever there is more data to be read from
1474 * the client socket. Will delegate actual work according to whether
1475 * SASL SSF layers are enabled (thus requiring decryption calls)
1476 * Returns 0 on success, -1 if client disconnected
1478 static int vnc_client_read(VncState
*vs
)
1482 #ifdef CONFIG_VNC_SASL
1483 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1484 ret
= vnc_client_read_sasl(vs
);
1486 #endif /* CONFIG_VNC_SASL */
1487 ret
= vnc_client_read_plain(vs
);
1489 if (vs
->disconnecting
) {
1490 vnc_disconnect_finish(vs
);
1496 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1497 size_t len
= vs
->read_handler_expect
;
1500 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1501 if (vs
->disconnecting
) {
1502 vnc_disconnect_finish(vs
);
1507 buffer_advance(&vs
->input
, len
);
1509 vs
->read_handler_expect
= ret
;
1515 gboolean
vnc_client_io(QIOChannel
*ioc G_GNUC_UNUSED
,
1516 GIOCondition condition
, void *opaque
)
1518 VncState
*vs
= opaque
;
1519 if (condition
& G_IO_IN
) {
1520 if (vnc_client_read(vs
) < 0) {
1524 if (condition
& G_IO_OUT
) {
1525 vnc_client_write(vs
);
1531 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1533 buffer_reserve(&vs
->output
, len
);
1535 if (vs
->ioc
!= NULL
&& buffer_empty(&vs
->output
)) {
1537 g_source_remove(vs
->ioc_tag
);
1539 vs
->ioc_tag
= qio_channel_add_watch(
1540 vs
->ioc
, G_IO_IN
| G_IO_OUT
, vnc_client_io
, vs
, NULL
);
1543 buffer_append(&vs
->output
, data
, len
);
1546 void vnc_write_s32(VncState
*vs
, int32_t value
)
1548 vnc_write_u32(vs
, *(uint32_t *)&value
);
1551 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1555 buf
[0] = (value
>> 24) & 0xFF;
1556 buf
[1] = (value
>> 16) & 0xFF;
1557 buf
[2] = (value
>> 8) & 0xFF;
1558 buf
[3] = value
& 0xFF;
1560 vnc_write(vs
, buf
, 4);
1563 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1567 buf
[0] = (value
>> 8) & 0xFF;
1568 buf
[1] = value
& 0xFF;
1570 vnc_write(vs
, buf
, 2);
1573 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1575 vnc_write(vs
, (char *)&value
, 1);
1578 void vnc_flush(VncState
*vs
)
1580 vnc_lock_output(vs
);
1581 if (vs
->ioc
!= NULL
&& vs
->output
.offset
) {
1582 vnc_client_write_locked(vs
);
1584 vnc_unlock_output(vs
);
1587 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1589 return data
[offset
];
1592 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1594 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1597 static int32_t read_s32(uint8_t *data
, size_t offset
)
1599 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1600 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1603 uint32_t read_u32(uint8_t *data
, size_t offset
)
1605 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1606 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1609 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1613 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1615 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1616 int absolute
= qemu_input_is_absolute();
1618 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1619 vnc_lock_output(vs
);
1620 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1621 vnc_write_u8(vs
, 0);
1622 vnc_write_u16(vs
, 1);
1623 vnc_framebuffer_update(vs
, absolute
, 0,
1624 pixman_image_get_width(vs
->vd
->server
),
1625 pixman_image_get_height(vs
->vd
->server
),
1626 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1627 vnc_unlock_output(vs
);
1630 vs
->absolute
= absolute
;
1633 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1635 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1636 [INPUT_BUTTON_LEFT
] = 0x01,
1637 [INPUT_BUTTON_MIDDLE
] = 0x02,
1638 [INPUT_BUTTON_RIGHT
] = 0x04,
1639 [INPUT_BUTTON_WHEEL_UP
] = 0x08,
1640 [INPUT_BUTTON_WHEEL_DOWN
] = 0x10,
1642 QemuConsole
*con
= vs
->vd
->dcl
.con
;
1643 int width
= pixman_image_get_width(vs
->vd
->server
);
1644 int height
= pixman_image_get_height(vs
->vd
->server
);
1646 if (vs
->last_bmask
!= button_mask
) {
1647 qemu_input_update_buttons(con
, bmap
, vs
->last_bmask
, button_mask
);
1648 vs
->last_bmask
= button_mask
;
1652 qemu_input_queue_abs(con
, INPUT_AXIS_X
, x
, width
);
1653 qemu_input_queue_abs(con
, INPUT_AXIS_Y
, y
, height
);
1654 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1655 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- 0x7FFF);
1656 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- 0x7FFF);
1658 if (vs
->last_x
!= -1) {
1659 qemu_input_queue_rel(con
, INPUT_AXIS_X
, x
- vs
->last_x
);
1660 qemu_input_queue_rel(con
, INPUT_AXIS_Y
, y
- vs
->last_y
);
1665 qemu_input_event_sync();
1668 static void reset_keys(VncState
*vs
)
1671 for(i
= 0; i
< 256; i
++) {
1672 if (vs
->modifiers_state
[i
]) {
1673 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, i
, false);
1674 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1675 vs
->modifiers_state
[i
] = 0;
1680 static void press_key(VncState
*vs
, int keysym
)
1682 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1683 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, true);
1684 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1685 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, false);
1686 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1689 static void vnc_led_state_change(VncState
*vs
)
1691 if (!vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
)) {
1695 vnc_lock_output(vs
);
1696 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1697 vnc_write_u8(vs
, 0);
1698 vnc_write_u16(vs
, 1);
1699 vnc_framebuffer_update(vs
, 0, 0, 1, 1, VNC_ENCODING_LED_STATE
);
1700 vnc_write_u8(vs
, vs
->vd
->ledstate
);
1701 vnc_unlock_output(vs
);
1705 static void kbd_leds(void *opaque
, int ledstate
)
1707 VncDisplay
*vd
= opaque
;
1710 trace_vnc_key_guest_leds((ledstate
& QEMU_CAPS_LOCK_LED
),
1711 (ledstate
& QEMU_NUM_LOCK_LED
),
1712 (ledstate
& QEMU_SCROLL_LOCK_LED
));
1714 if (ledstate
== vd
->ledstate
) {
1718 vd
->ledstate
= ledstate
;
1720 QTAILQ_FOREACH(client
, &vd
->clients
, next
) {
1721 vnc_led_state_change(client
);
1725 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1727 /* QEMU console switch */
1729 case 0x2a: /* Left Shift */
1730 case 0x36: /* Right Shift */
1731 case 0x1d: /* Left CTRL */
1732 case 0x9d: /* Right CTRL */
1733 case 0x38: /* Left ALT */
1734 case 0xb8: /* Right ALT */
1736 vs
->modifiers_state
[keycode
] = 1;
1738 vs
->modifiers_state
[keycode
] = 0;
1740 case 0x02 ... 0x0a: /* '1' to '9' keys */
1741 if (vs
->vd
->dcl
.con
== NULL
&&
1742 down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1743 /* Reset the modifiers sent to the current console */
1745 console_select(keycode
- 0x02);
1749 case 0x3a: /* CapsLock */
1750 case 0x45: /* NumLock */
1752 vs
->modifiers_state
[keycode
] ^= 1;
1756 /* Turn off the lock state sync logic if the client support the led
1759 if (down
&& vs
->vd
->lock_key_sync
&&
1760 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1761 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1762 /* If the numlock state needs to change then simulate an additional
1763 keypress before sending this one. This will happen if the user
1764 toggles numlock away from the VNC window.
1766 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1767 if (!vs
->modifiers_state
[0x45]) {
1768 trace_vnc_key_sync_numlock(true);
1769 vs
->modifiers_state
[0x45] = 1;
1770 press_key(vs
, 0xff7f);
1773 if (vs
->modifiers_state
[0x45]) {
1774 trace_vnc_key_sync_numlock(false);
1775 vs
->modifiers_state
[0x45] = 0;
1776 press_key(vs
, 0xff7f);
1781 if (down
&& vs
->vd
->lock_key_sync
&&
1782 !vnc_has_feature(vs
, VNC_FEATURE_LED_STATE
) &&
1783 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1784 /* If the capslock state needs to change then simulate an additional
1785 keypress before sending this one. This will happen if the user
1786 toggles capslock away from the VNC window.
1788 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1789 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1790 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1792 if (uppercase
== shift
) {
1793 trace_vnc_key_sync_capslock(false);
1794 vs
->modifiers_state
[0x3a] = 0;
1795 press_key(vs
, 0xffe5);
1798 if (uppercase
!= shift
) {
1799 trace_vnc_key_sync_capslock(true);
1800 vs
->modifiers_state
[0x3a] = 1;
1801 press_key(vs
, 0xffe5);
1806 if (qemu_console_is_graphic(NULL
)) {
1807 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, down
);
1808 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1810 bool numlock
= vs
->modifiers_state
[0x45];
1811 bool control
= (vs
->modifiers_state
[0x1d] ||
1812 vs
->modifiers_state
[0x9d]);
1813 /* QEMU console emulation */
1816 case 0x2a: /* Left Shift */
1817 case 0x36: /* Right Shift */
1818 case 0x1d: /* Left CTRL */
1819 case 0x9d: /* Right CTRL */
1820 case 0x38: /* Left ALT */
1821 case 0xb8: /* Right ALT */
1824 kbd_put_keysym(QEMU_KEY_UP
);
1827 kbd_put_keysym(QEMU_KEY_DOWN
);
1830 kbd_put_keysym(QEMU_KEY_LEFT
);
1833 kbd_put_keysym(QEMU_KEY_RIGHT
);
1836 kbd_put_keysym(QEMU_KEY_DELETE
);
1839 kbd_put_keysym(QEMU_KEY_HOME
);
1842 kbd_put_keysym(QEMU_KEY_END
);
1845 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1848 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1852 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1855 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1858 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1861 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1864 kbd_put_keysym('5');
1867 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1870 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1873 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1876 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1879 kbd_put_keysym('0');
1882 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1886 kbd_put_keysym('/');
1889 kbd_put_keysym('*');
1892 kbd_put_keysym('-');
1895 kbd_put_keysym('+');
1898 kbd_put_keysym('\n');
1903 kbd_put_keysym(sym
& 0x1f);
1905 kbd_put_keysym(sym
);
1913 static void vnc_release_modifiers(VncState
*vs
)
1915 static const int keycodes
[] = {
1916 /* shift, control, alt keys, both left & right */
1917 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1921 if (!qemu_console_is_graphic(NULL
)) {
1924 for (i
= 0; i
< ARRAY_SIZE(keycodes
); i
++) {
1925 keycode
= keycodes
[i
];
1926 if (!vs
->modifiers_state
[keycode
]) {
1929 qemu_input_event_send_key_number(vs
->vd
->dcl
.con
, keycode
, false);
1930 qemu_input_event_send_key_delay(vs
->vd
->key_delay_ms
);
1934 static const char *code2name(int keycode
)
1936 return QKeyCode_lookup
[qemu_input_key_number_to_qcode(keycode
)];
1939 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1944 if (lsym
>= 'A' && lsym
<= 'Z' && qemu_console_is_graphic(NULL
)) {
1945 lsym
= lsym
- 'A' + 'a';
1948 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1949 trace_vnc_key_event_map(down
, sym
, keycode
, code2name(keycode
));
1950 do_key_event(vs
, down
, keycode
, sym
);
1953 static void ext_key_event(VncState
*vs
, int down
,
1954 uint32_t sym
, uint16_t keycode
)
1956 /* if the user specifies a keyboard layout, always use it */
1957 if (keyboard_layout
) {
1958 key_event(vs
, down
, sym
);
1960 trace_vnc_key_event_ext(down
, sym
, keycode
, code2name(keycode
));
1961 do_key_event(vs
, down
, keycode
, sym
);
1965 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1966 int x
, int y
, int w
, int h
)
1968 vs
->need_update
= 1;
1974 vs
->force_update
= 1;
1975 vnc_set_area_dirty(vs
->dirty
, vs
->vd
, x
, y
, w
, h
);
1978 static void send_ext_key_event_ack(VncState
*vs
)
1980 vnc_lock_output(vs
);
1981 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1982 vnc_write_u8(vs
, 0);
1983 vnc_write_u16(vs
, 1);
1984 vnc_framebuffer_update(vs
, 0, 0,
1985 pixman_image_get_width(vs
->vd
->server
),
1986 pixman_image_get_height(vs
->vd
->server
),
1987 VNC_ENCODING_EXT_KEY_EVENT
);
1988 vnc_unlock_output(vs
);
1992 static void send_ext_audio_ack(VncState
*vs
)
1994 vnc_lock_output(vs
);
1995 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1996 vnc_write_u8(vs
, 0);
1997 vnc_write_u16(vs
, 1);
1998 vnc_framebuffer_update(vs
, 0, 0,
1999 pixman_image_get_width(vs
->vd
->server
),
2000 pixman_image_get_height(vs
->vd
->server
),
2001 VNC_ENCODING_AUDIO
);
2002 vnc_unlock_output(vs
);
2006 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
2009 unsigned int enc
= 0;
2012 vs
->vnc_encoding
= 0;
2013 vs
->tight
.compression
= 9;
2014 vs
->tight
.quality
= -1; /* Lossless by default */
2018 * Start from the end because the encodings are sent in order of preference.
2019 * This way the preferred encoding (first encoding defined in the array)
2020 * will be set at the end of the loop.
2022 for (i
= n_encodings
- 1; i
>= 0; i
--) {
2025 case VNC_ENCODING_RAW
:
2026 vs
->vnc_encoding
= enc
;
2028 case VNC_ENCODING_COPYRECT
:
2029 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
2031 case VNC_ENCODING_HEXTILE
:
2032 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
2033 vs
->vnc_encoding
= enc
;
2035 case VNC_ENCODING_TIGHT
:
2036 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
2037 vs
->vnc_encoding
= enc
;
2039 #ifdef CONFIG_VNC_PNG
2040 case VNC_ENCODING_TIGHT_PNG
:
2041 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
2042 vs
->vnc_encoding
= enc
;
2045 case VNC_ENCODING_ZLIB
:
2046 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
2047 vs
->vnc_encoding
= enc
;
2049 case VNC_ENCODING_ZRLE
:
2050 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
2051 vs
->vnc_encoding
= enc
;
2053 case VNC_ENCODING_ZYWRLE
:
2054 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
2055 vs
->vnc_encoding
= enc
;
2057 case VNC_ENCODING_DESKTOPRESIZE
:
2058 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
2060 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
2061 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
2063 case VNC_ENCODING_RICH_CURSOR
:
2064 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
2065 if (vs
->vd
->cursor
) {
2066 vnc_cursor_define(vs
);
2069 case VNC_ENCODING_EXT_KEY_EVENT
:
2070 send_ext_key_event_ack(vs
);
2072 case VNC_ENCODING_AUDIO
:
2073 send_ext_audio_ack(vs
);
2075 case VNC_ENCODING_WMVi
:
2076 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
2078 case VNC_ENCODING_LED_STATE
:
2079 vs
->features
|= VNC_FEATURE_LED_STATE_MASK
;
2081 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
2082 vs
->tight
.compression
= (enc
& 0x0F);
2084 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
2085 if (vs
->vd
->lossy
) {
2086 vs
->tight
.quality
= (enc
& 0x0F);
2090 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
2094 vnc_desktop_resize(vs
);
2095 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
2096 vnc_led_state_change(vs
);
2099 static void set_pixel_conversion(VncState
*vs
)
2101 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
2103 if (fmt
== VNC_SERVER_FB_FORMAT
) {
2104 vs
->write_pixels
= vnc_write_pixels_copy
;
2105 vnc_hextile_set_pixel_conversion(vs
, 0);
2107 vs
->write_pixels
= vnc_write_pixels_generic
;
2108 vnc_hextile_set_pixel_conversion(vs
, 1);
2112 static void send_color_map(VncState
*vs
)
2116 vnc_write_u8(vs
, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES
);
2117 vnc_write_u8(vs
, 0); /* padding */
2118 vnc_write_u16(vs
, 0); /* first color */
2119 vnc_write_u16(vs
, 256); /* # of colors */
2121 for (i
= 0; i
< 256; i
++) {
2122 PixelFormat
*pf
= &vs
->client_pf
;
2124 vnc_write_u16(vs
, (((i
>> pf
->rshift
) & pf
->rmax
) << (16 - pf
->rbits
)));
2125 vnc_write_u16(vs
, (((i
>> pf
->gshift
) & pf
->gmax
) << (16 - pf
->gbits
)));
2126 vnc_write_u16(vs
, (((i
>> pf
->bshift
) & pf
->bmax
) << (16 - pf
->bbits
)));
2130 static void set_pixel_format(VncState
*vs
, int bits_per_pixel
,
2131 int big_endian_flag
, int true_color_flag
,
2132 int red_max
, int green_max
, int blue_max
,
2133 int red_shift
, int green_shift
, int blue_shift
)
2135 if (!true_color_flag
) {
2136 /* Expose a reasonable default 256 color map */
2146 switch (bits_per_pixel
) {
2152 vnc_client_error(vs
);
2156 vs
->client_pf
.rmax
= red_max
? red_max
: 0xFF;
2157 vs
->client_pf
.rbits
= hweight_long(red_max
);
2158 vs
->client_pf
.rshift
= red_shift
;
2159 vs
->client_pf
.rmask
= red_max
<< red_shift
;
2160 vs
->client_pf
.gmax
= green_max
? green_max
: 0xFF;
2161 vs
->client_pf
.gbits
= hweight_long(green_max
);
2162 vs
->client_pf
.gshift
= green_shift
;
2163 vs
->client_pf
.gmask
= green_max
<< green_shift
;
2164 vs
->client_pf
.bmax
= blue_max
? blue_max
: 0xFF;
2165 vs
->client_pf
.bbits
= hweight_long(blue_max
);
2166 vs
->client_pf
.bshift
= blue_shift
;
2167 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
2168 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
2169 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
2170 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
2171 vs
->client_be
= big_endian_flag
;
2173 if (!true_color_flag
) {
2177 set_pixel_conversion(vs
);
2179 graphic_hw_invalidate(vs
->vd
->dcl
.con
);
2180 graphic_hw_update(vs
->vd
->dcl
.con
);
2183 static void pixel_format_message (VncState
*vs
) {
2184 char pad
[3] = { 0, 0, 0 };
2186 vs
->client_pf
= qemu_default_pixelformat(32);
2188 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
2189 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
2191 #ifdef HOST_WORDS_BIGENDIAN
2192 vnc_write_u8(vs
, 1); /* big-endian-flag */
2194 vnc_write_u8(vs
, 0); /* big-endian-flag */
2196 vnc_write_u8(vs
, 1); /* true-color-flag */
2197 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
2198 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
2199 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
2200 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
2201 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
2202 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
2203 vnc_write(vs
, pad
, 3); /* padding */
2205 vnc_hextile_set_pixel_conversion(vs
, 0);
2206 vs
->write_pixels
= vnc_write_pixels_copy
;
2209 static void vnc_colordepth(VncState
*vs
)
2211 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
2212 /* Sending a WMVi message to notify the client*/
2213 vnc_lock_output(vs
);
2214 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
2215 vnc_write_u8(vs
, 0);
2216 vnc_write_u16(vs
, 1); /* number of rects */
2217 vnc_framebuffer_update(vs
, 0, 0,
2218 pixman_image_get_width(vs
->vd
->server
),
2219 pixman_image_get_height(vs
->vd
->server
),
2221 pixel_format_message(vs
);
2222 vnc_unlock_output(vs
);
2225 set_pixel_conversion(vs
);
2229 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2233 VncDisplay
*vd
= vs
->vd
;
2236 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2240 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2244 set_pixel_format(vs
, read_u8(data
, 4),
2245 read_u8(data
, 6), read_u8(data
, 7),
2246 read_u16(data
, 8), read_u16(data
, 10),
2247 read_u16(data
, 12), read_u8(data
, 14),
2248 read_u8(data
, 15), read_u8(data
, 16));
2250 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2255 limit
= read_u16(data
, 2);
2257 return 4 + (limit
* 4);
2259 limit
= read_u16(data
, 2);
2261 for (i
= 0; i
< limit
; i
++) {
2262 int32_t val
= read_s32(data
, 4 + (i
* 4));
2263 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2266 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2268 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2272 framebuffer_update_request(vs
,
2273 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2274 read_u16(data
, 6), read_u16(data
, 8));
2276 case VNC_MSG_CLIENT_KEY_EVENT
:
2280 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2282 case VNC_MSG_CLIENT_POINTER_EVENT
:
2286 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2288 case VNC_MSG_CLIENT_CUT_TEXT
:
2293 uint32_t dlen
= read_u32(data
, 4);
2294 if (dlen
> (1 << 20)) {
2295 error_report("vnc: client_cut_text msg payload has %u bytes"
2296 " which exceeds our limit of 1MB.", dlen
);
2297 vnc_client_error(vs
);
2305 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2307 case VNC_MSG_CLIENT_QEMU
:
2311 switch (read_u8(data
, 1)) {
2312 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2316 ext_key_event(vs
, read_u16(data
, 2),
2317 read_u32(data
, 4), read_u32(data
, 8));
2319 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2323 switch (read_u16 (data
, 2)) {
2324 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2327 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2330 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2333 switch (read_u8(data
, 4)) {
2334 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2335 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2336 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2337 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2338 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2339 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2341 VNC_DEBUG("Invalid audio format %d\n", read_u8(data
, 4));
2342 vnc_client_error(vs
);
2345 vs
->as
.nchannels
= read_u8(data
, 5);
2346 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2347 VNC_DEBUG("Invalid audio channel coount %d\n",
2349 vnc_client_error(vs
);
2352 vs
->as
.freq
= read_u32(data
, 6);
2355 VNC_DEBUG("Invalid audio message %d\n", read_u8(data
, 4));
2356 vnc_client_error(vs
);
2362 VNC_DEBUG("Msg: %d\n", read_u16(data
, 0));
2363 vnc_client_error(vs
);
2368 VNC_DEBUG("Msg: %d\n", data
[0]);
2369 vnc_client_error(vs
);
2373 vnc_read_when(vs
, protocol_client_msg
, 1);
2377 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2383 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2384 switch (vs
->vd
->share_policy
) {
2385 case VNC_SHARE_POLICY_IGNORE
:
2387 * Ignore the shared flag. Nothing to do here.
2389 * Doesn't conform to the rfb spec but is traditional qemu
2390 * behavior, thus left here as option for compatibility
2394 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2396 * Policy: Allow clients ask for exclusive access.
2398 * Implementation: When a client asks for exclusive access,
2399 * disconnect all others. Shared connects are allowed as long
2400 * as no exclusive connection exists.
2402 * This is how the rfb spec suggests to handle the shared flag.
2404 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2406 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2410 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2411 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2414 vnc_disconnect_start(client
);
2417 if (mode
== VNC_SHARE_MODE_SHARED
) {
2418 if (vs
->vd
->num_exclusive
> 0) {
2419 vnc_disconnect_start(vs
);
2424 case VNC_SHARE_POLICY_FORCE_SHARED
:
2426 * Policy: Shared connects only.
2427 * Implementation: Disallow clients asking for exclusive access.
2429 * Useful for shared desktop sessions where you don't want
2430 * someone forgetting to say -shared when running the vnc
2431 * client disconnect everybody else.
2433 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2434 vnc_disconnect_start(vs
);
2439 vnc_set_share_mode(vs
, mode
);
2441 if (vs
->vd
->num_shared
> vs
->vd
->connections_limit
) {
2442 vnc_disconnect_start(vs
);
2446 vs
->client_width
= pixman_image_get_width(vs
->vd
->server
);
2447 vs
->client_height
= pixman_image_get_height(vs
->vd
->server
);
2448 vnc_write_u16(vs
, vs
->client_width
);
2449 vnc_write_u16(vs
, vs
->client_height
);
2451 pixel_format_message(vs
);
2454 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2455 if (size
> sizeof(buf
)) {
2459 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2462 vnc_write_u32(vs
, size
);
2463 vnc_write(vs
, buf
, size
);
2466 vnc_client_cache_auth(vs
);
2467 vnc_qmp_event(vs
, QAPI_EVENT_VNC_INITIALIZED
);
2469 vnc_read_when(vs
, protocol_client_msg
, 1);
2474 void start_client_init(VncState
*vs
)
2476 vnc_read_when(vs
, protocol_client_init
, 1);
2479 static void make_challenge(VncState
*vs
)
2483 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2485 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2486 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2489 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2491 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2493 unsigned char key
[8];
2494 time_t now
= time(NULL
);
2495 QCryptoCipher
*cipher
= NULL
;
2498 if (!vs
->vd
->password
) {
2499 VNC_DEBUG("No password configured on server");
2502 if (vs
->vd
->expires
< now
) {
2503 VNC_DEBUG("Password is expired");
2507 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2509 /* Calculate the expected challenge response */
2510 pwlen
= strlen(vs
->vd
->password
);
2511 for (i
=0; i
<sizeof(key
); i
++)
2512 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2514 cipher
= qcrypto_cipher_new(
2515 QCRYPTO_CIPHER_ALG_DES_RFB
,
2516 QCRYPTO_CIPHER_MODE_ECB
,
2517 key
, G_N_ELEMENTS(key
),
2520 VNC_DEBUG("Cannot initialize cipher %s",
2521 error_get_pretty(err
));
2526 if (qcrypto_cipher_encrypt(cipher
,
2529 VNC_AUTH_CHALLENGE_SIZE
,
2531 VNC_DEBUG("Cannot encrypt challenge %s",
2532 error_get_pretty(err
));
2537 /* Compare expected vs actual challenge response */
2538 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2539 VNC_DEBUG("Client challenge response did not match\n");
2542 VNC_DEBUG("Accepting VNC challenge response\n");
2543 vnc_write_u32(vs
, 0); /* Accept auth */
2546 start_client_init(vs
);
2549 qcrypto_cipher_free(cipher
);
2553 vnc_write_u32(vs
, 1); /* Reject auth */
2554 if (vs
->minor
>= 8) {
2555 static const char err
[] = "Authentication failed";
2556 vnc_write_u32(vs
, sizeof(err
));
2557 vnc_write(vs
, err
, sizeof(err
));
2560 vnc_client_error(vs
);
2561 qcrypto_cipher_free(cipher
);
2565 void start_auth_vnc(VncState
*vs
)
2568 /* Send client a 'random' challenge */
2569 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2572 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2576 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2578 /* We only advertise 1 auth scheme at a time, so client
2579 * must pick the one we sent. Verify this */
2580 if (data
[0] != vs
->auth
) { /* Reject auth */
2581 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
2582 vnc_write_u32(vs
, 1);
2583 if (vs
->minor
>= 8) {
2584 static const char err
[] = "Authentication failed";
2585 vnc_write_u32(vs
, sizeof(err
));
2586 vnc_write(vs
, err
, sizeof(err
));
2588 vnc_client_error(vs
);
2589 } else { /* Accept requested auth */
2590 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2593 VNC_DEBUG("Accept auth none\n");
2594 if (vs
->minor
>= 8) {
2595 vnc_write_u32(vs
, 0); /* Accept auth completion */
2598 start_client_init(vs
);
2602 VNC_DEBUG("Start VNC auth\n");
2606 case VNC_AUTH_VENCRYPT
:
2607 VNC_DEBUG("Accept VeNCrypt auth\n");
2608 start_auth_vencrypt(vs
);
2611 #ifdef CONFIG_VNC_SASL
2613 VNC_DEBUG("Accept SASL auth\n");
2614 start_auth_sasl(vs
);
2616 #endif /* CONFIG_VNC_SASL */
2618 default: /* Should not be possible, but just in case */
2619 VNC_DEBUG("Reject auth %d server code bug\n", vs
->auth
);
2620 vnc_write_u8(vs
, 1);
2621 if (vs
->minor
>= 8) {
2622 static const char err
[] = "Authentication failed";
2623 vnc_write_u32(vs
, sizeof(err
));
2624 vnc_write(vs
, err
, sizeof(err
));
2626 vnc_client_error(vs
);
2632 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2636 memcpy(local
, version
, 12);
2639 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2640 VNC_DEBUG("Malformed protocol version %s\n", local
);
2641 vnc_client_error(vs
);
2644 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2645 if (vs
->major
!= 3 ||
2651 VNC_DEBUG("Unsupported client version\n");
2652 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2654 vnc_client_error(vs
);
2657 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2658 * as equivalent to v3.3 by servers
2660 if (vs
->minor
== 4 || vs
->minor
== 5)
2663 if (vs
->minor
== 3) {
2664 if (vs
->auth
== VNC_AUTH_NONE
) {
2665 VNC_DEBUG("Tell client auth none\n");
2666 vnc_write_u32(vs
, vs
->auth
);
2668 start_client_init(vs
);
2669 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2670 VNC_DEBUG("Tell client VNC auth\n");
2671 vnc_write_u32(vs
, vs
->auth
);
2675 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->auth
);
2676 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2678 vnc_client_error(vs
);
2681 VNC_DEBUG("Telling client we support auth %d\n", vs
->auth
);
2682 vnc_write_u8(vs
, 1); /* num auth */
2683 vnc_write_u8(vs
, vs
->auth
);
2684 vnc_read_when(vs
, protocol_client_auth
, 1);
2691 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2693 struct VncSurface
*vs
= &vd
->guest
;
2695 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2698 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2702 w
= (x
+ w
) / VNC_STAT_RECT
;
2703 h
= (y
+ h
) / VNC_STAT_RECT
;
2707 for (j
= y
; j
<= h
; j
++) {
2708 for (i
= x
; i
<= w
; i
++) {
2709 vs
->lossy_rect
[j
][i
] = 1;
2714 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2717 int sty
= y
/ VNC_STAT_RECT
;
2718 int stx
= x
/ VNC_STAT_RECT
;
2721 y
= y
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2722 x
= x
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2724 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2727 /* kernel send buffers are full -> refresh later */
2728 if (vs
->output
.offset
) {
2732 if (!vs
->lossy_rect
[sty
][stx
]) {
2736 vs
->lossy_rect
[sty
][stx
] = 0;
2737 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2738 bitmap_set(vs
->dirty
[y
+ j
],
2739 x
/ VNC_DIRTY_PIXELS_PER_BIT
,
2740 VNC_STAT_RECT
/ VNC_DIRTY_PIXELS_PER_BIT
);
2748 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2750 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2751 pixman_image_get_width(vd
->server
));
2752 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2753 pixman_image_get_height(vd
->server
));
2758 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2759 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2760 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2762 rect
->updated
= false;
2766 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2768 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2771 vd
->guest
.last_freq_check
= *tv
;
2773 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2774 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2775 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2776 int count
= ARRAY_SIZE(rect
->times
);
2777 struct timeval min
, max
;
2779 if (!timerisset(&rect
->times
[count
- 1])) {
2783 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2784 qemu_timersub(tv
, &max
, &res
);
2786 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2788 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2789 memset(rect
->times
, 0, sizeof (rect
->times
));
2793 min
= rect
->times
[rect
->idx
];
2794 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2795 qemu_timersub(&max
, &min
, &res
);
2797 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2798 rect
->freq
/= count
;
2799 rect
->freq
= 1. / rect
->freq
;
2805 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2811 x
= (x
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2812 y
= (y
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2814 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2815 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2816 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2828 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2832 rect
= vnc_stat_rect(vd
, x
, y
);
2833 if (rect
->updated
) {
2836 rect
->times
[rect
->idx
] = *tv
;
2837 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2838 rect
->updated
= true;
2841 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2843 int width
= MIN(pixman_image_get_width(vd
->guest
.fb
),
2844 pixman_image_get_width(vd
->server
));
2845 int height
= MIN(pixman_image_get_height(vd
->guest
.fb
),
2846 pixman_image_get_height(vd
->server
));
2847 int cmp_bytes
, server_stride
, line_bytes
, guest_ll
, guest_stride
, y
= 0;
2848 uint8_t *guest_row0
= NULL
, *server_row0
;
2851 pixman_image_t
*tmpbuf
= NULL
;
2853 struct timeval tv
= { 0, 0 };
2855 if (!vd
->non_adaptive
) {
2856 gettimeofday(&tv
, NULL
);
2857 has_dirty
= vnc_update_stats(vd
, &tv
);
2861 * Walk through the guest dirty map.
2862 * Check and copy modified bits from guest to server surface.
2863 * Update server dirty map.
2865 server_row0
= (uint8_t *)pixman_image_get_data(vd
->server
);
2866 server_stride
= guest_stride
= guest_ll
=
2867 pixman_image_get_stride(vd
->server
);
2868 cmp_bytes
= MIN(VNC_DIRTY_PIXELS_PER_BIT
* VNC_SERVER_FB_BYTES
,
2870 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2871 int width
= pixman_image_get_width(vd
->server
);
2872 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2875 PIXMAN_FORMAT_BPP(pixman_image_get_format(vd
->guest
.fb
));
2876 guest_row0
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2877 guest_stride
= pixman_image_get_stride(vd
->guest
.fb
);
2878 guest_ll
= pixman_image_get_width(vd
->guest
.fb
) * ((guest_bpp
+ 7) / 8);
2880 line_bytes
= MIN(server_stride
, guest_ll
);
2884 uint8_t *guest_ptr
, *server_ptr
;
2885 unsigned long offset
= find_next_bit((unsigned long *) &vd
->guest
.dirty
,
2886 height
* VNC_DIRTY_BPL(&vd
->guest
),
2887 y
* VNC_DIRTY_BPL(&vd
->guest
));
2888 if (offset
== height
* VNC_DIRTY_BPL(&vd
->guest
)) {
2889 /* no more dirty bits */
2892 y
= offset
/ VNC_DIRTY_BPL(&vd
->guest
);
2893 x
= offset
% VNC_DIRTY_BPL(&vd
->guest
);
2895 server_ptr
= server_row0
+ y
* server_stride
+ x
* cmp_bytes
;
2897 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2898 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
2899 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2901 guest_ptr
= guest_row0
+ y
* guest_stride
;
2903 guest_ptr
+= x
* cmp_bytes
;
2905 for (; x
< DIV_ROUND_UP(width
, VNC_DIRTY_PIXELS_PER_BIT
);
2906 x
++, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2907 int _cmp_bytes
= cmp_bytes
;
2908 if (!test_and_clear_bit(x
, vd
->guest
.dirty
[y
])) {
2911 if ((x
+ 1) * cmp_bytes
> line_bytes
) {
2912 _cmp_bytes
= line_bytes
- x
* cmp_bytes
;
2914 assert(_cmp_bytes
>= 0);
2915 if (memcmp(server_ptr
, guest_ptr
, _cmp_bytes
) == 0) {
2918 memcpy(server_ptr
, guest_ptr
, _cmp_bytes
);
2919 if (!vd
->non_adaptive
) {
2920 vnc_rect_updated(vd
, x
* VNC_DIRTY_PIXELS_PER_BIT
,
2923 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2924 set_bit(x
, vs
->dirty
[y
]);
2931 qemu_pixman_image_unref(tmpbuf
);
2935 static void vnc_refresh(DisplayChangeListener
*dcl
)
2937 VncDisplay
*vd
= container_of(dcl
, VncDisplay
, dcl
);
2939 int has_dirty
, rects
= 0;
2941 if (QTAILQ_EMPTY(&vd
->clients
)) {
2942 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_MAX
);
2946 graphic_hw_update(vd
->dcl
.con
);
2948 if (vnc_trylock_display(vd
)) {
2949 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
2953 has_dirty
= vnc_refresh_server_surface(vd
);
2954 vnc_unlock_display(vd
);
2956 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2957 rects
+= vnc_update_client(vs
, has_dirty
, false);
2958 /* vs might be free()ed here */
2961 if (has_dirty
&& rects
) {
2962 vd
->dcl
.update_interval
/= 2;
2963 if (vd
->dcl
.update_interval
< VNC_REFRESH_INTERVAL_BASE
) {
2964 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_BASE
;
2967 vd
->dcl
.update_interval
+= VNC_REFRESH_INTERVAL_INC
;
2968 if (vd
->dcl
.update_interval
> VNC_REFRESH_INTERVAL_MAX
) {
2969 vd
->dcl
.update_interval
= VNC_REFRESH_INTERVAL_MAX
;
2974 static void vnc_connect(VncDisplay
*vd
, QIOChannelSocket
*sioc
,
2975 bool skipauth
, bool websocket
)
2977 VncState
*vs
= g_new0(VncState
, 1);
2978 bool first_client
= QTAILQ_EMPTY(&vd
->clients
);
2982 object_ref(OBJECT(vs
->sioc
));
2983 vs
->ioc
= QIO_CHANNEL(sioc
);
2984 object_ref(OBJECT(vs
->ioc
));
2987 buffer_init(&vs
->input
, "vnc-input/%p", sioc
);
2988 buffer_init(&vs
->output
, "vnc-output/%p", sioc
);
2989 buffer_init(&vs
->jobs_buffer
, "vnc-jobs_buffer/%p", sioc
);
2991 buffer_init(&vs
->tight
.tight
, "vnc-tight/%p", sioc
);
2992 buffer_init(&vs
->tight
.zlib
, "vnc-tight-zlib/%p", sioc
);
2993 buffer_init(&vs
->tight
.gradient
, "vnc-tight-gradient/%p", sioc
);
2994 #ifdef CONFIG_VNC_JPEG
2995 buffer_init(&vs
->tight
.jpeg
, "vnc-tight-jpeg/%p", sioc
);
2997 #ifdef CONFIG_VNC_PNG
2998 buffer_init(&vs
->tight
.png
, "vnc-tight-png/%p", sioc
);
3000 buffer_init(&vs
->zlib
.zlib
, "vnc-zlib/%p", sioc
);
3001 buffer_init(&vs
->zrle
.zrle
, "vnc-zrle/%p", sioc
);
3002 buffer_init(&vs
->zrle
.fb
, "vnc-zrle-fb/%p", sioc
);
3003 buffer_init(&vs
->zrle
.zlib
, "vnc-zrle-zlib/%p", sioc
);
3006 vs
->auth
= VNC_AUTH_NONE
;
3007 vs
->subauth
= VNC_AUTH_INVALID
;
3010 vs
->auth
= vd
->ws_auth
;
3011 vs
->subauth
= VNC_AUTH_INVALID
;
3013 vs
->auth
= vd
->auth
;
3014 vs
->subauth
= vd
->subauth
;
3017 VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
3018 sioc
, websocket
, vs
->auth
, vs
->subauth
);
3020 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
3021 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
3022 vs
->lossy_rect
[i
] = g_new0(uint8_t, VNC_STAT_COLS
);
3025 VNC_DEBUG("New client on socket %p\n", vs
->sioc
);
3026 update_displaychangelistener(&vd
->dcl
, VNC_REFRESH_INTERVAL_BASE
);
3027 qio_channel_set_blocking(vs
->ioc
, false, NULL
);
3031 vs
->ioc_tag
= qio_channel_add_watch(
3032 vs
->ioc
, G_IO_IN
, vncws_tls_handshake_io
, vs
, NULL
);
3034 vs
->ioc_tag
= qio_channel_add_watch(
3035 vs
->ioc
, G_IO_IN
, vncws_handshake_io
, vs
, NULL
);
3038 vs
->ioc_tag
= qio_channel_add_watch(
3039 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
3042 vnc_client_cache_addr(vs
);
3043 vnc_qmp_event(vs
, QAPI_EVENT_VNC_CONNECTED
);
3044 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
3049 vs
->as
.freq
= 44100;
3050 vs
->as
.nchannels
= 2;
3051 vs
->as
.fmt
= AUD_FMT_S16
;
3052 vs
->as
.endianness
= 0;
3054 qemu_mutex_init(&vs
->output_mutex
);
3055 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
3057 QTAILQ_INSERT_TAIL(&vd
->clients
, vs
, next
);
3059 vnc_update_server_surface(vd
);
3062 graphic_hw_update(vd
->dcl
.con
);
3064 if (!vs
->websocket
) {
3065 vnc_start_protocol(vs
);
3068 if (vd
->num_connecting
> vd
->connections_limit
) {
3069 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
3070 if (vs
->share_mode
== VNC_SHARE_MODE_CONNECTING
) {
3071 vnc_disconnect_start(vs
);
3078 void vnc_start_protocol(VncState
*vs
)
3080 vnc_write(vs
, "RFB 003.008\n", 12);
3082 vnc_read_when(vs
, protocol_version
, 12);
3084 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
3085 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
3088 static gboolean
vnc_listen_io(QIOChannel
*ioc
,
3089 GIOCondition condition
,
3092 VncDisplay
*vd
= opaque
;
3093 QIOChannelSocket
*sioc
= NULL
;
3095 bool isWebsock
= false;
3098 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
3099 if (ioc
== QIO_CHANNEL(vd
->lwebsock
[i
])) {
3105 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
), &err
);
3107 qio_channel_set_name(QIO_CHANNEL(sioc
),
3108 isWebsock
? "vnc-ws-server" : "vnc-server");
3109 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
3110 vnc_connect(vd
, sioc
, false, isWebsock
);
3111 object_unref(OBJECT(sioc
));
3113 /* client probably closed connection before we got there */
3120 static const DisplayChangeListenerOps dcl_ops
= {
3122 .dpy_refresh
= vnc_refresh
,
3123 .dpy_gfx_copy
= vnc_dpy_copy
,
3124 .dpy_gfx_update
= vnc_dpy_update
,
3125 .dpy_gfx_switch
= vnc_dpy_switch
,
3126 .dpy_gfx_check_format
= qemu_pixman_check_format
,
3127 .dpy_mouse_set
= vnc_mouse_set
,
3128 .dpy_cursor_define
= vnc_dpy_cursor_define
,
3131 void vnc_display_init(const char *id
)
3135 if (vnc_display_find(id
) != NULL
) {
3138 vd
= g_malloc0(sizeof(*vd
));
3140 vd
->id
= strdup(id
);
3141 QTAILQ_INSERT_TAIL(&vnc_displays
, vd
, next
);
3143 QTAILQ_INIT(&vd
->clients
);
3144 vd
->expires
= TIME_MAX
;
3146 if (keyboard_layout
) {
3147 trace_vnc_key_map_init(keyboard_layout
);
3148 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
3150 vd
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
3153 if (!vd
->kbd_layout
) {
3157 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3158 vd
->connections_limit
= 32;
3160 qemu_mutex_init(&vd
->mutex
);
3161 vnc_start_worker_thread();
3163 vd
->dcl
.ops
= &dcl_ops
;
3164 register_displaychangelistener(&vd
->dcl
);
3168 static void vnc_display_close(VncDisplay
*vd
)
3174 vd
->is_unix
= false;
3175 for (i
= 0; i
< vd
->nlsock
; i
++) {
3176 if (vd
->lsock_tag
[i
]) {
3177 g_source_remove(vd
->lsock_tag
[i
]);
3179 object_unref(OBJECT(vd
->lsock
[i
]));
3182 g_free(vd
->lsock_tag
);
3184 vd
->lsock_tag
= NULL
;
3187 for (i
= 0; i
< vd
->nlwebsock
; i
++) {
3188 if (vd
->lwebsock_tag
[i
]) {
3189 g_source_remove(vd
->lwebsock_tag
[i
]);
3191 object_unref(OBJECT(vd
->lwebsock
[i
]));
3193 g_free(vd
->lwebsock
);
3194 g_free(vd
->lwebsock_tag
);
3195 vd
->lwebsock
= NULL
;
3196 vd
->lwebsock_tag
= NULL
;
3199 vd
->auth
= VNC_AUTH_INVALID
;
3200 vd
->subauth
= VNC_AUTH_INVALID
;
3202 object_unparent(OBJECT(vd
->tlscreds
));
3203 vd
->tlscreds
= NULL
;
3205 g_free(vd
->tlsaclname
);
3206 vd
->tlsaclname
= NULL
;
3207 if (vd
->lock_key_sync
) {
3208 qemu_remove_led_event_handler(vd
->led
);
3213 int vnc_display_password(const char *id
, const char *password
)
3215 VncDisplay
*vd
= vnc_display_find(id
);
3220 if (vd
->auth
== VNC_AUTH_NONE
) {
3221 error_printf_unless_qmp("If you want use passwords please enable "
3222 "password auth using '-vnc ${dpy},password'.\n");
3226 g_free(vd
->password
);
3227 vd
->password
= g_strdup(password
);
3232 int vnc_display_pw_expire(const char *id
, time_t expires
)
3234 VncDisplay
*vd
= vnc_display_find(id
);
3240 vd
->expires
= expires
;
3244 static void vnc_display_print_local_addr(VncDisplay
*vd
)
3246 SocketAddress
*addr
;
3253 addr
= qio_channel_socket_get_local_address(vd
->lsock
[0], &err
);
3258 if (addr
->type
!= SOCKET_ADDRESS_KIND_INET
) {
3259 qapi_free_SocketAddress(addr
);
3262 error_printf_unless_qmp("VNC server running on %s:%s\n",
3263 addr
->u
.inet
.data
->host
,
3264 addr
->u
.inet
.data
->port
);
3265 qapi_free_SocketAddress(addr
);
3268 static QemuOptsList qemu_vnc_opts
= {
3270 .head
= QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts
.head
),
3271 .implied_opt_name
= "vnc",
3275 .type
= QEMU_OPT_STRING
,
3277 .name
= "websocket",
3278 .type
= QEMU_OPT_STRING
,
3280 .name
= "tls-creds",
3281 .type
= QEMU_OPT_STRING
,
3283 /* Deprecated in favour of tls-creds */
3285 .type
= QEMU_OPT_STRING
,
3288 .type
= QEMU_OPT_STRING
,
3291 .type
= QEMU_OPT_STRING
,
3294 .type
= QEMU_OPT_NUMBER
,
3296 .name
= "connections",
3297 .type
= QEMU_OPT_NUMBER
,
3300 .type
= QEMU_OPT_NUMBER
,
3303 .type
= QEMU_OPT_BOOL
,
3306 .type
= QEMU_OPT_BOOL
,
3309 .type
= QEMU_OPT_BOOL
,
3312 .type
= QEMU_OPT_BOOL
,
3314 .name
= "lock-key-sync",
3315 .type
= QEMU_OPT_BOOL
,
3317 .name
= "key-delay-ms",
3318 .type
= QEMU_OPT_NUMBER
,
3321 .type
= QEMU_OPT_BOOL
,
3323 /* Deprecated in favour of tls-creds */
3325 .type
= QEMU_OPT_BOOL
,
3327 /* Deprecated in favour of tls-creds */
3328 .name
= "x509verify",
3329 .type
= QEMU_OPT_STRING
,
3332 .type
= QEMU_OPT_BOOL
,
3335 .type
= QEMU_OPT_BOOL
,
3337 .name
= "non-adaptive",
3338 .type
= QEMU_OPT_BOOL
,
3340 { /* end of list */ }
3346 vnc_display_setup_auth(int *auth
,
3348 QCryptoTLSCreds
*tlscreds
,
3355 * We have a choice of 3 authentication options
3361 * The channel can be run in 2 modes
3366 * And TLS can use 2 types of credentials
3371 * We thus have 9 possible logical combinations
3376 * 4. tls + anon + none
3377 * 5. tls + anon + vnc
3378 * 6. tls + anon + sasl
3379 * 7. tls + x509 + none
3380 * 8. tls + x509 + vnc
3381 * 9. tls + x509 + sasl
3383 * These need to be mapped into the VNC auth schemes
3384 * in an appropriate manner. In regular VNC, all the
3385 * TLS options get mapped into VNC_AUTH_VENCRYPT
3388 * In websockets, the https:// protocol already provides
3389 * TLS support, so there is no need to make use of the
3390 * VeNCrypt extension. Furthermore, websockets browser
3391 * clients could not use VeNCrypt even if they wanted to,
3392 * as they cannot control when the TLS handshake takes
3393 * place. Thus there is no option but to rely on https://,
3394 * meaning combinations 4->6 and 7->9 will be mapped to
3395 * VNC auth schemes in the same way as combos 1->3.
3397 * Regardless of fact that we have a different mapping to
3398 * VNC auth mechs for plain VNC vs websockets VNC, the end
3399 * result has the same security characteristics.
3401 if (websocket
|| !tlscreds
) {
3403 VNC_DEBUG("Initializing VNC server with password auth\n");
3404 *auth
= VNC_AUTH_VNC
;
3406 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3407 *auth
= VNC_AUTH_SASL
;
3409 VNC_DEBUG("Initializing VNC server with no auth\n");
3410 *auth
= VNC_AUTH_NONE
;
3412 *subauth
= VNC_AUTH_INVALID
;
3414 bool is_x509
= object_dynamic_cast(OBJECT(tlscreds
),
3415 TYPE_QCRYPTO_TLS_CREDS_X509
) != NULL
;
3416 bool is_anon
= object_dynamic_cast(OBJECT(tlscreds
),
3417 TYPE_QCRYPTO_TLS_CREDS_ANON
) != NULL
;
3419 if (!is_x509
&& !is_anon
) {
3421 "Unsupported TLS cred type %s",
3422 object_get_typename(OBJECT(tlscreds
)));
3425 *auth
= VNC_AUTH_VENCRYPT
;
3428 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3429 *subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3431 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3432 *subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3437 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3438 *subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3440 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3441 *subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3445 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3446 *subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3448 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3449 *subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3458 * Handle back compat with old CLI syntax by creating some
3459 * suitable QCryptoTLSCreds objects
3461 static QCryptoTLSCreds
*
3462 vnc_display_create_creds(bool x509
,
3468 gchar
*credsid
= g_strdup_printf("tlsvnc%s", id
);
3469 Object
*parent
= object_get_objects_root();
3474 creds
= object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509
,
3478 "endpoint", "server",
3480 "verify-peer", x509verify
? "yes" : "no",
3483 creds
= object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON
,
3487 "endpoint", "server",
3494 error_propagate(errp
, err
);
3498 return QCRYPTO_TLS_CREDS(creds
);
3502 static int vnc_display_get_address(const char *addrstr
,
3510 SocketAddress
**retaddr
,
3514 SocketAddress
*addr
= NULL
;
3516 addr
= g_new0(SocketAddress
, 1);
3518 if (strncmp(addrstr
, "unix:", 5) == 0) {
3519 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
3520 addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
3521 addr
->u
.q_unix
.data
->path
= g_strdup(addrstr
+ 5);
3524 error_setg(errp
, "UNIX sockets not supported with websock");
3529 error_setg(errp
, "Port range not support with UNIX socket");
3536 unsigned long long baseport
= 0;
3537 InetSocketAddress
*inet
;
3539 port
= strrchr(addrstr
, ':');
3545 error_setg(errp
, "no vnc port specified");
3549 hostlen
= port
- addrstr
;
3551 if (*port
== '\0') {
3552 error_setg(errp
, "vnc port cannot be empty");
3557 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3558 inet
= addr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
3559 if (addrstr
[0] == '[' && addrstr
[hostlen
- 1] == ']') {
3560 inet
->host
= g_strndup(addrstr
+ 1, hostlen
- 2);
3562 inet
->host
= g_strndup(addrstr
, hostlen
);
3564 /* plain VNC port is just an offset, for websocket
3565 * port is absolute */
3567 if (g_str_equal(addrstr
, "") ||
3568 g_str_equal(addrstr
, "on")) {
3569 if (displaynum
== -1) {
3570 error_setg(errp
, "explicit websocket port is required");
3573 inet
->port
= g_strdup_printf(
3574 "%d", displaynum
+ 5700);
3576 inet
->has_to
= true;
3577 inet
->to
= to
+ 5700;
3580 inet
->port
= g_strdup(port
);
3583 if (parse_uint_full(port
, &baseport
, 10) < 0) {
3584 error_setg(errp
, "can't convert to a number: %s", port
);
3587 if (baseport
> 65535 ||
3588 baseport
+ 5900 > 65535) {
3589 error_setg(errp
, "port %s out of range", port
);
3592 inet
->port
= g_strdup_printf(
3593 "%d", (int)baseport
+ 5900);
3596 inet
->has_to
= true;
3597 inet
->to
= to
+ 5900;
3602 inet
->has_ipv4
= has_ipv4
;
3604 inet
->has_ipv6
= has_ipv6
;
3613 qapi_free_SocketAddress(addr
);
3618 static int vnc_display_get_addresses(QemuOpts
*opts
,
3619 SocketAddress
***retsaddr
,
3621 SocketAddress
***retwsaddr
,
3625 SocketAddress
*saddr
= NULL
;
3626 SocketAddress
*wsaddr
= NULL
;
3627 QemuOptsIter addriter
;
3629 int to
= qemu_opt_get_number(opts
, "to", 0);
3630 bool has_ipv4
= qemu_opt_get(opts
, "ipv4");
3631 bool has_ipv6
= qemu_opt_get(opts
, "ipv6");
3632 bool ipv4
= qemu_opt_get_bool(opts
, "ipv4", false);
3633 bool ipv6
= qemu_opt_get_bool(opts
, "ipv6", false);
3635 int displaynum
= -1;
3643 addr
= qemu_opt_get(opts
, "vnc");
3644 if (addr
== NULL
|| g_str_equal(addr
, "none")) {
3648 if (qemu_opt_get(opts
, "websocket") &&
3649 !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1
)) {
3651 "SHA1 hash support is required for websockets");
3655 qemu_opt_iter_init(&addriter
, opts
, "vnc");
3656 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3658 rv
= vnc_display_get_address(addr
, false, 0, to
,
3665 /* Historical compat - first listen address can be used
3666 * to set the default websocket port
3668 if (displaynum
== -1) {
3671 *retsaddr
= g_renew(SocketAddress
*, *retsaddr
, *retnsaddr
+ 1);
3672 (*retsaddr
)[(*retnsaddr
)++] = saddr
;
3675 /* If we had multiple primary displays, we don't do defaults
3676 * for websocket, and require explicit config instead. */
3677 if (*retnsaddr
> 1) {
3681 qemu_opt_iter_init(&addriter
, opts
, "websocket");
3682 while ((addr
= qemu_opt_iter_next(&addriter
)) != NULL
) {
3683 if (vnc_display_get_address(addr
, true, displaynum
, to
,
3686 &wsaddr
, errp
) < 0) {
3690 /* Historical compat - if only a single listen address was
3691 * provided, then this is used to set the default listen
3692 * address for websocket too
3694 if (*retnsaddr
== 1 &&
3695 (*retsaddr
)[0]->type
== SOCKET_ADDRESS_KIND_INET
&&
3696 wsaddr
->type
== SOCKET_ADDRESS_KIND_INET
&&
3697 g_str_equal(wsaddr
->u
.inet
.data
->host
, "") &&
3698 !g_str_equal((*retsaddr
)[0]->u
.inet
.data
->host
, "")) {
3699 g_free(wsaddr
->u
.inet
.data
->host
);
3700 wsaddr
->u
.inet
.data
->host
=
3701 g_strdup((*retsaddr
)[0]->u
.inet
.data
->host
);
3704 *retwsaddr
= g_renew(SocketAddress
*, *retwsaddr
, *retnwsaddr
+ 1);
3705 (*retwsaddr
)[(*retnwsaddr
)++] = wsaddr
;
3711 for (i
= 0; i
< *retnsaddr
; i
++) {
3712 qapi_free_SocketAddress((*retsaddr
)[i
]);
3715 for (i
= 0; i
< *retnwsaddr
; i
++) {
3716 qapi_free_SocketAddress((*retwsaddr
)[i
]);
3719 *retsaddr
= *retwsaddr
= NULL
;
3720 *retnsaddr
= *retnwsaddr
= 0;
3725 static int vnc_display_connect(VncDisplay
*vd
,
3726 SocketAddress
**saddr
,
3728 SocketAddress
**wsaddr
,
3732 /* connect to viewer */
3733 QIOChannelSocket
*sioc
= NULL
;
3735 error_setg(errp
, "Cannot use websockets in reverse mode");
3739 error_setg(errp
, "Expected a single address in reverse mode");
3742 vd
->is_unix
= saddr
[0]->type
== SOCKET_ADDRESS_KIND_UNIX
;
3743 sioc
= qio_channel_socket_new();
3744 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-reverse");
3745 if (qio_channel_socket_connect_sync(sioc
, saddr
[0], errp
) < 0) {
3748 vnc_connect(vd
, sioc
, false, false);
3749 object_unref(OBJECT(sioc
));
3754 static int vnc_display_listen_addr(VncDisplay
*vd
,
3755 SocketAddress
*addr
,
3757 QIOChannelSocket
***lsock
,
3762 QIODNSResolver
*resolver
= qio_dns_resolver_get_instance();
3763 SocketAddress
**rawaddrs
= NULL
;
3764 size_t nrawaddrs
= 0;
3765 Error
*listenerr
= NULL
;
3766 bool listening
= false;
3769 if (qio_dns_resolver_lookup_sync(resolver
, addr
, &nrawaddrs
,
3770 &rawaddrs
, errp
) < 0) {
3774 for (i
= 0; i
< nrawaddrs
; i
++) {
3775 QIOChannelSocket
*sioc
= qio_channel_socket_new();
3777 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
3778 if (qio_channel_socket_listen_sync(
3779 sioc
, rawaddrs
[i
], listenerr
== NULL
? &listenerr
: NULL
) < 0) {
3784 *lsock
= g_renew(QIOChannelSocket
*, *lsock
, *nlsock
);
3785 *lsock_tag
= g_renew(guint
, *lsock_tag
, *nlsock
);
3787 (*lsock
)[*nlsock
- 1] = sioc
;
3788 (*lsock_tag
)[*nlsock
- 1] = 0;
3791 for (i
= 0; i
< nrawaddrs
; i
++) {
3792 qapi_free_SocketAddress(rawaddrs
[i
]);
3798 error_propagate(errp
, listenerr
);
3801 error_free(listenerr
);
3805 for (i
= 0; i
< *nlsock
; i
++) {
3806 (*lsock_tag
)[i
] = qio_channel_add_watch(
3807 QIO_CHANNEL((*lsock
)[i
]),
3808 G_IO_IN
, vnc_listen_io
, vd
, NULL
);
3815 static int vnc_display_listen(VncDisplay
*vd
,
3816 SocketAddress
**saddr
,
3818 SocketAddress
**wsaddr
,
3824 for (i
= 0; i
< nsaddr
; i
++) {
3825 if (vnc_display_listen_addr(vd
, saddr
[i
],
3834 for (i
= 0; i
< nwsaddr
; i
++) {
3835 if (vnc_display_listen_addr(vd
, wsaddr
[i
],
3849 void vnc_display_open(const char *id
, Error
**errp
)
3851 VncDisplay
*vd
= vnc_display_find(id
);
3852 QemuOpts
*opts
= qemu_opts_find(&qemu_vnc_opts
, id
);
3853 SocketAddress
**saddr
= NULL
, **wsaddr
= NULL
;
3854 size_t nsaddr
, nwsaddr
;
3855 const char *share
, *device_id
;
3857 bool password
= false;
3858 bool reverse
= false;
3861 #ifdef CONFIG_VNC_SASL
3865 int lock_key_sync
= 1;
3870 error_setg(errp
, "VNC display not active");
3873 vnc_display_close(vd
);
3879 if (vnc_display_get_addresses(opts
, &saddr
, &nsaddr
,
3880 &wsaddr
, &nwsaddr
, errp
) < 0) {
3884 if (saddr
== NULL
) {
3888 password
= qemu_opt_get_bool(opts
, "password", false);
3890 if (fips_get_state()) {
3892 "VNC password auth disabled due to FIPS mode, "
3893 "consider using the VeNCrypt or SASL authentication "
3894 "methods as an alternative");
3897 if (!qcrypto_cipher_supports(
3898 QCRYPTO_CIPHER_ALG_DES_RFB
, QCRYPTO_CIPHER_MODE_ECB
)) {
3900 "Cipher backend does not support DES RFB algorithm");
3905 reverse
= qemu_opt_get_bool(opts
, "reverse", false);
3906 lock_key_sync
= qemu_opt_get_bool(opts
, "lock-key-sync", true);
3907 key_delay_ms
= qemu_opt_get_number(opts
, "key-delay-ms", 1);
3908 sasl
= qemu_opt_get_bool(opts
, "sasl", false);
3909 #ifndef CONFIG_VNC_SASL
3911 error_setg(errp
, "VNC SASL auth requires cyrus-sasl support");
3914 #endif /* CONFIG_VNC_SASL */
3915 credid
= qemu_opt_get(opts
, "tls-creds");
3918 if (qemu_opt_get(opts
, "tls") ||
3919 qemu_opt_get(opts
, "x509") ||
3920 qemu_opt_get(opts
, "x509verify")) {
3922 "'tls-creds' parameter is mutually exclusive with "
3923 "'tls', 'x509' and 'x509verify' parameters");
3927 creds
= object_resolve_path_component(
3928 object_get_objects_root(), credid
);
3930 error_setg(errp
, "No TLS credentials with id '%s'",
3934 vd
->tlscreds
= (QCryptoTLSCreds
*)
3935 object_dynamic_cast(creds
,
3936 TYPE_QCRYPTO_TLS_CREDS
);
3937 if (!vd
->tlscreds
) {
3938 error_setg(errp
, "Object with id '%s' is not TLS credentials",
3942 object_ref(OBJECT(vd
->tlscreds
));
3944 if (vd
->tlscreds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
3946 "Expecting TLS credentials with a server endpoint");
3951 bool tls
= false, x509
= false, x509verify
= false;
3952 tls
= qemu_opt_get_bool(opts
, "tls", false);
3954 path
= qemu_opt_get(opts
, "x509");
3959 path
= qemu_opt_get(opts
, "x509verify");
3965 vd
->tlscreds
= vnc_display_create_creds(x509
,
3970 if (!vd
->tlscreds
) {
3975 acl
= qemu_opt_get_bool(opts
, "acl", false);
3977 share
= qemu_opt_get(opts
, "share");
3979 if (strcmp(share
, "ignore") == 0) {
3980 vd
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
3981 } else if (strcmp(share
, "allow-exclusive") == 0) {
3982 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3983 } else if (strcmp(share
, "force-shared") == 0) {
3984 vd
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
3986 error_setg(errp
, "unknown vnc share= option");
3990 vd
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3992 vd
->connections_limit
= qemu_opt_get_number(opts
, "connections", 32);
3994 #ifdef CONFIG_VNC_JPEG
3995 vd
->lossy
= qemu_opt_get_bool(opts
, "lossy", false);
3997 vd
->non_adaptive
= qemu_opt_get_bool(opts
, "non-adaptive", false);
3998 /* adaptive updates are only used with tight encoding and
3999 * if lossy updates are enabled so we can disable all the
4000 * calculations otherwise */
4002 vd
->non_adaptive
= true;
4006 if (strcmp(vd
->id
, "default") == 0) {
4007 vd
->tlsaclname
= g_strdup("vnc.x509dname");
4009 vd
->tlsaclname
= g_strdup_printf("vnc.%s.x509dname", vd
->id
);
4011 qemu_acl_init(vd
->tlsaclname
);
4013 #ifdef CONFIG_VNC_SASL
4017 if (strcmp(vd
->id
, "default") == 0) {
4018 aclname
= g_strdup("vnc.username");
4020 aclname
= g_strdup_printf("vnc.%s.username", vd
->id
);
4022 vd
->sasl
.acl
= qemu_acl_init(aclname
);
4027 if (vnc_display_setup_auth(&vd
->auth
, &vd
->subauth
,
4028 vd
->tlscreds
, password
,
4029 sasl
, false, errp
) < 0) {
4033 if (vnc_display_setup_auth(&vd
->ws_auth
, &vd
->ws_subauth
,
4034 vd
->tlscreds
, password
,
4035 sasl
, true, errp
) < 0) {
4039 #ifdef CONFIG_VNC_SASL
4040 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
4041 error_setg(errp
, "Failed to initialize SASL auth: %s",
4042 sasl_errstring(saslErr
, NULL
, NULL
));
4046 vd
->lock_key_sync
= lock_key_sync
;
4047 if (lock_key_sync
) {
4048 vd
->led
= qemu_add_led_event_handler(kbd_leds
, vd
);
4051 vd
->key_delay_ms
= key_delay_ms
;
4053 device_id
= qemu_opt_get(opts
, "display");
4055 int head
= qemu_opt_get_number(opts
, "head", 0);
4058 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
4060 error_propagate(errp
, err
);
4067 if (con
!= vd
->dcl
.con
) {
4068 unregister_displaychangelistener(&vd
->dcl
);
4070 register_displaychangelistener(&vd
->dcl
);
4074 if (vnc_display_connect(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4078 if (vnc_display_listen(vd
, saddr
, nsaddr
, wsaddr
, nwsaddr
, errp
) < 0) {
4083 if (qemu_opt_get(opts
, "to")) {
4084 vnc_display_print_local_addr(vd
);
4088 for (i
= 0; i
< nsaddr
; i
++) {
4089 qapi_free_SocketAddress(saddr
[i
]);
4091 for (i
= 0; i
< nwsaddr
; i
++) {
4092 qapi_free_SocketAddress(wsaddr
[i
]);
4097 vnc_display_close(vd
);
4101 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
)
4103 VncDisplay
*vd
= vnc_display_find(id
);
4104 QIOChannelSocket
*sioc
;
4110 sioc
= qio_channel_socket_new_fd(csock
, NULL
);
4112 qio_channel_set_name(QIO_CHANNEL(sioc
), "vnc-server");
4113 vnc_connect(vd
, sioc
, skipauth
, false);
4114 object_unref(OBJECT(sioc
));
4118 static void vnc_auto_assign_id(QemuOptsList
*olist
, QemuOpts
*opts
)
4123 id
= g_strdup("default");
4124 while (qemu_opts_find(olist
, id
)) {
4126 id
= g_strdup_printf("vnc%d", i
++);
4128 qemu_opts_set_id(opts
, id
);
4131 QemuOpts
*vnc_parse(const char *str
, Error
**errp
)
4133 QemuOptsList
*olist
= qemu_find_opts("vnc");
4134 QemuOpts
*opts
= qemu_opts_parse(olist
, str
, true, errp
);
4141 id
= qemu_opts_id(opts
);
4143 /* auto-assign id if not present */
4144 vnc_auto_assign_id(olist
, opts
);
4149 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
4151 Error
*local_err
= NULL
;
4152 char *id
= (char *)qemu_opts_id(opts
);
4155 vnc_display_init(id
);
4156 vnc_display_open(id
, &local_err
);
4157 if (local_err
!= NULL
) {
4158 error_reportf_err(local_err
, "Failed to start VNC server: ");
4164 static void vnc_register_config(void)
4166 qemu_add_opts(&qemu_vnc_opts
);
4168 opts_init(vnc_register_config
);