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 273e230dd2SCorentin Chary #include "vnc.h" 28bd023f95SCorentin Chary #include "vnc-jobs.h" 2940066175SGerd Hoffmann #include "trace.h" 301d0d59feSGerd Hoffmann #include "hw/qdev.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" 401de7afc9SPaolo Bonzini #include "qemu/osdep.h" 418d447d10SGerd Hoffmann #include "ui/input.h" 42fb6ba0d5SWenchao Xia #include "qapi-event.h" 438e9b0d24SDaniel P. Berrange #include "crypto/hash.h" 443e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h" 453e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h" 463e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h" 473e230dd2SCorentin Chary 480f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 493e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 500f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 51999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 52999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 533e230dd2SCorentin Chary 543e230dd2SCorentin Chary #include "vnc_keysym.h" 55800567a6SDaniel P. Berrange #include "crypto/cipher.h" 563e230dd2SCorentin Chary 57d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays = 58d616ccc5SGerd Hoffmann QTAILQ_HEAD_INITIALIZER(vnc_displays); 593e230dd2SCorentin Chary 603e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 617bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs); 623e230dd2SCorentin Chary 638cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 648cf36489SGerd Hoffmann { 658cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 668cf36489SGerd Hoffmann static const char *mn[] = { 678cf36489SGerd Hoffmann [0] = "undefined", 688cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 698cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 708cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 718cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 728cf36489SGerd Hoffmann }; 7304d2529dSDaniel P. Berrange fprintf(stderr, "%s/%p: %s -> %s\n", __func__, 7404d2529dSDaniel P. Berrange vs->ioc, mn[vs->share_mode], mn[mode]); 758cf36489SGerd Hoffmann #endif 768cf36489SGerd Hoffmann 77e5f34cddSGerd Hoffmann switch (vs->share_mode) { 78e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 79e5f34cddSGerd Hoffmann vs->vd->num_connecting--; 80e5f34cddSGerd Hoffmann break; 81e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 82e5f34cddSGerd Hoffmann vs->vd->num_shared--; 83e5f34cddSGerd Hoffmann break; 84e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 858cf36489SGerd Hoffmann vs->vd->num_exclusive--; 86e5f34cddSGerd Hoffmann break; 87e5f34cddSGerd Hoffmann default: 88e5f34cddSGerd Hoffmann break; 898cf36489SGerd Hoffmann } 90e5f34cddSGerd Hoffmann 918cf36489SGerd Hoffmann vs->share_mode = mode; 92e5f34cddSGerd Hoffmann 93e5f34cddSGerd Hoffmann switch (vs->share_mode) { 94e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 95e5f34cddSGerd Hoffmann vs->vd->num_connecting++; 96e5f34cddSGerd Hoffmann break; 97e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 98e5f34cddSGerd Hoffmann vs->vd->num_shared++; 99e5f34cddSGerd Hoffmann break; 100e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 1018cf36489SGerd Hoffmann vs->vd->num_exclusive++; 102e5f34cddSGerd Hoffmann break; 103e5f34cddSGerd Hoffmann default: 104e5f34cddSGerd Hoffmann break; 1058cf36489SGerd Hoffmann } 1068cf36489SGerd Hoffmann } 1078cf36489SGerd Hoffmann 1083e230dd2SCorentin Chary 10904d2529dSDaniel P. Berrange static void vnc_init_basic_info(SocketAddress *addr, 11098481bfcSEric Blake VncBasicInfo *info, 11198481bfcSEric Blake Error **errp) 1123e230dd2SCorentin Chary { 11304d2529dSDaniel P. Berrange switch (addr->type) { 11404d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_INET: 11504d2529dSDaniel P. Berrange info->host = g_strdup(addr->u.inet->host); 11604d2529dSDaniel P. Berrange info->service = g_strdup(addr->u.inet->port); 11704d2529dSDaniel P. Berrange if (addr->u.inet->ipv6) { 11804d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 11904d2529dSDaniel P. Berrange } else { 12004d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 12104d2529dSDaniel P. Berrange } 12204d2529dSDaniel P. Berrange break; 1233e230dd2SCorentin Chary 12404d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_UNIX: 12504d2529dSDaniel P. Berrange info->host = g_strdup(""); 12604d2529dSDaniel P. Berrange info->service = g_strdup(addr->u.q_unix->path); 12704d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 12804d2529dSDaniel P. Berrange break; 12904d2529dSDaniel P. Berrange 13004d2529dSDaniel P. Berrange default: 13104d2529dSDaniel P. Berrange error_setg(errp, "Unsupported socket kind %d", 13204d2529dSDaniel P. Berrange addr->type); 13304d2529dSDaniel P. Berrange break; 13404d2529dSDaniel P. Berrange } 13504d2529dSDaniel P. Berrange 13698481bfcSEric Blake return; 1373e230dd2SCorentin Chary } 1383e230dd2SCorentin Chary 13904d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, 14004d2529dSDaniel P. Berrange VncBasicInfo *info, 14198481bfcSEric Blake Error **errp) 1423e230dd2SCorentin Chary { 14304d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 1443e230dd2SCorentin Chary 14504d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, errp); 14604d2529dSDaniel P. Berrange if (!addr) { 14798481bfcSEric Blake return; 1483e230dd2SCorentin Chary } 1493e230dd2SCorentin Chary 15004d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 15104d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 1523e230dd2SCorentin Chary } 1533e230dd2SCorentin Chary 15404d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, 15504d2529dSDaniel P. Berrange VncBasicInfo *info, 15698481bfcSEric Blake Error **errp) 1573e230dd2SCorentin Chary { 15804d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 1593e230dd2SCorentin Chary 16004d2529dSDaniel P. Berrange addr = qio_channel_socket_get_remote_address(ioc, errp); 16104d2529dSDaniel P. Berrange if (!addr) { 16298481bfcSEric Blake return; 1633e230dd2SCorentin Chary } 1643e230dd2SCorentin Chary 16504d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 16604d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 1673e230dd2SCorentin Chary } 1683e230dd2SCorentin Chary 1693e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1703e230dd2SCorentin Chary switch (vd->auth) { 1713e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1723e230dd2SCorentin Chary return "invalid"; 1733e230dd2SCorentin Chary case VNC_AUTH_NONE: 1743e230dd2SCorentin Chary return "none"; 1753e230dd2SCorentin Chary case VNC_AUTH_VNC: 1763e230dd2SCorentin Chary return "vnc"; 1773e230dd2SCorentin Chary case VNC_AUTH_RA2: 1783e230dd2SCorentin Chary return "ra2"; 1793e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1803e230dd2SCorentin Chary return "ra2ne"; 1813e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1823e230dd2SCorentin Chary return "tight"; 1833e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1843e230dd2SCorentin Chary return "ultra"; 1853e230dd2SCorentin Chary case VNC_AUTH_TLS: 1863e230dd2SCorentin Chary return "tls"; 1873e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1883e230dd2SCorentin Chary switch (vd->subauth) { 1893e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1903e230dd2SCorentin Chary return "vencrypt+plain"; 1913e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 1923e230dd2SCorentin Chary return "vencrypt+tls+none"; 1933e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 1943e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 1953e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 1963e230dd2SCorentin Chary return "vencrypt+tls+plain"; 1973e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 1983e230dd2SCorentin Chary return "vencrypt+x509+none"; 1993e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2003e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2013e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2023e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2033e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2043e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2053e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2063e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2073e230dd2SCorentin Chary default: 2083e230dd2SCorentin Chary return "vencrypt"; 2093e230dd2SCorentin Chary } 2103e230dd2SCorentin Chary case VNC_AUTH_SASL: 2113e230dd2SCorentin Chary return "sasl"; 2123e230dd2SCorentin Chary } 2133e230dd2SCorentin Chary return "unknown"; 2143e230dd2SCorentin Chary } 2153e230dd2SCorentin Chary 216d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd) 2173e230dd2SCorentin Chary { 218fb6ba0d5SWenchao Xia VncServerInfo *info; 21998481bfcSEric Blake Error *err = NULL; 2203e230dd2SCorentin Chary 221fb6ba0d5SWenchao Xia info = g_malloc(sizeof(*info)); 222ddf21908SEric Blake vnc_init_basic_info_from_server_addr(vd->lsock, 223ddf21908SEric Blake qapi_VncServerInfo_base(info), &err); 224fb6ba0d5SWenchao Xia info->has_auth = true; 225d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 22698481bfcSEric Blake if (err) { 22798481bfcSEric Blake qapi_free_VncServerInfo(info); 22898481bfcSEric Blake info = NULL; 22998481bfcSEric Blake error_free(err); 23098481bfcSEric Blake } 231fb6ba0d5SWenchao Xia return info; 2323e230dd2SCorentin Chary } 2333e230dd2SCorentin Chary 2343e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2353e230dd2SCorentin Chary { 2363e230dd2SCorentin Chary if (!client->info) { 2373e230dd2SCorentin Chary return; 2383e230dd2SCorentin Chary } 2393e230dd2SCorentin Chary 2403e305e4aSDaniel P. Berrange if (client->tls) { 2413e305e4aSDaniel P. Berrange client->info->x509_dname = 2423e305e4aSDaniel P. Berrange qcrypto_tls_session_get_peer_name(client->tls); 2433e305e4aSDaniel P. Berrange client->info->has_x509_dname = 2443e305e4aSDaniel P. Berrange client->info->x509_dname != NULL; 2453e230dd2SCorentin Chary } 2463e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2473e230dd2SCorentin Chary if (client->sasl.conn && 2483e230dd2SCorentin Chary client->sasl.username) { 249fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 250fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2513e230dd2SCorentin Chary } 2523e230dd2SCorentin Chary #endif 2533e230dd2SCorentin Chary } 2543e230dd2SCorentin Chary 2553e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2563e230dd2SCorentin Chary { 25798481bfcSEric Blake Error *err = NULL; 2583e230dd2SCorentin Chary 259fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 26004d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 261ddf21908SEric Blake qapi_VncClientInfo_base(client->info), 26298481bfcSEric Blake &err); 26398481bfcSEric Blake if (err) { 26498481bfcSEric Blake qapi_free_VncClientInfo(client->info); 26598481bfcSEric Blake client->info = NULL; 26698481bfcSEric Blake error_free(err); 267fb6ba0d5SWenchao Xia } 2683e230dd2SCorentin Chary } 2693e230dd2SCorentin Chary 270fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2713e230dd2SCorentin Chary { 272fb6ba0d5SWenchao Xia VncServerInfo *si; 2733e230dd2SCorentin Chary 2743e230dd2SCorentin Chary if (!vs->info) { 2753e230dd2SCorentin Chary return; 2763e230dd2SCorentin Chary } 2773e230dd2SCorentin Chary 278d616ccc5SGerd Hoffmann si = vnc_server_info_get(vs->vd); 279fb6ba0d5SWenchao Xia if (!si) { 2803e230dd2SCorentin Chary return; 2813e230dd2SCorentin Chary } 2823e230dd2SCorentin Chary 283fb6ba0d5SWenchao Xia switch (event) { 284fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 285ddf21908SEric Blake qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info), 286ddf21908SEric Blake &error_abort); 287fb6ba0d5SWenchao Xia break; 288fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 289fb6ba0d5SWenchao Xia qapi_event_send_vnc_initialized(si, vs->info, &error_abort); 290fb6ba0d5SWenchao Xia break; 291fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 292fb6ba0d5SWenchao Xia qapi_event_send_vnc_disconnected(si, vs->info, &error_abort); 293fb6ba0d5SWenchao Xia break; 294fb6ba0d5SWenchao Xia default: 295fb6ba0d5SWenchao Xia break; 296fb6ba0d5SWenchao Xia } 2973e230dd2SCorentin Chary 298fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 2993e230dd2SCorentin Chary } 3003e230dd2SCorentin Chary 3012b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3023e230dd2SCorentin Chary { 3032b54aa87SLuiz Capitulino VncClientInfo *info; 30404d2529dSDaniel P. Berrange Error *err = NULL; 3052b54aa87SLuiz Capitulino 3062b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 30704d2529dSDaniel P. Berrange 30804d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 30904d2529dSDaniel P. Berrange qapi_VncClientInfo_base(info), 31004d2529dSDaniel P. Berrange &err); 31104d2529dSDaniel P. Berrange if (err) { 31204d2529dSDaniel P. Berrange error_free(err); 31304d2529dSDaniel P. Berrange qapi_free_VncClientInfo(info); 31404d2529dSDaniel P. Berrange return NULL; 31504d2529dSDaniel P. Berrange } 31604d2529dSDaniel P. Berrange 317ddf21908SEric Blake info->websocket = client->websocket; 3183e230dd2SCorentin Chary 3193e305e4aSDaniel P. Berrange if (client->tls) { 3203e305e4aSDaniel P. Berrange info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); 3213e305e4aSDaniel P. Berrange info->has_x509_dname = info->x509_dname != NULL; 3222b54aa87SLuiz Capitulino } 3233e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3242b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3252b54aa87SLuiz Capitulino info->has_sasl_username = true; 3262b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3272b54aa87SLuiz Capitulino } 3283e230dd2SCorentin Chary #endif 3292b54aa87SLuiz Capitulino 3302b54aa87SLuiz Capitulino return info; 3313e230dd2SCorentin Chary } 3323e230dd2SCorentin Chary 333d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id) 334d616ccc5SGerd Hoffmann { 335d616ccc5SGerd Hoffmann VncDisplay *vd; 336d616ccc5SGerd Hoffmann 337d616ccc5SGerd Hoffmann if (id == NULL) { 338d616ccc5SGerd Hoffmann return QTAILQ_FIRST(&vnc_displays); 339d616ccc5SGerd Hoffmann } 340d616ccc5SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 341d616ccc5SGerd Hoffmann if (strcmp(id, vd->id) == 0) { 342d616ccc5SGerd Hoffmann return vd; 343d616ccc5SGerd Hoffmann } 344d616ccc5SGerd Hoffmann } 345d616ccc5SGerd Hoffmann return NULL; 346d616ccc5SGerd Hoffmann } 347d616ccc5SGerd Hoffmann 3482d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) 3492d29a436SGerd Hoffmann { 3502d29a436SGerd Hoffmann VncClientInfoList *cinfo, *prev = NULL; 3512d29a436SGerd Hoffmann VncState *client; 3522d29a436SGerd Hoffmann 3532d29a436SGerd Hoffmann QTAILQ_FOREACH(client, &vd->clients, next) { 3542d29a436SGerd Hoffmann cinfo = g_new0(VncClientInfoList, 1); 3552d29a436SGerd Hoffmann cinfo->value = qmp_query_vnc_client(client); 3562d29a436SGerd Hoffmann cinfo->next = prev; 3572d29a436SGerd Hoffmann prev = cinfo; 3582d29a436SGerd Hoffmann } 3592d29a436SGerd Hoffmann return prev; 3602d29a436SGerd Hoffmann } 3612d29a436SGerd Hoffmann 3622b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3633e230dd2SCorentin Chary { 3642b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 365d616ccc5SGerd Hoffmann VncDisplay *vd = vnc_display_find(NULL); 36604d2529dSDaniel P. Berrange SocketAddress *addr = NULL; 3673e230dd2SCorentin Chary 368bf7aa45eSGerd Hoffmann if (vd == NULL || !vd->enabled) { 3692b54aa87SLuiz Capitulino info->enabled = false; 3703e230dd2SCorentin Chary } else { 3712b54aa87SLuiz Capitulino info->enabled = true; 3722b54aa87SLuiz Capitulino 3732b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3742b54aa87SLuiz Capitulino info->has_clients = true; 3752d29a436SGerd Hoffmann info->clients = qmp_query_client_list(vd); 3763e230dd2SCorentin Chary 37704d2529dSDaniel P. Berrange if (vd->lsock == NULL) { 378417b0b88SPaolo Bonzini return info; 379417b0b88SPaolo Bonzini } 380417b0b88SPaolo Bonzini 38104d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock, errp); 38204d2529dSDaniel P. Berrange if (!addr) { 3832b54aa87SLuiz Capitulino goto out_error; 3842b54aa87SLuiz Capitulino } 3853e230dd2SCorentin Chary 38604d2529dSDaniel P. Berrange switch (addr->type) { 38704d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_INET: 38804d2529dSDaniel P. Berrange info->host = g_strdup(addr->u.inet->host); 38904d2529dSDaniel P. Berrange info->service = g_strdup(addr->u.inet->port); 39004d2529dSDaniel P. Berrange if (addr->u.inet->ipv6) { 39104d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 39204d2529dSDaniel P. Berrange } else { 39304d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 39404d2529dSDaniel P. Berrange } 39504d2529dSDaniel P. Berrange break; 39604d2529dSDaniel P. Berrange 39704d2529dSDaniel P. Berrange case SOCKET_ADDRESS_KIND_UNIX: 39804d2529dSDaniel P. Berrange info->host = g_strdup(""); 39904d2529dSDaniel P. Berrange info->service = g_strdup(addr->u.q_unix->path); 40004d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 40104d2529dSDaniel P. Berrange break; 40204d2529dSDaniel P. Berrange 40304d2529dSDaniel P. Berrange default: 40404d2529dSDaniel P. Berrange error_setg(errp, "Unsupported socket kind %d", 40504d2529dSDaniel P. Berrange addr->type); 4062b54aa87SLuiz Capitulino goto out_error; 4073e230dd2SCorentin Chary } 4082b54aa87SLuiz Capitulino 4092b54aa87SLuiz Capitulino info->has_host = true; 4102b54aa87SLuiz Capitulino info->has_service = true; 4112b54aa87SLuiz Capitulino info->has_family = true; 4122b54aa87SLuiz Capitulino 4132b54aa87SLuiz Capitulino info->has_auth = true; 414d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 4153e230dd2SCorentin Chary } 4162b54aa87SLuiz Capitulino 41704d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 4182b54aa87SLuiz Capitulino return info; 4192b54aa87SLuiz Capitulino 4202b54aa87SLuiz Capitulino out_error: 42104d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 4222b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4232b54aa87SLuiz Capitulino return NULL; 4243e230dd2SCorentin Chary } 4253e230dd2SCorentin Chary 42604d2529dSDaniel P. Berrange static VncBasicInfoList *qmp_query_server_entry(QIOChannelSocket *ioc, 4274478aa76SGerd Hoffmann bool websocket, 428df887684SGerd Hoffmann VncBasicInfoList *prev) 429df887684SGerd Hoffmann { 430df887684SGerd Hoffmann VncBasicInfoList *list; 431df887684SGerd Hoffmann VncBasicInfo *info; 43204d2529dSDaniel P. Berrange Error *err = NULL; 43304d2529dSDaniel P. Berrange SocketAddress *addr; 434df887684SGerd Hoffmann 43504d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, &err); 43604d2529dSDaniel P. Berrange if (!addr) { 43704d2529dSDaniel P. Berrange error_free(err); 438df887684SGerd Hoffmann return prev; 439df887684SGerd Hoffmann } 440df887684SGerd Hoffmann 441df887684SGerd Hoffmann info = g_new0(VncBasicInfo, 1); 44204d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, &err); 44304d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 44404d2529dSDaniel P. Berrange if (err) { 44504d2529dSDaniel P. Berrange qapi_free_VncBasicInfo(info); 44604d2529dSDaniel P. Berrange error_free(err); 44704d2529dSDaniel P. Berrange return prev; 44804d2529dSDaniel P. Berrange } 4494478aa76SGerd Hoffmann info->websocket = websocket; 450df887684SGerd Hoffmann 451df887684SGerd Hoffmann list = g_new0(VncBasicInfoList, 1); 452df887684SGerd Hoffmann list->value = info; 453df887684SGerd Hoffmann list->next = prev; 454df887684SGerd Hoffmann return list; 455df887684SGerd Hoffmann } 456df887684SGerd Hoffmann 457df887684SGerd Hoffmann static void qmp_query_auth(VncDisplay *vd, VncInfo2 *info) 458df887684SGerd Hoffmann { 459df887684SGerd Hoffmann switch (vd->auth) { 460df887684SGerd Hoffmann case VNC_AUTH_VNC: 461df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_VNC; 462df887684SGerd Hoffmann break; 463df887684SGerd Hoffmann case VNC_AUTH_RA2: 464df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_RA2; 465df887684SGerd Hoffmann break; 466df887684SGerd Hoffmann case VNC_AUTH_RA2NE: 467df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_RA2NE; 468df887684SGerd Hoffmann break; 469df887684SGerd Hoffmann case VNC_AUTH_TIGHT: 470df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_TIGHT; 471df887684SGerd Hoffmann break; 472df887684SGerd Hoffmann case VNC_AUTH_ULTRA: 473df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_ULTRA; 474df887684SGerd Hoffmann break; 475df887684SGerd Hoffmann case VNC_AUTH_TLS: 476df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_TLS; 477df887684SGerd Hoffmann break; 478df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT: 479df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_VENCRYPT; 480df887684SGerd Hoffmann info->has_vencrypt = true; 481df887684SGerd Hoffmann switch (vd->subauth) { 482df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_PLAIN: 483df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; 484df887684SGerd Hoffmann break; 485df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSNONE: 486df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; 487df887684SGerd Hoffmann break; 488df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSVNC: 489df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; 490df887684SGerd Hoffmann break; 491df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSPLAIN: 492df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; 493df887684SGerd Hoffmann break; 494df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509NONE: 495df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; 496df887684SGerd Hoffmann break; 497df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509VNC: 498df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; 499df887684SGerd Hoffmann break; 500df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509PLAIN: 501df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; 502df887684SGerd Hoffmann break; 503df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSSASL: 504df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; 505df887684SGerd Hoffmann break; 506df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509SASL: 507df887684SGerd Hoffmann info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; 508df887684SGerd Hoffmann break; 509df887684SGerd Hoffmann default: 510df887684SGerd Hoffmann info->has_vencrypt = false; 511df887684SGerd Hoffmann break; 512df887684SGerd Hoffmann } 513df887684SGerd Hoffmann break; 514df887684SGerd Hoffmann case VNC_AUTH_SASL: 515df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_SASL; 516df887684SGerd Hoffmann break; 517df887684SGerd Hoffmann case VNC_AUTH_NONE: 518df887684SGerd Hoffmann default: 519df887684SGerd Hoffmann info->auth = VNC_PRIMARY_AUTH_NONE; 520df887684SGerd Hoffmann break; 521df887684SGerd Hoffmann } 522df887684SGerd Hoffmann } 523df887684SGerd Hoffmann 524df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp) 525df887684SGerd Hoffmann { 526df887684SGerd Hoffmann VncInfo2List *item, *prev = NULL; 527df887684SGerd Hoffmann VncInfo2 *info; 528df887684SGerd Hoffmann VncDisplay *vd; 529df887684SGerd Hoffmann DeviceState *dev; 530df887684SGerd Hoffmann 531df887684SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 532df887684SGerd Hoffmann info = g_new0(VncInfo2, 1); 533df887684SGerd Hoffmann info->id = g_strdup(vd->id); 534df887684SGerd Hoffmann info->clients = qmp_query_client_list(vd); 535df887684SGerd Hoffmann qmp_query_auth(vd, info); 536df887684SGerd Hoffmann if (vd->dcl.con) { 537df887684SGerd Hoffmann dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con), 538df887684SGerd Hoffmann "device", NULL)); 539df887684SGerd Hoffmann info->has_display = true; 540df887684SGerd Hoffmann info->display = g_strdup(dev->id); 541df887684SGerd Hoffmann } 54204d2529dSDaniel P. Berrange if (vd->lsock != NULL) { 54304d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 54404d2529dSDaniel P. Berrange vd->lsock, false, info->server); 545df887684SGerd Hoffmann } 54604d2529dSDaniel P. Berrange if (vd->lwebsock != NULL) { 54704d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 54804d2529dSDaniel P. Berrange vd->lwebsock, true, info->server); 549df887684SGerd Hoffmann } 550df887684SGerd Hoffmann 551df887684SGerd Hoffmann item = g_new0(VncInfo2List, 1); 552df887684SGerd Hoffmann item->value = info; 553df887684SGerd Hoffmann item->next = prev; 554df887684SGerd Hoffmann prev = item; 555df887684SGerd Hoffmann } 556df887684SGerd Hoffmann return prev; 557df887684SGerd Hoffmann } 558df887684SGerd Hoffmann 5593e230dd2SCorentin Chary /* TODO 5603e230dd2SCorentin Chary 1) Get the queue working for IO. 5613e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 5623e230dd2SCorentin Chary and not totally invalidated 5633e230dd2SCorentin Chary 3) resolutions > 1024 5643e230dd2SCorentin Chary */ 5653e230dd2SCorentin Chary 56638ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync); 5673e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 5683e230dd2SCorentin Chary 5693e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 5703e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 5713e230dd2SCorentin Chary int x_position, int y_position, 5723e230dd2SCorentin Chary int w, int h); 5730f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 5743e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 5753e230dd2SCorentin Chary 576d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd) 577d05959c2SGerd Hoffmann { 578d05959c2SGerd Hoffmann return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 579d05959c2SGerd Hoffmann VNC_DIRTY_PIXELS_PER_BIT)); 580d05959c2SGerd Hoffmann } 581d05959c2SGerd Hoffmann 582d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd) 583d05959c2SGerd Hoffmann { 584d05959c2SGerd Hoffmann return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 585d05959c2SGerd Hoffmann } 586d05959c2SGerd Hoffmann 587bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 588bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 589f7b3d68cSGerd Hoffmann VncDisplay *vd, 590f7b3d68cSGerd Hoffmann int x, int y, int w, int h) 591f7b3d68cSGerd Hoffmann { 592f7b3d68cSGerd Hoffmann int width = vnc_width(vd); 593f7b3d68cSGerd Hoffmann int height = vnc_height(vd); 594f7b3d68cSGerd Hoffmann 59591937225SPeter Lieven /* this is needed this to ensure we updated all affected 59691937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 597b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 598b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 5993e230dd2SCorentin Chary 6009f64916dSGerd Hoffmann x = MIN(x, width); 6019f64916dSGerd Hoffmann y = MIN(y, height); 6029f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 60391937225SPeter Lieven h = MIN(y + h, height); 6043e230dd2SCorentin Chary 605b4c85ddcSPeter Lieven for (; y < h; y++) { 606bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 60791937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 608b4c85ddcSPeter Lieven } 6093e230dd2SCorentin Chary } 6103e230dd2SCorentin Chary 611bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 612bea60dd7SPeter Lieven int x, int y, int w, int h) 613bea60dd7SPeter Lieven { 614bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 615bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 616bea60dd7SPeter Lieven 617f7b3d68cSGerd Hoffmann vnc_set_area_dirty(s->dirty, vd, x, y, w, h); 618bea60dd7SPeter Lieven } 619bea60dd7SPeter Lieven 6203e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 6213e230dd2SCorentin Chary int32_t encoding) 6223e230dd2SCorentin Chary { 6233e230dd2SCorentin Chary vnc_write_u16(vs, x); 6243e230dd2SCorentin Chary vnc_write_u16(vs, y); 6253e230dd2SCorentin Chary vnc_write_u16(vs, w); 6263e230dd2SCorentin Chary vnc_write_u16(vs, h); 6273e230dd2SCorentin Chary 6283e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 6293e230dd2SCorentin Chary } 6303e230dd2SCorentin Chary 63132ed2680STim Hardeck 6323e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 6333e230dd2SCorentin Chary { 63404d2529dSDaniel P. Berrange if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 6353e230dd2SCorentin Chary return; 6363e230dd2SCorentin Chary } 637bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 638bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 6393e230dd2SCorentin Chary return; 6403e230dd2SCorentin Chary } 641bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 642bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 643bd023f95SCorentin Chary vnc_lock_output(vs); 6443e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 6453e230dd2SCorentin Chary vnc_write_u8(vs, 0); 6463e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 6473e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 6483e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 649bd023f95SCorentin Chary vnc_unlock_output(vs); 6503e230dd2SCorentin Chary vnc_flush(vs); 6513e230dd2SCorentin Chary } 6523e230dd2SCorentin Chary 653bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 654bd023f95SCorentin Chary { 655bd023f95SCorentin Chary VncState *vs; 656bd023f95SCorentin Chary 657bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 658bd023f95SCorentin Chary vnc_lock_output(vs); 659bd023f95SCorentin Chary vs->abort = true; 660bd023f95SCorentin Chary vnc_unlock_output(vs); 661bd023f95SCorentin Chary } 662bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 663bd023f95SCorentin Chary vnc_jobs_join(vs); 664bd023f95SCorentin Chary } 665bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 666bd023f95SCorentin Chary vnc_lock_output(vs); 667bd023f95SCorentin Chary vs->abort = false; 668bd023f95SCorentin Chary vnc_unlock_output(vs); 669bd023f95SCorentin Chary } 670bd023f95SCorentin Chary } 671bd023f95SCorentin Chary 6729f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 6739f64916dSGerd Hoffmann { 6749f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 6759f64916dSGerd Hoffmann } 6769f64916dSGerd Hoffmann 6779f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 6789f64916dSGerd Hoffmann { 6799f64916dSGerd Hoffmann uint8_t *ptr; 6809f64916dSGerd Hoffmann 6819f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 6829f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 6839f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 6849f64916dSGerd Hoffmann return ptr; 6859f64916dSGerd Hoffmann } 6869f64916dSGerd Hoffmann 687453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd) 688453f842bSGerd Hoffmann { 689453f842bSGerd Hoffmann qemu_pixman_image_unref(vd->server); 690453f842bSGerd Hoffmann vd->server = NULL; 691453f842bSGerd Hoffmann 692c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 693c7628bffSGerd Hoffmann return; 694c7628bffSGerd Hoffmann } 695c7628bffSGerd Hoffmann 696453f842bSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 697453f842bSGerd Hoffmann vnc_width(vd), 698453f842bSGerd Hoffmann vnc_height(vd), 699453f842bSGerd Hoffmann NULL, 0); 700453f842bSGerd Hoffmann } 701453f842bSGerd Hoffmann 702c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 703c12aeb86SGerd Hoffmann DisplaySurface *surface) 7043e230dd2SCorentin Chary { 70521ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7063e230dd2SCorentin Chary VncState *vs; 707bea60dd7SPeter Lieven int width, height; 7083e230dd2SCorentin Chary 709bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 710453f842bSGerd Hoffmann vd->ds = surface; 711bd023f95SCorentin Chary 7123e230dd2SCorentin Chary /* server surface */ 713453f842bSGerd Hoffmann vnc_update_server_surface(vd); 7143e230dd2SCorentin Chary 7153e230dd2SCorentin Chary /* guest surface */ 7169f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 717d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 718d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 719453f842bSGerd Hoffmann width = vnc_width(vd); 720453f842bSGerd Hoffmann height = vnc_height(vd); 721bea60dd7SPeter Lieven memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 722f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 723bea60dd7SPeter Lieven width, height); 7243e230dd2SCorentin Chary 7253e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7263e230dd2SCorentin Chary vnc_colordepth(vs); 7273e230dd2SCorentin Chary vnc_desktop_resize(vs); 7283e230dd2SCorentin Chary if (vs->vd->cursor) { 7293e230dd2SCorentin Chary vnc_cursor_define(vs); 7303e230dd2SCorentin Chary } 731bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 732f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vd, 0, 0, 733bea60dd7SPeter Lieven width, height); 7343e230dd2SCorentin Chary } 7353e230dd2SCorentin Chary } 7363e230dd2SCorentin Chary 7373e230dd2SCorentin Chary /* fastest code */ 7389f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 7393e230dd2SCorentin Chary void *pixels, int size) 7403e230dd2SCorentin Chary { 7413e230dd2SCorentin Chary vnc_write(vs, pixels, size); 7423e230dd2SCorentin Chary } 7433e230dd2SCorentin Chary 7443e230dd2SCorentin Chary /* slowest but generic code. */ 7453e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 7463e230dd2SCorentin Chary { 7473e230dd2SCorentin Chary uint8_t r, g, b; 7483e230dd2SCorentin Chary 7499f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 7509f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 7519f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 7529f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 7539f64916dSGerd Hoffmann #else 7549f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 7559f64916dSGerd Hoffmann #endif 7569f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 7579f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 7589f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 7599f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 7603e230dd2SCorentin Chary case 1: 7613e230dd2SCorentin Chary buf[0] = v; 7623e230dd2SCorentin Chary break; 7633e230dd2SCorentin Chary case 2: 7649f64916dSGerd Hoffmann if (vs->client_be) { 7653e230dd2SCorentin Chary buf[0] = v >> 8; 7663e230dd2SCorentin Chary buf[1] = v; 7673e230dd2SCorentin Chary } else { 7683e230dd2SCorentin Chary buf[1] = v >> 8; 7693e230dd2SCorentin Chary buf[0] = v; 7703e230dd2SCorentin Chary } 7713e230dd2SCorentin Chary break; 7723e230dd2SCorentin Chary default: 7733e230dd2SCorentin Chary case 4: 7749f64916dSGerd Hoffmann if (vs->client_be) { 7753e230dd2SCorentin Chary buf[0] = v >> 24; 7763e230dd2SCorentin Chary buf[1] = v >> 16; 7773e230dd2SCorentin Chary buf[2] = v >> 8; 7783e230dd2SCorentin Chary buf[3] = v; 7793e230dd2SCorentin Chary } else { 7803e230dd2SCorentin Chary buf[3] = v >> 24; 7813e230dd2SCorentin Chary buf[2] = v >> 16; 7823e230dd2SCorentin Chary buf[1] = v >> 8; 7833e230dd2SCorentin Chary buf[0] = v; 7843e230dd2SCorentin Chary } 7853e230dd2SCorentin Chary break; 7863e230dd2SCorentin Chary } 7873e230dd2SCorentin Chary } 7883e230dd2SCorentin Chary 7899f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 7903e230dd2SCorentin Chary void *pixels1, int size) 7913e230dd2SCorentin Chary { 7923e230dd2SCorentin Chary uint8_t buf[4]; 7933e230dd2SCorentin Chary 7949f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 7953e230dd2SCorentin Chary uint32_t *pixels = pixels1; 7963e230dd2SCorentin Chary int n, i; 7973e230dd2SCorentin Chary n = size >> 2; 7983e230dd2SCorentin Chary for (i = 0; i < n; i++) { 7993e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 8009f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 8013e230dd2SCorentin Chary } 8023e230dd2SCorentin Chary } 8033e230dd2SCorentin Chary } 8043e230dd2SCorentin Chary 8053e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8063e230dd2SCorentin Chary { 8073e230dd2SCorentin Chary int i; 8083e230dd2SCorentin Chary uint8_t *row; 8093e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8103e230dd2SCorentin Chary 8119f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 8123e230dd2SCorentin Chary for (i = 0; i < h; i++) { 8139f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 8149f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 8153e230dd2SCorentin Chary } 8163e230dd2SCorentin Chary return 1; 8173e230dd2SCorentin Chary } 8183e230dd2SCorentin Chary 819bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8203e230dd2SCorentin Chary { 8213e230dd2SCorentin Chary int n = 0; 822de3f7de7SPeter Lieven bool encode_raw = false; 823de3f7de7SPeter Lieven size_t saved_offs = vs->output.offset; 8243e230dd2SCorentin Chary 8253e230dd2SCorentin Chary switch(vs->vnc_encoding) { 8263e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 8273e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 8283e230dd2SCorentin Chary break; 8293e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 8303e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 8313e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 8323e230dd2SCorentin Chary break; 8333e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 8343e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 8353e230dd2SCorentin Chary break; 836efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 837efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 838efe556adSCorentin Chary break; 839148954faSCorentin Chary case VNC_ENCODING_ZRLE: 840148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 841148954faSCorentin Chary break; 842148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 843148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 844148954faSCorentin Chary break; 8453e230dd2SCorentin Chary default: 846de3f7de7SPeter Lieven encode_raw = true; 8473e230dd2SCorentin Chary break; 8483e230dd2SCorentin Chary } 849de3f7de7SPeter Lieven 850de3f7de7SPeter Lieven /* If the client has the same pixel format as our internal buffer and 851de3f7de7SPeter Lieven * a RAW encoding would need less space fall back to RAW encoding to 852de3f7de7SPeter Lieven * save bandwidth and processing power in the client. */ 853de3f7de7SPeter Lieven if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy && 854de3f7de7SPeter Lieven 12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) { 855de3f7de7SPeter Lieven vs->output.offset = saved_offs; 856de3f7de7SPeter Lieven encode_raw = true; 857de3f7de7SPeter Lieven } 858de3f7de7SPeter Lieven 859de3f7de7SPeter Lieven if (encode_raw) { 860de3f7de7SPeter Lieven vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 861de3f7de7SPeter Lieven n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 862de3f7de7SPeter Lieven } 863de3f7de7SPeter Lieven 8643e230dd2SCorentin Chary return n; 8653e230dd2SCorentin Chary } 8663e230dd2SCorentin Chary 8673e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 8683e230dd2SCorentin Chary { 8693e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 870bd023f95SCorentin Chary vnc_lock_output(vs); 8713e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8723e230dd2SCorentin Chary vnc_write_u8(vs, 0); 8733e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 8743e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 8753e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 8763e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 877bd023f95SCorentin Chary vnc_unlock_output(vs); 8783e230dd2SCorentin Chary vnc_flush(vs); 8793e230dd2SCorentin Chary } 8803e230dd2SCorentin Chary 8817c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl, 8827c20b4a3SGerd Hoffmann int src_x, int src_y, 8837c20b4a3SGerd Hoffmann int dst_x, int dst_y, int w, int h) 8843e230dd2SCorentin Chary { 88521ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 8863e230dd2SCorentin Chary VncState *vs, *vn; 8873e230dd2SCorentin Chary uint8_t *src_row; 8883e230dd2SCorentin Chary uint8_t *dst_row; 8899f64916dSGerd Hoffmann int i, x, y, pitch, inc, w_lim, s; 8903e230dd2SCorentin Chary int cmp_bytes; 8913e230dd2SCorentin Chary 8927fe4a41cSGerd Hoffmann if (!vd->server) { 8937fe4a41cSGerd Hoffmann /* no client connected */ 8947fe4a41cSGerd Hoffmann return; 8957fe4a41cSGerd Hoffmann } 8967fe4a41cSGerd Hoffmann 8973e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 8983e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 8993e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 9003e230dd2SCorentin Chary vs->force_update = 1; 90138ee14f4SGerd Hoffmann vnc_update_client(vs, 1, true); 9023e230dd2SCorentin Chary /* vs might be free()ed here */ 9033e230dd2SCorentin Chary } 9043e230dd2SCorentin Chary } 9053e230dd2SCorentin Chary 9063e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 9079f64916dSGerd Hoffmann pitch = vnc_server_fb_stride(vd); 9089f64916dSGerd Hoffmann src_row = vnc_server_fb_ptr(vd, src_x, src_y); 9099f64916dSGerd Hoffmann dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y); 9103e230dd2SCorentin Chary y = dst_y; 9113e230dd2SCorentin Chary inc = 1; 9123e230dd2SCorentin Chary if (dst_y > src_y) { 9133e230dd2SCorentin Chary /* copy backwards */ 9143e230dd2SCorentin Chary src_row += pitch * (h-1); 9153e230dd2SCorentin Chary dst_row += pitch * (h-1); 9163e230dd2SCorentin Chary pitch = -pitch; 9173e230dd2SCorentin Chary y = dst_y + h - 1; 9183e230dd2SCorentin Chary inc = -1; 9193e230dd2SCorentin Chary } 920b4c85ddcSPeter Lieven w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 921b4c85ddcSPeter Lieven if (w_lim < 0) { 9223e230dd2SCorentin Chary w_lim = w; 923b4c85ddcSPeter Lieven } else { 924b4c85ddcSPeter Lieven w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT); 925b4c85ddcSPeter Lieven } 9263e230dd2SCorentin Chary for (i = 0; i < h; i++) { 9273e230dd2SCorentin Chary for (x = 0; x <= w_lim; 9283e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 9293e230dd2SCorentin Chary if (x == w_lim) { 9303e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 9313e230dd2SCorentin Chary break; 9323e230dd2SCorentin Chary } else if (!x) { 933b4c85ddcSPeter Lieven s = (VNC_DIRTY_PIXELS_PER_BIT - 934b4c85ddcSPeter Lieven (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 9353e230dd2SCorentin Chary s = MIN(s, w_lim); 9363e230dd2SCorentin Chary } else { 937b4c85ddcSPeter Lieven s = VNC_DIRTY_PIXELS_PER_BIT; 9383e230dd2SCorentin Chary } 9399f64916dSGerd Hoffmann cmp_bytes = s * VNC_SERVER_FB_BYTES; 9403e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 9413e230dd2SCorentin Chary continue; 9423e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 9433e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9443e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 945b4c85ddcSPeter Lieven set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT), 946b4c85ddcSPeter Lieven vs->dirty[y]); 9473e230dd2SCorentin Chary } 9483e230dd2SCorentin Chary } 9493e230dd2SCorentin Chary } 9509f64916dSGerd Hoffmann src_row += pitch - w * VNC_SERVER_FB_BYTES; 9519f64916dSGerd Hoffmann dst_row += pitch - w * VNC_SERVER_FB_BYTES; 9523e230dd2SCorentin Chary y += inc; 9533e230dd2SCorentin Chary } 9543e230dd2SCorentin Chary 9553e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9563e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 9573e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 9583e230dd2SCorentin Chary } 9593e230dd2SCorentin Chary } 9603e230dd2SCorentin Chary } 9613e230dd2SCorentin Chary 9627c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 9637c20b4a3SGerd Hoffmann int x, int y, int visible) 9643e230dd2SCorentin Chary { 9653e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 9663e230dd2SCorentin Chary } 9673e230dd2SCorentin Chary 9683e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 9693e230dd2SCorentin Chary { 9703e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 9713e230dd2SCorentin Chary int isize; 9723e230dd2SCorentin Chary 9733e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 974d01f9595SCorentin Chary vnc_lock_output(vs); 9753e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 9763e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 9773e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 9783e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 9793e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 9809f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 9819f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 9823e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 983d01f9595SCorentin Chary vnc_unlock_output(vs); 9843e230dd2SCorentin Chary return 0; 9853e230dd2SCorentin Chary } 9863e230dd2SCorentin Chary return -1; 9873e230dd2SCorentin Chary } 9883e230dd2SCorentin Chary 9897c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 9907c20b4a3SGerd Hoffmann QEMUCursor *c) 9913e230dd2SCorentin Chary { 992d616ccc5SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 9933e230dd2SCorentin Chary VncState *vs; 9943e230dd2SCorentin Chary 9953e230dd2SCorentin Chary cursor_put(vd->cursor); 9967267c094SAnthony Liguori g_free(vd->cursor_mask); 9973e230dd2SCorentin Chary 9983e230dd2SCorentin Chary vd->cursor = c; 9993e230dd2SCorentin Chary cursor_get(vd->cursor); 10003e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 10017267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 10023e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 10033e230dd2SCorentin Chary 10043e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 10053e230dd2SCorentin Chary vnc_cursor_define(vs); 10063e230dd2SCorentin Chary } 10073e230dd2SCorentin Chary } 10083e230dd2SCorentin Chary 10094769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs, 10106c71a539SCorentin Chary int y, int last_x, int x, int height) 10113e230dd2SCorentin Chary { 10123e230dd2SCorentin Chary int h; 10133e230dd2SCorentin Chary 10146c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 1015bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 10163e230dd2SCorentin Chary break; 1017bc2429b9SCorentin Chary } 1018863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 10193e230dd2SCorentin Chary } 10203e230dd2SCorentin Chary 10213e230dd2SCorentin Chary return h; 10223e230dd2SCorentin Chary } 10233e230dd2SCorentin Chary 102438ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync) 10253e230dd2SCorentin Chary { 102663658280SGerd Hoffmann vs->has_dirty += has_dirty; 102704d2529dSDaniel P. Berrange if (vs->need_update && vs->ioc != NULL) { 10283e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 1029bd023f95SCorentin Chary VncJob *job; 10303e230dd2SCorentin Chary int y; 10312f487a3dSPeter Lieven int height, width; 1032bd023f95SCorentin Chary int n = 0; 1033bd023f95SCorentin Chary 10343e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 10353e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 10363e230dd2SCorentin Chary return 0; 10373e230dd2SCorentin Chary 103863658280SGerd Hoffmann if (!vs->has_dirty && !vs->audio_cap && !vs->force_update) 10393e230dd2SCorentin Chary return 0; 10403e230dd2SCorentin Chary 10413e230dd2SCorentin Chary /* 10423e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 10433e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 10443e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 10453e230dd2SCorentin Chary * send them to the client. 10463e230dd2SCorentin Chary */ 1047bd023f95SCorentin Chary job = vnc_job_new(vs); 10483e230dd2SCorentin Chary 1049bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 1050bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 10513e230dd2SCorentin Chary 105212b316d4SPeter Lieven y = 0; 105312b316d4SPeter Lieven for (;;) { 105412b316d4SPeter Lieven int x, h; 105512b316d4SPeter Lieven unsigned long x2; 105612b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 105712b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 105812b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 105912b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 106012b316d4SPeter Lieven /* no more dirty bits */ 106112b316d4SPeter Lieven break; 10623e230dd2SCorentin Chary } 106312b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 106412b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 106512b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 106612b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 106712b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 106812b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10692f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10702f487a3dSPeter Lieven if (x2 > x) { 107112b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 107212b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10733e230dd2SCorentin Chary } 10740e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10750e7d6f60SPeter Lieven y += h; 10760e7d6f60SPeter Lieven if (y == height) { 10770e7d6f60SPeter Lieven break; 10780e7d6f60SPeter Lieven } 10790e7d6f60SPeter Lieven } 10802f487a3dSPeter Lieven } 1081bd023f95SCorentin Chary 1082bd023f95SCorentin Chary vnc_job_push(job); 1083eb214ff8SGerd Hoffmann if (sync) { 1084eb214ff8SGerd Hoffmann vnc_jobs_join(vs); 1085eb214ff8SGerd Hoffmann } 10863e230dd2SCorentin Chary vs->force_update = 0; 108763658280SGerd Hoffmann vs->has_dirty = 0; 1088bd023f95SCorentin Chary return n; 10893e230dd2SCorentin Chary } 10903e230dd2SCorentin Chary 109104d2529dSDaniel P. Berrange if (vs->disconnecting) { 10923e230dd2SCorentin Chary vnc_disconnect_finish(vs); 109338ee14f4SGerd Hoffmann } else if (sync) { 109438ee14f4SGerd Hoffmann vnc_jobs_join(vs); 109538ee14f4SGerd Hoffmann } 10963e230dd2SCorentin Chary 10973e230dd2SCorentin Chary return 0; 10983e230dd2SCorentin Chary } 10993e230dd2SCorentin Chary 11003e230dd2SCorentin Chary /* audio */ 11013e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 11023e230dd2SCorentin Chary { 11033e230dd2SCorentin Chary VncState *vs = opaque; 11043e230dd2SCorentin Chary 11053e230dd2SCorentin Chary switch (cmd) { 11063e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1107bd023f95SCorentin Chary vnc_lock_output(vs); 11083e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11093e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11103e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1111bd023f95SCorentin Chary vnc_unlock_output(vs); 11123e230dd2SCorentin Chary vnc_flush(vs); 11133e230dd2SCorentin Chary break; 11143e230dd2SCorentin Chary 11153e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1116bd023f95SCorentin Chary vnc_lock_output(vs); 11173e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11183e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11193e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1120bd023f95SCorentin Chary vnc_unlock_output(vs); 11213e230dd2SCorentin Chary vnc_flush(vs); 11223e230dd2SCorentin Chary break; 11233e230dd2SCorentin Chary } 11243e230dd2SCorentin Chary } 11253e230dd2SCorentin Chary 11263e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 11273e230dd2SCorentin Chary { 11283e230dd2SCorentin Chary } 11293e230dd2SCorentin Chary 11303e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 11313e230dd2SCorentin Chary { 11323e230dd2SCorentin Chary VncState *vs = opaque; 11333e230dd2SCorentin Chary 1134bd023f95SCorentin Chary vnc_lock_output(vs); 11353e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11363e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11373e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 11383e230dd2SCorentin Chary vnc_write_u32(vs, size); 11393e230dd2SCorentin Chary vnc_write(vs, buf, size); 1140bd023f95SCorentin Chary vnc_unlock_output(vs); 11413e230dd2SCorentin Chary vnc_flush(vs); 11423e230dd2SCorentin Chary } 11433e230dd2SCorentin Chary 11443e230dd2SCorentin Chary static void audio_add(VncState *vs) 11453e230dd2SCorentin Chary { 11463e230dd2SCorentin Chary struct audio_capture_ops ops; 11473e230dd2SCorentin Chary 11483e230dd2SCorentin Chary if (vs->audio_cap) { 1149027a79c3SCole Robinson error_report("audio already running"); 11503e230dd2SCorentin Chary return; 11513e230dd2SCorentin Chary } 11523e230dd2SCorentin Chary 11533e230dd2SCorentin Chary ops.notify = audio_capture_notify; 11543e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 11553e230dd2SCorentin Chary ops.capture = audio_capture; 11563e230dd2SCorentin Chary 11573e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 11583e230dd2SCorentin Chary if (!vs->audio_cap) { 1159027a79c3SCole Robinson error_report("Failed to add audio capture"); 11603e230dd2SCorentin Chary } 11613e230dd2SCorentin Chary } 11623e230dd2SCorentin Chary 11633e230dd2SCorentin Chary static void audio_del(VncState *vs) 11643e230dd2SCorentin Chary { 11653e230dd2SCorentin Chary if (vs->audio_cap) { 11663e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11673e230dd2SCorentin Chary vs->audio_cap = NULL; 11683e230dd2SCorentin Chary } 11693e230dd2SCorentin Chary } 11703e230dd2SCorentin Chary 11713e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11723e230dd2SCorentin Chary { 117304d2529dSDaniel P. Berrange if (vs->disconnecting) { 11743e230dd2SCorentin Chary return; 117504d2529dSDaniel P. Berrange } 11768cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 117704d2529dSDaniel P. Berrange if (vs->ioc_tag) { 117804d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 117904d2529dSDaniel P. Berrange } 118004d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 118104d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11823e230dd2SCorentin Chary } 11833e230dd2SCorentin Chary 11847536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11853e230dd2SCorentin Chary { 11867d964c9dSCorentin Chary int i; 11877d964c9dSCorentin Chary 1188bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1189bd023f95SCorentin Chary 1190bd023f95SCorentin Chary vnc_lock_output(vs); 1191fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11923e230dd2SCorentin Chary 11933e230dd2SCorentin Chary buffer_free(&vs->input); 11943e230dd2SCorentin Chary buffer_free(&vs->output); 11953e230dd2SCorentin Chary 1196fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11973e230dd2SCorentin Chary 11983e230dd2SCorentin Chary vnc_zlib_clear(vs); 11993e230dd2SCorentin Chary vnc_tight_clear(vs); 1200148954faSCorentin Chary vnc_zrle_clear(vs); 12013e230dd2SCorentin Chary 12023e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12033e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 12043e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12053e230dd2SCorentin Chary audio_del(vs); 12067bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 12073e230dd2SCorentin Chary 12086fd8e79aSTim Hardeck if (vs->initialized) { 12093e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 12106fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 1211c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1212c7628bffSGerd Hoffmann /* last client gone */ 1213c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1214c7628bffSGerd Hoffmann } 12156fd8e79aSTim Hardeck } 12163e230dd2SCorentin Chary 12173e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 12183e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1219bd023f95SCorentin Chary vnc_unlock_output(vs); 1220bd023f95SCorentin Chary 1221bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 12226fd8e79aSTim Hardeck if (vs->bh != NULL) { 1223175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 12246fd8e79aSTim Hardeck } 1225175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1226175b2a6eSCorentin Chary 12277d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 12287267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 12297d964c9dSCorentin Chary } 12307267c094SAnthony Liguori g_free(vs->lossy_rect); 123104d2529dSDaniel P. Berrange 123204d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 123304d2529dSDaniel P. Berrange vs->ioc = NULL; 123404d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 123504d2529dSDaniel P. Berrange vs->sioc = NULL; 12367267c094SAnthony Liguori g_free(vs); 12373e230dd2SCorentin Chary } 12383e230dd2SCorentin Chary 123904d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 12403e230dd2SCorentin Chary { 124104d2529dSDaniel P. Berrange if (ret <= 0) { 124204d2529dSDaniel P. Berrange if (ret == 0) { 124304d2529dSDaniel P. Berrange VNC_DEBUG("Closing down client sock: EOF\n"); 124404d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 124504d2529dSDaniel P. Berrange VNC_DEBUG("Closing down client sock: ret %d (%s)\n", 124604d2529dSDaniel P. Berrange ret, errp ? error_get_pretty(*errp) : "Unknown"); 12473e230dd2SCorentin Chary } 12483e230dd2SCorentin Chary 12493e230dd2SCorentin Chary vnc_disconnect_start(vs); 125004d2529dSDaniel P. Berrange if (errp) { 125104d2529dSDaniel P. Berrange error_free(*errp); 125204d2529dSDaniel P. Berrange *errp = NULL; 125304d2529dSDaniel P. Berrange } 12543e230dd2SCorentin Chary return 0; 12553e230dd2SCorentin Chary } 12563e230dd2SCorentin Chary return ret; 12573e230dd2SCorentin Chary } 12583e230dd2SCorentin Chary 12593e230dd2SCorentin Chary 12603e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12613e230dd2SCorentin Chary { 12623e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12633e230dd2SCorentin Chary vnc_disconnect_start(vs); 12643e230dd2SCorentin Chary } 12653e230dd2SCorentin Chary 12663e305e4aSDaniel P. Berrange 12673e230dd2SCorentin Chary /* 12683e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12693e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12703e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12713e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12723e230dd2SCorentin Chary * 12733e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12743e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12753e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12763e230dd2SCorentin Chary * is active 12773e230dd2SCorentin Chary * 12783e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12793e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12803e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12813e230dd2SCorentin Chary */ 1282fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12833e230dd2SCorentin Chary { 128404d2529dSDaniel P. Berrange Error *err = NULL; 1285fdd1ab6aSDaniel P. Berrange ssize_t ret; 128604d2529dSDaniel P. Berrange ret = qio_channel_write( 128704d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12883e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 128904d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12903e230dd2SCorentin Chary } 12913e230dd2SCorentin Chary 12923e230dd2SCorentin Chary 12933e230dd2SCorentin Chary /* 12943e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12953e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12963e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12973e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12983e230dd2SCorentin Chary * 12993e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 13003e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 13013e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13023e230dd2SCorentin Chary */ 1303fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 13043e230dd2SCorentin Chary { 1305fdd1ab6aSDaniel P. Berrange ssize_t ret; 13063e230dd2SCorentin Chary 13073e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13083e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 13093e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 13103e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 13113e230dd2SCorentin Chary 13123e230dd2SCorentin Chary if (vs->sasl.conn && 13133e230dd2SCorentin Chary vs->sasl.runSSF && 13143e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 13153e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 13163e230dd2SCorentin Chary if (ret) 13173e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 13183e230dd2SCorentin Chary } else 13193e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13203e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 13213e230dd2SCorentin Chary if (!ret) 13223e230dd2SCorentin Chary return 0; 13233e230dd2SCorentin Chary 132432ed2680STim Hardeck buffer_advance(&vs->output, ret); 13253e230dd2SCorentin Chary 13263e230dd2SCorentin Chary if (vs->output.offset == 0) { 132704d2529dSDaniel P. Berrange if (vs->ioc_tag) { 132804d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 132904d2529dSDaniel P. Berrange } 133004d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 133104d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 13323e230dd2SCorentin Chary } 13333e230dd2SCorentin Chary 13343e230dd2SCorentin Chary return ret; 13353e230dd2SCorentin Chary } 13363e230dd2SCorentin Chary 13373e230dd2SCorentin Chary 13383e230dd2SCorentin Chary /* 13393e230dd2SCorentin Chary * First function called whenever there is data to be written to 13403e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13413e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 13423e230dd2SCorentin Chary */ 134304d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 13443e230dd2SCorentin Chary { 13453e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13463e230dd2SCorentin Chary if (vs->sasl.conn && 13473e230dd2SCorentin Chary vs->sasl.runSSF && 13483e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 13493e230dd2SCorentin Chary vnc_client_write_sasl(vs); 13503e230dd2SCorentin Chary } else 13513e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13527536ee4bSTim Hardeck { 13533e230dd2SCorentin Chary vnc_client_write_plain(vs); 13543e230dd2SCorentin Chary } 13557536ee4bSTim Hardeck } 13563e230dd2SCorentin Chary 135704d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1358bd023f95SCorentin Chary { 1359bd023f95SCorentin Chary 1360bd023f95SCorentin Chary vnc_lock_output(vs); 1361*d5f04223SDaniel P. Berrange if (vs->output.offset) { 136204d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 136304d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 136404d2529dSDaniel P. Berrange if (vs->ioc_tag) { 136504d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 136604d2529dSDaniel P. Berrange } 136704d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 136804d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1369bd023f95SCorentin Chary } 1370bd023f95SCorentin Chary vnc_unlock_output(vs); 1371bd023f95SCorentin Chary } 1372bd023f95SCorentin Chary 13733e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13743e230dd2SCorentin Chary { 13753e230dd2SCorentin Chary vs->read_handler = func; 13763e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13773e230dd2SCorentin Chary } 13783e230dd2SCorentin Chary 13793e230dd2SCorentin Chary 13803e230dd2SCorentin Chary /* 13813e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13823e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13833e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13843e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13853e230dd2SCorentin Chary * 13863e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13873e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13883e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13893e230dd2SCorentin Chary * is active 13903e230dd2SCorentin Chary * 13913e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13923e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13933e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13943e230dd2SCorentin Chary */ 1395fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13963e230dd2SCorentin Chary { 1397fdd1ab6aSDaniel P. Berrange ssize_t ret; 139804d2529dSDaniel P. Berrange Error *err = NULL; 139904d2529dSDaniel P. Berrange ret = qio_channel_read( 140004d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 14013e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 140204d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 14033e230dd2SCorentin Chary } 14043e230dd2SCorentin Chary 14053e230dd2SCorentin Chary 14063e230dd2SCorentin Chary /* 14073e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 14083e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 14093e230dd2SCorentin Chary * data as possible without blocking. 14103e230dd2SCorentin Chary * 14113e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 14123e230dd2SCorentin Chary * disconnects the client socket. 14133e230dd2SCorentin Chary */ 1414fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 14153e230dd2SCorentin Chary { 1416fdd1ab6aSDaniel P. Berrange ssize_t ret; 14173e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 14183e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 14193e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 14203e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 14213e230dd2SCorentin Chary if (!ret) 14223e230dd2SCorentin Chary return 0; 14233e230dd2SCorentin Chary vs->input.offset += ret; 14243e230dd2SCorentin Chary return ret; 14253e230dd2SCorentin Chary } 14263e230dd2SCorentin Chary 1427175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1428175b2a6eSCorentin Chary { 1429175b2a6eSCorentin Chary VncState *vs = opaque; 1430175b2a6eSCorentin Chary 1431175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1432175b2a6eSCorentin Chary } 14333e230dd2SCorentin Chary 14343e230dd2SCorentin Chary /* 14353e230dd2SCorentin Chary * First function called whenever there is more data to be read from 14363e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 14373e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 14383e230dd2SCorentin Chary */ 143904d2529dSDaniel P. Berrange static void vnc_client_read(VncState *vs) 14403e230dd2SCorentin Chary { 1441fdd1ab6aSDaniel P. Berrange ssize_t ret; 14423e230dd2SCorentin Chary 14433e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 14443e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 14453e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 14463e230dd2SCorentin Chary else 14473e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 14483e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 14493e230dd2SCorentin Chary if (!ret) { 145004d2529dSDaniel P. Berrange if (vs->disconnecting) { 14513e230dd2SCorentin Chary vnc_disconnect_finish(vs); 145204d2529dSDaniel P. Berrange } 14533e230dd2SCorentin Chary return; 14543e230dd2SCorentin Chary } 14553e230dd2SCorentin Chary 14563e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14573e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14583e230dd2SCorentin Chary int ret; 14593e230dd2SCorentin Chary 14603e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 146104d2529dSDaniel P. Berrange if (vs->disconnecting) { 14623e230dd2SCorentin Chary vnc_disconnect_finish(vs); 14633e230dd2SCorentin Chary return; 14643e230dd2SCorentin Chary } 14653e230dd2SCorentin Chary 14663e230dd2SCorentin Chary if (!ret) { 146732ed2680STim Hardeck buffer_advance(&vs->input, len); 14683e230dd2SCorentin Chary } else { 14693e230dd2SCorentin Chary vs->read_handler_expect = ret; 14703e230dd2SCorentin Chary } 14713e230dd2SCorentin Chary } 14723e230dd2SCorentin Chary } 14733e230dd2SCorentin Chary 147404d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 147504d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 147604d2529dSDaniel P. Berrange { 147704d2529dSDaniel P. Berrange VncState *vs = opaque; 147804d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 147904d2529dSDaniel P. Berrange vnc_client_read(vs); 148004d2529dSDaniel P. Berrange } 148104d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 148204d2529dSDaniel P. Berrange vnc_client_write(vs); 148304d2529dSDaniel P. Berrange } 148404d2529dSDaniel P. Berrange return TRUE; 148504d2529dSDaniel P. Berrange } 148604d2529dSDaniel P. Berrange 148704d2529dSDaniel P. Berrange 14883e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14893e230dd2SCorentin Chary { 14903e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14913e230dd2SCorentin Chary 149204d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 149304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 149404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 149504d2529dSDaniel P. Berrange } 149604d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 149704d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14983e230dd2SCorentin Chary } 14993e230dd2SCorentin Chary 15003e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 15013e230dd2SCorentin Chary } 15023e230dd2SCorentin Chary 15033e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 15043e230dd2SCorentin Chary { 15053e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 15063e230dd2SCorentin Chary } 15073e230dd2SCorentin Chary 15083e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 15093e230dd2SCorentin Chary { 15103e230dd2SCorentin Chary uint8_t buf[4]; 15113e230dd2SCorentin Chary 15123e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 15133e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 15143e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 15153e230dd2SCorentin Chary buf[3] = value & 0xFF; 15163e230dd2SCorentin Chary 15173e230dd2SCorentin Chary vnc_write(vs, buf, 4); 15183e230dd2SCorentin Chary } 15193e230dd2SCorentin Chary 15203e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 15213e230dd2SCorentin Chary { 15223e230dd2SCorentin Chary uint8_t buf[2]; 15233e230dd2SCorentin Chary 15243e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 15253e230dd2SCorentin Chary buf[1] = value & 0xFF; 15263e230dd2SCorentin Chary 15273e230dd2SCorentin Chary vnc_write(vs, buf, 2); 15283e230dd2SCorentin Chary } 15293e230dd2SCorentin Chary 15303e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 15313e230dd2SCorentin Chary { 15323e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 15333e230dd2SCorentin Chary } 15343e230dd2SCorentin Chary 15353e230dd2SCorentin Chary void vnc_flush(VncState *vs) 15363e230dd2SCorentin Chary { 1537bd023f95SCorentin Chary vnc_lock_output(vs); 1538*d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1539bd023f95SCorentin Chary vnc_client_write_locked(vs); 1540bd023f95SCorentin Chary } 1541bd023f95SCorentin Chary vnc_unlock_output(vs); 15423e230dd2SCorentin Chary } 15433e230dd2SCorentin Chary 154471a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 15453e230dd2SCorentin Chary { 15463e230dd2SCorentin Chary return data[offset]; 15473e230dd2SCorentin Chary } 15483e230dd2SCorentin Chary 154971a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 15503e230dd2SCorentin Chary { 15513e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15523e230dd2SCorentin Chary } 15533e230dd2SCorentin Chary 155471a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15553e230dd2SCorentin Chary { 15563e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15573e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15583e230dd2SCorentin Chary } 15593e230dd2SCorentin Chary 15603e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15613e230dd2SCorentin Chary { 15623e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15633e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15643e230dd2SCorentin Chary } 15653e230dd2SCorentin Chary 15663e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15673e230dd2SCorentin Chary { 15683e230dd2SCorentin Chary } 15693e230dd2SCorentin Chary 15709e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15713e230dd2SCorentin Chary { 15723e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 157314768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15743e230dd2SCorentin Chary 15753e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1576bd023f95SCorentin Chary vnc_lock_output(vs); 15773e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15783e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15793e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15803e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1581bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1582bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15833e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1584bd023f95SCorentin Chary vnc_unlock_output(vs); 15853e230dd2SCorentin Chary vnc_flush(vs); 15863e230dd2SCorentin Chary } 15873e230dd2SCorentin Chary vs->absolute = absolute; 15883e230dd2SCorentin Chary } 15893e230dd2SCorentin Chary 15903e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15913e230dd2SCorentin Chary { 15927fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 159314768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 159414768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 159514768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1596d20a580bSEric Blake [INPUT_BUTTON_WHEELUP] = 0x08, 1597d20a580bSEric Blake [INPUT_BUTTON_WHEELDOWN] = 0x10, 159814768ebaSGerd Hoffmann }; 159914768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1600bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1601bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 16023e230dd2SCorentin Chary 160314768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 160414768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 160514768ebaSGerd Hoffmann vs->last_bmask = button_mask; 160614768ebaSGerd Hoffmann } 16073e230dd2SCorentin Chary 16083e230dd2SCorentin Chary if (vs->absolute) { 160914768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_X, x, width); 161014768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height); 16113e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 161214768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 161314768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 16143e230dd2SCorentin Chary } else { 161514768ebaSGerd Hoffmann if (vs->last_x != -1) { 161614768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 161714768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 161814768ebaSGerd Hoffmann } 16193e230dd2SCorentin Chary vs->last_x = x; 16203e230dd2SCorentin Chary vs->last_y = y; 16213e230dd2SCorentin Chary } 162214768ebaSGerd Hoffmann qemu_input_event_sync(); 16233e230dd2SCorentin Chary } 16243e230dd2SCorentin Chary 16253e230dd2SCorentin Chary static void reset_keys(VncState *vs) 16263e230dd2SCorentin Chary { 16273e230dd2SCorentin Chary int i; 16283e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 16293e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 16308d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 16313e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 16323e230dd2SCorentin Chary } 16333e230dd2SCorentin Chary } 16343e230dd2SCorentin Chary } 16353e230dd2SCorentin Chary 16363e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 16373e230dd2SCorentin Chary { 16383e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 16398d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 16402deb4accSGerd Hoffmann qemu_input_event_send_key_delay(0); 16418d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 16422deb4accSGerd Hoffmann qemu_input_event_send_key_delay(0); 16433e230dd2SCorentin Chary } 16443e230dd2SCorentin Chary 1645ab99e5c1SLei Li static int current_led_state(VncState *vs) 1646ab99e5c1SLei Li { 1647ab99e5c1SLei Li int ledstate = 0; 1648ab99e5c1SLei Li 1649ab99e5c1SLei Li if (vs->modifiers_state[0x46]) { 1650ab99e5c1SLei Li ledstate |= QEMU_SCROLL_LOCK_LED; 1651ab99e5c1SLei Li } 1652ab99e5c1SLei Li if (vs->modifiers_state[0x45]) { 1653ab99e5c1SLei Li ledstate |= QEMU_NUM_LOCK_LED; 1654ab99e5c1SLei Li } 1655ab99e5c1SLei Li if (vs->modifiers_state[0x3a]) { 1656ab99e5c1SLei Li ledstate |= QEMU_CAPS_LOCK_LED; 1657ab99e5c1SLei Li } 1658ab99e5c1SLei Li 1659ab99e5c1SLei Li return ledstate; 1660ab99e5c1SLei Li } 1661ab99e5c1SLei Li 1662ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1663ab99e5c1SLei Li { 1664ab99e5c1SLei Li int ledstate = 0; 1665ab99e5c1SLei Li 1666ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1667ab99e5c1SLei Li return; 1668ab99e5c1SLei Li } 1669ab99e5c1SLei Li 1670ab99e5c1SLei Li ledstate = current_led_state(vs); 1671ab99e5c1SLei Li vnc_lock_output(vs); 1672ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1673ab99e5c1SLei Li vnc_write_u8(vs, 0); 1674ab99e5c1SLei Li vnc_write_u16(vs, 1); 1675ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1676ab99e5c1SLei Li vnc_write_u8(vs, ledstate); 1677ab99e5c1SLei Li vnc_unlock_output(vs); 1678ab99e5c1SLei Li vnc_flush(vs); 1679ab99e5c1SLei Li } 1680ab99e5c1SLei Li 16813e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16823e230dd2SCorentin Chary { 16833e230dd2SCorentin Chary VncState *vs = opaque; 168496f3d174SLei Li int caps, num, scr; 16851483adcfSLei Li bool has_changed = (ledstate != current_led_state(vs)); 16863e230dd2SCorentin Chary 168740066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 168840066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 168940066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 169040066175SGerd Hoffmann 16913e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 16923e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 169396f3d174SLei Li scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0; 16943e230dd2SCorentin Chary 16953e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 16963e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 16973e230dd2SCorentin Chary } 16983e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 16993e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 17003e230dd2SCorentin Chary } 170196f3d174SLei Li if (vs->modifiers_state[0x46] != scr) { 170296f3d174SLei Li vs->modifiers_state[0x46] = scr; 170396f3d174SLei Li } 1704ab99e5c1SLei Li 1705ab99e5c1SLei Li /* Sending the current led state message to the client */ 17061483adcfSLei Li if (has_changed) { 1707ab99e5c1SLei Li vnc_led_state_change(vs); 1708ab99e5c1SLei Li } 17093e230dd2SCorentin Chary } 17103e230dd2SCorentin Chary 17113e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 17123e230dd2SCorentin Chary { 17133e230dd2SCorentin Chary /* QEMU console switch */ 17143e230dd2SCorentin Chary switch(keycode) { 17153e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17163e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17173e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17183e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17193e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17203e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17213e230dd2SCorentin Chary if (down) 17223e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 17233e230dd2SCorentin Chary else 17243e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 17253e230dd2SCorentin Chary break; 17263e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 17271d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 17281d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 17293e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 17303e230dd2SCorentin Chary reset_keys(vs); 17313e230dd2SCorentin Chary console_select(keycode - 0x02); 17323e230dd2SCorentin Chary return; 17333e230dd2SCorentin Chary } 17343e230dd2SCorentin Chary break; 17353e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 17363e230dd2SCorentin Chary case 0x45: /* NumLock */ 17373e230dd2SCorentin Chary if (down) 17383e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 17393e230dd2SCorentin Chary break; 17403e230dd2SCorentin Chary } 17413e230dd2SCorentin Chary 1742e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1743e7b2aaccSLei Li state extension. 1744e7b2aaccSLei Li */ 17459892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1746e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 17473e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 17483e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 17493e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 17503e230dd2SCorentin Chary toggles numlock away from the VNC window. 17513e230dd2SCorentin Chary */ 17523e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 17533e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 175440066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 17553e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 17563e230dd2SCorentin Chary press_key(vs, 0xff7f); 17573e230dd2SCorentin Chary } 17583e230dd2SCorentin Chary } else { 17593e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 176040066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 17613e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 17623e230dd2SCorentin Chary press_key(vs, 0xff7f); 17633e230dd2SCorentin Chary } 17643e230dd2SCorentin Chary } 17653e230dd2SCorentin Chary } 17663e230dd2SCorentin Chary 17679892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1768e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 17693e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 17703e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 17713e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 17723e230dd2SCorentin Chary toggles capslock away from the VNC window. 17733e230dd2SCorentin Chary */ 17743e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 17753e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 17763e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 17773e230dd2SCorentin Chary if (capslock) { 17783e230dd2SCorentin Chary if (uppercase == shift) { 177940066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17803e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17813e230dd2SCorentin Chary press_key(vs, 0xffe5); 17823e230dd2SCorentin Chary } 17833e230dd2SCorentin Chary } else { 17843e230dd2SCorentin Chary if (uppercase != shift) { 178540066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17863e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17873e230dd2SCorentin Chary press_key(vs, 0xffe5); 17883e230dd2SCorentin Chary } 17893e230dd2SCorentin Chary } 17903e230dd2SCorentin Chary } 17913e230dd2SCorentin Chary 179281c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17938d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 17943e230dd2SCorentin Chary } else { 1795e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1796e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1797e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17983e230dd2SCorentin Chary /* QEMU console emulation */ 17993e230dd2SCorentin Chary if (down) { 18003e230dd2SCorentin Chary switch (keycode) { 18013e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 18023e230dd2SCorentin Chary case 0x36: /* Right Shift */ 18033e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 18043e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 18053e230dd2SCorentin Chary case 0x38: /* Left ALT */ 18063e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 18073e230dd2SCorentin Chary break; 18083e230dd2SCorentin Chary case 0xc8: 18093e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 18103e230dd2SCorentin Chary break; 18113e230dd2SCorentin Chary case 0xd0: 18123e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 18133e230dd2SCorentin Chary break; 18143e230dd2SCorentin Chary case 0xcb: 18153e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 18163e230dd2SCorentin Chary break; 18173e230dd2SCorentin Chary case 0xcd: 18183e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 18193e230dd2SCorentin Chary break; 18203e230dd2SCorentin Chary case 0xd3: 18213e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 18223e230dd2SCorentin Chary break; 18233e230dd2SCorentin Chary case 0xc7: 18243e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 18253e230dd2SCorentin Chary break; 18263e230dd2SCorentin Chary case 0xcf: 18273e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 18283e230dd2SCorentin Chary break; 18293e230dd2SCorentin Chary case 0xc9: 18303e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 18313e230dd2SCorentin Chary break; 18323e230dd2SCorentin Chary case 0xd1: 18333e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 18343e230dd2SCorentin Chary break; 18353e230dd2SCorentin Chary 18363e230dd2SCorentin Chary case 0x47: 18373e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 18383e230dd2SCorentin Chary break; 18393e230dd2SCorentin Chary case 0x48: 18403e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 18413e230dd2SCorentin Chary break; 18423e230dd2SCorentin Chary case 0x49: 18433e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 18443e230dd2SCorentin Chary break; 18453e230dd2SCorentin Chary case 0x4b: 18463e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 18473e230dd2SCorentin Chary break; 18483e230dd2SCorentin Chary case 0x4c: 18493e230dd2SCorentin Chary kbd_put_keysym('5'); 18503e230dd2SCorentin Chary break; 18513e230dd2SCorentin Chary case 0x4d: 18523e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 18533e230dd2SCorentin Chary break; 18543e230dd2SCorentin Chary case 0x4f: 18553e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 18563e230dd2SCorentin Chary break; 18573e230dd2SCorentin Chary case 0x50: 18583e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 18593e230dd2SCorentin Chary break; 18603e230dd2SCorentin Chary case 0x51: 18613e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 18623e230dd2SCorentin Chary break; 18633e230dd2SCorentin Chary case 0x52: 18643e230dd2SCorentin Chary kbd_put_keysym('0'); 18653e230dd2SCorentin Chary break; 18663e230dd2SCorentin Chary case 0x53: 18673e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 18683e230dd2SCorentin Chary break; 18693e230dd2SCorentin Chary 18703e230dd2SCorentin Chary case 0xb5: 18713e230dd2SCorentin Chary kbd_put_keysym('/'); 18723e230dd2SCorentin Chary break; 18733e230dd2SCorentin Chary case 0x37: 18743e230dd2SCorentin Chary kbd_put_keysym('*'); 18753e230dd2SCorentin Chary break; 18763e230dd2SCorentin Chary case 0x4a: 18773e230dd2SCorentin Chary kbd_put_keysym('-'); 18783e230dd2SCorentin Chary break; 18793e230dd2SCorentin Chary case 0x4e: 18803e230dd2SCorentin Chary kbd_put_keysym('+'); 18813e230dd2SCorentin Chary break; 18823e230dd2SCorentin Chary case 0x9c: 18833e230dd2SCorentin Chary kbd_put_keysym('\n'); 18843e230dd2SCorentin Chary break; 18853e230dd2SCorentin Chary 18863e230dd2SCorentin Chary default: 1887e26437c2SGerd Hoffmann if (control) { 1888e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1889e26437c2SGerd Hoffmann } else { 18903e230dd2SCorentin Chary kbd_put_keysym(sym); 1891e26437c2SGerd Hoffmann } 18923e230dd2SCorentin Chary break; 18933e230dd2SCorentin Chary } 18943e230dd2SCorentin Chary } 18953e230dd2SCorentin Chary } 18963e230dd2SCorentin Chary } 18973e230dd2SCorentin Chary 18987bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18997bc9318bSGerd Hoffmann { 19007bc9318bSGerd Hoffmann static const int keycodes[] = { 19017bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 19027bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 19037bc9318bSGerd Hoffmann }; 19047bc9318bSGerd Hoffmann int i, keycode; 19057bc9318bSGerd Hoffmann 190681c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 19077bc9318bSGerd Hoffmann return; 19087bc9318bSGerd Hoffmann } 19097bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 19107bc9318bSGerd Hoffmann keycode = keycodes[i]; 19117bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 19127bc9318bSGerd Hoffmann continue; 19137bc9318bSGerd Hoffmann } 19148d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 19157bc9318bSGerd Hoffmann } 19167bc9318bSGerd Hoffmann } 19177bc9318bSGerd Hoffmann 191840066175SGerd Hoffmann static const char *code2name(int keycode) 191940066175SGerd Hoffmann { 192040066175SGerd Hoffmann return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)]; 192140066175SGerd Hoffmann } 192240066175SGerd Hoffmann 19233e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 19243e230dd2SCorentin Chary { 19253e230dd2SCorentin Chary int keycode; 19263e230dd2SCorentin Chary int lsym = sym; 19273e230dd2SCorentin Chary 192881c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 19293e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 19303e230dd2SCorentin Chary } 19313e230dd2SCorentin Chary 19323e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 193340066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 19343e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19353e230dd2SCorentin Chary } 19363e230dd2SCorentin Chary 19373e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 19383e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 19393e230dd2SCorentin Chary { 19403e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 194140066175SGerd Hoffmann if (keyboard_layout) { 19423e230dd2SCorentin Chary key_event(vs, down, sym); 194340066175SGerd Hoffmann } else { 194440066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 19453e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19463e230dd2SCorentin Chary } 194740066175SGerd Hoffmann } 19483e230dd2SCorentin Chary 19493e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1950bea60dd7SPeter Lieven int x, int y, int w, int h) 19513e230dd2SCorentin Chary { 19523e230dd2SCorentin Chary vs->need_update = 1; 1953bea60dd7SPeter Lieven 1954bea60dd7SPeter Lieven if (incremental) { 1955bea60dd7SPeter Lieven return; 19563e230dd2SCorentin Chary } 1957bea60dd7SPeter Lieven 195807535a89SStephan Kulow vs->force_update = 1; 1959f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 19603e230dd2SCorentin Chary } 19613e230dd2SCorentin Chary 19623e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 19633e230dd2SCorentin Chary { 1964bd023f95SCorentin Chary vnc_lock_output(vs); 19653e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19663e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19673e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1968d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1969bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1970bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19713e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1972bd023f95SCorentin Chary vnc_unlock_output(vs); 19733e230dd2SCorentin Chary vnc_flush(vs); 19743e230dd2SCorentin Chary } 19753e230dd2SCorentin Chary 19763e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19773e230dd2SCorentin Chary { 1978bd023f95SCorentin Chary vnc_lock_output(vs); 19793e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19803e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19813e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1982d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1983bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1984bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19853e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1986bd023f95SCorentin Chary vnc_unlock_output(vs); 19873e230dd2SCorentin Chary vnc_flush(vs); 19883e230dd2SCorentin Chary } 19893e230dd2SCorentin Chary 19903e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19913e230dd2SCorentin Chary { 19923e230dd2SCorentin Chary int i; 19933e230dd2SCorentin Chary unsigned int enc = 0; 19943e230dd2SCorentin Chary 19953e230dd2SCorentin Chary vs->features = 0; 19963e230dd2SCorentin Chary vs->vnc_encoding = 0; 1997d1af0e05SCorentin Chary vs->tight.compression = 9; 1998d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19993e230dd2SCorentin Chary vs->absolute = -1; 20003e230dd2SCorentin Chary 20013e230dd2SCorentin Chary /* 20023e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 2003e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 20043e230dd2SCorentin Chary * will be set at the end of the loop. 20053e230dd2SCorentin Chary */ 20063e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 20073e230dd2SCorentin Chary enc = encodings[i]; 20083e230dd2SCorentin Chary switch (enc) { 20093e230dd2SCorentin Chary case VNC_ENCODING_RAW: 20103e230dd2SCorentin Chary vs->vnc_encoding = enc; 20113e230dd2SCorentin Chary break; 20123e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 20133e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 20143e230dd2SCorentin Chary break; 20153e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 20163e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 20173e230dd2SCorentin Chary vs->vnc_encoding = enc; 20183e230dd2SCorentin Chary break; 20193e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 20203e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 20213e230dd2SCorentin Chary vs->vnc_encoding = enc; 20223e230dd2SCorentin Chary break; 2023fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 2024efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 2025efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 2026efe556adSCorentin Chary vs->vnc_encoding = enc; 2027efe556adSCorentin Chary break; 2028fe3e7f2dSJoel Martin #endif 20293e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 20303e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 20313e230dd2SCorentin Chary vs->vnc_encoding = enc; 20323e230dd2SCorentin Chary break; 2033148954faSCorentin Chary case VNC_ENCODING_ZRLE: 2034148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 2035148954faSCorentin Chary vs->vnc_encoding = enc; 2036148954faSCorentin Chary break; 2037148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 2038148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 2039148954faSCorentin Chary vs->vnc_encoding = enc; 2040148954faSCorentin Chary break; 20413e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 20423e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 20433e230dd2SCorentin Chary break; 20443e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 20453e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 20463e230dd2SCorentin Chary break; 20473e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 20483e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 20493e230dd2SCorentin Chary break; 20503e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 20513e230dd2SCorentin Chary send_ext_key_event_ack(vs); 20523e230dd2SCorentin Chary break; 20533e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 20543e230dd2SCorentin Chary send_ext_audio_ack(vs); 20553e230dd2SCorentin Chary break; 20563e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 20573e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 20583e230dd2SCorentin Chary break; 2059ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 2060ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 2061ab99e5c1SLei Li break; 20623e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 2063d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 20643e230dd2SCorentin Chary break; 20653e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 2066b31f519eSCorentin Chary if (vs->vd->lossy) { 2067d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 2068b31f519eSCorentin Chary } 20693e230dd2SCorentin Chary break; 20703e230dd2SCorentin Chary default: 20713e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 20723e230dd2SCorentin Chary break; 20733e230dd2SCorentin Chary } 20743e230dd2SCorentin Chary } 20753e230dd2SCorentin Chary vnc_desktop_resize(vs); 20769e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2077ab99e5c1SLei Li vnc_led_state_change(vs); 20783e230dd2SCorentin Chary } 20793e230dd2SCorentin Chary 20803e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20813e230dd2SCorentin Chary { 20829f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20839f64916dSGerd Hoffmann 20849f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20853e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20863e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20873e230dd2SCorentin Chary } else { 20883e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20893e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20903e230dd2SCorentin Chary } 20913e230dd2SCorentin Chary } 20923e230dd2SCorentin Chary 20933e230dd2SCorentin Chary static void set_pixel_format(VncState *vs, 20943e230dd2SCorentin Chary int bits_per_pixel, int depth, 20953e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20963e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20973e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20983e230dd2SCorentin Chary { 20993e230dd2SCorentin Chary if (!true_color_flag) { 21003e230dd2SCorentin Chary vnc_client_error(vs); 21013e230dd2SCorentin Chary return; 21023e230dd2SCorentin Chary } 21033e230dd2SCorentin Chary 2104e6908bfeSPetr Matousek switch (bits_per_pixel) { 2105e6908bfeSPetr Matousek case 8: 2106e6908bfeSPetr Matousek case 16: 2107e6908bfeSPetr Matousek case 32: 2108e6908bfeSPetr Matousek break; 2109e6908bfeSPetr Matousek default: 2110e6908bfeSPetr Matousek vnc_client_error(vs); 2111e6908bfeSPetr Matousek return; 2112e6908bfeSPetr Matousek } 2113e6908bfeSPetr Matousek 21144c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 21159f64916dSGerd Hoffmann vs->client_pf.rbits = hweight_long(red_max); 21169f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 21179f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 21184c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 21199f64916dSGerd Hoffmann vs->client_pf.gbits = hweight_long(green_max); 21209f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 21219f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 21224c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 21239f64916dSGerd Hoffmann vs->client_pf.bbits = hweight_long(blue_max); 21249f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 21259f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 21269f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 21279f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 21289f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 21299f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 21303e230dd2SCorentin Chary 21313e230dd2SCorentin Chary set_pixel_conversion(vs); 21323e230dd2SCorentin Chary 21331d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 21341d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 21353e230dd2SCorentin Chary } 21363e230dd2SCorentin Chary 21373e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 21383e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 21393e230dd2SCorentin Chary 21409f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 21419f64916dSGerd Hoffmann 21429f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 21439f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 21443e230dd2SCorentin Chary 21453e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 21463e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 21473e230dd2SCorentin Chary #else 21483e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21493e230dd2SCorentin Chary #endif 21503e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21519f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21529f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21539f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21549f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21559f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21569f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21579f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21583e230dd2SCorentin Chary 21593e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21603e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21613e230dd2SCorentin Chary } 21623e230dd2SCorentin Chary 21633e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21643e230dd2SCorentin Chary { 21653e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 21663e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2167bd023f95SCorentin Chary vnc_lock_output(vs); 21683e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 21693e230dd2SCorentin Chary vnc_write_u8(vs, 0); 21703e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2171d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2172bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2173bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2174d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 21753e230dd2SCorentin Chary pixel_format_message(vs); 2176bd023f95SCorentin Chary vnc_unlock_output(vs); 21773e230dd2SCorentin Chary vnc_flush(vs); 21783e230dd2SCorentin Chary } else { 21793e230dd2SCorentin Chary set_pixel_conversion(vs); 21803e230dd2SCorentin Chary } 21813e230dd2SCorentin Chary } 21823e230dd2SCorentin Chary 21833e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 21843e230dd2SCorentin Chary { 21853e230dd2SCorentin Chary int i; 21863e230dd2SCorentin Chary uint16_t limit; 21873e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21883e230dd2SCorentin Chary 21893e230dd2SCorentin Chary if (data[0] > 3) { 21900f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21913e230dd2SCorentin Chary } 21923e230dd2SCorentin Chary 21933e230dd2SCorentin Chary switch (data[0]) { 21943e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21953e230dd2SCorentin Chary if (len == 1) 21963e230dd2SCorentin Chary return 20; 21973e230dd2SCorentin Chary 21983e230dd2SCorentin Chary set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), 21993e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 22003e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 22013e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 22023e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 22033e230dd2SCorentin Chary break; 22043e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 22053e230dd2SCorentin Chary if (len == 1) 22063e230dd2SCorentin Chary return 4; 22073e230dd2SCorentin Chary 22083e230dd2SCorentin Chary if (len == 4) { 22093e230dd2SCorentin Chary limit = read_u16(data, 2); 22103e230dd2SCorentin Chary if (limit > 0) 22113e230dd2SCorentin Chary return 4 + (limit * 4); 22123e230dd2SCorentin Chary } else 22133e230dd2SCorentin Chary limit = read_u16(data, 2); 22143e230dd2SCorentin Chary 22153e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 22163e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 22173e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 22183e230dd2SCorentin Chary } 22193e230dd2SCorentin Chary 22203e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 22213e230dd2SCorentin Chary break; 22223e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 22233e230dd2SCorentin Chary if (len == 1) 22243e230dd2SCorentin Chary return 10; 22253e230dd2SCorentin Chary 22263e230dd2SCorentin Chary framebuffer_update_request(vs, 22273e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 22283e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 22293e230dd2SCorentin Chary break; 22303e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 22313e230dd2SCorentin Chary if (len == 1) 22323e230dd2SCorentin Chary return 8; 22333e230dd2SCorentin Chary 22343e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 22353e230dd2SCorentin Chary break; 22363e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 22373e230dd2SCorentin Chary if (len == 1) 22383e230dd2SCorentin Chary return 6; 22393e230dd2SCorentin Chary 22403e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 22413e230dd2SCorentin Chary break; 22423e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2243f9a70e79SPeter Lieven if (len == 1) { 22443e230dd2SCorentin Chary return 8; 2245f9a70e79SPeter Lieven } 22463e230dd2SCorentin Chary if (len == 8) { 22473e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2248f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2249f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2250f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2251f9a70e79SPeter Lieven vnc_client_error(vs); 2252f9a70e79SPeter Lieven break; 2253f9a70e79SPeter Lieven } 2254f9a70e79SPeter Lieven if (dlen > 0) { 22553e230dd2SCorentin Chary return 8 + dlen; 22563e230dd2SCorentin Chary } 2257f9a70e79SPeter Lieven } 22583e230dd2SCorentin Chary 22593e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22603e230dd2SCorentin Chary break; 22613e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22623e230dd2SCorentin Chary if (len == 1) 22633e230dd2SCorentin Chary return 2; 22643e230dd2SCorentin Chary 22653e230dd2SCorentin Chary switch (read_u8(data, 1)) { 22663e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 22673e230dd2SCorentin Chary if (len == 2) 22683e230dd2SCorentin Chary return 12; 22693e230dd2SCorentin Chary 22703e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 22713e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 22723e230dd2SCorentin Chary break; 22733e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 22743e230dd2SCorentin Chary if (len == 2) 22753e230dd2SCorentin Chary return 4; 22763e230dd2SCorentin Chary 22773e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 22783e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 22793e230dd2SCorentin Chary audio_add(vs); 22803e230dd2SCorentin Chary break; 22813e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 22823e230dd2SCorentin Chary audio_del(vs); 22833e230dd2SCorentin Chary break; 22843e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22853e230dd2SCorentin Chary if (len == 4) 22863e230dd2SCorentin Chary return 10; 22873e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22883e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22893e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22903e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22913e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22923e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22933e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22943e230dd2SCorentin Chary default: 2295153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 22963e230dd2SCorentin Chary vnc_client_error(vs); 22973e230dd2SCorentin Chary break; 22983e230dd2SCorentin Chary } 22993e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 23003e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2301153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio channel coount %d\n", 23023e230dd2SCorentin Chary read_u8(data, 5)); 23033e230dd2SCorentin Chary vnc_client_error(vs); 23043e230dd2SCorentin Chary break; 23053e230dd2SCorentin Chary } 23063e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 23073e230dd2SCorentin Chary break; 23083e230dd2SCorentin Chary default: 2309153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 23103e230dd2SCorentin Chary vnc_client_error(vs); 23113e230dd2SCorentin Chary break; 23123e230dd2SCorentin Chary } 23133e230dd2SCorentin Chary break; 23143e230dd2SCorentin Chary 23153e230dd2SCorentin Chary default: 2316153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 23173e230dd2SCorentin Chary vnc_client_error(vs); 23183e230dd2SCorentin Chary break; 23193e230dd2SCorentin Chary } 23203e230dd2SCorentin Chary break; 23213e230dd2SCorentin Chary default: 2322153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 23233e230dd2SCorentin Chary vnc_client_error(vs); 23243e230dd2SCorentin Chary break; 23253e230dd2SCorentin Chary } 23263e230dd2SCorentin Chary 23273e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23283e230dd2SCorentin Chary return 0; 23293e230dd2SCorentin Chary } 23303e230dd2SCorentin Chary 23313e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 23323e230dd2SCorentin Chary { 23333e230dd2SCorentin Chary char buf[1024]; 23348cf36489SGerd Hoffmann VncShareMode mode; 23353e230dd2SCorentin Chary int size; 23363e230dd2SCorentin Chary 23378cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 23388cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 23398cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 23408cf36489SGerd Hoffmann /* 23418cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 23428cf36489SGerd Hoffmann * 23438cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 23448cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 23458cf36489SGerd Hoffmann * reasons. 23468cf36489SGerd Hoffmann */ 23478cf36489SGerd Hoffmann break; 23488cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23498cf36489SGerd Hoffmann /* 23508cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23518cf36489SGerd Hoffmann * 23528cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23538cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23548cf36489SGerd Hoffmann * as no exclusive connection exists. 23558cf36489SGerd Hoffmann * 23568cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23578cf36489SGerd Hoffmann */ 23588cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23598cf36489SGerd Hoffmann VncState *client; 23608cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23618cf36489SGerd Hoffmann if (vs == client) { 23628cf36489SGerd Hoffmann continue; 23638cf36489SGerd Hoffmann } 23648cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23658cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 23668cf36489SGerd Hoffmann continue; 23678cf36489SGerd Hoffmann } 23688cf36489SGerd Hoffmann vnc_disconnect_start(client); 23698cf36489SGerd Hoffmann } 23708cf36489SGerd Hoffmann } 23718cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 23728cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 23738cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23748cf36489SGerd Hoffmann return 0; 23758cf36489SGerd Hoffmann } 23768cf36489SGerd Hoffmann } 23778cf36489SGerd Hoffmann break; 23788cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 23798cf36489SGerd Hoffmann /* 23808cf36489SGerd Hoffmann * Policy: Shared connects only. 23818cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 23828cf36489SGerd Hoffmann * 23838cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 23848cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23858cf36489SGerd Hoffmann * client disconnect everybody else. 23868cf36489SGerd Hoffmann */ 23878cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23888cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23898cf36489SGerd Hoffmann return 0; 23908cf36489SGerd Hoffmann } 23918cf36489SGerd Hoffmann break; 23928cf36489SGerd Hoffmann } 23938cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23948cf36489SGerd Hoffmann 2395e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2396e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2397e5f34cddSGerd Hoffmann return 0; 2398e5f34cddSGerd Hoffmann } 2399e5f34cddSGerd Hoffmann 2400bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2401bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 24023e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 24033e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 24043e230dd2SCorentin Chary 24053e230dd2SCorentin Chary pixel_format_message(vs); 24063e230dd2SCorentin Chary 24073e230dd2SCorentin Chary if (qemu_name) 24083e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 24093e230dd2SCorentin Chary else 24103e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 24113e230dd2SCorentin Chary 24123e230dd2SCorentin Chary vnc_write_u32(vs, size); 24133e230dd2SCorentin Chary vnc_write(vs, buf, size); 24143e230dd2SCorentin Chary vnc_flush(vs); 24153e230dd2SCorentin Chary 24163e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2417fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 24183e230dd2SCorentin Chary 24193e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 24203e230dd2SCorentin Chary 24213e230dd2SCorentin Chary return 0; 24223e230dd2SCorentin Chary } 24233e230dd2SCorentin Chary 24243e230dd2SCorentin Chary void start_client_init(VncState *vs) 24253e230dd2SCorentin Chary { 24263e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 24273e230dd2SCorentin Chary } 24283e230dd2SCorentin Chary 24293e230dd2SCorentin Chary static void make_challenge(VncState *vs) 24303e230dd2SCorentin Chary { 24313e230dd2SCorentin Chary int i; 24323e230dd2SCorentin Chary 24333e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 24343e230dd2SCorentin Chary 24353e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 24363e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 24373e230dd2SCorentin Chary } 24383e230dd2SCorentin Chary 24393e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 24403e230dd2SCorentin Chary { 24413e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2442800567a6SDaniel P. Berrange size_t i, pwlen; 24433e230dd2SCorentin Chary unsigned char key[8]; 24443c9405a0SGerd Hoffmann time_t now = time(NULL); 244560928458SGonglei QCryptoCipher *cipher = NULL; 2446800567a6SDaniel P. Berrange Error *err = NULL; 24473e230dd2SCorentin Chary 24481cd20f8bSAnthony Liguori if (!vs->vd->password) { 24493e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 24506bffdf0fSGerd Hoffmann goto reject; 24513e230dd2SCorentin Chary } 24523c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 24533c9405a0SGerd Hoffmann VNC_DEBUG("Password is expired"); 24543c9405a0SGerd Hoffmann goto reject; 24553c9405a0SGerd Hoffmann } 24563e230dd2SCorentin Chary 24573e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24583e230dd2SCorentin Chary 24593e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24603e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24613e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24623e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2463800567a6SDaniel P. Berrange 2464800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2465800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2466800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2467800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2468800567a6SDaniel P. Berrange &err); 2469800567a6SDaniel P. Berrange if (!cipher) { 2470800567a6SDaniel P. Berrange VNC_DEBUG("Cannot initialize cipher %s", 2471800567a6SDaniel P. Berrange error_get_pretty(err)); 2472800567a6SDaniel P. Berrange error_free(err); 2473800567a6SDaniel P. Berrange goto reject; 2474800567a6SDaniel P. Berrange } 2475800567a6SDaniel P. Berrange 2476a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2477800567a6SDaniel P. Berrange vs->challenge, 2478800567a6SDaniel P. Berrange response, 2479800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2480800567a6SDaniel P. Berrange &err) < 0) { 2481800567a6SDaniel P. Berrange VNC_DEBUG("Cannot encrypt challenge %s", 2482800567a6SDaniel P. Berrange error_get_pretty(err)); 2483800567a6SDaniel P. Berrange error_free(err); 2484800567a6SDaniel P. Berrange goto reject; 2485800567a6SDaniel P. Berrange } 24863e230dd2SCorentin Chary 24873e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 24883e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2489e5bed759SDong Xu Wang VNC_DEBUG("Client challenge response did not match\n"); 24906bffdf0fSGerd Hoffmann goto reject; 24916bffdf0fSGerd Hoffmann } else { 24926bffdf0fSGerd Hoffmann VNC_DEBUG("Accepting VNC challenge response\n"); 24936bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 24946bffdf0fSGerd Hoffmann vnc_flush(vs); 24956bffdf0fSGerd Hoffmann 24966bffdf0fSGerd Hoffmann start_client_init(vs); 24976bffdf0fSGerd Hoffmann } 249860928458SGonglei 249960928458SGonglei qcrypto_cipher_free(cipher); 25006bffdf0fSGerd Hoffmann return 0; 25016bffdf0fSGerd Hoffmann 25026bffdf0fSGerd Hoffmann reject: 25033e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 25043e230dd2SCorentin Chary if (vs->minor >= 8) { 25053e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25063e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25073e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25083e230dd2SCorentin Chary } 25093e230dd2SCorentin Chary vnc_flush(vs); 25103e230dd2SCorentin Chary vnc_client_error(vs); 251160928458SGonglei qcrypto_cipher_free(cipher); 25123e230dd2SCorentin Chary return 0; 25133e230dd2SCorentin Chary } 25143e230dd2SCorentin Chary 25153e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 25163e230dd2SCorentin Chary { 25173e230dd2SCorentin Chary make_challenge(vs); 25183e230dd2SCorentin Chary /* Send client a 'random' challenge */ 25193e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 25203e230dd2SCorentin Chary vnc_flush(vs); 25213e230dd2SCorentin Chary 25223e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 25233e230dd2SCorentin Chary } 25243e230dd2SCorentin Chary 25253e230dd2SCorentin Chary 25263e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 25273e230dd2SCorentin Chary { 25283e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 25293e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 25307e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 25313e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 25323e230dd2SCorentin Chary vnc_write_u32(vs, 1); 25333e230dd2SCorentin Chary if (vs->minor >= 8) { 25343e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25353e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25363e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25373e230dd2SCorentin Chary } 25383e230dd2SCorentin Chary vnc_client_error(vs); 25393e230dd2SCorentin Chary } else { /* Accept requested auth */ 25403e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 25417e7e2ebcSDaniel P. Berrange switch (vs->auth) { 25423e230dd2SCorentin Chary case VNC_AUTH_NONE: 25433e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 25443e230dd2SCorentin Chary if (vs->minor >= 8) { 25453e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25463e230dd2SCorentin Chary vnc_flush(vs); 25473e230dd2SCorentin Chary } 25483e230dd2SCorentin Chary start_client_init(vs); 25493e230dd2SCorentin Chary break; 25503e230dd2SCorentin Chary 25513e230dd2SCorentin Chary case VNC_AUTH_VNC: 25523e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 25533e230dd2SCorentin Chary start_auth_vnc(vs); 25543e230dd2SCorentin Chary break; 25553e230dd2SCorentin Chary 25563e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25573a93113aSDong Xu Wang VNC_DEBUG("Accept VeNCrypt auth\n"); 25583e230dd2SCorentin Chary start_auth_vencrypt(vs); 25593e230dd2SCorentin Chary break; 25603e230dd2SCorentin Chary 25613e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25623e230dd2SCorentin Chary case VNC_AUTH_SASL: 25633e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 25643e230dd2SCorentin Chary start_auth_sasl(vs); 25653e230dd2SCorentin Chary break; 25663e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 25673e230dd2SCorentin Chary 25683e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 25697e7e2ebcSDaniel P. Berrange VNC_DEBUG("Reject auth %d server code bug\n", vs->auth); 25703e230dd2SCorentin Chary vnc_write_u8(vs, 1); 25713e230dd2SCorentin Chary if (vs->minor >= 8) { 25723e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25733e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25743e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25753e230dd2SCorentin Chary } 25763e230dd2SCorentin Chary vnc_client_error(vs); 25773e230dd2SCorentin Chary } 25783e230dd2SCorentin Chary } 25793e230dd2SCorentin Chary return 0; 25803e230dd2SCorentin Chary } 25813e230dd2SCorentin Chary 25823e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 25833e230dd2SCorentin Chary { 25843e230dd2SCorentin Chary char local[13]; 25853e230dd2SCorentin Chary 25863e230dd2SCorentin Chary memcpy(local, version, 12); 25873e230dd2SCorentin Chary local[12] = 0; 25883e230dd2SCorentin Chary 25893e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 25903e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 25913e230dd2SCorentin Chary vnc_client_error(vs); 25923e230dd2SCorentin Chary return 0; 25933e230dd2SCorentin Chary } 25943e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 25953e230dd2SCorentin Chary if (vs->major != 3 || 25963e230dd2SCorentin Chary (vs->minor != 3 && 25973e230dd2SCorentin Chary vs->minor != 4 && 25983e230dd2SCorentin Chary vs->minor != 5 && 25993e230dd2SCorentin Chary vs->minor != 7 && 26003e230dd2SCorentin Chary vs->minor != 8)) { 26013e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 26023e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 26033e230dd2SCorentin Chary vnc_flush(vs); 26043e230dd2SCorentin Chary vnc_client_error(vs); 26053e230dd2SCorentin Chary return 0; 26063e230dd2SCorentin Chary } 26073e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 26083e230dd2SCorentin Chary * as equivalent to v3.3 by servers 26093e230dd2SCorentin Chary */ 26103e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 26113e230dd2SCorentin Chary vs->minor = 3; 26123e230dd2SCorentin Chary 26133e230dd2SCorentin Chary if (vs->minor == 3) { 26147e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 26153e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 26167e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 26173e230dd2SCorentin Chary vnc_flush(vs); 26183e230dd2SCorentin Chary start_client_init(vs); 26197e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 26203e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 26217e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 26223e230dd2SCorentin Chary vnc_flush(vs); 26233e230dd2SCorentin Chary start_auth_vnc(vs); 26243e230dd2SCorentin Chary } else { 26257e7e2ebcSDaniel P. Berrange VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth); 26263e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 26273e230dd2SCorentin Chary vnc_flush(vs); 26283e230dd2SCorentin Chary vnc_client_error(vs); 26293e230dd2SCorentin Chary } 26303e230dd2SCorentin Chary } else { 26317e7e2ebcSDaniel P. Berrange VNC_DEBUG("Telling client we support auth %d\n", vs->auth); 26323e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 26337e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 26343e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 26353e230dd2SCorentin Chary vnc_flush(vs); 26363e230dd2SCorentin Chary } 26373e230dd2SCorentin Chary 26383e230dd2SCorentin Chary return 0; 26393e230dd2SCorentin Chary } 26403e230dd2SCorentin Chary 2641999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2642999342a0SCorentin Chary { 2643999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2644999342a0SCorentin Chary 2645999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2646999342a0SCorentin Chary } 2647999342a0SCorentin Chary 26487d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26497d964c9dSCorentin Chary { 26507d964c9dSCorentin Chary int i, j; 26517d964c9dSCorentin Chary 26527d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26537d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26547d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26557d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26567d964c9dSCorentin Chary 2657207f328aSCorentin Chary for (j = y; j <= h; j++) { 2658207f328aSCorentin Chary for (i = x; i <= w; i++) { 26597d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26607d964c9dSCorentin Chary } 26617d964c9dSCorentin Chary } 26627d964c9dSCorentin Chary } 26637d964c9dSCorentin Chary 26647d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26657d964c9dSCorentin Chary { 26667d964c9dSCorentin Chary VncState *vs; 26677d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 26687d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 26697d964c9dSCorentin Chary int has_dirty = 0; 26707d964c9dSCorentin Chary 26717d964c9dSCorentin Chary y = y / VNC_STAT_RECT * VNC_STAT_RECT; 26727d964c9dSCorentin Chary x = x / VNC_STAT_RECT * VNC_STAT_RECT; 26737d964c9dSCorentin Chary 26747d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2675bc2429b9SCorentin Chary int j; 26767d964c9dSCorentin Chary 26777d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 26787d964c9dSCorentin Chary if (vs->output.offset) { 26797d964c9dSCorentin Chary continue; 26807d964c9dSCorentin Chary } 26817d964c9dSCorentin Chary 26827d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 26837d964c9dSCorentin Chary continue; 26847d964c9dSCorentin Chary } 2685207f328aSCorentin Chary 26867d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 26877d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2688b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2689b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2690b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 26917d964c9dSCorentin Chary } 26927d964c9dSCorentin Chary has_dirty++; 26937d964c9dSCorentin Chary } 2694207f328aSCorentin Chary 26957d964c9dSCorentin Chary return has_dirty; 26967d964c9dSCorentin Chary } 26977d964c9dSCorentin Chary 26987d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2699999342a0SCorentin Chary { 27009f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 27019f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 2702999342a0SCorentin Chary int x, y; 2703999342a0SCorentin Chary struct timeval res; 27047d964c9dSCorentin Chary int has_dirty = 0; 2705999342a0SCorentin Chary 27069f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 27079f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2708999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2709999342a0SCorentin Chary 2710999342a0SCorentin Chary rect->updated = false; 2711999342a0SCorentin Chary } 2712999342a0SCorentin Chary } 2713999342a0SCorentin Chary 2714ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2715999342a0SCorentin Chary 2716999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 27177d964c9dSCorentin Chary return has_dirty; 2718999342a0SCorentin Chary } 2719999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2720999342a0SCorentin Chary 27219f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 27229f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2723999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2724999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2725999342a0SCorentin Chary struct timeval min, max; 2726999342a0SCorentin Chary 2727999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2728999342a0SCorentin Chary continue ; 2729999342a0SCorentin Chary } 2730999342a0SCorentin Chary 2731999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2732ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2733999342a0SCorentin Chary 2734999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2735999342a0SCorentin Chary rect->freq = 0; 27367d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2737999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2738999342a0SCorentin Chary continue ; 2739999342a0SCorentin Chary } 2740999342a0SCorentin Chary 2741999342a0SCorentin Chary min = rect->times[rect->idx]; 2742999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2743ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2744999342a0SCorentin Chary 2745999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2746999342a0SCorentin Chary rect->freq /= count; 2747999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2748999342a0SCorentin Chary } 2749999342a0SCorentin Chary } 27507d964c9dSCorentin Chary return has_dirty; 2751999342a0SCorentin Chary } 2752999342a0SCorentin Chary 2753999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2754999342a0SCorentin Chary { 2755999342a0SCorentin Chary int i, j; 2756999342a0SCorentin Chary double total = 0; 2757999342a0SCorentin Chary int num = 0; 2758999342a0SCorentin Chary 2759999342a0SCorentin Chary x = (x / VNC_STAT_RECT) * VNC_STAT_RECT; 2760999342a0SCorentin Chary y = (y / VNC_STAT_RECT) * VNC_STAT_RECT; 2761999342a0SCorentin Chary 2762999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2763999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2764999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2765999342a0SCorentin Chary num++; 2766999342a0SCorentin Chary } 2767999342a0SCorentin Chary } 2768999342a0SCorentin Chary 2769999342a0SCorentin Chary if (num) { 2770999342a0SCorentin Chary return total / num; 2771999342a0SCorentin Chary } else { 2772999342a0SCorentin Chary return 0; 2773999342a0SCorentin Chary } 2774999342a0SCorentin Chary } 2775999342a0SCorentin Chary 2776999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2777999342a0SCorentin Chary { 2778999342a0SCorentin Chary VncRectStat *rect; 2779999342a0SCorentin Chary 2780999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2781999342a0SCorentin Chary if (rect->updated) { 2782999342a0SCorentin Chary return ; 2783999342a0SCorentin Chary } 2784999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2785999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2786999342a0SCorentin Chary rect->updated = true; 2787999342a0SCorentin Chary } 2788999342a0SCorentin Chary 27893e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 27903e230dd2SCorentin Chary { 2791bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2792bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2793bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2794bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2795eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 279612b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 27973e230dd2SCorentin Chary VncState *vs; 27983e230dd2SCorentin Chary int has_dirty = 0; 27999f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 28003e230dd2SCorentin Chary 280180e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2802999342a0SCorentin Chary 280380e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2804999342a0SCorentin Chary gettimeofday(&tv, NULL); 28057d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 280680e0c8c3SCorentin Chary } 2807999342a0SCorentin Chary 28083e230dd2SCorentin Chary /* 28093e230dd2SCorentin Chary * Walk through the guest dirty map. 28103e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 28113e230dd2SCorentin Chary * Update server dirty map. 28123e230dd2SCorentin Chary */ 2813bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2814eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2815eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2816bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2817bea60dd7SPeter Lieven server_stride); 28189f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 28199f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 28209f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 282112b316d4SPeter Lieven } else { 2822eb8934b0SGerd Hoffmann int guest_bpp = 2823eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 282412b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 282512b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2826eb8934b0SGerd Hoffmann guest_ll = pixman_image_get_width(vd->guest.fb) * ((guest_bpp + 7) / 8); 28279f64916dSGerd Hoffmann } 2828eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 282912b316d4SPeter Lieven 283012b316d4SPeter Lieven for (;;) { 28313e230dd2SCorentin Chary int x; 283212b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 283312b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 283412b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 283512b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 283612b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 283712b316d4SPeter Lieven /* no more dirty bits */ 283812b316d4SPeter Lieven break; 283912b316d4SPeter Lieven } 284012b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 284112b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 284212b316d4SPeter Lieven 284312b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 28443e230dd2SCorentin Chary 28459f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2846bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28479f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28489f64916dSGerd Hoffmann } else { 284912b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28509f64916dSGerd Hoffmann } 285112b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28523e230dd2SCorentin Chary 285312b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 285412b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2855bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 285612b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28573e230dd2SCorentin Chary continue; 2858b4c85ddcSPeter Lieven } 2859eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2860eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2861bea60dd7SPeter Lieven } 2862eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2863bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28643e230dd2SCorentin Chary continue; 2865b4c85ddcSPeter Lieven } 2866bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 286712b316d4SPeter Lieven if (!vd->non_adaptive) { 286812b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 286912b316d4SPeter Lieven y, &tv); 287012b316d4SPeter Lieven } 28713e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 287212b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 28733e230dd2SCorentin Chary } 28743e230dd2SCorentin Chary has_dirty++; 28753e230dd2SCorentin Chary } 287612b316d4SPeter Lieven 287712b316d4SPeter Lieven y++; 28783e230dd2SCorentin Chary } 28799f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 28803e230dd2SCorentin Chary return has_dirty; 28813e230dd2SCorentin Chary } 28823e230dd2SCorentin Chary 28830f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 28843e230dd2SCorentin Chary { 28850f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 28863e230dd2SCorentin Chary VncState *vs, *vn; 28873e230dd2SCorentin Chary int has_dirty, rects = 0; 28883e230dd2SCorentin Chary 28899d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 28909d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 28919d6b2070SChenLiang return; 28929d6b2070SChenLiang } 28939d6b2070SChenLiang 28941d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 28953e230dd2SCorentin Chary 2896bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 28970f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2898bd023f95SCorentin Chary return; 2899bd023f95SCorentin Chary } 2900bd023f95SCorentin Chary 29013e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2902bd023f95SCorentin Chary vnc_unlock_display(vd); 29033e230dd2SCorentin Chary 29043e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 290538ee14f4SGerd Hoffmann rects += vnc_update_client(vs, has_dirty, false); 29063e230dd2SCorentin Chary /* vs might be free()ed here */ 29073e230dd2SCorentin Chary } 2908bd023f95SCorentin Chary 29093e230dd2SCorentin Chary if (has_dirty && rects) { 29100f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 29110f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 29120f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 29130f7b2864SGerd Hoffmann } 29143e230dd2SCorentin Chary } else { 29150f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 29160f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 29170f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 29183e230dd2SCorentin Chary } 29193e230dd2SCorentin Chary } 29203e230dd2SCorentin Chary } 29213e230dd2SCorentin Chary 292204d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 29232c8cf549SMichael Tokarev bool skipauth, bool websocket) 29243e230dd2SCorentin Chary { 2925fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 29267d964c9dSCorentin Chary int i; 29277d964c9dSCorentin Chary 292804d2529dSDaniel P. Berrange vs->sioc = sioc; 292904d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 293004d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 293104d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2932d616ccc5SGerd Hoffmann vs->vd = vd; 29337e7e2ebcSDaniel P. Berrange 293404d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 293504d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 293604d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2937543b9580SGerd Hoffmann 293804d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 293904d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 294004d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2941543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 294204d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2943543b9580SGerd Hoffmann #endif 2944543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 294504d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2946543b9580SGerd Hoffmann #endif 294704d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 294804d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 294904d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 295004d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2951543b9580SGerd Hoffmann 29527e7e2ebcSDaniel P. Berrange if (skipauth) { 29537e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29547e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29557e7e2ebcSDaniel P. Berrange } else { 2956f9148c8aSDaniel P. Berrange if (websocket) { 2957f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2958f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2959f9148c8aSDaniel P. Berrange } else { 29607e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29617e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29627e7e2ebcSDaniel P. Berrange } 2963f9148c8aSDaniel P. Berrange } 296404d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 296504d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 29667e7e2ebcSDaniel P. Berrange 29677267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 29687d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 2969fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 29707d964c9dSCorentin Chary } 29713e230dd2SCorentin Chary 297204d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 29730f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 297404d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 29757536ee4bSTim Hardeck if (websocket) { 29767536ee4bSTim Hardeck vs->websocket = 1; 2977f9148c8aSDaniel P. Berrange if (vd->ws_tls) { 297804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 297904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 29803e305e4aSDaniel P. Berrange } else { 298104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 298204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 29830057a0d5STim Hardeck } 298404d2529dSDaniel P. Berrange } else { 298504d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 298604d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 29877536ee4bSTim Hardeck } 29883e230dd2SCorentin Chary 29893e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2990fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 29918cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 29923e230dd2SCorentin Chary 29938e9b0d24SDaniel P. Berrange if (!vs->websocket) { 29947536ee4bSTim Hardeck vnc_init_state(vs); 29957536ee4bSTim Hardeck } 2996e5f34cddSGerd Hoffmann 2997e5f34cddSGerd Hoffmann if (vd->num_connecting > vd->connections_limit) { 2998e5f34cddSGerd Hoffmann QTAILQ_FOREACH(vs, &vd->clients, next) { 2999e5f34cddSGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 3000e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 3001e5f34cddSGerd Hoffmann return; 3002e5f34cddSGerd Hoffmann } 3003e5f34cddSGerd Hoffmann } 3004e5f34cddSGerd Hoffmann } 30057536ee4bSTim Hardeck } 30067536ee4bSTim Hardeck 30077536ee4bSTim Hardeck void vnc_init_state(VncState *vs) 30087536ee4bSTim Hardeck { 30096fd8e79aSTim Hardeck vs->initialized = true; 30107536ee4bSTim Hardeck VncDisplay *vd = vs->vd; 3011c7628bffSGerd Hoffmann bool first_client = QTAILQ_EMPTY(&vd->clients); 30127536ee4bSTim Hardeck 30133e230dd2SCorentin Chary vs->last_x = -1; 30143e230dd2SCorentin Chary vs->last_y = -1; 30153e230dd2SCorentin Chary 30163e230dd2SCorentin Chary vs->as.freq = 44100; 30173e230dd2SCorentin Chary vs->as.nchannels = 2; 30183e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 30193e230dd2SCorentin Chary vs->as.endianness = 0; 30203e230dd2SCorentin Chary 3021bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 3022175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 3023bd023f95SCorentin Chary 3024e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 3025c7628bffSGerd Hoffmann if (first_client) { 3026c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 3027c7628bffSGerd Hoffmann } 30283e230dd2SCorentin Chary 30291d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 30303e230dd2SCorentin Chary 30313e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 30323e230dd2SCorentin Chary vnc_flush(vs); 30333e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 30343e230dd2SCorentin Chary reset_keys(vs); 30353e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 30363e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 30373e230dd2SCorentin Chary 30383e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 30393e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 30403e230dd2SCorentin Chary 30413e230dd2SCorentin Chary /* vs might be free()ed here */ 30423e230dd2SCorentin Chary } 30433e230dd2SCorentin Chary 304404d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 304504d2529dSDaniel P. Berrange GIOCondition condition, 304604d2529dSDaniel P. Berrange void *opaque) 30473e230dd2SCorentin Chary { 30483e230dd2SCorentin Chary VncDisplay *vs = opaque; 304904d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 305004d2529dSDaniel P. Berrange Error *err = NULL; 30513e230dd2SCorentin Chary 30523e230dd2SCorentin Chary /* Catch-up */ 30531d0d59feSGerd Hoffmann graphic_hw_update(vs->dcl.con); 305404d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 305504d2529dSDaniel P. Berrange if (sioc != NULL) { 305604d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 305704d2529dSDaniel P. Berrange vnc_connect(vs, sioc, false, 305804d2529dSDaniel P. Berrange ioc != QIO_CHANNEL(vs->lsock)); 305904d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30608e9b0d24SDaniel P. Berrange } else { 306104d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 306204d2529dSDaniel P. Berrange error_free(err); 30637536ee4bSTim Hardeck } 30643e230dd2SCorentin Chary 306504d2529dSDaniel P. Berrange return TRUE; 30667536ee4bSTim Hardeck } 30677536ee4bSTim Hardeck 30687c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 30697c20b4a3SGerd Hoffmann .dpy_name = "vnc", 30700f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 30717c20b4a3SGerd Hoffmann .dpy_gfx_copy = vnc_dpy_copy, 30727c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3073c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 307434da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 30757c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 30767c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 30777c20b4a3SGerd Hoffmann }; 30787c20b4a3SGerd Hoffmann 307914f7143eSGerd Hoffmann void vnc_display_init(const char *id) 30803e230dd2SCorentin Chary { 30814db14629SGerd Hoffmann VncDisplay *vs; 30824db14629SGerd Hoffmann 30834db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 30844db14629SGerd Hoffmann return; 30854db14629SGerd Hoffmann } 30864db14629SGerd Hoffmann vs = g_malloc0(sizeof(*vs)); 30873e230dd2SCorentin Chary 308814f7143eSGerd Hoffmann vs->id = strdup(id); 3089d616ccc5SGerd Hoffmann QTAILQ_INSERT_TAIL(&vnc_displays, vs, next); 30903e230dd2SCorentin Chary 30913e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 30923c9405a0SGerd Hoffmann vs->expires = TIME_MAX; 30933e230dd2SCorentin Chary 309440066175SGerd Hoffmann if (keyboard_layout) { 309540066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 30963e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 309740066175SGerd Hoffmann } else { 30983e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 309940066175SGerd Hoffmann } 31003e230dd2SCorentin Chary 31013e230dd2SCorentin Chary if (!vs->kbd_layout) 31023e230dd2SCorentin Chary exit(1); 31033e230dd2SCorentin Chary 3104bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 3105bd023f95SCorentin Chary vnc_start_worker_thread(); 3106bd023f95SCorentin Chary 310721ef45d7SGerd Hoffmann vs->dcl.ops = &dcl_ops; 31085209089fSGerd Hoffmann register_displaychangelistener(&vs->dcl); 31093e230dd2SCorentin Chary } 31103e230dd2SCorentin Chary 31113e230dd2SCorentin Chary 311214f7143eSGerd Hoffmann static void vnc_display_close(VncDisplay *vs) 31133e230dd2SCorentin Chary { 31143e230dd2SCorentin Chary if (!vs) 31153e230dd2SCorentin Chary return; 3116bf7aa45eSGerd Hoffmann vs->enabled = false; 3117bf7aa45eSGerd Hoffmann vs->is_unix = false; 311804d2529dSDaniel P. Berrange if (vs->lsock != NULL) { 311904d2529dSDaniel P. Berrange if (vs->lsock_tag) { 312004d2529dSDaniel P. Berrange g_source_remove(vs->lsock_tag); 312104d2529dSDaniel P. Berrange } 312204d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lsock)); 312304d2529dSDaniel P. Berrange vs->lsock = NULL; 31243e230dd2SCorentin Chary } 3125bf7aa45eSGerd Hoffmann vs->ws_enabled = false; 312604d2529dSDaniel P. Berrange if (vs->lwebsock != NULL) { 312704d2529dSDaniel P. Berrange if (vs->lwebsock_tag) { 312804d2529dSDaniel P. Berrange g_source_remove(vs->lwebsock_tag); 312904d2529dSDaniel P. Berrange } 313004d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lwebsock)); 313104d2529dSDaniel P. Berrange vs->lwebsock = NULL; 31327536ee4bSTim Hardeck } 31333e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 31343e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31353e305e4aSDaniel P. Berrange if (vs->tlscreds) { 31363e305e4aSDaniel P. Berrange object_unparent(OBJECT(vs->tlscreds)); 31373e305e4aSDaniel P. Berrange } 31383e305e4aSDaniel P. Berrange g_free(vs->tlsaclname); 31393e305e4aSDaniel P. Berrange vs->tlsaclname = NULL; 31403e230dd2SCorentin Chary } 31413e230dd2SCorentin Chary 314214f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31431cd20f8bSAnthony Liguori { 314414f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 31451cd20f8bSAnthony Liguori 31461cd20f8bSAnthony Liguori if (!vs) { 3147a6aa9d3eSLuiz Capitulino return -EINVAL; 31481cd20f8bSAnthony Liguori } 3149cf864569SGerd Hoffmann if (vs->auth == VNC_AUTH_NONE) { 3150cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 3151cf864569SGerd Hoffmann "password auth using '-vnc ${dpy},password'."); 3152cf864569SGerd Hoffmann return -EINVAL; 31531cd20f8bSAnthony Liguori } 31541cd20f8bSAnthony Liguori 31557267c094SAnthony Liguori g_free(vs->password); 31567267c094SAnthony Liguori vs->password = g_strdup(password); 3157a6aa9d3eSLuiz Capitulino 3158a6aa9d3eSLuiz Capitulino return 0; 31593e230dd2SCorentin Chary } 31603e230dd2SCorentin Chary 316114f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31623c9405a0SGerd Hoffmann { 316314f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 31643c9405a0SGerd Hoffmann 31651643f2b2SGerd Hoffmann if (!vs) { 31661643f2b2SGerd Hoffmann return -EINVAL; 31671643f2b2SGerd Hoffmann } 31681643f2b2SGerd Hoffmann 31693c9405a0SGerd Hoffmann vs->expires = expires; 31703c9405a0SGerd Hoffmann return 0; 31713c9405a0SGerd Hoffmann } 31723c9405a0SGerd Hoffmann 317314f7143eSGerd Hoffmann char *vnc_display_local_addr(const char *id) 31743e230dd2SCorentin Chary { 317514f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 317604d2529dSDaniel P. Berrange SocketAddress *addr; 317704d2529dSDaniel P. Berrange char *ret; 317804d2529dSDaniel P. Berrange Error *err = NULL; 31793e230dd2SCorentin Chary 31809e0ff75eSGerd Hoffmann assert(vs); 318104d2529dSDaniel P. Berrange 318204d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(vs->lsock, &err); 318304d2529dSDaniel P. Berrange if (!addr) { 318404d2529dSDaniel P. Berrange return NULL; 318504d2529dSDaniel P. Berrange } 318604d2529dSDaniel P. Berrange 318704d2529dSDaniel P. Berrange if (addr->type != SOCKET_ADDRESS_KIND_INET) { 318804d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 318904d2529dSDaniel P. Berrange return NULL; 319004d2529dSDaniel P. Berrange } 319104d2529dSDaniel P. Berrange ret = g_strdup_printf("%s;%s", addr->u.inet->host, addr->u.inet->port); 319204d2529dSDaniel P. Berrange qapi_free_SocketAddress(addr); 319304d2529dSDaniel P. Berrange 319404d2529dSDaniel P. Berrange return ret; 31953e230dd2SCorentin Chary } 31963e230dd2SCorentin Chary 31974db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 31984db14629SGerd Hoffmann .name = "vnc", 31994db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 32004db14629SGerd Hoffmann .implied_opt_name = "vnc", 32014db14629SGerd Hoffmann .desc = { 32024db14629SGerd Hoffmann { 32034db14629SGerd Hoffmann .name = "vnc", 32044db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32054db14629SGerd Hoffmann },{ 32064db14629SGerd Hoffmann .name = "websocket", 32074db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32084db14629SGerd Hoffmann },{ 32093e305e4aSDaniel P. Berrange .name = "tls-creds", 32103e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 32113e305e4aSDaniel P. Berrange },{ 32123e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32134db14629SGerd Hoffmann .name = "x509", 32144db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32154db14629SGerd Hoffmann },{ 32164db14629SGerd Hoffmann .name = "share", 32174db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32184db14629SGerd Hoffmann },{ 32191d0d59feSGerd Hoffmann .name = "display", 32201d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 32211d0d59feSGerd Hoffmann },{ 32221d0d59feSGerd Hoffmann .name = "head", 32231d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32241d0d59feSGerd Hoffmann },{ 3225e5f34cddSGerd Hoffmann .name = "connections", 3226e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3227e5f34cddSGerd Hoffmann },{ 322888428b7aSGonglei .name = "to", 322988428b7aSGonglei .type = QEMU_OPT_NUMBER, 323088428b7aSGonglei },{ 323188428b7aSGonglei .name = "ipv4", 323288428b7aSGonglei .type = QEMU_OPT_BOOL, 323388428b7aSGonglei },{ 323488428b7aSGonglei .name = "ipv6", 323588428b7aSGonglei .type = QEMU_OPT_BOOL, 323688428b7aSGonglei },{ 32374db14629SGerd Hoffmann .name = "password", 32384db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32394db14629SGerd Hoffmann },{ 32404db14629SGerd Hoffmann .name = "reverse", 32414db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32424db14629SGerd Hoffmann },{ 32434db14629SGerd Hoffmann .name = "lock-key-sync", 32444db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32454db14629SGerd Hoffmann },{ 32464db14629SGerd Hoffmann .name = "sasl", 32474db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32484db14629SGerd Hoffmann },{ 32493e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32504db14629SGerd Hoffmann .name = "tls", 32514db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32524db14629SGerd Hoffmann },{ 32533e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32544db14629SGerd Hoffmann .name = "x509verify", 32558c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32564db14629SGerd Hoffmann },{ 32574db14629SGerd Hoffmann .name = "acl", 32584db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32594db14629SGerd Hoffmann },{ 32604db14629SGerd Hoffmann .name = "lossy", 32614db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32624db14629SGerd Hoffmann },{ 32634db14629SGerd Hoffmann .name = "non-adaptive", 32644db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32654db14629SGerd Hoffmann }, 32664db14629SGerd Hoffmann { /* end of list */ } 32674db14629SGerd Hoffmann }, 32684db14629SGerd Hoffmann }; 32694db14629SGerd Hoffmann 32700dd72e15SDaniel P. Berrange 32713e305e4aSDaniel P. Berrange static int 32720dd72e15SDaniel P. Berrange vnc_display_setup_auth(VncDisplay *vs, 32730dd72e15SDaniel P. Berrange bool password, 32740dd72e15SDaniel P. Berrange bool sasl, 32753e305e4aSDaniel P. Berrange bool websocket, 32763e305e4aSDaniel P. Berrange Error **errp) 32770dd72e15SDaniel P. Berrange { 32780dd72e15SDaniel P. Berrange /* 32790dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 32800dd72e15SDaniel P. Berrange * 32810dd72e15SDaniel P. Berrange * 1. none 32820dd72e15SDaniel P. Berrange * 2. vnc 32830dd72e15SDaniel P. Berrange * 3. sasl 32840dd72e15SDaniel P. Berrange * 32850dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 32860dd72e15SDaniel P. Berrange * 32870dd72e15SDaniel P. Berrange * 1. clear 32880dd72e15SDaniel P. Berrange * 2. tls 32890dd72e15SDaniel P. Berrange * 32900dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 32910dd72e15SDaniel P. Berrange * 32920dd72e15SDaniel P. Berrange * 1. anon 32930dd72e15SDaniel P. Berrange * 2. x509 32940dd72e15SDaniel P. Berrange * 32950dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 32960dd72e15SDaniel P. Berrange * 32970dd72e15SDaniel P. Berrange * 1. clear + none 32980dd72e15SDaniel P. Berrange * 2. clear + vnc 32990dd72e15SDaniel P. Berrange * 3. clear + sasl 33000dd72e15SDaniel P. Berrange * 4. tls + anon + none 33010dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 33020dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 33030dd72e15SDaniel P. Berrange * 7. tls + x509 + none 33040dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 33050dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 33060dd72e15SDaniel P. Berrange * 33070dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 33080dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 33090dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 33100dd72e15SDaniel P. Berrange * sub-auth types. 3311f9148c8aSDaniel P. Berrange * 3312f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3313f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3314f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3315f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3316f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3317f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3318f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3319f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3320f9148c8aSDaniel P. Berrange * 3321f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3322f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3323f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33240dd72e15SDaniel P. Berrange */ 33250dd72e15SDaniel P. Berrange if (password) { 33263e305e4aSDaniel P. Berrange if (vs->tlscreds) { 33270dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3328f9148c8aSDaniel P. Berrange if (websocket) { 3329f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3330f9148c8aSDaniel P. Berrange } 33313e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 33323e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 33330dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 33340dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 33353e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 33363e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 33370dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 33380dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 33393e305e4aSDaniel P. Berrange } else { 33403e305e4aSDaniel P. Berrange error_setg(errp, 33413e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 33423e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 33433e305e4aSDaniel P. Berrange return -1; 33440dd72e15SDaniel P. Berrange } 33450dd72e15SDaniel P. Berrange } else { 33460dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 33470dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VNC; 33480dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 33490dd72e15SDaniel P. Berrange } 3350f9148c8aSDaniel P. Berrange if (websocket) { 3351f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_VNC; 3352f9148c8aSDaniel P. Berrange } else { 3353f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3354f9148c8aSDaniel P. Berrange } 33550dd72e15SDaniel P. Berrange } else if (sasl) { 33563e305e4aSDaniel P. Berrange if (vs->tlscreds) { 33570dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3358f9148c8aSDaniel P. Berrange if (websocket) { 3359f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3360f9148c8aSDaniel P. Berrange } 33613e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 33623e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 33630dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 33640dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 33653e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 33663e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 33670dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 33680dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 33693e305e4aSDaniel P. Berrange } else { 33703e305e4aSDaniel P. Berrange error_setg(errp, 33713e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 33723e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 33733e305e4aSDaniel P. Berrange return -1; 33740dd72e15SDaniel P. Berrange } 33750dd72e15SDaniel P. Berrange } else { 33760dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 33770dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_SASL; 33780dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 33790dd72e15SDaniel P. Berrange } 3380f9148c8aSDaniel P. Berrange if (websocket) { 3381f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_SASL; 3382f9148c8aSDaniel P. Berrange } else { 3383f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3384f9148c8aSDaniel P. Berrange } 33850dd72e15SDaniel P. Berrange } else { 33863e305e4aSDaniel P. Berrange if (vs->tlscreds) { 33870dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_VENCRYPT; 3388f9148c8aSDaniel P. Berrange if (websocket) { 3389f9148c8aSDaniel P. Berrange vs->ws_tls = true; 3390f9148c8aSDaniel P. Berrange } 33913e305e4aSDaniel P. Berrange if (object_dynamic_cast(OBJECT(vs->tlscreds), 33923e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509)) { 33930dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 33940dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 33953e305e4aSDaniel P. Berrange } else if (object_dynamic_cast(OBJECT(vs->tlscreds), 33963e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON)) { 33970dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 33980dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 33993e305e4aSDaniel P. Berrange } else { 34003e305e4aSDaniel P. Berrange error_setg(errp, 34013e305e4aSDaniel P. Berrange "Unsupported TLS cred type %s", 34023e305e4aSDaniel P. Berrange object_get_typename(OBJECT(vs->tlscreds))); 34033e305e4aSDaniel P. Berrange return -1; 34040dd72e15SDaniel P. Berrange } 34050dd72e15SDaniel P. Berrange } else { 34060dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 34070dd72e15SDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 34080dd72e15SDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 34090dd72e15SDaniel P. Berrange } 3410f9148c8aSDaniel P. Berrange if (websocket) { 3411f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_NONE; 3412f9148c8aSDaniel P. Berrange } else { 3413f9148c8aSDaniel P. Berrange vs->ws_auth = VNC_AUTH_INVALID; 3414f9148c8aSDaniel P. Berrange } 34150dd72e15SDaniel P. Berrange } 34163e305e4aSDaniel P. Berrange return 0; 34170dd72e15SDaniel P. Berrange } 34180dd72e15SDaniel P. Berrange 34193e305e4aSDaniel P. Berrange 34203e305e4aSDaniel P. Berrange /* 34213e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 34223e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 34233e305e4aSDaniel P. Berrange */ 34243e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 34253e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 34263e305e4aSDaniel P. Berrange bool x509verify, 34273e305e4aSDaniel P. Berrange const char *dir, 34283e305e4aSDaniel P. Berrange const char *id, 34293e305e4aSDaniel P. Berrange Error **errp) 34303e305e4aSDaniel P. Berrange { 34313e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 34323e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 34333e305e4aSDaniel P. Berrange Object *creds; 34343e305e4aSDaniel P. Berrange Error *err = NULL; 34353e305e4aSDaniel P. Berrange 34363e305e4aSDaniel P. Berrange if (x509) { 34373e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 34383e305e4aSDaniel P. Berrange parent, 34393e305e4aSDaniel P. Berrange credsid, 34403e305e4aSDaniel P. Berrange &err, 34413e305e4aSDaniel P. Berrange "endpoint", "server", 34423e305e4aSDaniel P. Berrange "dir", dir, 34433e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 34443e305e4aSDaniel P. Berrange NULL); 34453e305e4aSDaniel P. Berrange } else { 34463e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 34473e305e4aSDaniel P. Berrange parent, 34483e305e4aSDaniel P. Berrange credsid, 34493e305e4aSDaniel P. Berrange &err, 34503e305e4aSDaniel P. Berrange "endpoint", "server", 34513e305e4aSDaniel P. Berrange NULL); 34523e305e4aSDaniel P. Berrange } 34533e305e4aSDaniel P. Berrange 34543e305e4aSDaniel P. Berrange g_free(credsid); 34553e305e4aSDaniel P. Berrange 34563e305e4aSDaniel P. Berrange if (err) { 34573e305e4aSDaniel P. Berrange error_propagate(errp, err); 34583e305e4aSDaniel P. Berrange return NULL; 34593e305e4aSDaniel P. Berrange } 34603e305e4aSDaniel P. Berrange 34613e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34623e305e4aSDaniel P. Berrange } 34633e305e4aSDaniel P. Berrange 34643e305e4aSDaniel P. Berrange 34654db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 34663e230dd2SCorentin Chary { 346714f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 34684db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3469e0d03b8cSDaniel P. Berrange SocketAddress *saddr = NULL, *wsaddr = NULL; 3470e2a11d9dSGonglei const char *share, *device_id; 34711d0d59feSGerd Hoffmann QemuConsole *con; 3472a2c72de0SGonglei bool password = false; 3473a2c72de0SGonglei bool reverse = false; 3474e2a11d9dSGonglei const char *vnc; 3475e5560329SGerd Hoffmann char *h; 34763e305e4aSDaniel P. Berrange const char *credid; 3477a2c72de0SGonglei bool sasl = false; 3478d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 34793e230dd2SCorentin Chary int saslErr; 34803e230dd2SCorentin Chary #endif 34813e230dd2SCorentin Chary int acl = 0; 34823e230dd2SCorentin Chary int lock_key_sync = 1; 34833e230dd2SCorentin Chary 3484d616ccc5SGerd Hoffmann if (!vs) { 34852d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 34862d55f0e8SPaolo Bonzini return; 34872d55f0e8SPaolo Bonzini } 348814f7143eSGerd Hoffmann vnc_display_close(vs); 34894db14629SGerd Hoffmann 34904db14629SGerd Hoffmann if (!opts) { 34912d55f0e8SPaolo Bonzini return; 34924db14629SGerd Hoffmann } 3493e2a11d9dSGonglei vnc = qemu_opt_get(opts, "vnc"); 3494e2a11d9dSGonglei if (!vnc || strcmp(vnc, "none") == 0) { 34954db14629SGerd Hoffmann return; 34964db14629SGerd Hoffmann } 3497e2a11d9dSGonglei 3498e5560329SGerd Hoffmann h = strrchr(vnc, ':'); 3499e5560329SGerd Hoffmann if (h) { 3500274c3b52SJán Tomko size_t hlen = h - vnc; 3501274c3b52SJán Tomko 3502e0d03b8cSDaniel P. Berrange const char *websocket = qemu_opt_get(opts, "websocket"); 3503e0d03b8cSDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3504e0d03b8cSDaniel P. Berrange bool has_ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3505e0d03b8cSDaniel P. Berrange bool has_ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3506e0d03b8cSDaniel P. Berrange 3507e0d03b8cSDaniel P. Berrange saddr = g_new0(SocketAddress, 1); 3508e0d03b8cSDaniel P. Berrange if (websocket) { 3509e0d03b8cSDaniel P. Berrange if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3510e0d03b8cSDaniel P. Berrange error_setg(errp, 3511e0d03b8cSDaniel P. Berrange "SHA1 hash support is required for websockets"); 3512e5560329SGerd Hoffmann goto fail; 3513e2a11d9dSGonglei } 3514e5560329SGerd Hoffmann 3515e0d03b8cSDaniel P. Berrange wsaddr = g_new0(SocketAddress, 1); 3516e0d03b8cSDaniel P. Berrange vs->ws_enabled = true; 3517e5560329SGerd Hoffmann } 3518e0d03b8cSDaniel P. Berrange 3519e0d03b8cSDaniel P. Berrange if (strncmp(vnc, "unix:", 5) == 0) { 35202d32addaSEric Blake saddr->type = SOCKET_ADDRESS_KIND_UNIX; 35212d32addaSEric Blake saddr->u.q_unix = g_new0(UnixSocketAddress, 1); 35222d32addaSEric Blake saddr->u.q_unix->path = g_strdup(vnc + 5); 3523e0d03b8cSDaniel P. Berrange 3524e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 3525e0d03b8cSDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3526e0d03b8cSDaniel P. Berrange goto fail; 3527e5560329SGerd Hoffmann } 3528e0d03b8cSDaniel P. Berrange } else { 3529e0d03b8cSDaniel P. Berrange unsigned long long baseport; 35302d32addaSEric Blake saddr->type = SOCKET_ADDRESS_KIND_INET; 35312d32addaSEric Blake saddr->u.inet = g_new0(InetSocketAddress, 1); 3532e0d03b8cSDaniel P. Berrange if (vnc[0] == '[' && vnc[hlen - 1] == ']') { 35332d32addaSEric Blake saddr->u.inet->host = g_strndup(vnc + 1, hlen - 2); 3534e0d03b8cSDaniel P. Berrange } else { 35352d32addaSEric Blake saddr->u.inet->host = g_strndup(vnc, hlen); 3536e0d03b8cSDaniel P. Berrange } 3537e0d03b8cSDaniel P. Berrange if (parse_uint_full(h + 1, &baseport, 10) < 0) { 3538e0d03b8cSDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", h + 1); 3539e0d03b8cSDaniel P. Berrange goto fail; 3540e0d03b8cSDaniel P. Berrange } 3541e0d03b8cSDaniel P. Berrange if (baseport > 65535 || 3542e0d03b8cSDaniel P. Berrange baseport + 5900 > 65535) { 3543e0d03b8cSDaniel P. Berrange error_setg(errp, "port %s out of range", h + 1); 3544e0d03b8cSDaniel P. Berrange goto fail; 3545e0d03b8cSDaniel P. Berrange } 35462d32addaSEric Blake saddr->u.inet->port = g_strdup_printf( 3547e0d03b8cSDaniel P. Berrange "%d", (int)baseport + 5900); 3548e0d03b8cSDaniel P. Berrange 3549e0d03b8cSDaniel P. Berrange if (to) { 35502d32addaSEric Blake saddr->u.inet->has_to = true; 35514d77b1f2SYang Hongyang saddr->u.inet->to = to + 5900; 3552e0d03b8cSDaniel P. Berrange } 35532d32addaSEric Blake saddr->u.inet->ipv4 = saddr->u.inet->has_ipv4 = has_ipv4; 35542d32addaSEric Blake saddr->u.inet->ipv6 = saddr->u.inet->has_ipv6 = has_ipv6; 3555e0d03b8cSDaniel P. Berrange 3556e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 35572d32addaSEric Blake wsaddr->type = SOCKET_ADDRESS_KIND_INET; 35582d32addaSEric Blake wsaddr->u.inet = g_new0(InetSocketAddress, 1); 35592d32addaSEric Blake wsaddr->u.inet->host = g_strdup(saddr->u.inet->host); 35602d32addaSEric Blake wsaddr->u.inet->port = g_strdup(websocket); 3561e0d03b8cSDaniel P. Berrange 3562e0d03b8cSDaniel P. Berrange if (to) { 35632d32addaSEric Blake wsaddr->u.inet->has_to = true; 35642d32addaSEric Blake wsaddr->u.inet->to = to; 3565e0d03b8cSDaniel P. Berrange } 35662d32addaSEric Blake wsaddr->u.inet->ipv4 = wsaddr->u.inet->has_ipv4 = has_ipv4; 35672d32addaSEric Blake wsaddr->u.inet->ipv6 = wsaddr->u.inet->has_ipv6 = has_ipv6; 3568e0d03b8cSDaniel P. Berrange } 3569e0d03b8cSDaniel P. Berrange } 3570e0d03b8cSDaniel P. Berrange } else { 3571e0d03b8cSDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3572e0d03b8cSDaniel P. Berrange goto fail; 3573e5560329SGerd Hoffmann } 35743e230dd2SCorentin Chary 35754db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3576800567a6SDaniel P. Berrange if (password) { 3577800567a6SDaniel P. Berrange if (fips_get_state()) { 35782d55f0e8SPaolo Bonzini error_setg(errp, 35790f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 35800f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 35812d55f0e8SPaolo Bonzini "methods as an alternative"); 35821ce52c78SPaolo Bonzini goto fail; 35830f66998fSPaul Moore } 3584800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3585800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB)) { 3586800567a6SDaniel P. Berrange error_setg(errp, 3587800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3588800567a6SDaniel P. Berrange goto fail; 3589800567a6SDaniel P. Berrange } 3590800567a6SDaniel P. Berrange } 35914db14629SGerd Hoffmann 35924db14629SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 35934db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 35944db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3595d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3596d169f04bSDaniel P. Berrange if (sasl) { 3597d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3598d169f04bSDaniel P. Berrange goto fail; 3599d169f04bSDaniel P. Berrange } 3600d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 36013e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 36023e305e4aSDaniel P. Berrange if (credid) { 36033e305e4aSDaniel P. Berrange Object *creds; 36043e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 36053e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 36063e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 36073e305e4aSDaniel P. Berrange error_setg(errp, 36083e305e4aSDaniel P. Berrange "'credid' parameter is mutually exclusive with " 36093e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 36103e305e4aSDaniel P. Berrange goto fail; 36113e305e4aSDaniel P. Berrange } 36123e305e4aSDaniel P. Berrange 36133e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 36143e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 36153e305e4aSDaniel P. Berrange if (!creds) { 36163e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 36173e305e4aSDaniel P. Berrange credid); 36183e305e4aSDaniel P. Berrange goto fail; 36193e305e4aSDaniel P. Berrange } 36203e305e4aSDaniel P. Berrange vs->tlscreds = (QCryptoTLSCreds *) 36213e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 36223e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 36233e305e4aSDaniel P. Berrange if (!vs->tlscreds) { 36243e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 36253e305e4aSDaniel P. Berrange credid); 36263e305e4aSDaniel P. Berrange goto fail; 36273e305e4aSDaniel P. Berrange } 36283e305e4aSDaniel P. Berrange object_ref(OBJECT(vs->tlscreds)); 36293e305e4aSDaniel P. Berrange 36303e305e4aSDaniel P. Berrange if (vs->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 36313e305e4aSDaniel P. Berrange error_setg(errp, 36323e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 36333e305e4aSDaniel P. Berrange goto fail; 36343e305e4aSDaniel P. Berrange } 36353e305e4aSDaniel P. Berrange } else { 36363e305e4aSDaniel P. Berrange const char *path; 36373e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 36384db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 36393e305e4aSDaniel P. Berrange if (tls) { 36404db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 36413e305e4aSDaniel P. Berrange 36424db14629SGerd Hoffmann if (path) { 3643a2c72de0SGonglei x509 = true; 36443e305e4aSDaniel P. Berrange } else { 36453e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 36463e305e4aSDaniel P. Berrange if (path) { 36473e305e4aSDaniel P. Berrange x509 = true; 36483e305e4aSDaniel P. Berrange x509verify = true; 36493e305e4aSDaniel P. Berrange } 36503e305e4aSDaniel P. Berrange } 36513e305e4aSDaniel P. Berrange vs->tlscreds = vnc_display_create_creds(x509, 36523e305e4aSDaniel P. Berrange x509verify, 36533e305e4aSDaniel P. Berrange path, 36543e305e4aSDaniel P. Berrange vs->id, 36553e305e4aSDaniel P. Berrange errp); 36563e305e4aSDaniel P. Berrange if (!vs->tlscreds) { 36571ce52c78SPaolo Bonzini goto fail; 36583e230dd2SCorentin Chary } 36593e230dd2SCorentin Chary } 3660d169f04bSDaniel P. Berrange } 36614db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 36624db14629SGerd Hoffmann 36634db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 36644db14629SGerd Hoffmann if (share) { 36654db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 36668cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_IGNORE; 36674db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 36688cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 36694db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 36708cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 36718cf36489SGerd Hoffmann } else { 36722d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 36731ce52c78SPaolo Bonzini goto fail; 36748cf36489SGerd Hoffmann } 36754db14629SGerd Hoffmann } else { 36764db14629SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 36773e230dd2SCorentin Chary } 3678e5f34cddSGerd Hoffmann vs->connections_limit = qemu_opt_get_number(opts, "connections", 32); 36793e230dd2SCorentin Chary 36804db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 36814db14629SGerd Hoffmann vs->lossy = qemu_opt_get_bool(opts, "lossy", false); 36824db14629SGerd Hoffmann #endif 36834db14629SGerd Hoffmann vs->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3684e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3685e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3686e22492d3SPeter Lieven * calculations otherwise */ 3687e22492d3SPeter Lieven if (!vs->lossy) { 3688e22492d3SPeter Lieven vs->non_adaptive = true; 3689e22492d3SPeter Lieven } 3690e22492d3SPeter Lieven 36913e305e4aSDaniel P. Berrange if (acl) { 3692c8496408SGerd Hoffmann if (strcmp(vs->id, "default") == 0) { 36933e305e4aSDaniel P. Berrange vs->tlsaclname = g_strdup("vnc.x509dname"); 3694c8496408SGerd Hoffmann } else { 36953e305e4aSDaniel P. Berrange vs->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vs->id); 3696c8496408SGerd Hoffmann } 36973e305e4aSDaniel P. Berrange qemu_acl_init(vs->tlsaclname); 36983e230dd2SCorentin Chary } 36993e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 37003e230dd2SCorentin Chary if (acl && sasl) { 3701c8496408SGerd Hoffmann char *aclname; 3702c8496408SGerd Hoffmann 3703c8496408SGerd Hoffmann if (strcmp(vs->id, "default") == 0) { 3704c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3705c8496408SGerd Hoffmann } else { 3706c8496408SGerd Hoffmann aclname = g_strdup_printf("vnc.%s.username", vs->id); 3707c8496408SGerd Hoffmann } 3708c8496408SGerd Hoffmann vs->sasl.acl = qemu_acl_init(aclname); 3709c8496408SGerd Hoffmann g_free(aclname); 37103e230dd2SCorentin Chary } 37113e230dd2SCorentin Chary #endif 37123e230dd2SCorentin Chary 3713e0d03b8cSDaniel P. Berrange if (vnc_display_setup_auth(vs, password, sasl, vs->ws_enabled, errp) < 0) { 37143e305e4aSDaniel P. Berrange goto fail; 37153e305e4aSDaniel P. Berrange } 37163e230dd2SCorentin Chary 37173e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 37183e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 37192d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 37203e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 37211ce52c78SPaolo Bonzini goto fail; 37223e230dd2SCorentin Chary } 37233e230dd2SCorentin Chary #endif 37243e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 37253e230dd2SCorentin Chary 37261d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 37271d0d59feSGerd Hoffmann if (device_id) { 37281d0d59feSGerd Hoffmann DeviceState *dev; 37291d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 37301d0d59feSGerd Hoffmann 37311d0d59feSGerd Hoffmann dev = qdev_find_recursive(sysbus_get_default(), device_id); 37321d0d59feSGerd Hoffmann if (dev == NULL) { 3733f3cf80e8SMarkus Armbruster error_setg(errp, "Device '%s' not found", device_id); 37341d0d59feSGerd Hoffmann goto fail; 37351d0d59feSGerd Hoffmann } 37361d0d59feSGerd Hoffmann 37371d0d59feSGerd Hoffmann con = qemu_console_lookup_by_device(dev, head); 37381d0d59feSGerd Hoffmann if (con == NULL) { 37391d0d59feSGerd Hoffmann error_setg(errp, "Device %s is not bound to a QemuConsole", 37401d0d59feSGerd Hoffmann device_id); 37411d0d59feSGerd Hoffmann goto fail; 37421d0d59feSGerd Hoffmann } 37431d0d59feSGerd Hoffmann } else { 37441d0d59feSGerd Hoffmann con = NULL; 37451d0d59feSGerd Hoffmann } 37461d0d59feSGerd Hoffmann 37471d0d59feSGerd Hoffmann if (con != vs->dcl.con) { 37481d0d59feSGerd Hoffmann unregister_displaychangelistener(&vs->dcl); 37491d0d59feSGerd Hoffmann vs->dcl.con = con; 37501d0d59feSGerd Hoffmann register_displaychangelistener(&vs->dcl); 37511d0d59feSGerd Hoffmann } 37521d0d59feSGerd Hoffmann 37533e230dd2SCorentin Chary if (reverse) { 37543e230dd2SCorentin Chary /* connect to viewer */ 375504d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 375604d2529dSDaniel P. Berrange vs->lsock = NULL; 375704d2529dSDaniel P. Berrange vs->lwebsock = NULL; 3758e0d03b8cSDaniel P. Berrange if (vs->ws_enabled) { 3759e0d03b8cSDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 3760e0d03b8cSDaniel P. Berrange goto fail; 37613e230dd2SCorentin Chary } 376204d2529dSDaniel P. Berrange vs->is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; 376304d2529dSDaniel P. Berrange sioc = qio_channel_socket_new(); 376404d2529dSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr, errp) < 0) { 3765007fcd3eSPaolo Bonzini goto fail; 3766007fcd3eSPaolo Bonzini } 376704d2529dSDaniel P. Berrange vnc_connect(vs, sioc, false, false); 376804d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 37693e230dd2SCorentin Chary } else { 377004d2529dSDaniel P. Berrange vs->lsock = qio_channel_socket_new(); 377104d2529dSDaniel P. Berrange if (qio_channel_socket_listen_sync(vs->lsock, saddr, errp) < 0) { 37723d00ac1aSCole Robinson goto fail; 37733d00ac1aSCole Robinson } 37742d32addaSEric Blake vs->is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; 377504d2529dSDaniel P. Berrange vs->enabled = true; 377604d2529dSDaniel P. Berrange 3777bf7aa45eSGerd Hoffmann if (vs->ws_enabled) { 377804d2529dSDaniel P. Berrange vs->lwebsock = qio_channel_socket_new(); 377904d2529dSDaniel P. Berrange if (qio_channel_socket_listen_sync(vs->lwebsock, 378004d2529dSDaniel P. Berrange wsaddr, errp) < 0) { 378104d2529dSDaniel P. Berrange object_unref(OBJECT(vs->lsock)); 378204d2529dSDaniel P. Berrange vs->lsock = NULL; 37837536ee4bSTim Hardeck goto fail; 37847536ee4bSTim Hardeck } 37857536ee4bSTim Hardeck } 378604d2529dSDaniel P. Berrange 378704d2529dSDaniel P. Berrange vs->lsock_tag = qio_channel_add_watch( 378804d2529dSDaniel P. Berrange QIO_CHANNEL(vs->lsock), 378904d2529dSDaniel P. Berrange G_IO_IN, vnc_listen_io, vs, NULL); 3790bf7aa45eSGerd Hoffmann if (vs->ws_enabled) { 379104d2529dSDaniel P. Berrange vs->lwebsock_tag = qio_channel_add_watch( 379204d2529dSDaniel P. Berrange QIO_CHANNEL(vs->lwebsock), 379304d2529dSDaniel P. Berrange G_IO_IN, vnc_listen_io, vs, NULL); 37947536ee4bSTim Hardeck } 37953e230dd2SCorentin Chary } 3796e0d03b8cSDaniel P. Berrange 3797e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(saddr); 3798e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(wsaddr); 37992d55f0e8SPaolo Bonzini return; 38001ce52c78SPaolo Bonzini 38011ce52c78SPaolo Bonzini fail: 3802e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(saddr); 3803e0d03b8cSDaniel P. Berrange qapi_free_SocketAddress(wsaddr); 3804bf7aa45eSGerd Hoffmann vs->enabled = false; 3805bf7aa45eSGerd Hoffmann vs->ws_enabled = false; 38063e230dd2SCorentin Chary } 380713661089SDaniel P. Berrange 380814f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 380913661089SDaniel P. Berrange { 381014f7143eSGerd Hoffmann VncDisplay *vs = vnc_display_find(id); 381104d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 381213661089SDaniel P. Berrange 3813d616ccc5SGerd Hoffmann if (!vs) { 3814d616ccc5SGerd Hoffmann return; 3815d616ccc5SGerd Hoffmann } 381604d2529dSDaniel P. Berrange 381704d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 381804d2529dSDaniel P. Berrange if (sioc) { 381904d2529dSDaniel P. Berrange vnc_connect(vs, sioc, skipauth, false); 382004d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 382104d2529dSDaniel P. Berrange } 382213661089SDaniel P. Berrange } 38234db14629SGerd Hoffmann 38249634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 38252779672fSGonglei { 38262779672fSGonglei int i = 2; 38272779672fSGonglei char *id; 38282779672fSGonglei 38292779672fSGonglei id = g_strdup("default"); 38302779672fSGonglei while (qemu_opts_find(olist, id)) { 38312779672fSGonglei g_free(id); 38322779672fSGonglei id = g_strdup_printf("vnc%d", i++); 38332779672fSGonglei } 38342779672fSGonglei qemu_opts_set_id(opts, id); 38352779672fSGonglei } 38362779672fSGonglei 383770b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 38384db14629SGerd Hoffmann { 38394db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 384070b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 384181607cbfSGonglei const char *id; 38424db14629SGerd Hoffmann 384381607cbfSGonglei if (!opts) { 384481607cbfSGonglei return NULL; 384581607cbfSGonglei } 384681607cbfSGonglei 384781607cbfSGonglei id = qemu_opts_id(opts); 38484db14629SGerd Hoffmann if (!id) { 38494db14629SGerd Hoffmann /* auto-assign id if not present */ 38502779672fSGonglei vnc_auto_assign_id(olist, opts); 38519634f4e3SGerd Hoffmann } 38529634f4e3SGerd Hoffmann return opts; 38534db14629SGerd Hoffmann } 38544db14629SGerd Hoffmann 385528d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 38569634f4e3SGerd Hoffmann { 38579634f4e3SGerd Hoffmann Error *local_err = NULL; 38589634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 38599634f4e3SGerd Hoffmann 38609634f4e3SGerd Hoffmann assert(id); 38614db14629SGerd Hoffmann vnc_display_init(id); 38624db14629SGerd Hoffmann vnc_display_open(id, &local_err); 38634db14629SGerd Hoffmann if (local_err != NULL) { 3864bc119048SCole Robinson error_report("Failed to start VNC server: %s", 38654db14629SGerd Hoffmann error_get_pretty(local_err)); 38664db14629SGerd Hoffmann error_free(local_err); 38674db14629SGerd Hoffmann exit(1); 38684db14629SGerd Hoffmann } 38694db14629SGerd Hoffmann return 0; 38704db14629SGerd Hoffmann } 38714db14629SGerd Hoffmann 38724db14629SGerd Hoffmann static void vnc_register_config(void) 38734db14629SGerd Hoffmann { 38744db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 38754db14629SGerd Hoffmann } 38764db14629SGerd Hoffmann machine_init(vnc_register_config); 3877