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" 299c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 301de7afc9SPaolo Bonzini #include "qemu/sockets.h" 311de7afc9SPaolo Bonzini #include "qemu/timer.h" 321de7afc9SPaolo Bonzini #include "qemu/acl.h" 337b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 342b54aa87SLuiz Capitulino #include "qmp-commands.h" 351de7afc9SPaolo Bonzini #include "qemu/osdep.h" 363e230dd2SCorentin Chary 373e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_BASE 30 383e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 393e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_MAX 2000 40999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 41999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 423e230dd2SCorentin Chary 433e230dd2SCorentin Chary #include "vnc_keysym.h" 443e230dd2SCorentin Chary #include "d3des.h" 453e230dd2SCorentin Chary 463e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */ 473e230dd2SCorentin Chary 483e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 497bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs); 503e230dd2SCorentin Chary 518cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 528cf36489SGerd Hoffmann { 538cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 548cf36489SGerd Hoffmann static const char *mn[] = { 558cf36489SGerd Hoffmann [0] = "undefined", 568cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 578cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 588cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 598cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 608cf36489SGerd Hoffmann }; 618cf36489SGerd Hoffmann fprintf(stderr, "%s/%d: %s -> %s\n", __func__, 628cf36489SGerd Hoffmann vs->csock, mn[vs->share_mode], mn[mode]); 638cf36489SGerd Hoffmann #endif 648cf36489SGerd Hoffmann 658cf36489SGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { 668cf36489SGerd Hoffmann vs->vd->num_exclusive--; 678cf36489SGerd Hoffmann } 688cf36489SGerd Hoffmann vs->share_mode = mode; 698cf36489SGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { 708cf36489SGerd Hoffmann vs->vd->num_exclusive++; 718cf36489SGerd Hoffmann } 728cf36489SGerd Hoffmann } 738cf36489SGerd Hoffmann 743e230dd2SCorentin Chary static char *addr_to_string(const char *format, 753e230dd2SCorentin Chary struct sockaddr_storage *sa, 763e230dd2SCorentin Chary socklen_t salen) { 773e230dd2SCorentin Chary char *addr; 783e230dd2SCorentin Chary char host[NI_MAXHOST]; 793e230dd2SCorentin Chary char serv[NI_MAXSERV]; 803e230dd2SCorentin Chary int err; 813e230dd2SCorentin Chary size_t addrlen; 823e230dd2SCorentin Chary 833e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 843e230dd2SCorentin Chary host, sizeof(host), 853e230dd2SCorentin Chary serv, sizeof(serv), 863e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 873e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 883e230dd2SCorentin Chary err, gai_strerror(err)); 893e230dd2SCorentin Chary return NULL; 903e230dd2SCorentin Chary } 913e230dd2SCorentin Chary 923e230dd2SCorentin Chary /* Enough for the existing format + the 2 vars we're 933e230dd2SCorentin Chary * substituting in. */ 943e230dd2SCorentin Chary addrlen = strlen(format) + strlen(host) + strlen(serv); 957267c094SAnthony Liguori addr = g_malloc(addrlen + 1); 963e230dd2SCorentin Chary snprintf(addr, addrlen, format, host, serv); 973e230dd2SCorentin Chary addr[addrlen] = '\0'; 983e230dd2SCorentin Chary 993e230dd2SCorentin Chary return addr; 1003e230dd2SCorentin Chary } 1013e230dd2SCorentin Chary 1023e230dd2SCorentin Chary 1033e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) { 1043e230dd2SCorentin Chary struct sockaddr_storage sa; 1053e230dd2SCorentin Chary socklen_t salen; 1063e230dd2SCorentin Chary 1073e230dd2SCorentin Chary salen = sizeof(sa); 1083e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) 1093e230dd2SCorentin Chary return NULL; 1103e230dd2SCorentin Chary 1113e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 1123e230dd2SCorentin Chary } 1133e230dd2SCorentin Chary 1143e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) { 1153e230dd2SCorentin Chary struct sockaddr_storage sa; 1163e230dd2SCorentin Chary socklen_t salen; 1173e230dd2SCorentin Chary 1183e230dd2SCorentin Chary salen = sizeof(sa); 1193e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) 1203e230dd2SCorentin Chary return NULL; 1213e230dd2SCorentin Chary 1223e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 1233e230dd2SCorentin Chary } 1243e230dd2SCorentin Chary 1253e230dd2SCorentin Chary static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa, 1263e230dd2SCorentin Chary socklen_t salen) 1273e230dd2SCorentin Chary { 1283e230dd2SCorentin Chary char host[NI_MAXHOST]; 1293e230dd2SCorentin Chary char serv[NI_MAXSERV]; 1303e230dd2SCorentin Chary int err; 1313e230dd2SCorentin Chary 1323e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 1333e230dd2SCorentin Chary host, sizeof(host), 1343e230dd2SCorentin Chary serv, sizeof(serv), 1353e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 1363e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 1373e230dd2SCorentin Chary err, gai_strerror(err)); 1383e230dd2SCorentin Chary return -1; 1393e230dd2SCorentin Chary } 1403e230dd2SCorentin Chary 1413e230dd2SCorentin Chary qdict_put(qdict, "host", qstring_from_str(host)); 1423e230dd2SCorentin Chary qdict_put(qdict, "service", qstring_from_str(serv)); 1433e230dd2SCorentin Chary qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family))); 1443e230dd2SCorentin Chary 1453e230dd2SCorentin Chary return 0; 1463e230dd2SCorentin Chary } 1473e230dd2SCorentin Chary 1483e230dd2SCorentin Chary static int vnc_server_addr_put(QDict *qdict, int fd) 1493e230dd2SCorentin Chary { 1503e230dd2SCorentin Chary struct sockaddr_storage sa; 1513e230dd2SCorentin Chary socklen_t salen; 1523e230dd2SCorentin Chary 1533e230dd2SCorentin Chary salen = sizeof(sa); 1543e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { 1553e230dd2SCorentin Chary return -1; 1563e230dd2SCorentin Chary } 1573e230dd2SCorentin Chary 1583e230dd2SCorentin Chary return put_addr_qdict(qdict, &sa, salen); 1593e230dd2SCorentin Chary } 1603e230dd2SCorentin Chary 1613e230dd2SCorentin Chary static int vnc_qdict_remote_addr(QDict *qdict, int fd) 1623e230dd2SCorentin Chary { 1633e230dd2SCorentin Chary struct sockaddr_storage sa; 1643e230dd2SCorentin Chary socklen_t salen; 1653e230dd2SCorentin Chary 1663e230dd2SCorentin Chary salen = sizeof(sa); 1673e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { 1683e230dd2SCorentin Chary return -1; 1693e230dd2SCorentin Chary } 1703e230dd2SCorentin Chary 1713e230dd2SCorentin Chary return put_addr_qdict(qdict, &sa, salen); 1723e230dd2SCorentin Chary } 1733e230dd2SCorentin Chary 1743e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1753e230dd2SCorentin Chary switch (vd->auth) { 1763e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1773e230dd2SCorentin Chary return "invalid"; 1783e230dd2SCorentin Chary case VNC_AUTH_NONE: 1793e230dd2SCorentin Chary return "none"; 1803e230dd2SCorentin Chary case VNC_AUTH_VNC: 1813e230dd2SCorentin Chary return "vnc"; 1823e230dd2SCorentin Chary case VNC_AUTH_RA2: 1833e230dd2SCorentin Chary return "ra2"; 1843e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1853e230dd2SCorentin Chary return "ra2ne"; 1863e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1873e230dd2SCorentin Chary return "tight"; 1883e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1893e230dd2SCorentin Chary return "ultra"; 1903e230dd2SCorentin Chary case VNC_AUTH_TLS: 1913e230dd2SCorentin Chary return "tls"; 1923e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1933e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 1943e230dd2SCorentin Chary switch (vd->subauth) { 1953e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1963e230dd2SCorentin Chary return "vencrypt+plain"; 1973e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 1983e230dd2SCorentin Chary return "vencrypt+tls+none"; 1993e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 2003e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 2013e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 2023e230dd2SCorentin Chary return "vencrypt+tls+plain"; 2033e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 2043e230dd2SCorentin Chary return "vencrypt+x509+none"; 2053e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2063e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2073e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2083e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2093e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2103e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2113e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2123e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2133e230dd2SCorentin Chary default: 2143e230dd2SCorentin Chary return "vencrypt"; 2153e230dd2SCorentin Chary } 2163e230dd2SCorentin Chary #else 2173e230dd2SCorentin Chary return "vencrypt"; 2183e230dd2SCorentin Chary #endif 2193e230dd2SCorentin Chary case VNC_AUTH_SASL: 2203e230dd2SCorentin Chary return "sasl"; 2213e230dd2SCorentin Chary } 2223e230dd2SCorentin Chary return "unknown"; 2233e230dd2SCorentin Chary } 2243e230dd2SCorentin Chary 2253e230dd2SCorentin Chary static int vnc_server_info_put(QDict *qdict) 2263e230dd2SCorentin Chary { 2273e230dd2SCorentin Chary if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) { 2283e230dd2SCorentin Chary return -1; 2293e230dd2SCorentin Chary } 2303e230dd2SCorentin Chary 2313e230dd2SCorentin Chary qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display))); 2323e230dd2SCorentin Chary return 0; 2333e230dd2SCorentin Chary } 2343e230dd2SCorentin Chary 2353e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2363e230dd2SCorentin Chary { 2372ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 2383e230dd2SCorentin Chary QDict *qdict; 2392ded6ad7SBlue Swirl #endif 2403e230dd2SCorentin Chary 2413e230dd2SCorentin Chary if (!client->info) { 2423e230dd2SCorentin Chary return; 2433e230dd2SCorentin Chary } 2443e230dd2SCorentin Chary 2452ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 2463e230dd2SCorentin Chary qdict = qobject_to_qdict(client->info); 2472ded6ad7SBlue Swirl #endif 2483e230dd2SCorentin Chary 2493e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 2503e230dd2SCorentin Chary if (client->tls.session && 2513e230dd2SCorentin Chary client->tls.dname) { 2523e230dd2SCorentin Chary qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname)); 2533e230dd2SCorentin Chary } 2543e230dd2SCorentin Chary #endif 2553e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2563e230dd2SCorentin Chary if (client->sasl.conn && 2573e230dd2SCorentin Chary client->sasl.username) { 2583e230dd2SCorentin Chary qdict_put(qdict, "sasl_username", 2593e230dd2SCorentin Chary qstring_from_str(client->sasl.username)); 2603e230dd2SCorentin Chary } 2613e230dd2SCorentin Chary #endif 2623e230dd2SCorentin Chary } 2633e230dd2SCorentin Chary 2643e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2653e230dd2SCorentin Chary { 2663e230dd2SCorentin Chary QDict *qdict; 2673e230dd2SCorentin Chary 2683e230dd2SCorentin Chary qdict = qdict_new(); 2693e230dd2SCorentin Chary if (vnc_qdict_remote_addr(qdict, client->csock) < 0) { 2703e230dd2SCorentin Chary QDECREF(qdict); 2713e230dd2SCorentin Chary /* XXX: how to report the error? */ 2723e230dd2SCorentin Chary return; 2733e230dd2SCorentin Chary } 2743e230dd2SCorentin Chary 2753e230dd2SCorentin Chary client->info = QOBJECT(qdict); 2763e230dd2SCorentin Chary } 2773e230dd2SCorentin Chary 2783e230dd2SCorentin Chary static void vnc_qmp_event(VncState *vs, MonitorEvent event) 2793e230dd2SCorentin Chary { 2803e230dd2SCorentin Chary QDict *server; 2813e230dd2SCorentin Chary QObject *data; 2823e230dd2SCorentin Chary 2833e230dd2SCorentin Chary if (!vs->info) { 2843e230dd2SCorentin Chary return; 2853e230dd2SCorentin Chary } 2863e230dd2SCorentin Chary 2873e230dd2SCorentin Chary server = qdict_new(); 2883e230dd2SCorentin Chary if (vnc_server_info_put(server) < 0) { 2893e230dd2SCorentin Chary QDECREF(server); 2903e230dd2SCorentin Chary return; 2913e230dd2SCorentin Chary } 2923e230dd2SCorentin Chary 2933e230dd2SCorentin Chary data = qobject_from_jsonf("{ 'client': %p, 'server': %p }", 2943e230dd2SCorentin Chary vs->info, QOBJECT(server)); 2953e230dd2SCorentin Chary 2963e230dd2SCorentin Chary monitor_protocol_event(event, data); 2973e230dd2SCorentin Chary 2983e230dd2SCorentin Chary qobject_incref(vs->info); 2993e230dd2SCorentin Chary qobject_decref(data); 3003e230dd2SCorentin Chary } 3013e230dd2SCorentin Chary 3022b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3033e230dd2SCorentin Chary { 3042b54aa87SLuiz Capitulino struct sockaddr_storage sa; 3052b54aa87SLuiz Capitulino socklen_t salen = sizeof(sa); 3062b54aa87SLuiz Capitulino char host[NI_MAXHOST]; 3072b54aa87SLuiz Capitulino char serv[NI_MAXSERV]; 3082b54aa87SLuiz Capitulino VncClientInfo *info; 3093e230dd2SCorentin Chary 3102b54aa87SLuiz Capitulino if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) { 3112b54aa87SLuiz Capitulino return NULL; 3122b54aa87SLuiz Capitulino } 3132b54aa87SLuiz Capitulino 3142b54aa87SLuiz Capitulino if (getnameinfo((struct sockaddr *)&sa, salen, 3152b54aa87SLuiz Capitulino host, sizeof(host), 3162b54aa87SLuiz Capitulino serv, sizeof(serv), 3172b54aa87SLuiz Capitulino NI_NUMERICHOST | NI_NUMERICSERV) < 0) { 3182b54aa87SLuiz Capitulino return NULL; 3192b54aa87SLuiz Capitulino } 3202b54aa87SLuiz Capitulino 3212b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 3222b54aa87SLuiz Capitulino info->host = g_strdup(host); 3232b54aa87SLuiz Capitulino info->service = g_strdup(serv); 3242b54aa87SLuiz Capitulino info->family = g_strdup(inet_strfamily(sa.ss_family)); 3253e230dd2SCorentin Chary 3263e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 3272b54aa87SLuiz Capitulino if (client->tls.session && client->tls.dname) { 3282b54aa87SLuiz Capitulino info->has_x509_dname = true; 3292b54aa87SLuiz Capitulino info->x509_dname = g_strdup(client->tls.dname); 3302b54aa87SLuiz Capitulino } 3313e230dd2SCorentin Chary #endif 3323e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3332b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3342b54aa87SLuiz Capitulino info->has_sasl_username = true; 3352b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3362b54aa87SLuiz Capitulino } 3373e230dd2SCorentin Chary #endif 3382b54aa87SLuiz Capitulino 3392b54aa87SLuiz Capitulino return info; 3403e230dd2SCorentin Chary } 3413e230dd2SCorentin Chary 3422b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3433e230dd2SCorentin Chary { 3442b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 3453e230dd2SCorentin Chary 3463e230dd2SCorentin Chary if (vnc_display == NULL || vnc_display->display == NULL) { 3472b54aa87SLuiz Capitulino info->enabled = false; 3483e230dd2SCorentin Chary } else { 3492b54aa87SLuiz Capitulino VncClientInfoList *cur_item = NULL; 3502b54aa87SLuiz Capitulino struct sockaddr_storage sa; 3512b54aa87SLuiz Capitulino socklen_t salen = sizeof(sa); 3522b54aa87SLuiz Capitulino char host[NI_MAXHOST]; 3532b54aa87SLuiz Capitulino char serv[NI_MAXSERV]; 3543e230dd2SCorentin Chary VncState *client; 3553e230dd2SCorentin Chary 3562b54aa87SLuiz Capitulino info->enabled = true; 3572b54aa87SLuiz Capitulino 3582b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3592b54aa87SLuiz Capitulino info->has_clients = true; 3602b54aa87SLuiz Capitulino 3613e230dd2SCorentin Chary QTAILQ_FOREACH(client, &vnc_display->clients, next) { 3622b54aa87SLuiz Capitulino VncClientInfoList *cinfo = g_malloc0(sizeof(*info)); 3632b54aa87SLuiz Capitulino cinfo->value = qmp_query_vnc_client(client); 3642b54aa87SLuiz Capitulino 3652b54aa87SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 3662b54aa87SLuiz Capitulino if (!cur_item) { 3672b54aa87SLuiz Capitulino info->clients = cur_item = cinfo; 3682b54aa87SLuiz Capitulino } else { 3692b54aa87SLuiz Capitulino cur_item->next = cinfo; 3702b54aa87SLuiz Capitulino cur_item = cinfo; 3713e230dd2SCorentin Chary } 3723e230dd2SCorentin Chary } 3733e230dd2SCorentin Chary 374417b0b88SPaolo Bonzini if (vnc_display->lsock == -1) { 375417b0b88SPaolo Bonzini return info; 376417b0b88SPaolo Bonzini } 377417b0b88SPaolo Bonzini 3782b54aa87SLuiz Capitulino if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa, 3792b54aa87SLuiz Capitulino &salen) == -1) { 3802b54aa87SLuiz Capitulino error_set(errp, QERR_UNDEFINED_ERROR); 3812b54aa87SLuiz Capitulino goto out_error; 3822b54aa87SLuiz Capitulino } 3833e230dd2SCorentin Chary 3842b54aa87SLuiz Capitulino if (getnameinfo((struct sockaddr *)&sa, salen, 3852b54aa87SLuiz Capitulino host, sizeof(host), 3862b54aa87SLuiz Capitulino serv, sizeof(serv), 3872b54aa87SLuiz Capitulino NI_NUMERICHOST | NI_NUMERICSERV) < 0) { 3882b54aa87SLuiz Capitulino error_set(errp, QERR_UNDEFINED_ERROR); 3892b54aa87SLuiz Capitulino goto out_error; 3903e230dd2SCorentin Chary } 3912b54aa87SLuiz Capitulino 3922b54aa87SLuiz Capitulino info->has_host = true; 3932b54aa87SLuiz Capitulino info->host = g_strdup(host); 3942b54aa87SLuiz Capitulino 3952b54aa87SLuiz Capitulino info->has_service = true; 3962b54aa87SLuiz Capitulino info->service = g_strdup(serv); 3972b54aa87SLuiz Capitulino 3982b54aa87SLuiz Capitulino info->has_family = true; 3992b54aa87SLuiz Capitulino info->family = g_strdup(inet_strfamily(sa.ss_family)); 4002b54aa87SLuiz Capitulino 4012b54aa87SLuiz Capitulino info->has_auth = true; 4022b54aa87SLuiz Capitulino info->auth = g_strdup(vnc_auth_name(vnc_display)); 4033e230dd2SCorentin Chary } 4042b54aa87SLuiz Capitulino 4052b54aa87SLuiz Capitulino return info; 4062b54aa87SLuiz Capitulino 4072b54aa87SLuiz Capitulino out_error: 4082b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4092b54aa87SLuiz Capitulino return NULL; 4103e230dd2SCorentin Chary } 4113e230dd2SCorentin Chary 4123e230dd2SCorentin Chary /* TODO 4133e230dd2SCorentin Chary 1) Get the queue working for IO. 4143e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 4153e230dd2SCorentin Chary and not totally invalidated 4163e230dd2SCorentin Chary 3) resolutions > 1024 4173e230dd2SCorentin Chary */ 4183e230dd2SCorentin Chary 4193e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty); 420bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty); 4213e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 4223e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd); 4233e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd); 4243e230dd2SCorentin Chary 4253e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 4263e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 4273e230dd2SCorentin Chary int x_position, int y_position, 4283e230dd2SCorentin Chary int w, int h); 4293e230dd2SCorentin Chary static void vnc_refresh(void *opaque); 4303e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 4313e230dd2SCorentin Chary 4327c20b4a3SGerd Hoffmann static void vnc_dpy_update(DisplayChangeListener *dcl, 4337c20b4a3SGerd Hoffmann DisplayState *ds, 4347c20b4a3SGerd Hoffmann int x, int y, int w, int h) 4353e230dd2SCorentin Chary { 4363e230dd2SCorentin Chary int i; 43721ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 4383e230dd2SCorentin Chary struct VncSurface *s = &vd->guest; 439*d39fa6d8SGerd Hoffmann int width = surface_width(vd->ds); 440*d39fa6d8SGerd Hoffmann int height = surface_height(vd->ds); 4413e230dd2SCorentin Chary 4423e230dd2SCorentin Chary h += y; 4433e230dd2SCorentin Chary 4443e230dd2SCorentin Chary /* round x down to ensure the loop only spans one 16-pixel block per, 4453e230dd2SCorentin Chary iteration. otherwise, if (x % 16) != 0, the last iteration may span 4463e230dd2SCorentin Chary two 16-pixel blocks but we only mark the first as dirty 4473e230dd2SCorentin Chary */ 4483e230dd2SCorentin Chary w += (x % 16); 4493e230dd2SCorentin Chary x -= (x % 16); 4503e230dd2SCorentin Chary 4519f64916dSGerd Hoffmann x = MIN(x, width); 4529f64916dSGerd Hoffmann y = MIN(y, height); 4539f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 4549f64916dSGerd Hoffmann h = MIN(h, height); 4553e230dd2SCorentin Chary 4563e230dd2SCorentin Chary for (; y < h; y++) 4573e230dd2SCorentin Chary for (i = 0; i < w; i += 16) 458bc2429b9SCorentin Chary set_bit((x + i) / 16, s->dirty[y]); 4593e230dd2SCorentin Chary } 4603e230dd2SCorentin Chary 4613e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 4623e230dd2SCorentin Chary int32_t encoding) 4633e230dd2SCorentin Chary { 4643e230dd2SCorentin Chary vnc_write_u16(vs, x); 4653e230dd2SCorentin Chary vnc_write_u16(vs, y); 4663e230dd2SCorentin Chary vnc_write_u16(vs, w); 4673e230dd2SCorentin Chary vnc_write_u16(vs, h); 4683e230dd2SCorentin Chary 4693e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 4703e230dd2SCorentin Chary } 4713e230dd2SCorentin Chary 4723e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len) 4733e230dd2SCorentin Chary { 4743e230dd2SCorentin Chary if ((buffer->capacity - buffer->offset) < len) { 4753e230dd2SCorentin Chary buffer->capacity += (len + 1024); 4767267c094SAnthony Liguori buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); 4773e230dd2SCorentin Chary if (buffer->buffer == NULL) { 4783e230dd2SCorentin Chary fprintf(stderr, "vnc: out of memory\n"); 4793e230dd2SCorentin Chary exit(1); 4803e230dd2SCorentin Chary } 4813e230dd2SCorentin Chary } 4823e230dd2SCorentin Chary } 4833e230dd2SCorentin Chary 48471a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer) 4853e230dd2SCorentin Chary { 4863e230dd2SCorentin Chary return buffer->offset == 0; 4873e230dd2SCorentin Chary } 4883e230dd2SCorentin Chary 4897536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer) 4903e230dd2SCorentin Chary { 4913e230dd2SCorentin Chary return buffer->buffer + buffer->offset; 4923e230dd2SCorentin Chary } 4933e230dd2SCorentin Chary 4943e230dd2SCorentin Chary void buffer_reset(Buffer *buffer) 4953e230dd2SCorentin Chary { 4963e230dd2SCorentin Chary buffer->offset = 0; 4973e230dd2SCorentin Chary } 4983e230dd2SCorentin Chary 4993e230dd2SCorentin Chary void buffer_free(Buffer *buffer) 5003e230dd2SCorentin Chary { 5017267c094SAnthony Liguori g_free(buffer->buffer); 5023e230dd2SCorentin Chary buffer->offset = 0; 5033e230dd2SCorentin Chary buffer->capacity = 0; 5043e230dd2SCorentin Chary buffer->buffer = NULL; 5053e230dd2SCorentin Chary } 5063e230dd2SCorentin Chary 5073e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len) 5083e230dd2SCorentin Chary { 5093e230dd2SCorentin Chary memcpy(buffer->buffer + buffer->offset, data, len); 5103e230dd2SCorentin Chary buffer->offset += len; 5113e230dd2SCorentin Chary } 5123e230dd2SCorentin Chary 51332ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len) 51432ed2680STim Hardeck { 51532ed2680STim Hardeck memmove(buf->buffer, buf->buffer + len, 51632ed2680STim Hardeck (buf->offset - len)); 51732ed2680STim Hardeck buf->offset -= len; 51832ed2680STim Hardeck } 51932ed2680STim Hardeck 5203e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 5213e230dd2SCorentin Chary { 522*d39fa6d8SGerd Hoffmann DisplaySurface *ds = vs->vd->ds; 5233e230dd2SCorentin Chary 5243e230dd2SCorentin Chary if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 5253e230dd2SCorentin Chary return; 5263e230dd2SCorentin Chary } 527*d39fa6d8SGerd Hoffmann if (vs->client_width == surface_width(ds) && 528*d39fa6d8SGerd Hoffmann vs->client_height == surface_height(ds)) { 5293e230dd2SCorentin Chary return; 5303e230dd2SCorentin Chary } 531*d39fa6d8SGerd Hoffmann vs->client_width = surface_width(ds); 532*d39fa6d8SGerd Hoffmann vs->client_height = surface_height(ds); 533bd023f95SCorentin Chary vnc_lock_output(vs); 5343e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 5353e230dd2SCorentin Chary vnc_write_u8(vs, 0); 5363e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 5373e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 5383e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 539bd023f95SCorentin Chary vnc_unlock_output(vs); 5403e230dd2SCorentin Chary vnc_flush(vs); 5413e230dd2SCorentin Chary } 5423e230dd2SCorentin Chary 543bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 544bd023f95SCorentin Chary { 545bd023f95SCorentin Chary VncState *vs; 546bd023f95SCorentin Chary 547bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 548bd023f95SCorentin Chary vnc_lock_output(vs); 549bd023f95SCorentin Chary vs->abort = true; 550bd023f95SCorentin Chary vnc_unlock_output(vs); 551bd023f95SCorentin Chary } 552bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 553bd023f95SCorentin Chary vnc_jobs_join(vs); 554bd023f95SCorentin Chary } 555bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 556bd023f95SCorentin Chary vnc_lock_output(vs); 557bd023f95SCorentin Chary vs->abort = false; 558bd023f95SCorentin Chary vnc_unlock_output(vs); 559bd023f95SCorentin Chary } 560bd023f95SCorentin Chary } 561bd023f95SCorentin Chary 5629f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 5639f64916dSGerd Hoffmann { 5649f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 5659f64916dSGerd Hoffmann } 5669f64916dSGerd Hoffmann 5679f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 5689f64916dSGerd Hoffmann { 5699f64916dSGerd Hoffmann uint8_t *ptr; 5709f64916dSGerd Hoffmann 5719f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 5729f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 5739f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 5749f64916dSGerd Hoffmann return ptr; 5759f64916dSGerd Hoffmann } 5769f64916dSGerd Hoffmann 577c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 578*d39fa6d8SGerd Hoffmann DisplayState *dontuse, 579c12aeb86SGerd Hoffmann DisplaySurface *surface) 5803e230dd2SCorentin Chary { 58121ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 5823e230dd2SCorentin Chary VncState *vs; 5833e230dd2SCorentin Chary 584bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 585bd023f95SCorentin Chary 5863e230dd2SCorentin Chary /* server surface */ 5879f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->server); 588*d39fa6d8SGerd Hoffmann vd->ds = surface; 5899f64916dSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 590*d39fa6d8SGerd Hoffmann surface_width(vd->ds), 591*d39fa6d8SGerd Hoffmann surface_height(vd->ds), 5929f64916dSGerd Hoffmann NULL, 0); 5933e230dd2SCorentin Chary 5943e230dd2SCorentin Chary /* guest surface */ 5959f64916dSGerd Hoffmann #if 0 /* FIXME */ 5963e230dd2SCorentin Chary if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel) 5973e230dd2SCorentin Chary console_color_init(ds); 5989f64916dSGerd Hoffmann #endif 5999f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 600*d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 601*d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 6023e230dd2SCorentin Chary memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty)); 6033e230dd2SCorentin Chary 6043e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 6053e230dd2SCorentin Chary vnc_colordepth(vs); 6063e230dd2SCorentin Chary vnc_desktop_resize(vs); 6073e230dd2SCorentin Chary if (vs->vd->cursor) { 6083e230dd2SCorentin Chary vnc_cursor_define(vs); 6093e230dd2SCorentin Chary } 6103e230dd2SCorentin Chary memset(vs->dirty, 0xFF, sizeof(vs->dirty)); 6113e230dd2SCorentin Chary } 6123e230dd2SCorentin Chary } 6133e230dd2SCorentin Chary 6143e230dd2SCorentin Chary /* fastest code */ 6159f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 6163e230dd2SCorentin Chary void *pixels, int size) 6173e230dd2SCorentin Chary { 6183e230dd2SCorentin Chary vnc_write(vs, pixels, size); 6193e230dd2SCorentin Chary } 6203e230dd2SCorentin Chary 6213e230dd2SCorentin Chary /* slowest but generic code. */ 6223e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 6233e230dd2SCorentin Chary { 6243e230dd2SCorentin Chary uint8_t r, g, b; 6253e230dd2SCorentin Chary 6269f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 6279f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 6289f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 6299f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 6309f64916dSGerd Hoffmann #else 6319f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 6329f64916dSGerd Hoffmann #endif 6339f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 6349f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 6359f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 6369f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 6373e230dd2SCorentin Chary case 1: 6383e230dd2SCorentin Chary buf[0] = v; 6393e230dd2SCorentin Chary break; 6403e230dd2SCorentin Chary case 2: 6419f64916dSGerd Hoffmann if (vs->client_be) { 6423e230dd2SCorentin Chary buf[0] = v >> 8; 6433e230dd2SCorentin Chary buf[1] = v; 6443e230dd2SCorentin Chary } else { 6453e230dd2SCorentin Chary buf[1] = v >> 8; 6463e230dd2SCorentin Chary buf[0] = v; 6473e230dd2SCorentin Chary } 6483e230dd2SCorentin Chary break; 6493e230dd2SCorentin Chary default: 6503e230dd2SCorentin Chary case 4: 6519f64916dSGerd Hoffmann if (vs->client_be) { 6523e230dd2SCorentin Chary buf[0] = v >> 24; 6533e230dd2SCorentin Chary buf[1] = v >> 16; 6543e230dd2SCorentin Chary buf[2] = v >> 8; 6553e230dd2SCorentin Chary buf[3] = v; 6563e230dd2SCorentin Chary } else { 6573e230dd2SCorentin Chary buf[3] = v >> 24; 6583e230dd2SCorentin Chary buf[2] = v >> 16; 6593e230dd2SCorentin Chary buf[1] = v >> 8; 6603e230dd2SCorentin Chary buf[0] = v; 6613e230dd2SCorentin Chary } 6623e230dd2SCorentin Chary break; 6633e230dd2SCorentin Chary } 6643e230dd2SCorentin Chary } 6653e230dd2SCorentin Chary 6669f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 6673e230dd2SCorentin Chary void *pixels1, int size) 6683e230dd2SCorentin Chary { 6693e230dd2SCorentin Chary uint8_t buf[4]; 6703e230dd2SCorentin Chary 6719f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 6723e230dd2SCorentin Chary uint32_t *pixels = pixels1; 6733e230dd2SCorentin Chary int n, i; 6743e230dd2SCorentin Chary n = size >> 2; 6753e230dd2SCorentin Chary for (i = 0; i < n; i++) { 6763e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6779f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 6783e230dd2SCorentin Chary } 6793e230dd2SCorentin Chary } 6803e230dd2SCorentin Chary } 6813e230dd2SCorentin Chary 6823e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6833e230dd2SCorentin Chary { 6843e230dd2SCorentin Chary int i; 6853e230dd2SCorentin Chary uint8_t *row; 6863e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 6873e230dd2SCorentin Chary 6889f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 6893e230dd2SCorentin Chary for (i = 0; i < h; i++) { 6909f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 6919f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 6923e230dd2SCorentin Chary } 6933e230dd2SCorentin Chary return 1; 6943e230dd2SCorentin Chary } 6953e230dd2SCorentin Chary 696bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6973e230dd2SCorentin Chary { 6983e230dd2SCorentin Chary int n = 0; 6993e230dd2SCorentin Chary 7003e230dd2SCorentin Chary switch(vs->vnc_encoding) { 7013e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 7023e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 7033e230dd2SCorentin Chary break; 7043e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 7053e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 7063e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 7073e230dd2SCorentin Chary break; 7083e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 7093e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 7103e230dd2SCorentin Chary break; 711efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 712efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 713efe556adSCorentin Chary break; 714148954faSCorentin Chary case VNC_ENCODING_ZRLE: 715148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 716148954faSCorentin Chary break; 717148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 718148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 719148954faSCorentin Chary break; 7203e230dd2SCorentin Chary default: 7213e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 7223e230dd2SCorentin Chary n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 7233e230dd2SCorentin Chary break; 7243e230dd2SCorentin Chary } 7253e230dd2SCorentin Chary return n; 7263e230dd2SCorentin Chary } 7273e230dd2SCorentin Chary 7283e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 7293e230dd2SCorentin Chary { 7303e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 731bd023f95SCorentin Chary vnc_lock_output(vs); 7323e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 7333e230dd2SCorentin Chary vnc_write_u8(vs, 0); 7343e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 7353e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 7363e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 7373e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 738bd023f95SCorentin Chary vnc_unlock_output(vs); 7393e230dd2SCorentin Chary vnc_flush(vs); 7403e230dd2SCorentin Chary } 7413e230dd2SCorentin Chary 7427c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl, 743*d39fa6d8SGerd Hoffmann DisplayState *dontuse, 7447c20b4a3SGerd Hoffmann int src_x, int src_y, 7457c20b4a3SGerd Hoffmann int dst_x, int dst_y, int w, int h) 7463e230dd2SCorentin Chary { 74721ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7483e230dd2SCorentin Chary VncState *vs, *vn; 7493e230dd2SCorentin Chary uint8_t *src_row; 7503e230dd2SCorentin Chary uint8_t *dst_row; 7519f64916dSGerd Hoffmann int i, x, y, pitch, inc, w_lim, s; 7523e230dd2SCorentin Chary int cmp_bytes; 7533e230dd2SCorentin Chary 7543e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 7553e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 7563e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7573e230dd2SCorentin Chary vs->force_update = 1; 758bd023f95SCorentin Chary vnc_update_client_sync(vs, 1); 7593e230dd2SCorentin Chary /* vs might be free()ed here */ 7603e230dd2SCorentin Chary } 7613e230dd2SCorentin Chary } 7623e230dd2SCorentin Chary 7633e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 7649f64916dSGerd Hoffmann pitch = vnc_server_fb_stride(vd); 7659f64916dSGerd Hoffmann src_row = vnc_server_fb_ptr(vd, src_x, src_y); 7669f64916dSGerd Hoffmann dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y); 7673e230dd2SCorentin Chary y = dst_y; 7683e230dd2SCorentin Chary inc = 1; 7693e230dd2SCorentin Chary if (dst_y > src_y) { 7703e230dd2SCorentin Chary /* copy backwards */ 7713e230dd2SCorentin Chary src_row += pitch * (h-1); 7723e230dd2SCorentin Chary dst_row += pitch * (h-1); 7733e230dd2SCorentin Chary pitch = -pitch; 7743e230dd2SCorentin Chary y = dst_y + h - 1; 7753e230dd2SCorentin Chary inc = -1; 7763e230dd2SCorentin Chary } 7773e230dd2SCorentin Chary w_lim = w - (16 - (dst_x % 16)); 7783e230dd2SCorentin Chary if (w_lim < 0) 7793e230dd2SCorentin Chary w_lim = w; 7803e230dd2SCorentin Chary else 7813e230dd2SCorentin Chary w_lim = w - (w_lim % 16); 7823e230dd2SCorentin Chary for (i = 0; i < h; i++) { 7833e230dd2SCorentin Chary for (x = 0; x <= w_lim; 7843e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 7853e230dd2SCorentin Chary if (x == w_lim) { 7863e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 7873e230dd2SCorentin Chary break; 7883e230dd2SCorentin Chary } else if (!x) { 7893e230dd2SCorentin Chary s = (16 - (dst_x % 16)); 7903e230dd2SCorentin Chary s = MIN(s, w_lim); 7913e230dd2SCorentin Chary } else { 7923e230dd2SCorentin Chary s = 16; 7933e230dd2SCorentin Chary } 7949f64916dSGerd Hoffmann cmp_bytes = s * VNC_SERVER_FB_BYTES; 7953e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 7963e230dd2SCorentin Chary continue; 7973e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 7983e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7993e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 800bc2429b9SCorentin Chary set_bit(((x + dst_x) / 16), vs->dirty[y]); 8013e230dd2SCorentin Chary } 8023e230dd2SCorentin Chary } 8033e230dd2SCorentin Chary } 8049f64916dSGerd Hoffmann src_row += pitch - w * VNC_SERVER_FB_BYTES; 8059f64916dSGerd Hoffmann dst_row += pitch - w * VNC_SERVER_FB_BYTES; 8063e230dd2SCorentin Chary y += inc; 8073e230dd2SCorentin Chary } 8083e230dd2SCorentin Chary 8093e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8103e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 8113e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 8123e230dd2SCorentin Chary } 8133e230dd2SCorentin Chary } 8143e230dd2SCorentin Chary } 8153e230dd2SCorentin Chary 8167c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 817*d39fa6d8SGerd Hoffmann DisplayState *dontuse, 8187c20b4a3SGerd Hoffmann int x, int y, int visible) 8193e230dd2SCorentin Chary { 8203e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 8213e230dd2SCorentin Chary } 8223e230dd2SCorentin Chary 8233e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 8243e230dd2SCorentin Chary { 8253e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 8263e230dd2SCorentin Chary int isize; 8273e230dd2SCorentin Chary 8283e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 829d01f9595SCorentin Chary vnc_lock_output(vs); 8303e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8313e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 8323e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 8333e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 8343e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 8359f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 8369f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 8373e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 838d01f9595SCorentin Chary vnc_unlock_output(vs); 8393e230dd2SCorentin Chary return 0; 8403e230dd2SCorentin Chary } 8413e230dd2SCorentin Chary return -1; 8423e230dd2SCorentin Chary } 8433e230dd2SCorentin Chary 8447c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 845*d39fa6d8SGerd Hoffmann DisplayState *dontuse, 8467c20b4a3SGerd Hoffmann QEMUCursor *c) 8473e230dd2SCorentin Chary { 8483e230dd2SCorentin Chary VncDisplay *vd = vnc_display; 8493e230dd2SCorentin Chary VncState *vs; 8503e230dd2SCorentin Chary 8513e230dd2SCorentin Chary cursor_put(vd->cursor); 8527267c094SAnthony Liguori g_free(vd->cursor_mask); 8533e230dd2SCorentin Chary 8543e230dd2SCorentin Chary vd->cursor = c; 8553e230dd2SCorentin Chary cursor_get(vd->cursor); 8563e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 8577267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 8583e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 8593e230dd2SCorentin Chary 8603e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8613e230dd2SCorentin Chary vnc_cursor_define(vs); 8623e230dd2SCorentin Chary } 8633e230dd2SCorentin Chary } 8643e230dd2SCorentin Chary 8653e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs, 8666c71a539SCorentin Chary int y, int last_x, int x, int height) 8673e230dd2SCorentin Chary { 8683e230dd2SCorentin Chary int h; 8693e230dd2SCorentin Chary 8706c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 8713e230dd2SCorentin Chary int tmp_x; 872bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 8733e230dd2SCorentin Chary break; 874bc2429b9SCorentin Chary } 875bc2429b9SCorentin Chary for (tmp_x = last_x; tmp_x < x; tmp_x++) { 876bc2429b9SCorentin Chary clear_bit(tmp_x, vs->dirty[y + h]); 877bc2429b9SCorentin Chary } 8783e230dd2SCorentin Chary } 8793e230dd2SCorentin Chary 8803e230dd2SCorentin Chary return h; 8813e230dd2SCorentin Chary } 8823e230dd2SCorentin Chary 883bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty) 884bd023f95SCorentin Chary { 885bd023f95SCorentin Chary int ret = vnc_update_client(vs, has_dirty); 886bd023f95SCorentin Chary vnc_jobs_join(vs); 887bd023f95SCorentin Chary return ret; 888bd023f95SCorentin Chary } 889bd023f95SCorentin Chary 8903e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty) 8913e230dd2SCorentin Chary { 8923e230dd2SCorentin Chary if (vs->need_update && vs->csock != -1) { 8933e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 894bd023f95SCorentin Chary VncJob *job; 8953e230dd2SCorentin Chary int y; 8963e230dd2SCorentin Chary int width, height; 897bd023f95SCorentin Chary int n = 0; 898bd023f95SCorentin Chary 8993e230dd2SCorentin Chary 9003e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 9013e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 9023e230dd2SCorentin Chary return 0; 9033e230dd2SCorentin Chary 9043e230dd2SCorentin Chary if (!has_dirty && !vs->audio_cap && !vs->force_update) 9053e230dd2SCorentin Chary return 0; 9063e230dd2SCorentin Chary 9073e230dd2SCorentin Chary /* 9083e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9093e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9103e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9113e230dd2SCorentin Chary * send them to the client. 9123e230dd2SCorentin Chary */ 913bd023f95SCorentin Chary job = vnc_job_new(vs); 9143e230dd2SCorentin Chary 9159f64916dSGerd Hoffmann width = MIN(pixman_image_get_width(vd->server), vs->client_width); 9169f64916dSGerd Hoffmann height = MIN(pixman_image_get_height(vd->server), vs->client_height); 9173e230dd2SCorentin Chary 9183e230dd2SCorentin Chary for (y = 0; y < height; y++) { 9193e230dd2SCorentin Chary int x; 9203e230dd2SCorentin Chary int last_x = -1; 9213e230dd2SCorentin Chary for (x = 0; x < width / 16; x++) { 922bc2429b9SCorentin Chary if (test_and_clear_bit(x, vs->dirty[y])) { 9233e230dd2SCorentin Chary if (last_x == -1) { 9243e230dd2SCorentin Chary last_x = x; 9253e230dd2SCorentin Chary } 9263e230dd2SCorentin Chary } else { 9273e230dd2SCorentin Chary if (last_x != -1) { 9286c71a539SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x, 9296c71a539SCorentin Chary height); 930bd023f95SCorentin Chary 931bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9323e230dd2SCorentin Chary (x - last_x) * 16, h); 9333e230dd2SCorentin Chary } 9343e230dd2SCorentin Chary last_x = -1; 9353e230dd2SCorentin Chary } 9363e230dd2SCorentin Chary } 9373e230dd2SCorentin Chary if (last_x != -1) { 9386c71a539SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x, height); 939bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9403e230dd2SCorentin Chary (x - last_x) * 16, h); 9413e230dd2SCorentin Chary } 9423e230dd2SCorentin Chary } 943bd023f95SCorentin Chary 944bd023f95SCorentin Chary vnc_job_push(job); 9453e230dd2SCorentin Chary vs->force_update = 0; 946bd023f95SCorentin Chary return n; 9473e230dd2SCorentin Chary } 9483e230dd2SCorentin Chary 9493e230dd2SCorentin Chary if (vs->csock == -1) 9503e230dd2SCorentin Chary vnc_disconnect_finish(vs); 9513e230dd2SCorentin Chary 9523e230dd2SCorentin Chary return 0; 9533e230dd2SCorentin Chary } 9543e230dd2SCorentin Chary 9553e230dd2SCorentin Chary /* audio */ 9563e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 9573e230dd2SCorentin Chary { 9583e230dd2SCorentin Chary VncState *vs = opaque; 9593e230dd2SCorentin Chary 9603e230dd2SCorentin Chary switch (cmd) { 9613e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 962bd023f95SCorentin Chary vnc_lock_output(vs); 9633e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9643e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9653e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 966bd023f95SCorentin Chary vnc_unlock_output(vs); 9673e230dd2SCorentin Chary vnc_flush(vs); 9683e230dd2SCorentin Chary break; 9693e230dd2SCorentin Chary 9703e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 971bd023f95SCorentin Chary vnc_lock_output(vs); 9723e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9733e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9743e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 975bd023f95SCorentin Chary vnc_unlock_output(vs); 9763e230dd2SCorentin Chary vnc_flush(vs); 9773e230dd2SCorentin Chary break; 9783e230dd2SCorentin Chary } 9793e230dd2SCorentin Chary } 9803e230dd2SCorentin Chary 9813e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 9823e230dd2SCorentin Chary { 9833e230dd2SCorentin Chary } 9843e230dd2SCorentin Chary 9853e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 9863e230dd2SCorentin Chary { 9873e230dd2SCorentin Chary VncState *vs = opaque; 9883e230dd2SCorentin Chary 989bd023f95SCorentin Chary vnc_lock_output(vs); 9903e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9913e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9923e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 9933e230dd2SCorentin Chary vnc_write_u32(vs, size); 9943e230dd2SCorentin Chary vnc_write(vs, buf, size); 995bd023f95SCorentin Chary vnc_unlock_output(vs); 9963e230dd2SCorentin Chary vnc_flush(vs); 9973e230dd2SCorentin Chary } 9983e230dd2SCorentin Chary 9993e230dd2SCorentin Chary static void audio_add(VncState *vs) 10003e230dd2SCorentin Chary { 10013e230dd2SCorentin Chary struct audio_capture_ops ops; 10023e230dd2SCorentin Chary 10033e230dd2SCorentin Chary if (vs->audio_cap) { 10043e230dd2SCorentin Chary monitor_printf(default_mon, "audio already running\n"); 10053e230dd2SCorentin Chary return; 10063e230dd2SCorentin Chary } 10073e230dd2SCorentin Chary 10083e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10093e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 10103e230dd2SCorentin Chary ops.capture = audio_capture; 10113e230dd2SCorentin Chary 10123e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 10133e230dd2SCorentin Chary if (!vs->audio_cap) { 10143e230dd2SCorentin Chary monitor_printf(default_mon, "Failed to add audio capture\n"); 10153e230dd2SCorentin Chary } 10163e230dd2SCorentin Chary } 10173e230dd2SCorentin Chary 10183e230dd2SCorentin Chary static void audio_del(VncState *vs) 10193e230dd2SCorentin Chary { 10203e230dd2SCorentin Chary if (vs->audio_cap) { 10213e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 10223e230dd2SCorentin Chary vs->audio_cap = NULL; 10233e230dd2SCorentin Chary } 10243e230dd2SCorentin Chary } 10253e230dd2SCorentin Chary 10263e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 10273e230dd2SCorentin Chary { 10283e230dd2SCorentin Chary if (vs->csock == -1) 10293e230dd2SCorentin Chary return; 10308cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 10313e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); 10323e230dd2SCorentin Chary closesocket(vs->csock); 10333e230dd2SCorentin Chary vs->csock = -1; 10343e230dd2SCorentin Chary } 10353e230dd2SCorentin Chary 10367536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 10373e230dd2SCorentin Chary { 10387d964c9dSCorentin Chary int i; 10397d964c9dSCorentin Chary 1040bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1041bd023f95SCorentin Chary 1042bd023f95SCorentin Chary vnc_lock_output(vs); 10433e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED); 10443e230dd2SCorentin Chary 10453e230dd2SCorentin Chary buffer_free(&vs->input); 10463e230dd2SCorentin Chary buffer_free(&vs->output); 10477536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 10487536ee4bSTim Hardeck buffer_free(&vs->ws_input); 10497536ee4bSTim Hardeck buffer_free(&vs->ws_output); 10507536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 10513e230dd2SCorentin Chary 10523e230dd2SCorentin Chary qobject_decref(vs->info); 10533e230dd2SCorentin Chary 10543e230dd2SCorentin Chary vnc_zlib_clear(vs); 10553e230dd2SCorentin Chary vnc_tight_clear(vs); 1056148954faSCorentin Chary vnc_zrle_clear(vs); 10573e230dd2SCorentin Chary 10583e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 10593e230dd2SCorentin Chary vnc_tls_client_cleanup(vs); 10603e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 10613e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 10623e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 10633e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 10643e230dd2SCorentin Chary audio_del(vs); 10657bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 10663e230dd2SCorentin Chary 10676fd8e79aSTim Hardeck if (vs->initialized) { 10683e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 10696fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 10706fd8e79aSTim Hardeck } 10713e230dd2SCorentin Chary 10723e230dd2SCorentin Chary if (QTAILQ_EMPTY(&vs->vd->clients)) { 107321ef45d7SGerd Hoffmann vs->vd->dcl.idle = 1; 10743e230dd2SCorentin Chary } 10753e230dd2SCorentin Chary 10763e230dd2SCorentin Chary vnc_remove_timer(vs->vd); 10773e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 10783e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1079bd023f95SCorentin Chary vnc_unlock_output(vs); 1080bd023f95SCorentin Chary 1081bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 10826fd8e79aSTim Hardeck if (vs->bh != NULL) { 1083175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 10846fd8e79aSTim Hardeck } 1085175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1086175b2a6eSCorentin Chary 10877d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 10887267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 10897d964c9dSCorentin Chary } 10907267c094SAnthony Liguori g_free(vs->lossy_rect); 10917267c094SAnthony Liguori g_free(vs); 10923e230dd2SCorentin Chary } 10933e230dd2SCorentin Chary 10943e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno) 10953e230dd2SCorentin Chary { 10963e230dd2SCorentin Chary if (ret == 0 || ret == -1) { 10973e230dd2SCorentin Chary if (ret == -1) { 10983e230dd2SCorentin Chary switch (last_errno) { 10993e230dd2SCorentin Chary case EINTR: 11003e230dd2SCorentin Chary case EAGAIN: 11013e230dd2SCorentin Chary #ifdef _WIN32 11023e230dd2SCorentin Chary case WSAEWOULDBLOCK: 11033e230dd2SCorentin Chary #endif 11043e230dd2SCorentin Chary return 0; 11053e230dd2SCorentin Chary default: 11063e230dd2SCorentin Chary break; 11073e230dd2SCorentin Chary } 11083e230dd2SCorentin Chary } 11093e230dd2SCorentin Chary 11103e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: ret %d, errno %d\n", 11113e230dd2SCorentin Chary ret, ret < 0 ? last_errno : 0); 11123e230dd2SCorentin Chary vnc_disconnect_start(vs); 11133e230dd2SCorentin Chary 11143e230dd2SCorentin Chary return 0; 11153e230dd2SCorentin Chary } 11163e230dd2SCorentin Chary return ret; 11173e230dd2SCorentin Chary } 11183e230dd2SCorentin Chary 11193e230dd2SCorentin Chary 11203e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 11213e230dd2SCorentin Chary { 11223e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 11233e230dd2SCorentin Chary vnc_disconnect_start(vs); 11243e230dd2SCorentin Chary } 11253e230dd2SCorentin Chary 11263e230dd2SCorentin Chary 11273e230dd2SCorentin Chary /* 11283e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 11293e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 11303e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 11313e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 11323e230dd2SCorentin Chary * 11333e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 11343e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 11353e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 11363e230dd2SCorentin Chary * is active 11373e230dd2SCorentin Chary * 11383e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11393e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 11403e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11413e230dd2SCorentin Chary */ 11423e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 11433e230dd2SCorentin Chary { 11443e230dd2SCorentin Chary long ret; 11453e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 11463e230dd2SCorentin Chary if (vs->tls.session) { 11473e230dd2SCorentin Chary ret = gnutls_write(vs->tls.session, data, datalen); 11483e230dd2SCorentin Chary if (ret < 0) { 11493e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 11503e230dd2SCorentin Chary errno = EAGAIN; 11513e230dd2SCorentin Chary else 11523e230dd2SCorentin Chary errno = EIO; 11533e230dd2SCorentin Chary ret = -1; 11543e230dd2SCorentin Chary } 11553e230dd2SCorentin Chary } else 11563e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 11573e230dd2SCorentin Chary ret = send(vs->csock, (const void *)data, datalen, 0); 11583e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 11593e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 11603e230dd2SCorentin Chary } 11613e230dd2SCorentin Chary 11623e230dd2SCorentin Chary 11633e230dd2SCorentin Chary /* 11643e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 11653e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 11663e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 11673e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 11683e230dd2SCorentin Chary * 11693e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11703e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 11713e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11723e230dd2SCorentin Chary */ 11733e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs) 11743e230dd2SCorentin Chary { 11753e230dd2SCorentin Chary long ret; 11763e230dd2SCorentin Chary 11773e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11783e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 11793e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 11803e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 11813e230dd2SCorentin Chary 11823e230dd2SCorentin Chary if (vs->sasl.conn && 11833e230dd2SCorentin Chary vs->sasl.runSSF && 11843e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 11853e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 11863e230dd2SCorentin Chary if (ret) 11873e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 11883e230dd2SCorentin Chary } else 11893e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11903e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 11913e230dd2SCorentin Chary if (!ret) 11923e230dd2SCorentin Chary return 0; 11933e230dd2SCorentin Chary 119432ed2680STim Hardeck buffer_advance(&vs->output, ret); 11953e230dd2SCorentin Chary 11963e230dd2SCorentin Chary if (vs->output.offset == 0) { 11973e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 11983e230dd2SCorentin Chary } 11993e230dd2SCorentin Chary 12003e230dd2SCorentin Chary return ret; 12013e230dd2SCorentin Chary } 12023e230dd2SCorentin Chary 12033e230dd2SCorentin Chary 12043e230dd2SCorentin Chary /* 12053e230dd2SCorentin Chary * First function called whenever there is data to be written to 12063e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12073e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12083e230dd2SCorentin Chary */ 1209bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque) 12103e230dd2SCorentin Chary { 12113e230dd2SCorentin Chary VncState *vs = opaque; 12123e230dd2SCorentin Chary 12133e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12143e230dd2SCorentin Chary if (vs->sasl.conn && 12153e230dd2SCorentin Chary vs->sasl.runSSF && 12163e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12173e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12183e230dd2SCorentin Chary } else 12193e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12207536ee4bSTim Hardeck { 12217536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12227536ee4bSTim Hardeck if (vs->encode_ws) { 12237536ee4bSTim Hardeck vnc_client_write_ws(vs); 12247536ee4bSTim Hardeck } else 12257536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 12267536ee4bSTim Hardeck { 12273e230dd2SCorentin Chary vnc_client_write_plain(vs); 12283e230dd2SCorentin Chary } 12297536ee4bSTim Hardeck } 12307536ee4bSTim Hardeck } 12313e230dd2SCorentin Chary 1232bd023f95SCorentin Chary void vnc_client_write(void *opaque) 1233bd023f95SCorentin Chary { 1234bd023f95SCorentin Chary VncState *vs = opaque; 1235bd023f95SCorentin Chary 1236bd023f95SCorentin Chary vnc_lock_output(vs); 12377536ee4bSTim Hardeck if (vs->output.offset 12387536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12397536ee4bSTim Hardeck || vs->ws_output.offset 12407536ee4bSTim Hardeck #endif 12417536ee4bSTim Hardeck ) { 1242bd023f95SCorentin Chary vnc_client_write_locked(opaque); 1243ac71103dSYoshiaki Tamura } else if (vs->csock != -1) { 1244bd023f95SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 1245bd023f95SCorentin Chary } 1246bd023f95SCorentin Chary vnc_unlock_output(vs); 1247bd023f95SCorentin Chary } 1248bd023f95SCorentin Chary 12493e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 12503e230dd2SCorentin Chary { 12513e230dd2SCorentin Chary vs->read_handler = func; 12523e230dd2SCorentin Chary vs->read_handler_expect = expecting; 12533e230dd2SCorentin Chary } 12543e230dd2SCorentin Chary 12553e230dd2SCorentin Chary 12563e230dd2SCorentin Chary /* 12573e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 12583e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 12593e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 12603e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12613e230dd2SCorentin Chary * 12623e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12633e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12643e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12653e230dd2SCorentin Chary * is active 12663e230dd2SCorentin Chary * 12673e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 12683e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12693e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12703e230dd2SCorentin Chary */ 12713e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 12723e230dd2SCorentin Chary { 12733e230dd2SCorentin Chary long ret; 12743e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 12753e230dd2SCorentin Chary if (vs->tls.session) { 12763e230dd2SCorentin Chary ret = gnutls_read(vs->tls.session, data, datalen); 12773e230dd2SCorentin Chary if (ret < 0) { 12783e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 12793e230dd2SCorentin Chary errno = EAGAIN; 12803e230dd2SCorentin Chary else 12813e230dd2SCorentin Chary errno = EIO; 12823e230dd2SCorentin Chary ret = -1; 12833e230dd2SCorentin Chary } 12843e230dd2SCorentin Chary } else 12853e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 128600aa0040SBlue Swirl ret = qemu_recv(vs->csock, data, datalen, 0); 12873e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 12883e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 12893e230dd2SCorentin Chary } 12903e230dd2SCorentin Chary 12913e230dd2SCorentin Chary 12923e230dd2SCorentin Chary /* 12933e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 12943e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 12953e230dd2SCorentin Chary * data as possible without blocking. 12963e230dd2SCorentin Chary * 12973e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 12983e230dd2SCorentin Chary * disconnects the client socket. 12993e230dd2SCorentin Chary */ 13003e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs) 13013e230dd2SCorentin Chary { 13023e230dd2SCorentin Chary int ret; 13033e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13043e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13053e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13063e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13073e230dd2SCorentin Chary if (!ret) 13083e230dd2SCorentin Chary return 0; 13093e230dd2SCorentin Chary vs->input.offset += ret; 13103e230dd2SCorentin Chary return ret; 13113e230dd2SCorentin Chary } 13123e230dd2SCorentin Chary 1313175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1314175b2a6eSCorentin Chary { 1315175b2a6eSCorentin Chary VncState *vs = opaque; 1316175b2a6eSCorentin Chary 1317175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1318175b2a6eSCorentin Chary } 13193e230dd2SCorentin Chary 13203e230dd2SCorentin Chary /* 13213e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13223e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13233e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 13243e230dd2SCorentin Chary */ 13253e230dd2SCorentin Chary void vnc_client_read(void *opaque) 13263e230dd2SCorentin Chary { 13273e230dd2SCorentin Chary VncState *vs = opaque; 13283e230dd2SCorentin Chary long ret; 13293e230dd2SCorentin Chary 13303e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13313e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13323e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13333e230dd2SCorentin Chary else 13343e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13357536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 13367536ee4bSTim Hardeck if (vs->encode_ws) { 13377536ee4bSTim Hardeck ret = vnc_client_read_ws(vs); 13387536ee4bSTim Hardeck if (ret == -1) { 13397536ee4bSTim Hardeck vnc_disconnect_start(vs); 13407536ee4bSTim Hardeck return; 13417536ee4bSTim Hardeck } else if (ret == -2) { 13427536ee4bSTim Hardeck vnc_client_error(vs); 13437536ee4bSTim Hardeck return; 13447536ee4bSTim Hardeck } 13457536ee4bSTim Hardeck } else 13467536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 13477536ee4bSTim Hardeck { 13483e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13497536ee4bSTim Hardeck } 13503e230dd2SCorentin Chary if (!ret) { 13513e230dd2SCorentin Chary if (vs->csock == -1) 13523e230dd2SCorentin Chary vnc_disconnect_finish(vs); 13533e230dd2SCorentin Chary return; 13543e230dd2SCorentin Chary } 13553e230dd2SCorentin Chary 13563e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 13573e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 13583e230dd2SCorentin Chary int ret; 13593e230dd2SCorentin Chary 13603e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 13613e230dd2SCorentin Chary if (vs->csock == -1) { 13623e230dd2SCorentin Chary vnc_disconnect_finish(vs); 13633e230dd2SCorentin Chary return; 13643e230dd2SCorentin Chary } 13653e230dd2SCorentin Chary 13663e230dd2SCorentin Chary if (!ret) { 136732ed2680STim Hardeck buffer_advance(&vs->input, len); 13683e230dd2SCorentin Chary } else { 13693e230dd2SCorentin Chary vs->read_handler_expect = ret; 13703e230dd2SCorentin Chary } 13713e230dd2SCorentin Chary } 13723e230dd2SCorentin Chary } 13733e230dd2SCorentin Chary 13743e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 13753e230dd2SCorentin Chary { 13763e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 13773e230dd2SCorentin Chary 13783e230dd2SCorentin Chary if (vs->csock != -1 && buffer_empty(&vs->output)) { 13793e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); 13803e230dd2SCorentin Chary } 13813e230dd2SCorentin Chary 13823e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 13833e230dd2SCorentin Chary } 13843e230dd2SCorentin Chary 13853e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 13863e230dd2SCorentin Chary { 13873e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 13883e230dd2SCorentin Chary } 13893e230dd2SCorentin Chary 13903e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 13913e230dd2SCorentin Chary { 13923e230dd2SCorentin Chary uint8_t buf[4]; 13933e230dd2SCorentin Chary 13943e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 13953e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 13963e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 13973e230dd2SCorentin Chary buf[3] = value & 0xFF; 13983e230dd2SCorentin Chary 13993e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14003e230dd2SCorentin Chary } 14013e230dd2SCorentin Chary 14023e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14033e230dd2SCorentin Chary { 14043e230dd2SCorentin Chary uint8_t buf[2]; 14053e230dd2SCorentin Chary 14063e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14073e230dd2SCorentin Chary buf[1] = value & 0xFF; 14083e230dd2SCorentin Chary 14093e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14103e230dd2SCorentin Chary } 14113e230dd2SCorentin Chary 14123e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14133e230dd2SCorentin Chary { 14143e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14153e230dd2SCorentin Chary } 14163e230dd2SCorentin Chary 14173e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14183e230dd2SCorentin Chary { 1419bd023f95SCorentin Chary vnc_lock_output(vs); 14207536ee4bSTim Hardeck if (vs->csock != -1 && (vs->output.offset 14217536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 14227536ee4bSTim Hardeck || vs->ws_output.offset 14237536ee4bSTim Hardeck #endif 14247536ee4bSTim Hardeck )) { 1425bd023f95SCorentin Chary vnc_client_write_locked(vs); 1426bd023f95SCorentin Chary } 1427bd023f95SCorentin Chary vnc_unlock_output(vs); 14283e230dd2SCorentin Chary } 14293e230dd2SCorentin Chary 143071a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14313e230dd2SCorentin Chary { 14323e230dd2SCorentin Chary return data[offset]; 14333e230dd2SCorentin Chary } 14343e230dd2SCorentin Chary 143571a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 14363e230dd2SCorentin Chary { 14373e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 14383e230dd2SCorentin Chary } 14393e230dd2SCorentin Chary 144071a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 14413e230dd2SCorentin Chary { 14423e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 14433e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14443e230dd2SCorentin Chary } 14453e230dd2SCorentin Chary 14463e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 14473e230dd2SCorentin Chary { 14483e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 14493e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14503e230dd2SCorentin Chary } 14513e230dd2SCorentin Chary 14523e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 14533e230dd2SCorentin Chary { 14543e230dd2SCorentin Chary } 14553e230dd2SCorentin Chary 14569e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 14573e230dd2SCorentin Chary { 14583e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 14593e230dd2SCorentin Chary int absolute = kbd_mouse_is_absolute(); 14603e230dd2SCorentin Chary 14613e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1462bd023f95SCorentin Chary vnc_lock_output(vs); 14633e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 14643e230dd2SCorentin Chary vnc_write_u8(vs, 0); 14653e230dd2SCorentin Chary vnc_write_u16(vs, 1); 14663e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1467*d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1468*d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 14693e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1470bd023f95SCorentin Chary vnc_unlock_output(vs); 14713e230dd2SCorentin Chary vnc_flush(vs); 14723e230dd2SCorentin Chary } 14733e230dd2SCorentin Chary vs->absolute = absolute; 14743e230dd2SCorentin Chary } 14753e230dd2SCorentin Chary 14763e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 14773e230dd2SCorentin Chary { 14783e230dd2SCorentin Chary int buttons = 0; 14793e230dd2SCorentin Chary int dz = 0; 1480*d39fa6d8SGerd Hoffmann int width = surface_width(vs->vd->ds); 1481*d39fa6d8SGerd Hoffmann int height = surface_height(vs->vd->ds); 14823e230dd2SCorentin Chary 14833e230dd2SCorentin Chary if (button_mask & 0x01) 14843e230dd2SCorentin Chary buttons |= MOUSE_EVENT_LBUTTON; 14853e230dd2SCorentin Chary if (button_mask & 0x02) 14863e230dd2SCorentin Chary buttons |= MOUSE_EVENT_MBUTTON; 14873e230dd2SCorentin Chary if (button_mask & 0x04) 14883e230dd2SCorentin Chary buttons |= MOUSE_EVENT_RBUTTON; 14893e230dd2SCorentin Chary if (button_mask & 0x08) 14903e230dd2SCorentin Chary dz = -1; 14913e230dd2SCorentin Chary if (button_mask & 0x10) 14923e230dd2SCorentin Chary dz = 1; 14933e230dd2SCorentin Chary 14943e230dd2SCorentin Chary if (vs->absolute) { 1495*d39fa6d8SGerd Hoffmann kbd_mouse_event(width > 1 ? x * 0x7FFF / (width - 1) : 0x4000, 1496*d39fa6d8SGerd Hoffmann height > 1 ? y * 0x7FFF / (height - 1) : 0x4000, 14973e230dd2SCorentin Chary dz, buttons); 14983e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 14993e230dd2SCorentin Chary x -= 0x7FFF; 15003e230dd2SCorentin Chary y -= 0x7FFF; 15013e230dd2SCorentin Chary 15023e230dd2SCorentin Chary kbd_mouse_event(x, y, dz, buttons); 15033e230dd2SCorentin Chary } else { 15043e230dd2SCorentin Chary if (vs->last_x != -1) 15053e230dd2SCorentin Chary kbd_mouse_event(x - vs->last_x, 15063e230dd2SCorentin Chary y - vs->last_y, 15073e230dd2SCorentin Chary dz, buttons); 15083e230dd2SCorentin Chary vs->last_x = x; 15093e230dd2SCorentin Chary vs->last_y = y; 15103e230dd2SCorentin Chary } 15113e230dd2SCorentin Chary } 15123e230dd2SCorentin Chary 15133e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15143e230dd2SCorentin Chary { 15153e230dd2SCorentin Chary int i; 15163e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15173e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15183e230dd2SCorentin Chary if (i & SCANCODE_GREY) 15193e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15203e230dd2SCorentin Chary kbd_put_keycode(i | SCANCODE_UP); 15213e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15223e230dd2SCorentin Chary } 15233e230dd2SCorentin Chary } 15243e230dd2SCorentin Chary } 15253e230dd2SCorentin Chary 15263e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15273e230dd2SCorentin Chary { 15283e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15293e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 15303e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15313e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 15323e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 15333e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15343e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 15353e230dd2SCorentin Chary } 15363e230dd2SCorentin Chary 15373e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 15383e230dd2SCorentin Chary { 15393e230dd2SCorentin Chary VncState *vs = opaque; 15403e230dd2SCorentin Chary int caps, num; 15413e230dd2SCorentin Chary 15423e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 15433e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 15443e230dd2SCorentin Chary 15453e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 15463e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 15473e230dd2SCorentin Chary } 15483e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 15493e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 15503e230dd2SCorentin Chary } 15513e230dd2SCorentin Chary } 15523e230dd2SCorentin Chary 15533e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 15543e230dd2SCorentin Chary { 15553e230dd2SCorentin Chary /* QEMU console switch */ 15563e230dd2SCorentin Chary switch(keycode) { 15573e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 15583e230dd2SCorentin Chary case 0x36: /* Right Shift */ 15593e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 15603e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 15613e230dd2SCorentin Chary case 0x38: /* Left ALT */ 15623e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 15633e230dd2SCorentin Chary if (down) 15643e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 15653e230dd2SCorentin Chary else 15663e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 15673e230dd2SCorentin Chary break; 15683e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 15693e230dd2SCorentin Chary if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 15703e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 15713e230dd2SCorentin Chary reset_keys(vs); 15723e230dd2SCorentin Chary console_select(keycode - 0x02); 15733e230dd2SCorentin Chary return; 15743e230dd2SCorentin Chary } 15753e230dd2SCorentin Chary break; 15763e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 15773e230dd2SCorentin Chary case 0x45: /* NumLock */ 15783e230dd2SCorentin Chary if (down) 15793e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 15803e230dd2SCorentin Chary break; 15813e230dd2SCorentin Chary } 15823e230dd2SCorentin Chary 15839892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 15843e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 15853e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 15863e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 15873e230dd2SCorentin Chary toggles numlock away from the VNC window. 15883e230dd2SCorentin Chary */ 15893e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 15903e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 15913e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 15923e230dd2SCorentin Chary press_key(vs, 0xff7f); 15933e230dd2SCorentin Chary } 15943e230dd2SCorentin Chary } else { 15953e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 15963e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 15973e230dd2SCorentin Chary press_key(vs, 0xff7f); 15983e230dd2SCorentin Chary } 15993e230dd2SCorentin Chary } 16003e230dd2SCorentin Chary } 16013e230dd2SCorentin Chary 16029892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 16033e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 16043e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 16053e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16063e230dd2SCorentin Chary toggles capslock away from the VNC window. 16073e230dd2SCorentin Chary */ 16083e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 16093e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 16103e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 16113e230dd2SCorentin Chary if (capslock) { 16123e230dd2SCorentin Chary if (uppercase == shift) { 16133e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 16143e230dd2SCorentin Chary press_key(vs, 0xffe5); 16153e230dd2SCorentin Chary } 16163e230dd2SCorentin Chary } else { 16173e230dd2SCorentin Chary if (uppercase != shift) { 16183e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 16193e230dd2SCorentin Chary press_key(vs, 0xffe5); 16203e230dd2SCorentin Chary } 16213e230dd2SCorentin Chary } 16223e230dd2SCorentin Chary } 16233e230dd2SCorentin Chary 16243e230dd2SCorentin Chary if (is_graphic_console()) { 16253e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 16263e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 16273e230dd2SCorentin Chary if (down) 16283e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 16293e230dd2SCorentin Chary else 16303e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 16313e230dd2SCorentin Chary } else { 1632e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1633e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1634e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 16353e230dd2SCorentin Chary /* QEMU console emulation */ 16363e230dd2SCorentin Chary if (down) { 16373e230dd2SCorentin Chary switch (keycode) { 16383e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16393e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16403e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16413e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16423e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16433e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16443e230dd2SCorentin Chary break; 16453e230dd2SCorentin Chary case 0xc8: 16463e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 16473e230dd2SCorentin Chary break; 16483e230dd2SCorentin Chary case 0xd0: 16493e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 16503e230dd2SCorentin Chary break; 16513e230dd2SCorentin Chary case 0xcb: 16523e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 16533e230dd2SCorentin Chary break; 16543e230dd2SCorentin Chary case 0xcd: 16553e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 16563e230dd2SCorentin Chary break; 16573e230dd2SCorentin Chary case 0xd3: 16583e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 16593e230dd2SCorentin Chary break; 16603e230dd2SCorentin Chary case 0xc7: 16613e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 16623e230dd2SCorentin Chary break; 16633e230dd2SCorentin Chary case 0xcf: 16643e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 16653e230dd2SCorentin Chary break; 16663e230dd2SCorentin Chary case 0xc9: 16673e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 16683e230dd2SCorentin Chary break; 16693e230dd2SCorentin Chary case 0xd1: 16703e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 16713e230dd2SCorentin Chary break; 16723e230dd2SCorentin Chary 16733e230dd2SCorentin Chary case 0x47: 16743e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 16753e230dd2SCorentin Chary break; 16763e230dd2SCorentin Chary case 0x48: 16773e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 16783e230dd2SCorentin Chary break; 16793e230dd2SCorentin Chary case 0x49: 16803e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 16813e230dd2SCorentin Chary break; 16823e230dd2SCorentin Chary case 0x4b: 16833e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 16843e230dd2SCorentin Chary break; 16853e230dd2SCorentin Chary case 0x4c: 16863e230dd2SCorentin Chary kbd_put_keysym('5'); 16873e230dd2SCorentin Chary break; 16883e230dd2SCorentin Chary case 0x4d: 16893e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 16903e230dd2SCorentin Chary break; 16913e230dd2SCorentin Chary case 0x4f: 16923e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 16933e230dd2SCorentin Chary break; 16943e230dd2SCorentin Chary case 0x50: 16953e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 16963e230dd2SCorentin Chary break; 16973e230dd2SCorentin Chary case 0x51: 16983e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 16993e230dd2SCorentin Chary break; 17003e230dd2SCorentin Chary case 0x52: 17013e230dd2SCorentin Chary kbd_put_keysym('0'); 17023e230dd2SCorentin Chary break; 17033e230dd2SCorentin Chary case 0x53: 17043e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 17053e230dd2SCorentin Chary break; 17063e230dd2SCorentin Chary 17073e230dd2SCorentin Chary case 0xb5: 17083e230dd2SCorentin Chary kbd_put_keysym('/'); 17093e230dd2SCorentin Chary break; 17103e230dd2SCorentin Chary case 0x37: 17113e230dd2SCorentin Chary kbd_put_keysym('*'); 17123e230dd2SCorentin Chary break; 17133e230dd2SCorentin Chary case 0x4a: 17143e230dd2SCorentin Chary kbd_put_keysym('-'); 17153e230dd2SCorentin Chary break; 17163e230dd2SCorentin Chary case 0x4e: 17173e230dd2SCorentin Chary kbd_put_keysym('+'); 17183e230dd2SCorentin Chary break; 17193e230dd2SCorentin Chary case 0x9c: 17203e230dd2SCorentin Chary kbd_put_keysym('\n'); 17213e230dd2SCorentin Chary break; 17223e230dd2SCorentin Chary 17233e230dd2SCorentin Chary default: 1724e26437c2SGerd Hoffmann if (control) { 1725e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1726e26437c2SGerd Hoffmann } else { 17273e230dd2SCorentin Chary kbd_put_keysym(sym); 1728e26437c2SGerd Hoffmann } 17293e230dd2SCorentin Chary break; 17303e230dd2SCorentin Chary } 17313e230dd2SCorentin Chary } 17323e230dd2SCorentin Chary } 17333e230dd2SCorentin Chary } 17343e230dd2SCorentin Chary 17357bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 17367bc9318bSGerd Hoffmann { 17377bc9318bSGerd Hoffmann static const int keycodes[] = { 17387bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 17397bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 17407bc9318bSGerd Hoffmann }; 17417bc9318bSGerd Hoffmann int i, keycode; 17427bc9318bSGerd Hoffmann 17437bc9318bSGerd Hoffmann if (!is_graphic_console()) { 17447bc9318bSGerd Hoffmann return; 17457bc9318bSGerd Hoffmann } 17467bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 17477bc9318bSGerd Hoffmann keycode = keycodes[i]; 17487bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 17497bc9318bSGerd Hoffmann continue; 17507bc9318bSGerd Hoffmann } 17517bc9318bSGerd Hoffmann if (keycode & SCANCODE_GREY) { 17527bc9318bSGerd Hoffmann kbd_put_keycode(SCANCODE_EMUL0); 17537bc9318bSGerd Hoffmann } 17547bc9318bSGerd Hoffmann kbd_put_keycode(keycode | SCANCODE_UP); 17557bc9318bSGerd Hoffmann } 17567bc9318bSGerd Hoffmann } 17577bc9318bSGerd Hoffmann 17583e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 17593e230dd2SCorentin Chary { 17603e230dd2SCorentin Chary int keycode; 17613e230dd2SCorentin Chary int lsym = sym; 17623e230dd2SCorentin Chary 17633e230dd2SCorentin Chary if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) { 17643e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 17653e230dd2SCorentin Chary } 17663e230dd2SCorentin Chary 17673e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 17683e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 17693e230dd2SCorentin Chary } 17703e230dd2SCorentin Chary 17713e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 17723e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 17733e230dd2SCorentin Chary { 17743e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 17753e230dd2SCorentin Chary if (keyboard_layout) 17763e230dd2SCorentin Chary key_event(vs, down, sym); 17773e230dd2SCorentin Chary else 17783e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 17793e230dd2SCorentin Chary } 17803e230dd2SCorentin Chary 17813e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 17823e230dd2SCorentin Chary int x_position, int y_position, 17833e230dd2SCorentin Chary int w, int h) 17843e230dd2SCorentin Chary { 17856ed391bfSWen Congyang int i; 1786*d39fa6d8SGerd Hoffmann const size_t width = surface_width(vs->vd->ds) / 16; 1787*d39fa6d8SGerd Hoffmann const size_t height = surface_height(vs->vd->ds); 17886ed391bfSWen Congyang 1789*d39fa6d8SGerd Hoffmann if (y_position > height) { 1790*d39fa6d8SGerd Hoffmann y_position = height; 1791*d39fa6d8SGerd Hoffmann } 1792*d39fa6d8SGerd Hoffmann if (y_position + h >= height) { 1793*d39fa6d8SGerd Hoffmann h = height - y_position; 1794*d39fa6d8SGerd Hoffmann } 17953e230dd2SCorentin Chary 17963e230dd2SCorentin Chary vs->need_update = 1; 17973e230dd2SCorentin Chary if (!incremental) { 17983e230dd2SCorentin Chary vs->force_update = 1; 17993e230dd2SCorentin Chary for (i = 0; i < h; i++) { 18006ed391bfSWen Congyang bitmap_set(vs->dirty[y_position + i], 0, width); 18016ed391bfSWen Congyang bitmap_clear(vs->dirty[y_position + i], width, 1802a0843a68SAnthony Liguori VNC_DIRTY_BITS - width); 18033e230dd2SCorentin Chary } 18043e230dd2SCorentin Chary } 18053e230dd2SCorentin Chary } 18063e230dd2SCorentin Chary 18073e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18083e230dd2SCorentin Chary { 1809bd023f95SCorentin Chary vnc_lock_output(vs); 18103e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18113e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18123e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1813*d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1814*d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1815*d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 18163e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1817bd023f95SCorentin Chary vnc_unlock_output(vs); 18183e230dd2SCorentin Chary vnc_flush(vs); 18193e230dd2SCorentin Chary } 18203e230dd2SCorentin Chary 18213e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 18223e230dd2SCorentin Chary { 1823bd023f95SCorentin Chary vnc_lock_output(vs); 18243e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18253e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18263e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1827*d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1828*d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1829*d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 18303e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1831bd023f95SCorentin Chary vnc_unlock_output(vs); 18323e230dd2SCorentin Chary vnc_flush(vs); 18333e230dd2SCorentin Chary } 18343e230dd2SCorentin Chary 18353e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 18363e230dd2SCorentin Chary { 18373e230dd2SCorentin Chary int i; 18383e230dd2SCorentin Chary unsigned int enc = 0; 18393e230dd2SCorentin Chary 18403e230dd2SCorentin Chary vs->features = 0; 18413e230dd2SCorentin Chary vs->vnc_encoding = 0; 1842d1af0e05SCorentin Chary vs->tight.compression = 9; 1843d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 18443e230dd2SCorentin Chary vs->absolute = -1; 18453e230dd2SCorentin Chary 18463e230dd2SCorentin Chary /* 18473e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1848e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 18493e230dd2SCorentin Chary * will be set at the end of the loop. 18503e230dd2SCorentin Chary */ 18513e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 18523e230dd2SCorentin Chary enc = encodings[i]; 18533e230dd2SCorentin Chary switch (enc) { 18543e230dd2SCorentin Chary case VNC_ENCODING_RAW: 18553e230dd2SCorentin Chary vs->vnc_encoding = enc; 18563e230dd2SCorentin Chary break; 18573e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 18583e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 18593e230dd2SCorentin Chary break; 18603e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 18613e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 18623e230dd2SCorentin Chary vs->vnc_encoding = enc; 18633e230dd2SCorentin Chary break; 18643e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 18653e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 18663e230dd2SCorentin Chary vs->vnc_encoding = enc; 18673e230dd2SCorentin Chary break; 1868fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1869efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1870efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1871efe556adSCorentin Chary vs->vnc_encoding = enc; 1872efe556adSCorentin Chary break; 1873fe3e7f2dSJoel Martin #endif 18743e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 18753e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 18763e230dd2SCorentin Chary vs->vnc_encoding = enc; 18773e230dd2SCorentin Chary break; 1878148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1879148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1880148954faSCorentin Chary vs->vnc_encoding = enc; 1881148954faSCorentin Chary break; 1882148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1883148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1884148954faSCorentin Chary vs->vnc_encoding = enc; 1885148954faSCorentin Chary break; 18863e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 18873e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 18883e230dd2SCorentin Chary break; 18893e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 18903e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 18913e230dd2SCorentin Chary break; 18923e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 18933e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 18943e230dd2SCorentin Chary break; 18953e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 18963e230dd2SCorentin Chary send_ext_key_event_ack(vs); 18973e230dd2SCorentin Chary break; 18983e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 18993e230dd2SCorentin Chary send_ext_audio_ack(vs); 19003e230dd2SCorentin Chary break; 19013e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19023e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19033e230dd2SCorentin Chary break; 19043e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1905d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 19063e230dd2SCorentin Chary break; 19073e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1908b31f519eSCorentin Chary if (vs->vd->lossy) { 1909d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1910b31f519eSCorentin Chary } 19113e230dd2SCorentin Chary break; 19123e230dd2SCorentin Chary default: 19133e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 19143e230dd2SCorentin Chary break; 19153e230dd2SCorentin Chary } 19163e230dd2SCorentin Chary } 19173e230dd2SCorentin Chary vnc_desktop_resize(vs); 19189e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 19193e230dd2SCorentin Chary } 19203e230dd2SCorentin Chary 19213e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 19223e230dd2SCorentin Chary { 19239f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 19249f64916dSGerd Hoffmann 19259f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 19263e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 19273e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 19283e230dd2SCorentin Chary } else { 19293e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 19303e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 19313e230dd2SCorentin Chary } 19323e230dd2SCorentin Chary } 19333e230dd2SCorentin Chary 19343e230dd2SCorentin Chary static void set_pixel_format(VncState *vs, 19353e230dd2SCorentin Chary int bits_per_pixel, int depth, 19363e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 19373e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 19383e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 19393e230dd2SCorentin Chary { 19403e230dd2SCorentin Chary if (!true_color_flag) { 19413e230dd2SCorentin Chary vnc_client_error(vs); 19423e230dd2SCorentin Chary return; 19433e230dd2SCorentin Chary } 19443e230dd2SCorentin Chary 19459f64916dSGerd Hoffmann vs->client_pf.rmax = red_max; 19469f64916dSGerd Hoffmann vs->client_pf.rbits = hweight_long(red_max); 19479f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 19489f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 19499f64916dSGerd Hoffmann vs->client_pf.gmax = green_max; 19509f64916dSGerd Hoffmann vs->client_pf.gbits = hweight_long(green_max); 19519f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 19529f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 19539f64916dSGerd Hoffmann vs->client_pf.bmax = blue_max; 19549f64916dSGerd Hoffmann vs->client_pf.bbits = hweight_long(blue_max); 19559f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 19569f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 19579f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 19589f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 19599f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 19609f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 19613e230dd2SCorentin Chary 19623e230dd2SCorentin Chary set_pixel_conversion(vs); 19633e230dd2SCorentin Chary 19643e230dd2SCorentin Chary vga_hw_invalidate(); 19653e230dd2SCorentin Chary vga_hw_update(); 19663e230dd2SCorentin Chary } 19673e230dd2SCorentin Chary 19683e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 19693e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 19703e230dd2SCorentin Chary 19719f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 19729f64916dSGerd Hoffmann 19739f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 19749f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 19753e230dd2SCorentin Chary 19763e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 19773e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 19783e230dd2SCorentin Chary #else 19793e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 19803e230dd2SCorentin Chary #endif 19813e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 19829f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 19839f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 19849f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 19859f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 19869f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 19879f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 19889f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 19893e230dd2SCorentin Chary 19903e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 19913e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 19923e230dd2SCorentin Chary } 19933e230dd2SCorentin Chary 19943e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 19953e230dd2SCorentin Chary { 19963e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 19973e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 1998bd023f95SCorentin Chary vnc_lock_output(vs); 19993e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 20003e230dd2SCorentin Chary vnc_write_u8(vs, 0); 20013e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2002*d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2003*d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 2004*d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 2005*d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 20063e230dd2SCorentin Chary pixel_format_message(vs); 2007bd023f95SCorentin Chary vnc_unlock_output(vs); 20083e230dd2SCorentin Chary vnc_flush(vs); 20093e230dd2SCorentin Chary } else { 20103e230dd2SCorentin Chary set_pixel_conversion(vs); 20113e230dd2SCorentin Chary } 20123e230dd2SCorentin Chary } 20133e230dd2SCorentin Chary 20143e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 20153e230dd2SCorentin Chary { 20163e230dd2SCorentin Chary int i; 20173e230dd2SCorentin Chary uint16_t limit; 20183e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 20193e230dd2SCorentin Chary 20203e230dd2SCorentin Chary if (data[0] > 3) { 20213e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 20227bd427d8SPaolo Bonzini if (!qemu_timer_expired(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval)) 20237bd427d8SPaolo Bonzini qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval); 20243e230dd2SCorentin Chary } 20253e230dd2SCorentin Chary 20263e230dd2SCorentin Chary switch (data[0]) { 20273e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 20283e230dd2SCorentin Chary if (len == 1) 20293e230dd2SCorentin Chary return 20; 20303e230dd2SCorentin Chary 20313e230dd2SCorentin Chary set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), 20323e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 20333e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 20343e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 20353e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 20363e230dd2SCorentin Chary break; 20373e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 20383e230dd2SCorentin Chary if (len == 1) 20393e230dd2SCorentin Chary return 4; 20403e230dd2SCorentin Chary 20413e230dd2SCorentin Chary if (len == 4) { 20423e230dd2SCorentin Chary limit = read_u16(data, 2); 20433e230dd2SCorentin Chary if (limit > 0) 20443e230dd2SCorentin Chary return 4 + (limit * 4); 20453e230dd2SCorentin Chary } else 20463e230dd2SCorentin Chary limit = read_u16(data, 2); 20473e230dd2SCorentin Chary 20483e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 20493e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 20503e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 20513e230dd2SCorentin Chary } 20523e230dd2SCorentin Chary 20533e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 20543e230dd2SCorentin Chary break; 20553e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 20563e230dd2SCorentin Chary if (len == 1) 20573e230dd2SCorentin Chary return 10; 20583e230dd2SCorentin Chary 20593e230dd2SCorentin Chary framebuffer_update_request(vs, 20603e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 20613e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 20623e230dd2SCorentin Chary break; 20633e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 20643e230dd2SCorentin Chary if (len == 1) 20653e230dd2SCorentin Chary return 8; 20663e230dd2SCorentin Chary 20673e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 20683e230dd2SCorentin Chary break; 20693e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 20703e230dd2SCorentin Chary if (len == 1) 20713e230dd2SCorentin Chary return 6; 20723e230dd2SCorentin Chary 20733e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 20743e230dd2SCorentin Chary break; 20753e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 20763e230dd2SCorentin Chary if (len == 1) 20773e230dd2SCorentin Chary return 8; 20783e230dd2SCorentin Chary 20793e230dd2SCorentin Chary if (len == 8) { 20803e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 20813e230dd2SCorentin Chary if (dlen > 0) 20823e230dd2SCorentin Chary return 8 + dlen; 20833e230dd2SCorentin Chary } 20843e230dd2SCorentin Chary 20853e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 20863e230dd2SCorentin Chary break; 20873e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 20883e230dd2SCorentin Chary if (len == 1) 20893e230dd2SCorentin Chary return 2; 20903e230dd2SCorentin Chary 20913e230dd2SCorentin Chary switch (read_u8(data, 1)) { 20923e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 20933e230dd2SCorentin Chary if (len == 2) 20943e230dd2SCorentin Chary return 12; 20953e230dd2SCorentin Chary 20963e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 20973e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 20983e230dd2SCorentin Chary break; 20993e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 21003e230dd2SCorentin Chary if (len == 2) 21013e230dd2SCorentin Chary return 4; 21023e230dd2SCorentin Chary 21033e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 21043e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 21053e230dd2SCorentin Chary audio_add(vs); 21063e230dd2SCorentin Chary break; 21073e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 21083e230dd2SCorentin Chary audio_del(vs); 21093e230dd2SCorentin Chary break; 21103e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 21113e230dd2SCorentin Chary if (len == 4) 21123e230dd2SCorentin Chary return 10; 21133e230dd2SCorentin Chary switch (read_u8(data, 4)) { 21143e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 21153e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 21163e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 21173e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 21183e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 21193e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 21203e230dd2SCorentin Chary default: 21213e230dd2SCorentin Chary printf("Invalid audio format %d\n", read_u8(data, 4)); 21223e230dd2SCorentin Chary vnc_client_error(vs); 21233e230dd2SCorentin Chary break; 21243e230dd2SCorentin Chary } 21253e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 21263e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 21273e230dd2SCorentin Chary printf("Invalid audio channel coount %d\n", 21283e230dd2SCorentin Chary read_u8(data, 5)); 21293e230dd2SCorentin Chary vnc_client_error(vs); 21303e230dd2SCorentin Chary break; 21313e230dd2SCorentin Chary } 21323e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 21333e230dd2SCorentin Chary break; 21343e230dd2SCorentin Chary default: 21353e230dd2SCorentin Chary printf ("Invalid audio message %d\n", read_u8(data, 4)); 21363e230dd2SCorentin Chary vnc_client_error(vs); 21373e230dd2SCorentin Chary break; 21383e230dd2SCorentin Chary } 21393e230dd2SCorentin Chary break; 21403e230dd2SCorentin Chary 21413e230dd2SCorentin Chary default: 21423e230dd2SCorentin Chary printf("Msg: %d\n", read_u16(data, 0)); 21433e230dd2SCorentin Chary vnc_client_error(vs); 21443e230dd2SCorentin Chary break; 21453e230dd2SCorentin Chary } 21463e230dd2SCorentin Chary break; 21473e230dd2SCorentin Chary default: 21483e230dd2SCorentin Chary printf("Msg: %d\n", data[0]); 21493e230dd2SCorentin Chary vnc_client_error(vs); 21503e230dd2SCorentin Chary break; 21513e230dd2SCorentin Chary } 21523e230dd2SCorentin Chary 21533e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 21543e230dd2SCorentin Chary return 0; 21553e230dd2SCorentin Chary } 21563e230dd2SCorentin Chary 21573e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 21583e230dd2SCorentin Chary { 21593e230dd2SCorentin Chary char buf[1024]; 21608cf36489SGerd Hoffmann VncShareMode mode; 21613e230dd2SCorentin Chary int size; 21623e230dd2SCorentin Chary 21638cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 21648cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 21658cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 21668cf36489SGerd Hoffmann /* 21678cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 21688cf36489SGerd Hoffmann * 21698cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 21708cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 21718cf36489SGerd Hoffmann * reasons. 21728cf36489SGerd Hoffmann */ 21738cf36489SGerd Hoffmann break; 21748cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 21758cf36489SGerd Hoffmann /* 21768cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 21778cf36489SGerd Hoffmann * 21788cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 21798cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 21808cf36489SGerd Hoffmann * as no exclusive connection exists. 21818cf36489SGerd Hoffmann * 21828cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 21838cf36489SGerd Hoffmann */ 21848cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 21858cf36489SGerd Hoffmann VncState *client; 21868cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 21878cf36489SGerd Hoffmann if (vs == client) { 21888cf36489SGerd Hoffmann continue; 21898cf36489SGerd Hoffmann } 21908cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 21918cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 21928cf36489SGerd Hoffmann continue; 21938cf36489SGerd Hoffmann } 21948cf36489SGerd Hoffmann vnc_disconnect_start(client); 21958cf36489SGerd Hoffmann } 21968cf36489SGerd Hoffmann } 21978cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 21988cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 21998cf36489SGerd Hoffmann vnc_disconnect_start(vs); 22008cf36489SGerd Hoffmann return 0; 22018cf36489SGerd Hoffmann } 22028cf36489SGerd Hoffmann } 22038cf36489SGerd Hoffmann break; 22048cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 22058cf36489SGerd Hoffmann /* 22068cf36489SGerd Hoffmann * Policy: Shared connects only. 22078cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 22088cf36489SGerd Hoffmann * 22098cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 22108cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 22118cf36489SGerd Hoffmann * client disconnect everybody else. 22128cf36489SGerd Hoffmann */ 22138cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 22148cf36489SGerd Hoffmann vnc_disconnect_start(vs); 22158cf36489SGerd Hoffmann return 0; 22168cf36489SGerd Hoffmann } 22178cf36489SGerd Hoffmann break; 22188cf36489SGerd Hoffmann } 22198cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 22208cf36489SGerd Hoffmann 2221*d39fa6d8SGerd Hoffmann vs->client_width = surface_width(vs->vd->ds); 2222*d39fa6d8SGerd Hoffmann vs->client_height = surface_height(vs->vd->ds); 22233e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 22243e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 22253e230dd2SCorentin Chary 22263e230dd2SCorentin Chary pixel_format_message(vs); 22273e230dd2SCorentin Chary 22283e230dd2SCorentin Chary if (qemu_name) 22293e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 22303e230dd2SCorentin Chary else 22313e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 22323e230dd2SCorentin Chary 22333e230dd2SCorentin Chary vnc_write_u32(vs, size); 22343e230dd2SCorentin Chary vnc_write(vs, buf, size); 22353e230dd2SCorentin Chary vnc_flush(vs); 22363e230dd2SCorentin Chary 22373e230dd2SCorentin Chary vnc_client_cache_auth(vs); 22383e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED); 22393e230dd2SCorentin Chary 22403e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22413e230dd2SCorentin Chary 22423e230dd2SCorentin Chary return 0; 22433e230dd2SCorentin Chary } 22443e230dd2SCorentin Chary 22453e230dd2SCorentin Chary void start_client_init(VncState *vs) 22463e230dd2SCorentin Chary { 22473e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 22483e230dd2SCorentin Chary } 22493e230dd2SCorentin Chary 22503e230dd2SCorentin Chary static void make_challenge(VncState *vs) 22513e230dd2SCorentin Chary { 22523e230dd2SCorentin Chary int i; 22533e230dd2SCorentin Chary 22543e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 22553e230dd2SCorentin Chary 22563e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 22573e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 22583e230dd2SCorentin Chary } 22593e230dd2SCorentin Chary 22603e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 22613e230dd2SCorentin Chary { 22623e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 22633e230dd2SCorentin Chary int i, j, pwlen; 22643e230dd2SCorentin Chary unsigned char key[8]; 22653c9405a0SGerd Hoffmann time_t now = time(NULL); 22663e230dd2SCorentin Chary 22671cd20f8bSAnthony Liguori if (!vs->vd->password) { 22683e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 22696bffdf0fSGerd Hoffmann goto reject; 22703e230dd2SCorentin Chary } 22713c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 22723c9405a0SGerd Hoffmann VNC_DEBUG("Password is expired"); 22733c9405a0SGerd Hoffmann goto reject; 22743c9405a0SGerd Hoffmann } 22753e230dd2SCorentin Chary 22763e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 22773e230dd2SCorentin Chary 22783e230dd2SCorentin Chary /* Calculate the expected challenge response */ 22793e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 22803e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 22813e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 22823e230dd2SCorentin Chary deskey(key, EN0); 22833e230dd2SCorentin Chary for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8) 22843e230dd2SCorentin Chary des(response+j, response+j); 22853e230dd2SCorentin Chary 22863e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 22873e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2288e5bed759SDong Xu Wang VNC_DEBUG("Client challenge response did not match\n"); 22896bffdf0fSGerd Hoffmann goto reject; 22906bffdf0fSGerd Hoffmann } else { 22916bffdf0fSGerd Hoffmann VNC_DEBUG("Accepting VNC challenge response\n"); 22926bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 22936bffdf0fSGerd Hoffmann vnc_flush(vs); 22946bffdf0fSGerd Hoffmann 22956bffdf0fSGerd Hoffmann start_client_init(vs); 22966bffdf0fSGerd Hoffmann } 22976bffdf0fSGerd Hoffmann return 0; 22986bffdf0fSGerd Hoffmann 22996bffdf0fSGerd Hoffmann reject: 23003e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 23013e230dd2SCorentin Chary if (vs->minor >= 8) { 23023e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23033e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23043e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23053e230dd2SCorentin Chary } 23063e230dd2SCorentin Chary vnc_flush(vs); 23073e230dd2SCorentin Chary vnc_client_error(vs); 23083e230dd2SCorentin Chary return 0; 23093e230dd2SCorentin Chary } 23103e230dd2SCorentin Chary 23113e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 23123e230dd2SCorentin Chary { 23133e230dd2SCorentin Chary make_challenge(vs); 23143e230dd2SCorentin Chary /* Send client a 'random' challenge */ 23153e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 23163e230dd2SCorentin Chary vnc_flush(vs); 23173e230dd2SCorentin Chary 23183e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 23193e230dd2SCorentin Chary } 23203e230dd2SCorentin Chary 23213e230dd2SCorentin Chary 23223e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 23233e230dd2SCorentin Chary { 23243e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 23253e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 23267e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 23273e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 23283e230dd2SCorentin Chary vnc_write_u32(vs, 1); 23293e230dd2SCorentin Chary if (vs->minor >= 8) { 23303e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23313e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23323e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23333e230dd2SCorentin Chary } 23343e230dd2SCorentin Chary vnc_client_error(vs); 23353e230dd2SCorentin Chary } else { /* Accept requested auth */ 23363e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 23377e7e2ebcSDaniel P. Berrange switch (vs->auth) { 23383e230dd2SCorentin Chary case VNC_AUTH_NONE: 23393e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 23403e230dd2SCorentin Chary if (vs->minor >= 8) { 23413e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 23423e230dd2SCorentin Chary vnc_flush(vs); 23433e230dd2SCorentin Chary } 23443e230dd2SCorentin Chary start_client_init(vs); 23453e230dd2SCorentin Chary break; 23463e230dd2SCorentin Chary 23473e230dd2SCorentin Chary case VNC_AUTH_VNC: 23483e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 23493e230dd2SCorentin Chary start_auth_vnc(vs); 23503e230dd2SCorentin Chary break; 23513e230dd2SCorentin Chary 23523e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 23533e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 23543a93113aSDong Xu Wang VNC_DEBUG("Accept VeNCrypt auth\n"); 23553e230dd2SCorentin Chary start_auth_vencrypt(vs); 23563e230dd2SCorentin Chary break; 23573e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 23583e230dd2SCorentin Chary 23593e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 23603e230dd2SCorentin Chary case VNC_AUTH_SASL: 23613e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 23623e230dd2SCorentin Chary start_auth_sasl(vs); 23633e230dd2SCorentin Chary break; 23643e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 23653e230dd2SCorentin Chary 23663e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 23677e7e2ebcSDaniel P. Berrange VNC_DEBUG("Reject auth %d server code bug\n", vs->auth); 23683e230dd2SCorentin Chary vnc_write_u8(vs, 1); 23693e230dd2SCorentin Chary if (vs->minor >= 8) { 23703e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23713e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23723e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23733e230dd2SCorentin Chary } 23743e230dd2SCorentin Chary vnc_client_error(vs); 23753e230dd2SCorentin Chary } 23763e230dd2SCorentin Chary } 23773e230dd2SCorentin Chary return 0; 23783e230dd2SCorentin Chary } 23793e230dd2SCorentin Chary 23803e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 23813e230dd2SCorentin Chary { 23823e230dd2SCorentin Chary char local[13]; 23833e230dd2SCorentin Chary 23843e230dd2SCorentin Chary memcpy(local, version, 12); 23853e230dd2SCorentin Chary local[12] = 0; 23863e230dd2SCorentin Chary 23873e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 23883e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 23893e230dd2SCorentin Chary vnc_client_error(vs); 23903e230dd2SCorentin Chary return 0; 23913e230dd2SCorentin Chary } 23923e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 23933e230dd2SCorentin Chary if (vs->major != 3 || 23943e230dd2SCorentin Chary (vs->minor != 3 && 23953e230dd2SCorentin Chary vs->minor != 4 && 23963e230dd2SCorentin Chary vs->minor != 5 && 23973e230dd2SCorentin Chary vs->minor != 7 && 23983e230dd2SCorentin Chary vs->minor != 8)) { 23993e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 24003e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 24013e230dd2SCorentin Chary vnc_flush(vs); 24023e230dd2SCorentin Chary vnc_client_error(vs); 24033e230dd2SCorentin Chary return 0; 24043e230dd2SCorentin Chary } 24053e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 24063e230dd2SCorentin Chary * as equivalent to v3.3 by servers 24073e230dd2SCorentin Chary */ 24083e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 24093e230dd2SCorentin Chary vs->minor = 3; 24103e230dd2SCorentin Chary 24113e230dd2SCorentin Chary if (vs->minor == 3) { 24127e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 24133e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 24147e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 24153e230dd2SCorentin Chary vnc_flush(vs); 24163e230dd2SCorentin Chary start_client_init(vs); 24177e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 24183e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 24197e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 24203e230dd2SCorentin Chary vnc_flush(vs); 24213e230dd2SCorentin Chary start_auth_vnc(vs); 24223e230dd2SCorentin Chary } else { 24237e7e2ebcSDaniel P. Berrange VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth); 24243e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 24253e230dd2SCorentin Chary vnc_flush(vs); 24263e230dd2SCorentin Chary vnc_client_error(vs); 24273e230dd2SCorentin Chary } 24283e230dd2SCorentin Chary } else { 24297e7e2ebcSDaniel P. Berrange VNC_DEBUG("Telling client we support auth %d\n", vs->auth); 24303e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 24317e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 24323e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 24333e230dd2SCorentin Chary vnc_flush(vs); 24343e230dd2SCorentin Chary } 24353e230dd2SCorentin Chary 24363e230dd2SCorentin Chary return 0; 24373e230dd2SCorentin Chary } 24383e230dd2SCorentin Chary 2439999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2440999342a0SCorentin Chary { 2441999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2442999342a0SCorentin Chary 2443999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2444999342a0SCorentin Chary } 2445999342a0SCorentin Chary 24467d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 24477d964c9dSCorentin Chary { 24487d964c9dSCorentin Chary int i, j; 24497d964c9dSCorentin Chary 24507d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 24517d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 24527d964c9dSCorentin Chary x /= VNC_STAT_RECT; 24537d964c9dSCorentin Chary y /= VNC_STAT_RECT; 24547d964c9dSCorentin Chary 2455207f328aSCorentin Chary for (j = y; j <= h; j++) { 2456207f328aSCorentin Chary for (i = x; i <= w; i++) { 24577d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 24587d964c9dSCorentin Chary } 24597d964c9dSCorentin Chary } 24607d964c9dSCorentin Chary } 24617d964c9dSCorentin Chary 24627d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 24637d964c9dSCorentin Chary { 24647d964c9dSCorentin Chary VncState *vs; 24657d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 24667d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 24677d964c9dSCorentin Chary int has_dirty = 0; 24687d964c9dSCorentin Chary 24697d964c9dSCorentin Chary y = y / VNC_STAT_RECT * VNC_STAT_RECT; 24707d964c9dSCorentin Chary x = x / VNC_STAT_RECT * VNC_STAT_RECT; 24717d964c9dSCorentin Chary 24727d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2473bc2429b9SCorentin Chary int j; 24747d964c9dSCorentin Chary 24757d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 24767d964c9dSCorentin Chary if (vs->output.offset) { 24777d964c9dSCorentin Chary continue; 24787d964c9dSCorentin Chary } 24797d964c9dSCorentin Chary 24807d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 24817d964c9dSCorentin Chary continue; 24827d964c9dSCorentin Chary } 2483207f328aSCorentin Chary 24847d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 24857d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2486bc2429b9SCorentin Chary bitmap_set(vs->dirty[y + j], x / 16, VNC_STAT_RECT / 16); 24877d964c9dSCorentin Chary } 24887d964c9dSCorentin Chary has_dirty++; 24897d964c9dSCorentin Chary } 2490207f328aSCorentin Chary 24917d964c9dSCorentin Chary return has_dirty; 24927d964c9dSCorentin Chary } 24937d964c9dSCorentin Chary 24947d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2495999342a0SCorentin Chary { 24969f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 24979f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 2498999342a0SCorentin Chary int x, y; 2499999342a0SCorentin Chary struct timeval res; 25007d964c9dSCorentin Chary int has_dirty = 0; 2501999342a0SCorentin Chary 25029f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 25039f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2504999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2505999342a0SCorentin Chary 2506999342a0SCorentin Chary rect->updated = false; 2507999342a0SCorentin Chary } 2508999342a0SCorentin Chary } 2509999342a0SCorentin Chary 2510ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2511999342a0SCorentin Chary 2512999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 25137d964c9dSCorentin Chary return has_dirty; 2514999342a0SCorentin Chary } 2515999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2516999342a0SCorentin Chary 25179f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 25189f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2519999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2520999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2521999342a0SCorentin Chary struct timeval min, max; 2522999342a0SCorentin Chary 2523999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2524999342a0SCorentin Chary continue ; 2525999342a0SCorentin Chary } 2526999342a0SCorentin Chary 2527999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2528ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2529999342a0SCorentin Chary 2530999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2531999342a0SCorentin Chary rect->freq = 0; 25327d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2533999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2534999342a0SCorentin Chary continue ; 2535999342a0SCorentin Chary } 2536999342a0SCorentin Chary 2537999342a0SCorentin Chary min = rect->times[rect->idx]; 2538999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2539ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2540999342a0SCorentin Chary 2541999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2542999342a0SCorentin Chary rect->freq /= count; 2543999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2544999342a0SCorentin Chary } 2545999342a0SCorentin Chary } 25467d964c9dSCorentin Chary return has_dirty; 2547999342a0SCorentin Chary } 2548999342a0SCorentin Chary 2549999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2550999342a0SCorentin Chary { 2551999342a0SCorentin Chary int i, j; 2552999342a0SCorentin Chary double total = 0; 2553999342a0SCorentin Chary int num = 0; 2554999342a0SCorentin Chary 2555999342a0SCorentin Chary x = (x / VNC_STAT_RECT) * VNC_STAT_RECT; 2556999342a0SCorentin Chary y = (y / VNC_STAT_RECT) * VNC_STAT_RECT; 2557999342a0SCorentin Chary 2558999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2559999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2560999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2561999342a0SCorentin Chary num++; 2562999342a0SCorentin Chary } 2563999342a0SCorentin Chary } 2564999342a0SCorentin Chary 2565999342a0SCorentin Chary if (num) { 2566999342a0SCorentin Chary return total / num; 2567999342a0SCorentin Chary } else { 2568999342a0SCorentin Chary return 0; 2569999342a0SCorentin Chary } 2570999342a0SCorentin Chary } 2571999342a0SCorentin Chary 2572999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2573999342a0SCorentin Chary { 2574999342a0SCorentin Chary VncRectStat *rect; 2575999342a0SCorentin Chary 2576999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2577999342a0SCorentin Chary if (rect->updated) { 2578999342a0SCorentin Chary return ; 2579999342a0SCorentin Chary } 2580999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2581999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2582999342a0SCorentin Chary rect->updated = true; 2583999342a0SCorentin Chary } 2584999342a0SCorentin Chary 25853e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 25863e230dd2SCorentin Chary { 25879f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 25889f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 25893e230dd2SCorentin Chary int y; 25903e230dd2SCorentin Chary uint8_t *guest_row; 25913e230dd2SCorentin Chary uint8_t *server_row; 25923e230dd2SCorentin Chary int cmp_bytes; 25933e230dd2SCorentin Chary VncState *vs; 25943e230dd2SCorentin Chary int has_dirty = 0; 25959f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 25963e230dd2SCorentin Chary 259780e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2598999342a0SCorentin Chary 259980e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2600999342a0SCorentin Chary gettimeofday(&tv, NULL); 26017d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 260280e0c8c3SCorentin Chary } 2603999342a0SCorentin Chary 26043e230dd2SCorentin Chary /* 26053e230dd2SCorentin Chary * Walk through the guest dirty map. 26063e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 26073e230dd2SCorentin Chary * Update server dirty map. 26083e230dd2SCorentin Chary */ 26099f64916dSGerd Hoffmann cmp_bytes = 64; 26109f64916dSGerd Hoffmann if (cmp_bytes > vnc_server_fb_stride(vd)) { 26119f64916dSGerd Hoffmann cmp_bytes = vnc_server_fb_stride(vd); 26129e4dd565SStefan Weil } 26139f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 26149f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 26159f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 26169f64916dSGerd Hoffmann } 26179f64916dSGerd Hoffmann guest_row = (uint8_t *)pixman_image_get_data(vd->guest.fb); 26189f64916dSGerd Hoffmann server_row = (uint8_t *)pixman_image_get_data(vd->server); 26199f64916dSGerd Hoffmann for (y = 0; y < height; y++) { 262023bfe28fSStefan Weil if (!bitmap_empty(vd->guest.dirty[y], VNC_DIRTY_BITS)) { 26213e230dd2SCorentin Chary int x; 26223e230dd2SCorentin Chary uint8_t *guest_ptr; 26233e230dd2SCorentin Chary uint8_t *server_ptr; 26243e230dd2SCorentin Chary 26259f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2626bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 26279f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 26289f64916dSGerd Hoffmann } else { 26293e230dd2SCorentin Chary guest_ptr = guest_row; 26309f64916dSGerd Hoffmann } 26313e230dd2SCorentin Chary server_ptr = server_row; 26323e230dd2SCorentin Chary 26339f64916dSGerd Hoffmann for (x = 0; x + 15 < width; 26343e230dd2SCorentin Chary x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2635bc2429b9SCorentin Chary if (!test_and_clear_bit((x / 16), vd->guest.dirty[y])) 26363e230dd2SCorentin Chary continue; 26373e230dd2SCorentin Chary if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) 26383e230dd2SCorentin Chary continue; 26393e230dd2SCorentin Chary memcpy(server_ptr, guest_ptr, cmp_bytes); 264080e0c8c3SCorentin Chary if (!vd->non_adaptive) 2641999342a0SCorentin Chary vnc_rect_updated(vd, x, y, &tv); 26423e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2643bc2429b9SCorentin Chary set_bit((x / 16), vs->dirty[y]); 26443e230dd2SCorentin Chary } 26453e230dd2SCorentin Chary has_dirty++; 26463e230dd2SCorentin Chary } 26473e230dd2SCorentin Chary } 26489f64916dSGerd Hoffmann guest_row += pixman_image_get_stride(vd->guest.fb); 26499f64916dSGerd Hoffmann server_row += pixman_image_get_stride(vd->server); 26503e230dd2SCorentin Chary } 26519f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 26523e230dd2SCorentin Chary return has_dirty; 26533e230dd2SCorentin Chary } 26543e230dd2SCorentin Chary 26553e230dd2SCorentin Chary static void vnc_refresh(void *opaque) 26563e230dd2SCorentin Chary { 26573e230dd2SCorentin Chary VncDisplay *vd = opaque; 26583e230dd2SCorentin Chary VncState *vs, *vn; 26593e230dd2SCorentin Chary int has_dirty, rects = 0; 26603e230dd2SCorentin Chary 26613e230dd2SCorentin Chary vga_hw_update(); 26623e230dd2SCorentin Chary 2663bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 2664bd023f95SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 26657bd427d8SPaolo Bonzini qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) + 2666bd023f95SCorentin Chary vd->timer_interval); 2667bd023f95SCorentin Chary return; 2668bd023f95SCorentin Chary } 2669bd023f95SCorentin Chary 26703e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2671bd023f95SCorentin Chary vnc_unlock_display(vd); 26723e230dd2SCorentin Chary 26733e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 26743e230dd2SCorentin Chary rects += vnc_update_client(vs, has_dirty); 26753e230dd2SCorentin Chary /* vs might be free()ed here */ 26763e230dd2SCorentin Chary } 2677bd023f95SCorentin Chary 26783e230dd2SCorentin Chary /* vd->timer could be NULL now if the last client disconnected, 26793e230dd2SCorentin Chary * in this case don't update the timer */ 26803e230dd2SCorentin Chary if (vd->timer == NULL) 26813e230dd2SCorentin Chary return; 26823e230dd2SCorentin Chary 26833e230dd2SCorentin Chary if (has_dirty && rects) { 26843e230dd2SCorentin Chary vd->timer_interval /= 2; 26853e230dd2SCorentin Chary if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE) 26863e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 26873e230dd2SCorentin Chary } else { 26883e230dd2SCorentin Chary vd->timer_interval += VNC_REFRESH_INTERVAL_INC; 26893e230dd2SCorentin Chary if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX) 26903e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_MAX; 26913e230dd2SCorentin Chary } 26927bd427d8SPaolo Bonzini qemu_mod_timer(vd->timer, qemu_get_clock_ms(rt_clock) + vd->timer_interval); 26933e230dd2SCorentin Chary } 26943e230dd2SCorentin Chary 26953e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd) 26963e230dd2SCorentin Chary { 26973e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 26983e230dd2SCorentin Chary if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) { 26997bd427d8SPaolo Bonzini vd->timer = qemu_new_timer_ms(rt_clock, vnc_refresh, vd); 2700*d39fa6d8SGerd Hoffmann vga_hw_update(); 27013e230dd2SCorentin Chary vnc_refresh(vd); 27023e230dd2SCorentin Chary } 27033e230dd2SCorentin Chary } 27043e230dd2SCorentin Chary 27053e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd) 27063e230dd2SCorentin Chary { 27073e230dd2SCorentin Chary if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) { 27083e230dd2SCorentin Chary qemu_del_timer(vd->timer); 27093e230dd2SCorentin Chary qemu_free_timer(vd->timer); 27103e230dd2SCorentin Chary vd->timer = NULL; 27113e230dd2SCorentin Chary } 27123e230dd2SCorentin Chary } 27133e230dd2SCorentin Chary 27147536ee4bSTim Hardeck static void vnc_connect(VncDisplay *vd, int csock, int skipauth, bool websocket) 27153e230dd2SCorentin Chary { 27167267c094SAnthony Liguori VncState *vs = g_malloc0(sizeof(VncState)); 27177d964c9dSCorentin Chary int i; 27187d964c9dSCorentin Chary 27193e230dd2SCorentin Chary vs->csock = csock; 27207e7e2ebcSDaniel P. Berrange 27217e7e2ebcSDaniel P. Berrange if (skipauth) { 27227e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 27237e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 27247e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 27257e7e2ebcSDaniel P. Berrange #endif 27267e7e2ebcSDaniel P. Berrange } else { 27277e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 27287e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 27297e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 27307e7e2ebcSDaniel P. Berrange #endif 27317e7e2ebcSDaniel P. Berrange } 27327e7e2ebcSDaniel P. Berrange 27337267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 27347d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 27357267c094SAnthony Liguori vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t)); 27367d964c9dSCorentin Chary } 27373e230dd2SCorentin Chary 27383e230dd2SCorentin Chary VNC_DEBUG("New client on socket %d\n", csock); 273921ef45d7SGerd Hoffmann vd->dcl.idle = 0; 27403e230dd2SCorentin Chary socket_set_nonblock(vs->csock); 27417536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27427536ee4bSTim Hardeck if (websocket) { 27437536ee4bSTim Hardeck vs->websocket = 1; 27447536ee4bSTim Hardeck qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, NULL, vs); 27457536ee4bSTim Hardeck } else 27467536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 27477536ee4bSTim Hardeck { 27483e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 27497536ee4bSTim Hardeck } 27503e230dd2SCorentin Chary 27513e230dd2SCorentin Chary vnc_client_cache_addr(vs); 27523e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_CONNECTED); 27538cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 27543e230dd2SCorentin Chary 27553e230dd2SCorentin Chary vs->vd = vd; 27567536ee4bSTim Hardeck 27577536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27587536ee4bSTim Hardeck if (!vs->websocket) 27597536ee4bSTim Hardeck #endif 27607536ee4bSTim Hardeck { 27617536ee4bSTim Hardeck vnc_init_state(vs); 27627536ee4bSTim Hardeck } 27637536ee4bSTim Hardeck } 27647536ee4bSTim Hardeck 27657536ee4bSTim Hardeck void vnc_init_state(VncState *vs) 27667536ee4bSTim Hardeck { 27676fd8e79aSTim Hardeck vs->initialized = true; 27687536ee4bSTim Hardeck VncDisplay *vd = vs->vd; 27697536ee4bSTim Hardeck 27703e230dd2SCorentin Chary vs->last_x = -1; 27713e230dd2SCorentin Chary vs->last_y = -1; 27723e230dd2SCorentin Chary 27733e230dd2SCorentin Chary vs->as.freq = 44100; 27743e230dd2SCorentin Chary vs->as.nchannels = 2; 27753e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 27763e230dd2SCorentin Chary vs->as.endianness = 0; 27773e230dd2SCorentin Chary 2778bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2779175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2780bd023f95SCorentin Chary 27813e230dd2SCorentin Chary QTAILQ_INSERT_HEAD(&vd->clients, vs, next); 27823e230dd2SCorentin Chary 27833e230dd2SCorentin Chary vga_hw_update(); 27843e230dd2SCorentin Chary 27853e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 27863e230dd2SCorentin Chary vnc_flush(vs); 27873e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 27883e230dd2SCorentin Chary reset_keys(vs); 27893e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 27903e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 27913e230dd2SCorentin Chary 27923e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 27933e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 27943e230dd2SCorentin Chary 27953e230dd2SCorentin Chary vnc_init_timer(vd); 27963e230dd2SCorentin Chary 27973e230dd2SCorentin Chary /* vs might be free()ed here */ 27983e230dd2SCorentin Chary } 27993e230dd2SCorentin Chary 28007536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket) 28013e230dd2SCorentin Chary { 28023e230dd2SCorentin Chary VncDisplay *vs = opaque; 28033e230dd2SCorentin Chary struct sockaddr_in addr; 28043e230dd2SCorentin Chary socklen_t addrlen = sizeof(addr); 28057536ee4bSTim Hardeck int csock; 28063e230dd2SCorentin Chary 28073e230dd2SCorentin Chary /* Catch-up */ 28083e230dd2SCorentin Chary vga_hw_update(); 28097536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28107536ee4bSTim Hardeck if (websocket) { 28117536ee4bSTim Hardeck csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen); 28127536ee4bSTim Hardeck } else 28137536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28147536ee4bSTim Hardeck { 28157536ee4bSTim Hardeck csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); 28167536ee4bSTim Hardeck } 28173e230dd2SCorentin Chary 28183e230dd2SCorentin Chary if (csock != -1) { 28197536ee4bSTim Hardeck vnc_connect(vs, csock, 0, websocket); 28203e230dd2SCorentin Chary } 28213e230dd2SCorentin Chary } 28223e230dd2SCorentin Chary 28237536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque) 28247536ee4bSTim Hardeck { 28257536ee4bSTim Hardeck vnc_listen_read(opaque, 0); 28267536ee4bSTim Hardeck } 28277536ee4bSTim Hardeck 28287536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28297536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque) 28307536ee4bSTim Hardeck { 28317536ee4bSTim Hardeck vnc_listen_read(opaque, 1); 28327536ee4bSTim Hardeck } 28337536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28347536ee4bSTim Hardeck 28357c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 28367c20b4a3SGerd Hoffmann .dpy_name = "vnc", 28377c20b4a3SGerd Hoffmann .dpy_gfx_copy = vnc_dpy_copy, 28387c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 2839c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 28407c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 28417c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 28427c20b4a3SGerd Hoffmann }; 28437c20b4a3SGerd Hoffmann 28443e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds) 28453e230dd2SCorentin Chary { 28467267c094SAnthony Liguori VncDisplay *vs = g_malloc0(sizeof(*vs)); 28473e230dd2SCorentin Chary 284821ef45d7SGerd Hoffmann vs->dcl.idle = 1; 28493e230dd2SCorentin Chary vnc_display = vs; 28503e230dd2SCorentin Chary 28513e230dd2SCorentin Chary vs->lsock = -1; 28527536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28537536ee4bSTim Hardeck vs->lwebsock = -1; 28547536ee4bSTim Hardeck #endif 28553e230dd2SCorentin Chary 28563e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 28573c9405a0SGerd Hoffmann vs->expires = TIME_MAX; 28583e230dd2SCorentin Chary 28593e230dd2SCorentin Chary if (keyboard_layout) 28603e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 28613e230dd2SCorentin Chary else 28623e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 28633e230dd2SCorentin Chary 28643e230dd2SCorentin Chary if (!vs->kbd_layout) 28653e230dd2SCorentin Chary exit(1); 28663e230dd2SCorentin Chary 2867bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 2868bd023f95SCorentin Chary vnc_start_worker_thread(); 2869bd023f95SCorentin Chary 287021ef45d7SGerd Hoffmann vs->dcl.ops = &dcl_ops; 287121ef45d7SGerd Hoffmann register_displaychangelistener(ds, &vs->dcl); 28723e230dd2SCorentin Chary } 28733e230dd2SCorentin Chary 28743e230dd2SCorentin Chary 287571a8cdecSBlue Swirl static void vnc_display_close(DisplayState *ds) 28763e230dd2SCorentin Chary { 287721ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 28783e230dd2SCorentin Chary 28793e230dd2SCorentin Chary if (!vs) 28803e230dd2SCorentin Chary return; 28813e230dd2SCorentin Chary if (vs->display) { 28827267c094SAnthony Liguori g_free(vs->display); 28833e230dd2SCorentin Chary vs->display = NULL; 28843e230dd2SCorentin Chary } 28853e230dd2SCorentin Chary if (vs->lsock != -1) { 28863e230dd2SCorentin Chary qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL); 28873e230dd2SCorentin Chary close(vs->lsock); 28883e230dd2SCorentin Chary vs->lsock = -1; 28893e230dd2SCorentin Chary } 28907536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28917536ee4bSTim Hardeck g_free(vs->ws_display); 28927536ee4bSTim Hardeck vs->ws_display = NULL; 28937536ee4bSTim Hardeck if (vs->lwebsock != -1) { 28947536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL); 28957536ee4bSTim Hardeck close(vs->lwebsock); 28967536ee4bSTim Hardeck vs->lwebsock = -1; 28977536ee4bSTim Hardeck } 28987536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28993e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 29003e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 29013e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 29023e230dd2SCorentin Chary vs->tls.x509verify = 0; 29033e230dd2SCorentin Chary #endif 29043e230dd2SCorentin Chary } 29053e230dd2SCorentin Chary 290671a8cdecSBlue Swirl static int vnc_display_disable_login(DisplayState *ds) 29073e230dd2SCorentin Chary { 290821ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29093e230dd2SCorentin Chary 29103e230dd2SCorentin Chary if (!vs) { 29113e230dd2SCorentin Chary return -1; 29123e230dd2SCorentin Chary } 29133e230dd2SCorentin Chary 29143e230dd2SCorentin Chary if (vs->password) { 29157267c094SAnthony Liguori g_free(vs->password); 29161cd20f8bSAnthony Liguori } 29171cd20f8bSAnthony Liguori 29181cd20f8bSAnthony Liguori vs->password = NULL; 29197dfbfc79SDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 29201cd20f8bSAnthony Liguori vs->auth = VNC_AUTH_VNC; 29217dfbfc79SDaniel P. Berrange } 29221cd20f8bSAnthony Liguori 29231cd20f8bSAnthony Liguori return 0; 29241cd20f8bSAnthony Liguori } 29251cd20f8bSAnthony Liguori 29261cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password) 29271cd20f8bSAnthony Liguori { 292821ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29291cd20f8bSAnthony Liguori 29301cd20f8bSAnthony Liguori if (!vs) { 2931a6aa9d3eSLuiz Capitulino return -EINVAL; 29321cd20f8bSAnthony Liguori } 29331cd20f8bSAnthony Liguori 29341cd20f8bSAnthony Liguori if (!password) { 29351cd20f8bSAnthony Liguori /* This is not the intention of this interface but err on the side 29361cd20f8bSAnthony Liguori of being safe */ 2937a6aa9d3eSLuiz Capitulino return vnc_display_disable_login(ds); 29381cd20f8bSAnthony Liguori } 29391cd20f8bSAnthony Liguori 29401cd20f8bSAnthony Liguori if (vs->password) { 29417267c094SAnthony Liguori g_free(vs->password); 29423e230dd2SCorentin Chary vs->password = NULL; 29433e230dd2SCorentin Chary } 29447267c094SAnthony Liguori vs->password = g_strdup(password); 29457dfbfc79SDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 29463e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 29477dfbfc79SDaniel P. Berrange } 2948a6aa9d3eSLuiz Capitulino 2949a6aa9d3eSLuiz Capitulino return 0; 29503e230dd2SCorentin Chary } 29513e230dd2SCorentin Chary 29523c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires) 29533c9405a0SGerd Hoffmann { 295421ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29553c9405a0SGerd Hoffmann 29561643f2b2SGerd Hoffmann if (!vs) { 29571643f2b2SGerd Hoffmann return -EINVAL; 29581643f2b2SGerd Hoffmann } 29591643f2b2SGerd Hoffmann 29603c9405a0SGerd Hoffmann vs->expires = expires; 29613c9405a0SGerd Hoffmann return 0; 29623c9405a0SGerd Hoffmann } 29633c9405a0SGerd Hoffmann 29643e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds) 29653e230dd2SCorentin Chary { 296621ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29673e230dd2SCorentin Chary 29683e230dd2SCorentin Chary return vnc_socket_local_addr("%s:%s", vs->lsock); 29693e230dd2SCorentin Chary } 29703e230dd2SCorentin Chary 29712d55f0e8SPaolo Bonzini void vnc_display_open(DisplayState *ds, const char *display, Error **errp) 29723e230dd2SCorentin Chary { 297321ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29743e230dd2SCorentin Chary const char *options; 29753e230dd2SCorentin Chary int password = 0; 29763e230dd2SCorentin Chary int reverse = 0; 29773e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 29783e230dd2SCorentin Chary int tls = 0, x509 = 0; 29793e230dd2SCorentin Chary #endif 29803e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 29813e230dd2SCorentin Chary int sasl = 0; 29823e230dd2SCorentin Chary int saslErr; 29833e230dd2SCorentin Chary #endif 29842ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 29853e230dd2SCorentin Chary int acl = 0; 29862ded6ad7SBlue Swirl #endif 29873e230dd2SCorentin Chary int lock_key_sync = 1; 29883e230dd2SCorentin Chary 29892d55f0e8SPaolo Bonzini if (!vnc_display) { 29902d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 29912d55f0e8SPaolo Bonzini return; 29922d55f0e8SPaolo Bonzini } 29933e230dd2SCorentin Chary vnc_display_close(ds); 29943e230dd2SCorentin Chary if (strcmp(display, "none") == 0) 29952d55f0e8SPaolo Bonzini return; 29963e230dd2SCorentin Chary 29971ce52c78SPaolo Bonzini vs->display = g_strdup(display); 29988cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 29993e230dd2SCorentin Chary 30003e230dd2SCorentin Chary options = display; 30013e230dd2SCorentin Chary while ((options = strchr(options, ','))) { 30023e230dd2SCorentin Chary options++; 30033e230dd2SCorentin Chary if (strncmp(options, "password", 8) == 0) { 30040f66998fSPaul Moore if (fips_get_state()) { 30052d55f0e8SPaolo Bonzini error_setg(errp, 30060f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 30070f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 30082d55f0e8SPaolo Bonzini "methods as an alternative"); 30091ce52c78SPaolo Bonzini goto fail; 30100f66998fSPaul Moore } 30113e230dd2SCorentin Chary password = 1; /* Require password auth */ 30123e230dd2SCorentin Chary } else if (strncmp(options, "reverse", 7) == 0) { 30133e230dd2SCorentin Chary reverse = 1; 3014cee8e6adSStefan Hajnoczi } else if (strncmp(options, "no-lock-key-sync", 16) == 0) { 30153e230dd2SCorentin Chary lock_key_sync = 0; 30163e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 30173e230dd2SCorentin Chary } else if (strncmp(options, "sasl", 4) == 0) { 30183e230dd2SCorentin Chary sasl = 1; /* Require SASL auth */ 30193e230dd2SCorentin Chary #endif 30207536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 30217536ee4bSTim Hardeck } else if (strncmp(options, "websocket", 9) == 0) { 30227536ee4bSTim Hardeck char *start, *end; 30237536ee4bSTim Hardeck vs->websocket = 1; 30247536ee4bSTim Hardeck 30257536ee4bSTim Hardeck /* Check for 'websocket=<port>' */ 30267536ee4bSTim Hardeck start = strchr(options, '='); 30277536ee4bSTim Hardeck end = strchr(options, ','); 30287536ee4bSTim Hardeck if (start && (!end || (start < end))) { 30297536ee4bSTim Hardeck int len = end ? end-(start+1) : strlen(start+1); 30307536ee4bSTim Hardeck if (len < 6) { 30317536ee4bSTim Hardeck /* extract the host specification from display */ 30327536ee4bSTim Hardeck char *host = NULL, *port = NULL, *host_end = NULL; 30337536ee4bSTim Hardeck port = g_strndup(start + 1, len); 30347536ee4bSTim Hardeck 30357536ee4bSTim Hardeck /* ipv6 hosts have colons */ 30367536ee4bSTim Hardeck end = strchr(display, ','); 30377536ee4bSTim Hardeck host_end = g_strrstr_len(display, end - display, ":"); 30387536ee4bSTim Hardeck 30397536ee4bSTim Hardeck if (host_end) { 30407536ee4bSTim Hardeck host = g_strndup(display, host_end - display + 1); 30417536ee4bSTim Hardeck } else { 30427536ee4bSTim Hardeck host = g_strndup(":", 1); 30437536ee4bSTim Hardeck } 30447536ee4bSTim Hardeck vs->ws_display = g_strconcat(host, port, NULL); 30457536ee4bSTim Hardeck g_free(host); 30467536ee4bSTim Hardeck g_free(port); 30477536ee4bSTim Hardeck } 30487536ee4bSTim Hardeck } 30497536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 30503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 30513e230dd2SCorentin Chary } else if (strncmp(options, "tls", 3) == 0) { 30523e230dd2SCorentin Chary tls = 1; /* Require TLS */ 30533e230dd2SCorentin Chary } else if (strncmp(options, "x509", 4) == 0) { 30543e230dd2SCorentin Chary char *start, *end; 30553e230dd2SCorentin Chary x509 = 1; /* Require x509 certificates */ 30563e230dd2SCorentin Chary if (strncmp(options, "x509verify", 10) == 0) 30573e230dd2SCorentin Chary vs->tls.x509verify = 1; /* ...and verify client certs */ 30583e230dd2SCorentin Chary 30593e230dd2SCorentin Chary /* Now check for 'x509=/some/path' postfix 30603e230dd2SCorentin Chary * and use that to setup x509 certificate/key paths */ 30613e230dd2SCorentin Chary start = strchr(options, '='); 30623e230dd2SCorentin Chary end = strchr(options, ','); 30633e230dd2SCorentin Chary if (start && (!end || (start < end))) { 30643e230dd2SCorentin Chary int len = end ? end-(start+1) : strlen(start+1); 30657267c094SAnthony Liguori char *path = g_strndup(start + 1, len); 30663e230dd2SCorentin Chary 30673e230dd2SCorentin Chary VNC_DEBUG("Trying certificate path '%s'\n", path); 30683e230dd2SCorentin Chary if (vnc_tls_set_x509_creds_dir(vs, path) < 0) { 30692d55f0e8SPaolo Bonzini error_setg(errp, "Failed to find x509 certificates/keys in %s", path); 30707267c094SAnthony Liguori g_free(path); 30711ce52c78SPaolo Bonzini goto fail; 30723e230dd2SCorentin Chary } 30737267c094SAnthony Liguori g_free(path); 30743e230dd2SCorentin Chary } else { 30752d55f0e8SPaolo Bonzini error_setg(errp, "No certificate path provided"); 30761ce52c78SPaolo Bonzini goto fail; 30773e230dd2SCorentin Chary } 30783e230dd2SCorentin Chary #endif 30792ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 30803e230dd2SCorentin Chary } else if (strncmp(options, "acl", 3) == 0) { 30813e230dd2SCorentin Chary acl = 1; 30822ded6ad7SBlue Swirl #endif 30833e230dd2SCorentin Chary } else if (strncmp(options, "lossy", 5) == 0) { 30843e230dd2SCorentin Chary vs->lossy = true; 30857eff5742SCatalin Patulea } else if (strncmp(options, "non-adaptive", 12) == 0) { 308680e0c8c3SCorentin Chary vs->non_adaptive = true; 30878cf36489SGerd Hoffmann } else if (strncmp(options, "share=", 6) == 0) { 30888cf36489SGerd Hoffmann if (strncmp(options+6, "ignore", 6) == 0) { 30898cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_IGNORE; 30908cf36489SGerd Hoffmann } else if (strncmp(options+6, "allow-exclusive", 15) == 0) { 30918cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 30928cf36489SGerd Hoffmann } else if (strncmp(options+6, "force-shared", 12) == 0) { 30938cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 30948cf36489SGerd Hoffmann } else { 30952d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 30961ce52c78SPaolo Bonzini goto fail; 30978cf36489SGerd Hoffmann } 30983e230dd2SCorentin Chary } 30993e230dd2SCorentin Chary } 31003e230dd2SCorentin Chary 31013e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31023e230dd2SCorentin Chary if (acl && x509 && vs->tls.x509verify) { 31033e230dd2SCorentin Chary if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) { 31043e230dd2SCorentin Chary fprintf(stderr, "Failed to create x509 dname ACL\n"); 31053e230dd2SCorentin Chary exit(1); 31063e230dd2SCorentin Chary } 31073e230dd2SCorentin Chary } 31083e230dd2SCorentin Chary #endif 31093e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31103e230dd2SCorentin Chary if (acl && sasl) { 31113e230dd2SCorentin Chary if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) { 31123e230dd2SCorentin Chary fprintf(stderr, "Failed to create username ACL\n"); 31133e230dd2SCorentin Chary exit(1); 31143e230dd2SCorentin Chary } 31153e230dd2SCorentin Chary } 31163e230dd2SCorentin Chary #endif 31173e230dd2SCorentin Chary 31183e230dd2SCorentin Chary /* 31193e230dd2SCorentin Chary * Combinations we support here: 31203e230dd2SCorentin Chary * 31213e230dd2SCorentin Chary * - no-auth (clear text, no auth) 31223e230dd2SCorentin Chary * - password (clear text, weak auth) 31233e230dd2SCorentin Chary * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI) 31243e230dd2SCorentin Chary * - tls (encrypt, weak anonymous creds, no auth) 31253e230dd2SCorentin Chary * - tls + password (encrypt, weak anonymous creds, weak auth) 31263e230dd2SCorentin Chary * - tls + sasl (encrypt, weak anonymous creds, good auth) 31273e230dd2SCorentin Chary * - tls + x509 (encrypt, good x509 creds, no auth) 31283e230dd2SCorentin Chary * - tls + x509 + password (encrypt, good x509 creds, weak auth) 31293e230dd2SCorentin Chary * - tls + x509 + sasl (encrypt, good x509 creds, good auth) 31303e230dd2SCorentin Chary * 31313e230dd2SCorentin Chary * NB1. TLS is a stackable auth scheme. 31323e230dd2SCorentin Chary * NB2. the x509 schemes have option to validate a client cert dname 31333e230dd2SCorentin Chary */ 31343e230dd2SCorentin Chary if (password) { 31353e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31363e230dd2SCorentin Chary if (tls) { 31373e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31383e230dd2SCorentin Chary if (x509) { 31393e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 31403e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 31413e230dd2SCorentin Chary } else { 31423e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 31433e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 31443e230dd2SCorentin Chary } 31453e230dd2SCorentin Chary } else { 31463e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31473e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with password auth\n"); 31483e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 31493e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31503e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31513e230dd2SCorentin Chary } 31523e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31533e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31543e230dd2SCorentin Chary } else if (sasl) { 31553e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31563e230dd2SCorentin Chary if (tls) { 31573e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31583e230dd2SCorentin Chary if (x509) { 31593e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 31603e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 31613e230dd2SCorentin Chary } else { 31623e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 31633e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 31643e230dd2SCorentin Chary } 31653e230dd2SCorentin Chary } else { 31663e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31673e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with SASL auth\n"); 31683e230dd2SCorentin Chary vs->auth = VNC_AUTH_SASL; 31693e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31703e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31713e230dd2SCorentin Chary } 31723e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31733e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 31743e230dd2SCorentin Chary } else { 31753e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31763e230dd2SCorentin Chary if (tls) { 31773e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31783e230dd2SCorentin Chary if (x509) { 31793e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 31803e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 31813e230dd2SCorentin Chary } else { 31823e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 31833e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 31843e230dd2SCorentin Chary } 31853e230dd2SCorentin Chary } else { 31863e230dd2SCorentin Chary #endif 31873e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with no auth\n"); 31883e230dd2SCorentin Chary vs->auth = VNC_AUTH_NONE; 31893e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31903e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31913e230dd2SCorentin Chary } 31923e230dd2SCorentin Chary #endif 31933e230dd2SCorentin Chary } 31943e230dd2SCorentin Chary 31953e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31963e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 31972d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 31983e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 31991ce52c78SPaolo Bonzini goto fail; 32003e230dd2SCorentin Chary } 32013e230dd2SCorentin Chary #endif 32023e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 32033e230dd2SCorentin Chary 32043e230dd2SCorentin Chary if (reverse) { 32053e230dd2SCorentin Chary /* connect to viewer */ 3206007fcd3eSPaolo Bonzini int csock; 32073e230dd2SCorentin Chary vs->lsock = -1; 32087536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32097536ee4bSTim Hardeck vs->lwebsock = -1; 32107536ee4bSTim Hardeck #endif 3211007fcd3eSPaolo Bonzini if (strncmp(display, "unix:", 5) == 0) { 32122d55f0e8SPaolo Bonzini csock = unix_connect(display+5, errp); 3213007fcd3eSPaolo Bonzini } else { 32142d55f0e8SPaolo Bonzini csock = inet_connect(display, errp); 32153e230dd2SCorentin Chary } 3216007fcd3eSPaolo Bonzini if (csock < 0) { 3217007fcd3eSPaolo Bonzini goto fail; 3218007fcd3eSPaolo Bonzini } 32197536ee4bSTim Hardeck vnc_connect(vs, csock, 0, 0); 32203e230dd2SCorentin Chary } else { 32213e230dd2SCorentin Chary /* listen for connects */ 32223e230dd2SCorentin Chary char *dpy; 32237267c094SAnthony Liguori dpy = g_malloc(256); 32243e230dd2SCorentin Chary if (strncmp(display, "unix:", 5) == 0) { 32253e230dd2SCorentin Chary pstrcpy(dpy, 256, "unix:"); 32262d55f0e8SPaolo Bonzini vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp); 32273e230dd2SCorentin Chary } else { 3228029409e5SAmos Kong vs->lsock = inet_listen(display, dpy, 256, 32292d55f0e8SPaolo Bonzini SOCK_STREAM, 5900, errp); 3230c1c1619cSPaolo Bonzini if (vs->lsock < 0) { 3231ae878b17SStefan Weil g_free(dpy); 32321ce52c78SPaolo Bonzini goto fail; 32331ce52c78SPaolo Bonzini } 32347536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32357536ee4bSTim Hardeck if (vs->websocket) { 32367536ee4bSTim Hardeck if (vs->ws_display) { 32377536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->ws_display, NULL, 256, 32387536ee4bSTim Hardeck SOCK_STREAM, 0, errp); 32397536ee4bSTim Hardeck } else { 32407536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->display, NULL, 256, 32417536ee4bSTim Hardeck SOCK_STREAM, 5700, errp); 32427536ee4bSTim Hardeck } 32437536ee4bSTim Hardeck 32447536ee4bSTim Hardeck if (vs->lwebsock < 0) { 32457536ee4bSTim Hardeck if (vs->lsock) { 32467536ee4bSTim Hardeck close(vs->lsock); 32477536ee4bSTim Hardeck vs->lsock = -1; 32487536ee4bSTim Hardeck } 32497536ee4bSTim Hardeck g_free(dpy); 32507536ee4bSTim Hardeck goto fail; 32517536ee4bSTim Hardeck } 32527536ee4bSTim Hardeck } 32537536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32547536ee4bSTim Hardeck } 3255ae878b17SStefan Weil g_free(vs->display); 32563e230dd2SCorentin Chary vs->display = dpy; 32577536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lsock, NULL, 32587536ee4bSTim Hardeck vnc_listen_regular_read, NULL, vs); 32597536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32607536ee4bSTim Hardeck if (vs->websocket) { 32617536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, 32627536ee4bSTim Hardeck vnc_listen_websocket_read, NULL, vs); 32637536ee4bSTim Hardeck } 32647536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32653e230dd2SCorentin Chary } 32662d55f0e8SPaolo Bonzini return; 32671ce52c78SPaolo Bonzini 32681ce52c78SPaolo Bonzini fail: 32691ce52c78SPaolo Bonzini g_free(vs->display); 32701ce52c78SPaolo Bonzini vs->display = NULL; 32717536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32727536ee4bSTim Hardeck g_free(vs->ws_display); 32737536ee4bSTim Hardeck vs->ws_display = NULL; 32747536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32753e230dd2SCorentin Chary } 327613661089SDaniel P. Berrange 327713661089SDaniel P. Berrange void vnc_display_add_client(DisplayState *ds, int csock, int skipauth) 327813661089SDaniel P. Berrange { 327921ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 328013661089SDaniel P. Berrange 32817536ee4bSTim Hardeck vnc_connect(vs, csock, skipauth, 0); 328213661089SDaniel P. Berrange } 3283