13e230dd2SCorentin Chary /* 23e230dd2SCorentin Chary * QEMU VNC display driver 33e230dd2SCorentin Chary * 43e230dd2SCorentin Chary * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> 53e230dd2SCorentin Chary * Copyright (C) 2006 Fabrice Bellard 63e230dd2SCorentin Chary * Copyright (C) 2009 Red Hat, Inc 73e230dd2SCorentin Chary * 83e230dd2SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy 93e230dd2SCorentin Chary * of this software and associated documentation files (the "Software"), to deal 103e230dd2SCorentin Chary * in the Software without restriction, including without limitation the rights 113e230dd2SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 123e230dd2SCorentin Chary * copies of the Software, and to permit persons to whom the Software is 133e230dd2SCorentin Chary * furnished to do so, subject to the following conditions: 143e230dd2SCorentin Chary * 153e230dd2SCorentin Chary * The above copyright notice and this permission notice shall be included in 163e230dd2SCorentin Chary * all copies or substantial portions of the Software. 173e230dd2SCorentin Chary * 183e230dd2SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193e230dd2SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203e230dd2SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 213e230dd2SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 223e230dd2SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 233e230dd2SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 243e230dd2SCorentin Chary * THE SOFTWARE. 253e230dd2SCorentin Chary */ 263e230dd2SCorentin Chary 27e16f4c87SPeter Maydell #include "qemu/osdep.h" 283e230dd2SCorentin Chary #include "vnc.h" 29bd023f95SCorentin Chary #include "vnc-jobs.h" 3040066175SGerd Hoffmann #include "trace.h" 311d0d59feSGerd Hoffmann #include "hw/qdev.h" 329c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 33d49b6836SMarkus Armbruster #include "qemu/error-report.h" 341de7afc9SPaolo Bonzini #include "qemu/sockets.h" 351de7afc9SPaolo Bonzini #include "qemu/timer.h" 361de7afc9SPaolo Bonzini #include "qemu/acl.h" 374db14629SGerd Hoffmann #include "qemu/config-file.h" 38cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 397b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 402b54aa87SLuiz Capitulino #include "qmp-commands.h" 418d447d10SGerd Hoffmann #include "ui/input.h" 42fb6ba0d5SWenchao Xia #include "qapi-event.h" 438e9b0d24SDaniel P. Berrange #include "crypto/hash.h" 443e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h" 453e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h" 463e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h" 47f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 483e230dd2SCorentin Chary 490f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 503e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 510f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 52999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 53999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 543e230dd2SCorentin Chary 553e230dd2SCorentin Chary #include "vnc_keysym.h" 56800567a6SDaniel P. Berrange #include "crypto/cipher.h" 573e230dd2SCorentin Chary 58d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays = 59d616ccc5SGerd Hoffmann QTAILQ_HEAD_INITIALIZER(vnc_displays); 603e230dd2SCorentin Chary 613e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 627bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs); 633e230dd2SCorentin Chary 648cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 658cf36489SGerd Hoffmann { 668cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 678cf36489SGerd Hoffmann static const char *mn[] = { 688cf36489SGerd Hoffmann [0] = "undefined", 698cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 708cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 718cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 728cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 738cf36489SGerd Hoffmann }; 7404d2529dSDaniel P. Berrange fprintf(stderr, "%s/%p: %s -> %s\n", __func__, 7504d2529dSDaniel P. Berrange vs->ioc, mn[vs->share_mode], mn[mode]); 768cf36489SGerd Hoffmann #endif 778cf36489SGerd Hoffmann 78e5f34cddSGerd Hoffmann switch (vs->share_mode) { 79e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 80e5f34cddSGerd Hoffmann vs->vd->num_connecting--; 81e5f34cddSGerd Hoffmann break; 82e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 83e5f34cddSGerd Hoffmann vs->vd->num_shared--; 84e5f34cddSGerd Hoffmann break; 85e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 868cf36489SGerd Hoffmann vs->vd->num_exclusive--; 87e5f34cddSGerd Hoffmann break; 88e5f34cddSGerd Hoffmann default: 89e5f34cddSGerd Hoffmann break; 908cf36489SGerd Hoffmann } 91e5f34cddSGerd Hoffmann 928cf36489SGerd Hoffmann vs->share_mode = mode; 93e5f34cddSGerd Hoffmann 94e5f34cddSGerd Hoffmann switch (vs->share_mode) { 95e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 96e5f34cddSGerd Hoffmann vs->vd->num_connecting++; 97e5f34cddSGerd Hoffmann break; 98e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 99e5f34cddSGerd Hoffmann vs->vd->num_shared++; 100e5f34cddSGerd Hoffmann break; 101e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 1028cf36489SGerd Hoffmann vs->vd->num_exclusive++; 103e5f34cddSGerd Hoffmann break; 104e5f34cddSGerd Hoffmann default: 105e5f34cddSGerd Hoffmann break; 1068cf36489SGerd Hoffmann } 1078cf36489SGerd Hoffmann } 1088cf36489SGerd Hoffmann 1093e230dd2SCorentin Chary 11004d2529dSDaniel P. Berrange static void vnc_init_basic_info(SocketAddress *addr, 11198481bfcSEric Blake VncBasicInfo *info, 11298481bfcSEric Blake Error **errp) 1133e230dd2SCorentin Chary { 11404d2529dSDaniel P. Berrange switch (addr->type) { 11504d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_INET: 11632bafa8fSEric Blake info->host = g_strdup(addr->u.inet.data->host); 11732bafa8fSEric Blake info->service = g_strdup(addr->u.inet.data->port); 11832bafa8fSEric Blake if (addr->u.inet.data->ipv6) { 11904d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 12004d2529dSDaniel P. Berrange } else { 12104d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 12204d2529dSDaniel P. Berrange } 12304d2529dSDaniel P. Berrange break; 1243e230dd2SCorentin Chary 12504d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_UNIX: 12604d2529dSDaniel P. Berrange info->host = g_strdup(""); 12732bafa8fSEric Blake info->service = g_strdup(addr->u.q_unix.data->path); 12804d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 12904d2529dSDaniel P. Berrange break; 13004d2529dSDaniel P. Berrange 13104d2529dSDaniel P. Berrange default: 13204d2529dSDaniel P. Berrange error_setg(errp, "Unsupported socket kind %d", 13304d2529dSDaniel P. Berrange addr->type); 13404d2529dSDaniel P. Berrange break; 13504d2529dSDaniel P. Berrange } 13604d2529dSDaniel P. Berrange 13798481bfcSEric Blake return; 1383e230dd2SCorentin Chary } 1393e230dd2SCorentin Chary 14004d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, 14104d2529dSDaniel P. Berrange VncBasicInfo *info, 14298481bfcSEric Blake Error **errp) 1433e230dd2SCorentin Chary { 14404d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 1453e230dd2SCorentin Chary 14604d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, errp); 14704d2529dSDaniel P. Berrange if (!addr) { 14898481bfcSEric Blake return; 1493e230dd2SCorentin Chary } 1503e230dd2SCorentin Chary 15104d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 15204d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 1533e230dd2SCorentin Chary } 1543e230dd2SCorentin Chary 15504d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, 15604d2529dSDaniel P. Berrange VncBasicInfo *info, 15798481bfcSEric Blake Error **errp) 1583e230dd2SCorentin Chary { 15904d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 1603e230dd2SCorentin Chary 16104d2529dSDaniel P. Berrange addr = qio_channel_socket_get_remote_address(ioc, errp); 16204d2529dSDaniel P. Berrange if (!addr) { 16398481bfcSEric Blake return; 1643e230dd2SCorentin Chary } 1653e230dd2SCorentin Chary 16604d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 16704d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 1683e230dd2SCorentin Chary } 1693e230dd2SCorentin Chary 1703e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1713e230dd2SCorentin Chary switch (vd->auth) { 1723e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1733e230dd2SCorentin Chary return "invalid"; 1743e230dd2SCorentin Chary case VNC_AUTH_NONE: 1753e230dd2SCorentin Chary return "none"; 1763e230dd2SCorentin Chary case VNC_AUTH_VNC: 1773e230dd2SCorentin Chary return "vnc"; 1783e230dd2SCorentin Chary case VNC_AUTH_RA2: 1793e230dd2SCorentin Chary return "ra2"; 1803e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1813e230dd2SCorentin Chary return "ra2ne"; 1823e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1833e230dd2SCorentin Chary return "tight"; 1843e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1853e230dd2SCorentin Chary return "ultra"; 1863e230dd2SCorentin Chary case VNC_AUTH_TLS: 1873e230dd2SCorentin Chary return "tls"; 1883e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1893e230dd2SCorentin Chary switch (vd->subauth) { 1903e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1913e230dd2SCorentin Chary return "vencrypt+plain"; 1923e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 1933e230dd2SCorentin Chary return "vencrypt+tls+none"; 1943e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 1953e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 1963e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 1973e230dd2SCorentin Chary return "vencrypt+tls+plain"; 1983e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 1993e230dd2SCorentin Chary return "vencrypt+x509+none"; 2003e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2013e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2023e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2033e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2043e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2053e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2063e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2073e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2083e230dd2SCorentin Chary default: 2093e230dd2SCorentin Chary return "vencrypt"; 2103e230dd2SCorentin Chary } 2113e230dd2SCorentin Chary case VNC_AUTH_SASL: 2123e230dd2SCorentin Chary return "sasl"; 2133e230dd2SCorentin Chary } 2143e230dd2SCorentin Chary return "unknown"; 2153e230dd2SCorentin Chary } 2163e230dd2SCorentin Chary 217d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd) 2183e230dd2SCorentin Chary { 219fb6ba0d5SWenchao Xia VncServerInfo *info; 22098481bfcSEric Blake Error *err = NULL; 2213e230dd2SCorentin Chary 222fb6ba0d5SWenchao Xia info = g_malloc(sizeof(*info)); 223ddf21908SEric Blake vnc_init_basic_info_from_server_addr(vd->lsock, 224ddf21908SEric Blake qapi_VncServerInfo_base(info), &err); 225fb6ba0d5SWenchao Xia info->has_auth = true; 226d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 22798481bfcSEric Blake if (err) { 22898481bfcSEric Blake qapi_free_VncServerInfo(info); 22998481bfcSEric Blake info = NULL; 23098481bfcSEric Blake error_free(err); 23198481bfcSEric Blake } 232fb6ba0d5SWenchao Xia return info; 2333e230dd2SCorentin Chary } 2343e230dd2SCorentin Chary 2353e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2363e230dd2SCorentin Chary { 2373e230dd2SCorentin Chary if (!client->info) { 2383e230dd2SCorentin Chary return; 2393e230dd2SCorentin Chary } 2403e230dd2SCorentin Chary 2413e305e4aSDaniel P. Berrange if (client->tls) { 2423e305e4aSDaniel P. Berrange client->info->x509_dname = 2433e305e4aSDaniel P. Berrange qcrypto_tls_session_get_peer_name(client->tls); 2443e305e4aSDaniel P. Berrange client->info->has_x509_dname = 2453e305e4aSDaniel P. Berrange client->info->x509_dname != NULL; 2463e230dd2SCorentin Chary } 2473e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2483e230dd2SCorentin Chary if (client->sasl.conn && 2493e230dd2SCorentin Chary client->sasl.username) { 250fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 251fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2523e230dd2SCorentin Chary } 2533e230dd2SCorentin Chary #endif 2543e230dd2SCorentin Chary } 2553e230dd2SCorentin Chary 2563e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2573e230dd2SCorentin Chary { 25898481bfcSEric Blake Error *err = NULL; 2593e230dd2SCorentin Chary 260fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 26104d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 262ddf21908SEric Blake qapi_VncClientInfo_base(client->info), 26398481bfcSEric Blake &err); 26498481bfcSEric Blake if (err) { 26598481bfcSEric Blake qapi_free_VncClientInfo(client->info); 26698481bfcSEric Blake client->info = NULL; 26798481bfcSEric Blake error_free(err); 268fb6ba0d5SWenchao Xia } 2693e230dd2SCorentin Chary } 2703e230dd2SCorentin Chary 271fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2723e230dd2SCorentin Chary { 273fb6ba0d5SWenchao Xia VncServerInfo *si; 2743e230dd2SCorentin Chary 2753e230dd2SCorentin Chary if (!vs->info) { 2763e230dd2SCorentin Chary return; 2773e230dd2SCorentin Chary } 2783e230dd2SCorentin Chary 279d616ccc5SGerd Hoffmann si = vnc_server_info_get(vs->vd); 280fb6ba0d5SWenchao Xia if (!si) { 2813e230dd2SCorentin Chary return; 2823e230dd2SCorentin Chary } 2833e230dd2SCorentin Chary 284fb6ba0d5SWenchao Xia switch (event) { 285fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 286ddf21908SEric Blake qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info), 287ddf21908SEric Blake &error_abort); 288fb6ba0d5SWenchao Xia break; 289fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 290fb6ba0d5SWenchao Xia qapi_event_send_vnc_initialized(si, vs->info, &error_abort); 291fb6ba0d5SWenchao Xia break; 292fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 293fb6ba0d5SWenchao Xia qapi_event_send_vnc_disconnected(si, vs->info, &error_abort); 294fb6ba0d5SWenchao Xia break; 295fb6ba0d5SWenchao Xia default: 296fb6ba0d5SWenchao Xia break; 297fb6ba0d5SWenchao Xia } 2983e230dd2SCorentin Chary 299fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 3003e230dd2SCorentin Chary } 3013e230dd2SCorentin Chary 3022b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3033e230dd2SCorentin Chary { 3042b54aa87SLuiz Capitulino VncClientInfo *info; 30504d2529dSDaniel P. Berrange Error *err = NULL; 3062b54aa87SLuiz Capitulino 3072b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 30804d2529dSDaniel P. Berrange 30904d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 31004d2529dSDaniel P. Berrange qapi_VncClientInfo_base(info), 31104d2529dSDaniel P. Berrange &err); 31204d2529dSDaniel P. Berrange if (err) { 31304d2529dSDaniel P. Berrange error_free(err); 31404d2529dSDaniel P. Berrange qapi_free_VncClientInfo(info); 31504d2529dSDaniel P. Berrange return NULL; 31604d2529dSDaniel P. Berrange } 31704d2529dSDaniel P. Berrange 318ddf21908SEric Blake info->websocket = client->websocket; 3193e230dd2SCorentin Chary 3203e305e4aSDaniel P. Berrange if (client->tls) { 3213e305e4aSDaniel P. Berrange info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); 3223e305e4aSDaniel P. Berrange info->has_x509_dname = info->x509_dname != NULL; 3232b54aa87SLuiz Capitulino } 3243e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3252b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3262b54aa87SLuiz Capitulino info->has_sasl_username = true; 3272b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3282b54aa87SLuiz Capitulino } 3293e230dd2SCorentin Chary #endif 3302b54aa87SLuiz Capitulino 3312b54aa87SLuiz Capitulino return info; 3323e230dd2SCorentin Chary } 3333e230dd2SCorentin Chary 334d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id) 335d616ccc5SGerd Hoffmann { 336d616ccc5SGerd Hoffmann VncDisplay *vd; 337d616ccc5SGerd Hoffmann 338d616ccc5SGerd Hoffmann if (id == NULL) { 339d616ccc5SGerd Hoffmann return QTAILQ_FIRST(&vnc_displays); 340d616ccc5SGerd Hoffmann } 341d616ccc5SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 342d616ccc5SGerd Hoffmann if (strcmp(id, vd->id) == 0) { 343d616ccc5SGerd Hoffmann return vd; 344d616ccc5SGerd Hoffmann } 345d616ccc5SGerd Hoffmann } 346d616ccc5SGerd Hoffmann return NULL; 347d616ccc5SGerd Hoffmann } 348d616ccc5SGerd Hoffmann 3492d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) 3502d29a436SGerd Hoffmann { 3512d29a436SGerd Hoffmann VncClientInfoList *cinfo, *prev = NULL; 3522d29a436SGerd Hoffmann VncState *client; 3532d29a436SGerd Hoffmann 3542d29a436SGerd Hoffmann QTAILQ_FOREACH(client, &vd->clients, next) { 3552d29a436SGerd Hoffmann cinfo = g_new0(VncClientInfoList, 1); 3562d29a436SGerd Hoffmann cinfo->value = qmp_query_vnc_client(client); 3572d29a436SGerd Hoffmann cinfo->next = prev; 3582d29a436SGerd Hoffmann prev = cinfo; 3592d29a436SGerd Hoffmann } 3602d29a436SGerd Hoffmann return prev; 3612d29a436SGerd Hoffmann } 3622d29a436SGerd Hoffmann 3632b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3643e230dd2SCorentin Chary { 3652b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 366d616ccc5SGerd Hoffmann VncDisplay *vd = vnc_display_find(NULL); 36704d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 3683e230dd2SCorentin Chary 369bf7aa45eSGerd Hoffmann if (vd == NULL || !vd->enabled) { 3702b54aa87SLuiz Capitulino info->enabled = false; 3713e230dd2SCorentin Chary } else { 3722b54aa87SLuiz Capitulino info->enabled = true; 3732b54aa87SLuiz Capitulino 3742b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3752b54aa87SLuiz Capitulino info->has_clients = true; 3762d29a436SGerd Hoffmann info->clients = qmp_query_client_list(vd); 3773e230dd2SCorentin Chary 37804d2529dSDaniel P. Berrange if (vd->lsock == NULL) { 379417b0b88SPaolo Bonzini return info; 380417b0b88SPaolo Bonzini } 381417b0b88SPaolo Bonzini 38204d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock, errp); 38304d2529dSDaniel P. Berrange if (!addr) { 3842b54aa87SLuiz Capitulino goto out_error; 3852b54aa87SLuiz Capitulino } 3863e230dd2SCorentin Chary 38704d2529dSDaniel P. Berrange switch (addr->type) { 38804d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_INET: 38932bafa8fSEric Blake info->host = g_strdup(addr->u.inet.data->host); 39032bafa8fSEric Blake info->service = g_strdup(addr->u.inet.data->port); 39132bafa8fSEric Blake if (addr->u.inet.data->ipv6) { 39204d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 39304d2529dSDaniel P. Berrange } else { 39404d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 39504d2529dSDaniel P. Berrange } 39604d2529dSDaniel P. Berrange break; 39704d2529dSDaniel P. Berrange 39804d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_UNIX: 39904d2529dSDaniel P. Berrange info->host = g_strdup(""); 40032bafa8fSEric Blake info->service = g_strdup(addr->u.q_unix.data->path); 40104d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 40204d2529dSDaniel P. Berrange break; 40304d2529dSDaniel P. Berrange 40404d2529dSDaniel P. Berrange default: 40504d2529dSDaniel P. Berrange error_setg(errp, "Unsupported socket kind %d", 40604d2529dSDaniel P. Berrange addr->type); 4072b54aa87SLuiz Capitulino goto out_error; 4083e230dd2SCorentin Chary } 4092b54aa87SLuiz Capitulino 4102b54aa87SLuiz Capitulino info->has_host = true; 4112b54aa87SLuiz Capitulino info->has_service = true; 4122b54aa87SLuiz Capitulino info->has_family = true; 4132b54aa87SLuiz Capitulino 4142b54aa87SLuiz Capitulino info->has_auth = true; 415d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 4163e230dd2SCorentin Chary } 4172b54aa87SLuiz Capitulino 41804d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 4192b54aa87SLuiz Capitulino return info; 4202b54aa87SLuiz Capitulino 4212b54aa87SLuiz Capitulino out_error: 42204d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 4232b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4242b54aa87SLuiz Capitulino return NULL; 4253e230dd2SCorentin Chary } 4263e230dd2SCorentin Chary 42704d2529dSDaniel P. Berrange static VncBasicInfoList *qmp_query_server_entry(QIOChannelSocket *ioc, 4284478aa76SGerd Hoffmann bool websocket, 429df887684SGerd Hoffmann VncBasicInfoList *prev) 430df887684SGerd Hoffmann { 431df887684SGerd Hoffmann VncBasicInfoList *list; 432df887684SGerd Hoffmann VncBasicInfo *info; 43304d2529dSDaniel P. Berrange Error *err = NULL; 43404d2529dSDaniel P. Berrange SocketAddress *addr; 435df887684SGerd Hoffmann 43604d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, &err); 43704d2529dSDaniel P. Berrange if (!addr) { 43804d2529dSDaniel P. Berrange error_free(err); 439df887684SGerd Hoffmann return prev; 440df887684SGerd Hoffmann } 441df887684SGerd Hoffmann 442df887684SGerd Hoffmann info = g_new0(VncBasicInfo, 1); 44304d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, &err); 44404d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 44504d2529dSDaniel P. Berrange if (err) { 44604d2529dSDaniel P. Berrange qapi_free_VncBasicInfo(info); 44704d2529dSDaniel P. Berrange error_free(err); 44804d2529dSDaniel P. Berrange return prev; 44904d2529dSDaniel P. Berrange } 4504478aa76SGerd Hoffmann info->websocket = websocket; 451df887684SGerd Hoffmann 452df887684SGerd Hoffmann list = g_new0(VncBasicInfoList, 1); 453df887684SGerd Hoffmann list->value = info; 454df887684SGerd Hoffmann list->next = prev; 455df887684SGerd Hoffmann return list; 456df887684SGerd Hoffmann } 457df887684SGerd Hoffmann 458df887684SGerd Hoffmann static void qmp_query_auth(VncDisplay *vd, VncInfo2 *info) 459df887684SGerd Hoffmann { 460df887684SGerd Hoffmann switch (vd->auth) { 461df887684SGerd Hoffmann case VNC_AUTH_VNC: 462df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_VNC; 463df887684SGerd Hoffmann break; 464df887684SGerd Hoffmann case VNC_AUTH_RA2: 465df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_RA2; 466df887684SGerd Hoffmann break; 467df887684SGerd Hoffmann case VNC_AUTH_RA2NE: 468df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_RA2NE; 469df887684SGerd Hoffmann break; 470df887684SGerd Hoffmann case VNC_AUTH_TIGHT: 471df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_TIGHT; 472df887684SGerd Hoffmann break; 473df887684SGerd Hoffmann case VNC_AUTH_ULTRA: 474df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_ULTRA; 475df887684SGerd Hoffmann break; 476df887684SGerd Hoffmann case VNC_AUTH_TLS: 477df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_TLS; 478df887684SGerd Hoffmann break; 479df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT: 480df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_VENCRYPT; 481df887684SGerd Hoffmann info->has_vencrypt = true; 482df887684SGerd Hoffmann switch (vd->subauth) { 483df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_PLAIN: 484df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; 485df887684SGerd Hoffmann break; 486df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSNONE: 487df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; 488df887684SGerd Hoffmann break; 489df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSVNC: 490df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; 491df887684SGerd Hoffmann break; 492df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSPLAIN: 493df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; 494df887684SGerd Hoffmann break; 495df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509NONE: 496df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; 497df887684SGerd Hoffmann break; 498df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509VNC: 499df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; 500df887684SGerd Hoffmann break; 501df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509PLAIN: 502df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; 503df887684SGerd Hoffmann break; 504df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSSASL: 505df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; 506df887684SGerd Hoffmann break; 507df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509SASL: 508df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; 509df887684SGerd Hoffmann break; 510df887684SGerd Hoffmann default: 511df887684SGerd Hoffmann info->has_vencrypt = false; 512df887684SGerd Hoffmann break; 513df887684SGerd Hoffmann } 514df887684SGerd Hoffmann break; 515df887684SGerd Hoffmann case VNC_AUTH_SASL: 516df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_SASL; 517df887684SGerd Hoffmann break; 518df887684SGerd Hoffmann case VNC_AUTH_NONE: 519df887684SGerd Hoffmann default: 520df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_NONE; 521df887684SGerd Hoffmann break; 522df887684SGerd Hoffmann } 523df887684SGerd Hoffmann } 524df887684SGerd Hoffmann 525df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp) 526df887684SGerd Hoffmann { 527df887684SGerd Hoffmann VncInfo2List *item, *prev = NULL; 528df887684SGerd Hoffmann VncInfo2 *info; 529df887684SGerd Hoffmann VncDisplay *vd; 530df887684SGerd Hoffmann DeviceState *dev; 531df887684SGerd Hoffmann 532df887684SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 533df887684SGerd Hoffmann info = g_new0(VncInfo2, 1); 534df887684SGerd Hoffmann info->id = g_strdup(vd->id); 535df887684SGerd Hoffmann info->clients = qmp_query_client_list(vd); 536df887684SGerd Hoffmann qmp_query_auth(vd, info); 537df887684SGerd Hoffmann if (vd->dcl.con) { 538df887684SGerd Hoffmann dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con), 539df887684SGerd Hoffmann "device", NULL)); 540df887684SGerd Hoffmann info->has_display = true; 541df887684SGerd Hoffmann info->display = g_strdup(dev->id); 542df887684SGerd Hoffmann } 54304d2529dSDaniel P. Berrange if (vd->lsock != NULL) { 54404d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 54504d2529dSDaniel P. Berrange vd->lsock, false, info->server); 546df887684SGerd Hoffmann } 54704d2529dSDaniel P. Berrange if (vd->lwebsock != NULL) { 54804d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 54904d2529dSDaniel P. Berrange vd->lwebsock, true, info->server); 550df887684SGerd Hoffmann } 551df887684SGerd Hoffmann 552df887684SGerd Hoffmann item = g_new0(VncInfo2List, 1); 553df887684SGerd Hoffmann item->value = info; 554df887684SGerd Hoffmann item->next = prev; 555df887684SGerd Hoffmann prev = item; 556df887684SGerd Hoffmann } 557df887684SGerd Hoffmann return prev; 558df887684SGerd Hoffmann } 559df887684SGerd Hoffmann 5603e230dd2SCorentin Chary /* TODO 5613e230dd2SCorentin Chary 1) Get the queue working for IO. 5623e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 5633e230dd2SCorentin Chary and not totally invalidated 5643e230dd2SCorentin Chary 3) resolutions > 1024 5653e230dd2SCorentin Chary */ 5663e230dd2SCorentin Chary 56738ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync); 5683e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 5693e230dd2SCorentin Chary 5703e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 5713e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 5723e230dd2SCorentin Chary int x_position, int y_position, 5733e230dd2SCorentin Chary int w, int h); 5740f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 5753e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 5763e230dd2SCorentin Chary 577d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd) 578d05959c2SGerd Hoffmann { 579d05959c2SGerd Hoffmann return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 580d05959c2SGerd Hoffmann VNC_DIRTY_PIXELS_PER_BIT)); 581d05959c2SGerd Hoffmann } 582d05959c2SGerd Hoffmann 583d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd) 584d05959c2SGerd Hoffmann { 585d05959c2SGerd Hoffmann return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 586d05959c2SGerd Hoffmann } 587d05959c2SGerd Hoffmann 588bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 589bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 590f7b3d68cSGerd Hoffmann VncDisplay *vd, 591f7b3d68cSGerd Hoffmann int x, int y, int w, int h) 592f7b3d68cSGerd Hoffmann { 593f7b3d68cSGerd Hoffmann int width = vnc_width(vd); 594f7b3d68cSGerd Hoffmann int height = vnc_height(vd); 595f7b3d68cSGerd Hoffmann 59691937225SPeter Lieven /* this is needed this to ensure we updated all affected 59791937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 598b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 599b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 6003e230dd2SCorentin Chary 6019f64916dSGerd Hoffmann x = MIN(x, width); 6029f64916dSGerd Hoffmann y = MIN(y, height); 6039f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 60491937225SPeter Lieven h = MIN(y + h, height); 6053e230dd2SCorentin Chary 606b4c85ddcSPeter Lieven for (; y < h; y++) { 607bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 60891937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 609b4c85ddcSPeter Lieven } 6103e230dd2SCorentin Chary } 6113e230dd2SCorentin Chary 612bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 613bea60dd7SPeter Lieven int x, int y, int w, int h) 614bea60dd7SPeter Lieven { 615bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 616bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 617bea60dd7SPeter Lieven 618f7b3d68cSGerd Hoffmann vnc_set_area_dirty(s->dirty, vd, x, y, w, h); 619bea60dd7SPeter Lieven } 620bea60dd7SPeter Lieven 6213e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 6223e230dd2SCorentin Chary int32_t encoding) 6233e230dd2SCorentin Chary { 6243e230dd2SCorentin Chary vnc_write_u16(vs, x); 6253e230dd2SCorentin Chary vnc_write_u16(vs, y); 6263e230dd2SCorentin Chary vnc_write_u16(vs, w); 6273e230dd2SCorentin Chary vnc_write_u16(vs, h); 6283e230dd2SCorentin Chary 6293e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 6303e230dd2SCorentin Chary } 6313e230dd2SCorentin Chary 63232ed2680STim Hardeck 6333e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 6343e230dd2SCorentin Chary { 63504d2529dSDaniel P. Berrange if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 6363e230dd2SCorentin Chary return; 6373e230dd2SCorentin Chary } 638bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 639bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 6403e230dd2SCorentin Chary return; 6413e230dd2SCorentin Chary } 642bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 643bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 644bd023f95SCorentin Chary vnc_lock_output(vs); 6453e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 6463e230dd2SCorentin Chary vnc_write_u8(vs, 0); 6473e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 6483e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 6493e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 650bd023f95SCorentin Chary vnc_unlock_output(vs); 6513e230dd2SCorentin Chary vnc_flush(vs); 6523e230dd2SCorentin Chary } 6533e230dd2SCorentin Chary 654bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 655bd023f95SCorentin Chary { 656bd023f95SCorentin Chary VncState *vs; 657bd023f95SCorentin Chary 658bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 659bd023f95SCorentin Chary vnc_lock_output(vs); 660bd023f95SCorentin Chary vs->abort = true; 661bd023f95SCorentin Chary vnc_unlock_output(vs); 662bd023f95SCorentin Chary } 663bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 664bd023f95SCorentin Chary vnc_jobs_join(vs); 665bd023f95SCorentin Chary } 666bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 667bd023f95SCorentin Chary vnc_lock_output(vs); 668bd023f95SCorentin Chary vs->abort = false; 669bd023f95SCorentin Chary vnc_unlock_output(vs); 670bd023f95SCorentin Chary } 671bd023f95SCorentin Chary } 672bd023f95SCorentin Chary 6739f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 6749f64916dSGerd Hoffmann { 6759f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 6769f64916dSGerd Hoffmann } 6779f64916dSGerd Hoffmann 6789f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 6799f64916dSGerd Hoffmann { 6809f64916dSGerd Hoffmann uint8_t *ptr; 6819f64916dSGerd Hoffmann 6829f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 6839f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 6849f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 6859f64916dSGerd Hoffmann return ptr; 6869f64916dSGerd Hoffmann } 6879f64916dSGerd Hoffmann 688453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd) 689453f842bSGerd Hoffmann { 690453f842bSGerd Hoffmann qemu_pixman_image_unref(vd->server); 691453f842bSGerd Hoffmann vd->server = NULL; 692453f842bSGerd Hoffmann 693c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 694c7628bffSGerd Hoffmann return; 695c7628bffSGerd Hoffmann } 696c7628bffSGerd Hoffmann 697453f842bSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 698453f842bSGerd Hoffmann vnc_width(vd), 699453f842bSGerd Hoffmann vnc_height(vd), 700453f842bSGerd Hoffmann NULL, 0); 701453f842bSGerd Hoffmann } 702453f842bSGerd Hoffmann 703c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 704c12aeb86SGerd Hoffmann DisplaySurface *surface) 7053e230dd2SCorentin Chary { 70621ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7073e230dd2SCorentin Chary VncState *vs; 708bea60dd7SPeter Lieven int width, height; 7093e230dd2SCorentin Chary 710bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 711453f842bSGerd Hoffmann vd->ds = surface; 712bd023f95SCorentin Chary 7133e230dd2SCorentin Chary /* server surface */ 714453f842bSGerd Hoffmann vnc_update_server_surface(vd); 7153e230dd2SCorentin Chary 7163e230dd2SCorentin Chary /* guest surface */ 7179f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 718d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 719d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 720453f842bSGerd Hoffmann width = vnc_width(vd); 721453f842bSGerd Hoffmann height = vnc_height(vd); 722bea60dd7SPeter Lieven memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 723f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 724bea60dd7SPeter Lieven width, height); 7253e230dd2SCorentin Chary 7263e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7273e230dd2SCorentin Chary vnc_colordepth(vs); 7283e230dd2SCorentin Chary vnc_desktop_resize(vs); 7293e230dd2SCorentin Chary if (vs->vd->cursor) { 7303e230dd2SCorentin Chary vnc_cursor_define(vs); 7313e230dd2SCorentin Chary } 732bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 733f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vd, 0, 0, 734bea60dd7SPeter Lieven width, height); 7353e230dd2SCorentin Chary } 7363e230dd2SCorentin Chary } 7373e230dd2SCorentin Chary 7383e230dd2SCorentin Chary /* fastest code */ 7399f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 7403e230dd2SCorentin Chary void *pixels, int size) 7413e230dd2SCorentin Chary { 7423e230dd2SCorentin Chary vnc_write(vs, pixels, size); 7433e230dd2SCorentin Chary } 7443e230dd2SCorentin Chary 7453e230dd2SCorentin Chary /* slowest but generic code. */ 7463e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 7473e230dd2SCorentin Chary { 7483e230dd2SCorentin Chary uint8_t r, g, b; 7493e230dd2SCorentin Chary 7509f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 7519f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 7529f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 7539f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 7549f64916dSGerd Hoffmann #else 7559f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 7569f64916dSGerd Hoffmann #endif 7579f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 7589f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 7599f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 7609f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 7613e230dd2SCorentin Chary case 1: 7623e230dd2SCorentin Chary buf[0] = v; 7633e230dd2SCorentin Chary break; 7643e230dd2SCorentin Chary case 2: 7659f64916dSGerd Hoffmann if (vs->client_be) { 7663e230dd2SCorentin Chary buf[0] = v >> 8; 7673e230dd2SCorentin Chary buf[1] = v; 7683e230dd2SCorentin Chary } else { 7693e230dd2SCorentin Chary buf[1] = v >> 8; 7703e230dd2SCorentin Chary buf[0] = v; 7713e230dd2SCorentin Chary } 7723e230dd2SCorentin Chary break; 7733e230dd2SCorentin Chary default: 7743e230dd2SCorentin Chary case 4: 7759f64916dSGerd Hoffmann if (vs->client_be) { 7763e230dd2SCorentin Chary buf[0] = v >> 24; 7773e230dd2SCorentin Chary buf[1] = v >> 16; 7783e230dd2SCorentin Chary buf[2] = v >> 8; 7793e230dd2SCorentin Chary buf[3] = v; 7803e230dd2SCorentin Chary } else { 7813e230dd2SCorentin Chary buf[3] = v >> 24; 7823e230dd2SCorentin Chary buf[2] = v >> 16; 7833e230dd2SCorentin Chary buf[1] = v >> 8; 7843e230dd2SCorentin Chary buf[0] = v; 7853e230dd2SCorentin Chary } 7863e230dd2SCorentin Chary break; 7873e230dd2SCorentin Chary } 7883e230dd2SCorentin Chary } 7893e230dd2SCorentin Chary 7909f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 7913e230dd2SCorentin Chary void *pixels1, int size) 7923e230dd2SCorentin Chary { 7933e230dd2SCorentin Chary uint8_t buf[4]; 7943e230dd2SCorentin Chary 7959f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 7963e230dd2SCorentin Chary uint32_t *pixels = pixels1; 7973e230dd2SCorentin Chary int n, i; 7983e230dd2SCorentin Chary n = size >> 2; 7993e230dd2SCorentin Chary for (i = 0; i < n; i++) { 8003e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 8019f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 8023e230dd2SCorentin Chary } 8033e230dd2SCorentin Chary } 8043e230dd2SCorentin Chary } 8053e230dd2SCorentin Chary 8063e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8073e230dd2SCorentin Chary { 8083e230dd2SCorentin Chary int i; 8093e230dd2SCorentin Chary uint8_t *row; 8103e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8113e230dd2SCorentin Chary 8129f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 8133e230dd2SCorentin Chary for (i = 0; i < h; i++) { 8149f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 8159f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 8163e230dd2SCorentin Chary } 8173e230dd2SCorentin Chary return 1; 8183e230dd2SCorentin Chary } 8193e230dd2SCorentin Chary 820bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8213e230dd2SCorentin Chary { 8223e230dd2SCorentin Chary int n = 0; 823de3f7de7SPeter Lieven bool encode_raw = false; 824de3f7de7SPeter Lieven size_t saved_offs = vs->output.offset; 8253e230dd2SCorentin Chary 8263e230dd2SCorentin Chary switch(vs->vnc_encoding) { 8273e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 8283e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 8293e230dd2SCorentin Chary break; 8303e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 8313e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 8323e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 8333e230dd2SCorentin Chary break; 8343e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 8353e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 8363e230dd2SCorentin Chary break; 837efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 838efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 839efe556adSCorentin Chary break; 840148954faSCorentin Chary case VNC_ENCODING_ZRLE: 841148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 842148954faSCorentin Chary break; 843148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 844148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 845148954faSCorentin Chary break; 8463e230dd2SCorentin Chary default: 847de3f7de7SPeter Lieven encode_raw = true; 8483e230dd2SCorentin Chary break; 8493e230dd2SCorentin Chary } 850de3f7de7SPeter Lieven 851de3f7de7SPeter Lieven /* If the client has the same pixel format as our internal buffer and 852de3f7de7SPeter Lieven * a RAW encoding would need less space fall back to RAW encoding to 853de3f7de7SPeter Lieven * save bandwidth and processing power in the client. */ 854de3f7de7SPeter Lieven if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy && 855de3f7de7SPeter Lieven 12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) { 856de3f7de7SPeter Lieven vs->output.offset = saved_offs; 857de3f7de7SPeter Lieven encode_raw = true; 858de3f7de7SPeter Lieven } 859de3f7de7SPeter Lieven 860de3f7de7SPeter Lieven if (encode_raw) { 861de3f7de7SPeter Lieven vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 862de3f7de7SPeter Lieven n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 863de3f7de7SPeter Lieven } 864de3f7de7SPeter Lieven 8653e230dd2SCorentin Chary return n; 8663e230dd2SCorentin Chary } 8673e230dd2SCorentin Chary 8683e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 8693e230dd2SCorentin Chary { 8703e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 871bd023f95SCorentin Chary vnc_lock_output(vs); 8723e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8733e230dd2SCorentin Chary vnc_write_u8(vs, 0); 8743e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 8753e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 8763e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 8773e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 878bd023f95SCorentin Chary vnc_unlock_output(vs); 8793e230dd2SCorentin Chary vnc_flush(vs); 8803e230dd2SCorentin Chary } 8813e230dd2SCorentin Chary 8827c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl, 8837c20b4a3SGerd Hoffmann int src_x, int src_y, 8847c20b4a3SGerd Hoffmann int dst_x, int dst_y, int w, int h) 8853e230dd2SCorentin Chary { 88621ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 8873e230dd2SCorentin Chary VncState *vs, *vn; 8883e230dd2SCorentin Chary uint8_t *src_row; 8893e230dd2SCorentin Chary uint8_t *dst_row; 8909f64916dSGerd Hoffmann int i, x, y, pitch, inc, w_lim, s; 8913e230dd2SCorentin Chary int cmp_bytes; 8923e230dd2SCorentin Chary 8937fe4a41cSGerd Hoffmann if (!vd->server) { 8947fe4a41cSGerd Hoffmann /* no client connected */ 8957fe4a41cSGerd Hoffmann return; 8967fe4a41cSGerd Hoffmann } 8977fe4a41cSGerd Hoffmann 8983e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 8993e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 9003e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 9013e230dd2SCorentin Chary vs->force_update = 1; 90238ee14f4SGerd Hoffmann vnc_update_client(vs, 1, true); 9033e230dd2SCorentin Chary /* vs might be free()ed here */ 9043e230dd2SCorentin Chary } 9053e230dd2SCorentin Chary } 9063e230dd2SCorentin Chary 9073e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 9089f64916dSGerd Hoffmann pitch = vnc_server_fb_stride(vd); 9099f64916dSGerd Hoffmann src_row = vnc_server_fb_ptr(vd, src_x, src_y); 9109f64916dSGerd Hoffmann dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y); 9113e230dd2SCorentin Chary y = dst_y; 9123e230dd2SCorentin Chary inc = 1; 9133e230dd2SCorentin Chary if (dst_y > src_y) { 9143e230dd2SCorentin Chary /* copy backwards */ 9153e230dd2SCorentin Chary src_row += pitch * (h-1); 9163e230dd2SCorentin Chary dst_row += pitch * (h-1); 9173e230dd2SCorentin Chary pitch = -pitch; 9183e230dd2SCorentin Chary y = dst_y + h - 1; 9193e230dd2SCorentin Chary inc = -1; 9203e230dd2SCorentin Chary } 921b4c85ddcSPeter Lieven w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 922b4c85ddcSPeter Lieven if (w_lim < 0) { 9233e230dd2SCorentin Chary w_lim = w; 924b4c85ddcSPeter Lieven } else { 925b4c85ddcSPeter Lieven w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT); 926b4c85ddcSPeter Lieven } 9273e230dd2SCorentin Chary for (i = 0; i < h; i++) { 9283e230dd2SCorentin Chary for (x = 0; x <= w_lim; 9293e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 9303e230dd2SCorentin Chary if (x == w_lim) { 9313e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 9323e230dd2SCorentin Chary break; 9333e230dd2SCorentin Chary } else if (!x) { 934b4c85ddcSPeter Lieven s = (VNC_DIRTY_PIXELS_PER_BIT - 935b4c85ddcSPeter Lieven (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 9363e230dd2SCorentin Chary s = MIN(s, w_lim); 9373e230dd2SCorentin Chary } else { 938b4c85ddcSPeter Lieven s = VNC_DIRTY_PIXELS_PER_BIT; 9393e230dd2SCorentin Chary } 9409f64916dSGerd Hoffmann cmp_bytes = s * VNC_SERVER_FB_BYTES; 9413e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 9423e230dd2SCorentin Chary continue; 9433e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 9443e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9453e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 946b4c85ddcSPeter Lieven set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT), 947b4c85ddcSPeter Lieven vs->dirty[y]); 9483e230dd2SCorentin Chary } 9493e230dd2SCorentin Chary } 9503e230dd2SCorentin Chary } 9519f64916dSGerd Hoffmann src_row += pitch - w * VNC_SERVER_FB_BYTES; 9529f64916dSGerd Hoffmann dst_row += pitch - w * VNC_SERVER_FB_BYTES; 9533e230dd2SCorentin Chary y += inc; 9543e230dd2SCorentin Chary } 9553e230dd2SCorentin Chary 9563e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9573e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 9583e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 9593e230dd2SCorentin Chary } 9603e230dd2SCorentin Chary } 9613e230dd2SCorentin Chary } 9623e230dd2SCorentin Chary 9637c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 9647c20b4a3SGerd Hoffmann int x, int y, int visible) 9653e230dd2SCorentin Chary { 9663e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 9673e230dd2SCorentin Chary } 9683e230dd2SCorentin Chary 9693e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 9703e230dd2SCorentin Chary { 9713e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 9723e230dd2SCorentin Chary int isize; 9733e230dd2SCorentin Chary 9743e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 975d01f9595SCorentin Chary vnc_lock_output(vs); 9763e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 9773e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 9783e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 9793e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 9803e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 9819f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 9829f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 9833e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 984d01f9595SCorentin Chary vnc_unlock_output(vs); 9853e230dd2SCorentin Chary return 0; 9863e230dd2SCorentin Chary } 9873e230dd2SCorentin Chary return -1; 9883e230dd2SCorentin Chary } 9893e230dd2SCorentin Chary 9907c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 9917c20b4a3SGerd Hoffmann QEMUCursor *c) 9923e230dd2SCorentin Chary { 993d616ccc5SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 9943e230dd2SCorentin Chary VncState *vs; 9953e230dd2SCorentin Chary 9963e230dd2SCorentin Chary cursor_put(vd->cursor); 9977267c094SAnthony Liguori g_free(vd->cursor_mask); 9983e230dd2SCorentin Chary 9993e230dd2SCorentin Chary vd->cursor = c; 10003e230dd2SCorentin Chary cursor_get(vd->cursor); 10013e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 10027267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 10033e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 10043e230dd2SCorentin Chary 10053e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 10063e230dd2SCorentin Chary vnc_cursor_define(vs); 10073e230dd2SCorentin Chary } 10083e230dd2SCorentin Chary } 10093e230dd2SCorentin Chary 10104769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs, 10116c71a539SCorentin Chary int y, int last_x, int x, int height) 10123e230dd2SCorentin Chary { 10133e230dd2SCorentin Chary int h; 10143e230dd2SCorentin Chary 10156c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 1016bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 10173e230dd2SCorentin Chary break; 1018bc2429b9SCorentin Chary } 1019863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 10203e230dd2SCorentin Chary } 10213e230dd2SCorentin Chary 10223e230dd2SCorentin Chary return h; 10233e230dd2SCorentin Chary } 10243e230dd2SCorentin Chary 102538ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync) 10263e230dd2SCorentin Chary { 102763658280SGerd Hoffmann vs->has_dirty += has_dirty; 102804d2529dSDaniel P. Berrange if (vs->need_update && vs->ioc != NULL) { 10293e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 1030bd023f95SCorentin Chary VncJob *job; 10313e230dd2SCorentin Chary int y; 10322f487a3dSPeter Lieven int height, width; 1033bd023f95SCorentin Chary int n = 0; 1034bd023f95SCorentin Chary 10353e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 10363e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 10373e230dd2SCorentin Chary return 0; 10383e230dd2SCorentin Chary 103963658280SGerd Hoffmann if (!vs->has_dirty && !vs->audio_cap && !vs->force_update) 10403e230dd2SCorentin Chary return 0; 10413e230dd2SCorentin Chary 10423e230dd2SCorentin Chary /* 10433e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 10443e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 10453e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 10463e230dd2SCorentin Chary * send them to the client. 10473e230dd2SCorentin Chary */ 1048bd023f95SCorentin Chary job = vnc_job_new(vs); 10493e230dd2SCorentin Chary 1050bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 1051bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 10523e230dd2SCorentin Chary 105312b316d4SPeter Lieven y = 0; 105412b316d4SPeter Lieven for (;;) { 105512b316d4SPeter Lieven int x, h; 105612b316d4SPeter Lieven unsigned long x2; 105712b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 105812b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 105912b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 106012b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 106112b316d4SPeter Lieven /* no more dirty bits */ 106212b316d4SPeter Lieven break; 10633e230dd2SCorentin Chary } 106412b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 106512b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 106612b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 106712b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 106812b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 106912b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10702f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10712f487a3dSPeter Lieven if (x2 > x) { 107212b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 107312b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10743e230dd2SCorentin Chary } 10750e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10760e7d6f60SPeter Lieven y += h; 10770e7d6f60SPeter Lieven if (y == height) { 10780e7d6f60SPeter Lieven break; 10790e7d6f60SPeter Lieven } 10800e7d6f60SPeter Lieven } 10812f487a3dSPeter Lieven } 1082bd023f95SCorentin Chary 1083bd023f95SCorentin Chary vnc_job_push(job); 1084eb214ff8SGerd Hoffmann if (sync) { 1085eb214ff8SGerd Hoffmann vnc_jobs_join(vs); 1086eb214ff8SGerd Hoffmann } 10873e230dd2SCorentin Chary vs->force_update = 0; 108863658280SGerd Hoffmann vs->has_dirty = 0; 1089bd023f95SCorentin Chary return n; 10903e230dd2SCorentin Chary } 10913e230dd2SCorentin Chary 109204d2529dSDaniel P. Berrange if (vs->disconnecting) { 10933e230dd2SCorentin Chary vnc_disconnect_finish(vs); 109438ee14f4SGerd Hoffmann } else if (sync) { 109538ee14f4SGerd Hoffmann vnc_jobs_join(vs); 109638ee14f4SGerd Hoffmann } 10973e230dd2SCorentin Chary 10983e230dd2SCorentin Chary return 0; 10993e230dd2SCorentin Chary } 11003e230dd2SCorentin Chary 11013e230dd2SCorentin Chary /* audio */ 11023e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 11033e230dd2SCorentin Chary { 11043e230dd2SCorentin Chary VncState *vs = opaque; 11053e230dd2SCorentin Chary 11063e230dd2SCorentin Chary switch (cmd) { 11073e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1108bd023f95SCorentin Chary vnc_lock_output(vs); 11093e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11103e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11113e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1112bd023f95SCorentin Chary vnc_unlock_output(vs); 11133e230dd2SCorentin Chary vnc_flush(vs); 11143e230dd2SCorentin Chary break; 11153e230dd2SCorentin Chary 11163e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1117bd023f95SCorentin Chary vnc_lock_output(vs); 11183e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11193e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11203e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1121bd023f95SCorentin Chary vnc_unlock_output(vs); 11223e230dd2SCorentin Chary vnc_flush(vs); 11233e230dd2SCorentin Chary break; 11243e230dd2SCorentin Chary } 11253e230dd2SCorentin Chary } 11263e230dd2SCorentin Chary 11273e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 11283e230dd2SCorentin Chary { 11293e230dd2SCorentin Chary } 11303e230dd2SCorentin Chary 11313e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 11323e230dd2SCorentin Chary { 11333e230dd2SCorentin Chary VncState *vs = opaque; 11343e230dd2SCorentin Chary 1135bd023f95SCorentin Chary vnc_lock_output(vs); 11363e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11373e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11383e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 11393e230dd2SCorentin Chary vnc_write_u32(vs, size); 11403e230dd2SCorentin Chary vnc_write(vs, buf, size); 1141bd023f95SCorentin Chary vnc_unlock_output(vs); 11423e230dd2SCorentin Chary vnc_flush(vs); 11433e230dd2SCorentin Chary } 11443e230dd2SCorentin Chary 11453e230dd2SCorentin Chary static void audio_add(VncState *vs) 11463e230dd2SCorentin Chary { 11473e230dd2SCorentin Chary struct audio_capture_ops ops; 11483e230dd2SCorentin Chary 11493e230dd2SCorentin Chary if (vs->audio_cap) { 1150027a79c3SCole Robinson error_report("audio already running"); 11513e230dd2SCorentin Chary return; 11523e230dd2SCorentin Chary } 11533e230dd2SCorentin Chary 11543e230dd2SCorentin Chary ops.notify = audio_capture_notify; 11553e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 11563e230dd2SCorentin Chary ops.capture = audio_capture; 11573e230dd2SCorentin Chary 11583e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 11593e230dd2SCorentin Chary if (!vs->audio_cap) { 1160027a79c3SCole Robinson error_report("Failed to add audio capture"); 11613e230dd2SCorentin Chary } 11623e230dd2SCorentin Chary } 11633e230dd2SCorentin Chary 11643e230dd2SCorentin Chary static void audio_del(VncState *vs) 11653e230dd2SCorentin Chary { 11663e230dd2SCorentin Chary if (vs->audio_cap) { 11673e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11683e230dd2SCorentin Chary vs->audio_cap = NULL; 11693e230dd2SCorentin Chary } 11703e230dd2SCorentin Chary } 11713e230dd2SCorentin Chary 11723e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11733e230dd2SCorentin Chary { 117404d2529dSDaniel P. Berrange if (vs->disconnecting) { 11753e230dd2SCorentin Chary return; 117604d2529dSDaniel P. Berrange } 11778cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 117804d2529dSDaniel P. Berrange if (vs->ioc_tag) { 117904d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 118004d2529dSDaniel P. Berrange } 118104d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 118204d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11833e230dd2SCorentin Chary } 11843e230dd2SCorentin Chary 11857536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11863e230dd2SCorentin Chary { 11877d964c9dSCorentin Chary int i; 11887d964c9dSCorentin Chary 1189bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1190bd023f95SCorentin Chary 1191bd023f95SCorentin Chary vnc_lock_output(vs); 1192fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11933e230dd2SCorentin Chary 11943e230dd2SCorentin Chary buffer_free(&vs->input); 11953e230dd2SCorentin Chary buffer_free(&vs->output); 11963e230dd2SCorentin Chary 1197fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11983e230dd2SCorentin Chary 11993e230dd2SCorentin Chary vnc_zlib_clear(vs); 12003e230dd2SCorentin Chary vnc_tight_clear(vs); 1201148954faSCorentin Chary vnc_zrle_clear(vs); 12023e230dd2SCorentin Chary 12033e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12043e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 12053e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12063e230dd2SCorentin Chary audio_del(vs); 12077bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 12083e230dd2SCorentin Chary 12096fd8e79aSTim Hardeck if (vs->initialized) { 12103e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 12116fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 1212c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1213c7628bffSGerd Hoffmann /* last client gone */ 1214c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1215c7628bffSGerd Hoffmann } 12166fd8e79aSTim Hardeck } 12173e230dd2SCorentin Chary 12183e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 12193e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1220bd023f95SCorentin Chary vnc_unlock_output(vs); 1221bd023f95SCorentin Chary 1222bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 12236fd8e79aSTim Hardeck if (vs->bh != NULL) { 1224175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 12256fd8e79aSTim Hardeck } 1226175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1227175b2a6eSCorentin Chary 12287d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 12297267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 12307d964c9dSCorentin Chary } 12317267c094SAnthony Liguori g_free(vs->lossy_rect); 123204d2529dSDaniel P. Berrange 123304d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 123404d2529dSDaniel P. Berrange vs->ioc = NULL; 123504d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 123604d2529dSDaniel P. Berrange vs->sioc = NULL; 12377267c094SAnthony Liguori g_free(vs); 12383e230dd2SCorentin Chary } 12393e230dd2SCorentin Chary 124004d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 12413e230dd2SCorentin Chary { 124204d2529dSDaniel P. Berrange if (ret <= 0) { 124304d2529dSDaniel P. Berrange if (ret == 0) { 124404d2529dSDaniel P. Berrange VNC_DEBUG("Closing down client sock: EOF\n"); 124504d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 124604d2529dSDaniel P. Berrange VNC_DEBUG("Closing down client sock: ret %d (%s)\n", 124704d2529dSDaniel P. Berrange ret, errp ? error_get_pretty(*errp) : "Unknown"); 12483e230dd2SCorentin Chary } 12493e230dd2SCorentin Chary 12503e230dd2SCorentin Chary vnc_disconnect_start(vs); 125104d2529dSDaniel P. Berrange if (errp) { 125204d2529dSDaniel P. Berrange error_free(*errp); 125304d2529dSDaniel P. Berrange *errp = NULL; 125404d2529dSDaniel P. Berrange } 12553e230dd2SCorentin Chary return 0; 12563e230dd2SCorentin Chary } 12573e230dd2SCorentin Chary return ret; 12583e230dd2SCorentin Chary } 12593e230dd2SCorentin Chary 12603e230dd2SCorentin Chary 12613e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12623e230dd2SCorentin Chary { 12633e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12643e230dd2SCorentin Chary vnc_disconnect_start(vs); 12653e230dd2SCorentin Chary } 12663e230dd2SCorentin Chary 12673e305e4aSDaniel P. Berrange 12683e230dd2SCorentin Chary /* 12693e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12703e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12713e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12723e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12733e230dd2SCorentin Chary * 12743e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12753e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12763e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12773e230dd2SCorentin Chary * is active 12783e230dd2SCorentin Chary * 12793e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12803e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12813e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12823e230dd2SCorentin Chary */ 1283fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12843e230dd2SCorentin Chary { 128504d2529dSDaniel P. Berrange Error *err = NULL; 1286fdd1ab6aSDaniel P. Berrange ssize_t ret; 128704d2529dSDaniel P. Berrange ret = qio_channel_write( 128804d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12893e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 129004d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12913e230dd2SCorentin Chary } 12923e230dd2SCorentin Chary 12933e230dd2SCorentin Chary 12943e230dd2SCorentin Chary /* 12953e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12963e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12973e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12983e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12993e230dd2SCorentin Chary * 13003e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 13013e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 13023e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13033e230dd2SCorentin Chary */ 1304fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 13053e230dd2SCorentin Chary { 1306fdd1ab6aSDaniel P. Berrange ssize_t ret; 13073e230dd2SCorentin Chary 13083e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13093e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 13103e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 13113e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 13123e230dd2SCorentin Chary 13133e230dd2SCorentin Chary if (vs->sasl.conn && 13143e230dd2SCorentin Chary vs->sasl.runSSF && 13153e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 13163e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 13173e230dd2SCorentin Chary if (ret) 13183e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 13193e230dd2SCorentin Chary } else 13203e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13213e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 13223e230dd2SCorentin Chary if (!ret) 13233e230dd2SCorentin Chary return 0; 13243e230dd2SCorentin Chary 132532ed2680STim Hardeck buffer_advance(&vs->output, ret); 13263e230dd2SCorentin Chary 13273e230dd2SCorentin Chary if (vs->output.offset == 0) { 132804d2529dSDaniel P. Berrange if (vs->ioc_tag) { 132904d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 133004d2529dSDaniel P. Berrange } 133104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 133204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 13333e230dd2SCorentin Chary } 13343e230dd2SCorentin Chary 13353e230dd2SCorentin Chary return ret; 13363e230dd2SCorentin Chary } 13373e230dd2SCorentin Chary 13383e230dd2SCorentin Chary 13393e230dd2SCorentin Chary /* 13403e230dd2SCorentin Chary * First function called whenever there is data to be written to 13413e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13423e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 13433e230dd2SCorentin Chary */ 134404d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 13453e230dd2SCorentin Chary { 13463e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13473e230dd2SCorentin Chary if (vs->sasl.conn && 13483e230dd2SCorentin Chary vs->sasl.runSSF && 13493e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 13503e230dd2SCorentin Chary vnc_client_write_sasl(vs); 13513e230dd2SCorentin Chary } else 13523e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13537536ee4bSTim Hardeck { 13543e230dd2SCorentin Chary vnc_client_write_plain(vs); 13553e230dd2SCorentin Chary } 13567536ee4bSTim Hardeck } 13573e230dd2SCorentin Chary 135804d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1359bd023f95SCorentin Chary { 1360bd023f95SCorentin Chary 1361bd023f95SCorentin Chary vnc_lock_output(vs); 1362d5f04223SDaniel P. Berrange if (vs->output.offset) { 136304d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 136404d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 136504d2529dSDaniel P. Berrange if (vs->ioc_tag) { 136604d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 136704d2529dSDaniel P. Berrange } 136804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 136904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1370bd023f95SCorentin Chary } 1371bd023f95SCorentin Chary vnc_unlock_output(vs); 1372bd023f95SCorentin Chary } 1373bd023f95SCorentin Chary 13743e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13753e230dd2SCorentin Chary { 13763e230dd2SCorentin Chary vs->read_handler = func; 13773e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13783e230dd2SCorentin Chary } 13793e230dd2SCorentin Chary 13803e230dd2SCorentin Chary 13813e230dd2SCorentin Chary /* 13823e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13833e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13843e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13853e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13863e230dd2SCorentin Chary * 13873e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13883e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13893e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13903e230dd2SCorentin Chary * is active 13913e230dd2SCorentin Chary * 13923e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13933e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13943e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13953e230dd2SCorentin Chary */ 1396fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13973e230dd2SCorentin Chary { 1398fdd1ab6aSDaniel P. Berrange ssize_t ret; 139904d2529dSDaniel P. Berrange Error *err = NULL; 140004d2529dSDaniel P. Berrange ret = qio_channel_read( 140104d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 14023e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 140304d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 14043e230dd2SCorentin Chary } 14053e230dd2SCorentin Chary 14063e230dd2SCorentin Chary 14073e230dd2SCorentin Chary /* 14083e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 14093e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 14103e230dd2SCorentin Chary * data as possible without blocking. 14113e230dd2SCorentin Chary * 14123e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 14133e230dd2SCorentin Chary * disconnects the client socket. 14143e230dd2SCorentin Chary */ 1415fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 14163e230dd2SCorentin Chary { 1417fdd1ab6aSDaniel P. Berrange ssize_t ret; 14183e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 14193e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 14203e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 14213e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 14223e230dd2SCorentin Chary if (!ret) 14233e230dd2SCorentin Chary return 0; 14243e230dd2SCorentin Chary vs->input.offset += ret; 14253e230dd2SCorentin Chary return ret; 14263e230dd2SCorentin Chary } 14273e230dd2SCorentin Chary 1428175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1429175b2a6eSCorentin Chary { 1430175b2a6eSCorentin Chary VncState *vs = opaque; 1431175b2a6eSCorentin Chary 1432175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1433175b2a6eSCorentin Chary } 14343e230dd2SCorentin Chary 14353e230dd2SCorentin Chary /* 14363e230dd2SCorentin Chary * First function called whenever there is more data to be read from 14373e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 14383e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 14393e230dd2SCorentin Chary */ 144004d2529dSDaniel P. Berrange static void vnc_client_read(VncState *vs) 14413e230dd2SCorentin Chary { 1442fdd1ab6aSDaniel P. Berrange ssize_t ret; 14433e230dd2SCorentin Chary 14443e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 14453e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 14463e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 14473e230dd2SCorentin Chary else 14483e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 14493e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 14503e230dd2SCorentin Chary if (!ret) { 145104d2529dSDaniel P. Berrange if (vs->disconnecting) { 14523e230dd2SCorentin Chary vnc_disconnect_finish(vs); 145304d2529dSDaniel P. Berrange } 14543e230dd2SCorentin Chary return; 14553e230dd2SCorentin Chary } 14563e230dd2SCorentin Chary 14573e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14583e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14593e230dd2SCorentin Chary int ret; 14603e230dd2SCorentin Chary 14613e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 146204d2529dSDaniel P. Berrange if (vs->disconnecting) { 14633e230dd2SCorentin Chary vnc_disconnect_finish(vs); 14643e230dd2SCorentin Chary return; 14653e230dd2SCorentin Chary } 14663e230dd2SCorentin Chary 14673e230dd2SCorentin Chary if (!ret) { 146832ed2680STim Hardeck buffer_advance(&vs->input, len); 14693e230dd2SCorentin Chary } else { 14703e230dd2SCorentin Chary vs->read_handler_expect = ret; 14713e230dd2SCorentin Chary } 14723e230dd2SCorentin Chary } 14733e230dd2SCorentin Chary } 14743e230dd2SCorentin Chary 147504d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 147604d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 147704d2529dSDaniel P. Berrange { 147804d2529dSDaniel P. Berrange VncState *vs = opaque; 147904d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 148004d2529dSDaniel P. Berrange vnc_client_read(vs); 148104d2529dSDaniel P. Berrange } 148204d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 148304d2529dSDaniel P. Berrange vnc_client_write(vs); 148404d2529dSDaniel P. Berrange } 148504d2529dSDaniel P. Berrange return TRUE; 148604d2529dSDaniel P. Berrange } 148704d2529dSDaniel P. Berrange 148804d2529dSDaniel P. Berrange 14893e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14903e230dd2SCorentin Chary { 14913e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14923e230dd2SCorentin Chary 149304d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 149404d2529dSDaniel P. Berrange if (vs->ioc_tag) { 149504d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 149604d2529dSDaniel P. Berrange } 149704d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 149804d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14993e230dd2SCorentin Chary } 15003e230dd2SCorentin Chary 15013e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 15023e230dd2SCorentin Chary } 15033e230dd2SCorentin Chary 15043e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 15053e230dd2SCorentin Chary { 15063e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 15073e230dd2SCorentin Chary } 15083e230dd2SCorentin Chary 15093e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 15103e230dd2SCorentin Chary { 15113e230dd2SCorentin Chary uint8_t buf[4]; 15123e230dd2SCorentin Chary 15133e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 15143e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 15153e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 15163e230dd2SCorentin Chary buf[3] = value & 0xFF; 15173e230dd2SCorentin Chary 15183e230dd2SCorentin Chary vnc_write(vs, buf, 4); 15193e230dd2SCorentin Chary } 15203e230dd2SCorentin Chary 15213e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 15223e230dd2SCorentin Chary { 15233e230dd2SCorentin Chary uint8_t buf[2]; 15243e230dd2SCorentin Chary 15253e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 15263e230dd2SCorentin Chary buf[1] = value & 0xFF; 15273e230dd2SCorentin Chary 15283e230dd2SCorentin Chary vnc_write(vs, buf, 2); 15293e230dd2SCorentin Chary } 15303e230dd2SCorentin Chary 15313e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 15323e230dd2SCorentin Chary { 15333e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 15343e230dd2SCorentin Chary } 15353e230dd2SCorentin Chary 15363e230dd2SCorentin Chary void vnc_flush(VncState *vs) 15373e230dd2SCorentin Chary { 1538bd023f95SCorentin Chary vnc_lock_output(vs); 1539d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1540bd023f95SCorentin Chary vnc_client_write_locked(vs); 1541bd023f95SCorentin Chary } 1542bd023f95SCorentin Chary vnc_unlock_output(vs); 15433e230dd2SCorentin Chary } 15443e230dd2SCorentin Chary 154571a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 15463e230dd2SCorentin Chary { 15473e230dd2SCorentin Chary return data[offset]; 15483e230dd2SCorentin Chary } 15493e230dd2SCorentin Chary 155071a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 15513e230dd2SCorentin Chary { 15523e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15533e230dd2SCorentin Chary } 15543e230dd2SCorentin Chary 155571a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15563e230dd2SCorentin Chary { 15573e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15583e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15593e230dd2SCorentin Chary } 15603e230dd2SCorentin Chary 15613e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15623e230dd2SCorentin Chary { 15633e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15643e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15653e230dd2SCorentin Chary } 15663e230dd2SCorentin Chary 15673e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15683e230dd2SCorentin Chary { 15693e230dd2SCorentin Chary } 15703e230dd2SCorentin Chary 15719e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15723e230dd2SCorentin Chary { 15733e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 157414768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15753e230dd2SCorentin Chary 15763e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1577bd023f95SCorentin Chary vnc_lock_output(vs); 15783e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15793e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15803e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15813e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1582bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1583bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15843e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1585bd023f95SCorentin Chary vnc_unlock_output(vs); 15863e230dd2SCorentin Chary vnc_flush(vs); 15873e230dd2SCorentin Chary } 15883e230dd2SCorentin Chary vs->absolute = absolute; 15893e230dd2SCorentin Chary } 15903e230dd2SCorentin Chary 15913e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15923e230dd2SCorentin Chary { 15937fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 159414768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 159514768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 159614768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1597f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1598f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 159914768ebaSGerd Hoffmann }; 160014768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1601bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1602bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 16033e230dd2SCorentin Chary 160414768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 160514768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 160614768ebaSGerd Hoffmann vs->last_bmask = button_mask; 160714768ebaSGerd Hoffmann } 16083e230dd2SCorentin Chary 16093e230dd2SCorentin Chary if (vs->absolute) { 161014768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_X, x, width); 161114768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height); 16123e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 161314768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 161414768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 16153e230dd2SCorentin Chary } else { 161614768ebaSGerd Hoffmann if (vs->last_x != -1) { 161714768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 161814768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 161914768ebaSGerd Hoffmann } 16203e230dd2SCorentin Chary vs->last_x = x; 16213e230dd2SCorentin Chary vs->last_y = y; 16223e230dd2SCorentin Chary } 162314768ebaSGerd Hoffmann qemu_input_event_sync(); 16243e230dd2SCorentin Chary } 16253e230dd2SCorentin Chary 16263e230dd2SCorentin Chary static void reset_keys(VncState *vs) 16273e230dd2SCorentin Chary { 16283e230dd2SCorentin Chary int i; 16293e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 16303e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 16318d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 1632c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 16333e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 16343e230dd2SCorentin Chary } 16353e230dd2SCorentin Chary } 16363e230dd2SCorentin Chary } 16373e230dd2SCorentin Chary 16383e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 16393e230dd2SCorentin Chary { 16403e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 16418d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 1642c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 16438d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1644c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 16453e230dd2SCorentin Chary } 16463e230dd2SCorentin Chary 1647ab99e5c1SLei Li static int current_led_state(VncState *vs) 1648ab99e5c1SLei Li { 1649ab99e5c1SLei Li int ledstate = 0; 1650ab99e5c1SLei Li 1651ab99e5c1SLei Li if (vs->modifiers_state[0x46]) { 1652ab99e5c1SLei Li ledstate |= QEMU_SCROLL_LOCK_LED; 1653ab99e5c1SLei Li } 1654ab99e5c1SLei Li if (vs->modifiers_state[0x45]) { 1655ab99e5c1SLei Li ledstate |= QEMU_NUM_LOCK_LED; 1656ab99e5c1SLei Li } 1657ab99e5c1SLei Li if (vs->modifiers_state[0x3a]) { 1658ab99e5c1SLei Li ledstate |= QEMU_CAPS_LOCK_LED; 1659ab99e5c1SLei Li } 1660ab99e5c1SLei Li 1661ab99e5c1SLei Li return ledstate; 1662ab99e5c1SLei Li } 1663ab99e5c1SLei Li 1664ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1665ab99e5c1SLei Li { 1666ab99e5c1SLei Li int ledstate = 0; 1667ab99e5c1SLei Li 1668ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1669ab99e5c1SLei Li return; 1670ab99e5c1SLei Li } 1671ab99e5c1SLei Li 1672ab99e5c1SLei Li ledstate = current_led_state(vs); 1673ab99e5c1SLei Li vnc_lock_output(vs); 1674ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1675ab99e5c1SLei Li vnc_write_u8(vs, 0); 1676ab99e5c1SLei Li vnc_write_u16(vs, 1); 1677ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1678ab99e5c1SLei Li vnc_write_u8(vs, ledstate); 1679ab99e5c1SLei Li vnc_unlock_output(vs); 1680ab99e5c1SLei Li vnc_flush(vs); 1681ab99e5c1SLei Li } 1682ab99e5c1SLei Li 16833e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16843e230dd2SCorentin Chary { 16853e230dd2SCorentin Chary VncState *vs = opaque; 168696f3d174SLei Li int caps, num, scr; 16871483adcfSLei Li bool has_changed = (ledstate != current_led_state(vs)); 16883e230dd2SCorentin Chary 168940066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 169040066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 169140066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 169240066175SGerd Hoffmann 16933e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 16943e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 169596f3d174SLei Li scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0; 16963e230dd2SCorentin Chary 16973e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 16983e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 16993e230dd2SCorentin Chary } 17003e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 17013e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 17023e230dd2SCorentin Chary } 170396f3d174SLei Li if (vs->modifiers_state[0x46] != scr) { 170496f3d174SLei Li vs->modifiers_state[0x46] = scr; 170596f3d174SLei Li } 1706ab99e5c1SLei Li 1707ab99e5c1SLei Li /* Sending the current led state message to the client */ 17081483adcfSLei Li if (has_changed) { 1709ab99e5c1SLei Li vnc_led_state_change(vs); 1710ab99e5c1SLei Li } 17113e230dd2SCorentin Chary } 17123e230dd2SCorentin Chary 17133e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 17143e230dd2SCorentin Chary { 17153e230dd2SCorentin Chary /* QEMU console switch */ 17163e230dd2SCorentin Chary switch(keycode) { 17173e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17183e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17193e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17203e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17213e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17223e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17233e230dd2SCorentin Chary if (down) 17243e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 17253e230dd2SCorentin Chary else 17263e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 17273e230dd2SCorentin Chary break; 17283e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 17291d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 17301d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 17313e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 17323e230dd2SCorentin Chary reset_keys(vs); 17333e230dd2SCorentin Chary console_select(keycode - 0x02); 17343e230dd2SCorentin Chary return; 17353e230dd2SCorentin Chary } 17363e230dd2SCorentin Chary break; 17373e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 17383e230dd2SCorentin Chary case 0x45: /* NumLock */ 17393e230dd2SCorentin Chary if (down) 17403e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 17413e230dd2SCorentin Chary break; 17423e230dd2SCorentin Chary } 17433e230dd2SCorentin Chary 1744e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1745e7b2aaccSLei Li state extension. 1746e7b2aaccSLei Li */ 17479892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1748e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 17493e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 17503e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 17513e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 17523e230dd2SCorentin Chary toggles numlock away from the VNC window. 17533e230dd2SCorentin Chary */ 17543e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 17553e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 175640066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 17573e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 17583e230dd2SCorentin Chary press_key(vs, 0xff7f); 17593e230dd2SCorentin Chary } 17603e230dd2SCorentin Chary } else { 17613e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 176240066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 17633e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 17643e230dd2SCorentin Chary press_key(vs, 0xff7f); 17653e230dd2SCorentin Chary } 17663e230dd2SCorentin Chary } 17673e230dd2SCorentin Chary } 17683e230dd2SCorentin Chary 17699892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1770e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 17713e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 17723e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 17733e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 17743e230dd2SCorentin Chary toggles capslock away from the VNC window. 17753e230dd2SCorentin Chary */ 17763e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 17773e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 17783e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 17793e230dd2SCorentin Chary if (capslock) { 17803e230dd2SCorentin Chary if (uppercase == shift) { 178140066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17823e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17833e230dd2SCorentin Chary press_key(vs, 0xffe5); 17843e230dd2SCorentin Chary } 17853e230dd2SCorentin Chary } else { 17863e230dd2SCorentin Chary if (uppercase != shift) { 178740066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17883e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17893e230dd2SCorentin Chary press_key(vs, 0xffe5); 17903e230dd2SCorentin Chary } 17913e230dd2SCorentin Chary } 17923e230dd2SCorentin Chary } 17933e230dd2SCorentin Chary 179481c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17958d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 1796c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 17973e230dd2SCorentin Chary } else { 1798e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1799e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1800e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 18013e230dd2SCorentin Chary /* QEMU console emulation */ 18023e230dd2SCorentin Chary if (down) { 18033e230dd2SCorentin Chary switch (keycode) { 18043e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 18053e230dd2SCorentin Chary case 0x36: /* Right Shift */ 18063e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 18073e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 18083e230dd2SCorentin Chary case 0x38: /* Left ALT */ 18093e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 18103e230dd2SCorentin Chary break; 18113e230dd2SCorentin Chary case 0xc8: 18123e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 18133e230dd2SCorentin Chary break; 18143e230dd2SCorentin Chary case 0xd0: 18153e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 18163e230dd2SCorentin Chary break; 18173e230dd2SCorentin Chary case 0xcb: 18183e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 18193e230dd2SCorentin Chary break; 18203e230dd2SCorentin Chary case 0xcd: 18213e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 18223e230dd2SCorentin Chary break; 18233e230dd2SCorentin Chary case 0xd3: 18243e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 18253e230dd2SCorentin Chary break; 18263e230dd2SCorentin Chary case 0xc7: 18273e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 18283e230dd2SCorentin Chary break; 18293e230dd2SCorentin Chary case 0xcf: 18303e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 18313e230dd2SCorentin Chary break; 18323e230dd2SCorentin Chary case 0xc9: 18333e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 18343e230dd2SCorentin Chary break; 18353e230dd2SCorentin Chary case 0xd1: 18363e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 18373e230dd2SCorentin Chary break; 18383e230dd2SCorentin Chary 18393e230dd2SCorentin Chary case 0x47: 18403e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 18413e230dd2SCorentin Chary break; 18423e230dd2SCorentin Chary case 0x48: 18433e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 18443e230dd2SCorentin Chary break; 18453e230dd2SCorentin Chary case 0x49: 18463e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 18473e230dd2SCorentin Chary break; 18483e230dd2SCorentin Chary case 0x4b: 18493e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 18503e230dd2SCorentin Chary break; 18513e230dd2SCorentin Chary case 0x4c: 18523e230dd2SCorentin Chary kbd_put_keysym('5'); 18533e230dd2SCorentin Chary break; 18543e230dd2SCorentin Chary case 0x4d: 18553e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 18563e230dd2SCorentin Chary break; 18573e230dd2SCorentin Chary case 0x4f: 18583e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 18593e230dd2SCorentin Chary break; 18603e230dd2SCorentin Chary case 0x50: 18613e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 18623e230dd2SCorentin Chary break; 18633e230dd2SCorentin Chary case 0x51: 18643e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 18653e230dd2SCorentin Chary break; 18663e230dd2SCorentin Chary case 0x52: 18673e230dd2SCorentin Chary kbd_put_keysym('0'); 18683e230dd2SCorentin Chary break; 18693e230dd2SCorentin Chary case 0x53: 18703e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 18713e230dd2SCorentin Chary break; 18723e230dd2SCorentin Chary 18733e230dd2SCorentin Chary case 0xb5: 18743e230dd2SCorentin Chary kbd_put_keysym('/'); 18753e230dd2SCorentin Chary break; 18763e230dd2SCorentin Chary case 0x37: 18773e230dd2SCorentin Chary kbd_put_keysym('*'); 18783e230dd2SCorentin Chary break; 18793e230dd2SCorentin Chary case 0x4a: 18803e230dd2SCorentin Chary kbd_put_keysym('-'); 18813e230dd2SCorentin Chary break; 18823e230dd2SCorentin Chary case 0x4e: 18833e230dd2SCorentin Chary kbd_put_keysym('+'); 18843e230dd2SCorentin Chary break; 18853e230dd2SCorentin Chary case 0x9c: 18863e230dd2SCorentin Chary kbd_put_keysym('\n'); 18873e230dd2SCorentin Chary break; 18883e230dd2SCorentin Chary 18893e230dd2SCorentin Chary default: 1890e26437c2SGerd Hoffmann if (control) { 1891e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1892e26437c2SGerd Hoffmann } else { 18933e230dd2SCorentin Chary kbd_put_keysym(sym); 1894e26437c2SGerd Hoffmann } 18953e230dd2SCorentin Chary break; 18963e230dd2SCorentin Chary } 18973e230dd2SCorentin Chary } 18983e230dd2SCorentin Chary } 18993e230dd2SCorentin Chary } 19003e230dd2SCorentin Chary 19017bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 19027bc9318bSGerd Hoffmann { 19037bc9318bSGerd Hoffmann static const int keycodes[] = { 19047bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 19057bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 19067bc9318bSGerd Hoffmann }; 19077bc9318bSGerd Hoffmann int i, keycode; 19087bc9318bSGerd Hoffmann 190981c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 19107bc9318bSGerd Hoffmann return; 19117bc9318bSGerd Hoffmann } 19127bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 19137bc9318bSGerd Hoffmann keycode = keycodes[i]; 19147bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 19157bc9318bSGerd Hoffmann continue; 19167bc9318bSGerd Hoffmann } 19178d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1918c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 19197bc9318bSGerd Hoffmann } 19207bc9318bSGerd Hoffmann } 19217bc9318bSGerd Hoffmann 192240066175SGerd Hoffmann static const char *code2name(int keycode) 192340066175SGerd Hoffmann { 192440066175SGerd Hoffmann return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)]; 192540066175SGerd Hoffmann } 192640066175SGerd Hoffmann 19273e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 19283e230dd2SCorentin Chary { 19293e230dd2SCorentin Chary int keycode; 19303e230dd2SCorentin Chary int lsym = sym; 19313e230dd2SCorentin Chary 193281c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 19333e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 19343e230dd2SCorentin Chary } 19353e230dd2SCorentin Chary 19363e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 193740066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 19383e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19393e230dd2SCorentin Chary } 19403e230dd2SCorentin Chary 19413e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 19423e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 19433e230dd2SCorentin Chary { 19443e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 194540066175SGerd Hoffmann if (keyboard_layout) { 19463e230dd2SCorentin Chary key_event(vs, down, sym); 194740066175SGerd Hoffmann } else { 194840066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 19493e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19503e230dd2SCorentin Chary } 195140066175SGerd Hoffmann } 19523e230dd2SCorentin Chary 19533e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1954bea60dd7SPeter Lieven int x, int y, int w, int h) 19553e230dd2SCorentin Chary { 19563e230dd2SCorentin Chary vs->need_update = 1; 1957bea60dd7SPeter Lieven 1958bea60dd7SPeter Lieven if (incremental) { 1959bea60dd7SPeter Lieven return; 19603e230dd2SCorentin Chary } 1961bea60dd7SPeter Lieven 196207535a89SStephan Kulow vs->force_update = 1; 1963f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 19643e230dd2SCorentin Chary } 19653e230dd2SCorentin Chary 19663e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 19673e230dd2SCorentin Chary { 1968bd023f95SCorentin Chary vnc_lock_output(vs); 19693e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19703e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19713e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1972d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1973bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1974bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19753e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1976bd023f95SCorentin Chary vnc_unlock_output(vs); 19773e230dd2SCorentin Chary vnc_flush(vs); 19783e230dd2SCorentin Chary } 19793e230dd2SCorentin Chary 19803e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19813e230dd2SCorentin Chary { 1982bd023f95SCorentin Chary vnc_lock_output(vs); 19833e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19843e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19853e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1986d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1987bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1988bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19893e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1990bd023f95SCorentin Chary vnc_unlock_output(vs); 19913e230dd2SCorentin Chary vnc_flush(vs); 19923e230dd2SCorentin Chary } 19933e230dd2SCorentin Chary 19943e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19953e230dd2SCorentin Chary { 19963e230dd2SCorentin Chary int i; 19973e230dd2SCorentin Chary unsigned int enc = 0; 19983e230dd2SCorentin Chary 19993e230dd2SCorentin Chary vs->features = 0; 20003e230dd2SCorentin Chary vs->vnc_encoding = 0; 2001d1af0e05SCorentin Chary vs->tight.compression = 9; 2002d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 20033e230dd2SCorentin Chary vs->absolute = -1; 20043e230dd2SCorentin Chary 20053e230dd2SCorentin Chary /* 20063e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 2007e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 20083e230dd2SCorentin Chary * will be set at the end of the loop. 20093e230dd2SCorentin Chary */ 20103e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 20113e230dd2SCorentin Chary enc = encodings[i]; 20123e230dd2SCorentin Chary switch (enc) { 20133e230dd2SCorentin Chary case VNC_ENCODING_RAW: 20143e230dd2SCorentin Chary vs->vnc_encoding = enc; 20153e230dd2SCorentin Chary break; 20163e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 20173e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 20183e230dd2SCorentin Chary break; 20193e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 20203e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 20213e230dd2SCorentin Chary vs->vnc_encoding = enc; 20223e230dd2SCorentin Chary break; 20233e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 20243e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 20253e230dd2SCorentin Chary vs->vnc_encoding = enc; 20263e230dd2SCorentin Chary break; 2027fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 2028efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 2029efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 2030efe556adSCorentin Chary vs->vnc_encoding = enc; 2031efe556adSCorentin Chary break; 2032fe3e7f2dSJoel Martin #endif 20333e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 20343e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 20353e230dd2SCorentin Chary vs->vnc_encoding = enc; 20363e230dd2SCorentin Chary break; 2037148954faSCorentin Chary case VNC_ENCODING_ZRLE: 2038148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 2039148954faSCorentin Chary vs->vnc_encoding = enc; 2040148954faSCorentin Chary break; 2041148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 2042148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 2043148954faSCorentin Chary vs->vnc_encoding = enc; 2044148954faSCorentin Chary break; 20453e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 20463e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 20473e230dd2SCorentin Chary break; 20483e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 20493e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 20503e230dd2SCorentin Chary break; 20513e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 20523e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 205391ec41dcSFrediano Ziglio if (vs->vd->cursor) { 205491ec41dcSFrediano Ziglio vnc_cursor_define(vs); 205591ec41dcSFrediano Ziglio } 20563e230dd2SCorentin Chary break; 20573e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 20583e230dd2SCorentin Chary send_ext_key_event_ack(vs); 20593e230dd2SCorentin Chary break; 20603e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 20613e230dd2SCorentin Chary send_ext_audio_ack(vs); 20623e230dd2SCorentin Chary break; 20633e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 20643e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 20653e230dd2SCorentin Chary break; 2066ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 2067ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 2068ab99e5c1SLei Li break; 20693e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 2070d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 20713e230dd2SCorentin Chary break; 20723e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 2073b31f519eSCorentin Chary if (vs->vd->lossy) { 2074d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 2075b31f519eSCorentin Chary } 20763e230dd2SCorentin Chary break; 20773e230dd2SCorentin Chary default: 20783e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 20793e230dd2SCorentin Chary break; 20803e230dd2SCorentin Chary } 20813e230dd2SCorentin Chary } 20823e230dd2SCorentin Chary vnc_desktop_resize(vs); 20839e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2084ab99e5c1SLei Li vnc_led_state_change(vs); 20853e230dd2SCorentin Chary } 20863e230dd2SCorentin Chary 20873e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20883e230dd2SCorentin Chary { 20899f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20909f64916dSGerd Hoffmann 20919f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20923e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20933e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20943e230dd2SCorentin Chary } else { 20953e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20963e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20973e230dd2SCorentin Chary } 20983e230dd2SCorentin Chary } 20993e230dd2SCorentin Chary 21000c426e45SAlexander Graf static void send_color_map(VncState *vs) 21010c426e45SAlexander Graf { 21020c426e45SAlexander Graf int i; 21030c426e45SAlexander Graf 21040c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 21050c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 21060c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 21070c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 21080c426e45SAlexander Graf 21090c426e45SAlexander Graf for (i = 0; i < 256; i++) { 21100c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 21110c426e45SAlexander Graf 21120c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 21130c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 21140c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 21150c426e45SAlexander Graf } 21160c426e45SAlexander Graf } 21170c426e45SAlexander Graf 2118ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 21193e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 21203e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 21213e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 21223e230dd2SCorentin Chary { 21233e230dd2SCorentin Chary if (!true_color_flag) { 21240c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 21250c426e45SAlexander Graf bits_per_pixel = 8; 21260c426e45SAlexander Graf red_max = 7; 21270c426e45SAlexander Graf green_max = 7; 21280c426e45SAlexander Graf blue_max = 3; 21290c426e45SAlexander Graf red_shift = 0; 21300c426e45SAlexander Graf green_shift = 3; 21310c426e45SAlexander Graf blue_shift = 6; 21323e230dd2SCorentin Chary } 21333e230dd2SCorentin Chary 2134e6908bfeSPetr Matousek switch (bits_per_pixel) { 2135e6908bfeSPetr Matousek case 8: 2136e6908bfeSPetr Matousek case 16: 2137e6908bfeSPetr Matousek case 32: 2138e6908bfeSPetr Matousek break; 2139e6908bfeSPetr Matousek default: 2140e6908bfeSPetr Matousek vnc_client_error(vs); 2141e6908bfeSPetr Matousek return; 2142e6908bfeSPetr Matousek } 2143e6908bfeSPetr Matousek 21444c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 21459f64916dSGerd Hoffmann vs->client_pf.rbits = hweight_long(red_max); 21469f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 21479f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 21484c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 21499f64916dSGerd Hoffmann vs->client_pf.gbits = hweight_long(green_max); 21509f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 21519f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 21524c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 21539f64916dSGerd Hoffmann vs->client_pf.bbits = hweight_long(blue_max); 21549f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 21559f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 21569f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 21579f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 21589f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 21599f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 21603e230dd2SCorentin Chary 21610c426e45SAlexander Graf if (!true_color_flag) { 21620c426e45SAlexander Graf send_color_map(vs); 21630c426e45SAlexander Graf } 21640c426e45SAlexander Graf 21653e230dd2SCorentin Chary set_pixel_conversion(vs); 21663e230dd2SCorentin Chary 21671d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 21681d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 21693e230dd2SCorentin Chary } 21703e230dd2SCorentin Chary 21713e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 21723e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 21733e230dd2SCorentin Chary 21749f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 21759f64916dSGerd Hoffmann 21769f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 21779f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 21783e230dd2SCorentin Chary 21793e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 21803e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 21813e230dd2SCorentin Chary #else 21823e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21833e230dd2SCorentin Chary #endif 21843e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21859f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21869f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21879f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21889f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21899f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21909f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21919f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21923e230dd2SCorentin Chary 21933e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21943e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21953e230dd2SCorentin Chary } 21963e230dd2SCorentin Chary 21973e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21983e230dd2SCorentin Chary { 21993e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 22003e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2201bd023f95SCorentin Chary vnc_lock_output(vs); 22023e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 22033e230dd2SCorentin Chary vnc_write_u8(vs, 0); 22043e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2205d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2206bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2207bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2208d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 22093e230dd2SCorentin Chary pixel_format_message(vs); 2210bd023f95SCorentin Chary vnc_unlock_output(vs); 22113e230dd2SCorentin Chary vnc_flush(vs); 22123e230dd2SCorentin Chary } else { 22133e230dd2SCorentin Chary set_pixel_conversion(vs); 22143e230dd2SCorentin Chary } 22153e230dd2SCorentin Chary } 22163e230dd2SCorentin Chary 22173e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 22183e230dd2SCorentin Chary { 22193e230dd2SCorentin Chary int i; 22203e230dd2SCorentin Chary uint16_t limit; 22213e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 22223e230dd2SCorentin Chary 22233e230dd2SCorentin Chary if (data[0] > 3) { 22240f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 22253e230dd2SCorentin Chary } 22263e230dd2SCorentin Chary 22273e230dd2SCorentin Chary switch (data[0]) { 22283e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 22293e230dd2SCorentin Chary if (len == 1) 22303e230dd2SCorentin Chary return 20; 22313e230dd2SCorentin Chary 2232ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 22333e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 22343e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 22353e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 22363e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 22373e230dd2SCorentin Chary break; 22383e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 22393e230dd2SCorentin Chary if (len == 1) 22403e230dd2SCorentin Chary return 4; 22413e230dd2SCorentin Chary 22423e230dd2SCorentin Chary if (len == 4) { 22433e230dd2SCorentin Chary limit = read_u16(data, 2); 22443e230dd2SCorentin Chary if (limit > 0) 22453e230dd2SCorentin Chary return 4 + (limit * 4); 22463e230dd2SCorentin Chary } else 22473e230dd2SCorentin Chary limit = read_u16(data, 2); 22483e230dd2SCorentin Chary 22493e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 22503e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 22513e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 22523e230dd2SCorentin Chary } 22533e230dd2SCorentin Chary 22543e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 22553e230dd2SCorentin Chary break; 22563e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 22573e230dd2SCorentin Chary if (len == 1) 22583e230dd2SCorentin Chary return 10; 22593e230dd2SCorentin Chary 22603e230dd2SCorentin Chary framebuffer_update_request(vs, 22613e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 22623e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 22633e230dd2SCorentin Chary break; 22643e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 22653e230dd2SCorentin Chary if (len == 1) 22663e230dd2SCorentin Chary return 8; 22673e230dd2SCorentin Chary 22683e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 22693e230dd2SCorentin Chary break; 22703e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 22713e230dd2SCorentin Chary if (len == 1) 22723e230dd2SCorentin Chary return 6; 22733e230dd2SCorentin Chary 22743e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 22753e230dd2SCorentin Chary break; 22763e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2277f9a70e79SPeter Lieven if (len == 1) { 22783e230dd2SCorentin Chary return 8; 2279f9a70e79SPeter Lieven } 22803e230dd2SCorentin Chary if (len == 8) { 22813e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2282f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2283f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2284f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2285f9a70e79SPeter Lieven vnc_client_error(vs); 2286f9a70e79SPeter Lieven break; 2287f9a70e79SPeter Lieven } 2288f9a70e79SPeter Lieven if (dlen > 0) { 22893e230dd2SCorentin Chary return 8 + dlen; 22903e230dd2SCorentin Chary } 2291f9a70e79SPeter Lieven } 22923e230dd2SCorentin Chary 22933e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22943e230dd2SCorentin Chary break; 22953e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22963e230dd2SCorentin Chary if (len == 1) 22973e230dd2SCorentin Chary return 2; 22983e230dd2SCorentin Chary 22993e230dd2SCorentin Chary switch (read_u8(data, 1)) { 23003e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 23013e230dd2SCorentin Chary if (len == 2) 23023e230dd2SCorentin Chary return 12; 23033e230dd2SCorentin Chary 23043e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 23053e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 23063e230dd2SCorentin Chary break; 23073e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 23083e230dd2SCorentin Chary if (len == 2) 23093e230dd2SCorentin Chary return 4; 23103e230dd2SCorentin Chary 23113e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 23123e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 23133e230dd2SCorentin Chary audio_add(vs); 23143e230dd2SCorentin Chary break; 23153e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 23163e230dd2SCorentin Chary audio_del(vs); 23173e230dd2SCorentin Chary break; 23183e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 23193e230dd2SCorentin Chary if (len == 4) 23203e230dd2SCorentin Chary return 10; 23213e230dd2SCorentin Chary switch (read_u8(data, 4)) { 23223e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 23233e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 23243e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 23253e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 23263e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 23273e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 23283e230dd2SCorentin Chary default: 2329153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 23303e230dd2SCorentin Chary vnc_client_error(vs); 23313e230dd2SCorentin Chary break; 23323e230dd2SCorentin Chary } 23333e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 23343e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2335153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio channel coount %d\n", 23363e230dd2SCorentin Chary read_u8(data, 5)); 23373e230dd2SCorentin Chary vnc_client_error(vs); 23383e230dd2SCorentin Chary break; 23393e230dd2SCorentin Chary } 23403e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 23413e230dd2SCorentin Chary break; 23423e230dd2SCorentin Chary default: 2343153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 23443e230dd2SCorentin Chary vnc_client_error(vs); 23453e230dd2SCorentin Chary break; 23463e230dd2SCorentin Chary } 23473e230dd2SCorentin Chary break; 23483e230dd2SCorentin Chary 23493e230dd2SCorentin Chary default: 2350153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 23513e230dd2SCorentin Chary vnc_client_error(vs); 23523e230dd2SCorentin Chary break; 23533e230dd2SCorentin Chary } 23543e230dd2SCorentin Chary break; 23553e230dd2SCorentin Chary default: 2356153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 23573e230dd2SCorentin Chary vnc_client_error(vs); 23583e230dd2SCorentin Chary break; 23593e230dd2SCorentin Chary } 23603e230dd2SCorentin Chary 23613e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23623e230dd2SCorentin Chary return 0; 23633e230dd2SCorentin Chary } 23643e230dd2SCorentin Chary 23653e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 23663e230dd2SCorentin Chary { 23673e230dd2SCorentin Chary char buf[1024]; 23688cf36489SGerd Hoffmann VncShareMode mode; 23693e230dd2SCorentin Chary int size; 23703e230dd2SCorentin Chary 23718cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 23728cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 23738cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 23748cf36489SGerd Hoffmann /* 23758cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 23768cf36489SGerd Hoffmann * 23778cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 23788cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 23798cf36489SGerd Hoffmann * reasons. 23808cf36489SGerd Hoffmann */ 23818cf36489SGerd Hoffmann break; 23828cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23838cf36489SGerd Hoffmann /* 23848cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23858cf36489SGerd Hoffmann * 23868cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23878cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23888cf36489SGerd Hoffmann * as no exclusive connection exists. 23898cf36489SGerd Hoffmann * 23908cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23918cf36489SGerd Hoffmann */ 23928cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23938cf36489SGerd Hoffmann VncState *client; 23948cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23958cf36489SGerd Hoffmann if (vs == client) { 23968cf36489SGerd Hoffmann continue; 23978cf36489SGerd Hoffmann } 23988cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23998cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 24008cf36489SGerd Hoffmann continue; 24018cf36489SGerd Hoffmann } 24028cf36489SGerd Hoffmann vnc_disconnect_start(client); 24038cf36489SGerd Hoffmann } 24048cf36489SGerd Hoffmann } 24058cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 24068cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 24078cf36489SGerd Hoffmann vnc_disconnect_start(vs); 24088cf36489SGerd Hoffmann return 0; 24098cf36489SGerd Hoffmann } 24108cf36489SGerd Hoffmann } 24118cf36489SGerd Hoffmann break; 24128cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 24138cf36489SGerd Hoffmann /* 24148cf36489SGerd Hoffmann * Policy: Shared connects only. 24158cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 24168cf36489SGerd Hoffmann * 24178cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 24188cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 24198cf36489SGerd Hoffmann * client disconnect everybody else. 24208cf36489SGerd Hoffmann */ 24218cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 24228cf36489SGerd Hoffmann vnc_disconnect_start(vs); 24238cf36489SGerd Hoffmann return 0; 24248cf36489SGerd Hoffmann } 24258cf36489SGerd Hoffmann break; 24268cf36489SGerd Hoffmann } 24278cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 24288cf36489SGerd Hoffmann 2429e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2430e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2431e5f34cddSGerd Hoffmann return 0; 2432e5f34cddSGerd Hoffmann } 2433e5f34cddSGerd Hoffmann 2434bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2435bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 24363e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 24373e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 24383e230dd2SCorentin Chary 24393e230dd2SCorentin Chary pixel_format_message(vs); 24403e230dd2SCorentin Chary 24413e230dd2SCorentin Chary if (qemu_name) 24423e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 24433e230dd2SCorentin Chary else 24443e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 24453e230dd2SCorentin Chary 24463e230dd2SCorentin Chary vnc_write_u32(vs, size); 24473e230dd2SCorentin Chary vnc_write(vs, buf, size); 24483e230dd2SCorentin Chary vnc_flush(vs); 24493e230dd2SCorentin Chary 24503e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2451fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 24523e230dd2SCorentin Chary 24533e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 24543e230dd2SCorentin Chary 24553e230dd2SCorentin Chary return 0; 24563e230dd2SCorentin Chary } 24573e230dd2SCorentin Chary 24583e230dd2SCorentin Chary void start_client_init(VncState *vs) 24593e230dd2SCorentin Chary { 24603e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 24613e230dd2SCorentin Chary } 24623e230dd2SCorentin Chary 24633e230dd2SCorentin Chary static void make_challenge(VncState *vs) 24643e230dd2SCorentin Chary { 24653e230dd2SCorentin Chary int i; 24663e230dd2SCorentin Chary 24673e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 24683e230dd2SCorentin Chary 24693e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 24703e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 24713e230dd2SCorentin Chary } 24723e230dd2SCorentin Chary 24733e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 24743e230dd2SCorentin Chary { 24753e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2476800567a6SDaniel P. Berrange size_t i, pwlen; 24773e230dd2SCorentin Chary unsigned char key[8]; 24783c9405a0SGerd Hoffmann time_t now = time(NULL); 247960928458SGonglei QCryptoCipher *cipher = NULL; 2480800567a6SDaniel P. Berrange Error *err = NULL; 24813e230dd2SCorentin Chary 24821cd20f8bSAnthony Liguori if (!vs->vd->password) { 24833e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 24846bffdf0fSGerd Hoffmann goto reject; 24853e230dd2SCorentin Chary } 24863c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 24873c9405a0SGerd Hoffmann VNC_DEBUG("Password is expired"); 24883c9405a0SGerd Hoffmann goto reject; 24893c9405a0SGerd Hoffmann } 24903e230dd2SCorentin Chary 24913e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24923e230dd2SCorentin Chary 24933e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24943e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24953e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24963e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2497800567a6SDaniel P. Berrange 2498800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2499800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2500800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2501800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2502800567a6SDaniel P. Berrange &err); 2503800567a6SDaniel P. Berrange if (!cipher) { 2504800567a6SDaniel P. Berrange VNC_DEBUG("Cannot initialize cipher %s", 2505800567a6SDaniel P. Berrange error_get_pretty(err)); 2506800567a6SDaniel P. Berrange error_free(err); 2507800567a6SDaniel P. Berrange goto reject; 2508800567a6SDaniel P. Berrange } 2509800567a6SDaniel P. Berrange 2510a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2511800567a6SDaniel P. Berrange vs->challenge, 2512800567a6SDaniel P. Berrange response, 2513800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2514800567a6SDaniel P. Berrange &err) < 0) { 2515800567a6SDaniel P. Berrange VNC_DEBUG("Cannot encrypt challenge %s", 2516800567a6SDaniel P. Berrange error_get_pretty(err)); 2517800567a6SDaniel P. Berrange error_free(err); 2518800567a6SDaniel P. Berrange goto reject; 2519800567a6SDaniel P. Berrange } 25203e230dd2SCorentin Chary 25213e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 25223e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2523e5bed759SDong Xu Wang VNC_DEBUG("Client challenge response did not match\n"); 25246bffdf0fSGerd Hoffmann goto reject; 25256bffdf0fSGerd Hoffmann } else { 25266bffdf0fSGerd Hoffmann VNC_DEBUG("Accepting VNC challenge response\n"); 25276bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 25286bffdf0fSGerd Hoffmann vnc_flush(vs); 25296bffdf0fSGerd Hoffmann 25306bffdf0fSGerd Hoffmann start_client_init(vs); 25316bffdf0fSGerd Hoffmann } 253260928458SGonglei 253360928458SGonglei qcrypto_cipher_free(cipher); 25346bffdf0fSGerd Hoffmann return 0; 25356bffdf0fSGerd Hoffmann 25366bffdf0fSGerd Hoffmann reject: 25373e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 25383e230dd2SCorentin Chary if (vs->minor >= 8) { 25393e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25403e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25413e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25423e230dd2SCorentin Chary } 25433e230dd2SCorentin Chary vnc_flush(vs); 25443e230dd2SCorentin Chary vnc_client_error(vs); 254560928458SGonglei qcrypto_cipher_free(cipher); 25463e230dd2SCorentin Chary return 0; 25473e230dd2SCorentin Chary } 25483e230dd2SCorentin Chary 25493e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 25503e230dd2SCorentin Chary { 25513e230dd2SCorentin Chary make_challenge(vs); 25523e230dd2SCorentin Chary /* Send client a 'random' challenge */ 25533e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 25543e230dd2SCorentin Chary vnc_flush(vs); 25553e230dd2SCorentin Chary 25563e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 25573e230dd2SCorentin Chary } 25583e230dd2SCorentin Chary 25593e230dd2SCorentin Chary 25603e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 25613e230dd2SCorentin Chary { 25623e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 25633e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 25647e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 25653e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 25663e230dd2SCorentin Chary vnc_write_u32(vs, 1); 25673e230dd2SCorentin Chary if (vs->minor >= 8) { 25683e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25693e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25703e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25713e230dd2SCorentin Chary } 25723e230dd2SCorentin Chary vnc_client_error(vs); 25733e230dd2SCorentin Chary } else { /* Accept requested auth */ 25743e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 25757e7e2ebcSDaniel P. Berrange switch (vs->auth) { 25763e230dd2SCorentin Chary case VNC_AUTH_NONE: 25773e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 25783e230dd2SCorentin Chary if (vs->minor >= 8) { 25793e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25803e230dd2SCorentin Chary vnc_flush(vs); 25813e230dd2SCorentin Chary } 25823e230dd2SCorentin Chary start_client_init(vs); 25833e230dd2SCorentin Chary break; 25843e230dd2SCorentin Chary 25853e230dd2SCorentin Chary case VNC_AUTH_VNC: 25863e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 25873e230dd2SCorentin Chary start_auth_vnc(vs); 25883e230dd2SCorentin Chary break; 25893e230dd2SCorentin Chary 25903e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25913a93113aSDong Xu Wang VNC_DEBUG("Accept VeNCrypt auth\n"); 25923e230dd2SCorentin Chary start_auth_vencrypt(vs); 25933e230dd2SCorentin Chary break; 25943e230dd2SCorentin Chary 25953e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25963e230dd2SCorentin Chary case VNC_AUTH_SASL: 25973e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 25983e230dd2SCorentin Chary start_auth_sasl(vs); 25993e230dd2SCorentin Chary break; 26003e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 26013e230dd2SCorentin Chary 26023e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 26037e7e2ebcSDaniel P. Berrange VNC_DEBUG("Reject auth %d server code bug\n", vs->auth); 26043e230dd2SCorentin Chary vnc_write_u8(vs, 1); 26053e230dd2SCorentin Chary if (vs->minor >= 8) { 26063e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 26073e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 26083e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 26093e230dd2SCorentin Chary } 26103e230dd2SCorentin Chary vnc_client_error(vs); 26113e230dd2SCorentin Chary } 26123e230dd2SCorentin Chary } 26133e230dd2SCorentin Chary return 0; 26143e230dd2SCorentin Chary } 26153e230dd2SCorentin Chary 26163e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 26173e230dd2SCorentin Chary { 26183e230dd2SCorentin Chary char local[13]; 26193e230dd2SCorentin Chary 26203e230dd2SCorentin Chary memcpy(local, version, 12); 26213e230dd2SCorentin Chary local[12] = 0; 26223e230dd2SCorentin Chary 26233e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 26243e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 26253e230dd2SCorentin Chary vnc_client_error(vs); 26263e230dd2SCorentin Chary return 0; 26273e230dd2SCorentin Chary } 26283e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 26293e230dd2SCorentin Chary if (vs->major != 3 || 26303e230dd2SCorentin Chary (vs->minor != 3 && 26313e230dd2SCorentin Chary vs->minor != 4 && 26323e230dd2SCorentin Chary vs->minor != 5 && 26333e230dd2SCorentin Chary vs->minor != 7 && 26343e230dd2SCorentin Chary vs->minor != 8)) { 26353e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 26363e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 26373e230dd2SCorentin Chary vnc_flush(vs); 26383e230dd2SCorentin Chary vnc_client_error(vs); 26393e230dd2SCorentin Chary return 0; 26403e230dd2SCorentin Chary } 26413e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 26423e230dd2SCorentin Chary * as equivalent to v3.3 by servers 26433e230dd2SCorentin Chary */ 26443e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 26453e230dd2SCorentin Chary vs->minor = 3; 26463e230dd2SCorentin Chary 26473e230dd2SCorentin Chary if (vs->minor == 3) { 26487e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 26493e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 26507e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 26513e230dd2SCorentin Chary vnc_flush(vs); 26523e230dd2SCorentin Chary start_client_init(vs); 26537e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 26543e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 26557e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 26563e230dd2SCorentin Chary vnc_flush(vs); 26573e230dd2SCorentin Chary start_auth_vnc(vs); 26583e230dd2SCorentin Chary } else { 26597e7e2ebcSDaniel P. Berrange VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth); 26603e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 26613e230dd2SCorentin Chary vnc_flush(vs); 26623e230dd2SCorentin Chary vnc_client_error(vs); 26633e230dd2SCorentin Chary } 26643e230dd2SCorentin Chary } else { 26657e7e2ebcSDaniel P. Berrange VNC_DEBUG("Telling client we support auth %d\n", vs->auth); 26663e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 26677e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 26683e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 26693e230dd2SCorentin Chary vnc_flush(vs); 26703e230dd2SCorentin Chary } 26713e230dd2SCorentin Chary 26723e230dd2SCorentin Chary return 0; 26733e230dd2SCorentin Chary } 26743e230dd2SCorentin Chary 2675999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2676999342a0SCorentin Chary { 2677999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2678999342a0SCorentin Chary 2679999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2680999342a0SCorentin Chary } 2681999342a0SCorentin Chary 26827d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26837d964c9dSCorentin Chary { 26847d964c9dSCorentin Chary int i, j; 26857d964c9dSCorentin Chary 26867d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26877d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26887d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26897d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26907d964c9dSCorentin Chary 2691207f328aSCorentin Chary for (j = y; j <= h; j++) { 2692207f328aSCorentin Chary for (i = x; i <= w; i++) { 26937d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26947d964c9dSCorentin Chary } 26957d964c9dSCorentin Chary } 26967d964c9dSCorentin Chary } 26977d964c9dSCorentin Chary 26987d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26997d964c9dSCorentin Chary { 27007d964c9dSCorentin Chary VncState *vs; 27017d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 27027d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 27037d964c9dSCorentin Chary int has_dirty = 0; 27047d964c9dSCorentin Chary 27057d964c9dSCorentin Chary y = y / VNC_STAT_RECT * VNC_STAT_RECT; 27067d964c9dSCorentin Chary x = x / VNC_STAT_RECT * VNC_STAT_RECT; 27077d964c9dSCorentin Chary 27087d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2709bc2429b9SCorentin Chary int j; 27107d964c9dSCorentin Chary 27117d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 27127d964c9dSCorentin Chary if (vs->output.offset) { 27137d964c9dSCorentin Chary continue; 27147d964c9dSCorentin Chary } 27157d964c9dSCorentin Chary 27167d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 27177d964c9dSCorentin Chary continue; 27187d964c9dSCorentin Chary } 2719207f328aSCorentin Chary 27207d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 27217d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2722b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2723b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2724b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 27257d964c9dSCorentin Chary } 27267d964c9dSCorentin Chary has_dirty++; 27277d964c9dSCorentin Chary } 2728207f328aSCorentin Chary 27297d964c9dSCorentin Chary return has_dirty; 27307d964c9dSCorentin Chary } 27317d964c9dSCorentin Chary 27327d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2733999342a0SCorentin Chary { 27349f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 27359f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 2736999342a0SCorentin Chary int x, y; 2737999342a0SCorentin Chary struct timeval res; 27387d964c9dSCorentin Chary int has_dirty = 0; 2739999342a0SCorentin Chary 27409f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 27419f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2742999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2743999342a0SCorentin Chary 2744999342a0SCorentin Chary rect->updated = false; 2745999342a0SCorentin Chary } 2746999342a0SCorentin Chary } 2747999342a0SCorentin Chary 2748ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2749999342a0SCorentin Chary 2750999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 27517d964c9dSCorentin Chary return has_dirty; 2752999342a0SCorentin Chary } 2753999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2754999342a0SCorentin Chary 27559f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 27569f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2757999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2758999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2759999342a0SCorentin Chary struct timeval min, max; 2760999342a0SCorentin Chary 2761999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2762999342a0SCorentin Chary continue ; 2763999342a0SCorentin Chary } 2764999342a0SCorentin Chary 2765999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2766ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2767999342a0SCorentin Chary 2768999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2769999342a0SCorentin Chary rect->freq = 0; 27707d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2771999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2772999342a0SCorentin Chary continue ; 2773999342a0SCorentin Chary } 2774999342a0SCorentin Chary 2775999342a0SCorentin Chary min = rect->times[rect->idx]; 2776999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2777ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2778999342a0SCorentin Chary 2779999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2780999342a0SCorentin Chary rect->freq /= count; 2781999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2782999342a0SCorentin Chary } 2783999342a0SCorentin Chary } 27847d964c9dSCorentin Chary return has_dirty; 2785999342a0SCorentin Chary } 2786999342a0SCorentin Chary 2787999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2788999342a0SCorentin Chary { 2789999342a0SCorentin Chary int i, j; 2790999342a0SCorentin Chary double total = 0; 2791999342a0SCorentin Chary int num = 0; 2792999342a0SCorentin Chary 2793999342a0SCorentin Chary x = (x / VNC_STAT_RECT) * VNC_STAT_RECT; 2794999342a0SCorentin Chary y = (y / VNC_STAT_RECT) * VNC_STAT_RECT; 2795999342a0SCorentin Chary 2796999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2797999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2798999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2799999342a0SCorentin Chary num++; 2800999342a0SCorentin Chary } 2801999342a0SCorentin Chary } 2802999342a0SCorentin Chary 2803999342a0SCorentin Chary if (num) { 2804999342a0SCorentin Chary return total / num; 2805999342a0SCorentin Chary } else { 2806999342a0SCorentin Chary return 0; 2807999342a0SCorentin Chary } 2808999342a0SCorentin Chary } 2809999342a0SCorentin Chary 2810999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2811999342a0SCorentin Chary { 2812999342a0SCorentin Chary VncRectStat *rect; 2813999342a0SCorentin Chary 2814999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2815999342a0SCorentin Chary if (rect->updated) { 2816999342a0SCorentin Chary return ; 2817999342a0SCorentin Chary } 2818999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2819999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2820999342a0SCorentin Chary rect->updated = true; 2821999342a0SCorentin Chary } 2822999342a0SCorentin Chary 28233e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 28243e230dd2SCorentin Chary { 2825bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2826bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2827bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2828bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2829eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 283012b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 28313e230dd2SCorentin Chary VncState *vs; 28323e230dd2SCorentin Chary int has_dirty = 0; 28339f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 28343e230dd2SCorentin Chary 283580e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2836999342a0SCorentin Chary 283780e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2838999342a0SCorentin Chary gettimeofday(&tv, NULL); 28397d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 284080e0c8c3SCorentin Chary } 2841999342a0SCorentin Chary 28423e230dd2SCorentin Chary /* 28433e230dd2SCorentin Chary * Walk through the guest dirty map. 28443e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 28453e230dd2SCorentin Chary * Update server dirty map. 28463e230dd2SCorentin Chary */ 2847bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2848eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2849eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2850bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2851bea60dd7SPeter Lieven server_stride); 28529f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 28539f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 28549f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 285512b316d4SPeter Lieven } else { 2856eb8934b0SGerd Hoffmann int guest_bpp = 2857eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 285812b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 285912b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2860eb8934b0SGerd Hoffmann guest_ll = pixman_image_get_width(vd->guest.fb) * ((guest_bpp + 7) / 8); 28619f64916dSGerd Hoffmann } 2862eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 286312b316d4SPeter Lieven 286412b316d4SPeter Lieven for (;;) { 28653e230dd2SCorentin Chary int x; 286612b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 286712b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 286812b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 286912b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 287012b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 287112b316d4SPeter Lieven /* no more dirty bits */ 287212b316d4SPeter Lieven break; 287312b316d4SPeter Lieven } 287412b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 287512b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 287612b316d4SPeter Lieven 287712b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 28783e230dd2SCorentin Chary 28799f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2880bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28819f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28829f64916dSGerd Hoffmann } else { 288312b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28849f64916dSGerd Hoffmann } 288512b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28863e230dd2SCorentin Chary 288712b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 288812b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2889bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 289012b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28913e230dd2SCorentin Chary continue; 2892b4c85ddcSPeter Lieven } 2893eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2894eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2895bea60dd7SPeter Lieven } 2896eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2897bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28983e230dd2SCorentin Chary continue; 2899b4c85ddcSPeter Lieven } 2900bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 290112b316d4SPeter Lieven if (!vd->non_adaptive) { 290212b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 290312b316d4SPeter Lieven y, &tv); 290412b316d4SPeter Lieven } 29053e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 290612b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 29073e230dd2SCorentin Chary } 29083e230dd2SCorentin Chary has_dirty++; 29093e230dd2SCorentin Chary } 291012b316d4SPeter Lieven 291112b316d4SPeter Lieven y++; 29123e230dd2SCorentin Chary } 29139f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 29143e230dd2SCorentin Chary return has_dirty; 29153e230dd2SCorentin Chary } 29163e230dd2SCorentin Chary 29170f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 29183e230dd2SCorentin Chary { 29190f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 29203e230dd2SCorentin Chary VncState *vs, *vn; 29213e230dd2SCorentin Chary int has_dirty, rects = 0; 29223e230dd2SCorentin Chary 29239d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 29249d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 29259d6b2070SChenLiang return; 29269d6b2070SChenLiang } 29279d6b2070SChenLiang 29281d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29293e230dd2SCorentin Chary 2930bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 29310f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2932bd023f95SCorentin Chary return; 2933bd023f95SCorentin Chary } 2934bd023f95SCorentin Chary 29353e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2936bd023f95SCorentin Chary vnc_unlock_display(vd); 29373e230dd2SCorentin Chary 29383e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 293938ee14f4SGerd Hoffmann rects += vnc_update_client(vs, has_dirty, false); 29403e230dd2SCorentin Chary /* vs might be free()ed here */ 29413e230dd2SCorentin Chary } 2942bd023f95SCorentin Chary 29433e230dd2SCorentin Chary if (has_dirty && rects) { 29440f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 29450f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 29460f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 29470f7b2864SGerd Hoffmann } 29483e230dd2SCorentin Chary } else { 29490f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 29500f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 29510f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 29523e230dd2SCorentin Chary } 29533e230dd2SCorentin Chary } 29543e230dd2SCorentin Chary } 29553e230dd2SCorentin Chary 295604d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 29572c8cf549SMichael Tokarev bool skipauth, bool websocket) 29583e230dd2SCorentin Chary { 2959fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 29607d964c9dSCorentin Chary int i; 29617d964c9dSCorentin Chary 296204d2529dSDaniel P. Berrange vs->sioc = sioc; 296304d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 296404d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 296504d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2966d616ccc5SGerd Hoffmann vs->vd = vd; 29677e7e2ebcSDaniel P. Berrange 296804d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 296904d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 297004d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2971543b9580SGerd Hoffmann 297204d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 297304d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 297404d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2975543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 297604d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2977543b9580SGerd Hoffmann #endif 2978543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 297904d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2980543b9580SGerd Hoffmann #endif 298104d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 298204d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 298304d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 298404d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2985543b9580SGerd Hoffmann 29867e7e2ebcSDaniel P. Berrange if (skipauth) { 29877e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29887e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29897e7e2ebcSDaniel P. Berrange } else { 2990f9148c8aSDaniel P. Berrange if (websocket) { 2991f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2992f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2993f9148c8aSDaniel P. Berrange } else { 29947e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29957e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29967e7e2ebcSDaniel P. Berrange } 2997f9148c8aSDaniel P. Berrange } 299804d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 299904d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 30007e7e2ebcSDaniel P. Berrange 30017267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 30027d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 3003fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 30047d964c9dSCorentin Chary } 30053e230dd2SCorentin Chary 300604d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 30070f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 300804d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 30097536ee4bSTim Hardeck if (websocket) { 30107536ee4bSTim Hardeck vs->websocket = 1; 3011f9148c8aSDaniel P. Berrange if (vd->ws_tls) { 301204d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 301304d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 30143e305e4aSDaniel P. Berrange } else { 301504d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 301604d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 30170057a0d5STim Hardeck } 301804d2529dSDaniel P. Berrange } else { 301904d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 302004d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 30217536ee4bSTim Hardeck } 30223e230dd2SCorentin Chary 30233e230dd2SCorentin Chary vnc_client_cache_addr(vs); 3024fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 30258cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 30263e230dd2SCorentin Chary 30278e9b0d24SDaniel P. Berrange if (!vs->websocket) { 30287536ee4bSTim Hardeck vnc_init_state(vs); 30297536ee4bSTim Hardeck } 3030e5f34cddSGerd Hoffmann 3031e5f34cddSGerd Hoffmann if (vd->num_connecting > vd->connections_limit) { 3032e5f34cddSGerd Hoffmann QTAILQ_FOREACH(vs, &vd->clients, next) { 3033e5f34cddSGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 3034e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 3035e5f34cddSGerd Hoffmann return; 3036e5f34cddSGerd Hoffmann } 3037e5f34cddSGerd Hoffmann } 3038e5f34cddSGerd Hoffmann } 30397536ee4bSTim Hardeck } 30407536ee4bSTim Hardeck 30417536ee4bSTim Hardeck void vnc_init_state(VncState *vs) 30427536ee4bSTim Hardeck { 30436fd8e79aSTim Hardeck vs->initialized = true; 30447536ee4bSTim Hardeck VncDisplay *vd = vs->vd; 3045c7628bffSGerd Hoffmann bool first_client = QTAILQ_EMPTY(&vd->clients); 30467536ee4bSTim Hardeck 30473e230dd2SCorentin Chary vs->last_x = -1; 30483e230dd2SCorentin Chary vs->last_y = -1; 30493e230dd2SCorentin Chary 30503e230dd2SCorentin Chary vs->as.freq = 44100; 30513e230dd2SCorentin Chary vs->as.nchannels = 2; 30523e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 30533e230dd2SCorentin Chary vs->as.endianness = 0; 30543e230dd2SCorentin Chary 3055bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 3056175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 3057bd023f95SCorentin Chary 3058e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 3059c7628bffSGerd Hoffmann if (first_client) { 3060c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 3061c7628bffSGerd Hoffmann } 30623e230dd2SCorentin Chary 30631d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 30643e230dd2SCorentin Chary 30653e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 30663e230dd2SCorentin Chary vnc_flush(vs); 30673e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 30683e230dd2SCorentin Chary reset_keys(vs); 30693e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 30703e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 30713e230dd2SCorentin Chary 30723e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 30733e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 30743e230dd2SCorentin Chary 30753e230dd2SCorentin Chary /* vs might be free()ed here */ 30763e230dd2SCorentin Chary } 30773e230dd2SCorentin Chary 307804d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 307904d2529dSDaniel P. Berrange GIOCondition condition, 308004d2529dSDaniel P. Berrange void *opaque) 30813e230dd2SCorentin Chary { 30823e230dd2SCorentin Chary VncDisplay *vs = opaque; 308304d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 308404d2529dSDaniel P. Berrange Error *err = NULL; 30853e230dd2SCorentin Chary 30863e230dd2SCorentin Chary /* Catch-up */ 30871d0d59feSGerd Hoffmann graphic_hw_update(vs->dcl.con); 308804d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 308904d2529dSDaniel P. Berrange if (sioc != NULL) { 309004d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 309104d2529dSDaniel P. Berrange vnc_connect(vs, sioc, false, 309204d2529dSDaniel P. Berrange ioc != QIO_CHANNEL(vs->lsock)); 309304d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30948e9b0d24SDaniel P. Berrange } else { 309504d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 309604d2529dSDaniel P. Berrange error_free(err); 30977536ee4bSTim Hardeck } 30983e230dd2SCorentin Chary 309904d2529dSDaniel P. Berrange return TRUE; 31007536ee4bSTim Hardeck } 31017536ee4bSTim Hardeck 31027c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 31037c20b4a3SGerd Hoffmann .dpy_name = "vnc", 31040f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 31057c20b4a3SGerd Hoffmann .dpy_gfx_copy = vnc_dpy_copy, 31067c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3107c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 310834da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 31097c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 31107c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 31117c20b4a3SGerd Hoffmann }; 31127c20b4a3SGerd Hoffmann 311314f7143eSGerd Hoffmann void vnc_display_init(const char *id) 31143e230dd2SCorentin Chary { 31154db14629SGerd Hoffmann VncDisplay *vs; 31164db14629SGerd Hoffmann 31174db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 31184db14629SGerd Hoffmann return; 31194db14629SGerd Hoffmann } 31204db14629SGerd Hoffmann vs = g_malloc0(sizeof(*vs)); 31213e230dd2SCorentin Chary 312214f7143eSGerd Hoffmann vs->id = strdup(id); 3123d616ccc5SGerd Hoffmann QTAILQ_INSERT_TAIL(&vnc_displays, vs, next); 31243e230dd2SCorentin Chary 31253e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 31263c9405a0SGerd Hoffmann vs->expires = TIME_MAX; 31273e230dd2SCorentin Chary 312840066175SGerd Hoffmann if (keyboard_layout) { 312940066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 31303e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 313140066175SGerd Hoffmann } else { 31323e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 313340066175SGerd Hoffmann } 31343e230dd2SCorentin Chary 31353e230dd2SCorentin Chary if (!vs->kbd_layout) 31363e230dd2SCorentin Chary exit(1); 31373e230dd2SCorentin Chary 3138bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 3139bd023f95SCorentin Chary vnc_start_worker_thread(); 3140bd023f95SCorentin Chary 314121ef45d7SGerd Hoffmann vs->dcl.ops = &dcl_ops; 31425209089fSGerd Hoffmann register_displaychangelistener(&vs->dcl); 31433e230dd2SCorentin Chary } 31443e230dd2SCorentin Chary 31453e230dd2SCorentin Chary 314614f7143eSGerd Hoffmann static void vnc_display_close(VncDisplay *vs) 31473e230dd2SCorentin Chary { 31483e230dd2SCorentin Chary if (!vs) 31493e230dd2SCorentin Chary return; 3150bf7aa45eSGerd Hoffmann vs->enabled = false; 3151bf7aa45eSGerd Hoffmann vs->is_unix = false; 315204d2529dSDaniel P. Berrange if (vs->lsock != NULL) { 315304d2529dSDaniel P. Berrange if (vs->lsock_tag) { 315404d2529dSDaniel P. Berrange g_source_remove(vs->lsock_tag); 315504d2529dSDaniel P. Berrange } 315604d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lsock)); 315704d2529dSDaniel P. Berrange vs->lsock = NULL; 31583e230dd2SCorentin Chary } 3159bf7aa45eSGerd Hoffmann vs->ws_enabled = false; 316004d2529dSDaniel P. Berrange if (vs->lwebsock != NULL) { 316104d2529dSDaniel P. Berrange if (vs->lwebsock_tag) { 316204d2529dSDaniel P. Berrange g_source_remove(vs->lwebsock_tag); 316304d2529dSDaniel P. Berrange } 316404d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lwebsock)); 316504d2529dSDaniel P. Berrange vs->lwebsock = NULL; 31667536ee4bSTim Hardeck } 31673e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 31683e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31693e305e4aSDaniel P. Berrange if (vs->tlscreds) { 31703e305e4aSDaniel P. Berrange object_unparent(OBJECT(vs->tlscreds)); 317167c4c2bdSWolfgang Bumiller vs->tlscreds = NULL; 31723e305e4aSDaniel P. Berrange } 31733e305e4aSDaniel P. Berrange g_free(vs->tlsaclname); 31743e305e4aSDaniel P. Berrange vs->tlsaclname = NULL; 31753e230dd2SCorentin Chary } 31763e230dd2SCorentin Chary 317714f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31781cd20f8bSAnthony Liguori { 317914f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 31801cd20f8bSAnthony Liguori 31811cd20f8bSAnthony Liguori if (!vs) { 3182a6aa9d3eSLuiz Capitulino return -EINVAL; 31831cd20f8bSAnthony Liguori } 3184cf864569SGerd Hoffmann if (vs->auth == VNC_AUTH_NONE) { 3185cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 3186cf864569SGerd Hoffmann "password auth using '-vnc ${dpy},password'."); 3187cf864569SGerd Hoffmann return -EINVAL; 31881cd20f8bSAnthony Liguori } 31891cd20f8bSAnthony Liguori 31907267c094SAnthony Liguori g_free(vs->password); 31917267c094SAnthony Liguori vs->password = g_strdup(password); 3192a6aa9d3eSLuiz Capitulino 3193a6aa9d3eSLuiz Capitulino return 0; 31943e230dd2SCorentin Chary } 31953e230dd2SCorentin Chary 319614f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31973c9405a0SGerd Hoffmann { 319814f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 31993c9405a0SGerd Hoffmann 32001643f2b2SGerd Hoffmann if (!vs) { 32011643f2b2SGerd Hoffmann return -EINVAL; 32021643f2b2SGerd Hoffmann } 32031643f2b2SGerd Hoffmann 32043c9405a0SGerd Hoffmann vs->expires = expires; 32053c9405a0SGerd Hoffmann return 0; 32063c9405a0SGerd Hoffmann } 32073c9405a0SGerd Hoffmann 3208*33df7bf3SPaolo Bonzini static void vnc_display_print_local_addr(VncDisplay *vs) 32093e230dd2SCorentin Chary { 321004d2529dSDaniel P. Berrange SocketAddress *addr; 321104d2529dSDaniel P. Berrange Error *err = NULL; 32123e230dd2SCorentin Chary 321304d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(vs->lsock, &err); 321404d2529dSDaniel P. Berrange if (!addr) { 3215*33df7bf3SPaolo Bonzini return; 321604d2529dSDaniel P. Berrange } 321704d2529dSDaniel P. Berrange 321804d2529dSDaniel P. Berrange if (addr->type != SOCKET_ADDRESS_KIND_INET) { 321904d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 3220*33df7bf3SPaolo Bonzini return; 322104d2529dSDaniel P. Berrange } 3222*33df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3223*33df7bf3SPaolo Bonzini addr->u.inet.data->host, 322432bafa8fSEric Blake addr->u.inet.data->port); 322504d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 32263e230dd2SCorentin Chary } 32273e230dd2SCorentin Chary 32284db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 32294db14629SGerd Hoffmann .name = "vnc", 32304db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 32314db14629SGerd Hoffmann .implied_opt_name = "vnc", 32324db14629SGerd Hoffmann .desc = { 32334db14629SGerd Hoffmann { 32344db14629SGerd Hoffmann .name = "vnc", 32354db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32364db14629SGerd Hoffmann },{ 32374db14629SGerd Hoffmann .name = "websocket", 32384db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32394db14629SGerd Hoffmann },{ 32403e305e4aSDaniel P. Berrange .name = "tls-creds", 32413e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 32423e305e4aSDaniel P. Berrange },{ 32433e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32444db14629SGerd Hoffmann .name = "x509", 32454db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32464db14629SGerd Hoffmann },{ 32474db14629SGerd Hoffmann .name = "share", 32484db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32494db14629SGerd Hoffmann },{ 32501d0d59feSGerd Hoffmann .name = "display", 32511d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 32521d0d59feSGerd Hoffmann },{ 32531d0d59feSGerd Hoffmann .name = "head", 32541d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32551d0d59feSGerd Hoffmann },{ 3256e5f34cddSGerd Hoffmann .name = "connections", 3257e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3258e5f34cddSGerd Hoffmann },{ 325988428b7aSGonglei .name = "to", 326088428b7aSGonglei .type = QEMU_OPT_NUMBER, 326188428b7aSGonglei },{ 326288428b7aSGonglei .name = "ipv4", 326388428b7aSGonglei .type = QEMU_OPT_BOOL, 326488428b7aSGonglei },{ 326588428b7aSGonglei .name = "ipv6", 326688428b7aSGonglei .type = QEMU_OPT_BOOL, 326788428b7aSGonglei },{ 32684db14629SGerd Hoffmann .name = "password", 32694db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32704db14629SGerd Hoffmann },{ 32714db14629SGerd Hoffmann .name = "reverse", 32724db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32734db14629SGerd Hoffmann },{ 32744db14629SGerd Hoffmann .name = "lock-key-sync", 32754db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32764db14629SGerd Hoffmann },{ 3277c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3278c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3279c5ce8333SGerd Hoffmann },{ 32804db14629SGerd Hoffmann .name = "sasl", 32814db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32824db14629SGerd Hoffmann },{ 32833e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32844db14629SGerd Hoffmann .name = "tls", 32854db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32864db14629SGerd Hoffmann },{ 32873e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32884db14629SGerd Hoffmann .name = "x509verify", 32898c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32904db14629SGerd Hoffmann },{ 32914db14629SGerd Hoffmann .name = "acl", 32924db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32934db14629SGerd Hoffmann },{ 32944db14629SGerd Hoffmann .name = "lossy", 32954db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32964db14629SGerd Hoffmann },{ 32974db14629SGerd Hoffmann .name = "non-adaptive", 32984db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32994db14629SGerd Hoffmann }, 33004db14629SGerd Hoffmann { /* end of list */ } 33014db14629SGerd Hoffmann }, 33024db14629SGerd Hoffmann }; 33034db14629SGerd Hoffmann 33040dd72e15SDaniel P. Berrange 33053e305e4aSDaniel P. Berrange static int 33060dd72e15SDaniel P. Berrange vnc_display_setup_auth(VncDisplay *vs, 33070dd72e15SDaniel P. Berrange bool password, 33080dd72e15SDaniel P. Berrange bool sasl, 33093e305e4aSDaniel P. Berrange bool websocket, 33103e305e4aSDaniel P. Berrange Error **errp) 33110dd72e15SDaniel P. Berrange { 33120dd72e15SDaniel P. Berrange /* 33130dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 33140dd72e15SDaniel P. Berrange * 33150dd72e15SDaniel P. Berrange * 1. none 33160dd72e15SDaniel P. Berrange * 2. vnc 33170dd72e15SDaniel P. Berrange * 3. sasl 33180dd72e15SDaniel P. Berrange * 33190dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 33200dd72e15SDaniel P. Berrange * 33210dd72e15SDaniel P. Berrange * 1. clear 33220dd72e15SDaniel P. Berrange * 2. tls 33230dd72e15SDaniel P. Berrange * 33240dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 33250dd72e15SDaniel P. Berrange * 33260dd72e15SDaniel P. Berrange * 1. anon 33270dd72e15SDaniel P. Berrange * 2. x509 33280dd72e15SDaniel P. Berrange * 33290dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 33300dd72e15SDaniel P. Berrange * 33310dd72e15SDaniel P. Berrange * 1. clear + none 33320dd72e15SDaniel P. Berrange * 2. clear + vnc 33330dd72e15SDaniel P. Berrange * 3. clear + sasl 33340dd72e15SDaniel P. Berrange * 4. tls + anon + none 33350dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 33360dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 33370dd72e15SDaniel P. Berrange * 7. tls + x509 + none 33380dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 33390dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 33400dd72e15SDaniel P. Berrange * 33410dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 33420dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 33430dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 33440dd72e15SDaniel P. Berrange * sub-auth types. 3345f9148c8aSDaniel P. Berrange * 3346f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3347f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3348f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3349f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3350f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3351f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3352f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3353f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3354f9148c8aSDaniel P. Berrange * 3355f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3356f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3357f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33580dd72e15SDaniel P. Berrange */ 33590dd72e15SDaniel P. Berrange if (password) { 33603e305e4aSDaniel P. Berrange if (vs->tlscreds) { 33610dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3362f9148c8aSDaniel P. Berrange if (websocket) { 3363f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3364f9148c8aSDaniel P. Berrange } 33653e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 33663e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 33670dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 33680dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 33693e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 33703e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 33710dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 33720dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 33733e305e4aSDaniel P. Berrange } else { 33743e305e4aSDaniel P. Berrange error_setg(errp, 33753e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 33763e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 33773e305e4aSDaniel P. Berrange return -1; 33780dd72e15SDaniel P. Berrange } 33790dd72e15SDaniel P. Berrange } else { 33800dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 33810dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VNC; 33820dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 33830dd72e15SDaniel P. Berrange } 3384f9148c8aSDaniel P. Berrange if (websocket) { 3385f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_VNC; 3386f9148c8aSDaniel P. Berrange } else { 3387f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3388f9148c8aSDaniel P. Berrange } 33890dd72e15SDaniel P. Berrange } else if (sasl) { 33903e305e4aSDaniel P. Berrange if (vs->tlscreds) { 33910dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3392f9148c8aSDaniel P. Berrange if (websocket) { 3393f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3394f9148c8aSDaniel P. Berrange } 33953e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 33963e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 33970dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 33980dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 33993e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 34003e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 34010dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 34020dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 34033e305e4aSDaniel P. Berrange } else { 34043e305e4aSDaniel P. Berrange error_setg(errp, 34053e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 34063e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 34073e305e4aSDaniel P. Berrange return -1; 34080dd72e15SDaniel P. Berrange } 34090dd72e15SDaniel P. Berrange } else { 34100dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 34110dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_SASL; 34120dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 34130dd72e15SDaniel P. Berrange } 3414f9148c8aSDaniel P. Berrange if (websocket) { 3415f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_SASL; 3416f9148c8aSDaniel P. Berrange } else { 3417f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3418f9148c8aSDaniel P. Berrange } 34190dd72e15SDaniel P. Berrange } else { 34203e305e4aSDaniel P. Berrange if (vs->tlscreds) { 34210dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3422f9148c8aSDaniel P. Berrange if (websocket) { 3423f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3424f9148c8aSDaniel P. Berrange } 34253e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 34263e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 34270dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 34280dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 34293e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 34303e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 34310dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 34320dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 34333e305e4aSDaniel P. Berrange } else { 34343e305e4aSDaniel P. Berrange error_setg(errp, 34353e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 34363e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 34373e305e4aSDaniel P. Berrange return -1; 34380dd72e15SDaniel P. Berrange } 34390dd72e15SDaniel P. Berrange } else { 34400dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 34410dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 34420dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 34430dd72e15SDaniel P. Berrange } 3444f9148c8aSDaniel P. Berrange if (websocket) { 3445f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_NONE; 3446f9148c8aSDaniel P. Berrange } else { 3447f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3448f9148c8aSDaniel P. Berrange } 34490dd72e15SDaniel P. Berrange } 34503e305e4aSDaniel P. Berrange return 0; 34510dd72e15SDaniel P. Berrange } 34520dd72e15SDaniel P. Berrange 34533e305e4aSDaniel P. Berrange 34543e305e4aSDaniel P. Berrange /* 34553e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 34563e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 34573e305e4aSDaniel P. Berrange */ 34583e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 34593e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 34603e305e4aSDaniel P. Berrange bool x509verify, 34613e305e4aSDaniel P. Berrange const char *dir, 34623e305e4aSDaniel P. Berrange const char *id, 34633e305e4aSDaniel P. Berrange Error **errp) 34643e305e4aSDaniel P. Berrange { 34653e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 34663e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 34673e305e4aSDaniel P. Berrange Object *creds; 34683e305e4aSDaniel P. Berrange Error *err = NULL; 34693e305e4aSDaniel P. Berrange 34703e305e4aSDaniel P. Berrange if (x509) { 34713e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 34723e305e4aSDaniel P. Berrange parent, 34733e305e4aSDaniel P. Berrange credsid, 34743e305e4aSDaniel P. Berrange &err, 34753e305e4aSDaniel P. Berrange "endpoint", "server", 34763e305e4aSDaniel P. Berrange "dir", dir, 34773e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 34783e305e4aSDaniel P. Berrange NULL); 34793e305e4aSDaniel P. Berrange } else { 34803e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 34813e305e4aSDaniel P. Berrange parent, 34823e305e4aSDaniel P. Berrange credsid, 34833e305e4aSDaniel P. Berrange &err, 34843e305e4aSDaniel P. Berrange "endpoint", "server", 34853e305e4aSDaniel P. Berrange NULL); 34863e305e4aSDaniel P. Berrange } 34873e305e4aSDaniel P. Berrange 34883e305e4aSDaniel P. Berrange g_free(credsid); 34893e305e4aSDaniel P. Berrange 34903e305e4aSDaniel P. Berrange if (err) { 34913e305e4aSDaniel P. Berrange error_propagate(errp, err); 34923e305e4aSDaniel P. Berrange return NULL; 34933e305e4aSDaniel P. Berrange } 34943e305e4aSDaniel P. Berrange 34953e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34963e305e4aSDaniel P. Berrange } 34973e305e4aSDaniel P. Berrange 34983e305e4aSDaniel P. Berrange 34994db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 35003e230dd2SCorentin Chary { 350114f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 35024db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3503e0d03b8cSDaniel P. Berrange SocketAddress *saddr = NULL, *wsaddr = NULL; 3504e2a11d9dSGonglei const char *share, *device_id; 35051d0d59feSGerd Hoffmann QemuConsole *con; 3506a2c72de0SGonglei bool password = false; 3507a2c72de0SGonglei bool reverse = false; 3508e2a11d9dSGonglei const char *vnc; 3509e5560329SGerd Hoffmann char *h; 35103e305e4aSDaniel P. Berrange const char *credid; 3511*33df7bf3SPaolo Bonzini int show_vnc_port = 0; 3512a2c72de0SGonglei bool sasl = false; 3513d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 35143e230dd2SCorentin Chary int saslErr; 35153e230dd2SCorentin Chary #endif 35163e230dd2SCorentin Chary int acl = 0; 35173e230dd2SCorentin Chary int lock_key_sync = 1; 3518c5ce8333SGerd Hoffmann int key_delay_ms; 35193e230dd2SCorentin Chary 3520d616ccc5SGerd Hoffmann if (!vs) { 35212d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 35222d55f0e8SPaolo Bonzini return; 35232d55f0e8SPaolo Bonzini } 352414f7143eSGerd Hoffmann vnc_display_close(vs); 35254db14629SGerd Hoffmann 35264db14629SGerd Hoffmann if (!opts) { 35272d55f0e8SPaolo Bonzini return; 35284db14629SGerd Hoffmann } 3529e2a11d9dSGonglei vnc = qemu_opt_get(opts, "vnc"); 3530e2a11d9dSGonglei if (!vnc || strcmp(vnc, "none") == 0) { 35314db14629SGerd Hoffmann return; 35324db14629SGerd Hoffmann } 3533e2a11d9dSGonglei 3534e5560329SGerd Hoffmann h = strrchr(vnc, ':'); 3535e5560329SGerd Hoffmann if (h) { 3536274c3b52SJán Tomko size_t hlen = h - vnc; 3537274c3b52SJán Tomko 3538e0d03b8cSDaniel P. Berrange const char *websocket = qemu_opt_get(opts, "websocket"); 3539e0d03b8cSDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 35403f0230e9SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 35413f0230e9SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 35423f0230e9SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 35433f0230e9SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3544e0d03b8cSDaniel P. Berrange 3545e0d03b8cSDaniel P. Berrange saddr = g_new0(SocketAddress, 1); 3546e0d03b8cSDaniel P. Berrange if (websocket) { 3547e0d03b8cSDaniel P. Berrange if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3548e0d03b8cSDaniel P. Berrange error_setg(errp, 3549e0d03b8cSDaniel P. Berrange "SHA1 hash support is required for websockets"); 3550e5560329SGerd Hoffmann goto fail; 3551e2a11d9dSGonglei } 3552e5560329SGerd Hoffmann 3553e0d03b8cSDaniel P. Berrange wsaddr = g_new0(SocketAddress, 1); 3554e0d03b8cSDaniel P. Berrange vs->ws_enabled = true; 3555e5560329SGerd Hoffmann } 3556e0d03b8cSDaniel P. Berrange 3557e0d03b8cSDaniel P. Berrange if (strncmp(vnc, "unix:", 5) == 0) { 35582d32addaSEric Blake saddr->type = SOCKET_ADDRESS_KIND_UNIX; 355932bafa8fSEric Blake saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1); 356032bafa8fSEric Blake saddr->u.q_unix.data->path = g_strdup(vnc + 5); 3561e0d03b8cSDaniel P. Berrange 3562e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 3563e0d03b8cSDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3564e0d03b8cSDaniel P. Berrange goto fail; 3565e5560329SGerd Hoffmann } 3566e0d03b8cSDaniel P. Berrange } else { 3567e0d03b8cSDaniel P. Berrange unsigned long long baseport; 35680399293eSEric Blake InetSocketAddress *inet; 35692d32addaSEric Blake saddr->type = SOCKET_ADDRESS_KIND_INET; 357032bafa8fSEric Blake inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1); 3571e0d03b8cSDaniel P. Berrange if (vnc[0] == '[' && vnc[hlen - 1] == ']') { 35720399293eSEric Blake inet->host = g_strndup(vnc + 1, hlen - 2); 3573e0d03b8cSDaniel P. Berrange } else { 35740399293eSEric Blake inet->host = g_strndup(vnc, hlen); 3575e0d03b8cSDaniel P. Berrange } 3576e0d03b8cSDaniel P. Berrange if (parse_uint_full(h + 1, &baseport, 10) < 0) { 3577e0d03b8cSDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", h + 1); 3578e0d03b8cSDaniel P. Berrange goto fail; 3579e0d03b8cSDaniel P. Berrange } 3580e0d03b8cSDaniel P. Berrange if (baseport > 65535 || 3581e0d03b8cSDaniel P. Berrange baseport + 5900 > 65535) { 3582e0d03b8cSDaniel P. Berrange error_setg(errp, "port %s out of range", h + 1); 3583e0d03b8cSDaniel P. Berrange goto fail; 3584e0d03b8cSDaniel P. Berrange } 35850399293eSEric Blake inet->port = g_strdup_printf( 3586e0d03b8cSDaniel P. Berrange "%d", (int)baseport + 5900); 3587e0d03b8cSDaniel P. Berrange 3588e0d03b8cSDaniel P. Berrange if (to) { 35890399293eSEric Blake inet->has_to = true; 35900399293eSEric Blake inet->to = to + 5900; 3591*33df7bf3SPaolo Bonzini show_vnc_port = 1; 3592e0d03b8cSDaniel P. Berrange } 35930399293eSEric Blake inet->ipv4 = ipv4; 35940399293eSEric Blake inet->has_ipv4 = has_ipv4; 35950399293eSEric Blake inet->ipv6 = ipv6; 35960399293eSEric Blake inet->has_ipv6 = has_ipv6; 3597e0d03b8cSDaniel P. Berrange 3598e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 35992d32addaSEric Blake wsaddr->type = SOCKET_ADDRESS_KIND_INET; 360032bafa8fSEric Blake inet = wsaddr->u.inet.data = g_new0(InetSocketAddress, 1); 360132bafa8fSEric Blake inet->host = g_strdup(saddr->u.inet.data->host); 36020399293eSEric Blake inet->port = g_strdup(websocket); 3603e0d03b8cSDaniel P. Berrange 3604e0d03b8cSDaniel P. Berrange if (to) { 36050399293eSEric Blake inet->has_to = true; 36060399293eSEric Blake inet->to = to; 3607e0d03b8cSDaniel P. Berrange } 36080399293eSEric Blake inet->ipv4 = ipv4; 36090399293eSEric Blake inet->has_ipv4 = has_ipv4; 36100399293eSEric Blake inet->ipv6 = ipv6; 36110399293eSEric Blake inet->has_ipv6 = has_ipv6; 3612e0d03b8cSDaniel P. Berrange } 3613e0d03b8cSDaniel P. Berrange } 3614e0d03b8cSDaniel P. Berrange } else { 3615e0d03b8cSDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3616e0d03b8cSDaniel P. Berrange goto fail; 3617e5560329SGerd Hoffmann } 36183e230dd2SCorentin Chary 36194db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3620800567a6SDaniel P. Berrange if (password) { 3621800567a6SDaniel P. Berrange if (fips_get_state()) { 36222d55f0e8SPaolo Bonzini error_setg(errp, 36230f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 36240f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 36252d55f0e8SPaolo Bonzini "methods as an alternative"); 36261ce52c78SPaolo Bonzini goto fail; 36270f66998fSPaul Moore } 3628800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3629800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB)) { 3630800567a6SDaniel P. Berrange error_setg(errp, 3631800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3632800567a6SDaniel P. Berrange goto fail; 3633800567a6SDaniel P. Berrange } 3634800567a6SDaniel P. Berrange } 36354db14629SGerd Hoffmann 36364db14629SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 36374db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3638c5ce8333SGerd Hoffmann key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 1); 36394db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3640d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3641d169f04bSDaniel P. Berrange if (sasl) { 3642d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3643d169f04bSDaniel P. Berrange goto fail; 3644d169f04bSDaniel P. Berrange } 3645d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 36463e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 36473e305e4aSDaniel P. Berrange if (credid) { 36483e305e4aSDaniel P. Berrange Object *creds; 36493e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 36503e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 36513e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 36523e305e4aSDaniel P. Berrange error_setg(errp, 3653c62e90afSWolfgang Bumiller "'tls-creds' parameter is mutually exclusive with " 36543e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 36553e305e4aSDaniel P. Berrange goto fail; 36563e305e4aSDaniel P. Berrange } 36573e305e4aSDaniel P. Berrange 36583e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 36593e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 36603e305e4aSDaniel P. Berrange if (!creds) { 36613e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 36623e305e4aSDaniel P. Berrange credid); 36633e305e4aSDaniel P. Berrange goto fail; 36643e305e4aSDaniel P. Berrange } 36653e305e4aSDaniel P. Berrange vs->tlscreds = (QCryptoTLSCreds *) 36663e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 36673e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 36683e305e4aSDaniel P. Berrange if (!vs->tlscreds) { 36693e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 36703e305e4aSDaniel P. Berrange credid); 36713e305e4aSDaniel P. Berrange goto fail; 36723e305e4aSDaniel P. Berrange } 36733e305e4aSDaniel P. Berrange object_ref(OBJECT(vs->tlscreds)); 36743e305e4aSDaniel P. Berrange 36753e305e4aSDaniel P. Berrange if (vs->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 36763e305e4aSDaniel P. Berrange error_setg(errp, 36773e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 36783e305e4aSDaniel P. Berrange goto fail; 36793e305e4aSDaniel P. Berrange } 36803e305e4aSDaniel P. Berrange } else { 36813e305e4aSDaniel P. Berrange const char *path; 36823e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 36834db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 36843e305e4aSDaniel P. Berrange if (tls) { 36854db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 36863e305e4aSDaniel P. Berrange 36874db14629SGerd Hoffmann if (path) { 3688a2c72de0SGonglei x509 = true; 36893e305e4aSDaniel P. Berrange } else { 36903e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 36913e305e4aSDaniel P. Berrange if (path) { 36923e305e4aSDaniel P. Berrange x509 = true; 36933e305e4aSDaniel P. Berrange x509verify = true; 36943e305e4aSDaniel P. Berrange } 36953e305e4aSDaniel P. Berrange } 36963e305e4aSDaniel P. Berrange vs->tlscreds = vnc_display_create_creds(x509, 36973e305e4aSDaniel P. Berrange x509verify, 36983e305e4aSDaniel P. Berrange path, 36993e305e4aSDaniel P. Berrange vs->id, 37003e305e4aSDaniel P. Berrange errp); 37013e305e4aSDaniel P. Berrange if (!vs->tlscreds) { 37021ce52c78SPaolo Bonzini goto fail; 37033e230dd2SCorentin Chary } 37043e230dd2SCorentin Chary } 3705d169f04bSDaniel P. Berrange } 37064db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 37074db14629SGerd Hoffmann 37084db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 37094db14629SGerd Hoffmann if (share) { 37104db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 37118cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_IGNORE; 37124db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 37138cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 37144db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 37158cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 37168cf36489SGerd Hoffmann } else { 37172d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 37181ce52c78SPaolo Bonzini goto fail; 37198cf36489SGerd Hoffmann } 37204db14629SGerd Hoffmann } else { 37214db14629SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 37223e230dd2SCorentin Chary } 3723e5f34cddSGerd Hoffmann vs->connections_limit = qemu_opt_get_number(opts, "connections", 32); 37243e230dd2SCorentin Chary 37254db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 37264db14629SGerd Hoffmann vs->lossy = qemu_opt_get_bool(opts, "lossy", false); 37274db14629SGerd Hoffmann #endif 37284db14629SGerd Hoffmann vs->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3729e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3730e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3731e22492d3SPeter Lieven * calculations otherwise */ 3732e22492d3SPeter Lieven if (!vs->lossy) { 3733e22492d3SPeter Lieven vs->non_adaptive = true; 3734e22492d3SPeter Lieven } 3735e22492d3SPeter Lieven 37363e305e4aSDaniel P. Berrange if (acl) { 3737c8496408SGerd Hoffmann if (strcmp(vs->id, "default") == 0) { 37383e305e4aSDaniel P. Berrange vs->tlsaclname = g_strdup("vnc.x509dname"); 3739c8496408SGerd Hoffmann } else { 37403e305e4aSDaniel P. Berrange vs->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vs->id); 3741c8496408SGerd Hoffmann } 37423e305e4aSDaniel P. Berrange qemu_acl_init(vs->tlsaclname); 37433e230dd2SCorentin Chary } 37443e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 37453e230dd2SCorentin Chary if (acl && sasl) { 3746c8496408SGerd Hoffmann char *aclname; 3747c8496408SGerd Hoffmann 3748c8496408SGerd Hoffmann if (strcmp(vs->id, "default") == 0) { 3749c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3750c8496408SGerd Hoffmann } else { 3751c8496408SGerd Hoffmann aclname = g_strdup_printf("vnc.%s.username", vs->id); 3752c8496408SGerd Hoffmann } 3753c8496408SGerd Hoffmann vs->sasl.acl = qemu_acl_init(aclname); 3754c8496408SGerd Hoffmann g_free(aclname); 37553e230dd2SCorentin Chary } 37563e230dd2SCorentin Chary #endif 37573e230dd2SCorentin Chary 3758e0d03b8cSDaniel P. Berrange if (vnc_display_setup_auth(vs, password, sasl, vs->ws_enabled, errp) < 0) { 37593e305e4aSDaniel P. Berrange goto fail; 37603e305e4aSDaniel P. Berrange } 37613e230dd2SCorentin Chary 37623e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 37633e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 37642d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 37653e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 37661ce52c78SPaolo Bonzini goto fail; 37673e230dd2SCorentin Chary } 37683e230dd2SCorentin Chary #endif 37693e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 3770c5ce8333SGerd Hoffmann vs->key_delay_ms = key_delay_ms; 37713e230dd2SCorentin Chary 37721d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 37731d0d59feSGerd Hoffmann if (device_id) { 37741d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3775f2c1d54cSGerd Hoffmann Error *err = NULL; 37761d0d59feSGerd Hoffmann 3777f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3778f2c1d54cSGerd Hoffmann if (err) { 3779f2c1d54cSGerd Hoffmann error_propagate(errp, err); 37801d0d59feSGerd Hoffmann goto fail; 37811d0d59feSGerd Hoffmann } 37821d0d59feSGerd Hoffmann } else { 37831d0d59feSGerd Hoffmann con = NULL; 37841d0d59feSGerd Hoffmann } 37851d0d59feSGerd Hoffmann 37861d0d59feSGerd Hoffmann if (con != vs->dcl.con) { 37871d0d59feSGerd Hoffmann unregister_displaychangelistener(&vs->dcl); 37881d0d59feSGerd Hoffmann vs->dcl.con = con; 37891d0d59feSGerd Hoffmann register_displaychangelistener(&vs->dcl); 37901d0d59feSGerd Hoffmann } 37911d0d59feSGerd Hoffmann 37923e230dd2SCorentin Chary if (reverse) { 37933e230dd2SCorentin Chary /* connect to viewer */ 379404d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 379504d2529dSDaniel P. Berrange vs->lsock = NULL; 379604d2529dSDaniel P. Berrange vs->lwebsock = NULL; 3797e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 3798e0d03b8cSDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 3799e0d03b8cSDaniel P. Berrange goto fail; 38003e230dd2SCorentin Chary } 380104d2529dSDaniel P. Berrange vs->is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; 380204d2529dSDaniel P. Berrange sioc = qio_channel_socket_new(); 380304d2529dSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr, errp) < 0) { 3804007fcd3eSPaolo Bonzini goto fail; 3805007fcd3eSPaolo Bonzini } 380604d2529dSDaniel P. Berrange vnc_connect(vs, sioc, false, false); 380704d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 38083e230dd2SCorentin Chary } else { 380904d2529dSDaniel P. Berrange vs->lsock = qio_channel_socket_new(); 381004d2529dSDaniel P. Berrange if (qio_channel_socket_listen_sync(vs->lsock, saddr, errp) < 0) { 38113d00ac1aSCole Robinson goto fail; 38123d00ac1aSCole Robinson } 38132d32addaSEric Blake vs->is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; 381404d2529dSDaniel P. Berrange vs->enabled = true; 381504d2529dSDaniel P. Berrange 3816bf7aa45eSGerd Hoffmann if (vs->ws_enabled) { 381704d2529dSDaniel P. Berrange vs->lwebsock = qio_channel_socket_new(); 381804d2529dSDaniel P. Berrange if (qio_channel_socket_listen_sync(vs->lwebsock, 381904d2529dSDaniel P. Berrange wsaddr, errp) < 0) { 382004d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lsock)); 382104d2529dSDaniel P. Berrange vs->lsock = NULL; 38227536ee4bSTim Hardeck goto fail; 38237536ee4bSTim Hardeck } 38247536ee4bSTim Hardeck } 382504d2529dSDaniel P. Berrange 382604d2529dSDaniel P. Berrange vs->lsock_tag = qio_channel_add_watch( 382704d2529dSDaniel P. Berrange QIO_CHANNEL(vs->lsock), 382804d2529dSDaniel P. Berrange G_IO_IN, vnc_listen_io, vs, NULL); 3829bf7aa45eSGerd Hoffmann if (vs->ws_enabled) { 383004d2529dSDaniel P. Berrange vs->lwebsock_tag = qio_channel_add_watch( 383104d2529dSDaniel P. Berrange QIO_CHANNEL(vs->lwebsock), 383204d2529dSDaniel P. Berrange G_IO_IN, vnc_listen_io, vs, NULL); 38337536ee4bSTim Hardeck } 38343e230dd2SCorentin Chary } 3835e0d03b8cSDaniel P. Berrange 3836*33df7bf3SPaolo Bonzini if (show_vnc_port) { 3837*33df7bf3SPaolo Bonzini vnc_display_print_local_addr(vs); 3838*33df7bf3SPaolo Bonzini } 3839*33df7bf3SPaolo Bonzini 3840e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(saddr); 3841e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(wsaddr); 38422d55f0e8SPaolo Bonzini return; 38431ce52c78SPaolo Bonzini 38441ce52c78SPaolo Bonzini fail: 3845e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(saddr); 3846e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(wsaddr); 3847bf7aa45eSGerd Hoffmann vs->enabled = false; 3848bf7aa45eSGerd Hoffmann vs->ws_enabled = false; 38493e230dd2SCorentin Chary } 385013661089SDaniel P. Berrange 385114f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 385213661089SDaniel P. Berrange { 385314f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 385404d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 385513661089SDaniel P. Berrange 3856d616ccc5SGerd Hoffmann if (!vs) { 3857d616ccc5SGerd Hoffmann return; 3858d616ccc5SGerd Hoffmann } 385904d2529dSDaniel P. Berrange 386004d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 386104d2529dSDaniel P. Berrange if (sioc) { 386204d2529dSDaniel P. Berrange vnc_connect(vs, sioc, skipauth, false); 386304d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 386404d2529dSDaniel P. Berrange } 386513661089SDaniel P. Berrange } 38664db14629SGerd Hoffmann 38679634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 38682779672fSGonglei { 38692779672fSGonglei int i = 2; 38702779672fSGonglei char *id; 38712779672fSGonglei 38722779672fSGonglei id = g_strdup("default"); 38732779672fSGonglei while (qemu_opts_find(olist, id)) { 38742779672fSGonglei g_free(id); 38752779672fSGonglei id = g_strdup_printf("vnc%d", i++); 38762779672fSGonglei } 38772779672fSGonglei qemu_opts_set_id(opts, id); 38782779672fSGonglei } 38792779672fSGonglei 388070b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 38814db14629SGerd Hoffmann { 38824db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 388370b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 388481607cbfSGonglei const char *id; 38854db14629SGerd Hoffmann 388681607cbfSGonglei if (!opts) { 388781607cbfSGonglei return NULL; 388881607cbfSGonglei } 388981607cbfSGonglei 389081607cbfSGonglei id = qemu_opts_id(opts); 38914db14629SGerd Hoffmann if (!id) { 38924db14629SGerd Hoffmann /* auto-assign id if not present */ 38932779672fSGonglei vnc_auto_assign_id(olist, opts); 38949634f4e3SGerd Hoffmann } 38959634f4e3SGerd Hoffmann return opts; 38964db14629SGerd Hoffmann } 38974db14629SGerd Hoffmann 389828d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 38999634f4e3SGerd Hoffmann { 39009634f4e3SGerd Hoffmann Error *local_err = NULL; 39019634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 39029634f4e3SGerd Hoffmann 39039634f4e3SGerd Hoffmann assert(id); 39044db14629SGerd Hoffmann vnc_display_init(id); 39054db14629SGerd Hoffmann vnc_display_open(id, &local_err); 39064db14629SGerd Hoffmann if (local_err != NULL) { 3907c29b77f9SMarkus Armbruster error_reportf_err(local_err, "Failed to start VNC server: "); 39084db14629SGerd Hoffmann exit(1); 39094db14629SGerd Hoffmann } 39104db14629SGerd Hoffmann return 0; 39114db14629SGerd Hoffmann } 39124db14629SGerd Hoffmann 39134db14629SGerd Hoffmann static void vnc_register_config(void) 39144db14629SGerd Hoffmann { 39154db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 39164db14629SGerd Hoffmann } 391734294e2fSEduardo Habkost opts_init(vnc_register_config); 3918