2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-common.h"
30 #include "qemu_socket.h"
31 #include "qemu-timer.h"
32 #include "audio/audio.h"
35 #define VNC_REFRESH_INTERVAL (1000 / 30)
38 #include "vnc_keysym.h"
43 #include <gnutls/gnutls.h>
44 #include <gnutls/x509.h>
45 #endif /* CONFIG_VNC_TLS */
47 // #define _VNC_DEBUG 1
50 #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
52 #if defined(CONFIG_VNC_TLS) && _VNC_DEBUG >= 2
53 /* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
54 static void vnc_debug_gnutls_log(int level
, const char* str
) {
55 VNC_DEBUG("%d %s", level
, str
);
57 #endif /* CONFIG_VNC_TLS && _VNC_DEBUG */
59 #define VNC_DEBUG(fmt, ...) do { } while (0)
62 #define count_bits(c, v) { \
63 for (c = 0; v; v >>= 1) \
76 typedef struct VncState VncState
;
78 typedef int VncReadEvent(VncState
*vs
, uint8_t *data
, size_t len
);
80 typedef void VncWritePixels(VncState
*vs
, void *data
, int size
);
82 typedef void VncSendHextileTile(VncState
*vs
,
83 int x
, int y
, int w
, int h
,
86 int *has_bg
, int *has_fg
);
88 #define VNC_MAX_WIDTH 2048
89 #define VNC_MAX_HEIGHT 2048
90 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
92 #define VNC_AUTH_CHALLENGE_SIZE 16
94 typedef struct VncDisplay VncDisplay
;
101 kbd_layout_t
*kbd_layout
;
106 #ifdef CONFIG_VNC_TLS
124 uint32_t dirty_row
[VNC_MAX_HEIGHT
][VNC_DIRTY_WORDS
];
131 uint32_t vnc_encoding
;
132 uint8_t tight_quality
;
133 uint8_t tight_compression
;
138 char challenge
[VNC_AUTH_CHALLENGE_SIZE
];
140 #ifdef CONFIG_VNC_TLS
142 gnutls_session_t tls_session
;
147 /* current output mode information */
148 VncWritePixels
*write_pixels
;
149 VncSendHextileTile
*send_hextile_tile
;
150 DisplaySurface clientds
, serverds
;
152 CaptureVoiceOut
*audio_cap
;
153 struct audsettings as
;
155 VncReadEvent
*read_handler
;
156 size_t read_handler_expect
;
158 uint8_t modifiers_state
[256];
162 z_stream zlib_stream
[4];
167 static VncDisplay
*vnc_display
; /* needed for info vnc */
168 static DisplayChangeListener
*dcl
;
170 static char *addr_to_string(const char *format
,
171 struct sockaddr_storage
*sa
,
174 char host
[NI_MAXHOST
];
175 char serv
[NI_MAXSERV
];
178 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
181 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
182 VNC_DEBUG("Cannot resolve address %d: %s\n",
183 err
, gai_strerror(err
));
187 if (asprintf(&addr
, format
, host
, serv
) < 0)
193 static char *vnc_socket_local_addr(const char *format
, int fd
) {
194 struct sockaddr_storage sa
;
198 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
201 return addr_to_string(format
, &sa
, salen
);
204 static char *vnc_socket_remote_addr(const char *format
, int fd
) {
205 struct sockaddr_storage sa
;
209 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
212 return addr_to_string(format
, &sa
, salen
);
215 static const char *vnc_auth_name(VncDisplay
*vd
) {
217 case VNC_AUTH_INVALID
:
233 case VNC_AUTH_VENCRYPT
:
234 #ifdef CONFIG_VNC_TLS
235 switch (vd
->subauth
) {
236 case VNC_AUTH_VENCRYPT_PLAIN
:
237 return "vencrypt+plain";
238 case VNC_AUTH_VENCRYPT_TLSNONE
:
239 return "vencrypt+tls+none";
240 case VNC_AUTH_VENCRYPT_TLSVNC
:
241 return "vencrypt+tls+vnc";
242 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
243 return "vencrypt+tls+plain";
244 case VNC_AUTH_VENCRYPT_X509NONE
:
245 return "vencrypt+x509+none";
246 case VNC_AUTH_VENCRYPT_X509VNC
:
247 return "vencrypt+x509+vnc";
248 case VNC_AUTH_VENCRYPT_X509PLAIN
:
249 return "vencrypt+x509+plain";
260 #define VNC_SOCKET_FORMAT_PRETTY "local %s:%s"
262 static void do_info_vnc_client(Monitor
*mon
, VncState
*client
)
265 vnc_socket_remote_addr(" address: %s:%s\n",
270 monitor_printf(mon
, "Client:\n");
271 monitor_printf(mon
, "%s", clientAddr
);
275 void do_info_vnc(Monitor
*mon
)
277 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
278 monitor_printf(mon
, "Server: disabled\n");
280 char *serverAddr
= vnc_socket_local_addr(" address: %s:%s\n",
286 monitor_printf(mon
, "Server:\n");
287 monitor_printf(mon
, "%s", serverAddr
);
289 monitor_printf(mon
, " auth: %s\n", vnc_auth_name(vnc_display
));
291 if (vnc_display
->clients
) {
292 VncState
*client
= vnc_display
->clients
;
294 do_info_vnc_client(mon
, client
);
295 client
= client
->next
;
298 monitor_printf(mon
, "Client: none\n");
303 static inline uint32_t vnc_has_feature(VncState
*vs
, int feature
) {
304 return (vs
->features
& (1 << feature
));
308 1) Get the queue working for IO.
309 2) there is some weirdness when using the -S option (the screen is grey
310 and not totally invalidated
311 3) resolutions > 1024
314 static void vnc_write(VncState
*vs
, const void *data
, size_t len
);
315 static void vnc_write_u32(VncState
*vs
, uint32_t value
);
316 static void vnc_write_s32(VncState
*vs
, int32_t value
);
317 static void vnc_write_u16(VncState
*vs
, uint16_t value
);
318 static void vnc_write_u8(VncState
*vs
, uint8_t value
);
319 static void vnc_flush(VncState
*vs
);
320 static void vnc_update_client(void *opaque
);
321 static void vnc_client_read(void *opaque
);
323 static void vnc_colordepth(VncState
*vs
);
325 static inline void vnc_set_bit(uint32_t *d
, int k
)
327 d
[k
>> 5] |= 1 << (k
& 0x1f);
330 static inline void vnc_clear_bit(uint32_t *d
, int k
)
332 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
335 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
345 d
[j
++] = (1 << n
) - 1;
350 static inline int vnc_get_bit(const uint32_t *d
, int k
)
352 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
355 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
359 for(i
= 0; i
< nb_words
; i
++) {
360 if ((d1
[i
] & d2
[i
]) != 0)
366 static void vnc_update(VncState
*vs
, int x
, int y
, int w
, int h
)
372 /* round x down to ensure the loop only spans one 16-pixel block per,
373 iteration. otherwise, if (x % 16) != 0, the last iteration may span
374 two 16-pixel blocks but we only mark the first as dirty
379 x
= MIN(x
, vs
->serverds
.width
);
380 y
= MIN(y
, vs
->serverds
.height
);
381 w
= MIN(x
+ w
, vs
->serverds
.width
) - x
;
382 h
= MIN(h
, vs
->serverds
.height
);
385 for (i
= 0; i
< w
; i
+= 16)
386 vnc_set_bit(vs
->dirty_row
[y
], (x
+ i
) / 16);
389 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
391 VncDisplay
*vd
= ds
->opaque
;
392 VncState
*vs
= vd
->clients
;
394 vnc_update(vs
, x
, y
, w
, h
);
399 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
402 vnc_write_u16(vs
, x
);
403 vnc_write_u16(vs
, y
);
404 vnc_write_u16(vs
, w
);
405 vnc_write_u16(vs
, h
);
407 vnc_write_s32(vs
, encoding
);
410 static void buffer_reserve(Buffer
*buffer
, size_t len
)
412 if ((buffer
->capacity
- buffer
->offset
) < len
) {
413 buffer
->capacity
+= (len
+ 1024);
414 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
415 if (buffer
->buffer
== NULL
) {
416 fprintf(stderr
, "vnc: out of memory\n");
422 static int buffer_empty(Buffer
*buffer
)
424 return buffer
->offset
== 0;
427 static uint8_t *buffer_end(Buffer
*buffer
)
429 return buffer
->buffer
+ buffer
->offset
;
432 static void buffer_reset(Buffer
*buffer
)
437 static void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
439 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
440 buffer
->offset
+= len
;
443 static void vnc_resize(VncState
*vs
)
445 DisplayState
*ds
= vs
->ds
;
449 vs
->old_data
= qemu_realloc(vs
->old_data
, ds_get_linesize(ds
) * ds_get_height(ds
));
451 if (vs
->old_data
== NULL
) {
452 fprintf(stderr
, "vnc: memory allocation failed\n");
456 if (ds_get_bytes_per_pixel(ds
) != vs
->serverds
.pf
.bytes_per_pixel
)
457 console_color_init(ds
);
459 size_changed
= ds_get_width(ds
) != vs
->serverds
.width
||
460 ds_get_height(ds
) != vs
->serverds
.height
;
461 vs
->serverds
= *(ds
->surface
);
463 if (vs
->csock
!= -1 && vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
464 vnc_write_u8(vs
, 0); /* msg id */
466 vnc_write_u16(vs
, 1); /* number of rects */
467 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(ds
), ds_get_height(ds
),
468 VNC_ENCODING_DESKTOPRESIZE
);
473 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
474 memset(vs
->old_data
, 42, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
477 static void vnc_dpy_resize(DisplayState
*ds
)
479 VncDisplay
*vd
= ds
->opaque
;
480 VncState
*vs
= vd
->clients
;
488 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
490 vnc_write(vs
, pixels
, size
);
493 /* slowest but generic code. */
494 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
498 r
= ((((v
& vs
->serverds
.pf
.rmask
) >> vs
->serverds
.pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
499 vs
->serverds
.pf
.rbits
);
500 g
= ((((v
& vs
->serverds
.pf
.gmask
) >> vs
->serverds
.pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
501 vs
->serverds
.pf
.gbits
);
502 b
= ((((v
& vs
->serverds
.pf
.bmask
) >> vs
->serverds
.pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
503 vs
->serverds
.pf
.bbits
);
504 v
= (r
<< vs
->clientds
.pf
.rshift
) |
505 (g
<< vs
->clientds
.pf
.gshift
) |
506 (b
<< vs
->clientds
.pf
.bshift
);
507 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
512 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
522 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
537 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
541 if (vs
->serverds
.pf
.bytes_per_pixel
== 4) {
542 uint32_t *pixels
= pixels1
;
545 for(i
= 0; i
< n
; i
++) {
546 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
547 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
549 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 2) {
550 uint16_t *pixels
= pixels1
;
553 for(i
= 0; i
< n
; i
++) {
554 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
555 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
557 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 1) {
558 uint8_t *pixels
= pixels1
;
561 for(i
= 0; i
< n
; i
++) {
562 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
563 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
566 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
570 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
575 row
= ds_get_data(vs
->ds
) + y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
576 for (i
= 0; i
< h
; i
++) {
577 vs
->write_pixels(vs
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
578 row
+= ds_get_linesize(vs
->ds
);
582 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
584 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
585 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
589 #include "vnchextile.h"
593 #include "vnchextile.h"
597 #include "vnchextile.h"
602 #include "vnchextile.h"
608 #include "vnchextile.h"
614 #include "vnchextile.h"
618 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
622 uint8_t *last_fg
, *last_bg
;
624 last_fg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
625 last_bg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
627 for (j
= y
; j
< (y
+ h
); j
+= 16) {
628 for (i
= x
; i
< (x
+ w
); i
+= 16) {
629 vs
->send_hextile_tile(vs
, i
, j
,
630 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
631 last_bg
, last_fg
, &has_bg
, &has_fg
);
639 static void vnc_zlib_init(VncState
*vs
)
642 for (i
=0; i
<(sizeof(vs
->zlib_stream
) / sizeof(z_stream
)); i
++)
643 vs
->zlib_stream
[i
].opaque
= NULL
;
646 static void vnc_zlib_start(VncState
*vs
)
648 buffer_reset(&vs
->zlib
);
650 // make the output buffer be the zlib buffer, so we can compress it later
651 vs
->zlib_tmp
= vs
->output
;
652 vs
->output
= vs
->zlib
;
655 static int vnc_zlib_stop(VncState
*vs
, int stream_id
)
657 z_streamp zstream
= &vs
->zlib_stream
[stream_id
];
660 // switch back to normal output/zlib buffers
661 vs
->zlib
= vs
->output
;
662 vs
->output
= vs
->zlib_tmp
;
664 // compress the zlib buffer
666 // initialize the stream
667 // XXX need one stream per session
668 if (zstream
->opaque
!= vs
) {
671 VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id
);
672 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
673 zstream
->zalloc
= Z_NULL
;
674 zstream
->zfree
= Z_NULL
;
676 err
= deflateInit2(zstream
, vs
->tight_compression
, Z_DEFLATED
, MAX_WBITS
,
677 MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
680 fprintf(stderr
, "VNC: error initializing zlib\n");
684 zstream
->opaque
= vs
;
687 // XXX what to do if tight_compression changed in between?
689 // reserve memory in output buffer
690 buffer_reserve(&vs
->output
, vs
->zlib
.offset
+ 64);
693 zstream
->next_in
= vs
->zlib
.buffer
;
694 zstream
->avail_in
= vs
->zlib
.offset
;
695 zstream
->next_out
= vs
->output
.buffer
+ vs
->output
.offset
;
696 zstream
->avail_out
= vs
->output
.capacity
- vs
->output
.offset
;
697 zstream
->data_type
= Z_BINARY
;
698 previous_out
= zstream
->total_out
;
701 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
702 fprintf(stderr
, "VNC: error during zlib compression\n");
706 vs
->output
.offset
= vs
->output
.capacity
- zstream
->avail_out
;
707 return zstream
->total_out
- previous_out
;
710 static void send_framebuffer_update_zlib(VncState
*vs
, int x
, int y
, int w
, int h
)
712 int old_offset
, new_offset
, bytes_written
;
714 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_ZLIB
);
716 // remember where we put in the follow-up size
717 old_offset
= vs
->output
.offset
;
718 vnc_write_s32(vs
, 0);
720 // compress the stream
722 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
723 bytes_written
= vnc_zlib_stop(vs
, 0);
725 if (bytes_written
== -1)
729 new_offset
= vs
->output
.offset
;
730 vs
->output
.offset
= old_offset
;
731 vnc_write_u32(vs
, bytes_written
);
732 vs
->output
.offset
= new_offset
;
735 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
737 switch(vs
->vnc_encoding
) {
738 case VNC_ENCODING_ZLIB
:
739 send_framebuffer_update_zlib(vs
, x
, y
, w
, h
);
741 case VNC_ENCODING_HEXTILE
:
742 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
743 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
746 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
747 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
752 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
754 vnc_update_client(vs
);
756 vnc_write_u8(vs
, 0); /* msg id */
758 vnc_write_u16(vs
, 1); /* number of rects */
759 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
760 vnc_write_u16(vs
, src_x
);
761 vnc_write_u16(vs
, src_y
);
765 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
767 VncDisplay
*vd
= ds
->opaque
;
768 VncState
*vs
= vd
->clients
;
770 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
))
771 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
773 vnc_update(vs
, dst_x
, dst_y
, w
, h
);
778 static int find_dirty_height(VncState
*vs
, int y
, int last_x
, int x
)
782 for (h
= 1; h
< (vs
->serverds
.height
- y
); h
++) {
784 if (!vnc_get_bit(vs
->dirty_row
[y
+ h
], last_x
))
786 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
787 vnc_clear_bit(vs
->dirty_row
[y
+ h
], tmp_x
);
793 static void vnc_update_client(void *opaque
)
795 VncState
*vs
= opaque
;
796 if (vs
->need_update
&& vs
->csock
!= -1) {
800 uint32_t width_mask
[VNC_DIRTY_WORDS
];
807 vnc_set_bits(width_mask
, (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
809 /* Walk through the dirty map and eliminate tiles that
810 really aren't dirty */
811 row
= ds_get_data(vs
->ds
);
812 old_row
= vs
->old_data
;
814 for (y
= 0; y
< ds_get_height(vs
->ds
); y
++) {
815 if (vnc_and_bits(vs
->dirty_row
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
821 old_ptr
= (char*)old_row
;
823 for (x
= 0; x
< ds_get_width(vs
->ds
); x
+= 16) {
824 if (memcmp(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
)) == 0) {
825 vnc_clear_bit(vs
->dirty_row
[y
], (x
/ 16));
828 memcpy(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
));
831 ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
832 old_ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
836 row
+= ds_get_linesize(vs
->ds
);
837 old_row
+= ds_get_linesize(vs
->ds
);
840 if (!has_dirty
&& !vs
->audio_cap
) {
841 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
845 /* Count rectangles */
847 vnc_write_u8(vs
, 0); /* msg id */
849 saved_offset
= vs
->output
.offset
;
850 vnc_write_u16(vs
, 0);
852 for (y
= 0; y
< vs
->serverds
.height
; y
++) {
855 for (x
= 0; x
< vs
->serverds
.width
/ 16; x
++) {
856 if (vnc_get_bit(vs
->dirty_row
[y
], x
)) {
860 vnc_clear_bit(vs
->dirty_row
[y
], x
);
863 int h
= find_dirty_height(vs
, y
, last_x
, x
);
864 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
871 int h
= find_dirty_height(vs
, y
, last_x
, x
);
872 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
876 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
877 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
882 if (vs
->csock
!= -1) {
883 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
889 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
891 VncState
*vs
= opaque
;
894 case AUD_CNOTIFY_DISABLE
:
895 vnc_write_u8(vs
, 255);
897 vnc_write_u16(vs
, 0);
901 case AUD_CNOTIFY_ENABLE
:
902 vnc_write_u8(vs
, 255);
904 vnc_write_u16(vs
, 1);
910 static void audio_capture_destroy(void *opaque
)
914 static void audio_capture(void *opaque
, void *buf
, int size
)
916 VncState
*vs
= opaque
;
918 vnc_write_u8(vs
, 255);
920 vnc_write_u16(vs
, 2);
921 vnc_write_u32(vs
, size
);
922 vnc_write(vs
, buf
, size
);
926 static void audio_add(VncState
*vs
)
928 Monitor
*mon
= cur_mon
;
929 struct audio_capture_ops ops
;
932 monitor_printf(mon
, "audio already running\n");
936 ops
.notify
= audio_capture_notify
;
937 ops
.destroy
= audio_capture_destroy
;
938 ops
.capture
= audio_capture
;
940 vs
->audio_cap
= AUD_add_capture(NULL
, &vs
->as
, &ops
, vs
);
941 if (!vs
->audio_cap
) {
942 monitor_printf(mon
, "Failed to add audio capture\n");
946 static void audio_del(VncState
*vs
)
949 AUD_del_capture(vs
->audio_cap
, vs
);
950 vs
->audio_cap
= NULL
;
954 static int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
956 if (ret
== 0 || ret
== -1) {
958 switch (last_errno
) {
970 VNC_DEBUG("Closing down client sock %d %d\n", ret
, ret
< 0 ? last_errno
: 0);
971 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
972 closesocket(vs
->csock
);
973 qemu_del_timer(vs
->timer
);
974 qemu_free_timer(vs
->timer
);
975 if (vs
->input
.buffer
) qemu_free(vs
->input
.buffer
);
976 if (vs
->output
.buffer
) qemu_free(vs
->output
.buffer
);
977 #ifdef CONFIG_VNC_TLS
978 if (vs
->tls_session
) {
979 gnutls_deinit(vs
->tls_session
);
980 vs
->tls_session
= NULL
;
982 #endif /* CONFIG_VNC_TLS */
985 VncState
*p
, *parent
= NULL
;
986 for (p
= vs
->vd
->clients
; p
!= NULL
; p
= p
->next
) {
989 parent
->next
= p
->next
;
991 vs
->vd
->clients
= p
->next
;
996 if (!vs
->vd
->clients
)
999 qemu_free(vs
->old_data
);
1007 static void vnc_client_error(VncState
*vs
)
1009 vnc_client_io_error(vs
, -1, EINVAL
);
1012 static void vnc_client_write(void *opaque
)
1015 VncState
*vs
= opaque
;
1017 #ifdef CONFIG_VNC_TLS
1018 if (vs
->tls_session
) {
1019 ret
= gnutls_write(vs
->tls_session
, vs
->output
.buffer
, vs
->output
.offset
);
1021 if (ret
== GNUTLS_E_AGAIN
)
1028 #endif /* CONFIG_VNC_TLS */
1029 ret
= send(vs
->csock
, vs
->output
.buffer
, vs
->output
.offset
, 0);
1030 ret
= vnc_client_io_error(vs
, ret
, socket_error());
1034 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1035 vs
->output
.offset
-= ret
;
1037 if (vs
->output
.offset
== 0) {
1038 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1042 static void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1044 vs
->read_handler
= func
;
1045 vs
->read_handler_expect
= expecting
;
1048 static void vnc_client_read(void *opaque
)
1050 VncState
*vs
= opaque
;
1053 buffer_reserve(&vs
->input
, 4096);
1055 #ifdef CONFIG_VNC_TLS
1056 if (vs
->tls_session
) {
1057 ret
= gnutls_read(vs
->tls_session
, buffer_end(&vs
->input
), 4096);
1059 if (ret
== GNUTLS_E_AGAIN
)
1066 #endif /* CONFIG_VNC_TLS */
1067 ret
= recv(vs
->csock
, buffer_end(&vs
->input
), 4096, 0);
1068 ret
= vnc_client_io_error(vs
, ret
, socket_error());
1072 vs
->input
.offset
+= ret
;
1074 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1075 size_t len
= vs
->read_handler_expect
;
1078 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1079 if (vs
->csock
== -1)
1083 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1084 vs
->input
.offset
-= len
;
1086 vs
->read_handler_expect
= ret
;
1091 static void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1093 buffer_reserve(&vs
->output
, len
);
1095 if (buffer_empty(&vs
->output
)) {
1096 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1099 buffer_append(&vs
->output
, data
, len
);
1102 static void vnc_write_s32(VncState
*vs
, int32_t value
)
1104 vnc_write_u32(vs
, *(uint32_t *)&value
);
1107 static void vnc_write_u32(VncState
*vs
, uint32_t value
)
1111 buf
[0] = (value
>> 24) & 0xFF;
1112 buf
[1] = (value
>> 16) & 0xFF;
1113 buf
[2] = (value
>> 8) & 0xFF;
1114 buf
[3] = value
& 0xFF;
1116 vnc_write(vs
, buf
, 4);
1119 static void vnc_write_u16(VncState
*vs
, uint16_t value
)
1123 buf
[0] = (value
>> 8) & 0xFF;
1124 buf
[1] = value
& 0xFF;
1126 vnc_write(vs
, buf
, 2);
1129 static void vnc_write_u8(VncState
*vs
, uint8_t value
)
1131 vnc_write(vs
, (char *)&value
, 1);
1134 static void vnc_flush(VncState
*vs
)
1136 if (vs
->output
.offset
)
1137 vnc_client_write(vs
);
1140 static uint8_t read_u8(uint8_t *data
, size_t offset
)
1142 return data
[offset
];
1145 static uint16_t read_u16(uint8_t *data
, size_t offset
)
1147 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1150 static int32_t read_s32(uint8_t *data
, size_t offset
)
1152 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1153 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1156 static uint32_t read_u32(uint8_t *data
, size_t offset
)
1158 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1159 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1162 #ifdef CONFIG_VNC_TLS
1163 static ssize_t
vnc_tls_push(gnutls_transport_ptr_t transport
,
1166 struct VncState
*vs
= (struct VncState
*)transport
;
1170 ret
= send(vs
->csock
, data
, len
, 0);
1180 static ssize_t
vnc_tls_pull(gnutls_transport_ptr_t transport
,
1183 struct VncState
*vs
= (struct VncState
*)transport
;
1187 ret
= recv(vs
->csock
, data
, len
, 0);
1195 #endif /* CONFIG_VNC_TLS */
1197 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1201 static void check_pointer_type_change(VncState
*vs
, int absolute
)
1203 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1204 vnc_write_u8(vs
, 0);
1205 vnc_write_u8(vs
, 0);
1206 vnc_write_u16(vs
, 1);
1207 vnc_framebuffer_update(vs
, absolute
, 0,
1208 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1209 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1212 vs
->absolute
= absolute
;
1215 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1220 if (button_mask
& 0x01)
1221 buttons
|= MOUSE_EVENT_LBUTTON
;
1222 if (button_mask
& 0x02)
1223 buttons
|= MOUSE_EVENT_MBUTTON
;
1224 if (button_mask
& 0x04)
1225 buttons
|= MOUSE_EVENT_RBUTTON
;
1226 if (button_mask
& 0x08)
1228 if (button_mask
& 0x10)
1232 kbd_mouse_event(x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1),
1233 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1),
1235 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1239 kbd_mouse_event(x
, y
, dz
, buttons
);
1241 if (vs
->last_x
!= -1)
1242 kbd_mouse_event(x
- vs
->last_x
,
1249 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1252 static void reset_keys(VncState
*vs
)
1255 for(i
= 0; i
< 256; i
++) {
1256 if (vs
->modifiers_state
[i
]) {
1258 kbd_put_keycode(0xe0);
1259 kbd_put_keycode(i
| 0x80);
1260 vs
->modifiers_state
[i
] = 0;
1265 static void press_key(VncState
*vs
, int keysym
)
1267 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & 0x7f);
1268 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) | 0x80);
1271 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1273 /* QEMU console switch */
1275 case 0x2a: /* Left Shift */
1276 case 0x36: /* Right Shift */
1277 case 0x1d: /* Left CTRL */
1278 case 0x9d: /* Right CTRL */
1279 case 0x38: /* Left ALT */
1280 case 0xb8: /* Right ALT */
1282 vs
->modifiers_state
[keycode
] = 1;
1284 vs
->modifiers_state
[keycode
] = 0;
1286 case 0x02 ... 0x0a: /* '1' to '9' keys */
1287 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1288 /* Reset the modifiers sent to the current console */
1290 console_select(keycode
- 0x02);
1294 case 0x3a: /* CapsLock */
1295 case 0x45: /* NumLock */
1297 vs
->modifiers_state
[keycode
] ^= 1;
1301 if (keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1302 /* If the numlock state needs to change then simulate an additional
1303 keypress before sending this one. This will happen if the user
1304 toggles numlock away from the VNC window.
1306 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1307 if (!vs
->modifiers_state
[0x45]) {
1308 vs
->modifiers_state
[0x45] = 1;
1309 press_key(vs
, 0xff7f);
1312 if (vs
->modifiers_state
[0x45]) {
1313 vs
->modifiers_state
[0x45] = 0;
1314 press_key(vs
, 0xff7f);
1319 if (is_graphic_console()) {
1321 kbd_put_keycode(0xe0);
1323 kbd_put_keycode(keycode
& 0x7f);
1325 kbd_put_keycode(keycode
| 0x80);
1327 /* QEMU console emulation */
1330 case 0x2a: /* Left Shift */
1331 case 0x36: /* Right Shift */
1332 case 0x1d: /* Left CTRL */
1333 case 0x9d: /* Right CTRL */
1334 case 0x38: /* Left ALT */
1335 case 0xb8: /* Right ALT */
1338 kbd_put_keysym(QEMU_KEY_UP
);
1341 kbd_put_keysym(QEMU_KEY_DOWN
);
1344 kbd_put_keysym(QEMU_KEY_LEFT
);
1347 kbd_put_keysym(QEMU_KEY_RIGHT
);
1350 kbd_put_keysym(QEMU_KEY_DELETE
);
1353 kbd_put_keysym(QEMU_KEY_HOME
);
1356 kbd_put_keysym(QEMU_KEY_END
);
1359 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1362 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1365 kbd_put_keysym(sym
);
1372 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1376 if (sym
>= 'A' && sym
<= 'Z' && is_graphic_console())
1377 sym
= sym
- 'A' + 'a';
1379 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, sym
& 0xFFFF);
1380 do_key_event(vs
, down
, keycode
, sym
);
1383 static void ext_key_event(VncState
*vs
, int down
,
1384 uint32_t sym
, uint16_t keycode
)
1386 /* if the user specifies a keyboard layout, always use it */
1387 if (keyboard_layout
)
1388 key_event(vs
, down
, sym
);
1390 do_key_event(vs
, down
, keycode
, sym
);
1393 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1394 int x_position
, int y_position
,
1397 if (x_position
> ds_get_width(vs
->ds
))
1398 x_position
= ds_get_width(vs
->ds
);
1399 if (y_position
> ds_get_height(vs
->ds
))
1400 y_position
= ds_get_height(vs
->ds
);
1401 if (x_position
+ w
>= ds_get_width(vs
->ds
))
1402 w
= ds_get_width(vs
->ds
) - x_position
;
1403 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1404 h
= ds_get_height(vs
->ds
) - y_position
;
1407 vs
->need_update
= 1;
1409 char *old_row
= vs
->old_data
+ y_position
* ds_get_linesize(vs
->ds
);
1411 for (i
= 0; i
< h
; i
++) {
1412 vnc_set_bits(vs
->dirty_row
[y_position
+ i
],
1413 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1414 memset(old_row
, 42, ds_get_width(vs
->ds
) * ds_get_bytes_per_pixel(vs
->ds
));
1415 old_row
+= ds_get_linesize(vs
->ds
);
1420 static void send_ext_key_event_ack(VncState
*vs
)
1422 vnc_write_u8(vs
, 0);
1423 vnc_write_u8(vs
, 0);
1424 vnc_write_u16(vs
, 1);
1425 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1426 VNC_ENCODING_EXT_KEY_EVENT
);
1430 static void send_ext_audio_ack(VncState
*vs
)
1432 vnc_write_u8(vs
, 0);
1433 vnc_write_u8(vs
, 0);
1434 vnc_write_u16(vs
, 1);
1435 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1436 VNC_ENCODING_AUDIO
);
1440 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1443 unsigned int enc
= 0;
1447 vs
->vnc_encoding
= 0;
1448 vs
->tight_compression
= 9;
1449 vs
->tight_quality
= 9;
1452 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1455 case VNC_ENCODING_RAW
:
1456 vs
->vnc_encoding
= enc
;
1458 case VNC_ENCODING_COPYRECT
:
1459 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1461 case VNC_ENCODING_HEXTILE
:
1462 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1463 vs
->vnc_encoding
= enc
;
1465 case VNC_ENCODING_ZLIB
:
1466 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1467 vs
->vnc_encoding
= enc
;
1469 case VNC_ENCODING_DESKTOPRESIZE
:
1470 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1472 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1473 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1475 case VNC_ENCODING_EXT_KEY_EVENT
:
1476 send_ext_key_event_ack(vs
);
1478 case VNC_ENCODING_AUDIO
:
1479 send_ext_audio_ack(vs
);
1481 case VNC_ENCODING_WMVi
:
1482 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1484 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1485 vs
->tight_compression
= (enc
& 0x0F);
1487 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1488 vs
->tight_quality
= (enc
& 0x0F);
1491 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1496 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1499 static void set_pixel_conversion(VncState
*vs
)
1501 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1502 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1503 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1504 vs
->write_pixels
= vnc_write_pixels_copy
;
1505 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1507 vs
->send_hextile_tile
= send_hextile_tile_8
;
1510 vs
->send_hextile_tile
= send_hextile_tile_16
;
1513 vs
->send_hextile_tile
= send_hextile_tile_32
;
1517 vs
->write_pixels
= vnc_write_pixels_generic
;
1518 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1520 vs
->send_hextile_tile
= send_hextile_tile_generic_8
;
1523 vs
->send_hextile_tile
= send_hextile_tile_generic_16
;
1526 vs
->send_hextile_tile
= send_hextile_tile_generic_32
;
1532 static void set_pixel_format(VncState
*vs
,
1533 int bits_per_pixel
, int depth
,
1534 int big_endian_flag
, int true_color_flag
,
1535 int red_max
, int green_max
, int blue_max
,
1536 int red_shift
, int green_shift
, int blue_shift
)
1538 if (!true_color_flag
) {
1539 vnc_client_error(vs
);
1543 vs
->clientds
= vs
->serverds
;
1544 vs
->clientds
.pf
.rmax
= red_max
;
1545 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1546 vs
->clientds
.pf
.rshift
= red_shift
;
1547 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1548 vs
->clientds
.pf
.gmax
= green_max
;
1549 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1550 vs
->clientds
.pf
.gshift
= green_shift
;
1551 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1552 vs
->clientds
.pf
.bmax
= blue_max
;
1553 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1554 vs
->clientds
.pf
.bshift
= blue_shift
;
1555 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1556 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1557 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1558 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1559 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1561 set_pixel_conversion(vs
);
1563 vga_hw_invalidate();
1567 static void pixel_format_message (VncState
*vs
) {
1568 char pad
[3] = { 0, 0, 0 };
1570 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1571 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1573 #ifdef WORDS_BIGENDIAN
1574 vnc_write_u8(vs
, 1); /* big-endian-flag */
1576 vnc_write_u8(vs
, 0); /* big-endian-flag */
1578 vnc_write_u8(vs
, 1); /* true-color-flag */
1579 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1580 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1581 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1582 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1583 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1584 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1585 if (vs
->ds
->surface
->pf
.bits_per_pixel
== 32)
1586 vs
->send_hextile_tile
= send_hextile_tile_32
;
1587 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 16)
1588 vs
->send_hextile_tile
= send_hextile_tile_16
;
1589 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 8)
1590 vs
->send_hextile_tile
= send_hextile_tile_8
;
1591 vs
->clientds
= *(vs
->ds
->surface
);
1592 vs
->clientds
.flags
|= ~QEMU_ALLOCATED_FLAG
;
1593 vs
->write_pixels
= vnc_write_pixels_copy
;
1595 vnc_write(vs
, pad
, 3); /* padding */
1598 static void vnc_dpy_setdata(DisplayState
*ds
)
1600 /* We don't have to do anything */
1603 static void vnc_colordepth(VncState
*vs
)
1605 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1606 /* Sending a WMVi message to notify the client*/
1607 vnc_write_u8(vs
, 0); /* msg id */
1608 vnc_write_u8(vs
, 0);
1609 vnc_write_u16(vs
, 1); /* number of rects */
1610 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1611 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1612 pixel_format_message(vs
);
1615 set_pixel_conversion(vs
);
1619 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1629 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1630 read_u8(data
, 6), read_u8(data
, 7),
1631 read_u16(data
, 8), read_u16(data
, 10),
1632 read_u16(data
, 12), read_u8(data
, 14),
1633 read_u8(data
, 15), read_u8(data
, 16));
1640 limit
= read_u16(data
, 2);
1642 return 4 + (limit
* 4);
1644 limit
= read_u16(data
, 2);
1646 for (i
= 0; i
< limit
; i
++) {
1647 int32_t val
= read_s32(data
, 4 + (i
* 4));
1648 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1651 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1657 framebuffer_update_request(vs
,
1658 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1659 read_u16(data
, 6), read_u16(data
, 8));
1665 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1671 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1678 uint32_t dlen
= read_u32(data
, 4);
1683 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1689 switch (read_u8(data
, 1)) {
1694 ext_key_event(vs
, read_u16(data
, 2),
1695 read_u32(data
, 4), read_u32(data
, 8));
1701 switch (read_u16 (data
, 2)) {
1711 switch (read_u8(data
, 4)) {
1712 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1713 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1714 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1715 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1716 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1717 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1719 printf("Invalid audio format %d\n", read_u8(data
, 4));
1720 vnc_client_error(vs
);
1723 vs
->as
.nchannels
= read_u8(data
, 5);
1724 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
1725 printf("Invalid audio channel coount %d\n",
1727 vnc_client_error(vs
);
1730 vs
->as
.freq
= read_u32(data
, 6);
1733 printf ("Invalid audio message %d\n", read_u8(data
, 4));
1734 vnc_client_error(vs
);
1740 printf("Msg: %d\n", read_u16(data
, 0));
1741 vnc_client_error(vs
);
1746 printf("Msg: %d\n", data
[0]);
1747 vnc_client_error(vs
);
1751 vnc_read_when(vs
, protocol_client_msg
, 1);
1755 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
1760 vnc_write_u16(vs
, ds_get_width(vs
->ds
));
1761 vnc_write_u16(vs
, ds_get_height(vs
->ds
));
1763 pixel_format_message(vs
);
1766 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
1768 size
= snprintf(buf
, sizeof(buf
), "QEMU");
1770 vnc_write_u32(vs
, size
);
1771 vnc_write(vs
, buf
, size
);
1774 vnc_read_when(vs
, protocol_client_msg
, 1);
1779 static void make_challenge(VncState
*vs
)
1783 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1785 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1786 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1789 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
1791 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
1793 unsigned char key
[8];
1795 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
1796 VNC_DEBUG("No password configured on server");
1797 vnc_write_u32(vs
, 1); /* Reject auth */
1798 if (vs
->minor
>= 8) {
1799 static const char err
[] = "Authentication failed";
1800 vnc_write_u32(vs
, sizeof(err
));
1801 vnc_write(vs
, err
, sizeof(err
));
1804 vnc_client_error(vs
);
1808 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1810 /* Calculate the expected challenge response */
1811 pwlen
= strlen(vs
->vd
->password
);
1812 for (i
=0; i
<sizeof(key
); i
++)
1813 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
1815 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1816 des(response
+j
, response
+j
);
1818 /* Compare expected vs actual challenge response */
1819 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1820 VNC_DEBUG("Client challenge reponse did not match\n");
1821 vnc_write_u32(vs
, 1); /* Reject auth */
1822 if (vs
->minor
>= 8) {
1823 static const char err
[] = "Authentication failed";
1824 vnc_write_u32(vs
, sizeof(err
));
1825 vnc_write(vs
, err
, sizeof(err
));
1828 vnc_client_error(vs
);
1830 VNC_DEBUG("Accepting VNC challenge response\n");
1831 vnc_write_u32(vs
, 0); /* Accept auth */
1834 vnc_read_when(vs
, protocol_client_init
, 1);
1839 static int start_auth_vnc(VncState
*vs
)
1842 /* Send client a 'random' challenge */
1843 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1846 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1851 #ifdef CONFIG_VNC_TLS
1852 #define DH_BITS 1024
1853 static gnutls_dh_params_t dh_params
;
1855 static int vnc_tls_initialize(void)
1857 static int tlsinitialized
= 0;
1862 if (gnutls_global_init () < 0)
1865 /* XXX ought to re-generate diffie-hellmen params periodically */
1866 if (gnutls_dh_params_init (&dh_params
) < 0)
1868 if (gnutls_dh_params_generate2 (dh_params
, DH_BITS
) < 0)
1871 #if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2
1872 gnutls_global_set_log_level(10);
1873 gnutls_global_set_log_function(vnc_debug_gnutls_log
);
1881 static gnutls_anon_server_credentials
vnc_tls_initialize_anon_cred(void)
1883 gnutls_anon_server_credentials anon_cred
;
1886 if ((ret
= gnutls_anon_allocate_server_credentials(&anon_cred
)) < 0) {
1887 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret
));
1891 gnutls_anon_set_server_dh_params(anon_cred
, dh_params
);
1897 static gnutls_certificate_credentials_t
vnc_tls_initialize_x509_cred(VncState
*vs
)
1899 gnutls_certificate_credentials_t x509_cred
;
1902 if (!vs
->vd
->x509cacert
) {
1903 VNC_DEBUG("No CA x509 certificate specified\n");
1906 if (!vs
->vd
->x509cert
) {
1907 VNC_DEBUG("No server x509 certificate specified\n");
1910 if (!vs
->vd
->x509key
) {
1911 VNC_DEBUG("No server private key specified\n");
1915 if ((ret
= gnutls_certificate_allocate_credentials(&x509_cred
)) < 0) {
1916 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret
));
1919 if ((ret
= gnutls_certificate_set_x509_trust_file(x509_cred
,
1921 GNUTLS_X509_FMT_PEM
)) < 0) {
1922 VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret
));
1923 gnutls_certificate_free_credentials(x509_cred
);
1927 if ((ret
= gnutls_certificate_set_x509_key_file (x509_cred
,
1930 GNUTLS_X509_FMT_PEM
)) < 0) {
1931 VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret
));
1932 gnutls_certificate_free_credentials(x509_cred
);
1936 if (vs
->vd
->x509cacrl
) {
1937 if ((ret
= gnutls_certificate_set_x509_crl_file(x509_cred
,
1939 GNUTLS_X509_FMT_PEM
)) < 0) {
1940 VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret
));
1941 gnutls_certificate_free_credentials(x509_cred
);
1946 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
1951 static int vnc_validate_certificate(struct VncState
*vs
)
1954 unsigned int status
;
1955 const gnutls_datum_t
*certs
;
1956 unsigned int nCerts
, i
;
1959 VNC_DEBUG("Validating client certificate\n");
1960 if ((ret
= gnutls_certificate_verify_peers2 (vs
->tls_session
, &status
)) < 0) {
1961 VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret
));
1965 if ((now
= time(NULL
)) == ((time_t)-1)) {
1970 if (status
& GNUTLS_CERT_INVALID
)
1971 VNC_DEBUG("The certificate is not trusted.\n");
1973 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1974 VNC_DEBUG("The certificate hasn't got a known issuer.\n");
1976 if (status
& GNUTLS_CERT_REVOKED
)
1977 VNC_DEBUG("The certificate has been revoked.\n");
1979 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1980 VNC_DEBUG("The certificate uses an insecure algorithm\n");
1984 VNC_DEBUG("Certificate is valid!\n");
1987 /* Only support x509 for now */
1988 if (gnutls_certificate_type_get(vs
->tls_session
) != GNUTLS_CRT_X509
)
1991 if (!(certs
= gnutls_certificate_get_peers(vs
->tls_session
, &nCerts
)))
1994 for (i
= 0 ; i
< nCerts
; i
++) {
1995 gnutls_x509_crt_t cert
;
1996 VNC_DEBUG ("Checking certificate chain %d\n", i
);
1997 if (gnutls_x509_crt_init (&cert
) < 0)
2000 if (gnutls_x509_crt_import(cert
, &certs
[i
], GNUTLS_X509_FMT_DER
) < 0) {
2001 gnutls_x509_crt_deinit (cert
);
2005 if (gnutls_x509_crt_get_expiration_time (cert
) < now
) {
2006 VNC_DEBUG("The certificate has expired\n");
2007 gnutls_x509_crt_deinit (cert
);
2011 if (gnutls_x509_crt_get_activation_time (cert
) > now
) {
2012 VNC_DEBUG("The certificate is not yet activated\n");
2013 gnutls_x509_crt_deinit (cert
);
2017 if (gnutls_x509_crt_get_activation_time (cert
) > now
) {
2018 VNC_DEBUG("The certificate is not yet activated\n");
2019 gnutls_x509_crt_deinit (cert
);
2023 gnutls_x509_crt_deinit (cert
);
2030 static int start_auth_vencrypt_subauth(VncState
*vs
)
2032 switch (vs
->vd
->subauth
) {
2033 case VNC_AUTH_VENCRYPT_TLSNONE
:
2034 case VNC_AUTH_VENCRYPT_X509NONE
:
2035 VNC_DEBUG("Accept TLS auth none\n");
2036 vnc_write_u32(vs
, 0); /* Accept auth completion */
2037 vnc_read_when(vs
, protocol_client_init
, 1);
2040 case VNC_AUTH_VENCRYPT_TLSVNC
:
2041 case VNC_AUTH_VENCRYPT_X509VNC
:
2042 VNC_DEBUG("Start TLS auth VNC\n");
2043 return start_auth_vnc(vs
);
2045 default: /* Should not be possible, but just in case */
2046 VNC_DEBUG("Reject auth %d\n", vs
->vd
->auth
);
2047 vnc_write_u8(vs
, 1);
2048 if (vs
->minor
>= 8) {
2049 static const char err
[] = "Unsupported authentication type";
2050 vnc_write_u32(vs
, sizeof(err
));
2051 vnc_write(vs
, err
, sizeof(err
));
2053 vnc_client_error(vs
);
2059 static void vnc_handshake_io(void *opaque
);
2061 static int vnc_continue_handshake(struct VncState
*vs
) {
2064 if ((ret
= gnutls_handshake(vs
->tls_session
)) < 0) {
2065 if (!gnutls_error_is_fatal(ret
)) {
2066 VNC_DEBUG("Handshake interrupted (blocking)\n");
2067 if (!gnutls_record_get_direction(vs
->tls_session
))
2068 qemu_set_fd_handler(vs
->csock
, vnc_handshake_io
, NULL
, vs
);
2070 qemu_set_fd_handler(vs
->csock
, NULL
, vnc_handshake_io
, vs
);
2073 VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret
));
2074 vnc_client_error(vs
);
2078 if (vs
->vd
->x509verify
) {
2079 if (vnc_validate_certificate(vs
) < 0) {
2080 VNC_DEBUG("Client verification failed\n");
2081 vnc_client_error(vs
);
2084 VNC_DEBUG("Client verification passed\n");
2088 VNC_DEBUG("Handshake done, switching to TLS data mode\n");
2089 vs
->wiremode
= VNC_WIREMODE_TLS
;
2090 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
2092 return start_auth_vencrypt_subauth(vs
);
2095 static void vnc_handshake_io(void *opaque
) {
2096 struct VncState
*vs
= (struct VncState
*)opaque
;
2098 VNC_DEBUG("Handshake IO continue\n");
2099 vnc_continue_handshake(vs
);
2102 #define NEED_X509_AUTH(vs) \
2103 ((vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
2104 (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
2105 (vs)->vd->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
2108 static int vnc_start_tls(struct VncState
*vs
) {
2109 static const int cert_type_priority
[] = { GNUTLS_CRT_X509
, 0 };
2110 static const int protocol_priority
[]= { GNUTLS_TLS1_1
, GNUTLS_TLS1_0
, GNUTLS_SSL3
, 0 };
2111 static const int kx_anon
[] = {GNUTLS_KX_ANON_DH
, 0};
2112 static const int kx_x509
[] = {GNUTLS_KX_DHE_DSS
, GNUTLS_KX_RSA
, GNUTLS_KX_DHE_RSA
, GNUTLS_KX_SRP
, 0};
2114 VNC_DEBUG("Do TLS setup\n");
2115 if (vnc_tls_initialize() < 0) {
2116 VNC_DEBUG("Failed to init TLS\n");
2117 vnc_client_error(vs
);
2120 if (vs
->tls_session
== NULL
) {
2121 if (gnutls_init(&vs
->tls_session
, GNUTLS_SERVER
) < 0) {
2122 vnc_client_error(vs
);
2126 if (gnutls_set_default_priority(vs
->tls_session
) < 0) {
2127 gnutls_deinit(vs
->tls_session
);
2128 vs
->tls_session
= NULL
;
2129 vnc_client_error(vs
);
2133 if (gnutls_kx_set_priority(vs
->tls_session
, NEED_X509_AUTH(vs
) ? kx_x509
: kx_anon
) < 0) {
2134 gnutls_deinit(vs
->tls_session
);
2135 vs
->tls_session
= NULL
;
2136 vnc_client_error(vs
);
2140 if (gnutls_certificate_type_set_priority(vs
->tls_session
, cert_type_priority
) < 0) {
2141 gnutls_deinit(vs
->tls_session
);
2142 vs
->tls_session
= NULL
;
2143 vnc_client_error(vs
);
2147 if (gnutls_protocol_set_priority(vs
->tls_session
, protocol_priority
) < 0) {
2148 gnutls_deinit(vs
->tls_session
);
2149 vs
->tls_session
= NULL
;
2150 vnc_client_error(vs
);
2154 if (NEED_X509_AUTH(vs
)) {
2155 gnutls_certificate_server_credentials x509_cred
= vnc_tls_initialize_x509_cred(vs
);
2157 gnutls_deinit(vs
->tls_session
);
2158 vs
->tls_session
= NULL
;
2159 vnc_client_error(vs
);
2162 if (gnutls_credentials_set(vs
->tls_session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
) < 0) {
2163 gnutls_deinit(vs
->tls_session
);
2164 vs
->tls_session
= NULL
;
2165 gnutls_certificate_free_credentials(x509_cred
);
2166 vnc_client_error(vs
);
2169 if (vs
->vd
->x509verify
) {
2170 VNC_DEBUG("Requesting a client certificate\n");
2171 gnutls_certificate_server_set_request (vs
->tls_session
, GNUTLS_CERT_REQUEST
);
2175 gnutls_anon_server_credentials anon_cred
= vnc_tls_initialize_anon_cred();
2177 gnutls_deinit(vs
->tls_session
);
2178 vs
->tls_session
= NULL
;
2179 vnc_client_error(vs
);
2182 if (gnutls_credentials_set(vs
->tls_session
, GNUTLS_CRD_ANON
, anon_cred
) < 0) {
2183 gnutls_deinit(vs
->tls_session
);
2184 vs
->tls_session
= NULL
;
2185 gnutls_anon_free_server_credentials(anon_cred
);
2186 vnc_client_error(vs
);
2191 gnutls_transport_set_ptr(vs
->tls_session
, (gnutls_transport_ptr_t
)vs
);
2192 gnutls_transport_set_push_function(vs
->tls_session
, vnc_tls_push
);
2193 gnutls_transport_set_pull_function(vs
->tls_session
, vnc_tls_pull
);
2196 VNC_DEBUG("Start TLS handshake process\n");
2197 return vnc_continue_handshake(vs
);
2200 static int protocol_client_vencrypt_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2202 int auth
= read_u32(data
, 0);
2204 if (auth
!= vs
->vd
->subauth
) {
2205 VNC_DEBUG("Rejecting auth %d\n", auth
);
2206 vnc_write_u8(vs
, 0); /* Reject auth */
2208 vnc_client_error(vs
);
2210 VNC_DEBUG("Accepting auth %d, starting handshake\n", auth
);
2211 vnc_write_u8(vs
, 1); /* Accept auth */
2214 if (vnc_start_tls(vs
) < 0) {
2215 VNC_DEBUG("Failed to complete TLS\n");
2222 static int protocol_client_vencrypt_init(VncState
*vs
, uint8_t *data
, size_t len
)
2226 VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data
[0], (int)data
[1]);
2227 vnc_write_u8(vs
, 1); /* Reject version */
2229 vnc_client_error(vs
);
2231 VNC_DEBUG("Sending allowed auth %d\n", vs
->vd
->subauth
);
2232 vnc_write_u8(vs
, 0); /* Accept version */
2233 vnc_write_u8(vs
, 1); /* Number of sub-auths */
2234 vnc_write_u32(vs
, vs
->vd
->subauth
); /* The supported auth */
2236 vnc_read_when(vs
, protocol_client_vencrypt_auth
, 4);
2241 static int start_auth_vencrypt(VncState
*vs
)
2243 /* Send VeNCrypt version 0.2 */
2244 vnc_write_u8(vs
, 0);
2245 vnc_write_u8(vs
, 2);
2247 vnc_read_when(vs
, protocol_client_vencrypt_init
, 2);
2250 #endif /* CONFIG_VNC_TLS */
2252 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2254 /* We only advertise 1 auth scheme at a time, so client
2255 * must pick the one we sent. Verify this */
2256 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
2257 VNC_DEBUG("Reject auth %d\n", (int)data
[0]);
2258 vnc_write_u32(vs
, 1);
2259 if (vs
->minor
>= 8) {
2260 static const char err
[] = "Authentication failed";
2261 vnc_write_u32(vs
, sizeof(err
));
2262 vnc_write(vs
, err
, sizeof(err
));
2264 vnc_client_error(vs
);
2265 } else { /* Accept requested auth */
2266 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2267 switch (vs
->vd
->auth
) {
2269 VNC_DEBUG("Accept auth none\n");
2270 if (vs
->minor
>= 8) {
2271 vnc_write_u32(vs
, 0); /* Accept auth completion */
2274 vnc_read_when(vs
, protocol_client_init
, 1);
2278 VNC_DEBUG("Start VNC auth\n");
2279 return start_auth_vnc(vs
);
2281 #ifdef CONFIG_VNC_TLS
2282 case VNC_AUTH_VENCRYPT
:
2283 VNC_DEBUG("Accept VeNCrypt auth\n");;
2284 return start_auth_vencrypt(vs
);
2285 #endif /* CONFIG_VNC_TLS */
2287 default: /* Should not be possible, but just in case */
2288 VNC_DEBUG("Reject auth %d\n", vs
->vd
->auth
);
2289 vnc_write_u8(vs
, 1);
2290 if (vs
->minor
>= 8) {
2291 static const char err
[] = "Authentication failed";
2292 vnc_write_u32(vs
, sizeof(err
));
2293 vnc_write(vs
, err
, sizeof(err
));
2295 vnc_client_error(vs
);
2301 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2305 memcpy(local
, version
, 12);
2308 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2309 VNC_DEBUG("Malformed protocol version %s\n", local
);
2310 vnc_client_error(vs
);
2313 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2314 if (vs
->major
!= 3 ||
2320 VNC_DEBUG("Unsupported client version\n");
2321 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2323 vnc_client_error(vs
);
2326 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2327 * as equivalent to v3.3 by servers
2329 if (vs
->minor
== 4 || vs
->minor
== 5)
2332 if (vs
->minor
== 3) {
2333 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
2334 VNC_DEBUG("Tell client auth none\n");
2335 vnc_write_u32(vs
, vs
->vd
->auth
);
2337 vnc_read_when(vs
, protocol_client_init
, 1);
2338 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
2339 VNC_DEBUG("Tell client VNC auth\n");
2340 vnc_write_u32(vs
, vs
->vd
->auth
);
2344 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
2345 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2347 vnc_client_error(vs
);
2350 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
2351 vnc_write_u8(vs
, 1); /* num auth */
2352 vnc_write_u8(vs
, vs
->vd
->auth
);
2353 vnc_read_when(vs
, protocol_client_auth
, 1);
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
);
2372 vs
->timer
= qemu_new_timer(rt_clock
, vnc_update_client
, vs
);
2376 vs
->as
.freq
= 44100;
2377 vs
->as
.nchannels
= 2;
2378 vs
->as
.fmt
= AUD_FMT_S16
;
2379 vs
->as
.endianness
= 0;
2382 vnc_write(vs
, "RFB 003.008\n", 12);
2384 vnc_read_when(vs
, protocol_version
, 12);
2385 memset(vs
->old_data
, 0, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
2386 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
2387 vnc_update_client(vs
);
2390 vs
->next
= vd
->clients
;
2394 static void vnc_listen_read(void *opaque
)
2396 VncDisplay
*vs
= opaque
;
2397 struct sockaddr_in addr
;
2398 socklen_t addrlen
= sizeof(addr
);
2403 int csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2405 vnc_connect(vs
, csock
);
2409 void vnc_display_init(DisplayState
*ds
)
2413 vs
= qemu_mallocz(sizeof(VncState
));
2414 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2424 if (keyboard_layout
)
2425 vs
->kbd_layout
= init_keyboard_layout(keyboard_layout
);
2427 vs
->kbd_layout
= init_keyboard_layout("en-us");
2429 if (!vs
->kbd_layout
)
2432 dcl
->dpy_copy
= vnc_dpy_copy
;
2433 dcl
->dpy_update
= vnc_dpy_update
;
2434 dcl
->dpy_resize
= vnc_dpy_resize
;
2435 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2436 register_displaychangelistener(ds
, dcl
);
2439 #ifdef CONFIG_VNC_TLS
2440 static int vnc_set_x509_credential(VncDisplay
*vs
,
2441 const char *certdir
,
2442 const char *filename
,
2453 *cred
= qemu_malloc(strlen(certdir
) + strlen(filename
) + 2);
2455 strcpy(*cred
, certdir
);
2457 strcat(*cred
, filename
);
2459 VNC_DEBUG("Check %s\n", *cred
);
2460 if (stat(*cred
, &sb
) < 0) {
2463 if (ignoreMissing
&& errno
== ENOENT
)
2471 static int vnc_set_x509_credential_dir(VncDisplay
*vs
,
2472 const char *certdir
)
2474 if (vnc_set_x509_credential(vs
, certdir
, X509_CA_CERT_FILE
, &vs
->x509cacert
, 0) < 0)
2476 if (vnc_set_x509_credential(vs
, certdir
, X509_CA_CRL_FILE
, &vs
->x509cacrl
, 1) < 0)
2478 if (vnc_set_x509_credential(vs
, certdir
, X509_SERVER_CERT_FILE
, &vs
->x509cert
, 0) < 0)
2480 if (vnc_set_x509_credential(vs
, certdir
, X509_SERVER_KEY_FILE
, &vs
->x509key
, 0) < 0)
2486 qemu_free(vs
->x509cacert
);
2487 qemu_free(vs
->x509cacrl
);
2488 qemu_free(vs
->x509cert
);
2489 qemu_free(vs
->x509key
);
2490 vs
->x509cacert
= vs
->x509cacrl
= vs
->x509cert
= vs
->x509key
= NULL
;
2493 #endif /* CONFIG_VNC_TLS */
2495 void vnc_display_close(DisplayState
*ds
)
2497 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2502 qemu_free(vs
->display
);
2505 if (vs
->lsock
!= -1) {
2506 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2510 vs
->auth
= VNC_AUTH_INVALID
;
2511 #ifdef CONFIG_VNC_TLS
2512 vs
->subauth
= VNC_AUTH_INVALID
;
2517 int vnc_display_password(DisplayState
*ds
, const char *password
)
2519 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2522 qemu_free(vs
->password
);
2523 vs
->password
= NULL
;
2525 if (password
&& password
[0]) {
2526 if (!(vs
->password
= qemu_strdup(password
)))
2533 int vnc_display_open(DisplayState
*ds
, const char *display
)
2535 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2536 const char *options
;
2540 #ifdef CONFIG_VNC_TLS
2541 int tls
= 0, x509
= 0;
2546 vnc_display_close(ds
);
2547 if (strcmp(display
, "none") == 0)
2550 if (!(vs
->display
= strdup(display
)))
2554 while ((options
= strchr(options
, ','))) {
2556 if (strncmp(options
, "password", 8) == 0) {
2557 password
= 1; /* Require password auth */
2558 } else if (strncmp(options
, "reverse", 7) == 0) {
2560 } else if (strncmp(options
, "to=", 3) == 0) {
2561 to_port
= atoi(options
+3) + 5900;
2562 #ifdef CONFIG_VNC_TLS
2563 } else if (strncmp(options
, "tls", 3) == 0) {
2564 tls
= 1; /* Require TLS */
2565 } else if (strncmp(options
, "x509", 4) == 0) {
2567 x509
= 1; /* Require x509 certificates */
2568 if (strncmp(options
, "x509verify", 10) == 0)
2569 vs
->x509verify
= 1; /* ...and verify client certs */
2571 /* Now check for 'x509=/some/path' postfix
2572 * and use that to setup x509 certificate/key paths */
2573 start
= strchr(options
, '=');
2574 end
= strchr(options
, ',');
2575 if (start
&& (!end
|| (start
< end
))) {
2576 int len
= end
? end
-(start
+1) : strlen(start
+1);
2577 char *path
= qemu_strndup(start
+ 1, len
);
2579 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2580 if (vnc_set_x509_credential_dir(vs
, path
) < 0) {
2581 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2583 qemu_free(vs
->display
);
2589 fprintf(stderr
, "No certificate path provided\n");
2590 qemu_free(vs
->display
);
2599 #ifdef CONFIG_VNC_TLS
2601 vs
->auth
= VNC_AUTH_VENCRYPT
;
2603 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2604 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2606 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2607 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2611 VNC_DEBUG("Initializing VNC server with password auth\n");
2612 vs
->auth
= VNC_AUTH_VNC
;
2613 #ifdef CONFIG_VNC_TLS
2614 vs
->subauth
= VNC_AUTH_INVALID
;
2618 #ifdef CONFIG_VNC_TLS
2620 vs
->auth
= VNC_AUTH_VENCRYPT
;
2622 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2623 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2625 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2626 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2630 VNC_DEBUG("Initializing VNC server with no auth\n");
2631 vs
->auth
= VNC_AUTH_NONE
;
2632 #ifdef CONFIG_VNC_TLS
2633 vs
->subauth
= VNC_AUTH_INVALID
;
2639 /* connect to viewer */
2640 if (strncmp(display
, "unix:", 5) == 0)
2641 vs
->lsock
= unix_connect(display
+5);
2643 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2644 if (-1 == vs
->lsock
) {
2649 int csock
= vs
->lsock
;
2651 vnc_connect(vs
, csock
);
2656 /* listen for connects */
2658 dpy
= qemu_malloc(256);
2659 if (strncmp(display
, "unix:", 5) == 0) {
2660 pstrcpy(dpy
, 256, "unix:");
2661 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2663 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2665 if (-1 == vs
->lsock
) {
2673 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);