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 370f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 383e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 390f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 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 4233e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 4243e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 4253e230dd2SCorentin Chary int x_position, int y_position, 4263e230dd2SCorentin Chary int w, int h); 4270f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 4283e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 4293e230dd2SCorentin Chary 4307c20b4a3SGerd Hoffmann static void vnc_dpy_update(DisplayChangeListener *dcl, 4317c20b4a3SGerd Hoffmann int x, int y, int w, int h) 4323e230dd2SCorentin Chary { 4333e230dd2SCorentin Chary int i; 43421ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 4353e230dd2SCorentin Chary struct VncSurface *s = &vd->guest; 436d39fa6d8SGerd Hoffmann int width = surface_width(vd->ds); 437d39fa6d8SGerd Hoffmann int height = surface_height(vd->ds); 4383e230dd2SCorentin Chary 4393e230dd2SCorentin Chary h += y; 4403e230dd2SCorentin Chary 4413e230dd2SCorentin Chary /* round x down to ensure the loop only spans one 16-pixel block per, 4423e230dd2SCorentin Chary iteration. otherwise, if (x % 16) != 0, the last iteration may span 4433e230dd2SCorentin Chary two 16-pixel blocks but we only mark the first as dirty 4443e230dd2SCorentin Chary */ 4453e230dd2SCorentin Chary w += (x % 16); 4463e230dd2SCorentin Chary x -= (x % 16); 4473e230dd2SCorentin Chary 4489f64916dSGerd Hoffmann x = MIN(x, width); 4499f64916dSGerd Hoffmann y = MIN(y, height); 4509f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 4519f64916dSGerd Hoffmann h = MIN(h, height); 4523e230dd2SCorentin Chary 4533e230dd2SCorentin Chary for (; y < h; y++) 4543e230dd2SCorentin Chary for (i = 0; i < w; i += 16) 455bc2429b9SCorentin Chary set_bit((x + i) / 16, s->dirty[y]); 4563e230dd2SCorentin Chary } 4573e230dd2SCorentin Chary 4583e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 4593e230dd2SCorentin Chary int32_t encoding) 4603e230dd2SCorentin Chary { 4613e230dd2SCorentin Chary vnc_write_u16(vs, x); 4623e230dd2SCorentin Chary vnc_write_u16(vs, y); 4633e230dd2SCorentin Chary vnc_write_u16(vs, w); 4643e230dd2SCorentin Chary vnc_write_u16(vs, h); 4653e230dd2SCorentin Chary 4663e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 4673e230dd2SCorentin Chary } 4683e230dd2SCorentin Chary 4693e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len) 4703e230dd2SCorentin Chary { 4713e230dd2SCorentin Chary if ((buffer->capacity - buffer->offset) < len) { 4723e230dd2SCorentin Chary buffer->capacity += (len + 1024); 4737267c094SAnthony Liguori buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); 4743e230dd2SCorentin Chary if (buffer->buffer == NULL) { 4753e230dd2SCorentin Chary fprintf(stderr, "vnc: out of memory\n"); 4763e230dd2SCorentin Chary exit(1); 4773e230dd2SCorentin Chary } 4783e230dd2SCorentin Chary } 4793e230dd2SCorentin Chary } 4803e230dd2SCorentin Chary 48171a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer) 4823e230dd2SCorentin Chary { 4833e230dd2SCorentin Chary return buffer->offset == 0; 4843e230dd2SCorentin Chary } 4853e230dd2SCorentin Chary 4867536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer) 4873e230dd2SCorentin Chary { 4883e230dd2SCorentin Chary return buffer->buffer + buffer->offset; 4893e230dd2SCorentin Chary } 4903e230dd2SCorentin Chary 4913e230dd2SCorentin Chary void buffer_reset(Buffer *buffer) 4923e230dd2SCorentin Chary { 4933e230dd2SCorentin Chary buffer->offset = 0; 4943e230dd2SCorentin Chary } 4953e230dd2SCorentin Chary 4963e230dd2SCorentin Chary void buffer_free(Buffer *buffer) 4973e230dd2SCorentin Chary { 4987267c094SAnthony Liguori g_free(buffer->buffer); 4993e230dd2SCorentin Chary buffer->offset = 0; 5003e230dd2SCorentin Chary buffer->capacity = 0; 5013e230dd2SCorentin Chary buffer->buffer = NULL; 5023e230dd2SCorentin Chary } 5033e230dd2SCorentin Chary 5043e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len) 5053e230dd2SCorentin Chary { 5063e230dd2SCorentin Chary memcpy(buffer->buffer + buffer->offset, data, len); 5073e230dd2SCorentin Chary buffer->offset += len; 5083e230dd2SCorentin Chary } 5093e230dd2SCorentin Chary 51032ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len) 51132ed2680STim Hardeck { 51232ed2680STim Hardeck memmove(buf->buffer, buf->buffer + len, 51332ed2680STim Hardeck (buf->offset - len)); 51432ed2680STim Hardeck buf->offset -= len; 51532ed2680STim Hardeck } 51632ed2680STim Hardeck 5173e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 5183e230dd2SCorentin Chary { 519d39fa6d8SGerd Hoffmann DisplaySurface *ds = vs->vd->ds; 5203e230dd2SCorentin Chary 5213e230dd2SCorentin Chary if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 5223e230dd2SCorentin Chary return; 5233e230dd2SCorentin Chary } 524d39fa6d8SGerd Hoffmann if (vs->client_width == surface_width(ds) && 525d39fa6d8SGerd Hoffmann vs->client_height == surface_height(ds)) { 5263e230dd2SCorentin Chary return; 5273e230dd2SCorentin Chary } 528d39fa6d8SGerd Hoffmann vs->client_width = surface_width(ds); 529d39fa6d8SGerd Hoffmann vs->client_height = surface_height(ds); 530bd023f95SCorentin Chary vnc_lock_output(vs); 5313e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 5323e230dd2SCorentin Chary vnc_write_u8(vs, 0); 5333e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 5343e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 5353e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 536bd023f95SCorentin Chary vnc_unlock_output(vs); 5373e230dd2SCorentin Chary vnc_flush(vs); 5383e230dd2SCorentin Chary } 5393e230dd2SCorentin Chary 540bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 541bd023f95SCorentin Chary { 542bd023f95SCorentin Chary VncState *vs; 543bd023f95SCorentin Chary 544bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 545bd023f95SCorentin Chary vnc_lock_output(vs); 546bd023f95SCorentin Chary vs->abort = true; 547bd023f95SCorentin Chary vnc_unlock_output(vs); 548bd023f95SCorentin Chary } 549bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 550bd023f95SCorentin Chary vnc_jobs_join(vs); 551bd023f95SCorentin Chary } 552bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 553bd023f95SCorentin Chary vnc_lock_output(vs); 554bd023f95SCorentin Chary vs->abort = false; 555bd023f95SCorentin Chary vnc_unlock_output(vs); 556bd023f95SCorentin Chary } 557bd023f95SCorentin Chary } 558bd023f95SCorentin Chary 5599f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 5609f64916dSGerd Hoffmann { 5619f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 5629f64916dSGerd Hoffmann } 5639f64916dSGerd Hoffmann 5649f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 5659f64916dSGerd Hoffmann { 5669f64916dSGerd Hoffmann uint8_t *ptr; 5679f64916dSGerd Hoffmann 5689f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 5699f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 5709f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 5719f64916dSGerd Hoffmann return ptr; 5729f64916dSGerd Hoffmann } 5739f64916dSGerd Hoffmann 574c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 575c12aeb86SGerd Hoffmann DisplaySurface *surface) 5763e230dd2SCorentin Chary { 57721ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 5783e230dd2SCorentin Chary VncState *vs; 5793e230dd2SCorentin Chary 580bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 581bd023f95SCorentin Chary 5823e230dd2SCorentin Chary /* server surface */ 5839f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->server); 584d39fa6d8SGerd Hoffmann vd->ds = surface; 5859f64916dSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 586d39fa6d8SGerd Hoffmann surface_width(vd->ds), 587d39fa6d8SGerd Hoffmann surface_height(vd->ds), 5889f64916dSGerd Hoffmann NULL, 0); 5893e230dd2SCorentin Chary 5903e230dd2SCorentin Chary /* guest surface */ 5919f64916dSGerd Hoffmann #if 0 /* FIXME */ 5923e230dd2SCorentin Chary if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel) 5933e230dd2SCorentin Chary console_color_init(ds); 5949f64916dSGerd Hoffmann #endif 5959f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 596d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 597d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 5983e230dd2SCorentin Chary memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty)); 5993e230dd2SCorentin Chary 6003e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 6013e230dd2SCorentin Chary vnc_colordepth(vs); 6023e230dd2SCorentin Chary vnc_desktop_resize(vs); 6033e230dd2SCorentin Chary if (vs->vd->cursor) { 6043e230dd2SCorentin Chary vnc_cursor_define(vs); 6053e230dd2SCorentin Chary } 6063e230dd2SCorentin Chary memset(vs->dirty, 0xFF, sizeof(vs->dirty)); 6073e230dd2SCorentin Chary } 6083e230dd2SCorentin Chary } 6093e230dd2SCorentin Chary 6103e230dd2SCorentin Chary /* fastest code */ 6119f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 6123e230dd2SCorentin Chary void *pixels, int size) 6133e230dd2SCorentin Chary { 6143e230dd2SCorentin Chary vnc_write(vs, pixels, size); 6153e230dd2SCorentin Chary } 6163e230dd2SCorentin Chary 6173e230dd2SCorentin Chary /* slowest but generic code. */ 6183e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 6193e230dd2SCorentin Chary { 6203e230dd2SCorentin Chary uint8_t r, g, b; 6213e230dd2SCorentin Chary 6229f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 6239f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 6249f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 6259f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 6269f64916dSGerd Hoffmann #else 6279f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 6289f64916dSGerd Hoffmann #endif 6299f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 6309f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 6319f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 6329f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 6333e230dd2SCorentin Chary case 1: 6343e230dd2SCorentin Chary buf[0] = v; 6353e230dd2SCorentin Chary break; 6363e230dd2SCorentin Chary case 2: 6379f64916dSGerd Hoffmann if (vs->client_be) { 6383e230dd2SCorentin Chary buf[0] = v >> 8; 6393e230dd2SCorentin Chary buf[1] = v; 6403e230dd2SCorentin Chary } else { 6413e230dd2SCorentin Chary buf[1] = v >> 8; 6423e230dd2SCorentin Chary buf[0] = v; 6433e230dd2SCorentin Chary } 6443e230dd2SCorentin Chary break; 6453e230dd2SCorentin Chary default: 6463e230dd2SCorentin Chary case 4: 6479f64916dSGerd Hoffmann if (vs->client_be) { 6483e230dd2SCorentin Chary buf[0] = v >> 24; 6493e230dd2SCorentin Chary buf[1] = v >> 16; 6503e230dd2SCorentin Chary buf[2] = v >> 8; 6513e230dd2SCorentin Chary buf[3] = v; 6523e230dd2SCorentin Chary } else { 6533e230dd2SCorentin Chary buf[3] = v >> 24; 6543e230dd2SCorentin Chary buf[2] = v >> 16; 6553e230dd2SCorentin Chary buf[1] = v >> 8; 6563e230dd2SCorentin Chary buf[0] = v; 6573e230dd2SCorentin Chary } 6583e230dd2SCorentin Chary break; 6593e230dd2SCorentin Chary } 6603e230dd2SCorentin Chary } 6613e230dd2SCorentin Chary 6629f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 6633e230dd2SCorentin Chary void *pixels1, int size) 6643e230dd2SCorentin Chary { 6653e230dd2SCorentin Chary uint8_t buf[4]; 6663e230dd2SCorentin Chary 6679f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 6683e230dd2SCorentin Chary uint32_t *pixels = pixels1; 6693e230dd2SCorentin Chary int n, i; 6703e230dd2SCorentin Chary n = size >> 2; 6713e230dd2SCorentin Chary for (i = 0; i < n; i++) { 6723e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6739f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 6743e230dd2SCorentin Chary } 6753e230dd2SCorentin Chary } 6763e230dd2SCorentin Chary } 6773e230dd2SCorentin Chary 6783e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6793e230dd2SCorentin Chary { 6803e230dd2SCorentin Chary int i; 6813e230dd2SCorentin Chary uint8_t *row; 6823e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 6833e230dd2SCorentin Chary 6849f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 6853e230dd2SCorentin Chary for (i = 0; i < h; i++) { 6869f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 6879f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 6883e230dd2SCorentin Chary } 6893e230dd2SCorentin Chary return 1; 6903e230dd2SCorentin Chary } 6913e230dd2SCorentin Chary 692bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6933e230dd2SCorentin Chary { 6943e230dd2SCorentin Chary int n = 0; 6953e230dd2SCorentin Chary 6963e230dd2SCorentin Chary switch(vs->vnc_encoding) { 6973e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 6983e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 6993e230dd2SCorentin Chary break; 7003e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 7013e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 7023e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 7033e230dd2SCorentin Chary break; 7043e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 7053e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 7063e230dd2SCorentin Chary break; 707efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 708efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 709efe556adSCorentin Chary break; 710148954faSCorentin Chary case VNC_ENCODING_ZRLE: 711148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 712148954faSCorentin Chary break; 713148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 714148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 715148954faSCorentin Chary break; 7163e230dd2SCorentin Chary default: 7173e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 7183e230dd2SCorentin Chary n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 7193e230dd2SCorentin Chary break; 7203e230dd2SCorentin Chary } 7213e230dd2SCorentin Chary return n; 7223e230dd2SCorentin Chary } 7233e230dd2SCorentin Chary 7243e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 7253e230dd2SCorentin Chary { 7263e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 727bd023f95SCorentin Chary vnc_lock_output(vs); 7283e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 7293e230dd2SCorentin Chary vnc_write_u8(vs, 0); 7303e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 7313e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 7323e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 7333e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 734bd023f95SCorentin Chary vnc_unlock_output(vs); 7353e230dd2SCorentin Chary vnc_flush(vs); 7363e230dd2SCorentin Chary } 7373e230dd2SCorentin Chary 7387c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl, 7397c20b4a3SGerd Hoffmann int src_x, int src_y, 7407c20b4a3SGerd Hoffmann int dst_x, int dst_y, int w, int h) 7413e230dd2SCorentin Chary { 74221ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7433e230dd2SCorentin Chary VncState *vs, *vn; 7443e230dd2SCorentin Chary uint8_t *src_row; 7453e230dd2SCorentin Chary uint8_t *dst_row; 7469f64916dSGerd Hoffmann int i, x, y, pitch, inc, w_lim, s; 7473e230dd2SCorentin Chary int cmp_bytes; 7483e230dd2SCorentin Chary 7493e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 7503e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 7513e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7523e230dd2SCorentin Chary vs->force_update = 1; 753bd023f95SCorentin Chary vnc_update_client_sync(vs, 1); 7543e230dd2SCorentin Chary /* vs might be free()ed here */ 7553e230dd2SCorentin Chary } 7563e230dd2SCorentin Chary } 7573e230dd2SCorentin Chary 7583e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 7599f64916dSGerd Hoffmann pitch = vnc_server_fb_stride(vd); 7609f64916dSGerd Hoffmann src_row = vnc_server_fb_ptr(vd, src_x, src_y); 7619f64916dSGerd Hoffmann dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y); 7623e230dd2SCorentin Chary y = dst_y; 7633e230dd2SCorentin Chary inc = 1; 7643e230dd2SCorentin Chary if (dst_y > src_y) { 7653e230dd2SCorentin Chary /* copy backwards */ 7663e230dd2SCorentin Chary src_row += pitch * (h-1); 7673e230dd2SCorentin Chary dst_row += pitch * (h-1); 7683e230dd2SCorentin Chary pitch = -pitch; 7693e230dd2SCorentin Chary y = dst_y + h - 1; 7703e230dd2SCorentin Chary inc = -1; 7713e230dd2SCorentin Chary } 7723e230dd2SCorentin Chary w_lim = w - (16 - (dst_x % 16)); 7733e230dd2SCorentin Chary if (w_lim < 0) 7743e230dd2SCorentin Chary w_lim = w; 7753e230dd2SCorentin Chary else 7763e230dd2SCorentin Chary w_lim = w - (w_lim % 16); 7773e230dd2SCorentin Chary for (i = 0; i < h; i++) { 7783e230dd2SCorentin Chary for (x = 0; x <= w_lim; 7793e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 7803e230dd2SCorentin Chary if (x == w_lim) { 7813e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 7823e230dd2SCorentin Chary break; 7833e230dd2SCorentin Chary } else if (!x) { 7843e230dd2SCorentin Chary s = (16 - (dst_x % 16)); 7853e230dd2SCorentin Chary s = MIN(s, w_lim); 7863e230dd2SCorentin Chary } else { 7873e230dd2SCorentin Chary s = 16; 7883e230dd2SCorentin Chary } 7899f64916dSGerd Hoffmann cmp_bytes = s * VNC_SERVER_FB_BYTES; 7903e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 7913e230dd2SCorentin Chary continue; 7923e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 7933e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7943e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 795bc2429b9SCorentin Chary set_bit(((x + dst_x) / 16), vs->dirty[y]); 7963e230dd2SCorentin Chary } 7973e230dd2SCorentin Chary } 7983e230dd2SCorentin Chary } 7999f64916dSGerd Hoffmann src_row += pitch - w * VNC_SERVER_FB_BYTES; 8009f64916dSGerd Hoffmann dst_row += pitch - w * VNC_SERVER_FB_BYTES; 8013e230dd2SCorentin Chary y += inc; 8023e230dd2SCorentin Chary } 8033e230dd2SCorentin Chary 8043e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8053e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 8063e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 8073e230dd2SCorentin Chary } 8083e230dd2SCorentin Chary } 8093e230dd2SCorentin Chary } 8103e230dd2SCorentin Chary 8117c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 8127c20b4a3SGerd Hoffmann int x, int y, int visible) 8133e230dd2SCorentin Chary { 8143e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 8153e230dd2SCorentin Chary } 8163e230dd2SCorentin Chary 8173e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 8183e230dd2SCorentin Chary { 8193e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 8203e230dd2SCorentin Chary int isize; 8213e230dd2SCorentin Chary 8223e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 823d01f9595SCorentin Chary vnc_lock_output(vs); 8243e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8253e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 8263e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 8273e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 8283e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 8299f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 8309f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 8313e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 832d01f9595SCorentin Chary vnc_unlock_output(vs); 8333e230dd2SCorentin Chary return 0; 8343e230dd2SCorentin Chary } 8353e230dd2SCorentin Chary return -1; 8363e230dd2SCorentin Chary } 8373e230dd2SCorentin Chary 8387c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 8397c20b4a3SGerd Hoffmann QEMUCursor *c) 8403e230dd2SCorentin Chary { 8413e230dd2SCorentin Chary VncDisplay *vd = vnc_display; 8423e230dd2SCorentin Chary VncState *vs; 8433e230dd2SCorentin Chary 8443e230dd2SCorentin Chary cursor_put(vd->cursor); 8457267c094SAnthony Liguori g_free(vd->cursor_mask); 8463e230dd2SCorentin Chary 8473e230dd2SCorentin Chary vd->cursor = c; 8483e230dd2SCorentin Chary cursor_get(vd->cursor); 8493e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 8507267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 8513e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 8523e230dd2SCorentin Chary 8533e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8543e230dd2SCorentin Chary vnc_cursor_define(vs); 8553e230dd2SCorentin Chary } 8563e230dd2SCorentin Chary } 8573e230dd2SCorentin Chary 8583e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs, 8596c71a539SCorentin Chary int y, int last_x, int x, int height) 8603e230dd2SCorentin Chary { 8613e230dd2SCorentin Chary int h; 8623e230dd2SCorentin Chary 8636c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 8643e230dd2SCorentin Chary int tmp_x; 865bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 8663e230dd2SCorentin Chary break; 867bc2429b9SCorentin Chary } 868bc2429b9SCorentin Chary for (tmp_x = last_x; tmp_x < x; tmp_x++) { 869bc2429b9SCorentin Chary clear_bit(tmp_x, vs->dirty[y + h]); 870bc2429b9SCorentin Chary } 8713e230dd2SCorentin Chary } 8723e230dd2SCorentin Chary 8733e230dd2SCorentin Chary return h; 8743e230dd2SCorentin Chary } 8753e230dd2SCorentin Chary 876bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty) 877bd023f95SCorentin Chary { 878bd023f95SCorentin Chary int ret = vnc_update_client(vs, has_dirty); 879bd023f95SCorentin Chary vnc_jobs_join(vs); 880bd023f95SCorentin Chary return ret; 881bd023f95SCorentin Chary } 882bd023f95SCorentin Chary 8833e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty) 8843e230dd2SCorentin Chary { 8853e230dd2SCorentin Chary if (vs->need_update && vs->csock != -1) { 8863e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 887bd023f95SCorentin Chary VncJob *job; 8883e230dd2SCorentin Chary int y; 8893e230dd2SCorentin Chary int width, height; 890bd023f95SCorentin Chary int n = 0; 891bd023f95SCorentin Chary 8923e230dd2SCorentin Chary 8933e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 8943e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 8953e230dd2SCorentin Chary return 0; 8963e230dd2SCorentin Chary 8973e230dd2SCorentin Chary if (!has_dirty && !vs->audio_cap && !vs->force_update) 8983e230dd2SCorentin Chary return 0; 8993e230dd2SCorentin Chary 9003e230dd2SCorentin Chary /* 9013e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9023e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9033e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9043e230dd2SCorentin Chary * send them to the client. 9053e230dd2SCorentin Chary */ 906bd023f95SCorentin Chary job = vnc_job_new(vs); 9073e230dd2SCorentin Chary 9089f64916dSGerd Hoffmann width = MIN(pixman_image_get_width(vd->server), vs->client_width); 9099f64916dSGerd Hoffmann height = MIN(pixman_image_get_height(vd->server), vs->client_height); 9103e230dd2SCorentin Chary 9113e230dd2SCorentin Chary for (y = 0; y < height; y++) { 9123e230dd2SCorentin Chary int x; 9133e230dd2SCorentin Chary int last_x = -1; 9143e230dd2SCorentin Chary for (x = 0; x < width / 16; x++) { 915bc2429b9SCorentin Chary if (test_and_clear_bit(x, vs->dirty[y])) { 9163e230dd2SCorentin Chary if (last_x == -1) { 9173e230dd2SCorentin Chary last_x = x; 9183e230dd2SCorentin Chary } 9193e230dd2SCorentin Chary } else { 9203e230dd2SCorentin Chary if (last_x != -1) { 9216c71a539SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x, 9226c71a539SCorentin Chary height); 923bd023f95SCorentin Chary 924bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9253e230dd2SCorentin Chary (x - last_x) * 16, h); 9263e230dd2SCorentin Chary } 9273e230dd2SCorentin Chary last_x = -1; 9283e230dd2SCorentin Chary } 9293e230dd2SCorentin Chary } 9303e230dd2SCorentin Chary if (last_x != -1) { 9316c71a539SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x, height); 932bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9333e230dd2SCorentin Chary (x - last_x) * 16, h); 9343e230dd2SCorentin Chary } 9353e230dd2SCorentin Chary } 936bd023f95SCorentin Chary 937bd023f95SCorentin Chary vnc_job_push(job); 9383e230dd2SCorentin Chary vs->force_update = 0; 939bd023f95SCorentin Chary return n; 9403e230dd2SCorentin Chary } 9413e230dd2SCorentin Chary 9423e230dd2SCorentin Chary if (vs->csock == -1) 9433e230dd2SCorentin Chary vnc_disconnect_finish(vs); 9443e230dd2SCorentin Chary 9453e230dd2SCorentin Chary return 0; 9463e230dd2SCorentin Chary } 9473e230dd2SCorentin Chary 9483e230dd2SCorentin Chary /* audio */ 9493e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 9503e230dd2SCorentin Chary { 9513e230dd2SCorentin Chary VncState *vs = opaque; 9523e230dd2SCorentin Chary 9533e230dd2SCorentin Chary switch (cmd) { 9543e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 955bd023f95SCorentin Chary vnc_lock_output(vs); 9563e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9573e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9583e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 959bd023f95SCorentin Chary vnc_unlock_output(vs); 9603e230dd2SCorentin Chary vnc_flush(vs); 9613e230dd2SCorentin Chary break; 9623e230dd2SCorentin Chary 9633e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 964bd023f95SCorentin Chary vnc_lock_output(vs); 9653e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9663e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9673e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 968bd023f95SCorentin Chary vnc_unlock_output(vs); 9693e230dd2SCorentin Chary vnc_flush(vs); 9703e230dd2SCorentin Chary break; 9713e230dd2SCorentin Chary } 9723e230dd2SCorentin Chary } 9733e230dd2SCorentin Chary 9743e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 9753e230dd2SCorentin Chary { 9763e230dd2SCorentin Chary } 9773e230dd2SCorentin Chary 9783e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 9793e230dd2SCorentin Chary { 9803e230dd2SCorentin Chary VncState *vs = opaque; 9813e230dd2SCorentin Chary 982bd023f95SCorentin Chary vnc_lock_output(vs); 9833e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9843e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9853e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 9863e230dd2SCorentin Chary vnc_write_u32(vs, size); 9873e230dd2SCorentin Chary vnc_write(vs, buf, size); 988bd023f95SCorentin Chary vnc_unlock_output(vs); 9893e230dd2SCorentin Chary vnc_flush(vs); 9903e230dd2SCorentin Chary } 9913e230dd2SCorentin Chary 9923e230dd2SCorentin Chary static void audio_add(VncState *vs) 9933e230dd2SCorentin Chary { 9943e230dd2SCorentin Chary struct audio_capture_ops ops; 9953e230dd2SCorentin Chary 9963e230dd2SCorentin Chary if (vs->audio_cap) { 9973e230dd2SCorentin Chary monitor_printf(default_mon, "audio already running\n"); 9983e230dd2SCorentin Chary return; 9993e230dd2SCorentin Chary } 10003e230dd2SCorentin Chary 10013e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10023e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 10033e230dd2SCorentin Chary ops.capture = audio_capture; 10043e230dd2SCorentin Chary 10053e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 10063e230dd2SCorentin Chary if (!vs->audio_cap) { 10073e230dd2SCorentin Chary monitor_printf(default_mon, "Failed to add audio capture\n"); 10083e230dd2SCorentin Chary } 10093e230dd2SCorentin Chary } 10103e230dd2SCorentin Chary 10113e230dd2SCorentin Chary static void audio_del(VncState *vs) 10123e230dd2SCorentin Chary { 10133e230dd2SCorentin Chary if (vs->audio_cap) { 10143e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 10153e230dd2SCorentin Chary vs->audio_cap = NULL; 10163e230dd2SCorentin Chary } 10173e230dd2SCorentin Chary } 10183e230dd2SCorentin Chary 10193e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 10203e230dd2SCorentin Chary { 10213e230dd2SCorentin Chary if (vs->csock == -1) 10223e230dd2SCorentin Chary return; 10238cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 10243e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); 10253e230dd2SCorentin Chary closesocket(vs->csock); 10263e230dd2SCorentin Chary vs->csock = -1; 10273e230dd2SCorentin Chary } 10283e230dd2SCorentin Chary 10297536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 10303e230dd2SCorentin Chary { 10317d964c9dSCorentin Chary int i; 10327d964c9dSCorentin Chary 1033bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1034bd023f95SCorentin Chary 1035bd023f95SCorentin Chary vnc_lock_output(vs); 10363e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED); 10373e230dd2SCorentin Chary 10383e230dd2SCorentin Chary buffer_free(&vs->input); 10393e230dd2SCorentin Chary buffer_free(&vs->output); 10407536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 10417536ee4bSTim Hardeck buffer_free(&vs->ws_input); 10427536ee4bSTim Hardeck buffer_free(&vs->ws_output); 10437536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 10443e230dd2SCorentin Chary 10453e230dd2SCorentin Chary qobject_decref(vs->info); 10463e230dd2SCorentin Chary 10473e230dd2SCorentin Chary vnc_zlib_clear(vs); 10483e230dd2SCorentin Chary vnc_tight_clear(vs); 1049148954faSCorentin Chary vnc_zrle_clear(vs); 10503e230dd2SCorentin Chary 10513e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 10523e230dd2SCorentin Chary vnc_tls_client_cleanup(vs); 10533e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 10543e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 10553e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 10563e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 10573e230dd2SCorentin Chary audio_del(vs); 10587bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 10593e230dd2SCorentin Chary 10606fd8e79aSTim Hardeck if (vs->initialized) { 10613e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 10626fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 10636fd8e79aSTim Hardeck } 10643e230dd2SCorentin Chary 10653e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 10663e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1067bd023f95SCorentin Chary vnc_unlock_output(vs); 1068bd023f95SCorentin Chary 1069bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 10706fd8e79aSTim Hardeck if (vs->bh != NULL) { 1071175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 10726fd8e79aSTim Hardeck } 1073175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1074175b2a6eSCorentin Chary 10757d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 10767267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 10777d964c9dSCorentin Chary } 10787267c094SAnthony Liguori g_free(vs->lossy_rect); 10797267c094SAnthony Liguori g_free(vs); 10803e230dd2SCorentin Chary } 10813e230dd2SCorentin Chary 10823e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno) 10833e230dd2SCorentin Chary { 10843e230dd2SCorentin Chary if (ret == 0 || ret == -1) { 10853e230dd2SCorentin Chary if (ret == -1) { 10863e230dd2SCorentin Chary switch (last_errno) { 10873e230dd2SCorentin Chary case EINTR: 10883e230dd2SCorentin Chary case EAGAIN: 10893e230dd2SCorentin Chary #ifdef _WIN32 10903e230dd2SCorentin Chary case WSAEWOULDBLOCK: 10913e230dd2SCorentin Chary #endif 10923e230dd2SCorentin Chary return 0; 10933e230dd2SCorentin Chary default: 10943e230dd2SCorentin Chary break; 10953e230dd2SCorentin Chary } 10963e230dd2SCorentin Chary } 10973e230dd2SCorentin Chary 10983e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: ret %d, errno %d\n", 10993e230dd2SCorentin Chary ret, ret < 0 ? last_errno : 0); 11003e230dd2SCorentin Chary vnc_disconnect_start(vs); 11013e230dd2SCorentin Chary 11023e230dd2SCorentin Chary return 0; 11033e230dd2SCorentin Chary } 11043e230dd2SCorentin Chary return ret; 11053e230dd2SCorentin Chary } 11063e230dd2SCorentin Chary 11073e230dd2SCorentin Chary 11083e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 11093e230dd2SCorentin Chary { 11103e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 11113e230dd2SCorentin Chary vnc_disconnect_start(vs); 11123e230dd2SCorentin Chary } 11133e230dd2SCorentin Chary 11143e230dd2SCorentin Chary 11153e230dd2SCorentin Chary /* 11163e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 11173e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 11183e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 11193e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 11203e230dd2SCorentin Chary * 11213e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 11223e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 11233e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 11243e230dd2SCorentin Chary * is active 11253e230dd2SCorentin Chary * 11263e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11273e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 11283e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11293e230dd2SCorentin Chary */ 11303e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 11313e230dd2SCorentin Chary { 11323e230dd2SCorentin Chary long ret; 11333e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 11343e230dd2SCorentin Chary if (vs->tls.session) { 11353e230dd2SCorentin Chary ret = gnutls_write(vs->tls.session, data, datalen); 11363e230dd2SCorentin Chary if (ret < 0) { 11373e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 11383e230dd2SCorentin Chary errno = EAGAIN; 11393e230dd2SCorentin Chary else 11403e230dd2SCorentin Chary errno = EIO; 11413e230dd2SCorentin Chary ret = -1; 11423e230dd2SCorentin Chary } 11433e230dd2SCorentin Chary } else 11443e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 11453e230dd2SCorentin Chary ret = send(vs->csock, (const void *)data, datalen, 0); 11463e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 11473e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 11483e230dd2SCorentin Chary } 11493e230dd2SCorentin Chary 11503e230dd2SCorentin Chary 11513e230dd2SCorentin Chary /* 11523e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 11533e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 11543e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 11553e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 11563e230dd2SCorentin Chary * 11573e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11583e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 11593e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11603e230dd2SCorentin Chary */ 11613e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs) 11623e230dd2SCorentin Chary { 11633e230dd2SCorentin Chary long ret; 11643e230dd2SCorentin Chary 11653e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11663e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 11673e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 11683e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 11693e230dd2SCorentin Chary 11703e230dd2SCorentin Chary if (vs->sasl.conn && 11713e230dd2SCorentin Chary vs->sasl.runSSF && 11723e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 11733e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 11743e230dd2SCorentin Chary if (ret) 11753e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 11763e230dd2SCorentin Chary } else 11773e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11783e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 11793e230dd2SCorentin Chary if (!ret) 11803e230dd2SCorentin Chary return 0; 11813e230dd2SCorentin Chary 118232ed2680STim Hardeck buffer_advance(&vs->output, ret); 11833e230dd2SCorentin Chary 11843e230dd2SCorentin Chary if (vs->output.offset == 0) { 11853e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 11863e230dd2SCorentin Chary } 11873e230dd2SCorentin Chary 11883e230dd2SCorentin Chary return ret; 11893e230dd2SCorentin Chary } 11903e230dd2SCorentin Chary 11913e230dd2SCorentin Chary 11923e230dd2SCorentin Chary /* 11933e230dd2SCorentin Chary * First function called whenever there is data to be written to 11943e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 11953e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 11963e230dd2SCorentin Chary */ 1197bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque) 11983e230dd2SCorentin Chary { 11993e230dd2SCorentin Chary VncState *vs = opaque; 12003e230dd2SCorentin Chary 12013e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12023e230dd2SCorentin Chary if (vs->sasl.conn && 12033e230dd2SCorentin Chary vs->sasl.runSSF && 12043e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12053e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12063e230dd2SCorentin Chary } else 12073e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12087536ee4bSTim Hardeck { 12097536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12107536ee4bSTim Hardeck if (vs->encode_ws) { 12117536ee4bSTim Hardeck vnc_client_write_ws(vs); 12127536ee4bSTim Hardeck } else 12137536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 12147536ee4bSTim Hardeck { 12153e230dd2SCorentin Chary vnc_client_write_plain(vs); 12163e230dd2SCorentin Chary } 12177536ee4bSTim Hardeck } 12187536ee4bSTim Hardeck } 12193e230dd2SCorentin Chary 1220bd023f95SCorentin Chary void vnc_client_write(void *opaque) 1221bd023f95SCorentin Chary { 1222bd023f95SCorentin Chary VncState *vs = opaque; 1223bd023f95SCorentin Chary 1224bd023f95SCorentin Chary vnc_lock_output(vs); 12257536ee4bSTim Hardeck if (vs->output.offset 12267536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12277536ee4bSTim Hardeck || vs->ws_output.offset 12287536ee4bSTim Hardeck #endif 12297536ee4bSTim Hardeck ) { 1230bd023f95SCorentin Chary vnc_client_write_locked(opaque); 1231ac71103dSYoshiaki Tamura } else if (vs->csock != -1) { 1232bd023f95SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 1233bd023f95SCorentin Chary } 1234bd023f95SCorentin Chary vnc_unlock_output(vs); 1235bd023f95SCorentin Chary } 1236bd023f95SCorentin Chary 12373e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 12383e230dd2SCorentin Chary { 12393e230dd2SCorentin Chary vs->read_handler = func; 12403e230dd2SCorentin Chary vs->read_handler_expect = expecting; 12413e230dd2SCorentin Chary } 12423e230dd2SCorentin Chary 12433e230dd2SCorentin Chary 12443e230dd2SCorentin Chary /* 12453e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 12463e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 12473e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 12483e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12493e230dd2SCorentin Chary * 12503e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12513e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12523e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12533e230dd2SCorentin Chary * is active 12543e230dd2SCorentin Chary * 12553e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 12563e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12573e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12583e230dd2SCorentin Chary */ 12593e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 12603e230dd2SCorentin Chary { 12613e230dd2SCorentin Chary long ret; 12623e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 12633e230dd2SCorentin Chary if (vs->tls.session) { 12643e230dd2SCorentin Chary ret = gnutls_read(vs->tls.session, data, datalen); 12653e230dd2SCorentin Chary if (ret < 0) { 12663e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 12673e230dd2SCorentin Chary errno = EAGAIN; 12683e230dd2SCorentin Chary else 12693e230dd2SCorentin Chary errno = EIO; 12703e230dd2SCorentin Chary ret = -1; 12713e230dd2SCorentin Chary } 12723e230dd2SCorentin Chary } else 12733e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 127400aa0040SBlue Swirl ret = qemu_recv(vs->csock, data, datalen, 0); 12753e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 12763e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 12773e230dd2SCorentin Chary } 12783e230dd2SCorentin Chary 12793e230dd2SCorentin Chary 12803e230dd2SCorentin Chary /* 12813e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 12823e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 12833e230dd2SCorentin Chary * data as possible without blocking. 12843e230dd2SCorentin Chary * 12853e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 12863e230dd2SCorentin Chary * disconnects the client socket. 12873e230dd2SCorentin Chary */ 12883e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs) 12893e230dd2SCorentin Chary { 12903e230dd2SCorentin Chary int ret; 12913e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 12923e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 12933e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 12943e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 12953e230dd2SCorentin Chary if (!ret) 12963e230dd2SCorentin Chary return 0; 12973e230dd2SCorentin Chary vs->input.offset += ret; 12983e230dd2SCorentin Chary return ret; 12993e230dd2SCorentin Chary } 13003e230dd2SCorentin Chary 1301175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1302175b2a6eSCorentin Chary { 1303175b2a6eSCorentin Chary VncState *vs = opaque; 1304175b2a6eSCorentin Chary 1305175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1306175b2a6eSCorentin Chary } 13073e230dd2SCorentin Chary 13083e230dd2SCorentin Chary /* 13093e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13103e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13113e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 13123e230dd2SCorentin Chary */ 13133e230dd2SCorentin Chary void vnc_client_read(void *opaque) 13143e230dd2SCorentin Chary { 13153e230dd2SCorentin Chary VncState *vs = opaque; 13163e230dd2SCorentin Chary long ret; 13173e230dd2SCorentin Chary 13183e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13193e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13203e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13213e230dd2SCorentin Chary else 13223e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13237536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 13247536ee4bSTim Hardeck if (vs->encode_ws) { 13257536ee4bSTim Hardeck ret = vnc_client_read_ws(vs); 13267536ee4bSTim Hardeck if (ret == -1) { 13277536ee4bSTim Hardeck vnc_disconnect_start(vs); 13287536ee4bSTim Hardeck return; 13297536ee4bSTim Hardeck } else if (ret == -2) { 13307536ee4bSTim Hardeck vnc_client_error(vs); 13317536ee4bSTim Hardeck return; 13327536ee4bSTim Hardeck } 13337536ee4bSTim Hardeck } else 13347536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 13357536ee4bSTim Hardeck { 13363e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13377536ee4bSTim Hardeck } 13383e230dd2SCorentin Chary if (!ret) { 13393e230dd2SCorentin Chary if (vs->csock == -1) 13403e230dd2SCorentin Chary vnc_disconnect_finish(vs); 13413e230dd2SCorentin Chary return; 13423e230dd2SCorentin Chary } 13433e230dd2SCorentin Chary 13443e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 13453e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 13463e230dd2SCorentin Chary int ret; 13473e230dd2SCorentin Chary 13483e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 13493e230dd2SCorentin Chary if (vs->csock == -1) { 13503e230dd2SCorentin Chary vnc_disconnect_finish(vs); 13513e230dd2SCorentin Chary return; 13523e230dd2SCorentin Chary } 13533e230dd2SCorentin Chary 13543e230dd2SCorentin Chary if (!ret) { 135532ed2680STim Hardeck buffer_advance(&vs->input, len); 13563e230dd2SCorentin Chary } else { 13573e230dd2SCorentin Chary vs->read_handler_expect = ret; 13583e230dd2SCorentin Chary } 13593e230dd2SCorentin Chary } 13603e230dd2SCorentin Chary } 13613e230dd2SCorentin Chary 13623e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 13633e230dd2SCorentin Chary { 13643e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 13653e230dd2SCorentin Chary 13663e230dd2SCorentin Chary if (vs->csock != -1 && buffer_empty(&vs->output)) { 13673e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); 13683e230dd2SCorentin Chary } 13693e230dd2SCorentin Chary 13703e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 13713e230dd2SCorentin Chary } 13723e230dd2SCorentin Chary 13733e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 13743e230dd2SCorentin Chary { 13753e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 13763e230dd2SCorentin Chary } 13773e230dd2SCorentin Chary 13783e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 13793e230dd2SCorentin Chary { 13803e230dd2SCorentin Chary uint8_t buf[4]; 13813e230dd2SCorentin Chary 13823e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 13833e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 13843e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 13853e230dd2SCorentin Chary buf[3] = value & 0xFF; 13863e230dd2SCorentin Chary 13873e230dd2SCorentin Chary vnc_write(vs, buf, 4); 13883e230dd2SCorentin Chary } 13893e230dd2SCorentin Chary 13903e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 13913e230dd2SCorentin Chary { 13923e230dd2SCorentin Chary uint8_t buf[2]; 13933e230dd2SCorentin Chary 13943e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 13953e230dd2SCorentin Chary buf[1] = value & 0xFF; 13963e230dd2SCorentin Chary 13973e230dd2SCorentin Chary vnc_write(vs, buf, 2); 13983e230dd2SCorentin Chary } 13993e230dd2SCorentin Chary 14003e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14013e230dd2SCorentin Chary { 14023e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14033e230dd2SCorentin Chary } 14043e230dd2SCorentin Chary 14053e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14063e230dd2SCorentin Chary { 1407bd023f95SCorentin Chary vnc_lock_output(vs); 14087536ee4bSTim Hardeck if (vs->csock != -1 && (vs->output.offset 14097536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 14107536ee4bSTim Hardeck || vs->ws_output.offset 14117536ee4bSTim Hardeck #endif 14127536ee4bSTim Hardeck )) { 1413bd023f95SCorentin Chary vnc_client_write_locked(vs); 1414bd023f95SCorentin Chary } 1415bd023f95SCorentin Chary vnc_unlock_output(vs); 14163e230dd2SCorentin Chary } 14173e230dd2SCorentin Chary 141871a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14193e230dd2SCorentin Chary { 14203e230dd2SCorentin Chary return data[offset]; 14213e230dd2SCorentin Chary } 14223e230dd2SCorentin Chary 142371a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 14243e230dd2SCorentin Chary { 14253e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 14263e230dd2SCorentin Chary } 14273e230dd2SCorentin Chary 142871a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 14293e230dd2SCorentin Chary { 14303e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 14313e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14323e230dd2SCorentin Chary } 14333e230dd2SCorentin Chary 14343e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 14353e230dd2SCorentin Chary { 14363e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 14373e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14383e230dd2SCorentin Chary } 14393e230dd2SCorentin Chary 14403e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 14413e230dd2SCorentin Chary { 14423e230dd2SCorentin Chary } 14433e230dd2SCorentin Chary 14449e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 14453e230dd2SCorentin Chary { 14463e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 14473e230dd2SCorentin Chary int absolute = kbd_mouse_is_absolute(); 14483e230dd2SCorentin Chary 14493e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1450bd023f95SCorentin Chary vnc_lock_output(vs); 14513e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 14523e230dd2SCorentin Chary vnc_write_u8(vs, 0); 14533e230dd2SCorentin Chary vnc_write_u16(vs, 1); 14543e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1455d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1456d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 14573e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1458bd023f95SCorentin Chary vnc_unlock_output(vs); 14593e230dd2SCorentin Chary vnc_flush(vs); 14603e230dd2SCorentin Chary } 14613e230dd2SCorentin Chary vs->absolute = absolute; 14623e230dd2SCorentin Chary } 14633e230dd2SCorentin Chary 14643e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 14653e230dd2SCorentin Chary { 14663e230dd2SCorentin Chary int buttons = 0; 14673e230dd2SCorentin Chary int dz = 0; 1468d39fa6d8SGerd Hoffmann int width = surface_width(vs->vd->ds); 1469d39fa6d8SGerd Hoffmann int height = surface_height(vs->vd->ds); 14703e230dd2SCorentin Chary 14713e230dd2SCorentin Chary if (button_mask & 0x01) 14723e230dd2SCorentin Chary buttons |= MOUSE_EVENT_LBUTTON; 14733e230dd2SCorentin Chary if (button_mask & 0x02) 14743e230dd2SCorentin Chary buttons |= MOUSE_EVENT_MBUTTON; 14753e230dd2SCorentin Chary if (button_mask & 0x04) 14763e230dd2SCorentin Chary buttons |= MOUSE_EVENT_RBUTTON; 14773e230dd2SCorentin Chary if (button_mask & 0x08) 14783e230dd2SCorentin Chary dz = -1; 14793e230dd2SCorentin Chary if (button_mask & 0x10) 14803e230dd2SCorentin Chary dz = 1; 14813e230dd2SCorentin Chary 14823e230dd2SCorentin Chary if (vs->absolute) { 1483d39fa6d8SGerd Hoffmann kbd_mouse_event(width > 1 ? x * 0x7FFF / (width - 1) : 0x4000, 1484d39fa6d8SGerd Hoffmann height > 1 ? y * 0x7FFF / (height - 1) : 0x4000, 14853e230dd2SCorentin Chary dz, buttons); 14863e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 14873e230dd2SCorentin Chary x -= 0x7FFF; 14883e230dd2SCorentin Chary y -= 0x7FFF; 14893e230dd2SCorentin Chary 14903e230dd2SCorentin Chary kbd_mouse_event(x, y, dz, buttons); 14913e230dd2SCorentin Chary } else { 14923e230dd2SCorentin Chary if (vs->last_x != -1) 14933e230dd2SCorentin Chary kbd_mouse_event(x - vs->last_x, 14943e230dd2SCorentin Chary y - vs->last_y, 14953e230dd2SCorentin Chary dz, buttons); 14963e230dd2SCorentin Chary vs->last_x = x; 14973e230dd2SCorentin Chary vs->last_y = y; 14983e230dd2SCorentin Chary } 14993e230dd2SCorentin Chary } 15003e230dd2SCorentin Chary 15013e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15023e230dd2SCorentin Chary { 15033e230dd2SCorentin Chary int i; 15043e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15053e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15063e230dd2SCorentin Chary if (i & SCANCODE_GREY) 15073e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15083e230dd2SCorentin Chary kbd_put_keycode(i | SCANCODE_UP); 15093e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15103e230dd2SCorentin Chary } 15113e230dd2SCorentin Chary } 15123e230dd2SCorentin Chary } 15133e230dd2SCorentin Chary 15143e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15153e230dd2SCorentin Chary { 15163e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15173e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 15183e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15193e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 15203e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 15213e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15223e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 15233e230dd2SCorentin Chary } 15243e230dd2SCorentin Chary 15253e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 15263e230dd2SCorentin Chary { 15273e230dd2SCorentin Chary VncState *vs = opaque; 1528*96f3d174SLei Li int caps, num, scr; 15293e230dd2SCorentin Chary 15303e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 15313e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 1532*96f3d174SLei Li scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0; 15333e230dd2SCorentin Chary 15343e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 15353e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 15363e230dd2SCorentin Chary } 15373e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 15383e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 15393e230dd2SCorentin Chary } 1540*96f3d174SLei Li if (vs->modifiers_state[0x46] != scr) { 1541*96f3d174SLei Li vs->modifiers_state[0x46] = scr; 1542*96f3d174SLei Li } 15433e230dd2SCorentin Chary } 15443e230dd2SCorentin Chary 15453e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 15463e230dd2SCorentin Chary { 15473e230dd2SCorentin Chary /* QEMU console switch */ 15483e230dd2SCorentin Chary switch(keycode) { 15493e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 15503e230dd2SCorentin Chary case 0x36: /* Right Shift */ 15513e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 15523e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 15533e230dd2SCorentin Chary case 0x38: /* Left ALT */ 15543e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 15553e230dd2SCorentin Chary if (down) 15563e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 15573e230dd2SCorentin Chary else 15583e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 15593e230dd2SCorentin Chary break; 15603e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 15613e230dd2SCorentin Chary if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 15623e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 15633e230dd2SCorentin Chary reset_keys(vs); 15643e230dd2SCorentin Chary console_select(keycode - 0x02); 15653e230dd2SCorentin Chary return; 15663e230dd2SCorentin Chary } 15673e230dd2SCorentin Chary break; 15683e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 15693e230dd2SCorentin Chary case 0x45: /* NumLock */ 15703e230dd2SCorentin Chary if (down) 15713e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 15723e230dd2SCorentin Chary break; 15733e230dd2SCorentin Chary } 15743e230dd2SCorentin Chary 15759892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 15763e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 15773e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 15783e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 15793e230dd2SCorentin Chary toggles numlock away from the VNC window. 15803e230dd2SCorentin Chary */ 15813e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 15823e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 15833e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 15843e230dd2SCorentin Chary press_key(vs, 0xff7f); 15853e230dd2SCorentin Chary } 15863e230dd2SCorentin Chary } else { 15873e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 15883e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 15893e230dd2SCorentin Chary press_key(vs, 0xff7f); 15903e230dd2SCorentin Chary } 15913e230dd2SCorentin Chary } 15923e230dd2SCorentin Chary } 15933e230dd2SCorentin Chary 15949892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 15953e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 15963e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 15973e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 15983e230dd2SCorentin Chary toggles capslock away from the VNC window. 15993e230dd2SCorentin Chary */ 16003e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 16013e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 16023e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 16033e230dd2SCorentin Chary if (capslock) { 16043e230dd2SCorentin Chary if (uppercase == shift) { 16053e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 16063e230dd2SCorentin Chary press_key(vs, 0xffe5); 16073e230dd2SCorentin Chary } 16083e230dd2SCorentin Chary } else { 16093e230dd2SCorentin Chary if (uppercase != shift) { 16103e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 16113e230dd2SCorentin Chary press_key(vs, 0xffe5); 16123e230dd2SCorentin Chary } 16133e230dd2SCorentin Chary } 16143e230dd2SCorentin Chary } 16153e230dd2SCorentin Chary 161681c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 16173e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 16183e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 16193e230dd2SCorentin Chary if (down) 16203e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 16213e230dd2SCorentin Chary else 16223e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 16233e230dd2SCorentin Chary } else { 1624e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1625e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1626e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 16273e230dd2SCorentin Chary /* QEMU console emulation */ 16283e230dd2SCorentin Chary if (down) { 16293e230dd2SCorentin Chary switch (keycode) { 16303e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16313e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16323e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16333e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16343e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16353e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16363e230dd2SCorentin Chary break; 16373e230dd2SCorentin Chary case 0xc8: 16383e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 16393e230dd2SCorentin Chary break; 16403e230dd2SCorentin Chary case 0xd0: 16413e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 16423e230dd2SCorentin Chary break; 16433e230dd2SCorentin Chary case 0xcb: 16443e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 16453e230dd2SCorentin Chary break; 16463e230dd2SCorentin Chary case 0xcd: 16473e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 16483e230dd2SCorentin Chary break; 16493e230dd2SCorentin Chary case 0xd3: 16503e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 16513e230dd2SCorentin Chary break; 16523e230dd2SCorentin Chary case 0xc7: 16533e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 16543e230dd2SCorentin Chary break; 16553e230dd2SCorentin Chary case 0xcf: 16563e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 16573e230dd2SCorentin Chary break; 16583e230dd2SCorentin Chary case 0xc9: 16593e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 16603e230dd2SCorentin Chary break; 16613e230dd2SCorentin Chary case 0xd1: 16623e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 16633e230dd2SCorentin Chary break; 16643e230dd2SCorentin Chary 16653e230dd2SCorentin Chary case 0x47: 16663e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 16673e230dd2SCorentin Chary break; 16683e230dd2SCorentin Chary case 0x48: 16693e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 16703e230dd2SCorentin Chary break; 16713e230dd2SCorentin Chary case 0x49: 16723e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 16733e230dd2SCorentin Chary break; 16743e230dd2SCorentin Chary case 0x4b: 16753e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 16763e230dd2SCorentin Chary break; 16773e230dd2SCorentin Chary case 0x4c: 16783e230dd2SCorentin Chary kbd_put_keysym('5'); 16793e230dd2SCorentin Chary break; 16803e230dd2SCorentin Chary case 0x4d: 16813e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 16823e230dd2SCorentin Chary break; 16833e230dd2SCorentin Chary case 0x4f: 16843e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 16853e230dd2SCorentin Chary break; 16863e230dd2SCorentin Chary case 0x50: 16873e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 16883e230dd2SCorentin Chary break; 16893e230dd2SCorentin Chary case 0x51: 16903e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 16913e230dd2SCorentin Chary break; 16923e230dd2SCorentin Chary case 0x52: 16933e230dd2SCorentin Chary kbd_put_keysym('0'); 16943e230dd2SCorentin Chary break; 16953e230dd2SCorentin Chary case 0x53: 16963e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 16973e230dd2SCorentin Chary break; 16983e230dd2SCorentin Chary 16993e230dd2SCorentin Chary case 0xb5: 17003e230dd2SCorentin Chary kbd_put_keysym('/'); 17013e230dd2SCorentin Chary break; 17023e230dd2SCorentin Chary case 0x37: 17033e230dd2SCorentin Chary kbd_put_keysym('*'); 17043e230dd2SCorentin Chary break; 17053e230dd2SCorentin Chary case 0x4a: 17063e230dd2SCorentin Chary kbd_put_keysym('-'); 17073e230dd2SCorentin Chary break; 17083e230dd2SCorentin Chary case 0x4e: 17093e230dd2SCorentin Chary kbd_put_keysym('+'); 17103e230dd2SCorentin Chary break; 17113e230dd2SCorentin Chary case 0x9c: 17123e230dd2SCorentin Chary kbd_put_keysym('\n'); 17133e230dd2SCorentin Chary break; 17143e230dd2SCorentin Chary 17153e230dd2SCorentin Chary default: 1716e26437c2SGerd Hoffmann if (control) { 1717e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1718e26437c2SGerd Hoffmann } else { 17193e230dd2SCorentin Chary kbd_put_keysym(sym); 1720e26437c2SGerd Hoffmann } 17213e230dd2SCorentin Chary break; 17223e230dd2SCorentin Chary } 17233e230dd2SCorentin Chary } 17243e230dd2SCorentin Chary } 17253e230dd2SCorentin Chary } 17263e230dd2SCorentin Chary 17277bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 17287bc9318bSGerd Hoffmann { 17297bc9318bSGerd Hoffmann static const int keycodes[] = { 17307bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 17317bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 17327bc9318bSGerd Hoffmann }; 17337bc9318bSGerd Hoffmann int i, keycode; 17347bc9318bSGerd Hoffmann 173581c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 17367bc9318bSGerd Hoffmann return; 17377bc9318bSGerd Hoffmann } 17387bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 17397bc9318bSGerd Hoffmann keycode = keycodes[i]; 17407bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 17417bc9318bSGerd Hoffmann continue; 17427bc9318bSGerd Hoffmann } 17437bc9318bSGerd Hoffmann if (keycode & SCANCODE_GREY) { 17447bc9318bSGerd Hoffmann kbd_put_keycode(SCANCODE_EMUL0); 17457bc9318bSGerd Hoffmann } 17467bc9318bSGerd Hoffmann kbd_put_keycode(keycode | SCANCODE_UP); 17477bc9318bSGerd Hoffmann } 17487bc9318bSGerd Hoffmann } 17497bc9318bSGerd Hoffmann 17503e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 17513e230dd2SCorentin Chary { 17523e230dd2SCorentin Chary int keycode; 17533e230dd2SCorentin Chary int lsym = sym; 17543e230dd2SCorentin Chary 175581c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 17563e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 17573e230dd2SCorentin Chary } 17583e230dd2SCorentin Chary 17593e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 17603e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 17613e230dd2SCorentin Chary } 17623e230dd2SCorentin Chary 17633e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 17643e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 17653e230dd2SCorentin Chary { 17663e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 17673e230dd2SCorentin Chary if (keyboard_layout) 17683e230dd2SCorentin Chary key_event(vs, down, sym); 17693e230dd2SCorentin Chary else 17703e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 17713e230dd2SCorentin Chary } 17723e230dd2SCorentin Chary 17733e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 17743e230dd2SCorentin Chary int x_position, int y_position, 17753e230dd2SCorentin Chary int w, int h) 17763e230dd2SCorentin Chary { 17776ed391bfSWen Congyang int i; 1778d39fa6d8SGerd Hoffmann const size_t width = surface_width(vs->vd->ds) / 16; 1779d39fa6d8SGerd Hoffmann const size_t height = surface_height(vs->vd->ds); 17806ed391bfSWen Congyang 1781d39fa6d8SGerd Hoffmann if (y_position > height) { 1782d39fa6d8SGerd Hoffmann y_position = height; 1783d39fa6d8SGerd Hoffmann } 1784d39fa6d8SGerd Hoffmann if (y_position + h >= height) { 1785d39fa6d8SGerd Hoffmann h = height - y_position; 1786d39fa6d8SGerd Hoffmann } 17873e230dd2SCorentin Chary 17883e230dd2SCorentin Chary vs->need_update = 1; 17893e230dd2SCorentin Chary if (!incremental) { 17903e230dd2SCorentin Chary vs->force_update = 1; 17913e230dd2SCorentin Chary for (i = 0; i < h; i++) { 17926ed391bfSWen Congyang bitmap_set(vs->dirty[y_position + i], 0, width); 17936ed391bfSWen Congyang bitmap_clear(vs->dirty[y_position + i], width, 1794a0843a68SAnthony Liguori VNC_DIRTY_BITS - width); 17953e230dd2SCorentin Chary } 17963e230dd2SCorentin Chary } 17973e230dd2SCorentin Chary } 17983e230dd2SCorentin Chary 17993e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18003e230dd2SCorentin Chary { 1801bd023f95SCorentin Chary vnc_lock_output(vs); 18023e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18033e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18043e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1805d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1806d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1807d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 18083e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1809bd023f95SCorentin Chary vnc_unlock_output(vs); 18103e230dd2SCorentin Chary vnc_flush(vs); 18113e230dd2SCorentin Chary } 18123e230dd2SCorentin Chary 18133e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 18143e230dd2SCorentin Chary { 1815bd023f95SCorentin Chary vnc_lock_output(vs); 18163e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18173e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18183e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1819d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1820d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1821d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 18223e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1823bd023f95SCorentin Chary vnc_unlock_output(vs); 18243e230dd2SCorentin Chary vnc_flush(vs); 18253e230dd2SCorentin Chary } 18263e230dd2SCorentin Chary 18273e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 18283e230dd2SCorentin Chary { 18293e230dd2SCorentin Chary int i; 18303e230dd2SCorentin Chary unsigned int enc = 0; 18313e230dd2SCorentin Chary 18323e230dd2SCorentin Chary vs->features = 0; 18333e230dd2SCorentin Chary vs->vnc_encoding = 0; 1834d1af0e05SCorentin Chary vs->tight.compression = 9; 1835d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 18363e230dd2SCorentin Chary vs->absolute = -1; 18373e230dd2SCorentin Chary 18383e230dd2SCorentin Chary /* 18393e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1840e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 18413e230dd2SCorentin Chary * will be set at the end of the loop. 18423e230dd2SCorentin Chary */ 18433e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 18443e230dd2SCorentin Chary enc = encodings[i]; 18453e230dd2SCorentin Chary switch (enc) { 18463e230dd2SCorentin Chary case VNC_ENCODING_RAW: 18473e230dd2SCorentin Chary vs->vnc_encoding = enc; 18483e230dd2SCorentin Chary break; 18493e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 18503e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 18513e230dd2SCorentin Chary break; 18523e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 18533e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 18543e230dd2SCorentin Chary vs->vnc_encoding = enc; 18553e230dd2SCorentin Chary break; 18563e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 18573e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 18583e230dd2SCorentin Chary vs->vnc_encoding = enc; 18593e230dd2SCorentin Chary break; 1860fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1861efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1862efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1863efe556adSCorentin Chary vs->vnc_encoding = enc; 1864efe556adSCorentin Chary break; 1865fe3e7f2dSJoel Martin #endif 18663e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 18673e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 18683e230dd2SCorentin Chary vs->vnc_encoding = enc; 18693e230dd2SCorentin Chary break; 1870148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1871148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1872148954faSCorentin Chary vs->vnc_encoding = enc; 1873148954faSCorentin Chary break; 1874148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1875148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1876148954faSCorentin Chary vs->vnc_encoding = enc; 1877148954faSCorentin Chary break; 18783e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 18793e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 18803e230dd2SCorentin Chary break; 18813e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 18823e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 18833e230dd2SCorentin Chary break; 18843e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 18853e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 18863e230dd2SCorentin Chary break; 18873e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 18883e230dd2SCorentin Chary send_ext_key_event_ack(vs); 18893e230dd2SCorentin Chary break; 18903e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 18913e230dd2SCorentin Chary send_ext_audio_ack(vs); 18923e230dd2SCorentin Chary break; 18933e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 18943e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 18953e230dd2SCorentin Chary break; 18963e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1897d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 18983e230dd2SCorentin Chary break; 18993e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1900b31f519eSCorentin Chary if (vs->vd->lossy) { 1901d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1902b31f519eSCorentin Chary } 19033e230dd2SCorentin Chary break; 19043e230dd2SCorentin Chary default: 19053e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 19063e230dd2SCorentin Chary break; 19073e230dd2SCorentin Chary } 19083e230dd2SCorentin Chary } 19093e230dd2SCorentin Chary vnc_desktop_resize(vs); 19109e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 19113e230dd2SCorentin Chary } 19123e230dd2SCorentin Chary 19133e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 19143e230dd2SCorentin Chary { 19159f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 19169f64916dSGerd Hoffmann 19179f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 19183e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 19193e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 19203e230dd2SCorentin Chary } else { 19213e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 19223e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 19233e230dd2SCorentin Chary } 19243e230dd2SCorentin Chary } 19253e230dd2SCorentin Chary 19263e230dd2SCorentin Chary static void set_pixel_format(VncState *vs, 19273e230dd2SCorentin Chary int bits_per_pixel, int depth, 19283e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 19293e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 19303e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 19313e230dd2SCorentin Chary { 19323e230dd2SCorentin Chary if (!true_color_flag) { 19333e230dd2SCorentin Chary vnc_client_error(vs); 19343e230dd2SCorentin Chary return; 19353e230dd2SCorentin Chary } 19363e230dd2SCorentin Chary 19379f64916dSGerd Hoffmann vs->client_pf.rmax = red_max; 19389f64916dSGerd Hoffmann vs->client_pf.rbits = hweight_long(red_max); 19399f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 19409f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 19419f64916dSGerd Hoffmann vs->client_pf.gmax = green_max; 19429f64916dSGerd Hoffmann vs->client_pf.gbits = hweight_long(green_max); 19439f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 19449f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 19459f64916dSGerd Hoffmann vs->client_pf.bmax = blue_max; 19469f64916dSGerd Hoffmann vs->client_pf.bbits = hweight_long(blue_max); 19479f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 19489f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 19499f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 19509f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 19519f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 19529f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 19533e230dd2SCorentin Chary 19543e230dd2SCorentin Chary set_pixel_conversion(vs); 19553e230dd2SCorentin Chary 19561dbfa005SGerd Hoffmann graphic_hw_invalidate(NULL); 19571dbfa005SGerd Hoffmann graphic_hw_update(NULL); 19583e230dd2SCorentin Chary } 19593e230dd2SCorentin Chary 19603e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 19613e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 19623e230dd2SCorentin Chary 19639f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 19649f64916dSGerd Hoffmann 19659f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 19669f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 19673e230dd2SCorentin Chary 19683e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 19693e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 19703e230dd2SCorentin Chary #else 19713e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 19723e230dd2SCorentin Chary #endif 19733e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 19749f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 19759f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 19769f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 19779f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 19789f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 19799f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 19809f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 19813e230dd2SCorentin Chary 19823e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 19833e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 19843e230dd2SCorentin Chary } 19853e230dd2SCorentin Chary 19863e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 19873e230dd2SCorentin Chary { 19883e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 19893e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 1990bd023f95SCorentin Chary vnc_lock_output(vs); 19913e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19923e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19933e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 1994d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1995d39fa6d8SGerd Hoffmann surface_width(vs->vd->ds), 1996d39fa6d8SGerd Hoffmann surface_height(vs->vd->ds), 1997d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 19983e230dd2SCorentin Chary pixel_format_message(vs); 1999bd023f95SCorentin Chary vnc_unlock_output(vs); 20003e230dd2SCorentin Chary vnc_flush(vs); 20013e230dd2SCorentin Chary } else { 20023e230dd2SCorentin Chary set_pixel_conversion(vs); 20033e230dd2SCorentin Chary } 20043e230dd2SCorentin Chary } 20053e230dd2SCorentin Chary 20063e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 20073e230dd2SCorentin Chary { 20083e230dd2SCorentin Chary int i; 20093e230dd2SCorentin Chary uint16_t limit; 20103e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 20113e230dd2SCorentin Chary 20123e230dd2SCorentin Chary if (data[0] > 3) { 20130f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 20143e230dd2SCorentin Chary } 20153e230dd2SCorentin Chary 20163e230dd2SCorentin Chary switch (data[0]) { 20173e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 20183e230dd2SCorentin Chary if (len == 1) 20193e230dd2SCorentin Chary return 20; 20203e230dd2SCorentin Chary 20213e230dd2SCorentin Chary set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), 20223e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 20233e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 20243e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 20253e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 20263e230dd2SCorentin Chary break; 20273e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 20283e230dd2SCorentin Chary if (len == 1) 20293e230dd2SCorentin Chary return 4; 20303e230dd2SCorentin Chary 20313e230dd2SCorentin Chary if (len == 4) { 20323e230dd2SCorentin Chary limit = read_u16(data, 2); 20333e230dd2SCorentin Chary if (limit > 0) 20343e230dd2SCorentin Chary return 4 + (limit * 4); 20353e230dd2SCorentin Chary } else 20363e230dd2SCorentin Chary limit = read_u16(data, 2); 20373e230dd2SCorentin Chary 20383e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 20393e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 20403e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 20413e230dd2SCorentin Chary } 20423e230dd2SCorentin Chary 20433e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 20443e230dd2SCorentin Chary break; 20453e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 20463e230dd2SCorentin Chary if (len == 1) 20473e230dd2SCorentin Chary return 10; 20483e230dd2SCorentin Chary 20493e230dd2SCorentin Chary framebuffer_update_request(vs, 20503e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 20513e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 20523e230dd2SCorentin Chary break; 20533e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 20543e230dd2SCorentin Chary if (len == 1) 20553e230dd2SCorentin Chary return 8; 20563e230dd2SCorentin Chary 20573e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 20583e230dd2SCorentin Chary break; 20593e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 20603e230dd2SCorentin Chary if (len == 1) 20613e230dd2SCorentin Chary return 6; 20623e230dd2SCorentin Chary 20633e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 20643e230dd2SCorentin Chary break; 20653e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 20663e230dd2SCorentin Chary if (len == 1) 20673e230dd2SCorentin Chary return 8; 20683e230dd2SCorentin Chary 20693e230dd2SCorentin Chary if (len == 8) { 20703e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 20713e230dd2SCorentin Chary if (dlen > 0) 20723e230dd2SCorentin Chary return 8 + dlen; 20733e230dd2SCorentin Chary } 20743e230dd2SCorentin Chary 20753e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 20763e230dd2SCorentin Chary break; 20773e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 20783e230dd2SCorentin Chary if (len == 1) 20793e230dd2SCorentin Chary return 2; 20803e230dd2SCorentin Chary 20813e230dd2SCorentin Chary switch (read_u8(data, 1)) { 20823e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 20833e230dd2SCorentin Chary if (len == 2) 20843e230dd2SCorentin Chary return 12; 20853e230dd2SCorentin Chary 20863e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 20873e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 20883e230dd2SCorentin Chary break; 20893e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 20903e230dd2SCorentin Chary if (len == 2) 20913e230dd2SCorentin Chary return 4; 20923e230dd2SCorentin Chary 20933e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 20943e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 20953e230dd2SCorentin Chary audio_add(vs); 20963e230dd2SCorentin Chary break; 20973e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 20983e230dd2SCorentin Chary audio_del(vs); 20993e230dd2SCorentin Chary break; 21003e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 21013e230dd2SCorentin Chary if (len == 4) 21023e230dd2SCorentin Chary return 10; 21033e230dd2SCorentin Chary switch (read_u8(data, 4)) { 21043e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 21053e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 21063e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 21073e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 21083e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 21093e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 21103e230dd2SCorentin Chary default: 21113e230dd2SCorentin Chary printf("Invalid audio format %d\n", read_u8(data, 4)); 21123e230dd2SCorentin Chary vnc_client_error(vs); 21133e230dd2SCorentin Chary break; 21143e230dd2SCorentin Chary } 21153e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 21163e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 21173e230dd2SCorentin Chary printf("Invalid audio channel coount %d\n", 21183e230dd2SCorentin Chary read_u8(data, 5)); 21193e230dd2SCorentin Chary vnc_client_error(vs); 21203e230dd2SCorentin Chary break; 21213e230dd2SCorentin Chary } 21223e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 21233e230dd2SCorentin Chary break; 21243e230dd2SCorentin Chary default: 21253e230dd2SCorentin Chary printf ("Invalid audio message %d\n", read_u8(data, 4)); 21263e230dd2SCorentin Chary vnc_client_error(vs); 21273e230dd2SCorentin Chary break; 21283e230dd2SCorentin Chary } 21293e230dd2SCorentin Chary break; 21303e230dd2SCorentin Chary 21313e230dd2SCorentin Chary default: 21323e230dd2SCorentin Chary printf("Msg: %d\n", read_u16(data, 0)); 21333e230dd2SCorentin Chary vnc_client_error(vs); 21343e230dd2SCorentin Chary break; 21353e230dd2SCorentin Chary } 21363e230dd2SCorentin Chary break; 21373e230dd2SCorentin Chary default: 21383e230dd2SCorentin Chary printf("Msg: %d\n", data[0]); 21393e230dd2SCorentin Chary vnc_client_error(vs); 21403e230dd2SCorentin Chary break; 21413e230dd2SCorentin Chary } 21423e230dd2SCorentin Chary 21433e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 21443e230dd2SCorentin Chary return 0; 21453e230dd2SCorentin Chary } 21463e230dd2SCorentin Chary 21473e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 21483e230dd2SCorentin Chary { 21493e230dd2SCorentin Chary char buf[1024]; 21508cf36489SGerd Hoffmann VncShareMode mode; 21513e230dd2SCorentin Chary int size; 21523e230dd2SCorentin Chary 21538cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 21548cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 21558cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 21568cf36489SGerd Hoffmann /* 21578cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 21588cf36489SGerd Hoffmann * 21598cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 21608cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 21618cf36489SGerd Hoffmann * reasons. 21628cf36489SGerd Hoffmann */ 21638cf36489SGerd Hoffmann break; 21648cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 21658cf36489SGerd Hoffmann /* 21668cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 21678cf36489SGerd Hoffmann * 21688cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 21698cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 21708cf36489SGerd Hoffmann * as no exclusive connection exists. 21718cf36489SGerd Hoffmann * 21728cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 21738cf36489SGerd Hoffmann */ 21748cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 21758cf36489SGerd Hoffmann VncState *client; 21768cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 21778cf36489SGerd Hoffmann if (vs == client) { 21788cf36489SGerd Hoffmann continue; 21798cf36489SGerd Hoffmann } 21808cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 21818cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 21828cf36489SGerd Hoffmann continue; 21838cf36489SGerd Hoffmann } 21848cf36489SGerd Hoffmann vnc_disconnect_start(client); 21858cf36489SGerd Hoffmann } 21868cf36489SGerd Hoffmann } 21878cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 21888cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 21898cf36489SGerd Hoffmann vnc_disconnect_start(vs); 21908cf36489SGerd Hoffmann return 0; 21918cf36489SGerd Hoffmann } 21928cf36489SGerd Hoffmann } 21938cf36489SGerd Hoffmann break; 21948cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 21958cf36489SGerd Hoffmann /* 21968cf36489SGerd Hoffmann * Policy: Shared connects only. 21978cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 21988cf36489SGerd Hoffmann * 21998cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 22008cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 22018cf36489SGerd Hoffmann * client disconnect everybody else. 22028cf36489SGerd Hoffmann */ 22038cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 22048cf36489SGerd Hoffmann vnc_disconnect_start(vs); 22058cf36489SGerd Hoffmann return 0; 22068cf36489SGerd Hoffmann } 22078cf36489SGerd Hoffmann break; 22088cf36489SGerd Hoffmann } 22098cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 22108cf36489SGerd Hoffmann 2211d39fa6d8SGerd Hoffmann vs->client_width = surface_width(vs->vd->ds); 2212d39fa6d8SGerd Hoffmann vs->client_height = surface_height(vs->vd->ds); 22133e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 22143e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 22153e230dd2SCorentin Chary 22163e230dd2SCorentin Chary pixel_format_message(vs); 22173e230dd2SCorentin Chary 22183e230dd2SCorentin Chary if (qemu_name) 22193e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 22203e230dd2SCorentin Chary else 22213e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 22223e230dd2SCorentin Chary 22233e230dd2SCorentin Chary vnc_write_u32(vs, size); 22243e230dd2SCorentin Chary vnc_write(vs, buf, size); 22253e230dd2SCorentin Chary vnc_flush(vs); 22263e230dd2SCorentin Chary 22273e230dd2SCorentin Chary vnc_client_cache_auth(vs); 22283e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED); 22293e230dd2SCorentin Chary 22303e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22313e230dd2SCorentin Chary 22323e230dd2SCorentin Chary return 0; 22333e230dd2SCorentin Chary } 22343e230dd2SCorentin Chary 22353e230dd2SCorentin Chary void start_client_init(VncState *vs) 22363e230dd2SCorentin Chary { 22373e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 22383e230dd2SCorentin Chary } 22393e230dd2SCorentin Chary 22403e230dd2SCorentin Chary static void make_challenge(VncState *vs) 22413e230dd2SCorentin Chary { 22423e230dd2SCorentin Chary int i; 22433e230dd2SCorentin Chary 22443e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 22453e230dd2SCorentin Chary 22463e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 22473e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 22483e230dd2SCorentin Chary } 22493e230dd2SCorentin Chary 22503e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 22513e230dd2SCorentin Chary { 22523e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 22533e230dd2SCorentin Chary int i, j, pwlen; 22543e230dd2SCorentin Chary unsigned char key[8]; 22553c9405a0SGerd Hoffmann time_t now = time(NULL); 22563e230dd2SCorentin Chary 22571cd20f8bSAnthony Liguori if (!vs->vd->password) { 22583e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 22596bffdf0fSGerd Hoffmann goto reject; 22603e230dd2SCorentin Chary } 22613c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 22623c9405a0SGerd Hoffmann VNC_DEBUG("Password is expired"); 22633c9405a0SGerd Hoffmann goto reject; 22643c9405a0SGerd Hoffmann } 22653e230dd2SCorentin Chary 22663e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 22673e230dd2SCorentin Chary 22683e230dd2SCorentin Chary /* Calculate the expected challenge response */ 22693e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 22703e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 22713e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 22723e230dd2SCorentin Chary deskey(key, EN0); 22733e230dd2SCorentin Chary for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8) 22743e230dd2SCorentin Chary des(response+j, response+j); 22753e230dd2SCorentin Chary 22763e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 22773e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2278e5bed759SDong Xu Wang VNC_DEBUG("Client challenge response did not match\n"); 22796bffdf0fSGerd Hoffmann goto reject; 22806bffdf0fSGerd Hoffmann } else { 22816bffdf0fSGerd Hoffmann VNC_DEBUG("Accepting VNC challenge response\n"); 22826bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 22836bffdf0fSGerd Hoffmann vnc_flush(vs); 22846bffdf0fSGerd Hoffmann 22856bffdf0fSGerd Hoffmann start_client_init(vs); 22866bffdf0fSGerd Hoffmann } 22876bffdf0fSGerd Hoffmann return 0; 22886bffdf0fSGerd Hoffmann 22896bffdf0fSGerd Hoffmann reject: 22903e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 22913e230dd2SCorentin Chary if (vs->minor >= 8) { 22923e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 22933e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 22943e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 22953e230dd2SCorentin Chary } 22963e230dd2SCorentin Chary vnc_flush(vs); 22973e230dd2SCorentin Chary vnc_client_error(vs); 22983e230dd2SCorentin Chary return 0; 22993e230dd2SCorentin Chary } 23003e230dd2SCorentin Chary 23013e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 23023e230dd2SCorentin Chary { 23033e230dd2SCorentin Chary make_challenge(vs); 23043e230dd2SCorentin Chary /* Send client a 'random' challenge */ 23053e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 23063e230dd2SCorentin Chary vnc_flush(vs); 23073e230dd2SCorentin Chary 23083e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 23093e230dd2SCorentin Chary } 23103e230dd2SCorentin Chary 23113e230dd2SCorentin Chary 23123e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 23133e230dd2SCorentin Chary { 23143e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 23153e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 23167e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 23173e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 23183e230dd2SCorentin Chary vnc_write_u32(vs, 1); 23193e230dd2SCorentin Chary if (vs->minor >= 8) { 23203e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23213e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23223e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23233e230dd2SCorentin Chary } 23243e230dd2SCorentin Chary vnc_client_error(vs); 23253e230dd2SCorentin Chary } else { /* Accept requested auth */ 23263e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 23277e7e2ebcSDaniel P. Berrange switch (vs->auth) { 23283e230dd2SCorentin Chary case VNC_AUTH_NONE: 23293e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 23303e230dd2SCorentin Chary if (vs->minor >= 8) { 23313e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 23323e230dd2SCorentin Chary vnc_flush(vs); 23333e230dd2SCorentin Chary } 23343e230dd2SCorentin Chary start_client_init(vs); 23353e230dd2SCorentin Chary break; 23363e230dd2SCorentin Chary 23373e230dd2SCorentin Chary case VNC_AUTH_VNC: 23383e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 23393e230dd2SCorentin Chary start_auth_vnc(vs); 23403e230dd2SCorentin Chary break; 23413e230dd2SCorentin Chary 23423e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 23433e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 23443a93113aSDong Xu Wang VNC_DEBUG("Accept VeNCrypt auth\n"); 23453e230dd2SCorentin Chary start_auth_vencrypt(vs); 23463e230dd2SCorentin Chary break; 23473e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 23483e230dd2SCorentin Chary 23493e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 23503e230dd2SCorentin Chary case VNC_AUTH_SASL: 23513e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 23523e230dd2SCorentin Chary start_auth_sasl(vs); 23533e230dd2SCorentin Chary break; 23543e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 23553e230dd2SCorentin Chary 23563e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 23577e7e2ebcSDaniel P. Berrange VNC_DEBUG("Reject auth %d server code bug\n", vs->auth); 23583e230dd2SCorentin Chary vnc_write_u8(vs, 1); 23593e230dd2SCorentin Chary if (vs->minor >= 8) { 23603e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23613e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23623e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23633e230dd2SCorentin Chary } 23643e230dd2SCorentin Chary vnc_client_error(vs); 23653e230dd2SCorentin Chary } 23663e230dd2SCorentin Chary } 23673e230dd2SCorentin Chary return 0; 23683e230dd2SCorentin Chary } 23693e230dd2SCorentin Chary 23703e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 23713e230dd2SCorentin Chary { 23723e230dd2SCorentin Chary char local[13]; 23733e230dd2SCorentin Chary 23743e230dd2SCorentin Chary memcpy(local, version, 12); 23753e230dd2SCorentin Chary local[12] = 0; 23763e230dd2SCorentin Chary 23773e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 23783e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 23793e230dd2SCorentin Chary vnc_client_error(vs); 23803e230dd2SCorentin Chary return 0; 23813e230dd2SCorentin Chary } 23823e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 23833e230dd2SCorentin Chary if (vs->major != 3 || 23843e230dd2SCorentin Chary (vs->minor != 3 && 23853e230dd2SCorentin Chary vs->minor != 4 && 23863e230dd2SCorentin Chary vs->minor != 5 && 23873e230dd2SCorentin Chary vs->minor != 7 && 23883e230dd2SCorentin Chary vs->minor != 8)) { 23893e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 23903e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 23913e230dd2SCorentin Chary vnc_flush(vs); 23923e230dd2SCorentin Chary vnc_client_error(vs); 23933e230dd2SCorentin Chary return 0; 23943e230dd2SCorentin Chary } 23953e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 23963e230dd2SCorentin Chary * as equivalent to v3.3 by servers 23973e230dd2SCorentin Chary */ 23983e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 23993e230dd2SCorentin Chary vs->minor = 3; 24003e230dd2SCorentin Chary 24013e230dd2SCorentin Chary if (vs->minor == 3) { 24027e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 24033e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 24047e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 24053e230dd2SCorentin Chary vnc_flush(vs); 24063e230dd2SCorentin Chary start_client_init(vs); 24077e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 24083e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 24097e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 24103e230dd2SCorentin Chary vnc_flush(vs); 24113e230dd2SCorentin Chary start_auth_vnc(vs); 24123e230dd2SCorentin Chary } else { 24137e7e2ebcSDaniel P. Berrange VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth); 24143e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 24153e230dd2SCorentin Chary vnc_flush(vs); 24163e230dd2SCorentin Chary vnc_client_error(vs); 24173e230dd2SCorentin Chary } 24183e230dd2SCorentin Chary } else { 24197e7e2ebcSDaniel P. Berrange VNC_DEBUG("Telling client we support auth %d\n", vs->auth); 24203e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 24217e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 24223e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 24233e230dd2SCorentin Chary vnc_flush(vs); 24243e230dd2SCorentin Chary } 24253e230dd2SCorentin Chary 24263e230dd2SCorentin Chary return 0; 24273e230dd2SCorentin Chary } 24283e230dd2SCorentin Chary 2429999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2430999342a0SCorentin Chary { 2431999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2432999342a0SCorentin Chary 2433999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2434999342a0SCorentin Chary } 2435999342a0SCorentin Chary 24367d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 24377d964c9dSCorentin Chary { 24387d964c9dSCorentin Chary int i, j; 24397d964c9dSCorentin Chary 24407d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 24417d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 24427d964c9dSCorentin Chary x /= VNC_STAT_RECT; 24437d964c9dSCorentin Chary y /= VNC_STAT_RECT; 24447d964c9dSCorentin Chary 2445207f328aSCorentin Chary for (j = y; j <= h; j++) { 2446207f328aSCorentin Chary for (i = x; i <= w; i++) { 24477d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 24487d964c9dSCorentin Chary } 24497d964c9dSCorentin Chary } 24507d964c9dSCorentin Chary } 24517d964c9dSCorentin Chary 24527d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 24537d964c9dSCorentin Chary { 24547d964c9dSCorentin Chary VncState *vs; 24557d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 24567d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 24577d964c9dSCorentin Chary int has_dirty = 0; 24587d964c9dSCorentin Chary 24597d964c9dSCorentin Chary y = y / VNC_STAT_RECT * VNC_STAT_RECT; 24607d964c9dSCorentin Chary x = x / VNC_STAT_RECT * VNC_STAT_RECT; 24617d964c9dSCorentin Chary 24627d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2463bc2429b9SCorentin Chary int j; 24647d964c9dSCorentin Chary 24657d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 24667d964c9dSCorentin Chary if (vs->output.offset) { 24677d964c9dSCorentin Chary continue; 24687d964c9dSCorentin Chary } 24697d964c9dSCorentin Chary 24707d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 24717d964c9dSCorentin Chary continue; 24727d964c9dSCorentin Chary } 2473207f328aSCorentin Chary 24747d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 24757d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2476bc2429b9SCorentin Chary bitmap_set(vs->dirty[y + j], x / 16, VNC_STAT_RECT / 16); 24777d964c9dSCorentin Chary } 24787d964c9dSCorentin Chary has_dirty++; 24797d964c9dSCorentin Chary } 2480207f328aSCorentin Chary 24817d964c9dSCorentin Chary return has_dirty; 24827d964c9dSCorentin Chary } 24837d964c9dSCorentin Chary 24847d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2485999342a0SCorentin Chary { 24869f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 24879f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 2488999342a0SCorentin Chary int x, y; 2489999342a0SCorentin Chary struct timeval res; 24907d964c9dSCorentin Chary int has_dirty = 0; 2491999342a0SCorentin Chary 24929f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 24939f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2494999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2495999342a0SCorentin Chary 2496999342a0SCorentin Chary rect->updated = false; 2497999342a0SCorentin Chary } 2498999342a0SCorentin Chary } 2499999342a0SCorentin Chary 2500ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2501999342a0SCorentin Chary 2502999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 25037d964c9dSCorentin Chary return has_dirty; 2504999342a0SCorentin Chary } 2505999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2506999342a0SCorentin Chary 25079f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 25089f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2509999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2510999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2511999342a0SCorentin Chary struct timeval min, max; 2512999342a0SCorentin Chary 2513999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2514999342a0SCorentin Chary continue ; 2515999342a0SCorentin Chary } 2516999342a0SCorentin Chary 2517999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2518ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2519999342a0SCorentin Chary 2520999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2521999342a0SCorentin Chary rect->freq = 0; 25227d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2523999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2524999342a0SCorentin Chary continue ; 2525999342a0SCorentin Chary } 2526999342a0SCorentin Chary 2527999342a0SCorentin Chary min = rect->times[rect->idx]; 2528999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2529ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2530999342a0SCorentin Chary 2531999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2532999342a0SCorentin Chary rect->freq /= count; 2533999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2534999342a0SCorentin Chary } 2535999342a0SCorentin Chary } 25367d964c9dSCorentin Chary return has_dirty; 2537999342a0SCorentin Chary } 2538999342a0SCorentin Chary 2539999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2540999342a0SCorentin Chary { 2541999342a0SCorentin Chary int i, j; 2542999342a0SCorentin Chary double total = 0; 2543999342a0SCorentin Chary int num = 0; 2544999342a0SCorentin Chary 2545999342a0SCorentin Chary x = (x / VNC_STAT_RECT) * VNC_STAT_RECT; 2546999342a0SCorentin Chary y = (y / VNC_STAT_RECT) * VNC_STAT_RECT; 2547999342a0SCorentin Chary 2548999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2549999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2550999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2551999342a0SCorentin Chary num++; 2552999342a0SCorentin Chary } 2553999342a0SCorentin Chary } 2554999342a0SCorentin Chary 2555999342a0SCorentin Chary if (num) { 2556999342a0SCorentin Chary return total / num; 2557999342a0SCorentin Chary } else { 2558999342a0SCorentin Chary return 0; 2559999342a0SCorentin Chary } 2560999342a0SCorentin Chary } 2561999342a0SCorentin Chary 2562999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2563999342a0SCorentin Chary { 2564999342a0SCorentin Chary VncRectStat *rect; 2565999342a0SCorentin Chary 2566999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2567999342a0SCorentin Chary if (rect->updated) { 2568999342a0SCorentin Chary return ; 2569999342a0SCorentin Chary } 2570999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2571999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2572999342a0SCorentin Chary rect->updated = true; 2573999342a0SCorentin Chary } 2574999342a0SCorentin Chary 25753e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 25763e230dd2SCorentin Chary { 25779f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 25789f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 25793e230dd2SCorentin Chary int y; 25803e230dd2SCorentin Chary uint8_t *guest_row; 25813e230dd2SCorentin Chary uint8_t *server_row; 25823e230dd2SCorentin Chary int cmp_bytes; 25833e230dd2SCorentin Chary VncState *vs; 25843e230dd2SCorentin Chary int has_dirty = 0; 25859f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 25863e230dd2SCorentin Chary 258780e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2588999342a0SCorentin Chary 258980e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2590999342a0SCorentin Chary gettimeofday(&tv, NULL); 25917d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 259280e0c8c3SCorentin Chary } 2593999342a0SCorentin Chary 25943e230dd2SCorentin Chary /* 25953e230dd2SCorentin Chary * Walk through the guest dirty map. 25963e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 25973e230dd2SCorentin Chary * Update server dirty map. 25983e230dd2SCorentin Chary */ 25999f64916dSGerd Hoffmann cmp_bytes = 64; 26009f64916dSGerd Hoffmann if (cmp_bytes > vnc_server_fb_stride(vd)) { 26019f64916dSGerd Hoffmann cmp_bytes = vnc_server_fb_stride(vd); 26029e4dd565SStefan Weil } 26039f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 26049f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 26059f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 26069f64916dSGerd Hoffmann } 26079f64916dSGerd Hoffmann guest_row = (uint8_t *)pixman_image_get_data(vd->guest.fb); 26089f64916dSGerd Hoffmann server_row = (uint8_t *)pixman_image_get_data(vd->server); 26099f64916dSGerd Hoffmann for (y = 0; y < height; y++) { 261023bfe28fSStefan Weil if (!bitmap_empty(vd->guest.dirty[y], VNC_DIRTY_BITS)) { 26113e230dd2SCorentin Chary int x; 26123e230dd2SCorentin Chary uint8_t *guest_ptr; 26133e230dd2SCorentin Chary uint8_t *server_ptr; 26143e230dd2SCorentin Chary 26159f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2616bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 26179f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 26189f64916dSGerd Hoffmann } else { 26193e230dd2SCorentin Chary guest_ptr = guest_row; 26209f64916dSGerd Hoffmann } 26213e230dd2SCorentin Chary server_ptr = server_row; 26223e230dd2SCorentin Chary 26239f64916dSGerd Hoffmann for (x = 0; x + 15 < width; 26243e230dd2SCorentin Chary x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2625bc2429b9SCorentin Chary if (!test_and_clear_bit((x / 16), vd->guest.dirty[y])) 26263e230dd2SCorentin Chary continue; 26273e230dd2SCorentin Chary if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) 26283e230dd2SCorentin Chary continue; 26293e230dd2SCorentin Chary memcpy(server_ptr, guest_ptr, cmp_bytes); 263080e0c8c3SCorentin Chary if (!vd->non_adaptive) 2631999342a0SCorentin Chary vnc_rect_updated(vd, x, y, &tv); 26323e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2633bc2429b9SCorentin Chary set_bit((x / 16), vs->dirty[y]); 26343e230dd2SCorentin Chary } 26353e230dd2SCorentin Chary has_dirty++; 26363e230dd2SCorentin Chary } 26373e230dd2SCorentin Chary } 26389f64916dSGerd Hoffmann guest_row += pixman_image_get_stride(vd->guest.fb); 26399f64916dSGerd Hoffmann server_row += pixman_image_get_stride(vd->server); 26403e230dd2SCorentin Chary } 26419f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 26423e230dd2SCorentin Chary return has_dirty; 26433e230dd2SCorentin Chary } 26443e230dd2SCorentin Chary 26450f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 26463e230dd2SCorentin Chary { 26470f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 26483e230dd2SCorentin Chary VncState *vs, *vn; 26493e230dd2SCorentin Chary int has_dirty, rects = 0; 26503e230dd2SCorentin Chary 26511dbfa005SGerd Hoffmann graphic_hw_update(NULL); 26523e230dd2SCorentin Chary 2653bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 26540f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2655bd023f95SCorentin Chary return; 2656bd023f95SCorentin Chary } 2657bd023f95SCorentin Chary 26583e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2659bd023f95SCorentin Chary vnc_unlock_display(vd); 26603e230dd2SCorentin Chary 26613e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 26623e230dd2SCorentin Chary rects += vnc_update_client(vs, has_dirty); 26633e230dd2SCorentin Chary /* vs might be free()ed here */ 26643e230dd2SCorentin Chary } 2665bd023f95SCorentin Chary 26660f7b2864SGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 26670f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 26683e230dd2SCorentin Chary return; 26690f7b2864SGerd Hoffmann } 26703e230dd2SCorentin Chary 26713e230dd2SCorentin Chary if (has_dirty && rects) { 26720f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 26730f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 26740f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 26750f7b2864SGerd Hoffmann } 26763e230dd2SCorentin Chary } else { 26770f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 26780f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 26790f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 26803e230dd2SCorentin Chary } 26813e230dd2SCorentin Chary } 26823e230dd2SCorentin Chary } 26833e230dd2SCorentin Chary 26847536ee4bSTim Hardeck static void vnc_connect(VncDisplay *vd, int csock, int skipauth, bool websocket) 26853e230dd2SCorentin Chary { 26867267c094SAnthony Liguori VncState *vs = g_malloc0(sizeof(VncState)); 26877d964c9dSCorentin Chary int i; 26887d964c9dSCorentin Chary 26893e230dd2SCorentin Chary vs->csock = csock; 26907e7e2ebcSDaniel P. Berrange 26917e7e2ebcSDaniel P. Berrange if (skipauth) { 26927e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 26937e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 26947e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 26957e7e2ebcSDaniel P. Berrange #endif 26967e7e2ebcSDaniel P. Berrange } else { 26977e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 26987e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 26997e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 27007e7e2ebcSDaniel P. Berrange #endif 27017e7e2ebcSDaniel P. Berrange } 27027e7e2ebcSDaniel P. Berrange 27037267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 27047d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 27057267c094SAnthony Liguori vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t)); 27067d964c9dSCorentin Chary } 27073e230dd2SCorentin Chary 27083e230dd2SCorentin Chary VNC_DEBUG("New client on socket %d\n", csock); 27090f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2710f9e8caccSStefan Hajnoczi qemu_set_nonblock(vs->csock); 27117536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27127536ee4bSTim Hardeck if (websocket) { 27137536ee4bSTim Hardeck vs->websocket = 1; 27147536ee4bSTim Hardeck qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, NULL, vs); 27157536ee4bSTim Hardeck } else 27167536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 27177536ee4bSTim Hardeck { 27183e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 27197536ee4bSTim Hardeck } 27203e230dd2SCorentin Chary 27213e230dd2SCorentin Chary vnc_client_cache_addr(vs); 27223e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_CONNECTED); 27238cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 27243e230dd2SCorentin Chary 27253e230dd2SCorentin Chary vs->vd = vd; 27267536ee4bSTim Hardeck 27277536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27287536ee4bSTim Hardeck if (!vs->websocket) 27297536ee4bSTim Hardeck #endif 27307536ee4bSTim Hardeck { 27317536ee4bSTim Hardeck vnc_init_state(vs); 27327536ee4bSTim Hardeck } 27337536ee4bSTim Hardeck } 27347536ee4bSTim Hardeck 27357536ee4bSTim Hardeck void vnc_init_state(VncState *vs) 27367536ee4bSTim Hardeck { 27376fd8e79aSTim Hardeck vs->initialized = true; 27387536ee4bSTim Hardeck VncDisplay *vd = vs->vd; 27397536ee4bSTim Hardeck 27403e230dd2SCorentin Chary vs->last_x = -1; 27413e230dd2SCorentin Chary vs->last_y = -1; 27423e230dd2SCorentin Chary 27433e230dd2SCorentin Chary vs->as.freq = 44100; 27443e230dd2SCorentin Chary vs->as.nchannels = 2; 27453e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 27463e230dd2SCorentin Chary vs->as.endianness = 0; 27473e230dd2SCorentin Chary 2748bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2749175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2750bd023f95SCorentin Chary 27513e230dd2SCorentin Chary QTAILQ_INSERT_HEAD(&vd->clients, vs, next); 27523e230dd2SCorentin Chary 27531dbfa005SGerd Hoffmann graphic_hw_update(NULL); 27543e230dd2SCorentin Chary 27553e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 27563e230dd2SCorentin Chary vnc_flush(vs); 27573e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 27583e230dd2SCorentin Chary reset_keys(vs); 27593e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 27603e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 27613e230dd2SCorentin Chary 27623e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 27633e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 27643e230dd2SCorentin Chary 27653e230dd2SCorentin Chary /* vs might be free()ed here */ 27663e230dd2SCorentin Chary } 27673e230dd2SCorentin Chary 27687536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket) 27693e230dd2SCorentin Chary { 27703e230dd2SCorentin Chary VncDisplay *vs = opaque; 27713e230dd2SCorentin Chary struct sockaddr_in addr; 27723e230dd2SCorentin Chary socklen_t addrlen = sizeof(addr); 27737536ee4bSTim Hardeck int csock; 27743e230dd2SCorentin Chary 27753e230dd2SCorentin Chary /* Catch-up */ 27761dbfa005SGerd Hoffmann graphic_hw_update(NULL); 27777536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27787536ee4bSTim Hardeck if (websocket) { 27797536ee4bSTim Hardeck csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen); 27807536ee4bSTim Hardeck } else 27817536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 27827536ee4bSTim Hardeck { 27837536ee4bSTim Hardeck csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); 27847536ee4bSTim Hardeck } 27853e230dd2SCorentin Chary 27863e230dd2SCorentin Chary if (csock != -1) { 27877536ee4bSTim Hardeck vnc_connect(vs, csock, 0, websocket); 27883e230dd2SCorentin Chary } 27893e230dd2SCorentin Chary } 27903e230dd2SCorentin Chary 27917536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque) 27927536ee4bSTim Hardeck { 27937536ee4bSTim Hardeck vnc_listen_read(opaque, 0); 27947536ee4bSTim Hardeck } 27957536ee4bSTim Hardeck 27967536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 27977536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque) 27987536ee4bSTim Hardeck { 27997536ee4bSTim Hardeck vnc_listen_read(opaque, 1); 28007536ee4bSTim Hardeck } 28017536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28027536ee4bSTim Hardeck 28037c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 28047c20b4a3SGerd Hoffmann .dpy_name = "vnc", 28050f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 28067c20b4a3SGerd Hoffmann .dpy_gfx_copy = vnc_dpy_copy, 28077c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 2808c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 28097c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 28107c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 28117c20b4a3SGerd Hoffmann }; 28127c20b4a3SGerd Hoffmann 28133e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds) 28143e230dd2SCorentin Chary { 28157267c094SAnthony Liguori VncDisplay *vs = g_malloc0(sizeof(*vs)); 28163e230dd2SCorentin Chary 28173e230dd2SCorentin Chary vnc_display = vs; 28183e230dd2SCorentin Chary 28193e230dd2SCorentin Chary vs->lsock = -1; 28207536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28217536ee4bSTim Hardeck vs->lwebsock = -1; 28227536ee4bSTim Hardeck #endif 28233e230dd2SCorentin Chary 28243e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 28253c9405a0SGerd Hoffmann vs->expires = TIME_MAX; 28263e230dd2SCorentin Chary 28273e230dd2SCorentin Chary if (keyboard_layout) 28283e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 28293e230dd2SCorentin Chary else 28303e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 28313e230dd2SCorentin Chary 28323e230dd2SCorentin Chary if (!vs->kbd_layout) 28333e230dd2SCorentin Chary exit(1); 28343e230dd2SCorentin Chary 2835bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 2836bd023f95SCorentin Chary vnc_start_worker_thread(); 2837bd023f95SCorentin Chary 283821ef45d7SGerd Hoffmann vs->dcl.ops = &dcl_ops; 28395209089fSGerd Hoffmann register_displaychangelistener(&vs->dcl); 28403e230dd2SCorentin Chary } 28413e230dd2SCorentin Chary 28423e230dd2SCorentin Chary 284371a8cdecSBlue Swirl static void vnc_display_close(DisplayState *ds) 28443e230dd2SCorentin Chary { 284521ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 28463e230dd2SCorentin Chary 28473e230dd2SCorentin Chary if (!vs) 28483e230dd2SCorentin Chary return; 28493e230dd2SCorentin Chary if (vs->display) { 28507267c094SAnthony Liguori g_free(vs->display); 28513e230dd2SCorentin Chary vs->display = NULL; 28523e230dd2SCorentin Chary } 28533e230dd2SCorentin Chary if (vs->lsock != -1) { 28543e230dd2SCorentin Chary qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL); 28553e230dd2SCorentin Chary close(vs->lsock); 28563e230dd2SCorentin Chary vs->lsock = -1; 28573e230dd2SCorentin Chary } 28587536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28597536ee4bSTim Hardeck g_free(vs->ws_display); 28607536ee4bSTim Hardeck vs->ws_display = NULL; 28617536ee4bSTim Hardeck if (vs->lwebsock != -1) { 28627536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL); 28637536ee4bSTim Hardeck close(vs->lwebsock); 28647536ee4bSTim Hardeck vs->lwebsock = -1; 28657536ee4bSTim Hardeck } 28667536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28673e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 28683e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 28693e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 28703e230dd2SCorentin Chary vs->tls.x509verify = 0; 28713e230dd2SCorentin Chary #endif 28723e230dd2SCorentin Chary } 28733e230dd2SCorentin Chary 287471a8cdecSBlue Swirl static int vnc_display_disable_login(DisplayState *ds) 28753e230dd2SCorentin Chary { 287621ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 28773e230dd2SCorentin Chary 28783e230dd2SCorentin Chary if (!vs) { 28793e230dd2SCorentin Chary return -1; 28803e230dd2SCorentin Chary } 28813e230dd2SCorentin Chary 28823e230dd2SCorentin Chary if (vs->password) { 28837267c094SAnthony Liguori g_free(vs->password); 28841cd20f8bSAnthony Liguori } 28851cd20f8bSAnthony Liguori 28861cd20f8bSAnthony Liguori vs->password = NULL; 28877dfbfc79SDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 28881cd20f8bSAnthony Liguori vs->auth = VNC_AUTH_VNC; 28897dfbfc79SDaniel P. Berrange } 28901cd20f8bSAnthony Liguori 28911cd20f8bSAnthony Liguori return 0; 28921cd20f8bSAnthony Liguori } 28931cd20f8bSAnthony Liguori 28941cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password) 28951cd20f8bSAnthony Liguori { 289621ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 28971cd20f8bSAnthony Liguori 28981cd20f8bSAnthony Liguori if (!vs) { 2899a6aa9d3eSLuiz Capitulino return -EINVAL; 29001cd20f8bSAnthony Liguori } 29011cd20f8bSAnthony Liguori 29021cd20f8bSAnthony Liguori if (!password) { 29031cd20f8bSAnthony Liguori /* This is not the intention of this interface but err on the side 29041cd20f8bSAnthony Liguori of being safe */ 2905a6aa9d3eSLuiz Capitulino return vnc_display_disable_login(ds); 29061cd20f8bSAnthony Liguori } 29071cd20f8bSAnthony Liguori 29081cd20f8bSAnthony Liguori if (vs->password) { 29097267c094SAnthony Liguori g_free(vs->password); 29103e230dd2SCorentin Chary vs->password = NULL; 29113e230dd2SCorentin Chary } 29127267c094SAnthony Liguori vs->password = g_strdup(password); 29137dfbfc79SDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 29143e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 29157dfbfc79SDaniel P. Berrange } 2916a6aa9d3eSLuiz Capitulino 2917a6aa9d3eSLuiz Capitulino return 0; 29183e230dd2SCorentin Chary } 29193e230dd2SCorentin Chary 29203c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires) 29213c9405a0SGerd Hoffmann { 292221ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29233c9405a0SGerd Hoffmann 29241643f2b2SGerd Hoffmann if (!vs) { 29251643f2b2SGerd Hoffmann return -EINVAL; 29261643f2b2SGerd Hoffmann } 29271643f2b2SGerd Hoffmann 29283c9405a0SGerd Hoffmann vs->expires = expires; 29293c9405a0SGerd Hoffmann return 0; 29303c9405a0SGerd Hoffmann } 29313c9405a0SGerd Hoffmann 29323e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds) 29333e230dd2SCorentin Chary { 293421ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29353e230dd2SCorentin Chary 29363e230dd2SCorentin Chary return vnc_socket_local_addr("%s:%s", vs->lsock); 29373e230dd2SCorentin Chary } 29383e230dd2SCorentin Chary 29392d55f0e8SPaolo Bonzini void vnc_display_open(DisplayState *ds, const char *display, Error **errp) 29403e230dd2SCorentin Chary { 294121ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29423e230dd2SCorentin Chary const char *options; 29433e230dd2SCorentin Chary int password = 0; 29443e230dd2SCorentin Chary int reverse = 0; 29453e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 29463e230dd2SCorentin Chary int tls = 0, x509 = 0; 29473e230dd2SCorentin Chary #endif 29483e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 29493e230dd2SCorentin Chary int sasl = 0; 29503e230dd2SCorentin Chary int saslErr; 29513e230dd2SCorentin Chary #endif 29522ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 29533e230dd2SCorentin Chary int acl = 0; 29542ded6ad7SBlue Swirl #endif 29553e230dd2SCorentin Chary int lock_key_sync = 1; 29563e230dd2SCorentin Chary 29572d55f0e8SPaolo Bonzini if (!vnc_display) { 29582d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 29592d55f0e8SPaolo Bonzini return; 29602d55f0e8SPaolo Bonzini } 29613e230dd2SCorentin Chary vnc_display_close(ds); 29623e230dd2SCorentin Chary if (strcmp(display, "none") == 0) 29632d55f0e8SPaolo Bonzini return; 29643e230dd2SCorentin Chary 29651ce52c78SPaolo Bonzini vs->display = g_strdup(display); 29668cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 29673e230dd2SCorentin Chary 29683e230dd2SCorentin Chary options = display; 29693e230dd2SCorentin Chary while ((options = strchr(options, ','))) { 29703e230dd2SCorentin Chary options++; 29713e230dd2SCorentin Chary if (strncmp(options, "password", 8) == 0) { 29720f66998fSPaul Moore if (fips_get_state()) { 29732d55f0e8SPaolo Bonzini error_setg(errp, 29740f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 29750f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 29762d55f0e8SPaolo Bonzini "methods as an alternative"); 29771ce52c78SPaolo Bonzini goto fail; 29780f66998fSPaul Moore } 29793e230dd2SCorentin Chary password = 1; /* Require password auth */ 29803e230dd2SCorentin Chary } else if (strncmp(options, "reverse", 7) == 0) { 29813e230dd2SCorentin Chary reverse = 1; 2982cee8e6adSStefan Hajnoczi } else if (strncmp(options, "no-lock-key-sync", 16) == 0) { 29833e230dd2SCorentin Chary lock_key_sync = 0; 29843e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 29853e230dd2SCorentin Chary } else if (strncmp(options, "sasl", 4) == 0) { 29863e230dd2SCorentin Chary sasl = 1; /* Require SASL auth */ 29873e230dd2SCorentin Chary #endif 29887536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 29897536ee4bSTim Hardeck } else if (strncmp(options, "websocket", 9) == 0) { 29907536ee4bSTim Hardeck char *start, *end; 29917536ee4bSTim Hardeck vs->websocket = 1; 29927536ee4bSTim Hardeck 29937536ee4bSTim Hardeck /* Check for 'websocket=<port>' */ 29947536ee4bSTim Hardeck start = strchr(options, '='); 29957536ee4bSTim Hardeck end = strchr(options, ','); 29967536ee4bSTim Hardeck if (start && (!end || (start < end))) { 29977536ee4bSTim Hardeck int len = end ? end-(start+1) : strlen(start+1); 29987536ee4bSTim Hardeck if (len < 6) { 29997536ee4bSTim Hardeck /* extract the host specification from display */ 30007536ee4bSTim Hardeck char *host = NULL, *port = NULL, *host_end = NULL; 30017536ee4bSTim Hardeck port = g_strndup(start + 1, len); 30027536ee4bSTim Hardeck 30037536ee4bSTim Hardeck /* ipv6 hosts have colons */ 30047536ee4bSTim Hardeck end = strchr(display, ','); 30057536ee4bSTim Hardeck host_end = g_strrstr_len(display, end - display, ":"); 30067536ee4bSTim Hardeck 30077536ee4bSTim Hardeck if (host_end) { 30087536ee4bSTim Hardeck host = g_strndup(display, host_end - display + 1); 30097536ee4bSTim Hardeck } else { 30107536ee4bSTim Hardeck host = g_strndup(":", 1); 30117536ee4bSTim Hardeck } 30127536ee4bSTim Hardeck vs->ws_display = g_strconcat(host, port, NULL); 30137536ee4bSTim Hardeck g_free(host); 30147536ee4bSTim Hardeck g_free(port); 30157536ee4bSTim Hardeck } 30167536ee4bSTim Hardeck } 30177536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 30183e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 30193e230dd2SCorentin Chary } else if (strncmp(options, "tls", 3) == 0) { 30203e230dd2SCorentin Chary tls = 1; /* Require TLS */ 30213e230dd2SCorentin Chary } else if (strncmp(options, "x509", 4) == 0) { 30223e230dd2SCorentin Chary char *start, *end; 30233e230dd2SCorentin Chary x509 = 1; /* Require x509 certificates */ 30243e230dd2SCorentin Chary if (strncmp(options, "x509verify", 10) == 0) 30253e230dd2SCorentin Chary vs->tls.x509verify = 1; /* ...and verify client certs */ 30263e230dd2SCorentin Chary 30273e230dd2SCorentin Chary /* Now check for 'x509=/some/path' postfix 30283e230dd2SCorentin Chary * and use that to setup x509 certificate/key paths */ 30293e230dd2SCorentin Chary start = strchr(options, '='); 30303e230dd2SCorentin Chary end = strchr(options, ','); 30313e230dd2SCorentin Chary if (start && (!end || (start < end))) { 30323e230dd2SCorentin Chary int len = end ? end-(start+1) : strlen(start+1); 30337267c094SAnthony Liguori char *path = g_strndup(start + 1, len); 30343e230dd2SCorentin Chary 30353e230dd2SCorentin Chary VNC_DEBUG("Trying certificate path '%s'\n", path); 30363e230dd2SCorentin Chary if (vnc_tls_set_x509_creds_dir(vs, path) < 0) { 30372d55f0e8SPaolo Bonzini error_setg(errp, "Failed to find x509 certificates/keys in %s", path); 30387267c094SAnthony Liguori g_free(path); 30391ce52c78SPaolo Bonzini goto fail; 30403e230dd2SCorentin Chary } 30417267c094SAnthony Liguori g_free(path); 30423e230dd2SCorentin Chary } else { 30432d55f0e8SPaolo Bonzini error_setg(errp, "No certificate path provided"); 30441ce52c78SPaolo Bonzini goto fail; 30453e230dd2SCorentin Chary } 30463e230dd2SCorentin Chary #endif 30472ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 30483e230dd2SCorentin Chary } else if (strncmp(options, "acl", 3) == 0) { 30493e230dd2SCorentin Chary acl = 1; 30502ded6ad7SBlue Swirl #endif 30513e230dd2SCorentin Chary } else if (strncmp(options, "lossy", 5) == 0) { 30523e230dd2SCorentin Chary vs->lossy = true; 30537eff5742SCatalin Patulea } else if (strncmp(options, "non-adaptive", 12) == 0) { 305480e0c8c3SCorentin Chary vs->non_adaptive = true; 30558cf36489SGerd Hoffmann } else if (strncmp(options, "share=", 6) == 0) { 30568cf36489SGerd Hoffmann if (strncmp(options+6, "ignore", 6) == 0) { 30578cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_IGNORE; 30588cf36489SGerd Hoffmann } else if (strncmp(options+6, "allow-exclusive", 15) == 0) { 30598cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 30608cf36489SGerd Hoffmann } else if (strncmp(options+6, "force-shared", 12) == 0) { 30618cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 30628cf36489SGerd Hoffmann } else { 30632d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 30641ce52c78SPaolo Bonzini goto fail; 30658cf36489SGerd Hoffmann } 30663e230dd2SCorentin Chary } 30673e230dd2SCorentin Chary } 30683e230dd2SCorentin Chary 30693e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 30703e230dd2SCorentin Chary if (acl && x509 && vs->tls.x509verify) { 30713e230dd2SCorentin Chary if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) { 30723e230dd2SCorentin Chary fprintf(stderr, "Failed to create x509 dname ACL\n"); 30733e230dd2SCorentin Chary exit(1); 30743e230dd2SCorentin Chary } 30753e230dd2SCorentin Chary } 30763e230dd2SCorentin Chary #endif 30773e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 30783e230dd2SCorentin Chary if (acl && sasl) { 30793e230dd2SCorentin Chary if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) { 30803e230dd2SCorentin Chary fprintf(stderr, "Failed to create username ACL\n"); 30813e230dd2SCorentin Chary exit(1); 30823e230dd2SCorentin Chary } 30833e230dd2SCorentin Chary } 30843e230dd2SCorentin Chary #endif 30853e230dd2SCorentin Chary 30863e230dd2SCorentin Chary /* 30873e230dd2SCorentin Chary * Combinations we support here: 30883e230dd2SCorentin Chary * 30893e230dd2SCorentin Chary * - no-auth (clear text, no auth) 30903e230dd2SCorentin Chary * - password (clear text, weak auth) 30913e230dd2SCorentin Chary * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI) 30923e230dd2SCorentin Chary * - tls (encrypt, weak anonymous creds, no auth) 30933e230dd2SCorentin Chary * - tls + password (encrypt, weak anonymous creds, weak auth) 30943e230dd2SCorentin Chary * - tls + sasl (encrypt, weak anonymous creds, good auth) 30953e230dd2SCorentin Chary * - tls + x509 (encrypt, good x509 creds, no auth) 30963e230dd2SCorentin Chary * - tls + x509 + password (encrypt, good x509 creds, weak auth) 30973e230dd2SCorentin Chary * - tls + x509 + sasl (encrypt, good x509 creds, good auth) 30983e230dd2SCorentin Chary * 30993e230dd2SCorentin Chary * NB1. TLS is a stackable auth scheme. 31003e230dd2SCorentin Chary * NB2. the x509 schemes have option to validate a client cert dname 31013e230dd2SCorentin Chary */ 31023e230dd2SCorentin Chary if (password) { 31033e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31043e230dd2SCorentin Chary if (tls) { 31053e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31063e230dd2SCorentin Chary if (x509) { 31073e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 31083e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 31093e230dd2SCorentin Chary } else { 31103e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 31113e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 31123e230dd2SCorentin Chary } 31133e230dd2SCorentin Chary } else { 31143e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31153e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with password auth\n"); 31163e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 31173e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31183e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31193e230dd2SCorentin Chary } 31203e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31213e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31223e230dd2SCorentin Chary } else if (sasl) { 31233e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31243e230dd2SCorentin Chary if (tls) { 31253e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31263e230dd2SCorentin Chary if (x509) { 31273e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 31283e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 31293e230dd2SCorentin Chary } else { 31303e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 31313e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 31323e230dd2SCorentin Chary } 31333e230dd2SCorentin Chary } else { 31343e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31353e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with SASL auth\n"); 31363e230dd2SCorentin Chary vs->auth = VNC_AUTH_SASL; 31373e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31383e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31393e230dd2SCorentin Chary } 31403e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 31413e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 31423e230dd2SCorentin Chary } else { 31433e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31443e230dd2SCorentin Chary if (tls) { 31453e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 31463e230dd2SCorentin Chary if (x509) { 31473e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 31483e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 31493e230dd2SCorentin Chary } else { 31503e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 31513e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 31523e230dd2SCorentin Chary } 31533e230dd2SCorentin Chary } else { 31543e230dd2SCorentin Chary #endif 31553e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with no auth\n"); 31563e230dd2SCorentin Chary vs->auth = VNC_AUTH_NONE; 31573e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31583e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 31593e230dd2SCorentin Chary } 31603e230dd2SCorentin Chary #endif 31613e230dd2SCorentin Chary } 31623e230dd2SCorentin Chary 31633e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31643e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 31652d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 31663e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 31671ce52c78SPaolo Bonzini goto fail; 31683e230dd2SCorentin Chary } 31693e230dd2SCorentin Chary #endif 31703e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 31713e230dd2SCorentin Chary 31723e230dd2SCorentin Chary if (reverse) { 31733e230dd2SCorentin Chary /* connect to viewer */ 3174007fcd3eSPaolo Bonzini int csock; 31753e230dd2SCorentin Chary vs->lsock = -1; 31767536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 31777536ee4bSTim Hardeck vs->lwebsock = -1; 31787536ee4bSTim Hardeck #endif 3179007fcd3eSPaolo Bonzini if (strncmp(display, "unix:", 5) == 0) { 31802d55f0e8SPaolo Bonzini csock = unix_connect(display+5, errp); 3181007fcd3eSPaolo Bonzini } else { 31822d55f0e8SPaolo Bonzini csock = inet_connect(display, errp); 31833e230dd2SCorentin Chary } 3184007fcd3eSPaolo Bonzini if (csock < 0) { 3185007fcd3eSPaolo Bonzini goto fail; 3186007fcd3eSPaolo Bonzini } 31877536ee4bSTim Hardeck vnc_connect(vs, csock, 0, 0); 31883e230dd2SCorentin Chary } else { 31893e230dd2SCorentin Chary /* listen for connects */ 31903e230dd2SCorentin Chary char *dpy; 31917267c094SAnthony Liguori dpy = g_malloc(256); 31923e230dd2SCorentin Chary if (strncmp(display, "unix:", 5) == 0) { 31933e230dd2SCorentin Chary pstrcpy(dpy, 256, "unix:"); 31942d55f0e8SPaolo Bonzini vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp); 31953e230dd2SCorentin Chary } else { 3196029409e5SAmos Kong vs->lsock = inet_listen(display, dpy, 256, 31972d55f0e8SPaolo Bonzini SOCK_STREAM, 5900, errp); 3198c1c1619cSPaolo Bonzini if (vs->lsock < 0) { 3199ae878b17SStefan Weil g_free(dpy); 32001ce52c78SPaolo Bonzini goto fail; 32011ce52c78SPaolo Bonzini } 32027536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32037536ee4bSTim Hardeck if (vs->websocket) { 32047536ee4bSTim Hardeck if (vs->ws_display) { 32057536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->ws_display, NULL, 256, 32067536ee4bSTim Hardeck SOCK_STREAM, 0, errp); 32077536ee4bSTim Hardeck } else { 32087536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->display, NULL, 256, 32097536ee4bSTim Hardeck SOCK_STREAM, 5700, errp); 32107536ee4bSTim Hardeck } 32117536ee4bSTim Hardeck 32127536ee4bSTim Hardeck if (vs->lwebsock < 0) { 32137536ee4bSTim Hardeck if (vs->lsock) { 32147536ee4bSTim Hardeck close(vs->lsock); 32157536ee4bSTim Hardeck vs->lsock = -1; 32167536ee4bSTim Hardeck } 32177536ee4bSTim Hardeck g_free(dpy); 32187536ee4bSTim Hardeck goto fail; 32197536ee4bSTim Hardeck } 32207536ee4bSTim Hardeck } 32217536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32227536ee4bSTim Hardeck } 3223ae878b17SStefan Weil g_free(vs->display); 32243e230dd2SCorentin Chary vs->display = dpy; 32257536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lsock, NULL, 32267536ee4bSTim Hardeck vnc_listen_regular_read, NULL, vs); 32277536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32287536ee4bSTim Hardeck if (vs->websocket) { 32297536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, 32307536ee4bSTim Hardeck vnc_listen_websocket_read, NULL, vs); 32317536ee4bSTim Hardeck } 32327536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32333e230dd2SCorentin Chary } 32342d55f0e8SPaolo Bonzini return; 32351ce52c78SPaolo Bonzini 32361ce52c78SPaolo Bonzini fail: 32371ce52c78SPaolo Bonzini g_free(vs->display); 32381ce52c78SPaolo Bonzini vs->display = NULL; 32397536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32407536ee4bSTim Hardeck g_free(vs->ws_display); 32417536ee4bSTim Hardeck vs->ws_display = NULL; 32427536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 32433e230dd2SCorentin Chary } 324413661089SDaniel P. Berrange 324513661089SDaniel P. Berrange void vnc_display_add_client(DisplayState *ds, int csock, int skipauth) 324613661089SDaniel P. Berrange { 324721ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 324813661089SDaniel P. Berrange 32497536ee4bSTim Hardeck vnc_connect(vs, csock, skipauth, 0); 325013661089SDaniel P. Berrange } 3251