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
30 #include "qemu_socket.h"
31 #include "qemu-timer.h"
33 #include "qemu-objects.h"
34 #include "qmp-commands.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_disconnect_finish(VncState
*vs
);
424 static void vnc_init_timer(VncDisplay
*vd
);
425 static void vnc_remove_timer(VncDisplay
*vd
);
427 static void vnc_colordepth(VncState
*vs
);
428 static void framebuffer_update_request(VncState
*vs
, int incremental
,
429 int x_position
, int y_position
,
431 static void vnc_refresh(void *opaque
);
432 static int vnc_refresh_server_surface(VncDisplay
*vd
);
434 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
437 VncDisplay
*vd
= ds
->opaque
;
438 struct VncSurface
*s
= &vd
->guest
;
439 int width
= ds_get_width(ds
);
440 int height
= ds_get_height(ds
);
444 /* round x down to ensure the loop only spans one 16-pixel block per,
445 iteration. otherwise, if (x % 16) != 0, the last iteration may span
446 two 16-pixel blocks but we only mark the first as dirty
453 w
= MIN(x
+ w
, width
) - x
;
457 for (i
= 0; i
< w
; i
+= 16)
458 set_bit((x
+ i
) / 16, s
->dirty
[y
]);
461 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
464 vnc_write_u16(vs
, x
);
465 vnc_write_u16(vs
, y
);
466 vnc_write_u16(vs
, w
);
467 vnc_write_u16(vs
, h
);
469 vnc_write_s32(vs
, encoding
);
472 void buffer_reserve(Buffer
*buffer
, size_t len
)
474 if ((buffer
->capacity
- buffer
->offset
) < len
) {
475 buffer
->capacity
+= (len
+ 1024);
476 buffer
->buffer
= g_realloc(buffer
->buffer
, buffer
->capacity
);
477 if (buffer
->buffer
== NULL
) {
478 fprintf(stderr
, "vnc: out of memory\n");
484 static int buffer_empty(Buffer
*buffer
)
486 return buffer
->offset
== 0;
489 static uint8_t *buffer_end(Buffer
*buffer
)
491 return buffer
->buffer
+ buffer
->offset
;
494 void buffer_reset(Buffer
*buffer
)
499 void buffer_free(Buffer
*buffer
)
501 g_free(buffer
->buffer
);
503 buffer
->capacity
= 0;
504 buffer
->buffer
= NULL
;
507 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
509 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
510 buffer
->offset
+= len
;
513 static void vnc_desktop_resize(VncState
*vs
)
515 DisplayState
*ds
= vs
->ds
;
517 if (vs
->csock
== -1 || !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
520 if (vs
->client_width
== ds_get_width(ds
) &&
521 vs
->client_height
== ds_get_height(ds
)) {
524 vs
->client_width
= ds_get_width(ds
);
525 vs
->client_height
= ds_get_height(ds
);
527 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
529 vnc_write_u16(vs
, 1); /* number of rects */
530 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
531 VNC_ENCODING_DESKTOPRESIZE
);
532 vnc_unlock_output(vs
);
536 static void vnc_abort_display_jobs(VncDisplay
*vd
)
540 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
543 vnc_unlock_output(vs
);
545 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
548 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
551 vnc_unlock_output(vs
);
555 int vnc_server_fb_stride(VncDisplay
*vd
)
557 return pixman_image_get_stride(vd
->server
);
560 void *vnc_server_fb_ptr(VncDisplay
*vd
, int x
, int y
)
564 ptr
= (uint8_t *)pixman_image_get_data(vd
->server
);
565 ptr
+= y
* vnc_server_fb_stride(vd
);
566 ptr
+= x
* VNC_SERVER_FB_BYTES
;
570 static void vnc_dpy_resize(DisplayState
*ds
)
572 VncDisplay
*vd
= ds
->opaque
;
575 vnc_abort_display_jobs(vd
);
578 qemu_pixman_image_unref(vd
->server
);
579 vd
->server
= pixman_image_create_bits(VNC_SERVER_FB_FORMAT
,
586 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
587 console_color_init(ds
);
589 qemu_pixman_image_unref(vd
->guest
.fb
);
590 vd
->guest
.fb
= pixman_image_ref(ds
->surface
->image
);
591 vd
->guest
.format
= ds
->surface
->format
;
592 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
594 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
596 vnc_desktop_resize(vs
);
597 if (vs
->vd
->cursor
) {
598 vnc_cursor_define(vs
);
600 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
605 static void vnc_write_pixels_copy(VncState
*vs
,
606 void *pixels
, int size
)
608 vnc_write(vs
, pixels
, size
);
611 /* slowest but generic code. */
612 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
616 #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
617 r
= (((v
& 0x00ff0000) >> 16) << vs
->client_pf
.rbits
) >> 8;
618 g
= (((v
& 0x0000ff00) >> 8) << vs
->client_pf
.gbits
) >> 8;
619 b
= (((v
& 0x000000ff) >> 0) << vs
->client_pf
.bbits
) >> 8;
621 # error need some bits here if you change VNC_SERVER_FB_FORMAT
623 v
= (r
<< vs
->client_pf
.rshift
) |
624 (g
<< vs
->client_pf
.gshift
) |
625 (b
<< vs
->client_pf
.bshift
);
626 switch (vs
->client_pf
.bytes_per_pixel
) {
656 static void vnc_write_pixels_generic(VncState
*vs
,
657 void *pixels1
, int size
)
661 if (VNC_SERVER_FB_BYTES
== 4) {
662 uint32_t *pixels
= pixels1
;
665 for (i
= 0; i
< n
; i
++) {
666 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
667 vnc_write(vs
, buf
, vs
->client_pf
.bytes_per_pixel
);
672 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
676 VncDisplay
*vd
= vs
->vd
;
678 row
= vnc_server_fb_ptr(vd
, x
, y
);
679 for (i
= 0; i
< h
; i
++) {
680 vs
->write_pixels(vs
, row
, w
* VNC_SERVER_FB_BYTES
);
681 row
+= vnc_server_fb_stride(vd
);
686 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
690 switch(vs
->vnc_encoding
) {
691 case VNC_ENCODING_ZLIB
:
692 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
694 case VNC_ENCODING_HEXTILE
:
695 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
696 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
698 case VNC_ENCODING_TIGHT
:
699 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
701 case VNC_ENCODING_TIGHT_PNG
:
702 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
704 case VNC_ENCODING_ZRLE
:
705 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
707 case VNC_ENCODING_ZYWRLE
:
708 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
711 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
712 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
718 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
720 /* send bitblit op to the vnc client */
722 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
724 vnc_write_u16(vs
, 1); /* number of rects */
725 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
726 vnc_write_u16(vs
, src_x
);
727 vnc_write_u16(vs
, src_y
);
728 vnc_unlock_output(vs
);
732 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
734 VncDisplay
*vd
= ds
->opaque
;
738 int i
, x
, y
, pitch
, inc
, w_lim
, s
;
741 vnc_refresh_server_surface(vd
);
742 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
743 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
744 vs
->force_update
= 1;
745 vnc_update_client_sync(vs
, 1);
746 /* vs might be free()ed here */
750 /* do bitblit op on the local surface too */
751 pitch
= vnc_server_fb_stride(vd
);
752 src_row
= vnc_server_fb_ptr(vd
, src_x
, src_y
);
753 dst_row
= vnc_server_fb_ptr(vd
, dst_x
, dst_y
);
758 src_row
+= pitch
* (h
-1);
759 dst_row
+= pitch
* (h
-1);
764 w_lim
= w
- (16 - (dst_x
% 16));
768 w_lim
= w
- (w_lim
% 16);
769 for (i
= 0; i
< h
; i
++) {
770 for (x
= 0; x
<= w_lim
;
771 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
773 if ((s
= w
- w_lim
) == 0)
776 s
= (16 - (dst_x
% 16));
781 cmp_bytes
= s
* VNC_SERVER_FB_BYTES
;
782 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
784 memmove(dst_row
, src_row
, cmp_bytes
);
785 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
786 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
787 set_bit(((x
+ dst_x
) / 16), vs
->dirty
[y
]);
791 src_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
792 dst_row
+= pitch
- w
* VNC_SERVER_FB_BYTES
;
796 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
797 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
798 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
803 static void vnc_mouse_set(DisplayState
*ds
, int x
, int y
, int visible
)
805 /* can we ask the client(s) to move the pointer ??? */
808 static int vnc_cursor_define(VncState
*vs
)
810 QEMUCursor
*c
= vs
->vd
->cursor
;
813 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
815 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
816 vnc_write_u8(vs
, 0); /* padding */
817 vnc_write_u16(vs
, 1); /* # of rects */
818 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
819 VNC_ENCODING_RICH_CURSOR
);
820 isize
= c
->width
* c
->height
* vs
->client_pf
.bytes_per_pixel
;
821 vnc_write_pixels_generic(vs
, c
->data
, isize
);
822 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
823 vnc_unlock_output(vs
);
829 static void vnc_dpy_cursor_define(DisplayState
*ds
, QEMUCursor
*c
)
831 VncDisplay
*vd
= vnc_display
;
834 cursor_put(vd
->cursor
);
835 g_free(vd
->cursor_mask
);
838 cursor_get(vd
->cursor
);
839 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
840 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
841 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
843 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
844 vnc_cursor_define(vs
);
848 static int find_and_clear_dirty_height(struct VncState
*vs
,
849 int y
, int last_x
, int x
, int height
)
853 for (h
= 1; h
< (height
- y
); h
++) {
855 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
858 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++) {
859 clear_bit(tmp_x
, vs
->dirty
[y
+ h
]);
866 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
868 int ret
= vnc_update_client(vs
, has_dirty
);
873 static int vnc_update_client(VncState
*vs
, int has_dirty
)
875 if (vs
->need_update
&& vs
->csock
!= -1) {
876 VncDisplay
*vd
= vs
->vd
;
883 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
884 /* kernel send buffers are full -> drop frames to throttle */
887 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
891 * Send screen updates to the vnc client using the server
892 * surface and server dirty map. guest surface updates
893 * happening in parallel don't disturb us, the next pass will
894 * send them to the client.
896 job
= vnc_job_new(vs
);
898 width
= MIN(pixman_image_get_width(vd
->server
), vs
->client_width
);
899 height
= MIN(pixman_image_get_height(vd
->server
), vs
->client_height
);
901 for (y
= 0; y
< height
; y
++) {
904 for (x
= 0; x
< width
/ 16; x
++) {
905 if (test_and_clear_bit(x
, vs
->dirty
[y
])) {
911 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
,
914 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
915 (x
- last_x
) * 16, h
);
921 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
, height
);
922 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
923 (x
- last_x
) * 16, h
);
928 vs
->force_update
= 0;
933 vnc_disconnect_finish(vs
);
939 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
941 VncState
*vs
= opaque
;
944 case AUD_CNOTIFY_DISABLE
:
946 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
947 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
948 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
949 vnc_unlock_output(vs
);
953 case AUD_CNOTIFY_ENABLE
:
955 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
956 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
957 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
958 vnc_unlock_output(vs
);
964 static void audio_capture_destroy(void *opaque
)
968 static void audio_capture(void *opaque
, void *buf
, int size
)
970 VncState
*vs
= opaque
;
973 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
974 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
975 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
976 vnc_write_u32(vs
, size
);
977 vnc_write(vs
, buf
, size
);
978 vnc_unlock_output(vs
);
982 static void audio_add(VncState
*vs
)
984 struct audio_capture_ops ops
;
987 monitor_printf(default_mon
, "audio already running\n");
991 ops
.notify
= audio_capture_notify
;
992 ops
.destroy
= audio_capture_destroy
;
993 ops
.capture
= audio_capture
;
995 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
996 if (!vs
->audio_cap
) {
997 monitor_printf(default_mon
, "Failed to add audio capture\n");
1001 static void audio_del(VncState
*vs
)
1003 if (vs
->audio_cap
) {
1004 AUD_del_capture(vs
->audio_cap
, vs
);
1005 vs
->audio_cap
= NULL
;
1009 static void vnc_disconnect_start(VncState
*vs
)
1011 if (vs
->csock
== -1)
1013 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1014 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
1015 closesocket(vs
->csock
);
1019 static void vnc_disconnect_finish(VncState
*vs
)
1023 vnc_jobs_join(vs
); /* Wait encoding jobs */
1025 vnc_lock_output(vs
);
1026 vnc_qmp_event(vs
, QEVENT_VNC_DISCONNECTED
);
1028 buffer_free(&vs
->input
);
1029 buffer_free(&vs
->output
);
1031 qobject_decref(vs
->info
);
1034 vnc_tight_clear(vs
);
1037 #ifdef CONFIG_VNC_TLS
1038 vnc_tls_client_cleanup(vs
);
1039 #endif /* CONFIG_VNC_TLS */
1040 #ifdef CONFIG_VNC_SASL
1041 vnc_sasl_client_cleanup(vs
);
1042 #endif /* CONFIG_VNC_SASL */
1044 vnc_release_modifiers(vs
);
1046 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1048 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1052 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1053 vnc_remove_timer(vs
->vd
);
1054 if (vs
->vd
->lock_key_sync
)
1055 qemu_remove_led_event_handler(vs
->led
);
1056 vnc_unlock_output(vs
);
1058 qemu_mutex_destroy(&vs
->output_mutex
);
1059 qemu_bh_delete(vs
->bh
);
1060 buffer_free(&vs
->jobs_buffer
);
1062 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1063 g_free(vs
->lossy_rect
[i
]);
1065 g_free(vs
->lossy_rect
);
1069 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
1071 if (ret
== 0 || ret
== -1) {
1073 switch (last_errno
) {
1077 case WSAEWOULDBLOCK
:
1085 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1086 ret
, ret
< 0 ? last_errno
: 0);
1087 vnc_disconnect_start(vs
);
1095 void vnc_client_error(VncState
*vs
)
1097 VNC_DEBUG("Closing down client sock: protocol error\n");
1098 vnc_disconnect_start(vs
);
1103 * Called to write a chunk of data to the client socket. The data may
1104 * be the raw data, or may have already been encoded by SASL.
1105 * The data will be written either straight onto the socket, or
1106 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1108 * NB, it is theoretically possible to have 2 layers of encryption,
1109 * both SASL, and this TLS layer. It is highly unlikely in practice
1110 * though, since SASL encryption will typically be a no-op if TLS
1113 * Returns the number of bytes written, which may be less than
1114 * the requested 'datalen' if the socket would block. Returns
1115 * -1 on error, and disconnects the client socket.
1117 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1120 #ifdef CONFIG_VNC_TLS
1121 if (vs
->tls
.session
) {
1122 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
1124 if (ret
== GNUTLS_E_AGAIN
)
1131 #endif /* CONFIG_VNC_TLS */
1132 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
1133 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1134 return vnc_client_io_error(vs
, ret
, socket_error());
1139 * Called to write buffered data to the client socket, when not
1140 * using any SASL SSF encryption layers. Will write as much data
1141 * as possible without blocking. If all buffered data is written,
1142 * will switch the FD poll() handler back to read monitoring.
1144 * Returns the number of bytes written, which may be less than
1145 * the buffered output data if the socket would block. Returns
1146 * -1 on error, and disconnects the client socket.
1148 static long vnc_client_write_plain(VncState
*vs
)
1152 #ifdef CONFIG_VNC_SASL
1153 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1154 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1155 vs
->sasl
.waitWriteSSF
);
1157 if (vs
->sasl
.conn
&&
1159 vs
->sasl
.waitWriteSSF
) {
1160 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1162 vs
->sasl
.waitWriteSSF
-= ret
;
1164 #endif /* CONFIG_VNC_SASL */
1165 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1169 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1170 vs
->output
.offset
-= ret
;
1172 if (vs
->output
.offset
== 0) {
1173 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1181 * First function called whenever there is data to be written to
1182 * the client socket. Will delegate actual work according to whether
1183 * SASL SSF layers are enabled (thus requiring encryption calls)
1185 static void vnc_client_write_locked(void *opaque
)
1187 VncState
*vs
= opaque
;
1189 #ifdef CONFIG_VNC_SASL
1190 if (vs
->sasl
.conn
&&
1192 !vs
->sasl
.waitWriteSSF
) {
1193 vnc_client_write_sasl(vs
);
1195 #endif /* CONFIG_VNC_SASL */
1196 vnc_client_write_plain(vs
);
1199 void vnc_client_write(void *opaque
)
1201 VncState
*vs
= opaque
;
1203 vnc_lock_output(vs
);
1204 if (vs
->output
.offset
) {
1205 vnc_client_write_locked(opaque
);
1206 } else if (vs
->csock
!= -1) {
1207 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1209 vnc_unlock_output(vs
);
1212 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1214 vs
->read_handler
= func
;
1215 vs
->read_handler_expect
= expecting
;
1220 * Called to read a chunk of data from the client socket. The data may
1221 * be the raw data, or may need to be further decoded by SASL.
1222 * The data will be read either straight from to the socket, or
1223 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1225 * NB, it is theoretically possible to have 2 layers of encryption,
1226 * both SASL, and this TLS layer. It is highly unlikely in practice
1227 * though, since SASL encryption will typically be a no-op if TLS
1230 * Returns the number of bytes read, which may be less than
1231 * the requested 'datalen' if the socket would block. Returns
1232 * -1 on error, and disconnects the client socket.
1234 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1237 #ifdef CONFIG_VNC_TLS
1238 if (vs
->tls
.session
) {
1239 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1241 if (ret
== GNUTLS_E_AGAIN
)
1248 #endif /* CONFIG_VNC_TLS */
1249 ret
= qemu_recv(vs
->csock
, data
, datalen
, 0);
1250 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1251 return vnc_client_io_error(vs
, ret
, socket_error());
1256 * Called to read data from the client socket to the input buffer,
1257 * when not using any SASL SSF encryption layers. Will read as much
1258 * data as possible without blocking.
1260 * Returns the number of bytes read. Returns -1 on error, and
1261 * disconnects the client socket.
1263 static long vnc_client_read_plain(VncState
*vs
)
1266 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1267 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1268 buffer_reserve(&vs
->input
, 4096);
1269 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1272 vs
->input
.offset
+= ret
;
1276 static void vnc_jobs_bh(void *opaque
)
1278 VncState
*vs
= opaque
;
1280 vnc_jobs_consume_buffer(vs
);
1284 * First function called whenever there is more data to be read from
1285 * the client socket. Will delegate actual work according to whether
1286 * SASL SSF layers are enabled (thus requiring decryption calls)
1288 void vnc_client_read(void *opaque
)
1290 VncState
*vs
= opaque
;
1293 #ifdef CONFIG_VNC_SASL
1294 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1295 ret
= vnc_client_read_sasl(vs
);
1297 #endif /* CONFIG_VNC_SASL */
1298 ret
= vnc_client_read_plain(vs
);
1300 if (vs
->csock
== -1)
1301 vnc_disconnect_finish(vs
);
1305 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1306 size_t len
= vs
->read_handler_expect
;
1309 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1310 if (vs
->csock
== -1) {
1311 vnc_disconnect_finish(vs
);
1316 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1317 vs
->input
.offset
-= len
;
1319 vs
->read_handler_expect
= ret
;
1324 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1326 buffer_reserve(&vs
->output
, len
);
1328 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1329 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1332 buffer_append(&vs
->output
, data
, len
);
1335 void vnc_write_s32(VncState
*vs
, int32_t value
)
1337 vnc_write_u32(vs
, *(uint32_t *)&value
);
1340 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1344 buf
[0] = (value
>> 24) & 0xFF;
1345 buf
[1] = (value
>> 16) & 0xFF;
1346 buf
[2] = (value
>> 8) & 0xFF;
1347 buf
[3] = value
& 0xFF;
1349 vnc_write(vs
, buf
, 4);
1352 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1356 buf
[0] = (value
>> 8) & 0xFF;
1357 buf
[1] = value
& 0xFF;
1359 vnc_write(vs
, buf
, 2);
1362 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1364 vnc_write(vs
, (char *)&value
, 1);
1367 void vnc_flush(VncState
*vs
)
1369 vnc_lock_output(vs
);
1370 if (vs
->csock
!= -1 && vs
->output
.offset
) {
1371 vnc_client_write_locked(vs
);
1373 vnc_unlock_output(vs
);
1376 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1378 return data
[offset
];
1381 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1383 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1386 static int32_t read_s32(uint8_t *data
, size_t offset
)
1388 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1389 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1392 uint32_t read_u32(uint8_t *data
, size_t offset
)
1394 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1395 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1398 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1402 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1404 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1405 int absolute
= kbd_mouse_is_absolute();
1407 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1408 vnc_lock_output(vs
);
1409 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1410 vnc_write_u8(vs
, 0);
1411 vnc_write_u16(vs
, 1);
1412 vnc_framebuffer_update(vs
, absolute
, 0,
1413 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1414 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1415 vnc_unlock_output(vs
);
1418 vs
->absolute
= absolute
;
1421 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1426 if (button_mask
& 0x01)
1427 buttons
|= MOUSE_EVENT_LBUTTON
;
1428 if (button_mask
& 0x02)
1429 buttons
|= MOUSE_EVENT_MBUTTON
;
1430 if (button_mask
& 0x04)
1431 buttons
|= MOUSE_EVENT_RBUTTON
;
1432 if (button_mask
& 0x08)
1434 if (button_mask
& 0x10)
1438 kbd_mouse_event(ds_get_width(vs
->ds
) > 1 ?
1439 x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1) : 0x4000,
1440 ds_get_height(vs
->ds
) > 1 ?
1441 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1) : 0x4000,
1443 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1447 kbd_mouse_event(x
, y
, dz
, buttons
);
1449 if (vs
->last_x
!= -1)
1450 kbd_mouse_event(x
- vs
->last_x
,
1458 static void reset_keys(VncState
*vs
)
1461 for(i
= 0; i
< 256; i
++) {
1462 if (vs
->modifiers_state
[i
]) {
1463 if (i
& SCANCODE_GREY
)
1464 kbd_put_keycode(SCANCODE_EMUL0
);
1465 kbd_put_keycode(i
| SCANCODE_UP
);
1466 vs
->modifiers_state
[i
] = 0;
1471 static void press_key(VncState
*vs
, int keysym
)
1473 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1474 if (keycode
& SCANCODE_GREY
)
1475 kbd_put_keycode(SCANCODE_EMUL0
);
1476 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1477 if (keycode
& SCANCODE_GREY
)
1478 kbd_put_keycode(SCANCODE_EMUL0
);
1479 kbd_put_keycode(keycode
| SCANCODE_UP
);
1482 static void kbd_leds(void *opaque
, int ledstate
)
1484 VncState
*vs
= opaque
;
1487 caps
= ledstate
& QEMU_CAPS_LOCK_LED
? 1 : 0;
1488 num
= ledstate
& QEMU_NUM_LOCK_LED
? 1 : 0;
1490 if (vs
->modifiers_state
[0x3a] != caps
) {
1491 vs
->modifiers_state
[0x3a] = caps
;
1493 if (vs
->modifiers_state
[0x45] != num
) {
1494 vs
->modifiers_state
[0x45] = num
;
1498 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1500 /* QEMU console switch */
1502 case 0x2a: /* Left Shift */
1503 case 0x36: /* Right Shift */
1504 case 0x1d: /* Left CTRL */
1505 case 0x9d: /* Right CTRL */
1506 case 0x38: /* Left ALT */
1507 case 0xb8: /* Right ALT */
1509 vs
->modifiers_state
[keycode
] = 1;
1511 vs
->modifiers_state
[keycode
] = 0;
1513 case 0x02 ... 0x0a: /* '1' to '9' keys */
1514 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1515 /* Reset the modifiers sent to the current console */
1517 console_select(keycode
- 0x02);
1521 case 0x3a: /* CapsLock */
1522 case 0x45: /* NumLock */
1524 vs
->modifiers_state
[keycode
] ^= 1;
1528 if (down
&& vs
->vd
->lock_key_sync
&&
1529 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1530 /* If the numlock state needs to change then simulate an additional
1531 keypress before sending this one. This will happen if the user
1532 toggles numlock away from the VNC window.
1534 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1535 if (!vs
->modifiers_state
[0x45]) {
1536 vs
->modifiers_state
[0x45] = 1;
1537 press_key(vs
, 0xff7f);
1540 if (vs
->modifiers_state
[0x45]) {
1541 vs
->modifiers_state
[0x45] = 0;
1542 press_key(vs
, 0xff7f);
1547 if (down
&& vs
->vd
->lock_key_sync
&&
1548 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1549 /* If the capslock state needs to change then simulate an additional
1550 keypress before sending this one. This will happen if the user
1551 toggles capslock away from the VNC window.
1553 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1554 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1555 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1557 if (uppercase
== shift
) {
1558 vs
->modifiers_state
[0x3a] = 0;
1559 press_key(vs
, 0xffe5);
1562 if (uppercase
!= shift
) {
1563 vs
->modifiers_state
[0x3a] = 1;
1564 press_key(vs
, 0xffe5);
1569 if (is_graphic_console()) {
1570 if (keycode
& SCANCODE_GREY
)
1571 kbd_put_keycode(SCANCODE_EMUL0
);
1573 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1575 kbd_put_keycode(keycode
| SCANCODE_UP
);
1577 bool numlock
= vs
->modifiers_state
[0x45];
1578 bool control
= (vs
->modifiers_state
[0x1d] ||
1579 vs
->modifiers_state
[0x9d]);
1580 /* QEMU console emulation */
1583 case 0x2a: /* Left Shift */
1584 case 0x36: /* Right Shift */
1585 case 0x1d: /* Left CTRL */
1586 case 0x9d: /* Right CTRL */
1587 case 0x38: /* Left ALT */
1588 case 0xb8: /* Right ALT */
1591 kbd_put_keysym(QEMU_KEY_UP
);
1594 kbd_put_keysym(QEMU_KEY_DOWN
);
1597 kbd_put_keysym(QEMU_KEY_LEFT
);
1600 kbd_put_keysym(QEMU_KEY_RIGHT
);
1603 kbd_put_keysym(QEMU_KEY_DELETE
);
1606 kbd_put_keysym(QEMU_KEY_HOME
);
1609 kbd_put_keysym(QEMU_KEY_END
);
1612 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1615 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1619 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1622 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1625 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1628 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1631 kbd_put_keysym('5');
1634 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1637 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1640 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1643 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1646 kbd_put_keysym('0');
1649 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1653 kbd_put_keysym('/');
1656 kbd_put_keysym('*');
1659 kbd_put_keysym('-');
1662 kbd_put_keysym('+');
1665 kbd_put_keysym('\n');
1670 kbd_put_keysym(sym
& 0x1f);
1672 kbd_put_keysym(sym
);
1680 static void vnc_release_modifiers(VncState
*vs
)
1682 static const int keycodes
[] = {
1683 /* shift, control, alt keys, both left & right */
1684 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1688 if (!is_graphic_console()) {
1691 for (i
= 0; i
< ARRAY_SIZE(keycodes
); i
++) {
1692 keycode
= keycodes
[i
];
1693 if (!vs
->modifiers_state
[keycode
]) {
1696 if (keycode
& SCANCODE_GREY
) {
1697 kbd_put_keycode(SCANCODE_EMUL0
);
1699 kbd_put_keycode(keycode
| SCANCODE_UP
);
1703 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1708 if (lsym
>= 'A' && lsym
<= 'Z' && is_graphic_console()) {
1709 lsym
= lsym
- 'A' + 'a';
1712 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1713 do_key_event(vs
, down
, keycode
, sym
);
1716 static void ext_key_event(VncState
*vs
, int down
,
1717 uint32_t sym
, uint16_t keycode
)
1719 /* if the user specifies a keyboard layout, always use it */
1720 if (keyboard_layout
)
1721 key_event(vs
, down
, sym
);
1723 do_key_event(vs
, down
, keycode
, sym
);
1726 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1727 int x_position
, int y_position
,
1731 const size_t width
= ds_get_width(vs
->ds
) / 16;
1733 if (y_position
> ds_get_height(vs
->ds
))
1734 y_position
= ds_get_height(vs
->ds
);
1735 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1736 h
= ds_get_height(vs
->ds
) - y_position
;
1738 vs
->need_update
= 1;
1740 vs
->force_update
= 1;
1741 for (i
= 0; i
< h
; i
++) {
1742 bitmap_set(vs
->dirty
[y_position
+ i
], 0, width
);
1743 bitmap_clear(vs
->dirty
[y_position
+ i
], width
,
1744 VNC_DIRTY_BITS
- width
);
1749 static void send_ext_key_event_ack(VncState
*vs
)
1751 vnc_lock_output(vs
);
1752 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1753 vnc_write_u8(vs
, 0);
1754 vnc_write_u16(vs
, 1);
1755 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1756 VNC_ENCODING_EXT_KEY_EVENT
);
1757 vnc_unlock_output(vs
);
1761 static void send_ext_audio_ack(VncState
*vs
)
1763 vnc_lock_output(vs
);
1764 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1765 vnc_write_u8(vs
, 0);
1766 vnc_write_u16(vs
, 1);
1767 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1768 VNC_ENCODING_AUDIO
);
1769 vnc_unlock_output(vs
);
1773 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1776 unsigned int enc
= 0;
1779 vs
->vnc_encoding
= 0;
1780 vs
->tight
.compression
= 9;
1781 vs
->tight
.quality
= -1; /* Lossless by default */
1785 * Start from the end because the encodings are sent in order of preference.
1786 * This way the preferred encoding (first encoding defined in the array)
1787 * will be set at the end of the loop.
1789 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1792 case VNC_ENCODING_RAW
:
1793 vs
->vnc_encoding
= enc
;
1795 case VNC_ENCODING_COPYRECT
:
1796 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1798 case VNC_ENCODING_HEXTILE
:
1799 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1800 vs
->vnc_encoding
= enc
;
1802 case VNC_ENCODING_TIGHT
:
1803 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
1804 vs
->vnc_encoding
= enc
;
1806 #ifdef CONFIG_VNC_PNG
1807 case VNC_ENCODING_TIGHT_PNG
:
1808 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
1809 vs
->vnc_encoding
= enc
;
1812 case VNC_ENCODING_ZLIB
:
1813 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1814 vs
->vnc_encoding
= enc
;
1816 case VNC_ENCODING_ZRLE
:
1817 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
1818 vs
->vnc_encoding
= enc
;
1820 case VNC_ENCODING_ZYWRLE
:
1821 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
1822 vs
->vnc_encoding
= enc
;
1824 case VNC_ENCODING_DESKTOPRESIZE
:
1825 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1827 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1828 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1830 case VNC_ENCODING_RICH_CURSOR
:
1831 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
1833 case VNC_ENCODING_EXT_KEY_EVENT
:
1834 send_ext_key_event_ack(vs
);
1836 case VNC_ENCODING_AUDIO
:
1837 send_ext_audio_ack(vs
);
1839 case VNC_ENCODING_WMVi
:
1840 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1842 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1843 vs
->tight
.compression
= (enc
& 0x0F);
1845 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1846 if (vs
->vd
->lossy
) {
1847 vs
->tight
.quality
= (enc
& 0x0F);
1851 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1855 vnc_desktop_resize(vs
);
1856 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
1859 static void set_pixel_conversion(VncState
*vs
)
1861 pixman_format_code_t fmt
= qemu_pixman_get_format(&vs
->client_pf
);
1863 if (fmt
== VNC_SERVER_FB_FORMAT
) {
1864 vs
->write_pixels
= vnc_write_pixels_copy
;
1865 vnc_hextile_set_pixel_conversion(vs
, 0);
1867 vs
->write_pixels
= vnc_write_pixels_generic
;
1868 vnc_hextile_set_pixel_conversion(vs
, 1);
1872 static void set_pixel_format(VncState
*vs
,
1873 int bits_per_pixel
, int depth
,
1874 int big_endian_flag
, int true_color_flag
,
1875 int red_max
, int green_max
, int blue_max
,
1876 int red_shift
, int green_shift
, int blue_shift
)
1878 if (!true_color_flag
) {
1879 vnc_client_error(vs
);
1883 vs
->client_pf
.rmax
= red_max
;
1884 vs
->client_pf
.rbits
= hweight_long(red_max
);
1885 vs
->client_pf
.rshift
= red_shift
;
1886 vs
->client_pf
.rmask
= red_max
<< red_shift
;
1887 vs
->client_pf
.gmax
= green_max
;
1888 vs
->client_pf
.gbits
= hweight_long(green_max
);
1889 vs
->client_pf
.gshift
= green_shift
;
1890 vs
->client_pf
.gmask
= green_max
<< green_shift
;
1891 vs
->client_pf
.bmax
= blue_max
;
1892 vs
->client_pf
.bbits
= hweight_long(blue_max
);
1893 vs
->client_pf
.bshift
= blue_shift
;
1894 vs
->client_pf
.bmask
= blue_max
<< blue_shift
;
1895 vs
->client_pf
.bits_per_pixel
= bits_per_pixel
;
1896 vs
->client_pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1897 vs
->client_pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1898 vs
->client_be
= big_endian_flag
;
1900 set_pixel_conversion(vs
);
1902 vga_hw_invalidate();
1906 static void pixel_format_message (VncState
*vs
) {
1907 char pad
[3] = { 0, 0, 0 };
1909 vs
->client_pf
= qemu_default_pixelformat(32);
1911 vnc_write_u8(vs
, vs
->client_pf
.bits_per_pixel
); /* bits-per-pixel */
1912 vnc_write_u8(vs
, vs
->client_pf
.depth
); /* depth */
1914 #ifdef HOST_WORDS_BIGENDIAN
1915 vnc_write_u8(vs
, 1); /* big-endian-flag */
1917 vnc_write_u8(vs
, 0); /* big-endian-flag */
1919 vnc_write_u8(vs
, 1); /* true-color-flag */
1920 vnc_write_u16(vs
, vs
->client_pf
.rmax
); /* red-max */
1921 vnc_write_u16(vs
, vs
->client_pf
.gmax
); /* green-max */
1922 vnc_write_u16(vs
, vs
->client_pf
.bmax
); /* blue-max */
1923 vnc_write_u8(vs
, vs
->client_pf
.rshift
); /* red-shift */
1924 vnc_write_u8(vs
, vs
->client_pf
.gshift
); /* green-shift */
1925 vnc_write_u8(vs
, vs
->client_pf
.bshift
); /* blue-shift */
1926 vnc_write(vs
, pad
, 3); /* padding */
1928 vnc_hextile_set_pixel_conversion(vs
, 0);
1929 vs
->write_pixels
= vnc_write_pixels_copy
;
1932 static void vnc_dpy_setdata(DisplayState
*ds
)
1934 VncDisplay
*vd
= ds
->opaque
;
1936 qemu_pixman_image_unref(vd
->guest
.fb
);
1937 vd
->guest
.fb
= pixman_image_ref(ds
->surface
->image
);
1938 vd
->guest
.format
= ds
->surface
->format
;
1939 vnc_dpy_update(ds
, 0, 0, ds_get_width(ds
), ds_get_height(ds
));
1942 static void vnc_colordepth(VncState
*vs
)
1944 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1945 /* Sending a WMVi message to notify the client*/
1946 vnc_lock_output(vs
);
1947 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1948 vnc_write_u8(vs
, 0);
1949 vnc_write_u16(vs
, 1); /* number of rects */
1950 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1951 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1952 pixel_format_message(vs
);
1953 vnc_unlock_output(vs
);
1956 set_pixel_conversion(vs
);
1960 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1964 VncDisplay
*vd
= vs
->vd
;
1967 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
1968 if (!qemu_timer_expired(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
))
1969 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
1973 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
1977 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1978 read_u8(data
, 6), read_u8(data
, 7),
1979 read_u16(data
, 8), read_u16(data
, 10),
1980 read_u16(data
, 12), read_u8(data
, 14),
1981 read_u8(data
, 15), read_u8(data
, 16));
1983 case VNC_MSG_CLIENT_SET_ENCODINGS
:
1988 limit
= read_u16(data
, 2);
1990 return 4 + (limit
* 4);
1992 limit
= read_u16(data
, 2);
1994 for (i
= 0; i
< limit
; i
++) {
1995 int32_t val
= read_s32(data
, 4 + (i
* 4));
1996 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1999 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2001 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2005 framebuffer_update_request(vs
,
2006 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2007 read_u16(data
, 6), read_u16(data
, 8));
2009 case VNC_MSG_CLIENT_KEY_EVENT
:
2013 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2015 case VNC_MSG_CLIENT_POINTER_EVENT
:
2019 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2021 case VNC_MSG_CLIENT_CUT_TEXT
:
2026 uint32_t dlen
= read_u32(data
, 4);
2031 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2033 case VNC_MSG_CLIENT_QEMU
:
2037 switch (read_u8(data
, 1)) {
2038 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2042 ext_key_event(vs
, read_u16(data
, 2),
2043 read_u32(data
, 4), read_u32(data
, 8));
2045 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2049 switch (read_u16 (data
, 2)) {
2050 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2053 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2056 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2059 switch (read_u8(data
, 4)) {
2060 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2061 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2062 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2063 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2064 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2065 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2067 printf("Invalid audio format %d\n", read_u8(data
, 4));
2068 vnc_client_error(vs
);
2071 vs
->as
.nchannels
= read_u8(data
, 5);
2072 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2073 printf("Invalid audio channel coount %d\n",
2075 vnc_client_error(vs
);
2078 vs
->as
.freq
= read_u32(data
, 6);
2081 printf ("Invalid audio message %d\n", read_u8(data
, 4));
2082 vnc_client_error(vs
);
2088 printf("Msg: %d\n", read_u16(data
, 0));
2089 vnc_client_error(vs
);
2094 printf("Msg: %d\n", data
[0]);
2095 vnc_client_error(vs
);
2099 vnc_read_when(vs
, protocol_client_msg
, 1);
2103 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2109 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2110 switch (vs
->vd
->share_policy
) {
2111 case VNC_SHARE_POLICY_IGNORE
:
2113 * Ignore the shared flag. Nothing to do here.
2115 * Doesn't conform to the rfb spec but is traditional qemu
2116 * behavior, thus left here as option for compatibility
2120 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2122 * Policy: Allow clients ask for exclusive access.
2124 * Implementation: When a client asks for exclusive access,
2125 * disconnect all others. Shared connects are allowed as long
2126 * as no exclusive connection exists.
2128 * This is how the rfb spec suggests to handle the shared flag.
2130 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2132 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2136 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2137 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2140 vnc_disconnect_start(client
);
2143 if (mode
== VNC_SHARE_MODE_SHARED
) {
2144 if (vs
->vd
->num_exclusive
> 0) {
2145 vnc_disconnect_start(vs
);
2150 case VNC_SHARE_POLICY_FORCE_SHARED
:
2152 * Policy: Shared connects only.
2153 * Implementation: Disallow clients asking for exclusive access.
2155 * Useful for shared desktop sessions where you don't want
2156 * someone forgetting to say -shared when running the vnc
2157 * client disconnect everybody else.
2159 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2160 vnc_disconnect_start(vs
);
2165 vnc_set_share_mode(vs
, mode
);
2167 vs
->client_width
= ds_get_width(vs
->ds
);
2168 vs
->client_height
= ds_get_height(vs
->ds
);
2169 vnc_write_u16(vs
, vs
->client_width
);
2170 vnc_write_u16(vs
, vs
->client_height
);
2172 pixel_format_message(vs
);
2175 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2177 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2179 vnc_write_u32(vs
, size
);
2180 vnc_write(vs
, buf
, size
);
2183 vnc_client_cache_auth(vs
);
2184 vnc_qmp_event(vs
, QEVENT_VNC_INITIALIZED
);
2186 vnc_read_when(vs
, protocol_client_msg
, 1);
2191 void start_client_init(VncState
*vs
)
2193 vnc_read_when(vs
, protocol_client_init
, 1);
2196 static void make_challenge(VncState
*vs
)
2200 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2202 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2203 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2206 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2208 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2210 unsigned char key
[8];
2211 time_t now
= time(NULL
);
2213 if (!vs
->vd
->password
) {
2214 VNC_DEBUG("No password configured on server");
2217 if (vs
->vd
->expires
< now
) {
2218 VNC_DEBUG("Password is expired");
2222 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2224 /* Calculate the expected challenge response */
2225 pwlen
= strlen(vs
->vd
->password
);
2226 for (i
=0; i
<sizeof(key
); i
++)
2227 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2229 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
2230 des(response
+j
, response
+j
);
2232 /* Compare expected vs actual challenge response */
2233 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2234 VNC_DEBUG("Client challenge response did not match\n");
2237 VNC_DEBUG("Accepting VNC challenge response\n");
2238 vnc_write_u32(vs
, 0); /* Accept auth */
2241 start_client_init(vs
);
2246 vnc_write_u32(vs
, 1); /* Reject auth */
2247 if (vs
->minor
>= 8) {
2248 static const char err
[] = "Authentication failed";
2249 vnc_write_u32(vs
, sizeof(err
));
2250 vnc_write(vs
, err
, sizeof(err
));
2253 vnc_client_error(vs
);
2257 void start_auth_vnc(VncState
*vs
)
2260 /* Send client a 'random' challenge */
2261 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2264 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2268 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2270 /* We only advertise 1 auth scheme at a time, so client
2271 * must pick the one we sent. Verify this */
2272 if (data
[0] != vs
->auth
) { /* Reject auth */
2273 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
2274 vnc_write_u32(vs
, 1);
2275 if (vs
->minor
>= 8) {
2276 static const char err
[] = "Authentication failed";
2277 vnc_write_u32(vs
, sizeof(err
));
2278 vnc_write(vs
, err
, sizeof(err
));
2280 vnc_client_error(vs
);
2281 } else { /* Accept requested auth */
2282 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2285 VNC_DEBUG("Accept auth none\n");
2286 if (vs
->minor
>= 8) {
2287 vnc_write_u32(vs
, 0); /* Accept auth completion */
2290 start_client_init(vs
);
2294 VNC_DEBUG("Start VNC auth\n");
2298 #ifdef CONFIG_VNC_TLS
2299 case VNC_AUTH_VENCRYPT
:
2300 VNC_DEBUG("Accept VeNCrypt auth\n");
2301 start_auth_vencrypt(vs
);
2303 #endif /* CONFIG_VNC_TLS */
2305 #ifdef CONFIG_VNC_SASL
2307 VNC_DEBUG("Accept SASL auth\n");
2308 start_auth_sasl(vs
);
2310 #endif /* CONFIG_VNC_SASL */
2312 default: /* Should not be possible, but just in case */
2313 VNC_DEBUG("Reject auth %d server code bug\n", vs
->auth
);
2314 vnc_write_u8(vs
, 1);
2315 if (vs
->minor
>= 8) {
2316 static const char err
[] = "Authentication failed";
2317 vnc_write_u32(vs
, sizeof(err
));
2318 vnc_write(vs
, err
, sizeof(err
));
2320 vnc_client_error(vs
);
2326 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2330 memcpy(local
, version
, 12);
2333 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2334 VNC_DEBUG("Malformed protocol version %s\n", local
);
2335 vnc_client_error(vs
);
2338 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2339 if (vs
->major
!= 3 ||
2345 VNC_DEBUG("Unsupported client version\n");
2346 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2348 vnc_client_error(vs
);
2351 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2352 * as equivalent to v3.3 by servers
2354 if (vs
->minor
== 4 || vs
->minor
== 5)
2357 if (vs
->minor
== 3) {
2358 if (vs
->auth
== VNC_AUTH_NONE
) {
2359 VNC_DEBUG("Tell client auth none\n");
2360 vnc_write_u32(vs
, vs
->auth
);
2362 start_client_init(vs
);
2363 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2364 VNC_DEBUG("Tell client VNC auth\n");
2365 vnc_write_u32(vs
, vs
->auth
);
2369 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->auth
);
2370 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2372 vnc_client_error(vs
);
2375 VNC_DEBUG("Telling client we support auth %d\n", vs
->auth
);
2376 vnc_write_u8(vs
, 1); /* num auth */
2377 vnc_write_u8(vs
, vs
->auth
);
2378 vnc_read_when(vs
, protocol_client_auth
, 1);
2385 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2387 struct VncSurface
*vs
= &vd
->guest
;
2389 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2392 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2396 w
= (x
+ w
) / VNC_STAT_RECT
;
2397 h
= (y
+ h
) / VNC_STAT_RECT
;
2401 for (j
= y
; j
<= h
; j
++) {
2402 for (i
= x
; i
<= w
; i
++) {
2403 vs
->lossy_rect
[j
][i
] = 1;
2408 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2411 int sty
= y
/ VNC_STAT_RECT
;
2412 int stx
= x
/ VNC_STAT_RECT
;
2415 y
= y
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2416 x
= x
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2418 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2421 /* kernel send buffers are full -> refresh later */
2422 if (vs
->output
.offset
) {
2426 if (!vs
->lossy_rect
[sty
][stx
]) {
2430 vs
->lossy_rect
[sty
][stx
] = 0;
2431 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2432 bitmap_set(vs
->dirty
[y
+ j
], x
/ 16, VNC_STAT_RECT
/ 16);
2440 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2442 int width
= pixman_image_get_width(vd
->guest
.fb
);
2443 int height
= pixman_image_get_height(vd
->guest
.fb
);
2448 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2449 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2450 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2452 rect
->updated
= false;
2456 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2458 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2461 vd
->guest
.last_freq_check
= *tv
;
2463 for (y
= 0; y
< height
; y
+= VNC_STAT_RECT
) {
2464 for (x
= 0; x
< width
; x
+= VNC_STAT_RECT
) {
2465 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2466 int count
= ARRAY_SIZE(rect
->times
);
2467 struct timeval min
, max
;
2469 if (!timerisset(&rect
->times
[count
- 1])) {
2473 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2474 qemu_timersub(tv
, &max
, &res
);
2476 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2478 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2479 memset(rect
->times
, 0, sizeof (rect
->times
));
2483 min
= rect
->times
[rect
->idx
];
2484 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2485 qemu_timersub(&max
, &min
, &res
);
2487 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2488 rect
->freq
/= count
;
2489 rect
->freq
= 1. / rect
->freq
;
2495 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2501 x
= (x
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2502 y
= (y
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2504 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2505 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2506 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2518 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2522 rect
= vnc_stat_rect(vd
, x
, y
);
2523 if (rect
->updated
) {
2526 rect
->times
[rect
->idx
] = *tv
;
2527 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2528 rect
->updated
= true;
2531 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2533 int width
= pixman_image_get_width(vd
->guest
.fb
);
2534 int height
= pixman_image_get_height(vd
->guest
.fb
);
2537 uint8_t *server_row
;
2541 pixman_image_t
*tmpbuf
= NULL
;
2543 struct timeval tv
= { 0, 0 };
2545 if (!vd
->non_adaptive
) {
2546 gettimeofday(&tv
, NULL
);
2547 has_dirty
= vnc_update_stats(vd
, &tv
);
2551 * Walk through the guest dirty map.
2552 * Check and copy modified bits from guest to server surface.
2553 * Update server dirty map.
2556 if (cmp_bytes
> vnc_server_fb_stride(vd
)) {
2557 cmp_bytes
= vnc_server_fb_stride(vd
);
2559 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2560 int width
= pixman_image_get_width(vd
->server
);
2561 tmpbuf
= qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT
, width
);
2563 guest_row
= (uint8_t *)pixman_image_get_data(vd
->guest
.fb
);
2564 server_row
= (uint8_t *)pixman_image_get_data(vd
->server
);
2565 for (y
= 0; y
< height
; y
++) {
2566 if (!bitmap_empty(vd
->guest
.dirty
[y
], VNC_DIRTY_BITS
)) {
2569 uint8_t *server_ptr
;
2571 if (vd
->guest
.format
!= VNC_SERVER_FB_FORMAT
) {
2572 qemu_pixman_linebuf_fill(tmpbuf
, vd
->guest
.fb
, width
, y
);
2573 guest_ptr
= (uint8_t *)pixman_image_get_data(tmpbuf
);
2575 guest_ptr
= guest_row
;
2577 server_ptr
= server_row
;
2579 for (x
= 0; x
+ 15 < width
;
2580 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2581 if (!test_and_clear_bit((x
/ 16), vd
->guest
.dirty
[y
]))
2583 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2585 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2586 if (!vd
->non_adaptive
)
2587 vnc_rect_updated(vd
, x
, y
, &tv
);
2588 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2589 set_bit((x
/ 16), vs
->dirty
[y
]);
2594 guest_row
+= pixman_image_get_stride(vd
->guest
.fb
);
2595 server_row
+= pixman_image_get_stride(vd
->server
);
2597 qemu_pixman_image_unref(tmpbuf
);
2601 static void vnc_refresh(void *opaque
)
2603 VncDisplay
*vd
= opaque
;
2605 int has_dirty
, rects
= 0;
2609 if (vnc_trylock_display(vd
)) {
2610 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2611 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) +
2612 vd
->timer_interval
);
2616 has_dirty
= vnc_refresh_server_surface(vd
);
2617 vnc_unlock_display(vd
);
2619 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2620 rects
+= vnc_update_client(vs
, has_dirty
);
2621 /* vs might be free()ed here */
2624 /* vd->timer could be NULL now if the last client disconnected,
2625 * in this case don't update the timer */
2626 if (vd
->timer
== NULL
)
2629 if (has_dirty
&& rects
) {
2630 vd
->timer_interval
/= 2;
2631 if (vd
->timer_interval
< VNC_REFRESH_INTERVAL_BASE
)
2632 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2634 vd
->timer_interval
+= VNC_REFRESH_INTERVAL_INC
;
2635 if (vd
->timer_interval
> VNC_REFRESH_INTERVAL_MAX
)
2636 vd
->timer_interval
= VNC_REFRESH_INTERVAL_MAX
;
2638 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
2641 static void vnc_init_timer(VncDisplay
*vd
)
2643 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2644 if (vd
->timer
== NULL
&& !QTAILQ_EMPTY(&vd
->clients
)) {
2645 vd
->timer
= qemu_new_timer_ms(rt_clock
, vnc_refresh
, vd
);
2646 vnc_dpy_resize(vd
->ds
);
2651 static void vnc_remove_timer(VncDisplay
*vd
)
2653 if (vd
->timer
!= NULL
&& QTAILQ_EMPTY(&vd
->clients
)) {
2654 qemu_del_timer(vd
->timer
);
2655 qemu_free_timer(vd
->timer
);
2660 static void vnc_connect(VncDisplay
*vd
, int csock
, int skipauth
)
2662 VncState
*vs
= g_malloc0(sizeof(VncState
));
2668 vs
->auth
= VNC_AUTH_NONE
;
2669 #ifdef CONFIG_VNC_TLS
2670 vs
->subauth
= VNC_AUTH_INVALID
;
2673 vs
->auth
= vd
->auth
;
2674 #ifdef CONFIG_VNC_TLS
2675 vs
->subauth
= vd
->subauth
;
2679 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
2680 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
2681 vs
->lossy_rect
[i
] = g_malloc0(VNC_STAT_COLS
* sizeof (uint8_t));
2684 VNC_DEBUG("New client on socket %d\n", csock
);
2686 socket_set_nonblock(vs
->csock
);
2687 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2689 vnc_client_cache_addr(vs
);
2690 vnc_qmp_event(vs
, QEVENT_VNC_CONNECTED
);
2691 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
2698 vs
->as
.freq
= 44100;
2699 vs
->as
.nchannels
= 2;
2700 vs
->as
.fmt
= AUD_FMT_S16
;
2701 vs
->as
.endianness
= 0;
2703 qemu_mutex_init(&vs
->output_mutex
);
2704 vs
->bh
= qemu_bh_new(vnc_jobs_bh
, vs
);
2706 QTAILQ_INSERT_HEAD(&vd
->clients
, vs
, next
);
2710 vnc_write(vs
, "RFB 003.008\n", 12);
2712 vnc_read_when(vs
, protocol_version
, 12);
2714 if (vs
->vd
->lock_key_sync
)
2715 vs
->led
= qemu_add_led_event_handler(kbd_leds
, vs
);
2717 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
2718 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
2722 /* vs might be free()ed here */
2725 static void vnc_listen_read(void *opaque
)
2727 VncDisplay
*vs
= opaque
;
2728 struct sockaddr_in addr
;
2729 socklen_t addrlen
= sizeof(addr
);
2734 int csock
= qemu_accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2736 vnc_connect(vs
, csock
, 0);
2740 void vnc_display_init(DisplayState
*ds
)
2742 VncDisplay
*vs
= g_malloc0(sizeof(*vs
));
2744 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
2753 QTAILQ_INIT(&vs
->clients
);
2754 vs
->expires
= TIME_MAX
;
2756 if (keyboard_layout
)
2757 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2759 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2761 if (!vs
->kbd_layout
)
2764 qemu_mutex_init(&vs
->mutex
);
2765 vnc_start_worker_thread();
2767 dcl
->dpy_gfx_copy
= vnc_dpy_copy
;
2768 dcl
->dpy_gfx_update
= vnc_dpy_update
;
2769 dcl
->dpy_gfx_resize
= vnc_dpy_resize
;
2770 dcl
->dpy_gfx_setdata
= vnc_dpy_setdata
;
2771 dcl
->dpy_mouse_set
= vnc_mouse_set
;
2772 dcl
->dpy_cursor_define
= vnc_dpy_cursor_define
;
2773 register_displaychangelistener(ds
, dcl
);
2777 static void vnc_display_close(DisplayState
*ds
)
2779 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2784 g_free(vs
->display
);
2787 if (vs
->lsock
!= -1) {
2788 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2792 vs
->auth
= VNC_AUTH_INVALID
;
2793 #ifdef CONFIG_VNC_TLS
2794 vs
->subauth
= VNC_AUTH_INVALID
;
2795 vs
->tls
.x509verify
= 0;
2799 static int vnc_display_disable_login(DisplayState
*ds
)
2801 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2808 g_free(vs
->password
);
2811 vs
->password
= NULL
;
2812 if (vs
->auth
== VNC_AUTH_NONE
) {
2813 vs
->auth
= VNC_AUTH_VNC
;
2819 int vnc_display_password(DisplayState
*ds
, const char *password
)
2821 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2828 /* This is not the intention of this interface but err on the side
2830 return vnc_display_disable_login(ds
);
2834 g_free(vs
->password
);
2835 vs
->password
= NULL
;
2837 vs
->password
= g_strdup(password
);
2838 if (vs
->auth
== VNC_AUTH_NONE
) {
2839 vs
->auth
= VNC_AUTH_VNC
;
2845 int vnc_display_pw_expire(DisplayState
*ds
, time_t expires
)
2847 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2853 vs
->expires
= expires
;
2857 char *vnc_display_local_addr(DisplayState
*ds
)
2859 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2861 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2864 void vnc_display_open(DisplayState
*ds
, const char *display
, Error
**errp
)
2866 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2867 const char *options
;
2870 #ifdef CONFIG_VNC_TLS
2871 int tls
= 0, x509
= 0;
2873 #ifdef CONFIG_VNC_SASL
2877 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2880 int lock_key_sync
= 1;
2883 error_setg(errp
, "VNC display not active");
2886 vnc_display_close(ds
);
2887 if (strcmp(display
, "none") == 0)
2890 vs
->display
= g_strdup(display
);
2891 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
2894 while ((options
= strchr(options
, ','))) {
2896 if (strncmp(options
, "password", 8) == 0) {
2897 if (fips_get_state()) {
2899 "VNC password auth disabled due to FIPS mode, "
2900 "consider using the VeNCrypt or SASL authentication "
2901 "methods as an alternative");
2904 password
= 1; /* Require password auth */
2905 } else if (strncmp(options
, "reverse", 7) == 0) {
2907 } else if (strncmp(options
, "no-lock-key-sync", 16) == 0) {
2909 #ifdef CONFIG_VNC_SASL
2910 } else if (strncmp(options
, "sasl", 4) == 0) {
2911 sasl
= 1; /* Require SASL auth */
2913 #ifdef CONFIG_VNC_TLS
2914 } else if (strncmp(options
, "tls", 3) == 0) {
2915 tls
= 1; /* Require TLS */
2916 } else if (strncmp(options
, "x509", 4) == 0) {
2918 x509
= 1; /* Require x509 certificates */
2919 if (strncmp(options
, "x509verify", 10) == 0)
2920 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2922 /* Now check for 'x509=/some/path' postfix
2923 * and use that to setup x509 certificate/key paths */
2924 start
= strchr(options
, '=');
2925 end
= strchr(options
, ',');
2926 if (start
&& (!end
|| (start
< end
))) {
2927 int len
= end
? end
-(start
+1) : strlen(start
+1);
2928 char *path
= g_strndup(start
+ 1, len
);
2930 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2931 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2932 error_setg(errp
, "Failed to find x509 certificates/keys in %s", path
);
2938 error_setg(errp
, "No certificate path provided");
2942 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2943 } else if (strncmp(options
, "acl", 3) == 0) {
2946 } else if (strncmp(options
, "lossy", 5) == 0) {
2948 } else if (strncmp(options
, "non-adapative", 13) == 0) {
2949 vs
->non_adaptive
= true;
2950 } else if (strncmp(options
, "share=", 6) == 0) {
2951 if (strncmp(options
+6, "ignore", 6) == 0) {
2952 vs
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
2953 } else if (strncmp(options
+6, "allow-exclusive", 15) == 0) {
2954 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
2955 } else if (strncmp(options
+6, "force-shared", 12) == 0) {
2956 vs
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
2958 error_setg(errp
, "unknown vnc share= option");
2964 #ifdef CONFIG_VNC_TLS
2965 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2966 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2967 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2972 #ifdef CONFIG_VNC_SASL
2974 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2975 fprintf(stderr
, "Failed to create username ACL\n");
2982 * Combinations we support here:
2984 * - no-auth (clear text, no auth)
2985 * - password (clear text, weak auth)
2986 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2987 * - tls (encrypt, weak anonymous creds, no auth)
2988 * - tls + password (encrypt, weak anonymous creds, weak auth)
2989 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2990 * - tls + x509 (encrypt, good x509 creds, no auth)
2991 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2992 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2994 * NB1. TLS is a stackable auth scheme.
2995 * NB2. the x509 schemes have option to validate a client cert dname
2998 #ifdef CONFIG_VNC_TLS
3000 vs
->auth
= VNC_AUTH_VENCRYPT
;
3002 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3003 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
3005 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3006 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
3009 #endif /* CONFIG_VNC_TLS */
3010 VNC_DEBUG("Initializing VNC server with password auth\n");
3011 vs
->auth
= VNC_AUTH_VNC
;
3012 #ifdef CONFIG_VNC_TLS
3013 vs
->subauth
= VNC_AUTH_INVALID
;
3015 #endif /* CONFIG_VNC_TLS */
3016 #ifdef CONFIG_VNC_SASL
3018 #ifdef CONFIG_VNC_TLS
3020 vs
->auth
= VNC_AUTH_VENCRYPT
;
3022 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3023 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3025 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3026 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3029 #endif /* CONFIG_VNC_TLS */
3030 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3031 vs
->auth
= VNC_AUTH_SASL
;
3032 #ifdef CONFIG_VNC_TLS
3033 vs
->subauth
= VNC_AUTH_INVALID
;
3035 #endif /* CONFIG_VNC_TLS */
3036 #endif /* CONFIG_VNC_SASL */
3038 #ifdef CONFIG_VNC_TLS
3040 vs
->auth
= VNC_AUTH_VENCRYPT
;
3042 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3043 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3045 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3046 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3050 VNC_DEBUG("Initializing VNC server with no auth\n");
3051 vs
->auth
= VNC_AUTH_NONE
;
3052 #ifdef CONFIG_VNC_TLS
3053 vs
->subauth
= VNC_AUTH_INVALID
;
3058 #ifdef CONFIG_VNC_SASL
3059 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
3060 error_setg(errp
, "Failed to initialize SASL auth: %s",
3061 sasl_errstring(saslErr
, NULL
, NULL
));
3065 vs
->lock_key_sync
= lock_key_sync
;
3068 /* connect to viewer */
3071 if (strncmp(display
, "unix:", 5) == 0) {
3072 csock
= unix_connect(display
+5, errp
);
3074 csock
= inet_connect(display
, errp
);
3079 vnc_connect(vs
, csock
, 0);
3081 /* listen for connects */
3083 dpy
= g_malloc(256);
3084 if (strncmp(display
, "unix:", 5) == 0) {
3085 pstrcpy(dpy
, 256, "unix:");
3086 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5, errp
);
3088 vs
->lsock
= inet_listen(display
, dpy
, 256,
3089 SOCK_STREAM
, 5900, errp
);
3091 if (vs
->lsock
< 0) {
3095 g_free(vs
->display
);
3097 qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);
3102 g_free(vs
->display
);
3106 void vnc_display_add_client(DisplayState
*ds
, int csock
, int skipauth
)
3108 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
3110 vnc_connect(vs
, csock
, skipauth
);