Line | Count | Source |
1 | | /* |
2 | | * QEMU VNC display driver |
3 | | * |
4 | | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> |
5 | | * Copyright (C) 2006 Fabrice Bellard |
6 | | * Copyright (C) 2009 Red Hat, Inc |
7 | | * |
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 | | |
27 | | #include "qemu/osdep.h" |
28 | | #include "vnc.h" |
29 | | #include "vnc-jobs.h" |
30 | | #include "trace.h" |
31 | | #include "hw/core/qdev.h" |
32 | | #include "system/system.h" |
33 | | #include "system/runstate.h" |
34 | | #include "qemu/error-report.h" |
35 | | #include "qemu/main-loop.h" |
36 | | #include "qemu/module.h" |
37 | | #include "qemu/option.h" |
38 | | #include "qemu/sockets.h" |
39 | | #include "qemu/timer.h" |
40 | | #include "authz/list.h" |
41 | | #include "qemu/config-file.h" |
42 | | #include "qapi/qapi-emit-events.h" |
43 | | #include "qapi/qapi-events-ui.h" |
44 | | #include "qapi/error.h" |
45 | | #include "qapi/qapi-commands-ui.h" |
46 | | #include "standard-headers/linux/input-event-codes.h" |
47 | | #include "ui/console.h" |
48 | | #include "ui/input.h" |
49 | | #include "crypto/hash.h" |
50 | | #include "crypto/tlscreds.h" |
51 | | #include "crypto/tlscredsanon.h" |
52 | | #include "crypto/tlscredsx509.h" |
53 | | #include "crypto/random.h" |
54 | | #include "crypto/secret_common.h" |
55 | | #include "qom/object_interfaces.h" |
56 | | #include "qemu/cutils.h" |
57 | | #include "qemu/help_option.h" |
58 | | #include "io/dns-resolver.h" |
59 | | #include "monitor/monitor.h" |
60 | | |
61 | 0 | #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT |
62 | 0 | #define VNC_REFRESH_INTERVAL_INC 50 |
63 | 0 | #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE |
64 | | static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; |
65 | | static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; |
66 | | |
67 | | #include "vnc_keysym.h" |
68 | | #include "crypto/cipher.h" |
69 | | |
70 | | static QTAILQ_HEAD(, VncDisplay) vnc_displays = |
71 | | QTAILQ_HEAD_INITIALIZER(vnc_displays); |
72 | | |
73 | | static int vnc_cursor_define(VncState *vs); |
74 | | static void vnc_update_throttle_offset(VncState *vs); |
75 | | static void vnc_disconnect_finish(VncState *vs); |
76 | | |
77 | | static void vnc_set_share_mode(VncState *vs, VncShareMode mode) |
78 | 0 | { |
79 | 0 | trace_vnc_set_share_mode(vs, vs->ioc, vs->share_mode, mode); |
80 | |
|
81 | 0 | switch (vs->share_mode) { |
82 | 0 | case VNC_SHARE_MODE_CONNECTING: |
83 | 0 | vs->vd->num_connecting--; |
84 | 0 | break; |
85 | 0 | case VNC_SHARE_MODE_SHARED: |
86 | 0 | vs->vd->num_shared--; |
87 | 0 | break; |
88 | 0 | case VNC_SHARE_MODE_EXCLUSIVE: |
89 | 0 | vs->vd->num_exclusive--; |
90 | 0 | break; |
91 | 0 | default: |
92 | 0 | break; |
93 | 0 | } |
94 | | |
95 | 0 | vs->share_mode = mode; |
96 | |
|
97 | 0 | switch (vs->share_mode) { |
98 | 0 | case VNC_SHARE_MODE_CONNECTING: |
99 | 0 | vs->vd->num_connecting++; |
100 | 0 | break; |
101 | 0 | case VNC_SHARE_MODE_SHARED: |
102 | 0 | vs->vd->num_shared++; |
103 | 0 | break; |
104 | 0 | case VNC_SHARE_MODE_EXCLUSIVE: |
105 | 0 | vs->vd->num_exclusive++; |
106 | 0 | break; |
107 | 0 | default: |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | |
113 | | static void vnc_init_basic_info(SocketAddress *addr, |
114 | | VncBasicInfo *info, |
115 | | Error **errp) |
116 | 0 | { |
117 | 0 | switch (addr->type) { |
118 | 0 | case SOCKET_ADDRESS_TYPE_INET: |
119 | 0 | info->host = g_strdup(addr->u.inet.host); |
120 | 0 | info->service = g_strdup(addr->u.inet.port); |
121 | 0 | if (addr->u.inet.ipv6) { |
122 | 0 | info->family = NETWORK_ADDRESS_FAMILY_IPV6; |
123 | 0 | } else { |
124 | 0 | info->family = NETWORK_ADDRESS_FAMILY_IPV4; |
125 | 0 | } |
126 | 0 | break; |
127 | | |
128 | 0 | case SOCKET_ADDRESS_TYPE_UNIX: |
129 | 0 | info->host = g_strdup(""); |
130 | 0 | info->service = g_strdup(addr->u.q_unix.path); |
131 | 0 | info->family = NETWORK_ADDRESS_FAMILY_UNIX; |
132 | 0 | break; |
133 | | |
134 | 0 | case SOCKET_ADDRESS_TYPE_VSOCK: |
135 | 0 | case SOCKET_ADDRESS_TYPE_FD: |
136 | 0 | error_setg(errp, "Unsupported socket address type %s", |
137 | 0 | SocketAddressType_str(addr->type)); |
138 | 0 | break; |
139 | 0 | default: |
140 | 0 | abort(); |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, |
145 | | VncBasicInfo *info, |
146 | | Error **errp) |
147 | 0 | { |
148 | 0 | SocketAddress *addr = NULL; |
149 | |
|
150 | 0 | if (!ioc) { |
151 | 0 | error_setg(errp, "No listener socket available"); |
152 | 0 | return; |
153 | 0 | } |
154 | | |
155 | 0 | addr = qio_channel_socket_get_local_address(ioc, errp); |
156 | 0 | if (!addr) { |
157 | 0 | return; |
158 | 0 | } |
159 | | |
160 | 0 | vnc_init_basic_info(addr, info, errp); |
161 | 0 | qapi_free_SocketAddress(addr); |
162 | 0 | } |
163 | | |
164 | | static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, |
165 | | VncBasicInfo *info, |
166 | | Error **errp) |
167 | 0 | { |
168 | 0 | SocketAddress *addr = NULL; |
169 | |
|
170 | 0 | addr = qio_channel_socket_get_remote_address(ioc, errp); |
171 | 0 | if (!addr) { |
172 | 0 | return; |
173 | 0 | } |
174 | | |
175 | 0 | vnc_init_basic_info(addr, info, errp); |
176 | 0 | qapi_free_SocketAddress(addr); |
177 | 0 | } |
178 | | |
179 | | static const char *vnc_auth_name(int auth, int subauth) |
180 | 0 | { |
181 | 0 | switch (auth) { |
182 | 0 | case VNC_AUTH_INVALID: |
183 | 0 | return "invalid"; |
184 | 0 | case VNC_AUTH_NONE: |
185 | 0 | return "none"; |
186 | 0 | case VNC_AUTH_VNC: |
187 | 0 | return "vnc"; |
188 | 0 | case VNC_AUTH_RA2: |
189 | 0 | return "ra2"; |
190 | 0 | case VNC_AUTH_RA2NE: |
191 | 0 | return "ra2ne"; |
192 | 0 | case VNC_AUTH_TIGHT: |
193 | 0 | return "tight"; |
194 | 0 | case VNC_AUTH_ULTRA: |
195 | 0 | return "ultra"; |
196 | 0 | case VNC_AUTH_TLS: |
197 | 0 | return "tls"; |
198 | 0 | case VNC_AUTH_VENCRYPT: |
199 | 0 | switch (subauth) { |
200 | 0 | case VNC_AUTH_VENCRYPT_PLAIN: |
201 | 0 | return "vencrypt+plain"; |
202 | 0 | case VNC_AUTH_VENCRYPT_TLSNONE: |
203 | 0 | return "vencrypt+tls+none"; |
204 | 0 | case VNC_AUTH_VENCRYPT_TLSVNC: |
205 | 0 | return "vencrypt+tls+vnc"; |
206 | 0 | case VNC_AUTH_VENCRYPT_TLSPLAIN: |
207 | 0 | return "vencrypt+tls+plain"; |
208 | 0 | case VNC_AUTH_VENCRYPT_X509NONE: |
209 | 0 | return "vencrypt+x509+none"; |
210 | 0 | case VNC_AUTH_VENCRYPT_X509VNC: |
211 | 0 | return "vencrypt+x509+vnc"; |
212 | 0 | case VNC_AUTH_VENCRYPT_X509PLAIN: |
213 | 0 | return "vencrypt+x509+plain"; |
214 | 0 | case VNC_AUTH_VENCRYPT_TLSSASL: |
215 | 0 | return "vencrypt+tls+sasl"; |
216 | 0 | case VNC_AUTH_VENCRYPT_X509SASL: |
217 | 0 | return "vencrypt+x509+sasl"; |
218 | 0 | default: |
219 | 0 | return "vencrypt"; |
220 | 0 | } |
221 | 0 | case VNC_AUTH_SASL: |
222 | 0 | return "sasl"; |
223 | 0 | } |
224 | 0 | return "unknown"; |
225 | 0 | } |
226 | | |
227 | | static VncServerInfo *vnc_server_info_get(VncDisplay *vd) |
228 | 0 | { |
229 | 0 | VncServerInfo *info; |
230 | 0 | Error *err = NULL; |
231 | |
|
232 | 0 | if (!vd->listener || !qio_net_listener_nsioc(vd->listener)) { |
233 | 0 | return NULL; |
234 | 0 | } |
235 | | |
236 | 0 | info = g_malloc0(sizeof(*info)); |
237 | 0 | vnc_init_basic_info_from_server_addr(qio_net_listener_sioc(vd->listener, 0), |
238 | 0 | qapi_VncServerInfo_base(info), &err); |
239 | 0 | info->auth = g_strdup(vnc_auth_name(vd->auth, vd->subauth)); |
240 | 0 | if (err) { |
241 | 0 | qapi_free_VncServerInfo(info); |
242 | 0 | info = NULL; |
243 | 0 | error_free(err); |
244 | 0 | } |
245 | 0 | return info; |
246 | 0 | } |
247 | | |
248 | | static void vnc_client_cache_auth(VncState *client) |
249 | 0 | { |
250 | 0 | if (!client->info) { |
251 | 0 | return; |
252 | 0 | } |
253 | | |
254 | 0 | if (client->tls) { |
255 | 0 | client->info->x509_dname = |
256 | 0 | qcrypto_tls_session_get_peer_name(client->tls); |
257 | 0 | } |
258 | | #ifdef CONFIG_VNC_SASL |
259 | | if (client->sasl.conn && |
260 | | client->sasl.username) { |
261 | | client->info->sasl_username = g_strdup(client->sasl.username); |
262 | | } |
263 | | #endif |
264 | 0 | } |
265 | | |
266 | | static void vnc_client_cache_addr(VncState *client) |
267 | 0 | { |
268 | 0 | Error *err = NULL; |
269 | |
|
270 | 0 | client->info = g_malloc0(sizeof(*client->info)); |
271 | 0 | vnc_init_basic_info_from_remote_addr(client->sioc, |
272 | 0 | qapi_VncClientInfo_base(client->info), |
273 | 0 | &err); |
274 | 0 | client->info->websocket = client->websocket; |
275 | 0 | if (err) { |
276 | 0 | qapi_free_VncClientInfo(client->info); |
277 | 0 | client->info = NULL; |
278 | 0 | error_free(err); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | | static void vnc_qmp_event(VncState *vs, QAPIEvent event) |
283 | 0 | { |
284 | 0 | VncServerInfo *si; |
285 | |
|
286 | 0 | if (!vs->info) { |
287 | 0 | return; |
288 | 0 | } |
289 | | |
290 | 0 | si = vnc_server_info_get(vs->vd); |
291 | 0 | if (!si) { |
292 | 0 | return; |
293 | 0 | } |
294 | | |
295 | 0 | switch (event) { |
296 | 0 | case QAPI_EVENT_VNC_CONNECTED: |
297 | 0 | qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info)); |
298 | 0 | break; |
299 | 0 | case QAPI_EVENT_VNC_INITIALIZED: |
300 | 0 | qapi_event_send_vnc_initialized(si, vs->info); |
301 | 0 | break; |
302 | 0 | case QAPI_EVENT_VNC_DISCONNECTED: |
303 | 0 | qapi_event_send_vnc_disconnected(si, vs->info); |
304 | 0 | break; |
305 | 0 | default: |
306 | 0 | break; |
307 | 0 | } |
308 | | |
309 | 0 | qapi_free_VncServerInfo(si); |
310 | 0 | } |
311 | | |
312 | | static VncClientInfo *qmp_query_vnc_client(const VncState *client) |
313 | 0 | { |
314 | 0 | VncClientInfo *info; |
315 | 0 | Error *err = NULL; |
316 | |
|
317 | 0 | info = g_malloc0(sizeof(*info)); |
318 | |
|
319 | 0 | vnc_init_basic_info_from_remote_addr(client->sioc, |
320 | 0 | qapi_VncClientInfo_base(info), |
321 | 0 | &err); |
322 | 0 | if (err) { |
323 | 0 | error_free(err); |
324 | 0 | qapi_free_VncClientInfo(info); |
325 | 0 | return NULL; |
326 | 0 | } |
327 | | |
328 | 0 | info->websocket = client->websocket; |
329 | |
|
330 | 0 | if (client->tls) { |
331 | 0 | info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); |
332 | 0 | } |
333 | | #ifdef CONFIG_VNC_SASL |
334 | | if (client->sasl.conn && client->sasl.username) { |
335 | | info->sasl_username = g_strdup(client->sasl.username); |
336 | | } |
337 | | #endif |
338 | |
|
339 | 0 | return info; |
340 | 0 | } |
341 | | |
342 | | static VncDisplay *vnc_display_find(const char *id) |
343 | 0 | { |
344 | 0 | VncDisplay *vd; |
345 | |
|
346 | 0 | if (id == NULL) { |
347 | 0 | return QTAILQ_FIRST(&vnc_displays); |
348 | 0 | } |
349 | 0 | QTAILQ_FOREACH(vd, &vnc_displays, next) { |
350 | 0 | if (strcmp(id, vd->id) == 0) { |
351 | 0 | return vd; |
352 | 0 | } |
353 | 0 | } |
354 | 0 | return NULL; |
355 | 0 | } |
356 | | |
357 | | static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) |
358 | 0 | { |
359 | 0 | VncClientInfoList *prev = NULL; |
360 | 0 | VncState *client; |
361 | |
|
362 | 0 | QTAILQ_FOREACH(client, &vd->clients, next) { |
363 | 0 | QAPI_LIST_PREPEND(prev, qmp_query_vnc_client(client)); |
364 | 0 | } |
365 | 0 | return prev; |
366 | 0 | } |
367 | | |
368 | | VncInfo *qmp_query_vnc(Error **errp) |
369 | 0 | { |
370 | 0 | VncInfo *info = g_malloc0(sizeof(*info)); |
371 | 0 | VncDisplay *vd = vnc_display_find(NULL); |
372 | 0 | SocketAddress *addr = NULL; |
373 | |
|
374 | 0 | if (vd == NULL || !vd->listener || !qio_net_listener_nsioc(vd->listener)) { |
375 | 0 | info->enabled = false; |
376 | 0 | } else { |
377 | 0 | info->enabled = true; |
378 | | |
379 | | /* for compatibility with the original command */ |
380 | 0 | info->has_clients = true; |
381 | 0 | info->clients = qmp_query_client_list(vd); |
382 | |
|
383 | 0 | addr = qio_net_listener_get_local_address(vd->listener, 0, errp); |
384 | 0 | if (!addr) { |
385 | 0 | goto out_error; |
386 | 0 | } |
387 | | |
388 | 0 | switch (addr->type) { |
389 | 0 | case SOCKET_ADDRESS_TYPE_INET: |
390 | 0 | info->host = g_strdup(addr->u.inet.host); |
391 | 0 | info->service = g_strdup(addr->u.inet.port); |
392 | 0 | if (addr->u.inet.ipv6) { |
393 | 0 | info->family = NETWORK_ADDRESS_FAMILY_IPV6; |
394 | 0 | } else { |
395 | 0 | info->family = NETWORK_ADDRESS_FAMILY_IPV4; |
396 | 0 | } |
397 | 0 | break; |
398 | | |
399 | 0 | case SOCKET_ADDRESS_TYPE_UNIX: |
400 | 0 | info->host = g_strdup(""); |
401 | 0 | info->service = g_strdup(addr->u.q_unix.path); |
402 | 0 | info->family = NETWORK_ADDRESS_FAMILY_UNIX; |
403 | 0 | break; |
404 | | |
405 | 0 | case SOCKET_ADDRESS_TYPE_VSOCK: |
406 | 0 | case SOCKET_ADDRESS_TYPE_FD: |
407 | 0 | error_setg(errp, "Unsupported socket address type %s", |
408 | 0 | SocketAddressType_str(addr->type)); |
409 | 0 | goto out_error; |
410 | 0 | default: |
411 | 0 | abort(); |
412 | 0 | } |
413 | | |
414 | 0 | info->has_family = true; |
415 | |
|
416 | 0 | info->auth = g_strdup(vnc_auth_name(vd->auth, vd->subauth)); |
417 | 0 | } |
418 | | |
419 | 0 | qapi_free_SocketAddress(addr); |
420 | 0 | return info; |
421 | | |
422 | 0 | out_error: |
423 | 0 | qapi_free_SocketAddress(addr); |
424 | 0 | qapi_free_VncInfo(info); |
425 | 0 | return NULL; |
426 | 0 | } |
427 | | |
428 | | |
429 | | static void qmp_query_auth(int auth, int subauth, |
430 | | VncPrimaryAuth *qmp_auth, |
431 | | VncVencryptSubAuth *qmp_vencrypt, |
432 | | bool *qmp_has_vencrypt); |
433 | | |
434 | | static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc, |
435 | | bool websocket, |
436 | | int auth, |
437 | | int subauth, |
438 | | VncServerInfo2List *prev) |
439 | 0 | { |
440 | 0 | VncServerInfo2 *info; |
441 | 0 | Error *err = NULL; |
442 | 0 | SocketAddress *addr; |
443 | |
|
444 | 0 | addr = qio_channel_socket_get_local_address(ioc, NULL); |
445 | 0 | if (!addr) { |
446 | 0 | return prev; |
447 | 0 | } |
448 | | |
449 | 0 | info = g_new0(VncServerInfo2, 1); |
450 | 0 | vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err); |
451 | 0 | qapi_free_SocketAddress(addr); |
452 | 0 | if (err) { |
453 | 0 | qapi_free_VncServerInfo2(info); |
454 | 0 | error_free(err); |
455 | 0 | return prev; |
456 | 0 | } |
457 | 0 | info->websocket = websocket; |
458 | |
|
459 | 0 | qmp_query_auth(auth, subauth, &info->auth, |
460 | 0 | &info->vencrypt, &info->has_vencrypt); |
461 | |
|
462 | 0 | QAPI_LIST_PREPEND(prev, info); |
463 | 0 | return prev; |
464 | 0 | } |
465 | | |
466 | | static void qmp_query_auth(int auth, int subauth, |
467 | | VncPrimaryAuth *qmp_auth, |
468 | | VncVencryptSubAuth *qmp_vencrypt, |
469 | | bool *qmp_has_vencrypt) |
470 | 0 | { |
471 | 0 | switch (auth) { |
472 | 0 | case VNC_AUTH_VNC: |
473 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_VNC; |
474 | 0 | break; |
475 | 0 | case VNC_AUTH_RA2: |
476 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_RA2; |
477 | 0 | break; |
478 | 0 | case VNC_AUTH_RA2NE: |
479 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_RA2NE; |
480 | 0 | break; |
481 | 0 | case VNC_AUTH_TIGHT: |
482 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_TIGHT; |
483 | 0 | break; |
484 | 0 | case VNC_AUTH_ULTRA: |
485 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_ULTRA; |
486 | 0 | break; |
487 | 0 | case VNC_AUTH_TLS: |
488 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_TLS; |
489 | 0 | break; |
490 | 0 | case VNC_AUTH_VENCRYPT: |
491 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT; |
492 | 0 | *qmp_has_vencrypt = true; |
493 | 0 | switch (subauth) { |
494 | 0 | case VNC_AUTH_VENCRYPT_PLAIN: |
495 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; |
496 | 0 | break; |
497 | 0 | case VNC_AUTH_VENCRYPT_TLSNONE: |
498 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; |
499 | 0 | break; |
500 | 0 | case VNC_AUTH_VENCRYPT_TLSVNC: |
501 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; |
502 | 0 | break; |
503 | 0 | case VNC_AUTH_VENCRYPT_TLSPLAIN: |
504 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; |
505 | 0 | break; |
506 | 0 | case VNC_AUTH_VENCRYPT_X509NONE: |
507 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; |
508 | 0 | break; |
509 | 0 | case VNC_AUTH_VENCRYPT_X509VNC: |
510 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; |
511 | 0 | break; |
512 | 0 | case VNC_AUTH_VENCRYPT_X509PLAIN: |
513 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; |
514 | 0 | break; |
515 | 0 | case VNC_AUTH_VENCRYPT_TLSSASL: |
516 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; |
517 | 0 | break; |
518 | 0 | case VNC_AUTH_VENCRYPT_X509SASL: |
519 | 0 | *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; |
520 | 0 | break; |
521 | 0 | default: |
522 | 0 | *qmp_has_vencrypt = false; |
523 | 0 | break; |
524 | 0 | } |
525 | 0 | break; |
526 | 0 | case VNC_AUTH_SASL: |
527 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_SASL; |
528 | 0 | break; |
529 | 0 | case VNC_AUTH_NONE: |
530 | 0 | default: |
531 | 0 | *qmp_auth = VNC_PRIMARY_AUTH_NONE; |
532 | 0 | break; |
533 | 0 | } |
534 | 0 | } |
535 | | |
536 | | VncInfo2List *qmp_query_vnc_servers(Error **errp) |
537 | 0 | { |
538 | 0 | VncInfo2List *prev = NULL; |
539 | 0 | VncInfo2 *info; |
540 | 0 | VncDisplay *vd; |
541 | 0 | DeviceState *dev; |
542 | 0 | size_t i; |
543 | |
|
544 | 0 | QTAILQ_FOREACH(vd, &vnc_displays, next) { |
545 | 0 | size_t nsioc = 0; |
546 | |
|
547 | 0 | info = g_new0(VncInfo2, 1); |
548 | 0 | info->id = g_strdup(vd->id); |
549 | 0 | info->clients = qmp_query_client_list(vd); |
550 | 0 | qmp_query_auth(vd->auth, vd->subauth, &info->auth, |
551 | 0 | &info->vencrypt, &info->has_vencrypt); |
552 | 0 | if (vd->dcl.con) { |
553 | 0 | Object *obj = object_property_get_link(OBJECT(vd->dcl.con), |
554 | 0 | "device", NULL); |
555 | 0 | if (obj) { |
556 | 0 | dev = DEVICE(obj); |
557 | 0 | info->display = g_strdup(dev->id); |
558 | 0 | } |
559 | 0 | } |
560 | 0 | if (vd->listener != NULL) { |
561 | 0 | nsioc = qio_net_listener_nsioc(vd->listener); |
562 | 0 | } |
563 | 0 | for (i = 0; i < nsioc; i++) { |
564 | 0 | info->server = qmp_query_server_entry( |
565 | 0 | qio_net_listener_sioc(vd->listener, i), false, vd->auth, |
566 | 0 | vd->subauth, info->server); |
567 | 0 | } |
568 | 0 | nsioc = 0; |
569 | 0 | if (vd->wslistener) { |
570 | 0 | nsioc = qio_net_listener_nsioc(vd->wslistener); |
571 | 0 | } |
572 | 0 | for (i = 0; i < nsioc; i++) { |
573 | 0 | info->server = qmp_query_server_entry( |
574 | 0 | qio_net_listener_sioc(vd->wslistener, i), true, vd->ws_auth, |
575 | 0 | vd->ws_subauth, info->server); |
576 | 0 | } |
577 | |
|
578 | 0 | QAPI_LIST_PREPEND(prev, info); |
579 | 0 | } |
580 | 0 | return prev; |
581 | 0 | } |
582 | | |
583 | | bool vnc_display_reload_certs(const char *id, Error **errp) |
584 | 0 | { |
585 | 0 | VncDisplay *vd = vnc_display_find(id); |
586 | |
|
587 | 0 | if (!vd) { |
588 | 0 | error_setg(errp, "Can not find vnc display"); |
589 | 0 | return false; |
590 | 0 | } |
591 | | |
592 | 0 | if (!vd->tlscreds) { |
593 | 0 | error_setg(errp, "vnc tls is not enabled"); |
594 | 0 | return false; |
595 | 0 | } |
596 | | |
597 | 0 | if (!qcrypto_tls_creds_reload(vd->tlscreds, errp)) { |
598 | 0 | return false; |
599 | 0 | } |
600 | | |
601 | 0 | return true; |
602 | 0 | } |
603 | | |
604 | | /* TODO |
605 | | 1) Get the queue working for IO. |
606 | | 2) there is some weirdness when using the -S option (the screen is grey |
607 | | and not totally invalidated |
608 | | 3) resolutions > 1024 |
609 | | */ |
610 | | |
611 | | static void vnc_colordepth(VncState *vs); |
612 | | |
613 | | static int vnc_width(VncDisplay *vd) |
614 | 0 | { |
615 | 0 | return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), |
616 | 0 | VNC_DIRTY_PIXELS_PER_BIT)); |
617 | 0 | } |
618 | | |
619 | | static int vnc_true_width(VncDisplay *vd) |
620 | 0 | { |
621 | 0 | return MIN(VNC_MAX_WIDTH, surface_width(vd->ds)); |
622 | 0 | } |
623 | | |
624 | | static int vnc_height(VncDisplay *vd) |
625 | 0 | { |
626 | 0 | return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); |
627 | 0 | } |
628 | | |
629 | | static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], |
630 | | VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), |
631 | | VncDisplay *vd, |
632 | | int x, int y, int w, int h) |
633 | 0 | { |
634 | 0 | int width = vnc_width(vd); |
635 | 0 | int height = vnc_height(vd); |
636 | | |
637 | | /* this is needed this to ensure we updated all affected |
638 | | * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ |
639 | 0 | w += (x % VNC_DIRTY_PIXELS_PER_BIT); |
640 | 0 | x -= (x % VNC_DIRTY_PIXELS_PER_BIT); |
641 | |
|
642 | 0 | x = MIN(x, width); |
643 | 0 | y = MIN(y, height); |
644 | 0 | w = MIN(x + w, width) - x; |
645 | 0 | h = MIN(y + h, height); |
646 | |
|
647 | 0 | for (; y < h; y++) { |
648 | 0 | bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, |
649 | 0 | DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | | static void vnc_dpy_update(DisplayChangeListener *dcl, |
654 | | int x, int y, int w, int h) |
655 | 0 | { |
656 | 0 | VncDisplay *vd = container_of(dcl, VncDisplay, dcl); |
657 | 0 | struct VncSurface *s = &vd->guest; |
658 | |
|
659 | 0 | vnc_set_area_dirty(s->dirty, vd, x, y, w, h); |
660 | 0 | } |
661 | | |
662 | | void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, |
663 | | int32_t encoding) |
664 | 0 | { |
665 | 0 | vnc_write_u16(vs, x); |
666 | 0 | vnc_write_u16(vs, y); |
667 | 0 | vnc_write_u16(vs, w); |
668 | 0 | vnc_write_u16(vs, h); |
669 | |
|
670 | 0 | vnc_write_s32(vs, encoding); |
671 | 0 | } |
672 | | |
673 | | static void vnc_desktop_resize_ext(VncState *vs, int reject_reason) |
674 | 0 | { |
675 | 0 | trace_vnc_msg_server_ext_desktop_resize( |
676 | 0 | vs, vs->ioc, vs->client_width, vs->client_height, reject_reason); |
677 | |
|
678 | 0 | vnc_lock_output(vs); |
679 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
680 | 0 | vnc_write_u8(vs, 0); |
681 | 0 | vnc_write_u16(vs, 1); /* number of rects */ |
682 | 0 | vnc_framebuffer_update(vs, |
683 | 0 | reject_reason ? 1 : 0, |
684 | 0 | reject_reason, |
685 | 0 | vs->client_width, vs->client_height, |
686 | 0 | VNC_ENCODING_DESKTOP_RESIZE_EXT); |
687 | 0 | vnc_write_u8(vs, 1); /* number of screens */ |
688 | 0 | vnc_write_u8(vs, 0); /* padding */ |
689 | 0 | vnc_write_u8(vs, 0); /* padding */ |
690 | 0 | vnc_write_u8(vs, 0); /* padding */ |
691 | 0 | vnc_write_u32(vs, 0); /* screen id */ |
692 | 0 | vnc_write_u16(vs, 0); /* screen x-pos */ |
693 | 0 | vnc_write_u16(vs, 0); /* screen y-pos */ |
694 | 0 | vnc_write_u16(vs, vs->client_width); |
695 | 0 | vnc_write_u16(vs, vs->client_height); |
696 | 0 | vnc_write_u32(vs, 0); /* screen flags */ |
697 | 0 | vnc_unlock_output(vs); |
698 | 0 | vnc_flush(vs); |
699 | 0 | } |
700 | | |
701 | | static void vnc_desktop_resize(VncState *vs) |
702 | 0 | { |
703 | 0 | if (vs->ioc == NULL || (!vnc_has_feature(vs, VNC_FEATURE_RESIZE) && |
704 | 0 | !vnc_has_feature(vs, VNC_FEATURE_RESIZE_EXT))) { |
705 | 0 | return; |
706 | 0 | } |
707 | 0 | if (vs->client_width == vs->vd->true_width && |
708 | 0 | vs->client_height == pixman_image_get_height(vs->vd->server)) { |
709 | 0 | return; |
710 | 0 | } |
711 | | |
712 | 0 | assert(vs->vd->true_width < 65536 && |
713 | 0 | vs->vd->true_width >= 0); |
714 | 0 | assert(pixman_image_get_height(vs->vd->server) < 65536 && |
715 | 0 | pixman_image_get_height(vs->vd->server) >= 0); |
716 | 0 | vs->client_width = vs->vd->true_width; |
717 | 0 | vs->client_height = pixman_image_get_height(vs->vd->server); |
718 | |
|
719 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_RESIZE_EXT)) { |
720 | 0 | vnc_desktop_resize_ext(vs, 0); |
721 | 0 | return; |
722 | 0 | } |
723 | | |
724 | 0 | trace_vnc_msg_server_desktop_resize( |
725 | 0 | vs, vs->ioc, vs->client_width, vs->client_height); |
726 | |
|
727 | 0 | vnc_lock_output(vs); |
728 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
729 | 0 | vnc_write_u8(vs, 0); |
730 | 0 | vnc_write_u16(vs, 1); /* number of rects */ |
731 | 0 | vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, |
732 | 0 | VNC_ENCODING_DESKTOPRESIZE); |
733 | 0 | vnc_unlock_output(vs); |
734 | 0 | vnc_flush(vs); |
735 | 0 | } |
736 | | |
737 | | static void vnc_abort_display_jobs(VncDisplay *vd) |
738 | 0 | { |
739 | 0 | VncState *vs; |
740 | |
|
741 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
742 | 0 | vnc_lock_output(vs); |
743 | 0 | vs->abort = true; |
744 | 0 | vnc_unlock_output(vs); |
745 | 0 | } |
746 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
747 | 0 | vnc_jobs_join(vs); |
748 | 0 | } |
749 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
750 | 0 | vnc_lock_output(vs); |
751 | 0 | if (vs->update == VNC_STATE_UPDATE_NONE && |
752 | 0 | vs->job_update != VNC_STATE_UPDATE_NONE) { |
753 | | /* job aborted before completion */ |
754 | 0 | vs->update = vs->job_update; |
755 | 0 | vs->job_update = VNC_STATE_UPDATE_NONE; |
756 | 0 | } |
757 | 0 | vs->abort = false; |
758 | 0 | vnc_unlock_output(vs); |
759 | 0 | } |
760 | 0 | } |
761 | | |
762 | | int vnc_server_fb_stride(VncDisplay *vd) |
763 | 0 | { |
764 | 0 | return pixman_image_get_stride(vd->server); |
765 | 0 | } |
766 | | |
767 | | void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) |
768 | 0 | { |
769 | 0 | uint8_t *ptr; |
770 | |
|
771 | 0 | ptr = (uint8_t *)pixman_image_get_data(vd->server); |
772 | 0 | ptr += y * vnc_server_fb_stride(vd); |
773 | 0 | ptr += x * VNC_SERVER_FB_BYTES; |
774 | 0 | return ptr; |
775 | 0 | } |
776 | | |
777 | | static void vnc_update_server_surface(VncDisplay *vd) |
778 | 0 | { |
779 | 0 | int width, height; |
780 | |
|
781 | 0 | qemu_pixman_image_unref(vd->server); |
782 | 0 | vd->server = NULL; |
783 | |
|
784 | 0 | if (QTAILQ_EMPTY(&vd->clients)) { |
785 | 0 | return; |
786 | 0 | } |
787 | | |
788 | 0 | width = vnc_width(vd); |
789 | 0 | height = vnc_height(vd); |
790 | 0 | vd->true_width = vnc_true_width(vd); |
791 | 0 | vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, |
792 | 0 | width, height, |
793 | 0 | NULL, 0); |
794 | |
|
795 | 0 | memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); |
796 | 0 | vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, |
797 | 0 | width, height); |
798 | 0 | } |
799 | | |
800 | | static bool vnc_check_pageflip(DisplaySurface *s1, |
801 | | DisplaySurface *s2) |
802 | 0 | { |
803 | 0 | return (s1 != NULL && |
804 | 0 | s2 != NULL && |
805 | 0 | surface_width(s1) == surface_width(s2) && |
806 | 0 | surface_height(s1) == surface_height(s2) && |
807 | 0 | surface_format(s1) == surface_format(s2)); |
808 | |
|
809 | 0 | } |
810 | | |
811 | | static void vnc_dpy_switch(DisplayChangeListener *dcl, |
812 | | DisplaySurface *surface) |
813 | 0 | { |
814 | 0 | VncDisplay *vd = container_of(dcl, VncDisplay, dcl); |
815 | 0 | bool pageflip = vnc_check_pageflip(vd->ds, surface); |
816 | 0 | VncState *vs; |
817 | |
|
818 | 0 | vnc_abort_display_jobs(vd); |
819 | 0 | vd->ds = surface; |
820 | | |
821 | | /* guest surface */ |
822 | 0 | qemu_pixman_image_unref(vd->guest.fb); |
823 | 0 | vd->guest.fb = pixman_image_ref(surface->image); |
824 | 0 | vd->guest.format = surface_format(surface); |
825 | | |
826 | |
|
827 | 0 | if (pageflip) { |
828 | 0 | trace_vnc_server_dpy_pageflip(vd, |
829 | 0 | surface_width(surface), |
830 | 0 | surface_height(surface), |
831 | 0 | surface_format(surface)); |
832 | 0 | vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, |
833 | 0 | surface_width(surface), |
834 | 0 | surface_height(surface)); |
835 | 0 | return; |
836 | 0 | } |
837 | | |
838 | 0 | trace_vnc_server_dpy_recreate(vd, |
839 | 0 | surface_width(surface), |
840 | 0 | surface_height(surface), |
841 | 0 | surface_format(surface)); |
842 | | /* server surface */ |
843 | 0 | vnc_update_server_surface(vd); |
844 | |
|
845 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
846 | 0 | vnc_colordepth(vs); |
847 | 0 | vnc_desktop_resize(vs); |
848 | 0 | vnc_cursor_define(vs); |
849 | 0 | memset(vs->dirty, 0x00, sizeof(vs->dirty)); |
850 | 0 | vnc_set_area_dirty(vs->dirty, vd, 0, 0, |
851 | 0 | vnc_width(vd), |
852 | 0 | vnc_height(vd)); |
853 | 0 | vnc_update_throttle_offset(vs); |
854 | 0 | } |
855 | 0 | } |
856 | | |
857 | | /* fastest code */ |
858 | | static void vnc_write_pixels_copy(VncState *vs, |
859 | | void *pixels, int size) |
860 | 0 | { |
861 | 0 | vnc_write(vs, pixels, size); |
862 | 0 | } |
863 | | |
864 | | /* slowest but generic code. */ |
865 | | void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) |
866 | 0 | { |
867 | 0 | uint8_t r, g, b; |
868 | |
|
869 | 0 | #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) |
870 | 0 | r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; |
871 | 0 | g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; |
872 | 0 | b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; |
873 | | #else |
874 | | # error need some bits here if you change VNC_SERVER_FB_FORMAT |
875 | | #endif |
876 | 0 | v = (r << vs->client_pf.rshift) | |
877 | 0 | (g << vs->client_pf.gshift) | |
878 | 0 | (b << vs->client_pf.bshift); |
879 | 0 | switch (vs->client_pf.bytes_per_pixel) { |
880 | 0 | case 1: |
881 | 0 | buf[0] = v; |
882 | 0 | break; |
883 | 0 | case 2: |
884 | 0 | if (vs->client_endian == G_BIG_ENDIAN) { |
885 | 0 | buf[0] = v >> 8; |
886 | 0 | buf[1] = v; |
887 | 0 | } else { |
888 | 0 | buf[1] = v >> 8; |
889 | 0 | buf[0] = v; |
890 | 0 | } |
891 | 0 | break; |
892 | 0 | default: |
893 | 0 | case 4: |
894 | 0 | if (vs->client_endian == G_BIG_ENDIAN) { |
895 | 0 | buf[0] = v >> 24; |
896 | 0 | buf[1] = v >> 16; |
897 | 0 | buf[2] = v >> 8; |
898 | 0 | buf[3] = v; |
899 | 0 | } else { |
900 | 0 | buf[3] = v >> 24; |
901 | 0 | buf[2] = v >> 16; |
902 | 0 | buf[1] = v >> 8; |
903 | 0 | buf[0] = v; |
904 | 0 | } |
905 | 0 | break; |
906 | 0 | } |
907 | 0 | } |
908 | | |
909 | | static void vnc_write_pixels_generic(VncState *vs, |
910 | | void *pixels1, int size) |
911 | 0 | { |
912 | 0 | uint8_t buf[4]; |
913 | |
|
914 | 0 | if (VNC_SERVER_FB_BYTES == 4) { |
915 | 0 | uint32_t *pixels = pixels1; |
916 | 0 | int n, i; |
917 | 0 | n = size >> 2; |
918 | 0 | for (i = 0; i < n; i++) { |
919 | 0 | vnc_convert_pixel(vs, buf, pixels[i]); |
920 | 0 | vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | | |
925 | | int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) |
926 | 0 | { |
927 | 0 | int i; |
928 | 0 | uint8_t *row; |
929 | 0 | VncDisplay *vd = vs->vd; |
930 | |
|
931 | 0 | row = vnc_server_fb_ptr(vd, x, y); |
932 | 0 | for (i = 0; i < h; i++) { |
933 | 0 | vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); |
934 | 0 | row += vnc_server_fb_stride(vd); |
935 | 0 | } |
936 | 0 | return 1; |
937 | 0 | } |
938 | | |
939 | | int vnc_send_framebuffer_update(VncState *vs, VncWorker *worker, |
940 | | int x, int y, int w, int h) |
941 | 0 | { |
942 | 0 | int n = 0; |
943 | |
|
944 | 0 | switch(vs->vnc_encoding) { |
945 | 0 | case VNC_ENCODING_ZLIB: |
946 | 0 | n = vnc_zlib_send_framebuffer_update(vs, worker, x, y, w, h); |
947 | 0 | break; |
948 | 0 | case VNC_ENCODING_HEXTILE: |
949 | 0 | vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); |
950 | 0 | n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); |
951 | 0 | break; |
952 | 0 | case VNC_ENCODING_TIGHT: |
953 | 0 | n = vnc_tight_send_framebuffer_update(vs, worker, x, y, w, h); |
954 | 0 | break; |
955 | 0 | case VNC_ENCODING_TIGHT_PNG: |
956 | 0 | n = vnc_tight_png_send_framebuffer_update(vs, worker, x, y, w, h); |
957 | 0 | break; |
958 | 0 | case VNC_ENCODING_ZRLE: |
959 | 0 | n = vnc_zrle_send_framebuffer_update(vs, worker, x, y, w, h); |
960 | 0 | break; |
961 | 0 | case VNC_ENCODING_ZYWRLE: |
962 | 0 | n = vnc_zywrle_send_framebuffer_update(vs, worker, x, y, w, h); |
963 | 0 | break; |
964 | 0 | default: |
965 | 0 | vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); |
966 | 0 | n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | return n; |
970 | 0 | } |
971 | | |
972 | | static void vnc_mouse_set(DisplayChangeListener *dcl, |
973 | | int x, int y, bool visible) |
974 | 0 | { |
975 | | /* can we ask the client(s) to move the pointer ??? */ |
976 | 0 | } |
977 | | |
978 | | static int vnc_cursor_define(VncState *vs) |
979 | 0 | { |
980 | 0 | QEMUCursor *c = qemu_console_get_cursor(vs->vd->dcl.con); |
981 | 0 | int isize; |
982 | |
|
983 | 0 | if (!c) { |
984 | 0 | return -1; |
985 | 0 | } |
986 | | |
987 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_ALPHA_CURSOR)) { |
988 | 0 | vnc_lock_output(vs); |
989 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
990 | 0 | vnc_write_u8(vs, 0); /* padding */ |
991 | 0 | vnc_write_u16(vs, 1); /* # of rects */ |
992 | 0 | vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, |
993 | 0 | VNC_ENCODING_ALPHA_CURSOR); |
994 | 0 | vnc_write_s32(vs, VNC_ENCODING_RAW); |
995 | 0 | vnc_write(vs, c->data, c->width * c->height * 4); |
996 | 0 | vnc_unlock_output(vs); |
997 | 0 | return 0; |
998 | 0 | } |
999 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { |
1000 | 0 | vnc_lock_output(vs); |
1001 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
1002 | 0 | vnc_write_u8(vs, 0); /* padding */ |
1003 | 0 | vnc_write_u16(vs, 1); /* # of rects */ |
1004 | 0 | vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, |
1005 | 0 | VNC_ENCODING_RICH_CURSOR); |
1006 | 0 | isize = c->width * c->height * vs->client_pf.bytes_per_pixel; |
1007 | 0 | vnc_write_pixels_generic(vs, c->data, isize); |
1008 | 0 | vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); |
1009 | 0 | vnc_unlock_output(vs); |
1010 | 0 | return 0; |
1011 | 0 | } |
1012 | 0 | return -1; |
1013 | 0 | } |
1014 | | |
1015 | | static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, |
1016 | | QEMUCursor *c) |
1017 | 0 | { |
1018 | 0 | VncDisplay *vd = container_of(dcl, VncDisplay, dcl); |
1019 | 0 | VncState *vs; |
1020 | |
|
1021 | 0 | g_free(vd->cursor_mask); |
1022 | 0 | vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; |
1023 | 0 | vd->cursor_mask = g_malloc0(vd->cursor_msize); |
1024 | 0 | cursor_get_mono_mask(c, 0, vd->cursor_mask); |
1025 | |
|
1026 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
1027 | 0 | vnc_cursor_define(vs); |
1028 | 0 | } |
1029 | 0 | } |
1030 | | |
1031 | | static int find_and_clear_dirty_height(VncState *vs, |
1032 | | int y, int last_x, int x, int height) |
1033 | 0 | { |
1034 | 0 | int h; |
1035 | |
|
1036 | 0 | for (h = 1; h < (height - y); h++) { |
1037 | 0 | if (!test_bit(last_x, vs->dirty[y + h])) { |
1038 | 0 | break; |
1039 | 0 | } |
1040 | 0 | bitmap_clear(vs->dirty[y + h], last_x, x - last_x); |
1041 | 0 | } |
1042 | |
|
1043 | 0 | return h; |
1044 | 0 | } |
1045 | | |
1046 | | /* |
1047 | | * Figure out how much pending data we should allow in the output |
1048 | | * buffer before we throttle incremental display updates, and/or |
1049 | | * drop audio samples. |
1050 | | * |
1051 | | * We allow for equiv of 1 full display's worth of FB updates, |
1052 | | * and 1 second of audio samples. If audio backlog was larger |
1053 | | * than that the client would already suffering awful audio |
1054 | | * glitches, so dropping samples is no worse really). |
1055 | | */ |
1056 | | static void vnc_update_throttle_offset(VncState *vs) |
1057 | 0 | { |
1058 | 0 | size_t offset = |
1059 | 0 | vs->client_width * vs->client_height * vs->client_pf.bytes_per_pixel; |
1060 | |
|
1061 | 0 | if (vs->audio_cap) { |
1062 | 0 | int bps; |
1063 | 0 | switch (vs->as.fmt) { |
1064 | 0 | default: |
1065 | 0 | case AUDIO_FORMAT_U8: |
1066 | 0 | case AUDIO_FORMAT_S8: |
1067 | 0 | bps = 1; |
1068 | 0 | break; |
1069 | 0 | case AUDIO_FORMAT_U16: |
1070 | 0 | case AUDIO_FORMAT_S16: |
1071 | 0 | bps = 2; |
1072 | 0 | break; |
1073 | 0 | case AUDIO_FORMAT_U32: |
1074 | 0 | case AUDIO_FORMAT_S32: |
1075 | 0 | bps = 4; |
1076 | 0 | break; |
1077 | 0 | } |
1078 | 0 | offset += vs->as.freq * bps * vs->as.nchannels; |
1079 | 0 | } |
1080 | | |
1081 | | /* Put a floor of 1MB on offset, so that if we have a large pending |
1082 | | * buffer and the display is resized to a small size & back again |
1083 | | * we don't suddenly apply a tiny send limit |
1084 | | */ |
1085 | 0 | offset = MAX(offset, 1024 * 1024); |
1086 | |
|
1087 | 0 | if (vs->throttle_output_offset != offset) { |
1088 | 0 | trace_vnc_client_throttle_threshold( |
1089 | 0 | vs, vs->ioc, vs->throttle_output_offset, offset, vs->client_width, |
1090 | 0 | vs->client_height, vs->client_pf.bytes_per_pixel, vs->audio_cap); |
1091 | 0 | } |
1092 | |
|
1093 | 0 | vs->throttle_output_offset = offset; |
1094 | 0 | } |
1095 | | |
1096 | | static bool vnc_should_update(VncState *vs) |
1097 | 0 | { |
1098 | 0 | switch (vs->update) { |
1099 | 0 | case VNC_STATE_UPDATE_NONE: |
1100 | 0 | break; |
1101 | 0 | case VNC_STATE_UPDATE_INCREMENTAL: |
1102 | | /* Only allow incremental updates if the pending send queue |
1103 | | * is less than the permitted threshold, and the job worker |
1104 | | * is completely idle. |
1105 | | */ |
1106 | 0 | if (vs->output.offset < vs->throttle_output_offset && |
1107 | 0 | vs->job_update == VNC_STATE_UPDATE_NONE) { |
1108 | 0 | return true; |
1109 | 0 | } |
1110 | 0 | trace_vnc_client_throttle_incremental( |
1111 | 0 | vs, vs->ioc, vs->job_update, vs->output.offset); |
1112 | 0 | break; |
1113 | 0 | case VNC_STATE_UPDATE_FORCE: |
1114 | | /* Only allow forced updates if the pending send queue |
1115 | | * does not contain a previous forced update, and the |
1116 | | * job worker is completely idle. |
1117 | | * |
1118 | | * Note this means we'll queue a forced update, even if |
1119 | | * the output buffer size is otherwise over the throttle |
1120 | | * output limit. |
1121 | | */ |
1122 | 0 | if (vs->force_update_offset == 0 && |
1123 | 0 | vs->job_update == VNC_STATE_UPDATE_NONE) { |
1124 | 0 | return true; |
1125 | 0 | } |
1126 | 0 | trace_vnc_client_throttle_forced( |
1127 | 0 | vs, vs->ioc, vs->job_update, vs->force_update_offset); |
1128 | 0 | break; |
1129 | 0 | } |
1130 | 0 | return false; |
1131 | 0 | } |
1132 | | |
1133 | | static int vnc_update_client(VncState *vs, int has_dirty) |
1134 | 0 | { |
1135 | 0 | VncDisplay *vd = vs->vd; |
1136 | 0 | VncJob *job; |
1137 | 0 | int y; |
1138 | 0 | int height, width; |
1139 | 0 | int n = 0; |
1140 | |
|
1141 | 0 | if (vs->disconnecting) { |
1142 | 0 | vnc_disconnect_finish(vs); |
1143 | 0 | return 0; |
1144 | 0 | } |
1145 | | |
1146 | 0 | vs->has_dirty += has_dirty; |
1147 | 0 | if (!vnc_should_update(vs)) { |
1148 | 0 | return 0; |
1149 | 0 | } |
1150 | | |
1151 | 0 | if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) { |
1152 | 0 | return 0; |
1153 | 0 | } |
1154 | | |
1155 | | /* |
1156 | | * Send screen updates to the vnc client using the server |
1157 | | * surface and server dirty map. guest surface updates |
1158 | | * happening in parallel don't disturb us, the next pass will |
1159 | | * send them to the client. |
1160 | | */ |
1161 | 0 | job = vnc_job_new(vs); |
1162 | |
|
1163 | 0 | height = pixman_image_get_height(vd->server); |
1164 | 0 | width = pixman_image_get_width(vd->server); |
1165 | |
|
1166 | 0 | y = 0; |
1167 | 0 | for (;;) { |
1168 | 0 | int x, h; |
1169 | 0 | unsigned long x2; |
1170 | 0 | unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, |
1171 | 0 | height * VNC_DIRTY_BPL(vs), |
1172 | 0 | y * VNC_DIRTY_BPL(vs)); |
1173 | 0 | if (offset == height * VNC_DIRTY_BPL(vs)) { |
1174 | | /* no more dirty bits */ |
1175 | 0 | break; |
1176 | 0 | } |
1177 | 0 | y = offset / VNC_DIRTY_BPL(vs); |
1178 | 0 | x = offset % VNC_DIRTY_BPL(vs); |
1179 | 0 | x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], |
1180 | 0 | VNC_DIRTY_BPL(vs), x); |
1181 | 0 | bitmap_clear(vs->dirty[y], x, x2 - x); |
1182 | 0 | h = find_and_clear_dirty_height(vs, y, x, x2, height); |
1183 | 0 | x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); |
1184 | 0 | if (x2 > x) { |
1185 | 0 | n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, |
1186 | 0 | (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); |
1187 | 0 | } |
1188 | 0 | if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { |
1189 | 0 | y += h; |
1190 | 0 | if (y == height) { |
1191 | 0 | break; |
1192 | 0 | } |
1193 | 0 | } |
1194 | 0 | } |
1195 | |
|
1196 | 0 | vs->job_update = vs->update; |
1197 | 0 | vs->update = VNC_STATE_UPDATE_NONE; |
1198 | 0 | vnc_job_push(job); |
1199 | 0 | vs->has_dirty = 0; |
1200 | 0 | return n; |
1201 | 0 | } |
1202 | | |
1203 | | /* audio */ |
1204 | | static void audio_capture_notify(void *opaque, audcnotification_e cmd) |
1205 | 0 | { |
1206 | 0 | VncState *vs = opaque; |
1207 | |
|
1208 | 0 | assert(vs->magic == VNC_MAGIC); |
1209 | 0 | switch (cmd) { |
1210 | 0 | case AUD_CNOTIFY_DISABLE: |
1211 | 0 | trace_vnc_msg_server_audio_end(vs, vs->ioc); |
1212 | 0 | vnc_lock_output(vs); |
1213 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); |
1214 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); |
1215 | 0 | vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); |
1216 | 0 | vnc_unlock_output(vs); |
1217 | 0 | vnc_flush(vs); |
1218 | 0 | break; |
1219 | | |
1220 | 0 | case AUD_CNOTIFY_ENABLE: |
1221 | 0 | trace_vnc_msg_server_audio_begin(vs, vs->ioc); |
1222 | 0 | vnc_lock_output(vs); |
1223 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); |
1224 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); |
1225 | 0 | vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); |
1226 | 0 | vnc_unlock_output(vs); |
1227 | 0 | vnc_flush(vs); |
1228 | 0 | break; |
1229 | 0 | } |
1230 | 0 | } |
1231 | | |
1232 | | static void audio_capture_destroy(void *opaque) |
1233 | 0 | { |
1234 | 0 | } |
1235 | | |
1236 | | static void audio_capture(void *opaque, const void *buf, int size) |
1237 | 0 | { |
1238 | 0 | VncState *vs = opaque; |
1239 | |
|
1240 | 0 | assert(vs->magic == VNC_MAGIC); |
1241 | 0 | trace_vnc_msg_server_audio_data(vs, vs->ioc, buf, size); |
1242 | 0 | vnc_lock_output(vs); |
1243 | 0 | if (vs->output.offset < vs->throttle_output_offset) { |
1244 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); |
1245 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); |
1246 | 0 | vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); |
1247 | 0 | vnc_write_u32(vs, size); |
1248 | 0 | vnc_write(vs, buf, size); |
1249 | 0 | } else { |
1250 | 0 | trace_vnc_client_throttle_audio(vs, vs->ioc, vs->output.offset); |
1251 | 0 | } |
1252 | 0 | vnc_unlock_output(vs); |
1253 | 0 | vnc_flush(vs); |
1254 | 0 | } |
1255 | | |
1256 | | static void audio_add(VncState *vs) |
1257 | 0 | { |
1258 | 0 | struct audio_capture_ops ops; |
1259 | |
|
1260 | 0 | if (vs->audio_cap) { |
1261 | 0 | error_report("audio already running"); |
1262 | 0 | return; |
1263 | 0 | } |
1264 | | |
1265 | 0 | ops.notify = audio_capture_notify; |
1266 | 0 | ops.destroy = audio_capture_destroy; |
1267 | 0 | ops.capture = audio_capture; |
1268 | |
|
1269 | 0 | vs->audio_cap = audio_be_add_capture(vs->vd->audio_be, &vs->as, &ops, vs); |
1270 | 0 | if (!vs->audio_cap) { |
1271 | 0 | error_report("Failed to add audio capture"); |
1272 | 0 | } |
1273 | 0 | } |
1274 | | |
1275 | | static void audio_del(VncState *vs) |
1276 | 0 | { |
1277 | 0 | if (vs->audio_cap) { |
1278 | 0 | audio_be_del_capture(vs->vd->audio_be, vs->audio_cap, vs); |
1279 | 0 | vs->audio_cap = NULL; |
1280 | 0 | } |
1281 | 0 | } |
1282 | | |
1283 | | static void vnc_disconnect_start(VncState *vs) |
1284 | 0 | { |
1285 | 0 | if (vs->disconnecting) { |
1286 | 0 | return; |
1287 | 0 | } |
1288 | 0 | trace_vnc_client_disconnect_start(vs, vs->ioc); |
1289 | 0 | vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); |
1290 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1291 | 0 | qio_channel_close(vs->ioc, NULL); |
1292 | 0 | vs->disconnecting = TRUE; |
1293 | 0 | } |
1294 | | |
1295 | | static void vnc_disconnect_finish(VncState *vs) |
1296 | 0 | { |
1297 | 0 | VncConnection *vc = container_of(vs, VncConnection, vs); |
1298 | |
|
1299 | 0 | trace_vnc_client_disconnect_finish(vs, vs->ioc); |
1300 | |
|
1301 | 0 | vnc_jobs_join(vs); /* Wait encoding jobs */ |
1302 | |
|
1303 | 0 | vnc_lock_output(vs); |
1304 | 0 | vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); |
1305 | |
|
1306 | 0 | buffer_free(&vs->input); |
1307 | 0 | buffer_free(&vs->output); |
1308 | |
|
1309 | 0 | qapi_free_VncClientInfo(vs->info); |
1310 | |
|
1311 | 0 | vnc_zlib_clear(&vc->worker); |
1312 | 0 | vnc_tight_clear(&vc->worker); |
1313 | 0 | vnc_zrle_clear(&vc->worker); |
1314 | |
|
1315 | | #ifdef CONFIG_VNC_SASL |
1316 | | vnc_sasl_client_cleanup(vs); |
1317 | | #endif /* CONFIG_VNC_SASL */ |
1318 | 0 | audio_del(vs); |
1319 | 0 | qkbd_state_lift_all_keys(vs->vd->kbd); |
1320 | |
|
1321 | 0 | if (vs->mouse_mode_notifier.notify != NULL) { |
1322 | 0 | qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); |
1323 | 0 | } |
1324 | 0 | QTAILQ_REMOVE(&vs->vd->clients, vs, next); |
1325 | 0 | if (QTAILQ_EMPTY(&vs->vd->clients)) { |
1326 | | /* last client gone */ |
1327 | 0 | vnc_update_server_surface(vs->vd); |
1328 | 0 | } |
1329 | 0 | vnc_unlock_output(vs); |
1330 | |
|
1331 | 0 | if (vs->cbpeer.notifier.notify) { |
1332 | 0 | qemu_clipboard_peer_unregister(&vs->cbpeer); |
1333 | 0 | } |
1334 | |
|
1335 | 0 | qemu_mutex_destroy(&vs->output_mutex); |
1336 | 0 | if (vs->bh != NULL) { |
1337 | 0 | qemu_bh_delete(vs->bh); |
1338 | 0 | } |
1339 | 0 | buffer_free(&vs->jobs_buffer); |
1340 | |
|
1341 | 0 | object_unref(OBJECT(vs->ioc)); |
1342 | 0 | vs->ioc = NULL; |
1343 | 0 | object_unref(OBJECT(vs->sioc)); |
1344 | 0 | vs->sioc = NULL; |
1345 | 0 | vs->magic = 0; |
1346 | 0 | g_free(vc); |
1347 | 0 | } |
1348 | | |
1349 | | size_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err) |
1350 | 0 | { |
1351 | 0 | if (ret <= 0) { |
1352 | 0 | if (ret == 0) { |
1353 | 0 | trace_vnc_client_eof(vs, vs->ioc); |
1354 | 0 | vnc_disconnect_start(vs); |
1355 | 0 | } else if (ret != QIO_CHANNEL_ERR_BLOCK) { |
1356 | 0 | trace_vnc_client_io_error(vs, vs->ioc, |
1357 | 0 | err ? error_get_pretty(err) : "Unknown"); |
1358 | 0 | vnc_disconnect_start(vs); |
1359 | 0 | } |
1360 | |
|
1361 | 0 | error_free(err); |
1362 | 0 | return 0; |
1363 | 0 | } |
1364 | 0 | return ret; |
1365 | 0 | } |
1366 | | |
1367 | | |
1368 | | void vnc_client_error(VncState *vs) |
1369 | 0 | { |
1370 | 0 | trace_vnc_client_protocol_error(vs); |
1371 | 0 | vnc_disconnect_start(vs); |
1372 | 0 | } |
1373 | | |
1374 | | |
1375 | | /* |
1376 | | * Called to write a chunk of data to the client socket. The data may |
1377 | | * be the raw data, or may have already been encoded by SASL. |
1378 | | * The data will be written either straight onto the socket, or |
1379 | | * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled |
1380 | | * |
1381 | | * NB, it is theoretically possible to have 2 layers of encryption, |
1382 | | * both SASL, and this TLS layer. It is highly unlikely in practice |
1383 | | * though, since SASL encryption will typically be a no-op if TLS |
1384 | | * is active |
1385 | | * |
1386 | | * Returns the number of bytes written, which may be less than |
1387 | | * the requested 'datalen' if the socket would block. Returns |
1388 | | * 0 on I/O error, and disconnects the client socket. |
1389 | | */ |
1390 | | size_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) |
1391 | 0 | { |
1392 | 0 | Error *err = NULL; |
1393 | 0 | ssize_t ret; |
1394 | 0 | ret = qio_channel_write(vs->ioc, (const char *)data, datalen, &err); |
1395 | 0 | trace_vnc_client_write_wire(vs, data, datalen, ret); |
1396 | 0 | return vnc_client_io_error(vs, ret, err); |
1397 | 0 | } |
1398 | | |
1399 | | |
1400 | | /* |
1401 | | * Called to write buffered data to the client socket, when not |
1402 | | * using any SASL SSF encryption layers. Will write as much data |
1403 | | * as possible without blocking. If all buffered data is written, |
1404 | | * will switch the FD poll() handler back to read monitoring. |
1405 | | * |
1406 | | * Returns the number of bytes written, which may be less than |
1407 | | * the buffered output data if the socket would block. Returns |
1408 | | * 0 on I/O error, and disconnects the client socket. |
1409 | | */ |
1410 | | static size_t vnc_client_write_plain(VncState *vs) |
1411 | 0 | { |
1412 | 0 | size_t offset; |
1413 | 0 | size_t ret; |
1414 | |
|
1415 | | #ifdef CONFIG_VNC_SASL |
1416 | | trace_vnc_client_write_plain(vs, vs->output.buffer, |
1417 | | vs->output.capacity, vs->output.offset, |
1418 | | vs->sasl.waitWriteSSF); |
1419 | | |
1420 | | if (vs->sasl.conn && |
1421 | | vs->sasl.runSSF && |
1422 | | vs->sasl.waitWriteSSF) { |
1423 | | ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); |
1424 | | if (ret) |
1425 | | vs->sasl.waitWriteSSF -= ret; |
1426 | | } else |
1427 | | #endif /* CONFIG_VNC_SASL */ |
1428 | 0 | ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); |
1429 | 0 | if (!ret) |
1430 | 0 | return 0; |
1431 | | |
1432 | 0 | if (ret >= vs->force_update_offset) { |
1433 | 0 | if (vs->force_update_offset != 0) { |
1434 | 0 | trace_vnc_client_unthrottle_forced(vs, vs->ioc); |
1435 | 0 | } |
1436 | 0 | vs->force_update_offset = 0; |
1437 | 0 | } else { |
1438 | 0 | vs->force_update_offset -= ret; |
1439 | 0 | } |
1440 | 0 | offset = vs->output.offset; |
1441 | 0 | buffer_advance(&vs->output, ret); |
1442 | 0 | if (offset >= vs->throttle_output_offset && |
1443 | 0 | vs->output.offset < vs->throttle_output_offset) { |
1444 | 0 | trace_vnc_client_unthrottle_incremental(vs, vs->ioc, vs->output.offset); |
1445 | 0 | } |
1446 | |
|
1447 | 0 | if (vs->output.offset == 0) { |
1448 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1449 | 0 | vs->ioc_tag = qio_channel_add_watch( |
1450 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
1451 | 0 | vnc_client_io, vs, NULL); |
1452 | 0 | } |
1453 | |
|
1454 | 0 | return ret; |
1455 | 0 | } |
1456 | | |
1457 | | |
1458 | | /* |
1459 | | * First function called whenever there is data to be written to |
1460 | | * the client socket. Will delegate actual work according to whether |
1461 | | * SASL SSF layers are enabled (thus requiring encryption calls) |
1462 | | */ |
1463 | | static void vnc_client_write_locked(VncState *vs) |
1464 | 0 | { |
1465 | | #ifdef CONFIG_VNC_SASL |
1466 | | if (vs->sasl.conn && |
1467 | | vs->sasl.runSSF && |
1468 | | !vs->sasl.waitWriteSSF) { |
1469 | | vnc_client_write_sasl(vs); |
1470 | | } else |
1471 | | #endif /* CONFIG_VNC_SASL */ |
1472 | 0 | { |
1473 | 0 | vnc_client_write_plain(vs); |
1474 | 0 | } |
1475 | 0 | } |
1476 | | |
1477 | | static void vnc_client_write(VncState *vs) |
1478 | 0 | { |
1479 | 0 | assert(vs->magic == VNC_MAGIC); |
1480 | 0 | vnc_lock_output(vs); |
1481 | 0 | if (vs->output.offset) { |
1482 | 0 | vnc_client_write_locked(vs); |
1483 | 0 | } else if (vs->ioc != NULL) { |
1484 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1485 | 0 | vs->ioc_tag = qio_channel_add_watch( |
1486 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
1487 | 0 | vnc_client_io, vs, NULL); |
1488 | 0 | } |
1489 | 0 | vnc_unlock_output(vs); |
1490 | 0 | } |
1491 | | |
1492 | | void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) |
1493 | 0 | { |
1494 | 0 | vs->read_handler = func; |
1495 | 0 | vs->read_handler_expect = expecting; |
1496 | 0 | } |
1497 | | |
1498 | | |
1499 | | /* |
1500 | | * Called to read a chunk of data from the client socket. The data may |
1501 | | * be the raw data, or may need to be further decoded by SASL. |
1502 | | * The data will be read either straight from to the socket, or |
1503 | | * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled |
1504 | | * |
1505 | | * NB, it is theoretically possible to have 2 layers of encryption, |
1506 | | * both SASL, and this TLS layer. It is highly unlikely in practice |
1507 | | * though, since SASL encryption will typically be a no-op if TLS |
1508 | | * is active |
1509 | | * |
1510 | | * Returns the number of bytes read, which may be less than |
1511 | | * the requested 'datalen' if the socket would block. Returns |
1512 | | * 0 on I/O error or EOF, and disconnects the client socket. |
1513 | | */ |
1514 | | size_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) |
1515 | 0 | { |
1516 | 0 | ssize_t ret; |
1517 | 0 | Error *err = NULL; |
1518 | 0 | ret = qio_channel_read(vs->ioc, (char *)data, datalen, &err); |
1519 | 0 | trace_vnc_client_read_wire(vs, data, datalen, ret); |
1520 | 0 | return vnc_client_io_error(vs, ret, err); |
1521 | 0 | } |
1522 | | |
1523 | | |
1524 | | /* |
1525 | | * Called to read data from the client socket to the input buffer, |
1526 | | * when not using any SASL SSF encryption layers. Will read as much |
1527 | | * data as possible without blocking. |
1528 | | * |
1529 | | * Returns the number of bytes read, which may be less than |
1530 | | * the requested 'datalen' if the socket would block. Returns |
1531 | | * 0 on I/O error or EOF, and disconnects the client socket. |
1532 | | */ |
1533 | | static size_t vnc_client_read_plain(VncState *vs) |
1534 | 0 | { |
1535 | 0 | size_t ret; |
1536 | 0 | trace_vnc_client_read_plain(vs, vs->input.buffer, |
1537 | 0 | vs->input.capacity, vs->input.offset); |
1538 | 0 | buffer_reserve(&vs->input, 4096); |
1539 | 0 | ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); |
1540 | 0 | if (!ret) |
1541 | 0 | return 0; |
1542 | 0 | vs->input.offset += ret; |
1543 | 0 | return ret; |
1544 | 0 | } |
1545 | | |
1546 | | static void vnc_jobs_bh(void *opaque) |
1547 | 0 | { |
1548 | 0 | VncState *vs = opaque; |
1549 | |
|
1550 | 0 | assert(vs->magic == VNC_MAGIC); |
1551 | 0 | vnc_jobs_consume_buffer(vs); |
1552 | 0 | } |
1553 | | |
1554 | | /* |
1555 | | * First function called whenever there is more data to be read from |
1556 | | * the client socket. Will delegate actual work according to whether |
1557 | | * SASL SSF layers are enabled (thus requiring decryption calls) |
1558 | | * Returns 0 on success, -1 if client disconnected |
1559 | | */ |
1560 | | static int vnc_client_read(VncState *vs) |
1561 | 0 | { |
1562 | 0 | size_t sz; |
1563 | |
|
1564 | | #ifdef CONFIG_VNC_SASL |
1565 | | if (vs->sasl.conn && vs->sasl.runSSF) |
1566 | | sz = vnc_client_read_sasl(vs); |
1567 | | else |
1568 | | #endif /* CONFIG_VNC_SASL */ |
1569 | 0 | sz = vnc_client_read_plain(vs); |
1570 | 0 | if (!sz) { |
1571 | 0 | if (vs->disconnecting) { |
1572 | 0 | vnc_disconnect_finish(vs); |
1573 | 0 | return -1; |
1574 | 0 | } |
1575 | 0 | return 0; |
1576 | 0 | } |
1577 | | |
1578 | 0 | while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { |
1579 | 0 | size_t len = vs->read_handler_expect; |
1580 | 0 | int ret; |
1581 | |
|
1582 | 0 | ret = vs->read_handler(vs, vs->input.buffer, len); |
1583 | 0 | if (vs->disconnecting) { |
1584 | 0 | vnc_disconnect_finish(vs); |
1585 | 0 | return -1; |
1586 | 0 | } |
1587 | | |
1588 | 0 | if (!ret) { |
1589 | 0 | buffer_advance(&vs->input, len); |
1590 | 0 | } else { |
1591 | 0 | vs->read_handler_expect = ret; |
1592 | 0 | } |
1593 | 0 | } |
1594 | 0 | return 0; |
1595 | 0 | } |
1596 | | |
1597 | | gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, |
1598 | | GIOCondition condition, void *opaque) |
1599 | 0 | { |
1600 | 0 | VncState *vs = opaque; |
1601 | |
|
1602 | 0 | assert(vs->magic == VNC_MAGIC); |
1603 | |
|
1604 | 0 | if (condition & (G_IO_HUP | G_IO_ERR)) { |
1605 | 0 | vnc_disconnect_start(vs); |
1606 | 0 | return TRUE; |
1607 | 0 | } |
1608 | | |
1609 | 0 | if (condition & G_IO_IN) { |
1610 | 0 | if (vnc_client_read(vs) < 0) { |
1611 | | /* vs is free()ed here */ |
1612 | 0 | return TRUE; |
1613 | 0 | } |
1614 | 0 | } |
1615 | 0 | if (condition & G_IO_OUT) { |
1616 | 0 | vnc_client_write(vs); |
1617 | 0 | } |
1618 | |
|
1619 | 0 | if (vs->disconnecting) { |
1620 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1621 | 0 | } |
1622 | 0 | return TRUE; |
1623 | 0 | } |
1624 | | |
1625 | | |
1626 | | /* |
1627 | | * Scale factor to apply to vs->throttle_output_offset when checking for |
1628 | | * hard limit. Worst case normal usage could be x2, if we have a complete |
1629 | | * incremental update and complete forced update in the output buffer. |
1630 | | * So x3 should be good enough, but we pick x5 to be conservative and thus |
1631 | | * (hopefully) never trigger incorrectly. |
1632 | | */ |
1633 | 0 | #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5 |
1634 | | |
1635 | | void vnc_write(VncState *vs, const void *data, size_t len) |
1636 | 0 | { |
1637 | 0 | assert(vs->magic == VNC_MAGIC); |
1638 | 0 | if (vs->disconnecting) { |
1639 | 0 | return; |
1640 | 0 | } |
1641 | | /* Protection against malicious client/guest to prevent our output |
1642 | | * buffer growing without bound if client stops reading data. This |
1643 | | * should rarely trigger, because we have earlier throttling code |
1644 | | * which stops issuing framebuffer updates and drops audio data |
1645 | | * if the throttle_output_offset value is exceeded. So we only reach |
1646 | | * this higher level if a huge number of pseudo-encodings get |
1647 | | * triggered while data can't be sent on the socket. |
1648 | | * |
1649 | | * NB throttle_output_offset can be zero during early protocol |
1650 | | * handshake, or from the job thread's VncState clone |
1651 | | */ |
1652 | 0 | if (vs->throttle_output_offset != 0 && |
1653 | 0 | (vs->output.offset / VNC_THROTTLE_OUTPUT_LIMIT_SCALE) > |
1654 | 0 | vs->throttle_output_offset) { |
1655 | 0 | trace_vnc_client_output_limit(vs, vs->ioc, vs->output.offset, |
1656 | 0 | vs->throttle_output_offset); |
1657 | 0 | vnc_disconnect_start(vs); |
1658 | 0 | return; |
1659 | 0 | } |
1660 | 0 | buffer_reserve(&vs->output, len); |
1661 | |
|
1662 | 0 | if (vs->ioc != NULL && buffer_empty(&vs->output)) { |
1663 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1664 | 0 | vs->ioc_tag = qio_channel_add_watch( |
1665 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT, |
1666 | 0 | vnc_client_io, vs, NULL); |
1667 | 0 | } |
1668 | |
|
1669 | 0 | buffer_append(&vs->output, data, len); |
1670 | 0 | } |
1671 | | |
1672 | | void vnc_write_s32(VncState *vs, int32_t value) |
1673 | 0 | { |
1674 | 0 | vnc_write_u32(vs, *(uint32_t *)&value); |
1675 | 0 | } |
1676 | | |
1677 | | void vnc_write_u32(VncState *vs, uint32_t value) |
1678 | 0 | { |
1679 | 0 | uint8_t buf[4]; |
1680 | |
|
1681 | 0 | buf[0] = (value >> 24) & 0xFF; |
1682 | 0 | buf[1] = (value >> 16) & 0xFF; |
1683 | 0 | buf[2] = (value >> 8) & 0xFF; |
1684 | 0 | buf[3] = value & 0xFF; |
1685 | |
|
1686 | 0 | vnc_write(vs, buf, 4); |
1687 | 0 | } |
1688 | | |
1689 | | void vnc_write_u16(VncState *vs, uint16_t value) |
1690 | 0 | { |
1691 | 0 | uint8_t buf[2]; |
1692 | |
|
1693 | 0 | buf[0] = (value >> 8) & 0xFF; |
1694 | 0 | buf[1] = value & 0xFF; |
1695 | |
|
1696 | 0 | vnc_write(vs, buf, 2); |
1697 | 0 | } |
1698 | | |
1699 | | void vnc_write_u8(VncState *vs, uint8_t value) |
1700 | 0 | { |
1701 | 0 | vnc_write(vs, (char *)&value, 1); |
1702 | 0 | } |
1703 | | |
1704 | | void vnc_flush(VncState *vs) |
1705 | 0 | { |
1706 | 0 | vnc_lock_output(vs); |
1707 | 0 | if (vs->ioc != NULL && !buffer_empty(&vs->output)) { |
1708 | 0 | vnc_client_write_locked(vs); |
1709 | 0 | } |
1710 | 0 | if (vs->disconnecting) { |
1711 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
1712 | 0 | } |
1713 | 0 | vnc_unlock_output(vs); |
1714 | 0 | } |
1715 | | |
1716 | | static uint8_t read_u8(uint8_t *data, size_t offset) |
1717 | 0 | { |
1718 | 0 | return data[offset]; |
1719 | 0 | } |
1720 | | |
1721 | | static uint16_t read_u16(uint8_t *data, size_t offset) |
1722 | 0 | { |
1723 | 0 | return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); |
1724 | 0 | } |
1725 | | |
1726 | | static int32_t read_s32(uint8_t *data, size_t offset) |
1727 | 0 | { |
1728 | 0 | return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | |
1729 | 0 | (data[offset + 2] << 8) | data[offset + 3]); |
1730 | 0 | } |
1731 | | |
1732 | | uint32_t read_u32(uint8_t *data, size_t offset) |
1733 | 0 | { |
1734 | 0 | return ((data[offset] << 24) | (data[offset + 1] << 16) | |
1735 | 0 | (data[offset + 2] << 8) | data[offset + 3]); |
1736 | 0 | } |
1737 | | |
1738 | | static void check_pointer_type_change(Notifier *notifier, void *data) |
1739 | 0 | { |
1740 | 0 | VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); |
1741 | 0 | int absolute = qemu_input_is_absolute(vs->vd->dcl.con); |
1742 | |
|
1743 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { |
1744 | 0 | vnc_lock_output(vs); |
1745 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
1746 | 0 | vnc_write_u8(vs, 0); |
1747 | 0 | vnc_write_u16(vs, 1); |
1748 | 0 | vnc_framebuffer_update(vs, absolute, 0, |
1749 | 0 | pixman_image_get_width(vs->vd->server), |
1750 | 0 | pixman_image_get_height(vs->vd->server), |
1751 | 0 | VNC_ENCODING_POINTER_TYPE_CHANGE); |
1752 | 0 | vnc_unlock_output(vs); |
1753 | 0 | vnc_flush(vs); |
1754 | 0 | } |
1755 | 0 | vs->absolute = absolute; |
1756 | 0 | } |
1757 | | |
1758 | | static void pointer_event(VncState *vs, uint8_t button_mask, |
1759 | | uint16_t x, uint16_t y) |
1760 | 0 | { |
1761 | 0 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
1762 | 0 | [INPUT_BUTTON_LEFT] = 0x01, |
1763 | 0 | [INPUT_BUTTON_MIDDLE] = 0x02, |
1764 | 0 | [INPUT_BUTTON_RIGHT] = 0x04, |
1765 | 0 | [INPUT_BUTTON_WHEEL_UP] = 0x08, |
1766 | 0 | [INPUT_BUTTON_WHEEL_DOWN] = 0x10, |
1767 | 0 | }; |
1768 | 0 | QemuConsole *con = vs->vd->dcl.con; |
1769 | 0 | int width = pixman_image_get_width(vs->vd->server); |
1770 | 0 | int height = pixman_image_get_height(vs->vd->server); |
1771 | |
|
1772 | 0 | if (vs->last_bmask != button_mask) { |
1773 | 0 | qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); |
1774 | 0 | vs->last_bmask = button_mask; |
1775 | 0 | } |
1776 | |
|
1777 | 0 | if (vs->absolute) { |
1778 | 0 | qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); |
1779 | 0 | qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); |
1780 | 0 | } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { |
1781 | 0 | qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); |
1782 | 0 | qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); |
1783 | 0 | } else { |
1784 | 0 | if (vs->last_x != -1) { |
1785 | 0 | qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); |
1786 | 0 | qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); |
1787 | 0 | } |
1788 | 0 | vs->last_x = x; |
1789 | 0 | vs->last_y = y; |
1790 | 0 | } |
1791 | 0 | qemu_input_event_sync(); |
1792 | 0 | } |
1793 | | |
1794 | | static void press_key(VncState *vs, unsigned int lnx) |
1795 | 0 | { |
1796 | 0 | qkbd_state_key_event(vs->vd->kbd, lnx, true); |
1797 | 0 | qkbd_state_key_event(vs->vd->kbd, lnx, false); |
1798 | 0 | } |
1799 | | |
1800 | | static void vnc_led_state_change(VncState *vs) |
1801 | 0 | { |
1802 | 0 | if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { |
1803 | 0 | return; |
1804 | 0 | } |
1805 | | |
1806 | 0 | vnc_lock_output(vs); |
1807 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
1808 | 0 | vnc_write_u8(vs, 0); |
1809 | 0 | vnc_write_u16(vs, 1); |
1810 | 0 | vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); |
1811 | 0 | vnc_write_u8(vs, vs->vd->ledstate); |
1812 | 0 | vnc_unlock_output(vs); |
1813 | 0 | vnc_flush(vs); |
1814 | 0 | } |
1815 | | |
1816 | | static void kbd_leds(Notifier *notifier, void *data) |
1817 | 0 | { |
1818 | 0 | VncDisplay *vd = container_of(notifier, VncDisplay, led_notifier); |
1819 | 0 | int ledstate = qemu_input_get_leds_mask(vd->dcl.con); |
1820 | 0 | VncState *client; |
1821 | |
|
1822 | 0 | trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), |
1823 | 0 | (ledstate & QEMU_NUM_LOCK_LED), |
1824 | 0 | (ledstate & QEMU_SCROLL_LOCK_LED)); |
1825 | |
|
1826 | 0 | if (ledstate == vd->ledstate) { |
1827 | 0 | return; |
1828 | 0 | } |
1829 | | |
1830 | 0 | vd->ledstate = ledstate; |
1831 | |
|
1832 | 0 | QTAILQ_FOREACH(client, &vd->clients, next) { |
1833 | 0 | vnc_led_state_change(client); |
1834 | 0 | } |
1835 | 0 | } |
1836 | | |
1837 | | static void do_key_event(VncState *vs, int down, int keycode, uint32_t sym) |
1838 | 0 | { |
1839 | 0 | unsigned int lnx = qemu_input_key_number_to_linux(keycode); |
1840 | | |
1841 | | /* QEMU console switch */ |
1842 | 0 | switch (lnx) { |
1843 | 0 | case KEY_1 ... KEY_9: /* '1' to '9' keys */ |
1844 | 0 | if (down && |
1845 | 0 | qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL) && |
1846 | 0 | qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_ALT)) { |
1847 | 0 | QemuConsole *con = qemu_console_lookup_by_index(lnx - KEY_1); |
1848 | 0 | if (con) { |
1849 | 0 | qemu_console_unregister_listener(&vs->vd->dcl); |
1850 | 0 | qkbd_state_switch_console(vs->vd->kbd, con); |
1851 | 0 | qemu_console_register_listener(con, &vs->vd->dcl, vs->vd->dcl.ops); |
1852 | 0 | } |
1853 | 0 | return; |
1854 | 0 | } |
1855 | 0 | default: |
1856 | 0 | break; |
1857 | 0 | } |
1858 | | |
1859 | | /* Turn off the lock state sync logic if the client support the led |
1860 | | state extension. |
1861 | | */ |
1862 | 0 | if (down && vs->vd->lock_key_sync && |
1863 | 0 | !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && |
1864 | 0 | keycode_is_keypad(vs->vd->kbd_layout, keycode)) { |
1865 | | /* If the numlock state needs to change then simulate an additional |
1866 | | keypress before sending this one. This will happen if the user |
1867 | | toggles numlock away from the VNC window. |
1868 | | */ |
1869 | 0 | if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { |
1870 | 0 | if (!qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) { |
1871 | 0 | trace_vnc_key_sync_numlock(true); |
1872 | 0 | press_key(vs, KEY_NUMLOCK); |
1873 | 0 | } |
1874 | 0 | } else { |
1875 | 0 | if (qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) { |
1876 | 0 | trace_vnc_key_sync_numlock(false); |
1877 | 0 | press_key(vs, KEY_NUMLOCK); |
1878 | 0 | } |
1879 | 0 | } |
1880 | 0 | } |
1881 | |
|
1882 | 0 | if (down && vs->vd->lock_key_sync && |
1883 | 0 | !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && |
1884 | 0 | ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { |
1885 | | /* If the capslock state needs to change then simulate an additional |
1886 | | keypress before sending this one. This will happen if the user |
1887 | | toggles capslock away from the VNC window. |
1888 | | */ |
1889 | 0 | int uppercase = !!(sym >= 'A' && sym <= 'Z'); |
1890 | 0 | bool shift = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_SHIFT); |
1891 | 0 | bool capslock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CAPSLOCK); |
1892 | 0 | if (capslock) { |
1893 | 0 | if (uppercase == shift) { |
1894 | 0 | trace_vnc_key_sync_capslock(false); |
1895 | 0 | press_key(vs, KEY_CAPSLOCK); |
1896 | 0 | } |
1897 | 0 | } else { |
1898 | 0 | if (uppercase != shift) { |
1899 | 0 | trace_vnc_key_sync_capslock(true); |
1900 | 0 | press_key(vs, KEY_CAPSLOCK); |
1901 | 0 | } |
1902 | 0 | } |
1903 | 0 | } |
1904 | |
|
1905 | 0 | qkbd_state_key_event(vs->vd->kbd, lnx, down); |
1906 | 0 | if (QEMU_IS_TEXT_CONSOLE(vs->vd->dcl.con)) { |
1907 | 0 | QemuTextConsole *con = QEMU_TEXT_CONSOLE(vs->vd->dcl.con); |
1908 | 0 | bool numlock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK); |
1909 | 0 | bool control = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL); |
1910 | | /* QEMU console emulation */ |
1911 | 0 | if (down) { |
1912 | 0 | switch (keycode) { |
1913 | 0 | case 0x2a: /* Left Shift */ |
1914 | 0 | case 0x36: /* Right Shift */ |
1915 | 0 | case 0x1d: /* Left CTRL */ |
1916 | 0 | case 0x9d: /* Right CTRL */ |
1917 | 0 | case 0x38: /* Left ALT */ |
1918 | 0 | case 0xb8: /* Right ALT */ |
1919 | 0 | break; |
1920 | 0 | case 0xc8: |
1921 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_UP); |
1922 | 0 | break; |
1923 | 0 | case 0xd0: |
1924 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_DOWN); |
1925 | 0 | break; |
1926 | 0 | case 0xcb: |
1927 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_LEFT); |
1928 | 0 | break; |
1929 | 0 | case 0xcd: |
1930 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_RIGHT); |
1931 | 0 | break; |
1932 | 0 | case 0xd3: |
1933 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_DELETE); |
1934 | 0 | break; |
1935 | 0 | case 0xc7: |
1936 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_HOME); |
1937 | 0 | break; |
1938 | 0 | case 0xcf: |
1939 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_END); |
1940 | 0 | break; |
1941 | 0 | case 0xc9: |
1942 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_PAGEUP); |
1943 | 0 | break; |
1944 | 0 | case 0xd1: |
1945 | 0 | qemu_text_console_put_keysym(con, QEMU_KEY_PAGEDOWN); |
1946 | 0 | break; |
1947 | | |
1948 | 0 | case 0x47: |
1949 | 0 | qemu_text_console_put_keysym(con, numlock ? '7' : QEMU_KEY_HOME); |
1950 | 0 | break; |
1951 | 0 | case 0x48: |
1952 | 0 | qemu_text_console_put_keysym(con, numlock ? '8' : QEMU_KEY_UP); |
1953 | 0 | break; |
1954 | 0 | case 0x49: |
1955 | 0 | qemu_text_console_put_keysym(con, numlock ? '9' : QEMU_KEY_PAGEUP); |
1956 | 0 | break; |
1957 | 0 | case 0x4b: |
1958 | 0 | qemu_text_console_put_keysym(con, numlock ? '4' : QEMU_KEY_LEFT); |
1959 | 0 | break; |
1960 | 0 | case 0x4c: |
1961 | 0 | qemu_text_console_put_keysym(con, '5'); |
1962 | 0 | break; |
1963 | 0 | case 0x4d: |
1964 | 0 | qemu_text_console_put_keysym(con, numlock ? '6' : QEMU_KEY_RIGHT); |
1965 | 0 | break; |
1966 | 0 | case 0x4f: |
1967 | 0 | qemu_text_console_put_keysym(con, numlock ? '1' : QEMU_KEY_END); |
1968 | 0 | break; |
1969 | 0 | case 0x50: |
1970 | 0 | qemu_text_console_put_keysym(con, numlock ? '2' : QEMU_KEY_DOWN); |
1971 | 0 | break; |
1972 | 0 | case 0x51: |
1973 | 0 | qemu_text_console_put_keysym(con, numlock ? '3' : QEMU_KEY_PAGEDOWN); |
1974 | 0 | break; |
1975 | 0 | case 0x52: |
1976 | 0 | qemu_text_console_put_keysym(con, '0'); |
1977 | 0 | break; |
1978 | 0 | case 0x53: |
1979 | 0 | qemu_text_console_put_keysym(con, numlock ? '.' : QEMU_KEY_DELETE); |
1980 | 0 | break; |
1981 | | |
1982 | 0 | case 0xb5: |
1983 | 0 | qemu_text_console_put_keysym(con, '/'); |
1984 | 0 | break; |
1985 | 0 | case 0x37: |
1986 | 0 | qemu_text_console_put_keysym(con, '*'); |
1987 | 0 | break; |
1988 | 0 | case 0x4a: |
1989 | 0 | qemu_text_console_put_keysym(con, '-'); |
1990 | 0 | break; |
1991 | 0 | case 0x4e: |
1992 | 0 | qemu_text_console_put_keysym(con, '+'); |
1993 | 0 | break; |
1994 | 0 | case 0x9c: |
1995 | 0 | qemu_text_console_put_keysym(con, '\n'); |
1996 | 0 | break; |
1997 | | |
1998 | 0 | default: |
1999 | 0 | if (control) { |
2000 | 0 | qemu_text_console_put_keysym(con, sym & 0x1f); |
2001 | 0 | } else { |
2002 | 0 | qemu_text_console_put_keysym(con, sym); |
2003 | 0 | } |
2004 | 0 | break; |
2005 | 0 | } |
2006 | 0 | } |
2007 | 0 | } |
2008 | 0 | } |
2009 | | |
2010 | | static const char *code2name(int keycode) |
2011 | 0 | { |
2012 | 0 | return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); |
2013 | 0 | } |
2014 | | |
2015 | | static void key_event(VncState *vs, bool down, uint32_t sym) |
2016 | 0 | { |
2017 | 0 | int keycode; |
2018 | 0 | int lsym = sym; |
2019 | |
|
2020 | 0 | if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(vs->vd->dcl.con)) { |
2021 | 0 | lsym = lsym - 'A' + 'a'; |
2022 | 0 | } |
2023 | |
|
2024 | 0 | keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF, |
2025 | 0 | vs->vd->kbd, down) & SCANCODE_KEYMASK; |
2026 | 0 | trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); |
2027 | 0 | do_key_event(vs, down, keycode, sym); |
2028 | 0 | } |
2029 | | |
2030 | | static void ext_key_event(VncState *vs, bool down, |
2031 | | uint32_t sym, uint32_t keycode) |
2032 | 0 | { |
2033 | | /* if the user specifies a keyboard layout, always use it */ |
2034 | 0 | if (keyboard_layout) { |
2035 | 0 | key_event(vs, down, sym); |
2036 | 0 | } else { |
2037 | 0 | trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); |
2038 | 0 | do_key_event(vs, down, keycode, sym); |
2039 | 0 | } |
2040 | 0 | } |
2041 | | |
2042 | | static void framebuffer_update_request(VncState *vs, uint8_t incremental, |
2043 | | uint16_t x, uint16_t y, |
2044 | | uint16_t w, uint16_t h) |
2045 | 0 | { |
2046 | 0 | if (incremental) { |
2047 | 0 | if (vs->update != VNC_STATE_UPDATE_FORCE) { |
2048 | 0 | vs->update = VNC_STATE_UPDATE_INCREMENTAL; |
2049 | 0 | } |
2050 | 0 | } else { |
2051 | 0 | vs->update = VNC_STATE_UPDATE_FORCE; |
2052 | 0 | vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); |
2053 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_RESIZE_EXT)) { |
2054 | 0 | vnc_desktop_resize_ext(vs, 0); |
2055 | 0 | } |
2056 | 0 | } |
2057 | 0 | } |
2058 | | |
2059 | | static void send_ext_key_event_ack(VncState *vs) |
2060 | 0 | { |
2061 | 0 | vnc_lock_output(vs); |
2062 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
2063 | 0 | vnc_write_u8(vs, 0); |
2064 | 0 | vnc_write_u16(vs, 1); |
2065 | 0 | vnc_framebuffer_update(vs, 0, 0, |
2066 | 0 | pixman_image_get_width(vs->vd->server), |
2067 | 0 | pixman_image_get_height(vs->vd->server), |
2068 | 0 | VNC_ENCODING_EXT_KEY_EVENT); |
2069 | 0 | vnc_unlock_output(vs); |
2070 | 0 | vnc_flush(vs); |
2071 | 0 | } |
2072 | | |
2073 | | static void send_ext_audio_ack(VncState *vs) |
2074 | 0 | { |
2075 | 0 | vnc_lock_output(vs); |
2076 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
2077 | 0 | vnc_write_u8(vs, 0); |
2078 | 0 | vnc_write_u16(vs, 1); |
2079 | 0 | vnc_framebuffer_update(vs, 0, 0, |
2080 | 0 | pixman_image_get_width(vs->vd->server), |
2081 | 0 | pixman_image_get_height(vs->vd->server), |
2082 | 0 | VNC_ENCODING_AUDIO); |
2083 | 0 | vnc_unlock_output(vs); |
2084 | 0 | vnc_flush(vs); |
2085 | 0 | } |
2086 | | |
2087 | | static void send_xvp_message(VncState *vs, int code) |
2088 | 0 | { |
2089 | 0 | vnc_lock_output(vs); |
2090 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_XVP); |
2091 | 0 | vnc_write_u8(vs, 0); /* pad */ |
2092 | 0 | vnc_write_u8(vs, 1); /* version */ |
2093 | 0 | vnc_write_u8(vs, code); |
2094 | 0 | vnc_unlock_output(vs); |
2095 | 0 | vnc_flush(vs); |
2096 | 0 | } |
2097 | | |
2098 | | static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) |
2099 | 0 | { |
2100 | 0 | VncConnection *vc = container_of(vs, VncConnection, vs); |
2101 | 0 | int i; |
2102 | 0 | unsigned int enc = 0; |
2103 | |
|
2104 | 0 | vs->features = 0; |
2105 | 0 | vs->vnc_encoding = 0; |
2106 | 0 | vc->worker.tight.compression = 9; |
2107 | 0 | vc->worker.tight.quality = -1; /* Lossless by default */ |
2108 | 0 | vs->absolute = -1; |
2109 | | |
2110 | | /* |
2111 | | * Start from the end because the encodings are sent in order of preference. |
2112 | | * This way the preferred encoding (first encoding defined in the array) |
2113 | | * will be set at the end of the loop. |
2114 | | */ |
2115 | 0 | for (i = n_encodings - 1; i >= 0; i--) { |
2116 | 0 | enc = encodings[i]; |
2117 | 0 | switch (enc) { |
2118 | 0 | case VNC_ENCODING_RAW: |
2119 | 0 | vs->vnc_encoding = enc; |
2120 | 0 | break; |
2121 | 0 | case VNC_ENCODING_HEXTILE: |
2122 | 0 | vnc_set_feature(vs, VNC_FEATURE_HEXTILE); |
2123 | 0 | vs->vnc_encoding = enc; |
2124 | 0 | break; |
2125 | 0 | case VNC_ENCODING_TIGHT: |
2126 | 0 | vnc_set_feature(vs, VNC_FEATURE_TIGHT); |
2127 | 0 | vs->vnc_encoding = enc; |
2128 | 0 | break; |
2129 | | #ifdef CONFIG_PNG |
2130 | | case VNC_ENCODING_TIGHT_PNG: |
2131 | | vnc_set_feature(vs, VNC_FEATURE_TIGHT_PNG); |
2132 | | vs->vnc_encoding = enc; |
2133 | | break; |
2134 | | #endif |
2135 | 0 | case VNC_ENCODING_ZLIB: |
2136 | | /* |
2137 | | * VNC_ENCODING_ZRLE compresses better than VNC_ENCODING_ZLIB. |
2138 | | * So prioritize ZRLE, even if the client hints that it prefers |
2139 | | * ZLIB. |
2140 | | */ |
2141 | 0 | if (!vnc_has_feature(vs, VNC_FEATURE_ZRLE)) { |
2142 | 0 | vnc_set_feature(vs, VNC_FEATURE_ZLIB); |
2143 | 0 | vs->vnc_encoding = enc; |
2144 | 0 | } |
2145 | 0 | break; |
2146 | 0 | case VNC_ENCODING_ZRLE: |
2147 | 0 | vnc_set_feature(vs, VNC_FEATURE_ZRLE); |
2148 | 0 | vs->vnc_encoding = enc; |
2149 | 0 | break; |
2150 | 0 | case VNC_ENCODING_ZYWRLE: |
2151 | 0 | vnc_set_feature(vs, VNC_FEATURE_ZYWRLE); |
2152 | 0 | vs->vnc_encoding = enc; |
2153 | 0 | break; |
2154 | 0 | case VNC_ENCODING_DESKTOPRESIZE: |
2155 | 0 | vnc_set_feature(vs, VNC_FEATURE_RESIZE); |
2156 | 0 | break; |
2157 | 0 | case VNC_ENCODING_DESKTOP_RESIZE_EXT: |
2158 | 0 | vnc_set_feature(vs, VNC_FEATURE_RESIZE_EXT); |
2159 | 0 | break; |
2160 | 0 | case VNC_ENCODING_POINTER_TYPE_CHANGE: |
2161 | 0 | vnc_set_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE); |
2162 | 0 | break; |
2163 | 0 | case VNC_ENCODING_RICH_CURSOR: |
2164 | 0 | vnc_set_feature(vs, VNC_FEATURE_RICH_CURSOR); |
2165 | 0 | break; |
2166 | 0 | case VNC_ENCODING_ALPHA_CURSOR: |
2167 | 0 | vnc_set_feature(vs, VNC_FEATURE_ALPHA_CURSOR); |
2168 | 0 | break; |
2169 | 0 | case VNC_ENCODING_EXT_KEY_EVENT: |
2170 | 0 | send_ext_key_event_ack(vs); |
2171 | 0 | break; |
2172 | 0 | case VNC_ENCODING_AUDIO: |
2173 | 0 | if (vs->vd->audio_be) { |
2174 | 0 | vnc_set_feature(vs, VNC_FEATURE_AUDIO); |
2175 | 0 | send_ext_audio_ack(vs); |
2176 | 0 | } |
2177 | 0 | break; |
2178 | 0 | case VNC_ENCODING_WMVi: |
2179 | 0 | vnc_set_feature(vs, VNC_FEATURE_WMVI); |
2180 | 0 | break; |
2181 | 0 | case VNC_ENCODING_LED_STATE: |
2182 | 0 | vnc_set_feature(vs, VNC_FEATURE_LED_STATE); |
2183 | 0 | break; |
2184 | 0 | case VNC_ENCODING_XVP: |
2185 | 0 | if (vs->vd->power_control) { |
2186 | 0 | vnc_set_feature(vs, VNC_FEATURE_XVP); |
2187 | 0 | send_xvp_message(vs, VNC_XVP_CODE_INIT); |
2188 | 0 | } |
2189 | 0 | break; |
2190 | 0 | case VNC_ENCODING_CLIPBOARD_EXT: |
2191 | 0 | vnc_set_feature(vs, VNC_FEATURE_CLIPBOARD_EXT); |
2192 | 0 | vnc_server_cut_text_caps(vs); |
2193 | 0 | break; |
2194 | 0 | case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: |
2195 | 0 | vc->worker.tight.compression = (enc & 0x0F); |
2196 | 0 | break; |
2197 | 0 | case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: |
2198 | 0 | if (vs->vd->lossy) { |
2199 | 0 | vc->worker.tight.quality = (enc & 0x0F); |
2200 | 0 | } |
2201 | 0 | break; |
2202 | 0 | default: |
2203 | 0 | trace_vnc_client_unknown_encoding(vs, i, enc); |
2204 | 0 | break; |
2205 | 0 | } |
2206 | 0 | } |
2207 | 0 | vnc_desktop_resize(vs); |
2208 | 0 | check_pointer_type_change(&vs->mouse_mode_notifier, NULL); |
2209 | 0 | vnc_led_state_change(vs); |
2210 | 0 | vnc_cursor_define(vs); |
2211 | 0 | } |
2212 | | |
2213 | | static void set_pixel_conversion(VncState *vs) |
2214 | 0 | { |
2215 | 0 | pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf, |
2216 | 0 | vs->client_endian); |
2217 | |
|
2218 | 0 | if (fmt == VNC_SERVER_FB_FORMAT) { |
2219 | 0 | vs->write_pixels = vnc_write_pixels_copy; |
2220 | 0 | vnc_hextile_set_pixel_conversion(vs, 0); |
2221 | 0 | } else { |
2222 | 0 | vs->write_pixels = vnc_write_pixels_generic; |
2223 | 0 | vnc_hextile_set_pixel_conversion(vs, 1); |
2224 | 0 | } |
2225 | 0 | } |
2226 | | |
2227 | | static void send_color_map(VncState *vs) |
2228 | 0 | { |
2229 | 0 | int i; |
2230 | |
|
2231 | 0 | vnc_lock_output(vs); |
2232 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); |
2233 | 0 | vnc_write_u8(vs, 0); /* padding */ |
2234 | 0 | vnc_write_u16(vs, 0); /* first color */ |
2235 | 0 | vnc_write_u16(vs, 256); /* # of colors */ |
2236 | |
|
2237 | 0 | for (i = 0; i < 256; i++) { |
2238 | 0 | PixelFormat *pf = &vs->client_pf; |
2239 | |
|
2240 | 0 | vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); |
2241 | 0 | vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); |
2242 | 0 | vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); |
2243 | 0 | } |
2244 | 0 | vnc_unlock_output(vs); |
2245 | 0 | } |
2246 | | |
2247 | | static void set_pixel_format(VncState *vs, uint8_t bits_per_pixel, |
2248 | | uint8_t big_endian_flag, uint8_t true_color_flag, |
2249 | | uint16_t red_max, uint16_t green_max, |
2250 | | uint16_t blue_max, uint8_t red_shift, |
2251 | | uint8_t green_shift, uint8_t blue_shift) |
2252 | 0 | { |
2253 | 0 | if (!true_color_flag) { |
2254 | | /* Expose a reasonable default 256 color map */ |
2255 | 0 | bits_per_pixel = 8; |
2256 | 0 | red_max = 7; |
2257 | 0 | green_max = 7; |
2258 | 0 | blue_max = 3; |
2259 | 0 | red_shift = 0; |
2260 | 0 | green_shift = 3; |
2261 | 0 | blue_shift = 6; |
2262 | 0 | } |
2263 | |
|
2264 | 0 | switch (bits_per_pixel) { |
2265 | 0 | case 8: |
2266 | 0 | case 16: |
2267 | 0 | case 32: |
2268 | 0 | break; |
2269 | 0 | default: |
2270 | 0 | vnc_client_error(vs); |
2271 | 0 | return; |
2272 | 0 | } |
2273 | | |
2274 | 0 | if (red_max > UINT8_MAX || green_max > UINT8_MAX || blue_max > UINT8_MAX) { |
2275 | 0 | vnc_client_error(vs); |
2276 | 0 | return; |
2277 | 0 | } |
2278 | | |
2279 | 0 | if (red_shift >= bits_per_pixel || red_shift >= 32 || |
2280 | 0 | green_shift >= bits_per_pixel || green_shift >= 32 || |
2281 | 0 | blue_shift >= bits_per_pixel || blue_shift >= 32) { |
2282 | 0 | vnc_client_error(vs); |
2283 | 0 | return; |
2284 | 0 | } |
2285 | | |
2286 | 0 | vs->client_pf.rmax = red_max ? red_max : 0xFF; |
2287 | 0 | vs->client_pf.rbits = ctpopl(red_max); |
2288 | 0 | vs->client_pf.rshift = red_shift; |
2289 | 0 | vs->client_pf.rmask = (uint32_t)red_max << red_shift; |
2290 | 0 | vs->client_pf.gmax = green_max ? green_max : 0xFF; |
2291 | 0 | vs->client_pf.gbits = ctpopl(green_max); |
2292 | 0 | vs->client_pf.gshift = green_shift; |
2293 | 0 | vs->client_pf.gmask = (uint32_t)green_max << green_shift; |
2294 | 0 | vs->client_pf.bmax = blue_max ? blue_max : 0xFF; |
2295 | 0 | vs->client_pf.bbits = ctpopl(blue_max); |
2296 | 0 | vs->client_pf.bshift = blue_shift; |
2297 | 0 | vs->client_pf.bmask = (uint32_t)blue_max << blue_shift; |
2298 | 0 | vs->client_pf.bits_per_pixel = bits_per_pixel; |
2299 | 0 | vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; |
2300 | 0 | vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; |
2301 | 0 | vs->client_endian = big_endian_flag ? G_BIG_ENDIAN : G_LITTLE_ENDIAN; |
2302 | 0 | trace_vnc_client_pixel_format(vs, vs->ioc, |
2303 | 0 | vs->client_pf.bits_per_pixel, |
2304 | 0 | vs->client_pf.depth, |
2305 | 0 | vs->client_endian); |
2306 | 0 | trace_vnc_client_pixel_format_red(vs, vs->ioc, |
2307 | 0 | vs->client_pf.rmax, |
2308 | 0 | vs->client_pf.rbits, |
2309 | 0 | vs->client_pf.rshift, |
2310 | 0 | vs->client_pf.rmask); |
2311 | 0 | trace_vnc_client_pixel_format_green(vs, vs->ioc, |
2312 | 0 | vs->client_pf.gmax, |
2313 | 0 | vs->client_pf.gbits, |
2314 | 0 | vs->client_pf.gshift, |
2315 | 0 | vs->client_pf.gmask); |
2316 | 0 | trace_vnc_client_pixel_format_blue(vs, vs->ioc, |
2317 | 0 | vs->client_pf.bmax, |
2318 | 0 | vs->client_pf.bbits, |
2319 | 0 | vs->client_pf.bshift, |
2320 | 0 | vs->client_pf.bmask); |
2321 | |
|
2322 | 0 | if (!true_color_flag) { |
2323 | 0 | send_color_map(vs); |
2324 | 0 | } |
2325 | |
|
2326 | 0 | set_pixel_conversion(vs); |
2327 | |
|
2328 | 0 | qemu_console_hw_invalidate(vs->vd->dcl.con); |
2329 | 0 | qemu_console_hw_update(vs->vd->dcl.con); |
2330 | 0 | } |
2331 | | |
2332 | 0 | static void pixel_format_message (VncState *vs) { |
2333 | 0 | char pad[3] = { 0, 0, 0 }; |
2334 | |
|
2335 | 0 | vs->client_pf = qemu_default_pixelformat(32); |
2336 | 0 | vs->client_endian = G_BYTE_ORDER; |
2337 | |
|
2338 | 0 | vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ |
2339 | 0 | vnc_write_u8(vs, vs->client_pf.depth); /* depth */ |
2340 | |
|
2341 | | #if HOST_BIG_ENDIAN |
2342 | | vnc_write_u8(vs, 1); /* big-endian-flag */ |
2343 | | #else |
2344 | 0 | vnc_write_u8(vs, 0); /* big-endian-flag */ |
2345 | 0 | #endif |
2346 | 0 | vnc_write_u8(vs, 1); /* true-color-flag */ |
2347 | 0 | vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ |
2348 | 0 | vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ |
2349 | 0 | vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ |
2350 | 0 | vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ |
2351 | 0 | vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ |
2352 | 0 | vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ |
2353 | 0 | vnc_write(vs, pad, 3); /* padding */ |
2354 | |
|
2355 | 0 | vnc_hextile_set_pixel_conversion(vs, 0); |
2356 | 0 | vs->write_pixels = vnc_write_pixels_copy; |
2357 | 0 | } |
2358 | | |
2359 | | static void vnc_colordepth(VncState *vs) |
2360 | 0 | { |
2361 | 0 | if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { |
2362 | | /* Sending a WMVi message to notify the client*/ |
2363 | 0 | vnc_lock_output(vs); |
2364 | 0 | vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
2365 | 0 | vnc_write_u8(vs, 0); |
2366 | 0 | vnc_write_u16(vs, 1); /* number of rects */ |
2367 | 0 | vnc_framebuffer_update(vs, 0, 0, |
2368 | 0 | vs->client_width, |
2369 | 0 | vs->client_height, |
2370 | 0 | VNC_ENCODING_WMVi); |
2371 | 0 | pixel_format_message(vs); |
2372 | 0 | vnc_unlock_output(vs); |
2373 | 0 | vnc_flush(vs); |
2374 | 0 | } else { |
2375 | 0 | set_pixel_conversion(vs); |
2376 | 0 | } |
2377 | 0 | } |
2378 | | |
2379 | | static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) |
2380 | 0 | { |
2381 | 0 | int i; |
2382 | 0 | uint16_t limit; |
2383 | 0 | uint32_t freq; |
2384 | 0 | VncDisplay *vd = vs->vd; |
2385 | |
|
2386 | 0 | if (data[0] > 3) { |
2387 | 0 | qemu_console_listener_set_refresh(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); |
2388 | 0 | } |
2389 | |
|
2390 | 0 | switch (data[0]) { |
2391 | 0 | case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: |
2392 | 0 | if (len == 1) |
2393 | 0 | return 20; |
2394 | | |
2395 | 0 | trace_vnc_msg_client_set_pixel_format(vs, vs->ioc, |
2396 | 0 | read_u8(data, 4), |
2397 | 0 | read_u8(data, 6), |
2398 | 0 | read_u8(data, 7)); |
2399 | 0 | trace_vnc_msg_client_set_pixel_format_rgb(vs, vs->ioc, |
2400 | 0 | read_u16(data, 8), |
2401 | 0 | read_u16(data, 10), |
2402 | 0 | read_u16(data, 12), |
2403 | 0 | read_u8(data, 14), |
2404 | 0 | read_u8(data, 15), |
2405 | 0 | read_u8(data, 16)); |
2406 | 0 | set_pixel_format(vs, read_u8(data, 4), |
2407 | 0 | read_u8(data, 6), read_u8(data, 7), |
2408 | 0 | read_u16(data, 8), read_u16(data, 10), |
2409 | 0 | read_u16(data, 12), read_u8(data, 14), |
2410 | 0 | read_u8(data, 15), read_u8(data, 16)); |
2411 | 0 | break; |
2412 | 0 | case VNC_MSG_CLIENT_SET_ENCODINGS: |
2413 | 0 | if (len == 1) |
2414 | 0 | return 4; |
2415 | | |
2416 | 0 | if (len == 4) { |
2417 | 0 | limit = read_u16(data, 2); |
2418 | 0 | if (limit > 0) |
2419 | 0 | return 4 + (limit * 4); |
2420 | 0 | } else |
2421 | 0 | limit = read_u16(data, 2); |
2422 | | |
2423 | 0 | for (i = 0; i < limit; i++) { |
2424 | 0 | int32_t val = read_s32(data, 4 + (i * 4)); |
2425 | 0 | memcpy(data + 4 + (i * 4), &val, sizeof(val)); |
2426 | 0 | } |
2427 | |
|
2428 | 0 | trace_vnc_msg_client_set_encodings(vs, vs->ioc, limit); |
2429 | 0 | set_encodings(vs, (int32_t *)(data + 4), limit); |
2430 | 0 | break; |
2431 | 0 | case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: |
2432 | 0 | if (len == 1) |
2433 | 0 | return 10; |
2434 | | |
2435 | 0 | trace_vnc_msg_client_framebuffer_update_request(vs, vs->ioc, |
2436 | 0 | read_u8(data, 1), |
2437 | 0 | read_u16(data, 2), |
2438 | 0 | read_u16(data, 4), |
2439 | 0 | read_u16(data, 6), |
2440 | 0 | read_u16(data, 8)); |
2441 | 0 | framebuffer_update_request(vs, |
2442 | 0 | read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), |
2443 | 0 | read_u16(data, 6), read_u16(data, 8)); |
2444 | 0 | break; |
2445 | 0 | case VNC_MSG_CLIENT_KEY_EVENT: |
2446 | 0 | if (len == 1) |
2447 | 0 | return 8; |
2448 | | |
2449 | 0 | trace_vnc_msg_client_key_event(vs, vs->ioc, |
2450 | 0 | read_u8(data, 1), |
2451 | 0 | read_u32(data, 4)); |
2452 | 0 | key_event(vs, read_u8(data, 1), read_u32(data, 4)); |
2453 | 0 | break; |
2454 | 0 | case VNC_MSG_CLIENT_POINTER_EVENT: |
2455 | 0 | if (len == 1) |
2456 | 0 | return 6; |
2457 | | |
2458 | 0 | trace_vnc_msg_client_pointer_event(vs, vs->ioc, |
2459 | 0 | read_u8(data, 1), |
2460 | 0 | read_u16(data, 2), |
2461 | 0 | read_u16(data, 4)); |
2462 | 0 | pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); |
2463 | 0 | break; |
2464 | 0 | case VNC_MSG_CLIENT_CUT_TEXT: |
2465 | 0 | if (len == 1) { |
2466 | 0 | return 8; |
2467 | 0 | } |
2468 | 0 | uint32_t dlen = abs(read_s32(data, 4)); |
2469 | 0 | if (len == 8) { |
2470 | 0 | if (dlen > (1 << 20)) { |
2471 | 0 | error_report("vnc: client_cut_text msg payload has %u bytes" |
2472 | 0 | " which exceeds our limit of 1MB.", dlen); |
2473 | 0 | vnc_client_error(vs); |
2474 | 0 | break; |
2475 | 0 | } |
2476 | 0 | if (dlen > 0) { |
2477 | 0 | return 8 + dlen; |
2478 | 0 | } |
2479 | 0 | } |
2480 | | |
2481 | 0 | if (read_s32(data, 4) < 0) { |
2482 | 0 | if (!vnc_has_feature(vs, VNC_FEATURE_CLIPBOARD_EXT)) { |
2483 | 0 | error_report("vnc: extended clipboard message while disabled"); |
2484 | 0 | vnc_client_error(vs); |
2485 | 0 | break; |
2486 | 0 | } |
2487 | 0 | if (dlen < 4) { |
2488 | 0 | error_report("vnc: malformed payload (header less than 4 bytes)" |
2489 | 0 | " in extended clipboard pseudo-encoding."); |
2490 | 0 | vnc_client_error(vs); |
2491 | 0 | break; |
2492 | 0 | } |
2493 | 0 | trace_vnc_msg_client_cut_text_ext(vs, vs->ioc, |
2494 | 0 | dlen, read_u32(data, 8)); |
2495 | 0 | vnc_client_cut_text_ext(vs, dlen, read_u32(data, 8), data + 12); |
2496 | 0 | break; |
2497 | 0 | } |
2498 | 0 | trace_vnc_msg_client_cut_text(vs, vs->ioc, read_u32(data, 4)); |
2499 | 0 | vnc_client_cut_text(vs, read_u32(data, 4), data + 8); |
2500 | 0 | break; |
2501 | 0 | case VNC_MSG_CLIENT_XVP: |
2502 | 0 | if (!vnc_has_feature(vs, VNC_FEATURE_XVP)) { |
2503 | 0 | error_report("vnc: xvp client message while disabled"); |
2504 | 0 | vnc_client_error(vs); |
2505 | 0 | break; |
2506 | 0 | } |
2507 | 0 | if (len == 1) { |
2508 | 0 | return 4; |
2509 | 0 | } |
2510 | 0 | if (len == 4) { |
2511 | 0 | uint8_t version = read_u8(data, 2); |
2512 | 0 | uint8_t action = read_u8(data, 3); |
2513 | 0 | trace_vnc_msg_client_xvp(vs, vs->ioc, version, action); |
2514 | |
|
2515 | 0 | if (version != 1) { |
2516 | 0 | error_report("vnc: xvp client message version %d != 1", |
2517 | 0 | version); |
2518 | 0 | vnc_client_error(vs); |
2519 | 0 | break; |
2520 | 0 | } |
2521 | | |
2522 | 0 | switch (action) { |
2523 | 0 | case VNC_XVP_ACTION_SHUTDOWN: |
2524 | 0 | vnc_action_shutdown(vs); |
2525 | 0 | break; |
2526 | 0 | case VNC_XVP_ACTION_REBOOT: |
2527 | 0 | send_xvp_message(vs, VNC_XVP_CODE_FAIL); |
2528 | 0 | break; |
2529 | 0 | case VNC_XVP_ACTION_RESET: |
2530 | 0 | vnc_action_reset(vs); |
2531 | 0 | break; |
2532 | 0 | default: |
2533 | 0 | send_xvp_message(vs, VNC_XVP_CODE_FAIL); |
2534 | 0 | break; |
2535 | 0 | } |
2536 | 0 | } |
2537 | 0 | break; |
2538 | 0 | case VNC_MSG_CLIENT_QEMU: |
2539 | 0 | if (len == 1) |
2540 | 0 | return 2; |
2541 | | |
2542 | 0 | switch (read_u8(data, 1)) { |
2543 | 0 | case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: |
2544 | 0 | if (len == 2) |
2545 | 0 | return 12; |
2546 | | |
2547 | 0 | trace_vnc_msg_client_ext_key_event(vs, vs->ioc, |
2548 | 0 | read_u16(data, 2), |
2549 | 0 | read_u32(data, 4), |
2550 | 0 | read_u32(data, 8)); |
2551 | 0 | ext_key_event(vs, read_u16(data, 2), |
2552 | 0 | read_u32(data, 4), read_u32(data, 8)); |
2553 | 0 | break; |
2554 | 0 | case VNC_MSG_CLIENT_QEMU_AUDIO: |
2555 | 0 | if (!vnc_has_feature(vs, VNC_FEATURE_AUDIO)) { |
2556 | 0 | error_report("Audio message %d with audio disabled", read_u8(data, 2)); |
2557 | 0 | vnc_client_error(vs); |
2558 | 0 | break; |
2559 | 0 | } |
2560 | | |
2561 | 0 | if (len == 2) |
2562 | 0 | return 4; |
2563 | | |
2564 | 0 | switch (read_u16 (data, 2)) { |
2565 | 0 | case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: |
2566 | 0 | trace_vnc_msg_client_audio_enable(vs, vs->ioc); |
2567 | 0 | audio_add(vs); |
2568 | 0 | break; |
2569 | 0 | case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: |
2570 | 0 | trace_vnc_msg_client_audio_disable(vs, vs->ioc); |
2571 | 0 | audio_del(vs); |
2572 | 0 | break; |
2573 | 0 | case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: |
2574 | 0 | if (len == 4) |
2575 | 0 | return 10; |
2576 | 0 | switch (read_u8(data, 4)) { |
2577 | 0 | case 0: vs->as.fmt = AUDIO_FORMAT_U8; break; |
2578 | 0 | case 1: vs->as.fmt = AUDIO_FORMAT_S8; break; |
2579 | 0 | case 2: vs->as.fmt = AUDIO_FORMAT_U16; break; |
2580 | 0 | case 3: vs->as.fmt = AUDIO_FORMAT_S16; break; |
2581 | 0 | case 4: vs->as.fmt = AUDIO_FORMAT_U32; break; |
2582 | 0 | case 5: vs->as.fmt = AUDIO_FORMAT_S32; break; |
2583 | 0 | default: |
2584 | 0 | trace_vnc_client_invalid_audio_format(vs, read_u8(data, 4)); |
2585 | 0 | vnc_client_error(vs); |
2586 | 0 | break; |
2587 | 0 | } |
2588 | 0 | vs->as.nchannels = read_u8(data, 5); |
2589 | 0 | if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { |
2590 | 0 | trace_vnc_client_invalid_audio_channels(vs, read_u8(data, 5)); |
2591 | 0 | vnc_client_error(vs); |
2592 | 0 | break; |
2593 | 0 | } |
2594 | 0 | freq = read_u32(data, 6); |
2595 | | /* No official limit for protocol, but 48khz is a sensible |
2596 | | * upper bound for trustworthy clients, and this limit |
2597 | | * protects calculations involving 'vs->as.freq' later. |
2598 | | */ |
2599 | 0 | if (freq > 48000) { |
2600 | 0 | trace_vnc_client_invalid_audio_freq(vs, freq); |
2601 | 0 | vnc_client_error(vs); |
2602 | 0 | break; |
2603 | 0 | } |
2604 | 0 | vs->as.freq = freq; |
2605 | 0 | trace_vnc_msg_client_audio_format( |
2606 | 0 | vs, vs->ioc, vs->as.fmt, vs->as.nchannels, vs->as.freq); |
2607 | 0 | break; |
2608 | 0 | default: |
2609 | 0 | trace_vnc_client_invalid_audio_msg(vs, read_u8(data, 2)); |
2610 | 0 | vnc_client_error(vs); |
2611 | 0 | break; |
2612 | 0 | } |
2613 | 0 | break; |
2614 | | |
2615 | 0 | default: |
2616 | 0 | trace_vnc_client_unknown_qemu_msg(vs, read_u16(data, 0)); |
2617 | 0 | vnc_client_error(vs); |
2618 | 0 | break; |
2619 | 0 | } |
2620 | 0 | break; |
2621 | 0 | case VNC_MSG_CLIENT_SET_DESKTOP_SIZE: |
2622 | 0 | { |
2623 | 0 | size_t size; |
2624 | 0 | uint8_t screens; |
2625 | 0 | int w, h; |
2626 | |
|
2627 | 0 | if (len < 8) { |
2628 | 0 | return 8; |
2629 | 0 | } |
2630 | | |
2631 | 0 | screens = read_u8(data, 6); |
2632 | 0 | size = 8 + screens * 16; |
2633 | 0 | if (len < size) { |
2634 | 0 | return size; |
2635 | 0 | } |
2636 | 0 | w = read_u16(data, 2); |
2637 | 0 | h = read_u16(data, 4); |
2638 | |
|
2639 | 0 | trace_vnc_msg_client_set_desktop_size(vs, vs->ioc, w, h, screens); |
2640 | 0 | if (qemu_console_ui_info_supported(vs->vd->dcl.con)) { |
2641 | 0 | QemuUIInfo info = { .width = w, .height = h }; |
2642 | 0 | qemu_console_set_ui_info(vs->vd->dcl.con, &info, false); |
2643 | 0 | vnc_desktop_resize_ext(vs, 4 /* Request forwarded */); |
2644 | 0 | } else { |
2645 | 0 | vnc_desktop_resize_ext(vs, 3 /* Invalid screen layout */); |
2646 | 0 | } |
2647 | |
|
2648 | 0 | break; |
2649 | 0 | } |
2650 | 0 | default: |
2651 | 0 | trace_vnc_client_unknown_msg(vs, data[0]); |
2652 | 0 | vnc_client_error(vs); |
2653 | 0 | break; |
2654 | 0 | } |
2655 | | |
2656 | 0 | vnc_update_throttle_offset(vs); |
2657 | 0 | vnc_read_when(vs, protocol_client_msg, 1); |
2658 | 0 | return 0; |
2659 | 0 | } |
2660 | | |
2661 | | static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) |
2662 | 0 | { |
2663 | 0 | char buf[1024]; |
2664 | 0 | VncShareMode mode; |
2665 | 0 | int size; |
2666 | |
|
2667 | 0 | mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; |
2668 | 0 | switch (vs->vd->share_policy) { |
2669 | 0 | case VNC_SHARE_POLICY_IGNORE: |
2670 | | /* |
2671 | | * Ignore the shared flag. Nothing to do here. |
2672 | | * |
2673 | | * Doesn't conform to the rfb spec but is traditional qemu |
2674 | | * behavior, thus left here as option for compatibility |
2675 | | * reasons. |
2676 | | */ |
2677 | 0 | break; |
2678 | 0 | case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: |
2679 | | /* |
2680 | | * Policy: Allow clients ask for exclusive access. |
2681 | | * |
2682 | | * Implementation: When a client asks for exclusive access, |
2683 | | * disconnect all others. Shared connects are allowed as long |
2684 | | * as no exclusive connection exists. |
2685 | | * |
2686 | | * This is how the rfb spec suggests to handle the shared flag. |
2687 | | */ |
2688 | 0 | if (mode == VNC_SHARE_MODE_EXCLUSIVE) { |
2689 | 0 | VncState *client; |
2690 | 0 | QTAILQ_FOREACH(client, &vs->vd->clients, next) { |
2691 | 0 | if (vs == client) { |
2692 | 0 | continue; |
2693 | 0 | } |
2694 | 0 | if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && |
2695 | 0 | client->share_mode != VNC_SHARE_MODE_SHARED) { |
2696 | 0 | continue; |
2697 | 0 | } |
2698 | 0 | vnc_disconnect_start(client); |
2699 | 0 | } |
2700 | 0 | } |
2701 | 0 | if (mode == VNC_SHARE_MODE_SHARED) { |
2702 | 0 | if (vs->vd->num_exclusive > 0) { |
2703 | 0 | vnc_disconnect_start(vs); |
2704 | 0 | return 0; |
2705 | 0 | } |
2706 | 0 | } |
2707 | 0 | break; |
2708 | 0 | case VNC_SHARE_POLICY_FORCE_SHARED: |
2709 | | /* |
2710 | | * Policy: Shared connects only. |
2711 | | * Implementation: Disallow clients asking for exclusive access. |
2712 | | * |
2713 | | * Useful for shared desktop sessions where you don't want |
2714 | | * someone forgetting to say -shared when running the vnc |
2715 | | * client disconnect everybody else. |
2716 | | */ |
2717 | 0 | if (mode == VNC_SHARE_MODE_EXCLUSIVE) { |
2718 | 0 | vnc_disconnect_start(vs); |
2719 | 0 | return 0; |
2720 | 0 | } |
2721 | 0 | break; |
2722 | 0 | } |
2723 | 0 | vnc_set_share_mode(vs, mode); |
2724 | |
|
2725 | 0 | if (vs->vd->num_shared > vs->vd->connections_limit) { |
2726 | 0 | vnc_disconnect_start(vs); |
2727 | 0 | return 0; |
2728 | 0 | } |
2729 | | |
2730 | 0 | assert(pixman_image_get_width(vs->vd->server) < 65536 && |
2731 | 0 | pixman_image_get_width(vs->vd->server) >= 0); |
2732 | 0 | assert(pixman_image_get_height(vs->vd->server) < 65536 && |
2733 | 0 | pixman_image_get_height(vs->vd->server) >= 0); |
2734 | 0 | vs->client_width = pixman_image_get_width(vs->vd->server); |
2735 | 0 | vs->client_height = pixman_image_get_height(vs->vd->server); |
2736 | 0 | vnc_write_u16(vs, vs->client_width); |
2737 | 0 | vnc_write_u16(vs, vs->client_height); |
2738 | |
|
2739 | 0 | pixel_format_message(vs); |
2740 | |
|
2741 | 0 | if (qemu_name) { |
2742 | 0 | size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); |
2743 | 0 | if (size > sizeof(buf)) { |
2744 | 0 | size = sizeof(buf); |
2745 | 0 | } |
2746 | 0 | } else { |
2747 | 0 | size = snprintf(buf, sizeof(buf), "QEMU"); |
2748 | 0 | } |
2749 | |
|
2750 | 0 | vnc_write_u32(vs, size); |
2751 | 0 | vnc_write(vs, buf, size); |
2752 | 0 | vnc_flush(vs); |
2753 | |
|
2754 | 0 | vnc_client_cache_auth(vs); |
2755 | 0 | vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); |
2756 | |
|
2757 | 0 | vnc_read_when(vs, protocol_client_msg, 1); |
2758 | |
|
2759 | 0 | return 0; |
2760 | 0 | } |
2761 | | |
2762 | | void start_client_init(VncState *vs) |
2763 | 0 | { |
2764 | 0 | vnc_read_when(vs, protocol_client_init, 1); |
2765 | 0 | } |
2766 | | |
2767 | | static void authentication_failed(VncState *vs) |
2768 | 0 | { |
2769 | 0 | vnc_write_u32(vs, 1); /* Reject auth */ |
2770 | 0 | if (vs->minor >= 8) { |
2771 | 0 | static const char err[] = "Authentication failed"; |
2772 | 0 | vnc_write_u32(vs, sizeof(err)); |
2773 | 0 | vnc_write(vs, err, sizeof(err)); |
2774 | 0 | } |
2775 | 0 | vnc_flush(vs); |
2776 | 0 | vnc_client_error(vs); |
2777 | 0 | } |
2778 | | |
2779 | | static void |
2780 | | vnc_munge_des_rfb_key(unsigned char *key, size_t nkey) |
2781 | 0 | { |
2782 | 0 | size_t i; |
2783 | 0 | for (i = 0; i < nkey; i++) { |
2784 | 0 | uint8_t r = key[i]; |
2785 | 0 | r = (r & 0xf0) >> 4 | (r & 0x0f) << 4; |
2786 | 0 | r = (r & 0xcc) >> 2 | (r & 0x33) << 2; |
2787 | 0 | r = (r & 0xaa) >> 1 | (r & 0x55) << 1; |
2788 | 0 | key[i] = r; |
2789 | 0 | } |
2790 | 0 | } |
2791 | | |
2792 | | static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) |
2793 | 0 | { |
2794 | 0 | unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; |
2795 | 0 | size_t i, pwlen; |
2796 | 0 | unsigned char key[8]; |
2797 | 0 | time_t now = time(NULL); |
2798 | 0 | QCryptoCipher *cipher = NULL; |
2799 | 0 | Error *err = NULL; |
2800 | |
|
2801 | 0 | if (!vs->vd->password) { |
2802 | 0 | trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); |
2803 | 0 | goto reject; |
2804 | 0 | } |
2805 | 0 | if (vs->vd->expires < now) { |
2806 | 0 | trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); |
2807 | 0 | goto reject; |
2808 | 0 | } |
2809 | | |
2810 | 0 | memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); |
2811 | | |
2812 | | /* Calculate the expected challenge response */ |
2813 | 0 | pwlen = strlen(vs->vd->password); |
2814 | 0 | for (i=0; i<sizeof(key); i++) |
2815 | 0 | key[i] = i<pwlen ? vs->vd->password[i] : 0; |
2816 | 0 | vnc_munge_des_rfb_key(key, sizeof(key)); |
2817 | |
|
2818 | 0 | cipher = qcrypto_cipher_new( |
2819 | 0 | QCRYPTO_CIPHER_ALGO_DES, |
2820 | 0 | QCRYPTO_CIPHER_MODE_ECB, |
2821 | 0 | key, G_N_ELEMENTS(key), |
2822 | 0 | &err); |
2823 | 0 | if (!cipher) { |
2824 | 0 | trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", |
2825 | 0 | error_get_pretty(err)); |
2826 | 0 | error_free(err); |
2827 | 0 | goto reject; |
2828 | 0 | } |
2829 | | |
2830 | 0 | if (qcrypto_cipher_encrypt(cipher, |
2831 | 0 | vs->challenge, |
2832 | 0 | response, |
2833 | 0 | VNC_AUTH_CHALLENGE_SIZE, |
2834 | 0 | &err) < 0) { |
2835 | 0 | trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", |
2836 | 0 | error_get_pretty(err)); |
2837 | 0 | error_free(err); |
2838 | 0 | goto reject; |
2839 | 0 | } |
2840 | | |
2841 | | /* Compare expected vs actual challenge response */ |
2842 | 0 | if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { |
2843 | 0 | trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); |
2844 | 0 | goto reject; |
2845 | 0 | } else { |
2846 | 0 | trace_vnc_auth_pass(vs, vs->auth); |
2847 | 0 | vnc_write_u32(vs, 0); /* Accept auth */ |
2848 | 0 | vnc_flush(vs); |
2849 | |
|
2850 | 0 | start_client_init(vs); |
2851 | 0 | } |
2852 | | |
2853 | 0 | qcrypto_cipher_free(cipher); |
2854 | 0 | return 0; |
2855 | | |
2856 | 0 | reject: |
2857 | 0 | authentication_failed(vs); |
2858 | 0 | qcrypto_cipher_free(cipher); |
2859 | 0 | return 0; |
2860 | 0 | } |
2861 | | |
2862 | | void start_auth_vnc(VncState *vs) |
2863 | 0 | { |
2864 | 0 | Error *err = NULL; |
2865 | |
|
2866 | 0 | if (qcrypto_random_bytes(vs->challenge, sizeof(vs->challenge), &err)) { |
2867 | 0 | trace_vnc_auth_fail(vs, vs->auth, "cannot get random bytes", |
2868 | 0 | error_get_pretty(err)); |
2869 | 0 | error_free(err); |
2870 | 0 | authentication_failed(vs); |
2871 | 0 | return; |
2872 | 0 | } |
2873 | | |
2874 | | /* Send client a 'random' challenge */ |
2875 | 0 | vnc_write(vs, vs->challenge, sizeof(vs->challenge)); |
2876 | 0 | vnc_flush(vs); |
2877 | |
|
2878 | 0 | vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); |
2879 | 0 | } |
2880 | | |
2881 | | |
2882 | | static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) |
2883 | 0 | { |
2884 | | /* We only advertise 1 auth scheme at a time, so client |
2885 | | * must pick the one we sent. Verify this */ |
2886 | 0 | if (data[0] != vs->auth) { /* Reject auth */ |
2887 | 0 | trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); |
2888 | 0 | authentication_failed(vs); |
2889 | 0 | } else { /* Accept requested auth */ |
2890 | 0 | trace_vnc_auth_start(vs, vs->auth); |
2891 | 0 | switch (vs->auth) { |
2892 | 0 | case VNC_AUTH_NONE: |
2893 | 0 | if (vs->minor >= 8) { |
2894 | 0 | vnc_write_u32(vs, 0); /* Accept auth completion */ |
2895 | 0 | vnc_flush(vs); |
2896 | 0 | } |
2897 | 0 | trace_vnc_auth_pass(vs, vs->auth); |
2898 | 0 | start_client_init(vs); |
2899 | 0 | break; |
2900 | | |
2901 | 0 | case VNC_AUTH_VNC: |
2902 | 0 | start_auth_vnc(vs); |
2903 | 0 | break; |
2904 | | |
2905 | 0 | case VNC_AUTH_VENCRYPT: |
2906 | 0 | start_auth_vencrypt(vs); |
2907 | 0 | break; |
2908 | | |
2909 | | #ifdef CONFIG_VNC_SASL |
2910 | | case VNC_AUTH_SASL: |
2911 | | start_auth_sasl(vs); |
2912 | | break; |
2913 | | #endif /* CONFIG_VNC_SASL */ |
2914 | | |
2915 | 0 | default: /* Should not be possible, but just in case */ |
2916 | 0 | trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); |
2917 | 0 | authentication_failed(vs); |
2918 | 0 | } |
2919 | 0 | } |
2920 | 0 | return 0; |
2921 | 0 | } |
2922 | | |
2923 | | static int protocol_version(VncState *vs, uint8_t *version, size_t len) |
2924 | 0 | { |
2925 | 0 | char local[13]; |
2926 | |
|
2927 | 0 | memcpy(local, version, 12); |
2928 | 0 | local[12] = 0; |
2929 | |
|
2930 | 0 | if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { |
2931 | 0 | trace_vnc_client_protocol_version_malformed(vs, local); |
2932 | 0 | vnc_client_error(vs); |
2933 | 0 | return 0; |
2934 | 0 | } |
2935 | 0 | trace_vnc_client_protocol_version(vs, vs->major, vs->minor); |
2936 | 0 | if (vs->major != 3 || |
2937 | 0 | (vs->minor != 3 && |
2938 | 0 | vs->minor != 4 && |
2939 | 0 | vs->minor != 5 && |
2940 | 0 | vs->minor != 7 && |
2941 | 0 | vs->minor != 8)) { |
2942 | 0 | trace_vnc_client_protocol_version_unsupported(vs); |
2943 | 0 | vnc_write_u32(vs, VNC_AUTH_INVALID); |
2944 | 0 | vnc_flush(vs); |
2945 | 0 | vnc_client_error(vs); |
2946 | 0 | return 0; |
2947 | 0 | } |
2948 | | /* Some broken clients report v3.4 or v3.5, which spec requires to be treated |
2949 | | * as equivalent to v3.3 by servers |
2950 | | */ |
2951 | 0 | if (vs->minor == 4 || vs->minor == 5) |
2952 | 0 | vs->minor = 3; |
2953 | |
|
2954 | 0 | if (vs->minor == 3) { |
2955 | 0 | trace_vnc_auth_start(vs, vs->auth); |
2956 | 0 | if (vs->auth == VNC_AUTH_NONE) { |
2957 | 0 | vnc_write_u32(vs, vs->auth); |
2958 | 0 | vnc_flush(vs); |
2959 | 0 | trace_vnc_auth_pass(vs, vs->auth); |
2960 | 0 | start_client_init(vs); |
2961 | 0 | } else if (vs->auth == VNC_AUTH_VNC) { |
2962 | 0 | trace_vnc_client_auth_method(vs, vs->auth); |
2963 | 0 | vnc_write_u32(vs, vs->auth); |
2964 | 0 | vnc_flush(vs); |
2965 | 0 | start_auth_vnc(vs); |
2966 | 0 | } else { |
2967 | 0 | trace_vnc_auth_fail(vs, vs->auth, |
2968 | 0 | "Unsupported auth method for v3.3", ""); |
2969 | 0 | vnc_write_u32(vs, VNC_AUTH_INVALID); |
2970 | 0 | vnc_flush(vs); |
2971 | 0 | vnc_client_error(vs); |
2972 | 0 | } |
2973 | 0 | } else { |
2974 | 0 | vnc_write_u8(vs, 1); /* num auth */ |
2975 | 0 | vnc_write_u8(vs, vs->auth); |
2976 | 0 | vnc_read_when(vs, protocol_client_auth, 1); |
2977 | 0 | vnc_flush(vs); |
2978 | 0 | } |
2979 | |
|
2980 | 0 | return 0; |
2981 | 0 | } |
2982 | | |
2983 | | static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) |
2984 | 0 | { |
2985 | 0 | struct VncSurface *vs = &vd->guest; |
2986 | |
|
2987 | 0 | return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; |
2988 | 0 | } |
2989 | | |
2990 | | void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h) |
2991 | 0 | { |
2992 | 0 | int i, j; |
2993 | |
|
2994 | 0 | w = DIV_ROUND_UP((x + w), VNC_STAT_RECT); |
2995 | 0 | h = DIV_ROUND_UP((y + h), VNC_STAT_RECT); |
2996 | 0 | assert(h <= VNC_STAT_ROWS); |
2997 | 0 | assert(w <= VNC_STAT_COLS); |
2998 | 0 | x /= VNC_STAT_RECT; |
2999 | 0 | y /= VNC_STAT_RECT; |
3000 | |
|
3001 | 0 | for (j = y; j < h; j++) { |
3002 | 0 | for (i = x; i < w; i++) { |
3003 | 0 | worker->lossy_rect[j][i] = 1; |
3004 | 0 | } |
3005 | 0 | } |
3006 | 0 | } |
3007 | | |
3008 | | static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y, |
3009 | | int height) |
3010 | 0 | { |
3011 | 0 | VncState *vs; |
3012 | 0 | int sty = y / VNC_STAT_RECT; |
3013 | 0 | int stx = x / VNC_STAT_RECT; |
3014 | 0 | int has_dirty = 0; |
3015 | 0 | int rows; |
3016 | |
|
3017 | 0 | y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); |
3018 | 0 | x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); |
3019 | |
|
3020 | 0 | rows = MIN(VNC_STAT_RECT, height - y); |
3021 | 0 | if (rows <= 0) { |
3022 | 0 | return 0; |
3023 | 0 | } |
3024 | | |
3025 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
3026 | 0 | VncConnection *vc = container_of(vs, VncConnection, vs); |
3027 | 0 | int j; |
3028 | | |
3029 | | /* kernel send buffers are full -> refresh later */ |
3030 | 0 | if (vs->output.offset) { |
3031 | 0 | continue; |
3032 | 0 | } |
3033 | | |
3034 | 0 | if (!vc->worker.lossy_rect[sty][stx]) { |
3035 | 0 | continue; |
3036 | 0 | } |
3037 | | |
3038 | 0 | vc->worker.lossy_rect[sty][stx] = 0; |
3039 | 0 | for (j = 0; j < rows; ++j) { |
3040 | 0 | bitmap_set(vs->dirty[y + j], |
3041 | 0 | x / VNC_DIRTY_PIXELS_PER_BIT, |
3042 | 0 | VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); |
3043 | 0 | } |
3044 | 0 | has_dirty++; |
3045 | 0 | } |
3046 | |
|
3047 | 0 | return has_dirty; |
3048 | 0 | } |
3049 | | |
3050 | | static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) |
3051 | 0 | { |
3052 | 0 | int width = MIN(pixman_image_get_width(vd->guest.fb), |
3053 | 0 | pixman_image_get_width(vd->server)); |
3054 | 0 | int height = MIN(pixman_image_get_height(vd->guest.fb), |
3055 | 0 | pixman_image_get_height(vd->server)); |
3056 | 0 | int x, y; |
3057 | 0 | struct timeval res; |
3058 | 0 | int has_dirty = 0; |
3059 | |
|
3060 | 0 | for (y = 0; y < height; y += VNC_STAT_RECT) { |
3061 | 0 | for (x = 0; x < width; x += VNC_STAT_RECT) { |
3062 | 0 | VncRectStat *rect = vnc_stat_rect(vd, x, y); |
3063 | |
|
3064 | 0 | rect->updated = false; |
3065 | 0 | } |
3066 | 0 | } |
3067 | |
|
3068 | 0 | qemu_timersub(tv, &VNC_REFRESH_STATS, &res); |
3069 | |
|
3070 | 0 | if (timercmp(&vd->guest.last_freq_check, &res, >)) { |
3071 | 0 | return has_dirty; |
3072 | 0 | } |
3073 | 0 | vd->guest.last_freq_check = *tv; |
3074 | |
|
3075 | 0 | for (y = 0; y < height; y += VNC_STAT_RECT) { |
3076 | 0 | for (x = 0; x < width; x += VNC_STAT_RECT) { |
3077 | 0 | VncRectStat *rect= vnc_stat_rect(vd, x, y); |
3078 | 0 | int count = ARRAY_SIZE(rect->times); |
3079 | 0 | struct timeval min, max; |
3080 | |
|
3081 | 0 | if (!timerisset(&rect->times[count - 1])) { |
3082 | 0 | continue ; |
3083 | 0 | } |
3084 | | |
3085 | 0 | max = rect->times[(rect->idx + count - 1) % count]; |
3086 | 0 | qemu_timersub(tv, &max, &res); |
3087 | |
|
3088 | 0 | if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { |
3089 | 0 | rect->freq = 0; |
3090 | 0 | has_dirty += vnc_refresh_lossy_rect(vd, x, y, height); |
3091 | 0 | memset(rect->times, 0, sizeof (rect->times)); |
3092 | 0 | continue ; |
3093 | 0 | } |
3094 | | |
3095 | 0 | min = rect->times[rect->idx]; |
3096 | 0 | max = rect->times[(rect->idx + count - 1) % count]; |
3097 | 0 | qemu_timersub(&max, &min, &res); |
3098 | |
|
3099 | 0 | rect->freq = res.tv_sec + res.tv_usec / 1000000.; |
3100 | 0 | rect->freq /= count; |
3101 | 0 | rect->freq = 1. / rect->freq; |
3102 | 0 | } |
3103 | 0 | } |
3104 | 0 | return has_dirty; |
3105 | 0 | } |
3106 | | |
3107 | | double vnc_update_freq(VncState *vs, int x, int y, int w, int h) |
3108 | 0 | { |
3109 | 0 | int i, j; |
3110 | 0 | double total = 0; |
3111 | 0 | int num = 0; |
3112 | 0 | int x_end = x + w; |
3113 | 0 | int y_end = y + h; |
3114 | |
|
3115 | 0 | x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); |
3116 | 0 | y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); |
3117 | |
|
3118 | 0 | for (j = y; j < y_end; j += VNC_STAT_RECT) { |
3119 | 0 | for (i = x; i < x_end; i += VNC_STAT_RECT) { |
3120 | 0 | total += vnc_stat_rect(vs->vd, i, j)->freq; |
3121 | 0 | num++; |
3122 | 0 | } |
3123 | 0 | } |
3124 | |
|
3125 | 0 | if (num) { |
3126 | 0 | return total / num; |
3127 | 0 | } else { |
3128 | 0 | return 0; |
3129 | 0 | } |
3130 | 0 | } |
3131 | | |
3132 | | static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) |
3133 | 0 | { |
3134 | 0 | VncRectStat *rect; |
3135 | |
|
3136 | 0 | rect = vnc_stat_rect(vd, x, y); |
3137 | 0 | if (rect->updated) { |
3138 | 0 | return; |
3139 | 0 | } |
3140 | 0 | rect->times[rect->idx] = *tv; |
3141 | 0 | rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); |
3142 | 0 | rect->updated = true; |
3143 | 0 | } |
3144 | | |
3145 | | static int vnc_refresh_server_surface(VncDisplay *vd) |
3146 | 0 | { |
3147 | 0 | int width = MIN(pixman_image_get_width(vd->guest.fb), |
3148 | 0 | pixman_image_get_width(vd->server)); |
3149 | 0 | int height = MIN(pixman_image_get_height(vd->guest.fb), |
3150 | 0 | pixman_image_get_height(vd->server)); |
3151 | 0 | int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; |
3152 | 0 | uint8_t *guest_row0 = NULL, *server_row0; |
3153 | 0 | VncState *vs; |
3154 | 0 | int has_dirty = 0; |
3155 | 0 | pixman_image_t *tmpbuf = NULL; |
3156 | 0 | unsigned long offset; |
3157 | 0 | int x; |
3158 | 0 | uint8_t *guest_ptr, *server_ptr; |
3159 | |
|
3160 | 0 | struct timeval tv = { 0, 0 }; |
3161 | |
|
3162 | 0 | if (!vd->non_adaptive) { |
3163 | 0 | gettimeofday(&tv, NULL); |
3164 | 0 | has_dirty = vnc_update_stats(vd, &tv); |
3165 | 0 | } |
3166 | |
|
3167 | 0 | offset = find_next_bit((unsigned long *) &vd->guest.dirty, |
3168 | 0 | height * VNC_DIRTY_BPL(&vd->guest), 0); |
3169 | 0 | if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { |
3170 | | /* no dirty bits in guest surface */ |
3171 | 0 | return has_dirty; |
3172 | 0 | } |
3173 | | |
3174 | | /* |
3175 | | * Walk through the guest dirty map. |
3176 | | * Check and copy modified bits from guest to server surface. |
3177 | | * Update server dirty map. |
3178 | | */ |
3179 | 0 | server_row0 = (uint8_t *)pixman_image_get_data(vd->server); |
3180 | 0 | server_stride = guest_stride = guest_ll = |
3181 | 0 | pixman_image_get_stride(vd->server); |
3182 | 0 | cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, |
3183 | 0 | server_stride); |
3184 | 0 | if (vd->guest.format != VNC_SERVER_FB_FORMAT) { |
3185 | 0 | int w = pixman_image_get_width(vd->server); |
3186 | 0 | tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, w); |
3187 | 0 | } else { |
3188 | 0 | int guest_bpp = |
3189 | 0 | PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); |
3190 | 0 | guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); |
3191 | 0 | guest_stride = pixman_image_get_stride(vd->guest.fb); |
3192 | 0 | guest_ll = pixman_image_get_width(vd->guest.fb) |
3193 | 0 | * DIV_ROUND_UP(guest_bpp, 8); |
3194 | 0 | } |
3195 | 0 | line_bytes = MIN(server_stride, guest_ll); |
3196 | |
|
3197 | 0 | for (;;) { |
3198 | 0 | y = offset / VNC_DIRTY_BPL(&vd->guest); |
3199 | 0 | x = offset % VNC_DIRTY_BPL(&vd->guest); |
3200 | |
|
3201 | 0 | server_ptr = server_row0 + y * server_stride + x * cmp_bytes; |
3202 | |
|
3203 | 0 | if (vd->guest.format != VNC_SERVER_FB_FORMAT) { |
3204 | 0 | qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); |
3205 | 0 | guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); |
3206 | 0 | } else { |
3207 | 0 | guest_ptr = guest_row0 + y * guest_stride; |
3208 | 0 | } |
3209 | 0 | guest_ptr += x * cmp_bytes; |
3210 | |
|
3211 | 0 | for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); |
3212 | 0 | x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { |
3213 | 0 | int _cmp_bytes = cmp_bytes; |
3214 | 0 | if (!test_and_clear_bit(x, vd->guest.dirty[y])) { |
3215 | 0 | continue; |
3216 | 0 | } |
3217 | 0 | if ((x + 1) * cmp_bytes > line_bytes) { |
3218 | 0 | _cmp_bytes = line_bytes - x * cmp_bytes; |
3219 | 0 | } |
3220 | 0 | assert(_cmp_bytes >= 0); |
3221 | 0 | if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { |
3222 | 0 | continue; |
3223 | 0 | } |
3224 | 0 | memcpy(server_ptr, guest_ptr, _cmp_bytes); |
3225 | 0 | if (!vd->non_adaptive) { |
3226 | 0 | vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, |
3227 | 0 | y, &tv); |
3228 | 0 | } |
3229 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
3230 | 0 | set_bit(x, vs->dirty[y]); |
3231 | 0 | } |
3232 | 0 | has_dirty++; |
3233 | 0 | } |
3234 | | |
3235 | 0 | y++; |
3236 | 0 | offset = find_next_bit((unsigned long *) &vd->guest.dirty, |
3237 | 0 | height * VNC_DIRTY_BPL(&vd->guest), |
3238 | 0 | y * VNC_DIRTY_BPL(&vd->guest)); |
3239 | 0 | if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { |
3240 | | /* no more dirty bits */ |
3241 | 0 | break; |
3242 | 0 | } |
3243 | 0 | } |
3244 | 0 | qemu_pixman_image_unref(tmpbuf); |
3245 | 0 | return has_dirty; |
3246 | 0 | } |
3247 | | |
3248 | | static void vnc_refresh(DisplayChangeListener *dcl) |
3249 | 0 | { |
3250 | 0 | VncDisplay *vd = container_of(dcl, VncDisplay, dcl); |
3251 | 0 | VncState *vs, *vn; |
3252 | 0 | int has_dirty, rects = 0; |
3253 | |
|
3254 | 0 | if (QTAILQ_EMPTY(&vd->clients)) { |
3255 | 0 | qemu_console_listener_set_refresh(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); |
3256 | 0 | return; |
3257 | 0 | } |
3258 | | |
3259 | 0 | qemu_console_hw_update(vd->dcl.con); |
3260 | |
|
3261 | 0 | if (vnc_trylock_display(vd)) { |
3262 | 0 | qemu_console_listener_set_refresh(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); |
3263 | 0 | return; |
3264 | 0 | } |
3265 | | |
3266 | 0 | has_dirty = vnc_refresh_server_surface(vd); |
3267 | 0 | vnc_unlock_display(vd); |
3268 | |
|
3269 | 0 | QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { |
3270 | 0 | rects += vnc_update_client(vs, has_dirty); |
3271 | | /* vs might be free()ed here */ |
3272 | 0 | } |
3273 | |
|
3274 | 0 | if (has_dirty && rects) { |
3275 | 0 | vd->dcl.update_interval /= 2; |
3276 | 0 | if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { |
3277 | 0 | vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; |
3278 | 0 | } |
3279 | 0 | } else { |
3280 | 0 | vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; |
3281 | 0 | if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { |
3282 | 0 | vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; |
3283 | 0 | } |
3284 | 0 | } |
3285 | 0 | } |
3286 | | |
3287 | | static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, |
3288 | | bool skipauth, bool websocket) |
3289 | 0 | { |
3290 | 0 | VncConnection *vc = g_new0(VncConnection, 1); |
3291 | 0 | VncState *vs = &vc->vs; |
3292 | 0 | bool first_client = QTAILQ_EMPTY(&vd->clients); |
3293 | |
|
3294 | 0 | trace_vnc_client_connect(vs, sioc); |
3295 | 0 | vs->magic = VNC_MAGIC; |
3296 | 0 | vs->sioc = sioc; |
3297 | 0 | object_ref(OBJECT(vs->sioc)); |
3298 | 0 | vs->ioc = QIO_CHANNEL(sioc); |
3299 | 0 | object_ref(OBJECT(vs->ioc)); |
3300 | 0 | vs->vd = vd; |
3301 | |
|
3302 | 0 | buffer_init(&vs->input, "vnc-input/%p", sioc); |
3303 | 0 | buffer_init(&vs->output, "vnc-output/%p", sioc); |
3304 | 0 | buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); |
3305 | |
|
3306 | 0 | buffer_init(&vc->worker.tight.tight, "vnc-tight/%p", sioc); |
3307 | 0 | buffer_init(&vc->worker.tight.zlib, "vnc-tight-zlib/%p", sioc); |
3308 | 0 | buffer_init(&vc->worker.tight.gradient, "vnc-tight-gradient/%p", sioc); |
3309 | | #ifdef CONFIG_VNC_JPEG |
3310 | | buffer_init(&vc->worker.tight.jpeg, "vnc-tight-jpeg/%p", sioc); |
3311 | | #endif |
3312 | | #ifdef CONFIG_PNG |
3313 | | buffer_init(&vc->worker.tight.png, "vnc-tight-png/%p", sioc); |
3314 | | #endif |
3315 | 0 | buffer_init(&vc->worker.zlib.zlib, "vnc-zlib/%p", sioc); |
3316 | 0 | buffer_init(&vc->worker.zrle.zrle, "vnc-zrle/%p", sioc); |
3317 | 0 | buffer_init(&vc->worker.zrle.fb, "vnc-zrle-fb/%p", sioc); |
3318 | 0 | buffer_init(&vc->worker.zrle.zlib, "vnc-zrle-zlib/%p", sioc); |
3319 | |
|
3320 | 0 | if (skipauth) { |
3321 | 0 | vs->auth = VNC_AUTH_NONE; |
3322 | 0 | vs->subauth = VNC_AUTH_INVALID; |
3323 | 0 | } else { |
3324 | 0 | if (websocket) { |
3325 | 0 | vs->auth = vd->ws_auth; |
3326 | 0 | vs->subauth = VNC_AUTH_INVALID; |
3327 | 0 | } else { |
3328 | 0 | vs->auth = vd->auth; |
3329 | 0 | vs->subauth = vd->subauth; |
3330 | 0 | } |
3331 | 0 | } |
3332 | 0 | trace_vnc_client_setup(vs, sioc, websocket, vs->auth, vs->subauth); |
3333 | 0 | qemu_console_listener_set_refresh(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); |
3334 | 0 | qio_channel_set_blocking(vs->ioc, false, &error_abort); |
3335 | 0 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
3336 | 0 | if (websocket) { |
3337 | 0 | vs->websocket = 1; |
3338 | 0 | if (vd->tlscreds) { |
3339 | 0 | vs->ioc_tag = qio_channel_add_watch( |
3340 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
3341 | 0 | vncws_tls_handshake_io, vs, NULL); |
3342 | 0 | } else { |
3343 | 0 | vs->ioc_tag = qio_channel_add_watch( |
3344 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
3345 | 0 | vncws_handshake_io, vs, NULL); |
3346 | 0 | } |
3347 | 0 | } else { |
3348 | 0 | vs->ioc_tag = qio_channel_add_watch( |
3349 | 0 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
3350 | 0 | vnc_client_io, vs, NULL); |
3351 | 0 | } |
3352 | |
|
3353 | 0 | vnc_client_cache_addr(vs); |
3354 | 0 | vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); |
3355 | 0 | vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); |
3356 | |
|
3357 | 0 | vs->last_x = -1; |
3358 | 0 | vs->last_y = -1; |
3359 | |
|
3360 | 0 | vs->as.freq = 44100; |
3361 | 0 | vs->as.nchannels = 2; |
3362 | 0 | vs->as.fmt = AUDIO_FORMAT_S16; |
3363 | 0 | vs->as.big_endian = false; |
3364 | |
|
3365 | 0 | qemu_mutex_init(&vs->output_mutex); |
3366 | 0 | vs->bh = qemu_bh_new(vnc_jobs_bh, vs); |
3367 | |
|
3368 | 0 | QTAILQ_INSERT_TAIL(&vd->clients, vs, next); |
3369 | 0 | if (first_client) { |
3370 | 0 | vnc_update_server_surface(vd); |
3371 | 0 | } |
3372 | |
|
3373 | 0 | qemu_console_hw_update(vd->dcl.con); |
3374 | |
|
3375 | 0 | if (!vs->websocket) { |
3376 | 0 | vnc_start_protocol(vs); |
3377 | 0 | } |
3378 | |
|
3379 | 0 | if (vd->num_connecting > vd->connections_limit) { |
3380 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
3381 | 0 | if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { |
3382 | 0 | vnc_disconnect_start(vs); |
3383 | 0 | return; |
3384 | 0 | } |
3385 | 0 | } |
3386 | 0 | } |
3387 | 0 | } |
3388 | | |
3389 | | void vnc_start_protocol(VncState *vs) |
3390 | 0 | { |
3391 | 0 | vnc_write(vs, "RFB 003.008\n", 12); |
3392 | 0 | vnc_flush(vs); |
3393 | 0 | vnc_read_when(vs, protocol_version, 12); |
3394 | |
|
3395 | 0 | vs->mouse_mode_notifier.notify = check_pointer_type_change; |
3396 | 0 | qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); |
3397 | 0 | } |
3398 | | |
3399 | | static void vnc_listen_io(QIONetListener *listener, |
3400 | | QIOChannelSocket *cioc, |
3401 | | void *opaque) |
3402 | 0 | { |
3403 | 0 | VncDisplay *vd = opaque; |
3404 | 0 | bool isWebsock = listener == vd->wslistener; |
3405 | |
|
3406 | 0 | qio_channel_set_name(QIO_CHANNEL(cioc), |
3407 | 0 | isWebsock ? "vnc-ws-server" : "vnc-server"); |
3408 | 0 | qio_channel_set_delay(QIO_CHANNEL(cioc), false); |
3409 | 0 | vnc_connect(vd, cioc, false, isWebsock); |
3410 | 0 | } |
3411 | | |
3412 | | static const DisplayChangeListenerOps dcl_ops = { |
3413 | | .dpy_name = "vnc", |
3414 | | .dpy_refresh = vnc_refresh, |
3415 | | .dpy_gfx_update = vnc_dpy_update, |
3416 | | .dpy_gfx_switch = vnc_dpy_switch, |
3417 | | .dpy_gfx_check_format = qemu_pixman_check_format, |
3418 | | .dpy_mouse_set = vnc_mouse_set, |
3419 | | .dpy_cursor_define = vnc_dpy_cursor_define, |
3420 | | }; |
3421 | | |
3422 | | static void vmstate_change_handler(void *opaque, bool running, RunState state) |
3423 | 0 | { |
3424 | 0 | VncDisplay *vd = opaque; |
3425 | |
|
3426 | 0 | if (state != RUN_STATE_RUNNING) { |
3427 | 0 | return; |
3428 | 0 | } |
3429 | 0 | qemu_console_listener_set_refresh(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); |
3430 | 0 | } |
3431 | | |
3432 | | static bool vnc_display_open(VncDisplay *vd, Error **errp); |
3433 | | |
3434 | | VncDisplay *vnc_display_new(const char *id, Error **errp) |
3435 | 0 | { |
3436 | 0 | VncDisplay *vd; |
3437 | |
|
3438 | 0 | assert(!vnc_display_find(id)); |
3439 | |
|
3440 | 0 | vd = g_new0(VncDisplay, 1); |
3441 | 0 | qemu_mutex_init(&vd->mutex); |
3442 | 0 | vd->id = g_strdup(id); |
3443 | |
|
3444 | 0 | QTAILQ_INIT(&vd->clients); |
3445 | 0 | vd->expires = TIME_MAX; |
3446 | |
|
3447 | 0 | if (keyboard_layout) { |
3448 | 0 | trace_vnc_key_map_init(keyboard_layout); |
3449 | 0 | vd->kbd_layout = kbd_layout_new(name2keysym, |
3450 | 0 | keyboard_layout, errp); |
3451 | 0 | } else { |
3452 | 0 | vd->kbd_layout = kbd_layout_new(name2keysym, "en-us", errp); |
3453 | 0 | } |
3454 | |
|
3455 | 0 | if (!vd->kbd_layout) { |
3456 | 0 | vnc_display_free(vd); |
3457 | 0 | return NULL; |
3458 | 0 | } |
3459 | | |
3460 | 0 | vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; |
3461 | 0 | vd->connections_limit = 32; |
3462 | |
|
3463 | 0 | vnc_start_worker_thread(vd); |
3464 | |
|
3465 | 0 | vd->vmstate_handler_entry = qemu_add_vm_change_state_handler( |
3466 | 0 | &vmstate_change_handler, vd); |
3467 | |
|
3468 | 0 | if (!vnc_display_open(vd, errp)) { |
3469 | 0 | vnc_display_free(vd); |
3470 | 0 | return NULL; |
3471 | 0 | } |
3472 | | |
3473 | 0 | QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); |
3474 | 0 | return vd; |
3475 | 0 | } |
3476 | | |
3477 | | static void vnc_display_close(VncDisplay *vd) |
3478 | 0 | { |
3479 | 0 | VncState *vs; |
3480 | |
|
3481 | 0 | assert(vd); |
3482 | |
|
3483 | 0 | QTAILQ_FOREACH(vs, &vd->clients, next) { |
3484 | 0 | vnc_disconnect_start(vs); |
3485 | 0 | } |
3486 | 0 | if (vd->listener) { |
3487 | 0 | qio_net_listener_disconnect(vd->listener); |
3488 | 0 | object_unref(OBJECT(vd->listener)); |
3489 | 0 | } |
3490 | 0 | vd->listener = NULL; |
3491 | |
|
3492 | 0 | if (vd->wslistener) { |
3493 | 0 | qio_net_listener_disconnect(vd->wslistener); |
3494 | 0 | object_unref(OBJECT(vd->wslistener)); |
3495 | 0 | } |
3496 | 0 | vd->wslistener = NULL; |
3497 | |
|
3498 | 0 | vd->auth = VNC_AUTH_INVALID; |
3499 | 0 | vd->subauth = VNC_AUTH_INVALID; |
3500 | 0 | if (vd->tlscreds) { |
3501 | 0 | object_unref(OBJECT(vd->tlscreds)); |
3502 | 0 | vd->tlscreds = NULL; |
3503 | 0 | } |
3504 | 0 | g_free(vd->tlsauthzid); |
3505 | 0 | vd->tlsauthzid = NULL; |
3506 | 0 | if (vd->lock_key_sync) { |
3507 | 0 | qemu_input_led_notifier_remove(&vd->led_notifier); |
3508 | 0 | } |
3509 | | #ifdef CONFIG_VNC_SASL |
3510 | | if (vd->sasl.authz) { |
3511 | | object_unparent(OBJECT(vd->sasl.authz)); |
3512 | | vd->sasl.authz = NULL; |
3513 | | } |
3514 | | g_free(vd->sasl.authzid); |
3515 | | vd->sasl.authzid = NULL; |
3516 | | #endif |
3517 | 0 | } |
3518 | | |
3519 | | void vnc_display_free(VncDisplay *vd) |
3520 | 0 | { |
3521 | 0 | if (!vd) { |
3522 | 0 | return; |
3523 | 0 | } |
3524 | | |
3525 | 0 | vnc_display_close(vd); |
3526 | 0 | while (!QTAILQ_EMPTY(&vd->clients)) { |
3527 | 0 | main_loop_wait(false); |
3528 | 0 | } |
3529 | |
|
3530 | 0 | vnc_stop_worker_thread(vd); |
3531 | 0 | qemu_console_unregister_listener(&vd->dcl); |
3532 | 0 | qkbd_state_free(vd->kbd); |
3533 | 0 | qemu_del_vm_change_state_handler(vd->vmstate_handler_entry); |
3534 | 0 | kbd_layout_free(vd->kbd_layout); |
3535 | 0 | qemu_mutex_destroy(&vd->mutex); |
3536 | 0 | if (QTAILQ_IN_USE(vd, next)) { |
3537 | 0 | QTAILQ_REMOVE(&vnc_displays, vd, next); |
3538 | 0 | } |
3539 | 0 | g_free(vd->id); |
3540 | 0 | g_free(vd); |
3541 | 0 | } |
3542 | | |
3543 | | int vnc_display_password(const char *id, const char *password, Error **errp) |
3544 | 0 | { |
3545 | 0 | VncDisplay *vd = vnc_display_find(id); |
3546 | |
|
3547 | 0 | if (!vd) { |
3548 | 0 | error_setg(errp, "No VNC display is present"); |
3549 | 0 | error_append_hint(errp, |
3550 | 0 | "To enable it, use '-vnc ...'"); |
3551 | 0 | return -EINVAL; |
3552 | 0 | } |
3553 | 0 | if (vd->auth == VNC_AUTH_NONE) { |
3554 | 0 | error_setg(errp, "VNC password authentication is disabled"); |
3555 | 0 | error_append_hint(errp, |
3556 | 0 | "To enable it, use '-vnc ...,password-secret=ID'"); |
3557 | 0 | return -EINVAL; |
3558 | 0 | } |
3559 | | |
3560 | 0 | g_free(vd->password); |
3561 | 0 | vd->password = g_strdup(password); |
3562 | |
|
3563 | 0 | return 0; |
3564 | 0 | } |
3565 | | |
3566 | | int vnc_display_pw_expire(const char *id, time_t expires) |
3567 | 0 | { |
3568 | 0 | VncDisplay *vd = vnc_display_find(id); |
3569 | |
|
3570 | 0 | if (!vd) { |
3571 | 0 | return -EINVAL; |
3572 | 0 | } |
3573 | | |
3574 | 0 | vd->expires = expires; |
3575 | 0 | return 0; |
3576 | 0 | } |
3577 | | |
3578 | | static void vnc_display_print_local_addr(VncDisplay *vd) |
3579 | 0 | { |
3580 | 0 | SocketAddress *addr; |
3581 | |
|
3582 | 0 | if (!vd->listener || !qio_net_listener_nsioc(vd->listener)) { |
3583 | 0 | return; |
3584 | 0 | } |
3585 | | |
3586 | 0 | addr = qio_net_listener_get_local_address(vd->listener, 0, NULL); |
3587 | 0 | if (!addr) { |
3588 | 0 | return; |
3589 | 0 | } |
3590 | | |
3591 | 0 | if (addr->type != SOCKET_ADDRESS_TYPE_INET) { |
3592 | 0 | qapi_free_SocketAddress(addr); |
3593 | 0 | return; |
3594 | 0 | } |
3595 | 0 | error_printf("VNC server running on %s:%s\n", |
3596 | 0 | addr->u.inet.host, |
3597 | 0 | addr->u.inet.port); |
3598 | 0 | qapi_free_SocketAddress(addr); |
3599 | 0 | } |
3600 | | |
3601 | | static QemuOptsList qemu_vnc_opts = { |
3602 | | .name = "vnc", |
3603 | | .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), |
3604 | | .implied_opt_name = "vnc", |
3605 | | .desc = { |
3606 | | { |
3607 | | .name = "vnc", |
3608 | | .type = QEMU_OPT_STRING, |
3609 | | },{ |
3610 | | .name = "websocket", |
3611 | | .type = QEMU_OPT_STRING, |
3612 | | },{ |
3613 | | .name = "tls-creds", |
3614 | | .type = QEMU_OPT_STRING, |
3615 | | },{ |
3616 | | .name = "share", |
3617 | | .type = QEMU_OPT_STRING, |
3618 | | },{ |
3619 | | .name = "display", |
3620 | | .type = QEMU_OPT_STRING, |
3621 | | },{ |
3622 | | .name = "head", |
3623 | | .type = QEMU_OPT_NUMBER, |
3624 | | },{ |
3625 | | .name = "connections", |
3626 | | .type = QEMU_OPT_NUMBER, |
3627 | | },{ |
3628 | | .name = "to", |
3629 | | .type = QEMU_OPT_NUMBER, |
3630 | | },{ |
3631 | | .name = "ipv4", |
3632 | | .type = QEMU_OPT_BOOL, |
3633 | | },{ |
3634 | | .name = "ipv6", |
3635 | | .type = QEMU_OPT_BOOL, |
3636 | | },{ |
3637 | | .name = "password", |
3638 | | .type = QEMU_OPT_BOOL, |
3639 | | },{ |
3640 | | .name = "password-secret", |
3641 | | .type = QEMU_OPT_STRING, |
3642 | | },{ |
3643 | | .name = "reverse", |
3644 | | .type = QEMU_OPT_BOOL, |
3645 | | },{ |
3646 | | .name = "lock-key-sync", |
3647 | | .type = QEMU_OPT_BOOL, |
3648 | | },{ |
3649 | | .name = "key-delay-ms", |
3650 | | .type = QEMU_OPT_NUMBER, |
3651 | | },{ |
3652 | | .name = "sasl", |
3653 | | .type = QEMU_OPT_BOOL, |
3654 | | },{ |
3655 | | .name = "tls-authz", |
3656 | | .type = QEMU_OPT_STRING, |
3657 | | },{ |
3658 | | .name = "sasl-authz", |
3659 | | .type = QEMU_OPT_STRING, |
3660 | | },{ |
3661 | | .name = "lossy", |
3662 | | .type = QEMU_OPT_BOOL, |
3663 | | },{ |
3664 | | .name = "non-adaptive", |
3665 | | .type = QEMU_OPT_BOOL, |
3666 | | },{ |
3667 | | .name = "audiodev", |
3668 | | .type = QEMU_OPT_STRING, |
3669 | | },{ |
3670 | | .name = "power-control", |
3671 | | .type = QEMU_OPT_BOOL, |
3672 | | }, |
3673 | | { /* end of list */ } |
3674 | | }, |
3675 | | }; |
3676 | | |
3677 | | |
3678 | | static int |
3679 | | vnc_display_setup_auth(int *auth, |
3680 | | int *subauth, |
3681 | | QCryptoTLSCreds *tlscreds, |
3682 | | bool password, |
3683 | | bool sasl, |
3684 | | bool websocket, |
3685 | | Error **errp) |
3686 | 0 | { |
3687 | | /* |
3688 | | * We have a choice of 3 authentication options |
3689 | | * |
3690 | | * 1. none |
3691 | | * 2. vnc |
3692 | | * 3. sasl |
3693 | | * |
3694 | | * The channel can be run in 2 modes |
3695 | | * |
3696 | | * 1. clear |
3697 | | * 2. tls |
3698 | | * |
3699 | | * And TLS can use 2 types of credentials |
3700 | | * |
3701 | | * 1. anon |
3702 | | * 2. x509 |
3703 | | * |
3704 | | * We thus have 9 possible logical combinations |
3705 | | * |
3706 | | * 1. clear + none |
3707 | | * 2. clear + vnc |
3708 | | * 3. clear + sasl |
3709 | | * 4. tls + anon + none |
3710 | | * 5. tls + anon + vnc |
3711 | | * 6. tls + anon + sasl |
3712 | | * 7. tls + x509 + none |
3713 | | * 8. tls + x509 + vnc |
3714 | | * 9. tls + x509 + sasl |
3715 | | * |
3716 | | * These need to be mapped into the VNC auth schemes |
3717 | | * in an appropriate manner. In regular VNC, all the |
3718 | | * TLS options get mapped into VNC_AUTH_VENCRYPT |
3719 | | * sub-auth types. |
3720 | | * |
3721 | | * In websockets, the https:// protocol already provides |
3722 | | * TLS support, so there is no need to make use of the |
3723 | | * VeNCrypt extension. Furthermore, websockets browser |
3724 | | * clients could not use VeNCrypt even if they wanted to, |
3725 | | * as they cannot control when the TLS handshake takes |
3726 | | * place. Thus there is no option but to rely on https://, |
3727 | | * meaning combinations 4->6 and 7->9 will be mapped to |
3728 | | * VNC auth schemes in the same way as combos 1->3. |
3729 | | * |
3730 | | * Regardless of fact that we have a different mapping to |
3731 | | * VNC auth mechs for plain VNC vs websockets VNC, the end |
3732 | | * result has the same security characteristics. |
3733 | | */ |
3734 | 0 | if (websocket || !tlscreds) { |
3735 | 0 | if (password) { |
3736 | 0 | *auth = VNC_AUTH_VNC; |
3737 | 0 | } else if (sasl) { |
3738 | 0 | *auth = VNC_AUTH_SASL; |
3739 | 0 | } else { |
3740 | 0 | *auth = VNC_AUTH_NONE; |
3741 | 0 | } |
3742 | 0 | *subauth = VNC_AUTH_INVALID; |
3743 | 0 | } else { |
3744 | 0 | bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), |
3745 | 0 | TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; |
3746 | 0 | bool is_anon = object_dynamic_cast(OBJECT(tlscreds), |
3747 | 0 | TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; |
3748 | |
|
3749 | 0 | if (!is_x509 && !is_anon) { |
3750 | 0 | error_setg(errp, |
3751 | 0 | "Unsupported TLS cred type %s", |
3752 | 0 | object_get_typename(OBJECT(tlscreds))); |
3753 | 0 | return -1; |
3754 | 0 | } |
3755 | 0 | *auth = VNC_AUTH_VENCRYPT; |
3756 | 0 | if (password) { |
3757 | 0 | if (is_x509) { |
3758 | 0 | *subauth = VNC_AUTH_VENCRYPT_X509VNC; |
3759 | 0 | } else { |
3760 | 0 | *subauth = VNC_AUTH_VENCRYPT_TLSVNC; |
3761 | 0 | } |
3762 | 0 | } else if (sasl) { |
3763 | 0 | if (is_x509) { |
3764 | 0 | *subauth = VNC_AUTH_VENCRYPT_X509SASL; |
3765 | 0 | } else { |
3766 | 0 | *subauth = VNC_AUTH_VENCRYPT_TLSSASL; |
3767 | 0 | } |
3768 | 0 | } else { |
3769 | 0 | if (is_x509) { |
3770 | 0 | *subauth = VNC_AUTH_VENCRYPT_X509NONE; |
3771 | 0 | } else { |
3772 | 0 | *subauth = VNC_AUTH_VENCRYPT_TLSNONE; |
3773 | 0 | } |
3774 | 0 | } |
3775 | 0 | } |
3776 | 0 | return 0; |
3777 | 0 | } |
3778 | | |
3779 | | |
3780 | | static int vnc_display_get_address(const char *addrstr, |
3781 | | bool websocket, |
3782 | | bool reverse, |
3783 | | int displaynum, |
3784 | | int to, |
3785 | | bool has_ipv4, |
3786 | | bool has_ipv6, |
3787 | | bool ipv4, |
3788 | | bool ipv6, |
3789 | | SocketAddress **retaddr, |
3790 | | Error **errp) |
3791 | 0 | { |
3792 | 0 | int ret = -1; |
3793 | 0 | SocketAddress *addr = NULL; |
3794 | |
|
3795 | 0 | addr = g_new0(SocketAddress, 1); |
3796 | |
|
3797 | 0 | if (strncmp(addrstr, "unix:", 5) == 0) { |
3798 | 0 | addr->type = SOCKET_ADDRESS_TYPE_UNIX; |
3799 | 0 | addr->u.q_unix.path = g_strdup(addrstr + 5); |
3800 | |
|
3801 | 0 | if (to) { |
3802 | 0 | error_setg(errp, "Port range not support with UNIX socket"); |
3803 | 0 | goto cleanup; |
3804 | 0 | } |
3805 | 0 | ret = 0; |
3806 | 0 | } else { |
3807 | 0 | const char *port; |
3808 | 0 | size_t hostlen; |
3809 | 0 | uint64_t baseport = 0; |
3810 | 0 | InetSocketAddress *inet; |
3811 | |
|
3812 | 0 | port = strrchr(addrstr, ':'); |
3813 | 0 | if (!port) { |
3814 | 0 | if (websocket) { |
3815 | 0 | hostlen = 0; |
3816 | 0 | port = addrstr; |
3817 | 0 | } else { |
3818 | 0 | error_setg(errp, "no vnc port specified"); |
3819 | 0 | goto cleanup; |
3820 | 0 | } |
3821 | 0 | } else { |
3822 | 0 | hostlen = port - addrstr; |
3823 | 0 | port++; |
3824 | 0 | if (*port == '\0') { |
3825 | 0 | error_setg(errp, "vnc port cannot be empty"); |
3826 | 0 | goto cleanup; |
3827 | 0 | } |
3828 | 0 | } |
3829 | | |
3830 | 0 | addr->type = SOCKET_ADDRESS_TYPE_INET; |
3831 | 0 | inet = &addr->u.inet; |
3832 | 0 | if (hostlen && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { |
3833 | 0 | inet->host = g_strndup(addrstr + 1, hostlen - 2); |
3834 | 0 | } else { |
3835 | 0 | inet->host = g_strndup(addrstr, hostlen); |
3836 | 0 | } |
3837 | | /* plain VNC port is just an offset, for websocket |
3838 | | * port is absolute */ |
3839 | 0 | if (websocket) { |
3840 | 0 | if (g_str_equal(addrstr, "") || |
3841 | 0 | g_str_equal(addrstr, "on")) { |
3842 | 0 | if (displaynum == -1) { |
3843 | 0 | error_setg(errp, "explicit websocket port is required"); |
3844 | 0 | goto cleanup; |
3845 | 0 | } |
3846 | 0 | inet->port = g_strdup_printf( |
3847 | 0 | "%d", displaynum + 5700); |
3848 | 0 | if (to) { |
3849 | 0 | inet->has_to = true; |
3850 | 0 | inet->to = to + 5700; |
3851 | 0 | } |
3852 | 0 | } else { |
3853 | 0 | inet->port = g_strdup(port); |
3854 | 0 | } |
3855 | 0 | } else { |
3856 | 0 | int offset = reverse ? 0 : 5900; |
3857 | 0 | if (parse_uint_full(port, 10, &baseport) < 0) { |
3858 | 0 | error_setg(errp, "can't convert to a number: %s", port); |
3859 | 0 | goto cleanup; |
3860 | 0 | } |
3861 | 0 | if (baseport > 65535 || |
3862 | 0 | baseport + offset > 65535) { |
3863 | 0 | error_setg(errp, "port %s out of range", port); |
3864 | 0 | goto cleanup; |
3865 | 0 | } |
3866 | 0 | inet->port = g_strdup_printf( |
3867 | 0 | "%d", (int)baseport + offset); |
3868 | |
|
3869 | 0 | if (to) { |
3870 | 0 | inet->has_to = true; |
3871 | 0 | inet->to = to + offset; |
3872 | 0 | } |
3873 | 0 | } |
3874 | | |
3875 | 0 | inet->ipv4 = ipv4; |
3876 | 0 | inet->has_ipv4 = has_ipv4; |
3877 | 0 | inet->ipv6 = ipv6; |
3878 | 0 | inet->has_ipv6 = has_ipv6; |
3879 | |
|
3880 | 0 | ret = baseport; |
3881 | 0 | } |
3882 | | |
3883 | 0 | *retaddr = addr; |
3884 | |
|
3885 | 0 | cleanup: |
3886 | 0 | if (ret < 0) { |
3887 | 0 | qapi_free_SocketAddress(addr); |
3888 | 0 | } |
3889 | 0 | return ret; |
3890 | 0 | } |
3891 | | |
3892 | | static int vnc_display_get_addresses(QemuOpts *opts, |
3893 | | bool reverse, |
3894 | | SocketAddressList **saddr_list_ret, |
3895 | | SocketAddressList **wsaddr_list_ret, |
3896 | | Error **errp) |
3897 | 0 | { |
3898 | 0 | SocketAddress *saddr = NULL; |
3899 | 0 | SocketAddress *wsaddr = NULL; |
3900 | 0 | g_autoptr(SocketAddressList) saddr_list = NULL; |
3901 | 0 | SocketAddressList **saddr_tail = &saddr_list; |
3902 | 0 | SocketAddress *single_saddr = NULL; |
3903 | 0 | g_autoptr(SocketAddressList) wsaddr_list = NULL; |
3904 | 0 | SocketAddressList **wsaddr_tail = &wsaddr_list; |
3905 | 0 | QemuOptsIter addriter; |
3906 | 0 | const char *addr; |
3907 | 0 | int to = qemu_opt_get_number(opts, "to", 0); |
3908 | 0 | bool has_ipv4 = qemu_opt_get(opts, "ipv4"); |
3909 | 0 | bool has_ipv6 = qemu_opt_get(opts, "ipv6"); |
3910 | 0 | bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); |
3911 | 0 | bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); |
3912 | 0 | int displaynum = -1; |
3913 | |
|
3914 | 0 | addr = qemu_opt_get(opts, "vnc"); |
3915 | 0 | if (addr == NULL || g_str_equal(addr, "none")) { |
3916 | 0 | return 0; |
3917 | 0 | } |
3918 | 0 | if (qemu_opt_get(opts, "websocket") && |
3919 | 0 | !qcrypto_hash_supports(QCRYPTO_HASH_ALGO_SHA1)) { |
3920 | 0 | error_setg(errp, |
3921 | 0 | "SHA1 hash support is required for websockets"); |
3922 | 0 | return -1; |
3923 | 0 | } |
3924 | | |
3925 | 0 | qemu_opt_iter_init(&addriter, opts, "vnc"); |
3926 | 0 | while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { |
3927 | 0 | int rv; |
3928 | 0 | rv = vnc_display_get_address(addr, false, reverse, 0, to, |
3929 | 0 | has_ipv4, has_ipv6, |
3930 | 0 | ipv4, ipv6, |
3931 | 0 | &saddr, errp); |
3932 | 0 | if (rv < 0) { |
3933 | 0 | return -1; |
3934 | 0 | } |
3935 | | /* Historical compat - first listen address can be used |
3936 | | * to set the default websocket port |
3937 | | */ |
3938 | 0 | if (displaynum == -1) { |
3939 | 0 | displaynum = rv; |
3940 | 0 | } |
3941 | 0 | QAPI_LIST_APPEND(saddr_tail, saddr); |
3942 | 0 | } |
3943 | | |
3944 | 0 | if (saddr_list && !saddr_list->next) { |
3945 | 0 | single_saddr = saddr_list->value; |
3946 | 0 | } else { |
3947 | | /* |
3948 | | * If we had multiple primary displays, we don't do defaults |
3949 | | * for websocket, and require explicit config instead. |
3950 | | */ |
3951 | 0 | displaynum = -1; |
3952 | 0 | } |
3953 | |
|
3954 | 0 | qemu_opt_iter_init(&addriter, opts, "websocket"); |
3955 | 0 | while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { |
3956 | 0 | if (vnc_display_get_address(addr, true, reverse, displaynum, to, |
3957 | 0 | has_ipv4, has_ipv6, |
3958 | 0 | ipv4, ipv6, |
3959 | 0 | &wsaddr, errp) < 0) { |
3960 | 0 | return -1; |
3961 | 0 | } |
3962 | | |
3963 | | /* Historical compat - if only a single listen address was |
3964 | | * provided, then this is used to set the default listen |
3965 | | * address for websocket too |
3966 | | */ |
3967 | 0 | if (single_saddr && |
3968 | 0 | single_saddr->type == SOCKET_ADDRESS_TYPE_INET && |
3969 | 0 | wsaddr->type == SOCKET_ADDRESS_TYPE_INET && |
3970 | 0 | g_str_equal(wsaddr->u.inet.host, "") && |
3971 | 0 | !g_str_equal(single_saddr->u.inet.host, "")) { |
3972 | 0 | g_free(wsaddr->u.inet.host); |
3973 | 0 | wsaddr->u.inet.host = g_strdup(single_saddr->u.inet.host); |
3974 | 0 | } |
3975 | |
|
3976 | 0 | QAPI_LIST_APPEND(wsaddr_tail, wsaddr); |
3977 | 0 | } |
3978 | | |
3979 | 0 | *saddr_list_ret = g_steal_pointer(&saddr_list); |
3980 | 0 | *wsaddr_list_ret = g_steal_pointer(&wsaddr_list); |
3981 | 0 | return 0; |
3982 | 0 | } |
3983 | | |
3984 | | static int vnc_display_connect(VncDisplay *vd, |
3985 | | SocketAddressList *saddr_list, |
3986 | | SocketAddressList *wsaddr_list, |
3987 | | Error **errp) |
3988 | 0 | { |
3989 | | /* connect to viewer */ |
3990 | 0 | QIOChannelSocket *sioc = NULL; |
3991 | 0 | if (wsaddr_list) { |
3992 | 0 | error_setg(errp, "Cannot use websockets in reverse mode"); |
3993 | 0 | return -1; |
3994 | 0 | } |
3995 | 0 | if (!saddr_list || saddr_list->next) { |
3996 | 0 | error_setg(errp, "Expected a single address in reverse mode"); |
3997 | 0 | return -1; |
3998 | 0 | } |
3999 | 0 | sioc = qio_channel_socket_new(); |
4000 | 0 | qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); |
4001 | 0 | if (qio_channel_socket_connect_sync(sioc, saddr_list->value, errp) < 0) { |
4002 | 0 | object_unref(OBJECT(sioc)); |
4003 | 0 | return -1; |
4004 | 0 | } |
4005 | 0 | vnc_connect(vd, sioc, false, false); |
4006 | 0 | object_unref(OBJECT(sioc)); |
4007 | 0 | return 0; |
4008 | 0 | } |
4009 | | |
4010 | | |
4011 | | static int vnc_display_listen(VncDisplay *vd, |
4012 | | SocketAddressList *saddr_list, |
4013 | | SocketAddressList *wsaddr_list, |
4014 | | Error **errp) |
4015 | 0 | { |
4016 | 0 | SocketAddressList *el; |
4017 | |
|
4018 | 0 | if (saddr_list) { |
4019 | 0 | vd->listener = qio_net_listener_new(); |
4020 | 0 | qio_net_listener_set_name(vd->listener, "vnc-listen"); |
4021 | 0 | for (el = saddr_list; el; el = el->next) { |
4022 | 0 | if (qio_net_listener_open_sync(vd->listener, |
4023 | 0 | el->value, 1, |
4024 | 0 | errp) < 0) { |
4025 | 0 | return -1; |
4026 | 0 | } |
4027 | 0 | } |
4028 | | |
4029 | 0 | qio_net_listener_set_client_func(vd->listener, |
4030 | 0 | vnc_listen_io, vd, NULL); |
4031 | 0 | } |
4032 | | |
4033 | 0 | if (wsaddr_list) { |
4034 | 0 | vd->wslistener = qio_net_listener_new(); |
4035 | 0 | qio_net_listener_set_name(vd->wslistener, "vnc-ws-listen"); |
4036 | 0 | for (el = wsaddr_list; el; el = el->next) { |
4037 | 0 | if (qio_net_listener_open_sync(vd->wslistener, |
4038 | 0 | el->value, 1, |
4039 | 0 | errp) < 0) { |
4040 | 0 | return -1; |
4041 | 0 | } |
4042 | 0 | } |
4043 | | |
4044 | 0 | qio_net_listener_set_client_func(vd->wslistener, |
4045 | 0 | vnc_listen_io, vd, NULL); |
4046 | 0 | } |
4047 | | |
4048 | 0 | return 0; |
4049 | 0 | } |
4050 | | |
4051 | | bool vnc_display_update(DisplayUpdateOptionsVNC *arg, Error **errp) |
4052 | 0 | { |
4053 | 0 | VncDisplay *vd = vnc_display_find(NULL); |
4054 | |
|
4055 | 0 | if (!vd) { |
4056 | 0 | error_setg(errp, "Can not find vnc display"); |
4057 | 0 | return false; |
4058 | 0 | } |
4059 | | |
4060 | 0 | if (arg->has_addresses) { |
4061 | 0 | if (vd->listener) { |
4062 | 0 | qio_net_listener_disconnect(vd->listener); |
4063 | 0 | object_unref(OBJECT(vd->listener)); |
4064 | 0 | vd->listener = NULL; |
4065 | 0 | } |
4066 | |
|
4067 | 0 | if (vnc_display_listen(vd, arg->addresses, NULL, errp) < 0) { |
4068 | 0 | return false; |
4069 | 0 | } |
4070 | 0 | } |
4071 | | |
4072 | 0 | return true; |
4073 | 0 | } |
4074 | | |
4075 | | static bool vnc_display_open(VncDisplay *vd, Error **errp) |
4076 | 0 | { |
4077 | 0 | QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, vd->id); |
4078 | 0 | g_autoptr(SocketAddressList) saddr_list = NULL; |
4079 | 0 | g_autoptr(SocketAddressList) wsaddr_list = NULL; |
4080 | 0 | const char *share, *device_id; |
4081 | 0 | QemuConsole *con; |
4082 | 0 | bool password = false; |
4083 | 0 | bool reverse = false; |
4084 | 0 | const char *credid; |
4085 | 0 | bool sasl = false; |
4086 | 0 | const char *tlsauthz; |
4087 | 0 | const char *saslauthz; |
4088 | 0 | int lock_key_sync = 1; |
4089 | 0 | int key_delay_ms; |
4090 | 0 | const char *audiodev; |
4091 | 0 | const char *passwordSecret; |
4092 | |
|
4093 | 0 | assert(vd); |
4094 | 0 | assert(opts); |
4095 | |
|
4096 | 0 | reverse = qemu_opt_get_bool(opts, "reverse", false); |
4097 | 0 | if (vnc_display_get_addresses(opts, reverse, &saddr_list, &wsaddr_list, |
4098 | 0 | errp) < 0) { |
4099 | 0 | return false; |
4100 | 0 | } |
4101 | | |
4102 | 0 | passwordSecret = qemu_opt_get(opts, "password-secret"); |
4103 | 0 | if (passwordSecret) { |
4104 | 0 | if (qemu_opt_get(opts, "password")) { |
4105 | 0 | error_setg(errp, |
4106 | 0 | "'password' flag is redundant with 'password-secret'"); |
4107 | 0 | return false; |
4108 | 0 | } |
4109 | 0 | vd->password = qcrypto_secret_lookup_as_utf8(passwordSecret, |
4110 | 0 | errp); |
4111 | 0 | if (!vd->password) { |
4112 | 0 | return false; |
4113 | 0 | } |
4114 | 0 | password = true; |
4115 | 0 | } else { |
4116 | 0 | password = qemu_opt_get_bool(opts, "password", false); |
4117 | 0 | } |
4118 | 0 | if (password) { |
4119 | 0 | if (!qcrypto_cipher_supports( |
4120 | 0 | QCRYPTO_CIPHER_ALGO_DES, QCRYPTO_CIPHER_MODE_ECB)) { |
4121 | 0 | error_setg(errp, |
4122 | 0 | "Cipher backend does not support DES algorithm"); |
4123 | 0 | return false; |
4124 | 0 | } |
4125 | 0 | } |
4126 | | |
4127 | 0 | lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); |
4128 | 0 | key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); |
4129 | 0 | sasl = qemu_opt_get_bool(opts, "sasl", false); |
4130 | 0 | #ifndef CONFIG_VNC_SASL |
4131 | 0 | if (sasl) { |
4132 | 0 | error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); |
4133 | 0 | return false; |
4134 | 0 | } |
4135 | 0 | #endif /* CONFIG_VNC_SASL */ |
4136 | 0 | credid = qemu_opt_get(opts, "tls-creds"); |
4137 | 0 | if (credid) { |
4138 | 0 | Object *creds; |
4139 | 0 | creds = object_resolve_path_component( |
4140 | 0 | object_get_objects_root(), credid); |
4141 | 0 | if (!creds) { |
4142 | 0 | error_setg(errp, "No TLS credentials with id '%s'", |
4143 | 0 | credid); |
4144 | 0 | return false; |
4145 | 0 | } |
4146 | 0 | vd->tlscreds = (QCryptoTLSCreds *) |
4147 | 0 | object_dynamic_cast(creds, |
4148 | 0 | TYPE_QCRYPTO_TLS_CREDS); |
4149 | 0 | if (!vd->tlscreds) { |
4150 | 0 | error_setg(errp, "Object with id '%s' is not TLS credentials", |
4151 | 0 | credid); |
4152 | 0 | return false; |
4153 | 0 | } |
4154 | 0 | object_ref(OBJECT(vd->tlscreds)); |
4155 | |
|
4156 | 0 | if (!qcrypto_tls_creds_check_endpoint(vd->tlscreds, |
4157 | 0 | QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, |
4158 | 0 | errp)) { |
4159 | 0 | return false; |
4160 | 0 | } |
4161 | 0 | } |
4162 | 0 | tlsauthz = qemu_opt_get(opts, "tls-authz"); |
4163 | 0 | if (tlsauthz && !vd->tlscreds) { |
4164 | 0 | error_setg(errp, "'tls-authz' provided but TLS is not enabled"); |
4165 | 0 | return false; |
4166 | 0 | } |
4167 | | |
4168 | 0 | saslauthz = qemu_opt_get(opts, "sasl-authz"); |
4169 | 0 | if (saslauthz && !sasl) { |
4170 | 0 | error_setg(errp, "'sasl-authz' provided but SASL auth is not enabled"); |
4171 | 0 | return false; |
4172 | 0 | } |
4173 | | |
4174 | 0 | share = qemu_opt_get(opts, "share"); |
4175 | 0 | if (share) { |
4176 | 0 | if (strcmp(share, "ignore") == 0) { |
4177 | 0 | vd->share_policy = VNC_SHARE_POLICY_IGNORE; |
4178 | 0 | } else if (strcmp(share, "allow-exclusive") == 0) { |
4179 | 0 | vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; |
4180 | 0 | } else if (strcmp(share, "force-shared") == 0) { |
4181 | 0 | vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; |
4182 | 0 | } else { |
4183 | 0 | error_setg(errp, "unknown vnc share= option"); |
4184 | 0 | return false; |
4185 | 0 | } |
4186 | 0 | } else { |
4187 | 0 | vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; |
4188 | 0 | } |
4189 | 0 | vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); |
4190 | |
|
4191 | | #ifdef CONFIG_VNC_JPEG |
4192 | | vd->lossy = qemu_opt_get_bool(opts, "lossy", false); |
4193 | | #endif |
4194 | 0 | vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); |
4195 | | /* adaptive updates are only used with tight encoding and |
4196 | | * if lossy updates are enabled so we can disable all the |
4197 | | * calculations otherwise */ |
4198 | 0 | if (!vd->lossy) { |
4199 | 0 | vd->non_adaptive = true; |
4200 | 0 | } |
4201 | |
|
4202 | 0 | vd->power_control = qemu_opt_get_bool(opts, "power-control", false); |
4203 | |
|
4204 | 0 | if (tlsauthz) { |
4205 | 0 | vd->tlsauthzid = g_strdup(tlsauthz); |
4206 | 0 | } |
4207 | | #ifdef CONFIG_VNC_SASL |
4208 | | if (sasl) { |
4209 | | if (saslauthz) { |
4210 | | vd->sasl.authzid = g_strdup(saslauthz); |
4211 | | } |
4212 | | } |
4213 | | #endif |
4214 | |
|
4215 | 0 | if (vnc_display_setup_auth(&vd->auth, &vd->subauth, |
4216 | 0 | vd->tlscreds, password, |
4217 | 0 | sasl, false, errp) < 0) { |
4218 | 0 | return false; |
4219 | 0 | } |
4220 | 0 | trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth, |
4221 | 0 | vnc_auth_name(vd->auth, vd->subauth)); |
4222 | |
|
4223 | 0 | if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, |
4224 | 0 | vd->tlscreds, password, |
4225 | 0 | sasl, true, errp) < 0) { |
4226 | 0 | return false; |
4227 | 0 | } |
4228 | 0 | trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth, |
4229 | 0 | vnc_auth_name(vd->ws_auth, vd->ws_subauth)); |
4230 | |
|
4231 | | #ifdef CONFIG_VNC_SASL |
4232 | | if (sasl && !vnc_sasl_server_init(errp)) { |
4233 | | return false; |
4234 | | } |
4235 | | #endif |
4236 | 0 | vd->lock_key_sync = lock_key_sync; |
4237 | 0 | if (lock_key_sync) { |
4238 | 0 | vd->led_notifier.notify = kbd_leds; |
4239 | 0 | qemu_input_led_notifier_add(&vd->led_notifier); |
4240 | 0 | } |
4241 | 0 | vd->ledstate = 0; |
4242 | |
|
4243 | 0 | audiodev = qemu_opt_get(opts, "audiodev"); |
4244 | 0 | if (audiodev) { |
4245 | 0 | vd->audio_be = audio_be_by_name(audiodev, errp); |
4246 | 0 | if (!vd->audio_be) { |
4247 | 0 | return false; |
4248 | 0 | } |
4249 | 0 | } else { |
4250 | 0 | vd->audio_be = audio_get_default_audio_be(NULL); |
4251 | 0 | } |
4252 | | |
4253 | 0 | device_id = qemu_opt_get(opts, "display"); |
4254 | 0 | if (device_id) { |
4255 | 0 | int head = qemu_opt_get_number(opts, "head", 0); |
4256 | 0 | Error *err = NULL; |
4257 | |
|
4258 | 0 | con = qemu_console_lookup_by_device_name(device_id, head, &err); |
4259 | 0 | if (err) { |
4260 | 0 | error_propagate(errp, err); |
4261 | 0 | return false; |
4262 | 0 | } |
4263 | 0 | } else { |
4264 | 0 | con = qemu_console_lookup_default(); |
4265 | 0 | } |
4266 | | |
4267 | 0 | qemu_console_register_listener(con, &vd->dcl, &dcl_ops); |
4268 | 0 | vd->kbd = qkbd_state_init(vd->dcl.con); |
4269 | 0 | qkbd_state_set_delay(vd->kbd, key_delay_ms); |
4270 | |
|
4271 | 0 | if (saddr_list == NULL) { |
4272 | 0 | return true; |
4273 | 0 | } |
4274 | | |
4275 | 0 | if (reverse) { |
4276 | 0 | if (vnc_display_connect(vd, saddr_list, wsaddr_list, errp) < 0) { |
4277 | 0 | return false; |
4278 | 0 | } |
4279 | 0 | } else { |
4280 | 0 | if (vnc_display_listen(vd, saddr_list, wsaddr_list, errp) < 0) { |
4281 | 0 | return false; |
4282 | 0 | } |
4283 | 0 | } |
4284 | | |
4285 | 0 | if (qemu_opt_get(opts, "to")) { |
4286 | 0 | vnc_display_print_local_addr(vd); |
4287 | 0 | } |
4288 | |
|
4289 | 0 | return true; |
4290 | 0 | } |
4291 | | |
4292 | | void vnc_display_add_client(const char *id, int csock, bool skipauth) |
4293 | 0 | { |
4294 | 0 | VncDisplay *vd = vnc_display_find(id); |
4295 | 0 | QIOChannelSocket *sioc; |
4296 | |
|
4297 | 0 | if (!vd) { |
4298 | 0 | return; |
4299 | 0 | } |
4300 | | |
4301 | 0 | sioc = qio_channel_socket_new_fd(csock, NULL); |
4302 | 0 | if (sioc) { |
4303 | 0 | qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); |
4304 | 0 | vnc_connect(vd, sioc, skipauth, false); |
4305 | 0 | object_unref(OBJECT(sioc)); |
4306 | 0 | } |
4307 | 0 | } |
4308 | | |
4309 | | static char *vnc_auto_assign_id(QemuOpts *opts) |
4310 | 0 | { |
4311 | 0 | QemuOptsList *olist = qemu_find_opts("vnc"); |
4312 | 0 | int i = 2; |
4313 | 0 | char *id; |
4314 | |
|
4315 | 0 | id = g_strdup("default"); |
4316 | 0 | while (qemu_opts_find(olist, id)) { |
4317 | 0 | g_free(id); |
4318 | 0 | id = g_strdup_printf("vnc%d", i++); |
4319 | 0 | } |
4320 | 0 | qemu_opts_set_id(opts, id); |
4321 | |
|
4322 | 0 | return id; |
4323 | 0 | } |
4324 | | |
4325 | | void vnc_parse(const char *str) |
4326 | 0 | { |
4327 | 0 | QemuOptsList *olist = qemu_find_opts("vnc"); |
4328 | 0 | QemuOpts *opts = qemu_opts_parse_noisily(olist, str, !is_help_option(str)); |
4329 | |
|
4330 | 0 | if (!opts) { |
4331 | 0 | exit(1); |
4332 | 0 | } |
4333 | 0 | } |
4334 | | |
4335 | | int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) |
4336 | 0 | { |
4337 | 0 | ERRP_GUARD(); |
4338 | 0 | char *id = (char *)qemu_opts_id(opts); |
4339 | |
|
4340 | 0 | if (!id) { |
4341 | | /* auto-assign id if not present */ |
4342 | 0 | id = vnc_auto_assign_id(opts); |
4343 | 0 | } |
4344 | |
|
4345 | 0 | return vnc_display_new(id, errp) != NULL ? 0 : -1; |
4346 | 0 | } |
4347 | | |
4348 | | void vnc_cleanup(void) |
4349 | 0 | { |
4350 | 0 | VncDisplay *vd, *vd_next; |
4351 | |
|
4352 | 0 | QTAILQ_FOREACH_SAFE(vd, &vnc_displays, next, vd_next) { |
4353 | 0 | vnc_display_free(vd); |
4354 | 0 | } |
4355 | 0 | } |
4356 | | |
4357 | | static void vnc_register_config(void) |
4358 | 61 | { |
4359 | 61 | qemu_add_opts(&qemu_vnc_opts); |
4360 | 61 | } |
4361 | | opts_init(vnc_register_config); |