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
29 #include "sysemu/sysemu.h"
30 #include "qemu/sockets.h"
31 #include "qemu/timer.h"
33 #include "qapi/qmp/types.h"
34 #include "qmp-commands.h"
35 #include "qemu/osdep.h"
37 #define VNC_REFRESH_INTERVAL_BASE 30
38 #define VNC_REFRESH_INTERVAL_INC 50
39 #define VNC_REFRESH_INTERVAL_MAX 2000
40 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
41 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
43 #include "vnc_keysym.h"
46 static VncDisplay
*vnc_display
; /* needed for info vnc */
47 static DisplayChangeListener
*dcl
;
49 static int vnc_cursor_define(VncState
*vs
);
50 static void vnc_release_modifiers(VncState
*vs
);
52 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
55 static const char *mn
[] = {
57 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
58 [VNC_SHARE_MODE_SHARED
] = "shared",
59 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
60 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
62 fprintf(stderr
, "%s/%d: %s -> %s\n", __func__
,
63 vs
->csock
, mn
[vs
->share_mode
], mn
[mode
]);
66 if (vs
->share_mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
67 vs
->vd
->num_exclusive
--;
69 vs
->share_mode
= mode
;
70 if (vs
->share_mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
71 vs
->vd
->num_exclusive
++;
75 static char *addr_to_string(const char *format
,
76 struct sockaddr_storage
*sa
,
79 char host
[NI_MAXHOST
];
80 char serv
[NI_MAXSERV
];
84 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
87 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
88 VNC_DEBUG("Cannot resolve address %d: %s\n",
89 err
, gai_strerror(err
));
93 /* Enough for the existing format + the 2 vars we're
95 addrlen
= strlen(format
) + strlen(host
) + strlen(serv
);
96 addr
= g_malloc(addrlen
+ 1);
97 snprintf(addr
, addrlen
, format
, host
, serv
);
104 char *vnc_socket_local_addr(const char *format
, int fd
) {
105 struct sockaddr_storage sa
;
109 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
112 return addr_to_string(format
, &sa
, salen
);
115 char *vnc_socket_remote_addr(const char *format
, int fd
) {
116 struct sockaddr_storage sa
;
120 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
123 return addr_to_string(format
, &sa
, salen
);
126 static int put_addr_qdict(QDict
*qdict
, struct sockaddr_storage
*sa
,
129 char host
[NI_MAXHOST
];
130 char serv
[NI_MAXSERV
];
133 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
136 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
137 VNC_DEBUG("Cannot resolve address %d: %s\n",
138 err
, gai_strerror(err
));
142 qdict_put(qdict
, "host", qstring_from_str(host
));
143 qdict_put(qdict
, "service", qstring_from_str(serv
));
144 qdict_put(qdict
, "family",qstring_from_str(inet_strfamily(sa
->ss_family
)));
149 static int vnc_server_addr_put(QDict
*qdict
, int fd
)
151 struct sockaddr_storage sa
;
155 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
159 return put_addr_qdict(qdict
, &sa
, salen
);
162 static int vnc_qdict_remote_addr(QDict
*qdict
, int fd
)
164 struct sockaddr_storage sa
;
168 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
172 return put_addr_qdict(qdict
, &sa
, salen
);
175 static const char *vnc_auth_name(VncDisplay
*vd
) {
177 case VNC_AUTH_INVALID
:
193 case VNC_AUTH_VENCRYPT
:
194 #ifdef CONFIG_VNC_TLS
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";
226 static int vnc_server_info_put(QDict
*qdict
)
228 if (vnc_server_addr_put(qdict
, vnc_display
->lsock
) < 0) {
232 qdict_put(qdict
, "auth", qstring_from_str(vnc_auth_name(vnc_display
)));
236 static void vnc_client_cache_auth(VncState
*client
)
238 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
246 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
247 qdict
= qobject_to_qdict(client
->info
);
250 #ifdef CONFIG_VNC_TLS
251 if (client
->tls
.session
&&
253 qdict_put(qdict
, "x509_dname", qstring_from_str(client
->tls
.dname
));
256 #ifdef CONFIG_VNC_SASL
257 if (client
->sasl
.conn
&&
258 client
->sasl
.username
) {
259 qdict_put(qdict
, "sasl_username",
260 qstring_from_str(client
->sasl
.username
));
265 static void vnc_client_cache_addr(VncState
*client
)
270 if (vnc_qdict_remote_addr(qdict
, client
->csock
) < 0) {
272 /* XXX: how to report the error? */
276 client
->info
= QOBJECT(qdict
);
279 static void vnc_qmp_event(VncState
*vs
, MonitorEvent event
)
288 server
= qdict_new();
289 if (vnc_server_info_put(server
) < 0) {
294 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
295 vs
->info
, QOBJECT(server
));
297 monitor_protocol_event(event
, data
);
299 qobject_incref(vs
->info
);
300 qobject_decref(data
);
303 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
305 struct sockaddr_storage sa
;
306 socklen_t salen
= sizeof(sa
);
307 char host
[NI_MAXHOST
];
308 char serv
[NI_MAXSERV
];
311 if (getpeername(client
->csock
, (struct sockaddr
*)&sa
, &salen
) < 0) {
315 if (getnameinfo((struct sockaddr
*)&sa
, salen
,
318 NI_NUMERICHOST
| NI_NUMERICSERV
) < 0) {
322 info
= g_malloc0(sizeof(*info
));
323 info
->host
= g_strdup(host
);
324 info
->service
= g_strdup(serv
);
325 info
->family
= g_strdup(inet_strfamily(sa
.ss_family
));
327 #ifdef CONFIG_VNC_TLS
328 if (client
->tls
.session
&& client
->tls
.dname
) {
329 info
->has_x509_dname
= true;
330 info
->x509_dname
= g_strdup(client
->tls
.dname
);
333 #ifdef CONFIG_VNC_SASL
334 if (client
->sasl
.conn
&& client
->sasl
.username
) {
335 info
->has_sasl_username
= true;
336 info
->sasl_username
= g_strdup(client
->sasl
.username
);
343 VncInfo
*qmp_query_vnc(Error
**errp
)
345 VncInfo
*info
= g_malloc0(sizeof(*info
));
347 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
348 info
->enabled
= false;
350 VncClientInfoList
*cur_item
= NULL
;
351 struct sockaddr_storage sa
;
352 socklen_t salen
= sizeof(sa
);
353 char host
[NI_MAXHOST
];
354 char serv
[NI_MAXSERV
];
357 info
->enabled
= true;
359 /* for compatibility with the original command */
360 info
->has_clients
= true;
362 QTAILQ_FOREACH(client
, &vnc_display
->clients
, next
) {
363 VncClientInfoList
*cinfo
= g_malloc0(sizeof(*info
));
364 cinfo
->value
= qmp_query_vnc_client(client
);
366 /* XXX: waiting for the qapi to support GSList */
368 info
->clients
= cur_item
= cinfo
;
370 cur_item
->next
= cinfo
;
375 if (vnc_display
->lsock
== -1) {
379 if (getsockname(vnc_display
->lsock
, (struct sockaddr
*)&sa
,
381 error_set(errp
, QERR_UNDEFINED_ERROR
);
385 if (getnameinfo((struct sockaddr
*)&sa
, salen
,
388 NI_NUMERICHOST
| NI_NUMERICSERV
) < 0) {
389 error_set(errp
, QERR_UNDEFINED_ERROR
);
393 info
->has_host
= true;
394 info
->host
= g_strdup(host
);
396 info
->has_service
= true;
397 info
->service
= g_strdup(serv
);
399 info
->has_family
= true;
400 info
->family
= g_strdup(inet_strfamily(sa
.ss_family
));
402 info
->has_auth
= true;
403 info
->auth
= g_strdup(vnc_auth_name(vnc_display
));
409 qapi_free_VncInfo(info
);
414 1) Get the queue working for IO.
415 2) there is some weirdness when using the -S option (the screen is grey
416 and not totally invalidated
417 3) resolutions > 1024
420 static int vnc_update_client(VncState
*vs
, int has_dirty
);
421 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
);
422 static void vnc_disconnect_start(VncState
*vs
);
423 static void vnc_init_timer(VncDisplay
*vd
);
424 static void vnc_remove_timer(VncDisplay
*vd
);
426 static void vnc_colordepth(VncState
*vs
);
427 static void framebuffer_update_request(VncState
*vs
, int incremental
,
428 int x_position
, int y_position
,
430 static void vnc_refresh(void *opaque
);
431 static int vnc_refresh_server_surface(VncDisplay
*vd
);
433 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
436 VncDisplay
*vd
= ds
->opaque
;
437 struct VncSurface
*s
= &vd
->guest
;
438 int width
= ds_get_width(ds
);
439 int height
= ds_get_height(ds
);
443 /* round x down to ensure the loop only spans one 16-pixel block per,
444 iteration. otherwise, if (x % 16) != 0, the last iteration may span
445 two 16-pixel blocks but we only mark the first as dirty
452 w
= MIN(x
+ w
, width
) - x
;
456 for (i
= 0; i
< w
; i
+= 16)
457 set_bit((x
+ i
) / 16, s
->dirty
[y
]);
460 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
463 vnc_write_u16(vs
, x
);
464 vnc_write_u16(vs
, y
);
465 vnc_write_u16(vs
, w
);
466 vnc_write_u16(vs
, h
);
468 vnc_write_s32(vs
, encoding
);
471 void buffer_reserve(Buffer
*buffer
, size_t len
)
473 if ((buffer
->capacity
- buffer
->offset
) < len
) {
474 buffer
->capacity
+= (len
+ 1024);
475 buffer
->buffer
= g_realloc(buffer
->buffer
, buffer
->capacity
);
476 if (buffer
->buffer
== NULL
) {
477 fprintf(stderr
, "vnc: out of memory\n");
483 static int buffer_empty(Buffer
*buffer
)
485 return buffer
->offset
== 0;
488 uint8_t *buffer_end(Buffer
*buffer
)
490 return buffer
->buffer
+ buffer
->offset
;
493 void buffer_reset(Buffer
*buffer
)
498 void buffer_free(Buffer
*buffer
)
500 g_free(buffer
->buffer
);
502 buffer
->capacity
= 0;
503 buffer
->buffer
= NULL
;
506 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
508 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
509 buffer
->offset
+= len
;
512 void buffer_advance(Buffer
*buf
, size_t len
)
514 memmove(buf
->buffer
, buf
->buffer
+ len
,
515 (buf
->offset
- len
));
519 static void vnc_desktop_resize(VncState
*vs
)
521 DisplayState
*ds
= vs
->ds
;
523 if (vs
->csock
== -1 || !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
526 if (vs
->client_width
== ds_get_width(ds
) &&
527 vs
->client_height
== ds_get_height(ds
)) {
530 vs
->client_width
= ds_get_width(ds
);
531 vs
->client_height
= ds_get_height(ds
);
533 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
535 vnc_write_u16(vs
, 1); /* number of rects */
536 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
537 VNC_ENCODING_DESKTOPRESIZE
);
538 vnc_unlock_output(vs
);
542 static void vnc_abort_display_jobs(VncDisplay
*vd
)
546 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
549 vnc_unlock_output(vs
);
551 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
554 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
557 vnc_unlock_output(vs
);
561 int vnc_server_fb_stride(VncDisplay
*vd
)
563 return pixman_image_get_stride(vd
->server
);
566 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
570 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
571 ptr
+= y
* vnc_server_fb_stride(vd
);
572 ptr
+= x
* VNC_SERVER_FB_BYTES
;
576 static void vnc_dpy_resize(DisplayState
*ds
)
578 VncDisplay
*vd
= ds
->opaque
;
581 vnc_abort_display_jobs(vd
);
584 qemu_pixman_image_unref(vd
->server
);
585 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
592 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
593 console_color_init(ds
);
595 qemu_pixman_image_unref(vd
->guest
.fb
);
596 vd
->guest
.fb
= pixman_image_ref(ds
->surface
->image
);
597 vd
->guest
.format
= ds
->surface
->format
;
598 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
600 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
602 vnc_desktop_resize(vs
);
603 if (vs
->vd
->cursor
) {
604 vnc_cursor_define(vs
);
606 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
611 static void vnc_write_pixels_copy(VncState
*vs
,
612 void *pixels
, int size
)
614 vnc_write(vs
, pixels
, size
);
617 /* slowest but generic code. */
618 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
622 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
623 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
624 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
625 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
627 # error need some bits here if you change VNC_SERVER_FB_FORMAT
629 v
= (r
<< vs
->client_pf
.rshift
) |
630 (g
<< vs
->client_pf
.gshift
) |
631 (b
<< vs
->client_pf
.bshift
);
632 switch (vs
->client_pf
.bytes_per_pixel
) {
662 static void vnc_write_pixels_generic(VncState
*vs
,
663 void *pixels1
, int size
)
667 if (VNC_SERVER_FB_BYTES
== 4) {
668 uint32_t *pixels
= pixels1
;
671 for (i
= 0; i
< n
; i
++) {
672 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
673 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
678 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
682 VncDisplay
*vd
= vs
->vd
;
684 row
= vnc_server_fb_ptr(vd
, x
, y
);
685 for (i
= 0; i
< h
; i
++) {
686 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
687 row
+= vnc_server_fb_stride(vd
);
692 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
696 switch(vs
->vnc_encoding
) {
697 case VNC_ENCODING_ZLIB
:
698 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
700 case VNC_ENCODING_HEXTILE
:
701 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
702 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
704 case VNC_ENCODING_TIGHT
:
705 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
707 case VNC_ENCODING_TIGHT_PNG
:
708 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
710 case VNC_ENCODING_ZRLE
:
711 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
713 case VNC_ENCODING_ZYWRLE
:
714 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
717 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
718 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
724 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
726 /* send bitblit op to the vnc client */
728 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
730 vnc_write_u16(vs
, 1); /* number of rects */
731 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
732 vnc_write_u16(vs
, src_x
);
733 vnc_write_u16(vs
, src_y
);
734 vnc_unlock_output(vs
);
738 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
740 VncDisplay
*vd
= ds
->opaque
;
744 int i
, x
, y
, pitch
, inc
, w_lim
, s
;
747 vnc_refresh_server_surface(vd
);
748 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
749 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
750 vs
->force_update
= 1;
751 vnc_update_client_sync(vs
, 1);
752 /* vs might be free()ed here */
756 /* do bitblit op on the local surface too */
757 pitch
= vnc_server_fb_stride(vd
);
758 src_row
= vnc_server_fb_ptr(vd
, src_x
, src_y
);
759 dst_row
= vnc_server_fb_ptr(vd
, dst_x
, dst_y
);
764 src_row
+= pitch
* (h
-1);
765 dst_row
+= pitch
* (h
-1);
770 w_lim
= w
- (16 - (dst_x
% 16));
774 w_lim
= w
- (w_lim
% 16);
775 for (i
= 0; i
< h
; i
++) {
776 for (x
= 0; x
<= w_lim
;
777 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
779 if ((s
= w
- w_lim
) == 0)
782 s
= (16 - (dst_x
% 16));
787 cmp_bytes
= s
* VNC_SERVER_FB_BYTES
;
788 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
790 memmove(dst_row
, src_row
, cmp_bytes
);
791 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
792 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
793 set_bit(((x
+ dst_x
) / 16), vs
->dirty
[y
]);
797 src_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
798 dst_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
802 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
803 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
804 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
809 static void vnc_mouse_set(DisplayState
*ds
, int x
, int y
, int visible
)
811 /* can we ask the client(s) to move the pointer ??? */
814 static int vnc_cursor_define(VncState
*vs
)
816 QEMUCursor
*c
= vs
->vd
->cursor
;
819 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
821 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
822 vnc_write_u8(vs
, 0); /* padding */
823 vnc_write_u16(vs
, 1); /* # of rects */
824 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
825 VNC_ENCODING_RICH_CURSOR
);
826 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
827 vnc_write_pixels_generic(vs
, c
->data
, isize
);
828 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
829 vnc_unlock_output(vs
);
835 static void vnc_dpy_cursor_define(DisplayState
*ds
, QEMUCursor
*c
)
837 VncDisplay
*vd
= vnc_display
;
840 cursor_put(vd
->cursor
);
841 g_free(vd
->cursor_mask
);
844 cursor_get(vd
->cursor
);
845 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
846 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
847 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
849 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
850 vnc_cursor_define(vs
);
854 static int find_and_clear_dirty_height(struct VncState
*vs
,
855 int y
, int last_x
, int x
, int height
)
859 for (h
= 1; h
< (height
- y
); h
++) {
861 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
864 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++) {
865 clear_bit(tmp_x
, vs
->dirty
[y
+ h
]);
872 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
874 int ret
= vnc_update_client(vs
, has_dirty
);
879 static int vnc_update_client(VncState
*vs
, int has_dirty
)
881 if (vs
->need_update
&& vs
->csock
!= -1) {
882 VncDisplay
*vd
= vs
->vd
;
889 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
890 /* kernel send buffers are full -> drop frames to throttle */
893 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
897 * Send screen updates to the vnc client using the server
898 * surface and server dirty map. guest surface updates
899 * happening in parallel don't disturb us, the next pass will
900 * send them to the client.
902 job
= vnc_job_new(vs
);
904 width
= MIN(pixman_image_get_width(vd
->server
), vs
->client_width
);
905 height
= MIN(pixman_image_get_height(vd
->server
), vs
->client_height
);
907 for (y
= 0; y
< height
; y
++) {
910 for (x
= 0; x
< width
/ 16; x
++) {
911 if (test_and_clear_bit(x
, vs
->dirty
[y
])) {
917 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
,
920 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
921 (x
- last_x
) * 16, h
);
927 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
, height
);
928 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
929 (x
- last_x
) * 16, h
);
934 vs
->force_update
= 0;
939 vnc_disconnect_finish(vs
);
945 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
947 VncState
*vs
= opaque
;
950 case AUD_CNOTIFY_DISABLE
:
952 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
953 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
954 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
955 vnc_unlock_output(vs
);
959 case AUD_CNOTIFY_ENABLE
:
961 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
962 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
963 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
964 vnc_unlock_output(vs
);
970 static void audio_capture_destroy(void *opaque
)
974 static void audio_capture(void *opaque
, void *buf
, int size
)
976 VncState
*vs
= opaque
;
979 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
980 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
981 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
982 vnc_write_u32(vs
, size
);
983 vnc_write(vs
, buf
, size
);
984 vnc_unlock_output(vs
);
988 static void audio_add(VncState
*vs
)
990 struct audio_capture_ops ops
;
993 monitor_printf(default_mon
, "audio already running\n");
997 ops
.notify
= audio_capture_notify
;
998 ops
.destroy
= audio_capture_destroy
;
999 ops
.capture
= audio_capture
;
1001 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1002 if (!vs
->audio_cap
) {
1003 monitor_printf(default_mon
, "Failed to add audio capture\n");
1007 static void audio_del(VncState
*vs
)
1009 if (vs
->audio_cap
) {
1010 AUD_del_capture(vs
->audio_cap
, vs
);
1011 vs
->audio_cap
= NULL
;
1015 static void vnc_disconnect_start(VncState
*vs
)
1017 if (vs
->csock
== -1)
1019 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1020 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
1021 closesocket(vs
->csock
);
1025 void vnc_disconnect_finish(VncState
*vs
)
1029 vnc_jobs_join(vs
); /* Wait encoding jobs */
1031 vnc_lock_output(vs
);
1032 vnc_qmp_event(vs
, QEVENT_VNC_DISCONNECTED
);
1034 buffer_free(&vs
->input
);
1035 buffer_free(&vs
->output
);
1036 #ifdef CONFIG_VNC_WS
1037 buffer_free(&vs
->ws_input
);
1038 buffer_free(&vs
->ws_output
);
1039 #endif /* CONFIG_VNC_WS */
1041 qobject_decref(vs
->info
);
1044 vnc_tight_clear(vs
);
1047 #ifdef CONFIG_VNC_TLS
1048 vnc_tls_client_cleanup(vs
);
1049 #endif /* CONFIG_VNC_TLS */
1050 #ifdef CONFIG_VNC_SASL
1051 vnc_sasl_client_cleanup(vs
);
1052 #endif /* CONFIG_VNC_SASL */
1054 vnc_release_modifiers(vs
);
1056 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1058 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1062 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1063 vnc_remove_timer(vs
->vd
);
1064 if (vs
->vd
->lock_key_sync
)
1065 qemu_remove_led_event_handler(vs
->led
);
1066 vnc_unlock_output(vs
);
1068 qemu_mutex_destroy(&vs
->output_mutex
);
1069 qemu_bh_delete(vs
->bh
);
1070 buffer_free(&vs
->jobs_buffer
);
1072 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1073 g_free(vs
->lossy_rect
[i
]);
1075 g_free(vs
->lossy_rect
);
1079 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
1081 if (ret
== 0 || ret
== -1) {
1083 switch (last_errno
) {
1087 case WSAEWOULDBLOCK
:
1095 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1096 ret
, ret
< 0 ? last_errno
: 0);
1097 vnc_disconnect_start(vs
);
1105 void vnc_client_error(VncState
*vs
)
1107 VNC_DEBUG("Closing down client sock: protocol error\n");
1108 vnc_disconnect_start(vs
);
1113 * Called to write a chunk of data to the client socket. The data may
1114 * be the raw data, or may have already been encoded by SASL.
1115 * The data will be written either straight onto the socket, or
1116 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1118 * NB, it is theoretically possible to have 2 layers of encryption,
1119 * both SASL, and this TLS layer. It is highly unlikely in practice
1120 * though, since SASL encryption will typically be a no-op if TLS
1123 * Returns the number of bytes written, which may be less than
1124 * the requested 'datalen' if the socket would block. Returns
1125 * -1 on error, and disconnects the client socket.
1127 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1130 #ifdef CONFIG_VNC_TLS
1131 if (vs
->tls
.session
) {
1132 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
1134 if (ret
== GNUTLS_E_AGAIN
)
1141 #endif /* CONFIG_VNC_TLS */
1142 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
1143 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1144 return vnc_client_io_error(vs
, ret
, socket_error());
1149 * Called to write buffered data to the client socket, when not
1150 * using any SASL SSF encryption layers. Will write as much data
1151 * as possible without blocking. If all buffered data is written,
1152 * will switch the FD poll() handler back to read monitoring.
1154 * Returns the number of bytes written, which may be less than
1155 * the buffered output data if the socket would block. Returns
1156 * -1 on error, and disconnects the client socket.
1158 static long vnc_client_write_plain(VncState
*vs
)
1162 #ifdef CONFIG_VNC_SASL
1163 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1164 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1165 vs
->sasl
.waitWriteSSF
);
1167 if (vs
->sasl
.conn
&&
1169 vs
->sasl
.waitWriteSSF
) {
1170 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1172 vs
->sasl
.waitWriteSSF
-= ret
;
1174 #endif /* CONFIG_VNC_SASL */
1175 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1179 buffer_advance(&vs
->output
, ret
);
1181 if (vs
->output
.offset
== 0) {
1182 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1190 * First function called whenever there is data to be written to
1191 * the client socket. Will delegate actual work according to whether
1192 * SASL SSF layers are enabled (thus requiring encryption calls)
1194 static void vnc_client_write_locked(void *opaque
)
1196 VncState
*vs
= opaque
;
1198 #ifdef CONFIG_VNC_SASL
1199 if (vs
->sasl
.conn
&&
1201 !vs
->sasl
.waitWriteSSF
) {
1202 vnc_client_write_sasl(vs
);
1204 #endif /* CONFIG_VNC_SASL */
1206 #ifdef CONFIG_VNC_WS
1207 if (vs
->encode_ws
) {
1208 vnc_client_write_ws(vs
);
1210 #endif /* CONFIG_VNC_WS */
1212 vnc_client_write_plain(vs
);
1217 void vnc_client_write(void *opaque
)
1219 VncState
*vs
= opaque
;
1221 vnc_lock_output(vs
);
1222 if (vs
->output
.offset
1223 #ifdef CONFIG_VNC_WS
1224 || vs
->ws_output
.offset
1227 vnc_client_write_locked(opaque
);
1228 } else if (vs
->csock
!= -1) {
1229 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1231 vnc_unlock_output(vs
);
1234 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1236 vs
->read_handler
= func
;
1237 vs
->read_handler_expect
= expecting
;
1242 * Called to read a chunk of data from the client socket. The data may
1243 * be the raw data, or may need to be further decoded by SASL.
1244 * The data will be read either straight from to the socket, or
1245 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1247 * NB, it is theoretically possible to have 2 layers of encryption,
1248 * both SASL, and this TLS layer. It is highly unlikely in practice
1249 * though, since SASL encryption will typically be a no-op if TLS
1252 * Returns the number of bytes read, which may be less than
1253 * the requested 'datalen' if the socket would block. Returns
1254 * -1 on error, and disconnects the client socket.
1256 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1259 #ifdef CONFIG_VNC_TLS
1260 if (vs
->tls
.session
) {
1261 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1263 if (ret
== GNUTLS_E_AGAIN
)
1270 #endif /* CONFIG_VNC_TLS */
1271 ret
= qemu_recv(vs
->csock
, data
, datalen
, 0);
1272 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1273 return vnc_client_io_error(vs
, ret
, socket_error());
1278 * Called to read data from the client socket to the input buffer,
1279 * when not using any SASL SSF encryption layers. Will read as much
1280 * data as possible without blocking.
1282 * Returns the number of bytes read. Returns -1 on error, and
1283 * disconnects the client socket.
1285 static long vnc_client_read_plain(VncState
*vs
)
1288 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1289 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1290 buffer_reserve(&vs
->input
, 4096);
1291 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1294 vs
->input
.offset
+= ret
;
1298 static void vnc_jobs_bh(void *opaque
)
1300 VncState
*vs
= opaque
;
1302 vnc_jobs_consume_buffer(vs
);
1306 * First function called whenever there is more data to be read from
1307 * the client socket. Will delegate actual work according to whether
1308 * SASL SSF layers are enabled (thus requiring decryption calls)
1310 void vnc_client_read(void *opaque
)
1312 VncState
*vs
= opaque
;
1315 #ifdef CONFIG_VNC_SASL
1316 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1317 ret
= vnc_client_read_sasl(vs
);
1319 #endif /* CONFIG_VNC_SASL */
1320 #ifdef CONFIG_VNC_WS
1321 if (vs
->encode_ws
) {
1322 ret
= vnc_client_read_ws(vs
);
1324 vnc_disconnect_start(vs
);
1326 } else if (ret
== -2) {
1327 vnc_client_error(vs
);
1331 #endif /* CONFIG_VNC_WS */
1333 ret
= vnc_client_read_plain(vs
);
1336 if (vs
->csock
== -1)
1337 vnc_disconnect_finish(vs
);
1341 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1342 size_t len
= vs
->read_handler_expect
;
1345 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1346 if (vs
->csock
== -1) {
1347 vnc_disconnect_finish(vs
);
1352 buffer_advance(&vs
->input
, len
);
1354 vs
->read_handler_expect
= ret
;
1359 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1361 buffer_reserve(&vs
->output
, len
);
1363 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1364 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1367 buffer_append(&vs
->output
, data
, len
);
1370 void vnc_write_s32(VncState
*vs
, int32_t value
)
1372 vnc_write_u32(vs
, *(uint32_t *)&value
);
1375 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1379 buf
[0] = (value
>> 24) & 0xFF;
1380 buf
[1] = (value
>> 16) & 0xFF;
1381 buf
[2] = (value
>> 8) & 0xFF;
1382 buf
[3] = value
& 0xFF;
1384 vnc_write(vs
, buf
, 4);
1387 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1391 buf
[0] = (value
>> 8) & 0xFF;
1392 buf
[1] = value
& 0xFF;
1394 vnc_write(vs
, buf
, 2);
1397 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1399 vnc_write(vs
, (char *)&value
, 1);
1402 void vnc_flush(VncState
*vs
)
1404 vnc_lock_output(vs
);
1405 if (vs
->csock
!= -1 && (vs
->output
.offset
1406 #ifdef CONFIG_VNC_WS
1407 || vs
->ws_output
.offset
1410 vnc_client_write_locked(vs
);
1412 vnc_unlock_output(vs
);
1415 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1417 return data
[offset
];
1420 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1422 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1425 static int32_t read_s32(uint8_t *data
, size_t offset
)
1427 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1428 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1431 uint32_t read_u32(uint8_t *data
, size_t offset
)
1433 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1434 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1437 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1441 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1443 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1444 int absolute
= kbd_mouse_is_absolute();
1446 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1447 vnc_lock_output(vs
);
1448 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1449 vnc_write_u8(vs
, 0);
1450 vnc_write_u16(vs
, 1);
1451 vnc_framebuffer_update(vs
, absolute
, 0,
1452 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1453 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1454 vnc_unlock_output(vs
);
1457 vs
->absolute
= absolute
;
1460 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1465 if (button_mask
& 0x01)
1466 buttons
|= MOUSE_EVENT_LBUTTON
;
1467 if (button_mask
& 0x02)
1468 buttons
|= MOUSE_EVENT_MBUTTON
;
1469 if (button_mask
& 0x04)
1470 buttons
|= MOUSE_EVENT_RBUTTON
;
1471 if (button_mask
& 0x08)
1473 if (button_mask
& 0x10)
1477 kbd_mouse_event(ds_get_width(vs
->ds
) > 1 ?
1478 x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1) : 0x4000,
1479 ds_get_height(vs
->ds
) > 1 ?
1480 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1) : 0x4000,
1482 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1486 kbd_mouse_event(x
, y
, dz
, buttons
);
1488 if (vs
->last_x
!= -1)
1489 kbd_mouse_event(x
- vs
->last_x
,
1497 static void reset_keys(VncState
*vs
)
1500 for(i
= 0; i
< 256; i
++) {
1501 if (vs
->modifiers_state
[i
]) {
1502 if (i
& SCANCODE_GREY
)
1503 kbd_put_keycode(SCANCODE_EMUL0
);
1504 kbd_put_keycode(i
| SCANCODE_UP
);
1505 vs
->modifiers_state
[i
] = 0;
1510 static void press_key(VncState
*vs
, int keysym
)
1512 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1513 if (keycode
& SCANCODE_GREY
)
1514 kbd_put_keycode(SCANCODE_EMUL0
);
1515 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1516 if (keycode
& SCANCODE_GREY
)
1517 kbd_put_keycode(SCANCODE_EMUL0
);
1518 kbd_put_keycode(keycode
| SCANCODE_UP
);
1521 static void kbd_leds(void *opaque
, int ledstate
)
1523 VncState
*vs
= opaque
;
1526 caps
= ledstate
& QEMU_CAPS_LOCK_LED
? 1 : 0;
1527 num
= ledstate
& QEMU_NUM_LOCK_LED
? 1 : 0;
1529 if (vs
->modifiers_state
[0x3a] != caps
) {
1530 vs
->modifiers_state
[0x3a] = caps
;
1532 if (vs
->modifiers_state
[0x45] != num
) {
1533 vs
->modifiers_state
[0x45] = num
;
1537 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1539 /* QEMU console switch */
1541 case 0x2a: /* Left Shift */
1542 case 0x36: /* Right Shift */
1543 case 0x1d: /* Left CTRL */
1544 case 0x9d: /* Right CTRL */
1545 case 0x38: /* Left ALT */
1546 case 0xb8: /* Right ALT */
1548 vs
->modifiers_state
[keycode
] = 1;
1550 vs
->modifiers_state
[keycode
] = 0;
1552 case 0x02 ... 0x0a: /* '1' to '9' keys */
1553 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1554 /* Reset the modifiers sent to the current console */
1556 console_select(keycode
- 0x02);
1560 case 0x3a: /* CapsLock */
1561 case 0x45: /* NumLock */
1563 vs
->modifiers_state
[keycode
] ^= 1;
1567 if (down
&& vs
->vd
->lock_key_sync
&&
1568 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1569 /* If the numlock state needs to change then simulate an additional
1570 keypress before sending this one. This will happen if the user
1571 toggles numlock away from the VNC window.
1573 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1574 if (!vs
->modifiers_state
[0x45]) {
1575 vs
->modifiers_state
[0x45] = 1;
1576 press_key(vs
, 0xff7f);
1579 if (vs
->modifiers_state
[0x45]) {
1580 vs
->modifiers_state
[0x45] = 0;
1581 press_key(vs
, 0xff7f);
1586 if (down
&& vs
->vd
->lock_key_sync
&&
1587 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1588 /* If the capslock state needs to change then simulate an additional
1589 keypress before sending this one. This will happen if the user
1590 toggles capslock away from the VNC window.
1592 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1593 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1594 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1596 if (uppercase
== shift
) {
1597 vs
->modifiers_state
[0x3a] = 0;
1598 press_key(vs
, 0xffe5);
1601 if (uppercase
!= shift
) {
1602 vs
->modifiers_state
[0x3a] = 1;
1603 press_key(vs
, 0xffe5);
1608 if (is_graphic_console()) {
1609 if (keycode
& SCANCODE_GREY
)
1610 kbd_put_keycode(SCANCODE_EMUL0
);
1612 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1614 kbd_put_keycode(keycode
| SCANCODE_UP
);
1616 bool numlock
= vs
->modifiers_state
[0x45];
1617 bool control
= (vs
->modifiers_state
[0x1d] ||
1618 vs
->modifiers_state
[0x9d]);
1619 /* QEMU console emulation */
1622 case 0x2a: /* Left Shift */
1623 case 0x36: /* Right Shift */
1624 case 0x1d: /* Left CTRL */
1625 case 0x9d: /* Right CTRL */
1626 case 0x38: /* Left ALT */
1627 case 0xb8: /* Right ALT */
1630 kbd_put_keysym(QEMU_KEY_UP
);
1633 kbd_put_keysym(QEMU_KEY_DOWN
);
1636 kbd_put_keysym(QEMU_KEY_LEFT
);
1639 kbd_put_keysym(QEMU_KEY_RIGHT
);
1642 kbd_put_keysym(QEMU_KEY_DELETE
);
1645 kbd_put_keysym(QEMU_KEY_HOME
);
1648 kbd_put_keysym(QEMU_KEY_END
);
1651 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1654 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1658 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1661 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1664 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1667 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1670 kbd_put_keysym('5');
1673 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1676 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1679 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1682 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1685 kbd_put_keysym('0');
1688 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1692 kbd_put_keysym('/');
1695 kbd_put_keysym('*');
1698 kbd_put_keysym('-');
1701 kbd_put_keysym('+');
1704 kbd_put_keysym('\n');
1709 kbd_put_keysym(sym
& 0x1f);
1711 kbd_put_keysym(sym
);
1719 static void vnc_release_modifiers(VncState
*vs
)
1721 static const int keycodes
[] = {
1722 /* shift, control, alt keys, both left & right */
1723 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1727 if (!is_graphic_console()) {
1730 for (i
= 0; i
< ARRAY_SIZE(keycodes
); i
++) {
1731 keycode
= keycodes
[i
];
1732 if (!vs
->modifiers_state
[keycode
]) {
1735 if (keycode
& SCANCODE_GREY
) {
1736 kbd_put_keycode(SCANCODE_EMUL0
);
1738 kbd_put_keycode(keycode
| SCANCODE_UP
);
1742 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1747 if (lsym
>= 'A' && lsym
<= 'Z' && is_graphic_console()) {
1748 lsym
= lsym
- 'A' + 'a';
1751 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1752 do_key_event(vs
, down
, keycode
, sym
);
1755 static void ext_key_event(VncState
*vs
, int down
,
1756 uint32_t sym
, uint16_t keycode
)
1758 /* if the user specifies a keyboard layout, always use it */
1759 if (keyboard_layout
)
1760 key_event(vs
, down
, sym
);
1762 do_key_event(vs
, down
, keycode
, sym
);
1765 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1766 int x_position
, int y_position
,
1770 const size_t width
= ds_get_width(vs
->ds
) / 16;
1772 if (y_position
> ds_get_height(vs
->ds
))
1773 y_position
= ds_get_height(vs
->ds
);
1774 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1775 h
= ds_get_height(vs
->ds
) - y_position
;
1777 vs
->need_update
= 1;
1779 vs
->force_update
= 1;
1780 for (i
= 0; i
< h
; i
++) {
1781 bitmap_set(vs
->dirty
[y_position
+ i
], 0, width
);
1782 bitmap_clear(vs
->dirty
[y_position
+ i
], width
,
1783 VNC_DIRTY_BITS
- width
);
1788 static void send_ext_key_event_ack(VncState
*vs
)
1790 vnc_lock_output(vs
);
1791 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1792 vnc_write_u8(vs
, 0);
1793 vnc_write_u16(vs
, 1);
1794 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1795 VNC_ENCODING_EXT_KEY_EVENT
);
1796 vnc_unlock_output(vs
);
1800 static void send_ext_audio_ack(VncState
*vs
)
1802 vnc_lock_output(vs
);
1803 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1804 vnc_write_u8(vs
, 0);
1805 vnc_write_u16(vs
, 1);
1806 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1807 VNC_ENCODING_AUDIO
);
1808 vnc_unlock_output(vs
);
1812 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1815 unsigned int enc
= 0;
1818 vs
->vnc_encoding
= 0;
1819 vs
->tight
.compression
= 9;
1820 vs
->tight
.quality
= -1; /* Lossless by default */
1824 * Start from the end because the encodings are sent in order of preference.
1825 * This way the preferred encoding (first encoding defined in the array)
1826 * will be set at the end of the loop.
1828 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1831 case VNC_ENCODING_RAW
:
1832 vs
->vnc_encoding
= enc
;
1834 case VNC_ENCODING_COPYRECT
:
1835 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1837 case VNC_ENCODING_HEXTILE
:
1838 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1839 vs
->vnc_encoding
= enc
;
1841 case VNC_ENCODING_TIGHT
:
1842 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
1843 vs
->vnc_encoding
= enc
;
1845 #ifdef CONFIG_VNC_PNG
1846 case VNC_ENCODING_TIGHT_PNG
:
1847 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
1848 vs
->vnc_encoding
= enc
;
1851 case VNC_ENCODING_ZLIB
:
1852 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1853 vs
->vnc_encoding
= enc
;
1855 case VNC_ENCODING_ZRLE
:
1856 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
1857 vs
->vnc_encoding
= enc
;
1859 case VNC_ENCODING_ZYWRLE
:
1860 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
1861 vs
->vnc_encoding
= enc
;
1863 case VNC_ENCODING_DESKTOPRESIZE
:
1864 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1866 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1867 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1869 case VNC_ENCODING_RICH_CURSOR
:
1870 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
1872 case VNC_ENCODING_EXT_KEY_EVENT
:
1873 send_ext_key_event_ack(vs
);
1875 case VNC_ENCODING_AUDIO
:
1876 send_ext_audio_ack(vs
);
1878 case VNC_ENCODING_WMVi
:
1879 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1881 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1882 vs
->tight
.compression
= (enc
& 0x0F);
1884 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1885 if (vs
->vd
->lossy
) {
1886 vs
->tight
.quality
= (enc
& 0x0F);
1890 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1894 vnc_desktop_resize(vs
);
1895 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
1898 static void set_pixel_conversion(VncState
*vs
)
1900 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
1902 if (fmt
== VNC_SERVER_FB_FORMAT
) {
1903 vs
->write_pixels
= vnc_write_pixels_copy
;
1904 vnc_hextile_set_pixel_conversion(vs
, 0);
1906 vs
->write_pixels
= vnc_write_pixels_generic
;
1907 vnc_hextile_set_pixel_conversion(vs
, 1);
1911 static void set_pixel_format(VncState
*vs
,
1912 int bits_per_pixel
, int depth
,
1913 int big_endian_flag
, int true_color_flag
,
1914 int red_max
, int green_max
, int blue_max
,
1915 int red_shift
, int green_shift
, int blue_shift
)
1917 if (!true_color_flag
) {
1918 vnc_client_error(vs
);
1922 vs
->client_pf
.rmax
= red_max
;
1923 vs
->client_pf
.rbits
= hweight_long(red_max
);
1924 vs
->client_pf
.rshift
= red_shift
;
1925 vs
->client_pf
.rmask
= red_max
<< red_shift
;
1926 vs
->client_pf
.gmax
= green_max
;
1927 vs
->client_pf
.gbits
= hweight_long(green_max
);
1928 vs
->client_pf
.gshift
= green_shift
;
1929 vs
->client_pf
.gmask
= green_max
<< green_shift
;
1930 vs
->client_pf
.bmax
= blue_max
;
1931 vs
->client_pf
.bbits
= hweight_long(blue_max
);
1932 vs
->client_pf
.bshift
= blue_shift
;
1933 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
1934 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
1935 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1936 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1937 vs
->client_be
= big_endian_flag
;
1939 set_pixel_conversion(vs
);
1941 vga_hw_invalidate();
1945 static void pixel_format_message (VncState
*vs
) {
1946 char pad
[3] = { 0, 0, 0 };
1948 vs
->client_pf
= qemu_default_pixelformat(32);
1950 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
1951 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
1953 #ifdef HOST_WORDS_BIGENDIAN
1954 vnc_write_u8(vs
, 1); /* big-endian-flag */
1956 vnc_write_u8(vs
, 0); /* big-endian-flag */
1958 vnc_write_u8(vs
, 1); /* true-color-flag */
1959 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
1960 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
1961 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
1962 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
1963 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
1964 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
1965 vnc_write(vs
, pad
, 3); /* padding */
1967 vnc_hextile_set_pixel_conversion(vs
, 0);
1968 vs
->write_pixels
= vnc_write_pixels_copy
;
1971 static void vnc_dpy_setdata(DisplayState
*ds
)
1973 VncDisplay
*vd
= ds
->opaque
;
1975 qemu_pixman_image_unref(vd
->guest
.fb
);
1976 vd
->guest
.fb
= pixman_image_ref(ds
->surface
->image
);
1977 vd
->guest
.format
= ds
->surface
->format
;
1978 vnc_dpy_update(ds
, 0, 0, ds_get_width(ds
), ds_get_height(ds
));
1981 static void vnc_colordepth(VncState
*vs
)
1983 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1984 /* Sending a WMVi message to notify the client*/
1985 vnc_lock_output(vs
);
1986 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1987 vnc_write_u8(vs
, 0);
1988 vnc_write_u16(vs
, 1); /* number of rects */
1989 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1990 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1991 pixel_format_message(vs
);
1992 vnc_unlock_output(vs
);
1995 set_pixel_conversion(vs
);
1999 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
2003 VncDisplay
*vd
= vs
->vd
;
2006 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2007 if (!qemu_timer_expired(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
))
2008 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
2012 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
2016 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
2017 read_u8(data
, 6), read_u8(data
, 7),
2018 read_u16(data
, 8), read_u16(data
, 10),
2019 read_u16(data
, 12), read_u8(data
, 14),
2020 read_u8(data
, 15), read_u8(data
, 16));
2022 case VNC_MSG_CLIENT_SET_ENCODINGS
:
2027 limit
= read_u16(data
, 2);
2029 return 4 + (limit
* 4);
2031 limit
= read_u16(data
, 2);
2033 for (i
= 0; i
< limit
; i
++) {
2034 int32_t val
= read_s32(data
, 4 + (i
* 4));
2035 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2038 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2040 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2044 framebuffer_update_request(vs
,
2045 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2046 read_u16(data
, 6), read_u16(data
, 8));
2048 case VNC_MSG_CLIENT_KEY_EVENT
:
2052 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2054 case VNC_MSG_CLIENT_POINTER_EVENT
:
2058 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2060 case VNC_MSG_CLIENT_CUT_TEXT
:
2065 uint32_t dlen
= read_u32(data
, 4);
2070 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2072 case VNC_MSG_CLIENT_QEMU
:
2076 switch (read_u8(data
, 1)) {
2077 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2081 ext_key_event(vs
, read_u16(data
, 2),
2082 read_u32(data
, 4), read_u32(data
, 8));
2084 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2088 switch (read_u16 (data
, 2)) {
2089 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2092 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2095 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2098 switch (read_u8(data
, 4)) {
2099 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2100 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2101 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2102 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2103 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2104 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2106 printf("Invalid audio format %d\n", read_u8(data
, 4));
2107 vnc_client_error(vs
);
2110 vs
->as
.nchannels
= read_u8(data
, 5);
2111 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2112 printf("Invalid audio channel coount %d\n",
2114 vnc_client_error(vs
);
2117 vs
->as
.freq
= read_u32(data
, 6);
2120 printf ("Invalid audio message %d\n", read_u8(data
, 4));
2121 vnc_client_error(vs
);
2127 printf("Msg: %d\n", read_u16(data
, 0));
2128 vnc_client_error(vs
);
2133 printf("Msg: %d\n", data
[0]);
2134 vnc_client_error(vs
);
2138 vnc_read_when(vs
, protocol_client_msg
, 1);
2142 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2148 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2149 switch (vs
->vd
->share_policy
) {
2150 case VNC_SHARE_POLICY_IGNORE
:
2152 * Ignore the shared flag. Nothing to do here.
2154 * Doesn't conform to the rfb spec but is traditional qemu
2155 * behavior, thus left here as option for compatibility
2159 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2161 * Policy: Allow clients ask for exclusive access.
2163 * Implementation: When a client asks for exclusive access,
2164 * disconnect all others. Shared connects are allowed as long
2165 * as no exclusive connection exists.
2167 * This is how the rfb spec suggests to handle the shared flag.
2169 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2171 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2175 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2176 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2179 vnc_disconnect_start(client
);
2182 if (mode
== VNC_SHARE_MODE_SHARED
) {
2183 if (vs
->vd
->num_exclusive
> 0) {
2184 vnc_disconnect_start(vs
);
2189 case VNC_SHARE_POLICY_FORCE_SHARED
:
2191 * Policy: Shared connects only.
2192 * Implementation: Disallow clients asking for exclusive access.
2194 * Useful for shared desktop sessions where you don't want
2195 * someone forgetting to say -shared when running the vnc
2196 * client disconnect everybody else.
2198 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2199 vnc_disconnect_start(vs
);
2204 vnc_set_share_mode(vs
, mode
);
2206 vs
->client_width
= ds_get_width(vs
->ds
);
2207 vs
->client_height
= ds_get_height(vs
->ds
);
2208 vnc_write_u16(vs
, vs
->client_width
);
2209 vnc_write_u16(vs
, vs
->client_height
);
2211 pixel_format_message(vs
);
2214 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2216 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2218 vnc_write_u32(vs
, size
);
2219 vnc_write(vs
, buf
, size
);
2222 vnc_client_cache_auth(vs
);
2223 vnc_qmp_event(vs
, QEVENT_VNC_INITIALIZED
);
2225 vnc_read_when(vs
, protocol_client_msg
, 1);
2230 void start_client_init(VncState
*vs
)
2232 vnc_read_when(vs
, protocol_client_init
, 1);
2235 static void make_challenge(VncState
*vs
)
2239 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2241 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2242 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2245 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2247 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2249 unsigned char key
[8];
2250 time_t now
= time(NULL
);
2252 if (!vs
->vd
->password
) {
2253 VNC_DEBUG("No password configured on server");
2256 if (vs
->vd
->expires
< now
) {
2257 VNC_DEBUG("Password is expired");
2261 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2263 /* Calculate the expected challenge response */
2264 pwlen
= strlen(vs
->vd
->password
);
2265 for (i
=0; i
<sizeof(key
); i
++)
2266 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2268 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
2269 des(response
+j
, response
+j
);
2271 /* Compare expected vs actual challenge response */
2272 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2273 VNC_DEBUG("Client challenge response did not match\n");
2276 VNC_DEBUG("Accepting VNC challenge response\n");
2277 vnc_write_u32(vs
, 0); /* Accept auth */
2280 start_client_init(vs
);
2285 vnc_write_u32(vs
, 1); /* Reject auth */
2286 if (vs
->minor
>= 8) {
2287 static const char err
[] = "Authentication failed";
2288 vnc_write_u32(vs
, sizeof(err
));
2289 vnc_write(vs
, err
, sizeof(err
));
2292 vnc_client_error(vs
);
2296 void start_auth_vnc(VncState
*vs
)
2299 /* Send client a 'random' challenge */
2300 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2303 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2307 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2309 /* We only advertise 1 auth scheme at a time, so client
2310 * must pick the one we sent. Verify this */
2311 if (data
[0] != vs
->auth
) { /* Reject auth */
2312 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
2313 vnc_write_u32(vs
, 1);
2314 if (vs
->minor
>= 8) {
2315 static const char err
[] = "Authentication failed";
2316 vnc_write_u32(vs
, sizeof(err
));
2317 vnc_write(vs
, err
, sizeof(err
));
2319 vnc_client_error(vs
);
2320 } else { /* Accept requested auth */
2321 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2324 VNC_DEBUG("Accept auth none\n");
2325 if (vs
->minor
>= 8) {
2326 vnc_write_u32(vs
, 0); /* Accept auth completion */
2329 start_client_init(vs
);
2333 VNC_DEBUG("Start VNC auth\n");
2337 #ifdef CONFIG_VNC_TLS
2338 case VNC_AUTH_VENCRYPT
:
2339 VNC_DEBUG("Accept VeNCrypt auth\n");
2340 start_auth_vencrypt(vs
);
2342 #endif /* CONFIG_VNC_TLS */
2344 #ifdef CONFIG_VNC_SASL
2346 VNC_DEBUG("Accept SASL auth\n");
2347 start_auth_sasl(vs
);
2349 #endif /* CONFIG_VNC_SASL */
2351 default: /* Should not be possible, but just in case */
2352 VNC_DEBUG("Reject auth %d server code bug\n", vs
->auth
);
2353 vnc_write_u8(vs
, 1);
2354 if (vs
->minor
>= 8) {
2355 static const char err
[] = "Authentication failed";
2356 vnc_write_u32(vs
, sizeof(err
));
2357 vnc_write(vs
, err
, sizeof(err
));
2359 vnc_client_error(vs
);
2365 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2369 memcpy(local
, version
, 12);
2372 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2373 VNC_DEBUG("Malformed protocol version %s\n", local
);
2374 vnc_client_error(vs
);
2377 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2378 if (vs
->major
!= 3 ||
2384 VNC_DEBUG("Unsupported client version\n");
2385 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2387 vnc_client_error(vs
);
2390 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2391 * as equivalent to v3.3 by servers
2393 if (vs
->minor
== 4 || vs
->minor
== 5)
2396 if (vs
->minor
== 3) {
2397 if (vs
->auth
== VNC_AUTH_NONE
) {
2398 VNC_DEBUG("Tell client auth none\n");
2399 vnc_write_u32(vs
, vs
->auth
);
2401 start_client_init(vs
);
2402 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2403 VNC_DEBUG("Tell client VNC auth\n");
2404 vnc_write_u32(vs
, vs
->auth
);
2408 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->auth
);
2409 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2411 vnc_client_error(vs
);
2414 VNC_DEBUG("Telling client we support auth %d\n", vs
->auth
);
2415 vnc_write_u8(vs
, 1); /* num auth */
2416 vnc_write_u8(vs
, vs
->auth
);
2417 vnc_read_when(vs
, protocol_client_auth
, 1);
2424 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2426 struct VncSurface
*vs
= &vd
->guest
;
2428 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2431 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2435 w
= (x
+ w
) / VNC_STAT_RECT
;
2436 h
= (y
+ h
) / VNC_STAT_RECT
;
2440 for (j
= y
; j
<= h
; j
++) {
2441 for (i
= x
; i
<= w
; i
++) {
2442 vs
->lossy_rect
[j
][i
] = 1;
2447 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2450 int sty
= y
/ VNC_STAT_RECT
;
2451 int stx
= x
/ VNC_STAT_RECT
;
2454 y
= y
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2455 x
= x
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2457 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2460 /* kernel send buffers are full -> refresh later */
2461 if (vs
->output
.offset
) {
2465 if (!vs
->lossy_rect
[sty
][stx
]) {
2469 vs
->lossy_rect
[sty
][stx
] = 0;
2470 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2471 bitmap_set(vs
->dirty
[y
+ j
], x
/ 16, VNC_STAT_RECT
/ 16);
2479 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2481 int width
= pixman_image_get_width(vd
->guest
.fb
);
2482 int height
= pixman_image_get_height(vd
->guest
.fb
);
2487 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2488 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2489 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2491 rect
->updated
= false;
2495 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2497 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2500 vd
->guest
.last_freq_check
= *tv
;
2502 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2503 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2504 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2505 int count
= ARRAY_SIZE(rect
->times
);
2506 struct timeval min
, max
;
2508 if (!timerisset(&rect
->times
[count
- 1])) {
2512 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2513 qemu_timersub(tv
, &max
, &res
);
2515 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2517 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2518 memset(rect
->times
, 0, sizeof (rect
->times
));
2522 min
= rect
->times
[rect
->idx
];
2523 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2524 qemu_timersub(&max
, &min
, &res
);
2526 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2527 rect
->freq
/= count
;
2528 rect
->freq
= 1. / rect
->freq
;
2534 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2540 x
= (x
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2541 y
= (y
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2543 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2544 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2545 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2557 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2561 rect
= vnc_stat_rect(vd
, x
, y
);
2562 if (rect
->updated
) {
2565 rect
->times
[rect
->idx
] = *tv
;
2566 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2567 rect
->updated
= true;
2570 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2572 int width
= pixman_image_get_width(vd
->guest
.fb
);
2573 int height
= pixman_image_get_height(vd
->guest
.fb
);
2576 uint8_t *server_row
;
2580 pixman_image_t
*tmpbuf
= NULL
;
2582 struct timeval tv
= { 0, 0 };
2584 if (!vd
->non_adaptive
) {
2585 gettimeofday(&tv
, NULL
);
2586 has_dirty
= vnc_update_stats(vd
, &tv
);
2590 * Walk through the guest dirty map.
2591 * Check and copy modified bits from guest to server surface.
2592 * Update server dirty map.
2595 if (cmp_bytes
> vnc_server_fb_stride(vd
)) {
2596 cmp_bytes
= vnc_server_fb_stride(vd
);
2598 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2599 int width
= pixman_image_get_width(vd
->server
);
2600 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2602 guest_row
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2603 server_row
= (uint8_t *)pixman_image_get_data(vd
->server
);
2604 for (y
= 0; y
< height
; y
++) {
2605 if (!bitmap_empty(vd
->guest
.dirty
[y
], VNC_DIRTY_BITS
)) {
2608 uint8_t *server_ptr
;
2610 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2611 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, 0, y
);
2612 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2614 guest_ptr
= guest_row
;
2616 server_ptr
= server_row
;
2618 for (x
= 0; x
+ 15 < width
;
2619 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2620 if (!test_and_clear_bit((x
/ 16), vd
->guest
.dirty
[y
]))
2622 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2624 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2625 if (!vd
->non_adaptive
)
2626 vnc_rect_updated(vd
, x
, y
, &tv
);
2627 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2628 set_bit((x
/ 16), vs
->dirty
[y
]);
2633 guest_row
+= pixman_image_get_stride(vd
->guest
.fb
);
2634 server_row
+= pixman_image_get_stride(vd
->server
);
2636 qemu_pixman_image_unref(tmpbuf
);
2640 static void vnc_refresh(void *opaque
)
2642 VncDisplay
*vd
= opaque
;
2644 int has_dirty
, rects
= 0;
2648 if (vnc_trylock_display(vd
)) {
2649 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2650 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) +
2651 vd
->timer_interval
);
2655 has_dirty
= vnc_refresh_server_surface(vd
);
2656 vnc_unlock_display(vd
);
2658 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2659 rects
+= vnc_update_client(vs
, has_dirty
);
2660 /* vs might be free()ed here */
2663 /* vd->timer could be NULL now if the last client disconnected,
2664 * in this case don't update the timer */
2665 if (vd
->timer
== NULL
)
2668 if (has_dirty
&& rects
) {
2669 vd
->timer_interval
/= 2;
2670 if (vd
->timer_interval
< VNC_REFRESH_INTERVAL_BASE
)
2671 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2673 vd
->timer_interval
+= VNC_REFRESH_INTERVAL_INC
;
2674 if (vd
->timer_interval
> VNC_REFRESH_INTERVAL_MAX
)
2675 vd
->timer_interval
= VNC_REFRESH_INTERVAL_MAX
;
2677 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
2680 static void vnc_init_timer(VncDisplay
*vd
)
2682 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2683 if (vd
->timer
== NULL
&& !QTAILQ_EMPTY(&vd
->clients
)) {
2684 vd
->timer
= qemu_new_timer_ms(rt_clock
, vnc_refresh
, vd
);
2685 vnc_dpy_resize(vd
->ds
);
2690 static void vnc_remove_timer(VncDisplay
*vd
)
2692 if (vd
->timer
!= NULL
&& QTAILQ_EMPTY(&vd
->clients
)) {
2693 qemu_del_timer(vd
->timer
);
2694 qemu_free_timer(vd
->timer
);
2699 static void vnc_connect(VncDisplay
*vd
, int csock
, int skipauth
, bool websocket
)
2701 VncState
*vs
= g_malloc0(sizeof(VncState
));
2707 vs
->auth
= VNC_AUTH_NONE
;
2708 #ifdef CONFIG_VNC_TLS
2709 vs
->subauth
= VNC_AUTH_INVALID
;
2712 vs
->auth
= vd
->auth
;
2713 #ifdef CONFIG_VNC_TLS
2714 vs
->subauth
= vd
->subauth
;
2718 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
2719 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
2720 vs
->lossy_rect
[i
] = g_malloc0(VNC_STAT_COLS
* sizeof (uint8_t));
2723 VNC_DEBUG("New client on socket %d\n", csock
);
2725 socket_set_nonblock(vs
->csock
);
2726 #ifdef CONFIG_VNC_WS
2729 qemu_set_fd_handler2(vs
->csock
, NULL
, vncws_handshake_read
, NULL
, vs
);
2731 #endif /* CONFIG_VNC_WS */
2733 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2736 vnc_client_cache_addr(vs
);
2737 vnc_qmp_event(vs
, QEVENT_VNC_CONNECTED
);
2738 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
2742 #ifdef CONFIG_VNC_WS
2750 void vnc_init_state(VncState
*vs
)
2752 VncDisplay
*vd
= vs
->vd
;
2758 vs
->as
.freq
= 44100;
2759 vs
->as
.nchannels
= 2;
2760 vs
->as
.fmt
= AUD_FMT_S16
;
2761 vs
->as
.endianness
= 0;
2763 qemu_mutex_init(&vs
->output_mutex
);
2764 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
2766 QTAILQ_INSERT_HEAD(&vd
->clients
, vs
, next
);
2770 vnc_write(vs
, "RFB 003.008\n", 12);
2772 vnc_read_when(vs
, protocol_version
, 12);
2774 if (vs
->vd
->lock_key_sync
)
2775 vs
->led
= qemu_add_led_event_handler(kbd_leds
, vs
);
2777 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
2778 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
2782 /* vs might be free()ed here */
2785 static void vnc_listen_read(void *opaque
, bool websocket
)
2787 VncDisplay
*vs
= opaque
;
2788 struct sockaddr_in addr
;
2789 socklen_t addrlen
= sizeof(addr
);
2794 #ifdef CONFIG_VNC_WS
2796 csock
= qemu_accept(vs
->lwebsock
, (struct sockaddr
*)&addr
, &addrlen
);
2798 #endif /* CONFIG_VNC_WS */
2800 csock
= qemu_accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2804 vnc_connect(vs
, csock
, 0, websocket
);
2808 static void vnc_listen_regular_read(void *opaque
)
2810 vnc_listen_read(opaque
, 0);
2813 #ifdef CONFIG_VNC_WS
2814 static void vnc_listen_websocket_read(void *opaque
)
2816 vnc_listen_read(opaque
, 1);
2818 #endif /* CONFIG_VNC_WS */
2820 void vnc_display_init(DisplayState
*ds
)
2822 VncDisplay
*vs
= g_malloc0(sizeof(*vs
));
2824 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
2831 #ifdef CONFIG_VNC_WS
2836 QTAILQ_INIT(&vs
->clients
);
2837 vs
->expires
= TIME_MAX
;
2839 if (keyboard_layout
)
2840 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2842 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2844 if (!vs
->kbd_layout
)
2847 qemu_mutex_init(&vs
->mutex
);
2848 vnc_start_worker_thread();
2850 dcl
->dpy_gfx_copy
= vnc_dpy_copy
;
2851 dcl
->dpy_gfx_update
= vnc_dpy_update
;
2852 dcl
->dpy_gfx_resize
= vnc_dpy_resize
;
2853 dcl
->dpy_gfx_setdata
= vnc_dpy_setdata
;
2854 dcl
->dpy_mouse_set
= vnc_mouse_set
;
2855 dcl
->dpy_cursor_define
= vnc_dpy_cursor_define
;
2856 register_displaychangelistener(ds
, dcl
);
2860 static void vnc_display_close(DisplayState
*ds
)
2862 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2867 g_free(vs
->display
);
2870 if (vs
->lsock
!= -1) {
2871 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2875 #ifdef CONFIG_VNC_WS
2876 g_free(vs
->ws_display
);
2877 vs
->ws_display
= NULL
;
2878 if (vs
->lwebsock
!= -1) {
2879 qemu_set_fd_handler2(vs
->lwebsock
, NULL
, NULL
, NULL
, NULL
);
2880 close(vs
->lwebsock
);
2883 #endif /* CONFIG_VNC_WS */
2884 vs
->auth
= VNC_AUTH_INVALID
;
2885 #ifdef CONFIG_VNC_TLS
2886 vs
->subauth
= VNC_AUTH_INVALID
;
2887 vs
->tls
.x509verify
= 0;
2891 static int vnc_display_disable_login(DisplayState
*ds
)
2893 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2900 g_free(vs
->password
);
2903 vs
->password
= NULL
;
2904 if (vs
->auth
== VNC_AUTH_NONE
) {
2905 vs
->auth
= VNC_AUTH_VNC
;
2911 int vnc_display_password(DisplayState
*ds
, const char *password
)
2913 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2920 /* This is not the intention of this interface but err on the side
2922 return vnc_display_disable_login(ds
);
2926 g_free(vs
->password
);
2927 vs
->password
= NULL
;
2929 vs
->password
= g_strdup(password
);
2930 if (vs
->auth
== VNC_AUTH_NONE
) {
2931 vs
->auth
= VNC_AUTH_VNC
;
2937 int vnc_display_pw_expire(DisplayState
*ds
, time_t expires
)
2939 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2945 vs
->expires
= expires
;
2949 char *vnc_display_local_addr(DisplayState
*ds
)
2951 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2953 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2956 void vnc_display_open(DisplayState
*ds
, const char *display
, Error
**errp
)
2958 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2959 const char *options
;
2962 #ifdef CONFIG_VNC_TLS
2963 int tls
= 0, x509
= 0;
2965 #ifdef CONFIG_VNC_SASL
2969 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2972 int lock_key_sync
= 1;
2975 error_setg(errp
, "VNC display not active");
2978 vnc_display_close(ds
);
2979 if (strcmp(display
, "none") == 0)
2982 vs
->display
= g_strdup(display
);
2983 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
2986 while ((options
= strchr(options
, ','))) {
2988 if (strncmp(options
, "password", 8) == 0) {
2989 if (fips_get_state()) {
2991 "VNC password auth disabled due to FIPS mode, "
2992 "consider using the VeNCrypt or SASL authentication "
2993 "methods as an alternative");
2996 password
= 1; /* Require password auth */
2997 } else if (strncmp(options
, "reverse", 7) == 0) {
2999 } else if (strncmp(options
, "no-lock-key-sync", 16) == 0) {
3001 #ifdef CONFIG_VNC_SASL
3002 } else if (strncmp(options
, "sasl", 4) == 0) {
3003 sasl
= 1; /* Require SASL auth */
3005 #ifdef CONFIG_VNC_WS
3006 } else if (strncmp(options
, "websocket", 9) == 0) {
3010 /* Check for 'websocket=<port>' */
3011 start
= strchr(options
, '=');
3012 end
= strchr(options
, ',');
3013 if (start
&& (!end
|| (start
< end
))) {
3014 int len
= end
? end
-(start
+1) : strlen(start
+1);
3016 /* extract the host specification from display */
3017 char *host
= NULL
, *port
= NULL
, *host_end
= NULL
;
3018 port
= g_strndup(start
+ 1, len
);
3020 /* ipv6 hosts have colons */
3021 end
= strchr(display
, ',');
3022 host_end
= g_strrstr_len(display
, end
- display
, ":");
3025 host
= g_strndup(display
, host_end
- display
+ 1);
3027 host
= g_strndup(":", 1);
3029 vs
->ws_display
= g_strconcat(host
, port
, NULL
);
3034 #endif /* CONFIG_VNC_WS */
3035 #ifdef CONFIG_VNC_TLS
3036 } else if (strncmp(options
, "tls", 3) == 0) {
3037 tls
= 1; /* Require TLS */
3038 } else if (strncmp(options
, "x509", 4) == 0) {
3040 x509
= 1; /* Require x509 certificates */
3041 if (strncmp(options
, "x509verify", 10) == 0)
3042 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
3044 /* Now check for 'x509=/some/path' postfix
3045 * and use that to setup x509 certificate/key paths */
3046 start
= strchr(options
, '=');
3047 end
= strchr(options
, ',');
3048 if (start
&& (!end
|| (start
< end
))) {
3049 int len
= end
? end
-(start
+1) : strlen(start
+1);
3050 char *path
= g_strndup(start
+ 1, len
);
3052 VNC_DEBUG("Trying certificate path '%s'\n", path
);
3053 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
3054 error_setg(errp
, "Failed to find x509 certificates/keys in %s", path
);
3060 error_setg(errp
, "No certificate path provided");
3064 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
3065 } else if (strncmp(options
, "acl", 3) == 0) {
3068 } else if (strncmp(options
, "lossy", 5) == 0) {
3070 } else if (strncmp(options
, "non-adaptive", 12) == 0) {
3071 vs
->non_adaptive
= true;
3072 } else if (strncmp(options
, "share=", 6) == 0) {
3073 if (strncmp(options
+6, "ignore", 6) == 0) {
3074 vs
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
3075 } else if (strncmp(options
+6, "allow-exclusive", 15) == 0) {
3076 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
3077 } else if (strncmp(options
+6, "force-shared", 12) == 0) {
3078 vs
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
3080 error_setg(errp
, "unknown vnc share= option");
3086 #ifdef CONFIG_VNC_TLS
3087 if (acl
&& x509
&& vs
->tls
.x509verify
) {
3088 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
3089 fprintf(stderr
, "Failed to create x509 dname ACL\n");
3094 #ifdef CONFIG_VNC_SASL
3096 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
3097 fprintf(stderr
, "Failed to create username ACL\n");
3104 * Combinations we support here:
3106 * - no-auth (clear text, no auth)
3107 * - password (clear text, weak auth)
3108 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
3109 * - tls (encrypt, weak anonymous creds, no auth)
3110 * - tls + password (encrypt, weak anonymous creds, weak auth)
3111 * - tls + sasl (encrypt, weak anonymous creds, good auth)
3112 * - tls + x509 (encrypt, good x509 creds, no auth)
3113 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
3114 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
3116 * NB1. TLS is a stackable auth scheme.
3117 * NB2. the x509 schemes have option to validate a client cert dname
3120 #ifdef CONFIG_VNC_TLS
3122 vs
->auth
= VNC_AUTH_VENCRYPT
;
3124 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3125 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3127 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3128 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3131 #endif /* CONFIG_VNC_TLS */
3132 VNC_DEBUG("Initializing VNC server with password auth\n");
3133 vs
->auth
= VNC_AUTH_VNC
;
3134 #ifdef CONFIG_VNC_TLS
3135 vs
->subauth
= VNC_AUTH_INVALID
;
3137 #endif /* CONFIG_VNC_TLS */
3138 #ifdef CONFIG_VNC_SASL
3140 #ifdef CONFIG_VNC_TLS
3142 vs
->auth
= VNC_AUTH_VENCRYPT
;
3144 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3145 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3147 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3148 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3151 #endif /* CONFIG_VNC_TLS */
3152 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3153 vs
->auth
= VNC_AUTH_SASL
;
3154 #ifdef CONFIG_VNC_TLS
3155 vs
->subauth
= VNC_AUTH_INVALID
;
3157 #endif /* CONFIG_VNC_TLS */
3158 #endif /* CONFIG_VNC_SASL */
3160 #ifdef CONFIG_VNC_TLS
3162 vs
->auth
= VNC_AUTH_VENCRYPT
;
3164 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3165 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3167 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3168 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3172 VNC_DEBUG("Initializing VNC server with no auth\n");
3173 vs
->auth
= VNC_AUTH_NONE
;
3174 #ifdef CONFIG_VNC_TLS
3175 vs
->subauth
= VNC_AUTH_INVALID
;
3180 #ifdef CONFIG_VNC_SASL
3181 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
3182 error_setg(errp
, "Failed to initialize SASL auth: %s",
3183 sasl_errstring(saslErr
, NULL
, NULL
));
3187 vs
->lock_key_sync
= lock_key_sync
;
3190 /* connect to viewer */
3193 #ifdef CONFIG_VNC_WS
3196 if (strncmp(display
, "unix:", 5) == 0) {
3197 csock
= unix_connect(display
+5, errp
);
3199 csock
= inet_connect(display
, errp
);
3204 vnc_connect(vs
, csock
, 0, 0);
3206 /* listen for connects */
3208 dpy
= g_malloc(256);
3209 if (strncmp(display
, "unix:", 5) == 0) {
3210 pstrcpy(dpy
, 256, "unix:");
3211 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5, errp
);
3213 vs
->lsock
= inet_listen(display
, dpy
, 256,
3214 SOCK_STREAM
, 5900, errp
);
3215 if (vs
->lsock
< 0) {
3219 #ifdef CONFIG_VNC_WS
3220 if (vs
->websocket
) {
3221 if (vs
->ws_display
) {
3222 vs
->lwebsock
= inet_listen(vs
->ws_display
, NULL
, 256,
3223 SOCK_STREAM
, 0, errp
);
3225 vs
->lwebsock
= inet_listen(vs
->display
, NULL
, 256,
3226 SOCK_STREAM
, 5700, errp
);
3229 if (vs
->lwebsock
< 0) {
3238 #endif /* CONFIG_VNC_WS */
3240 g_free(vs
->display
);
3242 qemu_set_fd_handler2(vs
->lsock
, NULL
,
3243 vnc_listen_regular_read
, NULL
, vs
);
3244 #ifdef CONFIG_VNC_WS
3245 if (vs
->websocket
) {
3246 qemu_set_fd_handler2(vs
->lwebsock
, NULL
,
3247 vnc_listen_websocket_read
, NULL
, vs
);
3249 #endif /* CONFIG_VNC_WS */
3254 g_free(vs
->display
);
3256 #ifdef CONFIG_VNC_WS
3257 g_free(vs
->ws_display
);
3258 vs
->ws_display
= NULL
;
3259 #endif /* CONFIG_VNC_WS */
3262 void vnc_display_add_client(DisplayState
*ds
, int csock
, int skipauth
)
3264 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
3266 vnc_connect(vs
, csock
, skipauth
, 0);