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
];
58 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
61 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
62 VNC_DEBUG("Cannot resolve address %d: %s\n",
63 err
, gai_strerror(err
));
67 /* Enough for the existing format + the 2 vars we're
69 addrlen
= strlen(format
) + strlen(host
) + strlen(serv
);
70 addr
= qemu_malloc(addrlen
+ 1);
71 snprintf(addr
, addrlen
, format
, host
, serv
);
78 char *vnc_socket_local_addr(const char *format
, int fd
) {
79 struct sockaddr_storage sa
;
83 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
86 return addr_to_string(format
, &sa
, salen
);
89 char *vnc_socket_remote_addr(const char *format
, int fd
) {
90 struct sockaddr_storage sa
;
94 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
97 return addr_to_string(format
, &sa
, salen
);
100 static const char *vnc_auth_name(VncDisplay
*vd
) {
102 case VNC_AUTH_INVALID
:
118 case VNC_AUTH_VENCRYPT
:
119 #ifdef CONFIG_VNC_TLS
120 switch (vd
->subauth
) {
121 case VNC_AUTH_VENCRYPT_PLAIN
:
122 return "vencrypt+plain";
123 case VNC_AUTH_VENCRYPT_TLSNONE
:
124 return "vencrypt+tls+none";
125 case VNC_AUTH_VENCRYPT_TLSVNC
:
126 return "vencrypt+tls+vnc";
127 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
128 return "vencrypt+tls+plain";
129 case VNC_AUTH_VENCRYPT_X509NONE
:
130 return "vencrypt+x509+none";
131 case VNC_AUTH_VENCRYPT_X509VNC
:
132 return "vencrypt+x509+vnc";
133 case VNC_AUTH_VENCRYPT_X509PLAIN
:
134 return "vencrypt+x509+plain";
135 case VNC_AUTH_VENCRYPT_TLSSASL
:
136 return "vencrypt+tls+sasl";
137 case VNC_AUTH_VENCRYPT_X509SASL
:
138 return "vencrypt+x509+sasl";
151 static void do_info_vnc_client(Monitor
*mon
, VncState
*client
)
154 vnc_socket_remote_addr(" address: %s:%s\n",
159 monitor_printf(mon
, "Client:\n");
160 monitor_printf(mon
, "%s", clientAddr
);
163 #ifdef CONFIG_VNC_TLS
164 if (client
->tls
.session
&&
166 monitor_printf(mon
, " x509 dname: %s\n", client
->tls
.dname
);
168 monitor_printf(mon
, " x509 dname: none\n");
170 #ifdef CONFIG_VNC_SASL
171 if (client
->sasl
.conn
&&
172 client
->sasl
.username
)
173 monitor_printf(mon
, " username: %s\n", client
->sasl
.username
);
175 monitor_printf(mon
, " username: none\n");
179 void do_info_vnc(Monitor
*mon
)
181 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
182 monitor_printf(mon
, "Server: disabled\n");
184 char *serverAddr
= vnc_socket_local_addr(" address: %s:%s\n",
190 monitor_printf(mon
, "Server:\n");
191 monitor_printf(mon
, "%s", serverAddr
);
193 monitor_printf(mon
, " auth: %s\n", vnc_auth_name(vnc_display
));
195 if (vnc_display
->clients
) {
196 VncState
*client
= vnc_display
->clients
;
198 do_info_vnc_client(mon
, client
);
199 client
= client
->next
;
202 monitor_printf(mon
, "Client: none\n");
207 static inline uint32_t vnc_has_feature(VncState
*vs
, int feature
) {
208 return (vs
->features
& (1 << feature
));
212 1) Get the queue working for IO.
213 2) there is some weirdness when using the -S option (the screen is grey
214 and not totally invalidated
215 3) resolutions > 1024
218 static void vnc_update_client(VncState
*vs
, int has_dirty
);
219 static void vnc_disconnect_start(VncState
*vs
);
220 static void vnc_disconnect_finish(VncState
*vs
);
221 static void vnc_init_timer(VncDisplay
*vd
);
222 static void vnc_remove_timer(VncDisplay
*vd
);
224 static void vnc_colordepth(VncState
*vs
);
225 static void framebuffer_update_request(VncState
*vs
, int incremental
,
226 int x_position
, int y_position
,
228 static void vnc_refresh(void *opaque
);
229 static int vnc_refresh_server_surface(VncDisplay
*vd
);
231 static inline void vnc_set_bit(uint32_t *d
, int k
)
233 d
[k
>> 5] |= 1 << (k
& 0x1f);
236 static inline void vnc_clear_bit(uint32_t *d
, int k
)
238 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
241 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
251 d
[j
++] = (1 << n
) - 1;
256 static inline int vnc_get_bit(const uint32_t *d
, int k
)
258 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
261 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
265 for(i
= 0; i
< nb_words
; i
++) {
266 if ((d1
[i
] & d2
[i
]) != 0)
272 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
275 VncDisplay
*vd
= ds
->opaque
;
276 struct VncSurface
*s
= &vd
->guest
;
280 /* round x down to ensure the loop only spans one 16-pixel block per,
281 iteration. otherwise, if (x % 16) != 0, the last iteration may span
282 two 16-pixel blocks but we only mark the first as dirty
287 x
= MIN(x
, s
->ds
->width
);
288 y
= MIN(y
, s
->ds
->height
);
289 w
= MIN(x
+ w
, s
->ds
->width
) - x
;
290 h
= MIN(h
, s
->ds
->height
);
293 for (i
= 0; i
< w
; i
+= 16)
294 vnc_set_bit(s
->dirty
[y
], (x
+ i
) / 16);
297 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
300 vnc_write_u16(vs
, x
);
301 vnc_write_u16(vs
, y
);
302 vnc_write_u16(vs
, w
);
303 vnc_write_u16(vs
, h
);
305 vnc_write_s32(vs
, encoding
);
308 void buffer_reserve(Buffer
*buffer
, size_t len
)
310 if ((buffer
->capacity
- buffer
->offset
) < len
) {
311 buffer
->capacity
+= (len
+ 1024);
312 buffer
->buffer
= qemu_realloc(buffer
->buffer
, buffer
->capacity
);
313 if (buffer
->buffer
== NULL
) {
314 fprintf(stderr
, "vnc: out of memory\n");
320 int buffer_empty(Buffer
*buffer
)
322 return buffer
->offset
== 0;
325 uint8_t *buffer_end(Buffer
*buffer
)
327 return buffer
->buffer
+ buffer
->offset
;
330 void buffer_reset(Buffer
*buffer
)
335 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
337 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
338 buffer
->offset
+= len
;
341 static void vnc_dpy_resize(DisplayState
*ds
)
344 VncDisplay
*vd
= ds
->opaque
;
345 VncState
*vs
= vd
->clients
;
349 vd
->server
= qemu_mallocz(sizeof(*vd
->server
));
350 if (vd
->server
->data
)
351 qemu_free(vd
->server
->data
);
352 *(vd
->server
) = *(ds
->surface
);
353 vd
->server
->data
= qemu_mallocz(vd
->server
->linesize
*
358 vd
->guest
.ds
= qemu_mallocz(sizeof(*vd
->guest
.ds
));
359 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
360 console_color_init(ds
);
361 size_changed
= ds_get_width(ds
) != vd
->guest
.ds
->width
||
362 ds_get_height(ds
) != vd
->guest
.ds
->height
;
363 *(vd
->guest
.ds
) = *(ds
->surface
);
364 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
369 if (vs
->csock
!= -1 && vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
370 vnc_write_u8(vs
, 0); /* msg id */
372 vnc_write_u16(vs
, 1); /* number of rects */
373 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(ds
), ds_get_height(ds
),
374 VNC_ENCODING_DESKTOPRESIZE
);
378 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
384 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
386 vnc_write(vs
, pixels
, size
);
389 /* slowest but generic code. */
390 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
393 VncDisplay
*vd
= vs
->vd
;
395 r
= ((((v
& vd
->server
->pf
.rmask
) >> vd
->server
->pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
396 vd
->server
->pf
.rbits
);
397 g
= ((((v
& vd
->server
->pf
.gmask
) >> vd
->server
->pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
398 vd
->server
->pf
.gbits
);
399 b
= ((((v
& vd
->server
->pf
.bmask
) >> vd
->server
->pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
400 vd
->server
->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
)
437 VncDisplay
*vd
= vs
->vd
;
439 if (vd
->server
->pf
.bytes_per_pixel
== 4) {
440 uint32_t *pixels
= pixels1
;
443 for(i
= 0; i
< n
; i
++) {
444 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
445 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
447 } else if (vd
->server
->pf
.bytes_per_pixel
== 2) {
448 uint16_t *pixels
= pixels1
;
451 for(i
= 0; i
< n
; i
++) {
452 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
453 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
455 } else if (vd
->server
->pf
.bytes_per_pixel
== 1) {
456 uint8_t *pixels
= pixels1
;
459 for(i
= 0; i
< n
; i
++) {
460 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
461 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
464 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
468 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
472 VncDisplay
*vd
= vs
->vd
;
474 row
= vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
475 for (i
= 0; i
< h
; i
++) {
476 vs
->write_pixels(vs
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
477 row
+= ds_get_linesize(vs
->ds
);
481 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
483 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
484 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
488 #include "vnchextile.h"
492 #include "vnchextile.h"
496 #include "vnchextile.h"
501 #include "vnchextile.h"
507 #include "vnchextile.h"
513 #include "vnchextile.h"
517 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
521 uint8_t *last_fg
, *last_bg
;
522 VncDisplay
*vd
= vs
->vd
;
524 last_fg
= (uint8_t *) qemu_malloc(vd
->server
->pf
.bytes_per_pixel
);
525 last_bg
= (uint8_t *) qemu_malloc(vd
->server
->pf
.bytes_per_pixel
);
527 for (j
= y
; j
< (y
+ h
); j
+= 16) {
528 for (i
= x
; i
< (x
+ w
); i
+= 16) {
529 vs
->send_hextile_tile(vs
, i
, j
,
530 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
531 last_bg
, last_fg
, &has_bg
, &has_fg
);
539 static void vnc_zlib_init(VncState
*vs
)
542 for (i
=0; i
<(sizeof(vs
->zlib_stream
) / sizeof(z_stream
)); i
++)
543 vs
->zlib_stream
[i
].opaque
= NULL
;
546 static void vnc_zlib_start(VncState
*vs
)
548 buffer_reset(&vs
->zlib
);
550 // make the output buffer be the zlib buffer, so we can compress it later
551 vs
->zlib_tmp
= vs
->output
;
552 vs
->output
= vs
->zlib
;
555 static int vnc_zlib_stop(VncState
*vs
, int stream_id
)
557 z_streamp zstream
= &vs
->zlib_stream
[stream_id
];
560 // switch back to normal output/zlib buffers
561 vs
->zlib
= vs
->output
;
562 vs
->output
= vs
->zlib_tmp
;
564 // compress the zlib buffer
566 // initialize the stream
567 // XXX need one stream per session
568 if (zstream
->opaque
!= vs
) {
571 VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id
);
572 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
573 zstream
->zalloc
= Z_NULL
;
574 zstream
->zfree
= Z_NULL
;
576 err
= deflateInit2(zstream
, vs
->tight_compression
, Z_DEFLATED
, MAX_WBITS
,
577 MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
580 fprintf(stderr
, "VNC: error initializing zlib\n");
584 zstream
->opaque
= vs
;
587 // XXX what to do if tight_compression changed in between?
589 // reserve memory in output buffer
590 buffer_reserve(&vs
->output
, vs
->zlib
.offset
+ 64);
593 zstream
->next_in
= vs
->zlib
.buffer
;
594 zstream
->avail_in
= vs
->zlib
.offset
;
595 zstream
->next_out
= vs
->output
.buffer
+ vs
->output
.offset
;
596 zstream
->avail_out
= vs
->output
.capacity
- vs
->output
.offset
;
597 zstream
->data_type
= Z_BINARY
;
598 previous_out
= zstream
->total_out
;
601 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
602 fprintf(stderr
, "VNC: error during zlib compression\n");
606 vs
->output
.offset
= vs
->output
.capacity
- zstream
->avail_out
;
607 return zstream
->total_out
- previous_out
;
610 static void send_framebuffer_update_zlib(VncState
*vs
, int x
, int y
, int w
, int h
)
612 int old_offset
, new_offset
, bytes_written
;
614 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_ZLIB
);
616 // remember where we put in the follow-up size
617 old_offset
= vs
->output
.offset
;
618 vnc_write_s32(vs
, 0);
620 // compress the stream
622 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
623 bytes_written
= vnc_zlib_stop(vs
, 0);
625 if (bytes_written
== -1)
629 new_offset
= vs
->output
.offset
;
630 vs
->output
.offset
= old_offset
;
631 vnc_write_u32(vs
, bytes_written
);
632 vs
->output
.offset
= new_offset
;
635 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
637 switch(vs
->vnc_encoding
) {
638 case VNC_ENCODING_ZLIB
:
639 send_framebuffer_update_zlib(vs
, x
, y
, w
, h
);
641 case VNC_ENCODING_HEXTILE
:
642 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
643 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
646 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
647 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
652 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
654 /* send bitblit op to the vnc client */
655 vnc_write_u8(vs
, 0); /* msg id */
657 vnc_write_u16(vs
, 1); /* number of rects */
658 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
659 vnc_write_u16(vs
, src_x
);
660 vnc_write_u16(vs
, src_y
);
664 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
666 VncDisplay
*vd
= ds
->opaque
;
670 int i
,x
,y
,pitch
,depth
,inc
,w_lim
,s
;
673 vnc_refresh_server_surface(vd
);
674 for (vs
= vd
->clients
; vs
!= NULL
; vs
= vn
) {
676 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
677 vs
->force_update
= 1;
678 vnc_update_client(vs
, 1);
679 /* vs might be free()ed here */
683 /* do bitblit op on the local surface too */
684 pitch
= ds_get_linesize(vd
->ds
);
685 depth
= ds_get_bytes_per_pixel(vd
->ds
);
686 src_row
= vd
->server
->data
+ pitch
* src_y
+ depth
* src_x
;
687 dst_row
= vd
->server
->data
+ pitch
* dst_y
+ depth
* dst_x
;
692 src_row
+= pitch
* (h
-1);
693 dst_row
+= pitch
* (h
-1);
698 w_lim
= w
- (16 - (dst_x
% 16));
702 w_lim
= w
- (w_lim
% 16);
703 for (i
= 0; i
< h
; i
++) {
704 for (x
= 0; x
<= w_lim
;
705 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
707 if ((s
= w
- w_lim
) == 0)
710 s
= (16 - (dst_x
% 16));
715 cmp_bytes
= s
* depth
;
716 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
718 memmove(dst_row
, src_row
, cmp_bytes
);
721 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
))
722 vnc_set_bit(vs
->dirty
[y
], ((x
+ dst_x
) / 16));
726 src_row
+= pitch
- w
* depth
;
727 dst_row
+= pitch
- w
* depth
;
731 for (vs
= vd
->clients
; vs
!= NULL
; vs
= vs
->next
) {
732 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
))
733 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
737 static int find_and_clear_dirty_height(struct VncState
*vs
,
738 int y
, int last_x
, int x
)
741 VncDisplay
*vd
= vs
->vd
;
743 for (h
= 1; h
< (vd
->server
->height
- y
); h
++) {
745 if (!vnc_get_bit(vs
->dirty
[y
+ h
], last_x
))
747 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
748 vnc_clear_bit(vs
->dirty
[y
+ h
], tmp_x
);
754 static void vnc_update_client(VncState
*vs
, int has_dirty
)
756 if (vs
->need_update
&& vs
->csock
!= -1) {
757 VncDisplay
*vd
= vs
->vd
;
762 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
763 /* kernel send buffers are full -> drop frames to throttle */
766 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
770 * Send screen updates to the vnc client using the server
771 * surface and server dirty map. guest surface updates
772 * happening in parallel don't disturb us, the next pass will
773 * send them to the client.
776 vnc_write_u8(vs
, 0); /* msg id */
778 saved_offset
= vs
->output
.offset
;
779 vnc_write_u16(vs
, 0);
781 for (y
= 0; y
< vd
->server
->height
; y
++) {
784 for (x
= 0; x
< vd
->server
->width
/ 16; x
++) {
785 if (vnc_get_bit(vs
->dirty
[y
], x
)) {
789 vnc_clear_bit(vs
->dirty
[y
], x
);
792 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
793 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
800 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
);
801 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
805 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
806 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
808 vs
->force_update
= 0;
813 vnc_disconnect_finish(vs
);
817 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
819 VncState
*vs
= opaque
;
822 case AUD_CNOTIFY_DISABLE
:
823 vnc_write_u8(vs
, 255);
825 vnc_write_u16(vs
, 0);
829 case AUD_CNOTIFY_ENABLE
:
830 vnc_write_u8(vs
, 255);
832 vnc_write_u16(vs
, 1);
838 static void audio_capture_destroy(void *opaque
)
842 static void audio_capture(void *opaque
, void *buf
, int size
)
844 VncState
*vs
= opaque
;
846 vnc_write_u8(vs
, 255);
848 vnc_write_u16(vs
, 2);
849 vnc_write_u32(vs
, size
);
850 vnc_write(vs
, buf
, size
);
854 static void audio_add(VncState
*vs
)
856 Monitor
*mon
= cur_mon
;
857 struct audio_capture_ops ops
;
860 monitor_printf(mon
, "audio already running\n");
864 ops
.notify
= audio_capture_notify
;
865 ops
.destroy
= audio_capture_destroy
;
866 ops
.capture
= audio_capture
;
868 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
869 if (!vs
->audio_cap
) {
870 monitor_printf(mon
, "Failed to add audio capture\n");
874 static void audio_del(VncState
*vs
)
877 AUD_del_capture(vs
->audio_cap
, vs
);
878 vs
->audio_cap
= NULL
;
882 static void vnc_disconnect_start(VncState
*vs
)
886 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
887 closesocket(vs
->csock
);
891 static void vnc_disconnect_finish(VncState
*vs
)
893 if (vs
->input
.buffer
) qemu_free(vs
->input
.buffer
);
894 if (vs
->output
.buffer
) qemu_free(vs
->output
.buffer
);
895 #ifdef CONFIG_VNC_TLS
896 vnc_tls_client_cleanup(vs
);
897 #endif /* CONFIG_VNC_TLS */
898 #ifdef CONFIG_VNC_SASL
899 vnc_sasl_client_cleanup(vs
);
900 #endif /* CONFIG_VNC_SASL */
903 VncState
*p
, *parent
= NULL
;
904 for (p
= vs
->vd
->clients
; p
!= NULL
; p
= p
->next
) {
907 parent
->next
= p
->next
;
909 vs
->vd
->clients
= p
->next
;
914 if (!vs
->vd
->clients
)
918 vnc_remove_timer(vs
->vd
);
921 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
923 if (ret
== 0 || ret
== -1) {
925 switch (last_errno
) {
937 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
938 ret
, ret
< 0 ? last_errno
: 0);
939 vnc_disconnect_start(vs
);
947 void vnc_client_error(VncState
*vs
)
949 VNC_DEBUG("Closing down client sock: protocol error\n");
950 vnc_disconnect_start(vs
);
955 * Called to write a chunk of data to the client socket. The data may
956 * be the raw data, or may have already been encoded by SASL.
957 * The data will be written either straight onto the socket, or
958 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
960 * NB, it is theoretically possible to have 2 layers of encryption,
961 * both SASL, and this TLS layer. It is highly unlikely in practice
962 * though, since SASL encryption will typically be a no-op if TLS
965 * Returns the number of bytes written, which may be less than
966 * the requested 'datalen' if the socket would block. Returns
967 * -1 on error, and disconnects the client socket.
969 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
972 #ifdef CONFIG_VNC_TLS
973 if (vs
->tls
.session
) {
974 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
976 if (ret
== GNUTLS_E_AGAIN
)
983 #endif /* CONFIG_VNC_TLS */
984 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
985 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
986 return vnc_client_io_error(vs
, ret
, socket_error());
991 * Called to write buffered data to the client socket, when not
992 * using any SASL SSF encryption layers. Will write as much data
993 * as possible without blocking. If all buffered data is written,
994 * will switch the FD poll() handler back to read monitoring.
996 * Returns the number of bytes written, which may be less than
997 * the buffered output data if the socket would block. Returns
998 * -1 on error, and disconnects the client socket.
1000 static long vnc_client_write_plain(VncState
*vs
)
1004 #ifdef CONFIG_VNC_SASL
1005 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1006 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1007 vs
->sasl
.waitWriteSSF
);
1009 if (vs
->sasl
.conn
&&
1011 vs
->sasl
.waitWriteSSF
) {
1012 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1014 vs
->sasl
.waitWriteSSF
-= ret
;
1016 #endif /* CONFIG_VNC_SASL */
1017 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1021 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1022 vs
->output
.offset
-= ret
;
1024 if (vs
->output
.offset
== 0) {
1025 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1033 * First function called whenever there is data to be written to
1034 * the client socket. Will delegate actual work according to whether
1035 * SASL SSF layers are enabled (thus requiring encryption calls)
1037 void vnc_client_write(void *opaque
)
1040 VncState
*vs
= opaque
;
1042 #ifdef CONFIG_VNC_SASL
1043 if (vs
->sasl
.conn
&&
1045 !vs
->sasl
.waitWriteSSF
)
1046 ret
= vnc_client_write_sasl(vs
);
1048 #endif /* CONFIG_VNC_SASL */
1049 ret
= vnc_client_write_plain(vs
);
1052 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1054 vs
->read_handler
= func
;
1055 vs
->read_handler_expect
= expecting
;
1060 * Called to read a chunk of data from the client socket. The data may
1061 * be the raw data, or may need to be further decoded by SASL.
1062 * The data will be read either straight from to the socket, or
1063 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1065 * NB, it is theoretically possible to have 2 layers of encryption,
1066 * both SASL, and this TLS layer. It is highly unlikely in practice
1067 * though, since SASL encryption will typically be a no-op if TLS
1070 * Returns the number of bytes read, which may be less than
1071 * the requested 'datalen' if the socket would block. Returns
1072 * -1 on error, and disconnects the client socket.
1074 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1077 #ifdef CONFIG_VNC_TLS
1078 if (vs
->tls
.session
) {
1079 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1081 if (ret
== GNUTLS_E_AGAIN
)
1088 #endif /* CONFIG_VNC_TLS */
1089 ret
= recv(vs
->csock
, (void *)data
, datalen
, 0);
1090 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1091 return vnc_client_io_error(vs
, ret
, socket_error());
1096 * Called to read data from the client socket to the input buffer,
1097 * when not using any SASL SSF encryption layers. Will read as much
1098 * data as possible without blocking.
1100 * Returns the number of bytes read. Returns -1 on error, and
1101 * disconnects the client socket.
1103 static long vnc_client_read_plain(VncState
*vs
)
1106 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1107 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1108 buffer_reserve(&vs
->input
, 4096);
1109 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1112 vs
->input
.offset
+= ret
;
1118 * First function called whenever there is more data to be read from
1119 * the client socket. Will delegate actual work according to whether
1120 * SASL SSF layers are enabled (thus requiring decryption calls)
1122 void vnc_client_read(void *opaque
)
1124 VncState
*vs
= opaque
;
1127 #ifdef CONFIG_VNC_SASL
1128 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1129 ret
= vnc_client_read_sasl(vs
);
1131 #endif /* CONFIG_VNC_SASL */
1132 ret
= vnc_client_read_plain(vs
);
1134 if (vs
->csock
== -1)
1135 vnc_disconnect_finish(vs
);
1139 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1140 size_t len
= vs
->read_handler_expect
;
1143 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1144 if (vs
->csock
== -1) {
1145 vnc_disconnect_finish(vs
);
1150 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1151 vs
->input
.offset
-= len
;
1153 vs
->read_handler_expect
= ret
;
1158 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1160 buffer_reserve(&vs
->output
, len
);
1162 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1163 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1166 buffer_append(&vs
->output
, data
, len
);
1169 void vnc_write_s32(VncState
*vs
, int32_t value
)
1171 vnc_write_u32(vs
, *(uint32_t *)&value
);
1174 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1178 buf
[0] = (value
>> 24) & 0xFF;
1179 buf
[1] = (value
>> 16) & 0xFF;
1180 buf
[2] = (value
>> 8) & 0xFF;
1181 buf
[3] = value
& 0xFF;
1183 vnc_write(vs
, buf
, 4);
1186 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1190 buf
[0] = (value
>> 8) & 0xFF;
1191 buf
[1] = value
& 0xFF;
1193 vnc_write(vs
, buf
, 2);
1196 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1198 vnc_write(vs
, (char *)&value
, 1);
1201 void vnc_flush(VncState
*vs
)
1203 if (vs
->csock
!= -1 && vs
->output
.offset
)
1204 vnc_client_write(vs
);
1207 uint8_t read_u8(uint8_t *data
, size_t offset
)
1209 return data
[offset
];
1212 uint16_t read_u16(uint8_t *data
, size_t offset
)
1214 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1217 int32_t read_s32(uint8_t *data
, size_t offset
)
1219 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1220 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1223 uint32_t read_u32(uint8_t *data
, size_t offset
)
1225 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1226 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1229 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1233 static void check_pointer_type_change(VncState
*vs
, int absolute
)
1235 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1236 vnc_write_u8(vs
, 0);
1237 vnc_write_u8(vs
, 0);
1238 vnc_write_u16(vs
, 1);
1239 vnc_framebuffer_update(vs
, absolute
, 0,
1240 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1241 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1244 vs
->absolute
= absolute
;
1247 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1252 if (button_mask
& 0x01)
1253 buttons
|= MOUSE_EVENT_LBUTTON
;
1254 if (button_mask
& 0x02)
1255 buttons
|= MOUSE_EVENT_MBUTTON
;
1256 if (button_mask
& 0x04)
1257 buttons
|= MOUSE_EVENT_RBUTTON
;
1258 if (button_mask
& 0x08)
1260 if (button_mask
& 0x10)
1264 kbd_mouse_event(x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1),
1265 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1),
1267 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1271 kbd_mouse_event(x
, y
, dz
, buttons
);
1273 if (vs
->last_x
!= -1)
1274 kbd_mouse_event(x
- vs
->last_x
,
1281 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1284 static void reset_keys(VncState
*vs
)
1287 for(i
= 0; i
< 256; i
++) {
1288 if (vs
->modifiers_state
[i
]) {
1290 kbd_put_keycode(0xe0);
1291 kbd_put_keycode(i
| 0x80);
1292 vs
->modifiers_state
[i
] = 0;
1297 static void press_key(VncState
*vs
, int keysym
)
1299 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & 0x7f);
1300 kbd_put_keycode(keysym2scancode(vs
->vd
->kbd_layout
, keysym
) | 0x80);
1303 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1305 /* QEMU console switch */
1307 case 0x2a: /* Left Shift */
1308 case 0x36: /* Right Shift */
1309 case 0x1d: /* Left CTRL */
1310 case 0x9d: /* Right CTRL */
1311 case 0x38: /* Left ALT */
1312 case 0xb8: /* Right ALT */
1314 vs
->modifiers_state
[keycode
] = 1;
1316 vs
->modifiers_state
[keycode
] = 0;
1318 case 0x02 ... 0x0a: /* '1' to '9' keys */
1319 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1320 /* Reset the modifiers sent to the current console */
1322 console_select(keycode
- 0x02);
1326 case 0x3a: /* CapsLock */
1327 case 0x45: /* NumLock */
1329 vs
->modifiers_state
[keycode
] ^= 1;
1333 if (keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1334 /* If the numlock state needs to change then simulate an additional
1335 keypress before sending this one. This will happen if the user
1336 toggles numlock away from the VNC window.
1338 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1339 if (!vs
->modifiers_state
[0x45]) {
1340 vs
->modifiers_state
[0x45] = 1;
1341 press_key(vs
, 0xff7f);
1344 if (vs
->modifiers_state
[0x45]) {
1345 vs
->modifiers_state
[0x45] = 0;
1346 press_key(vs
, 0xff7f);
1351 if (is_graphic_console()) {
1353 kbd_put_keycode(0xe0);
1355 kbd_put_keycode(keycode
& 0x7f);
1357 kbd_put_keycode(keycode
| 0x80);
1359 /* QEMU console emulation */
1361 int numlock
= vs
->modifiers_state
[0x45];
1363 case 0x2a: /* Left Shift */
1364 case 0x36: /* Right Shift */
1365 case 0x1d: /* Left CTRL */
1366 case 0x9d: /* Right CTRL */
1367 case 0x38: /* Left ALT */
1368 case 0xb8: /* Right ALT */
1371 kbd_put_keysym(QEMU_KEY_UP
);
1374 kbd_put_keysym(QEMU_KEY_DOWN
);
1377 kbd_put_keysym(QEMU_KEY_LEFT
);
1380 kbd_put_keysym(QEMU_KEY_RIGHT
);
1383 kbd_put_keysym(QEMU_KEY_DELETE
);
1386 kbd_put_keysym(QEMU_KEY_HOME
);
1389 kbd_put_keysym(QEMU_KEY_END
);
1392 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1395 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1399 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1402 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1405 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1408 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1411 kbd_put_keysym('5');
1414 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1417 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1420 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1423 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1426 kbd_put_keysym('0');
1429 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1433 kbd_put_keysym('/');
1436 kbd_put_keysym('*');
1439 kbd_put_keysym('-');
1442 kbd_put_keysym('+');
1445 kbd_put_keysym('\n');
1449 kbd_put_keysym(sym
);
1456 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1460 if (sym
>= 'A' && sym
<= 'Z' && is_graphic_console())
1461 sym
= sym
- 'A' + 'a';
1463 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, sym
& 0xFFFF);
1464 do_key_event(vs
, down
, keycode
, sym
);
1467 static void ext_key_event(VncState
*vs
, int down
,
1468 uint32_t sym
, uint16_t keycode
)
1470 /* if the user specifies a keyboard layout, always use it */
1471 if (keyboard_layout
)
1472 key_event(vs
, down
, sym
);
1474 do_key_event(vs
, down
, keycode
, sym
);
1477 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1478 int x_position
, int y_position
,
1481 if (x_position
> ds_get_width(vs
->ds
))
1482 x_position
= ds_get_width(vs
->ds
);
1483 if (y_position
> ds_get_height(vs
->ds
))
1484 y_position
= ds_get_height(vs
->ds
);
1485 if (x_position
+ w
>= ds_get_width(vs
->ds
))
1486 w
= ds_get_width(vs
->ds
) - x_position
;
1487 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1488 h
= ds_get_height(vs
->ds
) - y_position
;
1491 vs
->need_update
= 1;
1493 vs
->force_update
= 1;
1494 for (i
= 0; i
< h
; i
++) {
1495 vnc_set_bits(vs
->dirty
[y_position
+ i
],
1496 (ds_get_width(vs
->ds
) / 16), VNC_DIRTY_WORDS
);
1501 static void send_ext_key_event_ack(VncState
*vs
)
1503 vnc_write_u8(vs
, 0);
1504 vnc_write_u8(vs
, 0);
1505 vnc_write_u16(vs
, 1);
1506 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1507 VNC_ENCODING_EXT_KEY_EVENT
);
1511 static void send_ext_audio_ack(VncState
*vs
)
1513 vnc_write_u8(vs
, 0);
1514 vnc_write_u8(vs
, 0);
1515 vnc_write_u16(vs
, 1);
1516 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1517 VNC_ENCODING_AUDIO
);
1521 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1524 unsigned int enc
= 0;
1528 vs
->vnc_encoding
= 0;
1529 vs
->tight_compression
= 9;
1530 vs
->tight_quality
= 9;
1533 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1536 case VNC_ENCODING_RAW
:
1537 vs
->vnc_encoding
= enc
;
1539 case VNC_ENCODING_COPYRECT
:
1540 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1542 case VNC_ENCODING_HEXTILE
:
1543 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1544 vs
->vnc_encoding
= enc
;
1546 case VNC_ENCODING_ZLIB
:
1547 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1548 vs
->vnc_encoding
= enc
;
1550 case VNC_ENCODING_DESKTOPRESIZE
:
1551 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1553 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1554 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1556 case VNC_ENCODING_EXT_KEY_EVENT
:
1557 send_ext_key_event_ack(vs
);
1559 case VNC_ENCODING_AUDIO
:
1560 send_ext_audio_ack(vs
);
1562 case VNC_ENCODING_WMVi
:
1563 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1565 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1566 vs
->tight_compression
= (enc
& 0x0F);
1568 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1569 vs
->tight_quality
= (enc
& 0x0F);
1572 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1577 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1580 static void set_pixel_conversion(VncState
*vs
)
1582 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1583 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1584 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1585 vs
->write_pixels
= vnc_write_pixels_copy
;
1586 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1588 vs
->send_hextile_tile
= send_hextile_tile_8
;
1591 vs
->send_hextile_tile
= send_hextile_tile_16
;
1594 vs
->send_hextile_tile
= send_hextile_tile_32
;
1598 vs
->write_pixels
= vnc_write_pixels_generic
;
1599 switch (vs
->ds
->surface
->pf
.bits_per_pixel
) {
1601 vs
->send_hextile_tile
= send_hextile_tile_generic_8
;
1604 vs
->send_hextile_tile
= send_hextile_tile_generic_16
;
1607 vs
->send_hextile_tile
= send_hextile_tile_generic_32
;
1613 static void set_pixel_format(VncState
*vs
,
1614 int bits_per_pixel
, int depth
,
1615 int big_endian_flag
, int true_color_flag
,
1616 int red_max
, int green_max
, int blue_max
,
1617 int red_shift
, int green_shift
, int blue_shift
)
1619 if (!true_color_flag
) {
1620 vnc_client_error(vs
);
1624 vs
->clientds
= *(vs
->vd
->guest
.ds
);
1625 vs
->clientds
.pf
.rmax
= red_max
;
1626 count_bits(vs
->clientds
.pf
.rbits
, red_max
);
1627 vs
->clientds
.pf
.rshift
= red_shift
;
1628 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1629 vs
->clientds
.pf
.gmax
= green_max
;
1630 count_bits(vs
->clientds
.pf
.gbits
, green_max
);
1631 vs
->clientds
.pf
.gshift
= green_shift
;
1632 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1633 vs
->clientds
.pf
.bmax
= blue_max
;
1634 count_bits(vs
->clientds
.pf
.bbits
, blue_max
);
1635 vs
->clientds
.pf
.bshift
= blue_shift
;
1636 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1637 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1638 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1639 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1640 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1642 set_pixel_conversion(vs
);
1644 vga_hw_invalidate();
1648 static void pixel_format_message (VncState
*vs
) {
1649 char pad
[3] = { 0, 0, 0 };
1651 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1652 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1654 #ifdef HOST_WORDS_BIGENDIAN
1655 vnc_write_u8(vs
, 1); /* big-endian-flag */
1657 vnc_write_u8(vs
, 0); /* big-endian-flag */
1659 vnc_write_u8(vs
, 1); /* true-color-flag */
1660 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1661 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1662 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1663 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1664 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1665 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1666 if (vs
->ds
->surface
->pf
.bits_per_pixel
== 32)
1667 vs
->send_hextile_tile
= send_hextile_tile_32
;
1668 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 16)
1669 vs
->send_hextile_tile
= send_hextile_tile_16
;
1670 else if (vs
->ds
->surface
->pf
.bits_per_pixel
== 8)
1671 vs
->send_hextile_tile
= send_hextile_tile_8
;
1672 vs
->clientds
= *(vs
->ds
->surface
);
1673 vs
->clientds
.flags
&= ~QEMU_ALLOCATED_FLAG
;
1674 vs
->write_pixels
= vnc_write_pixels_copy
;
1676 vnc_write(vs
, pad
, 3); /* padding */
1679 static void vnc_dpy_setdata(DisplayState
*ds
)
1681 /* We don't have to do anything */
1684 static void vnc_colordepth(VncState
*vs
)
1686 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1687 /* Sending a WMVi message to notify the client*/
1688 vnc_write_u8(vs
, 0); /* msg id */
1689 vnc_write_u8(vs
, 0);
1690 vnc_write_u16(vs
, 1); /* number of rects */
1691 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1692 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1693 pixel_format_message(vs
);
1696 set_pixel_conversion(vs
);
1700 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1710 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1711 read_u8(data
, 6), read_u8(data
, 7),
1712 read_u16(data
, 8), read_u16(data
, 10),
1713 read_u16(data
, 12), read_u8(data
, 14),
1714 read_u8(data
, 15), read_u8(data
, 16));
1721 limit
= read_u16(data
, 2);
1723 return 4 + (limit
* 4);
1725 limit
= read_u16(data
, 2);
1727 for (i
= 0; i
< limit
; i
++) {
1728 int32_t val
= read_s32(data
, 4 + (i
* 4));
1729 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1732 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1738 framebuffer_update_request(vs
,
1739 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1740 read_u16(data
, 6), read_u16(data
, 8));
1746 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1752 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1759 uint32_t dlen
= read_u32(data
, 4);
1764 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1770 switch (read_u8(data
, 1)) {
1775 ext_key_event(vs
, read_u16(data
, 2),
1776 read_u32(data
, 4), read_u32(data
, 8));
1782 switch (read_u16 (data
, 2)) {
1792 switch (read_u8(data
, 4)) {
1793 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
1794 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
1795 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
1796 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
1797 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
1798 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
1800 printf("Invalid audio format %d\n", read_u8(data
, 4));
1801 vnc_client_error(vs
);
1804 vs
->as
.nchannels
= read_u8(data
, 5);
1805 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
1806 printf("Invalid audio channel coount %d\n",
1808 vnc_client_error(vs
);
1811 vs
->as
.freq
= read_u32(data
, 6);
1814 printf ("Invalid audio message %d\n", read_u8(data
, 4));
1815 vnc_client_error(vs
);
1821 printf("Msg: %d\n", read_u16(data
, 0));
1822 vnc_client_error(vs
);
1827 printf("Msg: %d\n", data
[0]);
1828 vnc_client_error(vs
);
1832 vnc_read_when(vs
, protocol_client_msg
, 1);
1836 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
1841 vnc_write_u16(vs
, ds_get_width(vs
->ds
));
1842 vnc_write_u16(vs
, ds_get_height(vs
->ds
));
1844 pixel_format_message(vs
);
1847 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
1849 size
= snprintf(buf
, sizeof(buf
), "QEMU");
1851 vnc_write_u32(vs
, size
);
1852 vnc_write(vs
, buf
, size
);
1855 vnc_read_when(vs
, protocol_client_msg
, 1);
1860 void start_client_init(VncState
*vs
)
1862 vnc_read_when(vs
, protocol_client_init
, 1);
1865 static void make_challenge(VncState
*vs
)
1869 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1871 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1872 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1875 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
1877 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
1879 unsigned char key
[8];
1881 if (!vs
->vd
->password
|| !vs
->vd
->password
[0]) {
1882 VNC_DEBUG("No password configured on server");
1883 vnc_write_u32(vs
, 1); /* Reject auth */
1884 if (vs
->minor
>= 8) {
1885 static const char err
[] = "Authentication failed";
1886 vnc_write_u32(vs
, sizeof(err
));
1887 vnc_write(vs
, err
, sizeof(err
));
1890 vnc_client_error(vs
);
1894 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1896 /* Calculate the expected challenge response */
1897 pwlen
= strlen(vs
->vd
->password
);
1898 for (i
=0; i
<sizeof(key
); i
++)
1899 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
1901 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1902 des(response
+j
, response
+j
);
1904 /* Compare expected vs actual challenge response */
1905 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1906 VNC_DEBUG("Client challenge reponse did not match\n");
1907 vnc_write_u32(vs
, 1); /* Reject auth */
1908 if (vs
->minor
>= 8) {
1909 static const char err
[] = "Authentication failed";
1910 vnc_write_u32(vs
, sizeof(err
));
1911 vnc_write(vs
, err
, sizeof(err
));
1914 vnc_client_error(vs
);
1916 VNC_DEBUG("Accepting VNC challenge response\n");
1917 vnc_write_u32(vs
, 0); /* Accept auth */
1920 start_client_init(vs
);
1925 void start_auth_vnc(VncState
*vs
)
1928 /* Send client a 'random' challenge */
1929 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1932 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1936 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
1938 /* We only advertise 1 auth scheme at a time, so client
1939 * must pick the one we sent. Verify this */
1940 if (data
[0] != vs
->vd
->auth
) { /* Reject auth */
1941 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
1942 vnc_write_u32(vs
, 1);
1943 if (vs
->minor
>= 8) {
1944 static const char err
[] = "Authentication failed";
1945 vnc_write_u32(vs
, sizeof(err
));
1946 vnc_write(vs
, err
, sizeof(err
));
1948 vnc_client_error(vs
);
1949 } else { /* Accept requested auth */
1950 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
1951 switch (vs
->vd
->auth
) {
1953 VNC_DEBUG("Accept auth none\n");
1954 if (vs
->minor
>= 8) {
1955 vnc_write_u32(vs
, 0); /* Accept auth completion */
1958 start_client_init(vs
);
1962 VNC_DEBUG("Start VNC auth\n");
1966 #ifdef CONFIG_VNC_TLS
1967 case VNC_AUTH_VENCRYPT
:
1968 VNC_DEBUG("Accept VeNCrypt auth\n");;
1969 start_auth_vencrypt(vs
);
1971 #endif /* CONFIG_VNC_TLS */
1973 #ifdef CONFIG_VNC_SASL
1975 VNC_DEBUG("Accept SASL auth\n");
1976 start_auth_sasl(vs
);
1978 #endif /* CONFIG_VNC_SASL */
1980 default: /* Should not be possible, but just in case */
1981 VNC_DEBUG("Reject auth %d server code bug\n", vs
->vd
->auth
);
1982 vnc_write_u8(vs
, 1);
1983 if (vs
->minor
>= 8) {
1984 static const char err
[] = "Authentication failed";
1985 vnc_write_u32(vs
, sizeof(err
));
1986 vnc_write(vs
, err
, sizeof(err
));
1988 vnc_client_error(vs
);
1994 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
1998 memcpy(local
, version
, 12);
2001 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2002 VNC_DEBUG("Malformed protocol version %s\n", local
);
2003 vnc_client_error(vs
);
2006 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2007 if (vs
->major
!= 3 ||
2013 VNC_DEBUG("Unsupported client version\n");
2014 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2016 vnc_client_error(vs
);
2019 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2020 * as equivalent to v3.3 by servers
2022 if (vs
->minor
== 4 || vs
->minor
== 5)
2025 if (vs
->minor
== 3) {
2026 if (vs
->vd
->auth
== VNC_AUTH_NONE
) {
2027 VNC_DEBUG("Tell client auth none\n");
2028 vnc_write_u32(vs
, vs
->vd
->auth
);
2030 start_client_init(vs
);
2031 } else if (vs
->vd
->auth
== VNC_AUTH_VNC
) {
2032 VNC_DEBUG("Tell client VNC auth\n");
2033 vnc_write_u32(vs
, vs
->vd
->auth
);
2037 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->vd
->auth
);
2038 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2040 vnc_client_error(vs
);
2043 VNC_DEBUG("Telling client we support auth %d\n", vs
->vd
->auth
);
2044 vnc_write_u8(vs
, 1); /* num auth */
2045 vnc_write_u8(vs
, vs
->vd
->auth
);
2046 vnc_read_when(vs
, protocol_client_auth
, 1);
2053 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2057 uint8_t *server_row
;
2059 uint32_t width_mask
[VNC_DIRTY_WORDS
];
2060 VncState
*vs
= NULL
;
2064 * Walk through the guest dirty map.
2065 * Check and copy modified bits from guest to server surface.
2066 * Update server dirty map.
2068 vnc_set_bits(width_mask
, (ds_get_width(vd
->ds
) / 16), VNC_DIRTY_WORDS
);
2069 cmp_bytes
= 16 * ds_get_bytes_per_pixel(vd
->ds
);
2070 guest_row
= vd
->guest
.ds
->data
;
2071 server_row
= vd
->server
->data
;
2072 for (y
= 0; y
< vd
->guest
.ds
->height
; y
++) {
2073 if (vnc_and_bits(vd
->guest
.dirty
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
2076 uint8_t *server_ptr
;
2078 guest_ptr
= guest_row
;
2079 server_ptr
= server_row
;
2081 for (x
= 0; x
< vd
->guest
.ds
->width
;
2082 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2083 if (!vnc_get_bit(vd
->guest
.dirty
[y
], (x
/ 16)))
2085 vnc_clear_bit(vd
->guest
.dirty
[y
], (x
/ 16));
2086 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2088 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2090 while (vs
!= NULL
) {
2091 vnc_set_bit(vs
->dirty
[y
], (x
/ 16));
2097 guest_row
+= ds_get_linesize(vd
->ds
);
2098 server_row
+= ds_get_linesize(vd
->ds
);
2103 static void vnc_refresh(void *opaque
)
2105 VncDisplay
*vd
= opaque
;
2106 VncState
*vs
= NULL
;
2111 has_dirty
= vnc_refresh_server_surface(vd
);
2114 while (vs
!= NULL
) {
2115 vnc_update_client(vs
, has_dirty
);
2119 qemu_mod_timer(vd
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
2122 static void vnc_init_timer(VncDisplay
*vd
)
2124 if (vd
->timer
== NULL
&& vd
->clients
!= NULL
) {
2125 vd
->timer
= qemu_new_timer(rt_clock
, vnc_refresh
, vd
);
2130 static void vnc_remove_timer(VncDisplay
*vd
)
2132 if (vd
->timer
!= NULL
&& vd
->clients
== NULL
) {
2133 qemu_del_timer(vd
->timer
);
2134 qemu_free_timer(vd
->timer
);
2139 static void vnc_connect(VncDisplay
*vd
, int csock
)
2141 VncState
*vs
= qemu_mallocz(sizeof(VncState
));
2144 VNC_DEBUG("New client on socket %d\n", csock
);
2146 socket_set_nonblock(vs
->csock
);
2147 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2154 vs
->as
.freq
= 44100;
2155 vs
->as
.nchannels
= 2;
2156 vs
->as
.fmt
= AUD_FMT_S16
;
2157 vs
->as
.endianness
= 0;
2159 vs
->next
= vd
->clients
;
2164 vnc_write(vs
, "RFB 003.008\n", 12);
2166 vnc_read_when(vs
, protocol_version
, 12);
2171 /* vs might be free()ed here */
2174 static void vnc_listen_read(void *opaque
)
2176 VncDisplay
*vs
= opaque
;
2177 struct sockaddr_in addr
;
2178 socklen_t addrlen
= sizeof(addr
);
2183 int csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2185 vnc_connect(vs
, csock
);
2189 void vnc_display_init(DisplayState
*ds
)
2191 VncDisplay
*vs
= qemu_mallocz(sizeof(*vs
));
2193 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2203 if (keyboard_layout
)
2204 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2206 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2208 if (!vs
->kbd_layout
)
2211 dcl
->dpy_copy
= vnc_dpy_copy
;
2212 dcl
->dpy_update
= vnc_dpy_update
;
2213 dcl
->dpy_resize
= vnc_dpy_resize
;
2214 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2215 register_displaychangelistener(ds
, dcl
);
2219 void vnc_display_close(DisplayState
*ds
)
2221 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2226 qemu_free(vs
->display
);
2229 if (vs
->lsock
!= -1) {
2230 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2234 vs
->auth
= VNC_AUTH_INVALID
;
2235 #ifdef CONFIG_VNC_TLS
2236 vs
->subauth
= VNC_AUTH_INVALID
;
2237 vs
->tls
.x509verify
= 0;
2241 int vnc_display_password(DisplayState
*ds
, const char *password
)
2243 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2246 qemu_free(vs
->password
);
2247 vs
->password
= NULL
;
2249 if (password
&& password
[0]) {
2250 if (!(vs
->password
= qemu_strdup(password
)))
2257 char *vnc_display_local_addr(DisplayState
*ds
)
2259 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2261 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2264 int vnc_display_open(DisplayState
*ds
, const char *display
)
2266 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2267 const char *options
;
2271 #ifdef CONFIG_VNC_TLS
2272 int tls
= 0, x509
= 0;
2274 #ifdef CONFIG_VNC_SASL
2282 vnc_display_close(ds
);
2283 if (strcmp(display
, "none") == 0)
2286 if (!(vs
->display
= strdup(display
)))
2290 while ((options
= strchr(options
, ','))) {
2292 if (strncmp(options
, "password", 8) == 0) {
2293 password
= 1; /* Require password auth */
2294 } else if (strncmp(options
, "reverse", 7) == 0) {
2296 } else if (strncmp(options
, "to=", 3) == 0) {
2297 to_port
= atoi(options
+3) + 5900;
2298 #ifdef CONFIG_VNC_SASL
2299 } else if (strncmp(options
, "sasl", 4) == 0) {
2300 sasl
= 1; /* Require SASL auth */
2302 #ifdef CONFIG_VNC_TLS
2303 } else if (strncmp(options
, "tls", 3) == 0) {
2304 tls
= 1; /* Require TLS */
2305 } else if (strncmp(options
, "x509", 4) == 0) {
2307 x509
= 1; /* Require x509 certificates */
2308 if (strncmp(options
, "x509verify", 10) == 0)
2309 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2311 /* Now check for 'x509=/some/path' postfix
2312 * and use that to setup x509 certificate/key paths */
2313 start
= strchr(options
, '=');
2314 end
= strchr(options
, ',');
2315 if (start
&& (!end
|| (start
< end
))) {
2316 int len
= end
? end
-(start
+1) : strlen(start
+1);
2317 char *path
= qemu_strndup(start
+ 1, len
);
2319 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2320 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2321 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2323 qemu_free(vs
->display
);
2329 fprintf(stderr
, "No certificate path provided\n");
2330 qemu_free(vs
->display
);
2335 } else if (strncmp(options
, "acl", 3) == 0) {
2340 #ifdef CONFIG_VNC_TLS
2341 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2342 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2343 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2348 #ifdef CONFIG_VNC_SASL
2350 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2351 fprintf(stderr
, "Failed to create username ACL\n");
2358 * Combinations we support here:
2360 * - no-auth (clear text, no auth)
2361 * - password (clear text, weak auth)
2362 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2363 * - tls (encrypt, weak anonymous creds, no auth)
2364 * - tls + password (encrypt, weak anonymous creds, weak auth)
2365 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2366 * - tls + x509 (encrypt, good x509 creds, no auth)
2367 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2368 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2370 * NB1. TLS is a stackable auth scheme.
2371 * NB2. the x509 schemes have option to validate a client cert dname
2374 #ifdef CONFIG_VNC_TLS
2376 vs
->auth
= VNC_AUTH_VENCRYPT
;
2378 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2379 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2381 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2382 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2385 #endif /* CONFIG_VNC_TLS */
2386 VNC_DEBUG("Initializing VNC server with password auth\n");
2387 vs
->auth
= VNC_AUTH_VNC
;
2388 #ifdef CONFIG_VNC_TLS
2389 vs
->subauth
= VNC_AUTH_INVALID
;
2391 #endif /* CONFIG_VNC_TLS */
2392 #ifdef CONFIG_VNC_SASL
2394 #ifdef CONFIG_VNC_TLS
2396 vs
->auth
= VNC_AUTH_VENCRYPT
;
2398 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2399 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
2401 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2402 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
2405 #endif /* CONFIG_VNC_TLS */
2406 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2407 vs
->auth
= VNC_AUTH_SASL
;
2408 #ifdef CONFIG_VNC_TLS
2409 vs
->subauth
= VNC_AUTH_INVALID
;
2411 #endif /* CONFIG_VNC_TLS */
2412 #endif /* CONFIG_VNC_SASL */
2414 #ifdef CONFIG_VNC_TLS
2416 vs
->auth
= VNC_AUTH_VENCRYPT
;
2418 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2419 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2421 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2422 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2426 VNC_DEBUG("Initializing VNC server with no auth\n");
2427 vs
->auth
= VNC_AUTH_NONE
;
2428 #ifdef CONFIG_VNC_TLS
2429 vs
->subauth
= VNC_AUTH_INVALID
;
2434 #ifdef CONFIG_VNC_SASL
2435 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
2436 fprintf(stderr
, "Failed to initialize SASL auth %s",
2437 sasl_errstring(saslErr
, NULL
, NULL
));
2445 /* connect to viewer */
2446 if (strncmp(display
, "unix:", 5) == 0)
2447 vs
->lsock
= unix_connect(display
+5);
2449 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
2450 if (-1 == vs
->lsock
) {
2455 int csock
= vs
->lsock
;
2457 vnc_connect(vs
, csock
);
2462 /* listen for connects */
2464 dpy
= qemu_malloc(256);
2465 if (strncmp(display
, "unix:", 5) == 0) {
2466 pstrcpy(dpy
, 256, "unix:");
2467 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
2469 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
2471 if (-1 == vs
->lsock
) {
2479 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);