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"
35 #define VNC_REFRESH_INTERVAL_BASE 30
36 #define VNC_REFRESH_INTERVAL_INC 50
37 #define VNC_REFRESH_INTERVAL_MAX 2000
39 #include "vnc_keysym.h"
42 #define count_bits(c, v) { \
43 for (c = 0; v; v >>= 1) \
49 static VncDisplay
*vnc_display
; /* needed for info vnc */
50 static DisplayChangeListener
*dcl
;
52 static int vnc_cursor_define(VncState
*vs
);
54 static char *addr_to_string(const char *format
,
55 struct sockaddr_storage
*sa
,
58 char host
[NI_MAXHOST
];
59 char serv
[NI_MAXSERV
];
63 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
66 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
67 VNC_DEBUG("Cannot resolve address %d: %s\n",
68 err
, gai_strerror(err
));
72 /* Enough for the existing format + the 2 vars we're
74 addrlen
= strlen(format
) + strlen(host
) + strlen(serv
);
75 addr
= qemu_malloc(addrlen
+ 1);
76 snprintf(addr
, addrlen
, format
, host
, serv
);
83 char *vnc_socket_local_addr(const char *format
, int fd
) {
84 struct sockaddr_storage sa
;
88 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
91 return addr_to_string(format
, &sa
, salen
);
94 char *vnc_socket_remote_addr(const char *format
, int fd
) {
95 struct sockaddr_storage sa
;
99 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
102 return addr_to_string(format
, &sa
, salen
);
105 static int put_addr_qdict(QDict
*qdict
, struct sockaddr_storage
*sa
,
108 char host
[NI_MAXHOST
];
109 char serv
[NI_MAXSERV
];
112 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
115 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
116 VNC_DEBUG("Cannot resolve address %d: %s\n",
117 err
, gai_strerror(err
));
121 qdict_put(qdict
, "host", qstring_from_str(host
));
122 qdict_put(qdict
, "service", qstring_from_str(serv
));
123 qdict_put(qdict
, "family",qstring_from_str(inet_strfamily(sa
->ss_family
)));
128 static int vnc_server_addr_put(QDict
*qdict
, int fd
)
130 struct sockaddr_storage sa
;
134 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
138 return put_addr_qdict(qdict
, &sa
, salen
);
141 static int vnc_qdict_remote_addr(QDict
*qdict
, int fd
)
143 struct sockaddr_storage sa
;
147 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
151 return put_addr_qdict(qdict
, &sa
, salen
);
154 static const char *vnc_auth_name(VncDisplay
*vd
) {
156 case VNC_AUTH_INVALID
:
172 case VNC_AUTH_VENCRYPT
:
173 #ifdef CONFIG_VNC_TLS
174 switch (vd
->subauth
) {
175 case VNC_AUTH_VENCRYPT_PLAIN
:
176 return "vencrypt+plain";
177 case VNC_AUTH_VENCRYPT_TLSNONE
:
178 return "vencrypt+tls+none";
179 case VNC_AUTH_VENCRYPT_TLSVNC
:
180 return "vencrypt+tls+vnc";
181 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
182 return "vencrypt+tls+plain";
183 case VNC_AUTH_VENCRYPT_X509NONE
:
184 return "vencrypt+x509+none";
185 case VNC_AUTH_VENCRYPT_X509VNC
:
186 return "vencrypt+x509+vnc";
187 case VNC_AUTH_VENCRYPT_X509PLAIN
:
188 return "vencrypt+x509+plain";
189 case VNC_AUTH_VENCRYPT_TLSSASL
:
190 return "vencrypt+tls+sasl";
191 case VNC_AUTH_VENCRYPT_X509SASL
:
192 return "vencrypt+x509+sasl";
205 static int vnc_server_info_put(QDict
*qdict
)
207 if (vnc_server_addr_put(qdict
, vnc_display
->lsock
) < 0) {
211 qdict_put(qdict
, "auth", qstring_from_str(vnc_auth_name(vnc_display
)));
215 static void vnc_client_cache_auth(VncState
*client
)
223 qdict
= qobject_to_qdict(client
->info
);
225 #ifdef CONFIG_VNC_TLS
226 if (client
->tls
.session
&&
228 qdict_put(qdict
, "x509_dname", qstring_from_str(client
->tls
.dname
));
231 #ifdef CONFIG_VNC_SASL
232 if (client
->sasl
.conn
&&
233 client
->sasl
.username
) {
234 qdict_put(qdict
, "sasl_username",
235 qstring_from_str(client
->sasl
.username
));
240 static void vnc_client_cache_addr(VncState
*client
)
245 if (vnc_qdict_remote_addr(qdict
, client
->csock
) < 0) {
247 /* XXX: how to report the error? */
251 client
->info
= QOBJECT(qdict
);
254 static void vnc_qmp_event(VncState
*vs
, MonitorEvent event
)
263 server
= qdict_new();
264 if (vnc_server_info_put(server
) < 0) {
269 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
270 vs
->info
, QOBJECT(server
));
272 monitor_protocol_event(event
, data
);
274 qobject_incref(vs
->info
);
275 qobject_decref(data
);
278 static void info_vnc_iter(QObject
*obj
, void *opaque
)
281 Monitor
*mon
= opaque
;
283 client
= qobject_to_qdict(obj
);
284 monitor_printf(mon
, "Client:\n");
285 monitor_printf(mon
, " address: %s:%s\n",
286 qdict_get_str(client
, "host"),
287 qdict_get_str(client
, "service"));
289 #ifdef CONFIG_VNC_TLS
290 monitor_printf(mon
, " x509_dname: %s\n",
291 qdict_haskey(client
, "x509_dname") ?
292 qdict_get_str(client
, "x509_dname") : "none");
294 #ifdef CONFIG_VNC_SASL
295 monitor_printf(mon
, " username: %s\n",
296 qdict_haskey(client
, "sasl_username") ?
297 qdict_get_str(client
, "sasl_username") : "none");
301 void do_info_vnc_print(Monitor
*mon
, const QObject
*data
)
306 server
= qobject_to_qdict(data
);
307 if (qdict_get_bool(server
, "enabled") == 0) {
308 monitor_printf(mon
, "Server: disabled\n");
312 monitor_printf(mon
, "Server:\n");
313 monitor_printf(mon
, " address: %s:%s\n",
314 qdict_get_str(server
, "host"),
315 qdict_get_str(server
, "service"));
316 monitor_printf(mon
, " auth: %s\n", qdict_get_str(server
, "auth"));
318 clients
= qdict_get_qlist(server
, "clients");
319 if (qlist_empty(clients
)) {
320 monitor_printf(mon
, "Client: none\n");
322 qlist_iter(clients
, info_vnc_iter
, mon
);
326 void do_info_vnc(Monitor
*mon
, QObject
**ret_data
)
328 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
329 *ret_data
= qobject_from_jsonf("{ 'enabled': false }");
335 QTAILQ_FOREACH(client
, &vnc_display
->clients
, next
) {
337 /* incref so that it's not freed by upper layers */
338 qobject_incref(client
->info
);
339 qlist_append_obj(clist
, client
->info
);
343 *ret_data
= qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
345 assert(*ret_data
!= NULL
);
347 if (vnc_server_info_put(qobject_to_qdict(*ret_data
)) < 0) {
348 qobject_decref(*ret_data
);
355 1) Get the queue working for IO.
356 2) there is some weirdness when using the -S option (the screen is grey
357 and not totally invalidated
358 3) resolutions > 1024
361 static int vnc_update_client(VncState
*vs
, int has_dirty
);
362 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
);
363 static void vnc_disconnect_start(VncState
*vs
);
364 static void vnc_disconnect_finish(VncState
*vs
);
365 static void vnc_init_timer(VncDisplay
*vd
);
366 static void vnc_remove_timer(VncDisplay
*vd
);
368 static void vnc_colordepth(VncState
*vs
);
369 static void framebuffer_update_request(VncState
*vs
, int incremental
,
370 int x_position
, int y_position
,
372 static void vnc_refresh(void *opaque
);
373 static int vnc_refresh_server_surface(VncDisplay
*vd
);
375 static inline void vnc_set_bit(uint32_t *d
, int k
)
377 d
[k
>> 5] |= 1 << (k
& 0x1f);
380 static inline void vnc_clear_bit(uint32_t *d
, int k
)
382 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
385 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
395 d
[j
++] = (1 << n
) - 1;
400 static inline int vnc_get_bit(const uint32_t *d
, int k
)
402 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
405 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
409 for(i
= 0; i
< nb_words
; i
++) {
410 if ((d1
[i
] & d2
[i
]) != 0)
416 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
419 VncDisplay
*vd
= ds
->opaque
;
420 struct VncSurface
*s
= &vd
->guest
;
424 /* round x down to ensure the loop only spans one 16-pixel block per,
425 iteration. otherwise, if (x % 16) != 0, the last iteration may span
426 two 16-pixel blocks but we only mark the first as dirty
431 x
= MIN(x
, s
->ds
->width
);
432 y
= MIN(y
, s
->ds
->height
);
433 w
= MIN(x
+ w
, s
->ds
->width
) - x
;
434 h
= MIN(h
, s
->ds
->height
);
437 for (i
= 0; i
< w
; i
+= 16)
438 vnc_set_bit(s
->dirty
[y
], (x
+ i
) / 16);
441 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
444 vnc_write_u16(vs
, x
);
445 vnc_write_u16(vs
, y
);
446 vnc_write_u16(vs
, w
);
447 vnc_write_u16(vs
, h
);
449 vnc_write_s32(vs
, encoding
);
452 void buffer_reserve(Buffer
*buffer
, size_t len
)
454 if ((buffer
->capacity
- buffer
->offset
) < len
) {
455 buffer
->capacity
+= (len
+ 1024);
456 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
457 if (buffer
->buffer
== NULL
) {
458 fprintf(stderr
, "vnc: out of memory\n");
464 int buffer_empty(Buffer
*buffer
)
466 return buffer
->offset
== 0;
469 uint8_t *buffer_end(Buffer
*buffer
)
471 return buffer
->buffer
+ buffer
->offset
;
474 void buffer_reset(Buffer
*buffer
)
479 void buffer_free(Buffer
*buffer
)
481 qemu_free(buffer
->buffer
);
483 buffer
->capacity
= 0;
484 buffer
->buffer
= NULL
;
487 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
489 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
490 buffer
->offset
+= len
;
493 static void vnc_desktop_resize(VncState
*vs
)
495 DisplayState
*ds
= vs
->ds
;
497 if (vs
->csock
== -1 || !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
500 if (vs
->client_width
== ds_get_width(ds
) &&
501 vs
->client_height
== ds_get_height(ds
)) {
504 vs
->client_width
= ds_get_width(ds
);
505 vs
->client_height
= ds_get_height(ds
);
507 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
509 vnc_write_u16(vs
, 1); /* number of rects */
510 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
511 VNC_ENCODING_DESKTOPRESIZE
);
512 vnc_unlock_output(vs
);
516 #ifdef CONFIG_VNC_THREAD
517 static void vnc_abort_display_jobs(VncDisplay
*vd
)
521 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
524 vnc_unlock_output(vs
);
526 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
529 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
532 vnc_unlock_output(vs
);
536 static void vnc_abort_display_jobs(VncDisplay
*vd
)
541 static void vnc_dpy_resize(DisplayState
*ds
)
543 VncDisplay
*vd
= ds
->opaque
;
546 vnc_abort_display_jobs(vd
);
550 vd
->server
= qemu_mallocz(sizeof(*vd
->server
));
551 if (vd
->server
->data
)
552 qemu_free(vd
->server
->data
);
553 *(vd
->server
) = *(ds
->surface
);
554 vd
->server
->data
= qemu_mallocz(vd
->server
->linesize
*
559 vd
->guest
.ds
= qemu_mallocz(sizeof(*vd
->guest
.ds
));
560 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
561 console_color_init(ds
);
562 *(vd
->guest
.ds
) = *(ds
->surface
);
563 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
565 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
567 vnc_desktop_resize(vs
);
568 if (vs
->vd
->cursor
) {
569 vnc_cursor_define(vs
);
571 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
576 static void vnc_write_pixels_copy(VncState
*vs
, struct PixelFormat
*pf
,
577 void *pixels
, int size
)
579 vnc_write(vs
, pixels
, size
);
582 /* slowest but generic code. */
583 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
586 VncDisplay
*vd
= vs
->vd
;
588 r
= ((((v
& vd
->server
->pf
.rmask
) >> vd
->server
->pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
589 vd
->server
->pf
.rbits
);
590 g
= ((((v
& vd
->server
->pf
.gmask
) >> vd
->server
->pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
591 vd
->server
->pf
.gbits
);
592 b
= ((((v
& vd
->server
->pf
.bmask
) >> vd
->server
->pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
593 vd
->server
->pf
.bbits
);
594 v
= (r
<< vs
->clientds
.pf
.rshift
) |
595 (g
<< vs
->clientds
.pf
.gshift
) |
596 (b
<< vs
->clientds
.pf
.bshift
);
597 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
602 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
612 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
627 static void vnc_write_pixels_generic(VncState
*vs
, struct PixelFormat
*pf
,
628 void *pixels1
, int size
)
632 if (pf
->bytes_per_pixel
== 4) {
633 uint32_t *pixels
= pixels1
;
636 for(i
= 0; i
< n
; i
++) {
637 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
638 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
640 } else if (pf
->bytes_per_pixel
== 2) {
641 uint16_t *pixels
= pixels1
;
644 for(i
= 0; i
< n
; i
++) {
645 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
646 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
648 } else if (pf
->bytes_per_pixel
== 1) {
649 uint8_t *pixels
= pixels1
;
652 for(i
= 0; i
< n
; i
++) {
653 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
654 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
657 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
661 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
665 VncDisplay
*vd
= vs
->vd
;
667 row
= vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
668 for (i
= 0; i
< h
; i
++) {
669 vs
->write_pixels(vs
, &vd
->server
->pf
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
670 row
+= ds_get_linesize(vs
->ds
);
675 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
679 switch(vs
->vnc_encoding
) {
680 case VNC_ENCODING_ZLIB
:
681 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
683 case VNC_ENCODING_HEXTILE
:
684 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
685 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
687 case VNC_ENCODING_TIGHT
:
688 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
690 case VNC_ENCODING_TIGHT_PNG
:
691 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
694 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
695 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
701 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
703 /* send bitblit op to the vnc client */
705 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
707 vnc_write_u16(vs
, 1); /* number of rects */
708 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
709 vnc_write_u16(vs
, src_x
);
710 vnc_write_u16(vs
, src_y
);
711 vnc_unlock_output(vs
);
715 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
717 VncDisplay
*vd
= ds
->opaque
;
721 int i
,x
,y
,pitch
,depth
,inc
,w_lim
,s
;
724 vnc_refresh_server_surface(vd
);
725 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
726 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
727 vs
->force_update
= 1;
728 vnc_update_client_sync(vs
, 1);
729 /* vs might be free()ed here */
733 /* do bitblit op on the local surface too */
734 pitch
= ds_get_linesize(vd
->ds
);
735 depth
= ds_get_bytes_per_pixel(vd
->ds
);
736 src_row
= vd
->server
->data
+ pitch
* src_y
+ depth
* src_x
;
737 dst_row
= vd
->server
->data
+ pitch
* dst_y
+ depth
* dst_x
;
742 src_row
+= pitch
* (h
-1);
743 dst_row
+= pitch
* (h
-1);
748 w_lim
= w
- (16 - (dst_x
% 16));
752 w_lim
= w
- (w_lim
% 16);
753 for (i
= 0; i
< h
; i
++) {
754 for (x
= 0; x
<= w_lim
;
755 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
757 if ((s
= w
- w_lim
) == 0)
760 s
= (16 - (dst_x
% 16));
765 cmp_bytes
= s
* depth
;
766 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
768 memmove(dst_row
, src_row
, cmp_bytes
);
769 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
770 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
771 vnc_set_bit(vs
->dirty
[y
], ((x
+ dst_x
) / 16));
775 src_row
+= pitch
- w
* depth
;
776 dst_row
+= pitch
- w
* depth
;
780 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
781 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
782 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
787 static void vnc_mouse_set(int x
, int y
, int visible
)
789 /* can we ask the client(s) to move the pointer ??? */
792 static int vnc_cursor_define(VncState
*vs
)
794 QEMUCursor
*c
= vs
->vd
->cursor
;
795 PixelFormat pf
= qemu_default_pixelformat(32);
798 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
800 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
801 vnc_write_u8(vs
, 0); /* padding */
802 vnc_write_u16(vs
, 1); /* # of rects */
803 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
804 VNC_ENCODING_RICH_CURSOR
);
805 isize
= c
->width
* c
->height
* vs
->clientds
.pf
.bytes_per_pixel
;
806 vnc_write_pixels_generic(vs
, &pf
, c
->data
, isize
);
807 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
808 vnc_unlock_output(vs
);
814 static void vnc_dpy_cursor_define(QEMUCursor
*c
)
816 VncDisplay
*vd
= vnc_display
;
819 cursor_put(vd
->cursor
);
820 qemu_free(vd
->cursor_mask
);
823 cursor_get(vd
->cursor
);
824 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
825 vd
->cursor_mask
= qemu_mallocz(vd
->cursor_msize
);
826 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
828 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
829 vnc_cursor_define(vs
);
833 static int find_and_clear_dirty_height(struct VncState
*vs
,
834 int y
, int last_x
, int x
)
837 VncDisplay
*vd
= vs
->vd
;
839 for (h
= 1; h
< (vd
->server
->height
- y
); h
++) {
841 if (!vnc_get_bit(vs
->dirty
[y
+ h
], last_x
))
843 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
844 vnc_clear_bit(vs
->dirty
[y
+ h
], tmp_x
);
850 #ifdef CONFIG_VNC_THREAD
851 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
853 int ret
= vnc_update_client(vs
, has_dirty
);
858 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
860 return vnc_update_client(vs
, has_dirty
);
864 static int vnc_update_client(VncState
*vs
, int has_dirty
)
866 if (vs
->need_update
&& vs
->csock
!= -1) {
867 VncDisplay
*vd
= vs
->vd
;
874 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
875 /* kernel send buffers are full -> drop frames to throttle */
878 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
882 * Send screen updates to the vnc client using the server
883 * surface and server dirty map. guest surface updates
884 * happening in parallel don't disturb us, the next pass will
885 * send them to the client.
887 job
= vnc_job_new(vs
);
889 width
= MIN(vd
->server
->width
, vs
->client_width
);
890 height
= MIN(vd
->server
->height
, vs
->client_height
);
892 for (y
= 0; y
< height
; y
++) {
895 for (x
= 0; x
< width
/ 16; x
++) {
896 if (vnc_get_bit(vs
->dirty
[y
], x
)) {
900 vnc_clear_bit(vs
->dirty
[y
], x
);
903 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
905 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
906 (x
- last_x
) * 16, h
);
912 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
913 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
914 (x
- last_x
) * 16, h
);
919 vs
->force_update
= 0;
924 vnc_disconnect_finish(vs
);
930 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
932 VncState
*vs
= opaque
;
935 case AUD_CNOTIFY_DISABLE
:
937 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
938 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
939 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
940 vnc_unlock_output(vs
);
944 case AUD_CNOTIFY_ENABLE
:
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_BEGIN
);
949 vnc_unlock_output(vs
);
955 static void audio_capture_destroy(void *opaque
)
959 static void audio_capture(void *opaque
, void *buf
, int size
)
961 VncState
*vs
= opaque
;
964 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
965 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
966 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
967 vnc_write_u32(vs
, size
);
968 vnc_write(vs
, buf
, size
);
969 vnc_unlock_output(vs
);
973 static void audio_add(VncState
*vs
)
975 struct audio_capture_ops ops
;
978 monitor_printf(default_mon
, "audio already running\n");
982 ops
.notify
= audio_capture_notify
;
983 ops
.destroy
= audio_capture_destroy
;
984 ops
.capture
= audio_capture
;
986 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
987 if (!vs
->audio_cap
) {
988 monitor_printf(default_mon
, "Failed to add audio capture\n");
992 static void audio_del(VncState
*vs
)
995 AUD_del_capture(vs
->audio_cap
, vs
);
996 vs
->audio_cap
= NULL
;
1000 static void vnc_disconnect_start(VncState
*vs
)
1002 if (vs
->csock
== -1)
1004 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
1005 closesocket(vs
->csock
);
1009 static void vnc_disconnect_finish(VncState
*vs
)
1011 vnc_jobs_join(vs
); /* Wait encoding jobs */
1013 vnc_lock_output(vs
);
1014 vnc_qmp_event(vs
, QEVENT_VNC_DISCONNECTED
);
1016 buffer_free(&vs
->input
);
1017 buffer_free(&vs
->output
);
1019 qobject_decref(vs
->info
);
1022 vnc_tight_clear(vs
);
1024 #ifdef CONFIG_VNC_TLS
1025 vnc_tls_client_cleanup(vs
);
1026 #endif /* CONFIG_VNC_TLS */
1027 #ifdef CONFIG_VNC_SASL
1028 vnc_sasl_client_cleanup(vs
);
1029 #endif /* CONFIG_VNC_SASL */
1032 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1034 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1038 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1039 vnc_remove_timer(vs
->vd
);
1040 if (vs
->vd
->lock_key_sync
)
1041 qemu_remove_led_event_handler(vs
->led
);
1042 vnc_unlock_output(vs
);
1044 #ifdef CONFIG_VNC_THREAD
1045 qemu_mutex_destroy(&vs
->output_mutex
);
1050 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
1052 if (ret
== 0 || ret
== -1) {
1054 switch (last_errno
) {
1058 case WSAEWOULDBLOCK
:
1066 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1067 ret
, ret
< 0 ? last_errno
: 0);
1068 vnc_disconnect_start(vs
);
1076 void vnc_client_error(VncState
*vs
)
1078 VNC_DEBUG("Closing down client sock: protocol error\n");
1079 vnc_disconnect_start(vs
);
1084 * Called to write a chunk of data to the client socket. The data may
1085 * be the raw data, or may have already been encoded by SASL.
1086 * The data will be written either straight onto the socket, or
1087 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1089 * NB, it is theoretically possible to have 2 layers of encryption,
1090 * both SASL, and this TLS layer. It is highly unlikely in practice
1091 * though, since SASL encryption will typically be a no-op if TLS
1094 * Returns the number of bytes written, which may be less than
1095 * the requested 'datalen' if the socket would block. Returns
1096 * -1 on error, and disconnects the client socket.
1098 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1101 #ifdef CONFIG_VNC_TLS
1102 if (vs
->tls
.session
) {
1103 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
1105 if (ret
== GNUTLS_E_AGAIN
)
1112 #endif /* CONFIG_VNC_TLS */
1113 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
1114 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1115 return vnc_client_io_error(vs
, ret
, socket_error());
1120 * Called to write buffered data to the client socket, when not
1121 * using any SASL SSF encryption layers. Will write as much data
1122 * as possible without blocking. If all buffered data is written,
1123 * will switch the FD poll() handler back to read monitoring.
1125 * Returns the number of bytes written, which may be less than
1126 * the buffered output data if the socket would block. Returns
1127 * -1 on error, and disconnects the client socket.
1129 static long vnc_client_write_plain(VncState
*vs
)
1133 #ifdef CONFIG_VNC_SASL
1134 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1135 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1136 vs
->sasl
.waitWriteSSF
);
1138 if (vs
->sasl
.conn
&&
1140 vs
->sasl
.waitWriteSSF
) {
1141 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1143 vs
->sasl
.waitWriteSSF
-= ret
;
1145 #endif /* CONFIG_VNC_SASL */
1146 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1150 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1151 vs
->output
.offset
-= ret
;
1153 if (vs
->output
.offset
== 0) {
1154 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1162 * First function called whenever there is data to be written to
1163 * the client socket. Will delegate actual work according to whether
1164 * SASL SSF layers are enabled (thus requiring encryption calls)
1166 static void vnc_client_write_locked(void *opaque
)
1168 VncState
*vs
= opaque
;
1170 #ifdef CONFIG_VNC_SASL
1171 if (vs
->sasl
.conn
&&
1173 !vs
->sasl
.waitWriteSSF
) {
1174 vnc_client_write_sasl(vs
);
1176 #endif /* CONFIG_VNC_SASL */
1177 vnc_client_write_plain(vs
);
1180 void vnc_client_write(void *opaque
)
1182 VncState
*vs
= opaque
;
1184 vnc_lock_output(vs
);
1185 if (vs
->output
.offset
) {
1186 vnc_client_write_locked(opaque
);
1187 } else if (vs
->csock
!= -1) {
1188 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1190 vnc_unlock_output(vs
);
1193 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1195 vs
->read_handler
= func
;
1196 vs
->read_handler_expect
= expecting
;
1201 * Called to read a chunk of data from the client socket. The data may
1202 * be the raw data, or may need to be further decoded by SASL.
1203 * The data will be read either straight from to the socket, or
1204 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1206 * NB, it is theoretically possible to have 2 layers of encryption,
1207 * both SASL, and this TLS layer. It is highly unlikely in practice
1208 * though, since SASL encryption will typically be a no-op if TLS
1211 * Returns the number of bytes read, which may be less than
1212 * the requested 'datalen' if the socket would block. Returns
1213 * -1 on error, and disconnects the client socket.
1215 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1218 #ifdef CONFIG_VNC_TLS
1219 if (vs
->tls
.session
) {
1220 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1222 if (ret
== GNUTLS_E_AGAIN
)
1229 #endif /* CONFIG_VNC_TLS */
1230 ret
= recv(vs
->csock
, (void *)data
, datalen
, 0);
1231 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1232 return vnc_client_io_error(vs
, ret
, socket_error());
1237 * Called to read data from the client socket to the input buffer,
1238 * when not using any SASL SSF encryption layers. Will read as much
1239 * data as possible without blocking.
1241 * Returns the number of bytes read. Returns -1 on error, and
1242 * disconnects the client socket.
1244 static long vnc_client_read_plain(VncState
*vs
)
1247 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1248 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1249 buffer_reserve(&vs
->input
, 4096);
1250 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1253 vs
->input
.offset
+= ret
;
1259 * First function called whenever there is more data to be read from
1260 * the client socket. Will delegate actual work according to whether
1261 * SASL SSF layers are enabled (thus requiring decryption calls)
1263 void vnc_client_read(void *opaque
)
1265 VncState
*vs
= opaque
;
1268 #ifdef CONFIG_VNC_SASL
1269 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1270 ret
= vnc_client_read_sasl(vs
);
1272 #endif /* CONFIG_VNC_SASL */
1273 ret
= vnc_client_read_plain(vs
);
1275 if (vs
->csock
== -1)
1276 vnc_disconnect_finish(vs
);
1280 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1281 size_t len
= vs
->read_handler_expect
;
1284 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1285 if (vs
->csock
== -1) {
1286 vnc_disconnect_finish(vs
);
1291 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1292 vs
->input
.offset
-= len
;
1294 vs
->read_handler_expect
= ret
;
1299 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1301 buffer_reserve(&vs
->output
, len
);
1303 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1304 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1307 buffer_append(&vs
->output
, data
, len
);
1310 void vnc_write_s32(VncState
*vs
, int32_t value
)
1312 vnc_write_u32(vs
, *(uint32_t *)&value
);
1315 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1319 buf
[0] = (value
>> 24) & 0xFF;
1320 buf
[1] = (value
>> 16) & 0xFF;
1321 buf
[2] = (value
>> 8) & 0xFF;
1322 buf
[3] = value
& 0xFF;
1324 vnc_write(vs
, buf
, 4);
1327 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1331 buf
[0] = (value
>> 8) & 0xFF;
1332 buf
[1] = value
& 0xFF;
1334 vnc_write(vs
, buf
, 2);
1337 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1339 vnc_write(vs
, (char *)&value
, 1);
1342 void vnc_flush(VncState
*vs
)
1344 vnc_lock_output(vs
);
1345 if (vs
->csock
!= -1 && vs
->output
.offset
) {
1346 vnc_client_write_locked(vs
);
1348 vnc_unlock_output(vs
);
1351 uint8_t read_u8(uint8_t *data
, size_t offset
)
1353 return data
[offset
];
1356 uint16_t read_u16(uint8_t *data
, size_t offset
)
1358 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1361 int32_t read_s32(uint8_t *data
, size_t offset
)
1363 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1364 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1367 uint32_t read_u32(uint8_t *data
, size_t offset
)
1369 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1370 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1373 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1377 static void check_pointer_type_change(Notifier
*notifier
)
1379 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1380 int absolute
= kbd_mouse_is_absolute();
1382 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1383 vnc_lock_output(vs
);
1384 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1385 vnc_write_u8(vs
, 0);
1386 vnc_write_u16(vs
, 1);
1387 vnc_framebuffer_update(vs
, absolute
, 0,
1388 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1389 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1390 vnc_unlock_output(vs
);
1393 vs
->absolute
= absolute
;
1396 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1401 if (button_mask
& 0x01)
1402 buttons
|= MOUSE_EVENT_LBUTTON
;
1403 if (button_mask
& 0x02)
1404 buttons
|= MOUSE_EVENT_MBUTTON
;
1405 if (button_mask
& 0x04)
1406 buttons
|= MOUSE_EVENT_RBUTTON
;
1407 if (button_mask
& 0x08)
1409 if (button_mask
& 0x10)
1413 kbd_mouse_event(ds_get_width(vs
->ds
) > 1 ?
1414 x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1) : 0x4000,
1415 ds_get_height(vs
->ds
) > 1 ?
1416 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1) : 0x4000,
1418 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1422 kbd_mouse_event(x
, y
, dz
, buttons
);
1424 if (vs
->last_x
!= -1)
1425 kbd_mouse_event(x
- vs
->last_x
,
1433 static void reset_keys(VncState
*vs
)
1436 for(i
= 0; i
< 256; i
++) {
1437 if (vs
->modifiers_state
[i
]) {
1438 if (i
& SCANCODE_GREY
)
1439 kbd_put_keycode(SCANCODE_EMUL0
);
1440 kbd_put_keycode(i
| SCANCODE_UP
);
1441 vs
->modifiers_state
[i
] = 0;
1446 static void press_key(VncState
*vs
, int keysym
)
1448 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1449 if (keycode
& SCANCODE_GREY
)
1450 kbd_put_keycode(SCANCODE_EMUL0
);
1451 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1452 if (keycode
& SCANCODE_GREY
)
1453 kbd_put_keycode(SCANCODE_EMUL0
);
1454 kbd_put_keycode(keycode
| SCANCODE_UP
);
1457 static void kbd_leds(void *opaque
, int ledstate
)
1459 VncState
*vs
= opaque
;
1462 caps
= ledstate
& QEMU_CAPS_LOCK_LED
? 1 : 0;
1463 num
= ledstate
& QEMU_NUM_LOCK_LED
? 1 : 0;
1465 if (vs
->modifiers_state
[0x3a] != caps
) {
1466 vs
->modifiers_state
[0x3a] = caps
;
1468 if (vs
->modifiers_state
[0x45] != num
) {
1469 vs
->modifiers_state
[0x45] = num
;
1473 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1475 /* QEMU console switch */
1477 case 0x2a: /* Left Shift */
1478 case 0x36: /* Right Shift */
1479 case 0x1d: /* Left CTRL */
1480 case 0x9d: /* Right CTRL */
1481 case 0x38: /* Left ALT */
1482 case 0xb8: /* Right ALT */
1484 vs
->modifiers_state
[keycode
] = 1;
1486 vs
->modifiers_state
[keycode
] = 0;
1488 case 0x02 ... 0x0a: /* '1' to '9' keys */
1489 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1490 /* Reset the modifiers sent to the current console */
1492 console_select(keycode
- 0x02);
1496 case 0x3a: /* CapsLock */
1497 case 0x45: /* NumLock */
1499 vs
->modifiers_state
[keycode
] ^= 1;
1503 if (vs
->vd
->lock_key_sync
&&
1504 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1505 /* If the numlock state needs to change then simulate an additional
1506 keypress before sending this one. This will happen if the user
1507 toggles numlock away from the VNC window.
1509 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1510 if (!vs
->modifiers_state
[0x45]) {
1511 vs
->modifiers_state
[0x45] = 1;
1512 press_key(vs
, 0xff7f);
1515 if (vs
->modifiers_state
[0x45]) {
1516 vs
->modifiers_state
[0x45] = 0;
1517 press_key(vs
, 0xff7f);
1522 if (vs
->vd
->lock_key_sync
&&
1523 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1524 /* If the capslock state needs to change then simulate an additional
1525 keypress before sending this one. This will happen if the user
1526 toggles capslock away from the VNC window.
1528 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1529 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1530 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1532 if (uppercase
== shift
) {
1533 vs
->modifiers_state
[0x3a] = 0;
1534 press_key(vs
, 0xffe5);
1537 if (uppercase
!= shift
) {
1538 vs
->modifiers_state
[0x3a] = 1;
1539 press_key(vs
, 0xffe5);
1544 if (is_graphic_console()) {
1545 if (keycode
& SCANCODE_GREY
)
1546 kbd_put_keycode(SCANCODE_EMUL0
);
1548 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1550 kbd_put_keycode(keycode
| SCANCODE_UP
);
1552 /* QEMU console emulation */
1554 int numlock
= vs
->modifiers_state
[0x45];
1556 case 0x2a: /* Left Shift */
1557 case 0x36: /* Right Shift */
1558 case 0x1d: /* Left CTRL */
1559 case 0x9d: /* Right CTRL */
1560 case 0x38: /* Left ALT */
1561 case 0xb8: /* Right ALT */
1564 kbd_put_keysym(QEMU_KEY_UP
);
1567 kbd_put_keysym(QEMU_KEY_DOWN
);
1570 kbd_put_keysym(QEMU_KEY_LEFT
);
1573 kbd_put_keysym(QEMU_KEY_RIGHT
);
1576 kbd_put_keysym(QEMU_KEY_DELETE
);
1579 kbd_put_keysym(QEMU_KEY_HOME
);
1582 kbd_put_keysym(QEMU_KEY_END
);
1585 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1588 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1592 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1595 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1598 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1601 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1604 kbd_put_keysym('5');
1607 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1610 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1613 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1616 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1619 kbd_put_keysym('0');
1622 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1626 kbd_put_keysym('/');
1629 kbd_put_keysym('*');
1632 kbd_put_keysym('-');
1635 kbd_put_keysym('+');
1638 kbd_put_keysym('\n');
1642 kbd_put_keysym(sym
);
1649 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1654 if (lsym
>= 'A' && lsym
<= 'Z' && is_graphic_console()) {
1655 lsym
= lsym
- 'A' + 'a';
1658 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1659 do_key_event(vs
, down
, keycode
, sym
);
1662 static void ext_key_event(VncState
*vs
, int down
,
1663 uint32_t sym
, uint16_t keycode
)
1665 /* if the user specifies a keyboard layout, always use it */
1666 if (keyboard_layout
)
1667 key_event(vs
, down
, sym
);
1669 do_key_event(vs
, down
, keycode
, sym
);
1672 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1673 int x_position
, int y_position
,
1676 if (y_position
> ds_get_height(vs
->ds
))
1677 y_position
= ds_get_height(vs
->ds
);
1678 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1679 h
= ds_get_height(vs
->ds
) - y_position
;
1682 vs
->need_update
= 1;
1684 vs
->force_update
= 1;
1685 for (i
= 0; i
< h
; i
++) {
1686 vnc_set_bits(vs
->dirty
[y_position
+ i
],
1687 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1692 static void send_ext_key_event_ack(VncState
*vs
)
1694 vnc_lock_output(vs
);
1695 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1696 vnc_write_u8(vs
, 0);
1697 vnc_write_u16(vs
, 1);
1698 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1699 VNC_ENCODING_EXT_KEY_EVENT
);
1700 vnc_unlock_output(vs
);
1704 static void send_ext_audio_ack(VncState
*vs
)
1706 vnc_lock_output(vs
);
1707 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1708 vnc_write_u8(vs
, 0);
1709 vnc_write_u16(vs
, 1);
1710 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1711 VNC_ENCODING_AUDIO
);
1712 vnc_unlock_output(vs
);
1716 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1719 unsigned int enc
= 0;
1722 vs
->vnc_encoding
= 0;
1723 vs
->tight
.compression
= 9;
1724 vs
->tight
.quality
= -1; /* Lossless by default */
1728 * Start from the end because the encodings are sent in order of preference.
1729 * This way the prefered encoding (first encoding defined in the array)
1730 * will be set at the end of the loop.
1732 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1735 case VNC_ENCODING_RAW
:
1736 vs
->vnc_encoding
= enc
;
1738 case VNC_ENCODING_COPYRECT
:
1739 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1741 case VNC_ENCODING_HEXTILE
:
1742 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1743 vs
->vnc_encoding
= enc
;
1745 case VNC_ENCODING_TIGHT
:
1746 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
1747 vs
->vnc_encoding
= enc
;
1749 case VNC_ENCODING_TIGHT_PNG
:
1750 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
1751 vs
->vnc_encoding
= enc
;
1753 case VNC_ENCODING_ZLIB
:
1754 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1755 vs
->vnc_encoding
= enc
;
1757 case VNC_ENCODING_DESKTOPRESIZE
:
1758 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1760 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1761 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1763 case VNC_ENCODING_RICH_CURSOR
:
1764 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
1766 case VNC_ENCODING_EXT_KEY_EVENT
:
1767 send_ext_key_event_ack(vs
);
1769 case VNC_ENCODING_AUDIO
:
1770 send_ext_audio_ack(vs
);
1772 case VNC_ENCODING_WMVi
:
1773 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1775 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1776 vs
->tight
.compression
= (enc
& 0x0F);
1778 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1779 vs
->tight
.quality
= (enc
& 0x0F);
1782 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1786 vnc_desktop_resize(vs
);
1787 check_pointer_type_change(&vs
->mouse_mode_notifier
);
1790 static void set_pixel_conversion(VncState
*vs
)
1792 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1793 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1794 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1795 vs
->write_pixels
= vnc_write_pixels_copy
;
1796 vnc_hextile_set_pixel_conversion(vs
, 0);
1798 vs
->write_pixels
= vnc_write_pixels_generic
;
1799 vnc_hextile_set_pixel_conversion(vs
, 1);
1803 static void set_pixel_format(VncState
*vs
,
1804 int bits_per_pixel
, int depth
,
1805 int big_endian_flag
, int true_color_flag
,
1806 int red_max
, int green_max
, int blue_max
,
1807 int red_shift
, int green_shift
, int blue_shift
)
1809 if (!true_color_flag
) {
1810 vnc_client_error(vs
);
1814 vs
->clientds
= *(vs
->vd
->guest
.ds
);
1815 vs
->clientds
.pf
.rmax
= red_max
;
1816 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1817 vs
->clientds
.pf
.rshift
= red_shift
;
1818 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1819 vs
->clientds
.pf
.gmax
= green_max
;
1820 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1821 vs
->clientds
.pf
.gshift
= green_shift
;
1822 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1823 vs
->clientds
.pf
.bmax
= blue_max
;
1824 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1825 vs
->clientds
.pf
.bshift
= blue_shift
;
1826 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1827 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1828 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1829 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1830 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1832 set_pixel_conversion(vs
);
1834 vga_hw_invalidate();
1838 static void pixel_format_message (VncState
*vs
) {
1839 char pad
[3] = { 0, 0, 0 };
1841 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1842 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1844 #ifdef HOST_WORDS_BIGENDIAN
1845 vnc_write_u8(vs
, 1); /* big-endian-flag */
1847 vnc_write_u8(vs
, 0); /* big-endian-flag */
1849 vnc_write_u8(vs
, 1); /* true-color-flag */
1850 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1851 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1852 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1853 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1854 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1855 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1857 vnc_hextile_set_pixel_conversion(vs
, 0);
1859 vs
->clientds
= *(vs
->ds
->surface
);
1860 vs
->clientds
.flags
&= ~QEMU_ALLOCATED_FLAG
;
1861 vs
->write_pixels
= vnc_write_pixels_copy
;
1863 vnc_write(vs
, pad
, 3); /* padding */
1866 static void vnc_dpy_setdata(DisplayState
*ds
)
1868 /* We don't have to do anything */
1871 static void vnc_colordepth(VncState
*vs
)
1873 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1874 /* Sending a WMVi message to notify the client*/
1875 vnc_lock_output(vs
);
1876 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1877 vnc_write_u8(vs
, 0);
1878 vnc_write_u16(vs
, 1); /* number of rects */
1879 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1880 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1881 pixel_format_message(vs
);
1882 vnc_unlock_output(vs
);
1885 set_pixel_conversion(vs
);
1889 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1893 VncDisplay
*vd
= vs
->vd
;
1896 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
1897 if (!qemu_timer_expired(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
))
1898 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
);
1902 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
1906 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1907 read_u8(data
, 6), read_u8(data
, 7),
1908 read_u16(data
, 8), read_u16(data
, 10),
1909 read_u16(data
, 12), read_u8(data
, 14),
1910 read_u8(data
, 15), read_u8(data
, 16));
1912 case VNC_MSG_CLIENT_SET_ENCODINGS
:
1917 limit
= read_u16(data
, 2);
1919 return 4 + (limit
* 4);
1921 limit
= read_u16(data
, 2);
1923 for (i
= 0; i
< limit
; i
++) {
1924 int32_t val
= read_s32(data
, 4 + (i
* 4));
1925 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1928 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1930 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
1934 framebuffer_update_request(vs
,
1935 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1936 read_u16(data
, 6), read_u16(data
, 8));
1938 case VNC_MSG_CLIENT_KEY_EVENT
:
1942 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1944 case VNC_MSG_CLIENT_POINTER_EVENT
:
1948 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1950 case VNC_MSG_CLIENT_CUT_TEXT
:
1955 uint32_t dlen
= read_u32(data
, 4);
1960 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1962 case VNC_MSG_CLIENT_QEMU
:
1966 switch (read_u8(data
, 1)) {
1967 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
1971 ext_key_event(vs
, read_u16(data
, 2),
1972 read_u32(data
, 4), read_u32(data
, 8));
1974 case VNC_MSG_CLIENT_QEMU_AUDIO
:
1978 switch (read_u16 (data
, 2)) {
1979 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
1982 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
1985 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
1988 switch (read_u8(data
, 4)) {
1989 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1990 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1991 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1992 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1993 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1994 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1996 printf("Invalid audio format %d\n", read_u8(data
, 4));
1997 vnc_client_error(vs
);
2000 vs
->as
.nchannels
= read_u8(data
, 5);
2001 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2002 printf("Invalid audio channel coount %d\n",
2004 vnc_client_error(vs
);
2007 vs
->as
.freq
= read_u32(data
, 6);
2010 printf ("Invalid audio message %d\n", read_u8(data
, 4));
2011 vnc_client_error(vs
);
2017 printf("Msg: %d\n", read_u16(data
, 0));
2018 vnc_client_error(vs
);
2023 printf("Msg: %d\n", data
[0]);
2024 vnc_client_error(vs
);
2028 vnc_read_when(vs
, protocol_client_msg
, 1);
2032 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2037 vs
->client_width
= ds_get_width(vs
->ds
);
2038 vs
->client_height
= ds_get_height(vs
->ds
);
2039 vnc_write_u16(vs
, vs
->client_width
);
2040 vnc_write_u16(vs
, vs
->client_height
);
2042 pixel_format_message(vs
);
2045 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2047 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2049 vnc_write_u32(vs
, size
);
2050 vnc_write(vs
, buf
, size
);
2053 vnc_client_cache_auth(vs
);
2054 vnc_qmp_event(vs
, QEVENT_VNC_INITIALIZED
);
2056 vnc_read_when(vs
, protocol_client_msg
, 1);
2061 void start_client_init(VncState
*vs
)
2063 vnc_read_when(vs
, protocol_client_init
, 1);
2066 static void make_challenge(VncState
*vs
)
2070 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2072 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2073 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2076 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2078 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2080 unsigned char key
[8];
2082 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
2083 VNC_DEBUG("No password configured on server");
2084 vnc_write_u32(vs
, 1); /* Reject auth */
2085 if (vs
->minor
>= 8) {
2086 static const char err
[] = "Authentication failed";
2087 vnc_write_u32(vs
, sizeof(err
));
2088 vnc_write(vs
, err
, sizeof(err
));
2091 vnc_client_error(vs
);
2095 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2097 /* Calculate the expected challenge response */
2098 pwlen
= strlen(vs
->vd
->password
);
2099 for (i
=0; i
<sizeof(key
); i
++)
2100 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2102 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
2103 des(response
+j
, response
+j
);
2105 /* Compare expected vs actual challenge response */
2106 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2107 VNC_DEBUG("Client challenge reponse did not match\n");
2108 vnc_write_u32(vs
, 1); /* Reject auth */
2109 if (vs
->minor
>= 8) {
2110 static const char err
[] = "Authentication failed";
2111 vnc_write_u32(vs
, sizeof(err
));
2112 vnc_write(vs
, err
, sizeof(err
));
2115 vnc_client_error(vs
);
2117 VNC_DEBUG("Accepting VNC challenge response\n");
2118 vnc_write_u32(vs
, 0); /* Accept auth */
2121 start_client_init(vs
);
2126 void start_auth_vnc(VncState
*vs
)
2129 /* Send client a 'random' challenge */
2130 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2133 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2137 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2139 /* We only advertise 1 auth scheme at a time, so client
2140 * must pick the one we sent. Verify this */
2141 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
2142 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
2143 vnc_write_u32(vs
, 1);
2144 if (vs
->minor
>= 8) {
2145 static const char err
[] = "Authentication failed";
2146 vnc_write_u32(vs
, sizeof(err
));
2147 vnc_write(vs
, err
, sizeof(err
));
2149 vnc_client_error(vs
);
2150 } else { /* Accept requested auth */
2151 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2152 switch (vs
->vd
->auth
) {
2154 VNC_DEBUG("Accept auth none\n");
2155 if (vs
->minor
>= 8) {
2156 vnc_write_u32(vs
, 0); /* Accept auth completion */
2159 start_client_init(vs
);
2163 VNC_DEBUG("Start VNC auth\n");
2167 #ifdef CONFIG_VNC_TLS
2168 case VNC_AUTH_VENCRYPT
:
2169 VNC_DEBUG("Accept VeNCrypt auth\n");;
2170 start_auth_vencrypt(vs
);
2172 #endif /* CONFIG_VNC_TLS */
2174 #ifdef CONFIG_VNC_SASL
2176 VNC_DEBUG("Accept SASL auth\n");
2177 start_auth_sasl(vs
);
2179 #endif /* CONFIG_VNC_SASL */
2181 default: /* Should not be possible, but just in case */
2182 VNC_DEBUG("Reject auth %d server code bug\n", vs
->vd
->auth
);
2183 vnc_write_u8(vs
, 1);
2184 if (vs
->minor
>= 8) {
2185 static const char err
[] = "Authentication failed";
2186 vnc_write_u32(vs
, sizeof(err
));
2187 vnc_write(vs
, err
, sizeof(err
));
2189 vnc_client_error(vs
);
2195 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2199 memcpy(local
, version
, 12);
2202 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2203 VNC_DEBUG("Malformed protocol version %s\n", local
);
2204 vnc_client_error(vs
);
2207 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2208 if (vs
->major
!= 3 ||
2214 VNC_DEBUG("Unsupported client version\n");
2215 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2217 vnc_client_error(vs
);
2220 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2221 * as equivalent to v3.3 by servers
2223 if (vs
->minor
== 4 || vs
->minor
== 5)
2226 if (vs
->minor
== 3) {
2227 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
2228 VNC_DEBUG("Tell client auth none\n");
2229 vnc_write_u32(vs
, vs
->vd
->auth
);
2231 start_client_init(vs
);
2232 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
2233 VNC_DEBUG("Tell client VNC auth\n");
2234 vnc_write_u32(vs
, vs
->vd
->auth
);
2238 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
2239 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2241 vnc_client_error(vs
);
2244 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
2245 vnc_write_u8(vs
, 1); /* num auth */
2246 vnc_write_u8(vs
, vs
->vd
->auth
);
2247 vnc_read_when(vs
, protocol_client_auth
, 1);
2254 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2258 uint8_t *server_row
;
2260 uint32_t width_mask
[VNC_DIRTY_WORDS
];
2265 * Walk through the guest dirty map.
2266 * Check and copy modified bits from guest to server surface.
2267 * Update server dirty map.
2269 vnc_set_bits(width_mask
, (ds_get_width(vd
->ds
) / 16), VNC_DIRTY_WORDS
);
2270 cmp_bytes
= 16 * ds_get_bytes_per_pixel(vd
->ds
);
2271 guest_row
= vd
->guest
.ds
->data
;
2272 server_row
= vd
->server
->data
;
2273 for (y
= 0; y
< vd
->guest
.ds
->height
; y
++) {
2274 if (vnc_and_bits(vd
->guest
.dirty
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
2277 uint8_t *server_ptr
;
2279 guest_ptr
= guest_row
;
2280 server_ptr
= server_row
;
2282 for (x
= 0; x
< vd
->guest
.ds
->width
;
2283 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2284 if (!vnc_get_bit(vd
->guest
.dirty
[y
], (x
/ 16)))
2286 vnc_clear_bit(vd
->guest
.dirty
[y
], (x
/ 16));
2287 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2289 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2290 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2291 vnc_set_bit(vs
->dirty
[y
], (x
/ 16));
2296 guest_row
+= ds_get_linesize(vd
->ds
);
2297 server_row
+= ds_get_linesize(vd
->ds
);
2302 static void vnc_refresh(void *opaque
)
2304 VncDisplay
*vd
= opaque
;
2306 int has_dirty
, rects
= 0;
2310 if (vnc_trylock_display(vd
)) {
2311 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2312 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) +
2313 vd
->timer_interval
);
2317 has_dirty
= vnc_refresh_server_surface(vd
);
2318 vnc_unlock_display(vd
);
2320 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2321 rects
+= vnc_update_client(vs
, has_dirty
);
2322 /* vs might be free()ed here */
2325 /* vd->timer could be NULL now if the last client disconnected,
2326 * in this case don't update the timer */
2327 if (vd
->timer
== NULL
)
2330 if (has_dirty
&& rects
) {
2331 vd
->timer_interval
/= 2;
2332 if (vd
->timer_interval
< VNC_REFRESH_INTERVAL_BASE
)
2333 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2335 vd
->timer_interval
+= VNC_REFRESH_INTERVAL_INC
;
2336 if (vd
->timer_interval
> VNC_REFRESH_INTERVAL_MAX
)
2337 vd
->timer_interval
= VNC_REFRESH_INTERVAL_MAX
;
2339 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
);
2342 static void vnc_init_timer(VncDisplay
*vd
)
2344 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2345 if (vd
->timer
== NULL
&& !QTAILQ_EMPTY(&vd
->clients
)) {
2346 vd
->timer
= qemu_new_timer(rt_clock
, vnc_refresh
, vd
);
2351 static void vnc_remove_timer(VncDisplay
*vd
)
2353 if (vd
->timer
!= NULL
&& QTAILQ_EMPTY(&vd
->clients
)) {
2354 qemu_del_timer(vd
->timer
);
2355 qemu_free_timer(vd
->timer
);
2360 static void vnc_connect(VncDisplay
*vd
, int csock
)
2362 VncState
*vs
= qemu_mallocz(sizeof(VncState
));
2365 VNC_DEBUG("New client on socket %d\n", csock
);
2367 socket_set_nonblock(vs
->csock
);
2368 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2370 vnc_client_cache_addr(vs
);
2371 vnc_qmp_event(vs
, QEVENT_VNC_CONNECTED
);
2378 vs
->as
.freq
= 44100;
2379 vs
->as
.nchannels
= 2;
2380 vs
->as
.fmt
= AUD_FMT_S16
;
2381 vs
->as
.endianness
= 0;
2383 #ifdef CONFIG_VNC_THREAD
2384 qemu_mutex_init(&vs
->output_mutex
);
2387 QTAILQ_INSERT_HEAD(&vd
->clients
, vs
, next
);
2391 vnc_write(vs
, "RFB 003.008\n", 12);
2393 vnc_read_when(vs
, protocol_version
, 12);
2395 if (vs
->vd
->lock_key_sync
)
2396 vs
->led
= qemu_add_led_event_handler(kbd_leds
, vs
);
2398 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
2399 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
2403 /* vs might be free()ed here */
2406 static void vnc_listen_read(void *opaque
)
2408 VncDisplay
*vs
= opaque
;
2409 struct sockaddr_in addr
;
2410 socklen_t addrlen
= sizeof(addr
);
2415 int csock
= qemu_accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2417 vnc_connect(vs
, csock
);
2421 void vnc_display_init(DisplayState
*ds
)
2423 VncDisplay
*vs
= qemu_mallocz(sizeof(*vs
));
2425 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2434 QTAILQ_INIT(&vs
->clients
);
2436 if (keyboard_layout
)
2437 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2439 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2441 if (!vs
->kbd_layout
)
2444 #ifdef CONFIG_VNC_THREAD
2445 qemu_mutex_init(&vs
->mutex
);
2446 vnc_start_worker_thread();
2449 dcl
->dpy_copy
= vnc_dpy_copy
;
2450 dcl
->dpy_update
= vnc_dpy_update
;
2451 dcl
->dpy_resize
= vnc_dpy_resize
;
2452 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2453 register_displaychangelistener(ds
, dcl
);
2454 ds
->mouse_set
= vnc_mouse_set
;
2455 ds
->cursor_define
= vnc_dpy_cursor_define
;
2459 void vnc_display_close(DisplayState
*ds
)
2461 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2466 qemu_free(vs
->display
);
2469 if (vs
->lsock
!= -1) {
2470 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2474 vs
->auth
= VNC_AUTH_INVALID
;
2475 #ifdef CONFIG_VNC_TLS
2476 vs
->subauth
= VNC_AUTH_INVALID
;
2477 vs
->tls
.x509verify
= 0;
2481 int vnc_display_password(DisplayState
*ds
, const char *password
)
2483 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2490 qemu_free(vs
->password
);
2491 vs
->password
= NULL
;
2493 if (password
&& password
[0]) {
2494 if (!(vs
->password
= qemu_strdup(password
)))
2496 if (vs
->auth
== VNC_AUTH_NONE
) {
2497 vs
->auth
= VNC_AUTH_VNC
;
2500 vs
->auth
= VNC_AUTH_NONE
;
2506 char *vnc_display_local_addr(DisplayState
*ds
)
2508 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2510 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2513 int vnc_display_open(DisplayState
*ds
, const char *display
)
2515 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2516 const char *options
;
2519 #ifdef CONFIG_VNC_TLS
2520 int tls
= 0, x509
= 0;
2522 #ifdef CONFIG_VNC_SASL
2527 int lock_key_sync
= 1;
2531 vnc_display_close(ds
);
2532 if (strcmp(display
, "none") == 0)
2535 if (!(vs
->display
= strdup(display
)))
2539 while ((options
= strchr(options
, ','))) {
2541 if (strncmp(options
, "password", 8) == 0) {
2542 password
= 1; /* Require password auth */
2543 } else if (strncmp(options
, "reverse", 7) == 0) {
2545 } else if (strncmp(options
, "no-lock-key-sync", 9) == 0) {
2547 #ifdef CONFIG_VNC_SASL
2548 } else if (strncmp(options
, "sasl", 4) == 0) {
2549 sasl
= 1; /* Require SASL auth */
2551 #ifdef CONFIG_VNC_TLS
2552 } else if (strncmp(options
, "tls", 3) == 0) {
2553 tls
= 1; /* Require TLS */
2554 } else if (strncmp(options
, "x509", 4) == 0) {
2556 x509
= 1; /* Require x509 certificates */
2557 if (strncmp(options
, "x509verify", 10) == 0)
2558 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2560 /* Now check for 'x509=/some/path' postfix
2561 * and use that to setup x509 certificate/key paths */
2562 start
= strchr(options
, '=');
2563 end
= strchr(options
, ',');
2564 if (start
&& (!end
|| (start
< end
))) {
2565 int len
= end
? end
-(start
+1) : strlen(start
+1);
2566 char *path
= qemu_strndup(start
+ 1, len
);
2568 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2569 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2570 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2572 qemu_free(vs
->display
);
2578 fprintf(stderr
, "No certificate path provided\n");
2579 qemu_free(vs
->display
);
2584 } else if (strncmp(options
, "acl", 3) == 0) {
2586 } else if (strncmp(options
, "lossy", 5) == 0) {
2591 #ifdef CONFIG_VNC_TLS
2592 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2593 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2594 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2599 #ifdef CONFIG_VNC_SASL
2601 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2602 fprintf(stderr
, "Failed to create username ACL\n");
2609 * Combinations we support here:
2611 * - no-auth (clear text, no auth)
2612 * - password (clear text, weak auth)
2613 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2614 * - tls (encrypt, weak anonymous creds, no auth)
2615 * - tls + password (encrypt, weak anonymous creds, weak auth)
2616 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2617 * - tls + x509 (encrypt, good x509 creds, no auth)
2618 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2619 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2621 * NB1. TLS is a stackable auth scheme.
2622 * NB2. the x509 schemes have option to validate a client cert dname
2625 #ifdef CONFIG_VNC_TLS
2627 vs
->auth
= VNC_AUTH_VENCRYPT
;
2629 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2630 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2632 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2633 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2636 #endif /* CONFIG_VNC_TLS */
2637 VNC_DEBUG("Initializing VNC server with password auth\n");
2638 vs
->auth
= VNC_AUTH_VNC
;
2639 #ifdef CONFIG_VNC_TLS
2640 vs
->subauth
= VNC_AUTH_INVALID
;
2642 #endif /* CONFIG_VNC_TLS */
2643 #ifdef CONFIG_VNC_SASL
2645 #ifdef CONFIG_VNC_TLS
2647 vs
->auth
= VNC_AUTH_VENCRYPT
;
2649 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2650 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
2652 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2653 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
2656 #endif /* CONFIG_VNC_TLS */
2657 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2658 vs
->auth
= VNC_AUTH_SASL
;
2659 #ifdef CONFIG_VNC_TLS
2660 vs
->subauth
= VNC_AUTH_INVALID
;
2662 #endif /* CONFIG_VNC_TLS */
2663 #endif /* CONFIG_VNC_SASL */
2665 #ifdef CONFIG_VNC_TLS
2667 vs
->auth
= VNC_AUTH_VENCRYPT
;
2669 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2670 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2672 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2673 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2677 VNC_DEBUG("Initializing VNC server with no auth\n");
2678 vs
->auth
= VNC_AUTH_NONE
;
2679 #ifdef CONFIG_VNC_TLS
2680 vs
->subauth
= VNC_AUTH_INVALID
;
2685 #ifdef CONFIG_VNC_SASL
2686 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
2687 fprintf(stderr
, "Failed to initialize SASL auth %s",
2688 sasl_errstring(saslErr
, NULL
, NULL
));
2694 vs
->lock_key_sync
= lock_key_sync
;
2697 /* connect to viewer */
2698 if (strncmp(display
, "unix:", 5) == 0)
2699 vs
->lsock
= unix_connect(display
+5);
2701 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2702 if (-1 == vs
->lsock
) {
2707 int csock
= vs
->lsock
;
2709 vnc_connect(vs
, csock
);
2714 /* listen for connects */
2716 dpy
= qemu_malloc(256);
2717 if (strncmp(display
, "unix:", 5) == 0) {
2718 pstrcpy(dpy
, 256, "unix:");
2719 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2721 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2723 if (-1 == vs
->lsock
) {
2731 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);