]> Git Repo - qemu.git/blame - ui/vnc.c
spice: don't use 'Yoda conditions'
[qemu.git] / ui / vnc.c
CommitLineData
7d510b8c
FB
1/*
2 * QEMU VNC display driver
5fafdf24 3 *
7d510b8c
FB
4 * Copyright (C) 2006 Anthony Liguori <[email protected]>
5 * Copyright (C) 2006 Fabrice Bellard
19a490bf 6 * Copyright (C) 2009 Red Hat, Inc
5fafdf24 7 *
7d510b8c
FB
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * THE SOFTWARE.
25 */
26
19a490bf 27#include "vnc.h"
bd023f95 28#include "vnc-jobs.h"
40066175 29#include "trace.h"
9c17d615 30#include "sysemu/sysemu.h"
1de7afc9
PB
31#include "qemu/sockets.h"
32#include "qemu/timer.h"
33#include "qemu/acl.h"
7b1b5d19 34#include "qapi/qmp/types.h"
2b54aa87 35#include "qmp-commands.h"
1de7afc9 36#include "qemu/osdep.h"
8d447d10 37#include "ui/input.h"
fb6ba0d5 38#include "qapi-event.h"
24236869 39
0f7b2864 40#define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
2430ffe4 41#define VNC_REFRESH_INTERVAL_INC 50
0f7b2864 42#define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
999342a0
CC
43static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
44static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
24236869
FB
45
46#include "vnc_keysym.h"
70848515
TS
47#include "d3des.h"
48
753b4053 49static VncDisplay *vnc_display; /* needed for info vnc */
a9ce8590 50
d467b679 51static int vnc_cursor_define(VncState *vs);
7bc9318b 52static void vnc_release_modifiers(VncState *vs);
d467b679 53
8cf36489
GH
54static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
55{
56#ifdef _VNC_DEBUG
57 static const char *mn[] = {
58 [0] = "undefined",
59 [VNC_SHARE_MODE_CONNECTING] = "connecting",
60 [VNC_SHARE_MODE_SHARED] = "shared",
61 [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive",
62 [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
63 };
64 fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
65 vs->csock, mn[vs->share_mode], mn[mode]);
66#endif
67
68 if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
69 vs->vd->num_exclusive--;
70 }
71 vs->share_mode = mode;
72 if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
73 vs->vd->num_exclusive++;
74 }
75}
76
1ff7df1a
AL
77static char *addr_to_string(const char *format,
78 struct sockaddr_storage *sa,
79 socklen_t salen) {
80 char *addr;
81 char host[NI_MAXHOST];
82 char serv[NI_MAXSERV];
83 int err;
457772e6 84 size_t addrlen;
1ff7df1a
AL
85
86 if ((err = getnameinfo((struct sockaddr *)sa, salen,
87 host, sizeof(host),
88 serv, sizeof(serv),
89 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
90 VNC_DEBUG("Cannot resolve address %d: %s\n",
91 err, gai_strerror(err));
92 return NULL;
93 }
94
457772e6 95 /* Enough for the existing format + the 2 vars we're
f425c278 96 * substituting in. */
457772e6 97 addrlen = strlen(format) + strlen(host) + strlen(serv);
7267c094 98 addr = g_malloc(addrlen + 1);
457772e6
AL
99 snprintf(addr, addrlen, format, host, serv);
100 addr[addrlen] = '\0';
1ff7df1a
AL
101
102 return addr;
103}
104
2f9606b3
AL
105
106char *vnc_socket_local_addr(const char *format, int fd) {
1ff7df1a
AL
107 struct sockaddr_storage sa;
108 socklen_t salen;
109
110 salen = sizeof(sa);
111 if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
112 return NULL;
113
114 return addr_to_string(format, &sa, salen);
115}
116
2f9606b3 117char *vnc_socket_remote_addr(const char *format, int fd) {
1ff7df1a
AL
118 struct sockaddr_storage sa;
119 socklen_t salen;
120
121 salen = sizeof(sa);
122 if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
123 return NULL;
124
125 return addr_to_string(format, &sa, salen);
126}
127
fb6ba0d5
WX
128static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
129 socklen_t salen)
d96fd29c 130{
fb6ba0d5 131 VncBasicInfo *info;
d96fd29c
LC
132 char host[NI_MAXHOST];
133 char serv[NI_MAXSERV];
134 int err;
135
136 if ((err = getnameinfo((struct sockaddr *)sa, salen,
137 host, sizeof(host),
138 serv, sizeof(serv),
139 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
140 VNC_DEBUG("Cannot resolve address %d: %s\n",
141 err, gai_strerror(err));
fb6ba0d5 142 return NULL;
d96fd29c
LC
143 }
144
fb6ba0d5
WX
145 info = g_malloc0(sizeof(VncBasicInfo));
146 info->host = g_strdup(host);
147 info->service = g_strdup(serv);
148 info->family = inet_netfamily(sa->ss_family);
149 return info;
d96fd29c
LC
150}
151
fb6ba0d5 152static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd)
d96fd29c
LC
153{
154 struct sockaddr_storage sa;
155 socklen_t salen;
156
157 salen = sizeof(sa);
158 if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
fb6ba0d5 159 return NULL;
d96fd29c
LC
160 }
161
fb6ba0d5 162 return vnc_basic_info_get(&sa, salen);
d96fd29c
LC
163}
164
fb6ba0d5 165static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd)
d96fd29c
LC
166{
167 struct sockaddr_storage sa;
168 socklen_t salen;
169
170 salen = sizeof(sa);
171 if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
fb6ba0d5 172 return NULL;
d96fd29c
LC
173 }
174
fb6ba0d5 175 return vnc_basic_info_get(&sa, salen);
d96fd29c
LC
176}
177
1ff7df1a
AL
178static const char *vnc_auth_name(VncDisplay *vd) {
179 switch (vd->auth) {
180 case VNC_AUTH_INVALID:
181 return "invalid";
182 case VNC_AUTH_NONE:
183 return "none";
184 case VNC_AUTH_VNC:
185 return "vnc";
186 case VNC_AUTH_RA2:
187 return "ra2";
188 case VNC_AUTH_RA2NE:
189 return "ra2ne";
190 case VNC_AUTH_TIGHT:
191 return "tight";
192 case VNC_AUTH_ULTRA:
193 return "ultra";
194 case VNC_AUTH_TLS:
195 return "tls";
196 case VNC_AUTH_VENCRYPT:
197#ifdef CONFIG_VNC_TLS
198 switch (vd->subauth) {
199 case VNC_AUTH_VENCRYPT_PLAIN:
200 return "vencrypt+plain";
201 case VNC_AUTH_VENCRYPT_TLSNONE:
202 return "vencrypt+tls+none";
203 case VNC_AUTH_VENCRYPT_TLSVNC:
204 return "vencrypt+tls+vnc";
205 case VNC_AUTH_VENCRYPT_TLSPLAIN:
206 return "vencrypt+tls+plain";
207 case VNC_AUTH_VENCRYPT_X509NONE:
208 return "vencrypt+x509+none";
209 case VNC_AUTH_VENCRYPT_X509VNC:
210 return "vencrypt+x509+vnc";
211 case VNC_AUTH_VENCRYPT_X509PLAIN:
212 return "vencrypt+x509+plain";
28a76be8
AL
213 case VNC_AUTH_VENCRYPT_TLSSASL:
214 return "vencrypt+tls+sasl";
215 case VNC_AUTH_VENCRYPT_X509SASL:
216 return "vencrypt+x509+sasl";
1ff7df1a
AL
217 default:
218 return "vencrypt";
219 }
220#else
221 return "vencrypt";
222#endif
2f9606b3 223 case VNC_AUTH_SASL:
28a76be8 224 return "sasl";
1ff7df1a
AL
225 }
226 return "unknown";
227}
228
fb6ba0d5 229static VncServerInfo *vnc_server_info_get(void)
a7789382 230{
fb6ba0d5
WX
231 VncServerInfo *info;
232 VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vnc_display->lsock);
233 if (!bi) {
234 return NULL;
a7789382
LC
235 }
236
fb6ba0d5
WX
237 info = g_malloc(sizeof(*info));
238 info->base = bi;
239 info->has_auth = true;
240 info->auth = g_strdup(vnc_auth_name(vnc_display));
241 return info;
a7789382
LC
242}
243
4a80dba3 244static void vnc_client_cache_auth(VncState *client)
1ff7df1a 245{
4a80dba3
LC
246 if (!client->info) {
247 return;
d96fd29c 248 }
1263b7d6
AL
249
250#ifdef CONFIG_VNC_TLS
251 if (client->tls.session &&
d96fd29c 252 client->tls.dname) {
fb6ba0d5
WX
253 client->info->has_x509_dname = true;
254 client->info->x509_dname = g_strdup(client->tls.dname);
d96fd29c 255 }
1263b7d6
AL
256#endif
257#ifdef CONFIG_VNC_SASL
258 if (client->sasl.conn &&
d96fd29c 259 client->sasl.username) {
fb6ba0d5
WX
260 client->info->has_sasl_username = true;
261 client->info->sasl_username = g_strdup(client->sasl.username);
d96fd29c 262 }
1263b7d6 263#endif
4a80dba3 264}
d96fd29c 265
4a80dba3
LC
266static void vnc_client_cache_addr(VncState *client)
267{
fb6ba0d5 268 VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock);
4a80dba3 269
fb6ba0d5
WX
270 if (bi) {
271 client->info = g_malloc0(sizeof(*client->info));
272 client->info->base = bi;
4a80dba3 273 }
1ff7df1a
AL
274}
275
fb6ba0d5 276static void vnc_qmp_event(VncState *vs, QAPIEvent event)
586153d9 277{
fb6ba0d5 278 VncServerInfo *si;
586153d9
LC
279
280 if (!vs->info) {
281 return;
282 }
fb6ba0d5 283 g_assert(vs->info->base);
586153d9 284
fb6ba0d5
WX
285 si = vnc_server_info_get();
286 if (!si) {
586153d9
LC
287 return;
288 }
289
fb6ba0d5
WX
290 switch (event) {
291 case QAPI_EVENT_VNC_CONNECTED:
292 qapi_event_send_vnc_connected(si, vs->info->base, &error_abort);
293 break;
294 case QAPI_EVENT_VNC_INITIALIZED:
295 qapi_event_send_vnc_initialized(si, vs->info, &error_abort);
296 break;
297 case QAPI_EVENT_VNC_DISCONNECTED:
298 qapi_event_send_vnc_disconnected(si, vs->info, &error_abort);
299 break;
300 default:
301 break;
302 }
586153d9 303
fb6ba0d5 304 qapi_free_VncServerInfo(si);
586153d9
LC
305}
306
2b54aa87 307static VncClientInfo *qmp_query_vnc_client(const VncState *client)
a9ce8590 308{
2b54aa87
LC
309 struct sockaddr_storage sa;
310 socklen_t salen = sizeof(sa);
311 char host[NI_MAXHOST];
312 char serv[NI_MAXSERV];
313 VncClientInfo *info;
314
315 if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
316 return NULL;
317 }
318
319 if (getnameinfo((struct sockaddr *)&sa, salen,
320 host, sizeof(host),
321 serv, sizeof(serv),
322 NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
323 return NULL;
324 }
d96fd29c 325
2b54aa87 326 info = g_malloc0(sizeof(*info));
a589569f
WX
327 info->base = g_malloc0(sizeof(*info->base));
328 info->base->host = g_strdup(host);
329 info->base->service = g_strdup(serv);
330 info->base->family = inet_netfamily(sa.ss_family);
d96fd29c
LC
331
332#ifdef CONFIG_VNC_TLS
2b54aa87
LC
333 if (client->tls.session && client->tls.dname) {
334 info->has_x509_dname = true;
335 info->x509_dname = g_strdup(client->tls.dname);
336 }
d96fd29c
LC
337#endif
338#ifdef CONFIG_VNC_SASL
2b54aa87
LC
339 if (client->sasl.conn && client->sasl.username) {
340 info->has_sasl_username = true;
341 info->sasl_username = g_strdup(client->sasl.username);
d96fd29c 342 }
2b54aa87 343#endif
1ff7df1a 344
2b54aa87 345 return info;
d96fd29c 346}
1ff7df1a 347
2b54aa87 348VncInfo *qmp_query_vnc(Error **errp)
d96fd29c 349{
2b54aa87
LC
350 VncInfo *info = g_malloc0(sizeof(*info));
351
d96fd29c 352 if (vnc_display == NULL || vnc_display->display == NULL) {
2b54aa87 353 info->enabled = false;
d96fd29c 354 } else {
2b54aa87
LC
355 VncClientInfoList *cur_item = NULL;
356 struct sockaddr_storage sa;
357 socklen_t salen = sizeof(sa);
358 char host[NI_MAXHOST];
359 char serv[NI_MAXSERV];
41b4bef6 360 VncState *client;
1ff7df1a 361
2b54aa87
LC
362 info->enabled = true;
363
364 /* for compatibility with the original command */
365 info->has_clients = true;
366
41b4bef6 367 QTAILQ_FOREACH(client, &vnc_display->clients, next) {
2b54aa87
LC
368 VncClientInfoList *cinfo = g_malloc0(sizeof(*info));
369 cinfo->value = qmp_query_vnc_client(client);
370
371 /* XXX: waiting for the qapi to support GSList */
372 if (!cur_item) {
373 info->clients = cur_item = cinfo;
374 } else {
375 cur_item->next = cinfo;
376 cur_item = cinfo;
1ff7df1a 377 }
d96fd29c
LC
378 }
379
417b0b88
PB
380 if (vnc_display->lsock == -1) {
381 return info;
382 }
383
2b54aa87
LC
384 if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa,
385 &salen) == -1) {
386 error_set(errp, QERR_UNDEFINED_ERROR);
387 goto out_error;
388 }
d96fd29c 389
2b54aa87
LC
390 if (getnameinfo((struct sockaddr *)&sa, salen,
391 host, sizeof(host),
392 serv, sizeof(serv),
393 NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
394 error_set(errp, QERR_UNDEFINED_ERROR);
395 goto out_error;
1ff7df1a 396 }
2b54aa87
LC
397
398 info->has_host = true;
399 info->host = g_strdup(host);
400
401 info->has_service = true;
402 info->service = g_strdup(serv);
403
404 info->has_family = true;
a589569f 405 info->family = inet_netfamily(sa.ss_family);
2b54aa87
LC
406
407 info->has_auth = true;
408 info->auth = g_strdup(vnc_auth_name(vnc_display));
a9ce8590 409 }
2b54aa87
LC
410
411 return info;
412
413out_error:
414 qapi_free_VncInfo(info);
415 return NULL;
a9ce8590
FB
416}
417
24236869
FB
418/* TODO
419 1) Get the queue working for IO.
420 2) there is some weirdness when using the -S option (the screen is grey
421 and not totally invalidated
422 3) resolutions > 1024
423*/
424
38ee14f4 425static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
198a0039 426static void vnc_disconnect_start(VncState *vs);
24236869 427
753b4053 428static void vnc_colordepth(VncState *vs);
1fc62412
SS
429static void framebuffer_update_request(VncState *vs, int incremental,
430 int x_position, int y_position,
431 int w, int h);
0f7b2864 432static void vnc_refresh(DisplayChangeListener *dcl);
1fc62412 433static int vnc_refresh_server_surface(VncDisplay *vd);
7eac3a87 434
bea60dd7
PL
435static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
436 VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
437 int width, int height,
438 int x, int y, int w, int h) {
91937225
PL
439 /* this is needed this to ensure we updated all affected
440 * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
b4c85ddc
PL
441 w += (x % VNC_DIRTY_PIXELS_PER_BIT);
442 x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
0486e8a7 443
9f64916d
GH
444 x = MIN(x, width);
445 y = MIN(y, height);
446 w = MIN(x + w, width) - x;
91937225 447 h = MIN(y + h, height);
788abf8e 448
b4c85ddc 449 for (; y < h; y++) {
bea60dd7 450 bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
91937225 451 DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
b4c85ddc 452 }
24236869
FB
453}
454
bea60dd7
PL
455static void vnc_dpy_update(DisplayChangeListener *dcl,
456 int x, int y, int w, int h)
457{
458 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
459 struct VncSurface *s = &vd->guest;
460 int width = pixman_image_get_width(vd->server);
461 int height = pixman_image_get_height(vd->server);
462
463 vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
464}
465
70a4568f
CC
466void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
467 int32_t encoding)
24236869
FB
468{
469 vnc_write_u16(vs, x);
470 vnc_write_u16(vs, y);
471 vnc_write_u16(vs, w);
472 vnc_write_u16(vs, h);
473
474 vnc_write_s32(vs, encoding);
475}
476
2f9606b3 477void buffer_reserve(Buffer *buffer, size_t len)
89064286
AL
478{
479 if ((buffer->capacity - buffer->offset) < len) {
28a76be8 480 buffer->capacity += (len + 1024);
7267c094 481 buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
28a76be8
AL
482 if (buffer->buffer == NULL) {
483 fprintf(stderr, "vnc: out of memory\n");
484 exit(1);
485 }
89064286
AL
486 }
487}
488
71a8cdec 489static int buffer_empty(Buffer *buffer)
89064286
AL
490{
491 return buffer->offset == 0;
492}
493
7536ee4b 494uint8_t *buffer_end(Buffer *buffer)
89064286
AL
495{
496 return buffer->buffer + buffer->offset;
497}
498
2f9606b3 499void buffer_reset(Buffer *buffer)
89064286 500{
28a76be8 501 buffer->offset = 0;
89064286
AL
502}
503
5d418e3b
CC
504void buffer_free(Buffer *buffer)
505{
7267c094 506 g_free(buffer->buffer);
5d418e3b
CC
507 buffer->offset = 0;
508 buffer->capacity = 0;
509 buffer->buffer = NULL;
510}
511
2f9606b3 512void buffer_append(Buffer *buffer, const void *data, size_t len)
89064286
AL
513{
514 memcpy(buffer->buffer + buffer->offset, data, len);
515 buffer->offset += len;
516}
517
32ed2680
TH
518void buffer_advance(Buffer *buf, size_t len)
519{
520 memmove(buf->buffer, buf->buffer + len,
521 (buf->offset - len));
522 buf->offset -= len;
523}
524
621aaeb9
GH
525static void vnc_desktop_resize(VncState *vs)
526{
621aaeb9
GH
527 if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
528 return;
529 }
bea60dd7
PL
530 if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
531 vs->client_height == pixman_image_get_height(vs->vd->server)) {
1d4b638a
GH
532 return;
533 }
bea60dd7
PL
534 vs->client_width = pixman_image_get_width(vs->vd->server);
535 vs->client_height = pixman_image_get_height(vs->vd->server);
bd023f95 536 vnc_lock_output(vs);
621aaeb9
GH
537 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
538 vnc_write_u8(vs, 0);
539 vnc_write_u16(vs, 1); /* number of rects */
5862d195 540 vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
621aaeb9 541 VNC_ENCODING_DESKTOPRESIZE);
bd023f95 542 vnc_unlock_output(vs);
621aaeb9
GH
543 vnc_flush(vs);
544}
545
bd023f95
CC
546static void vnc_abort_display_jobs(VncDisplay *vd)
547{
548 VncState *vs;
549
550 QTAILQ_FOREACH(vs, &vd->clients, next) {
551 vnc_lock_output(vs);
552 vs->abort = true;
553 vnc_unlock_output(vs);
554 }
555 QTAILQ_FOREACH(vs, &vd->clients, next) {
556 vnc_jobs_join(vs);
557 }
558 QTAILQ_FOREACH(vs, &vd->clients, next) {
559 vnc_lock_output(vs);
560 vs->abort = false;
561 vnc_unlock_output(vs);
562 }
563}
bd023f95 564
9f64916d
GH
565int vnc_server_fb_stride(VncDisplay *vd)
566{
567 return pixman_image_get_stride(vd->server);
568}
569
570void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
571{
572 uint8_t *ptr;
573
574 ptr = (uint8_t *)pixman_image_get_data(vd->server);
575 ptr += y * vnc_server_fb_stride(vd);
576 ptr += x * VNC_SERVER_FB_BYTES;
577 return ptr;
578}
579
c12aeb86 580static void vnc_dpy_switch(DisplayChangeListener *dcl,
c12aeb86 581 DisplaySurface *surface)
24236869 582{
21ef45d7 583 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
41b4bef6 584 VncState *vs;
bea60dd7 585 int width, height;
1fc62412 586
bd023f95
CC
587 vnc_abort_display_jobs(vd);
588
1fc62412 589 /* server surface */
9f64916d 590 qemu_pixman_image_unref(vd->server);
d39fa6d8 591 vd->ds = surface;
bea60dd7
PL
592 width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
593 VNC_DIRTY_PIXELS_PER_BIT));
594 height = MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
9f64916d 595 vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
bea60dd7 596 width, height, NULL, 0);
24236869 597
6baebed7 598 /* guest surface */
9f64916d 599#if 0 /* FIXME */
1fc62412 600 if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
a528b80c 601 console_color_init(ds);
9f64916d
GH
602#endif
603 qemu_pixman_image_unref(vd->guest.fb);
d39fa6d8
GH
604 vd->guest.fb = pixman_image_ref(surface->image);
605 vd->guest.format = surface->format;
bea60dd7
PL
606 memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
607 vnc_set_area_dirty(vd->guest.dirty, width, height, 0, 0,
608 width, height);
24236869 609
41b4bef6 610 QTAILQ_FOREACH(vs, &vd->clients, next) {
1fc62412 611 vnc_colordepth(vs);
1d4b638a 612 vnc_desktop_resize(vs);
d467b679
GH
613 if (vs->vd->cursor) {
614 vnc_cursor_define(vs);
615 }
bea60dd7
PL
616 memset(vs->dirty, 0x00, sizeof(vs->dirty));
617 vnc_set_area_dirty(vs->dirty, width, height, 0, 0,
618 width, height);
753b4053
AL
619 }
620}
621
3512779a 622/* fastest code */
9f64916d 623static void vnc_write_pixels_copy(VncState *vs,
d467b679 624 void *pixels, int size)
3512779a
FB
625{
626 vnc_write(vs, pixels, size);
627}
628
629/* slowest but generic code. */
70a4568f 630void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
3512779a 631{
7eac3a87 632 uint8_t r, g, b;
1fc62412 633
9f64916d
GH
634#if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
635 r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
636 g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8;
637 b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8;
638#else
639# error need some bits here if you change VNC_SERVER_FB_FORMAT
640#endif
641 v = (r << vs->client_pf.rshift) |
642 (g << vs->client_pf.gshift) |
643 (b << vs->client_pf.bshift);
644 switch (vs->client_pf.bytes_per_pixel) {
3512779a
FB
645 case 1:
646 buf[0] = v;
647 break;
648 case 2:
9f64916d 649 if (vs->client_be) {
3512779a
FB
650 buf[0] = v >> 8;
651 buf[1] = v;
652 } else {
653 buf[1] = v >> 8;
654 buf[0] = v;
655 }
656 break;
657 default:
658 case 4:
9f64916d 659 if (vs->client_be) {
3512779a
FB
660 buf[0] = v >> 24;
661 buf[1] = v >> 16;
662 buf[2] = v >> 8;
663 buf[3] = v;
664 } else {
665 buf[3] = v >> 24;
666 buf[2] = v >> 16;
667 buf[1] = v >> 8;
668 buf[0] = v;
669 }
670 break;
671 }
672}
673
9f64916d 674static void vnc_write_pixels_generic(VncState *vs,
d467b679 675 void *pixels1, int size)
3512779a 676{
3512779a 677 uint8_t buf[4];
3512779a 678
9f64916d 679 if (VNC_SERVER_FB_BYTES == 4) {
7eac3a87
AL
680 uint32_t *pixels = pixels1;
681 int n, i;
682 n = size >> 2;
9f64916d 683 for (i = 0; i < n; i++) {
7eac3a87 684 vnc_convert_pixel(vs, buf, pixels[i]);
9f64916d 685 vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
7eac3a87 686 }
3512779a
FB
687 }
688}
689
a885211e 690int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
24236869
FB
691{
692 int i;
60fe76f3 693 uint8_t *row;
1fc62412 694 VncDisplay *vd = vs->vd;
24236869 695
9f64916d 696 row = vnc_server_fb_ptr(vd, x, y);
24236869 697 for (i = 0; i < h; i++) {
9f64916d
GH
698 vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
699 row += vnc_server_fb_stride(vd);
24236869 700 }
a885211e 701 return 1;
24236869
FB
702}
703
bd023f95 704int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
24236869 705{
a885211e
CC
706 int n = 0;
707
fb437313 708 switch(vs->vnc_encoding) {
28a76be8 709 case VNC_ENCODING_ZLIB:
a885211e 710 n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
28a76be8
AL
711 break;
712 case VNC_ENCODING_HEXTILE:
713 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
a885211e 714 n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
28a76be8 715 break;
380282b0
CC
716 case VNC_ENCODING_TIGHT:
717 n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
718 break;
efe556ad
CC
719 case VNC_ENCODING_TIGHT_PNG:
720 n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
721 break;
148954fa
CC
722 case VNC_ENCODING_ZRLE:
723 n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
724 break;
725 case VNC_ENCODING_ZYWRLE:
726 n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
727 break;
28a76be8
AL
728 default:
729 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
a885211e 730 n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
28a76be8 731 break;
fb437313 732 }
a885211e 733 return n;
24236869
FB
734}
735
753b4053 736static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
24236869 737{
3e28c9ad 738 /* send bitblit op to the vnc client */
bd023f95 739 vnc_lock_output(vs);
46a183da 740 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
24236869
FB
741 vnc_write_u8(vs, 0);
742 vnc_write_u16(vs, 1); /* number of rects */
29fa4ed9 743 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
24236869
FB
744 vnc_write_u16(vs, src_x);
745 vnc_write_u16(vs, src_y);
bd023f95 746 vnc_unlock_output(vs);
24236869
FB
747 vnc_flush(vs);
748}
749
7c20b4a3 750static void vnc_dpy_copy(DisplayChangeListener *dcl,
7c20b4a3
GH
751 int src_x, int src_y,
752 int dst_x, int dst_y, int w, int h)
753b4053 753{
21ef45d7 754 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
198a0039 755 VncState *vs, *vn;
1fc62412
SS
756 uint8_t *src_row;
757 uint8_t *dst_row;
9f64916d 758 int i, x, y, pitch, inc, w_lim, s;
1fc62412 759 int cmp_bytes;
198a0039 760
1fc62412 761 vnc_refresh_server_surface(vd);
41b4bef6 762 QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
198a0039
GH
763 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
764 vs->force_update = 1;
38ee14f4 765 vnc_update_client(vs, 1, true);
198a0039
GH
766 /* vs might be free()ed here */
767 }
768 }
769
1fc62412 770 /* do bitblit op on the local surface too */
9f64916d
GH
771 pitch = vnc_server_fb_stride(vd);
772 src_row = vnc_server_fb_ptr(vd, src_x, src_y);
773 dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
1fc62412
SS
774 y = dst_y;
775 inc = 1;
776 if (dst_y > src_y) {
777 /* copy backwards */
778 src_row += pitch * (h-1);
779 dst_row += pitch * (h-1);
780 pitch = -pitch;
781 y = dst_y + h - 1;
782 inc = -1;
783 }
b4c85ddc
PL
784 w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
785 if (w_lim < 0) {
1fc62412 786 w_lim = w;
b4c85ddc
PL
787 } else {
788 w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
789 }
1fc62412
SS
790 for (i = 0; i < h; i++) {
791 for (x = 0; x <= w_lim;
792 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
793 if (x == w_lim) {
794 if ((s = w - w_lim) == 0)
795 break;
796 } else if (!x) {
b4c85ddc
PL
797 s = (VNC_DIRTY_PIXELS_PER_BIT -
798 (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
1fc62412
SS
799 s = MIN(s, w_lim);
800 } else {
b4c85ddc 801 s = VNC_DIRTY_PIXELS_PER_BIT;
1fc62412 802 }
9f64916d 803 cmp_bytes = s * VNC_SERVER_FB_BYTES;
1fc62412
SS
804 if (memcmp(src_row, dst_row, cmp_bytes) == 0)
805 continue;
806 memmove(dst_row, src_row, cmp_bytes);
41b4bef6
AS
807 QTAILQ_FOREACH(vs, &vd->clients, next) {
808 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
b4c85ddc
PL
809 set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
810 vs->dirty[y]);
41b4bef6 811 }
1fc62412
SS
812 }
813 }
9f64916d
GH
814 src_row += pitch - w * VNC_SERVER_FB_BYTES;
815 dst_row += pitch - w * VNC_SERVER_FB_BYTES;
1fc62412
SS
816 y += inc;
817 }
818
41b4bef6
AS
819 QTAILQ_FOREACH(vs, &vd->clients, next) {
820 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
753b4053 821 vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
41b4bef6 822 }
753b4053
AL
823 }
824}
825
7c20b4a3 826static void vnc_mouse_set(DisplayChangeListener *dcl,
7c20b4a3 827 int x, int y, int visible)
d467b679
GH
828{
829 /* can we ask the client(s) to move the pointer ??? */
830}
831
832static int vnc_cursor_define(VncState *vs)
833{
834 QEMUCursor *c = vs->vd->cursor;
d467b679
GH
835 int isize;
836
837 if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
d01f9595 838 vnc_lock_output(vs);
d467b679
GH
839 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
840 vnc_write_u8(vs, 0); /* padding */
841 vnc_write_u16(vs, 1); /* # of rects */
842 vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
843 VNC_ENCODING_RICH_CURSOR);
9f64916d
GH
844 isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
845 vnc_write_pixels_generic(vs, c->data, isize);
d467b679 846 vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
d01f9595 847 vnc_unlock_output(vs);
d467b679
GH
848 return 0;
849 }
850 return -1;
851}
852
7c20b4a3 853static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
7c20b4a3 854 QEMUCursor *c)
d467b679
GH
855{
856 VncDisplay *vd = vnc_display;
857 VncState *vs;
858
859 cursor_put(vd->cursor);
7267c094 860 g_free(vd->cursor_mask);
d467b679
GH
861
862 vd->cursor = c;
863 cursor_get(vd->cursor);
864 vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
7267c094 865 vd->cursor_mask = g_malloc0(vd->cursor_msize);
d467b679
GH
866 cursor_get_mono_mask(c, 0, vd->cursor_mask);
867
868 QTAILQ_FOREACH(vs, &vd->clients, next) {
869 vnc_cursor_define(vs);
870 }
871}
872
1fc62412 873static int find_and_clear_dirty_height(struct VncState *vs,
6c71a539 874 int y, int last_x, int x, int height)
24236869
FB
875{
876 int h;
877
6c71a539 878 for (h = 1; h < (height - y); h++) {
bc2429b9 879 if (!test_bit(last_x, vs->dirty[y + h])) {
28a76be8 880 break;
bc2429b9 881 }
863d7c91 882 bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
24236869
FB
883 }
884
885 return h;
886}
887
38ee14f4 888static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
24236869 889{
63658280 890 vs->has_dirty += has_dirty;
24236869 891 if (vs->need_update && vs->csock != -1) {
1fc62412 892 VncDisplay *vd = vs->vd;
bd023f95 893 VncJob *job;
28a76be8 894 int y;
2f487a3d 895 int height, width;
bd023f95
CC
896 int n = 0;
897
703bc68f 898 if (vs->output.offset && !vs->audio_cap && !vs->force_update)
c522d0e2 899 /* kernel send buffers are full -> drop frames to throttle */
2430ffe4 900 return 0;
a0ecfb73 901
63658280 902 if (!vs->has_dirty && !vs->audio_cap && !vs->force_update)
2430ffe4 903 return 0;
28a76be8 904
6baebed7
AL
905 /*
906 * Send screen updates to the vnc client using the server
907 * surface and server dirty map. guest surface updates
908 * happening in parallel don't disturb us, the next pass will
909 * send them to the client.
910 */
bd023f95 911 job = vnc_job_new(vs);
28a76be8 912
bea60dd7
PL
913 height = pixman_image_get_height(vd->server);
914 width = pixman_image_get_width(vd->server);
847ce6a1 915
12b316d4
PL
916 y = 0;
917 for (;;) {
918 int x, h;
919 unsigned long x2;
920 unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
921 height * VNC_DIRTY_BPL(vs),
922 y * VNC_DIRTY_BPL(vs));
923 if (offset == height * VNC_DIRTY_BPL(vs)) {
924 /* no more dirty bits */
925 break;
28a76be8 926 }
12b316d4
PL
927 y = offset / VNC_DIRTY_BPL(vs);
928 x = offset % VNC_DIRTY_BPL(vs);
929 x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
930 VNC_DIRTY_BPL(vs), x);
931 bitmap_clear(vs->dirty[y], x, x2 - x);
932 h = find_and_clear_dirty_height(vs, y, x, x2, height);
2f487a3d
PL
933 x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
934 if (x2 > x) {
935 n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
936 (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
937 }
28a76be8 938 }
bd023f95
CC
939
940 vnc_job_push(job);
eb214ff8
GH
941 if (sync) {
942 vnc_jobs_join(vs);
943 }
c522d0e2 944 vs->force_update = 0;
63658280 945 vs->has_dirty = 0;
bd023f95 946 return n;
24236869 947 }
24236869 948
38ee14f4 949 if (vs->csock == -1) {
198a0039 950 vnc_disconnect_finish(vs);
38ee14f4
GH
951 } else if (sync) {
952 vnc_jobs_join(vs);
953 }
2430ffe4
SS
954
955 return 0;
24236869
FB
956}
957
429a8ed3 958/* audio */
959static void audio_capture_notify(void *opaque, audcnotification_e cmd)
960{
961 VncState *vs = opaque;
962
963 switch (cmd) {
964 case AUD_CNOTIFY_DISABLE:
bd023f95 965 vnc_lock_output(vs);
46a183da
DB
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_END);
bd023f95 969 vnc_unlock_output(vs);
429a8ed3 970 vnc_flush(vs);
971 break;
972
973 case AUD_CNOTIFY_ENABLE:
bd023f95 974 vnc_lock_output(vs);
46a183da
DB
975 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
976 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
977 vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
bd023f95 978 vnc_unlock_output(vs);
429a8ed3 979 vnc_flush(vs);
980 break;
981 }
982}
983
984static void audio_capture_destroy(void *opaque)
985{
986}
987
988static void audio_capture(void *opaque, void *buf, int size)
989{
990 VncState *vs = opaque;
991
bd023f95 992 vnc_lock_output(vs);
46a183da
DB
993 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
994 vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
995 vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
429a8ed3 996 vnc_write_u32(vs, size);
997 vnc_write(vs, buf, size);
bd023f95 998 vnc_unlock_output(vs);
429a8ed3 999 vnc_flush(vs);
1000}
1001
1002static void audio_add(VncState *vs)
1003{
1004 struct audio_capture_ops ops;
1005
1006 if (vs->audio_cap) {
027a79c3 1007 error_report("audio already running");
429a8ed3 1008 return;
1009 }
1010
1011 ops.notify = audio_capture_notify;
1012 ops.destroy = audio_capture_destroy;
1013 ops.capture = audio_capture;
1014
1a7dafce 1015 vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
429a8ed3 1016 if (!vs->audio_cap) {
027a79c3 1017 error_report("Failed to add audio capture");
429a8ed3 1018 }
1019}
1020
1021static void audio_del(VncState *vs)
1022{
1023 if (vs->audio_cap) {
1024 AUD_del_capture(vs->audio_cap, vs);
1025 vs->audio_cap = NULL;
1026 }
1027}
1028
198a0039
GH
1029static void vnc_disconnect_start(VncState *vs)
1030{
1031 if (vs->csock == -1)
1032 return;
8cf36489 1033 vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
198a0039
GH
1034 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
1035 closesocket(vs->csock);
1036 vs->csock = -1;
1037}
1038
7536ee4b 1039void vnc_disconnect_finish(VncState *vs)
198a0039 1040{
7d964c9d
CC
1041 int i;
1042
bd023f95
CC
1043 vnc_jobs_join(vs); /* Wait encoding jobs */
1044
1045 vnc_lock_output(vs);
fb6ba0d5 1046 vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
0d72f3d3 1047
5d418e3b
CC
1048 buffer_free(&vs->input);
1049 buffer_free(&vs->output);
7536ee4b
TH
1050#ifdef CONFIG_VNC_WS
1051 buffer_free(&vs->ws_input);
1052 buffer_free(&vs->ws_output);
1053#endif /* CONFIG_VNC_WS */
4a80dba3 1054
fb6ba0d5 1055 qapi_free_VncClientInfo(vs->info);
4a80dba3 1056
161c4f20 1057 vnc_zlib_clear(vs);
380282b0 1058 vnc_tight_clear(vs);
148954fa 1059 vnc_zrle_clear(vs);
161c4f20 1060
198a0039
GH
1061#ifdef CONFIG_VNC_TLS
1062 vnc_tls_client_cleanup(vs);
1063#endif /* CONFIG_VNC_TLS */
1064#ifdef CONFIG_VNC_SASL
1065 vnc_sasl_client_cleanup(vs);
1066#endif /* CONFIG_VNC_SASL */
1067 audio_del(vs);
7bc9318b 1068 vnc_release_modifiers(vs);
198a0039 1069
6fd8e79a
TH
1070 if (vs->initialized) {
1071 QTAILQ_REMOVE(&vs->vd->clients, vs, next);
1072 qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
1073 }
41b4bef6 1074
3a0558b5
GH
1075 if (vs->vd->lock_key_sync)
1076 qemu_remove_led_event_handler(vs->led);
bd023f95
CC
1077 vnc_unlock_output(vs);
1078
bd023f95 1079 qemu_mutex_destroy(&vs->output_mutex);
6fd8e79a
TH
1080 if (vs->bh != NULL) {
1081 qemu_bh_delete(vs->bh);
1082 }
175b2a6e 1083 buffer_free(&vs->jobs_buffer);
175b2a6e 1084
7d964c9d 1085 for (i = 0; i < VNC_STAT_ROWS; ++i) {
7267c094 1086 g_free(vs->lossy_rect[i]);
7d964c9d 1087 }
7267c094
AL
1088 g_free(vs->lossy_rect);
1089 g_free(vs);
198a0039 1090}
2f9606b3
AL
1091
1092int vnc_client_io_error(VncState *vs, int ret, int last_errno)
24236869
FB
1093{
1094 if (ret == 0 || ret == -1) {
ea01e5fd
AZ
1095 if (ret == -1) {
1096 switch (last_errno) {
1097 case EINTR:
1098 case EAGAIN:
1099#ifdef _WIN32
1100 case WSAEWOULDBLOCK:
1101#endif
1102 return 0;
1103 default:
1104 break;
1105 }
1106 }
24236869 1107
198a0039
GH
1108 VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
1109 ret, ret < 0 ? last_errno : 0);
1110 vnc_disconnect_start(vs);
6baebed7 1111
28a76be8 1112 return 0;
24236869
FB
1113 }
1114 return ret;
1115}
1116
5fb6c7a8
AL
1117
1118void vnc_client_error(VncState *vs)
24236869 1119{
198a0039
GH
1120 VNC_DEBUG("Closing down client sock: protocol error\n");
1121 vnc_disconnect_start(vs);
24236869
FB
1122}
1123
0057a0d5
TH
1124#ifdef CONFIG_VNC_TLS
1125static long vnc_client_write_tls(gnutls_session_t *session,
1126 const uint8_t *data,
1127 size_t datalen)
1128{
1129 long ret = gnutls_write(*session, data, datalen);
1130 if (ret < 0) {
1131 if (ret == GNUTLS_E_AGAIN) {
1132 errno = EAGAIN;
1133 } else {
1134 errno = EIO;
1135 }
1136 ret = -1;
1137 }
1138 return ret;
1139}
1140#endif /* CONFIG_VNC_TLS */
2f9606b3
AL
1141
1142/*
1143 * Called to write a chunk of data to the client socket. The data may
1144 * be the raw data, or may have already been encoded by SASL.
1145 * The data will be written either straight onto the socket, or
1146 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1147 *
1148 * NB, it is theoretically possible to have 2 layers of encryption,
1149 * both SASL, and this TLS layer. It is highly unlikely in practice
1150 * though, since SASL encryption will typically be a no-op if TLS
1151 * is active
1152 *
1153 * Returns the number of bytes written, which may be less than
1154 * the requested 'datalen' if the socket would block. Returns
1155 * -1 on error, and disconnects the client socket.
1156 */
1157long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
24236869 1158{
ceb5caaf 1159 long ret;
eb38c52c 1160#ifdef CONFIG_VNC_TLS
5fb6c7a8 1161 if (vs->tls.session) {
0057a0d5
TH
1162 ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
1163 } else {
1164#ifdef CONFIG_VNC_WS
1165 if (vs->ws_tls.session) {
1166 ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen);
1167 } else
1168#endif /* CONFIG_VNC_WS */
1169#endif /* CONFIG_VNC_TLS */
1170 {
1171 ret = send(vs->csock, (const void *)data, datalen, 0);
28a76be8 1172 }
0057a0d5
TH
1173#ifdef CONFIG_VNC_TLS
1174 }
8d5d2d4c 1175#endif /* CONFIG_VNC_TLS */
23decc87 1176 VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
2f9606b3
AL
1177 return vnc_client_io_error(vs, ret, socket_error());
1178}
1179
1180
1181/*
1182 * Called to write buffered data to the client socket, when not
1183 * using any SASL SSF encryption layers. Will write as much data
1184 * as possible without blocking. If all buffered data is written,
1185 * will switch the FD poll() handler back to read monitoring.
1186 *
1187 * Returns the number of bytes written, which may be less than
1188 * the buffered output data if the socket would block. Returns
1189 * -1 on error, and disconnects the client socket.
1190 */
1191static long vnc_client_write_plain(VncState *vs)
1192{
1193 long ret;
1194
1195#ifdef CONFIG_VNC_SASL
23decc87 1196 VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
2f9606b3
AL
1197 vs->output.buffer, vs->output.capacity, vs->output.offset,
1198 vs->sasl.waitWriteSSF);
1199
1200 if (vs->sasl.conn &&
1201 vs->sasl.runSSF &&
1202 vs->sasl.waitWriteSSF) {
1203 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
1204 if (ret)
1205 vs->sasl.waitWriteSSF -= ret;
1206 } else
1207#endif /* CONFIG_VNC_SASL */
1208 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
24236869 1209 if (!ret)
2f9606b3 1210 return 0;
24236869 1211
32ed2680 1212 buffer_advance(&vs->output, ret);
24236869
FB
1213
1214 if (vs->output.offset == 0) {
28a76be8 1215 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
24236869 1216 }
2f9606b3
AL
1217
1218 return ret;
1219}
1220
1221
1222/*
1223 * First function called whenever there is data to be written to
1224 * the client socket. Will delegate actual work according to whether
1225 * SASL SSF layers are enabled (thus requiring encryption calls)
1226 */
bd023f95 1227static void vnc_client_write_locked(void *opaque)
2f9606b3 1228{
2f9606b3
AL
1229 VncState *vs = opaque;
1230
1231#ifdef CONFIG_VNC_SASL
1232 if (vs->sasl.conn &&
1233 vs->sasl.runSSF &&
9678d950
BS
1234 !vs->sasl.waitWriteSSF) {
1235 vnc_client_write_sasl(vs);
1236 } else
2f9606b3 1237#endif /* CONFIG_VNC_SASL */
7536ee4b
TH
1238 {
1239#ifdef CONFIG_VNC_WS
1240 if (vs->encode_ws) {
1241 vnc_client_write_ws(vs);
1242 } else
1243#endif /* CONFIG_VNC_WS */
1244 {
1245 vnc_client_write_plain(vs);
1246 }
1247 }
24236869
FB
1248}
1249
bd023f95
CC
1250void vnc_client_write(void *opaque)
1251{
1252 VncState *vs = opaque;
1253
1254 vnc_lock_output(vs);
7536ee4b
TH
1255 if (vs->output.offset
1256#ifdef CONFIG_VNC_WS
1257 || vs->ws_output.offset
1258#endif
1259 ) {
bd023f95 1260 vnc_client_write_locked(opaque);
ac71103d 1261 } else if (vs->csock != -1) {
bd023f95
CC
1262 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1263 }
1264 vnc_unlock_output(vs);
1265}
1266
5fb6c7a8 1267void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
24236869
FB
1268{
1269 vs->read_handler = func;
1270 vs->read_handler_expect = expecting;
1271}
1272
0057a0d5
TH
1273#ifdef CONFIG_VNC_TLS
1274static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
1275 size_t datalen)
1276{
1277 long ret = gnutls_read(*session, data, datalen);
1278 if (ret < 0) {
1279 if (ret == GNUTLS_E_AGAIN) {
1280 errno = EAGAIN;
1281 } else {
1282 errno = EIO;
1283 }
1284 ret = -1;
1285 }
1286 return ret;
1287}
1288#endif /* CONFIG_VNC_TLS */
2f9606b3
AL
1289
1290/*
1291 * Called to read a chunk of data from the client socket. The data may
1292 * be the raw data, or may need to be further decoded by SASL.
1293 * The data will be read either straight from to the socket, or
1294 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1295 *
1296 * NB, it is theoretically possible to have 2 layers of encryption,
1297 * both SASL, and this TLS layer. It is highly unlikely in practice
1298 * though, since SASL encryption will typically be a no-op if TLS
1299 * is active
1300 *
1301 * Returns the number of bytes read, which may be less than
1302 * the requested 'datalen' if the socket would block. Returns
1303 * -1 on error, and disconnects the client socket.
1304 */
1305long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
24236869 1306{
ceb5caaf 1307 long ret;
eb38c52c 1308#ifdef CONFIG_VNC_TLS
5fb6c7a8 1309 if (vs->tls.session) {
0057a0d5
TH
1310 ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
1311 } else {
1312#ifdef CONFIG_VNC_WS
1313 if (vs->ws_tls.session) {
1314 ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen);
1315 } else
1316#endif /* CONFIG_VNC_WS */
1317#endif /* CONFIG_VNC_TLS */
1318 {
1319 ret = qemu_recv(vs->csock, data, datalen, 0);
28a76be8 1320 }
0057a0d5
TH
1321#ifdef CONFIG_VNC_TLS
1322 }
8d5d2d4c 1323#endif /* CONFIG_VNC_TLS */
23decc87 1324 VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
2f9606b3
AL
1325 return vnc_client_io_error(vs, ret, socket_error());
1326}
24236869 1327
2f9606b3
AL
1328
1329/*
1330 * Called to read data from the client socket to the input buffer,
1331 * when not using any SASL SSF encryption layers. Will read as much
1332 * data as possible without blocking.
1333 *
1334 * Returns the number of bytes read. Returns -1 on error, and
1335 * disconnects the client socket.
1336 */
1337static long vnc_client_read_plain(VncState *vs)
1338{
1339 int ret;
23decc87 1340 VNC_DEBUG("Read plain %p size %zd offset %zd\n",
2f9606b3
AL
1341 vs->input.buffer, vs->input.capacity, vs->input.offset);
1342 buffer_reserve(&vs->input, 4096);
1343 ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
1344 if (!ret)
1345 return 0;
24236869 1346 vs->input.offset += ret;
2f9606b3
AL
1347 return ret;
1348}
1349
175b2a6e
CC
1350static void vnc_jobs_bh(void *opaque)
1351{
1352 VncState *vs = opaque;
1353
1354 vnc_jobs_consume_buffer(vs);
1355}
2f9606b3
AL
1356
1357/*
1358 * First function called whenever there is more data to be read from
1359 * the client socket. Will delegate actual work according to whether
1360 * SASL SSF layers are enabled (thus requiring decryption calls)
1361 */
1362void vnc_client_read(void *opaque)
1363{
1364 VncState *vs = opaque;
1365 long ret;
1366
1367#ifdef CONFIG_VNC_SASL
1368 if (vs->sasl.conn && vs->sasl.runSSF)
1369 ret = vnc_client_read_sasl(vs);
1370 else
1371#endif /* CONFIG_VNC_SASL */
7536ee4b
TH
1372#ifdef CONFIG_VNC_WS
1373 if (vs->encode_ws) {
1374 ret = vnc_client_read_ws(vs);
1375 if (ret == -1) {
1376 vnc_disconnect_start(vs);
1377 return;
1378 } else if (ret == -2) {
1379 vnc_client_error(vs);
1380 return;
1381 }
1382 } else
1383#endif /* CONFIG_VNC_WS */
1384 {
2f9606b3 1385 ret = vnc_client_read_plain(vs);
7536ee4b 1386 }
198a0039
GH
1387 if (!ret) {
1388 if (vs->csock == -1)
1389 vnc_disconnect_finish(vs);
28a76be8 1390 return;
198a0039 1391 }
24236869
FB
1392
1393 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
28a76be8
AL
1394 size_t len = vs->read_handler_expect;
1395 int ret;
1396
1397 ret = vs->read_handler(vs, vs->input.buffer, len);
198a0039
GH
1398 if (vs->csock == -1) {
1399 vnc_disconnect_finish(vs);
28a76be8 1400 return;
198a0039 1401 }
28a76be8
AL
1402
1403 if (!ret) {
32ed2680 1404 buffer_advance(&vs->input, len);
28a76be8
AL
1405 } else {
1406 vs->read_handler_expect = ret;
1407 }
24236869
FB
1408 }
1409}
1410
5fb6c7a8 1411void vnc_write(VncState *vs, const void *data, size_t len)
24236869
FB
1412{
1413 buffer_reserve(&vs->output, len);
1414
198a0039 1415 if (vs->csock != -1 && buffer_empty(&vs->output)) {
28a76be8 1416 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
24236869
FB
1417 }
1418
1419 buffer_append(&vs->output, data, len);
1420}
1421
5fb6c7a8 1422void vnc_write_s32(VncState *vs, int32_t value)
24236869
FB
1423{
1424 vnc_write_u32(vs, *(uint32_t *)&value);
1425}
1426
5fb6c7a8 1427void vnc_write_u32(VncState *vs, uint32_t value)
24236869
FB
1428{
1429 uint8_t buf[4];
1430
1431 buf[0] = (value >> 24) & 0xFF;
1432 buf[1] = (value >> 16) & 0xFF;
1433 buf[2] = (value >> 8) & 0xFF;
1434 buf[3] = value & 0xFF;
1435
1436 vnc_write(vs, buf, 4);
1437}
1438
5fb6c7a8 1439void vnc_write_u16(VncState *vs, uint16_t value)
24236869 1440{
64f5a135 1441 uint8_t buf[2];
24236869
FB
1442
1443 buf[0] = (value >> 8) & 0xFF;
1444 buf[1] = value & 0xFF;
1445
1446 vnc_write(vs, buf, 2);
1447}
1448
5fb6c7a8 1449void vnc_write_u8(VncState *vs, uint8_t value)
24236869
FB
1450{
1451 vnc_write(vs, (char *)&value, 1);
1452}
1453
5fb6c7a8 1454void vnc_flush(VncState *vs)
24236869 1455{
bd023f95 1456 vnc_lock_output(vs);
7536ee4b
TH
1457 if (vs->csock != -1 && (vs->output.offset
1458#ifdef CONFIG_VNC_WS
1459 || vs->ws_output.offset
1460#endif
1461 )) {
bd023f95
CC
1462 vnc_client_write_locked(vs);
1463 }
1464 vnc_unlock_output(vs);
24236869
FB
1465}
1466
71a8cdec 1467static uint8_t read_u8(uint8_t *data, size_t offset)
24236869
FB
1468{
1469 return data[offset];
1470}
1471
71a8cdec 1472static uint16_t read_u16(uint8_t *data, size_t offset)
24236869
FB
1473{
1474 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
1475}
1476
71a8cdec 1477static int32_t read_s32(uint8_t *data, size_t offset)
24236869
FB
1478{
1479 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
28a76be8 1480 (data[offset + 2] << 8) | data[offset + 3]);
24236869
FB
1481}
1482
5fb6c7a8 1483uint32_t read_u32(uint8_t *data, size_t offset)
24236869
FB
1484{
1485 return ((data[offset] << 24) | (data[offset + 1] << 16) |
28a76be8 1486 (data[offset + 2] << 8) | data[offset + 3]);
24236869
FB
1487}
1488
60fe76f3 1489static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
24236869
FB
1490{
1491}
1492
9e8dd451 1493static void check_pointer_type_change(Notifier *notifier, void *data)
564c337e 1494{
37c34d9d 1495 VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
14768eba 1496 int absolute = qemu_input_is_absolute();
37c34d9d 1497
29fa4ed9 1498 if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
bd023f95 1499 vnc_lock_output(vs);
46a183da 1500 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
28a76be8
AL
1501 vnc_write_u8(vs, 0);
1502 vnc_write_u16(vs, 1);
1503 vnc_framebuffer_update(vs, absolute, 0,
bea60dd7
PL
1504 pixman_image_get_width(vs->vd->server),
1505 pixman_image_get_height(vs->vd->server),
29fa4ed9 1506 VNC_ENCODING_POINTER_TYPE_CHANGE);
bd023f95 1507 vnc_unlock_output(vs);
28a76be8 1508 vnc_flush(vs);
564c337e
FB
1509 }
1510 vs->absolute = absolute;
1511}
1512
24236869
FB
1513static void pointer_event(VncState *vs, int button_mask, int x, int y)
1514{
14768eba
GH
1515 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1516 [INPUT_BUTTON_LEFT] = 0x01,
1517 [INPUT_BUTTON_MIDDLE] = 0x02,
1518 [INPUT_BUTTON_RIGHT] = 0x04,
1519 [INPUT_BUTTON_WHEEL_UP] = 0x08,
1520 [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
1521 };
1522 QemuConsole *con = vs->vd->dcl.con;
bea60dd7
PL
1523 int width = pixman_image_get_width(vs->vd->server);
1524 int height = pixman_image_get_height(vs->vd->server);
24236869 1525
14768eba
GH
1526 if (vs->last_bmask != button_mask) {
1527 qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
1528 vs->last_bmask = button_mask;
1529 }
564c337e
FB
1530
1531 if (vs->absolute) {
14768eba
GH
1532 qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
1533 qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
29fa4ed9 1534 } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
14768eba
GH
1535 qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
1536 qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
564c337e 1537 } else {
14768eba
GH
1538 if (vs->last_x != -1) {
1539 qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
1540 qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
1541 }
28a76be8
AL
1542 vs->last_x = x;
1543 vs->last_y = y;
24236869 1544 }
14768eba 1545 qemu_input_event_sync();
24236869
FB
1546}
1547
64f5a135
FB
1548static void reset_keys(VncState *vs)
1549{
1550 int i;
1551 for(i = 0; i < 256; i++) {
1552 if (vs->modifiers_state[i]) {
8d447d10 1553 qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
64f5a135
FB
1554 vs->modifiers_state[i] = 0;
1555 }
1556 }
1557}
1558
a528b80c
AZ
1559static void press_key(VncState *vs, int keysym)
1560{
44bb61c8 1561 int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
8d447d10 1562 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
2deb4acc 1563 qemu_input_event_send_key_delay(0);
8d447d10 1564 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
2deb4acc 1565 qemu_input_event_send_key_delay(0);
a528b80c
AZ
1566}
1567
ab99e5c1
LL
1568static int current_led_state(VncState *vs)
1569{
1570 int ledstate = 0;
1571
1572 if (vs->modifiers_state[0x46]) {
1573 ledstate |= QEMU_SCROLL_LOCK_LED;
1574 }
1575 if (vs->modifiers_state[0x45]) {
1576 ledstate |= QEMU_NUM_LOCK_LED;
1577 }
1578 if (vs->modifiers_state[0x3a]) {
1579 ledstate |= QEMU_CAPS_LOCK_LED;
1580 }
1581
1582 return ledstate;
1583}
1584
1585static void vnc_led_state_change(VncState *vs)
1586{
1587 int ledstate = 0;
1588
1589 if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1590 return;
1591 }
1592
1593 ledstate = current_led_state(vs);
1594 vnc_lock_output(vs);
1595 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1596 vnc_write_u8(vs, 0);
1597 vnc_write_u16(vs, 1);
1598 vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1599 vnc_write_u8(vs, ledstate);
1600 vnc_unlock_output(vs);
1601 vnc_flush(vs);
1602}
1603
7ffb82ca
GH
1604static void kbd_leds(void *opaque, int ledstate)
1605{
1606 VncState *vs = opaque;
96f3d174 1607 int caps, num, scr;
1483adcf 1608 bool has_changed = (ledstate != current_led_state(vs));
7ffb82ca 1609
40066175
GH
1610 trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
1611 (ledstate & QEMU_NUM_LOCK_LED),
1612 (ledstate & QEMU_SCROLL_LOCK_LED));
1613
7ffb82ca
GH
1614 caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
1615 num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0;
96f3d174 1616 scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
7ffb82ca
GH
1617
1618 if (vs->modifiers_state[0x3a] != caps) {
1619 vs->modifiers_state[0x3a] = caps;
1620 }
1621 if (vs->modifiers_state[0x45] != num) {
1622 vs->modifiers_state[0x45] = num;
1623 }
96f3d174
LL
1624 if (vs->modifiers_state[0x46] != scr) {
1625 vs->modifiers_state[0x46] = scr;
1626 }
ab99e5c1
LL
1627
1628 /* Sending the current led state message to the client */
1483adcf 1629 if (has_changed) {
ab99e5c1
LL
1630 vnc_led_state_change(vs);
1631 }
7ffb82ca
GH
1632}
1633
9ca313aa 1634static void do_key_event(VncState *vs, int down, int keycode, int sym)
24236869 1635{
64f5a135
FB
1636 /* QEMU console switch */
1637 switch(keycode) {
1638 case 0x2a: /* Left Shift */
1639 case 0x36: /* Right Shift */
1640 case 0x1d: /* Left CTRL */
1641 case 0x9d: /* Right CTRL */
1642 case 0x38: /* Left ALT */
1643 case 0xb8: /* Right ALT */
1644 if (down)
1645 vs->modifiers_state[keycode] = 1;
1646 else
1647 vs->modifiers_state[keycode] = 0;
1648 break;
5fafdf24 1649 case 0x02 ... 0x0a: /* '1' to '9' keys */
64f5a135
FB
1650 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
1651 /* Reset the modifiers sent to the current console */
1652 reset_keys(vs);
1653 console_select(keycode - 0x02);
1654 return;
1655 }
1656 break;
28a76be8
AL
1657 case 0x3a: /* CapsLock */
1658 case 0x45: /* NumLock */
7ffb82ca 1659 if (down)
a528b80c
AZ
1660 vs->modifiers_state[keycode] ^= 1;
1661 break;
1662 }
1663
e7b2aacc
LL
1664 /* Turn off the lock state sync logic if the client support the led
1665 state extension.
1666 */
9892088b 1667 if (down && vs->vd->lock_key_sync &&
e7b2aacc 1668 !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
3a0558b5 1669 keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
a528b80c
AZ
1670 /* If the numlock state needs to change then simulate an additional
1671 keypress before sending this one. This will happen if the user
1672 toggles numlock away from the VNC window.
1673 */
753b4053 1674 if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
a528b80c 1675 if (!vs->modifiers_state[0x45]) {
40066175 1676 trace_vnc_key_sync_numlock(true);
a528b80c
AZ
1677 vs->modifiers_state[0x45] = 1;
1678 press_key(vs, 0xff7f);
1679 }
1680 } else {
1681 if (vs->modifiers_state[0x45]) {
40066175 1682 trace_vnc_key_sync_numlock(false);
a528b80c
AZ
1683 vs->modifiers_state[0x45] = 0;
1684 press_key(vs, 0xff7f);
1685 }
1686 }
64f5a135 1687 }
24236869 1688
9892088b 1689 if (down && vs->vd->lock_key_sync &&
e7b2aacc 1690 !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
3a0558b5 1691 ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
6b132502
GH
1692 /* If the capslock state needs to change then simulate an additional
1693 keypress before sending this one. This will happen if the user
1694 toggles capslock away from the VNC window.
1695 */
1696 int uppercase = !!(sym >= 'A' && sym <= 'Z');
1697 int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
1698 int capslock = !!(vs->modifiers_state[0x3a]);
1699 if (capslock) {
1700 if (uppercase == shift) {
40066175 1701 trace_vnc_key_sync_capslock(false);
6b132502
GH
1702 vs->modifiers_state[0x3a] = 0;
1703 press_key(vs, 0xffe5);
1704 }
1705 } else {
1706 if (uppercase != shift) {
40066175 1707 trace_vnc_key_sync_capslock(true);
6b132502
GH
1708 vs->modifiers_state[0x3a] = 1;
1709 press_key(vs, 0xffe5);
1710 }
1711 }
1712 }
1713
81c0d5a6 1714 if (qemu_console_is_graphic(NULL)) {
8d447d10 1715 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
64f5a135 1716 } else {
e26437c2
GH
1717 bool numlock = vs->modifiers_state[0x45];
1718 bool control = (vs->modifiers_state[0x1d] ||
1719 vs->modifiers_state[0x9d]);
64f5a135
FB
1720 /* QEMU console emulation */
1721 if (down) {
1722 switch (keycode) {
1723 case 0x2a: /* Left Shift */
1724 case 0x36: /* Right Shift */
1725 case 0x1d: /* Left CTRL */
1726 case 0x9d: /* Right CTRL */
1727 case 0x38: /* Left ALT */
1728 case 0xb8: /* Right ALT */
1729 break;
1730 case 0xc8:
1731 kbd_put_keysym(QEMU_KEY_UP);
1732 break;
1733 case 0xd0:
1734 kbd_put_keysym(QEMU_KEY_DOWN);
1735 break;
1736 case 0xcb:
1737 kbd_put_keysym(QEMU_KEY_LEFT);
1738 break;
1739 case 0xcd:
1740 kbd_put_keysym(QEMU_KEY_RIGHT);
1741 break;
1742 case 0xd3:
1743 kbd_put_keysym(QEMU_KEY_DELETE);
1744 break;
1745 case 0xc7:
1746 kbd_put_keysym(QEMU_KEY_HOME);
1747 break;
1748 case 0xcf:
1749 kbd_put_keysym(QEMU_KEY_END);
1750 break;
1751 case 0xc9:
1752 kbd_put_keysym(QEMU_KEY_PAGEUP);
1753 break;
1754 case 0xd1:
1755 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
1756 break;
bb0a18e1
GH
1757
1758 case 0x47:
1759 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
1760 break;
1761 case 0x48:
1762 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
1763 break;
1764 case 0x49:
1765 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
1766 break;
1767 case 0x4b:
1768 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
1769 break;
1770 case 0x4c:
1771 kbd_put_keysym('5');
1772 break;
1773 case 0x4d:
1774 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
1775 break;
1776 case 0x4f:
1777 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
1778 break;
1779 case 0x50:
1780 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
1781 break;
1782 case 0x51:
1783 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
1784 break;
1785 case 0x52:
1786 kbd_put_keysym('0');
1787 break;
1788 case 0x53:
1789 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
1790 break;
1791
1792 case 0xb5:
1793 kbd_put_keysym('/');
1794 break;
1795 case 0x37:
1796 kbd_put_keysym('*');
1797 break;
1798 case 0x4a:
1799 kbd_put_keysym('-');
1800 break;
1801 case 0x4e:
1802 kbd_put_keysym('+');
1803 break;
1804 case 0x9c:
1805 kbd_put_keysym('\n');
1806 break;
1807
64f5a135 1808 default:
e26437c2
GH
1809 if (control) {
1810 kbd_put_keysym(sym & 0x1f);
1811 } else {
1812 kbd_put_keysym(sym);
1813 }
64f5a135
FB
1814 break;
1815 }
1816 }
1817 }
24236869
FB
1818}
1819
7bc9318b
GH
1820static void vnc_release_modifiers(VncState *vs)
1821{
1822 static const int keycodes[] = {
1823 /* shift, control, alt keys, both left & right */
1824 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
1825 };
1826 int i, keycode;
1827
81c0d5a6 1828 if (!qemu_console_is_graphic(NULL)) {
7bc9318b
GH
1829 return;
1830 }
1831 for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
1832 keycode = keycodes[i];
1833 if (!vs->modifiers_state[keycode]) {
1834 continue;
1835 }
8d447d10 1836 qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
7bc9318b
GH
1837 }
1838}
1839
40066175
GH
1840static const char *code2name(int keycode)
1841{
1842 return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)];
1843}
1844
bdbd7676
FB
1845static void key_event(VncState *vs, int down, uint32_t sym)
1846{
9ca313aa 1847 int keycode;
4a93fe17 1848 int lsym = sym;
9ca313aa 1849
81c0d5a6 1850 if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
4a93fe17
GH
1851 lsym = lsym - 'A' + 'a';
1852 }
9ca313aa 1853
44bb61c8 1854 keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
40066175 1855 trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
9ca313aa
AL
1856 do_key_event(vs, down, keycode, sym);
1857}
1858
1859static void ext_key_event(VncState *vs, int down,
1860 uint32_t sym, uint16_t keycode)
1861{
1862 /* if the user specifies a keyboard layout, always use it */
40066175 1863 if (keyboard_layout) {
9ca313aa 1864 key_event(vs, down, sym);
40066175
GH
1865 } else {
1866 trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
9ca313aa 1867 do_key_event(vs, down, keycode, sym);
40066175 1868 }
bdbd7676
FB
1869}
1870
24236869 1871static void framebuffer_update_request(VncState *vs, int incremental,
bea60dd7 1872 int x, int y, int w, int h)
24236869 1873{
bea60dd7
PL
1874 int width = pixman_image_get_width(vs->vd->server);
1875 int height = pixman_image_get_height(vs->vd->server);
cf2d385c 1876
24236869 1877 vs->need_update = 1;
bea60dd7
PL
1878
1879 if (incremental) {
1880 return;
24236869 1881 }
bea60dd7 1882
07535a89 1883 vs->force_update = 1;
bea60dd7 1884 vnc_set_area_dirty(vs->dirty, width, height, x, y, w, h);
24236869
FB
1885}
1886
9ca313aa
AL
1887static void send_ext_key_event_ack(VncState *vs)
1888{
bd023f95 1889 vnc_lock_output(vs);
46a183da 1890 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
9ca313aa
AL
1891 vnc_write_u8(vs, 0);
1892 vnc_write_u16(vs, 1);
d39fa6d8 1893 vnc_framebuffer_update(vs, 0, 0,
bea60dd7
PL
1894 pixman_image_get_width(vs->vd->server),
1895 pixman_image_get_height(vs->vd->server),
29fa4ed9 1896 VNC_ENCODING_EXT_KEY_EVENT);
bd023f95 1897 vnc_unlock_output(vs);
9ca313aa
AL
1898 vnc_flush(vs);
1899}
1900
429a8ed3 1901static void send_ext_audio_ack(VncState *vs)
1902{
bd023f95 1903 vnc_lock_output(vs);
46a183da 1904 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
429a8ed3 1905 vnc_write_u8(vs, 0);
1906 vnc_write_u16(vs, 1);
d39fa6d8 1907 vnc_framebuffer_update(vs, 0, 0,
bea60dd7
PL
1908 pixman_image_get_width(vs->vd->server),
1909 pixman_image_get_height(vs->vd->server),
29fa4ed9 1910 VNC_ENCODING_AUDIO);
bd023f95 1911 vnc_unlock_output(vs);
429a8ed3 1912 vnc_flush(vs);
1913}
1914
24236869
FB
1915static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1916{
1917 int i;
29fa4ed9 1918 unsigned int enc = 0;
24236869 1919
29fa4ed9 1920 vs->features = 0;
a9f20d31 1921 vs->vnc_encoding = 0;
d1af0e05
CC
1922 vs->tight.compression = 9;
1923 vs->tight.quality = -1; /* Lossless by default */
564c337e 1924 vs->absolute = -1;
24236869 1925
8a0f0d0c
CC
1926 /*
1927 * Start from the end because the encodings are sent in order of preference.
e5bed759 1928 * This way the preferred encoding (first encoding defined in the array)
8a0f0d0c
CC
1929 * will be set at the end of the loop.
1930 */
24236869 1931 for (i = n_encodings - 1; i >= 0; i--) {
29fa4ed9
AL
1932 enc = encodings[i];
1933 switch (enc) {
1934 case VNC_ENCODING_RAW:
a9f20d31 1935 vs->vnc_encoding = enc;
29fa4ed9
AL
1936 break;
1937 case VNC_ENCODING_COPYRECT:
753b4053 1938 vs->features |= VNC_FEATURE_COPYRECT_MASK;
29fa4ed9
AL
1939 break;
1940 case VNC_ENCODING_HEXTILE:
1941 vs->features |= VNC_FEATURE_HEXTILE_MASK;
a9f20d31 1942 vs->vnc_encoding = enc;
29fa4ed9 1943 break;
380282b0
CC
1944 case VNC_ENCODING_TIGHT:
1945 vs->features |= VNC_FEATURE_TIGHT_MASK;
1946 vs->vnc_encoding = enc;
1947 break;
fe3e7f2d 1948#ifdef CONFIG_VNC_PNG
efe556ad
CC
1949 case VNC_ENCODING_TIGHT_PNG:
1950 vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1951 vs->vnc_encoding = enc;
1952 break;
fe3e7f2d 1953#endif
059cef40
AL
1954 case VNC_ENCODING_ZLIB:
1955 vs->features |= VNC_FEATURE_ZLIB_MASK;
a9f20d31 1956 vs->vnc_encoding = enc;
059cef40 1957 break;
148954fa
CC
1958 case VNC_ENCODING_ZRLE:
1959 vs->features |= VNC_FEATURE_ZRLE_MASK;
1960 vs->vnc_encoding = enc;
1961 break;
1962 case VNC_ENCODING_ZYWRLE:
1963 vs->features |= VNC_FEATURE_ZYWRLE_MASK;
1964 vs->vnc_encoding = enc;
1965 break;
29fa4ed9
AL
1966 case VNC_ENCODING_DESKTOPRESIZE:
1967 vs->features |= VNC_FEATURE_RESIZE_MASK;
1968 break;
1969 case VNC_ENCODING_POINTER_TYPE_CHANGE:
1970 vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
1971 break;
d467b679
GH
1972 case VNC_ENCODING_RICH_CURSOR:
1973 vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
1974 break;
29fa4ed9 1975 case VNC_ENCODING_EXT_KEY_EVENT:
9ca313aa
AL
1976 send_ext_key_event_ack(vs);
1977 break;
29fa4ed9 1978 case VNC_ENCODING_AUDIO:
429a8ed3 1979 send_ext_audio_ack(vs);
1980 break;
29fa4ed9
AL
1981 case VNC_ENCODING_WMVi:
1982 vs->features |= VNC_FEATURE_WMVI_MASK;
ca4cca4d 1983 break;
ab99e5c1
LL
1984 case VNC_ENCODING_LED_STATE:
1985 vs->features |= VNC_FEATURE_LED_STATE_MASK;
1986 break;
fb437313 1987 case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
d1af0e05 1988 vs->tight.compression = (enc & 0x0F);
fb437313
AL
1989 break;
1990 case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
b31f519e
CC
1991 if (vs->vd->lossy) {
1992 vs->tight.quality = (enc & 0x0F);
1993 }
fb437313 1994 break;
29fa4ed9
AL
1995 default:
1996 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
1997 break;
1998 }
24236869 1999 }
6356e472 2000 vnc_desktop_resize(vs);
9e8dd451 2001 check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
ab99e5c1 2002 vnc_led_state_change(vs);
24236869
FB
2003}
2004
6cec5487
AL
2005static void set_pixel_conversion(VncState *vs)
2006{
9f64916d
GH
2007 pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
2008
2009 if (fmt == VNC_SERVER_FB_FORMAT) {
6cec5487 2010 vs->write_pixels = vnc_write_pixels_copy;
70a4568f 2011 vnc_hextile_set_pixel_conversion(vs, 0);
6cec5487
AL
2012 } else {
2013 vs->write_pixels = vnc_write_pixels_generic;
70a4568f 2014 vnc_hextile_set_pixel_conversion(vs, 1);
6cec5487
AL
2015 }
2016}
2017
24236869 2018static void set_pixel_format(VncState *vs,
28a76be8
AL
2019 int bits_per_pixel, int depth,
2020 int big_endian_flag, int true_color_flag,
2021 int red_max, int green_max, int blue_max,
2022 int red_shift, int green_shift, int blue_shift)
24236869 2023{
3512779a 2024 if (!true_color_flag) {
28a76be8 2025 vnc_client_error(vs);
3512779a
FB
2026 return;
2027 }
24236869 2028
9f64916d
GH
2029 vs->client_pf.rmax = red_max;
2030 vs->client_pf.rbits = hweight_long(red_max);
2031 vs->client_pf.rshift = red_shift;
2032 vs->client_pf.rmask = red_max << red_shift;
2033 vs->client_pf.gmax = green_max;
2034 vs->client_pf.gbits = hweight_long(green_max);
2035 vs->client_pf.gshift = green_shift;
2036 vs->client_pf.gmask = green_max << green_shift;
2037 vs->client_pf.bmax = blue_max;
2038 vs->client_pf.bbits = hweight_long(blue_max);
2039 vs->client_pf.bshift = blue_shift;
2040 vs->client_pf.bmask = blue_max << blue_shift;
2041 vs->client_pf.bits_per_pixel = bits_per_pixel;
2042 vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
2043 vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
2044 vs->client_be = big_endian_flag;
6cec5487
AL
2045
2046 set_pixel_conversion(vs);
24236869 2047
1dbfa005
GH
2048 graphic_hw_invalidate(NULL);
2049 graphic_hw_update(NULL);
24236869
FB
2050}
2051
ca4cca4d
AL
2052static void pixel_format_message (VncState *vs) {
2053 char pad[3] = { 0, 0, 0 };
2054
9f64916d
GH
2055 vs->client_pf = qemu_default_pixelformat(32);
2056
2057 vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
2058 vnc_write_u8(vs, vs->client_pf.depth); /* depth */
ca4cca4d 2059
e2542fe2 2060#ifdef HOST_WORDS_BIGENDIAN
ca4cca4d
AL
2061 vnc_write_u8(vs, 1); /* big-endian-flag */
2062#else
2063 vnc_write_u8(vs, 0); /* big-endian-flag */
2064#endif
2065 vnc_write_u8(vs, 1); /* true-color-flag */
9f64916d
GH
2066 vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */
2067 vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */
2068 vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */
2069 vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */
2070 vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */
2071 vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */
2072 vnc_write(vs, pad, 3); /* padding */
70a4568f
CC
2073
2074 vnc_hextile_set_pixel_conversion(vs, 0);
ca4cca4d 2075 vs->write_pixels = vnc_write_pixels_copy;
ca4cca4d
AL
2076}
2077
753b4053 2078static void vnc_colordepth(VncState *vs)
7eac3a87 2079{
753b4053 2080 if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
ca4cca4d 2081 /* Sending a WMVi message to notify the client*/
bd023f95 2082 vnc_lock_output(vs);
46a183da 2083 vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
ca4cca4d
AL
2084 vnc_write_u8(vs, 0);
2085 vnc_write_u16(vs, 1); /* number of rects */
d39fa6d8 2086 vnc_framebuffer_update(vs, 0, 0,
bea60dd7
PL
2087 pixman_image_get_width(vs->vd->server),
2088 pixman_image_get_height(vs->vd->server),
d39fa6d8 2089 VNC_ENCODING_WMVi);
ca4cca4d 2090 pixel_format_message(vs);
bd023f95 2091 vnc_unlock_output(vs);
ca4cca4d 2092 vnc_flush(vs);
7eac3a87 2093 } else {
6cec5487 2094 set_pixel_conversion(vs);
7eac3a87
AL
2095 }
2096}
2097
60fe76f3 2098static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
24236869
FB
2099{
2100 int i;
2101 uint16_t limit;
2430ffe4
SS
2102 VncDisplay *vd = vs->vd;
2103
2104 if (data[0] > 3) {
0f7b2864 2105 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2430ffe4 2106 }
24236869
FB
2107
2108 switch (data[0]) {
46a183da 2109 case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
28a76be8
AL
2110 if (len == 1)
2111 return 20;
2112
2113 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
2114 read_u8(data, 6), read_u8(data, 7),
2115 read_u16(data, 8), read_u16(data, 10),
2116 read_u16(data, 12), read_u8(data, 14),
2117 read_u8(data, 15), read_u8(data, 16));
2118 break;
46a183da 2119 case VNC_MSG_CLIENT_SET_ENCODINGS:
28a76be8
AL
2120 if (len == 1)
2121 return 4;
24236869 2122
28a76be8 2123 if (len == 4) {
69dd5c9f
AL
2124 limit = read_u16(data, 2);
2125 if (limit > 0)
2126 return 4 + (limit * 4);
2127 } else
2128 limit = read_u16(data, 2);
24236869 2129
28a76be8
AL
2130 for (i = 0; i < limit; i++) {
2131 int32_t val = read_s32(data, 4 + (i * 4));
2132 memcpy(data + 4 + (i * 4), &val, sizeof(val));
2133 }
24236869 2134
28a76be8
AL
2135 set_encodings(vs, (int32_t *)(data + 4), limit);
2136 break;
46a183da 2137 case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
28a76be8
AL
2138 if (len == 1)
2139 return 10;
24236869 2140
28a76be8
AL
2141 framebuffer_update_request(vs,
2142 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
2143 read_u16(data, 6), read_u16(data, 8));
2144 break;
46a183da 2145 case VNC_MSG_CLIENT_KEY_EVENT:
28a76be8
AL
2146 if (len == 1)
2147 return 8;
24236869 2148
28a76be8
AL
2149 key_event(vs, read_u8(data, 1), read_u32(data, 4));
2150 break;
46a183da 2151 case VNC_MSG_CLIENT_POINTER_EVENT:
28a76be8
AL
2152 if (len == 1)
2153 return 6;
24236869 2154
28a76be8
AL
2155 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
2156 break;
46a183da 2157 case VNC_MSG_CLIENT_CUT_TEXT:
f9a70e79 2158 if (len == 1) {
28a76be8 2159 return 8;
f9a70e79 2160 }
28a76be8 2161 if (len == 8) {
baa7666c 2162 uint32_t dlen = read_u32(data, 4);
f9a70e79
PL
2163 if (dlen > (1 << 20)) {
2164 error_report("vnc: client_cut_text msg payload has %u bytes"
2165 " which exceeds our limit of 1MB.", dlen);
2166 vnc_client_error(vs);
2167 break;
2168 }
2169 if (dlen > 0) {
baa7666c 2170 return 8 + dlen;
f9a70e79 2171 }
baa7666c 2172 }
24236869 2173
28a76be8
AL
2174 client_cut_text(vs, read_u32(data, 4), data + 8);
2175 break;
46a183da 2176 case VNC_MSG_CLIENT_QEMU:
9ca313aa
AL
2177 if (len == 1)
2178 return 2;
2179
2180 switch (read_u8(data, 1)) {
46a183da 2181 case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
9ca313aa
AL
2182 if (len == 2)
2183 return 12;
2184
2185 ext_key_event(vs, read_u16(data, 2),
2186 read_u32(data, 4), read_u32(data, 8));
2187 break;
46a183da 2188 case VNC_MSG_CLIENT_QEMU_AUDIO:
429a8ed3 2189 if (len == 2)
2190 return 4;
2191
2192 switch (read_u16 (data, 2)) {
46a183da 2193 case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
429a8ed3 2194 audio_add(vs);
2195 break;
46a183da 2196 case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
429a8ed3 2197 audio_del(vs);
2198 break;
46a183da 2199 case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
429a8ed3 2200 if (len == 4)
2201 return 10;
2202 switch (read_u8(data, 4)) {
2203 case 0: vs->as.fmt = AUD_FMT_U8; break;
2204 case 1: vs->as.fmt = AUD_FMT_S8; break;
2205 case 2: vs->as.fmt = AUD_FMT_U16; break;
2206 case 3: vs->as.fmt = AUD_FMT_S16; break;
2207 case 4: vs->as.fmt = AUD_FMT_U32; break;
2208 case 5: vs->as.fmt = AUD_FMT_S32; break;
2209 default:
2210 printf("Invalid audio format %d\n", read_u8(data, 4));
2211 vnc_client_error(vs);
2212 break;
2213 }
2214 vs->as.nchannels = read_u8(data, 5);
2215 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
2216 printf("Invalid audio channel coount %d\n",
2217 read_u8(data, 5));
2218 vnc_client_error(vs);
2219 break;
2220 }
2221 vs->as.freq = read_u32(data, 6);
2222 break;
2223 default:
2224 printf ("Invalid audio message %d\n", read_u8(data, 4));
2225 vnc_client_error(vs);
2226 break;
2227 }
2228 break;
2229
9ca313aa
AL
2230 default:
2231 printf("Msg: %d\n", read_u16(data, 0));
2232 vnc_client_error(vs);
2233 break;
2234 }
2235 break;
24236869 2236 default:
28a76be8
AL
2237 printf("Msg: %d\n", data[0]);
2238 vnc_client_error(vs);
2239 break;
24236869 2240 }
5fafdf24 2241
24236869
FB
2242 vnc_read_when(vs, protocol_client_msg, 1);
2243 return 0;
2244}
2245
60fe76f3 2246static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
24236869 2247{
c35734b2 2248 char buf[1024];
8cf36489 2249 VncShareMode mode;
c35734b2 2250 int size;
24236869 2251
8cf36489
GH
2252 mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
2253 switch (vs->vd->share_policy) {
2254 case VNC_SHARE_POLICY_IGNORE:
2255 /*
2256 * Ignore the shared flag. Nothing to do here.
2257 *
2258 * Doesn't conform to the rfb spec but is traditional qemu
2259 * behavior, thus left here as option for compatibility
2260 * reasons.
2261 */
2262 break;
2263 case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
2264 /*
2265 * Policy: Allow clients ask for exclusive access.
2266 *
2267 * Implementation: When a client asks for exclusive access,
2268 * disconnect all others. Shared connects are allowed as long
2269 * as no exclusive connection exists.
2270 *
2271 * This is how the rfb spec suggests to handle the shared flag.
2272 */
2273 if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
2274 VncState *client;
2275 QTAILQ_FOREACH(client, &vs->vd->clients, next) {
2276 if (vs == client) {
2277 continue;
2278 }
2279 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
2280 client->share_mode != VNC_SHARE_MODE_SHARED) {
2281 continue;
2282 }
2283 vnc_disconnect_start(client);
2284 }
2285 }
2286 if (mode == VNC_SHARE_MODE_SHARED) {
2287 if (vs->vd->num_exclusive > 0) {
2288 vnc_disconnect_start(vs);
2289 return 0;
2290 }
2291 }
2292 break;
2293 case VNC_SHARE_POLICY_FORCE_SHARED:
2294 /*
2295 * Policy: Shared connects only.
2296 * Implementation: Disallow clients asking for exclusive access.
2297 *
2298 * Useful for shared desktop sessions where you don't want
2299 * someone forgetting to say -shared when running the vnc
2300 * client disconnect everybody else.
2301 */
2302 if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
2303 vnc_disconnect_start(vs);
2304 return 0;
2305 }
2306 break;
2307 }
2308 vnc_set_share_mode(vs, mode);
2309
bea60dd7
PL
2310 vs->client_width = pixman_image_get_width(vs->vd->server);
2311 vs->client_height = pixman_image_get_height(vs->vd->server);
5862d195
GH
2312 vnc_write_u16(vs, vs->client_width);
2313 vnc_write_u16(vs, vs->client_height);
24236869 2314
ca4cca4d 2315 pixel_format_message(vs);
24236869 2316
c35734b2
TS
2317 if (qemu_name)
2318 size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
2319 else
2320 size = snprintf(buf, sizeof(buf), "QEMU");
2321
2322 vnc_write_u32(vs, size);
2323 vnc_write(vs, buf, size);
24236869
FB
2324 vnc_flush(vs);
2325
4a80dba3 2326 vnc_client_cache_auth(vs);
fb6ba0d5 2327 vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
4a80dba3 2328
24236869
FB
2329 vnc_read_when(vs, protocol_client_msg, 1);
2330
2331 return 0;
2332}
2333
5fb6c7a8
AL
2334void start_client_init(VncState *vs)
2335{
2336 vnc_read_when(vs, protocol_client_init, 1);
2337}
2338
70848515
TS
2339static void make_challenge(VncState *vs)
2340{
2341 int i;
2342
2343 srand(time(NULL)+getpid()+getpid()*987654+rand());
2344
2345 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
2346 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
2347}
2348
60fe76f3 2349static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
70848515 2350{
60fe76f3 2351 unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
70848515 2352 int i, j, pwlen;
60fe76f3 2353 unsigned char key[8];
3c9405a0 2354 time_t now = time(NULL);
70848515 2355
1cd20f8b 2356 if (!vs->vd->password) {
28a76be8 2357 VNC_DEBUG("No password configured on server");
6bffdf0f 2358 goto reject;
70848515 2359 }
3c9405a0
GH
2360 if (vs->vd->expires < now) {
2361 VNC_DEBUG("Password is expired");
2362 goto reject;
2363 }
70848515
TS
2364
2365 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
2366
2367 /* Calculate the expected challenge response */
753b4053 2368 pwlen = strlen(vs->vd->password);
70848515 2369 for (i=0; i<sizeof(key); i++)
753b4053 2370 key[i] = i<pwlen ? vs->vd->password[i] : 0;
70848515
TS
2371 deskey(key, EN0);
2372 for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
2373 des(response+j, response+j);
2374
2375 /* Compare expected vs actual challenge response */
2376 if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
e5bed759 2377 VNC_DEBUG("Client challenge response did not match\n");
6bffdf0f 2378 goto reject;
70848515 2379 } else {
28a76be8
AL
2380 VNC_DEBUG("Accepting VNC challenge response\n");
2381 vnc_write_u32(vs, 0); /* Accept auth */
2382 vnc_flush(vs);
70848515 2383
5fb6c7a8 2384 start_client_init(vs);
70848515
TS
2385 }
2386 return 0;
6bffdf0f
GH
2387
2388reject:
2389 vnc_write_u32(vs, 1); /* Reject auth */
2390 if (vs->minor >= 8) {
2391 static const char err[] = "Authentication failed";
2392 vnc_write_u32(vs, sizeof(err));
2393 vnc_write(vs, err, sizeof(err));
2394 }
2395 vnc_flush(vs);
2396 vnc_client_error(vs);
2397 return 0;
70848515
TS
2398}
2399
5fb6c7a8 2400void start_auth_vnc(VncState *vs)
70848515
TS
2401{
2402 make_challenge(vs);
2403 /* Send client a 'random' challenge */
2404 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
2405 vnc_flush(vs);
2406
2407 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
469b15c6
TS
2408}
2409
2410
60fe76f3 2411static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
70848515
TS
2412{
2413 /* We only advertise 1 auth scheme at a time, so client
2414 * must pick the one we sent. Verify this */
7e7e2ebc 2415 if (data[0] != vs->auth) { /* Reject auth */
1263b7d6 2416 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
70848515
TS
2417 vnc_write_u32(vs, 1);
2418 if (vs->minor >= 8) {
2419 static const char err[] = "Authentication failed";
2420 vnc_write_u32(vs, sizeof(err));
2421 vnc_write(vs, err, sizeof(err));
2422 }
2423 vnc_client_error(vs);
2424 } else { /* Accept requested auth */
2425 VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
7e7e2ebc 2426 switch (vs->auth) {
70848515
TS
2427 case VNC_AUTH_NONE:
2428 VNC_DEBUG("Accept auth none\n");
a26c97ad
AZ
2429 if (vs->minor >= 8) {
2430 vnc_write_u32(vs, 0); /* Accept auth completion */
2431 vnc_flush(vs);
2432 }
5fb6c7a8 2433 start_client_init(vs);
70848515
TS
2434 break;
2435
2436 case VNC_AUTH_VNC:
2437 VNC_DEBUG("Start VNC auth\n");
5fb6c7a8
AL
2438 start_auth_vnc(vs);
2439 break;
70848515 2440
eb38c52c 2441#ifdef CONFIG_VNC_TLS
8d5d2d4c 2442 case VNC_AUTH_VENCRYPT:
3a93113a 2443 VNC_DEBUG("Accept VeNCrypt auth\n");
5fb6c7a8
AL
2444 start_auth_vencrypt(vs);
2445 break;
8d5d2d4c
TS
2446#endif /* CONFIG_VNC_TLS */
2447
2f9606b3
AL
2448#ifdef CONFIG_VNC_SASL
2449 case VNC_AUTH_SASL:
2450 VNC_DEBUG("Accept SASL auth\n");
2451 start_auth_sasl(vs);
2452 break;
2453#endif /* CONFIG_VNC_SASL */
2454
70848515 2455 default: /* Should not be possible, but just in case */
7e7e2ebc 2456 VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
70848515
TS
2457 vnc_write_u8(vs, 1);
2458 if (vs->minor >= 8) {
2459 static const char err[] = "Authentication failed";
2460 vnc_write_u32(vs, sizeof(err));
2461 vnc_write(vs, err, sizeof(err));
2462 }
2463 vnc_client_error(vs);
2464 }
2465 }
2466 return 0;
2467}
2468
60fe76f3 2469static int protocol_version(VncState *vs, uint8_t *version, size_t len)
24236869
FB
2470{
2471 char local[13];
24236869
FB
2472
2473 memcpy(local, version, 12);
2474 local[12] = 0;
2475
70848515 2476 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
28a76be8
AL
2477 VNC_DEBUG("Malformed protocol version %s\n", local);
2478 vnc_client_error(vs);
2479 return 0;
24236869 2480 }
70848515
TS
2481 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
2482 if (vs->major != 3 ||
28a76be8
AL
2483 (vs->minor != 3 &&
2484 vs->minor != 4 &&
2485 vs->minor != 5 &&
2486 vs->minor != 7 &&
2487 vs->minor != 8)) {
2488 VNC_DEBUG("Unsupported client version\n");
2489 vnc_write_u32(vs, VNC_AUTH_INVALID);
2490 vnc_flush(vs);
2491 vnc_client_error(vs);
2492 return 0;
70848515 2493 }
b0566f4f 2494 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
70848515
TS
2495 * as equivalent to v3.3 by servers
2496 */
b0566f4f 2497 if (vs->minor == 4 || vs->minor == 5)
28a76be8 2498 vs->minor = 3;
70848515
TS
2499
2500 if (vs->minor == 3) {
7e7e2ebc 2501 if (vs->auth == VNC_AUTH_NONE) {
70848515 2502 VNC_DEBUG("Tell client auth none\n");
7e7e2ebc 2503 vnc_write_u32(vs, vs->auth);
70848515 2504 vnc_flush(vs);
28a76be8 2505 start_client_init(vs);
7e7e2ebc 2506 } else if (vs->auth == VNC_AUTH_VNC) {
70848515 2507 VNC_DEBUG("Tell client VNC auth\n");
7e7e2ebc 2508 vnc_write_u32(vs, vs->auth);
70848515
TS
2509 vnc_flush(vs);
2510 start_auth_vnc(vs);
2511 } else {
7e7e2ebc 2512 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
70848515
TS
2513 vnc_write_u32(vs, VNC_AUTH_INVALID);
2514 vnc_flush(vs);
2515 vnc_client_error(vs);
2516 }
2517 } else {
7e7e2ebc 2518 VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
28a76be8 2519 vnc_write_u8(vs, 1); /* num auth */
7e7e2ebc 2520 vnc_write_u8(vs, vs->auth);
28a76be8
AL
2521 vnc_read_when(vs, protocol_client_auth, 1);
2522 vnc_flush(vs);
70848515 2523 }
24236869
FB
2524
2525 return 0;
2526}
2527
999342a0
CC
2528static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2529{
2530 struct VncSurface *vs = &vd->guest;
2531
2532 return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2533}
2534
7d964c9d
CC
2535void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
2536{
2537 int i, j;
2538
2539 w = (x + w) / VNC_STAT_RECT;
2540 h = (y + h) / VNC_STAT_RECT;
2541 x /= VNC_STAT_RECT;
2542 y /= VNC_STAT_RECT;
2543
207f328a
CC
2544 for (j = y; j <= h; j++) {
2545 for (i = x; i <= w; i++) {
7d964c9d
CC
2546 vs->lossy_rect[j][i] = 1;
2547 }
2548 }
2549}
2550
2551static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
2552{
2553 VncState *vs;
2554 int sty = y / VNC_STAT_RECT;
2555 int stx = x / VNC_STAT_RECT;
2556 int has_dirty = 0;
2557
2558 y = y / VNC_STAT_RECT * VNC_STAT_RECT;
2559 x = x / VNC_STAT_RECT * VNC_STAT_RECT;
2560
2561 QTAILQ_FOREACH(vs, &vd->clients, next) {
bc2429b9 2562 int j;
7d964c9d
CC
2563
2564 /* kernel send buffers are full -> refresh later */
2565 if (vs->output.offset) {
2566 continue;
2567 }
2568
2569 if (!vs->lossy_rect[sty][stx]) {
2570 continue;
2571 }
207f328a 2572
7d964c9d
CC
2573 vs->lossy_rect[sty][stx] = 0;
2574 for (j = 0; j < VNC_STAT_RECT; ++j) {
b4c85ddc
PL
2575 bitmap_set(vs->dirty[y + j],
2576 x / VNC_DIRTY_PIXELS_PER_BIT,
2577 VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
7d964c9d
CC
2578 }
2579 has_dirty++;
2580 }
207f328a 2581
7d964c9d
CC
2582 return has_dirty;
2583}
2584
2585static int vnc_update_stats(VncDisplay *vd, struct timeval * tv)
999342a0 2586{
9f64916d
GH
2587 int width = pixman_image_get_width(vd->guest.fb);
2588 int height = pixman_image_get_height(vd->guest.fb);
999342a0
CC
2589 int x, y;
2590 struct timeval res;
7d964c9d 2591 int has_dirty = 0;
999342a0 2592
9f64916d
GH
2593 for (y = 0; y < height; y += VNC_STAT_RECT) {
2594 for (x = 0; x < width; x += VNC_STAT_RECT) {
999342a0
CC
2595 VncRectStat *rect = vnc_stat_rect(vd, x, y);
2596
2597 rect->updated = false;
2598 }
2599 }
2600
ad620c29 2601 qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
999342a0
CC
2602
2603 if (timercmp(&vd->guest.last_freq_check, &res, >)) {
7d964c9d 2604 return has_dirty;
999342a0
CC
2605 }
2606 vd->guest.last_freq_check = *tv;
2607
9f64916d
GH
2608 for (y = 0; y < height; y += VNC_STAT_RECT) {
2609 for (x = 0; x < width; x += VNC_STAT_RECT) {
999342a0
CC
2610 VncRectStat *rect= vnc_stat_rect(vd, x, y);
2611 int count = ARRAY_SIZE(rect->times);
2612 struct timeval min, max;
2613
2614 if (!timerisset(&rect->times[count - 1])) {
2615 continue ;
2616 }
2617
2618 max = rect->times[(rect->idx + count - 1) % count];
ad620c29 2619 qemu_timersub(tv, &max, &res);
999342a0
CC
2620
2621 if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2622 rect->freq = 0;
7d964c9d 2623 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
999342a0
CC
2624 memset(rect->times, 0, sizeof (rect->times));
2625 continue ;
2626 }
2627
2628 min = rect->times[rect->idx];
2629 max = rect->times[(rect->idx + count - 1) % count];
ad620c29 2630 qemu_timersub(&max, &min, &res);
999342a0
CC
2631
2632 rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2633 rect->freq /= count;
2634 rect->freq = 1. / rect->freq;
2635 }
2636 }
7d964c9d 2637 return has_dirty;
999342a0
CC
2638}
2639
2640double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2641{
2642 int i, j;
2643 double total = 0;
2644 int num = 0;
2645
2646 x = (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2647 y = (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2648
2649 for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2650 for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2651 total += vnc_stat_rect(vs->vd, i, j)->freq;
2652 num++;
2653 }
2654 }
2655
2656 if (num) {
2657 return total / num;
2658 } else {
2659 return 0;
2660 }
2661}
2662
2663static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2664{
2665 VncRectStat *rect;
2666
2667 rect = vnc_stat_rect(vd, x, y);
2668 if (rect->updated) {
2669 return ;
2670 }
2671 rect->times[rect->idx] = *tv;
2672 rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2673 rect->updated = true;
2674}
2675
1fc62412
SS
2676static int vnc_refresh_server_surface(VncDisplay *vd)
2677{
bea60dd7
PL
2678 int width = MIN(pixman_image_get_width(vd->guest.fb),
2679 pixman_image_get_width(vd->server));
2680 int height = MIN(pixman_image_get_height(vd->guest.fb),
2681 pixman_image_get_height(vd->server));
2682 int cmp_bytes, server_stride, min_stride, guest_stride, y = 0;
12b316d4 2683 uint8_t *guest_row0 = NULL, *server_row0;
41b4bef6 2684 VncState *vs;
1fc62412 2685 int has_dirty = 0;
9f64916d 2686 pixman_image_t *tmpbuf = NULL;
1fc62412 2687
80e0c8c3 2688 struct timeval tv = { 0, 0 };
999342a0 2689
80e0c8c3
CC
2690 if (!vd->non_adaptive) {
2691 gettimeofday(&tv, NULL);
2692 has_dirty = vnc_update_stats(vd, &tv);
2693 }
999342a0 2694
1fc62412
SS
2695 /*
2696 * Walk through the guest dirty map.
2697 * Check and copy modified bits from guest to server surface.
2698 * Update server dirty map.
2699 */
bea60dd7
PL
2700 server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
2701 server_stride = guest_stride = pixman_image_get_stride(vd->server);
2702 cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
2703 server_stride);
9f64916d
GH
2704 if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2705 int width = pixman_image_get_width(vd->server);
2706 tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
12b316d4
PL
2707 } else {
2708 guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
2709 guest_stride = pixman_image_get_stride(vd->guest.fb);
2710 }
bea60dd7 2711 min_stride = MIN(server_stride, guest_stride);
12b316d4 2712
12b316d4
PL
2713 for (;;) {
2714 int x;
2715 uint8_t *guest_ptr, *server_ptr;
2716 unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
2717 height * VNC_DIRTY_BPL(&vd->guest),
2718 y * VNC_DIRTY_BPL(&vd->guest));
2719 if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
2720 /* no more dirty bits */
2721 break;
2722 }
2723 y = offset / VNC_DIRTY_BPL(&vd->guest);
2724 x = offset % VNC_DIRTY_BPL(&vd->guest);
1fc62412 2725
12b316d4
PL
2726 server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
2727
2728 if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2729 qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
2730 guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
2731 } else {
2732 guest_ptr = guest_row0 + y * guest_stride;
2733 }
2734 guest_ptr += x * cmp_bytes;
2735
2736 for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
2737 x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
bea60dd7 2738 int _cmp_bytes = cmp_bytes;
12b316d4
PL
2739 if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
2740 continue;
2741 }
bea60dd7
PL
2742 if ((x + 1) * cmp_bytes > min_stride) {
2743 _cmp_bytes = min_stride - x * cmp_bytes;
2744 }
2745 if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
12b316d4
PL
2746 continue;
2747 }
bea60dd7 2748 memcpy(server_ptr, guest_ptr, _cmp_bytes);
12b316d4
PL
2749 if (!vd->non_adaptive) {
2750 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
2751 y, &tv);
1fc62412 2752 }
12b316d4
PL
2753 QTAILQ_FOREACH(vs, &vd->clients, next) {
2754 set_bit(x, vs->dirty[y]);
2755 }
2756 has_dirty++;
1fc62412 2757 }
12b316d4
PL
2758
2759 y++;
1fc62412 2760 }
9f64916d 2761 qemu_pixman_image_unref(tmpbuf);
1fc62412
SS
2762 return has_dirty;
2763}
2764
0f7b2864 2765static void vnc_refresh(DisplayChangeListener *dcl)
703bc68f 2766{
0f7b2864 2767 VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
41b4bef6
AS
2768 VncState *vs, *vn;
2769 int has_dirty, rects = 0;
703bc68f 2770
1dbfa005 2771 graphic_hw_update(NULL);
703bc68f 2772
bd023f95 2773 if (vnc_trylock_display(vd)) {
0f7b2864 2774 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
bd023f95
CC
2775 return;
2776 }
2777
1fc62412 2778 has_dirty = vnc_refresh_server_surface(vd);
bd023f95 2779 vnc_unlock_display(vd);
1fc62412 2780
41b4bef6 2781 QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
38ee14f4 2782 rects += vnc_update_client(vs, has_dirty, false);
6185c578 2783 /* vs might be free()ed here */
703bc68f 2784 }
bd023f95 2785
0f7b2864
GH
2786 if (QTAILQ_EMPTY(&vd->clients)) {
2787 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
83755c17 2788 return;
0f7b2864 2789 }
703bc68f 2790
2430ffe4 2791 if (has_dirty && rects) {
0f7b2864
GH
2792 vd->dcl.update_interval /= 2;
2793 if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
2794 vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
2795 }
2430ffe4 2796 } else {
0f7b2864
GH
2797 vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
2798 if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
2799 vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
2800 }
703bc68f
SS
2801 }
2802}
2803
2c8cf549
MT
2804static void vnc_connect(VncDisplay *vd, int csock,
2805 bool skipauth, bool websocket)
3aa3eea3 2806{
7267c094 2807 VncState *vs = g_malloc0(sizeof(VncState));
7d964c9d
CC
2808 int i;
2809
753b4053 2810 vs->csock = csock;
7e7e2ebc
DB
2811
2812 if (skipauth) {
2813 vs->auth = VNC_AUTH_NONE;
2814#ifdef CONFIG_VNC_TLS
2815 vs->subauth = VNC_AUTH_INVALID;
2816#endif
2817 } else {
2818 vs->auth = vd->auth;
2819#ifdef CONFIG_VNC_TLS
2820 vs->subauth = vd->subauth;
2821#endif
2822 }
2823
7267c094 2824 vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
7d964c9d 2825 for (i = 0; i < VNC_STAT_ROWS; ++i) {
7267c094 2826 vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
7d964c9d 2827 }
753b4053
AL
2828
2829 VNC_DEBUG("New client on socket %d\n", csock);
0f7b2864 2830 update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
f9e8cacc 2831 qemu_set_nonblock(vs->csock);
7536ee4b
TH
2832#ifdef CONFIG_VNC_WS
2833 if (websocket) {
2834 vs->websocket = 1;
0057a0d5
TH
2835#ifdef CONFIG_VNC_TLS
2836 if (vd->tls.x509cert) {
2837 qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek,
2838 NULL, vs);
2839 } else
2840#endif /* CONFIG_VNC_TLS */
2841 {
2842 qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read,
2843 NULL, vs);
2844 }
7536ee4b
TH
2845 } else
2846#endif /* CONFIG_VNC_WS */
2847 {
2848 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
2849 }
753b4053 2850
4a80dba3 2851 vnc_client_cache_addr(vs);
fb6ba0d5 2852 vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
8cf36489 2853 vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
4a80dba3 2854
753b4053 2855 vs->vd = vd;
7536ee4b
TH
2856
2857#ifdef CONFIG_VNC_WS
2858 if (!vs->websocket)
2859#endif
2860 {
2861 vnc_init_state(vs);
2862 }
2863}
2864
2865void vnc_init_state(VncState *vs)
2866{
6fd8e79a 2867 vs->initialized = true;
7536ee4b
TH
2868 VncDisplay *vd = vs->vd;
2869
753b4053
AL
2870 vs->last_x = -1;
2871 vs->last_y = -1;
2872
2873 vs->as.freq = 44100;
2874 vs->as.nchannels = 2;
2875 vs->as.fmt = AUD_FMT_S16;
2876 vs->as.endianness = 0;
2877
bd023f95 2878 qemu_mutex_init(&vs->output_mutex);
175b2a6e 2879 vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
bd023f95 2880
41b4bef6 2881 QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
1fc62412 2882
1dbfa005 2883 graphic_hw_update(NULL);
1fc62412 2884
3aa3eea3
AZ
2885 vnc_write(vs, "RFB 003.008\n", 12);
2886 vnc_flush(vs);
2887 vnc_read_when(vs, protocol_version, 12);
53762ddb 2888 reset_keys(vs);
3a0558b5
GH
2889 if (vs->vd->lock_key_sync)
2890 vs->led = qemu_add_led_event_handler(kbd_leds, vs);
753b4053 2891
37c34d9d
AL
2892 vs->mouse_mode_notifier.notify = check_pointer_type_change;
2893 qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
2894
198a0039 2895 /* vs might be free()ed here */
3aa3eea3
AZ
2896}
2897
7536ee4b 2898static void vnc_listen_read(void *opaque, bool websocket)
24236869 2899{
753b4053 2900 VncDisplay *vs = opaque;
24236869
FB
2901 struct sockaddr_in addr;
2902 socklen_t addrlen = sizeof(addr);
7536ee4b 2903 int csock;
24236869 2904
9f60ad50 2905 /* Catch-up */
1dbfa005 2906 graphic_hw_update(NULL);
7536ee4b
TH
2907#ifdef CONFIG_VNC_WS
2908 if (websocket) {
2909 csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
2910 } else
2911#endif /* CONFIG_VNC_WS */
2912 {
2913 csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
2914 }
9f60ad50 2915
753b4053 2916 if (csock != -1) {
2c8cf549 2917 vnc_connect(vs, csock, false, websocket);
24236869
FB
2918 }
2919}
2920
7536ee4b
TH
2921static void vnc_listen_regular_read(void *opaque)
2922{
2c8cf549 2923 vnc_listen_read(opaque, false);
7536ee4b
TH
2924}
2925
2926#ifdef CONFIG_VNC_WS
2927static void vnc_listen_websocket_read(void *opaque)
2928{
2c8cf549 2929 vnc_listen_read(opaque, true);
7536ee4b
TH
2930}
2931#endif /* CONFIG_VNC_WS */
2932
7c20b4a3
GH
2933static const DisplayChangeListenerOps dcl_ops = {
2934 .dpy_name = "vnc",
0f7b2864 2935 .dpy_refresh = vnc_refresh,
7c20b4a3
GH
2936 .dpy_gfx_copy = vnc_dpy_copy,
2937 .dpy_gfx_update = vnc_dpy_update,
c12aeb86 2938 .dpy_gfx_switch = vnc_dpy_switch,
7c20b4a3
GH
2939 .dpy_mouse_set = vnc_mouse_set,
2940 .dpy_cursor_define = vnc_dpy_cursor_define,
2941};
2942
71cab5ca 2943void vnc_display_init(DisplayState *ds)
24236869 2944{
7267c094 2945 VncDisplay *vs = g_malloc0(sizeof(*vs));
24236869 2946
753b4053 2947 vnc_display = vs;
24236869
FB
2948
2949 vs->lsock = -1;
7536ee4b
TH
2950#ifdef CONFIG_VNC_WS
2951 vs->lwebsock = -1;
2952#endif
24236869 2953
41b4bef6 2954 QTAILQ_INIT(&vs->clients);
3c9405a0 2955 vs->expires = TIME_MAX;
24236869 2956
40066175
GH
2957 if (keyboard_layout) {
2958 trace_vnc_key_map_init(keyboard_layout);
0483755a 2959 vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
40066175 2960 } else {
0483755a 2961 vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
40066175 2962 }
24236869 2963
24236869 2964 if (!vs->kbd_layout)
28a76be8 2965 exit(1);
24236869 2966
bd023f95
CC
2967 qemu_mutex_init(&vs->mutex);
2968 vnc_start_worker_thread();
bd023f95 2969
21ef45d7 2970 vs->dcl.ops = &dcl_ops;
5209089f 2971 register_displaychangelistener(&vs->dcl);
71cab5ca
TS
2972}
2973
6f43024c 2974
71a8cdec 2975static void vnc_display_close(DisplayState *ds)
71cab5ca 2976{
21ef45d7 2977 VncDisplay *vs = vnc_display;
71cab5ca 2978
452b4d88
AL
2979 if (!vs)
2980 return;
64641d87
MA
2981 g_free(vs->display);
2982 vs->display = NULL;
71cab5ca 2983 if (vs->lsock != -1) {
28a76be8
AL
2984 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
2985 close(vs->lsock);
2986 vs->lsock = -1;
71cab5ca 2987 }
7536ee4b
TH
2988#ifdef CONFIG_VNC_WS
2989 g_free(vs->ws_display);
2990 vs->ws_display = NULL;
2991 if (vs->lwebsock != -1) {
2992 qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
2993 close(vs->lwebsock);
2994 vs->lwebsock = -1;
2995 }
2996#endif /* CONFIG_VNC_WS */
70848515 2997 vs->auth = VNC_AUTH_INVALID;
eb38c52c 2998#ifdef CONFIG_VNC_TLS
8d5d2d4c 2999 vs->subauth = VNC_AUTH_INVALID;
5fb6c7a8 3000 vs->tls.x509verify = 0;
8d5d2d4c 3001#endif
70848515
TS
3002}
3003
3004int vnc_display_password(DisplayState *ds, const char *password)
3005{
21ef45d7 3006 VncDisplay *vs = vnc_display;
70848515 3007
7ef92331 3008 if (!vs) {
a6aa9d3e 3009 return -EINVAL;
7ef92331 3010 }
cf864569
GH
3011 if (vs->auth == VNC_AUTH_NONE) {
3012 error_printf_unless_qmp("If you want use passwords please enable "
3013 "password auth using '-vnc ${dpy},password'.");
3014 return -EINVAL;
1cd20f8b
AL
3015 }
3016
64641d87 3017 g_free(vs->password);
c14e9847 3018 vs->password = g_strdup(password);
a6aa9d3e
LC
3019
3020 return 0;
71cab5ca
TS
3021}
3022
3c9405a0
GH
3023int vnc_display_pw_expire(DisplayState *ds, time_t expires)
3024{
21ef45d7 3025 VncDisplay *vs = vnc_display;
3c9405a0 3026
1643f2b2
GH
3027 if (!vs) {
3028 return -EINVAL;
3029 }
3030
3c9405a0
GH
3031 vs->expires = expires;
3032 return 0;
3033}
3034
f92f8afe
AL
3035char *vnc_display_local_addr(DisplayState *ds)
3036{
21ef45d7 3037 VncDisplay *vs = vnc_display;
f92f8afe
AL
3038
3039 return vnc_socket_local_addr("%s:%s", vs->lsock);
3040}
3041
2d55f0e8 3042void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
71cab5ca 3043{
21ef45d7 3044 VncDisplay *vs = vnc_display;
70848515
TS
3045 const char *options;
3046 int password = 0;
3aa3eea3 3047 int reverse = 0;
eb38c52c 3048#ifdef CONFIG_VNC_TLS
3a702699 3049 int tls = 0, x509 = 0;
8d5d2d4c 3050#endif
2f9606b3
AL
3051#ifdef CONFIG_VNC_SASL
3052 int sasl = 0;
3053 int saslErr;
3054#endif
2ded6ad7 3055#if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
76655d6d 3056 int acl = 0;
2ded6ad7 3057#endif
3a0558b5 3058 int lock_key_sync = 1;
71cab5ca 3059
2d55f0e8
PB
3060 if (!vnc_display) {
3061 error_setg(errp, "VNC display not active");
3062 return;
3063 }
71cab5ca 3064 vnc_display_close(ds);
70848515 3065 if (strcmp(display, "none") == 0)
2d55f0e8 3066 return;
24236869 3067
1ce52c78 3068 vs->display = g_strdup(display);
8cf36489 3069 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
70848515
TS
3070
3071 options = display;
3072 while ((options = strchr(options, ','))) {
28a76be8
AL
3073 options++;
3074 if (strncmp(options, "password", 8) == 0) {
0f66998f 3075 if (fips_get_state()) {
2d55f0e8
PB
3076 error_setg(errp,
3077 "VNC password auth disabled due to FIPS mode, "
3078 "consider using the VeNCrypt or SASL authentication "
3079 "methods as an alternative");
1ce52c78 3080 goto fail;
0f66998f 3081 }
28a76be8
AL
3082 password = 1; /* Require password auth */
3083 } else if (strncmp(options, "reverse", 7) == 0) {
3084 reverse = 1;
cee8e6ad 3085 } else if (strncmp(options, "no-lock-key-sync", 16) == 0) {
3a0558b5 3086 lock_key_sync = 0;
2f9606b3 3087#ifdef CONFIG_VNC_SASL
28a76be8
AL
3088 } else if (strncmp(options, "sasl", 4) == 0) {
3089 sasl = 1; /* Require SASL auth */
2f9606b3 3090#endif
7536ee4b
TH
3091#ifdef CONFIG_VNC_WS
3092 } else if (strncmp(options, "websocket", 9) == 0) {
3093 char *start, *end;
3094 vs->websocket = 1;
3095
3096 /* Check for 'websocket=<port>' */
3097 start = strchr(options, '=');
3098 end = strchr(options, ',');
3099 if (start && (!end || (start < end))) {
3100 int len = end ? end-(start+1) : strlen(start+1);
3101 if (len < 6) {
3102 /* extract the host specification from display */
3103 char *host = NULL, *port = NULL, *host_end = NULL;
3104 port = g_strndup(start + 1, len);
3105
3106 /* ipv6 hosts have colons */
3107 end = strchr(display, ',');
3108 host_end = g_strrstr_len(display, end - display, ":");
3109
3110 if (host_end) {
3111 host = g_strndup(display, host_end - display + 1);
3112 } else {
3113 host = g_strndup(":", 1);
3114 }
3115 vs->ws_display = g_strconcat(host, port, NULL);
3116 g_free(host);
3117 g_free(port);
3118 }
3119 }
3120#endif /* CONFIG_VNC_WS */
eb38c52c 3121#ifdef CONFIG_VNC_TLS
28a76be8
AL
3122 } else if (strncmp(options, "tls", 3) == 0) {
3123 tls = 1; /* Require TLS */
3124 } else if (strncmp(options, "x509", 4) == 0) {
3125 char *start, *end;
3126 x509 = 1; /* Require x509 certificates */
3127 if (strncmp(options, "x509verify", 10) == 0)
3128 vs->tls.x509verify = 1; /* ...and verify client certs */
3129
3130 /* Now check for 'x509=/some/path' postfix
3131 * and use that to setup x509 certificate/key paths */
3132 start = strchr(options, '=');
3133 end = strchr(options, ',');
3134 if (start && (!end || (start < end))) {
3135 int len = end ? end-(start+1) : strlen(start+1);
7267c094 3136 char *path = g_strndup(start + 1, len);
28a76be8
AL
3137
3138 VNC_DEBUG("Trying certificate path '%s'\n", path);
3139 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
2d55f0e8 3140 error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
7267c094 3141 g_free(path);
1ce52c78 3142 goto fail;
28a76be8 3143 }
7267c094 3144 g_free(path);
28a76be8 3145 } else {
2d55f0e8 3146 error_setg(errp, "No certificate path provided");
1ce52c78 3147 goto fail;
28a76be8 3148 }
8d5d2d4c 3149#endif
2ded6ad7 3150#if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
28a76be8
AL
3151 } else if (strncmp(options, "acl", 3) == 0) {
3152 acl = 1;
2ded6ad7 3153#endif
6f9c78c1 3154 } else if (strncmp(options, "lossy", 5) == 0) {
e22492d3 3155#ifdef CONFIG_VNC_JPEG
6f9c78c1 3156 vs->lossy = true;
e22492d3 3157#endif
7eff5742 3158 } else if (strncmp(options, "non-adaptive", 12) == 0) {
80e0c8c3 3159 vs->non_adaptive = true;
8cf36489
GH
3160 } else if (strncmp(options, "share=", 6) == 0) {
3161 if (strncmp(options+6, "ignore", 6) == 0) {
3162 vs->share_policy = VNC_SHARE_POLICY_IGNORE;
3163 } else if (strncmp(options+6, "allow-exclusive", 15) == 0) {
3164 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
3165 } else if (strncmp(options+6, "force-shared", 12) == 0) {
3166 vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
3167 } else {
2d55f0e8 3168 error_setg(errp, "unknown vnc share= option");
1ce52c78 3169 goto fail;
8cf36489 3170 }
28a76be8 3171 }
70848515
TS
3172 }
3173
e22492d3
PL
3174 /* adaptive updates are only used with tight encoding and
3175 * if lossy updates are enabled so we can disable all the
3176 * calculations otherwise */
3177 if (!vs->lossy) {
3178 vs->non_adaptive = true;
3179 }
3180
76655d6d
AL
3181#ifdef CONFIG_VNC_TLS
3182 if (acl && x509 && vs->tls.x509verify) {
28a76be8
AL
3183 if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
3184 fprintf(stderr, "Failed to create x509 dname ACL\n");
3185 exit(1);
3186 }
76655d6d
AL
3187 }
3188#endif
3189#ifdef CONFIG_VNC_SASL
3190 if (acl && sasl) {
28a76be8
AL
3191 if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
3192 fprintf(stderr, "Failed to create username ACL\n");
3193 exit(1);
3194 }
76655d6d
AL
3195 }
3196#endif
3197
2f9606b3
AL
3198 /*
3199 * Combinations we support here:
3200 *
3201 * - no-auth (clear text, no auth)
3202 * - password (clear text, weak auth)
3203 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
3204 * - tls (encrypt, weak anonymous creds, no auth)
3205 * - tls + password (encrypt, weak anonymous creds, weak auth)
3206 * - tls + sasl (encrypt, weak anonymous creds, good auth)
3207 * - tls + x509 (encrypt, good x509 creds, no auth)
3208 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
3209 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
3210 *
3211 * NB1. TLS is a stackable auth scheme.
3212 * NB2. the x509 schemes have option to validate a client cert dname
3213 */
70848515 3214 if (password) {
eb38c52c 3215#ifdef CONFIG_VNC_TLS
28a76be8
AL
3216 if (tls) {
3217 vs->auth = VNC_AUTH_VENCRYPT;
3218 if (x509) {
3219 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3220 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
3221 } else {
3222 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3223 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
3224 }
3225 } else {
2f9606b3 3226#endif /* CONFIG_VNC_TLS */
28a76be8
AL
3227 VNC_DEBUG("Initializing VNC server with password auth\n");
3228 vs->auth = VNC_AUTH_VNC;
eb38c52c 3229#ifdef CONFIG_VNC_TLS
28a76be8
AL
3230 vs->subauth = VNC_AUTH_INVALID;
3231 }
2f9606b3
AL
3232#endif /* CONFIG_VNC_TLS */
3233#ifdef CONFIG_VNC_SASL
3234 } else if (sasl) {
3235#ifdef CONFIG_VNC_TLS
3236 if (tls) {
3237 vs->auth = VNC_AUTH_VENCRYPT;
3238 if (x509) {
28a76be8 3239 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2f9606b3
AL
3240 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
3241 } else {
28a76be8 3242 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2f9606b3
AL
3243 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
3244 }
3245 } else {
3246#endif /* CONFIG_VNC_TLS */
28a76be8 3247 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2f9606b3
AL
3248 vs->auth = VNC_AUTH_SASL;
3249#ifdef CONFIG_VNC_TLS
3250 vs->subauth = VNC_AUTH_INVALID;
3251 }
3252#endif /* CONFIG_VNC_TLS */
3253#endif /* CONFIG_VNC_SASL */
70848515 3254 } else {
eb38c52c 3255#ifdef CONFIG_VNC_TLS
28a76be8
AL
3256 if (tls) {
3257 vs->auth = VNC_AUTH_VENCRYPT;
3258 if (x509) {
3259 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3260 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
3261 } else {
3262 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3263 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
3264 }
3265 } else {
8d5d2d4c 3266#endif
28a76be8
AL
3267 VNC_DEBUG("Initializing VNC server with no auth\n");
3268 vs->auth = VNC_AUTH_NONE;
eb38c52c 3269#ifdef CONFIG_VNC_TLS
28a76be8
AL
3270 vs->subauth = VNC_AUTH_INVALID;
3271 }
8d5d2d4c 3272#endif
70848515 3273 }
24236869 3274
2f9606b3
AL
3275#ifdef CONFIG_VNC_SASL
3276 if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
2d55f0e8
PB
3277 error_setg(errp, "Failed to initialize SASL auth: %s",
3278 sasl_errstring(saslErr, NULL, NULL));
1ce52c78 3279 goto fail;
2f9606b3
AL
3280 }
3281#endif
3a0558b5 3282 vs->lock_key_sync = lock_key_sync;
2f9606b3 3283
3aa3eea3 3284 if (reverse) {
9712ecaf 3285 /* connect to viewer */
007fcd3e
PB
3286 int csock;
3287 vs->lsock = -1;
7536ee4b
TH
3288#ifdef CONFIG_VNC_WS
3289 vs->lwebsock = -1;
3290#endif
007fcd3e 3291 if (strncmp(display, "unix:", 5) == 0) {
2d55f0e8 3292 csock = unix_connect(display+5, errp);
3aa3eea3 3293 } else {
2d55f0e8 3294 csock = inet_connect(display, errp);
3aa3eea3 3295 }
007fcd3e
PB
3296 if (csock < 0) {
3297 goto fail;
3298 }
2c8cf549 3299 vnc_connect(vs, csock, false, false);
9712ecaf
AL
3300 } else {
3301 /* listen for connects */
3302 char *dpy;
7267c094 3303 dpy = g_malloc(256);
9712ecaf 3304 if (strncmp(display, "unix:", 5) == 0) {
bc575e95 3305 pstrcpy(dpy, 256, "unix:");
2d55f0e8 3306 vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
9712ecaf 3307 } else {
029409e5 3308 vs->lsock = inet_listen(display, dpy, 256,
2d55f0e8 3309 SOCK_STREAM, 5900, errp);
7536ee4b
TH
3310 if (vs->lsock < 0) {
3311 g_free(dpy);
3312 goto fail;
3313 }
3314#ifdef CONFIG_VNC_WS
3315 if (vs->websocket) {
3316 if (vs->ws_display) {
3317 vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
3318 SOCK_STREAM, 0, errp);
3319 } else {
3320 vs->lwebsock = inet_listen(vs->display, NULL, 256,
3321 SOCK_STREAM, 5700, errp);
3322 }
3323
3324 if (vs->lwebsock < 0) {
3325 if (vs->lsock) {
3326 close(vs->lsock);
3327 vs->lsock = -1;
3328 }
3329 g_free(dpy);
3330 goto fail;
3331 }
3332 }
3333#endif /* CONFIG_VNC_WS */
9712ecaf 3334 }
1ce52c78
PB
3335 g_free(vs->display);
3336 vs->display = dpy;
7536ee4b
TH
3337 qemu_set_fd_handler2(vs->lsock, NULL,
3338 vnc_listen_regular_read, NULL, vs);
3339#ifdef CONFIG_VNC_WS
3340 if (vs->websocket) {
3341 qemu_set_fd_handler2(vs->lwebsock, NULL,
3342 vnc_listen_websocket_read, NULL, vs);
3343 }
3344#endif /* CONFIG_VNC_WS */
24236869 3345 }
2d55f0e8 3346 return;
1ce52c78
PB
3347
3348fail:
3349 g_free(vs->display);
3350 vs->display = NULL;
7536ee4b
TH
3351#ifdef CONFIG_VNC_WS
3352 g_free(vs->ws_display);
3353 vs->ws_display = NULL;
3354#endif /* CONFIG_VNC_WS */
24236869 3355}
13661089 3356
2c8cf549 3357void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth)
13661089 3358{
21ef45d7 3359 VncDisplay *vs = vnc_display;
13661089 3360
2c8cf549 3361 vnc_connect(vs, csock, skipauth, false);
13661089 3362}
This page took 1.362941 seconds and 4 git commands to generate.