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"
33 #define VNC_REFRESH_INTERVAL (1000 / 30)
35 #include "vnc_keysym.h"
38 #define count_bits(c, v) { \
39 for (c = 0; v; v >>= 1) \
46 static VncDisplay
*vnc_display
; /* needed for info vnc */
47 static DisplayChangeListener
*dcl
;
49 static char *addr_to_string(const char *format
,
50 struct sockaddr_storage
*sa
,
53 char host
[NI_MAXHOST
];
54 char serv
[NI_MAXSERV
];
57 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
60 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
61 VNC_DEBUG("Cannot resolve address %d: %s\n",
62 err
, gai_strerror(err
));
66 if (asprintf(&addr
, format
, host
, serv
) < 0)
73 char *vnc_socket_local_addr(const char *format
, int fd
) {
74 struct sockaddr_storage sa
;
78 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
81 return addr_to_string(format
, &sa
, salen
);
85 char *vnc_socket_remote_addr(const char *format
, int fd
) {
86 struct sockaddr_storage sa
;
90 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
93 return addr_to_string(format
, &sa
, salen
);
96 static const char *vnc_auth_name(VncDisplay
*vd
) {
98 case VNC_AUTH_INVALID
:
114 case VNC_AUTH_VENCRYPT
:
115 #ifdef CONFIG_VNC_TLS
116 switch (vd
->subauth
) {
117 case VNC_AUTH_VENCRYPT_PLAIN
:
118 return "vencrypt+plain";
119 case VNC_AUTH_VENCRYPT_TLSNONE
:
120 return "vencrypt+tls+none";
121 case VNC_AUTH_VENCRYPT_TLSVNC
:
122 return "vencrypt+tls+vnc";
123 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
124 return "vencrypt+tls+plain";
125 case VNC_AUTH_VENCRYPT_X509NONE
:
126 return "vencrypt+x509+none";
127 case VNC_AUTH_VENCRYPT_X509VNC
:
128 return "vencrypt+x509+vnc";
129 case VNC_AUTH_VENCRYPT_X509PLAIN
:
130 return "vencrypt+x509+plain";
131 case VNC_AUTH_VENCRYPT_TLSSASL
:
132 return "vencrypt+tls+sasl";
133 case VNC_AUTH_VENCRYPT_X509SASL
:
134 return "vencrypt+x509+sasl";
147 #define VNC_SOCKET_FORMAT_PRETTY "local %s:%s"
149 static void do_info_vnc_client(VncState
*client
)
152 vnc_socket_remote_addr(" address: %s:%s\n",
157 term_puts("Client:\n");
158 term_puts(clientAddr
);
161 #ifdef CONFIG_VNC_TLS
162 if (client
->tls
.session
&&
164 term_printf(" x509 dname: %s\n", client
->tls
.dname
);
166 term_puts(" x509 dname: none\n");
168 #ifdef CONFIG_VNC_SASL
169 if (client
->sasl
.conn
&&
170 client
->sasl
.username
)
171 term_printf(" username: %s\n", client
->sasl
.username
);
173 term_puts(" username: none\n");
177 void do_info_vnc(void)
179 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
180 term_printf("Server: disabled\n");
182 char *serverAddr
= vnc_socket_local_addr(" address: %s:%s\n",
188 term_puts("Server:\n");
189 term_puts(serverAddr
);
191 term_printf(" auth: %s\n", vnc_auth_name(vnc_display
));
193 if (vnc_display
->clients
) {
194 VncState
*client
= vnc_display
->clients
;
196 do_info_vnc_client(client
);
197 client
= client
->next
;
200 term_printf("Client: none\n");
205 static inline uint32_t vnc_has_feature(VncState
*vs
, int feature
) {
206 return (vs
->features
& (1 << feature
));
210 1) Get the queue working for IO.
211 2) there is some weirdness when using the -S option (the screen is grey
212 and not totally invalidated
213 3) resolutions > 1024
216 static void vnc_update_client(void *opaque
);
217 static void vnc_disconnect_start(VncState
*vs
);
218 static void vnc_disconnect_finish(VncState
*vs
);
220 static void vnc_colordepth(VncState
*vs
);
222 static inline void vnc_set_bit(uint32_t *d
, int k
)
224 d
[k
>> 5] |= 1 << (k
& 0x1f);
227 static inline void vnc_clear_bit(uint32_t *d
, int k
)
229 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
232 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
242 d
[j
++] = (1 << n
) - 1;
247 static inline int vnc_get_bit(const uint32_t *d
, int k
)
249 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
252 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
256 for(i
= 0; i
< nb_words
; i
++) {
257 if ((d1
[i
] & d2
[i
]) != 0)
263 static void vnc_update(VncState
*vs
, int x
, int y
, int w
, int h
)
269 /* round x down to ensure the loop only spans one 16-pixel block per,
270 iteration. otherwise, if (x % 16) != 0, the last iteration may span
271 two 16-pixel blocks but we only mark the first as dirty
276 x
= MIN(x
, vs
->serverds
.width
);
277 y
= MIN(y
, vs
->serverds
.height
);
278 w
= MIN(x
+ w
, vs
->serverds
.width
) - x
;
279 h
= MIN(h
, vs
->serverds
.height
);
282 for (i
= 0; i
< w
; i
+= 16)
283 vnc_set_bit(vs
->dirty_row
[y
], (x
+ i
) / 16);
286 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
288 VncDisplay
*vd
= ds
->opaque
;
289 VncState
*vs
= vd
->clients
;
291 vnc_update(vs
, x
, y
, w
, h
);
296 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
299 vnc_write_u16(vs
, x
);
300 vnc_write_u16(vs
, y
);
301 vnc_write_u16(vs
, w
);
302 vnc_write_u16(vs
, h
);
304 vnc_write_s32(vs
, encoding
);
307 void buffer_reserve(Buffer
*buffer
, size_t len
)
309 if ((buffer
->capacity
- buffer
->offset
) < len
) {
310 buffer
->capacity
+= (len
+ 1024);
311 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
312 if (buffer
->buffer
== NULL
) {
313 fprintf(stderr
, "vnc: out of memory\n");
319 int buffer_empty(Buffer
*buffer
)
321 return buffer
->offset
== 0;
324 uint8_t *buffer_end(Buffer
*buffer
)
326 return buffer
->buffer
+ buffer
->offset
;
329 void buffer_reset(Buffer
*buffer
)
334 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
336 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
337 buffer
->offset
+= len
;
340 static void vnc_resize(VncState
*vs
)
342 DisplayState
*ds
= vs
->ds
;
346 vs
->old_data
= qemu_realloc(vs
->old_data
, ds_get_linesize(ds
) * ds_get_height(ds
));
348 if (vs
->old_data
== NULL
) {
349 fprintf(stderr
, "vnc: memory allocation failed\n");
353 if (ds_get_bytes_per_pixel(ds
) != vs
->serverds
.pf
.bytes_per_pixel
)
354 console_color_init(ds
);
356 size_changed
= ds_get_width(ds
) != vs
->serverds
.width
||
357 ds_get_height(ds
) != vs
->serverds
.height
;
358 vs
->serverds
= *(ds
->surface
);
360 if (vs
->csock
!= -1 && vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
361 vnc_write_u8(vs
, 0); /* msg id */
363 vnc_write_u16(vs
, 1); /* number of rects */
364 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(ds
), ds_get_height(ds
),
365 VNC_ENCODING_DESKTOPRESIZE
);
370 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
371 memset(vs
->old_data
, 42, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
374 static void vnc_dpy_resize(DisplayState
*ds
)
376 VncDisplay
*vd
= ds
->opaque
;
377 VncState
*vs
= vd
->clients
;
385 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
387 vnc_write(vs
, pixels
, size
);
390 /* slowest but generic code. */
391 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
395 r
= ((((v
& vs
->serverds
.pf
.rmask
) >> vs
->serverds
.pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
396 vs
->serverds
.pf
.rbits
);
397 g
= ((((v
& vs
->serverds
.pf
.gmask
) >> vs
->serverds
.pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
398 vs
->serverds
.pf
.gbits
);
399 b
= ((((v
& vs
->serverds
.pf
.bmask
) >> vs
->serverds
.pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
400 vs
->serverds
.pf
.bbits
);
401 v
= (r
<< vs
->clientds
.pf
.rshift
) |
402 (g
<< vs
->clientds
.pf
.gshift
) |
403 (b
<< vs
->clientds
.pf
.bshift
);
404 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
409 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
419 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
434 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
438 if (vs
->serverds
.pf
.bytes_per_pixel
== 4) {
439 uint32_t *pixels
= pixels1
;
442 for(i
= 0; i
< n
; i
++) {
443 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
444 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
446 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 2) {
447 uint16_t *pixels
= pixels1
;
450 for(i
= 0; i
< n
; i
++) {
451 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
452 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
454 } else if (vs
->serverds
.pf
.bytes_per_pixel
== 1) {
455 uint8_t *pixels
= pixels1
;
458 for(i
= 0; i
< n
; i
++) {
459 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
460 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
463 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
467 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
472 row
= ds_get_data(vs
->ds
) + y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
473 for (i
= 0; i
< h
; i
++) {
474 vs
->write_pixels(vs
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
475 row
+= ds_get_linesize(vs
->ds
);
479 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
481 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
482 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
486 #include "vnchextile.h"
490 #include "vnchextile.h"
494 #include "vnchextile.h"
499 #include "vnchextile.h"
505 #include "vnchextile.h"
511 #include "vnchextile.h"
515 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
519 uint8_t *last_fg
, *last_bg
;
521 last_fg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
522 last_bg
= (uint8_t *) qemu_malloc(vs
->serverds
.pf
.bytes_per_pixel
);
524 for (j
= y
; j
< (y
+ h
); j
+= 16) {
525 for (i
= x
; i
< (x
+ w
); i
+= 16) {
526 vs
->send_hextile_tile(vs
, i
, j
,
527 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
528 last_bg
, last_fg
, &has_bg
, &has_fg
);
536 static void vnc_zlib_init(VncState
*vs
)
539 for (i
=0; i
<(sizeof(vs
->zlib_stream
) / sizeof(z_stream
)); i
++)
540 vs
->zlib_stream
[i
].opaque
= NULL
;
543 static void vnc_zlib_start(VncState
*vs
)
545 buffer_reset(&vs
->zlib
);
547 // make the output buffer be the zlib buffer, so we can compress it later
548 vs
->zlib_tmp
= vs
->output
;
549 vs
->output
= vs
->zlib
;
552 static int vnc_zlib_stop(VncState
*vs
, int stream_id
)
554 z_streamp zstream
= &vs
->zlib_stream
[stream_id
];
557 // switch back to normal output/zlib buffers
558 vs
->zlib
= vs
->output
;
559 vs
->output
= vs
->zlib_tmp
;
561 // compress the zlib buffer
563 // initialize the stream
564 // XXX need one stream per session
565 if (zstream
->opaque
!= vs
) {
568 VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id
);
569 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
570 zstream
->zalloc
= Z_NULL
;
571 zstream
->zfree
= Z_NULL
;
573 err
= deflateInit2(zstream
, vs
->tight_compression
, Z_DEFLATED
, MAX_WBITS
,
574 MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
577 fprintf(stderr
, "VNC: error initializing zlib\n");
581 zstream
->opaque
= vs
;
584 // XXX what to do if tight_compression changed in between?
586 // reserve memory in output buffer
587 buffer_reserve(&vs
->output
, vs
->zlib
.offset
+ 64);
590 zstream
->next_in
= vs
->zlib
.buffer
;
591 zstream
->avail_in
= vs
->zlib
.offset
;
592 zstream
->next_out
= vs
->output
.buffer
+ vs
->output
.offset
;
593 zstream
->avail_out
= vs
->output
.capacity
- vs
->output
.offset
;
594 zstream
->data_type
= Z_BINARY
;
595 previous_out
= zstream
->total_out
;
598 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
599 fprintf(stderr
, "VNC: error during zlib compression\n");
603 vs
->output
.offset
= vs
->output
.capacity
- zstream
->avail_out
;
604 return zstream
->total_out
- previous_out
;
607 static void send_framebuffer_update_zlib(VncState
*vs
, int x
, int y
, int w
, int h
)
609 int old_offset
, new_offset
, bytes_written
;
611 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_ZLIB
);
613 // remember where we put in the follow-up size
614 old_offset
= vs
->output
.offset
;
615 vnc_write_s32(vs
, 0);
617 // compress the stream
619 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
620 bytes_written
= vnc_zlib_stop(vs
, 0);
622 if (bytes_written
== -1)
626 new_offset
= vs
->output
.offset
;
627 vs
->output
.offset
= old_offset
;
628 vnc_write_u32(vs
, bytes_written
);
629 vs
->output
.offset
= new_offset
;
632 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
634 switch(vs
->vnc_encoding
) {
635 case VNC_ENCODING_ZLIB
:
636 send_framebuffer_update_zlib(vs
, x
, y
, w
, h
);
638 case VNC_ENCODING_HEXTILE
:
639 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
640 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
643 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
644 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
649 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
656 vnc_update_client(vs
);
658 /* send bitblit op to the vnc client */
659 vnc_write_u8(vs
, 0); /* msg id */
661 vnc_write_u16(vs
, 1); /* number of rects */
662 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
663 vnc_write_u16(vs
, src_x
);
664 vnc_write_u16(vs
, src_y
);
667 /* do bitblit op on the local surface too */
668 pitch
= ds_get_linesize(vs
->ds
);
669 depth
= ds_get_bytes_per_pixel(vs
->ds
);
670 src_row
= ds_get_data(vs
->ds
) + pitch
* src_y
+ depth
* src_x
;
671 dst_row
= ds_get_data(vs
->ds
) + pitch
* dst_y
+ depth
* dst_x
;
674 src_row
+= pitch
* (h
-1);
675 dst_row
+= pitch
* (h
-1);
678 for (y
= 0; y
< h
; y
++) {
679 memmove(dst_row
, src_row
, w
* depth
);
685 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
687 VncDisplay
*vd
= ds
->opaque
;
690 for (vs
= vd
->clients
; vs
!= NULL
; vs
= vn
) {
692 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
693 vnc_update_client(vs
);
694 /* vs might be free()ed here */
698 for (vs
= vd
->clients
; vs
!= NULL
; vs
= vs
->next
) {
699 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
))
700 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
702 vnc_update(vs
, dst_x
, dst_y
, w
, h
);
706 static int find_dirty_height(VncState
*vs
, int y
, int last_x
, int x
)
710 for (h
= 1; h
< (vs
->serverds
.height
- y
); h
++) {
712 if (!vnc_get_bit(vs
->dirty_row
[y
+ h
], last_x
))
714 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
715 vnc_clear_bit(vs
->dirty_row
[y
+ h
], tmp_x
);
721 static void vnc_update_client(void *opaque
)
723 VncState
*vs
= opaque
;
724 if (vs
->need_update
&& vs
->csock
!= -1) {
728 uint32_t width_mask
[VNC_DIRTY_WORDS
];
735 vnc_set_bits(width_mask
, (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
737 /* Walk through the dirty map and eliminate tiles that
738 really aren't dirty */
739 row
= ds_get_data(vs
->ds
);
740 old_row
= vs
->old_data
;
742 for (y
= 0; y
< ds_get_height(vs
->ds
); y
++) {
743 if (vnc_and_bits(vs
->dirty_row
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
749 old_ptr
= (char*)old_row
;
751 for (x
= 0; x
< ds_get_width(vs
->ds
); x
+= 16) {
752 if (memcmp(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
)) == 0) {
753 vnc_clear_bit(vs
->dirty_row
[y
], (x
/ 16));
756 memcpy(old_ptr
, ptr
, 16 * ds_get_bytes_per_pixel(vs
->ds
));
759 ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
760 old_ptr
+= 16 * ds_get_bytes_per_pixel(vs
->ds
);
764 row
+= ds_get_linesize(vs
->ds
);
765 old_row
+= ds_get_linesize(vs
->ds
);
768 if (!has_dirty
&& !vs
->audio_cap
) {
769 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
773 /* Count rectangles */
775 vnc_write_u8(vs
, 0); /* msg id */
777 saved_offset
= vs
->output
.offset
;
778 vnc_write_u16(vs
, 0);
780 for (y
= 0; y
< vs
->serverds
.height
; y
++) {
783 for (x
= 0; x
< vs
->serverds
.width
/ 16; x
++) {
784 if (vnc_get_bit(vs
->dirty_row
[y
], x
)) {
788 vnc_clear_bit(vs
->dirty_row
[y
], x
);
791 int h
= find_dirty_height(vs
, y
, last_x
, x
);
792 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
799 int h
= find_dirty_height(vs
, y
, last_x
, x
);
800 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
804 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
805 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
810 if (vs
->csock
!= -1) {
811 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
813 vnc_disconnect_finish(vs
);
819 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
821 VncState
*vs
= opaque
;
824 case AUD_CNOTIFY_DISABLE
:
825 vnc_write_u8(vs
, 255);
827 vnc_write_u16(vs
, 0);
831 case AUD_CNOTIFY_ENABLE
:
832 vnc_write_u8(vs
, 255);
834 vnc_write_u16(vs
, 1);
840 static void audio_capture_destroy(void *opaque
)
844 static void audio_capture(void *opaque
, void *buf
, int size
)
846 VncState
*vs
= opaque
;
848 vnc_write_u8(vs
, 255);
850 vnc_write_u16(vs
, 2);
851 vnc_write_u32(vs
, size
);
852 vnc_write(vs
, buf
, size
);
856 static void audio_add(VncState
*vs
)
858 struct audio_capture_ops ops
;
861 term_printf ("audio already running\n");
865 ops
.notify
= audio_capture_notify
;
866 ops
.destroy
= audio_capture_destroy
;
867 ops
.capture
= audio_capture
;
869 vs
->audio_cap
= AUD_add_capture(NULL
, &vs
->as
, &ops
, vs
);
870 if (!vs
->audio_cap
) {
871 term_printf ("Failed to add audio capture\n");
875 static void audio_del(VncState
*vs
)
878 AUD_del_capture(vs
->audio_cap
, vs
);
879 vs
->audio_cap
= NULL
;
883 static void vnc_disconnect_start(VncState
*vs
)
887 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
888 closesocket(vs
->csock
);
892 static void vnc_disconnect_finish(VncState
*vs
)
894 qemu_del_timer(vs
->timer
);
895 qemu_free_timer(vs
->timer
);
896 if (vs
->input
.buffer
) qemu_free(vs
->input
.buffer
);
897 if (vs
->output
.buffer
) qemu_free(vs
->output
.buffer
);
898 #ifdef CONFIG_VNC_TLS
899 vnc_tls_client_cleanup(vs
);
900 #endif /* CONFIG_VNC_TLS */
901 #ifdef CONFIG_VNC_SASL
902 vnc_sasl_client_cleanup(vs
);
903 #endif /* CONFIG_VNC_SASL */
906 VncState
*p
, *parent
= NULL
;
907 for (p
= vs
->vd
->clients
; p
!= NULL
; p
= p
->next
) {
910 parent
->next
= p
->next
;
912 vs
->vd
->clients
= p
->next
;
917 if (!vs
->vd
->clients
)
920 qemu_free(vs
->old_data
);
924 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
926 if (ret
== 0 || ret
== -1) {
928 switch (last_errno
) {
940 VNC_DEBUG("Closing down client sock %d %d\n", ret
, ret
< 0 ? last_errno
: 0);
941 vnc_disconnect_start(vs
);
949 void vnc_client_error(VncState
*vs
)
951 VNC_DEBUG("Closing down client sock: protocol error\n");
952 vnc_disconnect_start(vs
);
957 * Called to write a chunk of data to the client socket. The data may
958 * be the raw data, or may have already been encoded by SASL.
959 * The data will be written either straight onto the socket, or
960 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
962 * NB, it is theoretically possible to have 2 layers of encryption,
963 * both SASL, and this TLS layer. It is highly unlikely in practice
964 * though, since SASL encryption will typically be a no-op if TLS
967 * Returns the number of bytes written, which may be less than
968 * the requested 'datalen' if the socket would block. Returns
969 * -1 on error, and disconnects the client socket.
971 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
974 #ifdef CONFIG_VNC_TLS
975 if (vs
->tls
.session
) {
976 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
978 if (ret
== GNUTLS_E_AGAIN
)
985 #endif /* CONFIG_VNC_TLS */
986 ret
= send(vs
->csock
, data
, datalen
, 0);
987 VNC_DEBUG("Wrote wire %p %d -> %ld\n", data
, datalen
, ret
);
988 return vnc_client_io_error(vs
, ret
, socket_error());
993 * Called to write buffered data to the client socket, when not
994 * using any SASL SSF encryption layers. Will write as much data
995 * as possible without blocking. If all buffered data is written,
996 * will switch the FD poll() handler back to read monitoring.
998 * Returns the number of bytes written, which may be less than
999 * the buffered output data if the socket would block. Returns
1000 * -1 on error, and disconnects the client socket.
1002 static long vnc_client_write_plain(VncState
*vs
)
1006 #ifdef CONFIG_VNC_SASL
1007 VNC_DEBUG("Write Plain: Pending output %p size %d offset %d. Wait SSF %d\n",
1008 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1009 vs
->sasl
.waitWriteSSF
);
1011 if (vs
->sasl
.conn
&&
1013 vs
->sasl
.waitWriteSSF
) {
1014 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1016 vs
->sasl
.waitWriteSSF
-= ret
;
1018 #endif /* CONFIG_VNC_SASL */
1019 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1023 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1024 vs
->output
.offset
-= ret
;
1026 if (vs
->output
.offset
== 0) {
1027 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1035 * First function called whenever there is data to be written to
1036 * the client socket. Will delegate actual work according to whether
1037 * SASL SSF layers are enabled (thus requiring encryption calls)
1039 void vnc_client_write(void *opaque
)
1042 VncState
*vs
= opaque
;
1044 #ifdef CONFIG_VNC_SASL
1045 if (vs
->sasl
.conn
&&
1047 !vs
->sasl
.waitWriteSSF
)
1048 ret
= vnc_client_write_sasl(vs
);
1050 #endif /* CONFIG_VNC_SASL */
1051 ret
= vnc_client_write_plain(vs
);
1054 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1056 vs
->read_handler
= func
;
1057 vs
->read_handler_expect
= expecting
;
1062 * Called to read a chunk of data from the client socket. The data may
1063 * be the raw data, or may need to be further decoded by SASL.
1064 * The data will be read either straight from to the socket, or
1065 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1067 * NB, it is theoretically possible to have 2 layers of encryption,
1068 * both SASL, and this TLS layer. It is highly unlikely in practice
1069 * though, since SASL encryption will typically be a no-op if TLS
1072 * Returns the number of bytes read, which may be less than
1073 * the requested 'datalen' if the socket would block. Returns
1074 * -1 on error, and disconnects the client socket.
1076 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1079 #ifdef CONFIG_VNC_TLS
1080 if (vs
->tls
.session
) {
1081 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1083 if (ret
== GNUTLS_E_AGAIN
)
1090 #endif /* CONFIG_VNC_TLS */
1091 ret
= recv(vs
->csock
, data
, datalen
, 0);
1092 VNC_DEBUG("Read wire %p %d -> %ld\n", data
, datalen
, ret
);
1093 return vnc_client_io_error(vs
, ret
, socket_error());
1098 * Called to read data from the client socket to the input buffer,
1099 * when not using any SASL SSF encryption layers. Will read as much
1100 * data as possible without blocking.
1102 * Returns the number of bytes read. Returns -1 on error, and
1103 * disconnects the client socket.
1105 static long vnc_client_read_plain(VncState
*vs
)
1108 VNC_DEBUG("Read plain %p size %d offset %d\n",
1109 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1110 buffer_reserve(&vs
->input
, 4096);
1111 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1114 vs
->input
.offset
+= ret
;
1120 * First function called whenever there is more data to be read from
1121 * the client socket. Will delegate actual work according to whether
1122 * SASL SSF layers are enabled (thus requiring decryption calls)
1124 void vnc_client_read(void *opaque
)
1126 VncState
*vs
= opaque
;
1129 #ifdef CONFIG_VNC_SASL
1130 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1131 ret
= vnc_client_read_sasl(vs
);
1133 #endif /* CONFIG_VNC_SASL */
1134 ret
= vnc_client_read_plain(vs
);
1136 if (vs
->csock
== -1)
1137 vnc_disconnect_finish(vs
);
1141 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1142 size_t len
= vs
->read_handler_expect
;
1145 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1146 if (vs
->csock
== -1) {
1147 vnc_disconnect_finish(vs
);
1152 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1153 vs
->input
.offset
-= len
;
1155 vs
->read_handler_expect
= ret
;
1160 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1162 buffer_reserve(&vs
->output
, len
);
1164 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1165 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1168 buffer_append(&vs
->output
, data
, len
);
1171 void vnc_write_s32(VncState
*vs
, int32_t value
)
1173 vnc_write_u32(vs
, *(uint32_t *)&value
);
1176 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1180 buf
[0] = (value
>> 24) & 0xFF;
1181 buf
[1] = (value
>> 16) & 0xFF;
1182 buf
[2] = (value
>> 8) & 0xFF;
1183 buf
[3] = value
& 0xFF;
1185 vnc_write(vs
, buf
, 4);
1188 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1192 buf
[0] = (value
>> 8) & 0xFF;
1193 buf
[1] = value
& 0xFF;
1195 vnc_write(vs
, buf
, 2);
1198 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1200 vnc_write(vs
, (char *)&value
, 1);
1203 void vnc_flush(VncState
*vs
)
1205 if (vs
->csock
!= -1 && vs
->output
.offset
)
1206 vnc_client_write(vs
);
1209 uint8_t read_u8(uint8_t *data
, size_t offset
)
1211 return data
[offset
];
1214 uint16_t read_u16(uint8_t *data
, size_t offset
)
1216 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1219 int32_t read_s32(uint8_t *data
, size_t offset
)
1221 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1222 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1225 uint32_t read_u32(uint8_t *data
, size_t offset
)
1227 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1228 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1231 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1235 static void check_pointer_type_change(VncState
*vs
, int absolute
)
1237 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1238 vnc_write_u8(vs
, 0);
1239 vnc_write_u8(vs
, 0);
1240 vnc_write_u16(vs
, 1);
1241 vnc_framebuffer_update(vs
, absolute
, 0,
1242 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1243 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1246 vs
->absolute
= absolute
;
1249 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1254 if (button_mask
& 0x01)
1255 buttons
|= MOUSE_EVENT_LBUTTON
;
1256 if (button_mask
& 0x02)
1257 buttons
|= MOUSE_EVENT_MBUTTON
;
1258 if (button_mask
& 0x04)
1259 buttons
|= MOUSE_EVENT_RBUTTON
;
1260 if (button_mask
& 0x08)
1262 if (button_mask
& 0x10)
1266 kbd_mouse_event(x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1),
1267 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1),
1269 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1273 kbd_mouse_event(x
, y
, dz
, buttons
);
1275 if (vs
->last_x
!= -1)
1276 kbd_mouse_event(x
- vs
->last_x
,
1283 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1286 static void reset_keys(VncState
*vs
)
1289 for(i
= 0; i
< 256; i
++) {
1290 if (vs
->modifiers_state
[i
]) {
1292 kbd_put_keycode(0xe0);
1293 kbd_put_keycode(i
| 0x80);
1294 vs
->modifiers_state
[i
] = 0;
1299 static void press_key(VncState
*vs
, int keysym
)
1301 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & 0x7f);
1302 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) | 0x80);
1305 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1307 /* QEMU console switch */
1309 case 0x2a: /* Left Shift */
1310 case 0x36: /* Right Shift */
1311 case 0x1d: /* Left CTRL */
1312 case 0x9d: /* Right CTRL */
1313 case 0x38: /* Left ALT */
1314 case 0xb8: /* Right ALT */
1316 vs
->modifiers_state
[keycode
] = 1;
1318 vs
->modifiers_state
[keycode
] = 0;
1320 case 0x02 ... 0x0a: /* '1' to '9' keys */
1321 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1322 /* Reset the modifiers sent to the current console */
1324 console_select(keycode
- 0x02);
1328 case 0x3a: /* CapsLock */
1329 case 0x45: /* NumLock */
1331 vs
->modifiers_state
[keycode
] ^= 1;
1335 if (keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1336 /* If the numlock state needs to change then simulate an additional
1337 keypress before sending this one. This will happen if the user
1338 toggles numlock away from the VNC window.
1340 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1341 if (!vs
->modifiers_state
[0x45]) {
1342 vs
->modifiers_state
[0x45] = 1;
1343 press_key(vs
, 0xff7f);
1346 if (vs
->modifiers_state
[0x45]) {
1347 vs
->modifiers_state
[0x45] = 0;
1348 press_key(vs
, 0xff7f);
1353 if (is_graphic_console()) {
1355 kbd_put_keycode(0xe0);
1357 kbd_put_keycode(keycode
& 0x7f);
1359 kbd_put_keycode(keycode
| 0x80);
1361 /* QEMU console emulation */
1364 case 0x2a: /* Left Shift */
1365 case 0x36: /* Right Shift */
1366 case 0x1d: /* Left CTRL */
1367 case 0x9d: /* Right CTRL */
1368 case 0x38: /* Left ALT */
1369 case 0xb8: /* Right ALT */
1373 kbd_put_keysym(QEMU_KEY_UP
);
1377 kbd_put_keysym(QEMU_KEY_DOWN
);
1381 kbd_put_keysym(QEMU_KEY_LEFT
);
1385 kbd_put_keysym(QEMU_KEY_RIGHT
);
1389 kbd_put_keysym(QEMU_KEY_DELETE
);
1393 kbd_put_keysym(QEMU_KEY_HOME
);
1397 kbd_put_keysym(QEMU_KEY_END
);
1401 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1405 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1408 kbd_put_keysym(sym
);
1415 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1419 if (sym
>= 'A' && sym
<= 'Z' && is_graphic_console())
1420 sym
= sym
- 'A' + 'a';
1422 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, sym
& 0xFFFF);
1423 do_key_event(vs
, down
, keycode
, sym
);
1426 static void ext_key_event(VncState
*vs
, int down
,
1427 uint32_t sym
, uint16_t keycode
)
1429 /* if the user specifies a keyboard layout, always use it */
1430 if (keyboard_layout
)
1431 key_event(vs
, down
, sym
);
1433 do_key_event(vs
, down
, keycode
, sym
);
1436 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1437 int x_position
, int y_position
,
1440 if (x_position
> ds_get_width(vs
->ds
))
1441 x_position
= ds_get_width(vs
->ds
);
1442 if (y_position
> ds_get_height(vs
->ds
))
1443 y_position
= ds_get_height(vs
->ds
);
1444 if (x_position
+ w
>= ds_get_width(vs
->ds
))
1445 w
= ds_get_width(vs
->ds
) - x_position
;
1446 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1447 h
= ds_get_height(vs
->ds
) - y_position
;
1450 vs
->need_update
= 1;
1452 char *old_row
= vs
->old_data
+ y_position
* ds_get_linesize(vs
->ds
);
1454 for (i
= 0; i
< h
; i
++) {
1455 vnc_set_bits(vs
->dirty_row
[y_position
+ i
],
1456 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1457 memset(old_row
, 42, ds_get_width(vs
->ds
) * ds_get_bytes_per_pixel(vs
->ds
));
1458 old_row
+= ds_get_linesize(vs
->ds
);
1463 static void send_ext_key_event_ack(VncState
*vs
)
1465 vnc_write_u8(vs
, 0);
1466 vnc_write_u8(vs
, 0);
1467 vnc_write_u16(vs
, 1);
1468 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1469 VNC_ENCODING_EXT_KEY_EVENT
);
1473 static void send_ext_audio_ack(VncState
*vs
)
1475 vnc_write_u8(vs
, 0);
1476 vnc_write_u8(vs
, 0);
1477 vnc_write_u16(vs
, 1);
1478 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1479 VNC_ENCODING_AUDIO
);
1483 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1486 unsigned int enc
= 0;
1490 vs
->vnc_encoding
= 0;
1491 vs
->tight_compression
= 9;
1492 vs
->tight_quality
= 9;
1495 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1498 case VNC_ENCODING_RAW
:
1499 vs
->vnc_encoding
= enc
;
1501 case VNC_ENCODING_COPYRECT
:
1502 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1504 case VNC_ENCODING_HEXTILE
:
1505 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1506 vs
->vnc_encoding
= enc
;
1508 case VNC_ENCODING_ZLIB
:
1509 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1510 vs
->vnc_encoding
= enc
;
1512 case VNC_ENCODING_DESKTOPRESIZE
:
1513 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1515 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1516 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1518 case VNC_ENCODING_EXT_KEY_EVENT
:
1519 send_ext_key_event_ack(vs
);
1521 case VNC_ENCODING_AUDIO
:
1522 send_ext_audio_ack(vs
);
1524 case VNC_ENCODING_WMVi
:
1525 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1527 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1528 vs
->tight_compression
= (enc
& 0x0F);
1530 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1531 vs
->tight_quality
= (enc
& 0x0F);
1534 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1539 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1542 static void set_pixel_conversion(VncState
*vs
)
1544 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1545 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1546 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1547 vs
->write_pixels
= vnc_write_pixels_copy
;
1548 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1550 vs
->send_hextile_tile
= send_hextile_tile_8
;
1553 vs
->send_hextile_tile
= send_hextile_tile_16
;
1556 vs
->send_hextile_tile
= send_hextile_tile_32
;
1560 vs
->write_pixels
= vnc_write_pixels_generic
;
1561 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1563 vs
->send_hextile_tile
= send_hextile_tile_generic_8
;
1566 vs
->send_hextile_tile
= send_hextile_tile_generic_16
;
1569 vs
->send_hextile_tile
= send_hextile_tile_generic_32
;
1575 static void set_pixel_format(VncState
*vs
,
1576 int bits_per_pixel
, int depth
,
1577 int big_endian_flag
, int true_color_flag
,
1578 int red_max
, int green_max
, int blue_max
,
1579 int red_shift
, int green_shift
, int blue_shift
)
1581 if (!true_color_flag
) {
1582 vnc_client_error(vs
);
1586 vs
->clientds
= vs
->serverds
;
1587 vs
->clientds
.pf
.rmax
= red_max
;
1588 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1589 vs
->clientds
.pf
.rshift
= red_shift
;
1590 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1591 vs
->clientds
.pf
.gmax
= green_max
;
1592 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1593 vs
->clientds
.pf
.gshift
= green_shift
;
1594 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1595 vs
->clientds
.pf
.bmax
= blue_max
;
1596 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1597 vs
->clientds
.pf
.bshift
= blue_shift
;
1598 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1599 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1600 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1601 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1602 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1604 set_pixel_conversion(vs
);
1606 vga_hw_invalidate();
1610 static void pixel_format_message (VncState
*vs
) {
1611 char pad
[3] = { 0, 0, 0 };
1613 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1614 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1616 #ifdef WORDS_BIGENDIAN
1617 vnc_write_u8(vs
, 1); /* big-endian-flag */
1619 vnc_write_u8(vs
, 0); /* big-endian-flag */
1621 vnc_write_u8(vs
, 1); /* true-color-flag */
1622 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1623 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1624 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1625 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1626 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1627 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1628 if (vs
->ds
->surface
->pf
.bits_per_pixel
== 32)
1629 vs
->send_hextile_tile
= send_hextile_tile_32
;
1630 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 16)
1631 vs
->send_hextile_tile
= send_hextile_tile_16
;
1632 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 8)
1633 vs
->send_hextile_tile
= send_hextile_tile_8
;
1634 vs
->clientds
= *(vs
->ds
->surface
);
1635 vs
->clientds
.flags
|= ~QEMU_ALLOCATED_FLAG
;
1636 vs
->write_pixels
= vnc_write_pixels_copy
;
1638 vnc_write(vs
, pad
, 3); /* padding */
1641 static void vnc_dpy_setdata(DisplayState
*ds
)
1643 /* We don't have to do anything */
1646 static void vnc_colordepth(VncState
*vs
)
1648 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1649 /* Sending a WMVi message to notify the client*/
1650 vnc_write_u8(vs
, 0); /* msg id */
1651 vnc_write_u8(vs
, 0);
1652 vnc_write_u16(vs
, 1); /* number of rects */
1653 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1654 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1655 pixel_format_message(vs
);
1658 set_pixel_conversion(vs
);
1662 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1672 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1673 read_u8(data
, 6), read_u8(data
, 7),
1674 read_u16(data
, 8), read_u16(data
, 10),
1675 read_u16(data
, 12), read_u8(data
, 14),
1676 read_u8(data
, 15), read_u8(data
, 16));
1683 limit
= read_u16(data
, 2);
1685 return 4 + (limit
* 4);
1687 limit
= read_u16(data
, 2);
1689 for (i
= 0; i
< limit
; i
++) {
1690 int32_t val
= read_s32(data
, 4 + (i
* 4));
1691 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1694 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1700 framebuffer_update_request(vs
,
1701 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1702 read_u16(data
, 6), read_u16(data
, 8));
1708 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1714 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1721 uint32_t dlen
= read_u32(data
, 4);
1726 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1732 switch (read_u8(data
, 1)) {
1737 ext_key_event(vs
, read_u16(data
, 2),
1738 read_u32(data
, 4), read_u32(data
, 8));
1744 switch (read_u16 (data
, 2)) {
1754 switch (read_u8(data
, 4)) {
1755 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1756 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1757 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1758 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1759 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1760 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1762 printf("Invalid audio format %d\n", read_u8(data
, 4));
1763 vnc_client_error(vs
);
1766 vs
->as
.nchannels
= read_u8(data
, 5);
1767 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
1768 printf("Invalid audio channel coount %d\n",
1770 vnc_client_error(vs
);
1773 vs
->as
.freq
= read_u32(data
, 6);
1776 printf ("Invalid audio message %d\n", read_u8(data
, 4));
1777 vnc_client_error(vs
);
1783 printf("Msg: %d\n", read_u16(data
, 0));
1784 vnc_client_error(vs
);
1789 printf("Msg: %d\n", data
[0]);
1790 vnc_client_error(vs
);
1794 vnc_read_when(vs
, protocol_client_msg
, 1);
1798 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
1803 vnc_write_u16(vs
, ds_get_width(vs
->ds
));
1804 vnc_write_u16(vs
, ds_get_height(vs
->ds
));
1806 pixel_format_message(vs
);
1809 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
1811 size
= snprintf(buf
, sizeof(buf
), "QEMU");
1813 vnc_write_u32(vs
, size
);
1814 vnc_write(vs
, buf
, size
);
1817 vnc_read_when(vs
, protocol_client_msg
, 1);
1822 void start_client_init(VncState
*vs
)
1824 vnc_read_when(vs
, protocol_client_init
, 1);
1827 static void make_challenge(VncState
*vs
)
1831 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1833 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1834 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1837 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
1839 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
1841 unsigned char key
[8];
1843 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
1844 VNC_DEBUG("No password configured on server");
1845 vnc_write_u32(vs
, 1); /* Reject auth */
1846 if (vs
->minor
>= 8) {
1847 static const char err
[] = "Authentication failed";
1848 vnc_write_u32(vs
, sizeof(err
));
1849 vnc_write(vs
, err
, sizeof(err
));
1852 vnc_client_error(vs
);
1856 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1858 /* Calculate the expected challenge response */
1859 pwlen
= strlen(vs
->vd
->password
);
1860 for (i
=0; i
<sizeof(key
); i
++)
1861 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
1863 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1864 des(response
+j
, response
+j
);
1866 /* Compare expected vs actual challenge response */
1867 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1868 VNC_DEBUG("Client challenge reponse did not match\n");
1869 vnc_write_u32(vs
, 1); /* Reject auth */
1870 if (vs
->minor
>= 8) {
1871 static const char err
[] = "Authentication failed";
1872 vnc_write_u32(vs
, sizeof(err
));
1873 vnc_write(vs
, err
, sizeof(err
));
1876 vnc_client_error(vs
);
1878 VNC_DEBUG("Accepting VNC challenge response\n");
1879 vnc_write_u32(vs
, 0); /* Accept auth */
1882 start_client_init(vs
);
1887 void start_auth_vnc(VncState
*vs
)
1890 /* Send client a 'random' challenge */
1891 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1894 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1898 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
1900 /* We only advertise 1 auth scheme at a time, so client
1901 * must pick the one we sent. Verify this */
1902 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
1903 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
1904 vnc_write_u32(vs
, 1);
1905 if (vs
->minor
>= 8) {
1906 static const char err
[] = "Authentication failed";
1907 vnc_write_u32(vs
, sizeof(err
));
1908 vnc_write(vs
, err
, sizeof(err
));
1910 vnc_client_error(vs
);
1911 } else { /* Accept requested auth */
1912 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
1913 switch (vs
->vd
->auth
) {
1915 VNC_DEBUG("Accept auth none\n");
1916 if (vs
->minor
>= 8) {
1917 vnc_write_u32(vs
, 0); /* Accept auth completion */
1920 start_client_init(vs
);
1924 VNC_DEBUG("Start VNC auth\n");
1928 #ifdef CONFIG_VNC_TLS
1929 case VNC_AUTH_VENCRYPT
:
1930 VNC_DEBUG("Accept VeNCrypt auth\n");;
1931 start_auth_vencrypt(vs
);
1933 #endif /* CONFIG_VNC_TLS */
1935 #ifdef CONFIG_VNC_SASL
1937 VNC_DEBUG("Accept SASL auth\n");
1938 start_auth_sasl(vs
);
1940 #endif /* CONFIG_VNC_SASL */
1942 default: /* Should not be possible, but just in case */
1943 VNC_DEBUG("Reject auth %d server code bug\n", vs
->vd
->auth
);
1944 vnc_write_u8(vs
, 1);
1945 if (vs
->minor
>= 8) {
1946 static const char err
[] = "Authentication failed";
1947 vnc_write_u32(vs
, sizeof(err
));
1948 vnc_write(vs
, err
, sizeof(err
));
1950 vnc_client_error(vs
);
1956 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
1960 memcpy(local
, version
, 12);
1963 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
1964 VNC_DEBUG("Malformed protocol version %s\n", local
);
1965 vnc_client_error(vs
);
1968 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
1969 if (vs
->major
!= 3 ||
1975 VNC_DEBUG("Unsupported client version\n");
1976 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
1978 vnc_client_error(vs
);
1981 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
1982 * as equivalent to v3.3 by servers
1984 if (vs
->minor
== 4 || vs
->minor
== 5)
1987 if (vs
->minor
== 3) {
1988 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
1989 VNC_DEBUG("Tell client auth none\n");
1990 vnc_write_u32(vs
, vs
->vd
->auth
);
1992 start_client_init(vs
);
1993 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
1994 VNC_DEBUG("Tell client VNC auth\n");
1995 vnc_write_u32(vs
, vs
->vd
->auth
);
1999 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
2000 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2002 vnc_client_error(vs
);
2005 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
2006 vnc_write_u8(vs
, 1); /* num auth */
2007 vnc_write_u8(vs
, vs
->vd
->auth
);
2008 vnc_read_when(vs
, protocol_client_auth
, 1);
2015 static void vnc_connect(VncDisplay
*vd
, int csock
)
2017 VncState
*vs
= qemu_mallocz(sizeof(VncState
));
2020 VNC_DEBUG("New client on socket %d\n", csock
);
2022 socket_set_nonblock(vs
->csock
);
2023 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2027 vs
->timer
= qemu_new_timer(rt_clock
, vnc_update_client
, vs
);
2031 vs
->as
.freq
= 44100;
2032 vs
->as
.nchannels
= 2;
2033 vs
->as
.fmt
= AUD_FMT_S16
;
2034 vs
->as
.endianness
= 0;
2037 vnc_write(vs
, "RFB 003.008\n", 12);
2039 vnc_read_when(vs
, protocol_version
, 12);
2040 memset(vs
->old_data
, 0, ds_get_linesize(vs
->ds
) * ds_get_height(vs
->ds
));
2041 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
2044 vs
->next
= vd
->clients
;
2047 vnc_update_client(vs
);
2048 /* vs might be free()ed here */
2051 static void vnc_listen_read(void *opaque
)
2053 VncDisplay
*vs
= opaque
;
2054 struct sockaddr_in addr
;
2055 socklen_t addrlen
= sizeof(addr
);
2060 int csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2062 vnc_connect(vs
, csock
);
2066 void vnc_display_init(DisplayState
*ds
)
2068 VncDisplay
*vs
= qemu_mallocz(sizeof(*vs
));
2070 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2080 if (keyboard_layout
)
2081 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2083 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2085 if (!vs
->kbd_layout
)
2088 dcl
->dpy_copy
= vnc_dpy_copy
;
2089 dcl
->dpy_update
= vnc_dpy_update
;
2090 dcl
->dpy_resize
= vnc_dpy_resize
;
2091 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2092 register_displaychangelistener(ds
, dcl
);
2096 void vnc_display_close(DisplayState
*ds
)
2098 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2103 qemu_free(vs
->display
);
2106 if (vs
->lsock
!= -1) {
2107 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2111 vs
->auth
= VNC_AUTH_INVALID
;
2112 #ifdef CONFIG_VNC_TLS
2113 vs
->subauth
= VNC_AUTH_INVALID
;
2114 vs
->tls
.x509verify
= 0;
2118 int vnc_display_password(DisplayState
*ds
, const char *password
)
2120 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2123 qemu_free(vs
->password
);
2124 vs
->password
= NULL
;
2126 if (password
&& password
[0]) {
2127 if (!(vs
->password
= qemu_strdup(password
)))
2134 int vnc_display_open(DisplayState
*ds
, const char *display
)
2136 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2137 const char *options
;
2141 #ifdef CONFIG_VNC_TLS
2142 int tls
= 0, x509
= 0;
2144 #ifdef CONFIG_VNC_SASL
2152 vnc_display_close(ds
);
2153 if (strcmp(display
, "none") == 0)
2156 if (!(vs
->display
= strdup(display
)))
2160 while ((options
= strchr(options
, ','))) {
2162 if (strncmp(options
, "password", 8) == 0) {
2163 password
= 1; /* Require password auth */
2164 } else if (strncmp(options
, "reverse", 7) == 0) {
2166 } else if (strncmp(options
, "to=", 3) == 0) {
2167 to_port
= atoi(options
+3) + 5900;
2168 #ifdef CONFIG_VNC_SASL
2169 } else if (strncmp(options
, "sasl", 4) == 0) {
2170 sasl
= 1; /* Require SASL auth */
2172 #ifdef CONFIG_VNC_TLS
2173 } else if (strncmp(options
, "tls", 3) == 0) {
2174 tls
= 1; /* Require TLS */
2175 } else if (strncmp(options
, "x509", 4) == 0) {
2177 x509
= 1; /* Require x509 certificates */
2178 if (strncmp(options
, "x509verify", 10) == 0)
2179 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2181 /* Now check for 'x509=/some/path' postfix
2182 * and use that to setup x509 certificate/key paths */
2183 start
= strchr(options
, '=');
2184 end
= strchr(options
, ',');
2185 if (start
&& (!end
|| (start
< end
))) {
2186 int len
= end
? end
-(start
+1) : strlen(start
+1);
2187 char *path
= qemu_strndup(start
+ 1, len
);
2189 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2190 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2191 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2193 qemu_free(vs
->display
);
2199 fprintf(stderr
, "No certificate path provided\n");
2200 qemu_free(vs
->display
);
2205 } else if (strncmp(options
, "acl", 3) == 0) {
2210 #ifdef CONFIG_VNC_TLS
2211 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2212 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2213 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2218 #ifdef CONFIG_VNC_SASL
2220 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2221 fprintf(stderr
, "Failed to create username ACL\n");
2228 * Combinations we support here:
2230 * - no-auth (clear text, no auth)
2231 * - password (clear text, weak auth)
2232 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2233 * - tls (encrypt, weak anonymous creds, no auth)
2234 * - tls + password (encrypt, weak anonymous creds, weak auth)
2235 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2236 * - tls + x509 (encrypt, good x509 creds, no auth)
2237 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2238 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2240 * NB1. TLS is a stackable auth scheme.
2241 * NB2. the x509 schemes have option to validate a client cert dname
2244 #ifdef CONFIG_VNC_TLS
2246 vs
->auth
= VNC_AUTH_VENCRYPT
;
2248 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2249 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2251 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2252 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2255 #endif /* CONFIG_VNC_TLS */
2256 VNC_DEBUG("Initializing VNC server with password auth\n");
2257 vs
->auth
= VNC_AUTH_VNC
;
2258 #ifdef CONFIG_VNC_TLS
2259 vs
->subauth
= VNC_AUTH_INVALID
;
2261 #endif /* CONFIG_VNC_TLS */
2262 #ifdef CONFIG_VNC_SASL
2264 #ifdef CONFIG_VNC_TLS
2266 vs
->auth
= VNC_AUTH_VENCRYPT
;
2268 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2269 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
2271 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2272 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
2275 #endif /* CONFIG_VNC_TLS */
2276 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2277 vs
->auth
= VNC_AUTH_SASL
;
2278 #ifdef CONFIG_VNC_TLS
2279 vs
->subauth
= VNC_AUTH_INVALID
;
2281 #endif /* CONFIG_VNC_TLS */
2282 #endif /* CONFIG_VNC_SASL */
2284 #ifdef CONFIG_VNC_TLS
2286 vs
->auth
= VNC_AUTH_VENCRYPT
;
2288 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2289 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2291 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2292 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2296 VNC_DEBUG("Initializing VNC server with no auth\n");
2297 vs
->auth
= VNC_AUTH_NONE
;
2298 #ifdef CONFIG_VNC_TLS
2299 vs
->subauth
= VNC_AUTH_INVALID
;
2304 #ifdef CONFIG_VNC_SASL
2305 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
2306 fprintf(stderr
, "Failed to initialize SASL auth %s",
2307 sasl_errstring(saslErr
, NULL
, NULL
));
2315 /* connect to viewer */
2316 if (strncmp(display
, "unix:", 5) == 0)
2317 vs
->lsock
= unix_connect(display
+5);
2319 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2320 if (-1 == vs
->lsock
) {
2325 int csock
= vs
->lsock
;
2327 vnc_connect(vs
, csock
);
2332 /* listen for connects */
2334 dpy
= qemu_malloc(256);
2335 if (strncmp(display
, "unix:", 5) == 0) {
2336 pstrcpy(dpy
, 256, "unix:");
2337 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2339 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2341 if (-1 == vs
->lsock
) {
2349 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);