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" 319c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 32d49b6836SMarkus Armbruster #include "qemu/error-report.h" 331de7afc9SPaolo Bonzini #include "qemu/sockets.h" 341de7afc9SPaolo Bonzini #include "qemu/timer.h" 351de7afc9SPaolo Bonzini #include "qemu/acl.h" 364db14629SGerd Hoffmann #include "qemu/config-file.h" 37cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 387b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 392b54aa87SLuiz Capitulino #include "qmp-commands.h" 408d447d10SGerd Hoffmann #include "ui/input.h" 41fb6ba0d5SWenchao Xia #include "qapi-event.h" 428e9b0d24SDaniel P. Berrange #include "crypto/hash.h" 433e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h" 443e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h" 453e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h" 46f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 4757a6d6d5SDaniel P. Berrange #include "io/dns-resolver.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 110bd269ebcSMarkus Armbruster static void vnc_init_basic_info(SocketAddress *addr, 11198481bfcSEric Blake VncBasicInfo *info, 11298481bfcSEric Blake Error **errp) 1133e230dd2SCorentin Chary { 11404d2529dSDaniel P. Berrange switch (addr->type) { 115bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 116bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 117bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 118bd269ebcSMarkus Armbruster if (addr->u.inet.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 125bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 12604d2529dSDaniel P. Berrange info->host = g_strdup(""); 127bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 12804d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 12904d2529dSDaniel P. Berrange break; 13004d2529dSDaniel P. Berrange 131bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 132bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 133a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 134977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 13504d2529dSDaniel P. Berrange break; 136a6c76285SMarkus Armbruster default: 137a6c76285SMarkus Armbruster abort(); 13804d2529dSDaniel P. Berrange } 13904d2529dSDaniel P. Berrange 14098481bfcSEric Blake return; 1413e230dd2SCorentin Chary } 1423e230dd2SCorentin Chary 14304d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, 14404d2529dSDaniel P. Berrange VncBasicInfo *info, 14598481bfcSEric Blake Error **errp) 1463e230dd2SCorentin Chary { 147bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1483e230dd2SCorentin Chary 149624cdd46SDaniel P. Berrange if (!ioc) { 150624cdd46SDaniel P. Berrange error_setg(errp, "No listener socket available"); 151624cdd46SDaniel P. Berrange return; 152624cdd46SDaniel P. Berrange } 153624cdd46SDaniel P. Berrange 15404d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, errp); 15504d2529dSDaniel P. Berrange if (!addr) { 15698481bfcSEric Blake return; 1573e230dd2SCorentin Chary } 1583e230dd2SCorentin Chary 15904d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 160bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1613e230dd2SCorentin Chary } 1623e230dd2SCorentin Chary 16304d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, 16404d2529dSDaniel P. Berrange VncBasicInfo *info, 16598481bfcSEric Blake Error **errp) 1663e230dd2SCorentin Chary { 167bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1683e230dd2SCorentin Chary 16904d2529dSDaniel P. Berrange addr = qio_channel_socket_get_remote_address(ioc, errp); 17004d2529dSDaniel P. Berrange if (!addr) { 17198481bfcSEric Blake return; 1723e230dd2SCorentin Chary } 1733e230dd2SCorentin Chary 17404d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 175bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1763e230dd2SCorentin Chary } 1773e230dd2SCorentin Chary 1783e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1793e230dd2SCorentin Chary switch (vd->auth) { 1803e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1813e230dd2SCorentin Chary return "invalid"; 1823e230dd2SCorentin Chary case VNC_AUTH_NONE: 1833e230dd2SCorentin Chary return "none"; 1843e230dd2SCorentin Chary case VNC_AUTH_VNC: 1853e230dd2SCorentin Chary return "vnc"; 1863e230dd2SCorentin Chary case VNC_AUTH_RA2: 1873e230dd2SCorentin Chary return "ra2"; 1883e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1893e230dd2SCorentin Chary return "ra2ne"; 1903e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1913e230dd2SCorentin Chary return "tight"; 1923e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1933e230dd2SCorentin Chary return "ultra"; 1943e230dd2SCorentin Chary case VNC_AUTH_TLS: 1953e230dd2SCorentin Chary return "tls"; 1963e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1973e230dd2SCorentin Chary switch (vd->subauth) { 1983e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1993e230dd2SCorentin Chary return "vencrypt+plain"; 2003e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 2013e230dd2SCorentin Chary return "vencrypt+tls+none"; 2023e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 2033e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 2043e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 2053e230dd2SCorentin Chary return "vencrypt+tls+plain"; 2063e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 2073e230dd2SCorentin Chary return "vencrypt+x509+none"; 2083e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2093e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2103e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2113e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2123e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2133e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2143e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2153e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2163e230dd2SCorentin Chary default: 2173e230dd2SCorentin Chary return "vencrypt"; 2183e230dd2SCorentin Chary } 2193e230dd2SCorentin Chary case VNC_AUTH_SASL: 2203e230dd2SCorentin Chary return "sasl"; 2213e230dd2SCorentin Chary } 2223e230dd2SCorentin Chary return "unknown"; 2233e230dd2SCorentin Chary } 2243e230dd2SCorentin Chary 225d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd) 2263e230dd2SCorentin Chary { 227fb6ba0d5SWenchao Xia VncServerInfo *info; 22898481bfcSEric Blake Error *err = NULL; 2293e230dd2SCorentin Chary 2304ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 2314ee74fa7SDaniel P. Berrange return NULL; 2324ee74fa7SDaniel P. Berrange } 2334ee74fa7SDaniel P. Berrange 2343e7f136dSDaniel P. Berrange info = g_malloc0(sizeof(*info)); 2354ee74fa7SDaniel P. Berrange vnc_init_basic_info_from_server_addr(vd->lsock[0], 236ddf21908SEric Blake qapi_VncServerInfo_base(info), &err); 237fb6ba0d5SWenchao Xia info->has_auth = true; 238d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 23998481bfcSEric Blake if (err) { 24098481bfcSEric Blake qapi_free_VncServerInfo(info); 24198481bfcSEric Blake info = NULL; 24298481bfcSEric Blake error_free(err); 24398481bfcSEric Blake } 244fb6ba0d5SWenchao Xia return info; 2453e230dd2SCorentin Chary } 2463e230dd2SCorentin Chary 2473e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2483e230dd2SCorentin Chary { 2493e230dd2SCorentin Chary if (!client->info) { 2503e230dd2SCorentin Chary return; 2513e230dd2SCorentin Chary } 2523e230dd2SCorentin Chary 2533e305e4aSDaniel P. Berrange if (client->tls) { 2543e305e4aSDaniel P. Berrange client->info->x509_dname = 2553e305e4aSDaniel P. Berrange qcrypto_tls_session_get_peer_name(client->tls); 2563e305e4aSDaniel P. Berrange client->info->has_x509_dname = 2573e305e4aSDaniel P. Berrange client->info->x509_dname != NULL; 2583e230dd2SCorentin Chary } 2593e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2603e230dd2SCorentin Chary if (client->sasl.conn && 2613e230dd2SCorentin Chary client->sasl.username) { 262fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 263fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2643e230dd2SCorentin Chary } 2653e230dd2SCorentin Chary #endif 2663e230dd2SCorentin Chary } 2673e230dd2SCorentin Chary 2683e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2693e230dd2SCorentin Chary { 27098481bfcSEric Blake Error *err = NULL; 2713e230dd2SCorentin Chary 272fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 27304d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 274ddf21908SEric Blake qapi_VncClientInfo_base(client->info), 27598481bfcSEric Blake &err); 27698481bfcSEric Blake if (err) { 27798481bfcSEric Blake qapi_free_VncClientInfo(client->info); 27898481bfcSEric Blake client->info = NULL; 27998481bfcSEric Blake error_free(err); 280fb6ba0d5SWenchao Xia } 2813e230dd2SCorentin Chary } 2823e230dd2SCorentin Chary 283fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2843e230dd2SCorentin Chary { 285fb6ba0d5SWenchao Xia VncServerInfo *si; 2863e230dd2SCorentin Chary 2873e230dd2SCorentin Chary if (!vs->info) { 2883e230dd2SCorentin Chary return; 2893e230dd2SCorentin Chary } 2903e230dd2SCorentin Chary 291d616ccc5SGerd Hoffmann si = vnc_server_info_get(vs->vd); 292fb6ba0d5SWenchao Xia if (!si) { 2933e230dd2SCorentin Chary return; 2943e230dd2SCorentin Chary } 2953e230dd2SCorentin Chary 296fb6ba0d5SWenchao Xia switch (event) { 297fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 298ddf21908SEric Blake qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info), 299ddf21908SEric Blake &error_abort); 300fb6ba0d5SWenchao Xia break; 301fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 302fb6ba0d5SWenchao Xia qapi_event_send_vnc_initialized(si, vs->info, &error_abort); 303fb6ba0d5SWenchao Xia break; 304fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 305fb6ba0d5SWenchao Xia qapi_event_send_vnc_disconnected(si, vs->info, &error_abort); 306fb6ba0d5SWenchao Xia break; 307fb6ba0d5SWenchao Xia default: 308fb6ba0d5SWenchao Xia break; 309fb6ba0d5SWenchao Xia } 3103e230dd2SCorentin Chary 311fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 3123e230dd2SCorentin Chary } 3133e230dd2SCorentin Chary 3142b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3153e230dd2SCorentin Chary { 3162b54aa87SLuiz Capitulino VncClientInfo *info; 31704d2529dSDaniel P. Berrange Error *err = NULL; 3182b54aa87SLuiz Capitulino 3192b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 32004d2529dSDaniel P. Berrange 32104d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 32204d2529dSDaniel P. Berrange qapi_VncClientInfo_base(info), 32304d2529dSDaniel P. Berrange &err); 32404d2529dSDaniel P. Berrange if (err) { 32504d2529dSDaniel P. Berrange error_free(err); 32604d2529dSDaniel P. Berrange qapi_free_VncClientInfo(info); 32704d2529dSDaniel P. Berrange return NULL; 32804d2529dSDaniel P. Berrange } 32904d2529dSDaniel P. Berrange 330ddf21908SEric Blake info->websocket = client->websocket; 3313e230dd2SCorentin Chary 3323e305e4aSDaniel P. Berrange if (client->tls) { 3333e305e4aSDaniel P. Berrange info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); 3343e305e4aSDaniel P. Berrange info->has_x509_dname = info->x509_dname != NULL; 3352b54aa87SLuiz Capitulino } 3363e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3372b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3382b54aa87SLuiz Capitulino info->has_sasl_username = true; 3392b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3402b54aa87SLuiz Capitulino } 3413e230dd2SCorentin Chary #endif 3422b54aa87SLuiz Capitulino 3432b54aa87SLuiz Capitulino return info; 3443e230dd2SCorentin Chary } 3453e230dd2SCorentin Chary 346d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id) 347d616ccc5SGerd Hoffmann { 348d616ccc5SGerd Hoffmann VncDisplay *vd; 349d616ccc5SGerd Hoffmann 350d616ccc5SGerd Hoffmann if (id == NULL) { 351d616ccc5SGerd Hoffmann return QTAILQ_FIRST(&vnc_displays); 352d616ccc5SGerd Hoffmann } 353d616ccc5SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 354d616ccc5SGerd Hoffmann if (strcmp(id, vd->id) == 0) { 355d616ccc5SGerd Hoffmann return vd; 356d616ccc5SGerd Hoffmann } 357d616ccc5SGerd Hoffmann } 358d616ccc5SGerd Hoffmann return NULL; 359d616ccc5SGerd Hoffmann } 360d616ccc5SGerd Hoffmann 3612d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) 3622d29a436SGerd Hoffmann { 3632d29a436SGerd Hoffmann VncClientInfoList *cinfo, *prev = NULL; 3642d29a436SGerd Hoffmann VncState *client; 3652d29a436SGerd Hoffmann 3662d29a436SGerd Hoffmann QTAILQ_FOREACH(client, &vd->clients, next) { 3672d29a436SGerd Hoffmann cinfo = g_new0(VncClientInfoList, 1); 3682d29a436SGerd Hoffmann cinfo->value = qmp_query_vnc_client(client); 3692d29a436SGerd Hoffmann cinfo->next = prev; 3702d29a436SGerd Hoffmann prev = cinfo; 3712d29a436SGerd Hoffmann } 3722d29a436SGerd Hoffmann return prev; 3732d29a436SGerd Hoffmann } 3742d29a436SGerd Hoffmann 3752b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3763e230dd2SCorentin Chary { 3772b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 378d616ccc5SGerd Hoffmann VncDisplay *vd = vnc_display_find(NULL); 379bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3803e230dd2SCorentin Chary 3814ee74fa7SDaniel P. Berrange if (vd == NULL || !vd->nlsock) { 3822b54aa87SLuiz Capitulino info->enabled = false; 3833e230dd2SCorentin Chary } else { 3842b54aa87SLuiz Capitulino info->enabled = true; 3852b54aa87SLuiz Capitulino 3862b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3872b54aa87SLuiz Capitulino info->has_clients = true; 3882d29a436SGerd Hoffmann info->clients = qmp_query_client_list(vd); 3893e230dd2SCorentin Chary 39004d2529dSDaniel P. Berrange if (vd->lsock == NULL) { 391417b0b88SPaolo Bonzini return info; 392417b0b88SPaolo Bonzini } 393417b0b88SPaolo Bonzini 3944ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], errp); 39504d2529dSDaniel P. Berrange if (!addr) { 3962b54aa87SLuiz Capitulino goto out_error; 3972b54aa87SLuiz Capitulino } 3983e230dd2SCorentin Chary 39904d2529dSDaniel P. Berrange switch (addr->type) { 400bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 401bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 402bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 403bd269ebcSMarkus Armbruster if (addr->u.inet.ipv6) { 40404d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 40504d2529dSDaniel P. Berrange } else { 40604d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 40704d2529dSDaniel P. Berrange } 40804d2529dSDaniel P. Berrange break; 40904d2529dSDaniel P. Berrange 410bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 41104d2529dSDaniel P. Berrange info->host = g_strdup(""); 412bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 41304d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 41404d2529dSDaniel P. Berrange break; 41504d2529dSDaniel P. Berrange 416bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 417bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 418a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 419977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 4202b54aa87SLuiz Capitulino goto out_error; 421a6c76285SMarkus Armbruster default: 422a6c76285SMarkus Armbruster abort(); 4233e230dd2SCorentin Chary } 4242b54aa87SLuiz Capitulino 4252b54aa87SLuiz Capitulino info->has_host = true; 4262b54aa87SLuiz Capitulino info->has_service = true; 4272b54aa87SLuiz Capitulino info->has_family = true; 4282b54aa87SLuiz Capitulino 4292b54aa87SLuiz Capitulino info->has_auth = true; 430d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 4313e230dd2SCorentin Chary } 4322b54aa87SLuiz Capitulino 433bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4342b54aa87SLuiz Capitulino return info; 4352b54aa87SLuiz Capitulino 4362b54aa87SLuiz Capitulino out_error: 437bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4382b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4392b54aa87SLuiz Capitulino return NULL; 4403e230dd2SCorentin Chary } 4413e230dd2SCorentin Chary 4422a7e6857SDaniel P. Berrange 4432a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4442a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4452a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4462a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt); 4472a7e6857SDaniel P. Berrange 4482a7e6857SDaniel P. Berrange static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc, 4494478aa76SGerd Hoffmann bool websocket, 4502a7e6857SDaniel P. Berrange int auth, 4512a7e6857SDaniel P. Berrange int subauth, 4522a7e6857SDaniel P. Berrange VncServerInfo2List *prev) 453df887684SGerd Hoffmann { 4542a7e6857SDaniel P. Berrange VncServerInfo2List *list; 4552a7e6857SDaniel P. Berrange VncServerInfo2 *info; 45604d2529dSDaniel P. Berrange Error *err = NULL; 457bd269ebcSMarkus Armbruster SocketAddress *addr; 458df887684SGerd Hoffmann 45904d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, &err); 46004d2529dSDaniel P. Berrange if (!addr) { 46104d2529dSDaniel P. Berrange error_free(err); 462df887684SGerd Hoffmann return prev; 463df887684SGerd Hoffmann } 464df887684SGerd Hoffmann 4652a7e6857SDaniel P. Berrange info = g_new0(VncServerInfo2, 1); 4662a7e6857SDaniel P. Berrange vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err); 467bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 46804d2529dSDaniel P. Berrange if (err) { 4692a7e6857SDaniel P. Berrange qapi_free_VncServerInfo2(info); 47004d2529dSDaniel P. Berrange error_free(err); 47104d2529dSDaniel P. Berrange return prev; 47204d2529dSDaniel P. Berrange } 4734478aa76SGerd Hoffmann info->websocket = websocket; 474df887684SGerd Hoffmann 4752a7e6857SDaniel P. Berrange qmp_query_auth(auth, subauth, &info->auth, 4762a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 4772a7e6857SDaniel P. Berrange 4782a7e6857SDaniel P. Berrange list = g_new0(VncServerInfo2List, 1); 479df887684SGerd Hoffmann list->value = info; 480df887684SGerd Hoffmann list->next = prev; 481df887684SGerd Hoffmann return list; 482df887684SGerd Hoffmann } 483df887684SGerd Hoffmann 4842a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4852a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4862a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4872a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt) 488df887684SGerd Hoffmann { 4892a7e6857SDaniel P. Berrange switch (auth) { 490df887684SGerd Hoffmann case VNC_AUTH_VNC: 4912a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VNC; 492df887684SGerd Hoffmann break; 493df887684SGerd Hoffmann case VNC_AUTH_RA2: 4942a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2; 495df887684SGerd Hoffmann break; 496df887684SGerd Hoffmann case VNC_AUTH_RA2NE: 4972a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2NE; 498df887684SGerd Hoffmann break; 499df887684SGerd Hoffmann case VNC_AUTH_TIGHT: 5002a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TIGHT; 501df887684SGerd Hoffmann break; 502df887684SGerd Hoffmann case VNC_AUTH_ULTRA: 5032a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_ULTRA; 504df887684SGerd Hoffmann break; 505df887684SGerd Hoffmann case VNC_AUTH_TLS: 5062a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TLS; 507df887684SGerd Hoffmann break; 508df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT: 5092a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT; 5102a7e6857SDaniel P. Berrange *qmp_has_vencrypt = true; 5112a7e6857SDaniel P. Berrange switch (subauth) { 512df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_PLAIN: 5132a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; 514df887684SGerd Hoffmann break; 515df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSNONE: 5162a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; 517df887684SGerd Hoffmann break; 518df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSVNC: 5192a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; 520df887684SGerd Hoffmann break; 521df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSPLAIN: 5222a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; 523df887684SGerd Hoffmann break; 524df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509NONE: 5252a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; 526df887684SGerd Hoffmann break; 527df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509VNC: 5282a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; 529df887684SGerd Hoffmann break; 530df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509PLAIN: 5312a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; 532df887684SGerd Hoffmann break; 533df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSSASL: 5342a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; 535df887684SGerd Hoffmann break; 536df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509SASL: 5372a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; 538df887684SGerd Hoffmann break; 539df887684SGerd Hoffmann default: 5402a7e6857SDaniel P. Berrange *qmp_has_vencrypt = false; 541df887684SGerd Hoffmann break; 542df887684SGerd Hoffmann } 543df887684SGerd Hoffmann break; 544df887684SGerd Hoffmann case VNC_AUTH_SASL: 5452a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_SASL; 546df887684SGerd Hoffmann break; 547df887684SGerd Hoffmann case VNC_AUTH_NONE: 548df887684SGerd Hoffmann default: 5492a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_NONE; 550df887684SGerd Hoffmann break; 551df887684SGerd Hoffmann } 552df887684SGerd Hoffmann } 553df887684SGerd Hoffmann 554df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp) 555df887684SGerd Hoffmann { 556df887684SGerd Hoffmann VncInfo2List *item, *prev = NULL; 557df887684SGerd Hoffmann VncInfo2 *info; 558df887684SGerd Hoffmann VncDisplay *vd; 559df887684SGerd Hoffmann DeviceState *dev; 5604ee74fa7SDaniel P. Berrange size_t i; 561df887684SGerd Hoffmann 562df887684SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 563df887684SGerd Hoffmann info = g_new0(VncInfo2, 1); 564df887684SGerd Hoffmann info->id = g_strdup(vd->id); 565df887684SGerd Hoffmann info->clients = qmp_query_client_list(vd); 5662a7e6857SDaniel P. Berrange qmp_query_auth(vd->auth, vd->subauth, &info->auth, 5672a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 568df887684SGerd Hoffmann if (vd->dcl.con) { 569df887684SGerd Hoffmann dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con), 570df887684SGerd Hoffmann "device", NULL)); 571df887684SGerd Hoffmann info->has_display = true; 572df887684SGerd Hoffmann info->display = g_strdup(dev->id); 573df887684SGerd Hoffmann } 5744ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 57504d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 5764ee74fa7SDaniel P. Berrange vd->lsock[i], false, vd->auth, vd->subauth, info->server); 577df887684SGerd Hoffmann } 5784ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 57904d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 5804ee74fa7SDaniel P. Berrange vd->lwebsock[i], true, vd->ws_auth, 5814ee74fa7SDaniel P. Berrange vd->ws_subauth, info->server); 582df887684SGerd Hoffmann } 583df887684SGerd Hoffmann 584df887684SGerd Hoffmann item = g_new0(VncInfo2List, 1); 585df887684SGerd Hoffmann item->value = info; 586df887684SGerd Hoffmann item->next = prev; 587df887684SGerd Hoffmann prev = item; 588df887684SGerd Hoffmann } 589df887684SGerd Hoffmann return prev; 590df887684SGerd Hoffmann } 591df887684SGerd Hoffmann 5923e230dd2SCorentin Chary /* TODO 5933e230dd2SCorentin Chary 1) Get the queue working for IO. 5943e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 5953e230dd2SCorentin Chary and not totally invalidated 5963e230dd2SCorentin Chary 3) resolutions > 1024 5973e230dd2SCorentin Chary */ 5983e230dd2SCorentin Chary 59938ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync); 6003e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 6013e230dd2SCorentin Chary 6023e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 6033e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 6043e230dd2SCorentin Chary int x_position, int y_position, 6053e230dd2SCorentin Chary int w, int h); 6060f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 6073e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 6083e230dd2SCorentin Chary 609d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd) 610d05959c2SGerd Hoffmann { 611d05959c2SGerd Hoffmann return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 612d05959c2SGerd Hoffmann VNC_DIRTY_PIXELS_PER_BIT)); 613d05959c2SGerd Hoffmann } 614d05959c2SGerd Hoffmann 615d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd) 616d05959c2SGerd Hoffmann { 617d05959c2SGerd Hoffmann return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 618d05959c2SGerd Hoffmann } 619d05959c2SGerd Hoffmann 620bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 621bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 622f7b3d68cSGerd Hoffmann VncDisplay *vd, 623f7b3d68cSGerd Hoffmann int x, int y, int w, int h) 624f7b3d68cSGerd Hoffmann { 625f7b3d68cSGerd Hoffmann int width = vnc_width(vd); 626f7b3d68cSGerd Hoffmann int height = vnc_height(vd); 627f7b3d68cSGerd Hoffmann 62891937225SPeter Lieven /* this is needed this to ensure we updated all affected 62991937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 630b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 631b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 6323e230dd2SCorentin Chary 6339f64916dSGerd Hoffmann x = MIN(x, width); 6349f64916dSGerd Hoffmann y = MIN(y, height); 6359f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 63691937225SPeter Lieven h = MIN(y + h, height); 6373e230dd2SCorentin Chary 638b4c85ddcSPeter Lieven for (; y < h; y++) { 639bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 64091937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 641b4c85ddcSPeter Lieven } 6423e230dd2SCorentin Chary } 6433e230dd2SCorentin Chary 644bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 645bea60dd7SPeter Lieven int x, int y, int w, int h) 646bea60dd7SPeter Lieven { 647bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 648bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 649bea60dd7SPeter Lieven 650f7b3d68cSGerd Hoffmann vnc_set_area_dirty(s->dirty, vd, x, y, w, h); 651bea60dd7SPeter Lieven } 652bea60dd7SPeter Lieven 6533e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 6543e230dd2SCorentin Chary int32_t encoding) 6553e230dd2SCorentin Chary { 6563e230dd2SCorentin Chary vnc_write_u16(vs, x); 6573e230dd2SCorentin Chary vnc_write_u16(vs, y); 6583e230dd2SCorentin Chary vnc_write_u16(vs, w); 6593e230dd2SCorentin Chary vnc_write_u16(vs, h); 6603e230dd2SCorentin Chary 6613e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 6623e230dd2SCorentin Chary } 6633e230dd2SCorentin Chary 66432ed2680STim Hardeck 6653e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 6663e230dd2SCorentin Chary { 66704d2529dSDaniel P. Berrange if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 6683e230dd2SCorentin Chary return; 6693e230dd2SCorentin Chary } 670bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 671bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 6723e230dd2SCorentin Chary return; 6733e230dd2SCorentin Chary } 674bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 675bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 676bd023f95SCorentin Chary vnc_lock_output(vs); 6773e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 6783e230dd2SCorentin Chary vnc_write_u8(vs, 0); 6793e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 6803e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 6813e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 682bd023f95SCorentin Chary vnc_unlock_output(vs); 6833e230dd2SCorentin Chary vnc_flush(vs); 6843e230dd2SCorentin Chary } 6853e230dd2SCorentin Chary 686bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 687bd023f95SCorentin Chary { 688bd023f95SCorentin Chary VncState *vs; 689bd023f95SCorentin Chary 690bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 691bd023f95SCorentin Chary vnc_lock_output(vs); 692bd023f95SCorentin Chary vs->abort = true; 693bd023f95SCorentin Chary vnc_unlock_output(vs); 694bd023f95SCorentin Chary } 695bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 696bd023f95SCorentin Chary vnc_jobs_join(vs); 697bd023f95SCorentin Chary } 698bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 699bd023f95SCorentin Chary vnc_lock_output(vs); 700bd023f95SCorentin Chary vs->abort = false; 701bd023f95SCorentin Chary vnc_unlock_output(vs); 702bd023f95SCorentin Chary } 703bd023f95SCorentin Chary } 704bd023f95SCorentin Chary 7059f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 7069f64916dSGerd Hoffmann { 7079f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 7089f64916dSGerd Hoffmann } 7099f64916dSGerd Hoffmann 7109f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 7119f64916dSGerd Hoffmann { 7129f64916dSGerd Hoffmann uint8_t *ptr; 7139f64916dSGerd Hoffmann 7149f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 7159f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 7169f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 7179f64916dSGerd Hoffmann return ptr; 7189f64916dSGerd Hoffmann } 7199f64916dSGerd Hoffmann 720453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd) 721453f842bSGerd Hoffmann { 722b69a553bSDaniel P. Berrange int width, height; 723b69a553bSDaniel P. Berrange 724453f842bSGerd Hoffmann qemu_pixman_image_unref(vd->server); 725453f842bSGerd Hoffmann vd->server = NULL; 726453f842bSGerd Hoffmann 727c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 728c7628bffSGerd Hoffmann return; 729c7628bffSGerd Hoffmann } 730c7628bffSGerd Hoffmann 731b69a553bSDaniel P. Berrange width = vnc_width(vd); 732b69a553bSDaniel P. Berrange height = vnc_height(vd); 733453f842bSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 734b69a553bSDaniel P. Berrange width, height, 735453f842bSGerd Hoffmann NULL, 0); 736b69a553bSDaniel P. Berrange 737b69a553bSDaniel P. Berrange memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 738b69a553bSDaniel P. Berrange vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 739b69a553bSDaniel P. Berrange width, height); 740453f842bSGerd Hoffmann } 741453f842bSGerd Hoffmann 742c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 743c12aeb86SGerd Hoffmann DisplaySurface *surface) 7443e230dd2SCorentin Chary { 74521ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7463e230dd2SCorentin Chary VncState *vs; 7473e230dd2SCorentin Chary 748bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 749453f842bSGerd Hoffmann vd->ds = surface; 750bd023f95SCorentin Chary 7513e230dd2SCorentin Chary /* server surface */ 752453f842bSGerd Hoffmann vnc_update_server_surface(vd); 7533e230dd2SCorentin Chary 7543e230dd2SCorentin Chary /* guest surface */ 7559f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 756d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 757d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 7583e230dd2SCorentin Chary 7593e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7603e230dd2SCorentin Chary vnc_colordepth(vs); 7613e230dd2SCorentin Chary vnc_desktop_resize(vs); 7623e230dd2SCorentin Chary if (vs->vd->cursor) { 7633e230dd2SCorentin Chary vnc_cursor_define(vs); 7643e230dd2SCorentin Chary } 765bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 766f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vd, 0, 0, 767b69a553bSDaniel P. Berrange vnc_width(vd), 768b69a553bSDaniel P. Berrange vnc_height(vd)); 7693e230dd2SCorentin Chary } 7703e230dd2SCorentin Chary } 7713e230dd2SCorentin Chary 7723e230dd2SCorentin Chary /* fastest code */ 7739f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 7743e230dd2SCorentin Chary void *pixels, int size) 7753e230dd2SCorentin Chary { 7763e230dd2SCorentin Chary vnc_write(vs, pixels, size); 7773e230dd2SCorentin Chary } 7783e230dd2SCorentin Chary 7793e230dd2SCorentin Chary /* slowest but generic code. */ 7803e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 7813e230dd2SCorentin Chary { 7823e230dd2SCorentin Chary uint8_t r, g, b; 7833e230dd2SCorentin Chary 7849f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 7859f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 7869f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 7879f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 7889f64916dSGerd Hoffmann #else 7899f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 7909f64916dSGerd Hoffmann #endif 7919f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 7929f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 7939f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 7949f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 7953e230dd2SCorentin Chary case 1: 7963e230dd2SCorentin Chary buf[0] = v; 7973e230dd2SCorentin Chary break; 7983e230dd2SCorentin Chary case 2: 7999f64916dSGerd Hoffmann if (vs->client_be) { 8003e230dd2SCorentin Chary buf[0] = v >> 8; 8013e230dd2SCorentin Chary buf[1] = v; 8023e230dd2SCorentin Chary } else { 8033e230dd2SCorentin Chary buf[1] = v >> 8; 8043e230dd2SCorentin Chary buf[0] = v; 8053e230dd2SCorentin Chary } 8063e230dd2SCorentin Chary break; 8073e230dd2SCorentin Chary default: 8083e230dd2SCorentin Chary case 4: 8099f64916dSGerd Hoffmann if (vs->client_be) { 8103e230dd2SCorentin Chary buf[0] = v >> 24; 8113e230dd2SCorentin Chary buf[1] = v >> 16; 8123e230dd2SCorentin Chary buf[2] = v >> 8; 8133e230dd2SCorentin Chary buf[3] = v; 8143e230dd2SCorentin Chary } else { 8153e230dd2SCorentin Chary buf[3] = v >> 24; 8163e230dd2SCorentin Chary buf[2] = v >> 16; 8173e230dd2SCorentin Chary buf[1] = v >> 8; 8183e230dd2SCorentin Chary buf[0] = v; 8193e230dd2SCorentin Chary } 8203e230dd2SCorentin Chary break; 8213e230dd2SCorentin Chary } 8223e230dd2SCorentin Chary } 8233e230dd2SCorentin Chary 8249f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 8253e230dd2SCorentin Chary void *pixels1, int size) 8263e230dd2SCorentin Chary { 8273e230dd2SCorentin Chary uint8_t buf[4]; 8283e230dd2SCorentin Chary 8299f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 8303e230dd2SCorentin Chary uint32_t *pixels = pixels1; 8313e230dd2SCorentin Chary int n, i; 8323e230dd2SCorentin Chary n = size >> 2; 8333e230dd2SCorentin Chary for (i = 0; i < n; i++) { 8343e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 8359f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 8363e230dd2SCorentin Chary } 8373e230dd2SCorentin Chary } 8383e230dd2SCorentin Chary } 8393e230dd2SCorentin Chary 8403e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8413e230dd2SCorentin Chary { 8423e230dd2SCorentin Chary int i; 8433e230dd2SCorentin Chary uint8_t *row; 8443e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8453e230dd2SCorentin Chary 8469f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 8473e230dd2SCorentin Chary for (i = 0; i < h; i++) { 8489f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 8499f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 8503e230dd2SCorentin Chary } 8513e230dd2SCorentin Chary return 1; 8523e230dd2SCorentin Chary } 8533e230dd2SCorentin Chary 854bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8553e230dd2SCorentin Chary { 8563e230dd2SCorentin Chary int n = 0; 857de3f7de7SPeter Lieven bool encode_raw = false; 858de3f7de7SPeter Lieven size_t saved_offs = vs->output.offset; 8593e230dd2SCorentin Chary 8603e230dd2SCorentin Chary switch(vs->vnc_encoding) { 8613e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 8623e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 8633e230dd2SCorentin Chary break; 8643e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 8653e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 8663e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 8673e230dd2SCorentin Chary break; 8683e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 8693e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 8703e230dd2SCorentin Chary break; 871efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 872efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 873efe556adSCorentin Chary break; 874148954faSCorentin Chary case VNC_ENCODING_ZRLE: 875148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 876148954faSCorentin Chary break; 877148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 878148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 879148954faSCorentin Chary break; 8803e230dd2SCorentin Chary default: 881de3f7de7SPeter Lieven encode_raw = true; 8823e230dd2SCorentin Chary break; 8833e230dd2SCorentin Chary } 884de3f7de7SPeter Lieven 885de3f7de7SPeter Lieven /* If the client has the same pixel format as our internal buffer and 886de3f7de7SPeter Lieven * a RAW encoding would need less space fall back to RAW encoding to 887de3f7de7SPeter Lieven * save bandwidth and processing power in the client. */ 888de3f7de7SPeter Lieven if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy && 889de3f7de7SPeter Lieven 12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) { 890de3f7de7SPeter Lieven vs->output.offset = saved_offs; 891de3f7de7SPeter Lieven encode_raw = true; 892de3f7de7SPeter Lieven } 893de3f7de7SPeter Lieven 894de3f7de7SPeter Lieven if (encode_raw) { 895de3f7de7SPeter Lieven vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 896de3f7de7SPeter Lieven n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 897de3f7de7SPeter Lieven } 898de3f7de7SPeter Lieven 8993e230dd2SCorentin Chary return n; 9003e230dd2SCorentin Chary } 9013e230dd2SCorentin Chary 9027c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 9037c20b4a3SGerd Hoffmann int x, int y, int visible) 9043e230dd2SCorentin Chary { 9053e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 9063e230dd2SCorentin Chary } 9073e230dd2SCorentin Chary 9083e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 9093e230dd2SCorentin Chary { 9103e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 9113e230dd2SCorentin Chary int isize; 9123e230dd2SCorentin Chary 9133e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 914d01f9595SCorentin Chary vnc_lock_output(vs); 9153e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 9163e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 9173e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 9183e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 9193e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 9209f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 9219f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 9223e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 923d01f9595SCorentin Chary vnc_unlock_output(vs); 9243e230dd2SCorentin Chary return 0; 9253e230dd2SCorentin Chary } 9263e230dd2SCorentin Chary return -1; 9273e230dd2SCorentin Chary } 9283e230dd2SCorentin Chary 9297c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 9307c20b4a3SGerd Hoffmann QEMUCursor *c) 9313e230dd2SCorentin Chary { 932d616ccc5SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 9333e230dd2SCorentin Chary VncState *vs; 9343e230dd2SCorentin Chary 9353e230dd2SCorentin Chary cursor_put(vd->cursor); 9367267c094SAnthony Liguori g_free(vd->cursor_mask); 9373e230dd2SCorentin Chary 9383e230dd2SCorentin Chary vd->cursor = c; 9393e230dd2SCorentin Chary cursor_get(vd->cursor); 9403e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 9417267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 9423e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 9433e230dd2SCorentin Chary 9443e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9453e230dd2SCorentin Chary vnc_cursor_define(vs); 9463e230dd2SCorentin Chary } 9473e230dd2SCorentin Chary } 9483e230dd2SCorentin Chary 9494769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs, 9506c71a539SCorentin Chary int y, int last_x, int x, int height) 9513e230dd2SCorentin Chary { 9523e230dd2SCorentin Chary int h; 9533e230dd2SCorentin Chary 9546c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 955bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 9563e230dd2SCorentin Chary break; 957bc2429b9SCorentin Chary } 958863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 9593e230dd2SCorentin Chary } 9603e230dd2SCorentin Chary 9613e230dd2SCorentin Chary return h; 9623e230dd2SCorentin Chary } 9633e230dd2SCorentin Chary 96438ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync) 9653e230dd2SCorentin Chary { 9665a8be0f7SGerd Hoffmann if (vs->disconnecting) { 9675a8be0f7SGerd Hoffmann vnc_disconnect_finish(vs); 9685a8be0f7SGerd Hoffmann return 0; 9695a8be0f7SGerd Hoffmann } 9705a8be0f7SGerd Hoffmann 97163658280SGerd Hoffmann vs->has_dirty += has_dirty; 9725a693efdSGonglei if (vs->need_update && !vs->disconnecting) { 9733e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 974bd023f95SCorentin Chary VncJob *job; 9753e230dd2SCorentin Chary int y; 9762f487a3dSPeter Lieven int height, width; 977bd023f95SCorentin Chary int n = 0; 978bd023f95SCorentin Chary 9793e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 9803e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 9813e230dd2SCorentin Chary return 0; 9823e230dd2SCorentin Chary 98363658280SGerd Hoffmann if (!vs->has_dirty && !vs->audio_cap && !vs->force_update) 9843e230dd2SCorentin Chary return 0; 9853e230dd2SCorentin Chary 9863e230dd2SCorentin Chary /* 9873e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9883e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9893e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9903e230dd2SCorentin Chary * send them to the client. 9913e230dd2SCorentin Chary */ 992bd023f95SCorentin Chary job = vnc_job_new(vs); 9933e230dd2SCorentin Chary 994bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 995bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 9963e230dd2SCorentin Chary 99712b316d4SPeter Lieven y = 0; 99812b316d4SPeter Lieven for (;;) { 99912b316d4SPeter Lieven int x, h; 100012b316d4SPeter Lieven unsigned long x2; 100112b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 100212b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 100312b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 100412b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 100512b316d4SPeter Lieven /* no more dirty bits */ 100612b316d4SPeter Lieven break; 10073e230dd2SCorentin Chary } 100812b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 100912b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 101012b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 101112b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 101212b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 101312b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10142f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10152f487a3dSPeter Lieven if (x2 > x) { 101612b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 101712b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10183e230dd2SCorentin Chary } 10190e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10200e7d6f60SPeter Lieven y += h; 10210e7d6f60SPeter Lieven if (y == height) { 10220e7d6f60SPeter Lieven break; 10230e7d6f60SPeter Lieven } 10240e7d6f60SPeter Lieven } 10252f487a3dSPeter Lieven } 1026bd023f95SCorentin Chary 1027bd023f95SCorentin Chary vnc_job_push(job); 1028eb214ff8SGerd Hoffmann if (sync) { 1029eb214ff8SGerd Hoffmann vnc_jobs_join(vs); 1030eb214ff8SGerd Hoffmann } 10313e230dd2SCorentin Chary vs->force_update = 0; 103263658280SGerd Hoffmann vs->has_dirty = 0; 1033bd023f95SCorentin Chary return n; 10343e230dd2SCorentin Chary } 10353e230dd2SCorentin Chary 103604d2529dSDaniel P. Berrange if (vs->disconnecting) { 10373e230dd2SCorentin Chary vnc_disconnect_finish(vs); 103838ee14f4SGerd Hoffmann } else if (sync) { 103938ee14f4SGerd Hoffmann vnc_jobs_join(vs); 104038ee14f4SGerd Hoffmann } 10413e230dd2SCorentin Chary 10423e230dd2SCorentin Chary return 0; 10433e230dd2SCorentin Chary } 10443e230dd2SCorentin Chary 10453e230dd2SCorentin Chary /* audio */ 10463e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 10473e230dd2SCorentin Chary { 10483e230dd2SCorentin Chary VncState *vs = opaque; 10493e230dd2SCorentin Chary 10503e230dd2SCorentin Chary switch (cmd) { 10513e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1052bd023f95SCorentin Chary vnc_lock_output(vs); 10533e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10543e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10553e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1056bd023f95SCorentin Chary vnc_unlock_output(vs); 10573e230dd2SCorentin Chary vnc_flush(vs); 10583e230dd2SCorentin Chary break; 10593e230dd2SCorentin Chary 10603e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1061bd023f95SCorentin Chary vnc_lock_output(vs); 10623e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10633e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10643e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1065bd023f95SCorentin Chary vnc_unlock_output(vs); 10663e230dd2SCorentin Chary vnc_flush(vs); 10673e230dd2SCorentin Chary break; 10683e230dd2SCorentin Chary } 10693e230dd2SCorentin Chary } 10703e230dd2SCorentin Chary 10713e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 10723e230dd2SCorentin Chary { 10733e230dd2SCorentin Chary } 10743e230dd2SCorentin Chary 10753e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 10763e230dd2SCorentin Chary { 10773e230dd2SCorentin Chary VncState *vs = opaque; 10783e230dd2SCorentin Chary 1079bd023f95SCorentin Chary vnc_lock_output(vs); 10803e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10813e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10823e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 10833e230dd2SCorentin Chary vnc_write_u32(vs, size); 10843e230dd2SCorentin Chary vnc_write(vs, buf, size); 1085bd023f95SCorentin Chary vnc_unlock_output(vs); 10863e230dd2SCorentin Chary vnc_flush(vs); 10873e230dd2SCorentin Chary } 10883e230dd2SCorentin Chary 10893e230dd2SCorentin Chary static void audio_add(VncState *vs) 10903e230dd2SCorentin Chary { 10913e230dd2SCorentin Chary struct audio_capture_ops ops; 10923e230dd2SCorentin Chary 10933e230dd2SCorentin Chary if (vs->audio_cap) { 1094027a79c3SCole Robinson error_report("audio already running"); 10953e230dd2SCorentin Chary return; 10963e230dd2SCorentin Chary } 10973e230dd2SCorentin Chary 10983e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10993e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 11003e230dd2SCorentin Chary ops.capture = audio_capture; 11013e230dd2SCorentin Chary 11023e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 11033e230dd2SCorentin Chary if (!vs->audio_cap) { 1104027a79c3SCole Robinson error_report("Failed to add audio capture"); 11053e230dd2SCorentin Chary } 11063e230dd2SCorentin Chary } 11073e230dd2SCorentin Chary 11083e230dd2SCorentin Chary static void audio_del(VncState *vs) 11093e230dd2SCorentin Chary { 11103e230dd2SCorentin Chary if (vs->audio_cap) { 11113e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11123e230dd2SCorentin Chary vs->audio_cap = NULL; 11133e230dd2SCorentin Chary } 11143e230dd2SCorentin Chary } 11153e230dd2SCorentin Chary 11163e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11173e230dd2SCorentin Chary { 111804d2529dSDaniel P. Berrange if (vs->disconnecting) { 11193e230dd2SCorentin Chary return; 112004d2529dSDaniel P. Berrange } 1121ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_start(vs, vs->ioc); 11228cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 112304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 112404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 112504d2529dSDaniel P. Berrange } 112604d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 112704d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11283e230dd2SCorentin Chary } 11293e230dd2SCorentin Chary 11307536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11313e230dd2SCorentin Chary { 11327d964c9dSCorentin Chary int i; 11337d964c9dSCorentin Chary 1134ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_finish(vs, vs->ioc); 1135ad6374c4SDaniel P. Berrange 1136bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1137bd023f95SCorentin Chary 1138bd023f95SCorentin Chary vnc_lock_output(vs); 1139fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11403e230dd2SCorentin Chary 11413e230dd2SCorentin Chary buffer_free(&vs->input); 11423e230dd2SCorentin Chary buffer_free(&vs->output); 11433e230dd2SCorentin Chary 1144fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11453e230dd2SCorentin Chary 11463e230dd2SCorentin Chary vnc_zlib_clear(vs); 11473e230dd2SCorentin Chary vnc_tight_clear(vs); 1148148954faSCorentin Chary vnc_zrle_clear(vs); 11493e230dd2SCorentin Chary 11503e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11513e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 11523e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11533e230dd2SCorentin Chary audio_del(vs); 11547bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 11553e230dd2SCorentin Chary 115690cd03a3SDaniel P. Berrange if (vs->mouse_mode_notifier.notify != NULL) { 11576fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 115890cd03a3SDaniel P. Berrange } 115990cd03a3SDaniel P. Berrange QTAILQ_REMOVE(&vs->vd->clients, vs, next); 1160c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1161c7628bffSGerd Hoffmann /* last client gone */ 1162c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1163c7628bffSGerd Hoffmann } 11643e230dd2SCorentin Chary 1165bd023f95SCorentin Chary vnc_unlock_output(vs); 1166bd023f95SCorentin Chary 1167bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 11686fd8e79aSTim Hardeck if (vs->bh != NULL) { 1169175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 11706fd8e79aSTim Hardeck } 1171175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1172175b2a6eSCorentin Chary 11737d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 11747267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 11757d964c9dSCorentin Chary } 11767267c094SAnthony Liguori g_free(vs->lossy_rect); 117704d2529dSDaniel P. Berrange 117804d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 117904d2529dSDaniel P. Berrange vs->ioc = NULL; 118004d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 118104d2529dSDaniel P. Berrange vs->sioc = NULL; 11827267c094SAnthony Liguori g_free(vs); 11833e230dd2SCorentin Chary } 11843e230dd2SCorentin Chary 118504d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 11863e230dd2SCorentin Chary { 118704d2529dSDaniel P. Berrange if (ret <= 0) { 118804d2529dSDaniel P. Berrange if (ret == 0) { 1189ad6374c4SDaniel P. Berrange trace_vnc_client_eof(vs, vs->ioc); 1190537848eeSMichael Tokarev vnc_disconnect_start(vs); 119104d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 1192ad6374c4SDaniel P. Berrange trace_vnc_client_io_error(vs, vs->ioc, 1193ad6374c4SDaniel P. Berrange errp ? error_get_pretty(*errp) : 1194ad6374c4SDaniel P. Berrange "Unknown"); 1195537848eeSMichael Tokarev vnc_disconnect_start(vs); 11963e230dd2SCorentin Chary } 11973e230dd2SCorentin Chary 119804d2529dSDaniel P. Berrange if (errp) { 119904d2529dSDaniel P. Berrange error_free(*errp); 120004d2529dSDaniel P. Berrange *errp = NULL; 120104d2529dSDaniel P. Berrange } 12023e230dd2SCorentin Chary return 0; 12033e230dd2SCorentin Chary } 12043e230dd2SCorentin Chary return ret; 12053e230dd2SCorentin Chary } 12063e230dd2SCorentin Chary 12073e230dd2SCorentin Chary 12083e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12093e230dd2SCorentin Chary { 12103e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12113e230dd2SCorentin Chary vnc_disconnect_start(vs); 12123e230dd2SCorentin Chary } 12133e230dd2SCorentin Chary 12143e305e4aSDaniel P. Berrange 12153e230dd2SCorentin Chary /* 12163e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12173e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12183e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12193e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12203e230dd2SCorentin Chary * 12213e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12223e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12233e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12243e230dd2SCorentin Chary * is active 12253e230dd2SCorentin Chary * 12263e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12273e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12283e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12293e230dd2SCorentin Chary */ 1230fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12313e230dd2SCorentin Chary { 123204d2529dSDaniel P. Berrange Error *err = NULL; 1233fdd1ab6aSDaniel P. Berrange ssize_t ret; 123404d2529dSDaniel P. Berrange ret = qio_channel_write( 123504d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12363e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 123704d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12383e230dd2SCorentin Chary } 12393e230dd2SCorentin Chary 12403e230dd2SCorentin Chary 12413e230dd2SCorentin Chary /* 12423e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12433e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12443e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12453e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12463e230dd2SCorentin Chary * 12473e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12483e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 12493e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12503e230dd2SCorentin Chary */ 1251fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 12523e230dd2SCorentin Chary { 1253fdd1ab6aSDaniel P. Berrange ssize_t ret; 12543e230dd2SCorentin Chary 12553e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12563e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 12573e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 12583e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 12593e230dd2SCorentin Chary 12603e230dd2SCorentin Chary if (vs->sasl.conn && 12613e230dd2SCorentin Chary vs->sasl.runSSF && 12623e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 12633e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 12643e230dd2SCorentin Chary if (ret) 12653e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 12663e230dd2SCorentin Chary } else 12673e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12683e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 12693e230dd2SCorentin Chary if (!ret) 12703e230dd2SCorentin Chary return 0; 12713e230dd2SCorentin Chary 127232ed2680STim Hardeck buffer_advance(&vs->output, ret); 12733e230dd2SCorentin Chary 12743e230dd2SCorentin Chary if (vs->output.offset == 0) { 127504d2529dSDaniel P. Berrange if (vs->ioc_tag) { 127604d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 127704d2529dSDaniel P. Berrange } 127804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 127904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 12803e230dd2SCorentin Chary } 12813e230dd2SCorentin Chary 12823e230dd2SCorentin Chary return ret; 12833e230dd2SCorentin Chary } 12843e230dd2SCorentin Chary 12853e230dd2SCorentin Chary 12863e230dd2SCorentin Chary /* 12873e230dd2SCorentin Chary * First function called whenever there is data to be written to 12883e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12893e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12903e230dd2SCorentin Chary */ 129104d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 12923e230dd2SCorentin Chary { 12933e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12943e230dd2SCorentin Chary if (vs->sasl.conn && 12953e230dd2SCorentin Chary vs->sasl.runSSF && 12963e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12973e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12983e230dd2SCorentin Chary } else 12993e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13007536ee4bSTim Hardeck { 13013e230dd2SCorentin Chary vnc_client_write_plain(vs); 13023e230dd2SCorentin Chary } 13037536ee4bSTim Hardeck } 13043e230dd2SCorentin Chary 130504d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1306bd023f95SCorentin Chary { 1307bd023f95SCorentin Chary 1308bd023f95SCorentin Chary vnc_lock_output(vs); 1309d5f04223SDaniel P. Berrange if (vs->output.offset) { 131004d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 131104d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 131204d2529dSDaniel P. Berrange if (vs->ioc_tag) { 131304d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 131404d2529dSDaniel P. Berrange } 131504d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 131604d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1317bd023f95SCorentin Chary } 1318bd023f95SCorentin Chary vnc_unlock_output(vs); 1319bd023f95SCorentin Chary } 1320bd023f95SCorentin Chary 13213e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13223e230dd2SCorentin Chary { 13233e230dd2SCorentin Chary vs->read_handler = func; 13243e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13253e230dd2SCorentin Chary } 13263e230dd2SCorentin Chary 13273e230dd2SCorentin Chary 13283e230dd2SCorentin Chary /* 13293e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13303e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13313e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13323e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13333e230dd2SCorentin Chary * 13343e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13353e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13363e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13373e230dd2SCorentin Chary * is active 13383e230dd2SCorentin Chary * 13393e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13403e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13413e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13423e230dd2SCorentin Chary */ 1343fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13443e230dd2SCorentin Chary { 1345fdd1ab6aSDaniel P. Berrange ssize_t ret; 134604d2529dSDaniel P. Berrange Error *err = NULL; 134704d2529dSDaniel P. Berrange ret = qio_channel_read( 134804d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 13493e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 135004d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 13513e230dd2SCorentin Chary } 13523e230dd2SCorentin Chary 13533e230dd2SCorentin Chary 13543e230dd2SCorentin Chary /* 13553e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 13563e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 13573e230dd2SCorentin Chary * data as possible without blocking. 13583e230dd2SCorentin Chary * 13593e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 13603e230dd2SCorentin Chary * disconnects the client socket. 13613e230dd2SCorentin Chary */ 1362fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 13633e230dd2SCorentin Chary { 1364fdd1ab6aSDaniel P. Berrange ssize_t ret; 13653e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13663e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13673e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13683e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13693e230dd2SCorentin Chary if (!ret) 13703e230dd2SCorentin Chary return 0; 13713e230dd2SCorentin Chary vs->input.offset += ret; 13723e230dd2SCorentin Chary return ret; 13733e230dd2SCorentin Chary } 13743e230dd2SCorentin Chary 1375175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1376175b2a6eSCorentin Chary { 1377175b2a6eSCorentin Chary VncState *vs = opaque; 1378175b2a6eSCorentin Chary 1379175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1380175b2a6eSCorentin Chary } 13813e230dd2SCorentin Chary 13823e230dd2SCorentin Chary /* 13833e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13843e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13853e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 1386ea697449SDaniel P. Berrange * Returns 0 on success, -1 if client disconnected 13873e230dd2SCorentin Chary */ 1388ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs) 13893e230dd2SCorentin Chary { 1390fdd1ab6aSDaniel P. Berrange ssize_t ret; 13913e230dd2SCorentin Chary 13923e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13933e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13943e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13953e230dd2SCorentin Chary else 13963e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13973e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13983e230dd2SCorentin Chary if (!ret) { 139904d2529dSDaniel P. Berrange if (vs->disconnecting) { 14003e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1401ea697449SDaniel P. Berrange return -1; 140204d2529dSDaniel P. Berrange } 1403ea697449SDaniel P. Berrange return 0; 14043e230dd2SCorentin Chary } 14053e230dd2SCorentin Chary 14063e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14073e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14083e230dd2SCorentin Chary int ret; 14093e230dd2SCorentin Chary 14103e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 141104d2529dSDaniel P. Berrange if (vs->disconnecting) { 14123e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1413ea697449SDaniel P. Berrange return -1; 14143e230dd2SCorentin Chary } 14153e230dd2SCorentin Chary 14163e230dd2SCorentin Chary if (!ret) { 141732ed2680STim Hardeck buffer_advance(&vs->input, len); 14183e230dd2SCorentin Chary } else { 14193e230dd2SCorentin Chary vs->read_handler_expect = ret; 14203e230dd2SCorentin Chary } 14213e230dd2SCorentin Chary } 1422ea697449SDaniel P. Berrange return 0; 14233e230dd2SCorentin Chary } 14243e230dd2SCorentin Chary 142504d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 142604d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 142704d2529dSDaniel P. Berrange { 142804d2529dSDaniel P. Berrange VncState *vs = opaque; 142904d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 1430ea697449SDaniel P. Berrange if (vnc_client_read(vs) < 0) { 1431ea697449SDaniel P. Berrange return TRUE; 1432ea697449SDaniel P. Berrange } 143304d2529dSDaniel P. Berrange } 143404d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 143504d2529dSDaniel P. Berrange vnc_client_write(vs); 143604d2529dSDaniel P. Berrange } 143704d2529dSDaniel P. Berrange return TRUE; 143804d2529dSDaniel P. Berrange } 143904d2529dSDaniel P. Berrange 144004d2529dSDaniel P. Berrange 14413e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14423e230dd2SCorentin Chary { 14433e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14443e230dd2SCorentin Chary 144504d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 144604d2529dSDaniel P. Berrange if (vs->ioc_tag) { 144704d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 144804d2529dSDaniel P. Berrange } 144904d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 145004d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14513e230dd2SCorentin Chary } 14523e230dd2SCorentin Chary 14533e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 14543e230dd2SCorentin Chary } 14553e230dd2SCorentin Chary 14563e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 14573e230dd2SCorentin Chary { 14583e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 14593e230dd2SCorentin Chary } 14603e230dd2SCorentin Chary 14613e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 14623e230dd2SCorentin Chary { 14633e230dd2SCorentin Chary uint8_t buf[4]; 14643e230dd2SCorentin Chary 14653e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 14663e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 14673e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 14683e230dd2SCorentin Chary buf[3] = value & 0xFF; 14693e230dd2SCorentin Chary 14703e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14713e230dd2SCorentin Chary } 14723e230dd2SCorentin Chary 14733e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14743e230dd2SCorentin Chary { 14753e230dd2SCorentin Chary uint8_t buf[2]; 14763e230dd2SCorentin Chary 14773e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14783e230dd2SCorentin Chary buf[1] = value & 0xFF; 14793e230dd2SCorentin Chary 14803e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14813e230dd2SCorentin Chary } 14823e230dd2SCorentin Chary 14833e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14843e230dd2SCorentin Chary { 14853e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14863e230dd2SCorentin Chary } 14873e230dd2SCorentin Chary 14883e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14893e230dd2SCorentin Chary { 1490bd023f95SCorentin Chary vnc_lock_output(vs); 1491d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1492bd023f95SCorentin Chary vnc_client_write_locked(vs); 1493bd023f95SCorentin Chary } 1494bd023f95SCorentin Chary vnc_unlock_output(vs); 14953e230dd2SCorentin Chary } 14963e230dd2SCorentin Chary 149771a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14983e230dd2SCorentin Chary { 14993e230dd2SCorentin Chary return data[offset]; 15003e230dd2SCorentin Chary } 15013e230dd2SCorentin Chary 150271a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 15033e230dd2SCorentin Chary { 15043e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15053e230dd2SCorentin Chary } 15063e230dd2SCorentin Chary 150771a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15083e230dd2SCorentin Chary { 15093e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15103e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15113e230dd2SCorentin Chary } 15123e230dd2SCorentin Chary 15133e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15143e230dd2SCorentin Chary { 15153e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15163e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15173e230dd2SCorentin Chary } 15183e230dd2SCorentin Chary 15193e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15203e230dd2SCorentin Chary { 15213e230dd2SCorentin Chary } 15223e230dd2SCorentin Chary 15239e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15243e230dd2SCorentin Chary { 15253e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 152614768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15273e230dd2SCorentin Chary 15283e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1529bd023f95SCorentin Chary vnc_lock_output(vs); 15303e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15313e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15323e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15333e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1534bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1535bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15363e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1537bd023f95SCorentin Chary vnc_unlock_output(vs); 15383e230dd2SCorentin Chary vnc_flush(vs); 15393e230dd2SCorentin Chary } 15403e230dd2SCorentin Chary vs->absolute = absolute; 15413e230dd2SCorentin Chary } 15423e230dd2SCorentin Chary 15433e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15443e230dd2SCorentin Chary { 15457fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 154614768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 154714768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 154814768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1549f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1550f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 155114768ebaSGerd Hoffmann }; 155214768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1553bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1554bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 15553e230dd2SCorentin Chary 155614768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 155714768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 155814768ebaSGerd Hoffmann vs->last_bmask = button_mask; 155914768ebaSGerd Hoffmann } 15603e230dd2SCorentin Chary 15613e230dd2SCorentin Chary if (vs->absolute) { 15629cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); 15639cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); 15643e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 156514768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 156614768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 15673e230dd2SCorentin Chary } else { 156814768ebaSGerd Hoffmann if (vs->last_x != -1) { 156914768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 157014768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 157114768ebaSGerd Hoffmann } 15723e230dd2SCorentin Chary vs->last_x = x; 15733e230dd2SCorentin Chary vs->last_y = y; 15743e230dd2SCorentin Chary } 157514768ebaSGerd Hoffmann qemu_input_event_sync(); 15763e230dd2SCorentin Chary } 15773e230dd2SCorentin Chary 15783e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15793e230dd2SCorentin Chary { 15803e230dd2SCorentin Chary int i; 15813e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15823e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15838d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 1584c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15853e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15863e230dd2SCorentin Chary } 15873e230dd2SCorentin Chary } 15883e230dd2SCorentin Chary } 15893e230dd2SCorentin Chary 15903e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15913e230dd2SCorentin Chary { 15923e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15938d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 1594c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15958d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1596c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15973e230dd2SCorentin Chary } 15983e230dd2SCorentin Chary 1599ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1600ab99e5c1SLei Li { 1601ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1602ab99e5c1SLei Li return; 1603ab99e5c1SLei Li } 1604ab99e5c1SLei Li 1605ab99e5c1SLei Li vnc_lock_output(vs); 1606ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1607ab99e5c1SLei Li vnc_write_u8(vs, 0); 1608ab99e5c1SLei Li vnc_write_u16(vs, 1); 1609ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1610a54f0d2bSPierre Ossman vnc_write_u8(vs, vs->vd->ledstate); 1611ab99e5c1SLei Li vnc_unlock_output(vs); 1612ab99e5c1SLei Li vnc_flush(vs); 1613ab99e5c1SLei Li } 1614ab99e5c1SLei Li 16153e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16163e230dd2SCorentin Chary { 1617a54f0d2bSPierre Ossman VncDisplay *vd = opaque; 1618a54f0d2bSPierre Ossman VncState *client; 16193e230dd2SCorentin Chary 162040066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 162140066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 162240066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 162340066175SGerd Hoffmann 1624a54f0d2bSPierre Ossman if (ledstate == vd->ledstate) { 1625a54f0d2bSPierre Ossman return; 162696f3d174SLei Li } 1627ab99e5c1SLei Li 1628a54f0d2bSPierre Ossman vd->ledstate = ledstate; 1629a54f0d2bSPierre Ossman 1630a54f0d2bSPierre Ossman QTAILQ_FOREACH(client, &vd->clients, next) { 1631a54f0d2bSPierre Ossman vnc_led_state_change(client); 1632ab99e5c1SLei Li } 16333e230dd2SCorentin Chary } 16343e230dd2SCorentin Chary 16353e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 16363e230dd2SCorentin Chary { 16373e230dd2SCorentin Chary /* QEMU console switch */ 16383e230dd2SCorentin Chary switch(keycode) { 16393e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16403e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16413e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16423e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16433e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16443e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16453e230dd2SCorentin Chary if (down) 16463e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 16473e230dd2SCorentin Chary else 16483e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 16493e230dd2SCorentin Chary break; 16503e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 16511d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 16521d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 16533e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 16543e230dd2SCorentin Chary reset_keys(vs); 16553e230dd2SCorentin Chary console_select(keycode - 0x02); 16563e230dd2SCorentin Chary return; 16573e230dd2SCorentin Chary } 16583e230dd2SCorentin Chary break; 16593e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 16603e230dd2SCorentin Chary case 0x45: /* NumLock */ 16613e230dd2SCorentin Chary if (down) 16623e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 16633e230dd2SCorentin Chary break; 16643e230dd2SCorentin Chary } 16653e230dd2SCorentin Chary 1666e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1667e7b2aaccSLei Li state extension. 1668e7b2aaccSLei Li */ 16699892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1670e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16713e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 16723e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 16733e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16743e230dd2SCorentin Chary toggles numlock away from the VNC window. 16753e230dd2SCorentin Chary */ 16763e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 16773e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 167840066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 16793e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 16803e230dd2SCorentin Chary press_key(vs, 0xff7f); 16813e230dd2SCorentin Chary } 16823e230dd2SCorentin Chary } else { 16833e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 168440066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 16853e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 16863e230dd2SCorentin Chary press_key(vs, 0xff7f); 16873e230dd2SCorentin Chary } 16883e230dd2SCorentin Chary } 16893e230dd2SCorentin Chary } 16903e230dd2SCorentin Chary 16919892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1692e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16933e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 16943e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 16953e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16963e230dd2SCorentin Chary toggles capslock away from the VNC window. 16973e230dd2SCorentin Chary */ 16983e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 16993e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 17003e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 17013e230dd2SCorentin Chary if (capslock) { 17023e230dd2SCorentin Chary if (uppercase == shift) { 170340066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17043e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17053e230dd2SCorentin Chary press_key(vs, 0xffe5); 17063e230dd2SCorentin Chary } 17073e230dd2SCorentin Chary } else { 17083e230dd2SCorentin Chary if (uppercase != shift) { 170940066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17103e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17113e230dd2SCorentin Chary press_key(vs, 0xffe5); 17123e230dd2SCorentin Chary } 17133e230dd2SCorentin Chary } 17143e230dd2SCorentin Chary } 17153e230dd2SCorentin Chary 171681c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17178d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 1718c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 17193e230dd2SCorentin Chary } else { 1720e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1721e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1722e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17233e230dd2SCorentin Chary /* QEMU console emulation */ 17243e230dd2SCorentin Chary if (down) { 17253e230dd2SCorentin Chary switch (keycode) { 17263e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17273e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17283e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17293e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17303e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17313e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17323e230dd2SCorentin Chary break; 17333e230dd2SCorentin Chary case 0xc8: 17343e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 17353e230dd2SCorentin Chary break; 17363e230dd2SCorentin Chary case 0xd0: 17373e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 17383e230dd2SCorentin Chary break; 17393e230dd2SCorentin Chary case 0xcb: 17403e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 17413e230dd2SCorentin Chary break; 17423e230dd2SCorentin Chary case 0xcd: 17433e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 17443e230dd2SCorentin Chary break; 17453e230dd2SCorentin Chary case 0xd3: 17463e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 17473e230dd2SCorentin Chary break; 17483e230dd2SCorentin Chary case 0xc7: 17493e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 17503e230dd2SCorentin Chary break; 17513e230dd2SCorentin Chary case 0xcf: 17523e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 17533e230dd2SCorentin Chary break; 17543e230dd2SCorentin Chary case 0xc9: 17553e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 17563e230dd2SCorentin Chary break; 17573e230dd2SCorentin Chary case 0xd1: 17583e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 17593e230dd2SCorentin Chary break; 17603e230dd2SCorentin Chary 17613e230dd2SCorentin Chary case 0x47: 17623e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 17633e230dd2SCorentin Chary break; 17643e230dd2SCorentin Chary case 0x48: 17653e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 17663e230dd2SCorentin Chary break; 17673e230dd2SCorentin Chary case 0x49: 17683e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 17693e230dd2SCorentin Chary break; 17703e230dd2SCorentin Chary case 0x4b: 17713e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 17723e230dd2SCorentin Chary break; 17733e230dd2SCorentin Chary case 0x4c: 17743e230dd2SCorentin Chary kbd_put_keysym('5'); 17753e230dd2SCorentin Chary break; 17763e230dd2SCorentin Chary case 0x4d: 17773e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 17783e230dd2SCorentin Chary break; 17793e230dd2SCorentin Chary case 0x4f: 17803e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 17813e230dd2SCorentin Chary break; 17823e230dd2SCorentin Chary case 0x50: 17833e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 17843e230dd2SCorentin Chary break; 17853e230dd2SCorentin Chary case 0x51: 17863e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 17873e230dd2SCorentin Chary break; 17883e230dd2SCorentin Chary case 0x52: 17893e230dd2SCorentin Chary kbd_put_keysym('0'); 17903e230dd2SCorentin Chary break; 17913e230dd2SCorentin Chary case 0x53: 17923e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 17933e230dd2SCorentin Chary break; 17943e230dd2SCorentin Chary 17953e230dd2SCorentin Chary case 0xb5: 17963e230dd2SCorentin Chary kbd_put_keysym('/'); 17973e230dd2SCorentin Chary break; 17983e230dd2SCorentin Chary case 0x37: 17993e230dd2SCorentin Chary kbd_put_keysym('*'); 18003e230dd2SCorentin Chary break; 18013e230dd2SCorentin Chary case 0x4a: 18023e230dd2SCorentin Chary kbd_put_keysym('-'); 18033e230dd2SCorentin Chary break; 18043e230dd2SCorentin Chary case 0x4e: 18053e230dd2SCorentin Chary kbd_put_keysym('+'); 18063e230dd2SCorentin Chary break; 18073e230dd2SCorentin Chary case 0x9c: 18083e230dd2SCorentin Chary kbd_put_keysym('\n'); 18093e230dd2SCorentin Chary break; 18103e230dd2SCorentin Chary 18113e230dd2SCorentin Chary default: 1812e26437c2SGerd Hoffmann if (control) { 1813e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1814e26437c2SGerd Hoffmann } else { 18153e230dd2SCorentin Chary kbd_put_keysym(sym); 1816e26437c2SGerd Hoffmann } 18173e230dd2SCorentin Chary break; 18183e230dd2SCorentin Chary } 18193e230dd2SCorentin Chary } 18203e230dd2SCorentin Chary } 18213e230dd2SCorentin Chary } 18223e230dd2SCorentin Chary 18237bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18247bc9318bSGerd Hoffmann { 18257bc9318bSGerd Hoffmann static const int keycodes[] = { 18267bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 18277bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 18287bc9318bSGerd Hoffmann }; 18297bc9318bSGerd Hoffmann int i, keycode; 18307bc9318bSGerd Hoffmann 183181c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 18327bc9318bSGerd Hoffmann return; 18337bc9318bSGerd Hoffmann } 18347bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 18357bc9318bSGerd Hoffmann keycode = keycodes[i]; 18367bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 18377bc9318bSGerd Hoffmann continue; 18387bc9318bSGerd Hoffmann } 18398d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1840c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 18417bc9318bSGerd Hoffmann } 18427bc9318bSGerd Hoffmann } 18437bc9318bSGerd Hoffmann 184440066175SGerd Hoffmann static const char *code2name(int keycode) 184540066175SGerd Hoffmann { 1846977c736fSMarkus Armbruster return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); 184740066175SGerd Hoffmann } 184840066175SGerd Hoffmann 18493e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 18503e230dd2SCorentin Chary { 18513e230dd2SCorentin Chary int keycode; 18523e230dd2SCorentin Chary int lsym = sym; 18533e230dd2SCorentin Chary 185481c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 18553e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 18563e230dd2SCorentin Chary } 18573e230dd2SCorentin Chary 18583e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 185940066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 18603e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18613e230dd2SCorentin Chary } 18623e230dd2SCorentin Chary 18633e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 18643e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 18653e230dd2SCorentin Chary { 18663e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 186740066175SGerd Hoffmann if (keyboard_layout) { 18683e230dd2SCorentin Chary key_event(vs, down, sym); 186940066175SGerd Hoffmann } else { 187040066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 18713e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18723e230dd2SCorentin Chary } 187340066175SGerd Hoffmann } 18743e230dd2SCorentin Chary 18753e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1876bea60dd7SPeter Lieven int x, int y, int w, int h) 18773e230dd2SCorentin Chary { 18783e230dd2SCorentin Chary vs->need_update = 1; 1879bea60dd7SPeter Lieven 1880bea60dd7SPeter Lieven if (incremental) { 1881bea60dd7SPeter Lieven return; 18823e230dd2SCorentin Chary } 1883bea60dd7SPeter Lieven 188407535a89SStephan Kulow vs->force_update = 1; 1885f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 18863e230dd2SCorentin Chary } 18873e230dd2SCorentin Chary 18883e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18893e230dd2SCorentin Chary { 1890bd023f95SCorentin Chary vnc_lock_output(vs); 18913e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18923e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18933e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1894d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1895bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1896bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 18973e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1898bd023f95SCorentin Chary vnc_unlock_output(vs); 18993e230dd2SCorentin Chary vnc_flush(vs); 19003e230dd2SCorentin Chary } 19013e230dd2SCorentin Chary 19023e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19033e230dd2SCorentin Chary { 1904bd023f95SCorentin Chary vnc_lock_output(vs); 19053e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19063e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19073e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1908d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1909bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1910bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19113e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1912bd023f95SCorentin Chary vnc_unlock_output(vs); 19133e230dd2SCorentin Chary vnc_flush(vs); 19143e230dd2SCorentin Chary } 19153e230dd2SCorentin Chary 19163e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19173e230dd2SCorentin Chary { 19183e230dd2SCorentin Chary int i; 19193e230dd2SCorentin Chary unsigned int enc = 0; 19203e230dd2SCorentin Chary 19213e230dd2SCorentin Chary vs->features = 0; 19223e230dd2SCorentin Chary vs->vnc_encoding = 0; 1923d1af0e05SCorentin Chary vs->tight.compression = 9; 1924d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19253e230dd2SCorentin Chary vs->absolute = -1; 19263e230dd2SCorentin Chary 19273e230dd2SCorentin Chary /* 19283e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1929e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 19303e230dd2SCorentin Chary * will be set at the end of the loop. 19313e230dd2SCorentin Chary */ 19323e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 19333e230dd2SCorentin Chary enc = encodings[i]; 19343e230dd2SCorentin Chary switch (enc) { 19353e230dd2SCorentin Chary case VNC_ENCODING_RAW: 19363e230dd2SCorentin Chary vs->vnc_encoding = enc; 19373e230dd2SCorentin Chary break; 19383e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 19393e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 19403e230dd2SCorentin Chary break; 19413e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 19423e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 19433e230dd2SCorentin Chary vs->vnc_encoding = enc; 19443e230dd2SCorentin Chary break; 19453e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 19463e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 19473e230dd2SCorentin Chary vs->vnc_encoding = enc; 19483e230dd2SCorentin Chary break; 1949fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1950efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1951efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1952efe556adSCorentin Chary vs->vnc_encoding = enc; 1953efe556adSCorentin Chary break; 1954fe3e7f2dSJoel Martin #endif 19553e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 19563e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 19573e230dd2SCorentin Chary vs->vnc_encoding = enc; 19583e230dd2SCorentin Chary break; 1959148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1960148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1961148954faSCorentin Chary vs->vnc_encoding = enc; 1962148954faSCorentin Chary break; 1963148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1964148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1965148954faSCorentin Chary vs->vnc_encoding = enc; 1966148954faSCorentin Chary break; 19673e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 19683e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 19693e230dd2SCorentin Chary break; 19703e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 19713e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 19723e230dd2SCorentin Chary break; 19733e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 19743e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 197591ec41dcSFrediano Ziglio if (vs->vd->cursor) { 197691ec41dcSFrediano Ziglio vnc_cursor_define(vs); 197791ec41dcSFrediano Ziglio } 19783e230dd2SCorentin Chary break; 19793e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 19803e230dd2SCorentin Chary send_ext_key_event_ack(vs); 19813e230dd2SCorentin Chary break; 19823e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 19833e230dd2SCorentin Chary send_ext_audio_ack(vs); 19843e230dd2SCorentin Chary break; 19853e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19863e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19873e230dd2SCorentin Chary break; 1988ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 1989ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 1990ab99e5c1SLei Li break; 19913e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1992d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 19933e230dd2SCorentin Chary break; 19943e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1995b31f519eSCorentin Chary if (vs->vd->lossy) { 1996d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1997b31f519eSCorentin Chary } 19983e230dd2SCorentin Chary break; 19993e230dd2SCorentin Chary default: 20003e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 20013e230dd2SCorentin Chary break; 20023e230dd2SCorentin Chary } 20033e230dd2SCorentin Chary } 20043e230dd2SCorentin Chary vnc_desktop_resize(vs); 20059e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2006ab99e5c1SLei Li vnc_led_state_change(vs); 20073e230dd2SCorentin Chary } 20083e230dd2SCorentin Chary 20093e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20103e230dd2SCorentin Chary { 20119f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20129f64916dSGerd Hoffmann 20139f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20143e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20153e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20163e230dd2SCorentin Chary } else { 20173e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20183e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20193e230dd2SCorentin Chary } 20203e230dd2SCorentin Chary } 20213e230dd2SCorentin Chary 20220c426e45SAlexander Graf static void send_color_map(VncState *vs) 20230c426e45SAlexander Graf { 20240c426e45SAlexander Graf int i; 20250c426e45SAlexander Graf 20260c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 20270c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 20280c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 20290c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 20300c426e45SAlexander Graf 20310c426e45SAlexander Graf for (i = 0; i < 256; i++) { 20320c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 20330c426e45SAlexander Graf 20340c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 20350c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 20360c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 20370c426e45SAlexander Graf } 20380c426e45SAlexander Graf } 20390c426e45SAlexander Graf 2040ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 20413e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20423e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20433e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20443e230dd2SCorentin Chary { 20453e230dd2SCorentin Chary if (!true_color_flag) { 20460c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 20470c426e45SAlexander Graf bits_per_pixel = 8; 20480c426e45SAlexander Graf red_max = 7; 20490c426e45SAlexander Graf green_max = 7; 20500c426e45SAlexander Graf blue_max = 3; 20510c426e45SAlexander Graf red_shift = 0; 20520c426e45SAlexander Graf green_shift = 3; 20530c426e45SAlexander Graf blue_shift = 6; 20543e230dd2SCorentin Chary } 20553e230dd2SCorentin Chary 2056e6908bfeSPetr Matousek switch (bits_per_pixel) { 2057e6908bfeSPetr Matousek case 8: 2058e6908bfeSPetr Matousek case 16: 2059e6908bfeSPetr Matousek case 32: 2060e6908bfeSPetr Matousek break; 2061e6908bfeSPetr Matousek default: 2062e6908bfeSPetr Matousek vnc_client_error(vs); 2063e6908bfeSPetr Matousek return; 2064e6908bfeSPetr Matousek } 2065e6908bfeSPetr Matousek 20664c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 20677c9209e7SCédric Le Goater vs->client_pf.rbits = ctpopl(red_max); 20689f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 20699f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 20704c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 20717c9209e7SCédric Le Goater vs->client_pf.gbits = ctpopl(green_max); 20729f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 20739f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 20744c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 20757c9209e7SCédric Le Goater vs->client_pf.bbits = ctpopl(blue_max); 20769f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 20779f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 20789f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 20799f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 20809f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 20819f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 20823e230dd2SCorentin Chary 20830c426e45SAlexander Graf if (!true_color_flag) { 20840c426e45SAlexander Graf send_color_map(vs); 20850c426e45SAlexander Graf } 20860c426e45SAlexander Graf 20873e230dd2SCorentin Chary set_pixel_conversion(vs); 20883e230dd2SCorentin Chary 20891d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 20901d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 20913e230dd2SCorentin Chary } 20923e230dd2SCorentin Chary 20933e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 20943e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 20953e230dd2SCorentin Chary 20969f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 20979f64916dSGerd Hoffmann 20989f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 20999f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 21003e230dd2SCorentin Chary 21013e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 21023e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 21033e230dd2SCorentin Chary #else 21043e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21053e230dd2SCorentin Chary #endif 21063e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21079f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21089f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21099f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21109f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21119f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21129f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21139f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21143e230dd2SCorentin Chary 21153e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21163e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21173e230dd2SCorentin Chary } 21183e230dd2SCorentin Chary 21193e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21203e230dd2SCorentin Chary { 21213e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 21223e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2123bd023f95SCorentin Chary vnc_lock_output(vs); 21243e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 21253e230dd2SCorentin Chary vnc_write_u8(vs, 0); 21263e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2127d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2128bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2129bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2130d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 21313e230dd2SCorentin Chary pixel_format_message(vs); 2132bd023f95SCorentin Chary vnc_unlock_output(vs); 21333e230dd2SCorentin Chary vnc_flush(vs); 21343e230dd2SCorentin Chary } else { 21353e230dd2SCorentin Chary set_pixel_conversion(vs); 21363e230dd2SCorentin Chary } 21373e230dd2SCorentin Chary } 21383e230dd2SCorentin Chary 21393e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 21403e230dd2SCorentin Chary { 21413e230dd2SCorentin Chary int i; 21423e230dd2SCorentin Chary uint16_t limit; 21433e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21443e230dd2SCorentin Chary 21453e230dd2SCorentin Chary if (data[0] > 3) { 21460f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21473e230dd2SCorentin Chary } 21483e230dd2SCorentin Chary 21493e230dd2SCorentin Chary switch (data[0]) { 21503e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21513e230dd2SCorentin Chary if (len == 1) 21523e230dd2SCorentin Chary return 20; 21533e230dd2SCorentin Chary 2154ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 21553e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 21563e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 21573e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 21583e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 21593e230dd2SCorentin Chary break; 21603e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 21613e230dd2SCorentin Chary if (len == 1) 21623e230dd2SCorentin Chary return 4; 21633e230dd2SCorentin Chary 21643e230dd2SCorentin Chary if (len == 4) { 21653e230dd2SCorentin Chary limit = read_u16(data, 2); 21663e230dd2SCorentin Chary if (limit > 0) 21673e230dd2SCorentin Chary return 4 + (limit * 4); 21683e230dd2SCorentin Chary } else 21693e230dd2SCorentin Chary limit = read_u16(data, 2); 21703e230dd2SCorentin Chary 21713e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 21723e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 21733e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 21743e230dd2SCorentin Chary } 21753e230dd2SCorentin Chary 21763e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 21773e230dd2SCorentin Chary break; 21783e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 21793e230dd2SCorentin Chary if (len == 1) 21803e230dd2SCorentin Chary return 10; 21813e230dd2SCorentin Chary 21823e230dd2SCorentin Chary framebuffer_update_request(vs, 21833e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 21843e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 21853e230dd2SCorentin Chary break; 21863e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 21873e230dd2SCorentin Chary if (len == 1) 21883e230dd2SCorentin Chary return 8; 21893e230dd2SCorentin Chary 21903e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 21913e230dd2SCorentin Chary break; 21923e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 21933e230dd2SCorentin Chary if (len == 1) 21943e230dd2SCorentin Chary return 6; 21953e230dd2SCorentin Chary 21963e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 21973e230dd2SCorentin Chary break; 21983e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2199f9a70e79SPeter Lieven if (len == 1) { 22003e230dd2SCorentin Chary return 8; 2201f9a70e79SPeter Lieven } 22023e230dd2SCorentin Chary if (len == 8) { 22033e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2204f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2205f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2206f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2207f9a70e79SPeter Lieven vnc_client_error(vs); 2208f9a70e79SPeter Lieven break; 2209f9a70e79SPeter Lieven } 2210f9a70e79SPeter Lieven if (dlen > 0) { 22113e230dd2SCorentin Chary return 8 + dlen; 22123e230dd2SCorentin Chary } 2213f9a70e79SPeter Lieven } 22143e230dd2SCorentin Chary 22153e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22163e230dd2SCorentin Chary break; 22173e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22183e230dd2SCorentin Chary if (len == 1) 22193e230dd2SCorentin Chary return 2; 22203e230dd2SCorentin Chary 22213e230dd2SCorentin Chary switch (read_u8(data, 1)) { 22223e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 22233e230dd2SCorentin Chary if (len == 2) 22243e230dd2SCorentin Chary return 12; 22253e230dd2SCorentin Chary 22263e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 22273e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 22283e230dd2SCorentin Chary break; 22293e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 22303e230dd2SCorentin Chary if (len == 2) 22313e230dd2SCorentin Chary return 4; 22323e230dd2SCorentin Chary 22333e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 22343e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 22353e230dd2SCorentin Chary audio_add(vs); 22363e230dd2SCorentin Chary break; 22373e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 22383e230dd2SCorentin Chary audio_del(vs); 22393e230dd2SCorentin Chary break; 22403e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22413e230dd2SCorentin Chary if (len == 4) 22423e230dd2SCorentin Chary return 10; 22433e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22443e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22453e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22463e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22473e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22483e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22493e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22503e230dd2SCorentin Chary default: 2251153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 22523e230dd2SCorentin Chary vnc_client_error(vs); 22533e230dd2SCorentin Chary break; 22543e230dd2SCorentin Chary } 22553e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 22563e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2257153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio channel coount %d\n", 22583e230dd2SCorentin Chary read_u8(data, 5)); 22593e230dd2SCorentin Chary vnc_client_error(vs); 22603e230dd2SCorentin Chary break; 22613e230dd2SCorentin Chary } 22623e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 22633e230dd2SCorentin Chary break; 22643e230dd2SCorentin Chary default: 2265153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 22663e230dd2SCorentin Chary vnc_client_error(vs); 22673e230dd2SCorentin Chary break; 22683e230dd2SCorentin Chary } 22693e230dd2SCorentin Chary break; 22703e230dd2SCorentin Chary 22713e230dd2SCorentin Chary default: 2272153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 22733e230dd2SCorentin Chary vnc_client_error(vs); 22743e230dd2SCorentin Chary break; 22753e230dd2SCorentin Chary } 22763e230dd2SCorentin Chary break; 22773e230dd2SCorentin Chary default: 2278153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 22793e230dd2SCorentin Chary vnc_client_error(vs); 22803e230dd2SCorentin Chary break; 22813e230dd2SCorentin Chary } 22823e230dd2SCorentin Chary 22833e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22843e230dd2SCorentin Chary return 0; 22853e230dd2SCorentin Chary } 22863e230dd2SCorentin Chary 22873e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 22883e230dd2SCorentin Chary { 22893e230dd2SCorentin Chary char buf[1024]; 22908cf36489SGerd Hoffmann VncShareMode mode; 22913e230dd2SCorentin Chary int size; 22923e230dd2SCorentin Chary 22938cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 22948cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 22958cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 22968cf36489SGerd Hoffmann /* 22978cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 22988cf36489SGerd Hoffmann * 22998cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 23008cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 23018cf36489SGerd Hoffmann * reasons. 23028cf36489SGerd Hoffmann */ 23038cf36489SGerd Hoffmann break; 23048cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23058cf36489SGerd Hoffmann /* 23068cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23078cf36489SGerd Hoffmann * 23088cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23098cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23108cf36489SGerd Hoffmann * as no exclusive connection exists. 23118cf36489SGerd Hoffmann * 23128cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23138cf36489SGerd Hoffmann */ 23148cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23158cf36489SGerd Hoffmann VncState *client; 23168cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23178cf36489SGerd Hoffmann if (vs == client) { 23188cf36489SGerd Hoffmann continue; 23198cf36489SGerd Hoffmann } 23208cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23218cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 23228cf36489SGerd Hoffmann continue; 23238cf36489SGerd Hoffmann } 23248cf36489SGerd Hoffmann vnc_disconnect_start(client); 23258cf36489SGerd Hoffmann } 23268cf36489SGerd Hoffmann } 23278cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 23288cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 23298cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23308cf36489SGerd Hoffmann return 0; 23318cf36489SGerd Hoffmann } 23328cf36489SGerd Hoffmann } 23338cf36489SGerd Hoffmann break; 23348cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 23358cf36489SGerd Hoffmann /* 23368cf36489SGerd Hoffmann * Policy: Shared connects only. 23378cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 23388cf36489SGerd Hoffmann * 23398cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 23408cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23418cf36489SGerd Hoffmann * client disconnect everybody else. 23428cf36489SGerd Hoffmann */ 23438cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23448cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23458cf36489SGerd Hoffmann return 0; 23468cf36489SGerd Hoffmann } 23478cf36489SGerd Hoffmann break; 23488cf36489SGerd Hoffmann } 23498cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23508cf36489SGerd Hoffmann 2351e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2352e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2353e5f34cddSGerd Hoffmann return 0; 2354e5f34cddSGerd Hoffmann } 2355e5f34cddSGerd Hoffmann 2356bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2357bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 23583e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 23593e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 23603e230dd2SCorentin Chary 23613e230dd2SCorentin Chary pixel_format_message(vs); 23623e230dd2SCorentin Chary 236397efe4f9SThomas Huth if (qemu_name) { 23643e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 236597efe4f9SThomas Huth if (size > sizeof(buf)) { 236697efe4f9SThomas Huth size = sizeof(buf); 236797efe4f9SThomas Huth } 236897efe4f9SThomas Huth } else { 23693e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 237097efe4f9SThomas Huth } 23713e230dd2SCorentin Chary 23723e230dd2SCorentin Chary vnc_write_u32(vs, size); 23733e230dd2SCorentin Chary vnc_write(vs, buf, size); 23743e230dd2SCorentin Chary vnc_flush(vs); 23753e230dd2SCorentin Chary 23763e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2377fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 23783e230dd2SCorentin Chary 23793e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23803e230dd2SCorentin Chary 23813e230dd2SCorentin Chary return 0; 23823e230dd2SCorentin Chary } 23833e230dd2SCorentin Chary 23843e230dd2SCorentin Chary void start_client_init(VncState *vs) 23853e230dd2SCorentin Chary { 23863e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 23873e230dd2SCorentin Chary } 23883e230dd2SCorentin Chary 23893e230dd2SCorentin Chary static void make_challenge(VncState *vs) 23903e230dd2SCorentin Chary { 23913e230dd2SCorentin Chary int i; 23923e230dd2SCorentin Chary 23933e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 23943e230dd2SCorentin Chary 23953e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 23963e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 23973e230dd2SCorentin Chary } 23983e230dd2SCorentin Chary 23993e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 24003e230dd2SCorentin Chary { 24013e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2402800567a6SDaniel P. Berrange size_t i, pwlen; 24033e230dd2SCorentin Chary unsigned char key[8]; 24043c9405a0SGerd Hoffmann time_t now = time(NULL); 240560928458SGonglei QCryptoCipher *cipher = NULL; 2406800567a6SDaniel P. Berrange Error *err = NULL; 24073e230dd2SCorentin Chary 24081cd20f8bSAnthony Liguori if (!vs->vd->password) { 2409*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); 24106bffdf0fSGerd Hoffmann goto reject; 24113e230dd2SCorentin Chary } 24123c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 2413*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); 24143c9405a0SGerd Hoffmann goto reject; 24153c9405a0SGerd Hoffmann } 24163e230dd2SCorentin Chary 24173e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24183e230dd2SCorentin Chary 24193e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24203e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24213e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24223e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2423800567a6SDaniel P. Berrange 2424800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2425800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2426800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2427800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2428800567a6SDaniel P. Berrange &err); 2429800567a6SDaniel P. Berrange if (!cipher) { 2430*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", 2431800567a6SDaniel P. Berrange error_get_pretty(err)); 2432800567a6SDaniel P. Berrange error_free(err); 2433800567a6SDaniel P. Berrange goto reject; 2434800567a6SDaniel P. Berrange } 2435800567a6SDaniel P. Berrange 2436a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2437800567a6SDaniel P. Berrange vs->challenge, 2438800567a6SDaniel P. Berrange response, 2439800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2440800567a6SDaniel P. Berrange &err) < 0) { 2441*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", 2442800567a6SDaniel P. Berrange error_get_pretty(err)); 2443800567a6SDaniel P. Berrange error_free(err); 2444800567a6SDaniel P. Berrange goto reject; 2445800567a6SDaniel P. Berrange } 24463e230dd2SCorentin Chary 24473e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 24483e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2449*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); 24506bffdf0fSGerd Hoffmann goto reject; 24516bffdf0fSGerd Hoffmann } else { 2452*7364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 24536bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 24546bffdf0fSGerd Hoffmann vnc_flush(vs); 24556bffdf0fSGerd Hoffmann 24566bffdf0fSGerd Hoffmann start_client_init(vs); 24576bffdf0fSGerd Hoffmann } 245860928458SGonglei 245960928458SGonglei qcrypto_cipher_free(cipher); 24606bffdf0fSGerd Hoffmann return 0; 24616bffdf0fSGerd Hoffmann 24626bffdf0fSGerd Hoffmann reject: 24633e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 24643e230dd2SCorentin Chary if (vs->minor >= 8) { 24653e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24663e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24673e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24683e230dd2SCorentin Chary } 24693e230dd2SCorentin Chary vnc_flush(vs); 24703e230dd2SCorentin Chary vnc_client_error(vs); 247160928458SGonglei qcrypto_cipher_free(cipher); 24723e230dd2SCorentin Chary return 0; 24733e230dd2SCorentin Chary } 24743e230dd2SCorentin Chary 24753e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 24763e230dd2SCorentin Chary { 24773e230dd2SCorentin Chary make_challenge(vs); 24783e230dd2SCorentin Chary /* Send client a 'random' challenge */ 24793e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 24803e230dd2SCorentin Chary vnc_flush(vs); 24813e230dd2SCorentin Chary 24823e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 24833e230dd2SCorentin Chary } 24843e230dd2SCorentin Chary 24853e230dd2SCorentin Chary 24863e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 24873e230dd2SCorentin Chary { 24883e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 24893e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 24907e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 2491*7364dbdaSDaniel P. Berrange trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); 24923e230dd2SCorentin Chary vnc_write_u32(vs, 1); 24933e230dd2SCorentin Chary if (vs->minor >= 8) { 24943e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24953e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24963e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24973e230dd2SCorentin Chary } 24983e230dd2SCorentin Chary vnc_client_error(vs); 24993e230dd2SCorentin Chary } else { /* Accept requested auth */ 2500*7364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25017e7e2ebcSDaniel P. Berrange switch (vs->auth) { 25023e230dd2SCorentin Chary case VNC_AUTH_NONE: 25033e230dd2SCorentin Chary if (vs->minor >= 8) { 25043e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25053e230dd2SCorentin Chary vnc_flush(vs); 25063e230dd2SCorentin Chary } 2507*7364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25083e230dd2SCorentin Chary start_client_init(vs); 25093e230dd2SCorentin Chary break; 25103e230dd2SCorentin Chary 25113e230dd2SCorentin Chary case VNC_AUTH_VNC: 25123e230dd2SCorentin Chary start_auth_vnc(vs); 25133e230dd2SCorentin Chary break; 25143e230dd2SCorentin Chary 25153e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25163e230dd2SCorentin Chary start_auth_vencrypt(vs); 25173e230dd2SCorentin Chary break; 25183e230dd2SCorentin Chary 25193e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25203e230dd2SCorentin Chary case VNC_AUTH_SASL: 25213e230dd2SCorentin Chary start_auth_sasl(vs); 25223e230dd2SCorentin Chary break; 25233e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 25243e230dd2SCorentin Chary 25253e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 2526*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); 25273e230dd2SCorentin Chary vnc_write_u8(vs, 1); 25283e230dd2SCorentin Chary if (vs->minor >= 8) { 25293e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25303e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25313e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25323e230dd2SCorentin Chary } 25333e230dd2SCorentin Chary vnc_client_error(vs); 25343e230dd2SCorentin Chary } 25353e230dd2SCorentin Chary } 25363e230dd2SCorentin Chary return 0; 25373e230dd2SCorentin Chary } 25383e230dd2SCorentin Chary 25393e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 25403e230dd2SCorentin Chary { 25413e230dd2SCorentin Chary char local[13]; 25423e230dd2SCorentin Chary 25433e230dd2SCorentin Chary memcpy(local, version, 12); 25443e230dd2SCorentin Chary local[12] = 0; 25453e230dd2SCorentin Chary 25463e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 25473e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 25483e230dd2SCorentin Chary vnc_client_error(vs); 25493e230dd2SCorentin Chary return 0; 25503e230dd2SCorentin Chary } 25513e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 25523e230dd2SCorentin Chary if (vs->major != 3 || 25533e230dd2SCorentin Chary (vs->minor != 3 && 25543e230dd2SCorentin Chary vs->minor != 4 && 25553e230dd2SCorentin Chary vs->minor != 5 && 25563e230dd2SCorentin Chary vs->minor != 7 && 25573e230dd2SCorentin Chary vs->minor != 8)) { 25583e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 25593e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25603e230dd2SCorentin Chary vnc_flush(vs); 25613e230dd2SCorentin Chary vnc_client_error(vs); 25623e230dd2SCorentin Chary return 0; 25633e230dd2SCorentin Chary } 25643e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 25653e230dd2SCorentin Chary * as equivalent to v3.3 by servers 25663e230dd2SCorentin Chary */ 25673e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 25683e230dd2SCorentin Chary vs->minor = 3; 25693e230dd2SCorentin Chary 25703e230dd2SCorentin Chary if (vs->minor == 3) { 2571*7364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25727e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 25737e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25743e230dd2SCorentin Chary vnc_flush(vs); 2575*7364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25763e230dd2SCorentin Chary start_client_init(vs); 25777e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 25783e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 25797e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25803e230dd2SCorentin Chary vnc_flush(vs); 25813e230dd2SCorentin Chary start_auth_vnc(vs); 25823e230dd2SCorentin Chary } else { 2583*7364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, 2584*7364dbdaSDaniel P. Berrange "Unsupported auth method for v3.3", ""); 25853e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25863e230dd2SCorentin Chary vnc_flush(vs); 25873e230dd2SCorentin Chary vnc_client_error(vs); 25883e230dd2SCorentin Chary } 25893e230dd2SCorentin Chary } else { 25903e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 25917e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 25923e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 25933e230dd2SCorentin Chary vnc_flush(vs); 25943e230dd2SCorentin Chary } 25953e230dd2SCorentin Chary 25963e230dd2SCorentin Chary return 0; 25973e230dd2SCorentin Chary } 25983e230dd2SCorentin Chary 2599999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2600999342a0SCorentin Chary { 2601999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2602999342a0SCorentin Chary 2603999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2604999342a0SCorentin Chary } 2605999342a0SCorentin Chary 26067d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26077d964c9dSCorentin Chary { 26087d964c9dSCorentin Chary int i, j; 26097d964c9dSCorentin Chary 26107d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26117d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26127d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26137d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26147d964c9dSCorentin Chary 2615207f328aSCorentin Chary for (j = y; j <= h; j++) { 2616207f328aSCorentin Chary for (i = x; i <= w; i++) { 26177d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26187d964c9dSCorentin Chary } 26197d964c9dSCorentin Chary } 26207d964c9dSCorentin Chary } 26217d964c9dSCorentin Chary 26227d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26237d964c9dSCorentin Chary { 26247d964c9dSCorentin Chary VncState *vs; 26257d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 26267d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 26277d964c9dSCorentin Chary int has_dirty = 0; 26287d964c9dSCorentin Chary 26295a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 26305a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 26317d964c9dSCorentin Chary 26327d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2633bc2429b9SCorentin Chary int j; 26347d964c9dSCorentin Chary 26357d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 26367d964c9dSCorentin Chary if (vs->output.offset) { 26377d964c9dSCorentin Chary continue; 26387d964c9dSCorentin Chary } 26397d964c9dSCorentin Chary 26407d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 26417d964c9dSCorentin Chary continue; 26427d964c9dSCorentin Chary } 2643207f328aSCorentin Chary 26447d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 26457d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2646b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2647b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2648b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 26497d964c9dSCorentin Chary } 26507d964c9dSCorentin Chary has_dirty++; 26517d964c9dSCorentin Chary } 2652207f328aSCorentin Chary 26537d964c9dSCorentin Chary return has_dirty; 26547d964c9dSCorentin Chary } 26557d964c9dSCorentin Chary 26567d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2657999342a0SCorentin Chary { 2658eebe0b79SGerd Hoffmann int width = MIN(pixman_image_get_width(vd->guest.fb), 2659eebe0b79SGerd Hoffmann pixman_image_get_width(vd->server)); 2660eebe0b79SGerd Hoffmann int height = MIN(pixman_image_get_height(vd->guest.fb), 2661eebe0b79SGerd Hoffmann pixman_image_get_height(vd->server)); 2662999342a0SCorentin Chary int x, y; 2663999342a0SCorentin Chary struct timeval res; 26647d964c9dSCorentin Chary int has_dirty = 0; 2665999342a0SCorentin Chary 26669f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26679f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2668999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2669999342a0SCorentin Chary 2670999342a0SCorentin Chary rect->updated = false; 2671999342a0SCorentin Chary } 2672999342a0SCorentin Chary } 2673999342a0SCorentin Chary 2674ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2675999342a0SCorentin Chary 2676999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 26777d964c9dSCorentin Chary return has_dirty; 2678999342a0SCorentin Chary } 2679999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2680999342a0SCorentin Chary 26819f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26829f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2683999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2684999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2685999342a0SCorentin Chary struct timeval min, max; 2686999342a0SCorentin Chary 2687999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2688999342a0SCorentin Chary continue ; 2689999342a0SCorentin Chary } 2690999342a0SCorentin Chary 2691999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2692ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2693999342a0SCorentin Chary 2694999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2695999342a0SCorentin Chary rect->freq = 0; 26967d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2697999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2698999342a0SCorentin Chary continue ; 2699999342a0SCorentin Chary } 2700999342a0SCorentin Chary 2701999342a0SCorentin Chary min = rect->times[rect->idx]; 2702999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2703ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2704999342a0SCorentin Chary 2705999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2706999342a0SCorentin Chary rect->freq /= count; 2707999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2708999342a0SCorentin Chary } 2709999342a0SCorentin Chary } 27107d964c9dSCorentin Chary return has_dirty; 2711999342a0SCorentin Chary } 2712999342a0SCorentin Chary 2713999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2714999342a0SCorentin Chary { 2715999342a0SCorentin Chary int i, j; 2716999342a0SCorentin Chary double total = 0; 2717999342a0SCorentin Chary int num = 0; 2718999342a0SCorentin Chary 27195a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 27205a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 2721999342a0SCorentin Chary 2722999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2723999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2724999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2725999342a0SCorentin Chary num++; 2726999342a0SCorentin Chary } 2727999342a0SCorentin Chary } 2728999342a0SCorentin Chary 2729999342a0SCorentin Chary if (num) { 2730999342a0SCorentin Chary return total / num; 2731999342a0SCorentin Chary } else { 2732999342a0SCorentin Chary return 0; 2733999342a0SCorentin Chary } 2734999342a0SCorentin Chary } 2735999342a0SCorentin Chary 2736999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2737999342a0SCorentin Chary { 2738999342a0SCorentin Chary VncRectStat *rect; 2739999342a0SCorentin Chary 2740999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2741999342a0SCorentin Chary if (rect->updated) { 2742999342a0SCorentin Chary return ; 2743999342a0SCorentin Chary } 2744999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2745999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2746999342a0SCorentin Chary rect->updated = true; 2747999342a0SCorentin Chary } 2748999342a0SCorentin Chary 27493e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 27503e230dd2SCorentin Chary { 2751bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2752bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2753bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2754bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2755eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 275612b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 27573e230dd2SCorentin Chary VncState *vs; 27583e230dd2SCorentin Chary int has_dirty = 0; 27599f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 27603e230dd2SCorentin Chary 276180e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2762999342a0SCorentin Chary 276380e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2764999342a0SCorentin Chary gettimeofday(&tv, NULL); 27657d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 276680e0c8c3SCorentin Chary } 2767999342a0SCorentin Chary 27683e230dd2SCorentin Chary /* 27693e230dd2SCorentin Chary * Walk through the guest dirty map. 27703e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 27713e230dd2SCorentin Chary * Update server dirty map. 27723e230dd2SCorentin Chary */ 2773bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2774eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2775eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2776bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2777bea60dd7SPeter Lieven server_stride); 27789f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 27799f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 27809f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 278112b316d4SPeter Lieven } else { 2782eb8934b0SGerd Hoffmann int guest_bpp = 2783eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 278412b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 278512b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2786659c90eeSMarc-André Lureau guest_ll = pixman_image_get_width(vd->guest.fb) * (DIV_ROUND_UP(guest_bpp, 8)); 27879f64916dSGerd Hoffmann } 2788eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 278912b316d4SPeter Lieven 279012b316d4SPeter Lieven for (;;) { 27913e230dd2SCorentin Chary int x; 279212b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 279312b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 279412b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 279512b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 279612b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 279712b316d4SPeter Lieven /* no more dirty bits */ 279812b316d4SPeter Lieven break; 279912b316d4SPeter Lieven } 280012b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 280112b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 280212b316d4SPeter Lieven 280312b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 28043e230dd2SCorentin Chary 28059f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2806bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28079f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28089f64916dSGerd Hoffmann } else { 280912b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28109f64916dSGerd Hoffmann } 281112b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28123e230dd2SCorentin Chary 281312b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 281412b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2815bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 281612b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28173e230dd2SCorentin Chary continue; 2818b4c85ddcSPeter Lieven } 2819eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2820eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2821bea60dd7SPeter Lieven } 2822eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2823bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28243e230dd2SCorentin Chary continue; 2825b4c85ddcSPeter Lieven } 2826bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 282712b316d4SPeter Lieven if (!vd->non_adaptive) { 282812b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 282912b316d4SPeter Lieven y, &tv); 283012b316d4SPeter Lieven } 28313e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 283212b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 28333e230dd2SCorentin Chary } 28343e230dd2SCorentin Chary has_dirty++; 28353e230dd2SCorentin Chary } 283612b316d4SPeter Lieven 283712b316d4SPeter Lieven y++; 28383e230dd2SCorentin Chary } 28399f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 28403e230dd2SCorentin Chary return has_dirty; 28413e230dd2SCorentin Chary } 28423e230dd2SCorentin Chary 28430f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 28443e230dd2SCorentin Chary { 28450f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 28463e230dd2SCorentin Chary VncState *vs, *vn; 28473e230dd2SCorentin Chary int has_dirty, rects = 0; 28483e230dd2SCorentin Chary 28499d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 28509d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 28519d6b2070SChenLiang return; 28529d6b2070SChenLiang } 28539d6b2070SChenLiang 28541d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 28553e230dd2SCorentin Chary 2856bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 28570f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2858bd023f95SCorentin Chary return; 2859bd023f95SCorentin Chary } 2860bd023f95SCorentin Chary 28613e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2862bd023f95SCorentin Chary vnc_unlock_display(vd); 28633e230dd2SCorentin Chary 28643e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 286538ee14f4SGerd Hoffmann rects += vnc_update_client(vs, has_dirty, false); 28663e230dd2SCorentin Chary /* vs might be free()ed here */ 28673e230dd2SCorentin Chary } 2868bd023f95SCorentin Chary 28693e230dd2SCorentin Chary if (has_dirty && rects) { 28700f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 28710f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 28720f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 28730f7b2864SGerd Hoffmann } 28743e230dd2SCorentin Chary } else { 28750f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 28760f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 28770f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 28783e230dd2SCorentin Chary } 28793e230dd2SCorentin Chary } 28803e230dd2SCorentin Chary } 28813e230dd2SCorentin Chary 288204d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 28832c8cf549SMichael Tokarev bool skipauth, bool websocket) 28843e230dd2SCorentin Chary { 2885fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 288690cd03a3SDaniel P. Berrange bool first_client = QTAILQ_EMPTY(&vd->clients); 28877d964c9dSCorentin Chary int i; 28887d964c9dSCorentin Chary 2889ad6374c4SDaniel P. Berrange trace_vnc_client_connect(vs, sioc); 289004d2529dSDaniel P. Berrange vs->sioc = sioc; 289104d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 289204d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 289304d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2894d616ccc5SGerd Hoffmann vs->vd = vd; 28957e7e2ebcSDaniel P. Berrange 289604d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 289704d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 289804d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2899543b9580SGerd Hoffmann 290004d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 290104d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 290204d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2903543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 290404d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2905543b9580SGerd Hoffmann #endif 2906543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 290704d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2908543b9580SGerd Hoffmann #endif 290904d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 291004d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 291104d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 291204d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2913543b9580SGerd Hoffmann 29147e7e2ebcSDaniel P. Berrange if (skipauth) { 29157e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29167e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29177e7e2ebcSDaniel P. Berrange } else { 2918f9148c8aSDaniel P. Berrange if (websocket) { 2919f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2920f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2921f9148c8aSDaniel P. Berrange } else { 29227e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29237e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29247e7e2ebcSDaniel P. Berrange } 2925f9148c8aSDaniel P. Berrange } 292604d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 292704d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 29287e7e2ebcSDaniel P. Berrange 29297267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 29307d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 2931fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 29327d964c9dSCorentin Chary } 29333e230dd2SCorentin Chary 293404d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 29350f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 293604d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 29377536ee4bSTim Hardeck if (websocket) { 29387536ee4bSTim Hardeck vs->websocket = 1; 293938e5756aSDaniel P. Berrange if (vd->tlscreds) { 294004d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294104d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 29423e305e4aSDaniel P. Berrange } else { 294304d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294404d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 29450057a0d5STim Hardeck } 294604d2529dSDaniel P. Berrange } else { 294704d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294804d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 29497536ee4bSTim Hardeck } 29503e230dd2SCorentin Chary 29513e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2952fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 29538cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 29543e230dd2SCorentin Chary 29553e230dd2SCorentin Chary vs->last_x = -1; 29563e230dd2SCorentin Chary vs->last_y = -1; 29573e230dd2SCorentin Chary 29583e230dd2SCorentin Chary vs->as.freq = 44100; 29593e230dd2SCorentin Chary vs->as.nchannels = 2; 29603e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 29613e230dd2SCorentin Chary vs->as.endianness = 0; 29623e230dd2SCorentin Chary 2963bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2964175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2965bd023f95SCorentin Chary 2966e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 2967c7628bffSGerd Hoffmann if (first_client) { 2968c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 2969c7628bffSGerd Hoffmann } 29703e230dd2SCorentin Chary 29711d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29723e230dd2SCorentin Chary 297390cd03a3SDaniel P. Berrange if (!vs->websocket) { 2974dbee9897SDaniel P. Berrange vnc_start_protocol(vs); 297590cd03a3SDaniel P. Berrange } 297690cd03a3SDaniel P. Berrange 297790cd03a3SDaniel P. Berrange if (vd->num_connecting > vd->connections_limit) { 297890cd03a3SDaniel P. Berrange QTAILQ_FOREACH(vs, &vd->clients, next) { 297990cd03a3SDaniel P. Berrange if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 298090cd03a3SDaniel P. Berrange vnc_disconnect_start(vs); 298190cd03a3SDaniel P. Berrange return; 298290cd03a3SDaniel P. Berrange } 298390cd03a3SDaniel P. Berrange } 298490cd03a3SDaniel P. Berrange } 298590cd03a3SDaniel P. Berrange } 298690cd03a3SDaniel P. Berrange 2987dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs) 298890cd03a3SDaniel P. Berrange { 29893e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 29903e230dd2SCorentin Chary vnc_flush(vs); 29913e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 29923e230dd2SCorentin Chary 29933e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 29943e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 29953e230dd2SCorentin Chary } 29963e230dd2SCorentin Chary 299704d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 299804d2529dSDaniel P. Berrange GIOCondition condition, 299904d2529dSDaniel P. Berrange void *opaque) 30003e230dd2SCorentin Chary { 3001bf01c179SDaniel P. Berrange VncDisplay *vd = opaque; 300204d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 300304d2529dSDaniel P. Berrange Error *err = NULL; 30044ee74fa7SDaniel P. Berrange bool isWebsock = false; 30054ee74fa7SDaniel P. Berrange size_t i; 30064ee74fa7SDaniel P. Berrange 30074ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30084ee74fa7SDaniel P. Berrange if (ioc == QIO_CHANNEL(vd->lwebsock[i])) { 30094ee74fa7SDaniel P. Berrange isWebsock = true; 30104ee74fa7SDaniel P. Berrange break; 30114ee74fa7SDaniel P. Berrange } 30124ee74fa7SDaniel P. Berrange } 30133e230dd2SCorentin Chary 301404d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 301504d2529dSDaniel P. Berrange if (sioc != NULL) { 301610bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), 30174ee74fa7SDaniel P. Berrange isWebsock ? "vnc-ws-server" : "vnc-server"); 301804d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 30194ee74fa7SDaniel P. Berrange vnc_connect(vd, sioc, false, isWebsock); 302004d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30218e9b0d24SDaniel P. Berrange } else { 302204d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 302304d2529dSDaniel P. Berrange error_free(err); 30247536ee4bSTim Hardeck } 30253e230dd2SCorentin Chary 302604d2529dSDaniel P. Berrange return TRUE; 30277536ee4bSTim Hardeck } 30287536ee4bSTim Hardeck 30297c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 30307c20b4a3SGerd Hoffmann .dpy_name = "vnc", 30310f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 30327c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3033c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 303434da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 30357c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 30367c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 30377c20b4a3SGerd Hoffmann }; 30387c20b4a3SGerd Hoffmann 303914f7143eSGerd Hoffmann void vnc_display_init(const char *id) 30403e230dd2SCorentin Chary { 3041bf01c179SDaniel P. Berrange VncDisplay *vd; 30424db14629SGerd Hoffmann 30434db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 30444db14629SGerd Hoffmann return; 30454db14629SGerd Hoffmann } 3046bf01c179SDaniel P. Berrange vd = g_malloc0(sizeof(*vd)); 30473e230dd2SCorentin Chary 3048bf01c179SDaniel P. Berrange vd->id = strdup(id); 3049bf01c179SDaniel P. Berrange QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); 30503e230dd2SCorentin Chary 3051bf01c179SDaniel P. Berrange QTAILQ_INIT(&vd->clients); 3052bf01c179SDaniel P. Berrange vd->expires = TIME_MAX; 30533e230dd2SCorentin Chary 305440066175SGerd Hoffmann if (keyboard_layout) { 305540066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 3056bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 305740066175SGerd Hoffmann } else { 3058bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 305940066175SGerd Hoffmann } 30603e230dd2SCorentin Chary 3061bf01c179SDaniel P. Berrange if (!vd->kbd_layout) { 30623e230dd2SCorentin Chary exit(1); 3063bf01c179SDaniel P. Berrange } 30643e230dd2SCorentin Chary 3065bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 3066bf01c179SDaniel P. Berrange vd->connections_limit = 32; 306712e29b16SDaniel P. Berrange 3068bf01c179SDaniel P. Berrange qemu_mutex_init(&vd->mutex); 3069bd023f95SCorentin Chary vnc_start_worker_thread(); 3070bd023f95SCorentin Chary 3071bf01c179SDaniel P. Berrange vd->dcl.ops = &dcl_ops; 3072bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 30733e230dd2SCorentin Chary } 30743e230dd2SCorentin Chary 30753e230dd2SCorentin Chary 3076bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd) 30773e230dd2SCorentin Chary { 30784ee74fa7SDaniel P. Berrange size_t i; 3079bf01c179SDaniel P. Berrange if (!vd) { 30803e230dd2SCorentin Chary return; 308104d2529dSDaniel P. Berrange } 3082bf01c179SDaniel P. Berrange vd->is_unix = false; 30834ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 30844ee74fa7SDaniel P. Berrange if (vd->lsock_tag[i]) { 30854ee74fa7SDaniel P. Berrange g_source_remove(vd->lsock_tag[i]); 30863e230dd2SCorentin Chary } 30874ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lsock[i])); 30884ee74fa7SDaniel P. Berrange } 30894ee74fa7SDaniel P. Berrange g_free(vd->lsock); 30904ee74fa7SDaniel P. Berrange g_free(vd->lsock_tag); 3091bf01c179SDaniel P. Berrange vd->lsock = NULL; 30922dc120beSGerd Hoffmann vd->lsock_tag = NULL; 30934ee74fa7SDaniel P. Berrange vd->nlsock = 0; 30944ee74fa7SDaniel P. Berrange 30954ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30964ee74fa7SDaniel P. Berrange if (vd->lwebsock_tag[i]) { 30974ee74fa7SDaniel P. Berrange g_source_remove(vd->lwebsock_tag[i]); 309804d2529dSDaniel P. Berrange } 30994ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lwebsock[i])); 31007536ee4bSTim Hardeck } 31014ee74fa7SDaniel P. Berrange g_free(vd->lwebsock); 31024ee74fa7SDaniel P. Berrange g_free(vd->lwebsock_tag); 3103bf01c179SDaniel P. Berrange vd->lwebsock = NULL; 31042dc120beSGerd Hoffmann vd->lwebsock_tag = NULL; 31054ee74fa7SDaniel P. Berrange vd->nlwebsock = 0; 31064ee74fa7SDaniel P. Berrange 3107bf01c179SDaniel P. Berrange vd->auth = VNC_AUTH_INVALID; 3108bf01c179SDaniel P. Berrange vd->subauth = VNC_AUTH_INVALID; 3109bf01c179SDaniel P. Berrange if (vd->tlscreds) { 3110bf01c179SDaniel P. Berrange object_unparent(OBJECT(vd->tlscreds)); 3111bf01c179SDaniel P. Berrange vd->tlscreds = NULL; 3112bf01c179SDaniel P. Berrange } 3113bf01c179SDaniel P. Berrange g_free(vd->tlsaclname); 3114bf01c179SDaniel P. Berrange vd->tlsaclname = NULL; 3115a54f0d2bSPierre Ossman if (vd->lock_key_sync) { 3116a54f0d2bSPierre Ossman qemu_remove_led_event_handler(vd->led); 31172dc120beSGerd Hoffmann vd->led = NULL; 3118a54f0d2bSPierre Ossman } 31193e230dd2SCorentin Chary } 31203e230dd2SCorentin Chary 312114f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31221cd20f8bSAnthony Liguori { 3123bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31241cd20f8bSAnthony Liguori 3125bf01c179SDaniel P. Berrange if (!vd) { 3126a6aa9d3eSLuiz Capitulino return -EINVAL; 31271cd20f8bSAnthony Liguori } 3128bf01c179SDaniel P. Berrange if (vd->auth == VNC_AUTH_NONE) { 3129cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 31307ea7d36eSMarkus Armbruster "password auth using '-vnc ${dpy},password'.\n"); 3131cf864569SGerd Hoffmann return -EINVAL; 31321cd20f8bSAnthony Liguori } 31331cd20f8bSAnthony Liguori 3134bf01c179SDaniel P. Berrange g_free(vd->password); 3135bf01c179SDaniel P. Berrange vd->password = g_strdup(password); 3136a6aa9d3eSLuiz Capitulino 3137a6aa9d3eSLuiz Capitulino return 0; 31383e230dd2SCorentin Chary } 31393e230dd2SCorentin Chary 314014f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31413c9405a0SGerd Hoffmann { 3142bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31433c9405a0SGerd Hoffmann 3144bf01c179SDaniel P. Berrange if (!vd) { 31451643f2b2SGerd Hoffmann return -EINVAL; 31461643f2b2SGerd Hoffmann } 31471643f2b2SGerd Hoffmann 3148bf01c179SDaniel P. Berrange vd->expires = expires; 31493c9405a0SGerd Hoffmann return 0; 31503c9405a0SGerd Hoffmann } 31513c9405a0SGerd Hoffmann 3152bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd) 31533e230dd2SCorentin Chary { 3154bd269ebcSMarkus Armbruster SocketAddress *addr; 315504d2529dSDaniel P. Berrange Error *err = NULL; 31563e230dd2SCorentin Chary 31574ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 31584ee74fa7SDaniel P. Berrange return; 31594ee74fa7SDaniel P. Berrange } 31604ee74fa7SDaniel P. Berrange 31614ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], &err); 316204d2529dSDaniel P. Berrange if (!addr) { 316333df7bf3SPaolo Bonzini return; 316404d2529dSDaniel P. Berrange } 316504d2529dSDaniel P. Berrange 3166bd269ebcSMarkus Armbruster if (addr->type != SOCKET_ADDRESS_TYPE_INET) { 3167bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 316833df7bf3SPaolo Bonzini return; 316904d2529dSDaniel P. Berrange } 317033df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3171bd269ebcSMarkus Armbruster addr->u.inet.host, 3172bd269ebcSMarkus Armbruster addr->u.inet.port); 3173bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 31743e230dd2SCorentin Chary } 31753e230dd2SCorentin Chary 31764db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 31774db14629SGerd Hoffmann .name = "vnc", 31784db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 31794db14629SGerd Hoffmann .implied_opt_name = "vnc", 31804db14629SGerd Hoffmann .desc = { 31814db14629SGerd Hoffmann { 31824db14629SGerd Hoffmann .name = "vnc", 31834db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31844db14629SGerd Hoffmann },{ 31854db14629SGerd Hoffmann .name = "websocket", 31864db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31874db14629SGerd Hoffmann },{ 31883e305e4aSDaniel P. Berrange .name = "tls-creds", 31893e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 31903e305e4aSDaniel P. Berrange },{ 31913e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 31924db14629SGerd Hoffmann .name = "x509", 31934db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31944db14629SGerd Hoffmann },{ 31954db14629SGerd Hoffmann .name = "share", 31964db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31974db14629SGerd Hoffmann },{ 31981d0d59feSGerd Hoffmann .name = "display", 31991d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 32001d0d59feSGerd Hoffmann },{ 32011d0d59feSGerd Hoffmann .name = "head", 32021d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32031d0d59feSGerd Hoffmann },{ 3204e5f34cddSGerd Hoffmann .name = "connections", 3205e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3206e5f34cddSGerd Hoffmann },{ 320788428b7aSGonglei .name = "to", 320888428b7aSGonglei .type = QEMU_OPT_NUMBER, 320988428b7aSGonglei },{ 321088428b7aSGonglei .name = "ipv4", 321188428b7aSGonglei .type = QEMU_OPT_BOOL, 321288428b7aSGonglei },{ 321388428b7aSGonglei .name = "ipv6", 321488428b7aSGonglei .type = QEMU_OPT_BOOL, 321588428b7aSGonglei },{ 32164db14629SGerd Hoffmann .name = "password", 32174db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32184db14629SGerd Hoffmann },{ 32194db14629SGerd Hoffmann .name = "reverse", 32204db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32214db14629SGerd Hoffmann },{ 32224db14629SGerd Hoffmann .name = "lock-key-sync", 32234db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32244db14629SGerd Hoffmann },{ 3225c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3226c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3227c5ce8333SGerd Hoffmann },{ 32284db14629SGerd Hoffmann .name = "sasl", 32294db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32304db14629SGerd Hoffmann },{ 32313e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32324db14629SGerd Hoffmann .name = "tls", 32334db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32344db14629SGerd Hoffmann },{ 32353e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32364db14629SGerd Hoffmann .name = "x509verify", 32378c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32384db14629SGerd Hoffmann },{ 32394db14629SGerd Hoffmann .name = "acl", 32404db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32414db14629SGerd Hoffmann },{ 32424db14629SGerd Hoffmann .name = "lossy", 32434db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32444db14629SGerd Hoffmann },{ 32454db14629SGerd Hoffmann .name = "non-adaptive", 32464db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32474db14629SGerd Hoffmann }, 32484db14629SGerd Hoffmann { /* end of list */ } 32494db14629SGerd Hoffmann }, 32504db14629SGerd Hoffmann }; 32514db14629SGerd Hoffmann 32520dd72e15SDaniel P. Berrange 32533e305e4aSDaniel P. Berrange static int 3254eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth, 3255eda24e18SDaniel P. Berrange int *subauth, 3256eda24e18SDaniel P. Berrange QCryptoTLSCreds *tlscreds, 32570dd72e15SDaniel P. Berrange bool password, 32580dd72e15SDaniel P. Berrange bool sasl, 32593e305e4aSDaniel P. Berrange bool websocket, 32603e305e4aSDaniel P. Berrange Error **errp) 32610dd72e15SDaniel P. Berrange { 32620dd72e15SDaniel P. Berrange /* 32630dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 32640dd72e15SDaniel P. Berrange * 32650dd72e15SDaniel P. Berrange * 1. none 32660dd72e15SDaniel P. Berrange * 2. vnc 32670dd72e15SDaniel P. Berrange * 3. sasl 32680dd72e15SDaniel P. Berrange * 32690dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 32700dd72e15SDaniel P. Berrange * 32710dd72e15SDaniel P. Berrange * 1. clear 32720dd72e15SDaniel P. Berrange * 2. tls 32730dd72e15SDaniel P. Berrange * 32740dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 32750dd72e15SDaniel P. Berrange * 32760dd72e15SDaniel P. Berrange * 1. anon 32770dd72e15SDaniel P. Berrange * 2. x509 32780dd72e15SDaniel P. Berrange * 32790dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 32800dd72e15SDaniel P. Berrange * 32810dd72e15SDaniel P. Berrange * 1. clear + none 32820dd72e15SDaniel P. Berrange * 2. clear + vnc 32830dd72e15SDaniel P. Berrange * 3. clear + sasl 32840dd72e15SDaniel P. Berrange * 4. tls + anon + none 32850dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 32860dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 32870dd72e15SDaniel P. Berrange * 7. tls + x509 + none 32880dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 32890dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 32900dd72e15SDaniel P. Berrange * 32910dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 32920dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 32930dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 32940dd72e15SDaniel P. Berrange * sub-auth types. 3295f9148c8aSDaniel P. Berrange * 3296f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3297f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3298f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3299f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3300f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3301f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3302f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3303f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3304f9148c8aSDaniel P. Berrange * 3305f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3306f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3307f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33080dd72e15SDaniel P. Berrange */ 3309eda24e18SDaniel P. Berrange if (websocket || !tlscreds) { 33100dd72e15SDaniel P. Berrange if (password) { 33110dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 3312eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VNC; 33130dd72e15SDaniel P. Berrange } else if (sasl) { 33140dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 3315eda24e18SDaniel P. Berrange *auth = VNC_AUTH_SASL; 33160dd72e15SDaniel P. Berrange } else { 33170dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 3318eda24e18SDaniel P. Berrange *auth = VNC_AUTH_NONE; 33190dd72e15SDaniel P. Berrange } 3320eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_INVALID; 3321f9148c8aSDaniel P. Berrange } else { 3322eda24e18SDaniel P. Berrange bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), 3323eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; 3324eda24e18SDaniel P. Berrange bool is_anon = object_dynamic_cast(OBJECT(tlscreds), 3325eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; 3326eda24e18SDaniel P. Berrange 3327eda24e18SDaniel P. Berrange if (!is_x509 && !is_anon) { 3328eda24e18SDaniel P. Berrange error_setg(errp, 3329eda24e18SDaniel P. Berrange "Unsupported TLS cred type %s", 3330eda24e18SDaniel P. Berrange object_get_typename(OBJECT(tlscreds))); 3331eda24e18SDaniel P. Berrange return -1; 3332eda24e18SDaniel P. Berrange } 3333eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VENCRYPT; 3334eda24e18SDaniel P. Berrange if (password) { 3335eda24e18SDaniel P. Berrange if (is_x509) { 3336eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 3337eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509VNC; 3338eda24e18SDaniel P. Berrange } else { 3339eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 3340eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSVNC; 3341eda24e18SDaniel P. Berrange } 3342eda24e18SDaniel P. Berrange 3343eda24e18SDaniel P. Berrange } else if (sasl) { 3344eda24e18SDaniel P. Berrange if (is_x509) { 3345eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 3346eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509SASL; 3347eda24e18SDaniel P. Berrange } else { 3348eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 3349eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSSASL; 3350eda24e18SDaniel P. Berrange } 3351eda24e18SDaniel P. Berrange } else { 3352eda24e18SDaniel P. Berrange if (is_x509) { 3353eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 3354eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509NONE; 3355eda24e18SDaniel P. Berrange } else { 3356eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 3357eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSNONE; 3358eda24e18SDaniel P. Berrange } 3359f9148c8aSDaniel P. Berrange } 33600dd72e15SDaniel P. Berrange } 33613e305e4aSDaniel P. Berrange return 0; 33620dd72e15SDaniel P. Berrange } 33630dd72e15SDaniel P. Berrange 33643e305e4aSDaniel P. Berrange 33653e305e4aSDaniel P. Berrange /* 33663e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 33673e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 33683e305e4aSDaniel P. Berrange */ 33693e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 33703e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 33713e305e4aSDaniel P. Berrange bool x509verify, 33723e305e4aSDaniel P. Berrange const char *dir, 33733e305e4aSDaniel P. Berrange const char *id, 33743e305e4aSDaniel P. Berrange Error **errp) 33753e305e4aSDaniel P. Berrange { 33763e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 33773e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 33783e305e4aSDaniel P. Berrange Object *creds; 33793e305e4aSDaniel P. Berrange Error *err = NULL; 33803e305e4aSDaniel P. Berrange 33813e305e4aSDaniel P. Berrange if (x509) { 33823e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 33833e305e4aSDaniel P. Berrange parent, 33843e305e4aSDaniel P. Berrange credsid, 33853e305e4aSDaniel P. Berrange &err, 33863e305e4aSDaniel P. Berrange "endpoint", "server", 33873e305e4aSDaniel P. Berrange "dir", dir, 33883e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 33893e305e4aSDaniel P. Berrange NULL); 33903e305e4aSDaniel P. Berrange } else { 33913e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 33923e305e4aSDaniel P. Berrange parent, 33933e305e4aSDaniel P. Berrange credsid, 33943e305e4aSDaniel P. Berrange &err, 33953e305e4aSDaniel P. Berrange "endpoint", "server", 33963e305e4aSDaniel P. Berrange NULL); 33973e305e4aSDaniel P. Berrange } 33983e305e4aSDaniel P. Berrange 33993e305e4aSDaniel P. Berrange g_free(credsid); 34003e305e4aSDaniel P. Berrange 34013e305e4aSDaniel P. Berrange if (err) { 34023e305e4aSDaniel P. Berrange error_propagate(errp, err); 34033e305e4aSDaniel P. Berrange return NULL; 34043e305e4aSDaniel P. Berrange } 34053e305e4aSDaniel P. Berrange 34063e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34073e305e4aSDaniel P. Berrange } 34083e305e4aSDaniel P. Berrange 34093e305e4aSDaniel P. Berrange 3410275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr, 3411275e0d61SDaniel P. Berrange bool websocket, 3412e5766eb4SGerd Hoffmann bool reverse, 3413275e0d61SDaniel P. Berrange int displaynum, 3414275e0d61SDaniel P. Berrange int to, 3415275e0d61SDaniel P. Berrange bool has_ipv4, 3416275e0d61SDaniel P. Berrange bool has_ipv6, 3417275e0d61SDaniel P. Berrange bool ipv4, 3418275e0d61SDaniel P. Berrange bool ipv6, 3419bd269ebcSMarkus Armbruster SocketAddress **retaddr, 3420275e0d61SDaniel P. Berrange Error **errp) 3421275e0d61SDaniel P. Berrange { 3422275e0d61SDaniel P. Berrange int ret = -1; 3423bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3424275e0d61SDaniel P. Berrange 3425bd269ebcSMarkus Armbruster addr = g_new0(SocketAddress, 1); 3426275e0d61SDaniel P. Berrange 3427275e0d61SDaniel P. Berrange if (strncmp(addrstr, "unix:", 5) == 0) { 3428bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_UNIX; 3429bd269ebcSMarkus Armbruster addr->u.q_unix.path = g_strdup(addrstr + 5); 3430275e0d61SDaniel P. Berrange 3431275e0d61SDaniel P. Berrange if (websocket) { 3432275e0d61SDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3433275e0d61SDaniel P. Berrange goto cleanup; 3434275e0d61SDaniel P. Berrange } 3435275e0d61SDaniel P. Berrange 3436275e0d61SDaniel P. Berrange if (to) { 3437275e0d61SDaniel P. Berrange error_setg(errp, "Port range not support with UNIX socket"); 3438275e0d61SDaniel P. Berrange goto cleanup; 3439275e0d61SDaniel P. Berrange } 3440275e0d61SDaniel P. Berrange ret = 0; 3441275e0d61SDaniel P. Berrange } else { 3442275e0d61SDaniel P. Berrange const char *port; 3443275e0d61SDaniel P. Berrange size_t hostlen; 3444275e0d61SDaniel P. Berrange unsigned long long baseport = 0; 3445275e0d61SDaniel P. Berrange InetSocketAddress *inet; 3446275e0d61SDaniel P. Berrange 3447275e0d61SDaniel P. Berrange port = strrchr(addrstr, ':'); 3448275e0d61SDaniel P. Berrange if (!port) { 3449275e0d61SDaniel P. Berrange if (websocket) { 3450275e0d61SDaniel P. Berrange hostlen = 0; 3451275e0d61SDaniel P. Berrange port = addrstr; 3452275e0d61SDaniel P. Berrange } else { 3453275e0d61SDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3454275e0d61SDaniel P. Berrange goto cleanup; 3455275e0d61SDaniel P. Berrange } 3456275e0d61SDaniel P. Berrange } else { 3457275e0d61SDaniel P. Berrange hostlen = port - addrstr; 3458275e0d61SDaniel P. Berrange port++; 3459275e0d61SDaniel P. Berrange if (*port == '\0') { 3460275e0d61SDaniel P. Berrange error_setg(errp, "vnc port cannot be empty"); 3461275e0d61SDaniel P. Berrange goto cleanup; 3462275e0d61SDaniel P. Berrange } 3463275e0d61SDaniel P. Berrange } 3464275e0d61SDaniel P. Berrange 3465bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_INET; 3466bd269ebcSMarkus Armbruster inet = &addr->u.inet; 3467275e0d61SDaniel P. Berrange if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { 3468275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr + 1, hostlen - 2); 3469275e0d61SDaniel P. Berrange } else { 3470275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr, hostlen); 3471275e0d61SDaniel P. Berrange } 3472275e0d61SDaniel P. Berrange /* plain VNC port is just an offset, for websocket 3473275e0d61SDaniel P. Berrange * port is absolute */ 3474275e0d61SDaniel P. Berrange if (websocket) { 3475275e0d61SDaniel P. Berrange if (g_str_equal(addrstr, "") || 3476275e0d61SDaniel P. Berrange g_str_equal(addrstr, "on")) { 3477396f935aSDaniel P. Berrange if (displaynum == -1) { 3478396f935aSDaniel P. Berrange error_setg(errp, "explicit websocket port is required"); 3479396f935aSDaniel P. Berrange goto cleanup; 3480396f935aSDaniel P. Berrange } 3481275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3482275e0d61SDaniel P. Berrange "%d", displaynum + 5700); 3483275e0d61SDaniel P. Berrange if (to) { 3484275e0d61SDaniel P. Berrange inet->has_to = true; 3485275e0d61SDaniel P. Berrange inet->to = to + 5700; 3486275e0d61SDaniel P. Berrange } 3487275e0d61SDaniel P. Berrange } else { 3488275e0d61SDaniel P. Berrange inet->port = g_strdup(port); 3489275e0d61SDaniel P. Berrange } 3490275e0d61SDaniel P. Berrange } else { 3491e5766eb4SGerd Hoffmann int offset = reverse ? 0 : 5900; 3492275e0d61SDaniel P. Berrange if (parse_uint_full(port, &baseport, 10) < 0) { 3493275e0d61SDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", port); 3494275e0d61SDaniel P. Berrange goto cleanup; 3495275e0d61SDaniel P. Berrange } 3496275e0d61SDaniel P. Berrange if (baseport > 65535 || 3497e5766eb4SGerd Hoffmann baseport + offset > 65535) { 3498275e0d61SDaniel P. Berrange error_setg(errp, "port %s out of range", port); 3499275e0d61SDaniel P. Berrange goto cleanup; 3500275e0d61SDaniel P. Berrange } 3501275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3502e5766eb4SGerd Hoffmann "%d", (int)baseport + offset); 3503275e0d61SDaniel P. Berrange 3504275e0d61SDaniel P. Berrange if (to) { 3505275e0d61SDaniel P. Berrange inet->has_to = true; 3506e5766eb4SGerd Hoffmann inet->to = to + offset; 3507275e0d61SDaniel P. Berrange } 3508275e0d61SDaniel P. Berrange } 3509275e0d61SDaniel P. Berrange 3510275e0d61SDaniel P. Berrange inet->ipv4 = ipv4; 3511275e0d61SDaniel P. Berrange inet->has_ipv4 = has_ipv4; 3512275e0d61SDaniel P. Berrange inet->ipv6 = ipv6; 3513275e0d61SDaniel P. Berrange inet->has_ipv6 = has_ipv6; 3514275e0d61SDaniel P. Berrange 3515275e0d61SDaniel P. Berrange ret = baseport; 3516275e0d61SDaniel P. Berrange } 3517275e0d61SDaniel P. Berrange 3518275e0d61SDaniel P. Berrange *retaddr = addr; 3519275e0d61SDaniel P. Berrange 3520275e0d61SDaniel P. Berrange cleanup: 3521275e0d61SDaniel P. Berrange if (ret < 0) { 3522bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 3523275e0d61SDaniel P. Berrange } 3524275e0d61SDaniel P. Berrange return ret; 3525275e0d61SDaniel P. Berrange } 3526275e0d61SDaniel P. Berrange 35279f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr, 35289f26f325SPhilippe Mathieu-Daudé size_t *retnsaddr) 35299f26f325SPhilippe Mathieu-Daudé { 35309f26f325SPhilippe Mathieu-Daudé size_t i; 35319f26f325SPhilippe Mathieu-Daudé 35329f26f325SPhilippe Mathieu-Daudé for (i = 0; i < *retnsaddr; i++) { 35339f26f325SPhilippe Mathieu-Daudé qapi_free_SocketAddress((*retsaddr)[i]); 35349f26f325SPhilippe Mathieu-Daudé } 35359f26f325SPhilippe Mathieu-Daudé g_free(*retsaddr); 35369f26f325SPhilippe Mathieu-Daudé 35379f26f325SPhilippe Mathieu-Daudé *retsaddr = NULL; 35389f26f325SPhilippe Mathieu-Daudé *retnsaddr = 0; 35399f26f325SPhilippe Mathieu-Daudé } 35409f26f325SPhilippe Mathieu-Daudé 3541275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts, 3542e5766eb4SGerd Hoffmann bool reverse, 3543bd269ebcSMarkus Armbruster SocketAddress ***retsaddr, 3544396f935aSDaniel P. Berrange size_t *retnsaddr, 3545bd269ebcSMarkus Armbruster SocketAddress ***retwsaddr, 3546396f935aSDaniel P. Berrange size_t *retnwsaddr, 3547275e0d61SDaniel P. Berrange Error **errp) 3548275e0d61SDaniel P. Berrange { 3549bd269ebcSMarkus Armbruster SocketAddress *saddr = NULL; 3550bd269ebcSMarkus Armbruster SocketAddress *wsaddr = NULL; 3551396f935aSDaniel P. Berrange QemuOptsIter addriter; 3552396f935aSDaniel P. Berrange const char *addr; 3553275e0d61SDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3554275e0d61SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 3555275e0d61SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 3556275e0d61SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3557275e0d61SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3558396f935aSDaniel P. Berrange int displaynum = -1; 3559396f935aSDaniel P. Berrange int ret = -1; 3560275e0d61SDaniel P. Berrange 3561275e0d61SDaniel P. Berrange *retsaddr = NULL; 3562396f935aSDaniel P. Berrange *retnsaddr = 0; 3563275e0d61SDaniel P. Berrange *retwsaddr = NULL; 3564396f935aSDaniel P. Berrange *retnwsaddr = 0; 3565275e0d61SDaniel P. Berrange 3566396f935aSDaniel P. Berrange addr = qemu_opt_get(opts, "vnc"); 3567396f935aSDaniel P. Berrange if (addr == NULL || g_str_equal(addr, "none")) { 3568396f935aSDaniel P. Berrange ret = 0; 3569396f935aSDaniel P. Berrange goto cleanup; 3570396f935aSDaniel P. Berrange } 3571396f935aSDaniel P. Berrange if (qemu_opt_get(opts, "websocket") && 3572275e0d61SDaniel P. Berrange !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3573275e0d61SDaniel P. Berrange error_setg(errp, 3574275e0d61SDaniel P. Berrange "SHA1 hash support is required for websockets"); 3575396f935aSDaniel P. Berrange goto cleanup; 3576275e0d61SDaniel P. Berrange } 3577275e0d61SDaniel P. Berrange 3578396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "vnc"); 3579396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3580396f935aSDaniel P. Berrange int rv; 3581e5766eb4SGerd Hoffmann rv = vnc_display_get_address(addr, false, reverse, 0, to, 3582275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3583275e0d61SDaniel P. Berrange ipv4, ipv6, 3584275e0d61SDaniel P. Berrange &saddr, errp); 3585396f935aSDaniel P. Berrange if (rv < 0) { 3586396f935aSDaniel P. Berrange goto cleanup; 3587275e0d61SDaniel P. Berrange } 3588396f935aSDaniel P. Berrange /* Historical compat - first listen address can be used 3589396f935aSDaniel P. Berrange * to set the default websocket port 3590396f935aSDaniel P. Berrange */ 3591396f935aSDaniel P. Berrange if (displaynum == -1) { 3592396f935aSDaniel P. Berrange displaynum = rv; 3593396f935aSDaniel P. Berrange } 3594bd269ebcSMarkus Armbruster *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1); 3595396f935aSDaniel P. Berrange (*retsaddr)[(*retnsaddr)++] = saddr; 3596396f935aSDaniel P. Berrange } 3597396f935aSDaniel P. Berrange 3598396f935aSDaniel P. Berrange /* If we had multiple primary displays, we don't do defaults 3599396f935aSDaniel P. Berrange * for websocket, and require explicit config instead. */ 3600396f935aSDaniel P. Berrange if (*retnsaddr > 1) { 3601396f935aSDaniel P. Berrange displaynum = -1; 3602396f935aSDaniel P. Berrange } 3603396f935aSDaniel P. Berrange 3604396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "websocket"); 3605396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3606e5766eb4SGerd Hoffmann if (vnc_display_get_address(addr, true, reverse, displaynum, to, 3607275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3608275e0d61SDaniel P. Berrange ipv4, ipv6, 3609275e0d61SDaniel P. Berrange &wsaddr, errp) < 0) { 3610396f935aSDaniel P. Berrange goto cleanup; 3611275e0d61SDaniel P. Berrange } 3612396f935aSDaniel P. Berrange 3613396f935aSDaniel P. Berrange /* Historical compat - if only a single listen address was 3614396f935aSDaniel P. Berrange * provided, then this is used to set the default listen 3615396f935aSDaniel P. Berrange * address for websocket too 3616396f935aSDaniel P. Berrange */ 3617396f935aSDaniel P. Berrange if (*retnsaddr == 1 && 3618bd269ebcSMarkus Armbruster (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET && 3619bd269ebcSMarkus Armbruster wsaddr->type == SOCKET_ADDRESS_TYPE_INET && 3620bd269ebcSMarkus Armbruster g_str_equal(wsaddr->u.inet.host, "") && 3621bd269ebcSMarkus Armbruster !g_str_equal((*retsaddr)[0]->u.inet.host, "")) { 3622bd269ebcSMarkus Armbruster g_free(wsaddr->u.inet.host); 3623bd269ebcSMarkus Armbruster wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host); 3624275e0d61SDaniel P. Berrange } 3625275e0d61SDaniel P. Berrange 3626bd269ebcSMarkus Armbruster *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1); 3627396f935aSDaniel P. Berrange (*retwsaddr)[(*retnwsaddr)++] = wsaddr; 3628396f935aSDaniel P. Berrange } 3629396f935aSDaniel P. Berrange 3630396f935aSDaniel P. Berrange ret = 0; 3631396f935aSDaniel P. Berrange cleanup: 3632396f935aSDaniel P. Berrange if (ret < 0) { 36339f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retsaddr, retnsaddr); 36349f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retwsaddr, retnwsaddr); 3635396f935aSDaniel P. Berrange } 3636396f935aSDaniel P. Berrange return ret; 3637275e0d61SDaniel P. Berrange } 3638275e0d61SDaniel P. Berrange 36398bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd, 3640bd269ebcSMarkus Armbruster SocketAddress **saddr, 3641396f935aSDaniel P. Berrange size_t nsaddr, 3642bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3643396f935aSDaniel P. Berrange size_t nwsaddr, 36448bd22f47SDaniel P. Berrange Error **errp) 36458bd22f47SDaniel P. Berrange { 36468bd22f47SDaniel P. Berrange /* connect to viewer */ 36478bd22f47SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3648396f935aSDaniel P. Berrange if (nwsaddr != 0) { 36498bd22f47SDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 36508bd22f47SDaniel P. Berrange return -1; 36518bd22f47SDaniel P. Berrange } 3652396f935aSDaniel P. Berrange if (nsaddr != 1) { 3653396f935aSDaniel P. Berrange error_setg(errp, "Expected a single address in reverse mode"); 3654396f935aSDaniel P. Berrange return -1; 3655396f935aSDaniel P. Berrange } 3656bd269ebcSMarkus Armbruster /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */ 3657bd269ebcSMarkus Armbruster vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX; 36588bd22f47SDaniel P. Berrange sioc = qio_channel_socket_new(); 36598bd22f47SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); 3660396f935aSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) { 36618bd22f47SDaniel P. Berrange return -1; 36628bd22f47SDaniel P. Berrange } 36638bd22f47SDaniel P. Berrange vnc_connect(vd, sioc, false, false); 36648bd22f47SDaniel P. Berrange object_unref(OBJECT(sioc)); 36658bd22f47SDaniel P. Berrange return 0; 36668bd22f47SDaniel P. Berrange } 36678bd22f47SDaniel P. Berrange 36688bd22f47SDaniel P. Berrange 36698bd22f47SDaniel P. Berrange static int vnc_display_listen_addr(VncDisplay *vd, 3670bd269ebcSMarkus Armbruster SocketAddress *addr, 36718bd22f47SDaniel P. Berrange const char *name, 36728bd22f47SDaniel P. Berrange QIOChannelSocket ***lsock, 36738bd22f47SDaniel P. Berrange guint **lsock_tag, 36748bd22f47SDaniel P. Berrange size_t *nlsock, 36758bd22f47SDaniel P. Berrange Error **errp) 36768bd22f47SDaniel P. Berrange { 367757a6d6d5SDaniel P. Berrange QIODNSResolver *resolver = qio_dns_resolver_get_instance(); 3678bd269ebcSMarkus Armbruster SocketAddress **rawaddrs = NULL; 367957a6d6d5SDaniel P. Berrange size_t nrawaddrs = 0; 368057a6d6d5SDaniel P. Berrange Error *listenerr = NULL; 3681396f935aSDaniel P. Berrange bool listening = false; 368257a6d6d5SDaniel P. Berrange size_t i; 36838bd22f47SDaniel P. Berrange 368457a6d6d5SDaniel P. Berrange if (qio_dns_resolver_lookup_sync(resolver, addr, &nrawaddrs, 368557a6d6d5SDaniel P. Berrange &rawaddrs, errp) < 0) { 36868bd22f47SDaniel P. Berrange return -1; 36878bd22f47SDaniel P. Berrange } 36888bd22f47SDaniel P. Berrange 368957a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 369057a6d6d5SDaniel P. Berrange QIOChannelSocket *sioc = qio_channel_socket_new(); 369157a6d6d5SDaniel P. Berrange 369257a6d6d5SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), name); 369357a6d6d5SDaniel P. Berrange if (qio_channel_socket_listen_sync( 369457a6d6d5SDaniel P. Berrange sioc, rawaddrs[i], listenerr == NULL ? &listenerr : NULL) < 0) { 36957bc4f084SMarc-André Lureau object_unref(OBJECT(sioc)); 369657a6d6d5SDaniel P. Berrange continue; 369757a6d6d5SDaniel P. Berrange } 3698396f935aSDaniel P. Berrange listening = true; 369957a6d6d5SDaniel P. Berrange (*nlsock)++; 370057a6d6d5SDaniel P. Berrange *lsock = g_renew(QIOChannelSocket *, *lsock, *nlsock); 370157a6d6d5SDaniel P. Berrange *lsock_tag = g_renew(guint, *lsock_tag, *nlsock); 370257a6d6d5SDaniel P. Berrange 370357a6d6d5SDaniel P. Berrange (*lsock)[*nlsock - 1] = sioc; 370457a6d6d5SDaniel P. Berrange (*lsock_tag)[*nlsock - 1] = 0; 370557a6d6d5SDaniel P. Berrange } 370657a6d6d5SDaniel P. Berrange 370757a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 3708bd269ebcSMarkus Armbruster qapi_free_SocketAddress(rawaddrs[i]); 370957a6d6d5SDaniel P. Berrange } 371057a6d6d5SDaniel P. Berrange g_free(rawaddrs); 371157a6d6d5SDaniel P. Berrange 371257a6d6d5SDaniel P. Berrange if (listenerr) { 3713396f935aSDaniel P. Berrange if (!listening) { 371457a6d6d5SDaniel P. Berrange error_propagate(errp, listenerr); 371557a6d6d5SDaniel P. Berrange return -1; 371657a6d6d5SDaniel P. Berrange } else { 371757a6d6d5SDaniel P. Berrange error_free(listenerr); 371857a6d6d5SDaniel P. Berrange } 371957a6d6d5SDaniel P. Berrange } 372057a6d6d5SDaniel P. Berrange 372157a6d6d5SDaniel P. Berrange for (i = 0; i < *nlsock; i++) { 372257a6d6d5SDaniel P. Berrange (*lsock_tag)[i] = qio_channel_add_watch( 372357a6d6d5SDaniel P. Berrange QIO_CHANNEL((*lsock)[i]), 37248bd22f47SDaniel P. Berrange G_IO_IN, vnc_listen_io, vd, NULL); 372557a6d6d5SDaniel P. Berrange } 37268bd22f47SDaniel P. Berrange 37278bd22f47SDaniel P. Berrange return 0; 37288bd22f47SDaniel P. Berrange } 37298bd22f47SDaniel P. Berrange 37308bd22f47SDaniel P. Berrange 37318bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd, 3732bd269ebcSMarkus Armbruster SocketAddress **saddr, 3733396f935aSDaniel P. Berrange size_t nsaddr, 3734bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3735396f935aSDaniel P. Berrange size_t nwsaddr, 37368bd22f47SDaniel P. Berrange Error **errp) 37378bd22f47SDaniel P. Berrange { 3738396f935aSDaniel P. Berrange size_t i; 37398bd22f47SDaniel P. Berrange 3740396f935aSDaniel P. Berrange for (i = 0; i < nsaddr; i++) { 3741396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, saddr[i], 37428bd22f47SDaniel P. Berrange "vnc-listen", 37438bd22f47SDaniel P. Berrange &vd->lsock, 37448bd22f47SDaniel P. Berrange &vd->lsock_tag, 37458bd22f47SDaniel P. Berrange &vd->nlsock, 37468bd22f47SDaniel P. Berrange errp) < 0) { 37478bd22f47SDaniel P. Berrange return -1; 37488bd22f47SDaniel P. Berrange } 3749396f935aSDaniel P. Berrange } 3750396f935aSDaniel P. Berrange for (i = 0; i < nwsaddr; i++) { 3751396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, wsaddr[i], 37528bd22f47SDaniel P. Berrange "vnc-ws-listen", 37538bd22f47SDaniel P. Berrange &vd->lwebsock, 37548bd22f47SDaniel P. Berrange &vd->lwebsock_tag, 37558bd22f47SDaniel P. Berrange &vd->nlwebsock, 37568bd22f47SDaniel P. Berrange errp) < 0) { 37578bd22f47SDaniel P. Berrange return -1; 37588bd22f47SDaniel P. Berrange } 3759396f935aSDaniel P. Berrange } 37608bd22f47SDaniel P. Berrange 37618bd22f47SDaniel P. Berrange return 0; 37628bd22f47SDaniel P. Berrange } 37638bd22f47SDaniel P. Berrange 37648bd22f47SDaniel P. Berrange 37654db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 37663e230dd2SCorentin Chary { 3767bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 37684db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3769bd269ebcSMarkus Armbruster SocketAddress **saddr = NULL, **wsaddr = NULL; 3770396f935aSDaniel P. Berrange size_t nsaddr, nwsaddr; 3771e2a11d9dSGonglei const char *share, *device_id; 37721d0d59feSGerd Hoffmann QemuConsole *con; 3773a2c72de0SGonglei bool password = false; 3774a2c72de0SGonglei bool reverse = false; 37753e305e4aSDaniel P. Berrange const char *credid; 3776a2c72de0SGonglei bool sasl = false; 3777d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 37783e230dd2SCorentin Chary int saslErr; 37793e230dd2SCorentin Chary #endif 37803e230dd2SCorentin Chary int acl = 0; 37813e230dd2SCorentin Chary int lock_key_sync = 1; 3782c5ce8333SGerd Hoffmann int key_delay_ms; 37833e230dd2SCorentin Chary 3784bf01c179SDaniel P. Berrange if (!vd) { 37852d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 37862d55f0e8SPaolo Bonzini return; 37872d55f0e8SPaolo Bonzini } 3788bf01c179SDaniel P. Berrange vnc_display_close(vd); 37894db14629SGerd Hoffmann 37904db14629SGerd Hoffmann if (!opts) { 37912d55f0e8SPaolo Bonzini return; 37924db14629SGerd Hoffmann } 3793275e0d61SDaniel P. Berrange 3794e5766eb4SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 3795e5766eb4SGerd Hoffmann if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr, 3796396f935aSDaniel P. Berrange &wsaddr, &nwsaddr, errp) < 0) { 3797275e0d61SDaniel P. Berrange goto fail; 3798275e0d61SDaniel P. Berrange } 3799275e0d61SDaniel P. Berrange 38004db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3801800567a6SDaniel P. Berrange if (password) { 3802800567a6SDaniel P. Berrange if (fips_get_state()) { 38032d55f0e8SPaolo Bonzini error_setg(errp, 38040f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 38050f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 38062d55f0e8SPaolo Bonzini "methods as an alternative"); 38071ce52c78SPaolo Bonzini goto fail; 38080f66998fSPaul Moore } 3809800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3810f844836dSGonglei QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) { 3811800567a6SDaniel P. Berrange error_setg(errp, 3812800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3813800567a6SDaniel P. Berrange goto fail; 3814800567a6SDaniel P. Berrange } 3815800567a6SDaniel P. Berrange } 38164db14629SGerd Hoffmann 38174db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3818d3b0db6dSAlexander Graf key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); 38194db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3820d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3821d169f04bSDaniel P. Berrange if (sasl) { 3822d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3823d169f04bSDaniel P. Berrange goto fail; 3824d169f04bSDaniel P. Berrange } 3825d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 38263e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 38273e305e4aSDaniel P. Berrange if (credid) { 38283e305e4aSDaniel P. Berrange Object *creds; 38293e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 38303e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 38313e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 38323e305e4aSDaniel P. Berrange error_setg(errp, 3833c62e90afSWolfgang Bumiller "'tls-creds' parameter is mutually exclusive with " 38343e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 38353e305e4aSDaniel P. Berrange goto fail; 38363e305e4aSDaniel P. Berrange } 38373e305e4aSDaniel P. Berrange 38383e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 38393e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 38403e305e4aSDaniel P. Berrange if (!creds) { 38413e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 38423e305e4aSDaniel P. Berrange credid); 38433e305e4aSDaniel P. Berrange goto fail; 38443e305e4aSDaniel P. Berrange } 3845bf01c179SDaniel P. Berrange vd->tlscreds = (QCryptoTLSCreds *) 38463e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 38473e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 3848bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38493e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 38503e305e4aSDaniel P. Berrange credid); 38513e305e4aSDaniel P. Berrange goto fail; 38523e305e4aSDaniel P. Berrange } 3853bf01c179SDaniel P. Berrange object_ref(OBJECT(vd->tlscreds)); 38543e305e4aSDaniel P. Berrange 3855bf01c179SDaniel P. Berrange if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 38563e305e4aSDaniel P. Berrange error_setg(errp, 38573e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 38583e305e4aSDaniel P. Berrange goto fail; 38593e305e4aSDaniel P. Berrange } 38603e305e4aSDaniel P. Berrange } else { 38613e305e4aSDaniel P. Berrange const char *path; 38623e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 38634db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 38643e305e4aSDaniel P. Berrange if (tls) { 38654db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 38663e305e4aSDaniel P. Berrange 38674db14629SGerd Hoffmann if (path) { 3868a2c72de0SGonglei x509 = true; 38693e305e4aSDaniel P. Berrange } else { 38703e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 38713e305e4aSDaniel P. Berrange if (path) { 38723e305e4aSDaniel P. Berrange x509 = true; 38733e305e4aSDaniel P. Berrange x509verify = true; 38743e305e4aSDaniel P. Berrange } 38753e305e4aSDaniel P. Berrange } 3876bf01c179SDaniel P. Berrange vd->tlscreds = vnc_display_create_creds(x509, 38773e305e4aSDaniel P. Berrange x509verify, 38783e305e4aSDaniel P. Berrange path, 3879bf01c179SDaniel P. Berrange vd->id, 38803e305e4aSDaniel P. Berrange errp); 3881bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38821ce52c78SPaolo Bonzini goto fail; 38833e230dd2SCorentin Chary } 38843e230dd2SCorentin Chary } 3885d169f04bSDaniel P. Berrange } 38864db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 38874db14629SGerd Hoffmann 38884db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 38894db14629SGerd Hoffmann if (share) { 38904db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 3891bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_IGNORE; 38924db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 3893bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 38944db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 3895bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 38968cf36489SGerd Hoffmann } else { 38972d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 38981ce52c78SPaolo Bonzini goto fail; 38998cf36489SGerd Hoffmann } 39004db14629SGerd Hoffmann } else { 3901bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39023e230dd2SCorentin Chary } 3903bf01c179SDaniel P. Berrange vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); 39043e230dd2SCorentin Chary 39054db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 3906bf01c179SDaniel P. Berrange vd->lossy = qemu_opt_get_bool(opts, "lossy", false); 39074db14629SGerd Hoffmann #endif 3908bf01c179SDaniel P. Berrange vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3909e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3910e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3911e22492d3SPeter Lieven * calculations otherwise */ 3912bf01c179SDaniel P. Berrange if (!vd->lossy) { 3913bf01c179SDaniel P. Berrange vd->non_adaptive = true; 3914e22492d3SPeter Lieven } 3915e22492d3SPeter Lieven 39163e305e4aSDaniel P. Berrange if (acl) { 3917bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3918bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup("vnc.x509dname"); 3919c8496408SGerd Hoffmann } else { 3920bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vd->id); 3921c8496408SGerd Hoffmann } 3922bf01c179SDaniel P. Berrange qemu_acl_init(vd->tlsaclname); 39233e230dd2SCorentin Chary } 39243e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39253e230dd2SCorentin Chary if (acl && sasl) { 3926c8496408SGerd Hoffmann char *aclname; 3927c8496408SGerd Hoffmann 3928bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3929c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3930c8496408SGerd Hoffmann } else { 3931bf01c179SDaniel P. Berrange aclname = g_strdup_printf("vnc.%s.username", vd->id); 3932c8496408SGerd Hoffmann } 3933bf01c179SDaniel P. Berrange vd->sasl.acl = qemu_acl_init(aclname); 3934c8496408SGerd Hoffmann g_free(aclname); 39353e230dd2SCorentin Chary } 39363e230dd2SCorentin Chary #endif 39373e230dd2SCorentin Chary 3938eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->auth, &vd->subauth, 3939eda24e18SDaniel P. Berrange vd->tlscreds, password, 3940eda24e18SDaniel P. Berrange sasl, false, errp) < 0) { 3941eda24e18SDaniel P. Berrange goto fail; 3942eda24e18SDaniel P. Berrange } 3943*7364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth); 3944eda24e18SDaniel P. Berrange 3945eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, 3946eda24e18SDaniel P. Berrange vd->tlscreds, password, 3947eda24e18SDaniel P. Berrange sasl, true, errp) < 0) { 39483e305e4aSDaniel P. Berrange goto fail; 39493e305e4aSDaniel P. Berrange } 3950*7364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth); 39513e230dd2SCorentin Chary 39523e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39533e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 39542d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 39553e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 39561ce52c78SPaolo Bonzini goto fail; 39573e230dd2SCorentin Chary } 39583e230dd2SCorentin Chary #endif 3959bf01c179SDaniel P. Berrange vd->lock_key_sync = lock_key_sync; 3960a54f0d2bSPierre Ossman if (lock_key_sync) { 3961a54f0d2bSPierre Ossman vd->led = qemu_add_led_event_handler(kbd_leds, vd); 3962a54f0d2bSPierre Ossman } 3963a54f0d2bSPierre Ossman vd->ledstate = 0; 3964bf01c179SDaniel P. Berrange vd->key_delay_ms = key_delay_ms; 39653e230dd2SCorentin Chary 39661d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 39671d0d59feSGerd Hoffmann if (device_id) { 39681d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3969f2c1d54cSGerd Hoffmann Error *err = NULL; 39701d0d59feSGerd Hoffmann 3971f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3972f2c1d54cSGerd Hoffmann if (err) { 3973f2c1d54cSGerd Hoffmann error_propagate(errp, err); 39741d0d59feSGerd Hoffmann goto fail; 39751d0d59feSGerd Hoffmann } 39761d0d59feSGerd Hoffmann } else { 39771d0d59feSGerd Hoffmann con = NULL; 39781d0d59feSGerd Hoffmann } 39791d0d59feSGerd Hoffmann 3980bf01c179SDaniel P. Berrange if (con != vd->dcl.con) { 3981bf01c179SDaniel P. Berrange unregister_displaychangelistener(&vd->dcl); 3982bf01c179SDaniel P. Berrange vd->dcl.con = con; 3983bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 39841d0d59feSGerd Hoffmann } 39851d0d59feSGerd Hoffmann 3986fa03cb7fSMarc-André Lureau if (saddr == NULL) { 3987fa03cb7fSMarc-André Lureau goto cleanup; 3988fa03cb7fSMarc-André Lureau } 3989fa03cb7fSMarc-André Lureau 39903e230dd2SCorentin Chary if (reverse) { 3991396f935aSDaniel P. Berrange if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 3992e0d03b8cSDaniel P. Berrange goto fail; 39933e230dd2SCorentin Chary } 39943e230dd2SCorentin Chary } else { 3995396f935aSDaniel P. Berrange if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 39963d00ac1aSCole Robinson goto fail; 39973d00ac1aSCole Robinson } 39983e230dd2SCorentin Chary } 3999e0d03b8cSDaniel P. Berrange 4000275e0d61SDaniel P. Berrange if (qemu_opt_get(opts, "to")) { 4001bf01c179SDaniel P. Berrange vnc_display_print_local_addr(vd); 400233df7bf3SPaolo Bonzini } 400333df7bf3SPaolo Bonzini 4004396f935aSDaniel P. Berrange cleanup: 40059f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&saddr, &nsaddr); 40069f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&wsaddr, &nwsaddr); 40072d55f0e8SPaolo Bonzini return; 40081ce52c78SPaolo Bonzini 40091ce52c78SPaolo Bonzini fail: 40104ee74fa7SDaniel P. Berrange vnc_display_close(vd); 4011396f935aSDaniel P. Berrange goto cleanup; 40123e230dd2SCorentin Chary } 401313661089SDaniel P. Berrange 401414f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 401513661089SDaniel P. Berrange { 4016bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 401704d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 401813661089SDaniel P. Berrange 4019bf01c179SDaniel P. Berrange if (!vd) { 4020d616ccc5SGerd Hoffmann return; 4021d616ccc5SGerd Hoffmann } 402204d2529dSDaniel P. Berrange 402304d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 402404d2529dSDaniel P. Berrange if (sioc) { 402510bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); 4026bf01c179SDaniel P. Berrange vnc_connect(vd, sioc, skipauth, false); 402704d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 402804d2529dSDaniel P. Berrange } 402913661089SDaniel P. Berrange } 40304db14629SGerd Hoffmann 40319634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 40322779672fSGonglei { 40332779672fSGonglei int i = 2; 40342779672fSGonglei char *id; 40352779672fSGonglei 40362779672fSGonglei id = g_strdup("default"); 40372779672fSGonglei while (qemu_opts_find(olist, id)) { 40382779672fSGonglei g_free(id); 40392779672fSGonglei id = g_strdup_printf("vnc%d", i++); 40402779672fSGonglei } 40412779672fSGonglei qemu_opts_set_id(opts, id); 40422779672fSGonglei } 40432779672fSGonglei 404470b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 40454db14629SGerd Hoffmann { 40464db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 404770b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 404881607cbfSGonglei const char *id; 40494db14629SGerd Hoffmann 405081607cbfSGonglei if (!opts) { 405181607cbfSGonglei return NULL; 405281607cbfSGonglei } 405381607cbfSGonglei 405481607cbfSGonglei id = qemu_opts_id(opts); 40554db14629SGerd Hoffmann if (!id) { 40564db14629SGerd Hoffmann /* auto-assign id if not present */ 40572779672fSGonglei vnc_auto_assign_id(olist, opts); 40589634f4e3SGerd Hoffmann } 40599634f4e3SGerd Hoffmann return opts; 40604db14629SGerd Hoffmann } 40614db14629SGerd Hoffmann 406228d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 40639634f4e3SGerd Hoffmann { 40649634f4e3SGerd Hoffmann Error *local_err = NULL; 40659634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 40669634f4e3SGerd Hoffmann 40679634f4e3SGerd Hoffmann assert(id); 40684db14629SGerd Hoffmann vnc_display_init(id); 40694db14629SGerd Hoffmann vnc_display_open(id, &local_err); 40704db14629SGerd Hoffmann if (local_err != NULL) { 4071c29b77f9SMarkus Armbruster error_reportf_err(local_err, "Failed to start VNC server: "); 40724db14629SGerd Hoffmann exit(1); 40734db14629SGerd Hoffmann } 40744db14629SGerd Hoffmann return 0; 40754db14629SGerd Hoffmann } 40764db14629SGerd Hoffmann 40774db14629SGerd Hoffmann static void vnc_register_config(void) 40784db14629SGerd Hoffmann { 40794db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 40804db14629SGerd Hoffmann } 408134294e2fSEduardo Habkost opts_init(vnc_register_config); 4082