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
30 #include "qemu_socket.h"
31 #include "qemu-timer.h"
33 #include "qemu-objects.h"
34 #include "qmp-commands.h"
36 #define VNC_REFRESH_INTERVAL_BASE 30
37 #define VNC_REFRESH_INTERVAL_INC 50
38 #define VNC_REFRESH_INTERVAL_MAX 2000
39 static const struct timeval VNC_REFRESH_STATS
= { 0, 500000 };
40 static const struct timeval VNC_REFRESH_LOSSY
= { 2, 0 };
42 #include "vnc_keysym.h"
45 static VncDisplay
*vnc_display
; /* needed for info vnc */
46 static DisplayChangeListener
*dcl
;
48 static int vnc_cursor_define(VncState
*vs
);
49 static void vnc_release_modifiers(VncState
*vs
);
51 static void vnc_set_share_mode(VncState
*vs
, VncShareMode mode
)
54 static const char *mn
[] = {
56 [VNC_SHARE_MODE_CONNECTING
] = "connecting",
57 [VNC_SHARE_MODE_SHARED
] = "shared",
58 [VNC_SHARE_MODE_EXCLUSIVE
] = "exclusive",
59 [VNC_SHARE_MODE_DISCONNECTED
] = "disconnected",
61 fprintf(stderr
, "%s/%d: %s -> %s\n", __func__
,
62 vs
->csock
, mn
[vs
->share_mode
], mn
[mode
]);
65 if (vs
->share_mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
66 vs
->vd
->num_exclusive
--;
68 vs
->share_mode
= mode
;
69 if (vs
->share_mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
70 vs
->vd
->num_exclusive
++;
74 static char *addr_to_string(const char *format
,
75 struct sockaddr_storage
*sa
,
78 char host
[NI_MAXHOST
];
79 char serv
[NI_MAXSERV
];
83 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
86 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
87 VNC_DEBUG("Cannot resolve address %d: %s\n",
88 err
, gai_strerror(err
));
92 /* Enough for the existing format + the 2 vars we're
94 addrlen
= strlen(format
) + strlen(host
) + strlen(serv
);
95 addr
= g_malloc(addrlen
+ 1);
96 snprintf(addr
, addrlen
, format
, host
, serv
);
103 char *vnc_socket_local_addr(const char *format
, int fd
) {
104 struct sockaddr_storage sa
;
108 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
111 return addr_to_string(format
, &sa
, salen
);
114 char *vnc_socket_remote_addr(const char *format
, int fd
) {
115 struct sockaddr_storage sa
;
119 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0)
122 return addr_to_string(format
, &sa
, salen
);
125 static int put_addr_qdict(QDict
*qdict
, struct sockaddr_storage
*sa
,
128 char host
[NI_MAXHOST
];
129 char serv
[NI_MAXSERV
];
132 if ((err
= getnameinfo((struct sockaddr
*)sa
, salen
,
135 NI_NUMERICHOST
| NI_NUMERICSERV
)) != 0) {
136 VNC_DEBUG("Cannot resolve address %d: %s\n",
137 err
, gai_strerror(err
));
141 qdict_put(qdict
, "host", qstring_from_str(host
));
142 qdict_put(qdict
, "service", qstring_from_str(serv
));
143 qdict_put(qdict
, "family",qstring_from_str(inet_strfamily(sa
->ss_family
)));
148 static int vnc_server_addr_put(QDict
*qdict
, int fd
)
150 struct sockaddr_storage sa
;
154 if (getsockname(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
158 return put_addr_qdict(qdict
, &sa
, salen
);
161 static int vnc_qdict_remote_addr(QDict
*qdict
, int fd
)
163 struct sockaddr_storage sa
;
167 if (getpeername(fd
, (struct sockaddr
*)&sa
, &salen
) < 0) {
171 return put_addr_qdict(qdict
, &sa
, salen
);
174 static const char *vnc_auth_name(VncDisplay
*vd
) {
176 case VNC_AUTH_INVALID
:
192 case VNC_AUTH_VENCRYPT
:
193 #ifdef CONFIG_VNC_TLS
194 switch (vd
->subauth
) {
195 case VNC_AUTH_VENCRYPT_PLAIN
:
196 return "vencrypt+plain";
197 case VNC_AUTH_VENCRYPT_TLSNONE
:
198 return "vencrypt+tls+none";
199 case VNC_AUTH_VENCRYPT_TLSVNC
:
200 return "vencrypt+tls+vnc";
201 case VNC_AUTH_VENCRYPT_TLSPLAIN
:
202 return "vencrypt+tls+plain";
203 case VNC_AUTH_VENCRYPT_X509NONE
:
204 return "vencrypt+x509+none";
205 case VNC_AUTH_VENCRYPT_X509VNC
:
206 return "vencrypt+x509+vnc";
207 case VNC_AUTH_VENCRYPT_X509PLAIN
:
208 return "vencrypt+x509+plain";
209 case VNC_AUTH_VENCRYPT_TLSSASL
:
210 return "vencrypt+tls+sasl";
211 case VNC_AUTH_VENCRYPT_X509SASL
:
212 return "vencrypt+x509+sasl";
225 static int vnc_server_info_put(QDict
*qdict
)
227 if (vnc_server_addr_put(qdict
, vnc_display
->lsock
) < 0) {
231 qdict_put(qdict
, "auth", qstring_from_str(vnc_auth_name(vnc_display
)));
235 static void vnc_client_cache_auth(VncState
*client
)
237 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
245 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
246 qdict
= qobject_to_qdict(client
->info
);
249 #ifdef CONFIG_VNC_TLS
250 if (client
->tls
.session
&&
252 qdict_put(qdict
, "x509_dname", qstring_from_str(client
->tls
.dname
));
255 #ifdef CONFIG_VNC_SASL
256 if (client
->sasl
.conn
&&
257 client
->sasl
.username
) {
258 qdict_put(qdict
, "sasl_username",
259 qstring_from_str(client
->sasl
.username
));
264 static void vnc_client_cache_addr(VncState
*client
)
269 if (vnc_qdict_remote_addr(qdict
, client
->csock
) < 0) {
271 /* XXX: how to report the error? */
275 client
->info
= QOBJECT(qdict
);
278 static void vnc_qmp_event(VncState
*vs
, MonitorEvent event
)
287 server
= qdict_new();
288 if (vnc_server_info_put(server
) < 0) {
293 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
294 vs
->info
, QOBJECT(server
));
296 monitor_protocol_event(event
, data
);
298 qobject_incref(vs
->info
);
299 qobject_decref(data
);
302 static VncClientInfo
*qmp_query_vnc_client(const VncState
*client
)
304 struct sockaddr_storage sa
;
305 socklen_t salen
= sizeof(sa
);
306 char host
[NI_MAXHOST
];
307 char serv
[NI_MAXSERV
];
310 if (getpeername(client
->csock
, (struct sockaddr
*)&sa
, &salen
) < 0) {
314 if (getnameinfo((struct sockaddr
*)&sa
, salen
,
317 NI_NUMERICHOST
| NI_NUMERICSERV
) < 0) {
321 info
= g_malloc0(sizeof(*info
));
322 info
->host
= g_strdup(host
);
323 info
->service
= g_strdup(serv
);
324 info
->family
= g_strdup(inet_strfamily(sa
.ss_family
));
326 #ifdef CONFIG_VNC_TLS
327 if (client
->tls
.session
&& client
->tls
.dname
) {
328 info
->has_x509_dname
= true;
329 info
->x509_dname
= g_strdup(client
->tls
.dname
);
332 #ifdef CONFIG_VNC_SASL
333 if (client
->sasl
.conn
&& client
->sasl
.username
) {
334 info
->has_sasl_username
= true;
335 info
->sasl_username
= g_strdup(client
->sasl
.username
);
342 VncInfo
*qmp_query_vnc(Error
**errp
)
344 VncInfo
*info
= g_malloc0(sizeof(*info
));
346 if (vnc_display
== NULL
|| vnc_display
->display
== NULL
) {
347 info
->enabled
= false;
349 VncClientInfoList
*cur_item
= NULL
;
350 struct sockaddr_storage sa
;
351 socklen_t salen
= sizeof(sa
);
352 char host
[NI_MAXHOST
];
353 char serv
[NI_MAXSERV
];
356 info
->enabled
= true;
358 /* for compatibility with the original command */
359 info
->has_clients
= true;
361 QTAILQ_FOREACH(client
, &vnc_display
->clients
, next
) {
362 VncClientInfoList
*cinfo
= g_malloc0(sizeof(*info
));
363 cinfo
->value
= qmp_query_vnc_client(client
);
365 /* XXX: waiting for the qapi to support GSList */
367 info
->clients
= cur_item
= cinfo
;
369 cur_item
->next
= cinfo
;
374 if (getsockname(vnc_display
->lsock
, (struct sockaddr
*)&sa
,
376 error_set(errp
, QERR_UNDEFINED_ERROR
);
380 if (getnameinfo((struct sockaddr
*)&sa
, salen
,
383 NI_NUMERICHOST
| NI_NUMERICSERV
) < 0) {
384 error_set(errp
, QERR_UNDEFINED_ERROR
);
388 info
->has_host
= true;
389 info
->host
= g_strdup(host
);
391 info
->has_service
= true;
392 info
->service
= g_strdup(serv
);
394 info
->has_family
= true;
395 info
->family
= g_strdup(inet_strfamily(sa
.ss_family
));
397 info
->has_auth
= true;
398 info
->auth
= g_strdup(vnc_auth_name(vnc_display
));
404 qapi_free_VncInfo(info
);
409 1) Get the queue working for IO.
410 2) there is some weirdness when using the -S option (the screen is grey
411 and not totally invalidated
412 3) resolutions > 1024
415 static int vnc_update_client(VncState
*vs
, int has_dirty
);
416 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
);
417 static void vnc_disconnect_start(VncState
*vs
);
418 static void vnc_disconnect_finish(VncState
*vs
);
419 static void vnc_init_timer(VncDisplay
*vd
);
420 static void vnc_remove_timer(VncDisplay
*vd
);
422 static void vnc_colordepth(VncState
*vs
);
423 static void framebuffer_update_request(VncState
*vs
, int incremental
,
424 int x_position
, int y_position
,
426 static void vnc_refresh(void *opaque
);
427 static int vnc_refresh_server_surface(VncDisplay
*vd
);
429 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
432 VncDisplay
*vd
= ds
->opaque
;
433 struct VncSurface
*s
= &vd
->guest
;
437 /* round x down to ensure the loop only spans one 16-pixel block per,
438 iteration. otherwise, if (x % 16) != 0, the last iteration may span
439 two 16-pixel blocks but we only mark the first as dirty
444 x
= MIN(x
, s
->ds
->width
);
445 y
= MIN(y
, s
->ds
->height
);
446 w
= MIN(x
+ w
, s
->ds
->width
) - x
;
447 h
= MIN(h
, s
->ds
->height
);
450 for (i
= 0; i
< w
; i
+= 16)
451 set_bit((x
+ i
) / 16, s
->dirty
[y
]);
454 void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
457 vnc_write_u16(vs
, x
);
458 vnc_write_u16(vs
, y
);
459 vnc_write_u16(vs
, w
);
460 vnc_write_u16(vs
, h
);
462 vnc_write_s32(vs
, encoding
);
465 void buffer_reserve(Buffer
*buffer
, size_t len
)
467 if ((buffer
->capacity
- buffer
->offset
) < len
) {
468 buffer
->capacity
+= (len
+ 1024);
469 buffer
->buffer
= g_realloc(buffer
->buffer
, buffer
->capacity
);
470 if (buffer
->buffer
== NULL
) {
471 fprintf(stderr
, "vnc: out of memory\n");
477 int buffer_empty(Buffer
*buffer
)
479 return buffer
->offset
== 0;
482 uint8_t *buffer_end(Buffer
*buffer
)
484 return buffer
->buffer
+ buffer
->offset
;
487 void buffer_reset(Buffer
*buffer
)
492 void buffer_free(Buffer
*buffer
)
494 g_free(buffer
->buffer
);
496 buffer
->capacity
= 0;
497 buffer
->buffer
= NULL
;
500 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
502 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
503 buffer
->offset
+= len
;
506 static void vnc_desktop_resize(VncState
*vs
)
508 DisplayState
*ds
= vs
->ds
;
510 if (vs
->csock
== -1 || !vnc_has_feature(vs
, VNC_FEATURE_RESIZE
)) {
513 if (vs
->client_width
== ds_get_width(ds
) &&
514 vs
->client_height
== ds_get_height(ds
)) {
517 vs
->client_width
= ds_get_width(ds
);
518 vs
->client_height
= ds_get_height(ds
);
520 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
522 vnc_write_u16(vs
, 1); /* number of rects */
523 vnc_framebuffer_update(vs
, 0, 0, vs
->client_width
, vs
->client_height
,
524 VNC_ENCODING_DESKTOPRESIZE
);
525 vnc_unlock_output(vs
);
529 #ifdef CONFIG_VNC_THREAD
530 static void vnc_abort_display_jobs(VncDisplay
*vd
)
534 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
537 vnc_unlock_output(vs
);
539 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
542 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
545 vnc_unlock_output(vs
);
549 static void vnc_abort_display_jobs(VncDisplay
*vd
)
554 static void vnc_dpy_resize(DisplayState
*ds
)
556 VncDisplay
*vd
= ds
->opaque
;
559 vnc_abort_display_jobs(vd
);
563 vd
->server
= g_malloc0(sizeof(*vd
->server
));
564 if (vd
->server
->data
)
565 g_free(vd
->server
->data
);
566 *(vd
->server
) = *(ds
->surface
);
567 vd
->server
->data
= g_malloc0(vd
->server
->linesize
*
572 vd
->guest
.ds
= g_malloc0(sizeof(*vd
->guest
.ds
));
573 if (ds_get_bytes_per_pixel(ds
) != vd
->guest
.ds
->pf
.bytes_per_pixel
)
574 console_color_init(ds
);
575 *(vd
->guest
.ds
) = *(ds
->surface
);
576 memset(vd
->guest
.dirty
, 0xFF, sizeof(vd
->guest
.dirty
));
578 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
580 vnc_desktop_resize(vs
);
581 if (vs
->vd
->cursor
) {
582 vnc_cursor_define(vs
);
584 memset(vs
->dirty
, 0xFF, sizeof(vs
->dirty
));
589 static void vnc_write_pixels_copy(VncState
*vs
, struct PixelFormat
*pf
,
590 void *pixels
, int size
)
592 vnc_write(vs
, pixels
, size
);
595 /* slowest but generic code. */
596 void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
599 VncDisplay
*vd
= vs
->vd
;
601 r
= ((((v
& vd
->server
->pf
.rmask
) >> vd
->server
->pf
.rshift
) << vs
->clientds
.pf
.rbits
) >>
602 vd
->server
->pf
.rbits
);
603 g
= ((((v
& vd
->server
->pf
.gmask
) >> vd
->server
->pf
.gshift
) << vs
->clientds
.pf
.gbits
) >>
604 vd
->server
->pf
.gbits
);
605 b
= ((((v
& vd
->server
->pf
.bmask
) >> vd
->server
->pf
.bshift
) << vs
->clientds
.pf
.bbits
) >>
606 vd
->server
->pf
.bbits
);
607 v
= (r
<< vs
->clientds
.pf
.rshift
) |
608 (g
<< vs
->clientds
.pf
.gshift
) |
609 (b
<< vs
->clientds
.pf
.bshift
);
610 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
615 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
625 if (vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) {
640 static void vnc_write_pixels_generic(VncState
*vs
, struct PixelFormat
*pf
,
641 void *pixels1
, int size
)
645 if (pf
->bytes_per_pixel
== 4) {
646 uint32_t *pixels
= pixels1
;
649 for(i
= 0; i
< n
; i
++) {
650 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
651 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
653 } else if (pf
->bytes_per_pixel
== 2) {
654 uint16_t *pixels
= pixels1
;
657 for(i
= 0; i
< n
; i
++) {
658 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
659 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
661 } else if (pf
->bytes_per_pixel
== 1) {
662 uint8_t *pixels
= pixels1
;
665 for(i
= 0; i
< n
; i
++) {
666 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
667 vnc_write(vs
, buf
, vs
->clientds
.pf
.bytes_per_pixel
);
670 fprintf(stderr
, "vnc_write_pixels_generic: VncState color depth not supported\n");
674 int vnc_raw_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
678 VncDisplay
*vd
= vs
->vd
;
680 row
= vd
->server
->data
+ y
* ds_get_linesize(vs
->ds
) + x
* ds_get_bytes_per_pixel(vs
->ds
);
681 for (i
= 0; i
< h
; i
++) {
682 vs
->write_pixels(vs
, &vd
->server
->pf
, row
, w
* ds_get_bytes_per_pixel(vs
->ds
));
683 row
+= ds_get_linesize(vs
->ds
);
688 int vnc_send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
692 switch(vs
->vnc_encoding
) {
693 case VNC_ENCODING_ZLIB
:
694 n
= vnc_zlib_send_framebuffer_update(vs
, x
, y
, w
, h
);
696 case VNC_ENCODING_HEXTILE
:
697 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_HEXTILE
);
698 n
= vnc_hextile_send_framebuffer_update(vs
, x
, y
, w
, h
);
700 case VNC_ENCODING_TIGHT
:
701 n
= vnc_tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
703 case VNC_ENCODING_TIGHT_PNG
:
704 n
= vnc_tight_png_send_framebuffer_update(vs
, x
, y
, w
, h
);
706 case VNC_ENCODING_ZRLE
:
707 n
= vnc_zrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
709 case VNC_ENCODING_ZYWRLE
:
710 n
= vnc_zywrle_send_framebuffer_update(vs
, x
, y
, w
, h
);
713 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_RAW
);
714 n
= vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
720 static void vnc_copy(VncState
*vs
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
722 /* send bitblit op to the vnc client */
724 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
726 vnc_write_u16(vs
, 1); /* number of rects */
727 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, VNC_ENCODING_COPYRECT
);
728 vnc_write_u16(vs
, src_x
);
729 vnc_write_u16(vs
, src_y
);
730 vnc_unlock_output(vs
);
734 static void vnc_dpy_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
736 VncDisplay
*vd
= ds
->opaque
;
740 int i
,x
,y
,pitch
,depth
,inc
,w_lim
,s
;
743 vnc_refresh_server_surface(vd
);
744 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
745 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
746 vs
->force_update
= 1;
747 vnc_update_client_sync(vs
, 1);
748 /* vs might be free()ed here */
752 /* do bitblit op on the local surface too */
753 pitch
= ds_get_linesize(vd
->ds
);
754 depth
= ds_get_bytes_per_pixel(vd
->ds
);
755 src_row
= vd
->server
->data
+ pitch
* src_y
+ depth
* src_x
;
756 dst_row
= vd
->server
->data
+ pitch
* dst_y
+ depth
* dst_x
;
761 src_row
+= pitch
* (h
-1);
762 dst_row
+= pitch
* (h
-1);
767 w_lim
= w
- (16 - (dst_x
% 16));
771 w_lim
= w
- (w_lim
% 16);
772 for (i
= 0; i
< h
; i
++) {
773 for (x
= 0; x
<= w_lim
;
774 x
+= s
, src_row
+= cmp_bytes
, dst_row
+= cmp_bytes
) {
776 if ((s
= w
- w_lim
) == 0)
779 s
= (16 - (dst_x
% 16));
784 cmp_bytes
= s
* depth
;
785 if (memcmp(src_row
, dst_row
, cmp_bytes
) == 0)
787 memmove(dst_row
, src_row
, cmp_bytes
);
788 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
789 if (!vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
790 set_bit(((x
+ dst_x
) / 16), vs
->dirty
[y
]);
794 src_row
+= pitch
- w
* depth
;
795 dst_row
+= pitch
- w
* depth
;
799 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
800 if (vnc_has_feature(vs
, VNC_FEATURE_COPYRECT
)) {
801 vnc_copy(vs
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
806 static void vnc_mouse_set(int x
, int y
, int visible
)
808 /* can we ask the client(s) to move the pointer ??? */
811 static int vnc_cursor_define(VncState
*vs
)
813 QEMUCursor
*c
= vs
->vd
->cursor
;
814 PixelFormat pf
= qemu_default_pixelformat(32);
817 if (vnc_has_feature(vs
, VNC_FEATURE_RICH_CURSOR
)) {
819 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
820 vnc_write_u8(vs
, 0); /* padding */
821 vnc_write_u16(vs
, 1); /* # of rects */
822 vnc_framebuffer_update(vs
, c
->hot_x
, c
->hot_y
, c
->width
, c
->height
,
823 VNC_ENCODING_RICH_CURSOR
);
824 isize
= c
->width
* c
->height
* vs
->clientds
.pf
.bytes_per_pixel
;
825 vnc_write_pixels_generic(vs
, &pf
, c
->data
, isize
);
826 vnc_write(vs
, vs
->vd
->cursor_mask
, vs
->vd
->cursor_msize
);
827 vnc_unlock_output(vs
);
833 static void vnc_dpy_cursor_define(QEMUCursor
*c
)
835 VncDisplay
*vd
= vnc_display
;
838 cursor_put(vd
->cursor
);
839 g_free(vd
->cursor_mask
);
842 cursor_get(vd
->cursor
);
843 vd
->cursor_msize
= cursor_get_mono_bpl(c
) * c
->height
;
844 vd
->cursor_mask
= g_malloc0(vd
->cursor_msize
);
845 cursor_get_mono_mask(c
, 0, vd
->cursor_mask
);
847 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
848 vnc_cursor_define(vs
);
852 static int find_and_clear_dirty_height(struct VncState
*vs
,
853 int y
, int last_x
, int x
, int height
)
857 for (h
= 1; h
< (height
- y
); h
++) {
859 if (!test_bit(last_x
, vs
->dirty
[y
+ h
])) {
862 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++) {
863 clear_bit(tmp_x
, vs
->dirty
[y
+ h
]);
870 #ifdef CONFIG_VNC_THREAD
871 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
873 int ret
= vnc_update_client(vs
, has_dirty
);
878 static int vnc_update_client_sync(VncState
*vs
, int has_dirty
)
880 return vnc_update_client(vs
, has_dirty
);
884 static int vnc_update_client(VncState
*vs
, int has_dirty
)
886 if (vs
->need_update
&& vs
->csock
!= -1) {
887 VncDisplay
*vd
= vs
->vd
;
894 if (vs
->output
.offset
&& !vs
->audio_cap
&& !vs
->force_update
)
895 /* kernel send buffers are full -> drop frames to throttle */
898 if (!has_dirty
&& !vs
->audio_cap
&& !vs
->force_update
)
902 * Send screen updates to the vnc client using the server
903 * surface and server dirty map. guest surface updates
904 * happening in parallel don't disturb us, the next pass will
905 * send them to the client.
907 job
= vnc_job_new(vs
);
909 width
= MIN(vd
->server
->width
, vs
->client_width
);
910 height
= MIN(vd
->server
->height
, vs
->client_height
);
912 for (y
= 0; y
< height
; y
++) {
915 for (x
= 0; x
< width
/ 16; x
++) {
916 if (test_and_clear_bit(x
, vs
->dirty
[y
])) {
922 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
,
925 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
926 (x
- last_x
) * 16, h
);
932 int h
= find_and_clear_dirty_height(vs
, y
, last_x
, x
, height
);
933 n
+= vnc_job_add_rect(job
, last_x
* 16, y
,
934 (x
- last_x
) * 16, h
);
939 vs
->force_update
= 0;
944 vnc_disconnect_finish(vs
);
950 static void audio_capture_notify(void *opaque
, audcnotification_e cmd
)
952 VncState
*vs
= opaque
;
955 case AUD_CNOTIFY_DISABLE
:
957 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
958 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
959 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_END
);
960 vnc_unlock_output(vs
);
964 case AUD_CNOTIFY_ENABLE
:
966 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
967 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
968 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN
);
969 vnc_unlock_output(vs
);
975 static void audio_capture_destroy(void *opaque
)
979 static void audio_capture(void *opaque
, void *buf
, int size
)
981 VncState
*vs
= opaque
;
984 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU
);
985 vnc_write_u8(vs
, VNC_MSG_SERVER_QEMU_AUDIO
);
986 vnc_write_u16(vs
, VNC_MSG_SERVER_QEMU_AUDIO_DATA
);
987 vnc_write_u32(vs
, size
);
988 vnc_write(vs
, buf
, size
);
989 vnc_unlock_output(vs
);
993 static void audio_add(VncState
*vs
)
995 struct audio_capture_ops ops
;
998 monitor_printf(default_mon
, "audio already running\n");
1002 ops
.notify
= audio_capture_notify
;
1003 ops
.destroy
= audio_capture_destroy
;
1004 ops
.capture
= audio_capture
;
1006 vs
->audio_cap
= AUD_add_capture(&vs
->as
, &ops
, vs
);
1007 if (!vs
->audio_cap
) {
1008 monitor_printf(default_mon
, "Failed to add audio capture\n");
1012 static void audio_del(VncState
*vs
)
1014 if (vs
->audio_cap
) {
1015 AUD_del_capture(vs
->audio_cap
, vs
);
1016 vs
->audio_cap
= NULL
;
1020 static void vnc_disconnect_start(VncState
*vs
)
1022 if (vs
->csock
== -1)
1024 vnc_set_share_mode(vs
, VNC_SHARE_MODE_DISCONNECTED
);
1025 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
1026 closesocket(vs
->csock
);
1030 static void vnc_disconnect_finish(VncState
*vs
)
1034 vnc_jobs_join(vs
); /* Wait encoding jobs */
1036 vnc_lock_output(vs
);
1037 vnc_qmp_event(vs
, QEVENT_VNC_DISCONNECTED
);
1039 buffer_free(&vs
->input
);
1040 buffer_free(&vs
->output
);
1042 qobject_decref(vs
->info
);
1045 vnc_tight_clear(vs
);
1048 #ifdef CONFIG_VNC_TLS
1049 vnc_tls_client_cleanup(vs
);
1050 #endif /* CONFIG_VNC_TLS */
1051 #ifdef CONFIG_VNC_SASL
1052 vnc_sasl_client_cleanup(vs
);
1053 #endif /* CONFIG_VNC_SASL */
1055 vnc_release_modifiers(vs
);
1057 QTAILQ_REMOVE(&vs
->vd
->clients
, vs
, next
);
1059 if (QTAILQ_EMPTY(&vs
->vd
->clients
)) {
1063 qemu_remove_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
1064 vnc_remove_timer(vs
->vd
);
1065 if (vs
->vd
->lock_key_sync
)
1066 qemu_remove_led_event_handler(vs
->led
);
1067 vnc_unlock_output(vs
);
1069 #ifdef CONFIG_VNC_THREAD
1070 qemu_mutex_destroy(&vs
->output_mutex
);
1072 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
1073 g_free(vs
->lossy_rect
[i
]);
1075 g_free(vs
->lossy_rect
);
1079 int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
1081 if (ret
== 0 || ret
== -1) {
1083 switch (last_errno
) {
1087 case WSAEWOULDBLOCK
:
1095 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1096 ret
, ret
< 0 ? last_errno
: 0);
1097 vnc_disconnect_start(vs
);
1105 void vnc_client_error(VncState
*vs
)
1107 VNC_DEBUG("Closing down client sock: protocol error\n");
1108 vnc_disconnect_start(vs
);
1113 * Called to write a chunk of data to the client socket. The data may
1114 * be the raw data, or may have already been encoded by SASL.
1115 * The data will be written either straight onto the socket, or
1116 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1118 * NB, it is theoretically possible to have 2 layers of encryption,
1119 * both SASL, and this TLS layer. It is highly unlikely in practice
1120 * though, since SASL encryption will typically be a no-op if TLS
1123 * Returns the number of bytes written, which may be less than
1124 * the requested 'datalen' if the socket would block. Returns
1125 * -1 on error, and disconnects the client socket.
1127 long vnc_client_write_buf(VncState
*vs
, const uint8_t *data
, size_t datalen
)
1130 #ifdef CONFIG_VNC_TLS
1131 if (vs
->tls
.session
) {
1132 ret
= gnutls_write(vs
->tls
.session
, data
, datalen
);
1134 if (ret
== GNUTLS_E_AGAIN
)
1141 #endif /* CONFIG_VNC_TLS */
1142 ret
= send(vs
->csock
, (const void *)data
, datalen
, 0);
1143 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data
, datalen
, ret
);
1144 return vnc_client_io_error(vs
, ret
, socket_error());
1149 * Called to write buffered data to the client socket, when not
1150 * using any SASL SSF encryption layers. Will write as much data
1151 * as possible without blocking. If all buffered data is written,
1152 * will switch the FD poll() handler back to read monitoring.
1154 * Returns the number of bytes written, which may be less than
1155 * the buffered output data if the socket would block. Returns
1156 * -1 on error, and disconnects the client socket.
1158 static long vnc_client_write_plain(VncState
*vs
)
1162 #ifdef CONFIG_VNC_SASL
1163 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
1164 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
1165 vs
->sasl
.waitWriteSSF
);
1167 if (vs
->sasl
.conn
&&
1169 vs
->sasl
.waitWriteSSF
) {
1170 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->sasl
.waitWriteSSF
);
1172 vs
->sasl
.waitWriteSSF
-= ret
;
1174 #endif /* CONFIG_VNC_SASL */
1175 ret
= vnc_client_write_buf(vs
, vs
->output
.buffer
, vs
->output
.offset
);
1179 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
1180 vs
->output
.offset
-= ret
;
1182 if (vs
->output
.offset
== 0) {
1183 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1191 * First function called whenever there is data to be written to
1192 * the client socket. Will delegate actual work according to whether
1193 * SASL SSF layers are enabled (thus requiring encryption calls)
1195 static void vnc_client_write_locked(void *opaque
)
1197 VncState
*vs
= opaque
;
1199 #ifdef CONFIG_VNC_SASL
1200 if (vs
->sasl
.conn
&&
1202 !vs
->sasl
.waitWriteSSF
) {
1203 vnc_client_write_sasl(vs
);
1205 #endif /* CONFIG_VNC_SASL */
1206 vnc_client_write_plain(vs
);
1209 void vnc_client_write(void *opaque
)
1211 VncState
*vs
= opaque
;
1213 vnc_lock_output(vs
);
1214 if (vs
->output
.offset
) {
1215 vnc_client_write_locked(opaque
);
1216 } else if (vs
->csock
!= -1) {
1217 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
1219 vnc_unlock_output(vs
);
1222 void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
1224 vs
->read_handler
= func
;
1225 vs
->read_handler_expect
= expecting
;
1230 * Called to read a chunk of data from the client socket. The data may
1231 * be the raw data, or may need to be further decoded by SASL.
1232 * The data will be read either straight from to the socket, or
1233 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1235 * NB, it is theoretically possible to have 2 layers of encryption,
1236 * both SASL, and this TLS layer. It is highly unlikely in practice
1237 * though, since SASL encryption will typically be a no-op if TLS
1240 * Returns the number of bytes read, which may be less than
1241 * the requested 'datalen' if the socket would block. Returns
1242 * -1 on error, and disconnects the client socket.
1244 long vnc_client_read_buf(VncState
*vs
, uint8_t *data
, size_t datalen
)
1247 #ifdef CONFIG_VNC_TLS
1248 if (vs
->tls
.session
) {
1249 ret
= gnutls_read(vs
->tls
.session
, data
, datalen
);
1251 if (ret
== GNUTLS_E_AGAIN
)
1258 #endif /* CONFIG_VNC_TLS */
1259 ret
= qemu_recv(vs
->csock
, data
, datalen
, 0);
1260 VNC_DEBUG("Read wire %p %zd -> %ld\n", data
, datalen
, ret
);
1261 return vnc_client_io_error(vs
, ret
, socket_error());
1266 * Called to read data from the client socket to the input buffer,
1267 * when not using any SASL SSF encryption layers. Will read as much
1268 * data as possible without blocking.
1270 * Returns the number of bytes read. Returns -1 on error, and
1271 * disconnects the client socket.
1273 static long vnc_client_read_plain(VncState
*vs
)
1276 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
1277 vs
->input
.buffer
, vs
->input
.capacity
, vs
->input
.offset
);
1278 buffer_reserve(&vs
->input
, 4096);
1279 ret
= vnc_client_read_buf(vs
, buffer_end(&vs
->input
), 4096);
1282 vs
->input
.offset
+= ret
;
1288 * First function called whenever there is more data to be read from
1289 * the client socket. Will delegate actual work according to whether
1290 * SASL SSF layers are enabled (thus requiring decryption calls)
1292 void vnc_client_read(void *opaque
)
1294 VncState
*vs
= opaque
;
1297 #ifdef CONFIG_VNC_SASL
1298 if (vs
->sasl
.conn
&& vs
->sasl
.runSSF
)
1299 ret
= vnc_client_read_sasl(vs
);
1301 #endif /* CONFIG_VNC_SASL */
1302 ret
= vnc_client_read_plain(vs
);
1304 if (vs
->csock
== -1)
1305 vnc_disconnect_finish(vs
);
1309 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
1310 size_t len
= vs
->read_handler_expect
;
1313 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
1314 if (vs
->csock
== -1) {
1315 vnc_disconnect_finish(vs
);
1320 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
1321 vs
->input
.offset
-= len
;
1323 vs
->read_handler_expect
= ret
;
1328 void vnc_write(VncState
*vs
, const void *data
, size_t len
)
1330 buffer_reserve(&vs
->output
, len
);
1332 if (vs
->csock
!= -1 && buffer_empty(&vs
->output
)) {
1333 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1336 buffer_append(&vs
->output
, data
, len
);
1339 void vnc_write_s32(VncState
*vs
, int32_t value
)
1341 vnc_write_u32(vs
, *(uint32_t *)&value
);
1344 void vnc_write_u32(VncState
*vs
, uint32_t value
)
1348 buf
[0] = (value
>> 24) & 0xFF;
1349 buf
[1] = (value
>> 16) & 0xFF;
1350 buf
[2] = (value
>> 8) & 0xFF;
1351 buf
[3] = value
& 0xFF;
1353 vnc_write(vs
, buf
, 4);
1356 void vnc_write_u16(VncState
*vs
, uint16_t value
)
1360 buf
[0] = (value
>> 8) & 0xFF;
1361 buf
[1] = value
& 0xFF;
1363 vnc_write(vs
, buf
, 2);
1366 void vnc_write_u8(VncState
*vs
, uint8_t value
)
1368 vnc_write(vs
, (char *)&value
, 1);
1371 void vnc_flush(VncState
*vs
)
1373 vnc_lock_output(vs
);
1374 if (vs
->csock
!= -1 && vs
->output
.offset
) {
1375 vnc_client_write_locked(vs
);
1377 vnc_unlock_output(vs
);
1380 uint8_t read_u8(uint8_t *data
, size_t offset
)
1382 return data
[offset
];
1385 uint16_t read_u16(uint8_t *data
, size_t offset
)
1387 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
1390 int32_t read_s32(uint8_t *data
, size_t offset
)
1392 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1393 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1396 uint32_t read_u32(uint8_t *data
, size_t offset
)
1398 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
1399 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
1402 static void client_cut_text(VncState
*vs
, size_t len
, uint8_t *text
)
1406 static void check_pointer_type_change(Notifier
*notifier
, void *data
)
1408 VncState
*vs
= container_of(notifier
, VncState
, mouse_mode_notifier
);
1409 int absolute
= kbd_mouse_is_absolute();
1411 if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
) && vs
->absolute
!= absolute
) {
1412 vnc_lock_output(vs
);
1413 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1414 vnc_write_u8(vs
, 0);
1415 vnc_write_u16(vs
, 1);
1416 vnc_framebuffer_update(vs
, absolute
, 0,
1417 ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1418 VNC_ENCODING_POINTER_TYPE_CHANGE
);
1419 vnc_unlock_output(vs
);
1422 vs
->absolute
= absolute
;
1425 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
1430 if (button_mask
& 0x01)
1431 buttons
|= MOUSE_EVENT_LBUTTON
;
1432 if (button_mask
& 0x02)
1433 buttons
|= MOUSE_EVENT_MBUTTON
;
1434 if (button_mask
& 0x04)
1435 buttons
|= MOUSE_EVENT_RBUTTON
;
1436 if (button_mask
& 0x08)
1438 if (button_mask
& 0x10)
1442 kbd_mouse_event(ds_get_width(vs
->ds
) > 1 ?
1443 x
* 0x7FFF / (ds_get_width(vs
->ds
) - 1) : 0x4000,
1444 ds_get_height(vs
->ds
) > 1 ?
1445 y
* 0x7FFF / (ds_get_height(vs
->ds
) - 1) : 0x4000,
1447 } else if (vnc_has_feature(vs
, VNC_FEATURE_POINTER_TYPE_CHANGE
)) {
1451 kbd_mouse_event(x
, y
, dz
, buttons
);
1453 if (vs
->last_x
!= -1)
1454 kbd_mouse_event(x
- vs
->last_x
,
1462 static void reset_keys(VncState
*vs
)
1465 for(i
= 0; i
< 256; i
++) {
1466 if (vs
->modifiers_state
[i
]) {
1467 if (i
& SCANCODE_GREY
)
1468 kbd_put_keycode(SCANCODE_EMUL0
);
1469 kbd_put_keycode(i
| SCANCODE_UP
);
1470 vs
->modifiers_state
[i
] = 0;
1475 static void press_key(VncState
*vs
, int keysym
)
1477 int keycode
= keysym2scancode(vs
->vd
->kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
1478 if (keycode
& SCANCODE_GREY
)
1479 kbd_put_keycode(SCANCODE_EMUL0
);
1480 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1481 if (keycode
& SCANCODE_GREY
)
1482 kbd_put_keycode(SCANCODE_EMUL0
);
1483 kbd_put_keycode(keycode
| SCANCODE_UP
);
1486 static void kbd_leds(void *opaque
, int ledstate
)
1488 VncState
*vs
= opaque
;
1491 caps
= ledstate
& QEMU_CAPS_LOCK_LED
? 1 : 0;
1492 num
= ledstate
& QEMU_NUM_LOCK_LED
? 1 : 0;
1494 if (vs
->modifiers_state
[0x3a] != caps
) {
1495 vs
->modifiers_state
[0x3a] = caps
;
1497 if (vs
->modifiers_state
[0x45] != num
) {
1498 vs
->modifiers_state
[0x45] = num
;
1502 static void do_key_event(VncState
*vs
, int down
, int keycode
, int sym
)
1504 /* QEMU console switch */
1506 case 0x2a: /* Left Shift */
1507 case 0x36: /* Right Shift */
1508 case 0x1d: /* Left CTRL */
1509 case 0x9d: /* Right CTRL */
1510 case 0x38: /* Left ALT */
1511 case 0xb8: /* Right ALT */
1513 vs
->modifiers_state
[keycode
] = 1;
1515 vs
->modifiers_state
[keycode
] = 0;
1517 case 0x02 ... 0x0a: /* '1' to '9' keys */
1518 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
1519 /* Reset the modifiers sent to the current console */
1521 console_select(keycode
- 0x02);
1525 case 0x3a: /* CapsLock */
1526 case 0x45: /* NumLock */
1528 vs
->modifiers_state
[keycode
] ^= 1;
1532 if (down
&& vs
->vd
->lock_key_sync
&&
1533 keycode_is_keypad(vs
->vd
->kbd_layout
, keycode
)) {
1534 /* If the numlock state needs to change then simulate an additional
1535 keypress before sending this one. This will happen if the user
1536 toggles numlock away from the VNC window.
1538 if (keysym_is_numlock(vs
->vd
->kbd_layout
, sym
& 0xFFFF)) {
1539 if (!vs
->modifiers_state
[0x45]) {
1540 vs
->modifiers_state
[0x45] = 1;
1541 press_key(vs
, 0xff7f);
1544 if (vs
->modifiers_state
[0x45]) {
1545 vs
->modifiers_state
[0x45] = 0;
1546 press_key(vs
, 0xff7f);
1551 if (down
&& vs
->vd
->lock_key_sync
&&
1552 ((sym
>= 'A' && sym
<= 'Z') || (sym
>= 'a' && sym
<= 'z'))) {
1553 /* If the capslock state needs to change then simulate an additional
1554 keypress before sending this one. This will happen if the user
1555 toggles capslock away from the VNC window.
1557 int uppercase
= !!(sym
>= 'A' && sym
<= 'Z');
1558 int shift
= !!(vs
->modifiers_state
[0x2a] | vs
->modifiers_state
[0x36]);
1559 int capslock
= !!(vs
->modifiers_state
[0x3a]);
1561 if (uppercase
== shift
) {
1562 vs
->modifiers_state
[0x3a] = 0;
1563 press_key(vs
, 0xffe5);
1566 if (uppercase
!= shift
) {
1567 vs
->modifiers_state
[0x3a] = 1;
1568 press_key(vs
, 0xffe5);
1573 if (is_graphic_console()) {
1574 if (keycode
& SCANCODE_GREY
)
1575 kbd_put_keycode(SCANCODE_EMUL0
);
1577 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
1579 kbd_put_keycode(keycode
| SCANCODE_UP
);
1581 bool numlock
= vs
->modifiers_state
[0x45];
1582 bool control
= (vs
->modifiers_state
[0x1d] ||
1583 vs
->modifiers_state
[0x9d]);
1584 /* QEMU console emulation */
1587 case 0x2a: /* Left Shift */
1588 case 0x36: /* Right Shift */
1589 case 0x1d: /* Left CTRL */
1590 case 0x9d: /* Right CTRL */
1591 case 0x38: /* Left ALT */
1592 case 0xb8: /* Right ALT */
1595 kbd_put_keysym(QEMU_KEY_UP
);
1598 kbd_put_keysym(QEMU_KEY_DOWN
);
1601 kbd_put_keysym(QEMU_KEY_LEFT
);
1604 kbd_put_keysym(QEMU_KEY_RIGHT
);
1607 kbd_put_keysym(QEMU_KEY_DELETE
);
1610 kbd_put_keysym(QEMU_KEY_HOME
);
1613 kbd_put_keysym(QEMU_KEY_END
);
1616 kbd_put_keysym(QEMU_KEY_PAGEUP
);
1619 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
1623 kbd_put_keysym(numlock
? '7' : QEMU_KEY_HOME
);
1626 kbd_put_keysym(numlock
? '8' : QEMU_KEY_UP
);
1629 kbd_put_keysym(numlock
? '9' : QEMU_KEY_PAGEUP
);
1632 kbd_put_keysym(numlock
? '4' : QEMU_KEY_LEFT
);
1635 kbd_put_keysym('5');
1638 kbd_put_keysym(numlock
? '6' : QEMU_KEY_RIGHT
);
1641 kbd_put_keysym(numlock
? '1' : QEMU_KEY_END
);
1644 kbd_put_keysym(numlock
? '2' : QEMU_KEY_DOWN
);
1647 kbd_put_keysym(numlock
? '3' : QEMU_KEY_PAGEDOWN
);
1650 kbd_put_keysym('0');
1653 kbd_put_keysym(numlock
? '.' : QEMU_KEY_DELETE
);
1657 kbd_put_keysym('/');
1660 kbd_put_keysym('*');
1663 kbd_put_keysym('-');
1666 kbd_put_keysym('+');
1669 kbd_put_keysym('\n');
1674 kbd_put_keysym(sym
& 0x1f);
1676 kbd_put_keysym(sym
);
1684 static void vnc_release_modifiers(VncState
*vs
)
1686 static const int keycodes
[] = {
1687 /* shift, control, alt keys, both left & right */
1688 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1692 if (!is_graphic_console()) {
1695 for (i
= 0; i
< ARRAY_SIZE(keycodes
); i
++) {
1696 keycode
= keycodes
[i
];
1697 if (!vs
->modifiers_state
[keycode
]) {
1700 if (keycode
& SCANCODE_GREY
) {
1701 kbd_put_keycode(SCANCODE_EMUL0
);
1703 kbd_put_keycode(keycode
| SCANCODE_UP
);
1707 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
1712 if (lsym
>= 'A' && lsym
<= 'Z' && is_graphic_console()) {
1713 lsym
= lsym
- 'A' + 'a';
1716 keycode
= keysym2scancode(vs
->vd
->kbd_layout
, lsym
& 0xFFFF) & SCANCODE_KEYMASK
;
1717 do_key_event(vs
, down
, keycode
, sym
);
1720 static void ext_key_event(VncState
*vs
, int down
,
1721 uint32_t sym
, uint16_t keycode
)
1723 /* if the user specifies a keyboard layout, always use it */
1724 if (keyboard_layout
)
1725 key_event(vs
, down
, sym
);
1727 do_key_event(vs
, down
, keycode
, sym
);
1730 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1731 int x_position
, int y_position
,
1735 const size_t width
= ds_get_width(vs
->ds
) / 16;
1737 if (y_position
> ds_get_height(vs
->ds
))
1738 y_position
= ds_get_height(vs
->ds
);
1739 if (y_position
+ h
>= ds_get_height(vs
->ds
))
1740 h
= ds_get_height(vs
->ds
) - y_position
;
1742 vs
->need_update
= 1;
1744 vs
->force_update
= 1;
1745 for (i
= 0; i
< h
; i
++) {
1746 bitmap_set(vs
->dirty
[y_position
+ i
], 0, width
);
1747 bitmap_clear(vs
->dirty
[y_position
+ i
], width
,
1748 VNC_DIRTY_BITS
- width
);
1753 static void send_ext_key_event_ack(VncState
*vs
)
1755 vnc_lock_output(vs
);
1756 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1757 vnc_write_u8(vs
, 0);
1758 vnc_write_u16(vs
, 1);
1759 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1760 VNC_ENCODING_EXT_KEY_EVENT
);
1761 vnc_unlock_output(vs
);
1765 static void send_ext_audio_ack(VncState
*vs
)
1767 vnc_lock_output(vs
);
1768 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1769 vnc_write_u8(vs
, 0);
1770 vnc_write_u16(vs
, 1);
1771 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
), ds_get_height(vs
->ds
),
1772 VNC_ENCODING_AUDIO
);
1773 vnc_unlock_output(vs
);
1777 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1780 unsigned int enc
= 0;
1783 vs
->vnc_encoding
= 0;
1784 vs
->tight
.compression
= 9;
1785 vs
->tight
.quality
= -1; /* Lossless by default */
1789 * Start from the end because the encodings are sent in order of preference.
1790 * This way the preferred encoding (first encoding defined in the array)
1791 * will be set at the end of the loop.
1793 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1796 case VNC_ENCODING_RAW
:
1797 vs
->vnc_encoding
= enc
;
1799 case VNC_ENCODING_COPYRECT
:
1800 vs
->features
|= VNC_FEATURE_COPYRECT_MASK
;
1802 case VNC_ENCODING_HEXTILE
:
1803 vs
->features
|= VNC_FEATURE_HEXTILE_MASK
;
1804 vs
->vnc_encoding
= enc
;
1806 case VNC_ENCODING_TIGHT
:
1807 vs
->features
|= VNC_FEATURE_TIGHT_MASK
;
1808 vs
->vnc_encoding
= enc
;
1810 case VNC_ENCODING_TIGHT_PNG
:
1811 vs
->features
|= VNC_FEATURE_TIGHT_PNG_MASK
;
1812 vs
->vnc_encoding
= enc
;
1814 case VNC_ENCODING_ZLIB
:
1815 vs
->features
|= VNC_FEATURE_ZLIB_MASK
;
1816 vs
->vnc_encoding
= enc
;
1818 case VNC_ENCODING_ZRLE
:
1819 vs
->features
|= VNC_FEATURE_ZRLE_MASK
;
1820 vs
->vnc_encoding
= enc
;
1822 case VNC_ENCODING_ZYWRLE
:
1823 vs
->features
|= VNC_FEATURE_ZYWRLE_MASK
;
1824 vs
->vnc_encoding
= enc
;
1826 case VNC_ENCODING_DESKTOPRESIZE
:
1827 vs
->features
|= VNC_FEATURE_RESIZE_MASK
;
1829 case VNC_ENCODING_POINTER_TYPE_CHANGE
:
1830 vs
->features
|= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK
;
1832 case VNC_ENCODING_RICH_CURSOR
:
1833 vs
->features
|= VNC_FEATURE_RICH_CURSOR_MASK
;
1835 case VNC_ENCODING_EXT_KEY_EVENT
:
1836 send_ext_key_event_ack(vs
);
1838 case VNC_ENCODING_AUDIO
:
1839 send_ext_audio_ack(vs
);
1841 case VNC_ENCODING_WMVi
:
1842 vs
->features
|= VNC_FEATURE_WMVI_MASK
;
1844 case VNC_ENCODING_COMPRESSLEVEL0
... VNC_ENCODING_COMPRESSLEVEL0
+ 9:
1845 vs
->tight
.compression
= (enc
& 0x0F);
1847 case VNC_ENCODING_QUALITYLEVEL0
... VNC_ENCODING_QUALITYLEVEL0
+ 9:
1848 if (vs
->vd
->lossy
) {
1849 vs
->tight
.quality
= (enc
& 0x0F);
1853 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i
, enc
, enc
);
1857 vnc_desktop_resize(vs
);
1858 check_pointer_type_change(&vs
->mouse_mode_notifier
, NULL
);
1861 static void set_pixel_conversion(VncState
*vs
)
1863 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
1864 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
) &&
1865 !memcmp(&(vs
->clientds
.pf
), &(vs
->ds
->surface
->pf
), sizeof(PixelFormat
))) {
1866 vs
->write_pixels
= vnc_write_pixels_copy
;
1867 vnc_hextile_set_pixel_conversion(vs
, 0);
1869 vs
->write_pixels
= vnc_write_pixels_generic
;
1870 vnc_hextile_set_pixel_conversion(vs
, 1);
1874 static void set_pixel_format(VncState
*vs
,
1875 int bits_per_pixel
, int depth
,
1876 int big_endian_flag
, int true_color_flag
,
1877 int red_max
, int green_max
, int blue_max
,
1878 int red_shift
, int green_shift
, int blue_shift
)
1880 if (!true_color_flag
) {
1881 vnc_client_error(vs
);
1885 vs
->clientds
= *(vs
->vd
->guest
.ds
);
1886 vs
->clientds
.pf
.rmax
= red_max
;
1887 vs
->clientds
.pf
.rbits
= hweight_long(red_max
);
1888 vs
->clientds
.pf
.rshift
= red_shift
;
1889 vs
->clientds
.pf
.rmask
= red_max
<< red_shift
;
1890 vs
->clientds
.pf
.gmax
= green_max
;
1891 vs
->clientds
.pf
.gbits
= hweight_long(green_max
);
1892 vs
->clientds
.pf
.gshift
= green_shift
;
1893 vs
->clientds
.pf
.gmask
= green_max
<< green_shift
;
1894 vs
->clientds
.pf
.bmax
= blue_max
;
1895 vs
->clientds
.pf
.bbits
= hweight_long(blue_max
);
1896 vs
->clientds
.pf
.bshift
= blue_shift
;
1897 vs
->clientds
.pf
.bmask
= blue_max
<< blue_shift
;
1898 vs
->clientds
.pf
.bits_per_pixel
= bits_per_pixel
;
1899 vs
->clientds
.pf
.bytes_per_pixel
= bits_per_pixel
/ 8;
1900 vs
->clientds
.pf
.depth
= bits_per_pixel
== 32 ? 24 : bits_per_pixel
;
1901 vs
->clientds
.flags
= big_endian_flag
? QEMU_BIG_ENDIAN_FLAG
: 0x00;
1903 set_pixel_conversion(vs
);
1905 vga_hw_invalidate();
1909 static void pixel_format_message (VncState
*vs
) {
1910 char pad
[3] = { 0, 0, 0 };
1912 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bits_per_pixel
); /* bits-per-pixel */
1913 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.depth
); /* depth */
1915 #ifdef HOST_WORDS_BIGENDIAN
1916 vnc_write_u8(vs
, 1); /* big-endian-flag */
1918 vnc_write_u8(vs
, 0); /* big-endian-flag */
1920 vnc_write_u8(vs
, 1); /* true-color-flag */
1921 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.rmax
); /* red-max */
1922 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.gmax
); /* green-max */
1923 vnc_write_u16(vs
, vs
->ds
->surface
->pf
.bmax
); /* blue-max */
1924 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.rshift
); /* red-shift */
1925 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.gshift
); /* green-shift */
1926 vnc_write_u8(vs
, vs
->ds
->surface
->pf
.bshift
); /* blue-shift */
1928 vnc_hextile_set_pixel_conversion(vs
, 0);
1930 vs
->clientds
= *(vs
->ds
->surface
);
1931 vs
->clientds
.flags
&= ~QEMU_ALLOCATED_FLAG
;
1932 vs
->write_pixels
= vnc_write_pixels_copy
;
1934 vnc_write(vs
, pad
, 3); /* padding */
1937 static void vnc_dpy_setdata(DisplayState
*ds
)
1939 VncDisplay
*vd
= ds
->opaque
;
1941 *(vd
->guest
.ds
) = *(ds
->surface
);
1942 vnc_dpy_update(ds
, 0, 0, ds_get_width(ds
), ds_get_height(ds
));
1945 static void vnc_colordepth(VncState
*vs
)
1947 if (vnc_has_feature(vs
, VNC_FEATURE_WMVI
)) {
1948 /* Sending a WMVi message to notify the client*/
1949 vnc_lock_output(vs
);
1950 vnc_write_u8(vs
, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE
);
1951 vnc_write_u8(vs
, 0);
1952 vnc_write_u16(vs
, 1); /* number of rects */
1953 vnc_framebuffer_update(vs
, 0, 0, ds_get_width(vs
->ds
),
1954 ds_get_height(vs
->ds
), VNC_ENCODING_WMVi
);
1955 pixel_format_message(vs
);
1956 vnc_unlock_output(vs
);
1959 set_pixel_conversion(vs
);
1963 static int protocol_client_msg(VncState
*vs
, uint8_t *data
, size_t len
)
1967 VncDisplay
*vd
= vs
->vd
;
1970 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
1971 if (!qemu_timer_expired(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
))
1972 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
1976 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT
:
1980 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1981 read_u8(data
, 6), read_u8(data
, 7),
1982 read_u16(data
, 8), read_u16(data
, 10),
1983 read_u16(data
, 12), read_u8(data
, 14),
1984 read_u8(data
, 15), read_u8(data
, 16));
1986 case VNC_MSG_CLIENT_SET_ENCODINGS
:
1991 limit
= read_u16(data
, 2);
1993 return 4 + (limit
* 4);
1995 limit
= read_u16(data
, 2);
1997 for (i
= 0; i
< limit
; i
++) {
1998 int32_t val
= read_s32(data
, 4 + (i
* 4));
1999 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
2002 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
2004 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST
:
2008 framebuffer_update_request(vs
,
2009 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
2010 read_u16(data
, 6), read_u16(data
, 8));
2012 case VNC_MSG_CLIENT_KEY_EVENT
:
2016 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
2018 case VNC_MSG_CLIENT_POINTER_EVENT
:
2022 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
2024 case VNC_MSG_CLIENT_CUT_TEXT
:
2029 uint32_t dlen
= read_u32(data
, 4);
2034 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
2036 case VNC_MSG_CLIENT_QEMU
:
2040 switch (read_u8(data
, 1)) {
2041 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT
:
2045 ext_key_event(vs
, read_u16(data
, 2),
2046 read_u32(data
, 4), read_u32(data
, 8));
2048 case VNC_MSG_CLIENT_QEMU_AUDIO
:
2052 switch (read_u16 (data
, 2)) {
2053 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE
:
2056 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE
:
2059 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT
:
2062 switch (read_u8(data
, 4)) {
2063 case 0: vs
->as
.fmt
= AUD_FMT_U8
; break;
2064 case 1: vs
->as
.fmt
= AUD_FMT_S8
; break;
2065 case 2: vs
->as
.fmt
= AUD_FMT_U16
; break;
2066 case 3: vs
->as
.fmt
= AUD_FMT_S16
; break;
2067 case 4: vs
->as
.fmt
= AUD_FMT_U32
; break;
2068 case 5: vs
->as
.fmt
= AUD_FMT_S32
; break;
2070 printf("Invalid audio format %d\n", read_u8(data
, 4));
2071 vnc_client_error(vs
);
2074 vs
->as
.nchannels
= read_u8(data
, 5);
2075 if (vs
->as
.nchannels
!= 1 && vs
->as
.nchannels
!= 2) {
2076 printf("Invalid audio channel coount %d\n",
2078 vnc_client_error(vs
);
2081 vs
->as
.freq
= read_u32(data
, 6);
2084 printf ("Invalid audio message %d\n", read_u8(data
, 4));
2085 vnc_client_error(vs
);
2091 printf("Msg: %d\n", read_u16(data
, 0));
2092 vnc_client_error(vs
);
2097 printf("Msg: %d\n", data
[0]);
2098 vnc_client_error(vs
);
2102 vnc_read_when(vs
, protocol_client_msg
, 1);
2106 static int protocol_client_init(VncState
*vs
, uint8_t *data
, size_t len
)
2112 mode
= data
[0] ? VNC_SHARE_MODE_SHARED
: VNC_SHARE_MODE_EXCLUSIVE
;
2113 switch (vs
->vd
->share_policy
) {
2114 case VNC_SHARE_POLICY_IGNORE
:
2116 * Ignore the shared flag. Nothing to do here.
2118 * Doesn't conform to the rfb spec but is traditional qemu
2119 * behavior, thus left here as option for compatibility
2123 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
:
2125 * Policy: Allow clients ask for exclusive access.
2127 * Implementation: When a client asks for exclusive access,
2128 * disconnect all others. Shared connects are allowed as long
2129 * as no exclusive connection exists.
2131 * This is how the rfb spec suggests to handle the shared flag.
2133 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2135 QTAILQ_FOREACH(client
, &vs
->vd
->clients
, next
) {
2139 if (client
->share_mode
!= VNC_SHARE_MODE_EXCLUSIVE
&&
2140 client
->share_mode
!= VNC_SHARE_MODE_SHARED
) {
2143 vnc_disconnect_start(client
);
2146 if (mode
== VNC_SHARE_MODE_SHARED
) {
2147 if (vs
->vd
->num_exclusive
> 0) {
2148 vnc_disconnect_start(vs
);
2153 case VNC_SHARE_POLICY_FORCE_SHARED
:
2155 * Policy: Shared connects only.
2156 * Implementation: Disallow clients asking for exclusive access.
2158 * Useful for shared desktop sessions where you don't want
2159 * someone forgetting to say -shared when running the vnc
2160 * client disconnect everybody else.
2162 if (mode
== VNC_SHARE_MODE_EXCLUSIVE
) {
2163 vnc_disconnect_start(vs
);
2168 vnc_set_share_mode(vs
, mode
);
2170 vs
->client_width
= ds_get_width(vs
->ds
);
2171 vs
->client_height
= ds_get_height(vs
->ds
);
2172 vnc_write_u16(vs
, vs
->client_width
);
2173 vnc_write_u16(vs
, vs
->client_height
);
2175 pixel_format_message(vs
);
2178 size
= snprintf(buf
, sizeof(buf
), "QEMU (%s)", qemu_name
);
2180 size
= snprintf(buf
, sizeof(buf
), "QEMU");
2182 vnc_write_u32(vs
, size
);
2183 vnc_write(vs
, buf
, size
);
2186 vnc_client_cache_auth(vs
);
2187 vnc_qmp_event(vs
, QEVENT_VNC_INITIALIZED
);
2189 vnc_read_when(vs
, protocol_client_msg
, 1);
2194 void start_client_init(VncState
*vs
)
2196 vnc_read_when(vs
, protocol_client_init
, 1);
2199 static void make_challenge(VncState
*vs
)
2203 srand(time(NULL
)+getpid()+getpid()*987654+rand());
2205 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
2206 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
2209 static int protocol_client_auth_vnc(VncState
*vs
, uint8_t *data
, size_t len
)
2211 unsigned char response
[VNC_AUTH_CHALLENGE_SIZE
];
2213 unsigned char key
[8];
2214 time_t now
= time(NULL
);
2216 if (!vs
->vd
->password
) {
2217 VNC_DEBUG("No password configured on server");
2220 if (vs
->vd
->expires
< now
) {
2221 VNC_DEBUG("Password is expired");
2225 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
2227 /* Calculate the expected challenge response */
2228 pwlen
= strlen(vs
->vd
->password
);
2229 for (i
=0; i
<sizeof(key
); i
++)
2230 key
[i
] = i
<pwlen
? vs
->vd
->password
[i
] : 0;
2232 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
2233 des(response
+j
, response
+j
);
2235 /* Compare expected vs actual challenge response */
2236 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
2237 VNC_DEBUG("Client challenge response did not match\n");
2240 VNC_DEBUG("Accepting VNC challenge response\n");
2241 vnc_write_u32(vs
, 0); /* Accept auth */
2244 start_client_init(vs
);
2249 vnc_write_u32(vs
, 1); /* Reject auth */
2250 if (vs
->minor
>= 8) {
2251 static const char err
[] = "Authentication failed";
2252 vnc_write_u32(vs
, sizeof(err
));
2253 vnc_write(vs
, err
, sizeof(err
));
2256 vnc_client_error(vs
);
2260 void start_auth_vnc(VncState
*vs
)
2263 /* Send client a 'random' challenge */
2264 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
2267 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
2271 static int protocol_client_auth(VncState
*vs
, uint8_t *data
, size_t len
)
2273 /* We only advertise 1 auth scheme at a time, so client
2274 * must pick the one we sent. Verify this */
2275 if (data
[0] != vs
->auth
) { /* Reject auth */
2276 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data
[0]);
2277 vnc_write_u32(vs
, 1);
2278 if (vs
->minor
>= 8) {
2279 static const char err
[] = "Authentication failed";
2280 vnc_write_u32(vs
, sizeof(err
));
2281 vnc_write(vs
, err
, sizeof(err
));
2283 vnc_client_error(vs
);
2284 } else { /* Accept requested auth */
2285 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
2288 VNC_DEBUG("Accept auth none\n");
2289 if (vs
->minor
>= 8) {
2290 vnc_write_u32(vs
, 0); /* Accept auth completion */
2293 start_client_init(vs
);
2297 VNC_DEBUG("Start VNC auth\n");
2301 #ifdef CONFIG_VNC_TLS
2302 case VNC_AUTH_VENCRYPT
:
2303 VNC_DEBUG("Accept VeNCrypt auth\n");
2304 start_auth_vencrypt(vs
);
2306 #endif /* CONFIG_VNC_TLS */
2308 #ifdef CONFIG_VNC_SASL
2310 VNC_DEBUG("Accept SASL auth\n");
2311 start_auth_sasl(vs
);
2313 #endif /* CONFIG_VNC_SASL */
2315 default: /* Should not be possible, but just in case */
2316 VNC_DEBUG("Reject auth %d server code bug\n", vs
->auth
);
2317 vnc_write_u8(vs
, 1);
2318 if (vs
->minor
>= 8) {
2319 static const char err
[] = "Authentication failed";
2320 vnc_write_u32(vs
, sizeof(err
));
2321 vnc_write(vs
, err
, sizeof(err
));
2323 vnc_client_error(vs
);
2329 static int protocol_version(VncState
*vs
, uint8_t *version
, size_t len
)
2333 memcpy(local
, version
, 12);
2336 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
2337 VNC_DEBUG("Malformed protocol version %s\n", local
);
2338 vnc_client_error(vs
);
2341 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
2342 if (vs
->major
!= 3 ||
2348 VNC_DEBUG("Unsupported client version\n");
2349 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2351 vnc_client_error(vs
);
2354 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
2355 * as equivalent to v3.3 by servers
2357 if (vs
->minor
== 4 || vs
->minor
== 5)
2360 if (vs
->minor
== 3) {
2361 if (vs
->auth
== VNC_AUTH_NONE
) {
2362 VNC_DEBUG("Tell client auth none\n");
2363 vnc_write_u32(vs
, vs
->auth
);
2365 start_client_init(vs
);
2366 } else if (vs
->auth
== VNC_AUTH_VNC
) {
2367 VNC_DEBUG("Tell client VNC auth\n");
2368 vnc_write_u32(vs
, vs
->auth
);
2372 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->auth
);
2373 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
2375 vnc_client_error(vs
);
2378 VNC_DEBUG("Telling client we support auth %d\n", vs
->auth
);
2379 vnc_write_u8(vs
, 1); /* num auth */
2380 vnc_write_u8(vs
, vs
->auth
);
2381 vnc_read_when(vs
, protocol_client_auth
, 1);
2388 static VncRectStat
*vnc_stat_rect(VncDisplay
*vd
, int x
, int y
)
2390 struct VncSurface
*vs
= &vd
->guest
;
2392 return &vs
->stats
[y
/ VNC_STAT_RECT
][x
/ VNC_STAT_RECT
];
2395 void vnc_sent_lossy_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
2399 w
= (x
+ w
) / VNC_STAT_RECT
;
2400 h
= (y
+ h
) / VNC_STAT_RECT
;
2404 for (j
= y
; j
<= h
; j
++) {
2405 for (i
= x
; i
<= w
; i
++) {
2406 vs
->lossy_rect
[j
][i
] = 1;
2411 static int vnc_refresh_lossy_rect(VncDisplay
*vd
, int x
, int y
)
2414 int sty
= y
/ VNC_STAT_RECT
;
2415 int stx
= x
/ VNC_STAT_RECT
;
2418 y
= y
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2419 x
= x
/ VNC_STAT_RECT
* VNC_STAT_RECT
;
2421 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2424 /* kernel send buffers are full -> refresh later */
2425 if (vs
->output
.offset
) {
2429 if (!vs
->lossy_rect
[sty
][stx
]) {
2433 vs
->lossy_rect
[sty
][stx
] = 0;
2434 for (j
= 0; j
< VNC_STAT_RECT
; ++j
) {
2435 bitmap_set(vs
->dirty
[y
+ j
], x
/ 16, VNC_STAT_RECT
/ 16);
2443 static int vnc_update_stats(VncDisplay
*vd
, struct timeval
* tv
)
2449 for (y
= 0; y
< vd
->guest
.ds
->height
; y
+= VNC_STAT_RECT
) {
2450 for (x
= 0; x
< vd
->guest
.ds
->width
; x
+= VNC_STAT_RECT
) {
2451 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2453 rect
->updated
= false;
2457 qemu_timersub(tv
, &VNC_REFRESH_STATS
, &res
);
2459 if (timercmp(&vd
->guest
.last_freq_check
, &res
, >)) {
2462 vd
->guest
.last_freq_check
= *tv
;
2464 for (y
= 0; y
< vd
->guest
.ds
->height
; y
+= VNC_STAT_RECT
) {
2465 for (x
= 0; x
< vd
->guest
.ds
->width
; x
+= VNC_STAT_RECT
) {
2466 VncRectStat
*rect
= vnc_stat_rect(vd
, x
, y
);
2467 int count
= ARRAY_SIZE(rect
->times
);
2468 struct timeval min
, max
;
2470 if (!timerisset(&rect
->times
[count
- 1])) {
2474 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2475 qemu_timersub(tv
, &max
, &res
);
2477 if (timercmp(&res
, &VNC_REFRESH_LOSSY
, >)) {
2479 has_dirty
+= vnc_refresh_lossy_rect(vd
, x
, y
);
2480 memset(rect
->times
, 0, sizeof (rect
->times
));
2484 min
= rect
->times
[rect
->idx
];
2485 max
= rect
->times
[(rect
->idx
+ count
- 1) % count
];
2486 qemu_timersub(&max
, &min
, &res
);
2488 rect
->freq
= res
.tv_sec
+ res
.tv_usec
/ 1000000.;
2489 rect
->freq
/= count
;
2490 rect
->freq
= 1. / rect
->freq
;
2496 double vnc_update_freq(VncState
*vs
, int x
, int y
, int w
, int h
)
2502 x
= (x
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2503 y
= (y
/ VNC_STAT_RECT
) * VNC_STAT_RECT
;
2505 for (j
= y
; j
<= y
+ h
; j
+= VNC_STAT_RECT
) {
2506 for (i
= x
; i
<= x
+ w
; i
+= VNC_STAT_RECT
) {
2507 total
+= vnc_stat_rect(vs
->vd
, i
, j
)->freq
;
2519 static void vnc_rect_updated(VncDisplay
*vd
, int x
, int y
, struct timeval
* tv
)
2523 rect
= vnc_stat_rect(vd
, x
, y
);
2524 if (rect
->updated
) {
2527 rect
->times
[rect
->idx
] = *tv
;
2528 rect
->idx
= (rect
->idx
+ 1) % ARRAY_SIZE(rect
->times
);
2529 rect
->updated
= true;
2532 static int vnc_refresh_server_surface(VncDisplay
*vd
)
2536 uint8_t *server_row
;
2541 struct timeval tv
= { 0, 0 };
2543 if (!vd
->non_adaptive
) {
2544 gettimeofday(&tv
, NULL
);
2545 has_dirty
= vnc_update_stats(vd
, &tv
);
2549 * Walk through the guest dirty map.
2550 * Check and copy modified bits from guest to server surface.
2551 * Update server dirty map.
2553 cmp_bytes
= 16 * ds_get_bytes_per_pixel(vd
->ds
);
2554 guest_row
= vd
->guest
.ds
->data
;
2555 server_row
= vd
->server
->data
;
2556 for (y
= 0; y
< vd
->guest
.ds
->height
; y
++) {
2557 if (!bitmap_empty(vd
->guest
.dirty
[y
], VNC_DIRTY_BITS
)) {
2560 uint8_t *server_ptr
;
2562 guest_ptr
= guest_row
;
2563 server_ptr
= server_row
;
2565 for (x
= 0; x
+ 15 < vd
->guest
.ds
->width
;
2566 x
+= 16, guest_ptr
+= cmp_bytes
, server_ptr
+= cmp_bytes
) {
2567 if (!test_and_clear_bit((x
/ 16), vd
->guest
.dirty
[y
]))
2569 if (memcmp(server_ptr
, guest_ptr
, cmp_bytes
) == 0)
2571 memcpy(server_ptr
, guest_ptr
, cmp_bytes
);
2572 if (!vd
->non_adaptive
)
2573 vnc_rect_updated(vd
, x
, y
, &tv
);
2574 QTAILQ_FOREACH(vs
, &vd
->clients
, next
) {
2575 set_bit((x
/ 16), vs
->dirty
[y
]);
2580 guest_row
+= ds_get_linesize(vd
->ds
);
2581 server_row
+= ds_get_linesize(vd
->ds
);
2586 static void vnc_refresh(void *opaque
)
2588 VncDisplay
*vd
= opaque
;
2590 int has_dirty
, rects
= 0;
2594 if (vnc_trylock_display(vd
)) {
2595 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2596 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) +
2597 vd
->timer_interval
);
2601 has_dirty
= vnc_refresh_server_surface(vd
);
2602 vnc_unlock_display(vd
);
2604 QTAILQ_FOREACH_SAFE(vs
, &vd
->clients
, next
, vn
) {
2605 rects
+= vnc_update_client(vs
, has_dirty
);
2606 /* vs might be free()ed here */
2609 /* vd->timer could be NULL now if the last client disconnected,
2610 * in this case don't update the timer */
2611 if (vd
->timer
== NULL
)
2614 if (has_dirty
&& rects
) {
2615 vd
->timer_interval
/= 2;
2616 if (vd
->timer_interval
< VNC_REFRESH_INTERVAL_BASE
)
2617 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2619 vd
->timer_interval
+= VNC_REFRESH_INTERVAL_INC
;
2620 if (vd
->timer_interval
> VNC_REFRESH_INTERVAL_MAX
)
2621 vd
->timer_interval
= VNC_REFRESH_INTERVAL_MAX
;
2623 qemu_mod_timer(vd
->timer
, qemu_get_clock_ms(rt_clock
) + vd
->timer_interval
);
2626 static void vnc_init_timer(VncDisplay
*vd
)
2628 vd
->timer_interval
= VNC_REFRESH_INTERVAL_BASE
;
2629 if (vd
->timer
== NULL
&& !QTAILQ_EMPTY(&vd
->clients
)) {
2630 vd
->timer
= qemu_new_timer_ms(rt_clock
, vnc_refresh
, vd
);
2631 vnc_dpy_resize(vd
->ds
);
2636 static void vnc_remove_timer(VncDisplay
*vd
)
2638 if (vd
->timer
!= NULL
&& QTAILQ_EMPTY(&vd
->clients
)) {
2639 qemu_del_timer(vd
->timer
);
2640 qemu_free_timer(vd
->timer
);
2645 static void vnc_connect(VncDisplay
*vd
, int csock
, int skipauth
)
2647 VncState
*vs
= g_malloc0(sizeof(VncState
));
2653 vs
->auth
= VNC_AUTH_NONE
;
2654 #ifdef CONFIG_VNC_TLS
2655 vs
->subauth
= VNC_AUTH_INVALID
;
2658 vs
->auth
= vd
->auth
;
2659 #ifdef CONFIG_VNC_TLS
2660 vs
->subauth
= vd
->subauth
;
2664 vs
->lossy_rect
= g_malloc0(VNC_STAT_ROWS
* sizeof (*vs
->lossy_rect
));
2665 for (i
= 0; i
< VNC_STAT_ROWS
; ++i
) {
2666 vs
->lossy_rect
[i
] = g_malloc0(VNC_STAT_COLS
* sizeof (uint8_t));
2669 VNC_DEBUG("New client on socket %d\n", csock
);
2671 socket_set_nonblock(vs
->csock
);
2672 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
2674 vnc_client_cache_addr(vs
);
2675 vnc_qmp_event(vs
, QEVENT_VNC_CONNECTED
);
2676 vnc_set_share_mode(vs
, VNC_SHARE_MODE_CONNECTING
);
2683 vs
->as
.freq
= 44100;
2684 vs
->as
.nchannels
= 2;
2685 vs
->as
.fmt
= AUD_FMT_S16
;
2686 vs
->as
.endianness
= 0;
2688 #ifdef CONFIG_VNC_THREAD
2689 qemu_mutex_init(&vs
->output_mutex
);
2692 QTAILQ_INSERT_HEAD(&vd
->clients
, vs
, next
);
2696 vnc_write(vs
, "RFB 003.008\n", 12);
2698 vnc_read_when(vs
, protocol_version
, 12);
2700 if (vs
->vd
->lock_key_sync
)
2701 vs
->led
= qemu_add_led_event_handler(kbd_leds
, vs
);
2703 vs
->mouse_mode_notifier
.notify
= check_pointer_type_change
;
2704 qemu_add_mouse_mode_change_notifier(&vs
->mouse_mode_notifier
);
2708 /* vs might be free()ed here */
2711 static void vnc_listen_read(void *opaque
)
2713 VncDisplay
*vs
= opaque
;
2714 struct sockaddr_in addr
;
2715 socklen_t addrlen
= sizeof(addr
);
2720 int csock
= qemu_accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
2722 vnc_connect(vs
, csock
, 0);
2726 void vnc_display_init(DisplayState
*ds
)
2728 VncDisplay
*vs
= g_malloc0(sizeof(*vs
));
2730 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
2739 QTAILQ_INIT(&vs
->clients
);
2740 vs
->expires
= TIME_MAX
;
2742 if (keyboard_layout
)
2743 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
2745 vs
->kbd_layout
= init_keyboard_layout(name2keysym
, "en-us");
2747 if (!vs
->kbd_layout
)
2750 #ifdef CONFIG_VNC_THREAD
2751 qemu_mutex_init(&vs
->mutex
);
2752 vnc_start_worker_thread();
2755 dcl
->dpy_copy
= vnc_dpy_copy
;
2756 dcl
->dpy_update
= vnc_dpy_update
;
2757 dcl
->dpy_resize
= vnc_dpy_resize
;
2758 dcl
->dpy_setdata
= vnc_dpy_setdata
;
2759 register_displaychangelistener(ds
, dcl
);
2760 ds
->mouse_set
= vnc_mouse_set
;
2761 ds
->cursor_define
= vnc_dpy_cursor_define
;
2765 void vnc_display_close(DisplayState
*ds
)
2767 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2772 g_free(vs
->display
);
2775 if (vs
->lsock
!= -1) {
2776 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2780 vs
->auth
= VNC_AUTH_INVALID
;
2781 #ifdef CONFIG_VNC_TLS
2782 vs
->subauth
= VNC_AUTH_INVALID
;
2783 vs
->tls
.x509verify
= 0;
2787 int vnc_display_disable_login(DisplayState
*ds
)
2789 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2796 g_free(vs
->password
);
2799 vs
->password
= NULL
;
2800 if (vs
->auth
== VNC_AUTH_NONE
) {
2801 vs
->auth
= VNC_AUTH_VNC
;
2807 int vnc_display_password(DisplayState
*ds
, const char *password
)
2809 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2816 /* This is not the intention of this interface but err on the side
2818 return vnc_display_disable_login(ds
);
2822 g_free(vs
->password
);
2823 vs
->password
= NULL
;
2825 vs
->password
= g_strdup(password
);
2826 if (vs
->auth
== VNC_AUTH_NONE
) {
2827 vs
->auth
= VNC_AUTH_VNC
;
2833 int vnc_display_pw_expire(DisplayState
*ds
, time_t expires
)
2835 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2837 vs
->expires
= expires
;
2841 char *vnc_display_local_addr(DisplayState
*ds
)
2843 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2845 return vnc_socket_local_addr("%s:%s", vs
->lsock
);
2848 int vnc_display_open(DisplayState
*ds
, const char *display
)
2850 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
2851 const char *options
;
2854 #ifdef CONFIG_VNC_TLS
2855 int tls
= 0, x509
= 0;
2857 #ifdef CONFIG_VNC_SASL
2861 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2864 int lock_key_sync
= 1;
2868 vnc_display_close(ds
);
2869 if (strcmp(display
, "none") == 0)
2872 if (!(vs
->display
= strdup(display
)))
2874 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
2877 while ((options
= strchr(options
, ','))) {
2879 if (strncmp(options
, "password", 8) == 0) {
2880 password
= 1; /* Require password auth */
2881 } else if (strncmp(options
, "reverse", 7) == 0) {
2883 } else if (strncmp(options
, "no-lock-key-sync", 16) == 0) {
2885 #ifdef CONFIG_VNC_SASL
2886 } else if (strncmp(options
, "sasl", 4) == 0) {
2887 sasl
= 1; /* Require SASL auth */
2889 #ifdef CONFIG_VNC_TLS
2890 } else if (strncmp(options
, "tls", 3) == 0) {
2891 tls
= 1; /* Require TLS */
2892 } else if (strncmp(options
, "x509", 4) == 0) {
2894 x509
= 1; /* Require x509 certificates */
2895 if (strncmp(options
, "x509verify", 10) == 0)
2896 vs
->tls
.x509verify
= 1; /* ...and verify client certs */
2898 /* Now check for 'x509=/some/path' postfix
2899 * and use that to setup x509 certificate/key paths */
2900 start
= strchr(options
, '=');
2901 end
= strchr(options
, ',');
2902 if (start
&& (!end
|| (start
< end
))) {
2903 int len
= end
? end
-(start
+1) : strlen(start
+1);
2904 char *path
= g_strndup(start
+ 1, len
);
2906 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2907 if (vnc_tls_set_x509_creds_dir(vs
, path
) < 0) {
2908 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2910 g_free(vs
->display
);
2916 fprintf(stderr
, "No certificate path provided\n");
2917 g_free(vs
->display
);
2922 #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2923 } else if (strncmp(options
, "acl", 3) == 0) {
2926 } else if (strncmp(options
, "lossy", 5) == 0) {
2928 } else if (strncmp(options
, "non-adapative", 13) == 0) {
2929 vs
->non_adaptive
= true;
2930 } else if (strncmp(options
, "share=", 6) == 0) {
2931 if (strncmp(options
+6, "ignore", 6) == 0) {
2932 vs
->share_policy
= VNC_SHARE_POLICY_IGNORE
;
2933 } else if (strncmp(options
+6, "allow-exclusive", 15) == 0) {
2934 vs
->share_policy
= VNC_SHARE_POLICY_ALLOW_EXCLUSIVE
;
2935 } else if (strncmp(options
+6, "force-shared", 12) == 0) {
2936 vs
->share_policy
= VNC_SHARE_POLICY_FORCE_SHARED
;
2938 fprintf(stderr
, "unknown vnc share= option\n");
2939 g_free(vs
->display
);
2946 #ifdef CONFIG_VNC_TLS
2947 if (acl
&& x509
&& vs
->tls
.x509verify
) {
2948 if (!(vs
->tls
.acl
= qemu_acl_init("vnc.x509dname"))) {
2949 fprintf(stderr
, "Failed to create x509 dname ACL\n");
2954 #ifdef CONFIG_VNC_SASL
2956 if (!(vs
->sasl
.acl
= qemu_acl_init("vnc.username"))) {
2957 fprintf(stderr
, "Failed to create username ACL\n");
2964 * Combinations we support here:
2966 * - no-auth (clear text, no auth)
2967 * - password (clear text, weak auth)
2968 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2969 * - tls (encrypt, weak anonymous creds, no auth)
2970 * - tls + password (encrypt, weak anonymous creds, weak auth)
2971 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2972 * - tls + x509 (encrypt, good x509 creds, no auth)
2973 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2974 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2976 * NB1. TLS is a stackable auth scheme.
2977 * NB2. the x509 schemes have option to validate a client cert dname
2980 #ifdef CONFIG_VNC_TLS
2982 vs
->auth
= VNC_AUTH_VENCRYPT
;
2984 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2985 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2987 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2988 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2991 #endif /* CONFIG_VNC_TLS */
2992 VNC_DEBUG("Initializing VNC server with password auth\n");
2993 vs
->auth
= VNC_AUTH_VNC
;
2994 #ifdef CONFIG_VNC_TLS
2995 vs
->subauth
= VNC_AUTH_INVALID
;
2997 #endif /* CONFIG_VNC_TLS */
2998 #ifdef CONFIG_VNC_SASL
3000 #ifdef CONFIG_VNC_TLS
3002 vs
->auth
= VNC_AUTH_VENCRYPT
;
3004 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3005 vs
->subauth
= VNC_AUTH_VENCRYPT_X509SASL
;
3007 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3008 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSSASL
;
3011 #endif /* CONFIG_VNC_TLS */
3012 VNC_DEBUG("Initializing VNC server with SASL auth\n");
3013 vs
->auth
= VNC_AUTH_SASL
;
3014 #ifdef CONFIG_VNC_TLS
3015 vs
->subauth
= VNC_AUTH_INVALID
;
3017 #endif /* CONFIG_VNC_TLS */
3018 #endif /* CONFIG_VNC_SASL */
3020 #ifdef CONFIG_VNC_TLS
3022 vs
->auth
= VNC_AUTH_VENCRYPT
;
3024 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3025 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
3027 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3028 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
3032 VNC_DEBUG("Initializing VNC server with no auth\n");
3033 vs
->auth
= VNC_AUTH_NONE
;
3034 #ifdef CONFIG_VNC_TLS
3035 vs
->subauth
= VNC_AUTH_INVALID
;
3040 #ifdef CONFIG_VNC_SASL
3041 if ((saslErr
= sasl_server_init(NULL
, "qemu")) != SASL_OK
) {
3042 fprintf(stderr
, "Failed to initialize SASL auth %s",
3043 sasl_errstring(saslErr
, NULL
, NULL
));
3044 g_free(vs
->display
);
3049 vs
->lock_key_sync
= lock_key_sync
;
3052 /* connect to viewer */
3053 if (strncmp(display
, "unix:", 5) == 0)
3054 vs
->lsock
= unix_connect(display
+5);
3056 vs
->lsock
= inet_connect(display
, SOCK_STREAM
);
3057 if (-1 == vs
->lsock
) {
3058 g_free(vs
->display
);
3062 int csock
= vs
->lsock
;
3064 vnc_connect(vs
, csock
, 0);
3069 /* listen for connects */
3071 dpy
= g_malloc(256);
3072 if (strncmp(display
, "unix:", 5) == 0) {
3073 pstrcpy(dpy
, 256, "unix:");
3074 vs
->lsock
= unix_listen(display
+5, dpy
+5, 256-5);
3076 vs
->lsock
= inet_listen(display
, dpy
, 256, SOCK_STREAM
, 5900);
3078 if (-1 == vs
->lsock
) {
3082 g_free(vs
->display
);
3086 return qemu_set_fd_handler2(vs
->lsock
, NULL
, vnc_listen_read
, NULL
, vs
);
3089 void vnc_display_add_client(DisplayState
*ds
, int csock
, int skipauth
)
3091 VncDisplay
*vs
= ds
? (VncDisplay
*)ds
->opaque
: vnc_display
;
3093 return vnc_connect(vs
, csock
, skipauth
);