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 #define VNC_REFRESH_INTERVAL (1000 / 30)
34 #include "vnc_keysym.h"
37 #define count_bits(c, v) { \
38 for (c = 0; v; v >>= 1) \
45 static VncDisplay
*vnc_display
; /* needed for info vnc */
46 static DisplayChangeListener
*dcl
;
48 static char *addr_to_string(const char *format
,
49 struct sockaddr_storage
*sa
,
52 char host
[NI_MAXHOST
];
53 char serv
[NI_MAXSERV
];
56 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
59 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
60 VNC_DEBUG("Cannot resolve address %d: %s\n",
61 err
, gai_strerror(err
));
65 if (asprintf(&addr
, format
, host
, serv
) < 0)
71 static char *vnc_socket_local_addr(const char *format
, int fd
) {
72 struct sockaddr_storage sa
;
76 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
79 return addr_to_string(format
, &sa
, salen
);
82 static char *vnc_socket_remote_addr(const char *format
, int fd
) {
83 struct sockaddr_storage sa
;
87 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
90 return addr_to_string(format
, &sa
, salen
);
93 static const char *vnc_auth_name(VncDisplay
*vd
) {
95 case VNC_AUTH_INVALID
:
111 case VNC_AUTH_VENCRYPT
:
112 #ifdef CONFIG_VNC_TLS
113 switch (vd
->subauth
) {
114 case VNC_AUTH_VENCRYPT_PLAIN
:
115 return "vencrypt+plain";
116 case VNC_AUTH_VENCRYPT_TLSNONE
:
117 return "vencrypt+tls+none";
118 case VNC_AUTH_VENCRYPT_TLSVNC
:
119 return "vencrypt+tls+vnc";
120 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
121 return "vencrypt+tls+plain";
122 case VNC_AUTH_VENCRYPT_X509NONE
:
123 return "vencrypt+x509+none";
124 case VNC_AUTH_VENCRYPT_X509VNC
:
125 return "vencrypt+x509+vnc";
126 case VNC_AUTH_VENCRYPT_X509PLAIN
:
127 return "vencrypt+x509+plain";
138 #define VNC_SOCKET_FORMAT_PRETTY "local %s:%s"
140 static void do_info_vnc_client(Monitor
*mon
, VncState
*client
)
143 vnc_socket_remote_addr(" address: %s:%s\n",
148 monitor_printf(mon
, "Client:\n");
149 monitor_printf(mon
, "%s", clientAddr
);
153 void do_info_vnc(Monitor
*mon
)
155 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
156 monitor_printf(mon
, "Server: disabled\n");
158 char *serverAddr
= vnc_socket_local_addr(" address: %s:%s\n",
164 monitor_printf(mon
, "Server:\n");
165 monitor_printf(mon
, "%s", serverAddr
);
167 monitor_printf(mon
, " auth: %s\n", vnc_auth_name(vnc_display
));
169 if (vnc_display
->clients
) {
170 VncState
*client
= vnc_display
->clients
;
172 do_info_vnc_client(mon
, client
);
173 client
= client
->next
;
176 monitor_printf(mon
, "Client: none\n");
181 static inline uint32_t vnc_has_feature(VncState
*vs
, int feature
) {
182 return (vs
->features
& (1 << feature
));
186 1) Get the queue working for IO.
187 2) there is some weirdness when using the -S option (the screen is grey
188 and not totally invalidated
189 3) resolutions > 1024
192 static void vnc_update_client(void *opaque
);
194 static void vnc_colordepth(VncState
*vs
);
196 static inline void vnc_set_bit(uint32_t *d
, int k
)
198 d
[k
>> 5] |= 1 << (k
& 0x1f);
201 static inline void vnc_clear_bit(uint32_t *d
, int k
)
203 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
206 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
216 d
[j
++] = (1 << n
) - 1;
221 static inline int vnc_get_bit(const uint32_t *d
, int k
)
223 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
226 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
230 for(i
= 0; i
< nb_words
; i
++) {
231 if ((d1
[i
] & d2
[i
]) != 0)
237 static void vnc_update(VncState
*vs
, int x
, int y
, int w
, int h
)
243 /* round x down to ensure the loop only spans one 16-pixel block per,
244 iteration. otherwise, if (x % 16) != 0, the last iteration may span
245 two 16-pixel blocks but we only mark the first as dirty
250 x
= MIN(x
, vs
->serverds
.width
);
251 y
= MIN(y
, vs
->serverds
.height
);
252 w
= MIN(x
+ w
, vs
->serverds
.width
) - x
;
253 h
= MIN(h
, vs
->serverds
.height
);
256 for (i
= 0; i
< w
; i
+= 16)
257 vnc_set_bit(vs
->dirty_row
[y
], (x
+ i
) / 16);
260 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
262 VncDisplay
*vd
= ds
->opaque
;
263 VncState
*vs
= vd
->clients
;
265 vnc_update(vs
, x
, y
, w
, h
);
270 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
273 vnc_write_u16(vs
, x
);
274 vnc_write_u16(vs
, y
);
275 vnc_write_u16(vs
, w
);
276 vnc_write_u16(vs
, h
);
278 vnc_write_s32(vs
, encoding
);
281 static void buffer_reserve(Buffer
*buffer
, size_t len
)
283 if ((buffer
->capacity
- buffer
->offset
) < len
) {
284 buffer
->capacity
+= (len
+ 1024);
285 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
286 if (buffer
->buffer
== NULL
) {
287 fprintf(stderr
, "vnc: out of memory\n");
293 static int buffer_empty(Buffer
*buffer
)
295 return buffer
->offset
== 0;
298 static uint8_t *buffer_end(Buffer
*buffer
)
300 return buffer
->buffer
+ buffer
->offset
;
303 static void buffer_reset(Buffer
*buffer
)
308 static void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
310 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
311 buffer
->offset
+= len
;
314 static void vnc_resize(VncState
*vs
)
316 DisplayState
*ds
= vs
->ds
;
320 vs
->old_data
= qemu_realloc(vs
->old_data
, ds_get_linesize(ds
) * ds_get_height(ds
));
322 if (vs
->old_data
== NULL
) {
323 fprintf(stderr
, "vnc: memory allocation failed\n");
327 if (ds_get_bytes_per_pixel(ds
) != vs
->serverds
.pf
.bytes_per_pixel
)
328 console_color_init(ds
);
330 size_changed
= ds_get_width(ds
) != vs
->serverds
.width
||
331 ds_get_height(ds
) != vs
->serverds
.height
;
332 vs
->serverds
= *(ds
->surface
);
334 if (vs
->csock
!= -1 && vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
335 vnc_write_u8(vs
, 0); /* msg id */
337 vnc_write_u16(vs
, 1); /* number of rects */
338 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(ds
), ds_get_height(ds
),
339 VNC_ENCODING_DESKTOPRESIZE
);
344 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
345 memset(vs
->old_data
, 42, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
348 static void vnc_dpy_resize(DisplayState
*ds
)
350 VncDisplay
*vd
= ds
->opaque
;
351 VncState
*vs
= vd
->clients
;
359 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
361 vnc_write(vs
, pixels
, size
);
364 /* slowest but generic code. */
365 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
369 r
= ((((v
& vs
->serverds
.pf
.rmask
) >> vs
->serverds
.pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
370 vs
->serverds
.pf
.rbits
);
371 g
= ((((v
& vs
->serverds
.pf
.gmask
) >> vs
->serverds
.pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
372 vs
->serverds
.pf
.gbits
);
373 b
= ((((v
& vs
->serverds
.pf
.bmask
) >> vs
->serverds
.pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
374 vs
->serverds
.pf
.bbits
);
375 v
= (r
<< vs
->clientds
.pf
.rshift
) |
376 (g
<< vs
->clientds
.pf
.gshift
) |
377 (b
<< vs
->clientds
.pf
.bshift
);
378 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
383 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
393 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
408 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
412 if (vs
->serverds
.pf
.bytes_per_pixel
== 4) {
413 uint32_t *pixels
= pixels1
;
416 for(i
= 0; i
< n
; i
++) {
417 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
418 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
420 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 2) {
421 uint16_t *pixels
= pixels1
;
424 for(i
= 0; i
< n
; i
++) {
425 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
426 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
428 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 1) {
429 uint8_t *pixels
= pixels1
;
432 for(i
= 0; i
< n
; i
++) {
433 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
434 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
437 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
441 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
446 row
= ds_get_data(vs
->ds
) + y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
447 for (i
= 0; i
< h
; i
++) {
448 vs
->write_pixels(vs
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
449 row
+= ds_get_linesize(vs
->ds
);
453 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
455 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
456 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
460 #include "vnchextile.h"
464 #include "vnchextile.h"
468 #include "vnchextile.h"
473 #include "vnchextile.h"
479 #include "vnchextile.h"
485 #include "vnchextile.h"
489 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
493 uint8_t *last_fg
, *last_bg
;
495 last_fg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
496 last_bg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
498 for (j
= y
; j
< (y
+ h
); j
+= 16) {
499 for (i
= x
; i
< (x
+ w
); i
+= 16) {
500 vs
->send_hextile_tile(vs
, i
, j
,
501 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
502 last_bg
, last_fg
, &has_bg
, &has_fg
);
510 static void vnc_zlib_init(VncState
*vs
)
513 for (i
=0; i
<(sizeof(vs
->zlib_stream
) / sizeof(z_stream
)); i
++)
514 vs
->zlib_stream
[i
].opaque
= NULL
;
517 static void vnc_zlib_start(VncState
*vs
)
519 buffer_reset(&vs
->zlib
);
521 // make the output buffer be the zlib buffer, so we can compress it later
522 vs
->zlib_tmp
= vs
->output
;
523 vs
->output
= vs
->zlib
;
526 static int vnc_zlib_stop(VncState
*vs
, int stream_id
)
528 z_streamp zstream
= &vs
->zlib_stream
[stream_id
];
531 // switch back to normal output/zlib buffers
532 vs
->zlib
= vs
->output
;
533 vs
->output
= vs
->zlib_tmp
;
535 // compress the zlib buffer
537 // initialize the stream
538 // XXX need one stream per session
539 if (zstream
->opaque
!= vs
) {
542 VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id
);
543 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
544 zstream
->zalloc
= Z_NULL
;
545 zstream
->zfree
= Z_NULL
;
547 err
= deflateInit2(zstream
, vs
->tight_compression
, Z_DEFLATED
, MAX_WBITS
,
548 MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
551 fprintf(stderr
, "VNC: error initializing zlib\n");
555 zstream
->opaque
= vs
;
558 // XXX what to do if tight_compression changed in between?
560 // reserve memory in output buffer
561 buffer_reserve(&vs
->output
, vs
->zlib
.offset
+ 64);
564 zstream
->next_in
= vs
->zlib
.buffer
;
565 zstream
->avail_in
= vs
->zlib
.offset
;
566 zstream
->next_out
= vs
->output
.buffer
+ vs
->output
.offset
;
567 zstream
->avail_out
= vs
->output
.capacity
- vs
->output
.offset
;
568 zstream
->data_type
= Z_BINARY
;
569 previous_out
= zstream
->total_out
;
572 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
573 fprintf(stderr
, "VNC: error during zlib compression\n");
577 vs
->output
.offset
= vs
->output
.capacity
- zstream
->avail_out
;
578 return zstream
->total_out
- previous_out
;
581 static void send_framebuffer_update_zlib(VncState
*vs
, int x
, int y
, int w
, int h
)
583 int old_offset
, new_offset
, bytes_written
;
585 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_ZLIB
);
587 // remember where we put in the follow-up size
588 old_offset
= vs
->output
.offset
;
589 vnc_write_s32(vs
, 0);
591 // compress the stream
593 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
594 bytes_written
= vnc_zlib_stop(vs
, 0);
596 if (bytes_written
== -1)
600 new_offset
= vs
->output
.offset
;
601 vs
->output
.offset
= old_offset
;
602 vnc_write_u32(vs
, bytes_written
);
603 vs
->output
.offset
= new_offset
;
606 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
608 switch(vs
->vnc_encoding
) {
609 case VNC_ENCODING_ZLIB
:
610 send_framebuffer_update_zlib(vs
, x
, y
, w
, h
);
612 case VNC_ENCODING_HEXTILE
:
613 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
614 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
617 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
618 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
623 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
625 vnc_update_client(vs
);
627 vnc_write_u8(vs
, 0); /* msg id */
629 vnc_write_u16(vs
, 1); /* number of rects */
630 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
631 vnc_write_u16(vs
, src_x
);
632 vnc_write_u16(vs
, src_y
);
636 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
638 VncDisplay
*vd
= ds
->opaque
;
639 VncState
*vs
= vd
->clients
;
641 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
))
642 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
644 vnc_update(vs
, dst_x
, dst_y
, w
, h
);
649 static int find_dirty_height(VncState
*vs
, int y
, int last_x
, int x
)
653 for (h
= 1; h
< (vs
->serverds
.height
- y
); h
++) {
655 if (!vnc_get_bit(vs
->dirty_row
[y
+ h
], last_x
))
657 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
658 vnc_clear_bit(vs
->dirty_row
[y
+ h
], tmp_x
);
664 static void vnc_update_client(void *opaque
)
666 VncState
*vs
= opaque
;
667 if (vs
->need_update
&& vs
->csock
!= -1) {
671 uint32_t width_mask
[VNC_DIRTY_WORDS
];
678 vnc_set_bits(width_mask
, (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
680 /* Walk through the dirty map and eliminate tiles that
681 really aren't dirty */
682 row
= ds_get_data(vs
->ds
);
683 old_row
= vs
->old_data
;
685 for (y
= 0; y
< ds_get_height(vs
->ds
); y
++) {
686 if (vnc_and_bits(vs
->dirty_row
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
692 old_ptr
= (char*)old_row
;
694 for (x
= 0; x
< ds_get_width(vs
->ds
); x
+= 16) {
695 if (memcmp(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
)) == 0) {
696 vnc_clear_bit(vs
->dirty_row
[y
], (x
/ 16));
699 memcpy(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
));
702 ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
703 old_ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
707 row
+= ds_get_linesize(vs
->ds
);
708 old_row
+= ds_get_linesize(vs
->ds
);
711 if (!has_dirty
&& !vs
->audio_cap
) {
712 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
716 /* Count rectangles */
718 vnc_write_u8(vs
, 0); /* msg id */
720 saved_offset
= vs
->output
.offset
;
721 vnc_write_u16(vs
, 0);
723 for (y
= 0; y
< vs
->serverds
.height
; y
++) {
726 for (x
= 0; x
< vs
->serverds
.width
/ 16; x
++) {
727 if (vnc_get_bit(vs
->dirty_row
[y
], x
)) {
731 vnc_clear_bit(vs
->dirty_row
[y
], x
);
734 int h
= find_dirty_height(vs
, y
, last_x
, x
);
735 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
742 int h
= find_dirty_height(vs
, y
, last_x
, x
);
743 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
747 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
748 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
753 if (vs
->csock
!= -1) {
754 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
760 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
762 VncState
*vs
= opaque
;
765 case AUD_CNOTIFY_DISABLE
:
766 vnc_write_u8(vs
, 255);
768 vnc_write_u16(vs
, 0);
772 case AUD_CNOTIFY_ENABLE
:
773 vnc_write_u8(vs
, 255);
775 vnc_write_u16(vs
, 1);
781 static void audio_capture_destroy(void *opaque
)
785 static void audio_capture(void *opaque
, void *buf
, int size
)
787 VncState
*vs
= opaque
;
789 vnc_write_u8(vs
, 255);
791 vnc_write_u16(vs
, 2);
792 vnc_write_u32(vs
, size
);
793 vnc_write(vs
, buf
, size
);
797 static void audio_add(VncState
*vs
)
799 Monitor
*mon
= cur_mon
;
800 struct audio_capture_ops ops
;
803 monitor_printf(mon
, "audio already running\n");
807 ops
.notify
= audio_capture_notify
;
808 ops
.destroy
= audio_capture_destroy
;
809 ops
.capture
= audio_capture
;
811 vs
->audio_cap
= AUD_add_capture(NULL
, &vs
->as
, &ops
, vs
);
812 if (!vs
->audio_cap
) {
813 monitor_printf(mon
, "Failed to add audio capture\n");
817 static void audio_del(VncState
*vs
)
820 AUD_del_capture(vs
->audio_cap
, vs
);
821 vs
->audio_cap
= NULL
;
825 static int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
827 if (ret
== 0 || ret
== -1) {
829 switch (last_errno
) {
841 VNC_DEBUG("Closing down client sock %d %d\n", ret
, ret
< 0 ? last_errno
: 0);
842 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
843 closesocket(vs
->csock
);
844 qemu_del_timer(vs
->timer
);
845 qemu_free_timer(vs
->timer
);
846 if (vs
->input
.buffer
) qemu_free(vs
->input
.buffer
);
847 if (vs
->output
.buffer
) qemu_free(vs
->output
.buffer
);
848 #ifdef CONFIG_VNC_TLS
849 vnc_tls_client_cleanup(vs
);
850 #endif /* CONFIG_VNC_TLS */
853 VncState
*p
, *parent
= NULL
;
854 for (p
= vs
->vd
->clients
; p
!= NULL
; p
= p
->next
) {
857 parent
->next
= p
->next
;
859 vs
->vd
->clients
= p
->next
;
864 if (!vs
->vd
->clients
)
867 qemu_free(vs
->old_data
);
876 void vnc_client_error(VncState
*vs
)
878 vnc_client_io_error(vs
, -1, EINVAL
);
881 void vnc_client_write(void *opaque
)
884 VncState
*vs
= opaque
;
886 #ifdef CONFIG_VNC_TLS
887 if (vs
->tls
.session
) {
888 ret
= gnutls_write(vs
->tls
.session
, vs
->output
.buffer
, vs
->output
.offset
);
890 if (ret
== GNUTLS_E_AGAIN
)
897 #endif /* CONFIG_VNC_TLS */
898 ret
= send(vs
->csock
, vs
->output
.buffer
, vs
->output
.offset
, 0);
899 ret
= vnc_client_io_error(vs
, ret
, socket_error());
903 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
904 vs
->output
.offset
-= ret
;
906 if (vs
->output
.offset
== 0) {
907 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
911 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
913 vs
->read_handler
= func
;
914 vs
->read_handler_expect
= expecting
;
917 void vnc_client_read(void *opaque
)
919 VncState
*vs
= opaque
;
922 buffer_reserve(&vs
->input
, 4096);
924 #ifdef CONFIG_VNC_TLS
925 if (vs
->tls
.session
) {
926 ret
= gnutls_read(vs
->tls
.session
, buffer_end(&vs
->input
), 4096);
928 if (ret
== GNUTLS_E_AGAIN
)
935 #endif /* CONFIG_VNC_TLS */
936 ret
= recv(vs
->csock
, buffer_end(&vs
->input
), 4096, 0);
937 ret
= vnc_client_io_error(vs
, ret
, socket_error());
941 vs
->input
.offset
+= ret
;
943 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
944 size_t len
= vs
->read_handler_expect
;
947 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
952 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
953 vs
->input
.offset
-= len
;
955 vs
->read_handler_expect
= ret
;
960 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
962 buffer_reserve(&vs
->output
, len
);
964 if (buffer_empty(&vs
->output
)) {
965 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
968 buffer_append(&vs
->output
, data
, len
);
971 void vnc_write_s32(VncState
*vs
, int32_t value
)
973 vnc_write_u32(vs
, *(uint32_t *)&value
);
976 void vnc_write_u32(VncState
*vs
, uint32_t value
)
980 buf
[0] = (value
>> 24) & 0xFF;
981 buf
[1] = (value
>> 16) & 0xFF;
982 buf
[2] = (value
>> 8) & 0xFF;
983 buf
[3] = value
& 0xFF;
985 vnc_write(vs
, buf
, 4);
988 void vnc_write_u16(VncState
*vs
, uint16_t value
)
992 buf
[0] = (value
>> 8) & 0xFF;
993 buf
[1] = value
& 0xFF;
995 vnc_write(vs
, buf
, 2);
998 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1000 vnc_write(vs
, (char *)&value
, 1);
1003 void vnc_flush(VncState
*vs
)
1005 if (vs
->output
.offset
)
1006 vnc_client_write(vs
);
1009 uint8_t read_u8(uint8_t *data
, size_t offset
)
1011 return data
[offset
];
1014 uint16_t read_u16(uint8_t *data
, size_t offset
)
1016 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1019 int32_t read_s32(uint8_t *data
, size_t offset
)
1021 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1022 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1025 uint32_t read_u32(uint8_t *data
, size_t offset
)
1027 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1028 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1031 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1035 static void check_pointer_type_change(VncState
*vs
, int absolute
)
1037 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1038 vnc_write_u8(vs
, 0);
1039 vnc_write_u8(vs
, 0);
1040 vnc_write_u16(vs
, 1);
1041 vnc_framebuffer_update(vs
, absolute
, 0,
1042 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1043 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1046 vs
->absolute
= absolute
;
1049 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1054 if (button_mask
& 0x01)
1055 buttons
|= MOUSE_EVENT_LBUTTON
;
1056 if (button_mask
& 0x02)
1057 buttons
|= MOUSE_EVENT_MBUTTON
;
1058 if (button_mask
& 0x04)
1059 buttons
|= MOUSE_EVENT_RBUTTON
;
1060 if (button_mask
& 0x08)
1062 if (button_mask
& 0x10)
1066 kbd_mouse_event(x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1),
1067 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1),
1069 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1073 kbd_mouse_event(x
, y
, dz
, buttons
);
1075 if (vs
->last_x
!= -1)
1076 kbd_mouse_event(x
- vs
->last_x
,
1083 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1086 static void reset_keys(VncState
*vs
)
1089 for(i
= 0; i
< 256; i
++) {
1090 if (vs
->modifiers_state
[i
]) {
1092 kbd_put_keycode(0xe0);
1093 kbd_put_keycode(i
| 0x80);
1094 vs
->modifiers_state
[i
] = 0;
1099 static void press_key(VncState
*vs
, int keysym
)
1101 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & 0x7f);
1102 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) | 0x80);
1105 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1107 /* QEMU console switch */
1109 case 0x2a: /* Left Shift */
1110 case 0x36: /* Right Shift */
1111 case 0x1d: /* Left CTRL */
1112 case 0x9d: /* Right CTRL */
1113 case 0x38: /* Left ALT */
1114 case 0xb8: /* Right ALT */
1116 vs
->modifiers_state
[keycode
] = 1;
1118 vs
->modifiers_state
[keycode
] = 0;
1120 case 0x02 ... 0x0a: /* '1' to '9' keys */
1121 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1122 /* Reset the modifiers sent to the current console */
1124 console_select(keycode
- 0x02);
1128 case 0x3a: /* CapsLock */
1129 case 0x45: /* NumLock */
1131 vs
->modifiers_state
[keycode
] ^= 1;
1135 if (keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1136 /* If the numlock state needs to change then simulate an additional
1137 keypress before sending this one. This will happen if the user
1138 toggles numlock away from the VNC window.
1140 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1141 if (!vs
->modifiers_state
[0x45]) {
1142 vs
->modifiers_state
[0x45] = 1;
1143 press_key(vs
, 0xff7f);
1146 if (vs
->modifiers_state
[0x45]) {
1147 vs
->modifiers_state
[0x45] = 0;
1148 press_key(vs
, 0xff7f);
1153 if (is_graphic_console()) {
1155 kbd_put_keycode(0xe0);
1157 kbd_put_keycode(keycode
& 0x7f);
1159 kbd_put_keycode(keycode
| 0x80);
1161 /* QEMU console emulation */
1164 case 0x2a: /* Left Shift */
1165 case 0x36: /* Right Shift */
1166 case 0x1d: /* Left CTRL */
1167 case 0x9d: /* Right CTRL */
1168 case 0x38: /* Left ALT */
1169 case 0xb8: /* Right ALT */
1172 kbd_put_keysym(QEMU_KEY_UP
);
1175 kbd_put_keysym(QEMU_KEY_DOWN
);
1178 kbd_put_keysym(QEMU_KEY_LEFT
);
1181 kbd_put_keysym(QEMU_KEY_RIGHT
);
1184 kbd_put_keysym(QEMU_KEY_DELETE
);
1187 kbd_put_keysym(QEMU_KEY_HOME
);
1190 kbd_put_keysym(QEMU_KEY_END
);
1193 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1196 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1199 kbd_put_keysym(sym
);
1206 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1210 if (sym
>= 'A' && sym
<= 'Z' && is_graphic_console())
1211 sym
= sym
- 'A' + 'a';
1213 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, sym
& 0xFFFF);
1214 do_key_event(vs
, down
, keycode
, sym
);
1217 static void ext_key_event(VncState
*vs
, int down
,
1218 uint32_t sym
, uint16_t keycode
)
1220 /* if the user specifies a keyboard layout, always use it */
1221 if (keyboard_layout
)
1222 key_event(vs
, down
, sym
);
1224 do_key_event(vs
, down
, keycode
, sym
);
1227 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1228 int x_position
, int y_position
,
1231 if (x_position
> ds_get_width(vs
->ds
))
1232 x_position
= ds_get_width(vs
->ds
);
1233 if (y_position
> ds_get_height(vs
->ds
))
1234 y_position
= ds_get_height(vs
->ds
);
1235 if (x_position
+ w
>= ds_get_width(vs
->ds
))
1236 w
= ds_get_width(vs
->ds
) - x_position
;
1237 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1238 h
= ds_get_height(vs
->ds
) - y_position
;
1241 vs
->need_update
= 1;
1243 char *old_row
= vs
->old_data
+ y_position
* ds_get_linesize(vs
->ds
);
1245 for (i
= 0; i
< h
; i
++) {
1246 vnc_set_bits(vs
->dirty_row
[y_position
+ i
],
1247 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1248 memset(old_row
, 42, ds_get_width(vs
->ds
) * ds_get_bytes_per_pixel(vs
->ds
));
1249 old_row
+= ds_get_linesize(vs
->ds
);
1254 static void send_ext_key_event_ack(VncState
*vs
)
1256 vnc_write_u8(vs
, 0);
1257 vnc_write_u8(vs
, 0);
1258 vnc_write_u16(vs
, 1);
1259 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1260 VNC_ENCODING_EXT_KEY_EVENT
);
1264 static void send_ext_audio_ack(VncState
*vs
)
1266 vnc_write_u8(vs
, 0);
1267 vnc_write_u8(vs
, 0);
1268 vnc_write_u16(vs
, 1);
1269 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1270 VNC_ENCODING_AUDIO
);
1274 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1277 unsigned int enc
= 0;
1281 vs
->vnc_encoding
= 0;
1282 vs
->tight_compression
= 9;
1283 vs
->tight_quality
= 9;
1286 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1289 case VNC_ENCODING_RAW
:
1290 vs
->vnc_encoding
= enc
;
1292 case VNC_ENCODING_COPYRECT
:
1293 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1295 case VNC_ENCODING_HEXTILE
:
1296 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1297 vs
->vnc_encoding
= enc
;
1299 case VNC_ENCODING_ZLIB
:
1300 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1301 vs
->vnc_encoding
= enc
;
1303 case VNC_ENCODING_DESKTOPRESIZE
:
1304 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1306 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1307 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1309 case VNC_ENCODING_EXT_KEY_EVENT
:
1310 send_ext_key_event_ack(vs
);
1312 case VNC_ENCODING_AUDIO
:
1313 send_ext_audio_ack(vs
);
1315 case VNC_ENCODING_WMVi
:
1316 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1318 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1319 vs
->tight_compression
= (enc
& 0x0F);
1321 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1322 vs
->tight_quality
= (enc
& 0x0F);
1325 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1330 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1333 static void set_pixel_conversion(VncState
*vs
)
1335 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1336 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1337 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1338 vs
->write_pixels
= vnc_write_pixels_copy
;
1339 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1341 vs
->send_hextile_tile
= send_hextile_tile_8
;
1344 vs
->send_hextile_tile
= send_hextile_tile_16
;
1347 vs
->send_hextile_tile
= send_hextile_tile_32
;
1351 vs
->write_pixels
= vnc_write_pixels_generic
;
1352 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1354 vs
->send_hextile_tile
= send_hextile_tile_generic_8
;
1357 vs
->send_hextile_tile
= send_hextile_tile_generic_16
;
1360 vs
->send_hextile_tile
= send_hextile_tile_generic_32
;
1366 static void set_pixel_format(VncState
*vs
,
1367 int bits_per_pixel
, int depth
,
1368 int big_endian_flag
, int true_color_flag
,
1369 int red_max
, int green_max
, int blue_max
,
1370 int red_shift
, int green_shift
, int blue_shift
)
1372 if (!true_color_flag
) {
1373 vnc_client_error(vs
);
1377 vs
->clientds
= vs
->serverds
;
1378 vs
->clientds
.pf
.rmax
= red_max
;
1379 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1380 vs
->clientds
.pf
.rshift
= red_shift
;
1381 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1382 vs
->clientds
.pf
.gmax
= green_max
;
1383 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1384 vs
->clientds
.pf
.gshift
= green_shift
;
1385 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1386 vs
->clientds
.pf
.bmax
= blue_max
;
1387 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1388 vs
->clientds
.pf
.bshift
= blue_shift
;
1389 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1390 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1391 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1392 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1393 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1395 set_pixel_conversion(vs
);
1397 vga_hw_invalidate();
1401 static void pixel_format_message (VncState
*vs
) {
1402 char pad
[3] = { 0, 0, 0 };
1404 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1405 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1407 #ifdef WORDS_BIGENDIAN
1408 vnc_write_u8(vs
, 1); /* big-endian-flag */
1410 vnc_write_u8(vs
, 0); /* big-endian-flag */
1412 vnc_write_u8(vs
, 1); /* true-color-flag */
1413 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1414 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1415 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1416 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1417 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1418 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1419 if (vs
->ds
->surface
->pf
.bits_per_pixel
== 32)
1420 vs
->send_hextile_tile
= send_hextile_tile_32
;
1421 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 16)
1422 vs
->send_hextile_tile
= send_hextile_tile_16
;
1423 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 8)
1424 vs
->send_hextile_tile
= send_hextile_tile_8
;
1425 vs
->clientds
= *(vs
->ds
->surface
);
1426 vs
->clientds
.flags
|= ~QEMU_ALLOCATED_FLAG
;
1427 vs
->write_pixels
= vnc_write_pixels_copy
;
1429 vnc_write(vs
, pad
, 3); /* padding */
1432 static void vnc_dpy_setdata(DisplayState
*ds
)
1434 /* We don't have to do anything */
1437 static void vnc_colordepth(VncState
*vs
)
1439 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1440 /* Sending a WMVi message to notify the client*/
1441 vnc_write_u8(vs
, 0); /* msg id */
1442 vnc_write_u8(vs
, 0);
1443 vnc_write_u16(vs
, 1); /* number of rects */
1444 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1445 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1446 pixel_format_message(vs
);
1449 set_pixel_conversion(vs
);
1453 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1463 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1464 read_u8(data
, 6), read_u8(data
, 7),
1465 read_u16(data
, 8), read_u16(data
, 10),
1466 read_u16(data
, 12), read_u8(data
, 14),
1467 read_u8(data
, 15), read_u8(data
, 16));
1474 limit
= read_u16(data
, 2);
1476 return 4 + (limit
* 4);
1478 limit
= read_u16(data
, 2);
1480 for (i
= 0; i
< limit
; i
++) {
1481 int32_t val
= read_s32(data
, 4 + (i
* 4));
1482 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1485 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1491 framebuffer_update_request(vs
,
1492 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1493 read_u16(data
, 6), read_u16(data
, 8));
1499 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1505 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1512 uint32_t dlen
= read_u32(data
, 4);
1517 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1523 switch (read_u8(data
, 1)) {
1528 ext_key_event(vs
, read_u16(data
, 2),
1529 read_u32(data
, 4), read_u32(data
, 8));
1535 switch (read_u16 (data
, 2)) {
1545 switch (read_u8(data
, 4)) {
1546 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1547 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1548 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1549 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1550 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1551 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1553 printf("Invalid audio format %d\n", read_u8(data
, 4));
1554 vnc_client_error(vs
);
1557 vs
->as
.nchannels
= read_u8(data
, 5);
1558 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
1559 printf("Invalid audio channel coount %d\n",
1561 vnc_client_error(vs
);
1564 vs
->as
.freq
= read_u32(data
, 6);
1567 printf ("Invalid audio message %d\n", read_u8(data
, 4));
1568 vnc_client_error(vs
);
1574 printf("Msg: %d\n", read_u16(data
, 0));
1575 vnc_client_error(vs
);
1580 printf("Msg: %d\n", data
[0]);
1581 vnc_client_error(vs
);
1585 vnc_read_when(vs
, protocol_client_msg
, 1);
1589 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
1594 vnc_write_u16(vs
, ds_get_width(vs
->ds
));
1595 vnc_write_u16(vs
, ds_get_height(vs
->ds
));
1597 pixel_format_message(vs
);
1600 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
1602 size
= snprintf(buf
, sizeof(buf
), "QEMU");
1604 vnc_write_u32(vs
, size
);
1605 vnc_write(vs
, buf
, size
);
1608 vnc_read_when(vs
, protocol_client_msg
, 1);
1613 void start_client_init(VncState
*vs
)
1615 vnc_read_when(vs
, protocol_client_init
, 1);
1618 static void make_challenge(VncState
*vs
)
1622 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1624 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1625 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1628 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
1630 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
1632 unsigned char key
[8];
1634 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
1635 VNC_DEBUG("No password configured on server");
1636 vnc_write_u32(vs
, 1); /* Reject auth */
1637 if (vs
->minor
>= 8) {
1638 static const char err
[] = "Authentication failed";
1639 vnc_write_u32(vs
, sizeof(err
));
1640 vnc_write(vs
, err
, sizeof(err
));
1643 vnc_client_error(vs
);
1647 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1649 /* Calculate the expected challenge response */
1650 pwlen
= strlen(vs
->vd
->password
);
1651 for (i
=0; i
<sizeof(key
); i
++)
1652 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
1654 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1655 des(response
+j
, response
+j
);
1657 /* Compare expected vs actual challenge response */
1658 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1659 VNC_DEBUG("Client challenge reponse did not match\n");
1660 vnc_write_u32(vs
, 1); /* Reject auth */
1661 if (vs
->minor
>= 8) {
1662 static const char err
[] = "Authentication failed";
1663 vnc_write_u32(vs
, sizeof(err
));
1664 vnc_write(vs
, err
, sizeof(err
));
1667 vnc_client_error(vs
);
1669 VNC_DEBUG("Accepting VNC challenge response\n");
1670 vnc_write_u32(vs
, 0); /* Accept auth */
1673 start_client_init(vs
);
1678 void start_auth_vnc(VncState
*vs
)
1681 /* Send client a 'random' challenge */
1682 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1685 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1689 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
1691 /* We only advertise 1 auth scheme at a time, so client
1692 * must pick the one we sent. Verify this */
1693 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
1694 VNC_DEBUG("Reject auth %d\n", (int)data
[0]);
1695 vnc_write_u32(vs
, 1);
1696 if (vs
->minor
>= 8) {
1697 static const char err
[] = "Authentication failed";
1698 vnc_write_u32(vs
, sizeof(err
));
1699 vnc_write(vs
, err
, sizeof(err
));
1701 vnc_client_error(vs
);
1702 } else { /* Accept requested auth */
1703 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
1704 switch (vs
->vd
->auth
) {
1706 VNC_DEBUG("Accept auth none\n");
1707 if (vs
->minor
>= 8) {
1708 vnc_write_u32(vs
, 0); /* Accept auth completion */
1711 start_client_init(vs
);
1715 VNC_DEBUG("Start VNC auth\n");
1719 #ifdef CONFIG_VNC_TLS
1720 case VNC_AUTH_VENCRYPT
:
1721 VNC_DEBUG("Accept VeNCrypt auth\n");;
1722 start_auth_vencrypt(vs
);
1724 #endif /* CONFIG_VNC_TLS */
1726 default: /* Should not be possible, but just in case */
1727 VNC_DEBUG("Reject auth %d\n", vs
->vd
->auth
);
1728 vnc_write_u8(vs
, 1);
1729 if (vs
->minor
>= 8) {
1730 static const char err
[] = "Authentication failed";
1731 vnc_write_u32(vs
, sizeof(err
));
1732 vnc_write(vs
, err
, sizeof(err
));
1734 vnc_client_error(vs
);
1740 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
1744 memcpy(local
, version
, 12);
1747 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
1748 VNC_DEBUG("Malformed protocol version %s\n", local
);
1749 vnc_client_error(vs
);
1752 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
1753 if (vs
->major
!= 3 ||
1759 VNC_DEBUG("Unsupported client version\n");
1760 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
1762 vnc_client_error(vs
);
1765 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
1766 * as equivalent to v3.3 by servers
1768 if (vs
->minor
== 4 || vs
->minor
== 5)
1771 if (vs
->minor
== 3) {
1772 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
1773 VNC_DEBUG("Tell client auth none\n");
1774 vnc_write_u32(vs
, vs
->vd
->auth
);
1776 start_client_init(vs
);
1777 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
1778 VNC_DEBUG("Tell client VNC auth\n");
1779 vnc_write_u32(vs
, vs
->vd
->auth
);
1783 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
1784 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
1786 vnc_client_error(vs
);
1789 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
1790 vnc_write_u8(vs
, 1); /* num auth */
1791 vnc_write_u8(vs
, vs
->vd
->auth
);
1792 vnc_read_when(vs
, protocol_client_auth
, 1);
1799 static void vnc_connect(VncDisplay
*vd
, int csock
)
1801 VncState
*vs
= qemu_mallocz(sizeof(VncState
));
1804 VNC_DEBUG("New client on socket %d\n", csock
);
1806 socket_set_nonblock(vs
->csock
);
1807 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1811 vs
->timer
= qemu_new_timer(rt_clock
, vnc_update_client
, vs
);
1815 vs
->as
.freq
= 44100;
1816 vs
->as
.nchannels
= 2;
1817 vs
->as
.fmt
= AUD_FMT_S16
;
1818 vs
->as
.endianness
= 0;
1821 vnc_write(vs
, "RFB 003.008\n", 12);
1823 vnc_read_when(vs
, protocol_version
, 12);
1824 memset(vs
->old_data
, 0, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
1825 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
1826 vnc_update_client(vs
);
1829 vs
->next
= vd
->clients
;
1833 static void vnc_listen_read(void *opaque
)
1835 VncDisplay
*vs
= opaque
;
1836 struct sockaddr_in addr
;
1837 socklen_t addrlen
= sizeof(addr
);
1842 int csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
1844 vnc_connect(vs
, csock
);
1848 void vnc_display_init(DisplayState
*ds
)
1852 vs
= qemu_mallocz(sizeof(VncState
));
1853 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
1863 if (keyboard_layout
)
1864 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
1866 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
1868 if (!vs
->kbd_layout
)
1871 dcl
->dpy_copy
= vnc_dpy_copy
;
1872 dcl
->dpy_update
= vnc_dpy_update
;
1873 dcl
->dpy_resize
= vnc_dpy_resize
;
1874 dcl
->dpy_setdata
= vnc_dpy_setdata
;
1875 register_displaychangelistener(ds
, dcl
);
1879 void vnc_display_close(DisplayState
*ds
)
1881 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
1886 qemu_free(vs
->display
);
1889 if (vs
->lsock
!= -1) {
1890 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
1894 vs
->auth
= VNC_AUTH_INVALID
;
1895 #ifdef CONFIG_VNC_TLS
1896 vs
->subauth
= VNC_AUTH_INVALID
;
1897 vs
->tls
.x509verify
= 0;
1901 int vnc_display_password(DisplayState
*ds
, const char *password
)
1903 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
1906 qemu_free(vs
->password
);
1907 vs
->password
= NULL
;
1909 if (password
&& password
[0]) {
1910 if (!(vs
->password
= qemu_strdup(password
)))
1917 int vnc_display_open(DisplayState
*ds
, const char *display
)
1919 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
1920 const char *options
;
1924 #ifdef CONFIG_VNC_TLS
1925 int tls
= 0, x509
= 0;
1930 vnc_display_close(ds
);
1931 if (strcmp(display
, "none") == 0)
1934 if (!(vs
->display
= strdup(display
)))
1938 while ((options
= strchr(options
, ','))) {
1940 if (strncmp(options
, "password", 8) == 0) {
1941 password
= 1; /* Require password auth */
1942 } else if (strncmp(options
, "reverse", 7) == 0) {
1944 } else if (strncmp(options
, "to=", 3) == 0) {
1945 to_port
= atoi(options
+3) + 5900;
1946 #ifdef CONFIG_VNC_TLS
1947 } else if (strncmp(options
, "tls", 3) == 0) {
1948 tls
= 1; /* Require TLS */
1949 } else if (strncmp(options
, "x509", 4) == 0) {
1951 x509
= 1; /* Require x509 certificates */
1952 if (strncmp(options
, "x509verify", 10) == 0)
1953 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
1955 /* Now check for 'x509=/some/path' postfix
1956 * and use that to setup x509 certificate/key paths */
1957 start
= strchr(options
, '=');
1958 end
= strchr(options
, ',');
1959 if (start
&& (!end
|| (start
< end
))) {
1960 int len
= end
? end
-(start
+1) : strlen(start
+1);
1961 char *path
= qemu_strndup(start
+ 1, len
);
1963 VNC_DEBUG("Trying certificate path '%s'\n", path
);
1964 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
1965 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
1967 qemu_free(vs
->display
);
1973 fprintf(stderr
, "No certificate path provided\n");
1974 qemu_free(vs
->display
);
1983 #ifdef CONFIG_VNC_TLS
1985 vs
->auth
= VNC_AUTH_VENCRYPT
;
1987 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
1988 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
1990 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
1991 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
1995 VNC_DEBUG("Initializing VNC server with password auth\n");
1996 vs
->auth
= VNC_AUTH_VNC
;
1997 #ifdef CONFIG_VNC_TLS
1998 vs
->subauth
= VNC_AUTH_INVALID
;
2002 #ifdef CONFIG_VNC_TLS
2004 vs
->auth
= VNC_AUTH_VENCRYPT
;
2006 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2007 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2009 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2010 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2014 VNC_DEBUG("Initializing VNC server with no auth\n");
2015 vs
->auth
= VNC_AUTH_NONE
;
2016 #ifdef CONFIG_VNC_TLS
2017 vs
->subauth
= VNC_AUTH_INVALID
;
2023 /* connect to viewer */
2024 if (strncmp(display
, "unix:", 5) == 0)
2025 vs
->lsock
= unix_connect(display
+5);
2027 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2028 if (-1 == vs
->lsock
) {
2033 int csock
= vs
->lsock
;
2035 vnc_connect(vs
, csock
);
2040 /* listen for connects */
2042 dpy
= qemu_malloc(256);
2043 if (strncmp(display
, "unix:", 5) == 0) {
2044 pstrcpy(dpy
, 256, "unix:");
2045 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2047 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2049 if (-1 == vs
->lsock
) {
2057 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);