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