2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu_socket.h"
30 #include "qemu-timer.h"
32 #include "qemu-objects.h"
34 #define VNC_REFRESH_INTERVAL_BASE 30
35 #define VNC_REFRESH_INTERVAL_INC 50
36 #define VNC_REFRESH_INTERVAL_MAX 2000
38 #include "vnc_keysym.h"
41 #define count_bits(c, v) { \
42 for (c = 0; v; v >>= 1) \
49 static VncDisplay
*vnc_display
; /* needed for info vnc */
50 static DisplayChangeListener
*dcl
;
52 static char *addr_to_string(const char *format
,
53 struct sockaddr_storage
*sa
,
56 char host
[NI_MAXHOST
];
57 char serv
[NI_MAXSERV
];
61 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
64 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
65 VNC_DEBUG("Cannot resolve address %d: %s\n",
66 err
, gai_strerror(err
));
70 /* Enough for the existing format + the 2 vars we're
72 addrlen
= strlen(format
) + strlen(host
) + strlen(serv
);
73 addr
= qemu_malloc(addrlen
+ 1);
74 snprintf(addr
, addrlen
, format
, host
, serv
);
81 char *vnc_socket_local_addr(const char *format
, int fd
) {
82 struct sockaddr_storage sa
;
86 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
89 return addr_to_string(format
, &sa
, salen
);
92 char *vnc_socket_remote_addr(const char *format
, int fd
) {
93 struct sockaddr_storage sa
;
97 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
100 return addr_to_string(format
, &sa
, salen
);
103 static int put_addr_qdict(QDict
*qdict
, struct sockaddr_storage
*sa
,
106 char host
[NI_MAXHOST
];
107 char serv
[NI_MAXSERV
];
110 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
113 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
114 VNC_DEBUG("Cannot resolve address %d: %s\n",
115 err
, gai_strerror(err
));
119 qdict_put(qdict
, "host", qstring_from_str(host
));
120 qdict_put(qdict
, "service", qstring_from_str(serv
));
121 qdict_put(qdict
, "family",qstring_from_str(inet_strfamily(sa
->ss_family
)));
126 static int vnc_server_addr_put(QDict
*qdict
, int fd
)
128 struct sockaddr_storage sa
;
132 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
136 return put_addr_qdict(qdict
, &sa
, salen
);
139 static int vnc_qdict_remote_addr(QDict
*qdict
, int fd
)
141 struct sockaddr_storage sa
;
145 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
149 return put_addr_qdict(qdict
, &sa
, salen
);
152 static const char *vnc_auth_name(VncDisplay
*vd
) {
154 case VNC_AUTH_INVALID
:
170 case VNC_AUTH_VENCRYPT
:
171 #ifdef CONFIG_VNC_TLS
172 switch (vd
->subauth
) {
173 case VNC_AUTH_VENCRYPT_PLAIN
:
174 return "vencrypt+plain";
175 case VNC_AUTH_VENCRYPT_TLSNONE
:
176 return "vencrypt+tls+none";
177 case VNC_AUTH_VENCRYPT_TLSVNC
:
178 return "vencrypt+tls+vnc";
179 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
180 return "vencrypt+tls+plain";
181 case VNC_AUTH_VENCRYPT_X509NONE
:
182 return "vencrypt+x509+none";
183 case VNC_AUTH_VENCRYPT_X509VNC
:
184 return "vencrypt+x509+vnc";
185 case VNC_AUTH_VENCRYPT_X509PLAIN
:
186 return "vencrypt+x509+plain";
187 case VNC_AUTH_VENCRYPT_TLSSASL
:
188 return "vencrypt+tls+sasl";
189 case VNC_AUTH_VENCRYPT_X509SASL
:
190 return "vencrypt+x509+sasl";
203 static int vnc_server_info_put(QDict
*qdict
)
205 if (vnc_server_addr_put(qdict
, vnc_display
->lsock
) < 0) {
209 qdict_put(qdict
, "auth", qstring_from_str(vnc_auth_name(vnc_display
)));
213 static void vnc_client_cache_auth(VncState
*client
)
221 qdict
= qobject_to_qdict(client
->info
);
223 #ifdef CONFIG_VNC_TLS
224 if (client
->tls
.session
&&
226 qdict_put(qdict
, "x509_dname", qstring_from_str(client
->tls
.dname
));
229 #ifdef CONFIG_VNC_SASL
230 if (client
->sasl
.conn
&&
231 client
->sasl
.username
) {
232 qdict_put(qdict
, "sasl_username",
233 qstring_from_str(client
->sasl
.username
));
238 static void vnc_client_cache_addr(VncState
*client
)
243 if (vnc_qdict_remote_addr(qdict
, client
->csock
) < 0) {
245 /* XXX: how to report the error? */
249 client
->info
= QOBJECT(qdict
);
252 static void vnc_qmp_event(VncState
*vs
, MonitorEvent event
)
261 server
= qdict_new();
262 if (vnc_server_info_put(server
) < 0) {
267 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
268 vs
->info
, QOBJECT(server
));
270 monitor_protocol_event(event
, data
);
272 qobject_incref(vs
->info
);
273 qobject_decref(data
);
276 static void info_vnc_iter(QObject
*obj
, void *opaque
)
279 Monitor
*mon
= opaque
;
281 client
= qobject_to_qdict(obj
);
282 monitor_printf(mon
, "Client:\n");
283 monitor_printf(mon
, " address: %s:%s\n",
284 qdict_get_str(client
, "host"),
285 qdict_get_str(client
, "service"));
287 #ifdef CONFIG_VNC_TLS
288 monitor_printf(mon
, " x509_dname: %s\n",
289 qdict_haskey(client
, "x509_dname") ?
290 qdict_get_str(client
, "x509_dname") : "none");
292 #ifdef CONFIG_VNC_SASL
293 monitor_printf(mon
, " username: %s\n",
294 qdict_haskey(client
, "sasl_username") ?
295 qdict_get_str(client
, "sasl_username") : "none");
299 void do_info_vnc_print(Monitor
*mon
, const QObject
*data
)
304 server
= qobject_to_qdict(data
);
305 if (qdict_get_bool(server
, "enabled") == 0) {
306 monitor_printf(mon
, "Server: disabled\n");
310 monitor_printf(mon
, "Server:\n");
311 monitor_printf(mon
, " address: %s:%s\n",
312 qdict_get_str(server
, "host"),
313 qdict_get_str(server
, "service"));
314 monitor_printf(mon
, " auth: %s\n", qdict_get_str(server
, "auth"));
316 clients
= qdict_get_qlist(server
, "clients");
317 if (qlist_empty(clients
)) {
318 monitor_printf(mon
, "Client: none\n");
320 qlist_iter(clients
, info_vnc_iter
, mon
);
325 * do_info_vnc(): Show VNC server information
327 * Return a QDict with server information. Connected clients are returned
328 * as a QList of QDicts.
330 * The main QDict contains the following:
332 * - "enabled": true or false
333 * - "host": server's IP address
334 * - "family": address family ("ipv4" or "ipv6")
335 * - "service": server's port number
336 * - "auth": authentication method
337 * - "clients": a QList of all connected clients
339 * Clients are described by a QDict, with the following information:
341 * - "host": client's IP address
342 * - "family": address family ("ipv4" or "ipv6")
343 * - "service": client's port number
344 * - "x509_dname": TLS dname (optional)
345 * - "sasl_username": SASL username (optional)
349 * { "enabled": true, "host": "0.0.0.0", "service": "50402", "auth": "vnc",
351 * "clients": [{ "host": "127.0.0.1", "service": "50401", "family": "ipv4" }]}
353 void do_info_vnc(Monitor
*mon
, QObject
**ret_data
)
355 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
356 *ret_data
= qobject_from_jsonf("{ 'enabled': false }");
362 QTAILQ_FOREACH(client
, &vnc_display
->clients
, next
) {
364 /* incref so that it's not freed by upper layers */
365 qobject_incref(client
->info
);
366 qlist_append_obj(clist
, client
->info
);
370 *ret_data
= qobject_from_jsonf("{ 'enabled': true, 'clients': %p }",
372 assert(*ret_data
!= NULL
);
374 if (vnc_server_info_put(qobject_to_qdict(*ret_data
)) < 0) {
375 qobject_decref(*ret_data
);
381 static inline uint32_t vnc_has_feature(VncState
*vs
, int feature
) {
382 return (vs
->features
& (1 << feature
));
386 1) Get the queue working for IO.
387 2) there is some weirdness when using the -S option (the screen is grey
388 and not totally invalidated
389 3) resolutions > 1024
392 static int vnc_update_client(VncState
*vs
, int has_dirty
);
393 static void vnc_disconnect_start(VncState
*vs
);
394 static void vnc_disconnect_finish(VncState
*vs
);
395 static void vnc_init_timer(VncDisplay
*vd
);
396 static void vnc_remove_timer(VncDisplay
*vd
);
398 static void vnc_colordepth(VncState
*vs
);
399 static void framebuffer_update_request(VncState
*vs
, int incremental
,
400 int x_position
, int y_position
,
402 static void vnc_refresh(void *opaque
);
403 static int vnc_refresh_server_surface(VncDisplay
*vd
);
405 static inline void vnc_set_bit(uint32_t *d
, int k
)
407 d
[k
>> 5] |= 1 << (k
& 0x1f);
410 static inline void vnc_clear_bit(uint32_t *d
, int k
)
412 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
415 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
425 d
[j
++] = (1 << n
) - 1;
430 static inline int vnc_get_bit(const uint32_t *d
, int k
)
432 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
435 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
439 for(i
= 0; i
< nb_words
; i
++) {
440 if ((d1
[i
] & d2
[i
]) != 0)
446 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
449 VncDisplay
*vd
= ds
->opaque
;
450 struct VncSurface
*s
= &vd
->guest
;
454 /* round x down to ensure the loop only spans one 16-pixel block per,
455 iteration. otherwise, if (x % 16) != 0, the last iteration may span
456 two 16-pixel blocks but we only mark the first as dirty
461 x
= MIN(x
, s
->ds
->width
);
462 y
= MIN(y
, s
->ds
->height
);
463 w
= MIN(x
+ w
, s
->ds
->width
) - x
;
464 h
= MIN(h
, s
->ds
->height
);
467 for (i
= 0; i
< w
; i
+= 16)
468 vnc_set_bit(s
->dirty
[y
], (x
+ i
) / 16);
471 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
474 vnc_write_u16(vs
, x
);
475 vnc_write_u16(vs
, y
);
476 vnc_write_u16(vs
, w
);
477 vnc_write_u16(vs
, h
);
479 vnc_write_s32(vs
, encoding
);
482 void buffer_reserve(Buffer
*buffer
, size_t len
)
484 if ((buffer
->capacity
- buffer
->offset
) < len
) {
485 buffer
->capacity
+= (len
+ 1024);
486 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
487 if (buffer
->buffer
== NULL
) {
488 fprintf(stderr
, "vnc: out of memory\n");
494 int buffer_empty(Buffer
*buffer
)
496 return buffer
->offset
== 0;
499 uint8_t *buffer_end(Buffer
*buffer
)
501 return buffer
->buffer
+ buffer
->offset
;
504 void buffer_reset(Buffer
*buffer
)
509 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
511 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
512 buffer
->offset
+= len
;
515 static void vnc_dpy_resize(DisplayState
*ds
)
518 VncDisplay
*vd
= ds
->opaque
;
523 vd
->server
= qemu_mallocz(sizeof(*vd
->server
));
524 if (vd
->server
->data
)
525 qemu_free(vd
->server
->data
);
526 *(vd
->server
) = *(ds
->surface
);
527 vd
->server
->data
= qemu_mallocz(vd
->server
->linesize
*
532 vd
->guest
.ds
= qemu_mallocz(sizeof(*vd
->guest
.ds
));
533 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
534 console_color_init(ds
);
535 size_changed
= ds_get_width(ds
) != vd
->guest
.ds
->width
||
536 ds_get_height(ds
) != vd
->guest
.ds
->height
;
537 *(vd
->guest
.ds
) = *(ds
->surface
);
538 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
540 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
543 if (vs
->csock
!= -1 && vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
544 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
546 vnc_write_u16(vs
, 1); /* number of rects */
547 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(ds
), ds_get_height(ds
),
548 VNC_ENCODING_DESKTOPRESIZE
);
552 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
557 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
559 vnc_write(vs
, pixels
, size
);
562 /* slowest but generic code. */
563 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
566 VncDisplay
*vd
= vs
->vd
;
568 r
= ((((v
& vd
->server
->pf
.rmask
) >> vd
->server
->pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
569 vd
->server
->pf
.rbits
);
570 g
= ((((v
& vd
->server
->pf
.gmask
) >> vd
->server
->pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
571 vd
->server
->pf
.gbits
);
572 b
= ((((v
& vd
->server
->pf
.bmask
) >> vd
->server
->pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
573 vd
->server
->pf
.bbits
);
574 v
= (r
<< vs
->clientds
.pf
.rshift
) |
575 (g
<< vs
->clientds
.pf
.gshift
) |
576 (b
<< vs
->clientds
.pf
.bshift
);
577 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
582 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
592 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
607 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
610 VncDisplay
*vd
= vs
->vd
;
612 if (vd
->server
->pf
.bytes_per_pixel
== 4) {
613 uint32_t *pixels
= pixels1
;
616 for(i
= 0; i
< n
; i
++) {
617 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
618 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
620 } else if (vd
->server
->pf
.bytes_per_pixel
== 2) {
621 uint16_t *pixels
= pixels1
;
624 for(i
= 0; i
< n
; i
++) {
625 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
626 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
628 } else if (vd
->server
->pf
.bytes_per_pixel
== 1) {
629 uint8_t *pixels
= pixels1
;
632 for(i
= 0; i
< n
; i
++) {
633 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
634 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
637 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
641 void vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
645 VncDisplay
*vd
= vs
->vd
;
647 row
= vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
648 for (i
= 0; i
< h
; i
++) {
649 vs
->write_pixels(vs
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
650 row
+= ds_get_linesize(vs
->ds
);
654 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
656 switch(vs
->vnc_encoding
) {
657 case VNC_ENCODING_ZLIB
:
658 vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
660 case VNC_ENCODING_HEXTILE
:
661 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
662 vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
665 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
666 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
671 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
673 /* send bitblit op to the vnc client */
674 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
676 vnc_write_u16(vs
, 1); /* number of rects */
677 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
678 vnc_write_u16(vs
, src_x
);
679 vnc_write_u16(vs
, src_y
);
683 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
685 VncDisplay
*vd
= ds
->opaque
;
689 int i
,x
,y
,pitch
,depth
,inc
,w_lim
,s
;
692 vnc_refresh_server_surface(vd
);
693 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
694 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
695 vs
->force_update
= 1;
696 vnc_update_client(vs
, 1);
697 /* vs might be free()ed here */
701 /* do bitblit op on the local surface too */
702 pitch
= ds_get_linesize(vd
->ds
);
703 depth
= ds_get_bytes_per_pixel(vd
->ds
);
704 src_row
= vd
->server
->data
+ pitch
* src_y
+ depth
* src_x
;
705 dst_row
= vd
->server
->data
+ pitch
* dst_y
+ depth
* dst_x
;
710 src_row
+= pitch
* (h
-1);
711 dst_row
+= pitch
* (h
-1);
716 w_lim
= w
- (16 - (dst_x
% 16));
720 w_lim
= w
- (w_lim
% 16);
721 for (i
= 0; i
< h
; i
++) {
722 for (x
= 0; x
<= w_lim
;
723 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
725 if ((s
= w
- w_lim
) == 0)
728 s
= (16 - (dst_x
% 16));
733 cmp_bytes
= s
* depth
;
734 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
736 memmove(dst_row
, src_row
, cmp_bytes
);
737 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
738 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
739 vnc_set_bit(vs
->dirty
[y
], ((x
+ dst_x
) / 16));
743 src_row
+= pitch
- w
* depth
;
744 dst_row
+= pitch
- w
* depth
;
748 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
749 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
750 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
755 static int find_and_clear_dirty_height(struct VncState
*vs
,
756 int y
, int last_x
, int x
)
759 VncDisplay
*vd
= vs
->vd
;
761 for (h
= 1; h
< (vd
->server
->height
- y
); h
++) {
763 if (!vnc_get_bit(vs
->dirty
[y
+ h
], last_x
))
765 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
766 vnc_clear_bit(vs
->dirty
[y
+ h
], tmp_x
);
772 static int vnc_update_client(VncState
*vs
, int has_dirty
)
774 if (vs
->need_update
&& vs
->csock
!= -1) {
775 VncDisplay
*vd
= vs
->vd
;
780 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
781 /* kernel send buffers are full -> drop frames to throttle */
784 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
788 * Send screen updates to the vnc client using the server
789 * surface and server dirty map. guest surface updates
790 * happening in parallel don't disturb us, the next pass will
791 * send them to the client.
794 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
796 saved_offset
= vs
->output
.offset
;
797 vnc_write_u16(vs
, 0);
799 for (y
= 0; y
< vd
->server
->height
; y
++) {
802 for (x
= 0; x
< vd
->server
->width
/ 16; x
++) {
803 if (vnc_get_bit(vs
->dirty
[y
], x
)) {
807 vnc_clear_bit(vs
->dirty
[y
], x
);
810 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
811 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
818 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
819 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
823 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
824 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
826 vs
->force_update
= 0;
831 vnc_disconnect_finish(vs
);
837 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
839 VncState
*vs
= opaque
;
842 case AUD_CNOTIFY_DISABLE
:
843 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
844 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
845 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
849 case AUD_CNOTIFY_ENABLE
:
850 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
851 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
852 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
858 static void audio_capture_destroy(void *opaque
)
862 static void audio_capture(void *opaque
, void *buf
, int size
)
864 VncState
*vs
= opaque
;
866 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
867 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
868 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
869 vnc_write_u32(vs
, size
);
870 vnc_write(vs
, buf
, size
);
874 static void audio_add(VncState
*vs
)
876 struct audio_capture_ops ops
;
879 monitor_printf(default_mon
, "audio already running\n");
883 ops
.notify
= audio_capture_notify
;
884 ops
.destroy
= audio_capture_destroy
;
885 ops
.capture
= audio_capture
;
887 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
888 if (!vs
->audio_cap
) {
889 monitor_printf(default_mon
, "Failed to add audio capture\n");
893 static void audio_del(VncState
*vs
)
896 AUD_del_capture(vs
->audio_cap
, vs
);
897 vs
->audio_cap
= NULL
;
901 static void vnc_disconnect_start(VncState
*vs
)
905 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
906 closesocket(vs
->csock
);
910 static void vnc_disconnect_finish(VncState
*vs
)
912 vnc_qmp_event(vs
, QEVENT_VNC_DISCONNECTED
);
914 if (vs
->input
.buffer
) {
915 qemu_free(vs
->input
.buffer
);
916 vs
->input
.buffer
= NULL
;
918 if (vs
->output
.buffer
) {
919 qemu_free(vs
->output
.buffer
);
920 vs
->output
.buffer
= NULL
;
923 qobject_decref(vs
->info
);
925 #ifdef CONFIG_VNC_TLS
926 vnc_tls_client_cleanup(vs
);
927 #endif /* CONFIG_VNC_TLS */
928 #ifdef CONFIG_VNC_SASL
929 vnc_sasl_client_cleanup(vs
);
930 #endif /* CONFIG_VNC_SASL */
933 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
935 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
939 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
940 vnc_remove_timer(vs
->vd
);
941 if (vs
->vd
->lock_key_sync
)
942 qemu_remove_led_event_handler(vs
->led
);
946 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
948 if (ret
== 0 || ret
== -1) {
950 switch (last_errno
) {
962 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
963 ret
, ret
< 0 ? last_errno
: 0);
964 vnc_disconnect_start(vs
);
972 void vnc_client_error(VncState
*vs
)
974 VNC_DEBUG("Closing down client sock: protocol error\n");
975 vnc_disconnect_start(vs
);
980 * Called to write a chunk of data to the client socket. The data may
981 * be the raw data, or may have already been encoded by SASL.
982 * The data will be written either straight onto the socket, or
983 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
985 * NB, it is theoretically possible to have 2 layers of encryption,
986 * both SASL, and this TLS layer. It is highly unlikely in practice
987 * though, since SASL encryption will typically be a no-op if TLS
990 * Returns the number of bytes written, which may be less than
991 * the requested 'datalen' if the socket would block. Returns
992 * -1 on error, and disconnects the client socket.
994 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
997 #ifdef CONFIG_VNC_TLS
998 if (vs
->tls
.session
) {
999 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
1001 if (ret
== GNUTLS_E_AGAIN
)
1008 #endif /* CONFIG_VNC_TLS */
1009 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
1010 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1011 return vnc_client_io_error(vs
, ret
, socket_error());
1016 * Called to write buffered data to the client socket, when not
1017 * using any SASL SSF encryption layers. Will write as much data
1018 * as possible without blocking. If all buffered data is written,
1019 * will switch the FD poll() handler back to read monitoring.
1021 * Returns the number of bytes written, which may be less than
1022 * the buffered output data if the socket would block. Returns
1023 * -1 on error, and disconnects the client socket.
1025 static long vnc_client_write_plain(VncState
*vs
)
1029 #ifdef CONFIG_VNC_SASL
1030 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1031 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1032 vs
->sasl
.waitWriteSSF
);
1034 if (vs
->sasl
.conn
&&
1036 vs
->sasl
.waitWriteSSF
) {
1037 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1039 vs
->sasl
.waitWriteSSF
-= ret
;
1041 #endif /* CONFIG_VNC_SASL */
1042 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1046 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1047 vs
->output
.offset
-= ret
;
1049 if (vs
->output
.offset
== 0) {
1050 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1058 * First function called whenever there is data to be written to
1059 * the client socket. Will delegate actual work according to whether
1060 * SASL SSF layers are enabled (thus requiring encryption calls)
1062 void vnc_client_write(void *opaque
)
1064 VncState
*vs
= opaque
;
1066 #ifdef CONFIG_VNC_SASL
1067 if (vs
->sasl
.conn
&&
1069 !vs
->sasl
.waitWriteSSF
) {
1070 vnc_client_write_sasl(vs
);
1072 #endif /* CONFIG_VNC_SASL */
1073 vnc_client_write_plain(vs
);
1076 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1078 vs
->read_handler
= func
;
1079 vs
->read_handler_expect
= expecting
;
1084 * Called to read a chunk of data from the client socket. The data may
1085 * be the raw data, or may need to be further decoded by SASL.
1086 * The data will be read either straight from to the socket, or
1087 * read 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 read, 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_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1101 #ifdef CONFIG_VNC_TLS
1102 if (vs
->tls
.session
) {
1103 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1105 if (ret
== GNUTLS_E_AGAIN
)
1112 #endif /* CONFIG_VNC_TLS */
1113 ret
= recv(vs
->csock
, (void *)data
, datalen
, 0);
1114 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1115 return vnc_client_io_error(vs
, ret
, socket_error());
1120 * Called to read data from the client socket to the input buffer,
1121 * when not using any SASL SSF encryption layers. Will read as much
1122 * data as possible without blocking.
1124 * Returns the number of bytes read. Returns -1 on error, and
1125 * disconnects the client socket.
1127 static long vnc_client_read_plain(VncState
*vs
)
1130 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1131 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1132 buffer_reserve(&vs
->input
, 4096);
1133 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1136 vs
->input
.offset
+= ret
;
1142 * First function called whenever there is more data to be read from
1143 * the client socket. Will delegate actual work according to whether
1144 * SASL SSF layers are enabled (thus requiring decryption calls)
1146 void vnc_client_read(void *opaque
)
1148 VncState
*vs
= opaque
;
1151 #ifdef CONFIG_VNC_SASL
1152 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1153 ret
= vnc_client_read_sasl(vs
);
1155 #endif /* CONFIG_VNC_SASL */
1156 ret
= vnc_client_read_plain(vs
);
1158 if (vs
->csock
== -1)
1159 vnc_disconnect_finish(vs
);
1163 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1164 size_t len
= vs
->read_handler_expect
;
1167 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1168 if (vs
->csock
== -1) {
1169 vnc_disconnect_finish(vs
);
1174 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1175 vs
->input
.offset
-= len
;
1177 vs
->read_handler_expect
= ret
;
1182 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1184 buffer_reserve(&vs
->output
, len
);
1186 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1187 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1190 buffer_append(&vs
->output
, data
, len
);
1193 void vnc_write_s32(VncState
*vs
, int32_t value
)
1195 vnc_write_u32(vs
, *(uint32_t *)&value
);
1198 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1202 buf
[0] = (value
>> 24) & 0xFF;
1203 buf
[1] = (value
>> 16) & 0xFF;
1204 buf
[2] = (value
>> 8) & 0xFF;
1205 buf
[3] = value
& 0xFF;
1207 vnc_write(vs
, buf
, 4);
1210 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1214 buf
[0] = (value
>> 8) & 0xFF;
1215 buf
[1] = value
& 0xFF;
1217 vnc_write(vs
, buf
, 2);
1220 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1222 vnc_write(vs
, (char *)&value
, 1);
1225 void vnc_flush(VncState
*vs
)
1227 if (vs
->csock
!= -1 && vs
->output
.offset
)
1228 vnc_client_write(vs
);
1231 uint8_t read_u8(uint8_t *data
, size_t offset
)
1233 return data
[offset
];
1236 uint16_t read_u16(uint8_t *data
, size_t offset
)
1238 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1241 int32_t read_s32(uint8_t *data
, size_t offset
)
1243 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1244 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1247 uint32_t read_u32(uint8_t *data
, size_t offset
)
1249 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1250 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1253 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1257 static void check_pointer_type_change(Notifier
*notifier
)
1259 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1260 int absolute
= kbd_mouse_is_absolute();
1262 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1263 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1264 vnc_write_u8(vs
, 0);
1265 vnc_write_u16(vs
, 1);
1266 vnc_framebuffer_update(vs
, absolute
, 0,
1267 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1268 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1271 vs
->absolute
= absolute
;
1274 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1279 if (button_mask
& 0x01)
1280 buttons
|= MOUSE_EVENT_LBUTTON
;
1281 if (button_mask
& 0x02)
1282 buttons
|= MOUSE_EVENT_MBUTTON
;
1283 if (button_mask
& 0x04)
1284 buttons
|= MOUSE_EVENT_RBUTTON
;
1285 if (button_mask
& 0x08)
1287 if (button_mask
& 0x10)
1291 kbd_mouse_event(ds_get_width(vs
->ds
) > 1 ?
1292 x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1) : 0x4000,
1293 ds_get_height(vs
->ds
) > 1 ?
1294 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1) : 0x4000,
1296 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1300 kbd_mouse_event(x
, y
, dz
, buttons
);
1302 if (vs
->last_x
!= -1)
1303 kbd_mouse_event(x
- vs
->last_x
,
1311 static void reset_keys(VncState
*vs
)
1314 for(i
= 0; i
< 256; i
++) {
1315 if (vs
->modifiers_state
[i
]) {
1316 if (i
& SCANCODE_GREY
)
1317 kbd_put_keycode(SCANCODE_EMUL0
);
1318 kbd_put_keycode(i
| SCANCODE_UP
);
1319 vs
->modifiers_state
[i
] = 0;
1324 static void press_key(VncState
*vs
, int keysym
)
1326 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1327 if (keycode
& SCANCODE_GREY
)
1328 kbd_put_keycode(SCANCODE_EMUL0
);
1329 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1330 if (keycode
& SCANCODE_GREY
)
1331 kbd_put_keycode(SCANCODE_EMUL0
);
1332 kbd_put_keycode(keycode
| SCANCODE_UP
);
1335 static void kbd_leds(void *opaque
, int ledstate
)
1337 VncState
*vs
= opaque
;
1340 caps
= ledstate
& QEMU_CAPS_LOCK_LED
? 1 : 0;
1341 num
= ledstate
& QEMU_NUM_LOCK_LED
? 1 : 0;
1343 if (vs
->modifiers_state
[0x3a] != caps
) {
1344 vs
->modifiers_state
[0x3a] = caps
;
1346 if (vs
->modifiers_state
[0x45] != num
) {
1347 vs
->modifiers_state
[0x45] = num
;
1351 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1353 /* QEMU console switch */
1355 case 0x2a: /* Left Shift */
1356 case 0x36: /* Right Shift */
1357 case 0x1d: /* Left CTRL */
1358 case 0x9d: /* Right CTRL */
1359 case 0x38: /* Left ALT */
1360 case 0xb8: /* Right ALT */
1362 vs
->modifiers_state
[keycode
] = 1;
1364 vs
->modifiers_state
[keycode
] = 0;
1366 case 0x02 ... 0x0a: /* '1' to '9' keys */
1367 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1368 /* Reset the modifiers sent to the current console */
1370 console_select(keycode
- 0x02);
1374 case 0x3a: /* CapsLock */
1375 case 0x45: /* NumLock */
1377 vs
->modifiers_state
[keycode
] ^= 1;
1381 if (vs
->vd
->lock_key_sync
&&
1382 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1383 /* If the numlock state needs to change then simulate an additional
1384 keypress before sending this one. This will happen if the user
1385 toggles numlock away from the VNC window.
1387 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1388 if (!vs
->modifiers_state
[0x45]) {
1389 vs
->modifiers_state
[0x45] = 1;
1390 press_key(vs
, 0xff7f);
1393 if (vs
->modifiers_state
[0x45]) {
1394 vs
->modifiers_state
[0x45] = 0;
1395 press_key(vs
, 0xff7f);
1400 if (vs
->vd
->lock_key_sync
&&
1401 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1402 /* If the capslock state needs to change then simulate an additional
1403 keypress before sending this one. This will happen if the user
1404 toggles capslock away from the VNC window.
1406 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1407 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1408 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1410 if (uppercase
== shift
) {
1411 vs
->modifiers_state
[0x3a] = 0;
1412 press_key(vs
, 0xffe5);
1415 if (uppercase
!= shift
) {
1416 vs
->modifiers_state
[0x3a] = 1;
1417 press_key(vs
, 0xffe5);
1422 if (is_graphic_console()) {
1423 if (keycode
& SCANCODE_GREY
)
1424 kbd_put_keycode(SCANCODE_EMUL0
);
1426 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1428 kbd_put_keycode(keycode
| SCANCODE_UP
);
1430 /* QEMU console emulation */
1432 int numlock
= vs
->modifiers_state
[0x45];
1434 case 0x2a: /* Left Shift */
1435 case 0x36: /* Right Shift */
1436 case 0x1d: /* Left CTRL */
1437 case 0x9d: /* Right CTRL */
1438 case 0x38: /* Left ALT */
1439 case 0xb8: /* Right ALT */
1442 kbd_put_keysym(QEMU_KEY_UP
);
1445 kbd_put_keysym(QEMU_KEY_DOWN
);
1448 kbd_put_keysym(QEMU_KEY_LEFT
);
1451 kbd_put_keysym(QEMU_KEY_RIGHT
);
1454 kbd_put_keysym(QEMU_KEY_DELETE
);
1457 kbd_put_keysym(QEMU_KEY_HOME
);
1460 kbd_put_keysym(QEMU_KEY_END
);
1463 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1466 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1470 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1473 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1476 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1479 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1482 kbd_put_keysym('5');
1485 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1488 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1491 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1494 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1497 kbd_put_keysym('0');
1500 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1504 kbd_put_keysym('/');
1507 kbd_put_keysym('*');
1510 kbd_put_keysym('-');
1513 kbd_put_keysym('+');
1516 kbd_put_keysym('\n');
1520 kbd_put_keysym(sym
);
1527 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1532 if (lsym
>= 'A' && lsym
<= 'Z' && is_graphic_console()) {
1533 lsym
= lsym
- 'A' + 'a';
1536 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1537 do_key_event(vs
, down
, keycode
, sym
);
1540 static void ext_key_event(VncState
*vs
, int down
,
1541 uint32_t sym
, uint16_t keycode
)
1543 /* if the user specifies a keyboard layout, always use it */
1544 if (keyboard_layout
)
1545 key_event(vs
, down
, sym
);
1547 do_key_event(vs
, down
, keycode
, sym
);
1550 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1551 int x_position
, int y_position
,
1554 if (y_position
> ds_get_height(vs
->ds
))
1555 y_position
= ds_get_height(vs
->ds
);
1556 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1557 h
= ds_get_height(vs
->ds
) - y_position
;
1560 vs
->need_update
= 1;
1562 vs
->force_update
= 1;
1563 for (i
= 0; i
< h
; i
++) {
1564 vnc_set_bits(vs
->dirty
[y_position
+ i
],
1565 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1570 static void send_ext_key_event_ack(VncState
*vs
)
1572 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1573 vnc_write_u8(vs
, 0);
1574 vnc_write_u16(vs
, 1);
1575 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1576 VNC_ENCODING_EXT_KEY_EVENT
);
1580 static void send_ext_audio_ack(VncState
*vs
)
1582 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1583 vnc_write_u8(vs
, 0);
1584 vnc_write_u16(vs
, 1);
1585 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1586 VNC_ENCODING_AUDIO
);
1590 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1593 unsigned int enc
= 0;
1597 vs
->vnc_encoding
= 0;
1598 vs
->tight_compression
= 9;
1599 vs
->tight_quality
= 9;
1602 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1605 case VNC_ENCODING_RAW
:
1606 vs
->vnc_encoding
= enc
;
1608 case VNC_ENCODING_COPYRECT
:
1609 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1611 case VNC_ENCODING_HEXTILE
:
1612 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1613 vs
->vnc_encoding
= enc
;
1615 case VNC_ENCODING_ZLIB
:
1616 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1617 vs
->vnc_encoding
= enc
;
1619 case VNC_ENCODING_DESKTOPRESIZE
:
1620 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1622 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1623 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1625 case VNC_ENCODING_EXT_KEY_EVENT
:
1626 send_ext_key_event_ack(vs
);
1628 case VNC_ENCODING_AUDIO
:
1629 send_ext_audio_ack(vs
);
1631 case VNC_ENCODING_WMVi
:
1632 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1634 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1635 vs
->tight_compression
= (enc
& 0x0F);
1637 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1638 vs
->tight_quality
= (enc
& 0x0F);
1641 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1647 static void set_pixel_conversion(VncState
*vs
)
1649 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1650 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1651 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1652 vs
->write_pixels
= vnc_write_pixels_copy
;
1653 vnc_hextile_set_pixel_conversion(vs
, 0);
1655 vs
->write_pixels
= vnc_write_pixels_generic
;
1656 vnc_hextile_set_pixel_conversion(vs
, 1);
1660 static void set_pixel_format(VncState
*vs
,
1661 int bits_per_pixel
, int depth
,
1662 int big_endian_flag
, int true_color_flag
,
1663 int red_max
, int green_max
, int blue_max
,
1664 int red_shift
, int green_shift
, int blue_shift
)
1666 if (!true_color_flag
) {
1667 vnc_client_error(vs
);
1671 vs
->clientds
= *(vs
->vd
->guest
.ds
);
1672 vs
->clientds
.pf
.rmax
= red_max
;
1673 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1674 vs
->clientds
.pf
.rshift
= red_shift
;
1675 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1676 vs
->clientds
.pf
.gmax
= green_max
;
1677 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1678 vs
->clientds
.pf
.gshift
= green_shift
;
1679 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1680 vs
->clientds
.pf
.bmax
= blue_max
;
1681 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1682 vs
->clientds
.pf
.bshift
= blue_shift
;
1683 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1684 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1685 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1686 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1687 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1689 set_pixel_conversion(vs
);
1691 vga_hw_invalidate();
1695 static void pixel_format_message (VncState
*vs
) {
1696 char pad
[3] = { 0, 0, 0 };
1698 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1699 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1701 #ifdef HOST_WORDS_BIGENDIAN
1702 vnc_write_u8(vs
, 1); /* big-endian-flag */
1704 vnc_write_u8(vs
, 0); /* big-endian-flag */
1706 vnc_write_u8(vs
, 1); /* true-color-flag */
1707 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1708 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1709 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1710 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1711 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1712 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1714 vnc_hextile_set_pixel_conversion(vs
, 0);
1716 vs
->clientds
= *(vs
->ds
->surface
);
1717 vs
->clientds
.flags
&= ~QEMU_ALLOCATED_FLAG
;
1718 vs
->write_pixels
= vnc_write_pixels_copy
;
1720 vnc_write(vs
, pad
, 3); /* padding */
1723 static void vnc_dpy_setdata(DisplayState
*ds
)
1725 /* We don't have to do anything */
1728 static void vnc_colordepth(VncState
*vs
)
1730 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1731 /* Sending a WMVi message to notify the client*/
1732 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1733 vnc_write_u8(vs
, 0);
1734 vnc_write_u16(vs
, 1); /* number of rects */
1735 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1736 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1737 pixel_format_message(vs
);
1740 set_pixel_conversion(vs
);
1744 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1748 VncDisplay
*vd
= vs
->vd
;
1751 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
1752 if (!qemu_timer_expired(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
))
1753 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
);
1757 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
1761 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1762 read_u8(data
, 6), read_u8(data
, 7),
1763 read_u16(data
, 8), read_u16(data
, 10),
1764 read_u16(data
, 12), read_u8(data
, 14),
1765 read_u8(data
, 15), read_u8(data
, 16));
1767 case VNC_MSG_CLIENT_SET_ENCODINGS
:
1772 limit
= read_u16(data
, 2);
1774 return 4 + (limit
* 4);
1776 limit
= read_u16(data
, 2);
1778 for (i
= 0; i
< limit
; i
++) {
1779 int32_t val
= read_s32(data
, 4 + (i
* 4));
1780 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1783 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1785 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
1789 framebuffer_update_request(vs
,
1790 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1791 read_u16(data
, 6), read_u16(data
, 8));
1793 case VNC_MSG_CLIENT_KEY_EVENT
:
1797 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1799 case VNC_MSG_CLIENT_POINTER_EVENT
:
1803 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1805 case VNC_MSG_CLIENT_CUT_TEXT
:
1810 uint32_t dlen
= read_u32(data
, 4);
1815 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1817 case VNC_MSG_CLIENT_QEMU
:
1821 switch (read_u8(data
, 1)) {
1822 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
1826 ext_key_event(vs
, read_u16(data
, 2),
1827 read_u32(data
, 4), read_u32(data
, 8));
1829 case VNC_MSG_CLIENT_QEMU_AUDIO
:
1833 switch (read_u16 (data
, 2)) {
1834 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
1837 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
1840 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
1843 switch (read_u8(data
, 4)) {
1844 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1845 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1846 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1847 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1848 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1849 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1851 printf("Invalid audio format %d\n", read_u8(data
, 4));
1852 vnc_client_error(vs
);
1855 vs
->as
.nchannels
= read_u8(data
, 5);
1856 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
1857 printf("Invalid audio channel coount %d\n",
1859 vnc_client_error(vs
);
1862 vs
->as
.freq
= read_u32(data
, 6);
1865 printf ("Invalid audio message %d\n", read_u8(data
, 4));
1866 vnc_client_error(vs
);
1872 printf("Msg: %d\n", read_u16(data
, 0));
1873 vnc_client_error(vs
);
1878 printf("Msg: %d\n", data
[0]);
1879 vnc_client_error(vs
);
1883 vnc_read_when(vs
, protocol_client_msg
, 1);
1887 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
1892 vnc_write_u16(vs
, ds_get_width(vs
->ds
));
1893 vnc_write_u16(vs
, ds_get_height(vs
->ds
));
1895 pixel_format_message(vs
);
1898 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
1900 size
= snprintf(buf
, sizeof(buf
), "QEMU");
1902 vnc_write_u32(vs
, size
);
1903 vnc_write(vs
, buf
, size
);
1906 vnc_client_cache_auth(vs
);
1907 vnc_qmp_event(vs
, QEVENT_VNC_INITIALIZED
);
1909 vnc_read_when(vs
, protocol_client_msg
, 1);
1914 void start_client_init(VncState
*vs
)
1916 vnc_read_when(vs
, protocol_client_init
, 1);
1919 static void make_challenge(VncState
*vs
)
1923 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1925 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1926 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1929 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
1931 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
1933 unsigned char key
[8];
1935 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
1936 VNC_DEBUG("No password configured on server");
1937 vnc_write_u32(vs
, 1); /* Reject auth */
1938 if (vs
->minor
>= 8) {
1939 static const char err
[] = "Authentication failed";
1940 vnc_write_u32(vs
, sizeof(err
));
1941 vnc_write(vs
, err
, sizeof(err
));
1944 vnc_client_error(vs
);
1948 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1950 /* Calculate the expected challenge response */
1951 pwlen
= strlen(vs
->vd
->password
);
1952 for (i
=0; i
<sizeof(key
); i
++)
1953 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
1955 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1956 des(response
+j
, response
+j
);
1958 /* Compare expected vs actual challenge response */
1959 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1960 VNC_DEBUG("Client challenge reponse did not match\n");
1961 vnc_write_u32(vs
, 1); /* Reject auth */
1962 if (vs
->minor
>= 8) {
1963 static const char err
[] = "Authentication failed";
1964 vnc_write_u32(vs
, sizeof(err
));
1965 vnc_write(vs
, err
, sizeof(err
));
1968 vnc_client_error(vs
);
1970 VNC_DEBUG("Accepting VNC challenge response\n");
1971 vnc_write_u32(vs
, 0); /* Accept auth */
1974 start_client_init(vs
);
1979 void start_auth_vnc(VncState
*vs
)
1982 /* Send client a 'random' challenge */
1983 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1986 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1990 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
1992 /* We only advertise 1 auth scheme at a time, so client
1993 * must pick the one we sent. Verify this */
1994 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
1995 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
1996 vnc_write_u32(vs
, 1);
1997 if (vs
->minor
>= 8) {
1998 static const char err
[] = "Authentication failed";
1999 vnc_write_u32(vs
, sizeof(err
));
2000 vnc_write(vs
, err
, sizeof(err
));
2002 vnc_client_error(vs
);
2003 } else { /* Accept requested auth */
2004 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2005 switch (vs
->vd
->auth
) {
2007 VNC_DEBUG("Accept auth none\n");
2008 if (vs
->minor
>= 8) {
2009 vnc_write_u32(vs
, 0); /* Accept auth completion */
2012 start_client_init(vs
);
2016 VNC_DEBUG("Start VNC auth\n");
2020 #ifdef CONFIG_VNC_TLS
2021 case VNC_AUTH_VENCRYPT
:
2022 VNC_DEBUG("Accept VeNCrypt auth\n");;
2023 start_auth_vencrypt(vs
);
2025 #endif /* CONFIG_VNC_TLS */
2027 #ifdef CONFIG_VNC_SASL
2029 VNC_DEBUG("Accept SASL auth\n");
2030 start_auth_sasl(vs
);
2032 #endif /* CONFIG_VNC_SASL */
2034 default: /* Should not be possible, but just in case */
2035 VNC_DEBUG("Reject auth %d server code bug\n", vs
->vd
->auth
);
2036 vnc_write_u8(vs
, 1);
2037 if (vs
->minor
>= 8) {
2038 static const char err
[] = "Authentication failed";
2039 vnc_write_u32(vs
, sizeof(err
));
2040 vnc_write(vs
, err
, sizeof(err
));
2042 vnc_client_error(vs
);
2048 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2052 memcpy(local
, version
, 12);
2055 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2056 VNC_DEBUG("Malformed protocol version %s\n", local
);
2057 vnc_client_error(vs
);
2060 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2061 if (vs
->major
!= 3 ||
2067 VNC_DEBUG("Unsupported client version\n");
2068 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2070 vnc_client_error(vs
);
2073 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2074 * as equivalent to v3.3 by servers
2076 if (vs
->minor
== 4 || vs
->minor
== 5)
2079 if (vs
->minor
== 3) {
2080 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
2081 VNC_DEBUG("Tell client auth none\n");
2082 vnc_write_u32(vs
, vs
->vd
->auth
);
2084 start_client_init(vs
);
2085 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
2086 VNC_DEBUG("Tell client VNC auth\n");
2087 vnc_write_u32(vs
, vs
->vd
->auth
);
2091 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
2092 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2094 vnc_client_error(vs
);
2097 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
2098 vnc_write_u8(vs
, 1); /* num auth */
2099 vnc_write_u8(vs
, vs
->vd
->auth
);
2100 vnc_read_when(vs
, protocol_client_auth
, 1);
2107 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2111 uint8_t *server_row
;
2113 uint32_t width_mask
[VNC_DIRTY_WORDS
];
2118 * Walk through the guest dirty map.
2119 * Check and copy modified bits from guest to server surface.
2120 * Update server dirty map.
2122 vnc_set_bits(width_mask
, (ds_get_width(vd
->ds
) / 16), VNC_DIRTY_WORDS
);
2123 cmp_bytes
= 16 * ds_get_bytes_per_pixel(vd
->ds
);
2124 guest_row
= vd
->guest
.ds
->data
;
2125 server_row
= vd
->server
->data
;
2126 for (y
= 0; y
< vd
->guest
.ds
->height
; y
++) {
2127 if (vnc_and_bits(vd
->guest
.dirty
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
2130 uint8_t *server_ptr
;
2132 guest_ptr
= guest_row
;
2133 server_ptr
= server_row
;
2135 for (x
= 0; x
< vd
->guest
.ds
->width
;
2136 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2137 if (!vnc_get_bit(vd
->guest
.dirty
[y
], (x
/ 16)))
2139 vnc_clear_bit(vd
->guest
.dirty
[y
], (x
/ 16));
2140 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2142 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2143 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2144 vnc_set_bit(vs
->dirty
[y
], (x
/ 16));
2149 guest_row
+= ds_get_linesize(vd
->ds
);
2150 server_row
+= ds_get_linesize(vd
->ds
);
2155 static void vnc_refresh(void *opaque
)
2157 VncDisplay
*vd
= opaque
;
2159 int has_dirty
, rects
= 0;
2163 has_dirty
= vnc_refresh_server_surface(vd
);
2165 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2166 rects
+= vnc_update_client(vs
, has_dirty
);
2167 /* vs might be free()ed here */
2169 /* vd->timer could be NULL now if the last client disconnected,
2170 * in this case don't update the timer */
2171 if (vd
->timer
== NULL
)
2174 if (has_dirty
&& rects
) {
2175 vd
->timer_interval
/= 2;
2176 if (vd
->timer_interval
< VNC_REFRESH_INTERVAL_BASE
)
2177 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2179 vd
->timer_interval
+= VNC_REFRESH_INTERVAL_INC
;
2180 if (vd
->timer_interval
> VNC_REFRESH_INTERVAL_MAX
)
2181 vd
->timer_interval
= VNC_REFRESH_INTERVAL_MAX
;
2183 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) + vd
->timer_interval
);
2186 static void vnc_init_timer(VncDisplay
*vd
)
2188 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2189 if (vd
->timer
== NULL
&& !QTAILQ_EMPTY(&vd
->clients
)) {
2190 vd
->timer
= qemu_new_timer(rt_clock
, vnc_refresh
, vd
);
2195 static void vnc_remove_timer(VncDisplay
*vd
)
2197 if (vd
->timer
!= NULL
&& QTAILQ_EMPTY(&vd
->clients
)) {
2198 qemu_del_timer(vd
->timer
);
2199 qemu_free_timer(vd
->timer
);
2204 static void vnc_connect(VncDisplay
*vd
, int csock
)
2206 VncState
*vs
= qemu_mallocz(sizeof(VncState
));
2209 VNC_DEBUG("New client on socket %d\n", csock
);
2211 socket_set_nonblock(vs
->csock
);
2212 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2214 vnc_client_cache_addr(vs
);
2215 vnc_qmp_event(vs
, QEVENT_VNC_CONNECTED
);
2222 vs
->as
.freq
= 44100;
2223 vs
->as
.nchannels
= 2;
2224 vs
->as
.fmt
= AUD_FMT_S16
;
2225 vs
->as
.endianness
= 0;
2227 QTAILQ_INSERT_HEAD(&vd
->clients
, vs
, next
);
2231 vnc_write(vs
, "RFB 003.008\n", 12);
2233 vnc_read_when(vs
, protocol_version
, 12);
2235 if (vs
->vd
->lock_key_sync
)
2236 vs
->led
= qemu_add_led_event_handler(kbd_leds
, vs
);
2238 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
2239 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
2243 /* vs might be free()ed here */
2246 static void vnc_listen_read(void *opaque
)
2248 VncDisplay
*vs
= opaque
;
2249 struct sockaddr_in addr
;
2250 socklen_t addrlen
= sizeof(addr
);
2255 int csock
= qemu_accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2257 vnc_connect(vs
, csock
);
2261 void vnc_display_init(DisplayState
*ds
)
2263 VncDisplay
*vs
= qemu_mallocz(sizeof(*vs
));
2265 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2274 QTAILQ_INIT(&vs
->clients
);
2276 if (keyboard_layout
)
2277 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2279 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2281 if (!vs
->kbd_layout
)
2284 dcl
->dpy_copy
= vnc_dpy_copy
;
2285 dcl
->dpy_update
= vnc_dpy_update
;
2286 dcl
->dpy_resize
= vnc_dpy_resize
;
2287 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2288 register_displaychangelistener(ds
, dcl
);
2292 void vnc_display_close(DisplayState
*ds
)
2294 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2299 qemu_free(vs
->display
);
2302 if (vs
->lsock
!= -1) {
2303 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2307 vs
->auth
= VNC_AUTH_INVALID
;
2308 #ifdef CONFIG_VNC_TLS
2309 vs
->subauth
= VNC_AUTH_INVALID
;
2310 vs
->tls
.x509verify
= 0;
2314 int vnc_display_password(DisplayState
*ds
, const char *password
)
2316 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2323 qemu_free(vs
->password
);
2324 vs
->password
= NULL
;
2326 if (password
&& password
[0]) {
2327 if (!(vs
->password
= qemu_strdup(password
)))
2329 if (vs
->auth
== VNC_AUTH_NONE
) {
2330 vs
->auth
= VNC_AUTH_VNC
;
2333 vs
->auth
= VNC_AUTH_NONE
;
2339 char *vnc_display_local_addr(DisplayState
*ds
)
2341 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2343 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2346 int vnc_display_open(DisplayState
*ds
, const char *display
)
2348 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2349 const char *options
;
2352 #ifdef CONFIG_VNC_TLS
2353 int tls
= 0, x509
= 0;
2355 #ifdef CONFIG_VNC_SASL
2360 int lock_key_sync
= 1;
2364 vnc_display_close(ds
);
2365 if (strcmp(display
, "none") == 0)
2368 if (!(vs
->display
= strdup(display
)))
2372 while ((options
= strchr(options
, ','))) {
2374 if (strncmp(options
, "password", 8) == 0) {
2375 password
= 1; /* Require password auth */
2376 } else if (strncmp(options
, "reverse", 7) == 0) {
2378 } else if (strncmp(options
, "no-lock-key-sync", 9) == 0) {
2380 #ifdef CONFIG_VNC_SASL
2381 } else if (strncmp(options
, "sasl", 4) == 0) {
2382 sasl
= 1; /* Require SASL auth */
2384 #ifdef CONFIG_VNC_TLS
2385 } else if (strncmp(options
, "tls", 3) == 0) {
2386 tls
= 1; /* Require TLS */
2387 } else if (strncmp(options
, "x509", 4) == 0) {
2389 x509
= 1; /* Require x509 certificates */
2390 if (strncmp(options
, "x509verify", 10) == 0)
2391 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2393 /* Now check for 'x509=/some/path' postfix
2394 * and use that to setup x509 certificate/key paths */
2395 start
= strchr(options
, '=');
2396 end
= strchr(options
, ',');
2397 if (start
&& (!end
|| (start
< end
))) {
2398 int len
= end
? end
-(start
+1) : strlen(start
+1);
2399 char *path
= qemu_strndup(start
+ 1, len
);
2401 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2402 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2403 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2405 qemu_free(vs
->display
);
2411 fprintf(stderr
, "No certificate path provided\n");
2412 qemu_free(vs
->display
);
2417 } else if (strncmp(options
, "acl", 3) == 0) {
2422 #ifdef CONFIG_VNC_TLS
2423 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2424 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2425 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2430 #ifdef CONFIG_VNC_SASL
2432 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2433 fprintf(stderr
, "Failed to create username ACL\n");
2440 * Combinations we support here:
2442 * - no-auth (clear text, no auth)
2443 * - password (clear text, weak auth)
2444 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2445 * - tls (encrypt, weak anonymous creds, no auth)
2446 * - tls + password (encrypt, weak anonymous creds, weak auth)
2447 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2448 * - tls + x509 (encrypt, good x509 creds, no auth)
2449 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2450 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2452 * NB1. TLS is a stackable auth scheme.
2453 * NB2. the x509 schemes have option to validate a client cert dname
2456 #ifdef CONFIG_VNC_TLS
2458 vs
->auth
= VNC_AUTH_VENCRYPT
;
2460 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2461 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2463 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2464 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2467 #endif /* CONFIG_VNC_TLS */
2468 VNC_DEBUG("Initializing VNC server with password auth\n");
2469 vs
->auth
= VNC_AUTH_VNC
;
2470 #ifdef CONFIG_VNC_TLS
2471 vs
->subauth
= VNC_AUTH_INVALID
;
2473 #endif /* CONFIG_VNC_TLS */
2474 #ifdef CONFIG_VNC_SASL
2476 #ifdef CONFIG_VNC_TLS
2478 vs
->auth
= VNC_AUTH_VENCRYPT
;
2480 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2481 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
2483 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2484 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
2487 #endif /* CONFIG_VNC_TLS */
2488 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2489 vs
->auth
= VNC_AUTH_SASL
;
2490 #ifdef CONFIG_VNC_TLS
2491 vs
->subauth
= VNC_AUTH_INVALID
;
2493 #endif /* CONFIG_VNC_TLS */
2494 #endif /* CONFIG_VNC_SASL */
2496 #ifdef CONFIG_VNC_TLS
2498 vs
->auth
= VNC_AUTH_VENCRYPT
;
2500 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2501 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2503 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2504 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2508 VNC_DEBUG("Initializing VNC server with no auth\n");
2509 vs
->auth
= VNC_AUTH_NONE
;
2510 #ifdef CONFIG_VNC_TLS
2511 vs
->subauth
= VNC_AUTH_INVALID
;
2516 #ifdef CONFIG_VNC_SASL
2517 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
2518 fprintf(stderr
, "Failed to initialize SASL auth %s",
2519 sasl_errstring(saslErr
, NULL
, NULL
));
2525 vs
->lock_key_sync
= lock_key_sync
;
2528 /* connect to viewer */
2529 if (strncmp(display
, "unix:", 5) == 0)
2530 vs
->lsock
= unix_connect(display
+5);
2532 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2533 if (-1 == vs
->lsock
) {
2538 int csock
= vs
->lsock
;
2540 vnc_connect(vs
, csock
);
2545 /* listen for connects */
2547 dpy
= qemu_malloc(256);
2548 if (strncmp(display
, "unix:", 5) == 0) {
2549 pstrcpy(dpy
, 256, "unix:");
2550 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2552 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2554 if (-1 == vs
->lsock
) {
2562 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);